Files
ucvl-zonghengjia-open-source/windows/ReplacementContinuitySelfTest.cs
2026-10-11 23:23:20 +00:00

81 lines
3.9 KiB
C#

using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text.Json;
namespace UCVL.VPNPlus;
// Only synthetic TEMP data; calls the actual production replacement method.
internal static class ReplacementContinuitySelfTest
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern uint GetFileAttributesW(string path);
internal static void Run(string root, Dictionary<string, bool> checks)
{
var dir = Path.Combine(root, "concurrent-replacement");
Directory.CreateDirectory(dir);
var first = Path.Combine(dir, "first.bin");
var second = Path.Combine(dir, "second.bin");
var target = Path.Combine(dir, "main.bin");
File.WriteAllBytes(first, Enumerable.Repeat((byte)0x35, 4096).ToArray());
File.WriteAllBytes(second, Enumerable.Repeat((byte)0x71, 4096).ToArray());
File.Copy(first, target);
var acl = new FileInfo(target).GetAccessControl();
acl.SetAccessRuleProtection(true, true);
new FileInfo(target).SetAccessControl(acl);
const AccessControlSections sections = AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group;
var oldSecurity = new FileInfo(target).GetAccessControl(sections).GetSecurityDescriptorSddlForm(sections);
var errors = new Dictionary<string, int>();
var observations = 0L;
var completed = 0;
var stop = 0;
using var ready = new ManualResetEventSlim();
var observer = new Thread(() => {
ready.Set();
while (Volatile.Read(ref stop) == 0)
{
if (GetFileAttributesW(target) == uint.MaxValue)
{
var error = Marshal.GetLastWin32Error().ToString();
errors[error] = errors.GetValueOrDefault(error) + 1;
}
observations++;
}
});
observer.Start(); ready.Wait();
try
{
for (var i = 0; i < 500; i++)
{
var source = (i % 2 == 0) ? second : first;
InstallationTransaction.Replace(source, target);
if (!File.ReadAllBytes(source).SequenceEqual(File.ReadAllBytes(target)))
throw new IOException("Replacement bytes do not match");
completed++;
}
}
finally
{
Volatile.Write(ref stop, 1); observer.Join();
File.WriteAllText(Path.Combine(root, "replacement-observations.json"), JsonSerializer.Serialize(
new { Completed = completed, Observations = observations, Win32Errors = errors }));
}
void Check(string key, bool ok) { checks[key] = ok; if (!ok) throw new IOException(key); }
Check("concurrent_500_replacements_keep_public_path", completed == 500 && observations > 0 && errors.Count == 0);
Check("replacement_keeps_owner_group_and_protected_dacl", new FileInfo(target).GetAccessControl(sections)
.GetSecurityDescriptorSddlForm(sections) == oldSecurity);
var before = InstallationTransaction.Digest(target);
var refused = false;
using (var held = new FileStream(target, FileMode.Open, FileAccess.Read, FileShare.Read))
{
try { InstallationTransaction.Replace(second, target); }
catch (IOException) { refused = true; }
}
Check("locked_replacement_preserves_exact_original", refused && InstallationTransaction.Digest(target) == before);
Check("replacement_removes_only_own_staging_files", Directory.GetFiles(dir, "*.update-*.tmp").Length == 0);
var fresh = Path.Combine(dir, "new.bin");
InstallationTransaction.Replace(second, fresh);
Check("replacement_new_file_matches_source", InstallationTransaction.Digest(second) == InstallationTransaction.Digest(fresh));
}
}