56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using NATS.Server.JetStream.Storage;
|
|
|
|
namespace NATS.Server.Tests;
|
|
|
|
public class JetStreamFileStoreCompressionEncryptionParityTests
|
|
{
|
|
[Fact]
|
|
public async Task Compression_and_encryption_roundtrip_is_versioned_and_detects_wrong_key_corruption()
|
|
{
|
|
var dir = Path.Combine(Path.GetTempPath(), $"nats-js-fs-crypto-{Guid.NewGuid():N}");
|
|
var options = new FileStoreOptions
|
|
{
|
|
Directory = dir,
|
|
EnableCompression = true,
|
|
EnableEncryption = true,
|
|
EncryptionKey = [1, 2, 3, 4],
|
|
};
|
|
|
|
try
|
|
{
|
|
ulong sequence;
|
|
await using (var store = new FileStore(options))
|
|
{
|
|
sequence = await store.AppendAsync("orders.created", Encoding.UTF8.GetBytes("payload"), default);
|
|
var loaded = await store.LoadAsync(sequence, default);
|
|
loaded.ShouldNotBeNull();
|
|
Encoding.UTF8.GetString(loaded.Payload.ToArray()).ShouldBe("payload");
|
|
}
|
|
|
|
var firstLine = File.ReadLines(Path.Combine(dir, "messages.jsonl")).First();
|
|
var payloadBase64 = JsonDocument.Parse(firstLine).RootElement.GetProperty("PayloadBase64").GetString();
|
|
payloadBase64.ShouldNotBeNull();
|
|
var persisted = Convert.FromBase64String(payloadBase64!);
|
|
persisted.Take(4).SequenceEqual("FSV1"u8.ToArray()).ShouldBeTrue();
|
|
|
|
Should.Throw<InvalidDataException>(() =>
|
|
{
|
|
_ = new FileStore(new FileStoreOptions
|
|
{
|
|
Directory = dir,
|
|
EnableCompression = true,
|
|
EnableEncryption = true,
|
|
EncryptionKey = [9, 9, 9, 9],
|
|
});
|
|
});
|
|
}
|
|
finally
|
|
{
|
|
if (Directory.Exists(dir))
|
|
Directory.Delete(dir, recursive: true);
|
|
}
|
|
}
|
|
}
|