Publish reviewed history-free open-source snapshot (2026-10-12)
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace UCVL.VPNPlus;
|
||||
|
||||
internal static class InstallationSelfTest
|
||||
{
|
||||
internal static int Run()
|
||||
{
|
||||
var root = Path.Combine(Path.GetTempPath(), "ucvl-install-fixture-" + Guid.NewGuid().ToString("N"));
|
||||
var checks = new Dictionary<string, bool>();
|
||||
Directory.CreateDirectory(root);
|
||||
(string Source, string Target) Fixture(string name)
|
||||
{
|
||||
var source = Path.Combine(root, name, "payload");
|
||||
var target = Path.Combine(root, name, "installed");
|
||||
Directory.CreateDirectory(Path.Combine(source, "xray"));
|
||||
Directory.CreateDirectory(Path.Combine(target, "xray"));
|
||||
foreach (var file in new[] { "VPNPlus.exe", "xray/xray.exe", "xray/wintun.dll", "README.md" })
|
||||
{
|
||||
File.WriteAllText(Path.Combine(source, file), "new fixture " + file);
|
||||
if (file != "README.md") File.WriteAllText(Path.Combine(target, file), "old fixture " + file);
|
||||
}
|
||||
// This is synthetic test data, not the user's actual saved login.
|
||||
File.WriteAllText(Path.Combine(target, "session.bin"), "fixture session");
|
||||
File.WriteAllText(Path.Combine(target, "device-id.txt"), "fixture device");
|
||||
File.WriteAllText(Path.Combine(source, "install-manifest.json"), JsonSerializer.Serialize(
|
||||
Directory.GetFiles(source, "*", SearchOption.AllDirectories).Select(f =>
|
||||
new InstallationTransaction.PayloadFile(Path.GetRelativePath(source, f).Replace('\\', '/'),
|
||||
Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(f)))))));
|
||||
return (source, target);
|
||||
}
|
||||
void Assert(string name, bool result)
|
||||
{
|
||||
checks[name] = result;
|
||||
if (!result) throw new Exception("Fixture failed: " + name);
|
||||
}
|
||||
bool OldIntact(string target) => File.ReadAllText(Path.Combine(target, "VPNPlus.exe")) == "old fixture VPNPlus.exe"
|
||||
&& File.ReadAllText(Path.Combine(target, "xray", "xray.exe")) == "old fixture xray/xray.exe"
|
||||
&& File.ReadAllText(Path.Combine(target, "xray", "wintun.dll")) == "old fixture xray/wintun.dll"
|
||||
&& !File.Exists(Path.Combine(target, "README.md"));
|
||||
bool Fails(Action action)
|
||||
{
|
||||
try { action(); return false; }
|
||||
catch (IOException) { return true; }
|
||||
}
|
||||
var report = new Dictionary<string, object> {
|
||||
["kind"] = "isolated_install_transaction_fault_injection",
|
||||
["real_installation_changed"] = false, ["network_changed"] = false,
|
||||
["fixture_directory"] = root, ["checks"] = checks,
|
||||
};
|
||||
try
|
||||
{
|
||||
// Inspect the actual launch policy without starting PowerShell or changing DNS.
|
||||
const string fixtureScript = "Write-Output 'synthetic launch fixture'";
|
||||
var powerShell = SystemPowerShell.CreateStartInfo(fixtureScript);
|
||||
var systemDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
|
||||
Assert("powershell_uses_absolute_system_executable", Path.IsPathFullyQualified(powerShell.FileName)
|
||||
&& powerShell.FileName == Path.Combine(systemDirectory, "WindowsPowerShell", "v1.0", "powershell.exe"));
|
||||
Assert("powershell_uses_existing_system_working_directory", powerShell.WorkingDirectory == systemDirectory
|
||||
&& Directory.Exists(powerShell.WorkingDirectory));
|
||||
var originalDirectory = Environment.CurrentDirectory;
|
||||
try
|
||||
{
|
||||
Environment.CurrentDirectory = root;
|
||||
Assert("powershell_does_not_inherit_current_directory", SystemPowerShell.CreateStartInfo(fixtureScript).WorkingDirectory == systemDirectory);
|
||||
}
|
||||
finally { Environment.CurrentDirectory = originalDirectory; }
|
||||
const string prefix = "-NoProfile -NonInteractive -EncodedCommand ";
|
||||
Assert("powershell_keeps_encoded_command_unchanged", powerShell.Arguments.StartsWith(prefix, StringComparison.Ordinal)
|
||||
&& System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(powerShell.Arguments[prefix.Length..])) == fixtureScript);
|
||||
Assert("powershell_has_no_shell_or_visible_window", !powerShell.UseShellExecute && powerShell.CreateNoWindow
|
||||
&& powerShell.WindowStyle == System.Diagnostics.ProcessWindowStyle.Hidden);
|
||||
Assert("powershell_redirects_both_diagnostic_streams", powerShell.RedirectStandardOutput && powerShell.RedirectStandardError);
|
||||
ReplacementContinuitySelfTest.Run(root, checks);
|
||||
var isolated = Path.Combine(Path.GetTempPath(), "ucvl-nsis-acceptance-" + Guid.NewGuid().ToString("N"));
|
||||
Directory.CreateDirectory(isolated);
|
||||
var isolatedTarget = Path.Combine(isolated, "installed");
|
||||
Assert("isolated_nsis_target_allowed", InstallationTransaction.IsolatedTarget(isolatedTarget) == isolatedTarget.TrimEnd(Path.DirectorySeparatorChar));
|
||||
Assert("isolated_nsis_rejects_live_install", Fails(() => InstallationTransaction.IsolatedTarget(
|
||||
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs", "UCVL", "VPNPlus"))));
|
||||
Assert("isolated_nsis_rejects_temp_root", Fails(() => InstallationTransaction.IsolatedTarget(Path.GetTempPath())));
|
||||
Assert("isolated_nsis_rejects_nested_target", Fails(() => InstallationTransaction.IsolatedTarget(Path.Combine(isolatedTarget, "installed"))));
|
||||
Assert("isolated_nsis_rejects_missing_job", Fails(() => InstallationTransaction.IsolatedTarget(
|
||||
Path.Combine(Path.GetTempPath(), "ucvl-nsis-acceptance-" + Guid.NewGuid().ToString("N"), "installed"))));
|
||||
Assert("isolated_nsis_rejects_invalid_identifier", Fails(() => InstallationTransaction.IsolatedTarget(
|
||||
Path.Combine(Path.GetTempPath(), "ucvl-nsis-acceptance-not-a-guid", "installed"))));
|
||||
var success = Fixture("success");
|
||||
InstallationTransaction.Apply(success.Source, success.Target, _ =>
|
||||
Assert("public_executable_never_missing", File.Exists(Path.Combine(success.Target, "VPNPlus.exe"))));
|
||||
Assert("new_program_installed", File.ReadAllText(Path.Combine(success.Target, "VPNPlus.exe")) == "new fixture VPNPlus.exe");
|
||||
Assert("unlisted_configuration_preserved", File.ReadAllText(Path.Combine(success.Target, "session.bin")) == "fixture session"
|
||||
&& File.ReadAllText(Path.Combine(success.Target, "device-id.txt")) == "fixture device");
|
||||
|
||||
var bad = Fixture("bad-payload");
|
||||
File.AppendAllText(Path.Combine(bad.Source, "VPNPlus.exe"), "corrupted");
|
||||
Assert("corrupt_payload_rejected_before_write", Fails(() => InstallationTransaction.Apply(bad.Source, bad.Target)) && OldIntact(bad.Target));
|
||||
|
||||
for (var index = 0; index < 4; index++)
|
||||
{
|
||||
var fault = Fixture("fault-" + index);
|
||||
Assert("rollback_after_write_" + index, Fails(() => InstallationTransaction.Apply(fault.Source, fault.Target,
|
||||
i => { if (i == index) throw new IOException("simulated write failure"); })) && OldIntact(fault.Target));
|
||||
}
|
||||
|
||||
var locked = Fixture("locked-main");
|
||||
using (var handle = new FileStream(Path.Combine(locked.Target, "VPNPlus.exe"), FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||
Assert("locked_executable_rolls_back_dependencies", Fails(() => InstallationTransaction.Apply(locked.Source, locked.Target)) && OldIntact(locked.Target));
|
||||
|
||||
var interrupted = Fixture("interrupted");
|
||||
Assert("interruption_injected", Fails(() => InstallationTransaction.Apply(interrupted.Source, interrupted.Target,
|
||||
i => { if (i == 1) throw new IOException("simulated interrupted installer"); }, recoverOnFailure: false)));
|
||||
InstallationTransaction.Recover(interrupted.Target);
|
||||
Assert("pending_journal_recovers", OldIntact(interrupted.Target));
|
||||
InstallationTransaction.Recover(interrupted.Target);
|
||||
Assert("recovery_idempotent", OldIntact(interrupted.Target));
|
||||
InstallationTransaction.Apply(interrupted.Source, interrupted.Target);
|
||||
Assert("upgrade_after_recovery", File.ReadAllText(Path.Combine(interrupted.Target, "VPNPlus.exe")) == "new fixture VPNPlus.exe");
|
||||
|
||||
var traversal = Fixture("traversal");
|
||||
var manifest = Path.Combine(traversal.Source, "install-manifest.json");
|
||||
File.WriteAllText(manifest, File.ReadAllText(manifest).Replace("README.md", "../README.md"));
|
||||
Assert("traversal_rejected", Fails(() => InstallationTransaction.Apply(traversal.Source, traversal.Target)) && OldIntact(traversal.Target));
|
||||
|
||||
var modified = Fixture("external-change");
|
||||
Fails(() => InstallationTransaction.Apply(modified.Source, modified.Target,
|
||||
i => { if (i == 0) throw new IOException("simulated interruption"); }, recoverOnFailure: false));
|
||||
File.WriteAllText(Path.Combine(modified.Target, "README.md"), "user changed this file after interruption");
|
||||
Assert("recovery_does_not_overwrite_external_change", Fails(() => InstallationTransaction.Recover(modified.Target))
|
||||
&& File.ReadAllText(Path.Combine(modified.Target, "README.md")) == "user changed this file after interruption");
|
||||
InstallationIntegrationSelfTest.Run(checks);
|
||||
report["passed"] = true;
|
||||
return 0;
|
||||
}
|
||||
catch (Exception exception) { report["passed"] = false; report["error"] = exception.ToString(); return 21; }
|
||||
finally
|
||||
{
|
||||
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "installation-self-test.json"),
|
||||
JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true }));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user