166 lines
7.4 KiB
C#
166 lines
7.4 KiB
C#
using System.IO.Compression;
|
|
using System.Security.Cryptography;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
|
|
using ZB.MOM.WW.ScadaBridge.Transport.Encryption;
|
|
using ZB.MOM.WW.ScadaBridge.Transport.Serialization;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Transport.Tests.Serialization;
|
|
|
|
public sealed class BundleSerializerTests
|
|
{
|
|
// The documented PBKDF2 floor is 100_000 (OWASP minimum).
|
|
// Using the floor keeps the suite fast while passing EncryptionMetadata
|
|
// validation.
|
|
private const int TestIterations = EncryptionMetadata.MinPbkdf2Iterations;
|
|
|
|
private static BundleContentDto SampleContent() => new(
|
|
TemplateFolders: new[] { new TemplateFolderDto("Root", ParentName: null, SortOrder: 0) },
|
|
Templates: Array.Empty<TemplateDto>(),
|
|
SharedScripts: new[] { new SharedScriptDto("util", "return 42;", ParameterDefinitions: null, ReturnDefinition: null) },
|
|
ExternalSystems: Array.Empty<ExternalSystemDto>(),
|
|
DatabaseConnections: Array.Empty<DatabaseConnectionDto>(),
|
|
NotificationLists: Array.Empty<NotificationListDto>(),
|
|
SmtpConfigs: Array.Empty<SmtpConfigDto>(),
|
|
ApiMethods: Array.Empty<ApiMethodDto>());
|
|
|
|
private static BundleManifest BuildManifestFor(byte[] contentBytes, EncryptionMetadata? encryption = null) =>
|
|
new ManifestBuilder().Build(
|
|
sourceEnvironment: "test-env",
|
|
exportedBy: "tester",
|
|
scadaBridgeVersion: "1.0.0",
|
|
encryption: encryption,
|
|
summary: new BundleSummary(0, 1, 1, 0, 0, 0, 0, 0),
|
|
contents: Array.Empty<ManifestContentEntry>(),
|
|
contentBytes: contentBytes);
|
|
|
|
/// <summary>Verifies that packing without a passphrase emits a manifest and plain JSON content.</summary>
|
|
[Fact]
|
|
public void Pack_emits_manifest_and_content_json_when_no_passphrase()
|
|
{
|
|
var sut = new BundleSerializer();
|
|
var content = SampleContent();
|
|
var contentJson = sut.SerializeContentBytes(content);
|
|
var manifest = BuildManifestFor(contentJson);
|
|
|
|
using var stream = sut.Pack(content, manifest, passphrase: null, encryptor: null);
|
|
|
|
using var archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true);
|
|
Assert.NotNull(archive.GetEntry("manifest.json"));
|
|
Assert.NotNull(archive.GetEntry("content.json"));
|
|
Assert.Null(archive.GetEntry("content.enc"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Review 2026-10-21 04/P8a: the exporter serialises the content DTO once and packs
|
|
/// those bytes — the pre-serialised overload writes them verbatim as the plaintext
|
|
/// payload and they round-trip, encrypted or not.
|
|
/// </summary>
|
|
/// <param name="passphrase">Null for a plaintext bundle, otherwise the passphrase.</param>
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("pass")]
|
|
public void PackContentBytes_packs_preserialised_content_verbatim(string? passphrase)
|
|
{
|
|
var sut = new BundleSerializer();
|
|
var encryptor = new BundleSecretEncryptor();
|
|
var contentJson = sut.SerializeContentBytes(SampleContent());
|
|
var manifest = passphrase is null
|
|
? BuildManifestFor(contentJson)
|
|
: BuildManifestFor(contentJson, encryption: new EncryptionMetadata(
|
|
"AES-256-GCM", "PBKDF2-SHA256", TestIterations, string.Empty, string.Empty));
|
|
|
|
using var stream = sut.PackContentBytes(contentJson, manifest, passphrase, encryptor);
|
|
|
|
var readManifest = sut.ReadManifest(stream);
|
|
stream.Position = 0;
|
|
var payload = sut.ReadContentBytes(stream, readManifest);
|
|
if (passphrase is null)
|
|
{
|
|
Assert.Equal(contentJson, payload);
|
|
}
|
|
var unpacked = sut.UnpackContent(payload, readManifest, passphrase, encryptor);
|
|
Assert.Equal("util", Assert.Single(unpacked.SharedScripts).Name);
|
|
}
|
|
|
|
/// <summary>Verifies that packing with a passphrase emits a manifest and encrypted content.</summary>
|
|
[Fact]
|
|
public void Pack_emits_manifest_and_content_enc_when_passphrase_supplied()
|
|
{
|
|
var sut = new BundleSerializer();
|
|
var encryptor = new BundleSecretEncryptor();
|
|
var content = SampleContent();
|
|
var contentJson = sut.SerializeContentBytes(content);
|
|
var (cipher, meta) = encryptor.Encrypt(contentJson, "pass", TestIterations);
|
|
var manifest = BuildManifestFor(cipher, encryption: meta);
|
|
|
|
using var stream = sut.Pack(content, manifest, passphrase: "pass", encryptor: encryptor);
|
|
|
|
using var archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true);
|
|
Assert.NotNull(archive.GetEntry("manifest.json"));
|
|
Assert.Null(archive.GetEntry("content.json"));
|
|
Assert.NotNull(archive.GetEntry("content.enc"));
|
|
}
|
|
|
|
/// <summary>Verifies that a round trip through a temporary stream recovers identical content.</summary>
|
|
[Fact]
|
|
public void Roundtrip_through_temp_stream_recovers_identical_content()
|
|
{
|
|
var sut = new BundleSerializer();
|
|
var content = SampleContent();
|
|
var contentJson = sut.SerializeContentBytes(content);
|
|
var manifest = BuildManifestFor(contentJson);
|
|
|
|
using var stream = sut.Pack(content, manifest, passphrase: null, encryptor: null);
|
|
|
|
stream.Position = 0;
|
|
var readManifest = sut.ReadManifest(stream);
|
|
stream.Position = 0;
|
|
var contentBytes = sut.ReadContentBytes(stream, readManifest);
|
|
var unpacked = sut.UnpackContent(contentBytes, readManifest, passphrase: null, encryptor: null);
|
|
|
|
Assert.Equal(manifest.ContentHash, readManifest.ContentHash);
|
|
Assert.Single(unpacked.TemplateFolders);
|
|
Assert.Equal("Root", unpacked.TemplateFolders[0].Name);
|
|
var shared = Assert.Single(unpacked.SharedScripts);
|
|
Assert.Equal("util", shared.Name);
|
|
Assert.Equal("return 42;", shared.Code);
|
|
}
|
|
|
|
/// <summary>Verifies that reading the manifest throws when the archive has no manifest file.</summary>
|
|
[Fact]
|
|
public void ReadManifest_throws_when_zip_missing_manifest_json()
|
|
{
|
|
var sut = new BundleSerializer();
|
|
using var ms = new MemoryStream();
|
|
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true))
|
|
{
|
|
var entry = archive.CreateEntry("garbage.txt");
|
|
using var es = entry.Open();
|
|
es.Write(new byte[] { 1, 2, 3 });
|
|
}
|
|
ms.Position = 0;
|
|
|
|
Assert.Throws<InvalidDataException>(() => sut.ReadManifest(ms));
|
|
}
|
|
|
|
/// <summary>Verifies that unpacking throws a CryptographicException when the passphrase is wrong.</summary>
|
|
[Fact]
|
|
public void UnpackContent_throws_CryptographicException_on_wrong_passphrase()
|
|
{
|
|
var sut = new BundleSerializer();
|
|
var encryptor = new BundleSecretEncryptor();
|
|
var content = SampleContent();
|
|
var contentJson = sut.SerializeContentBytes(content);
|
|
var (cipher, meta) = encryptor.Encrypt(contentJson, "right", TestIterations);
|
|
var manifest = BuildManifestFor(cipher, encryption: meta);
|
|
|
|
using var stream = sut.Pack(content, manifest, passphrase: "right", encryptor: encryptor);
|
|
stream.Position = 0;
|
|
var readManifest = sut.ReadManifest(stream);
|
|
stream.Position = 0;
|
|
var contentBytes = sut.ReadContentBytes(stream, readManifest);
|
|
|
|
Assert.ThrowsAny<CryptographicException>(() => sut.UnpackContent(contentBytes, readManifest, "wrong", encryptor));
|
|
}
|
|
}
|