#region ========================================================================= USING ===================================================================================== using Nemera.Application.Common.Infrastructure.Models.DTO.MediaLibraryScanJobPayloads; using Nemera.Application.Fixtures.Common.Infrastructure.Models.DTO.MediaLibraryScanJobPayloads; using Nemera.Infrastructure.Core.Security; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.IO.Hashing; using System.Linq; using System.Threading; using System.Threading.Tasks; #endregion namespace Nemera.Infrastructure.IntegrationTests.Core.Security; /// /// Contains integration tests for the class. /// [ExcludeFromCodeCoverage] public class FileHashServiceTests { private readonly FileHashService _sut = new(); private readonly HashedFileSystemFileDtoFixture _hashedFileSystemFileDtoFixture = new(); [Fact] public async Task HashFilesAsync_WhenCalledWithFiles_ShouldInvokeCallbackAndComputeHashes() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string filePath1 = temporaryTestDirectory.CreateFile("book1.pdf", "content of the first book"); string filePath2 = temporaryTestDirectory.CreateFile("book2.pdf", "content of the second book"); List inputFiles = [ _hashedFileSystemFileDtoFixture.Create(path: filePath1, size: new FileInfo(filePath1).Length, currentHash: 0, oldHash: 0, ticks: 0), _hashedFileSystemFileDtoFixture.Create(path: filePath2, size: new FileInfo(filePath2).Length, currentHash: 0, oldHash: 0, ticks: 0) ]; int callbackInvocationCount = 0; // Act List result = await _sut.HashFilesAsync( inputFiles, shouldParallelizeReads: true, () => { // The callback is invoked concurrently by the parallel reads, so a plain increment could lose a count. Interlocked.Increment(ref callbackInvocationCount); return Task.CompletedTask; }, CancellationToken.None); // Assert Assert.Equal(2, result.Count); Assert.Equal(2, callbackInvocationCount); Assert.All(result, file => Assert.NotEqual(0UL, file.CurrentHash)); // The hashing is parallelized, so the results are not guaranteed to be in the input order. Assert.Equal( inputFiles.Select(file => file.Path).OrderBy(path => path, StringComparer.Ordinal), result.Select(file => file.Path).OrderBy(path => path, StringComparer.Ordinal)); } [Fact] public async Task HashFilesAsync_WhenFileIsEmpty_ShouldUseEmptyFileHash() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string filePath = temporaryTestDirectory.CreateFile("empty.pdf", string.Empty); List inputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)]; // Act List result = await _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None); // Assert HashedFileSystemFileDto hashedFile = Assert.Single(result); Assert.Equal(filePath, hashedFile.Path); Assert.Equal(BitConverter.ToUInt64(new XxHash64().GetCurrentHash()), hashedFile.CurrentHash); } [Fact] public async Task HashFilesAsync_WhenFileContentChanges_ShouldProduceDifferentHashes() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string filePath = temporaryTestDirectory.CreateFile("book.pdf", "original content"); List firstInputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)]; // Act List firstResult = await _sut.HashFilesAsync(firstInputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None); await File.WriteAllTextAsync(filePath, "changed content"); List secondInputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)]; List secondResult = await _sut.HashFilesAsync(secondInputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None); // Assert Assert.NotEqual(firstResult[0].CurrentHash, secondResult[0].CurrentHash); } [Fact] public async Task HashFilesAsync_WhenFileDoesNotExist_ShouldSkipFileAndNotThrow() { // Arrange string nonExistentFilePath = Path.Combine(Path.GetTempPath(), "does-not-exist.pdf"); List inputFiles = [_hashedFileSystemFileDtoFixture.Create(path: nonExistentFilePath, size: 100, currentHash: 0, oldHash: 0, ticks: 0)]; // Act List result = await _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None); // Assert Assert.Empty(result); } [Fact] public async Task HashFilesAsync_WhenOneFileIsMissing_ShouldHashTheExistingFilesOnly() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string existingFilePath = temporaryTestDirectory.CreateFile("book.pdf", "content"); List inputFiles = [ _hashedFileSystemFileDtoFixture.Create(path: existingFilePath, size: new FileInfo(existingFilePath).Length, currentHash: 0, oldHash: 0, ticks: 0), _hashedFileSystemFileDtoFixture.Create(path: Path.Combine(temporaryTestDirectory.Path, "missing.pdf"), size: 100, currentHash: 0, oldHash: 0, ticks: 0) ]; // Act List result = await _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None); // Assert HashedFileSystemFileDto hashedFile = Assert.Single(result); Assert.Equal(existingFilePath, hashedFile.Path); Assert.NotEqual(0UL, hashedFile.CurrentHash); } [Fact] public async Task HashFilesAsync_WhenReadsAreNotParallelized_ShouldHashAllFilesSequentially() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string filePath1 = temporaryTestDirectory.CreateFile("book1.pdf", "content of the first book"); string filePath2 = temporaryTestDirectory.CreateFile("book2.pdf", "content of the second book"); List inputFiles = [ _hashedFileSystemFileDtoFixture.Create(path: filePath1, size: new FileInfo(filePath1).Length, currentHash: 0, oldHash: 0, ticks: 0), _hashedFileSystemFileDtoFixture.Create(path: filePath2, size: new FileInfo(filePath2).Length, currentHash: 0, oldHash: 0, ticks: 0) ]; int callbackInvocationCount = 0; // Act List result = await _sut.HashFilesAsync( inputFiles, shouldParallelizeReads: false, () => { callbackInvocationCount++; return Task.CompletedTask; }, CancellationToken.None); // Assert Assert.Equal(2, result.Count); Assert.Equal(2, callbackInvocationCount); Assert.All(result, file => Assert.NotEqual(0UL, file.CurrentHash)); } [Fact] public async Task HashFilesAsync_WhenCancellationRequested_ShouldCancelOperation() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string filePath = temporaryTestDirectory.CreateFile("book.pdf", "content"); List inputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)]; using CancellationTokenSource cancellationTokenSource = new(); cancellationTokenSource.Cancel(); // Act Task> operation = _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, cancellationTokenSource.Token); // Assert await Assert.ThrowsAnyAsync(() => operation); } [Fact] public void ComputeFileHash_WhenFileIsEmpty_ShouldReturnEmptyFileHash() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string filePath = temporaryTestDirectory.CreateFile("empty.pdf", string.Empty); // Act ulong hash = _sut.ComputeFileHash(filePath); // Assert Assert.Equal(BitConverter.ToUInt64(new XxHash64().GetCurrentHash()), hash); } [Fact] public void ComputeFileHash_WhenFileHasContent_ShouldReturnNonZeroHash() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string filePath = temporaryTestDirectory.CreateFile("book.pdf", "content of the book"); // Act ulong hash = _sut.ComputeFileHash(filePath); // Assert Assert.NotEqual(0UL, hash); } [Fact] public async Task HashFilesAsync_WhenFileIsLargerThanTheSamplingThreshold_ShouldSampleTheWholeFile() { // Arrange using TemporaryTestDirectory temporaryTestDirectory = new(); string filePath = Path.Combine(temporaryTestDirectory.Path, "large.pdf"); File.WriteAllBytes(filePath, new byte[500_000]); List inputFiles = [_hashedFileSystemFileDtoFixture.Create(path: filePath, size: new FileInfo(filePath).Length, currentHash: 0, oldHash: 0, ticks: 0)]; // Act List result = await _sut.HashFilesAsync(inputFiles, shouldParallelizeReads: true, () => Task.CompletedTask, CancellationToken.None); // Assert HashedFileSystemFileDto hashedFile = Assert.Single(result); Assert.Equal(filePath, hashedFile.Path); Assert.NotEqual(0UL, hashedFile.CurrentHash); } /// /// Test helper managing a temporary directory, deleted when the test finishes. /// private sealed class TemporaryTestDirectory : IDisposable { private bool _disposed; public TemporaryTestDirectory() { Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"nemera-file-hash-tests-{Guid.NewGuid()}"); Directory.CreateDirectory(Path); } public string Path { get; } public string CreateFile(string fileName, string content) { string filePath = System.IO.Path.Combine(Path, fileName); File.WriteAllText(filePath, content); return filePath; } public void Dispose() { if (!_disposed) { try { Directory.Delete(Path, recursive: true); } catch (IOException) { // Best effort cleanup of the temporary test directory. } _disposed = true; } } } }