Files

104 lines
5.8 KiB
C#
Raw Permalink Normal View History

using System.Text.Json;
namespace UCVL.VPNPlus;
internal static class PublicTailnetSelectionSelfTest
{
internal static async Task<int> RunAsync()
{
var checks = new Dictionary<string, bool>();
NodeInfo[] profile = [new() { Slug = "primary" }, new() { Slug = "backup" }, new PrivateAuxiliaryNode { Slug = "private" }];
var auto = ConnectionSelection.Create(false, null, profile.Select(n => n.Slug));
var auxiliary = new PrivateAuxiliaryNode { Slug = "private", ProbeIpv4 = "100.84.199.104" };
var failed = new Dictionary<string, DateTimeOffset>();
var requests = new List<string>();
var privateSearches = 0;
Task<IReadOnlyList<PrivateAuxiliaryNode>> RankPrivate(CancellationToken _)
{
privateSearches++;
return Task.FromResult<IReadOnlyList<PrivateAuxiliaryNode>>([auxiliary]);
}
ConfigResponse Response(string slug, string route = "smart", string scope = "public") =>
new() { Node = new() { Slug = slug }, RouteMode = route, ConnectionScope = scope };
await foreach (var attempt in TransportPlan.AttemptsAsync(auto, "smart", 8, null, RankPrivate, default))
{
requests.Add(VpnApiClient.ConfigPath(attempt.Slug, "smart", failed.Keys));
auto.ValidateConfig(attempt, Response("primary"), "smart", profile, failed);
break;
}
checks["healthy_primary_consumes_one_sticky_dispatch"] = privateSearches == 0 && requests.SequenceEqual(
["api/v1/client/config/?node=auto&route=smart"]);
requests.Clear();
var attempted = new List<string>();
await foreach (var attempt in TransportPlan.AttemptsAsync(auto, "smart", 8, null, RankPrivate, default))
{
if (attempt.IsPrivate) { attempted.Add(attempt.Slug); continue; }
requests.Add(VpnApiClient.ConfigPath(attempt.Slug, "smart", failed.Keys));
var node = failed.ContainsKey("primary") ? "backup" : "primary";
auto.ValidateConfig(attempt, Response(node), "smart", profile, failed);
attempted.Add(node);
if (node == "backup") break;
failed[node] = DateTimeOffset.UtcNow.AddMinutes(5);
}
checks["primary_failure_defers_private_search_and_backup_dispatch"] = privateSearches == 1
&& attempted.SequenceEqual(["primary", "private", "backup"])
&& requests.SequenceEqual(["api/v1/client/config/?node=auto&route=smart", "api/v1/client/config/?node=auto&route=smart&exclude=primary"]);
attempted.Clear();
await foreach (var attempt in TransportPlan.AttemptsAsync(auto, "global", 99, "primary", RankPrivate, default))
attempted.Add(attempt.Slug);
checks["same_node_retry_precedes_discovery_and_two_public_backups"] =
attempted.SequenceEqual(["primary", "private", "auto", "auto"]);
checks["same_node_config_does_not_request_rebalance_or_exclusions"] =
VpnApiClient.ConfigPath("primary", "global", failed.Keys) == "api/v1/client/config/?node=primary&route=global";
var manual = ConnectionSelection.Create(true, "private", profile.Select(n => n.Slug));
attempted.Clear();
await foreach (var attempt in TransportPlan.AttemptsAsync(manual, "smart", 3, "primary", RankPrivate, default))
attempted.Add(attempt.Slug);
checks["manual_stays_at_selected_site"] = attempted.SequenceEqual(["private"]);
var before = privateSearches;
attempted.Clear();
await foreach (var attempt in TransportPlan.AttemptsAsync(auto, "direct", 3, null, RankPrivate, default))
attempted.Add(attempt.Slug);
checks["direct_has_no_endpoint_discovery"] = privateSearches == before && attempted.SequenceEqual(["auto", "auto", "auto"]);
bool Rejected(ConfigResponse response, string requested = "auto")
{
try { auto.ValidateConfig(new(requested, null), response, "smart", profile, failed); return false; }
catch (InvalidOperationException) { return true; }
}
checks["auto_cannot_repeat_excluded_node"] = Rejected(Response("primary"));
checks["auto_cannot_probe_node_outside_authorized_profile"] = Rejected(Response("unexpected"));
checks["public_config_cannot_use_private_scope_or_private_slug"] =
Rejected(Response("backup", scope: PrivateAuxiliaryRouting.Scope)) && Rejected(Response("private"));
checks["mismatched_route_and_explicit_node_rejected"] =
Rejected(Response("backup", "global")) && Rejected(Response("backup"), "primary");
using var cancelled = new CancellationTokenSource();
var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var never = new TaskCompletionSource<IReadOnlyList<PrivateAuxiliaryNode>>(TaskCreationOptions.RunContinuationsAsynchronously);
async Task ConsumeAsync()
{
await foreach (var _ in TransportPlan.AttemptsAsync(auto, "smart", 0, null, _ =>
{ entered.SetResult(); return never.Task; }, cancelled.Token)) { }
}
var pending = ConsumeAsync();
await entered.Task.WaitAsync(TimeSpan.FromSeconds(1));
cancelled.Cancel();
try
{
await pending.WaitAsync(TimeSpan.FromSeconds(1));
checks["cancellation_interrupts_discovery"] = false;
}
catch (OperationCanceledException) { checks["cancellation_interrupts_discovery"] = true; }
var report = new { passed = checks.Values.All(v => v), checks, simulated_transport = true, network_modified = false };
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "public-tailnet-selection-self-test.json"),
JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true }));
return report.passed ? 0 : 83;
}
}