mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-22 17:50:03 +08:00
8d2d363d04
## Summary Adds hand-written partial state classes that enforce OPC UA Part 8 §5.3.3 write validation for the `DiscreteItemType` subtypes, plus tests. ## Details - **`MultiStateDiscreteState`** — a written value is treated as an index into `EnumStrings`. Returns `BadOutOfRange` when the index is outside `[0, len)`. - **`MultiStateValueDiscreteState`** — a written value is matched against the sparse `EnumValues` set. Returns `BadOutOfRange` when no entry matches. On a good write, `ValueAsText` is refreshed from the matching entry's display name. - **`DiscreteItemState`** — provides the shared `TryGetIntegerValue` helper that normalizes any scalar integer built-in type to `long` (the exact-type `Variant` accessors return `default` on a built-in-type mismatch, so a `BuiltInType` switch is required). - **`TwoStateDiscreteType`** intentionally has no hand-written class. These 3 classes are `public partial class` extending the source-generated DataAccess partials already present on `master`. They read via the typed properties (`EnumStrings`, `EnumValues`, `ValueAsText`) rather than `FindChild`, because `BaseDataVariableState.FindChild` special-cases the `EnumStrings` BrowseName and never falls through to the `AddChild`'d children. ## Testing - `dotnet build src\Opc.Ua.Core.Types\Opc.Ua.Core.Types.csproj -c Release -f net10.0` → 0 warnings, 0 errors. - 13 tests across `MultiStateDiscreteStateTests` and `MultiStateValueDiscreteStateTests` pass (13 passed, 0 failed). --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
203 lines
7.9 KiB
C#
203 lines
7.9 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 NUnit.Framework;
|
|
using Opc.Ua.Tests;
|
|
|
|
namespace Opc.Ua.Core.Tests.Stack.State
|
|
{
|
|
/// <summary>
|
|
/// Tests for the handwritten MultiStateValueDiscreteState write validation which
|
|
/// rejects numeric writes that do not match an EnumValues entry and refreshes the
|
|
/// ValueAsText property on a successful scalar write.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("NodeState")]
|
|
[SetCulture("en-us")]
|
|
[SetUICulture("en-us")]
|
|
[Parallelizable]
|
|
public class MultiStateValueDiscreteStateTests
|
|
{
|
|
// Deliberately sparse / non-zero-based to exercise the EnumValues lookup.
|
|
private static readonly (long Value, string Name)[] s_states =
|
|
[(1, "one"), (2, "two"), (4, "four"), (8, "eight")];
|
|
|
|
private SystemContext m_context;
|
|
|
|
[OneTimeSetUp]
|
|
public void OneTimeSetUp()
|
|
{
|
|
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
|
|
var messageContext = ServiceMessageContext.CreateEmpty(telemetry);
|
|
m_context = new SystemContext(telemetry)
|
|
{
|
|
NamespaceUris = messageContext.NamespaceUris,
|
|
ServerUris = messageContext.ServerUris,
|
|
EncodeableFactory = messageContext.Factory,
|
|
TypeTable = new TypeTable(messageContext.NamespaceUris)
|
|
};
|
|
}
|
|
|
|
[Test]
|
|
public void WriteMatchingSparseValueSucceeds()
|
|
{
|
|
MultiStateValueDiscreteState node = CreateNode(s_states);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)4));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(node.WrappedValue.TryGetValue(out uint written), Is.True);
|
|
Assert.That(written, Is.EqualTo(4u));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteMatchingValueUpdatesValueAsText()
|
|
{
|
|
MultiStateValueDiscreteState node = CreateNode(s_states);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)8));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(node.ValueAsText, Is.Not.Null);
|
|
Assert.That(node.ValueAsText.WrappedValue.TryGetValue(out LocalizedText text), Is.True);
|
|
Assert.That(text.Text, Is.EqualTo("eight"));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteValueBetweenGapsReturnsBadOutOfRange()
|
|
{
|
|
MultiStateValueDiscreteState node = CreateNode(s_states);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)3));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadOutOfRange));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteValueBetweenGapsLeavesValueAsTextUnchanged()
|
|
{
|
|
MultiStateValueDiscreteState node = CreateNode(s_states);
|
|
|
|
_ = WriteValue(node, Variant.From((uint)3));
|
|
|
|
Assert.That(node.ValueAsText, Is.Not.Null);
|
|
Assert.That(node.ValueAsText.WrappedValue.TryGetValue(out LocalizedText text), Is.True);
|
|
Assert.That(text.Text, Is.EqualTo("initial"));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteWithEmptyEnumValuesRejectsEveryValue()
|
|
{
|
|
MultiStateValueDiscreteState node = CreateNode([]);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)1));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadOutOfRange));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteWithoutEnumValuesChildFallsThroughToBase()
|
|
{
|
|
MultiStateValueDiscreteState node = CreateNode(null);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)99));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(node.WrappedValue.TryGetValue(out uint written), Is.True);
|
|
Assert.That(written, Is.EqualTo(99u));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteMatchingButNotWritableReturnsBadNotWritable()
|
|
{
|
|
MultiStateValueDiscreteState node = CreateNode(s_states);
|
|
node.AccessLevel = AccessLevels.CurrentRead;
|
|
node.UserAccessLevel = AccessLevels.CurrentRead;
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)4));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNotWritable));
|
|
}
|
|
|
|
private MultiStateValueDiscreteState CreateNode((long Value, string Name)[] states)
|
|
{
|
|
var node = new MultiStateValueDiscreteState(null)
|
|
{
|
|
NodeId = new NodeId(1000),
|
|
BrowseName = new QualifiedName("States"),
|
|
DataType = DataTypeIds.UInt32,
|
|
ValueRank = ValueRanks.Scalar,
|
|
AccessLevel = AccessLevels.CurrentReadOrWrite,
|
|
UserAccessLevel = AccessLevels.CurrentReadOrWrite,
|
|
Value = Variant.From((uint)0)
|
|
};
|
|
|
|
if (states != null)
|
|
{
|
|
var values = new EnumValueType[states.Length];
|
|
for (int ii = 0; ii < states.Length; ii++)
|
|
{
|
|
LocalizedText text = LocalizedText.From(states[ii].Name);
|
|
values[ii] = new EnumValueType
|
|
{
|
|
Value = states[ii].Value,
|
|
DisplayName = text,
|
|
Description = text
|
|
};
|
|
}
|
|
|
|
node.EnumValues = PropertyState<ArrayOf<EnumValueType>>
|
|
.With<StructureBuilder<EnumValueType>>(node, values.ToArrayOf());
|
|
node.EnumValues.NodeId = new NodeId(1001);
|
|
node.EnumValues.BrowseName = new QualifiedName(BrowseNames.EnumValues);
|
|
node.EnumValues.DataType = DataTypeIds.EnumValueType;
|
|
node.EnumValues.ValueRank = ValueRanks.OneDimension;
|
|
}
|
|
|
|
node.ValueAsText = PropertyState<LocalizedText>.With<VariantBuilder>(
|
|
node, LocalizedText.From("initial"));
|
|
node.ValueAsText.NodeId = new NodeId(1002);
|
|
node.ValueAsText.BrowseName = new QualifiedName(BrowseNames.ValueAsText);
|
|
node.ValueAsText.DataType = DataTypeIds.LocalizedText;
|
|
node.ValueAsText.ValueRank = ValueRanks.Scalar;
|
|
|
|
return node;
|
|
}
|
|
|
|
private ServiceResult WriteValue(MultiStateValueDiscreteState node, Variant value)
|
|
{
|
|
return node.WriteAttribute(
|
|
m_context,
|
|
Attributes.Value,
|
|
NumericRange.Null,
|
|
new DataValue(value));
|
|
}
|
|
}
|
|
}
|