namespace UCVL.VPNPlus; // Real plan/iterator with fake dispatch and connection outcomes only. No core, // account token, device, socket, or operating-system network state is touched. internal static class TransportPlanSelfTest { internal static async Task> CheckAsync() { var checks = new Dictionary(); var auxiliary = new PrivateAuxiliaryNode { Slug = "private-inner", ProbeIpv4 = "100.64.0.4" }; var phoneDomain = new PrivateAuxiliaryNode { Slug = "private-phone", ProbeIpv4 = PrivateAuxiliaryRouting.PublicPhoneAddress }; var auto = ConnectionSelection.Create(false, null, ["node-a", "node-b"]); var a = new PlannedTransport(new("node-a", null), new ConfigResponse { Node = new NodeInfo { Slug = "node-a" } }, true, true); var b = new PlannedTransport(new("node-b", null), new ConfigResponse { Node = new NodeInfo { Slug = "node-b" } }, true, true); var calls = 0; Task> Dispatch(CancellationToken _) { calls++; return Task.FromResult>([a, b]); } var plan = TransportPlan.Create(auto, [auxiliary, phoneDomain], "smart", 2); checks["transport_plan_creation_does_not_reserve_public_node"] = calls == 0 && plan.Any(p => p.DispatchPass); await foreach (var candidate in TransportPlan.ExpandAsync(plan, Dispatch, CancellationToken.None)) { checks["private_success_keeps_public_dispatch_unexecuted"] = candidate.Attempt.IsPrivate && candidate.Attempt.Slug == auxiliary.Slug && calls == 0; break; // A successful private connection stops the production loop. } checks["private_success_has_zero_public_reservations"] = calls == 0; var names = new List(); var dispatchStartedAfterPrivate = false; Task> AfterPrivate(CancellationToken token) { dispatchStartedAfterPrivate = names.SequenceEqual(new[] { auxiliary.Slug }); return Dispatch(token); } await foreach (var candidate in TransportPlan.ExpandAsync(plan, AfterPrivate, CancellationToken.None)) names.Add(candidate.Attempt.Slug); // Private attempt failed: request next. checks["public_dispatch_starts_only_after_private_failure"] = calls == 1 && dispatchStartedAfterPrivate; checks["all_inner_candidates_precede_phone_domain_and_domain_fallback"] = names.SequenceEqual(new[] { auxiliary.Slug, "node-a", "node-b", phoneDomain.Slug, "auto", "auto" }); checks["deferred_public_pass_keeps_inner_retry_priority"] = plan.Single(p => p.DispatchPass).TailnetOnly; foreach (var mode in new[] { "manual", "direct" }) { var selection = mode == "manual" ? ConnectionSelection.Create(true, "node-a", ["node-a"]) : auto; var before = calls; var special = TransportPlan.Create(selection, [], mode == "direct" ? "direct" : "smart", 2); await foreach (var _ in TransportPlan.ExpandAsync(special, Dispatch, CancellationToken.None)) { } checks[mode + "_has_no_public_dispatch_pass"] = calls == before && special.All(p => !p.DispatchPass); } var knownBeforeConfirmation = a.KnownCandidate; checks["dispatched_node_known_before_confirmation_returns"] = ReferenceEquals(knownBeforeConfirmation, a.Cached!.Node); names.Clear(); var confirmationFailures = new List(); var connected = ""; await foreach (var candidate in TransportPlan.ExpandAsync(TransportPlan.Create(auto, [], "smart", 2), Dispatch, CancellationToken.None)) { var known = candidate.KnownCandidate; names.Add(candidate.Attempt.Slug); try { if (candidate.Attempt.Slug == "node-a") throw new ApiException(404, "fixture candidate removed"); connected = candidate.Attempt.Slug; break; } catch (ApiException failure) when (failure.StatusCode is not (401 or 403)) { if (known is null) break; confirmationFailures.Add(known.Slug); } } checks["candidate_a_confirmation_404_keeps_candidate_b"] = connected == "node-b" && names.SequenceEqual(new[] { "node-a", "node-b" }) && confirmationFailures.SequenceEqual(new[] { "node-a" }); names.Clear(); await foreach (var candidate in TransportPlan.ExpandAsync(TransportPlan.Create(auto, [], "smart", 2), Dispatch, CancellationToken.None)) { if (candidate.Dispatched && candidate.KnownCandidate is not null) continue; // Both confirmations unavailable. names.Add(candidate.Attempt.Slug); } checks["unavailable_dispatched_nodes_retain_domain_fallback"] = names.SequenceEqual(new[] { "auto", "auto" }); foreach (var status in new[] { 401, 403 }) { var denied = new ApiException(status, "fixture authorization revoked"); ApiException? observed = null; try { await foreach (var _ in TransportPlan.ExpandAsync(TransportPlan.Create(auto, [], "smart", 2), _ => Task.FromException>(denied), CancellationToken.None)) { } } catch (ApiException error) { observed = error; } checks["deferred_dispatch_auth_" + status + "_is_not_swallowed"] = ReferenceEquals(denied, observed); } using (var cancelled = new CancellationTokenSource()) { cancelled.Cancel(); var before = calls; var observed = false; try { await foreach (var _ in TransportPlan.ExpandAsync(plan, Dispatch, cancelled.Token)) { } } catch (OperationCanceledException) { observed = true; } checks["cancelled_iteration_does_not_dispatch"] = observed && calls == before; } using (var cancelled = new CancellationTokenSource()) { var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var never = new TaskCompletionSource>(TaskCreationOptions.RunContinuationsAsynchronously); async Task Consume() { await foreach (var _ in TransportPlan.ExpandAsync(TransportPlan.Create(auto, [], "smart", 2), _ => { started.SetResult(); return never.Task; }, cancelled.Token)) { } } var operation = Consume(); await started.Task.WaitAsync(TimeSpan.FromSeconds(1)); cancelled.Cancel(); var observed = false; try { await operation.WaitAsync(TimeSpan.FromSeconds(1)); } catch (OperationCanceledException) { observed = true; } checks["inflight_deferred_dispatch_cancellation_is_bounded"] = observed; } return checks; } }