using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; /// /// Golden vectors for the plan's §5.2 condition_key formula: /// base64url(HMAC-SHA256(K, UTF8(upper(cluster)) ‖ 0x00 ‖ realm ‖ UTF8(ConditionId))) with /// K = HKDF-SHA256(UTF8(secret), salt = empty, info = "otopcua/condition-key/v1", 32). The expected keys were /// computed independently of (Python hmac/hashlib), so a change to the /// derivation — the realm byte, the upper-casing, the separator, the HKDF parameters — turns this red. A changed key /// re-mints every imported ConditionId on every consumer, so it must never change silently. /// [Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "A1")] public sealed class ConditionKeysTests { private const string Secret = "otopcua-golden-vector-secret-0123456789abcdef"; private const string ConditionId = "Plant/Modbus/dev1/TankHi"; [Theory] [InlineData(AddressSpaceRealm.Raw, "vSnDDtNB8ECednVRpnb1uqlfJ3T3RTOyUwjHmnZH2G4")] [InlineData(AddressSpaceRealm.Uns, "pfQgj3iDae6TsV9SvYlsBdcNivACEqdHBRP5RNNPAbU")] public void A_fixed_secret_cluster_realm_and_id_give_a_fixed_key(AddressSpaceRealm realm, string expected) { var keys = ConditionKeys.FromSecret(Secret); keys.Derive("Site-a", realm, ConditionId).ShouldBe(expected); // The cluster id is upper-cased before hashing: any casing of it gives the same key. keys.Derive("SITE-A", realm, ConditionId).ShouldBe(expected); } }