124 lines
7.0 KiB
C#
124 lines
7.0 KiB
C#
using System.Reflection;
|
|
using System.Text.Json;
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
internal static class ClientShutdownUiSelfTest
|
|
{
|
|
internal static int Run()
|
|
{
|
|
var checks = new Dictionary<string, bool>();
|
|
string? error = null;
|
|
try
|
|
{
|
|
var uiThread = Environment.CurrentManagedThreadId;
|
|
var disposals = 0;
|
|
var background = false;
|
|
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
using (var form = new MainForm(uiOnly: true, exitRecoveryFixture: new(
|
|
async () => { Interlocked.Increment(ref disposals); background = Environment.CurrentManagedThreadId != uiThread; await release.Task; })))
|
|
{
|
|
form.Show(); PumpUntil(() => form.LoginReadyForTest);
|
|
form.WindowChromeForTest!.SetLoginMode(false);
|
|
form.Close();
|
|
checks["ordinary_authenticated_close_is_blocked"] = form.Visible && !form.IsDisposed && disposals == 0;
|
|
form.RequestExitForUpdate();
|
|
PumpUntil(() => Volatile.Read(ref disposals) == 1);
|
|
form.RequestExitForUpdate();
|
|
checks["installer_exit_bypasses_tray_and_starts_cleanup_once"] = disposals == 1 && !form.IsDisposed && background;
|
|
release.SetResult();
|
|
checks["installer_closes_when_tracked_cleanup_finishes"] = PumpUntil(() => form.IsDisposed);
|
|
}
|
|
|
|
using (var failed = new MainForm(uiOnly: true, exitRecoveryFixture: new(
|
|
() => Task.FromException(new IOException("isolated recovery failure")))))
|
|
{
|
|
failed.Show(); PumpUntil(() => failed.LoginReadyForTest);
|
|
failed.RequestExitForUpdate();
|
|
checks["installer_recovery_error_closes_without_blocking_modal"] = PumpUntil(() => failed.IsDisposed)
|
|
&& failed.NetworkRecoveryFailed;
|
|
}
|
|
|
|
var timedOutRecovery = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
using (var timedOut = new MainForm(uiOnly: true, exitRecoveryFixture: new(
|
|
() => timedOutRecovery.Task, TimeSpan.FromMilliseconds(80))))
|
|
{
|
|
timedOut.Show(); PumpUntil(() => timedOut.LoginReadyForTest);
|
|
timedOut.RequestExitForUpdate();
|
|
checks["stalled_recovery_bounds_exit_and_blocks_install_success"] = PumpUntil(() => timedOut.IsDisposed)
|
|
&& timedOut.NetworkRecoveryFailed;
|
|
timedOutRecovery.SetResult();
|
|
}
|
|
|
|
disposals = 0;
|
|
using (var canceled = new MainForm(uiOnly: true, exitRecoveryFixture: new(
|
|
() => { Interlocked.Increment(ref disposals); return Task.CompletedTask; })))
|
|
{
|
|
canceled.Show(); PumpUntil(() => canceled.LoginReadyForTest);
|
|
Invoke(canceled, "SetBusy", true, null);
|
|
var watch = System.Diagnostics.Stopwatch.StartNew();
|
|
var query = Dispatch(canceled, 0x11, IntPtr.Zero);
|
|
var repeat = Dispatch(canceled, 0x11, IntPtr.Zero);
|
|
checks["repeated_session_query_returns_promptly_without_recovery"] = watch.ElapsedMilliseconds < 300
|
|
&& query == (IntPtr)1 && repeat == (IntPtr)1 && disposals == 0
|
|
&& !Lifetime(canceled).IsCancellationRequested && !canceled.IsDisposed;
|
|
// Query/canceled END must not reset an active operation or its UI.
|
|
canceled.Enabled = false; canceled.UseWaitCursor = true;
|
|
Dispatch(canceled, 0x16, IntPtr.Zero);
|
|
Dispatch(canceled, 0x16, IntPtr.Zero);
|
|
checks["canceled_session_query_preserves_active_ui_state"] = !canceled.Enabled && canceled.UseWaitCursor
|
|
&& !Lifetime(canceled).IsCancellationRequested && disposals == 0;
|
|
canceled.Enabled = true; Invoke(canceled, "SetBusy", false, null);
|
|
checks["operation_can_finish_after_canceled_shutdown"] = canceled.LoginReadyForTest;
|
|
canceled.RequestExitForUpdate();
|
|
checks["canceled_query_client_can_later_exit"] = PumpUntil(() => canceled.IsDisposed) && disposals == 1;
|
|
}
|
|
|
|
disposals = 0;
|
|
using (var queued = new MainForm(uiOnly: true, exitRecoveryFixture: new(
|
|
() => { Interlocked.Increment(ref disposals); return Task.CompletedTask; })))
|
|
{
|
|
queued.Show(); PumpUntil(() => queued.LoginReadyForTest);
|
|
Dispatch(queued, 0x11, IntPtr.Zero);
|
|
queued.RequestExitForUpdate();
|
|
checks["installer_exit_still_works_after_session_query"] = PumpUntil(() => queued.IsDisposed) && disposals == 1;
|
|
}
|
|
|
|
disposals = 0;
|
|
using (var confirmed = new MainForm(uiOnly: true, exitRecoveryFixture: new(
|
|
() => { Interlocked.Increment(ref disposals); return Task.CompletedTask; })))
|
|
{
|
|
confirmed.Show(); PumpUntil(() => confirmed.LoginReadyForTest);
|
|
Dispatch(confirmed, 0x11, IntPtr.Zero);
|
|
Invoke(confirmed, "CompleteSessionEnding");
|
|
Invoke(confirmed, "CompleteSessionEnding");
|
|
Dispatch(confirmed, 0x16, IntPtr.Zero);
|
|
checks["confirmed_session_finalizes_once_and_cannot_resume"] = disposals == 1
|
|
&& Lifetime(confirmed).IsCancellationRequested && !confirmed.NetworkRecoveryFailed;
|
|
}
|
|
}
|
|
catch (Exception exception) { error = exception.ToString(); }
|
|
var passed = error is null && checks.Values.All(v => v);
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "client-exit-ui-self-test.json"),
|
|
JsonSerializer.Serialize(new { Passed = passed, Checks = checks, Error = error, ProductionNetworking = false, ActualWindowsShutdown = false }, new JsonSerializerOptions { WriteIndented = true }));
|
|
return passed ? 0 : 86;
|
|
}
|
|
|
|
private static object? Invoke(MainForm form, string method, params object?[] args) => typeof(MainForm)
|
|
.GetMethod(method, BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(form, args);
|
|
private static IntPtr Dispatch(MainForm form, int message, IntPtr wparam)
|
|
{
|
|
object?[] args = [Message.Create(form.Handle, message, wparam, IntPtr.Zero)];
|
|
typeof(MainForm).GetMethod("WndProc", BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(form, args);
|
|
return ((Message)args[0]!).Result;
|
|
}
|
|
private static CancellationTokenSource Lifetime(MainForm form) => (CancellationTokenSource)typeof(MainForm)
|
|
.GetField("_lifetime", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(form)!;
|
|
private static bool PumpUntil(Func<bool> condition)
|
|
{
|
|
var watch = System.Diagnostics.Stopwatch.StartNew();
|
|
while (!condition() && watch.ElapsedMilliseconds < 2500) { Application.DoEvents(); Thread.Sleep(5); }
|
|
return condition();
|
|
}
|
|
}
|