using System.Diagnostics; using System.Security.Cryptography; using System.Text.Json; namespace UCVL.VPNPlus; // Integration with real release files, but exclusively fresh temporary targets. // Does not run NSIS, stop the live app, alter registry/shortcuts or use AppPaths. internal static class InstallationPackageSelfTest { internal static int Run(string oldPayload) { var source = AppContext.BaseDirectory; var folder = Path.Combine(Path.GetTempPath(), "ucvl-real-payload-test-" + Guid.NewGuid().ToString("N")); var checks = new Dictionary(); var report = new Dictionary { ["time"] = DateTimeOffset.Now, ["temporary_directory"] = folder, ["live_installation_modified"] = false, ["network_modified"] = false, ["nsis_ui_tested"] = false, ["checks"] = checks, }; try { var files = JsonSerializer.Deserialize>( File.ReadAllText(Path.Combine(source, "install-manifest.json")))!; if (files.Count == 0 || !File.Exists(Path.Combine(oldPayload, "VPNPlus.exe"))) throw new IOException("Missing complete old release fixture."); // Validate before any use of manifest paths. This only reads the candidate. if (InstallationTransaction.Run("", verifyOnly: true) != 0) throw new IOException("Candidate verification failed."); Directory.CreateDirectory(folder); string Seed(string name) { var target = Path.Combine(folder, name); Directory.CreateDirectory(target); foreach (var item in files) { var previous = Path.Combine(oldPayload, item.Path); if (!File.Exists(previous)) continue; var destination = Path.Combine(target, item.Path); Directory.CreateDirectory(Path.GetDirectoryName(destination)!); File.Copy(previous, destination); } File.WriteAllText(Path.Combine(target, "user-settings.fixture"), "unchanged synthetic settings"); return target; } void Assert(string name, bool value) { checks[name] = value; if (!value) throw new IOException("Real payload check failed: " + name); } string Hash(string file) => Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(file))); bool Original(string target) => files.All(item => { var previous = Path.Combine(oldPayload, item.Path); var destination = Path.Combine(target, item.Path); return File.Exists(previous) ? File.Exists(destination) && Hash(previous) == Hash(destination) : !File.Exists(destination); }); bool Rejected(Action operation) { try { operation(); return false; } catch (IOException) { return true; } } var success = Seed("upgrade"); InstallationTransaction.Apply(source, success, _ => Assert("exe_path_always_present", File.Exists(Path.Combine(success, "VPNPlus.exe")))); Assert("all_candidate_hashes_match", files.All(item => Hash(Path.Combine(success, item.Path)) == item.Sha256)); Assert("synthetic_configuration_preserved", File.ReadAllText(Path.Combine(success, "user-settings.fixture")) == "unchanged synthetic settings"); using (var child = Process.Start(new ProcessStartInfo { FileName = Path.Combine(success, "VPNPlus.exe"), Arguments = "--startup-self-test", WorkingDirectory = success, UseShellExecute = false, CreateNoWindow = true, }) ?? throw new IOException("Installed fixture could not start.")) { if (!child.WaitForExit(30_000)) { child.Kill(); throw new IOException("Startup check timed out."); } Assert("copied_application_startup_self_test", child.ExitCode == 0); } var locked = Seed("locked-dependency"); using (var handle = new FileStream(Path.Combine(locked, "xray", "xray.exe"), FileMode.Open, FileAccess.Read, FileShare.Read)) Assert("locked_real_dependency_rolls_back_all_files", Rejected(() => InstallationTransaction.Apply(source, locked)) && Original(locked)); var interrupted = Seed("interrupted-after-main"); Assert("interruption_after_final_replacement_recorded", Rejected(() => InstallationTransaction.Apply(source, interrupted, index => { if (index == files.Count - 1) throw new IOException("Synthetic interruption after final replacement"); }, recoverOnFailure: false))); InstallationTransaction.Recover(interrupted); Assert("full_old_payload_restored_after_interruption", Original(interrupted)); InstallationTransaction.Recover(interrupted); Assert("full_payload_recovery_is_idempotent", Original(interrupted)); report["passed"] = true; return 0; } catch (Exception error) { report["passed"] = false; report["error"] = error.ToString(); return 23; } finally { File.WriteAllText(Path.Combine(source, "installation-package-self-test.json"), JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true })); } } }