/* ======================================================================== * Copyright (c) 2005-2026 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/ * ======================================================================*/ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using NUnit.Framework; using Opc.Ua.Server.TestFramework; using Opc.Ua.Tests; namespace Opc.Ua.Server.Tests.CoverageNodeSet { /// /// The shared, data-driven assertion battery that is executed identically /// against both coverage pipelines (see /// and /// ). Every attribute or /// reference dropped by either pipeline fails a test here. /// /// /// The concrete host under test. /// public abstract class CoverageNodeSetAssertionsBase where TServer : global::Quickstarts.ReferenceServer.ReferenceServer { private ServerFixture m_serverFixture; private TServer m_server; private string m_pkiRoot; private ushort m_ns; /// /// Creates the concrete server host from a telemetry context. /// protected abstract TServer CreateServer(ITelemetryContext telemetry); /// /// Whether this pipeline stamps the model version and publication date /// onto the published NamespaceMetadata Object. The /// source-generated pipelines carry a ModelDependency assembly /// attribute that the NamespaceMetadataPublisher reads back to /// populate the metadata; the runtime importer materialises the address /// space from raw bytes and surfaces no such attribute, so it publishes /// the metadata Object without the model version. /// protected virtual bool PublishesModelVersion => true; /// /// Starts the concrete coverage server host. /// [OneTimeSetUp] public async Task OneTimeSetUpAsync() { m_pkiRoot = Path.Combine( TestContext.CurrentContext.WorkDirectory, Guid.NewGuid().ToString("N").Substring(0, 8)); m_serverFixture = new ServerFixture(CreateServer) { UriScheme = Utils.UriSchemeOpcTcp, SecurityNone = true, AutoAccept = true }; m_server = await m_serverFixture.StartAsync(m_pkiRoot).ConfigureAwait(false); int namespaceIndex = m_server.CurrentInstance.NamespaceUris .GetIndex(CoverageTestCatalogue.NamespaceUri); Assert.That(namespaceIndex, Is.GreaterThanOrEqualTo(0), "The coverage model namespace should be registered."); m_ns = (ushort)namespaceIndex; } /// /// Stops the server and cleans up PKI artefacts. /// [OneTimeTearDown] public async Task OneTimeTearDownAsync() { m_server?.Dispose(); if (m_serverFixture != null) { await m_serverFixture.StopAsync().ConfigureAwait(false); } if (!string.IsNullOrEmpty(m_pkiRoot) && Directory.Exists(m_pkiRoot)) { Directory.Delete(m_pkiRoot, recursive: true); } } /// /// The model namespace must be registered after startup, and its /// version/publication metadata must be discoverable through the /// NamespaceMetadata Object published under /// Server/Namespaces. /// [Test] [Order(100)] public async Task ModelNamespaceRegisteredAsync() { Assert.That(m_ns, Is.GreaterThan(0), "The coverage model namespace should be registered."); IConfigurationNodeManager configuration = m_server.CurrentInstance.ConfigurationNodeManager; NamespaceMetadataState primary = (await configuration .GetNamespaceMetadataStateAsync(CoverageTestCatalogue.NamespaceUri) .ConfigureAwait(false))!; NamespaceMetadataState secondary = (await configuration .GetNamespaceMetadataStateAsync(CoverageTestCatalogue.SecondaryNamespaceUri) .ConfigureAwait(false))!; Assert.Multiple(() => { Assert.That(primary, Is.Not.Null, "A NamespaceMetadata Object should describe the primary model namespace."); Assert.That(secondary, Is.Not.Null, "A NamespaceMetadata Object should describe the secondary model namespace."); Assert.That(primary!.NamespaceUri?.Value, Is.EqualTo(CoverageTestCatalogue.NamespaceUri)); Assert.That(secondary!.NamespaceUri?.Value, Is.EqualTo(CoverageTestCatalogue.SecondaryNamespaceUri)); }); if (PublishesModelVersion) { Assert.Multiple(() => { Assert.That(primary.NamespaceVersion?.Value, Is.EqualTo("1.2.3"), "The published NamespaceVersion should match the model's Version attribute."); Assert.That( primary.NamespacePublicationDate?.Value.ToDateTime(), Is.EqualTo(new DateTime(2024, 6, 1, 0, 0, 0, DateTimeKind.Utc)), "The published NamespacePublicationDate should match the model's PublicationDate."); }); } } /// /// Every authored node resolves in the address space with the correct /// BrowseName and NodeClass. /// [Test] [Order(200)] [TestCaseSource(typeof(CoverageTestCatalogue), nameof(CoverageTestCatalogue.Nodes))] public async Task NodePresentWithBrowseNameAndClassAsync(CoverageTestCatalogue.ExpectedNode expected) { NodeState node = (await FindNodeAsync(expected.Id).ConfigureAwait(false))!; Assert.That(node, Is.Not.Null, $"Node ns={m_ns};i={expected.Id} ({expected.BrowseName}) should be present."); Assert.Multiple(() => { Assert.That(node.BrowseName.Name, Is.EqualTo(expected.BrowseName)); Assert.That(node.NodeClass, Is.EqualTo(expected.NodeClass)); }); } /// /// Every authored node materialises as the expected /// family for its NodeClass. /// [Test] [Order(210)] [TestCaseSource(typeof(CoverageTestCatalogue), nameof(CoverageTestCatalogue.Nodes))] public async Task NodeMaterialisesAsExpectedStateFamilyAsync(CoverageTestCatalogue.ExpectedNode expected) { NodeState node = (await FindNodeAsync(expected.Id).ConfigureAwait(false))!; Assert.That(node, Is.Not.Null); Assert.That(node, Is.AssignableTo(ExpectedStateFamily(expected.NodeClass)), $"Node {expected.BrowseName} should materialise as {ExpectedStateFamily(expected.NodeClass).Name}."); } /// /// The authored node count in the model namespace matches the catalogue /// exactly (no extra, no missing). /// [Test] [Order(220)] public async Task OwnedNodeCountMatchesCatalogueAsync() { int found = 0; foreach (CoverageTestCatalogue.ExpectedNode expected in CoverageTestCatalogue.Nodes) { if (await FindNodeAsync(expected.Id).ConfigureAwait(false) != null) { found++; } } Assert.That(found, Is.EqualTo(CoverageTestCatalogue.Nodes.Count), "All catalogue nodes must be present."); } /// /// Every reference relation in the catalogue exists on the source node /// with the correct type, direction and target. /// [Test] [Order(300)] [TestCaseSource(typeof(CoverageTestCatalogue), nameof(CoverageTestCatalogue.References))] public async Task ReferenceExistsAsync(CoverageTestCatalogue.ExpectedReference expected) { NodeState source = (await FindNodeAsync(expected.Source).ConfigureAwait(false))!; Assert.That(source, Is.Not.Null, $"Reference source ns={m_ns};i={expected.Source} should exist."); NodeId referenceTypeId = expected.ReferenceType is >= 5001 and <= 5004 ? new NodeId(expected.ReferenceType, m_ns) : new NodeId(expected.ReferenceType, 0); NodeId targetId = expected.TargetIsOwned ? new NodeId(expected.Target, m_ns) : new NodeId(expected.Target, 0); bool exists = ReferenceExists( source, referenceTypeId, isInverse: !expected.IsForward, targetId: targetId); Assert.That(exists, Is.True, $"{expected.Source} should have {(expected.IsForward ? "forward" : "inverse")} " + $"reference {referenceTypeId} to {targetId}."); } /// /// The root instance carries its non-default common-node attributes. /// [Test] [Order(400)] public async Task RootAttributesRoundTripAsync() { NodeState root = (await FindNodeAsync(5400).ConfigureAwait(false))!; Assert.That(root, Is.Not.Null); Assert.Multiple(() => { Assert.That((uint)root.WriteMask, Is.EqualTo(2097151u)); Assert.That((uint)root.UserWriteMask, Is.EqualTo(2097151u)); }); } /// /// The boolean value variable carries its non-default variable /// attributes and multi-locale DisplayName/Description. /// [Test] [Order(410)] public async Task BooleanValueAttributesRoundTripAsync() { var v = (BaseVariableState)(await FindNodeAsync(5420).ConfigureAwait(false))!; Assert.That(v, Is.Not.Null); Assert.Multiple(() => { Assert.That(v.AccessLevel, Is.EqualTo((byte)3)); Assert.That(v.UserAccessLevel, Is.EqualTo((byte)3)); Assert.That(v.MinimumSamplingInterval, Is.EqualTo(100.0)); Assert.That(v.Historizing, Is.True); Assert.That(v.DisplayName.Text, Is.EqualTo("BooleanValue")); }); } /// /// Reference-type attributes (Symmetric, InverseName, IsAbstract) /// round-trip on the custom reference types. /// [Test] [Order(420)] public async Task ReferenceTypeAttributesRoundTripAsync() { var hierarchical = (ReferenceTypeState)(await FindNodeAsync(5001).ConfigureAwait(false))!; var symmetric = (ReferenceTypeState)(await FindNodeAsync(5002).ConfigureAwait(false))!; var abstractRef = (ReferenceTypeState)(await FindNodeAsync(5003).ConfigureAwait(false))!; Assert.Multiple(() => { Assert.That(hierarchical!.Symmetric, Is.False); Assert.That(hierarchical.InverseName.Text, Is.EqualTo("IsCoverageChildOf")); Assert.That(symmetric!.Symmetric, Is.True); Assert.That(abstractRef!.IsAbstract, Is.True); }); } /// /// The view carries ContainsNoLoops and a non-zero EventNotifier. /// [Test] [Order(430)] public async Task ViewAttributesRoundTripAsync() { var view = (ViewState)(await FindNodeAsync(5460).ConfigureAwait(false))!; Assert.That(view, Is.Not.Null); Assert.That(view.ContainsNoLoops, Is.True); } /// /// The event-source object carries a non-zero EventNotifier bitmask. /// [Test] [Order(440)] public async Task EventSourceEventNotifierRoundTripAsync() { var obj = (BaseObjectState)(await FindNodeAsync(5410).ConfigureAwait(false))!; Assert.That(obj, Is.Not.Null); Assert.That(obj.EventNotifier, Is.EqualTo((byte)1)); } /// /// The locked method is not executable. /// [Test] [Order(450)] public async Task LockedMethodNotExecutableAsync() { var method = (MethodState)(await FindNodeAsync(5451).ConfigureAwait(false))!; Assert.That(method, Is.Not.Null); Assert.Multiple(() => { Assert.That(method.Executable, Is.False); Assert.That(method.UserExecutable, Is.False); }); } /// /// The data-type definitions round-trip to the expected shapes. /// [Test] [Order(460)] public async Task DataTypeDefinitionsRoundTripAsync() { var enumeration = (DataTypeState)(await FindNodeAsync(5100).ConfigureAwait(false))!; var structure = (DataTypeState)(await FindNodeAsync(5130).ConfigureAwait(false))!; var union = (DataTypeState)(await FindNodeAsync(5150).ConfigureAwait(false))!; var optionSet = (DataTypeState)(await FindNodeAsync(5110).ConfigureAwait(false))!; var abstractType = (DataTypeState)(await FindNodeAsync(5120).ConfigureAwait(false))!; Assert.Multiple(() => { Assert.That(enumeration!.DataTypeDefinition.TryGetValue(out _), Is.True); Assert.That(structure!.DataTypeDefinition.TryGetValue(out _), Is.True); Assert.That(optionSet!.DataTypeDefinition.TryGetValue(out _), Is.True); Assert.That(abstractType!.IsAbstract, Is.True); Assert.That(union!.DataTypeDefinition.TryGetValue(out StructureDefinition? unionDef), Is.True); Assert.That(unionDef!.StructureType, Is.EqualTo(StructureType.Union)); }); } /// /// The scalar, array and matrix variable value shapes round-trip. /// [Test] [Order(470)] public async Task ValueShapesRoundTripAsync() { var scalar = (BaseVariableState)(await FindNodeAsync(5421).ConfigureAwait(false))!; var array = (BaseVariableState)(await FindNodeAsync(5422).ConfigureAwait(false))!; var matrix = (BaseVariableState)(await FindNodeAsync(5423).ConfigureAwait(false))!; Assert.Multiple(() => { Assert.That(scalar!.ValueRank, Is.EqualTo(ValueRanks.Scalar)); Assert.That(scalar.WrappedValue.TryGetValue(out int scalarValue), Is.True); Assert.That(scalarValue, Is.EqualTo(-12345)); Assert.That(array!.ValueRank, Is.EqualTo(ValueRanks.OneDimension)); Assert.That(array.WrappedValue.TryGetValue(out ArrayOf arrayValue), Is.True); Assert.That(arrayValue.Count, Is.EqualTo(2)); Assert.That(matrix!.ValueRank, Is.EqualTo(ValueRanks.TwoDimensions)); Assert.That(matrix.WrappedValue.IsNull, Is.False); Assert.That(matrix.WrappedValue.TypeInfo.ValueRank, Is.EqualTo(2)); }); } /// /// The deep, branching instance tree is navigable child-by-child in /// both pipelines (TreeRoot → BranchA → SubBranchA → LeafA2). /// [Test] [Order(480)] public async Task DeepTreeNavigableAsync() { NodeState treeRoot = (await FindNodeAsync(5490).ConfigureAwait(false))!; Assert.That(treeRoot, Is.Not.Null); var context = m_server.CurrentInstance.DefaultSystemContext; BaseInstanceState branchA = treeRoot.FindChild(context, new QualifiedName("BranchA", m_ns))!; BaseInstanceState subBranchA = (branchA?.FindChild(context, new QualifiedName("SubBranchA", m_ns)))!; BaseInstanceState leafA2 = (subBranchA?.FindChild(context, new QualifiedName("LeafA2", m_ns)))!; Assert.Multiple(() => { Assert.That(branchA?.NodeId, Is.EqualTo(new NodeId(5491u, m_ns))); Assert.That(subBranchA?.NodeId, Is.EqualTo(new NodeId(5493u, m_ns))); Assert.That(leafA2, Is.Not.Null); Assert.That(leafA2!.NodeId, Is.EqualTo(new NodeId(5494u, m_ns))); }); } /// /// The callable method exposes its argument metadata. /// [Test] [Order(500)] public async Task MethodArgumentMetadataRoundTripsAsync() { var inputArgs = (BaseVariableState)(await FindNodeAsync(5452).ConfigureAwait(false))!; var outputArgs = (BaseVariableState)(await FindNodeAsync(5453).ConfigureAwait(false))!; Argument[] inputs = ReadArguments(inputArgs!); Argument[] outputs = ReadArguments(outputArgs!); Assert.Multiple(() => { Assert.That(inputs, Is.Not.Null.And.Length.EqualTo(2)); Assert.That(inputs[0].Name, Is.EqualTo("A")); Assert.That(inputs[1].Name, Is.EqualTo("B")); Assert.That(outputs, Is.Not.Null.And.Length.EqualTo(1)); Assert.That(outputs[0].Name, Is.EqualTo("Sum")); }); } /// /// The authored method carries no server-side implementation, so /// invoking it exercises the call contract /// and yields a deterministic BadNotImplemented status on every /// call, identically across all pipelines. /// [Test] [Order(505)] public async Task MethodCallYieldsDeterministicStatusAsync() { var method = (MethodState)(await FindNodeAsync(5450).ConfigureAwait(false))!; Assert.That(method, Is.Not.Null); ISystemContext context = m_server.CurrentInstance.DefaultSystemContext; var objectId = new NodeId(5400u, m_ns); // Supply exactly as many Int32 inputs as the method validates against // so the call clears argument validation and reaches the (unbound) // handler regardless of how each pipeline wired the arguments. int expectedInputs = method.InputArguments?.Value.Count ?? 0; var inputs = new Variant[expectedInputs]; for (int ii = 0; ii < expectedInputs; ii++) { inputs[ii] = new Variant(0); } ArrayOf inputArguments = [.. inputs]; StatusCode firstStatus = await CallMethodAsync(method, context, objectId, inputArguments) .ConfigureAwait(false); StatusCode secondStatus = await CallMethodAsync(method, context, objectId, inputArguments) .ConfigureAwait(false); Assert.Multiple(() => { Assert.That(firstStatus.Code, Is.EqualTo(StatusCodes.BadNotImplemented), "An unbound imported method should report BadNotImplemented."); Assert.That(secondStatus.Code, Is.EqualTo(firstStatus.Code), "Repeated calls should yield the same status code."); }); } /// /// The method instance points at its type-level declaration. /// [Test] [Order(510)] public async Task MethodDeclarationIdWiredAsync() { var method = (MethodState)(await FindNodeAsync(5404).ConfigureAwait(false))!; Assert.That(method, Is.Not.Null); Assert.That(method.MethodDeclarationId, Is.EqualTo(new NodeId(5216u, m_ns))); } /// /// The complex-type loader registered stand-ins for the runtime-loaded /// enumeration and option set (mirrors the default server complex-type /// path). Structure definitions are additionally validated by /// . /// [Test] [Order(600)] public void ComplexTypesRegistered() { IServerInternal server = m_server.CurrentInstance; ExpandedNodeId enumTypeId = NodeId.ToExpandedNodeId( new NodeId(5100u, m_ns), server.NamespaceUris); ExpandedNodeId optionSetTypeId = NodeId.ToExpandedNodeId( new NodeId(5110u, m_ns), server.NamespaceUris); Assert.Multiple(() => { Assert.That( server.Factory.TryGetEnumeratedType(enumTypeId, out IEnumeratedType? enumType), Is.True, "CoverageEnumeration stand-in should be registered in the server factory."); Assert.That(enumType, Is.Not.Null); Assert.That( server.Factory.TryGetEncodeableType(optionSetTypeId, out IEncodeableType? optionSetType), Is.True, "CoverageOptionSet stand-in should be registered in the server factory."); Assert.That(optionSetType, Is.Not.Null); }); } /// /// The secondary (dependent) model namespace is loaded, its nodes are /// present, and the cross-namespace references between the two models /// resolve in both directions. /// [Test] [Order(700)] public async Task SecondaryNamespaceRoundTripsAsync() { int secondaryNamespaceIndex = m_server.CurrentInstance.NamespaceUris .GetIndex(CoverageTestCatalogue.SecondaryNamespaceUri); Assert.That(secondaryNamespaceIndex, Is.GreaterThanOrEqualTo(0), "The secondary coverage model namespace should be registered."); ushort ns2 = (ushort)secondaryNamespaceIndex; foreach (CoverageTestCatalogue.ExpectedNode expected in CoverageTestCatalogue.SecondaryNodes) { NodeState node = (await m_server.CurrentInstance.NodeManager .FindNodeInAddressSpaceAsync(new NodeId(expected.Id, ns2)) .ConfigureAwait(false))!; Assert.That(node, Is.Not.Null, $"secondary node ns={ns2};i={expected.Id} ({expected.BrowseName}) should be present."); Assert.That(node.NodeClass, Is.EqualTo(expected.NodeClass)); } NodeState secondaryInstance = (await m_server.CurrentInstance.NodeManager .FindNodeInAddressSpaceAsync(new NodeId(6010u, ns2)).ConfigureAwait(false))!; NodeState secondaryType = (await m_server.CurrentInstance.NodeManager .FindNodeInAddressSpaceAsync(new NodeId(6001u, ns2)).ConfigureAwait(false))!; NodeState primaryRoot = (await FindNodeAsync(5400).ConfigureAwait(false))!; Assert.Multiple(() => { // SecondaryInstance is parented under the primary CoverageRoot. Assert.That( ReferenceExists(secondaryInstance!, new NodeId(ReferenceTypes.HasComponent, 0), isInverse: true, new NodeId(5400u, m_ns)), Is.True, "SecondaryInstance should have an inverse HasComponent to the primary CoverageRoot."); // SecondaryObjectType is a subtype of the primary CoverageObjectType. Assert.That( ReferenceExists(secondaryType!, new NodeId(ReferenceTypes.HasSubtype, 0), isInverse: true, new NodeId(5210u, m_ns)), Is.True, "SecondaryObjectType should be a HasSubtype of the primary CoverageObjectType."); // The forward edge is visible from the primary root (resolved // across NodeManagers where the two models are hosted separately). Assert.That( ReferenceExists(primaryRoot!, new NodeId(ReferenceTypes.HasComponent, 0), isInverse: false, new NodeId(6010u, ns2)), Is.True, "The primary CoverageRoot should have a forward HasComponent to SecondaryInstance."); }); } private ValueTask FindNodeAsync(uint id) { return m_server.CurrentInstance.NodeManager .FindNodeInAddressSpaceAsync(new NodeId(id, m_ns)); } private static async ValueTask CallMethodAsync( MethodState method, ISystemContext context, NodeId objectId, ArrayOf inputArguments) { var argumentErrors = new List(); var outputArguments = new List(); ServiceResult result = await method.CallAsync( context, objectId, inputArguments, argumentErrors, outputArguments).ConfigureAwait(false); return result.StatusCode; } private bool ReferenceExists(NodeState node, NodeId referenceTypeId, bool isInverse, NodeId targetId) { var context = m_server.CurrentInstance.DefaultSystemContext; using INodeBrowser browser = node.CreateBrowser( context, null, NodeId.Null, false, BrowseDirection.Both, QualifiedName.Null, null, true); for (IReference reference = browser.Next()!; reference != null; reference = browser.Next()!) { if (reference.ReferenceTypeId != referenceTypeId || reference.IsInverse != isInverse) { continue; } NodeId target = ExpandedNodeId.ToNodeId(reference.TargetId, context.NamespaceUris); if (target == targetId) { return true; } } return false; } private static Type ExpectedStateFamily(NodeClass nodeClass) { return nodeClass switch { NodeClass.Object => typeof(BaseObjectState), NodeClass.Variable => typeof(BaseVariableState), NodeClass.Method => typeof(MethodState), NodeClass.ObjectType => typeof(BaseObjectTypeState), NodeClass.VariableType => typeof(BaseVariableTypeState), NodeClass.ReferenceType => typeof(ReferenceTypeState), NodeClass.DataType => typeof(DataTypeState), NodeClass.View => typeof(ViewState), _ => typeof(NodeState) }; } private Argument[] ReadArguments(BaseVariableState arguments) { if (arguments == null) { return null!; } IServiceMessageContext context = m_server.CurrentInstance.MessageContext; if (arguments.WrappedValue.TryGetValue(out ArrayOf decoded, context)) { return decoded.ToArray()!; } return null!; } } }