From e12ff456e817ed9f8ae4a93594776790177a3b53 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Fri, 4 Dec 2026 22:14:18 -0500 Subject: [PATCH] test(script-worker): #1109 keep the worker's result UNSUPPORTED_TYPE check as defence in depth, and pin it Since #1100/#1104 the shared ScriptResultMaterializer refuses every type the wire cannot carry, so the worker's result-encoding error branch is unreachable from a script. Kept (not removed): PublishableScalarTypes and VariantCodec's tags are separate lists, and the branch turns any future drift into a reported UNSUPPORTED_TYPE failure instead of a value silently missing from the frame. Moved into VariantCodec.SetScriptResult (the worker's only result-encoding path, same message) with a comment saying so, and called directly by ProtocolTests. --- .../VariantCodec.cs | 30 +++++++++++++++++++ .../WorkerHost.cs | 12 ++------ .../ProtocolTests.cs | 30 +++++++++++++++++++ 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol/VariantCodec.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol/VariantCodec.cs index 1916db429..735715c15 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol/VariantCodec.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Protocol/VariantCodec.cs @@ -62,6 +62,36 @@ public static class VariantCodec } } + /// + /// Puts a value script's (already materialised) result on : the encoded value, or an + /// UNSUPPORTED_TYPE error naming the CLR type the wire has no tag for. The worker's only result-encoding path. + /// + /// + /// lmxopcua#1109 — DEFENCE IN DEPTH, deliberately kept. Since lmxopcua#1100/#1104 the shared result rule + /// (ScriptResultMaterializer.Materialize, which the worker runs first, inside the run) refuses every type the + /// wire cannot carry with a "script threw: … cannot carry …" reason, so no script reaches the error branch today. It + /// stays because the two type lists are separate code: if PublishableScalarTypes ever gains a type without a + /// wire tag here (or a tag is dropped), this branch turns the gap into a reported UNSUPPORTED_TYPE run failure + /// instead of a value silently missing from the frame. Pinned directly by ProtocolTests, since no script can + /// reach it. + /// + /// The eval result to fill. + /// The script's returned value, after the shared result rule. + public static void SetScriptResult(EvalResult result, object? value) + { + ArgumentNullException.ThrowIfNull(result); + if (TryEncode(value, out var variant, out var unsupported)) + { + result.Value = variant; + return; + } + result.Error = new ScriptError + { + Kind = ScriptError.Types.Kind.UnsupportedType, + Message = $"script returned a value of unsupported type {unsupported}", + }; + } + /// Decodes back to the boxed CLR value it was encoded from. /// The variant. /// The boxed value. diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker/WorkerHost.cs b/src/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker/WorkerHost.cs index fb8e1c92a..a02a06719 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker/WorkerHost.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker/WorkerHost.cs @@ -469,17 +469,11 @@ internal sealed class WorkerHost { result.Predicate = (bool)value!; } - else if (VariantCodec.TryEncode(value, out var variant, out var unsupported)) - { - result.Value = variant; - } else { - result.Error = new ScriptError - { - Kind = ScriptError.Types.Kind.UnsupportedType, - Message = $"script returned a value of unsupported type {unsupported}", - }; + // lmxopcua#1109: its UNSUPPORTED_TYPE branch is unreachable from a script since the shared rule above + // refuses first — kept as defence in depth against the two type lists drifting (see SetScriptResult). + VariantCodec.SetScriptResult(result, value); } } catch (Exception ex) diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests/ProtocolTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests/ProtocolTests.cs index a7a91c75d..f92c418f6 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests/ProtocolTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.ScriptWorker.Tests/ProtocolTests.cs @@ -67,6 +67,36 @@ public sealed class ProtocolTests } } + /// + /// lmxopcua#1109: the worker's result-encoding step. Its UNSUPPORTED_TYPE branch is unreachable from a script (the + /// shared result rule refuses first) and is kept as defence in depth, so it is called directly here: a value with no + /// wire tag — including one the shared rule also refuses, like a jagged array or a decimal — is a reported + /// UnsupportedType error naming the type, never a missing value; a supported value is encoded and carries no error. + /// + [Fact] + public void SetScriptResult_reports_an_unencodable_value_as_UnsupportedType() + { + foreach (var value in new object[] { 1.5m, new[] { new[] { 1 } }, new[] { new byte[] { 1 } }, TimeSpan.FromSeconds(1) }) + { + var result = new EvalResult { EvalId = 7 }; + VariantCodec.SetScriptResult(result, value); + result.Value.ShouldBeNull(value.GetType().Name); + result.Error.ShouldNotBeNull(value.GetType().Name); + result.Error.Kind.ShouldBe(ScriptError.Types.Kind.UnsupportedType); + result.Error.Message.ShouldBe($"script returned a value of unsupported type {value.GetType().FullName}"); + } + + var ok = new EvalResult { EvalId = 8 }; + VariantCodec.SetScriptResult(ok, new[] { 1.0, 2.0 }); + ok.Error.ShouldBeNull(); + VariantCodec.Decode(ok.Value).ShouldBe(new[] { 1.0, 2.0 }); + + var nothing = new EvalResult { EvalId = 9 }; + VariantCodec.SetScriptResult(nothing, null); + nothing.Error.ShouldBeNull(); + nothing.Value.Type.ShouldBe(Variant.Types.TypeTag.Null); + } + /// /// lmxopcua#1100: the wire carries every type a script result may have (, /// scalar and one-dimensional array), so nothing the materializer passes can fail "unsupported type" in the worker