f5475868f7
#556 item 1 — leading whitespace defeated every Inspect's pre-filter. Seven factories (Modbus/S7/AbCip/AbLegacy/TwinCAT/FOCAS/Mqtt) tested `reference[0] != '{'`, so a padded blob returned EMPTY and a tag the driver refuses deployed clean. The check now trims first, matching the idiom Sql/Calculation/MTConnect/OpcUaClient already used. The raw reference is still what is handed to the mapper — the driver reads the stored blob, not a trimmed copy — so the gate keeps mirroring the runtime rather than becoming lenient. Both directions are pinned: a padded blob the mapper accepts still draws nothing, and one that is not object-framed AFTER trimming still draws nothing (it is not an equipment-tag TagConfig at all). Six of the seven mappers hand the blob to a whitespace-tolerant JsonDocument.Parse, so their verdict is unchanged by padding. MQTT is the exception: both its readers carry the same framing gate, so a padded blob is genuinely refused — which is why the untrimmed pre-filter was a real defect there and not a cosmetic mismatch. #556 item 2 — FOCAS and MQTT appended advisories unconditionally and then gated the catch-all rejection message on `warnings.Count == 0`, so an advisory (which fires on ACCEPTED blobs too, and says nothing about a refusal) silently swallowed the only message naming a refusal nothing else can. The two classes now accumulate in SEPARATE lists and the backstop gates on the rejection count alone — structural, not string matching. MQTT's swallow was reachable in production (a padded blob its readers refuse while every describe branch finds the parsed document well-formed); FOCAS's backstop is currently unreachable by audit, so its guard is protective. #557 item 2 — decided and pinned: KEEP the union verdict. Evidence: FromSparkplugTagConfig hard-codes PayloadFormat=Json and never consults the key; only MqttSubscriptionManager (the Plain ingest path) decodes by it; and MqttTagConfigModel.ToJson deliberately omits the key entirely in Sparkplug mode (#513). So a Sparkplug driver genuinely serves the tag and rejecting it would be stricter than the runtime. The cost — served behaviour depends on the driver's MqttMode, which no tag row records — is paid by an ADVISORY naming the key and the mode dependency, not by changing the verdict, so both gates keep agreeing. The same invalid value on a blob the union REFUSES stays a rejection; the class is decided by the accept gate, never by the key. Falsified: reverting Modbus's trim reddens the padded-rejection theory; neutering MQTT's backstop guard to the pre-#556 shape reddens the advisory-plus-unnameable-rejection test; disabling the #557 advisory reddens the contract pin; and for FOCAS, removing a describe branch alone stays green (the backstop covers it) while additionally neutering the guard goes red.
160 lines
7.8 KiB
C#
160 lines
7.8 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Tests;
|
|
|
|
/// <summary>
|
|
/// Gitea #551 item 2: <see cref="S7TagDefinitionFactory.Inspect"/> is <b>accept-gate-first</b> — it asks
|
|
/// <see cref="S7TagDefinitionFactory.FromTagConfig"/> for the verdict and only explains a refusal. See
|
|
/// <c>ModbusInspectAcceptGateTests</c>'s remarks for why both directions (never stricter, never silent)
|
|
/// are pinned.
|
|
/// <para>
|
|
/// S7 carries the fleet's one unique rule: the reader REQUIRES the <c>dataType</c> key to be
|
|
/// present. A blob with only an <c>address</c> therefore deploys green and reads
|
|
/// <c>BadNodeIdUnknown</c> forever — and neither this gate nor the AdminUI model said a word about
|
|
/// it until #551.
|
|
/// </para>
|
|
/// </summary>
|
|
public sealed class S7InspectAcceptGateTests
|
|
{
|
|
private const string RawPath = "cell/s7/dev1/T1";
|
|
|
|
/// <summary>Blobs the mapper ACCEPTS — the gate must say nothing about any of them.</summary>
|
|
public static TheoryData<string> Accepted() =>
|
|
[
|
|
"""{"address":"DB1.DBW0","dataType":"Int16"}""",
|
|
"""{"address":"M0.0","dataType":"Bool"}""",
|
|
"""{"address":"DB1.DBB0","dataType":"String","stringLength":254}""",
|
|
// stringLength is irrelevant to a non-String tag, so an out-of-range remnant is ACCEPTED.
|
|
"""{"address":"DB1.DBW0","dataType":"Int16","stringLength":9999}""",
|
|
// A non-string dataType token satisfies the PRESENCE rule and then reads as absent ⇒ Int16.
|
|
"""{"address":"DB1.DBW0","dataType":7}""",
|
|
];
|
|
|
|
/// <summary>Blobs the mapper REJECTS — the gate must produce at least one warning for each.</summary>
|
|
public static TheoryData<string> Rejected() =>
|
|
[
|
|
"""{"dataType":"Int16"}""", // no address
|
|
"""{"address":"","dataType":"Int16"}""", // blank address
|
|
"""{"address":123,"dataType":"Int16"}""", // address not a string
|
|
"""{"address":"DB1.DBW0"}""", // no dataType key at all (#551)
|
|
"""{"address":"DB1.DBW0","dataType":"Intt16"}""", // typo'd dataType
|
|
"""{"address":"DB1.DBB0","dataType":"String","stringLength":255}""", // over the 254 S7 limit
|
|
"""{"address":"DB1.DBB0","dataType":"String","stringLength":-1}""", // negative length
|
|
];
|
|
|
|
[Theory]
|
|
[MemberData(nameof(Accepted))]
|
|
public void A_config_the_mapper_accepts_draws_no_warning(string tagConfig)
|
|
{
|
|
S7TagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeTrue("fixture must be accepted");
|
|
S7TagDefinitionFactory.Inspect(tagConfig).ShouldBeEmpty();
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(Rejected))]
|
|
public void A_config_the_mapper_rejects_is_never_passed_over_in_silence(string tagConfig)
|
|
{
|
|
S7TagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeFalse("fixture must be rejected");
|
|
S7TagDefinitionFactory.Inspect(tagConfig).ShouldNotBeEmpty();
|
|
}
|
|
|
|
/// <summary>The headline case: present-and-typo'd was reported; ABSENT was not (Gitea #551 item 2).</summary>
|
|
[Fact]
|
|
public void An_absent_dataType_is_named()
|
|
=> S7TagDefinitionFactory.Inspect("""{"address":"DB1.DBW0"}""")
|
|
.ShouldContain(w => w.Contains("no 'dataType'"));
|
|
|
|
[Fact]
|
|
public void An_absent_address_is_named()
|
|
=> S7TagDefinitionFactory.Inspect("""{"dataType":"Int16"}""")
|
|
.ShouldContain(w => w.Contains("'address'"));
|
|
|
|
[Fact]
|
|
public void An_out_of_range_string_length_is_named()
|
|
=> S7TagDefinitionFactory.Inspect("""{"address":"DB1.DBB0","dataType":"String","stringLength":255}""")
|
|
.ShouldContain(w => w.Contains("stringLength"));
|
|
|
|
// ---- DeclaresDataType is the shared authority the AdminUI model asks (one rule, two doors) ----
|
|
|
|
[Theory]
|
|
[InlineData("""{"dataType":"Int16"}""", true)]
|
|
[InlineData("""{"dataType":7}""", true)] // presence is judged on the KEY, not the value
|
|
[InlineData("""{"dataType":null}""", true)]
|
|
[InlineData("""{"address":"DB1.DBW0"}""", false)]
|
|
public void DeclaresDataType_judges_the_key_not_the_value(string tagConfig, bool expected)
|
|
{
|
|
using var doc = System.Text.Json.JsonDocument.Parse(tagConfig);
|
|
S7TagDefinitionFactory.DeclaresDataType(doc.RootElement).ShouldBe(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_blank_reference_draws_nothing()
|
|
=> S7TagDefinitionFactory.Inspect(" ").ShouldBeEmpty();
|
|
|
|
[Fact]
|
|
public void Unparseable_json_names_the_json()
|
|
=> S7TagDefinitionFactory.Inspect("{oops").ShouldContain(w => w.Contains("not valid JSON"));
|
|
|
|
// ---- Gitea #556 item 1: leading whitespace no longer defeats the framing pre-filter ----
|
|
|
|
/// <summary>
|
|
/// Padding a blob with leading whitespace. A driver is handed whatever authoring persisted, and
|
|
/// <c>FromTagConfig</c> passes it straight to <c>JsonDocument.Parse</c>, which skips leading
|
|
/// whitespace — so the MAPPER's verdict is identical padded or not, and the gate's must be too.
|
|
/// </summary>
|
|
private const string Pad = " \t\r\n ";
|
|
|
|
/// <summary>
|
|
/// Never stricter, under padding: a padded blob the mapper still accepts draws nothing. (This
|
|
/// direction already held before #556 — the pre-filter returned empty for everything padded — and
|
|
/// is kept so a future "just trim everywhere" change cannot start warning about a served tag.)
|
|
/// </summary>
|
|
/// <param name="tagConfig">An accepted fixture, padded below.</param>
|
|
[Theory]
|
|
[MemberData(nameof(Accepted))]
|
|
public void Leading_whitespace_leaves_an_accepted_config_silent(string tagConfig)
|
|
{
|
|
var padded = Pad + tagConfig;
|
|
S7TagDefinitionFactory.FromTagConfig(padded, RawPath, out _)
|
|
.ShouldBeTrue("the mapper's JsonDocument.Parse skips leading whitespace");
|
|
S7TagDefinitionFactory.Inspect(padded).ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Never silent, under padding — the #556 item 1 defect itself. The pre-filter tested
|
|
/// <c>reference[0] != '{'</c>, so EVERY padded blob returned empty and a padded config the driver
|
|
/// refuses deployed clean. Asserting equality with the untrimmed inspection (rather than merely
|
|
/// "not empty") also pins that padding changes no message.
|
|
/// </summary>
|
|
/// <param name="tagConfig">A rejected fixture, padded below.</param>
|
|
[Theory]
|
|
[MemberData(nameof(Rejected))]
|
|
public void Leading_whitespace_draws_exactly_the_warnings_the_untrimmed_blob_draws(string tagConfig)
|
|
{
|
|
var padded = Pad + tagConfig;
|
|
S7TagDefinitionFactory.FromTagConfig(padded, RawPath, out _)
|
|
.ShouldBeFalse("the mapper's verdict is unchanged by padding");
|
|
S7TagDefinitionFactory.Inspect(padded).ShouldBe(S7TagDefinitionFactory.Inspect(tagConfig));
|
|
}
|
|
|
|
/// <summary>
|
|
/// The other half of the same fix, and the one it would be easy to lose: trimming widens the gate
|
|
/// ONLY for JSON objects. A reference that is still not object-framed after the trim is not an
|
|
/// equipment-tag TagConfig at all — the pre-existing convention shared by all eleven inspectors —
|
|
/// so it keeps drawing nothing even though the mapper refuses it.
|
|
/// </summary>
|
|
/// <param name="reference">A padded, non-object reference.</param>
|
|
[Theory]
|
|
[InlineData(" just a string")]
|
|
[InlineData(" [1,2,3]")]
|
|
[InlineData(" \t\r\n 42")]
|
|
[InlineData(" \"a\"")]
|
|
[InlineData(" ")]
|
|
public void A_non_object_reference_still_draws_nothing_after_trimming(string reference)
|
|
{
|
|
S7TagDefinitionFactory.FromTagConfig(reference, RawPath, out _).ShouldBeFalse();
|
|
S7TagDefinitionFactory.Inspect(reference).ShouldBeEmpty();
|
|
}
|
|
}
|