Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.Transport.Tests/Serialization/BundleSerializerTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

134 lines
5.9 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>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));
}
}