60 lines
4.2 KiB
C#
60 lines
4.2 KiB
C#
using System.Text.Json;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Host.Configuration;
|
|
using static ZB.MOM.WW.OtOpcUa.Host.Tests.DataConnections.GrpcDataSecretProvisioningTests;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.DataConnections;
|
|
|
|
[Trait("Feature", "GrpcData"), Trait("Phase", "P07")]
|
|
public sealed class GrpcDataBundleLifecycleTests
|
|
{
|
|
[Fact]
|
|
public async Task Missing_file_is_not_revocation_and_restarted_loader_replays_completed_bundle()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken; var directory = Directory.CreateTempSubdirectory("data-bundle-");
|
|
try
|
|
{
|
|
var store = new MemoryStore(); var cipher = new TestCipher(); var changes = new DataConnectionSecretChangeHandler();
|
|
var p = new DataConnectionSecretProvisioner(store, cipher, new NoopInvalidator(), changes, "pair", "node");
|
|
var trust = new DataConnectionTrustPolicyStore(store, cipher, changes, "pair", "node");
|
|
await new DataConnectionScopeActivator(p, trust).SetScopeAsync(1, new Dictionary<string, string> { ["c"] = "secret:data/key" }, ct);
|
|
var credentials = Path.Combine(directory.FullName, "credentials.json"); var trustFile = Path.Combine(directory.FullName, "trust.json");
|
|
await File.WriteAllTextAsync(credentials, JsonSerializer.Serialize(new DataConnectionCredentialBundle("pair", "node", 1, [new("c", "secret:data/key", "bearer", false)], 1)), ct);
|
|
await File.WriteAllTextAsync(trustFile, JsonSerializer.Serialize(new DataConnectionTrustBundle("pair", "node", 1, [new("c", 1, [])])), ct);
|
|
var loader = new DataConnectionBundleLoader(p, trust, "pair", "node");
|
|
var first = await loader.ApplyFilesAsync(credentials, trustFile, ct);
|
|
first.CredentialsLoaded.ShouldBeTrue(); first.TrustLoaded.ShouldBeTrue();
|
|
var restarted = new DataConnectionBundleLoader(p, trust, "PAIR", "NODE");
|
|
var replay = await restarted.ApplyFilesAsync(credentials, trustFile, ct);
|
|
replay.CredentialsLoaded.ShouldBeTrue(); replay.TrustLoaded.ShouldBeTrue();
|
|
File.Delete(credentials);
|
|
var absent = await restarted.ApplyFilesAsync(credentials, trustFile, ct);
|
|
absent.CredentialsLoaded.ShouldBeFalse();
|
|
(await new DataConnectionCredentialResolver(store, cipher, "pair", "node").ResolveAsync("c", ct))!.Value.ShouldBe("bearer");
|
|
}
|
|
finally { directory.Delete(true); }
|
|
}
|
|
[Fact]
|
|
public async Task Invalid_trust_batch_is_not_partially_applied_and_transient_failure_retries()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken; var directory = Directory.CreateTempSubdirectory("data-bundle-");
|
|
try
|
|
{
|
|
var store = new MemoryStore(); var cipher = new TestCipher(); var changes = new DataConnectionSecretChangeHandler();
|
|
var p = new DataConnectionSecretProvisioner(store, cipher, new NoopInvalidator(), changes, "pair", "node");
|
|
var trust = new DataConnectionTrustPolicyStore(store, cipher, changes, "pair", "node");
|
|
await new DataConnectionScopeActivator(p, trust).SetScopeAsync(1, new Dictionary<string, string> { ["c"] = "secret:data/key" }, ct);
|
|
var path = Path.Combine(directory.FullName, "trust.json"); var loader = new DataConnectionBundleLoader(p, trust, "pair", "node");
|
|
await File.WriteAllTextAsync(path, JsonSerializer.Serialize(new DataConnectionTrustBundle("pair", "node", 1, [new("c", 1, []), new("unauthorized", 1, [])])), ct);
|
|
(await loader.ApplyFilesAsync(null, path, ct)).TrustLoaded.ShouldBeFalse();
|
|
await trust.SetAsync("c", 1, [], 1, ct); // failed batch did not commit its first entry
|
|
await File.WriteAllTextAsync(path, JsonSerializer.Serialize(new DataConnectionTrustBundle("wrong", "node", 1, [new("c", 2, [])])), ct);
|
|
(await loader.ApplyFilesAsync(null, path, ct)).TrustLoaded.ShouldBeFalse();
|
|
await File.WriteAllTextAsync(path, JsonSerializer.Serialize(new DataConnectionTrustBundle("pair", "node", 1, [new("c", 2, [])])), ct);
|
|
(await loader.ApplyFilesAsync(null, path, ct)).TrustLoaded.ShouldBeTrue();
|
|
}
|
|
finally { directory.Delete(true); }
|
|
}
|
|
}
|