Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
225 lines
8.7 KiB
C#
225 lines
8.7 KiB
C#
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;
|
|
|
|
/// <summary>Read-only detection decided by the test.</summary>
|
|
internal sealed class FixedAccessProbe(bool readOnly) : ISourceAccessProbe
|
|
{
|
|
public bool IsReadOnly(string fullPath) => readOnly;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The real file system, except that copies in the library can be damaged right before «Sposta» verifies them, and
|
|
/// deletions of originals are counted.
|
|
/// </summary>
|
|
internal sealed class CorruptingFileSystem(string libraryRoot) : IImportFileSystem
|
|
{
|
|
private readonly string _libraryRoot = Path.GetFullPath(libraryRoot);
|
|
private readonly HashSet<string> _damaged = new(StringComparer.Ordinal);
|
|
|
|
/// <summary>Damage every library file (once: the damage stays) before it is hashed.</summary>
|
|
public bool CorruptCopies { get; set; }
|
|
|
|
public List<string> Deleted { get; } = [];
|
|
|
|
public Stream OpenRead(string fullPath) => ImportFileSystem.Instance.OpenRead(fullPath);
|
|
|
|
public Task<string> 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);
|
|
}
|
|
|
|
/// <summary>Deleting an original fails, as for a file open in another program.</summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>A library with database and services plus a source folder to import from.</summary>
|
|
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; }
|
|
|
|
/// <summary>Full path of the source folder.</summary>
|
|
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<ImportTestBed> CreateAsync(TempFolder temp, bool readOnlySource = false) =>
|
|
new(await TestLibrary.CreateAsync(temp), temp, readOnlySource);
|
|
|
|
/// <summary>Uses another environment (e.g. with the Docker root).</summary>
|
|
public void Use(ImportEnvironment environment) => Environment = environment;
|
|
|
|
/// <summary>Writes a file in the source; the modification time is set when given.</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>Path, size, modification time and SHA-256 of every file of the source: compares «before» and «after».</summary>
|
|
public IReadOnlyList<string> 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();
|
|
|
|
/// <summary>Checks the source, records the import and runs the analysis to the end.</summary>
|
|
public async Task<ImportRun> 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<IReadOnlyList<ImportFile>> FilesAsync(long runId) =>
|
|
await Library.Db.Imports.ListFilesAsync(new Core.Storage.ImportFileQuery { RunId = runId, Limit = 10_000 });
|
|
|
|
public async Task<ImportFile> FileAsync(long runId, string relativePath) =>
|
|
await Library.Db.Imports.FindFileAsync(runId, relativePath) ?? throw new AssertFailedException($"{relativePath} non è nell'importazione.");
|
|
|
|
/// <summary>An account with a photo already in the library (stored as the collection would).</summary>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reading a source file whose path matches <paramref name="blocks"/> waits until the job is cancelled: an import can
|
|
/// be interrupted at a known point.
|
|
/// </summary>
|
|
internal sealed class GatedFileSystem(IImportFileSystem inner, Func<string, bool> blocks) : IImportFileSystem
|
|
{
|
|
/// <summary>Set when the first blocked file is opened.</summary>
|
|
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<string> 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<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
|
|
{
|
|
await Task.Delay(Timeout.Infinite, cancellationToken);
|
|
return 0;
|
|
}
|
|
|
|
public override Task<int> 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);
|
|
}
|
|
}
|
|
}
|