Files
ucvl-zonghengjia-open-source/windows/NextGenerationSelfTest.cs
T

69 lines
4.2 KiB
C#
Raw Permalink Normal View History

using System.Text.Json;
namespace UCVL.VPNPlus;
internal static class NextGenerationSelfTest
{
internal static int Run()
{
var checks = new Dictionary<string, bool>();
checks["same_live_session_accepts_result"] = SessionOperationFence.IsCurrent(3, 3, "fixture-a", "fixture-a", false);
checks["older_login_rejects_success_even_with_reused_token"] = !SessionOperationFence.IsCurrent(2, 3, "fixture-a", "fixture-a", false);
checks["old_account_cannot_apply_profile_or_clear_new_account"] = !SessionOperationFence.IsCurrent(3, 4, "fixture-a", "fixture-b", false);
checks["changed_token_rejected_without_epoch_change"] = !SessionOperationFence.IsCurrent(3, 3, "fixture-a", "fixture-b", false);
checks["logout_rejects_pending_profile"] = !SessionOperationFence.IsCurrent(3, 4, "fixture-a", null, false);
checks["shutdown_rejects_pending_profile"] = !SessionOperationFence.IsCurrent(3, 3, "fixture-a", "fixture-a", true);
checks["empty_session_never_valid"] = !SessionOperationFence.IsCurrent(3, 3, "", "", false);
using (var form = new MainForm(preview: true, uiOnly: true))
foreach (var check in form.CheckNextGenerationLifecycle()) checks.Add(check.Key, check.Value);
var passed = checks.Values.All(v => v);
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "nextgen-lifecycle-self-test.json"), JsonSerializer.Serialize(
new { passed, checks, network_modified = false, credentials_loaded = false,
scope = "synthetic lifecycle and owned WinForms controls; not installed-client acceptance" },
new JsonSerializerOptions { WriteIndented = true }));
return passed ? 0 : 88;
}
}
internal sealed partial class MainForm
{
internal Dictionary<string, bool> CheckNextGenerationLifecycle()
{
var checks = new Dictionary<string, bool>();
SetSessionToken("fixture-a");
var oldRevision = _sessionRevision;
_observationQueue.Enqueue(("fixture-a", new LearningObservation(), Environment.TickCount64));
SetSessionToken("fixture-b");
checks["session_replacement_advances_epoch"] = _sessionRevision > oldRevision;
checks["session_replacement_removes_old_credential_queue"] = _observationQueue.IsEmpty;
checks["session_replacement_disables_overlay_reconnect_action"] = !_tailnetReconnectItem.Enabled;
checks["old_refresh_fence_rejects_new_session"] = !SessionOperationFence.IsCurrent(oldRevision, _sessionRevision, "fixture-a", _accessToken, false);
var currentRevision = _sessionRevision;
SetSessionToken("fixture-b");
checks["relogin_same_token_still_invalidates_old_operations"] = _sessionRevision > currentRevision;
using var pending = new CancellationTokenSource();
_connectionAttemptCancellation = pending;
_availableNodeCount = 1;
_wantConnected = true;
SetBusy(true);
checks["connecting_button_remains_cancellable"] = _connectButton.Enabled && _connectButton.Text == "取消连接";
checks["explicit_cancel_is_accepted"] = TryCancelPendingConnection();
checks["explicit_cancel_invalidates_attempt_token"] = pending.IsCancellationRequested;
checks["explicit_cancel_does_not_leave_reconnect_intent"] = !_wantConnected;
checks["cancel_waits_for_cleanup_before_reenable"] = !_connectButton.Enabled;
_connectionAttemptCancellation = null;
SetBusy(false);
checks["button_returns_to_connect_after_cleanup"] = _connectButton.Enabled && _connectButton.Text == "连接";
checks["no_attempt_cancel_is_noop"] = !TryCancelPendingConnection();
using var oldAccountWork = new CancellationTokenSource();
_connectionAttemptCancellation = oldAccountWork;
SetSessionToken("fixture-c");
checks["account_replacement_cancels_old_connection_work"] = oldAccountWork.IsCancellationRequested;
_connectionAttemptCancellation = null;
checks["readiness_does_not_fake_authenticated_transport"] = !_currentNodeUsesTailnet && !_tailnetReadiness.IsReady;
checks["tray_action_explicitly_warns_connection_rebuild"] = _tailnetReconnectItem.Text.Contains("会重建连接");
SetSessionToken(null);
return checks;
}
}