2026-11-29 23:48:07 +08:00
|
|
|
using System.Text.Json;
|
2026-11-02 04:13:58 +00:00
|
|
|
|
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
|
|
|
|
|
|
internal static class ConnectionSelectionSelfTest
|
|
|
|
|
{
|
|
|
|
|
internal static int Run()
|
|
|
|
|
{
|
|
|
|
|
var checks = new Dictionary<string, bool>();
|
2026-11-29 23:48:07 +08:00
|
|
|
string? error = null;
|
|
|
|
|
try
|
2026-11-02 04:13:58 +00:00
|
|
|
{
|
2026-11-29 23:48:07 +08:00
|
|
|
string[] authorized = ["aliyun-tokyo-002", "racknerd-newyork-004"];
|
|
|
|
|
var selection = ConnectionSelection.Create(false, null, authorized);
|
|
|
|
|
checks["default_is_fixed_vpn004"] = selection.NodeSlug == SingleNodeClientPolicy.FixedNodeSlug
|
|
|
|
|
&& !selection.Manual && selection.Attempts(99) == 1;
|
|
|
|
|
checks["server_must_return_fixed_vpn004"] = Rejected(() => selection.ValidateResponse("aliyun-tokyo-002"))
|
|
|
|
|
&& !Rejected(() => selection.ValidateResponse(SingleNodeClientPolicy.FixedNodeSlug));
|
|
|
|
|
checks["missing_vpn004_authorization_rejected"] = Rejected(() =>
|
|
|
|
|
ConnectionSelection.Create(false, null, ["aliyun-tokyo-002"]));
|
|
|
|
|
using var form = new MainForm(preview: true, uiOnly: true) { Opacity = 0, ShowInTaskbar = false };
|
|
|
|
|
foreach (var check in form.CheckSingleNodeUi()) checks["ui_" + check.Key] = check.Value;
|
2026-11-02 04:13:58 +00:00
|
|
|
}
|
2026-11-29 23:48:07 +08:00
|
|
|
catch (Exception exception) { error = exception.ToString(); }
|
|
|
|
|
var passed = error is null && checks.Count >= 11 && checks.Values.All(value => value);
|
2026-11-02 04:13:58 +00:00
|
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "connection-selection-self-test.json"),
|
2026-11-29 23:48:07 +08:00
|
|
|
JsonSerializer.Serialize(new { passed, checks, error, fixed_node = SingleNodeClientPolicy.FixedNodeSlug,
|
|
|
|
|
network_modified = false, local_office_gateway_tested = false },
|
2026-11-02 04:13:58 +00:00
|
|
|
new JsonSerializerOptions { WriteIndented = true }));
|
|
|
|
|
return passed ? 0 : 18;
|
2026-11-13 10:35:04 +08:00
|
|
|
|
2026-11-29 23:48:07 +08:00
|
|
|
static bool Rejected(Action action)
|
2026-11-13 10:35:04 +08:00
|
|
|
{
|
2026-11-29 23:48:07 +08:00
|
|
|
try { action(); return false; }
|
|
|
|
|
catch (InvalidOperationException) { return true; }
|
2026-11-13 10:35:04 +08:00
|
|
|
}
|
2026-11-02 04:13:58 +00:00
|
|
|
}
|
|
|
|
|
}
|