using System.Security.Cryptography; using Febe.Core.Domain; using Febe.Core.Import; using Febe.Core.Library; using Febe.Tests.Support; namespace Febe.Tests.Import.Support; /// Read-only detection decided by the test. internal sealed class FixedAccessProbe(bool readOnly) : ISourceAccessProbe { public bool IsReadOnly(string fullPath) => readOnly; } /// /// The real file system, except that copies in the library can be damaged right before «Sposta» verifies them, and /// deletions of originals are counted. /// internal sealed class CorruptingFileSystem(string libraryRoot) : IImportFileSystem { private readonly string _libraryRoot = Path.GetFullPath(libraryRoot); private readonly HashSet _damaged = new(StringComparer.Ordinal); /// Damage every library file (once: the damage stays) before it is hashed. public bool CorruptCopies { get; set; } public List Deleted { get; } = []; public Stream OpenRead(string fullPath) => ImportFileSystem.Instance.OpenRead(fullPath); public Task ComputeSha256Async(string fullPath, CancellationToken ct) { var full = Path.GetFullPath(fullPath); if (CorruptCopies && full.StartsWith(_libraryRoot, StringComparison.Ordinal) && _damaged.Add(full)) { var bytes = File.ReadAllBytes(full); bytes[^1] ^= 0xFF; File.WriteAllBytes(full, bytes); } return ImportFileSystem.Instance.ComputeSha256Async(fullPath, ct); } /// Deleting an original fails, as for a file open in another program. public bool FailDeletes { get; set; } public void DeleteOriginal(string fullPath) { if (FailDeletes) { throw new IOException("File in uso."); } Deleted.Add(fullPath); ImportFileSystem.Instance.DeleteOriginal(fullPath); } } /// A library with database and services plus a source folder to import from. internal sealed class ImportTestBed : IAsyncDisposable { private ImportTestBed(TestLibrary library, TempFolder temp, bool readOnlySource) { Library = library; Temp = temp; Source = temp.Combine("sorgente"); Directory.CreateDirectory(Source); FileSystem = new CorruptingFileSystem(library.Layout.Root); Environment = new ImportEnvironment { Storage = library.Db, Layout = library.Layout, Journal = library.Journal, Clock = library.Clock, Thumbnails = library.Thumbnails, HashIndex = library.Index, DataFolder = library.Data.Root, AccessProbe = new FixedAccessProbe(readOnlySource), FileSystem = FileSystem, Parallelism = 2, }; } public TestLibrary Library { get; } public TempFolder Temp { get; } /// Full path of the source folder. public string Source { get; } public CorruptingFileSystem FileSystem { get; } public ImportEnvironment Environment { get; private set; } public ImportAnalyzer Analyzer => new(Environment); public ImportCopier Copier => new(Environment); public ImportReview Review => new(Environment); public ImportReports Reports => new(Environment); public static async Task CreateAsync(TempFolder temp, bool readOnlySource = false) => new(await TestLibrary.CreateAsync(temp), temp, readOnlySource); /// Uses another environment (e.g. with the Docker root). public void Use(ImportEnvironment environment) => Environment = environment; /// Writes a file in the source; the modification time is set when given. public string Write(string relativePath, byte[] bytes, DateTime? modifiedUtc = null) { var full = Path.Combine(Source, relativePath.Replace('/', Path.DirectorySeparatorChar)); Directory.CreateDirectory(Path.GetDirectoryName(full)!); File.WriteAllBytes(full, bytes); File.SetLastWriteTimeUtc(full, modifiedUtc ?? new DateTime(2020, 1, 2, 3, 4, 5, DateTimeKind.Utc)); return full; } /// Path, size, modification time and SHA-256 of every file of the source: compares «before» and «after». public IReadOnlyList Fingerprint() => Directory.EnumerateFiles(Source, "*", SearchOption.AllDirectories) .Order(StringComparer.Ordinal) .Select(f => { var info = new FileInfo(f); return $"{Path.GetRelativePath(Source, f)}|{info.Length}|{info.LastWriteTimeUtc:O}|{Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(f)))}"; }) .ToList(); /// Checks the source, records the import and runs the analysis to the end. public async Task AnalyzeAsync(ImportAnalysisOptions? options = null) { var info = Environment.CreateSources().Inspect(Source); var run = await Analyzer.CreateRunAsync(info, options); return await Analyzer.AnalyzeAsync(run.Id); } public async Task> FilesAsync(long runId) => await Library.Db.Imports.ListFilesAsync(new Core.Storage.ImportFileQuery { RunId = runId, Limit = 10_000 }); public async Task FileAsync(long runId, string relativePath) => await Library.Db.Imports.FindFileAsync(runId, relativePath) ?? throw new AssertFailedException($"{relativePath} non è nell'importazione."); /// An account with a photo already in the library (stored as the collection would). public async Task<(Account Account, EntryWithMedia Saved)> AccountWithPhotoAsync(string username, string instagramId, byte[] photo, string? code = null, DateTimeOffset? publishedAt = null) { var account = await Library.Db.Accounts.FindByUsernameAsync(username) ?? await Library.Db.Accounts.AddAsync(Make.Account(username)); var entry = Make.Entry(account.Id, instagramId, publishedAt ?? Make.T0.AddDays(-30)) with { Code = code ?? "C" + instagramId }; var saved = await Library.SaveAsync(account, entry, [new TestFile(Make.Photo(1), photo, ".jpg")]); return (account, saved); } public ValueTask DisposeAsync() => Library.DisposeAsync(); } /// /// Reading a source file whose path matches waits until the job is cancelled: an import can /// be interrupted at a known point. /// internal sealed class GatedFileSystem(IImportFileSystem inner, Func blocks) : IImportFileSystem { /// Set when the first blocked file is opened. public TaskCompletionSource Reached { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); public Stream OpenRead(string fullPath) { var stream = inner.OpenRead(fullPath); if (!blocks(fullPath)) { return stream; } Reached.TrySetResult(); return new BlockedStream(stream); } public Task ComputeSha256Async(string fullPath, CancellationToken ct) => inner.ComputeSha256Async(fullPath, ct); public void DeleteOriginal(string fullPath) => inner.DeleteOriginal(fullPath); private sealed class BlockedStream(Stream inner) : Stream { public override bool CanRead => true; public override bool CanSeek => false; public override bool CanWrite => false; public override long Length => inner.Length; public override long Position { get => inner.Position; set => throw new NotSupportedException(); } public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) { await Task.Delay(Timeout.Infinite, cancellationToken); return 0; } public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); public override void Flush() { } public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); public override void SetLength(long value) => throw new NotSupportedException(); public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); protected override void Dispose(bool disposing) { if (disposing) { inner.Dispose(); } base.Dispose(disposing); } } }