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.
This commit is contained in:
Joseph Doherty
2026-12-04 22:14:18 -05:00
parent ec17a83aea
commit e12ff456e8
3 changed files with 63 additions and 9 deletions
@@ -62,6 +62,36 @@ public static class VariantCodec
}
}
/// <summary>
/// Puts a value script's (already materialised) result on <paramref name="result"/>: the encoded value, or an
/// <c>UNSUPPORTED_TYPE</c> error naming the CLR type the wire has no tag for. The worker's only result-encoding path.
/// </summary>
/// <remarks>
/// lmxopcua#1109 — DEFENCE IN DEPTH, deliberately kept. Since lmxopcua#1100/#1104 the shared result rule
/// (<c>ScriptResultMaterializer.Materialize</c>, 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 <c>PublishableScalarTypes</c> ever gains a type without a
/// wire tag here (or a tag is dropped), this branch turns the gap into a reported <c>UNSUPPORTED_TYPE</c> run failure
/// instead of a value silently missing from the frame. Pinned directly by <c>ProtocolTests</c>, since no script can
/// reach it.
/// </remarks>
/// <param name="result">The eval result to fill.</param>
/// <param name="value">The script's returned value, after the shared result rule.</param>
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}",
};
}
/// <summary>Decodes <paramref name="variant"/> back to the boxed CLR value it was encoded from.</summary>
/// <param name="variant">The variant.</param>
/// <returns>The boxed value.</returns>
@@ -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)
@@ -67,6 +67,36 @@ public sealed class ProtocolTests
}
}
/// <summary>
/// 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.
/// </summary>
[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);
}
/// <summary>
/// lmxopcua#1100: the wire carries every type a script result may have (<see cref="Core.Scripting.ScriptResultMaterializer.PublishableScalarTypes"/>,
/// scalar and one-dimensional array), so nothing the materializer passes can fail "unsupported type" in the worker