Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/SdkValueBoundaryScriptResultTests.cs
T
Joseph Doherty 9dbccf231e fix(scripting): #1100 a script result is what an OPC UA node can carry, in both runners — Guid crosses the wire, unpublishable types are refused alike
Where an in-process result really ends up: VirtualTagActor -> VirtualTagHostActor -> OpcUaPublishActor ->
OtOpcUaNodeManager.WriteValueLocked -> SdkValueBoundary.FromClr (VariantHelper.TryCastFrom). Probed against
SDK 2.0.1: Guid / Guid[] become an OPC UA Guid scalar/array; KeyValuePair<,>[] (a returned Dictionary),
decimal, enum, TimeSpan, char, DateTimeOffset, tuples, Nullable<T>[] and rank>1 arrays are refused, so the
node write threw, OpcUaPublishActor logged a Warning, and the node kept its PREVIOUS value and status (a
silent freeze, possibly under Good) while the worker answered "unsupported type".

Decision, per type, by what a client can see:
- Guid (scalar + 1-D array): representable -> the wire gains an additive GUID tag (15, `bytes g = 10`,
  16 ToByteArray bytes; a wrong length is a protocol fault). Guid INPUTS cross too. protocol_version stays 1:
  the worker ships in the host's script-worker/ folder, and an unknown tag already fails closed.
- Everything FromClr refuses: refused identically in both runners by ScriptResultMaterializer, inside the run,
  with one reason ("script threw: The script returned a value of type 'KeyValuePair<Int32, Double>[]', which
  an OPC UA node cannot carry; return ..."). A lazy unpublishable sequence is refused before it runs.
  ScriptResultMaterializer.PublishableScalarTypes / IsPublishable own the set.

Pins: SdkValueBoundaryScriptResultTests (OpcUaServer.Tests, new Core.Scripting ref) checks IsPublishable ==
"FromClr takes it" over 34 values; ProtocolTests checks the wire carries every publishable type scalar + array;
ParityTests gains guid / guid array / guid list / dictionary / KVP[] / lazy KVPs / KVP / decimal / decimal[] /
enum / TimeSpan / char[] / 2-D rows, a Guid unboxing row, and a positive+negative control. Mutations: with the
type check disabled 11 parity tests fail; with Guid not encoded 9 protocol/parity tests fail.

Behaviour change: a value script returning a Dictionary, decimal, enum, TimeSpan, char or tuple now fails
(Bad + script log) in process too, instead of "succeeding" and leaving its node frozen. Remaining divergence:
a jagged array (publish path carries it as Variant[], worker answers "unsupported type") — follow-up.
2026-12-04 17:27:22 -05:00

62 lines
2.9 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
/// <summary>
/// lmxopcua#1100: the script-result rule (<see cref="ScriptResultMaterializer.IsPublishable"/>, applied by both
/// script runners) must agree with what the publish path really takes — <see cref="SdkValueBoundary.FromClr"/>, which
/// every node value write goes through. A value the rule passes but FromClr refuses is the defect #1100 found (the
/// node write threw, the node silently kept its previous value and status); a value FromClr takes but the rule refuses
/// would take a working result away from clients.
/// </summary>
[Trait("Category", "Unit")]
public sealed class SdkValueBoundaryScriptResultTests
{
public static TheoryData<string, object> Values() => new()
{
// publishable
{ "bool", true }, { "sbyte", (sbyte)1 }, { "byte", (byte)1 }, { "short", (short)1 }, { "ushort", (ushort)1 },
{ "int", 1 }, { "uint", 1u }, { "long", 1L }, { "ulong", 1UL }, { "float", 1f }, { "double", 1.0 },
{ "string", "s" }, { "DateTime", DateTime.UtcNow }, { "Guid", Guid.NewGuid() },
{ "int[]", new[] { 1 } }, { "byte[]", new byte[] { 1 } }, { "string[]", new[] { "a", null! } },
{ "DateTime[]", new[] { DateTime.UtcNow } }, { "Guid[]", new[] { Guid.NewGuid() } }, { "int[][]", new[] { new[] { 1 } } },
// not publishable
{ "decimal", 1.5m }, { "decimal[]", new[] { 1.5m } }, { "enum", DayOfWeek.Monday }, { "enum[]", new[] { DayOfWeek.Monday } },
{ "TimeSpan", TimeSpan.FromSeconds(1) }, { "char", 'a' }, { "char[]", new[] { 'a' } },
{ "DateTimeOffset", DateTimeOffset.UnixEpoch }, { "KeyValuePair", new KeyValuePair<int, int>(1, 2) },
{ "KeyValuePair[]", new[] { new KeyValuePair<int, int>(1, 2) } }, { "tuple[]", new[] { (1, 2) } },
{ "double[,]", new double[1, 1] }, { "Guid?[]", new Guid?[] { Guid.Empty } },
};
[Theory]
[MemberData(nameof(Values))]
public void The_script_result_rule_is_what_the_publish_path_takes(string name, object value)
{
var publishable = ScriptResultMaterializer.IsPublishable(value.GetType());
bool taken;
try
{
SdkValueBoundary.FromClr(value);
taken = true;
}
catch (Exception)
{
taken = false;
}
publishable.ShouldBe(taken, name);
}
[Fact]
public void Every_publishable_scalar_type_and_its_array_reach_the_publish_path()
{
foreach (var type in ScriptResultMaterializer.PublishableScalarTypes)
{
var scalar = type == typeof(string) ? "s" : Activator.CreateInstance(type)!;
Should.NotThrow(() => SdkValueBoundary.FromClr(scalar), type.Name);
Should.NotThrow(() => SdkValueBoundary.FromClr(Array.CreateInstance(type, 1)), type.Name + "[]");
}
}
}