Files
poli0981andClaude Opus 5.5 c956930186 refactor(app): rebuild the UI layer on DI, the Core engine and Avalonia 12
- Composition root (AppHost) with Microsoft.Extensions.DependencyInjection;
  view models receive dialog, storage, clipboard and launcher services.
- Logging: daily rolling log file (invariant timestamps, user paths masked,
  14-day retention), in-memory sink for the Developer console, Avalonia
  warnings forwarded, synchronous crash reports.
- Settings: one SettingsService over SettingsStore (atomic debounced saves
  taken under a lock, corrupt-file recovery, 1.x font settings migration).
- Create/Verify run on BatchProcessor and HashEngine: dropping folders no
  longer crashes, imports run off the UI thread in batches, rows can be
  started/stopped individually, UI updates are pumped every 100 ms, and
  statuses are stored as enums and translated when displayed.
- Checksum files: export GNU/BSD/SFV/JSON/CSV manifests, ZIP archives with
  unique entry names plus a manifest, verify any manifest or sidecar file
  (UNC and device paths are rejected).
- OS integration: --create/--verify arguments, single instance with argument
  forwarding, optional Explorer context menu (per-user, removed on
  uninstall), macOS file activation, on-demand restart as administrator.
- Updates: stable channel only, optional daily background check,
  cancellable download, install now or on exit.
- Removed: Free Memory, Force Quit and its timeout, automatic elevation,
  developer update channel and disclaimer window, ViewLocator, unused
  converters.
- Fixed: search highlight duplicating the end of file names, single-letter
  shortcuts firing while typing, culture reverting after language changes,
  colour-blind and high-contrast palettes not reaching status colours.
- Headless --smoke-test mode for CI and the release upgrade gate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 13:16:40 +07:00

122 lines
5.0 KiB
C#

using System;
using System.IO;
using System.IO.Hashing;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HashTool.Core.Hashing;
using Xunit;
namespace HashTool.Core.Tests.Hashing;
/// <summary>Published test vectors for every algorithm, computed through files on disk.</summary>
public sealed class KnownAnswerTests : IDisposable
{
private readonly DirectoryInfo _dir = Directory.CreateTempSubdirectory("hashtool-tests-");
private readonly HashEngine _engine = new(new HashEngineOptions { BufferSize = 4096 });
public void Dispose() => _dir.Delete(recursive: true);
private string WriteFile(byte[] content, string name = "input.bin")
{
var path = Path.Combine(_dir.FullName, name);
File.WriteAllBytes(path, content);
return path;
}
[Theory]
[InlineData(HashType.MD5, "", "D41D8CD98F00B204E9800998ECF8427E")]
[InlineData(HashType.MD5, "abc", "900150983CD24FB0D6963F7D28E17F72")]
[InlineData(HashType.SHA1, "abc", "A9993E364706816ABA3E25717850C26C9CD0D89D")]
[InlineData(HashType.SHA256, "", "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855")]
[InlineData(HashType.SHA256, "abc", "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD")]
[InlineData(HashType.SHA384, "abc",
"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7")]
[InlineData(HashType.SHA512, "abc",
"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F")]
[InlineData(HashType.BLAKE3, "", "AF1349B9F5F9A1A6A0404DEA36DCC9499BCB25C9ADC112B7CC9A93CAE41F3262")]
[InlineData(HashType.BLAKE3, "abc", "6437B3AC38465133FFB63B75273A8DB548C558465D79DB03FD359C6CD5BD9D85")]
[InlineData(HashType.XxHash32, "abc", "32D153FF")]
[InlineData(HashType.XxHash64, "", "EF46DB3751D8E999")]
[InlineData(HashType.XxHash64, "abc", "44BC2CF5AD770999")]
[InlineData(HashType.XxHash3, "abc", "78AF5F94892F3950")]
[InlineData(HashType.XxHash128, "abc", "06B05AB6733A618578AF5F94892F3950")]
public async Task ComputesKnownAnswerVectors(HashType type, string input, string expected)
{
var path = WriteFile(Encoding.ASCII.GetBytes(input));
var actual = (await _engine.ComputeFileAsync(path, type, cancellationToken: TestContext.Current.CancellationToken)).ToHex();
Assert.Equal(expected, actual);
}
[Fact]
public async Task Crc32_UsesConventionalByteOrder()
{
// 1.1.x printed the little-endian bytes (2639F4CB); every other tool prints the CRC check value CBF43926.
var path = WriteFile("123456789"u8.ToArray());
var actual = (await _engine.ComputeFileAsync(path, HashType.CRC32, cancellationToken: TestContext.Current.CancellationToken)).ToHex();
Assert.Equal("CBF43926", actual);
}
[Theory]
[InlineData(HashType.SHA256, 4096)]
[InlineData(HashType.XxHash64, 4096)]
[InlineData(HashType.BLAKE3, 4096)]
[InlineData(HashType.SHA256, null)]
public async Task LargeFile_MatchesOneShotHash(HashType type, int? bufferSize)
{
var content = new byte[3 * 1024 * 1024 + 123];
new Random(42).NextBytes(content);
var path = WriteFile(content);
var engine = bufferSize is { } size ? new HashEngine(new HashEngineOptions { BufferSize = size }) : new HashEngine();
var actual = (await engine.ComputeFileAsync(path, type, cancellationToken: TestContext.Current.CancellationToken)).ToHex();
var expected = type switch
{
HashType.SHA256 => Convert.ToHexString(SHA256.HashData(content)),
HashType.XxHash64 => Convert.ToHexString(XxHash64.Hash(content)),
HashType.BLAKE3 => Convert.ToHexString(Blake3.Hasher.Hash(content).AsSpan()),
_ => throw new ArgumentOutOfRangeException(nameof(type))
};
Assert.Equal(expected, actual);
}
[Fact]
public async Task ReportsProgressForEveryByte()
{
var content = new byte[1_000_000];
var path = WriteFile(content);
long total = 0;
await _engine.ComputeFileAsync(path, HashType.SHA256, read => Interlocked.Add(ref total, read),
TestContext.Current.CancellationToken);
Assert.Equal(content.Length, total);
}
[Fact]
public async Task CancelledToken_Throws()
{
var path = WriteFile(new byte[1024]);
using var cts = new CancellationTokenSource();
await cts.CancelAsync();
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => _engine.ComputeFileAsync(path, HashType.SHA256, cancellationToken: cts.Token));
}
[Fact]
public async Task MissingFile_ThrowsFileNotFound()
{
var path = Path.Combine(_dir.FullName, "missing.bin");
await Assert.ThrowsAsync<FileNotFoundException>(
() => _engine.ComputeFileAsync(path, HashType.SHA256, cancellationToken: TestContext.Current.CancellationToken));
}
}