84 lines
6.2 KiB
C#
84 lines
6.2 KiB
C#
|
|
using System.Text.Json;
|
||
|
|
|
||
|
|
namespace UCVL.VPNPlus;
|
||
|
|
|
||
|
|
internal static class NodeTrafficSelfTest
|
||
|
|
{
|
||
|
|
internal static int Run()
|
||
|
|
{
|
||
|
|
var checks = new Dictionary<string, bool>();
|
||
|
|
string? error = null;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var now = new DateTimeOffset(2026, 10, 22, 8, 0, 0, TimeSpan.Zero);
|
||
|
|
NodeTrafficReading Row(string slug = "aliyun-tokyo-002", bool stale = false, DateTimeOffset? sampled = null,
|
||
|
|
long? up = 10, long? down = 20, long? totalUp = 30, long? totalDown = 40, double? interval = 5, string? epoch = "fixture") => new()
|
||
|
|
{ Slug = slug, Stale = stale, SampledAt = sampled ?? now, UploadRate = up, DownloadRate = down,
|
||
|
|
TotalUpload = totalUp, TotalDownload = totalDown, IntervalSeconds = interval, CounterEpoch = epoch };
|
||
|
|
NodeTrafficSnapshot Snapshot(List<NodeTrafficReading>? rows = null, string scope = "server_host_interface",
|
||
|
|
string source = "linux_host_interface", DateTimeOffset? generated = null, string direction = "interface_perspective") => new()
|
||
|
|
{ Contract = 1, Scope = scope, Source = source, Direction = direction, CounterSince = "boot_or_interface_reset",
|
||
|
|
GeneratedAt = generated ?? now, Nodes = rows ?? [Row(), Row(NodeTrafficPresentation.Slugs[1])] };
|
||
|
|
bool Valid(NodeTrafficSnapshot value) => NodeTrafficPresentation.Valid(value, now);
|
||
|
|
checks["exact_two_authorized_server_contract"] = Valid(Snapshot());
|
||
|
|
checks["empty_authorized_set_is_not_fake_servers"] = Valid(Snapshot([]));
|
||
|
|
checks["other_nodes_rejected"] = !Valid(Snapshot([Row("phone")]));
|
||
|
|
checks["duplicate_node_rejected"] = !Valid(Snapshot([Row(), Row()]));
|
||
|
|
checks["too_many_rows_rejected"] = !Valid(Snapshot([Row(), Row(), Row()]));
|
||
|
|
checks["proxy_accounting_cannot_be_mislabelled_host"] = !Valid(Snapshot(scope: "server_proxy_aggregate"));
|
||
|
|
checks["unknown_source_rejected"] = !Valid(Snapshot(source: "unknown"));
|
||
|
|
checks["wrong_direction_rejected"] = !Valid(Snapshot(direction: "client_perspective"));
|
||
|
|
checks["stale_envelope_rejected"] = !Valid(Snapshot(generated: now.AddSeconds(-16)));
|
||
|
|
checks["future_envelope_rejected"] = !Valid(Snapshot(generated: now.AddSeconds(6)));
|
||
|
|
checks["negative_upload_rejected"] = !Valid(Snapshot([Row(up: -1)]));
|
||
|
|
checks["negative_download_rejected"] = !Valid(Snapshot([Row(down: -1)]));
|
||
|
|
checks["negative_total_rejected"] = !Valid(Snapshot([Row(totalUp: -1)]));
|
||
|
|
checks["nonfinite_interval_rejected"] = !Valid(Snapshot([Row(interval: double.NaN)]));
|
||
|
|
checks["unbounded_interval_rejected"] = !Valid(Snapshot([Row(interval: 31)]));
|
||
|
|
checks["fresh_sample_is_current"] = NodeTrafficPresentation.Fresh(Row(), now);
|
||
|
|
checks["backend_stale_is_not_live"] = !NodeTrafficPresentation.Fresh(Row(stale: true), now);
|
||
|
|
checks["client_checks_sample_age_independently"] = !NodeTrafficPresentation.Fresh(Row(sampled: now.AddSeconds(-16)), now);
|
||
|
|
checks["future_sample_is_not_live"] = !NodeTrafficPresentation.Fresh(Row(sampled: now.AddSeconds(6)), now);
|
||
|
|
checks["first_counter_has_no_invented_rate"] = !NodeTrafficPresentation.Fresh(Row(up: null, down: null, interval: null), now);
|
||
|
|
checks["reset_epoch_required"] = !NodeTrafficPresentation.Fresh(Row(epoch: null), now);
|
||
|
|
checks["missing_is_dash_not_zero"] = NodeTrafficPresentation.Bytes(null, true) == "—";
|
||
|
|
checks["measured_zero_retained"] = NodeTrafficPresentation.Bytes(0, true) == "0 B/s";
|
||
|
|
checks["rate_uses_bytes_not_bits"] = NodeTrafficPresentation.Bytes(204800, true) == "200 KB/s";
|
||
|
|
checks["cumulative_has_no_speed_suffix"] = NodeTrafficPresentation.Bytes(204800) == "200 KB";
|
||
|
|
checks["all_geometry_monitor_tailnet_cases_fit"] = DashboardLayout.SelfTest();
|
||
|
|
using (var font = Brand.Font(7.1f))
|
||
|
|
{
|
||
|
|
var measuredHeight = TextRenderer.MeasureText("VPN002 上传", font).Height;
|
||
|
|
foreach (var dpi in new[] { 96, 120, 144, 192, 336, 384 })
|
||
|
|
{
|
||
|
|
var view = new Size(620 * dpi / 96, 350 * dpi / 96);
|
||
|
|
var panel = DashboardLayout.Calculate(view, dpi, true, true).ServerTrafficCard;
|
||
|
|
var text = NodeTrafficPanel.TextLayout(panel.Size, dpi, (int)Math.Ceiling(measuredHeight * dpi / 96d));
|
||
|
|
checks["minimum_two_text_rows_fit_" + dpi] = text.Rows.Length == 2
|
||
|
|
&& text.Rows.All(row => row.Height >= measuredHeight * dpi / 96d && row.Bottom <= panel.Height)
|
||
|
|
&& !text.Rows[0].IntersectsWith(text.Rows[1]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
foreach (var tailnet in new[] { false, true })
|
||
|
|
{
|
||
|
|
var view = new Size(960, 612);
|
||
|
|
var layout = DashboardLayout.Calculate(view, 96, tailnet, true);
|
||
|
|
checks["server_rows_below_real_speed_" + tailnet] = layout.ServerTrafficCard.Top > layout.DailyCard.Bottom
|
||
|
|
&& layout.ServerTrafficCard.Top > layout.TrafficCard.Bottom
|
||
|
|
&& (!tailnet || layout.ServerTrafficCard.Top > layout.TailnetTrafficCard.Bottom);
|
||
|
|
checks["no_extra_window_height_or_scroll_" + tailnet] = layout.Footer.Bottom <= view.Height
|
||
|
|
&& layout.ContentHeight <= view.Height && layout.ServerTrafficCard.Bottom < layout.Footer.Top;
|
||
|
|
}
|
||
|
|
var oldProfile = JsonSerializer.Deserialize<Profile>("{\"email\":\"fixture@example.invalid\"}")!;
|
||
|
|
checks["older_backend_omission_does_not_enable_monitor"] = !oldProfile.CanViewNodeTraffic;
|
||
|
|
}
|
||
|
|
catch (Exception exception) { error = exception.GetType().Name; }
|
||
|
|
var passed = error is null && checks.Count >= 30 && checks.Values.All(value => value);
|
||
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "node-traffic-self-test.json"),
|
||
|
|
JsonSerializer.Serialize(new { passed, checks, error, scope = "offline contract and fixed-height geometry only",
|
||
|
|
network_modified = false, credentials_loaded = false, production_sample_observed = false },
|
||
|
|
new JsonSerializerOptions { WriteIndented = true }));
|
||
|
|
return passed ? 0 : 84;
|
||
|
|
}
|
||
|
|
}
|