using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Configuration.Entities; using ZB.MOM.WW.OtOpcUa.Core.Authorization; using ZB.MOM.WW.OtOpcUa.Host.OpcUa; namespace ZB.MOM.WW.OtOpcUa.Host.Tests.OpcUa; /// /// Covers — the deadline and TTL cache the OPC UA impersonation /// callback's key lookup runs under (arch-review 03/S18). /// /// /// /// The deadline cases are driven through the real /// rather than the store directly, because the fail-closed conversion — a throw becoming the /// standard opaque denial — is part of what is being asserted, and it lives in the authenticator's /// catch-all rather than in the decorator. /// /// /// ⚠️ Deadline cases use a REAL short timeout; TTL cases use the fake clock. The hand-rolled /// overrides GetUtcNow() only, so CreateTimer falls through to /// the base implementation and produces a real system timer — advancing the fake clock does not /// fire a CancelAfter. A deadline test "improved" to advance the fake clock would pass for /// the wrong reason: the inner task simply never completing. /// /// public sealed class BoundedApiKeyStoreTests { private const string Pepper = "unit-test-pepper"; private const string Principal = "scadabridge-dcl"; /// /// A store that never answers must not hold the activation open: the deadline fires and the /// activation is denied. /// /// /// The SDK raises impersonation under a SessionManager-wide lock, so a lookup that does not /// return stalls every session activation in the server — not just this one. That is the whole of /// 03/S18, and it is why the bound is a deadline rather than a cache. /// [Fact] public async Task A_blocked_store_denies_without_hanging_the_activation() { var (presented, inner) = SetUp(); inner.BlockForever = true; var auth = Auth(Bounded(inner, timeoutMs: 150, cacheSeconds: 0)); var result = await auth.AuthenticateUserNameAsync("apikey", presented, CancellationToken.None) .WaitAsync(TimeSpan.FromSeconds(5)); result.Success.ShouldBeFalse(); result.Error.ShouldBe(ApiKeyOpcUaUserAuthenticator.DenyMessage); } /// /// The positive control for : /// the same fake, answering normally, still authenticates. /// /// /// Without it a build whose decorator denied everything would pass every deadline case in /// this class, and the bound would look correct while having locked the fleet out. /// [Fact] public async Task A_reachable_store_still_authenticates() { var (presented, inner) = SetUp(); var auth = Auth(Bounded(inner, timeoutMs: 150, cacheSeconds: 0)); var result = await auth.AuthenticateUserNameAsync("apikey", presented, CancellationToken.None); result.Success.ShouldBeTrue(); result.Principal.ShouldBe(Principal); } /// /// A lookup that completes with a perfectly valid key after the deadline still denies — /// nothing races back in past the bound. /// [Fact] public async Task A_deadline_never_admits() { var (presented, inner) = SetUp(); var gate = new TaskCompletionSource(); inner.Gate = gate.Task; var auth = Auth(Bounded(inner, timeoutMs: 100, cacheSeconds: 0)); var pending = auth.AuthenticateUserNameAsync("apikey", presented, CancellationToken.None); // Let the deadline pass, THEN hand the inner store its (valid) answer. await Task.Delay(400); gate.SetResult(); var result = await pending.WaitAsync(TimeSpan.FromSeconds(5)); result.Success.ShouldBeFalse(); result.Error.ShouldBe(ApiKeyOpcUaUserAuthenticator.DenyMessage); } /// /// A non-positive StoreTimeoutMs disables the deadline, restoring the unbounded behaviour /// byte-for-byte — the documented rollback. /// [Fact] public async Task A_zero_timeout_disables_the_deadline() { var (presented, inner) = SetUp(); var gate = new TaskCompletionSource(); inner.Gate = gate.Task; var auth = Auth(Bounded(inner, timeoutMs: 0, cacheSeconds: 0)); var pending = auth.AuthenticateUserNameAsync("apikey", presented, CancellationToken.None); // Far longer than any deadline this class configures; an active bound would have denied by now. await Task.Delay(300); pending.IsCompleted.ShouldBeFalse("a disabled deadline must not cancel a slow lookup"); gate.SetResult(); (await pending.WaitAsync(TimeSpan.FromSeconds(5))).Success.ShouldBeTrue(); } /// The anonymous activation path gets the same deadline, and the same denial. /// /// It runs on every unauthenticated connect, so leaving it unbounded would leave the /// server-wide-stall hazard fully open on the busiest activation path. /// [Fact] public async Task Anonymous_grant_resolution_is_bounded_too() { var (_, inner) = SetUp(); inner.BlockForever = true; var auth = Auth(Bounded(inner, timeoutMs: 150, cacheSeconds: 0)); var result = await auth.AuthenticateAnonymousAsync(CancellationToken.None) .WaitAsync(TimeSpan.FromSeconds(5)); result.Success.ShouldBeFalse(); } // ---------- helpers ---------- private static BoundedApiKeyStore Bounded( IApiKeyStore inner, int timeoutMs, int cacheSeconds, TimeProvider? clock = null) => new( inner, Options.Create(new ApiKeyOptions { Username = "apikey", Pepper = Pepper, StoreTimeoutMs = timeoutMs, StoreCacheSeconds = cacheSeconds, }), NullLogger.Instance, clock ?? TimeProvider.System); private static ApiKeyOpcUaUserAuthenticator Auth(IApiKeyStore store) => new( store, Options.Create(new ApiKeyOptions { Username = "apikey", Pepper = Pepper }), NullLogger.Instance); private static (string Presented, GatedApiKeyStore Store) SetUp() { var (presented, apiKeyId, hash) = ApiKeySecret.Generate(Pepper); var key = new ApiKey { ApiKeyId = apiKeyId, Name = "test key", SecretHash = hash, Principal = Principal, Enabled = true, CreatedUtc = DateTime.UtcNow, }; return (presented, new GatedApiKeyStore(key)); } /// /// An inner store whose answer can be withheld — a hang the EF in-memory provider cannot /// represent — and whose calls are counted. /// private sealed class GatedApiKeyStore(ApiKey key) : IApiKeyStore { /// When set, every call awaits this before answering. public Task? Gate { get; set; } /// When true, no call ever completes. public bool BlockForever { get; set; } /// How many calls reached this store (i.e. were not served from the cache). public int Calls => _calls; /// When true, the key lookup answers "no usable key" — a revocation. public bool Revoked { get; set; } /// Number of leading calls that throw before the store starts answering. public int ThrowsRemaining { get; set; } private int _calls; public async Task FindAsync(string apiKeyId, DateTime utcNow, CancellationToken ct) { await WaitAsync(ct).ConfigureAwait(false); if (Revoked) return null; return string.Equals(apiKeyId, key.ApiKeyId, StringComparison.Ordinal) ? new ApiKeyLookup(key, []) : null; } public async Task> FindGrantsAsync(string principal, CancellationToken ct) { await WaitAsync(ct).ConfigureAwait(false); return []; } private async Task WaitAsync(CancellationToken ct) { Interlocked.Increment(ref _calls); if (ThrowsRemaining > 0) { ThrowsRemaining--; throw new InvalidOperationException("store is down"); } if (BlockForever) await Task.Delay(Timeout.Infinite, ct).ConfigureAwait(false); if (Gate is { } gate) await gate.WaitAsync(ct).ConfigureAwait(false); } } /// /// A hand-advanced clock. Overrides GetUtcNow() only — see the class remarks for why that /// confines it to the TTL cases. /// private sealed class FakeClock(DateTimeOffset utcNow) : TimeProvider { private DateTimeOffset _now = utcNow; /// public override DateTimeOffset GetUtcNow() => _now; /// Moves the clock forward. /// How far forward to move. public void Advance(TimeSpan by) => _now = _now.Add(by); } }