XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Buffers.Binary;
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using XFEToolBox.Indexing.Service.Engine;

var tests = new (string Name, Func<Task> Run)[]
{
    ("Native volume discovery reads mount metadata without enumerating files", () => Sync(VolumeDiscovery)),
    ("Native layout retains every hardlink and excludes DOS aliases", () => Sync(LayoutRetainsAllHardlinks)),
    ("Native parsers reject corrupted and cyclic offsets", () => Sync(ParsersRejectCorruption)),
    ("USN parser reads rename and delete reasons", () => Sync(JournalParser)),
    ("Catalog resolves nested directories and hardlinks", () => Sync(CatalogPaths)),
    ("Atomic checkpoint validates volume and journal identity", () => Sync(CacheIdentity)),
    ("Restart replays USN without enumerating MFT again", RestartReplaysJournal),
    ("Changes during initial MFT enumeration are replayed", InitialScanRace),
    ("Directory rename updates every descendant and hardlinks refresh", DirectoryRenameAndHardlinks),
    ("Truncated journal triggers exactly one full rebuild", InvalidJournalRebuild),
    ("ACL predicate filters matches and totals on the calling thread", AccessFilter),
    ("Unavailable USN is explicit and does not loop/full scan", NoJournalFallback),
    ("A damaged volume does not stop healthy volume listeners", DamagedVolumeIsolation),
    ("Slow native metadata IO never blocks Start or Search", SlowMetadataDoesNotBlockSearch),
    ("Protected phase audit proves cold scan versus resumed USN without names", AuditColdAndWarmStart),
    ("Diagnostics write failures never stop indexing", AuditFailureDoesNotStopIndex),
    ("Rapid rebuild notifications are merged without semaphore exceptions", RebuildWakeCoalesces),
};
var failures = 0;
foreach (var test in tests)
{
    try { await test.Run(); Console.WriteLine("PASS " + test.Name); }
    catch (Exception exception) { failures++; Console.WriteLine("FAIL " + test.Name + ": " + exception); }
}
Console.WriteLine($"{tests.Length - failures} passed, {failures} failed.");
return failures == 0 ? 0 : 1;

