bd4fff6444
#1083: OTSCRIPT_DYNPATH was an error in every editor mode while the deploy gate refuses a non-literal tag path only for a scripted-alarm predicate (editor stricter than the gate). Decision: do not make the gate stricter (it would reject configurations that deploy today). ScriptCompileRules now owns the split — RefusesNonLiteralTagPaths(kind) and TagPathFindings(kind, source), each finding carrying Refuses — the gate refuses on exactly those findings and the editor draws them as errors (8) and the rest as warnings (4), whose text says what the path does at runtime (not a declared input: not subscribed, does not re-trigger, reads BadNodeIdUnknown unless also read literally). DependencyRejection gains Kind + MethodName for that wording. EditorGateParityTests carries dynamic / concatenated / interpolated / empty paths in all three modes (14 rows fail with the old always-error severity). #1093: hovering a {{equip}} path literal in Calculation mode showed the VirtualTag "resolves ... when deployed" text, contradicting the OTSCRIPT_EQUIPCALC marker on the same span. It now says the token is refused in a Calculation script and why (no owning equipment, never substituted, the gate refuses it). The {{equip}} hover no longer needs a tag catalog.
158 lines
9.3 KiB
C#
158 lines
9.3 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.ScriptAnalysis;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.ScriptAnalysis;
|
|
|
|
public sealed class DiagnoseTests
|
|
{
|
|
private static readonly ScriptAnalysisService Svc = new();
|
|
private static IReadOnlyList<DiagnosticMarker> Diag(string code) => Svc.Diagnose(new DiagnoseRequest(code)).Markers;
|
|
|
|
[Fact] public void Clean_script_has_no_markers()
|
|
=> Diag("""return (double)ctx.GetTag("A").Value * 2.0;""").ShouldBeEmpty();
|
|
|
|
[Fact] public void Roslyn_error_is_reported_on_user_line_1()
|
|
{
|
|
var m = Diag("return ctx.NoSuchMember();");
|
|
m.ShouldNotBeEmpty();
|
|
m.ShouldContain(x => x.Severity == 8);
|
|
m.First(x => x.Severity == 8).StartLineNumber.ShouldBe(1); // #line 1 → user coords (GetMappedLineSpan)
|
|
}
|
|
|
|
[Fact] public void Roslyn_error_maps_to_correct_user_line()
|
|
{
|
|
// error on the THIRD user line — proves mapping isn't off by the preamble height
|
|
var m = Diag("var a = 1;\nvar b = 2;\nreturn ctx.Nope();");
|
|
m.ShouldContain(x => x.Severity == 8 && x.StartLineNumber == 3);
|
|
}
|
|
|
|
[Fact] public void Forbidden_or_unresolved_dangerous_type_is_flagged()
|
|
=> Diag("""var c = new System.Net.Http.HttpClient(); return 0;""")
|
|
.ShouldContain(x => x.Severity == 8);
|
|
|
|
// Sandbox hardening parity (R05:10): the editor runs the SAME ForbiddenTypeAnalyzer the publish gate
|
|
// runs, so every newly-denied form must surface as an OTSCRIPT_FORBIDDEN marker here — editor accepts
|
|
// ⇔ publish accepts. One case per deny mechanism (namespace, member, type), plus the allow-list.
|
|
[Theory]
|
|
[InlineData("""new System.Threading.SemaphoreSlim(0).Wait(); return 0;""")] // System.Threading namespace
|
|
[InlineData("""System.Threading.SpinWait.SpinUntil(() => false); return 0;""")]
|
|
[InlineData("""var t = System.Type.GetType("System.IO.File"); return t is null ? 0 : 1;""")] // forbidden member
|
|
[InlineData("""return typeof(string).GetMethods().Length;""")]
|
|
[InlineData("""var o = System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(typeof(string)); return 0;""")] // forbidden type
|
|
public void Hardened_sandbox_forms_are_flagged_in_the_editor(string code)
|
|
=> Diag(code).ShouldContain(x => x.Code == "OTSCRIPT_FORBIDDEN" && x.Severity == 8);
|
|
|
|
[Theory]
|
|
[InlineData("""var t = System.Threading.CancellationToken.None; return t.IsCancellationRequested ? 1 : 0;""")]
|
|
[InlineData("""var x = 41; System.Threading.Interlocked.Increment(ref x); return x;""")]
|
|
[InlineData("""var x = 42; return System.Threading.Volatile.Read(ref x);""")]
|
|
[InlineData("""object o = 42; return o.GetType().IsValueType ? 1 : 0;""")]
|
|
[InlineData("""ctx.CancellationToken.ThrowIfCancellationRequested(); return ctx.CancellationToken.IsCancellationRequested ? 1 : 0;""")]
|
|
public void Allow_listed_forms_are_not_flagged_in_the_editor(string code)
|
|
=> Diag(code).ShouldBeEmpty();
|
|
|
|
// lmxopcua#971 (review 2026-12-02 K-1): the allow-list sandbox. Each bypass the old deny-list let through is now an
|
|
// error marker in the editor — OTSCRIPT_FORBIDDEN where the API still binds against the narrowed reference set,
|
|
// a compiler error where it no longer binds at all — and the editor's verdict matches the publish gate's.
|
|
[Theory]
|
|
[InlineData("""var g = (Func<string, Type>)Delegate.CreateDelegate(typeof(Func<string, Type>), typeof(Type), "GetType"); return 0;""", true)]
|
|
[InlineData("""Serilog.Log.Logger = Serilog.Core.Logger.None; return 0;""", true)]
|
|
[InlineData("""return typeof(int).Assembly.Location;""", true)]
|
|
[InlineData("""ctx.CancellationToken.Register(() => { }); return 0;""", true)]
|
|
[InlineData("""Func<int> f = () => 1; return f.DynamicInvoke();""", true)]
|
|
[InlineData("""int i = 1; var r = __makeref(i); return 0;""", true)]
|
|
[InlineData("""return System.Xml.Linq.XDocument.Load("/etc/hosts").ToString();""", false)]
|
|
[InlineData("""System.Formats.Tar.TarFile.CreateFromDirectory("/tmp", "/tmp/x.tar", false); return 0;""", false)]
|
|
[InlineData("""var t = new System.Timers.Timer(50); t.Start(); return 0;""", false)]
|
|
[InlineData("""return Enumerable.Range(0, 100).AsParallel().Sum();""", false)]
|
|
[InlineData("""return new System.Security.Cryptography.X509Certificates.X509Store().Name;""", false)]
|
|
public void K1_bypass_is_an_error_marker_in_the_editor(string code, bool bindsAndIsForbidden)
|
|
{
|
|
var markers = Diag(code);
|
|
if (bindsAndIsForbidden) markers.ShouldContain(x => x.Code == "OTSCRIPT_FORBIDDEN" && x.Severity == 8);
|
|
else markers.ShouldContain(x => x.Severity == 8);
|
|
}
|
|
|
|
// Editor ⇔ publish parity over both refused and accepted scripts: an error marker exactly when the publish gate
|
|
// (ScriptEvaluator over the same VirtualTagContext) refuses.
|
|
[Theory]
|
|
[InlineData("""var g = (Func<string, Type>)Delegate.CreateDelegate(typeof(Func<string, Type>), typeof(Type), "GetType"); return 0;""")]
|
|
[InlineData("""return typeof(int).Assembly.Location;""")]
|
|
[InlineData("""return System.Xml.Linq.XDocument.Load("/etc/hosts").ToString();""")]
|
|
[InlineData("""lock (typeof(string)) { } return 0;""")]
|
|
[InlineData("""return typeof(int).Name + System.Math.Round(1.25, 1).ToString(System.Globalization.CultureInfo.InvariantCulture);""")]
|
|
[InlineData("""var d = new System.Collections.Generic.Dictionary<string, int> { ["a"] = 1 }; return d.Values.Sum();""")]
|
|
[InlineData("""return System.Text.RegularExpressions.Regex.IsMatch("T-1", @"T-\d") ? ctx.Now.Hour : 0;""")]
|
|
[InlineData("""ctx.Logger.Information("v {V}", ctx.GetTag("A").Value); return ctx.GetTag("A").StatusCode == 0u;""")]
|
|
public void Editor_and_publish_gate_agree(string code)
|
|
{
|
|
bool publishRefuses;
|
|
try
|
|
{
|
|
ZB.MOM.WW.OtOpcUa.Core.Scripting.ScriptEvaluator<ZB.MOM.WW.OtOpcUa.Core.VirtualTags.VirtualTagContext, object?>.CompileToImage(code);
|
|
publishRefuses = false;
|
|
}
|
|
catch (Exception ex) when (ex is ZB.MOM.WW.OtOpcUa.Core.Scripting.ScriptSandboxViolationException
|
|
or ZB.MOM.WW.OtOpcUa.Core.Scripting.CompilationErrorException)
|
|
{
|
|
publishRefuses = true;
|
|
}
|
|
Diag(code).Any(x => x.Severity == 8).ShouldBe(publishRefuses);
|
|
}
|
|
|
|
// lmxopcua#1083: flagged as a WARNING in the (default) value mode — the deploy gate accepts it there; only a
|
|
// scripted-alarm predicate refuses it (EditorGateParityTests pins the per-mode split).
|
|
[Fact] public void Dynamic_tag_path_is_flagged()
|
|
=> Diag("""var p = "A"; return ctx.GetTag(p).Value;""")
|
|
.ShouldContain(x => x.Code == "OTSCRIPT_DYNPATH" && x.Severity == 4 && x.Message.Contains("literal"));
|
|
|
|
// lmxopcua#1063: this used to assert the OPPOSITE ("does not produce a phantom preamble marker"). CS0161 is not a
|
|
// phantom: Roslyn pins it on the wrapper's Run, and the runtime compile / deploy gate refuse the script. It is now
|
|
// reported, on the script's last line (the wrapper location means "the script as a whole").
|
|
[Fact] public void Missing_return_is_an_error_on_the_scripts_last_line()
|
|
{
|
|
var m = Diag("var x = 1;\nvar y = x + 1;").Single(x => x.Code == "CS0161");
|
|
m.Severity.ShouldBe(8);
|
|
m.StartLineNumber.ShouldBe(2);
|
|
m.EndLineNumber.ShouldBe(2);
|
|
m.StartColumn.ShouldBe(1);
|
|
m.EndColumn.ShouldBe("var y = x + 1;".Length + 1);
|
|
m.Message.ShouldBe(ScriptAnalysisService.NotAllPathsReturnMessage);
|
|
}
|
|
|
|
[Fact] public void Trailing_blank_lines_do_not_move_the_whole_script_marker_onto_nothing()
|
|
{
|
|
var m = Diag("var x = 1;\nvar y = x + 1;\n\n \n").Single(x => x.Code == "CS0161");
|
|
m.StartLineNumber.ShouldBe(2);
|
|
m.EndColumn.ShouldBe("var y = x + 1;".Length + 1);
|
|
}
|
|
|
|
[Fact] public void A_blank_predicate_is_refused_like_the_gate_refuses_it()
|
|
{
|
|
// A blank VALUE script never compiles at runtime (passes); a blank PREDICATE compiles and fails CS0161.
|
|
Diag("").ShouldBeEmpty();
|
|
Svc.Diagnose(new DiagnoseRequest("", Mode: ScriptAnalysisModes.AlarmPredicate), TestContext.Current.CancellationToken)
|
|
.Markers.ShouldContain(m => m.Code == "CS0161" && m.StartLineNumber == 1 && m.EndColumn >= 1);
|
|
}
|
|
|
|
[Fact] public void A_path_that_falls_off_the_end_is_an_error()
|
|
=> Diag("""if (ctx.GetTag("A").Value is null) return 1;""").ShouldContain(m => m.Code == "CS0161" && m.Severity == 8);
|
|
|
|
[Fact] public void Every_path_returning_is_clean_positive_control()
|
|
=> Diag("""if (ctx.GetTag("A").Value is null) return 1; return 2;""").ShouldBeEmpty();
|
|
|
|
[Fact] public void A_wrapper_injection_is_an_error_like_the_runtime_compile()
|
|
=> Diag("return 0; } public static int Evil() { return 0;").ShouldContain(m => m.Code == "LMX002" && m.Severity == 8);
|
|
|
|
[Fact] public void Empty_code_has_no_markers()
|
|
=> Diag("").ShouldBeEmpty();
|
|
|
|
[Fact] public void Crlf_source_maps_dynamic_path_to_the_correct_line()
|
|
{
|
|
// \r\n line endings; the dynamic-path rejection must land on user line 3, not be column-shifted.
|
|
var m = Diag("var a = 1;\r\nvar b = 2;\r\nreturn ctx.GetTag(b > 0 ? \"x\" : \"y\").Value;");
|
|
m.ShouldContain(x => x.Message.Contains("literal") && x.StartLineNumber == 3);
|
|
}
|
|
}
|