d50ee809e3
Native LSV2 TCP transport (default port 19000; no Java/Python/vendor SDK), ported from the ignitionoee v0.20.3 reference driver: - Lsv2ReadOnlyPolicy: payload-aware outbound allowlist (A_LG INSPECT/DNC/DATA, R_VR, R_PR, R_RI 22-28/30-34/51, bounded R_MB, R_DP), applied in Lsv2Codec.Frame before any byte is written; no public raw-command API. - Lsv2Client: serialized transactions, one monotonic deadline per transaction, EOF/oversize/ unexpected/trickle close the client; Close never waits on the gate. - Lsv2Data / HeidenhainBindingCodec: reference decoders, R_PR byte-capacity + element-index rules, next-area cap, DATA Boolean 00/01/FF, strict non-truncating coercion. - HeidenhainSession: per-batch wire cache, refusal suppression, provisional DNC denial retry, alarm cursor (57 = end, overflow = BadOutOfRange), capability strings. - HeidenhainDeviceRuntime: one loop per control, generation-guarded publication, all-node BadNoCommunication on comms loss, bounded exponential reconnect. - HeidenhainDriver: cached ReadAsync, push subscriptions deduped on (value,status), static catalog discovery (no connect), host status, diagnostics, skipped-tag report. No IWritable. - Probe (INSPECT login + version) and factory registered in DriverFactoryBootstrap (probe on admin nodes too); DriverTypeNames.Heidenhain. Tests: 159 unit tests against an in-process LSV2 simulator incl. the Java fixtures' captured iTNC 530 replies as golden vectors; env-gated LiveIntegration category for real hardware.
164 lines
8.7 KiB
C#
164 lines
8.7 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests;
|
|
|
|
/// <summary>TagConfig grammar, catalog and options validation (reference <c>HeidenhainBindingsTest</c> / <c>HeidenhainConfigTest</c>).</summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class HeidenhainConfigTests
|
|
{
|
|
private static bool Accepts(string json) => HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", json, out _, out _);
|
|
|
|
[Fact]
|
|
public void Parses_both_binding_families_and_preserves_colons_in_data_paths()
|
|
{
|
|
HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", """{"address":"PLC:MARKER:32","dataType":"Boolean"}""", out var a, out _).ShouldBeTrue();
|
|
a!.Address.Area.ShouldBe(HeidenhainPlcArea.Marker);
|
|
a.Address.Index.ShouldBe(32);
|
|
HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", """{"address":"DATA:/TABLE/TNC:TOOL/DOC","dataType":"String"}""", out var b, out _).ShouldBeTrue();
|
|
b!.Address.DataPath.ShouldBe("/TABLE/TNC:TOOL/DOC");
|
|
b.DataType.ShouldBe(DriverDataType.String);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("""{"address":"PLC:WORD:0","dataType":"Boolean"}""")] // area/type mismatch
|
|
[InlineData("""{"address":"PLC:MARKER:0","dataType":"Int32"}""")]
|
|
[InlineData("""{"address":"PLC:BYTE:-1","dataType":"Int16"}""")]
|
|
[InlineData("""{"address":"PLC:DWORD:0","dataType":"Int16"}""")] // narrows native range
|
|
[InlineData("""{"address":"PLC:INPUT_WORD:0","dataType":"Int16"}""")] // uint16 into int16
|
|
[InlineData("""{"address":"PLC:STRING:0","dataType":"Int32"}""")]
|
|
[InlineData("""{"address":"PLC:WORD:99999999999","dataType":"Int64"}""")] // > uint32
|
|
[InlineData("""{"address":"PLC:WORD:4294967296","dataType":"Int64"}""")]
|
|
[InlineData("""{"address":"PLC:WORDS:0","dataType":"Int64"}""")]
|
|
[InlineData("""{"address":"PLC:WORD:0"}""")] // custom needs a dataType
|
|
[InlineData("""{"address":"PLC:WORD:0","dataType":"Float32"}""")]
|
|
[InlineData("""{"address":"PLC:WORD:0","dataType":"UInt16"}""")]
|
|
[InlineData("""{"address":"PLC:WORD:0","dataType":"8"}""")] // numerically-serialized enum
|
|
[InlineData("""{"address":"PLC:WORD:0","dataType":8}""")]
|
|
[InlineData("""{"address":"DATA:/a\u0000b","dataType":"String"}""")]
|
|
[InlineData("""{"address":"DATA:/€","dataType":"String"}""")]
|
|
[InlineData("""{"address":"DATA:","dataType":"String"}""")]
|
|
[InlineData("""{"address":"Status/Nope"}""")]
|
|
[InlineData("""{"address":"status/executionstate"}""")]
|
|
[InlineData("""{"address":"Status/ExecutionState","dataType":"String"}""")] // catalog type mismatch
|
|
[InlineData("""{"address":"Axes/Names"}""")] // array leaf without isArray
|
|
[InlineData("""{"address":"Status/ExecutionState","isArray":true}""")] // scalar marked array
|
|
[InlineData("""{"address":"Status/ExecutionState","writable":true}""")] // read-only driver
|
|
[InlineData("""{"address":42}""")]
|
|
[InlineData("""[]""")]
|
|
[InlineData("""not json""")]
|
|
[InlineData("")]
|
|
public void Rejects_unsafe_mismatched_or_malformed_blobs(string json)
|
|
{
|
|
Accepts(json).ShouldBeFalse(json);
|
|
HeidenhainTagDefinitionFactory.Inspect(json).ShouldNotBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Inspect_is_never_stricter_than_the_runtime_parse()
|
|
{
|
|
foreach (var e in HeidenhainCatalog.Entries)
|
|
{
|
|
var json = TestTags.Config(e.Address);
|
|
Accepts(json).ShouldBeTrue(json);
|
|
HeidenhainTagDefinitionFactory.Inspect(json).ShouldBeEmpty();
|
|
}
|
|
|
|
Accepts("""{"address":"PLC:INPUT_WORD:0","dataType":"Int32","isHistorized":true}""").ShouldBeTrue();
|
|
Accepts("""{"address":"Status/ExecutionState","dataType":"int32"}""").ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Catalog_is_general_purpose_read_only_and_without_oee_mapping()
|
|
{
|
|
HeidenhainCatalog.Entries.ShouldContain(e => e.Address == "Axes/Positions" && e.IsArray && e.DataType == DriverDataType.Float64);
|
|
HeidenhainCatalog.Entries.ShouldContain(e => e.Address == "Status/ExecutionState" && e.DataType == DriverDataType.Int32);
|
|
HeidenhainCatalog.Entries.ShouldNotContain(e => e.Address.Contains("RunState") || e.Address.Contains("GoodCount") || e.Address.Contains("IdealRate"));
|
|
HeidenhainCatalog.Entries.Select(e => e.Address).ShouldBeUnique();
|
|
HeidenhainCatalog.Entries.Where(e => e.IsCapability).ShouldAllBe(e => e.DataType == DriverDataType.String);
|
|
HeidenhainCatalog.Entries.Count(e => e.Address.StartsWith("Timers/")).ShouldBe(5);
|
|
}
|
|
|
|
[Fact]
|
|
public void Options_have_reference_defaults()
|
|
{
|
|
var o = HeidenhainOptionsParser.Parse("{}");
|
|
o.TimeoutMs.ShouldBe(5000);
|
|
o.Polling.StatusMs.ShouldBe(1000);
|
|
o.Polling.CustomMs.ShouldBe(1000);
|
|
o.Polling.SlowMs.ShouldBe(10000);
|
|
o.Polling.MaxAlarms.ShouldBe(64);
|
|
o.Polling.TimersEnabled.ShouldBeFalse();
|
|
o.Access.TelemetryEnabled.ShouldBeTrue();
|
|
o.Access.DataEnabled.ShouldBeFalse();
|
|
new HeidenhainDeviceOptions().Port.ShouldBe(19000);
|
|
HeidenhainOptionsParser.Validate(o).ShouldBeEmpty();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("""{"TimeoutMs":99}""")]
|
|
[InlineData("""{"TimeoutMs":60001}""")]
|
|
[InlineData("""{"Polling":{"StatusMs":50}}""")]
|
|
[InlineData("""{"Polling":{"SlowMs":3600001}}""")]
|
|
[InlineData("""{"Polling":{"MaxAlarms":0}}""")]
|
|
[InlineData("""{"Polling":{"MaxAlarms":257}}""")]
|
|
[InlineData("""{"Reconnect":{"InitialDelayMs":5000,"MaxDelayMs":1000}}""")]
|
|
[InlineData("""{"Access":{"InspectPassword":"hunter2"}}""")]
|
|
[InlineData("""{"Access":{"DncPassword":"secret:"}}""")]
|
|
[InlineData("""{"Devices":[{"Host":"h","DataPassword":"plain"}]}""")]
|
|
public void Invalid_options_are_rejected_without_echoing_credentials(string json)
|
|
{
|
|
var errors = HeidenhainOptionsParser.Validate(HeidenhainOptionsParser.Parse(json));
|
|
errors.ShouldNotBeEmpty();
|
|
string.Join(" ", errors).ShouldNotContain("hunter2");
|
|
string.Join(" ", errors).ShouldNotContain("plain'");
|
|
Should.Throw<InvalidOperationException>(() => HeidenhainDriver.ParseOptions(json));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("secret:lsv2-inspect")]
|
|
[InlineData("env:LSV2_DNC_PASSWORD")]
|
|
[InlineData("")]
|
|
[InlineData(null)]
|
|
public void Credential_references_are_accepted(string? value) =>
|
|
HeidenhainOptionsParser.IsCredentialReference(value).ShouldBeTrue();
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("http://host")]
|
|
[InlineData("host name")]
|
|
[InlineData("a\\b")]
|
|
public void Device_hosts_are_validated(string host) =>
|
|
HeidenhainOptionsParser.ValidateDevice(new HeidenhainDeviceOptions { Host = host }).ShouldNotBeEmpty();
|
|
|
|
[Fact]
|
|
public void Device_port_is_validated() =>
|
|
HeidenhainOptionsParser.ValidateDevice(new HeidenhainDeviceOptions { Host = "10.0.0.1", Port = 0 }).ShouldNotBeEmpty();
|
|
|
|
[Fact]
|
|
public async Task Credentials_resolve_from_secret_store_and_environment_and_fail_closed()
|
|
{
|
|
var resolver = new FakeResolver(new() { ["lsv2"] = "s3cret" });
|
|
var env = new Dictionary<string, string> { ["LSV2_PW"] = "envpw" };
|
|
var c = new HeidenhainCredentials(resolver, n => env.GetValueOrDefault(n));
|
|
var ct = TestContext.Current.CancellationToken;
|
|
(await c.ResolveReferenceAsync("secret:lsv2", "INSPECT", ct)).ShouldBe("s3cret");
|
|
(await c.ResolveReferenceAsync("env:LSV2_PW", "DNC", ct)).ShouldBe("envpw");
|
|
(await c.ResolveReferenceAsync(null, "DNC", ct)).ShouldBe("");
|
|
await Should.ThrowAsync<InvalidOperationException>(() => c.ResolveReferenceAsync("secret:missing", "DNC", ct).AsTask());
|
|
await Should.ThrowAsync<InvalidOperationException>(() => c.ResolveReferenceAsync("env:UNSET", "DNC", ct).AsTask());
|
|
await Should.ThrowAsync<InvalidOperationException>(() => c.ResolveReferenceAsync("literal", "DNC", ct).AsTask());
|
|
await Should.ThrowAsync<InvalidOperationException>(() => new HeidenhainCredentials(null).ResolveReferenceAsync("secret:lsv2", "DNC", ct).AsTask());
|
|
// device override wins over the driver-level reference
|
|
var o = new HeidenhainDriverOptions { Access = new() { InspectPassword = "secret:missing" } };
|
|
(await c.ResolveAsync(o, new HeidenhainDeviceOptions { Host = "h", InspectPassword = "secret:lsv2" }, "INSPECT", ct)).ShouldBe("s3cret");
|
|
}
|
|
|
|
private sealed class FakeResolver(Dictionary<string, string> secrets) : ZB.MOM.WW.Secrets.Abstractions.ISecretResolver
|
|
{
|
|
public Task<string?> GetAsync(ZB.MOM.WW.Secrets.Abstractions.SecretName name, CancellationToken ct) =>
|
|
Task.FromResult(secrets.TryGetValue(name.Value, out var v) ? v : null);
|
|
}
|
|
}
|