51 lines
2.1 KiB
C#
51 lines
2.1 KiB
C#
using System.Text;
|
|
using Ludarium.Domain;
|
|
using Ludarium.Infrastructure;
|
|
|
|
namespace Ludarium.IntegrationTests;
|
|
|
|
public sealed class GameMediaStoreTests
|
|
{
|
|
[Fact]
|
|
public async Task AppOwnedManualIsBoundedHashedAndDeletable()
|
|
{
|
|
var directory = Path.Combine(Path.GetTempPath(), "ludarium-media-" + Guid.NewGuid().ToString("N"));
|
|
try
|
|
{
|
|
var store = new GameMediaStore(directory);
|
|
var bytes = Encoding.ASCII.GetBytes("%PDF-1.7\n% deterministic synthetic fixture\n");
|
|
var media = await store.SaveAsync(Guid.NewGuid(), Guid.NewGuid(), GameMediaKind.Manual,
|
|
"Synthetic manual", new MemoryStream(bytes), bytes.Length, CancellationToken.None);
|
|
Assert.Equal("application/pdf", media.ContentType);
|
|
Assert.True(media.AppOwned);
|
|
Assert.Equal(64, media.Sha256?.Length);
|
|
var opened = Assert.IsType<StoredGameMedia>(await store.OpenAsync(media, CancellationToken.None));
|
|
await using (opened.Content) Assert.Equal(bytes.Length, opened.Length);
|
|
Assert.True(store.Delete(media));
|
|
Assert.Null(await store.OpenAsync(media, CancellationToken.None));
|
|
}
|
|
finally
|
|
{
|
|
if (Directory.Exists(directory)) Directory.Delete(directory, true);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecutableBytesAreNeverAcceptedAsMedia()
|
|
{
|
|
var directory = Path.Combine(Path.GetTempPath(), "ludarium-media-" + Guid.NewGuid().ToString("N"));
|
|
try
|
|
{
|
|
var store = new GameMediaStore(directory);
|
|
var executable = new byte[] { 0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00 };
|
|
await Assert.ThrowsAsync<MediaValidationException>(() => store.SaveAsync(Guid.NewGuid(), Guid.NewGuid(),
|
|
GameMediaKind.Manual, "Do not execute", new MemoryStream(executable), executable.Length, CancellationToken.None));
|
|
Assert.Empty(Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories));
|
|
}
|
|
finally
|
|
{
|
|
if (Directory.Exists(directory)) Directory.Delete(directory, true);
|
|
}
|
|
}
|
|
}
|