Files
UA-.NETStandard/tests/Opc.Ua.InformationModel.Tests/AttributeWriteValuesTests.cs
2026-07-16 06:25:54 +02:00

504 lines
23 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;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Client.TestFramework;
namespace Opc.Ua.InformationModel.Tests
{
/// <summary>
/// compliance tests for Attribute Write Values.
/// </summary>
[TestFixture]
[Category("Conformance")]
[Category("AttributeServices")]
public class AttributeWriteValuesTests : TestFixture
{
[Description("Write to the Value attribute of a Variable, where the AccessLevel == CurrentWriteService.*/")]
[Test]
public async Task WriteValueWithCurrentWriteAccessSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write the minimum value for each supported data-type. Some items may fail with BadOutOfRange. */")]
[Test]
public async Task WriteMinimumValueForSupportedDataTypesSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write the MAXIMUM value for each supported data-type. */")]
[Test]
public async Task WriteMaximumValueForSupportedDataTypesSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write to a localizedText passing in all params, no params, and some params. */")]
[Test]
public async Task WriteLocalizedTextWithVariousParametersSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("write to a node of type INTEGER where the data-type of the value is specified as SByte, Int16, Int32, and Int64; where the value is: - Data-type max - Data-type min */")]
[Test]
public async Task WriteIntegerWithSignedDataTypeVariantsSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("write to a node of type UINTEGER where the data-type of the value is specified as Byte, UInt16, UInt32, and UInt64; where the value is: - Data-type max - Data-type min */")]
[Test]
public async Task WriteUIntegerWithUnsignedDataTypeVariantsSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write() a value of NaN to all configured floating point numbers (Float, Double &amp; Duration). */")]
[Test]
public async Task WriteNaNToFloatingPointTypesSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write to a string variable; specify a value within the extended code-page")]
[Test]
public async Task WriteStringWithExtendedCodePageSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Read the value of a one-dimensional array node and write the entire value " +
"back. Expect Good.")]
[Test]
public async Task WriteEntireArrayValueSucceedsAsync()
{
await AssertWriteBackSucceedsAsync(Constants.ScalarStaticArrayInt32).ConfigureAwait(false);
}
[Description("Round-trip the value of a one-dimensional Variant array whose elements are " +
"themselves arrays of mixed built-in types. Regression for a BadTypeMismatch write failure.")]
[Test]
public async Task WriteVariantArrayWithNestedArraysSucceedsAsync()
{
await AssertWriteBackSucceedsAsync(Constants.ScalarStaticArrayVariant).ConfigureAwait(false);
}
[Description("Read the value of a multi-dimensional array node and write the entire value " +
"back. Expect Good.")]
[Test]
public async Task WriteEntireMultiDimensionalArraySucceedsAsync()
{
await AssertWriteBackSucceedsAsync(Constants.ScalarStaticArrays2DInt32).ConfigureAwait(false);
}
[Description("Read and write back the value of multiple multi-dimensional array nodes. " +
"All should return Good.")]
[Test]
public async Task WriteEntireMultiDimensionalArrayToMultipleNodesSucceedsAsync()
{
foreach (ExpandedNodeId node in Constants.ScalarStaticArrays2DNodes)
{
await AssertWriteBackSucceedsAsync(node).ConfigureAwait(false);
}
}
[Description("nodesToWrite array empty; expected service result = BadNothingToDo. */")]
[Test]
public async Task WriteEmptyArrayReturnsBadNothingToDoAsync()
{
ArrayOf<WriteValue> wv = Array.Empty<WriteValue>().ToArrayOf();
WriteResponse response;
try
{
response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
}
catch (ServiceResultException sre)
{
Assert.That(sre.StatusCode, Is.EqualTo(StatusCodes.BadNothingToDo));
return;
}
// BadNothingToDo may be reported as the service result or per-operation result.
StatusCode serviceResult = response.ResponseHeader.ServiceResult;
if (serviceResult != StatusCodes.Good)
{
Assert.That((uint)serviceResult, Is.EqualTo((uint)StatusCodes.BadNothingToDo));
Assert.That(response.Results, Is.Null.Or.Empty);
}
else
{
Assert.That(response.Results, Is.Not.Null);
Assert.That(response.Results.Count, Is.Zero);
}
}
[Description("Write to valid attributes of multiple unknown nodes, in a single call. */")]
[Test]
public async Task WriteToMultipleUnknownNodesReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = new NodeId("NonExistent_InvalidNode_1", 2),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(1))
},
new() {
NodeId = new NodeId("NonExistent_InvalidNode_2", 2),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(2))
},
new() {
NodeId = new NodeId("NonExistent_InvalidNode_3", 2),
AttributeId = Attributes.DisplayName,
Value = new DataValue(new Variant(new LocalizedText("en", "Test3")))
},
new() {
NodeId = new NodeId("NonExistent_InvalidNode_4", 2),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(4))
},
new() {
NodeId = new NodeId(Guid.NewGuid(), 2),
AttributeId = Attributes.DisplayName,
Value = new DataValue(new Variant(new LocalizedText("en", "Test5")))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(StatusCode.IsGood(response.ResponseHeader.ServiceResult), Is.True);
Assert.That(response.Results.Count, Is.EqualTo(wv.Count));
for (int i = 0; i < response.Results.Count; i++)
{
Assert.That(StatusCode.IsBad(response.Results[i]), Is.True,
$"Expected Bad status for unknown node at index {i}, got {response.Results[i]}");
}
}
[Description("Write to an invalid (non-writable) attribute of a valid node. */")]
[Test]
public async Task WriteToNonWritableAttributeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.NodeClass,
Value = new DataValue(new Variant((int)NodeClass.Variable))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(StatusCode.IsGood(response.ResponseHeader.ServiceResult), Is.True);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True,
$"Expected Bad status when writing non-writable attribute, got {response.Results[0]}");
uint code = response.Results[0].Code;
Assert.That(code,
Is.EqualTo(StatusCodes.BadNotWritable)
.Or.EqualTo(StatusCodes.BadAttributeIdInvalid)
.Or.EqualTo(StatusCodes.BadWriteNotSupported)
.Or.EqualTo(StatusCodes.BadUserAccessDenied),
$"Unexpected status code 0x{code:X8}");
}
[Description("Write to invalid (non-writable) attributes of a valid node, multiple times in the same call. */")]
[Test]
public async Task WriteToMultipleNonWritableAttributesReturnsBadStatusAsync()
{
NodeId target = ToNodeId(Constants.ScalarStaticInt32);
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = target,
AttributeId = Attributes.NodeClass,
Value = new DataValue(new Variant((int)NodeClass.Variable))
},
new() {
NodeId = target,
AttributeId = Attributes.BrowseName,
Value = new DataValue(new Variant(new QualifiedName("Renamed", 2)))
},
new() {
NodeId = target,
AttributeId = Attributes.NodeId,
Value = new DataValue(new Variant(new NodeId(99u, 2)))
},
new() {
NodeId = target,
AttributeId = Attributes.DataType,
Value = new DataValue(new Variant(DataTypeIds.String))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(StatusCode.IsGood(response.ResponseHeader.ServiceResult), Is.True);
Assert.That(response.Results.Count, Is.EqualTo(wv.Count));
for (int i = 0; i < response.Results.Count; i++)
{
Assert.That(StatusCode.IsBad(response.Results[i]), Is.True,
$"Expected Bad status at index {i}, got {response.Results[i]}");
}
}
[Description("Write to a node whose AccessLevel does not contain write capabilities. */")]
[Test]
public async Task WriteToReadOnlyNodeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = VariableIds.Server_ServerStatus_State,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant((int)ServerState.Running))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(StatusCode.IsGood(response.ResponseHeader.ServiceResult), Is.True);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True,
$"Expected Bad status when writing read-only node, got {response.Results[0]}");
uint code = response.Results[0].Code;
Assert.That(code,
Is.EqualTo(StatusCodes.BadNotWritable)
.Or.EqualTo(StatusCodes.BadUserAccessDenied)
.Or.EqualTo(StatusCodes.BadWriteNotSupported),
$"Unexpected status code 0x{code:X8}");
}
[Description("Write a NULL value for each supported data-type. Expect a Bad_TypeMismatch for each operation level result. */")]
[Test]
public async Task WriteNullValueReturnsBadTypeMismatchAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
[Description("write to a node of type UINTEGER where the data-type of the value is specified as SByte, Int16, Int32, and Int64 */")]
[Test]
public async Task WriteSignedTypeToUIntegerNodeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
[Description("write to a node of type INTEGER where the data-type of the value is specified as SByte, UInt16, UInt32, and UInt64 */")]
[Test]
public async Task WriteUnsignedTypeToIntegerNodeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
[Description("Write to a node of type INTEGER where the data-type of the value is specified as float. */")]
[Test]
public async Task WriteFloatToIntegerNodeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
[Description("Write to a LocalizedText with a valid value, but specify a localeId that is known to not be supported, e g. aardvark. */")]
[Test]
public async Task WriteLocalizedTextWithUnsupportedLocaleReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
/// <summary>
/// Reads the Value attribute of a node and writes the exact same value
/// back, asserting both the read and the write return Good.
/// </summary>
private async Task AssertWriteBackSucceedsAsync(ExpandedNodeId node)
{
NodeId nodeId = ToNodeId(node);
ReadResponse read = await Session.ReadAsync(
null, 0, TimestampsToReturn.Both,
new ReadValueId[]
{
new() { NodeId = nodeId, AttributeId = Attributes.Value }
}.ToArrayOf(),
CancellationToken.None).ConfigureAwait(false);
Assert.That(read.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(read.Results[0].StatusCode), Is.True,
$"Read of '{node}' should return Good.");
ArrayOf<WriteValue> wv = new WriteValue[]
{
new()
{
NodeId = nodeId,
AttributeId = Attributes.Value,
Value = new DataValue(read.Results[0].WrappedValue)
}
}.ToArrayOf();
WriteResponse write = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(write.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(write.Results[0]), Is.True,
$"Writing the read value back to '{node}' should return Good.");
}
}
}