Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/ActorNodeWriteGatewayTests.cs
T
Joseph Doherty 13521354cc feat(galaxy): #1017 SecuredWrite attributes can be written as the calling API key's Galaxy identity (opt-in); VerifiedWrite stays refused
- MxAccess.SecuredWrites = Refuse (default, unchanged #991 local BadUserAccessDenied) |
  ApiKeyIdentity. Opted in, a SecuredWrite runs as the calling key's own /api-keys Galaxy
  identity (ResolveKeyIdentityAsync: no fallback account; anonymous / no identity / broken
  identity refused before the wire): own plain-advised handle (apart from the raw path's
  supervisory handle) -> AuthenticateUser every write -> WriteSecured(userId, 0), the sequence
  mxaccessgw live-captured committing. Auth failure / user id 0 -> BadUserAccessDenied; password
  only in the AuthenticateUser command, scrubbed from every log. Identity resolved once per batch.
- VerifiedWrite is refused in every mode: an OPC UA Write carries no verifier (MXAccess refuses
  no verifier 1008 / self-verifier 8018), and a stored verifier would defeat the two-person rule.
- Caller plumbing: WriteCaller + optional ICallerAwareWritable (Core.Abstractions); node manager
  passes principal + key id -> IOpcUaNodeWriteGateway caller overload (default drops it) ->
  RouteNodeWrite.Caller -> WriteAttribute.Caller -> ICallerAwareWritable. The gRPC data-connection
  write path carries no caller yet (secured writes refused there).
- AdminUI Galaxy form 'Secured writes' select (owned path); enum serialises as its name; factory
  refuses an unknown value. Docs: Galaxy.md design + live-gate checklist. Not live-gated.
2026-12-03 15:29:02 -05:00

108 lines
5.1 KiB
C#

using Akka.Actor;
using Microsoft.Extensions.Logging.Abstractions;
using Shouldly;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
/// <summary>
/// Verifies the <see cref="ActorNodeWriteGateway"/> adapter: it implements the Commons
/// <c>IOpcUaNodeWriteGateway</c> by lazily resolving the <see cref="DriverHostActor"/> and Asking it a
/// <see cref="DriverHostActor.RouteNodeWrite"/>, translating the actor's
/// <see cref="DriverHostActor.NodeWriteResult"/> reply into a Commons <c>NodeWriteOutcome</c>. A
/// <c>TestProbe</c> stands in for the DriverHostActor (passed via <c>resolveDriverHost</c>) so the
/// gateway can be driven without the full driver harness.
/// </summary>
public sealed class ActorNodeWriteGatewayTests : RuntimeActorTestBase
{
/// <summary>Probe receives the RouteNodeWrite and replies success → the outcome is (true, null).</summary>
[Fact]
public async Task Success_reply_maps_to_successful_outcome()
{
var probe = CreateTestProbe();
var gateway = new ActorNodeWriteGateway(resolveDriverHost: () => probe.Ref, NullLogger.Instance);
var writeTask = gateway.WriteAsync("eq-1/speed", 123.0, AddressSpaceRealm.Uns, CancellationToken.None);
var routed = probe.ExpectMsg<DriverHostActor.RouteNodeWrite>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
routed.NodeId.ShouldBe("eq-1/speed");
routed.Value.ShouldBe(123.0);
probe.Reply(new DriverHostActor.NodeWriteResult(true, null));
var outcome = await writeTask;
outcome.Success.ShouldBeTrue();
outcome.Reason.ShouldBeNull();
}
/// <summary>#1017: the caller-carrying overload puts the caller on the RouteNodeWrite; the plain one carries none.</summary>
[Fact]
public async Task The_caller_rides_the_route_message()
{
var probe = CreateTestProbe();
IOpcUaNodeWriteGateway gateway = new ActorNodeWriteGateway(resolveDriverHost: () => probe.Ref, NullLogger.Instance);
var caller = new ZB.MOM.WW.OtOpcUa.Core.Abstractions.WriteCaller("scadabridge", "0a1b2c3d4e5f6071");
var withCaller = gateway.WriteAsync("eq-1/speed", 1.0, AddressSpaceRealm.Uns, caller, CancellationToken.None);
probe.ExpectMsg<DriverHostActor.RouteNodeWrite>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken)
.Caller.ShouldBe(caller);
probe.Reply(new DriverHostActor.NodeWriteResult(true, null));
(await withCaller).Success.ShouldBeTrue();
var without = gateway.WriteAsync("eq-1/speed", 2.0, AddressSpaceRealm.Uns, CancellationToken.None);
probe.ExpectMsg<DriverHostActor.RouteNodeWrite>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken)
.Caller.ShouldBeNull();
probe.Reply(new DriverHostActor.NodeWriteResult(true, null));
(await without).Success.ShouldBeTrue();
}
/// <summary>Probe replies failure → the outcome carries the same (false, reason) verbatim.</summary>
[Fact]
public async Task Failure_reply_maps_to_failure_outcome_with_reason()
{
var probe = CreateTestProbe();
var gateway = new ActorNodeWriteGateway(resolveDriverHost: () => probe.Ref, NullLogger.Instance);
var writeTask = gateway.WriteAsync("eq-1/speed", 123.0, AddressSpaceRealm.Uns, CancellationToken.None);
probe.ExpectMsg<DriverHostActor.RouteNodeWrite>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
probe.Reply(new DriverHostActor.NodeWriteResult(false, "not primary"));
var outcome = await writeTask;
outcome.Success.ShouldBeFalse();
outcome.Reason.ShouldBe("not primary");
}
/// <summary>The probe never replies; a short Ask timeout makes the gateway fast-fail with a negative,
/// non-null-reason outcome rather than hanging.</summary>
[Fact]
public async Task No_reply_within_timeout_maps_to_negative_outcome()
{
var probe = CreateTestProbe();
var gateway = new ActorNodeWriteGateway(
resolveDriverHost: () => probe.Ref, NullLogger.Instance,
askTimeout: TimeSpan.FromMilliseconds(200));
var outcome = await gateway.WriteAsync("eq-1/speed", 123.0, AddressSpaceRealm.Uns, CancellationToken.None);
outcome.Success.ShouldBeFalse();
outcome.Reason.ShouldNotBeNull();
outcome.Reason.ShouldBe("write timeout");
}
/// <summary>When no DriverHostActor is registered (resolver returns null), the gateway short-circuits
/// to (false, "writes unavailable") and never messages any actor.</summary>
[Fact]
public async Task No_actor_registered_maps_to_writes_unavailable()
{
var gateway = new ActorNodeWriteGateway(resolveDriverHost: () => null, NullLogger.Instance);
var outcome = await gateway.WriteAsync("eq-1/speed", 123.0, AddressSpaceRealm.Uns, CancellationToken.None);
outcome.Success.ShouldBeFalse();
outcome.Reason.ShouldBe("writes unavailable");
}
}