Files
UA-.NETStandard/tests/Opc.Ua.Server.Tests/ServerUtilsTests.cs

388 lines
15 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/
* ======================================================================*/
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Opc.Ua.Tests;
namespace Opc.Ua.Server.Tests
{
[TestFixture]
[Category("ServerUtils")]
[SetCulture("en-us")]
[SetUICulture("en-us")]
[Parallelizable]
public class ServerUtilsTests
{
private ILogger m_logger;
[SetUp]
public void SetUp()
{
m_logger = NUnitTelemetryContext.Create().CreateLogger("ServerUtilsTests");
}
private static OperationContext CreateContext(DiagnosticsMasks mask)
{
var header = new RequestHeader
{
ReturnDiagnostics = (uint)mask
};
return new OperationContext(header, null, RequestType.Read, RequestLifetime.None);
}
[Test]
public void CreateErrorByIndex_WithDiagnostics_SetsDiagnosticInfo()
{
OperationContext context = CreateContext(DiagnosticsMasks.OperationAll);
var diagnosticInfos = new List<DiagnosticInfo> { null, null };
uint code = ServerUtils.CreateError(
StatusCodes.BadNodeIdInvalid.Code, context, diagnosticInfos, 0, m_logger);
Assert.That(code, Is.EqualTo(StatusCodes.BadNodeIdInvalid.Code));
Assert.That(diagnosticInfos[0], Is.Not.Null);
Assert.That(diagnosticInfos[1], Is.Null);
}
[Test]
public void CreateErrorByIndex_WithoutDiagnostics_LeavesNull()
{
OperationContext context = CreateContext(DiagnosticsMasks.None);
var diagnosticInfos = new List<DiagnosticInfo> { null };
uint code = ServerUtils.CreateError(
StatusCodes.BadNodeIdInvalid.Code, context, diagnosticInfos, 0, m_logger);
Assert.That(code, Is.EqualTo(StatusCodes.BadNodeIdInvalid.Code));
Assert.That(diagnosticInfos[0], Is.Null);
}
[Test]
public void CreateErrorAppend_WithDiagnostics_AddsResultAndDiagnostic()
{
OperationContext context = CreateContext(DiagnosticsMasks.OperationAll);
var results = new List<StatusCode>();
var diagnosticInfos = new List<DiagnosticInfo>();
bool hasDiag = ServerUtils.CreateError(
StatusCodes.BadTypeMismatch.Code, results, diagnosticInfos, context, m_logger);
Assert.That(hasDiag, Is.True);
Assert.That(results, Has.Count.EqualTo(1));
Assert.That(results[0], Is.EqualTo(StatusCodes.BadTypeMismatch));
Assert.That(diagnosticInfos, Has.Count.EqualTo(1));
Assert.That(diagnosticInfos[0], Is.Not.Null);
}
[Test]
public void CreateErrorAppend_WithoutDiagnostics_ReturnsFalse()
{
OperationContext context = CreateContext(DiagnosticsMasks.None);
var results = new List<StatusCode>();
var diagnosticInfos = new List<DiagnosticInfo>();
bool hasDiag = ServerUtils.CreateError(
StatusCodes.BadTypeMismatch.Code, results, diagnosticInfos, context, m_logger);
Assert.That(hasDiag, Is.False);
Assert.That(results, Has.Count.EqualTo(1));
Assert.That(diagnosticInfos, Is.Empty);
}
[Test]
public void CreateErrorAtIndex_WithDiagnostics_SetsAtPosition()
{
OperationContext context = CreateContext(DiagnosticsMasks.OperationAll);
var results = new List<StatusCode> { StatusCodes.Good, StatusCodes.Good };
var diagnosticInfos = new List<DiagnosticInfo> { null, null };
bool hasDiag = ServerUtils.CreateError(
StatusCodes.BadAttributeIdInvalid.Code, results, diagnosticInfos, 1, context, m_logger);
Assert.That(hasDiag, Is.True);
Assert.That(results[0], Is.EqualTo(StatusCodes.Good));
Assert.That(results[1], Is.EqualTo(StatusCodes.BadAttributeIdInvalid));
Assert.That(diagnosticInfos[0], Is.Null);
Assert.That(diagnosticInfos[1], Is.Not.Null);
}
[Test]
public void CreateErrorAtIndex_WithoutDiagnostics_ReturnsFalse()
{
OperationContext context = CreateContext(DiagnosticsMasks.None);
var results = new List<StatusCode> { StatusCodes.Good };
var diagnosticInfos = new List<DiagnosticInfo> { null };
bool hasDiag = ServerUtils.CreateError(
StatusCodes.BadAttributeIdInvalid.Code, results, diagnosticInfos, 0, context, m_logger);
Assert.That(hasDiag, Is.False);
Assert.That(results[0], Is.EqualTo(StatusCodes.BadAttributeIdInvalid));
Assert.That(diagnosticInfos[0], Is.Null);
}
[Test]
public void CreateSuccess_AddsGoodStatusCode()
{
OperationContext context = CreateContext(DiagnosticsMasks.None);
var results = new List<StatusCode>();
var diagnosticInfos = new List<DiagnosticInfo>();
ServerUtils.CreateSuccess(results, diagnosticInfos, context);
Assert.That(results, Has.Count.EqualTo(1));
Assert.That(results[0], Is.EqualTo(StatusCodes.Good));
Assert.That(diagnosticInfos, Is.Empty);
}
[Test]
public void CreateSuccess_WithDiagnostics_AddsNullDiagnostic()
{
OperationContext context = CreateContext(DiagnosticsMasks.OperationAll);
var results = new List<StatusCode>();
var diagnosticInfos = new List<DiagnosticInfo>();
ServerUtils.CreateSuccess(results, diagnosticInfos, context);
Assert.That(results, Has.Count.EqualTo(1));
Assert.That(results[0], Is.EqualTo(StatusCodes.Good));
Assert.That(diagnosticInfos, Has.Count.EqualTo(1));
Assert.That(diagnosticInfos[0], Is.Null);
}
[Test]
public void CreateDiagnosticInfoCollection_WithDiagnostics_ReturnsCollection()
{
OperationContext context = CreateContext(DiagnosticsMasks.OperationAll);
var errors = new List<ServiceResult>
{
ServiceResult.Good,
new(StatusCodes.BadNodeIdInvalid),
ServiceResult.Good
};
List<DiagnosticInfo> result =
ServerUtils.CreateDiagnosticInfoCollection(context, errors, m_logger);
Assert.That(result, Is.Not.Null);
Assert.That(result, Has.Count.EqualTo(3));
Assert.That(result[0], Is.Null);
Assert.That(result[1], Is.Not.Null);
Assert.That(result[2], Is.Null);
}
[Test]
public void CreateDiagnosticInfoCollection_WithoutDiagnostics_ReturnsNull()
{
OperationContext context = CreateContext(DiagnosticsMasks.None);
var errors = new List<ServiceResult>
{
new(StatusCodes.BadNodeIdInvalid)
};
List<DiagnosticInfo> result =
ServerUtils.CreateDiagnosticInfoCollection(context, errors, m_logger);
Assert.That(result, Is.Null);
}
[Test]
public void CreateStatusCodeCollection_AllGood_ReturnsGoodCodes()
{
OperationContext context = CreateContext(DiagnosticsMasks.None);
var errors = new List<ServiceResult>
{
ServiceResult.Good,
ServiceResult.Good
};
List<StatusCode> result =
ServerUtils.CreateStatusCodeCollection(context, errors, out _, m_logger);
Assert.That(result, Has.Count.EqualTo(2));
Assert.That(result[0], Is.EqualTo(StatusCodes.Good));
Assert.That(result[1], Is.EqualTo(StatusCodes.Good));
}
[Test]
public void CreateStatusCodeCollection_WithErrors_ReturnsErrorCodes()
{
OperationContext context = CreateContext(DiagnosticsMasks.None);
var errors = new List<ServiceResult>
{
ServiceResult.Good,
new(StatusCodes.BadNodeIdInvalid)
};
List<StatusCode> result =
ServerUtils.CreateStatusCodeCollection(context, errors, out _, m_logger);
Assert.That(result, Has.Count.EqualTo(2));
Assert.That(result[0], Is.EqualTo(StatusCodes.Good));
Assert.That(result[1], Is.EqualTo(StatusCodes.BadNodeIdInvalid));
}
[Test]
public void CreateDiagnosticInfo_WithNullError_ReturnsNull()
{
var serverMock = new Mock<IServerInternal>();
OperationContext context = CreateContext(DiagnosticsMasks.OperationAll);
DiagnosticInfo result = ServerUtils.CreateDiagnosticInfo(
serverMock.Object, context, null, m_logger);
Assert.That(result, Is.Null);
}
[Test]
public void CreateDiagnosticInfo_WithError_ReturnsDiagnosticInfo()
{
var serverMock = new Mock<IServerInternal>();
using var resourceMgr = new ResourceManager(new ApplicationConfiguration());
serverMock.Setup(s => s.ResourceManager).Returns(resourceMgr);
OperationContext context = CreateContext(DiagnosticsMasks.ServiceLocalizedText);
var error = new ServiceResult(StatusCodes.BadNodeIdInvalid);
DiagnosticInfo result = ServerUtils.CreateDiagnosticInfo(
serverMock.Object, context, error, m_logger);
Assert.That(result, Is.Not.Null);
}
[Test]
public void CreateDiagnosticInfo_WithoutServiceLocalizedText_SkipsTranslation()
{
var serverMock = new Mock<IServerInternal>();
OperationContext context = CreateContext(DiagnosticsMasks.OperationSymbolicId);
var error = new ServiceResult(StatusCodes.BadNodeIdInvalid);
DiagnosticInfo result = ServerUtils.CreateDiagnosticInfo(
serverMock.Object, context, error, m_logger);
Assert.That(result, Is.Not.Null);
serverMock.Verify(s => s.ResourceManager, Times.Never);
}
[Test]
[NonParallelizable]
public void ReportEventsWhenEnabledQueuesAllEventKindsAndDisableClearsQueue()
{
ServerUtils.EventsEnabled = false;
Assert.That(GetQueuedEventCount(), Is.Zero);
var nodeId = new NodeId("event-node", 2);
var value = new DataValue(new Variant(42), StatusCodes.Good);
var filter = new DataChangeFilter();
ServerUtils.EventsEnabled = true;
ServerUtils.ReportWriteValue(nodeId, value, StatusCodes.BadOutOfRange);
ServerUtils.ReportQueuedValue(nodeId, 1, value);
ServerUtils.ReportFilteredValue(nodeId, 2, value);
ServerUtils.ReportDiscardedValue(nodeId, 3, value);
ServerUtils.ReportPublishValue(nodeId, 4, value);
ServerUtils.ReportCreateMonitoredItem(
nodeId,
5,
samplingInterval: 100,
queueSize: 10,
discardOldest: true,
filter,
MonitoringMode.Reporting);
ServerUtils.ReportModifyMonitoredItem(
nodeId,
6,
samplingInterval: 200,
queueSize: 20,
discardOldest: false,
filter,
MonitoringMode.Sampling);
Assert.That(GetQueuedEventCount(), Is.EqualTo(7));
ServerUtils.EventsEnabled = false;
Assert.That(GetQueuedEventCount(), Is.Zero);
}
[Test]
[NonParallelizable]
public void ReportEventsWhenDisabledDoNotQueue()
{
ServerUtils.EventsEnabled = false;
var nodeId = new NodeId("disabled-event-node", 2);
var value = new DataValue(new Variant(42), StatusCodes.Good);
ServerUtils.ReportWriteValue(nodeId, value, StatusCodes.Good);
ServerUtils.ReportQueuedValue(nodeId, 1, value);
ServerUtils.ReportFilteredValue(nodeId, 2, value);
ServerUtils.ReportDiscardedValue(nodeId, 3, value);
ServerUtils.ReportPublishValue(nodeId, 4, value);
ServerUtils.ReportCreateMonitoredItem(
nodeId,
5,
samplingInterval: 100,
queueSize: 10,
discardOldest: true,
new DataChangeFilter(),
MonitoringMode.Reporting);
ServerUtils.ReportModifyMonitoredItem(
nodeId,
6,
samplingInterval: 200,
queueSize: 20,
discardOldest: false,
new DataChangeFilter(),
MonitoringMode.Sampling);
Assert.That(GetQueuedEventCount(), Is.Zero);
}
[TearDown]
public void TearDown()
{
ServerUtils.EventsEnabled = false;
}
private static int GetQueuedEventCount()
{
FieldInfo field = typeof(ServerUtils).GetField(
"s_events",
BindingFlags.NonPublic | BindingFlags.Static)!;
object queue = field.GetValue(null)!;
PropertyInfo countProperty = queue.GetType().GetProperty("Count")!;
return (int)countProperty.GetValue(queue)!;
}
}
}