Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/ConditionKeysTests.cs
T

33 lines
1.7 KiB
C#

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;
/// <summary>
/// Golden vectors for the plan's §5.2 <c>condition_key</c> formula:
/// <c>base64url(HMAC-SHA256(K, UTF8(upper(cluster)) ‖ 0x00 ‖ realm ‖ UTF8(ConditionId)))</c> with
/// <c>K = HKDF-SHA256(UTF8(secret), salt = empty, info = "otopcua/condition-key/v1", 32)</c>. The expected keys were
/// computed independently of <see cref="ConditionKeys"/> (Python <c>hmac</c>/<c>hashlib</c>), 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.
/// </summary>
[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);
}
}