static Task Sync(Action test) { test(); return Task.CompletedTask; }
static void VolumeDiscovery()
{
    if (!OperatingSystem.IsWindows()) return;
    var volumes = NtfsNativeVolume.GetVolumes();
    Ensure(volumes.All(volume => volume.Root.EndsWith('\\') && volume.VolumeId.StartsWith("\\\\?\\Volume{")));
    Ensure(volumes.DistinctBy(volume => volume.VolumeId).Count() == volumes.Length);
    Console.WriteLine($"Native mount metadata: {volumes.Length} fixed NTFS volumes.");
}
static void Ensure(bool condition, string message = "Assertion failed")
{
    if (!condition) throw new InvalidOperationException(message);
}
static async Task Until(Func<bool> predicate)
{
    var timer = Stopwatch.StartNew();
    while (!predicate())
    {
        if (timer.Elapsed > TimeSpan.FromSeconds(12)) throw new TimeoutException("Index worker did not reach expected state.");
        await Task.Delay(20);
    }
}
static void LayoutRetainsAllHardlinks()
{
    var bytes = BuildLayout(21, 0, [(5, "first.txt", 1), (11, "second.txt", 0), (5, "FIRST~1.TXT", 2)]);
    var nodes = NtfsMetadataParser.ParseLayout(bytes);
    Ensure(nodes.Length == 1 && nodes[0].FileId == 21 && nodes[0].Names.Length == 2);
    Ensure(nodes[0].Names[1] == new NtfsName(11, "second.txt"));
}
static void ParsersRejectCorruption()
{
    var valid = BuildLayout(21, 0, [(5, "first.txt", 1)]);
    foreach (var invalid in new[] { valid[..15], valid[..60], (byte[])valid.Clone() })
    {
        if (invalid.Length == valid.Length)
            BinaryPrimitives.WriteUInt32LittleEndian(invalid.AsSpan(56), 1); // invalid next-name offset
        try { NtfsMetadataParser.ParseLayout(invalid); throw new InvalidOperationException("Corrupt layout accepted."); }
        catch (InvalidDataException) { }
    }
    var journal = new byte[68];
    BinaryPrimitives.WriteUInt32LittleEndian(journal.AsSpan(8), 60);
    BinaryPrimitives.WriteUInt16LittleEndian(journal.AsSpan(12), 3);
    try { NtfsMetadataParser.ParseJournal(journal); throw new InvalidOperationException("Unknown USN version accepted."); }
    catch (InvalidDataException) { }
}
static void JournalParser()
{
    var bytes = new byte[72];
    BinaryPrimitives.WriteInt64LittleEndian(bytes, 101);
    BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(8), 64);
    BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(12), 2);
    BinaryPrimitives.WriteUInt64LittleEndian(bytes.AsSpan(16), 21);
    BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(48), 0x2200);
    BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(64), 2);
    BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(66), 60);
    Encoding.Unicode.GetBytes("a").CopyTo(bytes, 68);
    var parsed = NtfsMetadataParser.ParseJournal(bytes);
    Ensure(parsed.NextUsn == 101 && parsed.Changes.Single() == new NtfsChange(21, 0x2200));
}
static void CatalogPaths()
{
    var catalog = NewCatalog();
    var directories = catalog.ResolveDirectories("T:\\", default);
    Ensure(directories[11] == "T:\\work");
    var paths = NtfsCatalog.GetEntries(catalog.Nodes[21], "T:\\", directories).Select(entry => entry.FullPath).ToArray();
    Ensure(paths.SequenceEqual(new[] { "T:\\work\\alpha.txt", "T:\\alpha-link.txt" }));
    catalog.Nodes[11] = new(11, 0x10, [new(99, "orphan")]);
    Ensure(!NtfsCatalog.GetEntries(catalog.Nodes[21], "T:\\", catalog.ResolveDirectories("T:\\", default))
        .Any(entry => entry.FullPath.Contains("orphan")));
}
static void CacheIdentity()
{
    using var fixture = new Fixture();
    var catalog = NewCatalog();
    catalog.JournalId = 17;
    catalog.NextUsn = 100;
    var path = NtfsCatalog.GetCachePath(fixture.Directory, fixture.Volume);
    catalog.SaveAtomic(path, fixture.Volume, default);
    Ensure(NtfsCatalog.TryLoad(path, fixture.Volume, new(17, 0, 120, 0))?.Nodes.Count == 3);
    Ensure(NtfsCatalog.TryLoad(path, fixture.Volume with { SerialNumber = 2 }, new(17, 0, 120, 0)) is null);
    Ensure(NtfsCatalog.TryLoad(path, fixture.Volume, new(18, 0, 120, 0)) is null);
    Ensure(NtfsCatalog.TryLoad(path, fixture.Volume, new(17, 110, 120, 110)) is null);
    // A left-over .pending generation cannot change the trusted checkpoint.
    using (var stream = File.Create(path + ".pending")) stream.Write([1, 2, 3]);
    Ensure(NtfsCatalog.TryLoad(path, fixture.Volume, new(17, 0, 120, 0)) is not null);
    using (var stream = File.Open(path, FileMode.Truncate)) stream.Write([1, 2, 3]);
    Ensure(NtfsCatalog.TryLoad(path, fixture.Volume, new(17, 0, 120, 0)) is null);
}
static async Task RestartReplaysJournal()
{
    using var fixture = new Fixture();
    await using (var engine = fixture.Engine())
    {
        await engine.StartAsync();
        await Until(() => engine.GetStatus().ReadyVolumes == 1);
        Ensure(engine.Search("alpha", 0, 100).TotalMatches == 2);
    }
    fixture.Source.SetNode(new(22, 0, [new(11, "after-stop.txt")]), 0x100);
    await using (var engine = fixture.Engine())
    {
        await engine.StartAsync();
        await Until(() => engine.GetStatus().ReadyVolumes == 1);
        Ensure(engine.Search("after-stop", 0, 100).TotalMatches == 1);
        Ensure(fixture.Source.Enumerations == 1, "Warm restart enumerated MFT again.");
        Ensure(fixture.Source.FilteredReads > 0);
    }
}
static async Task InitialScanRace()
{
    using var fixture = new Fixture();
    fixture.Source.AfterEnumeration = () => fixture.Source.SetNode(new(22, 0, [new(11, "during-cold-scan.txt")]), 0x100);
    await using var engine = fixture.Engine();
    await engine.StartAsync();
    await Until(() => engine.GetStatus().ReadyVolumes == 1);
    Ensure(engine.Search("during-cold-scan", 0, 100).TotalMatches == 1);
    Ensure(engine.GetStatus().VolumeStatuses.Single().NextUsn == fixture.Source.NextUsn);
}
static async Task DirectoryRenameAndHardlinks()
{
    using var fixture = new Fixture();
    fixture.Source.SetNode(new(31, 0, [new(5, "unrelated.txt")]), 0x100);
    await using var engine = fixture.Engine();
    await engine.StartAsync();
    await Until(() => engine.GetStatus().ReadyVolumes == 1);
    var unrelated = engine.Search("unrelated", 0, 100).Items.Single().Entry;
    fixture.Source.SetNode(new(11, 0x10, [new(5, "renamed")]), 0x2000);
    await Until(() => engine.Search("path:renamed alpha", 0, 100).TotalMatches == 1);
    Ensure(engine.Search("path:work", 0, 100).TotalMatches == 0);
    Ensure(ReferenceEquals(unrelated, engine.Search("unrelated", 0, 100).Items.Single().Entry),
        "Directory rename unnecessarily rebuilt unrelated volume entries.");
    Ensure(fixture.Source.Enumerations == 1, "Directory rename rescanned the MFT.");
    fixture.Source.SetNode(new(21, 0, [new(11, "alpha.txt"), new(5, "new-link.txt")]), 0x10000);
    await Until(() => engine.Search("new-link", 0, 100).TotalMatches == 1);
    Ensure(engine.Search("alpha-link", 0, 100).TotalMatches == 0);
    fixture.Source.DeleteNode(21);
    await Until(() => engine.Search("alpha", 0, 100).TotalMatches == 0);
}
static async Task InvalidJournalRebuild()
{
    using var fixture = new Fixture();
    await using var engine = fixture.Engine();
    await engine.StartAsync();
    await Until(() => engine.GetStatus().ReadyVolumes == 1);
    fixture.Source.ResetJournal(25);
    await Until(() => fixture.Source.Enumerations == 2 && engine.GetStatus().ReadyVolumes == 1);
    Ensure(engine.Search("alpha", 0, 100).TotalMatches == 2);
}
static async Task AccessFilter()
{
    using var fixture = new Fixture();
    await using var engine = fixture.Engine();
    await engine.StartAsync();
    await Until(() => engine.GetStatus().ReadyVolumes == 1);
    var caller = Environment.CurrentManagedThreadId;
    var result = engine.Search("alpha", 0, 100, default, entry =>
    {
        Ensure(Environment.CurrentManagedThreadId == caller, "ACL filter moved off caller impersonation thread.");
        return !entry.FullPath.Contains("link");
    });
    Ensure(result.TotalMatches == 1 && result.Items.Count == 1);
    using var cancelled = new CancellationTokenSource();
    cancelled.Cancel();
    Ensure(engine.TrySearch("alpha", 0, 100, cancelled.Token) is null);
}
static async Task NoJournalFallback()
{
    using var fixture = new Fixture();
    fixture.Source.NoJournal = true;
    await using var engine = fixture.Engine();
    await engine.StartAsync();
    await Until(() => engine.GetStatus().UnavailableRoots.Count == 1);
    Ensure(engine.GetStatus().IndexedRoots.Count == 0 && fixture.Source.Enumerations == 0);
    Ensure(engine.GetStatus().Error is not null);
}
static async Task RebuildWakeCoalesces()
{
    using var fixture = new Fixture();
    await using var engine = fixture.Engine();
    await engine.StartAsync();
    await Until(() => engine.GetStatus().ReadyVolumes == 1);
    await Task.WhenAll(Enumerable.Range(0, 20_000).Select(_ => engine.RebuildAsync()));
    await Until(() => fixture.Source.Enumerations >= 2 && engine.GetStatus().ReadyVolumes == 1);
}
static async Task DamagedVolumeIsolation()
{
    using var fixture = new Fixture();
    var bad = new FakeSource { AfterEnumeration = () => throw new InvalidDataException("Damaged native test data") };
    var healthy = fixture.Volume with { Root = "U:\\", VolumeId = "\\\\?\\Volume{xfetoolbox-unit-test-healthy}\\" };
    await using var engine = new NtfsIndexEngine(fixture.Directory, 4, () => [fixture.Volume, healthy],
        volume => volume.Root == "T:\\" ? bad : fixture.Source);
    await engine.StartAsync();
    await Until(() => engine.GetStatus().ReadyVolumes == 1);
    Ensure(engine.GetStatus().UnavailableRoots.SequenceEqual(new[] { "T:\\" }));
    Ensure(engine.Search("alpha", 0, 100).Items.All(match => match.Entry.FullPath.StartsWith("U:\\")));
}
static async Task SlowMetadataDoesNotBlockSearch()
{
    using var fixture = new Fixture();
    using var release = new ManualResetEventSlim();
    using var entered = new ManualResetEventSlim();
    fixture.Source.AfterEnumeration = () => { entered.Set(); release.Wait(TimeSpan.FromSeconds(3)); };
    await using var engine = fixture.Engine();
    try
    {
        var elapsed = Stopwatch.StartNew();
        await engine.StartAsync();
        Ensure(elapsed.Elapsed < TimeSpan.FromMilliseconds(250), "Start waited for native scanning.");
        Ensure(entered.Wait(TimeSpan.FromSeconds(3)));
        elapsed.Restart();
        engine.Search("alpha", 0, 100);
        Ensure(elapsed.Elapsed < TimeSpan.FromMilliseconds(250), "Search waited for native scanning.");
    }
    finally { release.Set(); }
}
static async Task AuditColdAndWarmStart()
{
    using var fixture = new Fixture();
    await using (var engine = fixture.Engine())
    {
        await engine.StartAsync();
        await Until(() => engine.GetStatus().ReadyVolumes == 1);
    }
    fixture.Source.SetNode(new(22, 0, [new(11, "audit-must-not-log-this-name.txt")]), 0x100);
    await using (var engine = fixture.Engine())
    {
        await engine.StartAsync();
        await Until(() => engine.GetStatus().ReadyVolumes == 1);
    }
    var text = File.ReadAllText(Path.Combine(fixture.Directory, NtfsAuditLog.FileName));
    Ensure(!text.Contains("alpha") && !text.Contains("audit-must-not") && !text.Contains(fixture.Directory),
        "Diagnostics leaked a filename or user path.");
    var documents = text.Split('\n', StringSplitOptions.RemoveEmptyEntries).Select(line => JsonDocument.Parse(line)).ToArray();
    try
    {
        var rows = documents.Select(document => document.RootElement).ToArray();
        var ready = rows.Where(row => row.GetProperty("phase").GetString() == "volume-ready").ToArray();
        Ensure(ready.Length == 2);
        Ensure(ready[0].GetProperty("mftEnumerations").GetInt32() == 1
            && !ready[0].GetProperty("cacheRestored").GetBoolean());
        Ensure(ready[1].GetProperty("mftEnumerations").GetInt32() == 0
            && ready[1].GetProperty("cacheRestored").GetBoolean());
        Ensure(ready[0].GetProperty("runId").GetString() != ready[1].GetProperty("runId").GetString());
        Ensure(rows.Count(row => row.GetProperty("phase").GetString() == "mft-enumerated") == 1);
        Ensure(rows.Any(row => row.GetProperty("phase").GetString() == "usn-replayed"
            && row.GetProperty("mftEnumerations").GetInt32() == 0
            && row.GetProperty("nextUsn").GetInt64() > row.GetProperty("startUsn").GetInt64()));
        Ensure(rows.Any(row => row.GetProperty("phase").GetString() == "cache-restored"
            && row.GetProperty("elapsedMs").GetDouble() >= 0));
    }
    finally { foreach (var document in documents) document.Dispose(); }
    Ensure(NtfsAuditLog.PublicVolumeIdentity(fixture.Volume with { Root = "C:\\Users\\secret\\mount\\" })
        == fixture.Volume.VolumeId, "Mounted-folder root leaked a user path.");
}
static async Task AuditFailureDoesNotStopIndex()
{
    using var fixture = new Fixture();
    var unusableLog = Path.Combine(fixture.Directory, NtfsAuditLog.FileName);
    Directory.CreateDirectory(unusableLog);
    try
    {
        await using var engine = fixture.Engine();
        await engine.StartAsync();
        await Until(() => engine.GetStatus().ReadyVolumes == 1);
        Ensure(engine.Search("alpha", 0, 100).TotalMatches == 2);
    }
    finally { Directory.Delete(unusableLog); }
}
static NtfsCatalog NewCatalog()
{
    var catalog = new NtfsCatalog();
    catalog.Nodes.Add(5, new(5, 0x10, [new(5, ".")]));
    catalog.Nodes.Add(11, new(11, 0x10, [new(5, "work")]));
    catalog.Nodes.Add(21, new(21, 0, [new(11, "alpha.txt"), new(5, "alpha-link.txt")]));
    return catalog;
}
static byte[] BuildLayout(ulong id, uint attributes, (ulong Parent, string Name, uint Flags)[] names)
{
    var size = 56 + names.Sum(name => 24 + Encoding.Unicode.GetByteCount(name.Name));
    var bytes = new byte[size];
    BinaryPrimitives.WriteUInt32LittleEndian(bytes, 1);
    BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(4), 16);
    BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(16), 1);
    BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(28), attributes);
    BinaryPrimitives.WriteUInt64LittleEndian(bytes.AsSpan(32), id);
    BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(40), 40);
    var offset = 56;
    for (var index = 0; index < names.Length; index++)
    {
        var name = names[index];
        var length = Encoding.Unicode.GetByteCount(name.Name);
        BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(offset), index + 1 == names.Length ? 0u : (uint)(24 + length));
        BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(offset + 4), name.Flags);
        BinaryPrimitives.WriteUInt64LittleEndian(bytes.AsSpan(offset + 8), name.Parent);
        BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(offset + 16), (uint)length);
        Encoding.Unicode.GetBytes(name.Name).CopyTo(bytes, offset + 24);
        offset += 24 + length;
    }
    return bytes;
}

