Documents need attachments and generated files (SPEC 9.4) before the UI of M3 can show them, and the spec forbids external libraries for PDF and spreadsheets. - VolumeFileStore keeps files under <data>/files/<COMPANY>/<yyyy>/<guid>, writing a .part file in the same folder with size limit and SHA-256, then quota and scanner checks (IFileScanner hook, no-op by default). - attachments table (migration 007) with kind, version series, lock, trash and the uploading user; AttachmentService uploads, adds generated files as the next version, lists, opens, zips, locks, trashes, restores and purges after 30 days (new system job system.attachments-trash). - FileGenerationService runs IFileGenerator implementations into the store; three generators for the fictitious test document (PDF, CSV, XLSX). - CsvWriter (Italian Excel defaults, formula-injection guard), XlsxWriter, ZipStreamWriter and PdfWriter (standard Type1 fonts, WinAnsi, AFM widths, JPEG, Flate, xref) only write asynchronously, so they can stream to an HTTP response. - ADR 0021 records versioning, storage layout, PDF scope and CSV defaults. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
6.2 KiB
C#
146 lines
6.2 KiB
C#
using System.Security.Cryptography;
|
|
using Khaemhat.Core.Files;
|
|
using Khaemhat.Infrastructure.Files;
|
|
|
|
namespace Khaemhat.Infrastructure.Tests.Files;
|
|
|
|
/// <summary>The file store on the data volume (SPEC §9.4, issue #34).</summary>
|
|
[TestClass]
|
|
public sealed class VolumeFileStoreTests
|
|
{
|
|
private const string Company = "ACME";
|
|
private string _root = string.Empty;
|
|
|
|
[TestInitialize]
|
|
public void CreateRoot() => _root = Path.Combine(Path.GetTempPath(), "khaemhat-tests", "files-" + Guid.NewGuid().ToString("N"));
|
|
|
|
[TestCleanup]
|
|
public void DeleteRoot()
|
|
{
|
|
if (Directory.Exists(_root))
|
|
{
|
|
Directory.Delete(_root, recursive: true);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task Save_StoresUnderCompanyAndYear_WithSizeAndSha256()
|
|
{
|
|
var store = NewStore();
|
|
var content = RandomNumberGenerator.GetBytes(200_000);
|
|
|
|
var stored = await store.SaveAsync("acme", new MemoryStream(content));
|
|
|
|
StringAssert.Matches(stored.Key, new System.Text.RegularExpressions.Regex("^2026/[0-9a-f]{32}$"));
|
|
Assert.AreEqual(content.Length, stored.Size);
|
|
Assert.AreEqual(Convert.ToHexStringLower(SHA256.HashData(content)), stored.Sha256);
|
|
Assert.IsTrue(File.Exists(Path.Combine(_root, Company, "2026", stored.Key[5..])));
|
|
Assert.IsEmpty(Directory.GetFiles(_root, "*.part", SearchOption.AllDirectories));
|
|
|
|
await using (var read = store.OpenRead(Company, stored.Key))
|
|
{
|
|
using var copy = new MemoryStream();
|
|
await read.CopyToAsync(copy);
|
|
CollectionAssert.AreEqual(content, copy.ToArray());
|
|
}
|
|
|
|
var usage = await store.GetUsageAsync(Company);
|
|
Assert.AreEqual((1L, (long)content.Length), (usage.Files, usage.Bytes));
|
|
Assert.IsTrue(store.Delete(Company, stored.Key));
|
|
Assert.IsFalse(store.Delete(Company, stored.Key));
|
|
Assert.ThrowsExactly<FileStoreException>(() => store.OpenRead(Company, stored.Key));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TooLarge_IsRefused_AndLeavesNothingBehind()
|
|
{
|
|
var store = NewStore(new FileStoreOptions(_root) { MaxFileBytes = 1000 });
|
|
|
|
var error = await Assert.ThrowsExactlyAsync<FileStoreException>(() => store.SaveAsync(Company, new MemoryStream(new byte[1001])));
|
|
|
|
Assert.AreEqual(FileStoreException.TooLarge, error.Code);
|
|
Assert.IsEmpty(Directory.GetFiles(_root, "*", SearchOption.AllDirectories));
|
|
Assert.AreEqual(1000, (await store.SaveAsync(Company, new MemoryStream(new byte[1000]))).Size);
|
|
Assert.AreEqual(5000, (await store.SaveAsync(Company, new MemoryStream(new byte[5000]), FileLimits.None)).Size);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task Quota_IsCheckedPerCompany()
|
|
{
|
|
var store = NewStore();
|
|
var limits = new FileLimits(0, 1500);
|
|
await store.SaveAsync(Company, new MemoryStream(new byte[1000]), limits);
|
|
|
|
var error = await Assert.ThrowsExactlyAsync<FileStoreException>(() => store.SaveAsync(Company, new MemoryStream(new byte[600]), limits));
|
|
|
|
Assert.AreEqual(FileStoreException.QuotaExceeded, error.Code);
|
|
await store.SaveAsync("OTHER", new MemoryStream(new byte[600]), limits);
|
|
var usage = await store.GetUsageAsync(Company, 1500);
|
|
Assert.AreEqual(500, usage.RemainingBytes);
|
|
Assert.IsFalse(usage.IsOverQuota);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task Scanner_CanRejectAFile()
|
|
{
|
|
var store = new VolumeFileStore(new FileStoreOptions(_root), new RejectingScanner(), new FixedClock());
|
|
|
|
var error = await Assert.ThrowsExactlyAsync<FileStoreException>(() => store.SaveAsync(Company, new MemoryStream("X5O!EICAR"u8.ToArray())));
|
|
|
|
Assert.AreEqual(FileStoreException.Rejected, error.Code);
|
|
StringAssert.Contains(error.Message, "EICAR");
|
|
Assert.IsEmpty(Directory.GetFiles(_root, "*", SearchOption.AllDirectories));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task Writer_NotCommitted_IsDiscarded_AndKeysCannotEscapeTheCompanyFolder()
|
|
{
|
|
var store = NewStore();
|
|
await using (var writer = await store.CreateAsync(Company))
|
|
{
|
|
await writer.Content.WriteAsync(new byte[10]);
|
|
Assert.HasCount(1, Directory.GetFiles(_root, "*.part", SearchOption.AllDirectories));
|
|
}
|
|
|
|
Assert.IsEmpty(Directory.GetFiles(_root, "*", SearchOption.AllDirectories));
|
|
Assert.ThrowsExactly<ArgumentException>(() => store.OpenRead(Company, "../OTHER/2026/" + new string('a', 32)));
|
|
Assert.ThrowsExactly<ArgumentException>(() => store.Exists(Company, "2026/x"));
|
|
Assert.ThrowsExactly<ArgumentException>(() => store.OpenRead("../etc", "2026/" + new string('a', 32)));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AbandonedParts_AreRemoved()
|
|
{
|
|
var store = NewStore();
|
|
var folder = Directory.CreateDirectory(Path.Combine(_root, Company, "2026")).FullName;
|
|
var old = Path.Combine(folder, Guid.NewGuid().ToString("N") + ".part");
|
|
File.WriteAllBytes(old, [1, 2, 3]);
|
|
File.SetLastWriteTimeUtc(old, new DateTime(2026, 11, 1, 0, 0, 0, DateTimeKind.Utc));
|
|
var recent = Path.Combine(folder, Guid.NewGuid().ToString("N") + ".part");
|
|
File.WriteAllBytes(recent, [1]);
|
|
File.SetLastWriteTimeUtc(recent, new DateTime(2026, 11, 11, 9, 0, 0, DateTimeKind.Utc));
|
|
|
|
Assert.AreEqual(1, store.DeleteAbandonedParts(Company, TimeSpan.FromDays(1)));
|
|
Assert.IsFalse(File.Exists(old));
|
|
Assert.IsTrue(File.Exists(recent));
|
|
}
|
|
|
|
private VolumeFileStore NewStore(FileStoreOptions? options = null) =>
|
|
new(options ?? new FileStoreOptions(_root), new NoOpFileScanner(), new FixedClock());
|
|
|
|
private sealed class FixedClock : TimeProvider
|
|
{
|
|
public override DateTimeOffset GetUtcNow() => new(2026, 11, 11, 10, 0, 0, TimeSpan.Zero);
|
|
}
|
|
|
|
private sealed class RejectingScanner : IFileScanner
|
|
{
|
|
public async ValueTask<FileScanResult> ScanAsync(string companyCode, Stream content, CancellationToken cancellationToken = default)
|
|
{
|
|
using var reader = new StreamReader(content);
|
|
var text = await reader.ReadToEndAsync(cancellationToken);
|
|
return text.Contains("EICAR", StringComparison.Ordinal) ? new FileScanResult(false, "EICAR test signature") : FileScanResult.Ok;
|
|
}
|
|
}
|
|
}
|