using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; /// /// H3 — the resume rule of a consumer Remote cursor: it resumes only under the caller, mapping revision, deployed /// connection, projection generation, origin view and serving identity it was issued under. A mapping revision change, /// a failover / origin restart (serving node, incarnation, generation or role), a projection rebuild or another caller /// ⇒ the cursor never resumes (BadContinuationPointInvalid, origin token released). /// [Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "H3")] public sealed class GrpcDataProjectedHistoryCursorTests { private static readonly DataView View = new(new DataAddress("origin", "urn:raw", "string", "Plant/Speed"), DataRealm.Raw, null, 0, "Plant/Speed"); private static readonly DataMetadata Metadata = new(View, "Speed", "Tag", DataValueKind.Int32, -1, [], false, DataOperations.Read | DataOperations.HistoryRead, 1, null, null, false, History: new DataHistoryInfo(true)); private static readonly ProjectedHistoryTarget Issued = new("map", 3, "link", Metadata, new ServingIdentity("node-a", "boot-1", "Primary", 5, 6, 2), 7); private const string Caller = "peer\0key\0v1"; private static readonly Dictionary ChangeMap = new() { ["mapping revision change"] = (Issued with { MappingRevision = 4 }, Caller), ["another mapping"] = (Issued with { MappingId = "other" }, Caller), ["another connection"] = (Issued with { ConnectionId = "link-2" }, Caller), ["projection rebuild"] = (Issued with { ProjectionGeneration = 8 }, Caller), ["failover to the pair member"] = (Issued with { Serving = Issued.Serving with { NodeId = "node-b" } }, Caller), ["origin restart"] = (Issued with { Serving = Issued.Serving with { Incarnation = "boot-2" } }, Caller), ["serving generation bump"] = (Issued with { Serving = Issued.Serving with { ServingGeneration = 3 } }, Caller), ["role change"] = (Issued with { Serving = Issued.Serving with { Role = "Secondary" } }, Caller), ["another origin view"] = (Issued with { OriginMetadata = Metadata with { View = View with { Subject = "Plant/Other" } } }, Caller), ["another caller"] = (Issued, "intruder\0key\0v1"), ["a rotated credential"] = (Issued, "peer\0key\0v2"), }; [Fact] public void The_same_binding_resumes_even_when_only_the_catalog_or_authority_epoch_moved() { ProjectedHistoryAccess.CursorMatches(Issued, Caller, Issued, Caller).ShouldBeTrue(); // A metadata renewal or ACL redeploy at the origin moves epochs, not the serving identity the token is bound to // (the origin itself still refuses a token its own history epoch no longer matches). ProjectedHistoryAccess.CursorMatches(Issued, Caller, Issued with { Serving = Issued.Serving with { CatalogEpoch = 9, AuthorityEpoch = 9 } }, Caller).ShouldBeTrue(); } public static TheoryData Changes => new(ChangeMap.Keys); [Theory] [MemberData(nameof(Changes))] public void Any_binding_change_refuses_the_resume(string change) => ProjectedHistoryAccess.CursorMatches(Issued, Caller, ChangeMap[change].Now, ChangeMap[change].Caller).ShouldBeFalse(change); }