Publish Zonghengjia Windows 0.3.20 client source
This commit is contained in:
@@ -1,80 +1,109 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.AccessControl;
|
||||
using System.Security.Principal;
|
||||
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);
|
||||
|
||||
private sealed record SecuritySnapshot(string Owner, string Group, bool Protected, string[] Rules);
|
||||
|
||||
private static SecuritySnapshot Snapshot(string path)
|
||||
{
|
||||
var security = new FileInfo(path).GetAccessControl(
|
||||
AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group);
|
||||
var rules = security.GetAccessRules(true, true, typeof(SecurityIdentifier))
|
||||
.OfType<FileSystemAccessRule>()
|
||||
.Select(rule => string.Join('|',
|
||||
rule.IdentityReference.Value,
|
||||
(int)rule.AccessControlType,
|
||||
(int)rule.FileSystemRights,
|
||||
(int)rule.InheritanceFlags,
|
||||
(int)rule.PropagationFlags,
|
||||
rule.IsInherited))
|
||||
.OrderBy(value => value, StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
return new(
|
||||
security.GetOwner(typeof(SecurityIdentifier))!.Value,
|
||||
security.GetGroup(typeof(SecurityIdentifier))!.Value,
|
||||
security.AreAccessRulesProtected,
|
||||
rules);
|
||||
}
|
||||
|
||||
private static bool SameSecurity(SecuritySnapshot expected, SecuritySnapshot actual) =>
|
||||
expected.Owner == actual.Owner
|
||||
&& expected.Group == actual.Group
|
||||
&& expected.Protected == actual.Protected
|
||||
&& expected.Rules.SequenceEqual(actual.Rules, StringComparer.Ordinal);
|
||||
|
||||
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);
|
||||
var oldSecurity = Snapshot(target);
|
||||
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", SameSecurity(oldSecurity, Snapshot(target)));
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user