Files
UA-.NETStandard/tests/Opc.Ua.Server.Tests/NodeManager/CustomNodeManagerDeterministicTests.cs
Marc Schier 2836da7ec8 Fix sessionless ResultTransfer ownership bypass and other issues. (#4834)
# Description

Correct ownership, endpoint validation, policy propagation, subscription
delivery, file handling and generated-model behavior across the stack,
tools and samples. Add focused regression tests and update the
corresponding guides and sample documentation.

### Previously included corrections

- Require an active, matching session to generate or access transient
ResultTransfer file handles. Cover both sessionless generation and reads
against a handle owned by an active session, using live session owners
in the Machinery tests.
- Keep physical filesystem operations within their configured mount and
apply the same confinement checks throughout file operations.
- Validate LDS semaphore paths consistently during server registration
and registration-store maintenance.

### Individual issues addressed

| # | Component | Issue and correction |
|---|---|---|
| 1 | AI jobs and transfers | Jobs and transfers did not consistently
retain their creator's identity and session. Apply ownership checks to
descendants, file handles, subscriptions and execution, and recheck
deployment permissions before dispatch. |
| 2 | Password-management client | Password-bearing calls could use
signing-only channels or follow a replacement connection. Capture an
encrypted transport generation and keep dispatch bound to it; preserve
ordinary non-password edits. |
| 3 | GDS and certificate-push clients | Connections omitted hostname
verification. Validate the original configured DNS name or IP address
before session activation. |
| 4 | Certificate push | Private-key uploads could use signing-only
calls. Require an encrypted, generation-bound dispatch while preserving
certificate-only updates and restoring the caller's identity afterward.
|
| 5 | MQTT client | The .NET Framework custom-trust callback could
discard TLS hostname, certificate-availability and usage errors. Retain
those checks while supporting configured issuer trust. |
| 6 | DTLS handshake | Certificate trust was not tied to the configured
peer address. Match the certificate's DNS or IP identity to the intended
endpoint. |
| 7 | Replicated address spaces | Serialized nodes lost per-node and
descendant permission metadata. Preserve role permissions, user-role
permissions and access restrictions, including updates to existing
replica nodes. |
| 8 | MCP PubSub imports | Imports without groups skipped checks on the
effective broker destination. Check the actual endpoint before resolving
credentials or activating the connection. |
| 9 | Reference-server accounts | A disabled username account could
authenticate again with its previous roles. Reject disabled accounts
during fresh authentication. |
| 10 | Console reference client | The sample disabled hostname checks
when connecting to an otherwise trusted peer. Retain the original
requested host and validate it before activation. |
| 11 | Capture replay and export | Replay and raw export could accept
non-capture files or include key material. Require complete PCAP/PCAPNG
containers and exclude key sidecars and decryption-secret blocks. |
| 12 | PubSub adapter fallback | The fallback client configuration
automatically accepted peer certificates. Apply normal trust and
hostname validation before activation or reads. |
| 13 | Kafka transport | Explicit TLS enablement was checked but did not
reach both final Confluent configurations. Use the same effective
protocol for validation, producers and consumers. |
| 14 | PubSub actions | Configuration rebuilds could widen a
connection-local action handler's scope. Preserve its original
connection association and reject mismatched dispatch. |
| 15 | PubSub readers | Reader-level message-protection settings were
accepted but ignored. Apply the effective group and reader settings
before accepting and publishing received values. |
| 16 | PubSub key groups | Different `SecurityGroupId` values could
share one connection-wide key context. Reject mixed groups on a
connection and require separate connections for distinct groups. |
| 17 | MCP session registrations | Session-name replacement or reuse
could retain another registration's credential bindings. Bind access to
the registration, caller and authenticated endpoint, and preserve that
identity when borrowing sessions. |
| 18 | Viewer HTTP host | Requests reached the viewer's OPC UA session
without consistent Host, Origin and port checks. Validate the request
destination and browser origin before using the session, while retaining
supported local MCP clients. |
| 19 | Vision file operations | File operations did not consistently
apply protected-file checks, and remote handles lacked caller and
connection context. Apply the shared checks and use host-side opaque
handles bound to caller, target and generation. |
| 20 | DTLS record integrity | The integrity-only MAC used a truncated
sequence number. Authenticate the full reconstructed sequence number and
epoch so records remain distinct across rollover. |
| 21 | AMB client sample | Connection setup omitted hostname validation.
Preserve the originally requested host and verify the peer identity
before activation. |
| 22 | Robot sample writes | The R2 `SpeedOverride` callback could
target R1 state. Bind each callback to its own robot and apply that
robot's write policy. |
| 23 | AI result properties | Optional result children inherited
remotely writable defaults. Preserve their declaration restrictions and
keep server-produced results read-only. |
| 24 | WSS peer-trust updates | Existing channels did not consistently
respond to peer-trust changes. Revalidate complete chains for forward
and reverse WSS channels and close only those that no longer validate. |
| 25 | GDS revocation checks | Caller-provided certificates could cause
issuer retrieval from embedded URLs. Resolve validation from configured
local trust, issuers and CRLs without fetching caller-provided
locations. |
| 26 | GMS observations | Published correction observations retained
writable model defaults. Reject remote writes while preserving
publisher-side updates. |
| 27 | Production-dataset monitoring | Subscription admission and
delivery could use the reporter's context instead of each subscriber's
permissions. Evaluate the subscriber at admission and on every delivery,
including cancellation and disposal paths. |
| 28 | WoT MQTT destinations | Hostless MQTT forms could imply a
localhost destination before loopback checks. Reject them before
credential lookup or connection attempts and validate the effective
endpoint. |
| 29 | XRegistry opaque reads | Content-ID reads and notifications did
not consistently honor encrypted-read requirements or the currently
accepted transport. Recheck the bound endpoint and reject transport
downgrades before rebinding, including when protected values are already
queued. |
| 30 | PubSub capture dissection | Key loading did not consistently
check the configured capture-root boundary. Check each actual key path
immediately before opening it. |
| 31 | PubSub publication queues | Imported WriterGroup queues were not
fully checked against publish-topic grants. Check the destination
actually used by each group. |
| 32 | MCP PubSub mutations | A caller-supplied mode could select
subscribe grants for an operation that publishes. Derive the required
direction and parent context from the actual mutation. |
| 33 | Generated write masks | Model import and generation could widen
an explicit `UserWriteMask`. Preserve it through import, copy, merge,
inheritance and dependency metadata. |
| 34 | Generated method flags | An explicit `UserExecutable=false` could
be lost. Preserve its specified state and resulting method behavior
through the same model paths. |
| 35 | Generated role identifiers | Unresolved role identifiers could
cause permission entries to be omitted. Preserve namespace-qualified
role IDs and report a diagnostic when a policy cannot be represented. |
| 36 | Aggregation-server defaults | The `localhost` default did not
reliably constrain listener binding. Use numeric loopback for the
default endpoint and command-line fallback. |
| 37 | Certificate-validation cache | Cached results could skip current
leaf or issuer validity checks. Recheck validity windows on cache hits
and keep hostname validation independent of cached chain trust. |
| 38 | Software-update folders | Revision names and marker contents
containing dot segments could select paths outside the software root.
Normalize and check every derived path against that root. |
| 39 | USDA arrays | Array elements were not encoded for their string or
asset context. Escape them as data and preserve valid composition and
round-trip behavior. |
| 40 | Adapter hot reload | A changed dataset definition with the same
name could keep old fields, subscriptions or cached values active.
Compare complete immutable definitions and retire old state before
replacement. |
| 41 | Adapter index ranges | Subscription publication could ignore
`IndexRange` and return the complete value. Preserve the requested
projection and its status and timestamps; reject unsupported range
providers. |
| 42 | PubSub metadata and targets | Metadata from one connection could
change another connection's target mapping. Scope metadata to its
connection and provenance, and resolve targets using actual typed field
IDs rather than field names or unintended positional fallback. |
| 43 | Scales product locks | Product locks were not applied across
every remote mutation path. Apply them consistently to value writes,
filling calculations and related recipe/controller operations while
retaining permitted updates. |
| 44 | Event permission caching | Cached event decisions could outlive
identity, role or node-policy changes. Revalidate the current
subscriber, event type and source permissions instead of reusing a stale
decision. |
| 45 | WoT Modbus destinations | Hostless Modbus forms could imply
localhost before loopback checks. Reject them before connecting and
validate the effective endpoint. |
| 46 | MCP local PKI tools | Peer-trust mutations did not consistently
require the caller's `CertificateAdministration` grant. Check the
capability before trust and untrust operations. |
| 47 | Robot-control HTTP bridge | The bridge lacked consistent Host,
Origin and port validation and caller-specific session grants. Apply
those checks before borrowing the OPC UA session and retain supported
local clients. |
| 48 | Subscription transfers | Transferred subscriptions could retain
cached notifications after the destination's permissions changed.
Require authorization continuity between source and destination
identities and roles, invalidate stale retransmission data when
continuity fails, and preserve retransmission behavior for compatible
transfers. |

### Compatibility notes

- Replication uses the versioned NSRP v1 policy envelope. Upgrade peers
together and re-encode or reseed legacy topology from the authoritative
model; missing permission metadata is not inferred from old records.
- Integrity-only DTLS peers must be upgraded together for the full
sequence/epoch MAC. AEAD behavior is unchanged.
- Distinct PubSub key groups require separate connections.
- Custom sessions that cannot capture the actual encrypted transport,
and subscription providers that cannot honor a requested range, are
rejected rather than silently changing the requested behavior.
- Subscription transfers retain retransmission data only when the
destination authorization remains compatible with the source snapshot;
otherwise the old sequence numbers and cached data are invalidated.

## Related Issues

No separate tracking issue is linked. The individual corrections are
listed above.

## Checklist

_Put an `x` in the boxes that apply. You can complete these step by step
after opening the PR._

- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [ ] I have added all necessary documentation.
- [x] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.
2026-10-21 05:37:29 +02:00

1358 lines
57 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
// CA2000: test code; disposables are ownership-transferred to the test
// harness or are short-lived, making CA2000 noisy without a real leak risk.
#pragma warning disable CA2000
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
namespace Opc.Ua.Server.Tests.NodeManager
{
/// <summary>
/// Deterministic, offline unit tests that drive the synchronous surface of
/// <see cref="CustomNodeManager2"/> (the members implemented in
/// CustomNodeManager.cs) directly against a fully mocked
/// <see cref="IServerInternal"/>. No sockets, no sampling timers and no
/// wall-clock assertions are used, so the tests are order independent and
/// stable on every target framework.
/// </summary>
[TestFixture]
[Category("NodeManager")]
[Category("CustomNodeManagerDeterministic")]
[Parallelizable(ParallelScope.All)]
public sealed class CustomNodeManagerDeterministicTests
{
private const string TestNamespaceUri = "http://test.org/UA/CustomNodeManagerDeterministic/";
[Test]
public void CreateNode_AddsNodeToPredefinedNodesAndEmitsModelChange()
{
using Harness h = CreateHarness();
ushort ns = h.NamespaceIndex;
var instance = new BaseObjectState(null);
// Opt into unconditional emission so the model-change event fires for a
// node that carries no NodeVersion property (the default requires one).
h.Manager.RequireNodeVersionForModelChange = false;
NodeId resultId = h.Manager.CreateNode(
h.Context,
NodeId.Null,
ReferenceTypeIds.Organizes,
new QualifiedName("Created", ns),
instance);
Assert.That(resultId.IsNull, Is.False);
Assert.That(resultId, Is.EqualTo(instance.NodeId));
Assert.That(instance.BrowseName, Is.EqualTo(new QualifiedName("Created", ns)));
Assert.That(h.Manager.PredefinedNodes.ContainsKey(resultId), Is.True);
Assert.That(h.Manager.PredefinedNodes[resultId], Is.SameAs(instance));
// The model-change event is emitted through Server.ReportEvent.
h.MockServer.Verify(s => s.ReportEvent(It.IsAny<IFilterTarget>()), Times.AtLeastOnce);
}
[Test]
public void CreateNode_WithUnknownParent_ThrowsBadNodeIdUnknown()
{
using Harness h = CreateHarness();
ushort ns = h.NamespaceIndex;
var child = new BaseObjectState(null)
{
NodeId = new NodeId("OrphanChild", ns)
};
ServiceResultException ex = Assert.Throws<ServiceResultException>(
() => h.Manager.CreateNode(
h.Context,
new NodeId("MissingParent", ns),
ReferenceTypeIds.Organizes,
new QualifiedName("OrphanChild", ns),
child))!;
Assert.That(ex.StatusCode, Is.EqualTo(StatusCodes.BadNodeIdUnknown));
}
[Test]
public void AddPredefinedNode_AddsNodeToPredefinedNodes()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewVariable("Added", 7);
h.Manager.AddPredefinedNodePublic(h.Context, variable);
Assert.That(h.Manager.PredefinedNodes.ContainsKey(variable.NodeId), Is.True);
Assert.That(h.Manager.PredefinedNodes[variable.NodeId], Is.SameAs(variable));
}
[Test]
public void DeleteNode_RemovesKnownNodeAndReturnsTrue()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("ToDelete");
h.Manager.AddPredefinedNodePublic(h.Context, node);
Assert.That(h.Manager.PredefinedNodes.ContainsKey(node.NodeId), Is.True);
bool result = h.Manager.DeleteNode(h.Context, node.NodeId);
Assert.That(result, Is.True);
Assert.That(h.Manager.PredefinedNodes.ContainsKey(node.NodeId), Is.False);
}
[Test]
public void DeleteNode_ReturnsFalseForUnknownNode()
{
using Harness h = CreateHarness();
bool result = h.Manager.DeleteNode(h.Context, new NodeId("Ghost", h.NamespaceIndex));
Assert.That(result, Is.False);
}
[Test]
public void CreateAddressSpace_LoadsNodesFromOverride()
{
using Harness h = CreateHarness();
var folder = new FolderState(null);
folder.CreateAsPredefinedNode(h.Context);
folder.NodeId = new NodeId("Folder", h.NamespaceIndex);
folder.BrowseName = new QualifiedName("Folder", h.NamespaceIndex);
folder.DisplayName = new LocalizedText("Folder");
h.Manager.NodesToLoad = [folder];
h.Manager.CreateAddressSpace(new Dictionary<NodeId, IList<IReference>>());
Assert.That(h.Manager.PredefinedNodes.ContainsKey(folder.NodeId), Is.True);
Assert.That(h.Manager.PredefinedNodes[folder.NodeId], Is.SameAs(folder));
}
[Test]
public void DeleteAddressSpace_ClearsPredefinedNodes()
{
using Harness h = CreateHarness();
h.Manager.AddPredefinedNodePublic(h.Context, h.NewObject("A"));
h.Manager.AddPredefinedNodePublic(h.Context, h.NewObject("B"));
Assert.That(h.Manager.PredefinedNodes, Has.Count.EqualTo(2));
h.Manager.DeleteAddressSpace();
Assert.That(h.Manager.PredefinedNodes, Is.Empty);
}
[Test]
public void DeleteAddressSpaceRemovesRootNotifiers()
{
using Harness h = CreateHarness();
BaseObjectState notifier = h.NewObject("Notifier");
h.Manager.AddPredefinedNodePublic(h.Context, notifier);
h.Manager.AddRootNotifierPublic(notifier);
Assert.That(h.Manager.RootNotifiersDictionary.ContainsKey(notifier.NodeId), Is.True);
h.Manager.DeleteAddressSpace();
Assert.That(h.Manager.RootNotifiersDictionary, Is.Empty);
}
[Test]
public void DeleteAddressSpaceTracksAndClearsRemovedExternalReferences()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("WithExternalRef");
h.Manager.AddPredefinedNodePublic(h.Context, node);
var externalTarget = new NodeId("External", h.NamespaceIndex);
node.AddReference(ReferenceTypeIds.HasNotifier, false, externalTarget);
Assert.That(h.Manager.GetRemovedExternalReferences(), Is.Empty);
h.Manager.DeleteAddressSpace();
List<LocalReference> removed = h.Manager.GetRemovedExternalReferences();
Assert.That(
removed,
Has.Some.Matches<LocalReference>(
r => r is not null && r.SourceId == externalTarget && r.TargetId == node.NodeId));
h.Manager.ClearRemovedExternalReferences();
Assert.That(h.Manager.GetRemovedExternalReferences(), Is.Empty);
}
[Test]
public void DeleteNodeWhenTypeNotOwnedByAnotherNodeManagerRemovesFromTypeTree()
{
using Harness h = CreateHarness();
BaseObjectTypeState type = h.NewObjectType("SoleType");
h.Manager.AddPredefinedNodePublic(h.Context, type);
Assert.That(h.MockServer.Object.TypeTree.IsKnown(type.NodeId), Is.True);
bool result = h.Manager.DeleteNode(h.Context, type.NodeId);
Assert.That(result, Is.True);
Assert.That(h.MockServer.Object.TypeTree.IsKnown(type.NodeId), Is.False);
}
[Test]
public void DeleteNodeWhenTypeOwnedByAnotherNodeManagerKeepsTypeTreeEntry()
{
using Harness h = CreateHarness();
BaseObjectTypeState type = h.NewObjectType("SharedType");
h.Manager.AddPredefinedNodePublic(h.Context, type);
Assert.That(h.MockServer.Object.TypeTree.IsKnown(type.NodeId), Is.True);
var otherSyncManager = new Mock<INodeManager>();
otherSyncManager.Setup(m => m.GetManagerHandle(type.NodeId)).Returns(new object());
var otherAsyncManager = new Mock<IAsyncNodeManager>();
otherAsyncManager.Setup(m => m.SyncNodeManager).Returns(otherSyncManager.Object);
var masterNodeManagerMock =
Mock.Get(h.MockServer.Object.NodeManager);
masterNodeManagerMock
.Setup(m => m.AsyncNodeManagers)
.Returns([h.Manager.ToAsyncNodeManager(), otherAsyncManager.Object]);
bool result = h.Manager.DeleteNode(h.Context, type.NodeId);
Assert.That(result, Is.True);
Assert.That(h.Manager.PredefinedNodes.ContainsKey(type.NodeId), Is.False);
Assert.That(h.MockServer.Object.TypeTree.IsKnown(type.NodeId), Is.True);
}
[Test]
public void RebuildTypeTreeRegistersSubtypeAndEncodingRelationshipsForDirectlyInsertedNodes()
{
using Harness h = CreateHarness();
ushort ns = h.NamespaceIndex;
BaseObjectTypeState type = h.NewObjectType("RebuiltType");
// Insert directly into PredefinedNodes to bypass AddPredefinedNode's own
// automatic type-tree wiring, mirroring nodes restored after a reload where
// the caller explicitly triggers RebuildTypeTree afterwards.
h.Manager.PredefinedNodes.Add(type.NodeId, type);
Assert.That(h.MockServer.Object.TypeTree.IsKnown(type.NodeId), Is.False);
var dataTypeId = new NodeId("RebuiltDataType", ns);
h.MockServer.Object.TypeTree.AddSubtype(dataTypeId, NodeId.Null);
BaseObjectState encoding = h.NewObject("RebuiltEncoding");
encoding.AddReference(ReferenceTypeIds.HasEncoding, true, dataTypeId);
h.Manager.PredefinedNodes.Add(encoding.NodeId, encoding);
Assert.That(h.MockServer.Object.TypeTree.IsEncodingOf(encoding.NodeId, dataTypeId), Is.False);
h.Manager.RebuildTypeTree();
Assert.That(h.MockServer.Object.TypeTree.IsKnown(type.NodeId), Is.True);
Assert.That(h.MockServer.Object.TypeTree.IsEncodingOf(encoding.NodeId, dataTypeId), Is.True);
}
[Test]
public void DisposeCalledTwiceDoesNotThrow()
{
Harness h = CreateHarness();
h.Manager.AddPredefinedNodePublic(h.Context, h.NewObject("DisposeTarget"));
h.Manager.Dispose();
Assert.DoesNotThrow(h.Manager.Dispose);
}
[Test]
public void FindPredefinedNodeGeneric_ReturnsNodeWhenTypeMatches()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewVariable("Find", 1);
h.Manager.AddPredefinedNodePublic(h.Context, variable);
BaseDataVariableState? found = h.Manager.FindPredefinedNode<BaseDataVariableState>(variable.NodeId);
Assert.That(found, Is.SameAs(variable));
}
[Test]
public void FindPredefinedNodeGeneric_ReturnsNullWhenTypeMismatch()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("TypeMismatch");
h.Manager.AddPredefinedNodePublic(h.Context, node);
BaseDataVariableState? found = h.Manager.FindPredefinedNode<BaseDataVariableState>(node.NodeId);
Assert.That(found, Is.Null);
}
[Test]
public void FindPredefinedNodeGeneric_ReturnsNullWhenNotFound()
{
using Harness h = CreateHarness();
NodeState? found = h.Manager.FindPredefinedNode<NodeState>(new NodeId("Unknown", h.NamespaceIndex));
Assert.That(found, Is.Null);
}
[Test]
public void FindPredefinedNodeGeneric_ReturnsNullForNullNodeId()
{
using Harness h = CreateHarness();
NodeState? found = h.Manager.FindPredefinedNode<NodeState>(NodeId.Null);
Assert.That(found, Is.Null);
}
[Test]
public void FindPredefinedNodeObsolete_ChecksExpectedType()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("Obsolete");
h.Manager.AddPredefinedNodePublic(h.Context, node);
#pragma warning disable CS0618, CA2263 // exercising the obsolete overload on purpose.
NodeState? match = h.Manager.FindPredefinedNode(node.NodeId, typeof(BaseObjectState));
NodeState? mismatch = h.Manager.FindPredefinedNode(node.NodeId, typeof(BaseDataVariableState));
NodeState? missing = h.Manager.FindPredefinedNode(new NodeId("None", h.NamespaceIndex), typeof(NodeState));
#pragma warning restore CS0618, CA2263
Assert.That(match, Is.SameAs(node));
Assert.That(mismatch, Is.Null);
Assert.That(missing, Is.Null);
}
[Test]
public void GetManagerHandle_ReturnsValidatedHandleForKnownNode()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("Handle");
h.Manager.AddPredefinedNodePublic(h.Context, node);
object? handle = h.Manager.GetManagerHandle(node.NodeId);
Assert.That(handle, Is.InstanceOf<NodeHandle>());
var nodeHandle = (NodeHandle)handle!;
Assert.That(nodeHandle.NodeId, Is.EqualTo(node.NodeId));
Assert.That(nodeHandle.Node, Is.SameAs(node));
Assert.That(nodeHandle.Validated, Is.True);
}
[Test]
public void GetManagerHandle_ReturnsNullForUnknownNode()
{
using Harness h = CreateHarness();
object? foreignHandle = h.Manager.GetManagerHandle(ObjectIds.Server);
object? unknownHandle = h.Manager.GetManagerHandle(new NodeId("Unknown", h.NamespaceIndex));
Assert.That(foreignHandle, Is.Null);
Assert.That(unknownHandle, Is.Null);
}
[Test]
public void Read_ReturnsValueForKnownNode()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewVariable("ReadVar", 42);
h.Manager.AddPredefinedNodePublic(h.Context, variable);
var nodesToRead = new List<ReadValueId>
{
new() { NodeId = variable.NodeId, AttributeId = Attributes.Value },
new() { NodeId = ObjectIds.Server, AttributeId = Attributes.Value }
};
var values = new List<DataValue> { new() };
var errors = new List<ServiceResult> { ServiceResult.Good };
h.Manager.Read(h.NewContext(RequestType.Read), 0, nodesToRead, values, errors);
Assert.That(nodesToRead[0].Processed, Is.True);
Assert.That(nodesToRead[1].Processed, Is.False);
Assert.That(ServiceResult.IsGood(errors[0]), Is.True);
Assert.That((int)values[0].WrappedValue, Is.EqualTo(42));
Assert.That(values[0].StatusCode, Is.EqualTo(StatusCodes.Good));
Assert.That(values[0].ServerTimestamp, Is.EqualTo(values[0].SourceTimestamp));
}
[Test]
public void Read_StampsFreshServerTimestampForStaticNode()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewVariable("StaleVar", 42);
// simulate a static node whose value was produced in the past.
DateTimeUtc staleTimestamp = DateTimeUtc.Now - TimeSpan.FromMinutes(1);
variable.Timestamp = staleTimestamp;
h.Manager.AddPredefinedNodePublic(h.Context, variable);
var nodesToRead = new List<ReadValueId>
{
new() { NodeId = variable.NodeId, AttributeId = Attributes.Value }
};
var values = new List<DataValue> { new() };
var errors = new List<ServiceResult> { ServiceResult.Good };
DateTimeUtc beforeRead = DateTimeUtc.Now;
h.Manager.Read(h.NewContext(RequestType.Read), 0, nodesToRead, values, errors);
Assert.That(ServiceResult.IsGood(errors[0]), Is.True);
// SourceTimestamp reflects the (stale) time the value was produced.
Assert.That(values[0].SourceTimestamp, Is.EqualTo(staleTimestamp));
// ServerTimestamp reflects when the server verified the value: the
// read time, so a MaxAge read is satisfied even for a static value.
Assert.That(values[0].ServerTimestamp, Is.GreaterThanOrEqualTo(beforeRead));
Assert.That(values[0].ServerTimestamp, Is.Not.EqualTo(values[0].SourceTimestamp));
}
[Test]
public void Read_ReturnsBrowseNameForNonValueAttribute()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewVariable("MetaVar", 1);
h.Manager.AddPredefinedNodePublic(h.Context, variable);
var nodesToRead = new List<ReadValueId>
{
new() { NodeId = variable.NodeId, AttributeId = Attributes.BrowseName }
};
var values = new List<DataValue> { new() };
var errors = new List<ServiceResult> { ServiceResult.Good };
h.Manager.Read(h.NewContext(RequestType.Read), 0, nodesToRead, values, errors);
Assert.That(ServiceResult.IsGood(errors[0]), Is.True);
Assert.That(values[0].StatusCode, Is.EqualTo(StatusCodes.Good));
var browseName = (QualifiedName)values[0].WrappedValue;
Assert.That(browseName, Is.EqualTo(new QualifiedName("MetaVar", h.NamespaceIndex)));
}
[Test]
public void Read_ReturnsBadAttributeIdInvalidForInvalidAttribute()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("ReadObject");
h.Manager.AddPredefinedNodePublic(h.Context, node);
var nodesToRead = new List<ReadValueId>
{
// Value is not a valid attribute of an Object node.
new() { NodeId = node.NodeId, AttributeId = Attributes.Value }
};
var values = new List<DataValue> { new() };
var errors = new List<ServiceResult> { ServiceResult.Good };
h.Manager.Read(h.NewContext(RequestType.Read), 0, nodesToRead, values, errors);
Assert.That(nodesToRead[0].Processed, Is.True);
Assert.That(errors[0].StatusCode, Is.EqualTo(StatusCodes.BadAttributeIdInvalid));
}
[Test]
public void Read_ReturnsBadNodeIdUnknownForPlaceholderNode()
{
using Harness h = CreatePlaceholderHarness(out PlaceholderNodeManager manager);
manager.PlaceholderNodeId = new NodeId("Ghost", h.NamespaceIndex);
var nodesToRead = new List<ReadValueId>
{
new() { NodeId = manager.PlaceholderNodeId, AttributeId = Attributes.Value }
};
var values = new List<DataValue> { new() };
var errors = new List<ServiceResult> { ServiceResult.Good };
manager.Read(h.NewContext(RequestType.Read), 0, nodesToRead, values, errors);
Assert.That(nodesToRead[0].Processed, Is.True);
Assert.That(errors[0].StatusCode, Is.EqualTo(StatusCodes.BadNodeIdUnknown));
}
[Test]
public void Write_WritesValueToKnownNode()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewWritableVariable("WriteVar", 0);
h.Manager.AddPredefinedNodePublic(h.Context, variable);
var nodesToWrite = new List<WriteValue>
{
new() {
NodeId = variable.NodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(99))
},
new() {
NodeId = ObjectIds.Server,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(99))
}
};
var errors = new List<ServiceResult> { ServiceResult.Good };
h.Manager.Write(h.NewContext(RequestType.Write), nodesToWrite, errors);
Assert.That(nodesToWrite[0].Processed, Is.True);
Assert.That(nodesToWrite[1].Processed, Is.False);
Assert.That(ServiceResult.IsGood(errors[0]), Is.True);
Assert.That(variable.Value, Is.EqualTo(99));
}
[Test]
public void Write_ReturnsBadNotWritableForReadOnlyNode()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewVariable("ReadOnlyVar", 0);
h.Manager.AddPredefinedNodePublic(h.Context, variable);
var nodesToWrite = new List<WriteValue>
{
new() {
NodeId = variable.NodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(5))
}
};
var errors = new List<ServiceResult> { ServiceResult.Good };
h.Manager.Write(h.NewContext(RequestType.Write), nodesToWrite, errors);
Assert.That(errors[0].StatusCode, Is.EqualTo(StatusCodes.BadNotWritable));
}
[Test]
public void Write_ReturnsBadWriteNotSupportedForNonValueAttributeWithIndexRange()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewWritableVariable("IdxRangeVar", 0);
h.Manager.AddPredefinedNodePublic(h.Context, variable);
var nodesToWrite = new List<WriteValue>
{
new() {
NodeId = variable.NodeId,
AttributeId = Attributes.DisplayName,
IndexRange = "0",
Value = new DataValue(new Variant(new LocalizedText("X")))
}
};
var errors = new List<ServiceResult> { ServiceResult.Good };
h.Manager.Write(h.NewContext(RequestType.Write), nodesToWrite, errors);
Assert.That(nodesToWrite[0].Processed, Is.True);
Assert.That(errors[0].StatusCode, Is.EqualTo(StatusCodes.BadWriteNotSupported));
}
[Test]
public void Write_ReturnsBadTypeMismatchForWrongValueType()
{
using Harness h = CreateHarness();
BaseDataVariableState variable = h.NewWritableVariable("TypeVar", 0);
h.Manager.AddPredefinedNodePublic(h.Context, variable);
var nodesToWrite = new List<WriteValue>
{
new() {
NodeId = variable.NodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant("not-an-int"))
}
};
var errors = new List<ServiceResult> { ServiceResult.Good };
h.Manager.Write(h.NewContext(RequestType.Write), nodesToWrite, errors);
Assert.That(errors[0].StatusCode, Is.EqualTo(StatusCodes.BadTypeMismatch));
}
[Test]
public void Write_ReturnsBadNodeIdUnknownForPlaceholderNode()
{
using Harness h = CreatePlaceholderHarness(out PlaceholderNodeManager manager);
manager.PlaceholderNodeId = new NodeId("Ghost", h.NamespaceIndex);
var nodesToWrite = new List<WriteValue>
{
new() {
NodeId = manager.PlaceholderNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(1))
}
};
var errors = new List<ServiceResult> { ServiceResult.Good };
manager.Write(h.NewContext(RequestType.Write), nodesToWrite, errors);
Assert.That(nodesToWrite[0].Processed, Is.True);
Assert.That(errors[0].StatusCode, Is.EqualTo(StatusCodes.BadNodeIdUnknown));
}
[Test]
public void Browse_ReturnsChildReferences()
{
using Harness h = CreateHarness();
(BaseObjectState parent, BaseObjectState child) = h.AddParentWithChild(ReferenceTypeIds.Organizes);
object handle = h.Manager.GetManagerHandle(parent.NodeId)!;
ContinuationPoint continuationPoint = h.NewContinuationPoint(handle);
var references = new List<ReferenceDescription>();
h.Manager.Browse(h.NewContext(RequestType.Browse), ref continuationPoint, references);
Assert.That(continuationPoint, Is.Null);
Assert.That(references, Has.Count.EqualTo(1));
Assert.That(references[0].BrowseName, Is.EqualTo(child.BrowseName));
Assert.That(references[0].NodeId, Is.EqualTo(new ExpandedNodeId(child.NodeId)));
}
[Test]
public void Browse_WithMaxResultsToReturn_LeavesContinuationPoint()
{
using Harness h = CreateHarness();
BaseObjectState parent = h.NewObject("BrowseParent");
h.Manager.AddPredefinedNodePublic(h.Context, parent);
BaseObjectState child1 = h.NewObject("BrowseChild1");
BaseObjectState child2 = h.NewObject("BrowseChild2");
h.Manager.AddPredefinedNodePublic(h.Context, child1);
h.Manager.AddPredefinedNodePublic(h.Context, child2);
parent.AddReference(ReferenceTypeIds.Organizes, false, child1.NodeId);
parent.AddReference(ReferenceTypeIds.Organizes, false, child2.NodeId);
object handle = h.Manager.GetManagerHandle(parent.NodeId)!;
ContinuationPoint continuationPoint = h.NewContinuationPoint(handle);
continuationPoint.MaxResultsToReturn = 1;
var references = new List<ReferenceDescription>();
h.Manager.Browse(h.NewContext(RequestType.Browse), ref continuationPoint, references);
Assert.That(references, Has.Count.EqualTo(1));
Assert.That(continuationPoint, Is.Not.Null);
continuationPoint.Dispose();
}
[Test]
public void Browse_WithReferenceTypeFilter_ExcludesNonMatchingReferences()
{
using Harness h = CreateHarness();
(BaseObjectState parent, _) = h.AddParentWithChild(ReferenceTypeIds.Organizes);
object handle = h.Manager.GetManagerHandle(parent.NodeId)!;
ContinuationPoint continuationPoint = h.NewContinuationPoint(handle);
// The child is linked with Organizes only, so a HasComponent filter
// (without subtypes) must return no references.
continuationPoint.ReferenceTypeId = ReferenceTypeIds.HasComponent;
continuationPoint.IncludeSubtypes = false;
var references = new List<ReferenceDescription>();
h.Manager.Browse(h.NewContext(RequestType.Browse), ref continuationPoint, references);
Assert.That(references, Is.Empty);
}
[Test]
public void Browse_WithInverseDirection_ReturnsParentReference()
{
using Harness h = CreateHarness();
BaseObjectState parent = h.NewObject("InverseParent");
BaseObjectState child = h.NewObject("InverseChild");
h.Manager.AddPredefinedNodePublic(h.Context, parent);
h.Manager.AddPredefinedNodePublic(h.Context, child);
parent.AddReference(ReferenceTypeIds.Organizes, false, child.NodeId);
child.AddReference(ReferenceTypeIds.Organizes, true, parent.NodeId);
object handle = h.Manager.GetManagerHandle(child.NodeId)!;
ContinuationPoint continuationPoint = h.NewContinuationPoint(handle);
continuationPoint.BrowseDirection = BrowseDirection.Inverse;
var references = new List<ReferenceDescription>();
h.Manager.Browse(h.NewContext(RequestType.Browse), ref continuationPoint, references);
Assert.That(references, Has.Count.EqualTo(1));
Assert.That(references[0].NodeId, Is.EqualTo(new ExpandedNodeId(parent.NodeId)));
}
[Test]
public void Browse_NullContinuationPoint_ThrowsArgumentNullException()
{
using Harness h = CreateHarness();
ContinuationPoint continuationPoint = null!;
var references = new List<ReferenceDescription>();
Assert.Throws<ArgumentNullException>(
() => h.Manager.Browse(h.NewContext(RequestType.Browse), ref continuationPoint, references));
}
[Test]
public void Browse_NullReferences_ThrowsArgumentNullException()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("BrowseNullRefs");
h.Manager.AddPredefinedNodePublic(h.Context, node);
object handle = h.Manager.GetManagerHandle(node.NodeId)!;
ContinuationPoint continuationPoint = h.NewContinuationPoint(handle);
Assert.Throws<ArgumentNullException>(
() => h.Manager.Browse(h.NewContext(RequestType.Browse), ref continuationPoint, null!));
continuationPoint.Dispose();
}
[Test]
public void Browse_ForeignHandle_ThrowsBadNodeIdUnknown()
{
using Harness h = CreateHarness();
var foreignHandle = new NodeHandle { NodeId = ObjectIds.Server };
ContinuationPoint continuationPoint = h.NewContinuationPoint(foreignHandle);
var references = new List<ReferenceDescription>();
ServiceResultException ex = Assert.Throws<ServiceResultException>(
() => h.Manager.Browse(h.NewContext(RequestType.Browse), ref continuationPoint, references))!;
Assert.That(ex.StatusCode, Is.EqualTo(StatusCodes.BadNodeIdUnknown));
}
[Test]
public void Browse_UnknownView_ThrowsBadViewIdUnknown()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("BrowseView");
h.Manager.AddPredefinedNodePublic(h.Context, node);
object handle = h.Manager.GetManagerHandle(node.NodeId)!;
ContinuationPoint continuationPoint = h.NewContinuationPoint(handle);
continuationPoint.View = new ViewDescription { ViewId = new NodeId("NoView", h.NamespaceIndex) };
var references = new List<ReferenceDescription>();
ServiceResultException ex = Assert.Throws<ServiceResultException>(
() => h.Manager.Browse(h.NewContext(RequestType.Browse), ref continuationPoint, references))!;
Assert.That(ex.StatusCode, Is.EqualTo(StatusCodes.BadViewIdUnknown));
continuationPoint.Dispose();
}
[Test]
public void TranslateBrowsePath_ResolvesTarget()
{
using Harness h = CreateHarness();
(BaseObjectState parent, BaseObjectState child) = h.AddParentWithChild(ReferenceTypeIds.Organizes);
object handle = h.Manager.GetManagerHandle(parent.NodeId)!;
var relativePath = new RelativePathElement
{
IncludeSubtypes = true,
IsInverse = false,
TargetName = child.BrowseName
};
var targetIds = new List<ExpandedNodeId>();
var unresolved = new List<NodeId>();
h.Manager.TranslateBrowsePath(
h.NewContext(RequestType.TranslateBrowsePathsToNodeIds),
handle,
relativePath,
targetIds,
unresolved);
Assert.That(targetIds, Has.Count.EqualTo(1));
Assert.That(targetIds[0], Is.EqualTo(new ExpandedNodeId(child.NodeId)));
Assert.That(unresolved, Is.Empty);
}
[Test]
public void TranslateBrowsePath_NoMatch_ReturnsEmptyTargets()
{
using Harness h = CreateHarness();
(BaseObjectState parent, _) = h.AddParentWithChild(ReferenceTypeIds.Organizes);
object handle = h.Manager.GetManagerHandle(parent.NodeId)!;
var relativePath = new RelativePathElement
{
IncludeSubtypes = true,
IsInverse = false,
TargetName = new QualifiedName("DoesNotExist", h.NamespaceIndex)
};
var targetIds = new List<ExpandedNodeId>();
var unresolved = new List<NodeId>();
h.Manager.TranslateBrowsePath(
h.NewContext(RequestType.TranslateBrowsePathsToNodeIds),
handle,
relativePath,
targetIds,
unresolved);
Assert.That(targetIds, Is.Empty);
Assert.That(unresolved, Is.Empty);
}
[Test]
public void TranslateBrowsePath_ForeignHandle_ReturnsEmptyTargets()
{
using Harness h = CreateHarness();
var foreignHandle = new NodeHandle { NodeId = ObjectIds.Server };
var relativePath = new RelativePathElement
{
TargetName = new QualifiedName("Anything", h.NamespaceIndex)
};
var targetIds = new List<ExpandedNodeId>();
var unresolved = new List<NodeId>();
h.Manager.TranslateBrowsePath(
h.NewContext(RequestType.TranslateBrowsePathsToNodeIds),
foreignHandle,
relativePath,
targetIds,
unresolved);
Assert.That(targetIds, Is.Empty);
Assert.That(unresolved, Is.Empty);
}
[Test]
public void GetNodeMetadata_ReturnsMetadataForNode()
{
using Harness h = CreateHarness();
var variable = new BaseDataVariableState(null);
variable.CreateAsPredefinedNode(h.Context);
variable.NodeId = new NodeId("MetaVar", h.NamespaceIndex);
variable.BrowseName = new QualifiedName("MetaVar", h.NamespaceIndex);
variable.DisplayName = new LocalizedText("MetaVar");
variable.DataType = DataTypeIds.Int32;
variable.ValueRank = ValueRanks.Scalar;
variable.AccessLevel = AccessLevels.CurrentRead;
variable.UserAccessLevel = AccessLevels.CurrentRead;
variable.Value = 10;
h.Manager.AddPredefinedNodePublic(h.Context, variable);
object handle = h.Manager.GetManagerHandle(variable.NodeId)!;
NodeMetadata? metadata = h.Manager.GetNodeMetadata(
h.NewContext(RequestType.Read), handle, BrowseResultMask.All);
Assert.That(metadata, Is.Not.Null);
Assert.That(metadata!.NodeClass, Is.EqualTo(NodeClass.Variable));
Assert.That(metadata.DataType, Is.EqualTo(DataTypeIds.Int32));
Assert.That(metadata.ValueRank, Is.EqualTo(ValueRanks.Scalar));
Assert.That(metadata.AccessLevel, Is.EqualTo(AccessLevels.CurrentRead));
}
[Test]
public void GetNodeMetadata_ForeignHandle_ReturnsNull()
{
using Harness h = CreateHarness();
var foreignHandle = new NodeHandle { NodeId = ObjectIds.Server };
NodeMetadata? metadata = h.Manager.GetNodeMetadata(
h.NewContext(RequestType.Read), foreignHandle, BrowseResultMask.All);
Assert.That(metadata, Is.Null);
}
[Test]
public void GetNodeMetadata_UnvalidatedHandle_ReturnsNull()
{
using Harness h = CreateHarness();
// In namespace, but not backed by a node in memory.
var placeholder = new NodeHandle { NodeId = new NodeId("Ghost", h.NamespaceIndex) };
NodeMetadata? metadata = h.Manager.GetNodeMetadata(
h.NewContext(RequestType.Read), placeholder, BrowseResultMask.All);
Assert.That(metadata, Is.Null);
}
[Test]
public void AddReferences_AddsExternalReferenceAndIsIdempotent()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("RefSource");
h.Manager.AddPredefinedNodePublic(h.Context, node);
var targetId = new NodeId("Target", 0);
var references = new Dictionary<NodeId, IList<IReference>>
{
[node.NodeId] =
[
new ReferenceNode
{
ReferenceTypeId = ReferenceTypeIds.HasComponent,
IsInverse = false,
TargetId = targetId
}
],
// Unknown source node is silently skipped.
[new NodeId("Missing", h.NamespaceIndex)] =
[
new ReferenceNode
{
ReferenceTypeId = ReferenceTypeIds.HasComponent,
IsInverse = false,
TargetId = targetId
}
]
};
h.Manager.AddReferences(references);
h.Manager.AddReferences(references);
var refs = new List<IReference>();
node.GetReferences(h.Context, refs);
List<IReference> matching = refs.FindAll(
r => r.TargetId == targetId && r.ReferenceTypeId == ReferenceTypeIds.HasComponent);
Assert.That(matching, Has.Count.EqualTo(1));
}
[Test]
public void DeleteReference_RemovesBidirectionalReference()
{
using Harness h = CreateHarness();
BaseObjectState source = h.NewObject("DelSource");
BaseObjectState target = h.NewObject("DelTarget");
h.Manager.AddPredefinedNodePublic(h.Context, source);
h.Manager.AddPredefinedNodePublic(h.Context, target);
source.AddReference(ReferenceTypeIds.Organizes, false, target.NodeId);
target.AddReference(ReferenceTypeIds.Organizes, true, source.NodeId);
object handle = h.Manager.GetManagerHandle(source.NodeId)!;
ServiceResult result = h.Manager.DeleteReference(
handle, ReferenceTypeIds.Organizes, false, target.NodeId, true);
Assert.That(ServiceResult.IsGood(result), Is.True);
Assert.That(source.ReferenceExists(ReferenceTypeIds.Organizes, false, target.NodeId), Is.False);
Assert.That(target.ReferenceExists(ReferenceTypeIds.Organizes, true, source.NodeId), Is.False);
}
[Test]
public void DeleteReference_ForeignHandle_ReturnsBadNodeIdUnknown()
{
using Harness h = CreateHarness();
var foreignHandle = new NodeHandle { NodeId = ObjectIds.Server };
ServiceResult result = h.Manager.DeleteReference(
foreignHandle, ReferenceTypeIds.Organizes, false, ObjectIds.Server, false);
Assert.That(result.StatusCode, Is.EqualTo(StatusCodes.BadNodeIdUnknown));
}
[Test]
public void DeleteReference_UnvalidatedHandle_ReturnsBadNotSupported()
{
using Harness h = CreateHarness();
// In namespace, but not validated / no node in memory.
var placeholder = new NodeHandle { NodeId = new NodeId("Ghost", h.NamespaceIndex) };
ServiceResult result = h.Manager.DeleteReference(
placeholder, ReferenceTypeIds.Organizes, false, ObjectIds.Server, false);
Assert.That(result.StatusCode, Is.EqualTo(StatusCodes.BadNotSupported));
}
[Test]
public void CreateMonitoredItems_SkipsNodeNotOwnedByManager()
{
using Harness h = CreateHarness();
var itemToCreate = new MonitoredItemCreateRequest
{
ItemToMonitor = new ReadValueId { NodeId = ObjectIds.Server, AttributeId = Attributes.Value },
RequestedParameters = new MonitoringParameters { SamplingInterval = -1, QueueSize = 1 }
};
var errors = new List<ServiceResult> { ServiceResult.Good };
var filterErrors = new List<MonitoringFilterResult> { null! };
var monitoredItems = new List<IMonitoredItem> { null! };
var idFactory = new MonitoredItemIdFactory();
h.Manager.CreateMonitoredItems(
h.NewContext(RequestType.CreateMonitoredItems),
1, 100, TimestampsToReturn.Both,
new List<MonitoredItemCreateRequest> { itemToCreate },
errors, filterErrors, monitoredItems, false, idFactory);
Assert.That(itemToCreate.Processed, Is.False);
Assert.That(monitoredItems[0], Is.Null);
}
[Test]
public void CreateMonitoredItems_ReturnsBadAttributeIdInvalidForInvalidAttribute()
{
using Harness h = CreateHarness();
BaseObjectState node = h.NewObject("MonitorObject");
h.Manager.AddPredefinedNodePublic(h.Context, node);
var itemToCreate = new MonitoredItemCreateRequest
{
// Value is invalid for an Object node.
ItemToMonitor = new ReadValueId { NodeId = node.NodeId, AttributeId = Attributes.Value },
RequestedParameters = new MonitoringParameters { SamplingInterval = -1, QueueSize = 1 }
};
var errors = new List<ServiceResult> { ServiceResult.Good };
var filterErrors = new List<MonitoringFilterResult> { null! };
var monitoredItems = new List<IMonitoredItem> { null! };
var idFactory = new MonitoredItemIdFactory();
h.Manager.CreateMonitoredItems(
h.NewContext(RequestType.CreateMonitoredItems),
1, 100, TimestampsToReturn.Both,
new List<MonitoredItemCreateRequest> { itemToCreate },
errors, filterErrors, monitoredItems, false, idFactory);
Assert.That(itemToCreate.Processed, Is.True);
Assert.That(errors[0].StatusCode, Is.EqualTo(StatusCodes.BadAttributeIdInvalid));
Assert.That(monitoredItems[0], Is.Null);
}
[Test]
public void ModifyMonitoredItems_SkipsNullItem()
{
using Harness h = CreateHarness();
var errors = new List<ServiceResult> { ServiceResult.Good };
var filterErrors = new List<MonitoringFilterResult> { null! };
var itemsToModify = new List<MonitoredItemModifyRequest> { new() };
var monitoredItems = new List<IMonitoredItem> { null! };
h.Manager.ModifyMonitoredItems(
h.NewContext(RequestType.ModifyMonitoredItems),
TimestampsToReturn.Both, monitoredItems, itemsToModify, errors, filterErrors);
Assert.That(itemsToModify[0].Processed, Is.False);
Assert.That(ServiceResult.IsGood(errors[0]), Is.True);
}
[Test]
public void ModifyMonitoredItems_SkipsForeignHandle()
{
using Harness h = CreateHarness();
var mockItem = new Mock<IMonitoredItem>();
mockItem.Setup(m => m.ManagerHandle).Returns(new NodeHandle { NodeId = new NodeId("x", 0) });
var errors = new List<ServiceResult> { ServiceResult.Good };
var filterErrors = new List<MonitoringFilterResult> { null! };
var itemsToModify = new List<MonitoredItemModifyRequest> { new() };
var monitoredItems = new List<IMonitoredItem> { mockItem.Object };
h.Manager.ModifyMonitoredItems(
h.NewContext(RequestType.ModifyMonitoredItems),
TimestampsToReturn.Both, monitoredItems, itemsToModify, errors, filterErrors);
Assert.That(itemsToModify[0].Processed, Is.False);
}
[Test]
public void DeleteMonitoredItems_SkipsNullItem()
{
using Harness h = CreateHarness();
var errors = new List<ServiceResult> { ServiceResult.Good };
var processed = new List<bool> { false };
var monitoredItems = new List<IMonitoredItem> { null! };
h.Manager.DeleteMonitoredItems(
h.NewContext(RequestType.DeleteMonitoredItems), monitoredItems, processed, errors);
Assert.That(processed[0], Is.False);
Assert.That(ServiceResult.IsGood(errors[0]), Is.True);
}
[Test]
public void DeleteMonitoredItems_SkipsForeignHandle()
{
using Harness h = CreateHarness();
var mockItem = new Mock<IMonitoredItem>();
mockItem.Setup(m => m.ManagerHandle).Returns(new NodeHandle { NodeId = new NodeId("x", 0) });
var errors = new List<ServiceResult> { ServiceResult.Good };
var processed = new List<bool> { false };
var monitoredItems = new List<IMonitoredItem> { mockItem.Object };
h.Manager.DeleteMonitoredItems(
h.NewContext(RequestType.DeleteMonitoredItems), monitoredItems, processed, errors);
Assert.That(processed[0], Is.False);
}
[Test]
public void IsNodeInView_ReturnsFalseForNonHandle()
{
using Harness h = CreateHarness();
bool result = h.Manager.IsNodeInView(
h.NewContext(RequestType.Browse), ObjectIds.ViewsFolder, new object());
Assert.That(result, Is.False);
}
[Test]
public void IsNodeInView_ReturnsFalseWhenHandleNodeIsNull()
{
using Harness h = CreateHarness();
var placeholder = new NodeHandle { NodeId = new NodeId("Ghost", h.NamespaceIndex) };
bool result = h.Manager.IsNodeInView(
h.NewContext(RequestType.Browse), ObjectIds.ViewsFolder, placeholder);
Assert.That(result, Is.False);
}
[Test]
public void IsNodeInView_ValidatedHandleDelegatesToProtectedOverload()
{
// Regression test: the public IsNodeInView(OperationContext, NodeId, object)
// used to forward the OperationContext unchanged, so overload resolution bound
// back to the same public overload instead of the protected
// (ServerSystemContext, NodeId, NodeState) helper, and every call reported
// "not in view". It now copies the context and delegates to the overridable
// protected helper, which accepts any view that is a predefined node.
using Harness h = CreateHarness();
var view = new ViewState();
view.CreateAsPredefinedNode(h.Context);
view.NodeId = new NodeId("MyView", h.NamespaceIndex);
view.BrowseName = new QualifiedName("MyView", h.NamespaceIndex);
h.Manager.AddPredefinedNodePublic(h.Context, view);
BaseObjectState node = h.NewObject("ViewedNode");
h.Manager.AddPredefinedNodePublic(h.Context, node);
var handle = new NodeHandle(node.NodeId, node);
Assert.That(h.Manager.FindPredefinedNode<ViewState>(view.NodeId), Is.SameAs(view));
bool result = h.Manager.IsNodeInView(
h.NewContext(RequestType.Browse), view.NodeId, handle);
Assert.That(result, Is.True);
bool unknownView = h.Manager.IsNodeInView(
h.NewContext(RequestType.Browse), new NodeId("UnknownView", h.NamespaceIndex), handle);
Assert.That(unknownView, Is.False);
}
[Test]
public void SessionActivated_WithNoMonitoredNodes_DoesNotThrow()
{
using Harness h = CreateHarness();
Assert.DoesNotThrow(
() => h.Manager.SessionActivated(
h.NewContext(RequestType.ActivateSession), new NodeId("Session", h.NamespaceIndex)));
}
private static Harness CreateHarness()
{
Mock<IServerInternal> mockServer = BuildMockServer(out MonitoredItemQueueFactory queueFactory);
var manager = new TestableCustomNodeManager2(
mockServer.Object, CreateConfiguration(), false, new Mock<ILogger>().Object, TestNamespaceUri);
return new Harness(manager, mockServer, queueFactory);
}
private static Harness CreatePlaceholderHarness(out PlaceholderNodeManager manager)
{
Mock<IServerInternal> mockServer = BuildMockServer(out MonitoredItemQueueFactory queueFactory);
manager = new PlaceholderNodeManager(
mockServer.Object, CreateConfiguration(), new Mock<ILogger>().Object, TestNamespaceUri);
return new Harness(manager, mockServer, queueFactory);
}
private static ApplicationConfiguration CreateConfiguration()
{
return new ApplicationConfiguration
{
ServerConfiguration = new ServerConfiguration
{
MaxNotificationQueueSize = 100,
MaxDurableNotificationQueueSize = 200
}
};
}
private static Mock<IServerInternal> BuildMockServer(out MonitoredItemQueueFactory queueFactory)
{
var mockServer = new Mock<IServerInternal>();
var mockMasterNodeManager = new Mock<IMasterNodeManager>();
var mockConfigurationNodeManager = new Mock<IConfigurationNodeManager>();
NamespaceMetadataState? noMetadata = null;
mockConfigurationNodeManager.As<INamespaceMetadataSnapshot>()
.Setup(manager => manager.TryGetNamespaceMetadataState(It.IsAny<ushort>(), out noMetadata))
.Returns(true);
var namespaceTable = new NamespaceTable();
namespaceTable.Append(TestNamespaceUri);
mockServer.Setup(s => s.NamespaceUris).Returns(namespaceTable);
mockServer.Setup(s => s.ServerUris).Returns(new StringTable());
mockServer.Setup(s => s.TypeTree).Returns(new TypeTable(namespaceTable));
mockServer.Setup(s => s.Factory).Returns(EncodeableFactory.Create());
mockServer.Setup(s => s.NodeManager).Returns(mockMasterNodeManager.Object);
mockMasterNodeManager.Setup(m => m.ConfigurationNodeManager)
.Returns(mockConfigurationNodeManager.Object);
mockMasterNodeManager
.Setup(m => m.AsyncNodeManagers)
.Returns([]);
var mockTelemetry = new Mock<ITelemetryContext>();
mockServer.Setup(s => s.Telemetry).Returns(mockTelemetry.Object);
queueFactory = new MonitoredItemQueueFactory(mockTelemetry.Object);
mockServer.Setup(s => s.MonitoredItemQueueFactory).Returns(queueFactory);
var serverSystemContext = new ServerSystemContext(mockServer.Object);
mockServer.Setup(s => s.DefaultSystemContext).Returns(serverSystemContext);
return mockServer;
}
private sealed class Harness : IDisposable
{
private readonly MonitoredItemQueueFactory m_queueFactory;
public Harness(
TestableCustomNodeManager2 manager,
Mock<IServerInternal> mockServer,
MonitoredItemQueueFactory queueFactory)
{
Manager = manager;
MockServer = mockServer;
m_queueFactory = queueFactory;
}
public TestableCustomNodeManager2 Manager { get; }
public Mock<IServerInternal> MockServer { get; }
public ServerSystemContext Context => Manager.SystemContext;
public ushort NamespaceIndex => Manager.NamespaceIndex;
public OperationContext NewContext(RequestType requestType)
{
return new OperationContext(
new RequestHeader(), null, requestType, RequestLifetime.None);
}
public BaseObjectState NewObject(string name)
{
var node = new BaseObjectState(null);
node.CreateAsPredefinedNode(Context);
node.NodeId = new NodeId(name, NamespaceIndex);
node.BrowseName = new QualifiedName(name, NamespaceIndex);
node.DisplayName = new LocalizedText(name);
return node;
}
public BaseObjectTypeState NewObjectType(string name)
{
var type = new BaseObjectTypeState
{
SuperTypeId = NodeId.Null
};
type.CreateAsPredefinedNode(Context);
type.NodeId = new NodeId(name, NamespaceIndex);
type.BrowseName = new QualifiedName(name, NamespaceIndex);
type.DisplayName = new LocalizedText(name);
return type;
}
public BaseDataVariableState NewVariable(string name, int value)
{
var variable = new BaseDataVariableState(null);
variable.CreateAsPredefinedNode(Context);
variable.NodeId = new NodeId(name, NamespaceIndex);
variable.BrowseName = new QualifiedName(name, NamespaceIndex);
variable.DisplayName = new LocalizedText(name);
variable.DataType = DataTypeIds.Int32;
variable.ValueRank = ValueRanks.Scalar;
variable.AccessLevel = AccessLevels.CurrentRead;
variable.UserAccessLevel = AccessLevels.CurrentRead;
variable.Value = value;
return variable;
}
public BaseDataVariableState NewWritableVariable(string name, int value)
{
BaseDataVariableState variable = NewVariable(name, value);
variable.AccessLevel = AccessLevels.CurrentReadOrWrite;
variable.UserAccessLevel = AccessLevels.CurrentReadOrWrite;
return variable;
}
public (BaseObjectState Parent, BaseObjectState Child) AddParentWithChild(NodeId referenceTypeId)
{
BaseObjectState parent = NewObject("Parent");
BaseObjectState child = NewObject("Child");
Manager.AddPredefinedNodePublic(Context, parent);
Manager.AddPredefinedNodePublic(Context, child);
parent.AddReference(referenceTypeId, false, child.NodeId);
child.AddReference(referenceTypeId, true, parent.NodeId);
return (parent, child);
}
public ContinuationPoint NewContinuationPoint(object handle)
{
return new ContinuationPoint
{
NodeToBrowse = handle,
View = new ViewDescription(),
BrowseDirection = BrowseDirection.Forward,
IncludeSubtypes = true,
ResultMask = BrowseResultMask.All
};
}
public void Dispose()
{
m_queueFactory.Dispose();
Manager.Dispose();
}
}
/// <summary>
/// A node manager that returns an unvalidated placeholder handle for a
/// designated NodeId so the "node is not in memory" branches of Read and
/// Write (which the default GetManagerHandle never reaches) can be
/// exercised deterministically.
/// </summary>
private sealed class PlaceholderNodeManager : TestableCustomNodeManager2
{
public PlaceholderNodeManager(
IServerInternal server,
ApplicationConfiguration configuration,
ILogger logger,
params string[] namespaceUris)
: base(server, configuration, false, logger, namespaceUris)
{
}
public NodeId PlaceholderNodeId { get; set; } = NodeId.Null;
protected override NodeHandle? GetManagerHandle(
ServerSystemContext context,
NodeId nodeId,
IDictionary<NodeId, NodeState> cache)
{
if (!PlaceholderNodeId.IsNull && nodeId == PlaceholderNodeId)
{
return new NodeHandle { NodeId = nodeId, RootId = nodeId, Validated = false };
}
return base.GetManagerHandle(context, nodeId, cache);
}
}
}
}