using Microsoft.Win32; using System.Text.Json; namespace UCVL.VPNPlus; internal static class InstallationIntegrationSelfTest { private static readonly string[] Files = ["VPNPlus.exe", "xray/xray.exe", "xray/wintun.dll"]; internal static void Run(Dictionary checks) { using var hive = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32); var fixtures = new List(); (string Source, string Target) Fixture(bool existing = true) { var job = Path.Combine(Path.GetTempPath(), "ucvl-nsis-acceptance-" + Guid.NewGuid().ToString("N")); var source = Path.Combine(job, "payload"); var target = Path.Combine(job, "installed"); Directory.CreateDirectory(Path.Combine(source, "xray")); Directory.CreateDirectory(Path.Combine(target, "xray")); Directory.CreateDirectory(Path.Combine(job, "integration")); fixtures.Add(target); foreach (var file in Files) { File.WriteAllText(Path.Combine(source, file), "new " + file); if (existing) File.WriteAllText(Path.Combine(target, file), "old " + file); } File.WriteAllText(Path.Combine(target, "synthetic-settings.txt"), "preserve"); File.WriteAllText(Path.Combine(source, "install-manifest.json"), JsonSerializer.Serialize(Files.Select(f => new InstallationTransaction.PayloadFile(f, InstallationTransaction.Digest(Path.Combine(source, f)))))); var names = new[] { "Uninstall.exe", "Desktop.lnk", "StartMenu.lnk" }; for (var i = 0; i < 3; i++) { File.WriteAllText(Path.Combine(job, "integration", names[i]), "new component " + i); if (!existing) continue; var dest = InstallationIntegration.Destination(target, true, i); Directory.CreateDirectory(Path.GetDirectoryName(dest)!); File.WriteAllText(dest, "old component " + i); } if (existing) { using var app = hive.CreateSubKey(InstallationIntegration.Key(target, true, 0)); app.SetValue("Path", target); app.SetValue("", Path.Combine(target, "VPNPlus.exe")); app.SetValue("fixture-sentinel", "keep"); using var uninstall = hive.CreateSubKey(InstallationIntegration.Key(target, true, 1)); uninstall.SetValue("DisplayVersion", "old version"); uninstall.SetValue("Publisher", "%fixture%", RegistryValueKind.ExpandString); uninstall.SetValue("NoModify", new byte[] { 1, 2, 3 }, RegistryValueKind.Binary); uninstall.SetValue("NoRepair", 4294967297L, RegistryValueKind.QWord); } return (source, target); } void Assert(string name, bool passed) { checks[name] = passed; if (!passed) throw new IOException("Integration fixture failed: " + name); } bool Fails(Action work) { try { work(); return false; } catch (IOException) { return true; } } string Snapshot(string target) { var snapshot = new SortedDictionary(); foreach (var file in Files.Append("synthetic-settings.txt")) snapshot[file] = File.Exists(Path.Combine(target, file)) ? InstallationTransaction.Digest(Path.Combine(target, file)) : null; for (var i = 0; i < 3; i++) { var file = InstallationIntegration.Destination(target, true, i); snapshot["component-" + i] = File.Exists(file) ? InstallationTransaction.Digest(file) : null; } for (var i = 0; i < 2; i++) { using var key = hive.OpenSubKey(InstallationIntegration.Key(target, true, i)); snapshot["key-" + i] = key is null ? null : "exists"; if (key is not null) foreach (var name in key.GetValueNames().Order(StringComparer.Ordinal)) snapshot[$"key-{i}-{name}"] = key.GetValueKind(name) + ":" + JsonSerializer.Serialize(key.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames)); } return JsonSerializer.Serialize(snapshot); } try { for (var index = 0; index < 13; index++) { var fixture = Fixture(); var before = Snapshot(fixture.Target); Assert("integration_rollback_after_component_or_registry_write_" + index, Fails(() => InstallationTransaction.Apply(fixture.Source, fixture.Target, integrated: true, isolated: true, afterIntegrationWrite: i => { if (i == index) throw new IOException("synthetic integration failure"); })) && Snapshot(fixture.Target) == before); } var interrupted = Fixture(); var interruptedBefore = Snapshot(interrupted.Target); Assert("integration_interruption_before_commit_injected", Fails(() => InstallationTransaction.Apply(interrupted.Source, interrupted.Target, integrated: true, isolated: true, recoverOnFailure: false, afterIntegrationWrite: i => { if (i == 12) throw new IOException("synthetic process interruption"); }))); Assert("integration_pending_install_refuses_client_network_start", Fails(() => InstallationTransaction.ValidateClientLaunch(interrupted.Target))); InstallationTransaction.Recover(interrupted.Target, isolated: true); Assert("integration_restart_recovery_restores_files_shortcuts_and_typed_registry", Snapshot(interrupted.Target) == interruptedBefore); InstallationTransaction.Recover(interrupted.Target, isolated: true); Assert("integration_recovery_idempotent", Snapshot(interrupted.Target) == interruptedBefore); InstallationTransaction.ValidateClientLaunch(interrupted.Target); Assert("integration_rolled_back_install_allows_client_start", true); var fresh = Fixture(existing: false); var freshBefore = Snapshot(fresh.Target); Assert("integration_failed_first_install_removes_only_its_new_entries", Fails(() => InstallationTransaction.Apply(fresh.Source, fresh.Target, integrated: true, isolated: true, afterIntegrationWrite: i => { if (i == 12) throw new IOException("failure"); })) && Snapshot(fresh.Target) == freshBefore); var locked = Fixture(); var lockedBefore = Snapshot(locked.Target); using (var handle = File.Open(InstallationIntegration.Destination(locked.Target, true, 0), FileMode.Open, FileAccess.Read, FileShare.Read)) Assert("integration_locked_uninstaller_rolls_back_entire_payload", Fails(() => InstallationTransaction.Apply(locked.Source, locked.Target, integrated: true, isolated: true)) && Snapshot(locked.Target) == lockedBefore); var missing = Fixture(); var missingBefore = Snapshot(missing.Target); File.Delete(Path.Combine(Path.GetDirectoryName(missing.Source)!, "integration", "Desktop.lnk")); Assert("integration_missing_staged_component_rejected_before_payload", Fails(() => InstallationTransaction.Apply(missing.Source, missing.Target, integrated: true, isolated: true)) && Snapshot(missing.Target) == missingBefore); var success = Fixture(); InstallationTransaction.Apply(success.Source, success.Target, integrated: true, isolated: true); var committed = Snapshot(success.Target); InstallationTransaction.Recover(success.Target, isolated: true); InstallationTransaction.ValidateClientLaunch(success.Target); Assert("integration_committed_install_allows_client_start", true); Assert("integration_success_commits_payload_and_all_metadata", Files.All(f => File.ReadAllText(Path.Combine(success.Target, f)) == "new " + f) && Enumerable.Range(0, 3).All(i => File.ReadAllText(InstallationIntegration.Destination(success.Target, true, i)) == "new component " + i) && Snapshot(success.Target) == committed); using (var key = hive.OpenSubKey(InstallationIntegration.Key(success.Target, true, 0))) Assert("integration_preserves_unowned_registry_values", (string?)key!.GetValue("fixture-sentinel") == "keep"); var changed = Fixture(); Fails(() => InstallationTransaction.Apply(changed.Source, changed.Target, integrated: true, isolated: true, recoverOnFailure: false, afterIntegrationWrite: i => { if (i == 12) throw new IOException("interrupted"); })); using (var key = hive.OpenSubKey(InstallationIntegration.Key(changed.Target, true, 1), true)) key!.SetValue("Publisher", "external change"); var external = Snapshot(changed.Target); Assert("integration_recovery_refuses_external_metadata_changes", Fails(() => InstallationTransaction.Recover(changed.Target, isolated: true)) && Snapshot(changed.Target) == external); var conflict = Fixture(); using (var key = hive.OpenSubKey(InstallationIntegration.Key(conflict.Target, true, 0), true)) key!.SetValue("Path", Path.GetTempPath()); var conflictBefore = Snapshot(conflict.Target); Assert("integration_refuses_other_registered_directory", Fails(() => InstallationTransaction.Apply(conflict.Source, conflict.Target, integrated: true, isolated: true)) && Snapshot(conflict.Target) == conflictBefore); var scope = Fixture(); Fails(() => InstallationTransaction.Apply(scope.Source, scope.Target, integrated: true, isolated: true, recoverOnFailure: false, afterIntegrationWrite: i => { if (i == 0) throw new IOException("interrupted"); })); var scopeBefore = Snapshot(scope.Target); Assert("integration_production_recovery_refuses_isolated_journal", Fails(() => InstallationTransaction.Recover(scope.Target)) && Snapshot(scope.Target) == scopeBefore); var journalPath = Path.Combine(scope.Target, ".ucvl-installer", "journal.json"); var journal = JsonSerializer.Deserialize(File.ReadAllText(journalPath))!; File.WriteAllText(journalPath, JsonSerializer.Serialize(journal with { Integration = journal.Integration! with { Isolated = false } })); Assert("integration_isolated_recovery_refuses_production_scope_tampering", InstallationTransaction.RecoverIsolated(scope.Target) == 22 && Snapshot(scope.Target) == scopeBefore); } finally { // These exact generated GUID leaves belong solely to this run. Never // delete the shared test namespace or any production registration. foreach (var target in fixtures) { for (var i = 0; i < 2; i++) { var path = InstallationIntegration.Key(target, true, i); using (var key = hive.OpenSubKey(path, true)) if (key is not null) foreach (var name in key.GetValueNames()) key.DeleteValue(name); hive.DeleteSubKey(path, false); } hive.DeleteSubKey(Path.GetDirectoryName(InstallationIntegration.Key(target, true, 0))!, false); } } } }