using System.Text; using Ludarium.Application; using Ludarium.Infrastructure; namespace Ludarium.IntegrationTests; public sealed class FirmwareVaultFileStoreTests : IDisposable { private readonly string root = Path.Combine(Path.GetTempPath(), $"ludarium-firmware-{Guid.NewGuid():N}"); [Fact] public async Task AppOwnedUploadIsHashedVerifiedAndDeletable() { var store = new FirmwareVaultFileStore(root); var bytes = Enumerable.Range(0, 512).Select(value => (byte)(value % 251)).ToArray(); var asset = await store.SaveAsync(FirmwarePolicy.Get("psx", "bios"), Guid.NewGuid(), "personal.bin", new MemoryStream(bytes), bytes.Length, CancellationToken.None); Assert.Equal(bytes.Length, asset.Length); Assert.Equal(64, asset.Sha256.Length); await using (var opened = (await store.OpenAsync(asset, CancellationToken.None))!.Content) { using var copy = new MemoryStream(); await opened.CopyToAsync(copy); Assert.Equal(bytes, copy.ToArray()); } Assert.True(store.Delete(asset)); Assert.Null(await store.OpenAsync(asset, CancellationToken.None)); } [Theory] [InlineData("MZhost executable")] [InlineData("#!/bin/sh")] [InlineData("PK\u0003\u0004archive")] public async Task HostExecutablesScriptsAndArchivesFailClosed(string text) { var store = new FirmwareVaultFileStore(root); await Assert.ThrowsAsync(() => store.SaveAsync( FirmwarePolicy.Get("psx", "bios"), Guid.NewGuid(), "personal.bin", new MemoryStream(Encoding.UTF8.GetBytes(text)), null, CancellationToken.None)); } public void Dispose() { if (Directory.Exists(root)) Directory.Delete(root, true); } }