mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-21 09:06:37 +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>
172 lines
6.4 KiB
C#
172 lines
6.4 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 MultiStateDiscreteState write validation which rejects
|
|
/// numeric writes that fall outside the range of the EnumStrings lookup table.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("NodeState")]
|
|
[SetCulture("en-us")]
|
|
[SetUICulture("en-us")]
|
|
[Parallelizable]
|
|
public class MultiStateDiscreteStateTests
|
|
{
|
|
private static readonly string[] s_states = ["open", "closed", "jammed"];
|
|
|
|
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 WriteInRangeValueSucceeds()
|
|
{
|
|
MultiStateDiscreteState node = CreateNode(s_states);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)2));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(node.WrappedValue.TryGetValue(out uint written), Is.True);
|
|
Assert.That(written, Is.EqualTo(2u));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteValueEqualToLengthReturnsBadOutOfRange()
|
|
{
|
|
MultiStateDiscreteState node = CreateNode(s_states);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)3));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadOutOfRange));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteValueAboveRangeReturnsBadOutOfRange()
|
|
{
|
|
MultiStateDiscreteState node = CreateNode(s_states);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)99));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadOutOfRange));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteWithEmptyEnumStringsRejectsEveryValue()
|
|
{
|
|
MultiStateDiscreteState node = CreateNode([]);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)0));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadOutOfRange));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteWithoutEnumStringsChildFallsThroughToBase()
|
|
{
|
|
MultiStateDiscreteState node = CreateNode(null);
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)42));
|
|
|
|
Assert.That(ServiceResult.IsGood(result), Is.True);
|
|
Assert.That(node.WrappedValue.TryGetValue(out uint written), Is.True);
|
|
Assert.That(written, Is.EqualTo(42u));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteMemberButNotWritableReturnsBadNotWritable()
|
|
{
|
|
MultiStateDiscreteState node = CreateNode(s_states);
|
|
node.AccessLevel = AccessLevels.CurrentRead;
|
|
node.UserAccessLevel = AccessLevels.CurrentRead;
|
|
|
|
ServiceResult result = WriteValue(node, Variant.From((uint)1));
|
|
|
|
Assert.That(result.StatusCode.Code, Is.EqualTo(StatusCodes.BadNotWritable));
|
|
}
|
|
|
|
private MultiStateDiscreteState CreateNode(string[] enumStrings)
|
|
{
|
|
var node = new MultiStateDiscreteState(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 (enumStrings != null)
|
|
{
|
|
var entries = new LocalizedText[enumStrings.Length];
|
|
for (int ii = 0; ii < enumStrings.Length; ii++)
|
|
{
|
|
entries[ii] = LocalizedText.From(enumStrings[ii]);
|
|
}
|
|
|
|
node.EnumStrings = PropertyState<ArrayOf<LocalizedText>>.With<VariantBuilder>(
|
|
node, entries.ToArrayOf());
|
|
node.EnumStrings.NodeId = new NodeId(1001);
|
|
node.EnumStrings.BrowseName = new QualifiedName(BrowseNames.EnumStrings);
|
|
node.EnumStrings.DataType = DataTypeIds.LocalizedText;
|
|
node.EnumStrings.ValueRank = ValueRanks.OneDimension;
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
private ServiceResult WriteValue(MultiStateDiscreteState node, Variant value)
|
|
{
|
|
return node.WriteAttribute(
|
|
m_context,
|
|
Attributes.Value,
|
|
NumericRange.Null,
|
|
new DataValue(value));
|
|
}
|
|
}
|
|
}
|