84a050420f
Bundles did not record that a template was a derived slot template, so the
importer re-created every slot as a BASE template named after the slot.
- Export: TemplateDto gains an additive, null-omitted `Derivation`
(owner path + base path by stable names); a derived template carries only
its own rows (overrides/additions, not inherited placeholders). The closure
pulls in each derived template's base/owner; base orders first.
schemaVersion 1.1 -> 1.2, bundleFormatVersion stays 1.
- Import: base templates go through the ordinary passes; derived templates are
ordered topologically (DerivedTemplatePlan; cycle / duplicate / malformed =
preview Blocker + SemanticValidationException before anything is staged),
their owner and base resolved by path (#100 root rule), and the slot is
created with TemplateService.AddCompositionAsync, synced in place when it
already exists with the same base, or deleted+recomposed when the base
differs; own rows applied as overrides, then ResyncInheritedMembersAsync.
Missing owner/base fails that record (BundleImportDerivedTemplateUnresolved
audit + ImportResult warning), never creating a base in its place.
- Preview/diff: derived rows are keyed by qualified name and compared against
the target slot at the same owner path (own rows + base), so an unchanged
slot is Identical and a same-named base template is never matched.
- Pre-#118 bundles import exactly as before (new fixture
pre-118-derived-plaintext.scadabundle, written by a13aabc7). The field is in
content, pinned by the signed manifest's contentHash.
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
164 lines
6.6 KiB
C#
164 lines
6.6 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
|
|
using ZB.MOM.WW.ScadaBridge.Transport.Serialization;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Transport.Tests.Serialization;
|
|
|
|
public sealed class ManifestBuilderTests
|
|
{
|
|
private static BundleSummary EmptySummary => new(0, 0, 0, 0, 0, 0, 0, 0);
|
|
private static IReadOnlyList<ManifestContentEntry> NoContents => Array.Empty<ManifestContentEntry>();
|
|
|
|
/// <summary>Verifies that building a manifest populates the summary from the contents.</summary>
|
|
[Fact]
|
|
public void Build_populates_summary_from_contents()
|
|
{
|
|
var sut = new ManifestBuilder();
|
|
// Summary now carries trailing Sites/DataConnections/Instances counts.
|
|
var summary = new BundleSummary(2, 1, 0, 0, 0, 0, 0, 0, Sites: 3, DataConnections: 4, Instances: 5);
|
|
var contents = new[]
|
|
{
|
|
new ManifestContentEntry("Template", "T1", 1, Array.Empty<string>()),
|
|
new ManifestContentEntry("Template", "T2", 1, new[] { "T1" }),
|
|
new ManifestContentEntry("TemplateFolder", "F1", 1, Array.Empty<string>()),
|
|
};
|
|
|
|
var manifest = sut.Build("env", "user", "1.0.0", encryption: null, summary, contents, contentBytes: new byte[] { 1, 2, 3 });
|
|
|
|
Assert.Equal(summary, manifest.Summary);
|
|
// New summary counts flow through the builder verbatim.
|
|
Assert.Equal(3, manifest.Summary.Sites);
|
|
Assert.Equal(4, manifest.Summary.DataConnections);
|
|
Assert.Equal(5, manifest.Summary.Instances);
|
|
Assert.Equal(contents, manifest.Contents);
|
|
Assert.Equal("env", manifest.SourceEnvironment);
|
|
Assert.Equal("user", manifest.ExportedBy);
|
|
Assert.Equal("1.0.0", manifest.ScadaBridgeVersion);
|
|
Assert.Equal(1, manifest.BundleFormatVersion);
|
|
// Schema minor bumped 1.0 → 1.1; format version stays 1.
|
|
Assert.Equal("1.2", manifest.SchemaVersion);
|
|
Assert.Null(manifest.Encryption);
|
|
}
|
|
|
|
/// <summary>Verifies that building a manifest computes a content hash with the sha256 prefix.</summary>
|
|
[Fact]
|
|
public void Build_computes_content_hash_with_sha256_prefix()
|
|
{
|
|
var sut = new ManifestBuilder();
|
|
var bytes = Encoding.UTF8.GetBytes("known-content");
|
|
var expectedHashHex = Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant();
|
|
|
|
var manifest = sut.Build("env", "user", "v", encryption: null, EmptySummary, NoContents, bytes);
|
|
|
|
Assert.Equal("sha256:" + expectedHashHex, manifest.ContentHash);
|
|
}
|
|
|
|
/// <summary>Verifies that validation rejects an unsupported bundle format version.</summary>
|
|
[Fact]
|
|
public void Validate_rejects_unsupported_bundleFormatVersion()
|
|
{
|
|
var bytes = new byte[] { 1, 2, 3 };
|
|
var hash = "sha256:" + Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant();
|
|
var manifest = new BundleManifest(
|
|
BundleFormatVersion: 999,
|
|
SchemaVersion: "1.0",
|
|
CreatedAtUtc: DateTimeOffset.UtcNow,
|
|
SourceEnvironment: "env",
|
|
ExportedBy: "u",
|
|
ScadaBridgeVersion: "v",
|
|
ContentHash: hash,
|
|
Encryption: null,
|
|
Summary: EmptySummary,
|
|
Contents: NoContents);
|
|
|
|
var result = new ManifestValidator().Validate(manifest, bytes);
|
|
|
|
Assert.Equal(ManifestValidationResult.UnsupportedFormatVersion, result);
|
|
}
|
|
|
|
/// <summary>Verifies that validation rejects a manifest whose content hash does not match.</summary>
|
|
[Fact]
|
|
public void Validate_rejects_when_contentHash_mismatch()
|
|
{
|
|
var bytes = new byte[] { 1, 2, 3 };
|
|
var manifest = new BundleManifest(
|
|
BundleFormatVersion: 1,
|
|
SchemaVersion: "1.0",
|
|
CreatedAtUtc: DateTimeOffset.UtcNow,
|
|
SourceEnvironment: "env",
|
|
ExportedBy: "u",
|
|
ScadaBridgeVersion: "v",
|
|
ContentHash: "sha256:deadbeef",
|
|
Encryption: null,
|
|
Summary: EmptySummary,
|
|
Contents: NoContents);
|
|
|
|
var result = new ManifestValidator().Validate(manifest, bytes);
|
|
|
|
Assert.Equal(ManifestValidationResult.ContentHashMismatch, result);
|
|
}
|
|
|
|
/// <summary>Verifies that validation accepts a well-formed version 1 manifest.</summary>
|
|
[Fact]
|
|
public void Validate_accepts_well_formed_v1_manifest()
|
|
{
|
|
var sut = new ManifestBuilder();
|
|
var bytes = Encoding.UTF8.GetBytes("hello");
|
|
var manifest = sut.Build("env", "u", "v", encryption: null, EmptySummary, NoContents, bytes);
|
|
|
|
var result = new ManifestValidator().Validate(manifest, bytes);
|
|
|
|
Assert.Equal(ManifestValidationResult.Ok, result);
|
|
}
|
|
|
|
/// <summary>Verifies that validation accepts an older schema version 1.0 manifest.</summary>
|
|
[Fact]
|
|
public void Validate_accepts_older_schemaVersion_1_0_manifest()
|
|
{
|
|
// An older "1.0" bundle must still import: schema minor differences
|
|
// are additive and the validator gates only on BundleFormatVersion.
|
|
var bytes = Encoding.UTF8.GetBytes("legacy-bundle");
|
|
var hash = "sha256:" + Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant();
|
|
var manifest = new BundleManifest(
|
|
BundleFormatVersion: ManifestBuilder.CurrentBundleFormatVersion,
|
|
SchemaVersion: "1.0",
|
|
CreatedAtUtc: DateTimeOffset.UtcNow,
|
|
SourceEnvironment: "env",
|
|
ExportedBy: "u",
|
|
ScadaBridgeVersion: "v",
|
|
ContentHash: hash,
|
|
Encryption: null,
|
|
Summary: EmptySummary,
|
|
Contents: NoContents);
|
|
|
|
var result = new ManifestValidator().Validate(manifest, bytes);
|
|
|
|
Assert.Equal(ManifestValidationResult.Ok, result);
|
|
}
|
|
|
|
/// <summary>Verifies that validation refuses bundle format version 2.</summary>
|
|
[Fact]
|
|
public void Validate_refuses_bundleFormatVersion_2()
|
|
{
|
|
// A future format-version 2 is a hard refusal (not a minor schema bump).
|
|
var bytes = Encoding.UTF8.GetBytes("future-bundle");
|
|
var hash = "sha256:" + Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant();
|
|
var manifest = new BundleManifest(
|
|
BundleFormatVersion: 2,
|
|
SchemaVersion: "1.1",
|
|
CreatedAtUtc: DateTimeOffset.UtcNow,
|
|
SourceEnvironment: "env",
|
|
ExportedBy: "u",
|
|
ScadaBridgeVersion: "v",
|
|
ContentHash: hash,
|
|
Encryption: null,
|
|
Summary: EmptySummary,
|
|
Contents: NoContents);
|
|
|
|
var result = new ManifestValidator().Validate(manifest, bytes);
|
|
|
|
Assert.Equal(ManifestValidationResult.UnsupportedFormatVersion, result);
|
|
}
|
|
}
|