using System.Security.Cryptography; using Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; using Xunit; namespace Admin.NET.Plugin.AiDOP.Tests.DataPlatform; public sealed class MdpIngestSecretTests { [Fact] public void Signature_UsesStableV1Vector() { const string canonical = "POST&/api/v1/ingest/lab.erp.purchase-order.v1&fixture-key&1795073000&fixture-nonce&" + "9c8ada8004b856060cba6771fb0b7c0d91a04f4f84cad30e55548ca7fb19f547&fixture-idempotency"; const string signature = "dyDfzHBPw51eHa3fB1PaPKjI6CCee78yT+LVIRTUAxg="; var key = Enumerable.Range(0, 32).Select(value => (byte)value).ToArray(); using var credential = new MdpIngestSigningCredential(101, 102, key); Assert.True(credential.Verify(canonical, signature)); Assert.False(credential.Verify(canonical.Replace("fixture-nonce", "changed"), signature)); Assert.False(credential.Verify(canonical.Replace("purchase-order", "other-entity"), signature)); credential.Dispose(); Assert.All(key, value => Assert.Equal((byte)0, value)); Assert.Throws(() => credential.Verify(canonical, signature)); } [Fact] public void EnvironmentProvider_OnlyResolvesWellFormedSecretReferences() { var environmentKey = $"MDP_INGRESS_SECRET_TEST_{Guid.NewGuid():N}".ToUpperInvariant(); var key = RandomNumberGenerator.GetBytes(32); var provider = new EnvironmentMdpIngestSecretProvider(); try { Environment.SetEnvironmentVariable(environmentKey, Convert.ToBase64String(key)); Assert.Equal(key, provider.Read(environmentKey)); Assert.Null(provider.Read("looks-like-a-plaintext-secret")); Environment.SetEnvironmentVariable(environmentKey, "bad-format"); Assert.Null(provider.Read(environmentKey)); Environment.SetEnvironmentVariable(environmentKey, Convert.ToBase64String(key.AsSpan(0, 16))); Assert.Null(provider.Read(environmentKey)); } finally { Environment.SetEnvironmentVariable(environmentKey, null); CryptographicOperations.ZeroMemory(key); } } }