Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests/HeidenhainLiveIntegrationTests.cs
T
Joseph Doherty f7a3588b24 fix(heidenhain): namespaced credentials, optional-role credential isolation, loop robustness (#803 review)
- Credential exfiltration (review 2): password references are scoped to
  secret:heidenhain/<name> and env:OTOPCUA_HEIDENHAIN_<NAME> only. Enforced by
  HeidenhainOptionsParser.TryParseCredentialReference (parser, probe, deploy gate) AND again at
  resolve time in HeidenhainCredentials (fail closed without consulting the store or the
  environment), so Test Connect can no longer send env:ConnectionStrings__ConfigDb or another
  subsystem's secret to an arbitrary Host. The probe also refuses an unsendable password.
- Optional-role credentials (review 6): an unresolvable/NUL/non-Latin-1/>247-byte DNC or DATA
  credential never sends a login and never fails the session; the reads needing that role are
  BadConfigurationError (capability ConfigurationError) and INSPECT telemetry continues. A
  policy refusal inside AcquireAsync is per-tag, not a session teardown.
- Start after DisposeAsync is a no-op (review 3); initial subscription push and Publish are
  ordered under one lock (review 4); JSON-null sections are normalised and the acquisition loop
  catches every fault (review 5); backoff resets only after a session published a poll batch,
  with clamped delays (review 7).

Tests: HeidenhainReviewFixTests + namespace/normalisation tests in HeidenhainConfigTests;
each was confirmed red against the reverted fix.
2026-11-10 23:49:48 -05:00

89 lines
4.5 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Lsv2;
namespace ZB.MOM.WW.OtOpcUa.Driver.Heidenhain.Tests;
/// <summary>
/// Real-controller commissioning gate — env-gated, skipped unless <c>HEIDENHAIN_LSV2_HOST</c> is set. READ-ONLY:
/// it issues only the allowlisted INSPECT login, version, layout and runtime-info reads that the driver itself
/// uses (every frame goes through <see cref="Lsv2ReadOnlyPolicy"/>). Run with
/// <c>dotnet test --filter Category=LiveIntegration</c>; record the output as .NET commissioning evidence.
/// </summary>
/// <remarks>
/// Env: <c>HEIDENHAIN_LSV2_HOST</c> (required), <c>HEIDENHAIN_LSV2_PORT</c> (default 19000),
/// <c>OTOPCUA_HEIDENHAIN_INSPECT_PASSWORD</c> (optional; never logged).
/// </remarks>
[Trait("Category", "LiveIntegration")]
public sealed class HeidenhainLiveIntegrationTests
{
private static string? Host => Environment.GetEnvironmentVariable("HEIDENHAIN_LSV2_HOST");
private static int Port => int.TryParse(Environment.GetEnvironmentVariable("HEIDENHAIN_LSV2_PORT"), out var p) ? p : 19000;
[Fact]
public async Task Real_control_inspect_identity_layout_and_status()
{
Assert.SkipWhen(string.IsNullOrWhiteSpace(Host), "HEIDENHAIN_LSV2_HOST not set — real-controller gate skipped.");
var ct = TestContext.Current.CancellationToken;
await using var client = new Lsv2Client(Host!, Port, TimeSpan.FromSeconds(5));
await client.OpenAsync(ct);
await client.LoginAsync("INSPECT", Environment.GetEnvironmentVariable("OTOPCUA_HEIDENHAIN_INSPECT_PASSWORD") ?? "", ct);
var versions = Lsv2Data.Versions(await client.ReadVersionAsync(ct));
TestContext.Current.SendDiagnosticMessage($"model={versions.Model} nc={versions.Nc} plc={versions.Plc}");
versions.Model.ShouldNotBeNullOrWhiteSpace();
foreach (var selector in new[] { 23, 26, 24, 25, 22, 51 })
{
try
{
var bytes = await client.ReadRuntimeAsync(selector, ct);
TestContext.Current.SendDiagnosticMessage($"R_RI {selector}: {bytes.Length} bytes");
}
catch (Lsv2RefusalException ex)
{
TestContext.Current.SendDiagnosticMessage($"R_RI {selector}: refused code {ex.Code} -> {HeidenhainStatus.State(HeidenhainStatus.FromRefusal(ex.Code))}");
}
}
try
{
var layout = Lsv2Data.Layout(await client.ReadParametersAsync(ct));
TestContext.Current.SendDiagnosticMessage($"R_PR WORD region base={layout.Region(HeidenhainPlcArea.Word)!.Base} bytes={layout.Region(HeidenhainPlcArea.Word)!.Bytes}");
}
catch (Lsv2RefusalException ex)
{
TestContext.Current.SendDiagnosticMessage($"R_PR refused code {ex.Code}");
}
client.IsClosed.ShouldBeFalse();
}
[Fact]
public async Task Real_control_driver_end_to_end_status_poll()
{
Assert.SkipWhen(string.IsNullOrWhiteSpace(Host), "HEIDENHAIN_LSV2_HOST not set — real-controller gate skipped.");
var ct = TestContext.Current.CancellationToken;
var tags = new[] { TestTags.Raw("Controller/Model"), TestTags.Raw("Status/ExecutionState"), TestTags.Raw("Capabilities/Execution") };
var options = new HeidenhainDriverOptions
{
Devices = [new HeidenhainDeviceOptions { Host = Host!, Port = Port, DeviceName = "dev1" }],
RawTags = tags,
Access = new HeidenhainAccessOptions { InspectPassword = Environment.GetEnvironmentVariable("OTOPCUA_HEIDENHAIN_INSPECT_PASSWORD") is { Length: > 0 } ? "env:OTOPCUA_HEIDENHAIN_INSPECT_PASSWORD" : null },
};
await using var driver = new HeidenhainDriver(options, "live");
await driver.InitializeAsync("{}", ct);
await driver.SubscribeAsync(tags.Select(t => t.RawPath).ToList(), TimeSpan.FromSeconds(1), ct);
var deadline = DateTime.UtcNow.AddSeconds(30);
IReadOnlyList<Core.Abstractions.DataValueSnapshot> values;
do
{
await Task.Delay(500, ct);
values = await driver.ReadAsync(tags.Select(t => t.RawPath).ToList(), ct);
} while (values[0].StatusCode != HeidenhainStatus.Good && DateTime.UtcNow < deadline);
for (var i = 0; i < tags.Length; i++)
TestContext.Current.SendDiagnosticMessage($"{tags[i].RawPath} = {values[i].Value} (0x{values[i].StatusCode:X8})");
values[0].StatusCode.ShouldBe(HeidenhainStatus.Good);
}
}