sealed class Fixture : IDisposable
{
    public string Directory { get; } = Path.Combine(Path.GetTempPath(), "xfetoolbox-ntfs-unit-" + Guid.NewGuid().ToString("N"));
    public NtfsVolume Volume { get; } = new("T:\\", "\\\\?\\Volume{xfetoolbox-unit-test}\\", 1);
    public FakeSource Source { get; } = new();
    public Fixture() => System.IO.Directory.CreateDirectory(Directory);
    public NtfsIndexEngine Engine() => new(Directory, 4, () => [Volume], _ => Source);
    public void Dispose()
    {
        // Only individually named fixture files are removed, never a recursive deletion.
        foreach (var file in System.IO.Directory.GetFiles(Directory)) File.Delete(file);
        System.IO.Directory.Delete(Directory);
    }
}

sealed class FakeSource : INtfsMetadataSource
{
    private readonly object gate = new();
    private readonly Dictionary<ulong, NtfsNode> nodes = new()
    {
        [5] = new(5, 0x10, [new(5, ".")]),
        [11] = new(11, 0x10, [new(5, "work")]),
        [21] = new(21, 0, [new(11, "alpha.txt"), new(5, "alpha-link.txt")])
    };
    private readonly List<(long Usn, NtfsChange Change)> events = [];
    private ulong journalId = 17;
    private long nextUsn = 100;
    public long NextUsn { get { lock (gate) return nextUsn; } }
    public bool IsOpen => true;
    public string? OpenError => null;
    public int Enumerations;
    public int FilteredReads;
    public bool NoJournal;
    public Action? AfterEnumeration;
    public void SetNode(NtfsNode node, uint reason)
    {
        lock (gate) { nodes[node.FileId] = node; events.Add((nextUsn++, new(node.FileId, reason))); }
    }
    public void DeleteNode(ulong id)
    {
        lock (gate) { nodes.Remove(id); events.Add((nextUsn++, new(id, 0x200))); }
    }
    public void ResetJournal(ulong id) { lock (gate) { journalId = id; events.Clear(); nextUsn += 100; } }
    public bool TryGetJournal(out NtfsJournal journal, out string? error)
    {
        lock (gate) { journal = new(journalId, 0, nextUsn, 0); error = NoJournal ? "No USN journal" : null; return !NoJournal; }
    }
    public bool TryEnumerate(Action<NtfsNode[]> accept, CancellationToken token, out string? error)
    {
        NtfsNode[] snapshot;
        lock (gate) { Enumerations++; snapshot = nodes.Values.ToArray(); }
        accept(snapshot);
        AfterEnumeration?.Invoke();
        error = null;
        return true;
    }
    public bool TryReadNodes(IReadOnlyList<ulong> ids, Action<NtfsNode[]> accept, CancellationToken token, out string? error)
    {
        NtfsNode[] snapshot;
        lock (gate) { FilteredReads++; snapshot = ids.Where(nodes.ContainsKey).Select(id => nodes[id]).ToArray(); }
        accept(snapshot);
        error = null;
        return true;
    }
    public bool TryReadChanges(NtfsJournal journal, long cursor, out long next, out NtfsChange[] changes, out string? error)
    {
        lock (gate) { next = nextUsn; changes = events.Where(item => item.Usn >= cursor).Select(item => item.Change).ToArray(); }
        error = null;
        return true;
    }
    public void Dispose() { }
}