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

39 lines
1.9 KiB
C#
Raw Normal View History

using System.Text.Json;
namespace UCVL.VPNPlus;
internal static class ConnectionSelectionSelfTest
{
internal static int Run()
{
var checks = new Dictionary<string, bool>();
string? error = null;
try
{
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;
}
catch (Exception exception) { error = exception.ToString(); }
var passed = error is null && checks.Count >= 11 && checks.Values.All(value => value);
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "connection-selection-self-test.json"),
JsonSerializer.Serialize(new { passed, checks, error, fixed_node = SingleNodeClientPolicy.FixedNodeSlug,
network_modified = false, local_office_gateway_tested = false },
new JsonSerializerOptions { WriteIndented = true }));
return passed ? 0 : 18;
static bool Rejected(Action action)
{
try { action(); return false; }
catch (InvalidOperationException) { return true; }
}
}
}