Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Uns/HeidenhainAuthoringTests.cs
T
Joseph Doherty 54f58c583c fix(heidenhain): refuse literal credentials and writable/mistyped tags at the save path (#803 review)
- Review 1: RawTreeService Create/UpdateDriver and Create/UpdateDevice refuse a Heidenhain
  config whose password fields are not blank / secret:heidenhain/... / env:OTOPCUA_HEIDENHAIN_...
  (DriverConfigSaveGate over HeidenhainOptionsParser.DescribeCredentialProblems: top level,
  Access, Devices[], case-insensitive keys; the value is never echoed), so a literal password
  never reaches the draft DB. The driver and device modals show the reason and disable Save.
- Review 8: CreateTag/UpdateTag/ImportTags (tag modal, manual entry, CSV import, browse commit)
  refuse a ReadWrite Heidenhain tag or a Tag.DataType that disagrees with what the driver
  publishes (HeidenhainTagRules: catalog type, Float64 => Double, or the binding's dataType).
  The tag modal and manual-entry grid fix the access level at Read and derive the data type from
  the address (RawTagRowDefaults). New deploy gate HeidenhainTagDataTypeMismatch.
- docs/drivers/Heidenhain.md: credential namespace convention + why, the save-path and deploy
  enforcement, optional-role credential behaviour, BadConfigurationError, backoff/ordering notes.

Tests: HeidenhainSavePathTests (service save path; red with the gates disabled) and new
DraftValidatorHeidenhainTests cases.
2026-11-10 23:54:32 -05:00

123 lines
5.5 KiB
C#

using System.Text.Json.Nodes;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.DeviceForms;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Forms;
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns.TagEditors;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.Heidenhain;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
/// <summary>
/// lmxopcua#803: the Heidenhain authoring surfaces (driver form, device form, tag editor, CSV, browse commit)
/// write exactly what the driver factory and the deploy gate accept — the "registered but unauthorable" and
/// "numerically-serialized enum" defect classes.
/// </summary>
[Trait("Category", "Unit")]
public sealed class HeidenhainAuthoringTests
{
[Fact]
public void Every_authoring_seam_is_registered()
{
DriverConfigFormMap.Resolve(DriverTypeNames.Heidenhain).ShouldBe(typeof(HeidenhainDriverForm));
DeviceFormMap.Resolve(DriverTypeNames.Heidenhain).ShouldBe(typeof(HeidenhainDeviceForm));
TagConfigEditorMap.Resolve(DriverTypeNames.Heidenhain).ShouldBe(typeof(Components.Shared.Uns.TagEditors.HeidenhainTagConfigEditor));
TagConfigValidator.Validate(DriverTypeNames.Heidenhain, "{}").ShouldNotBeNull();
CsvColumnMap.Resolve(DriverTypeNames.Heidenhain).ShouldNotBeNull();
}
[Fact]
public void A_default_driver_form_save_is_accepted_by_the_factory_and_preserves_devices()
{
var authored = """{"Devices":[{"Host":"10.0.0.5","DeviceName":"m1"}],"Custom":42}""";
var m = HeidenhainDriverFormModel.FromJson(authored);
m.Validate().ShouldBeNull();
m.InspectPassword = "secret:heidenhain/inspect";
m.TimersEnabled = true;
var json = m.ToJson();
var o = HeidenhainOptionsParser.Parse(json);
HeidenhainOptionsParser.Validate(o).ShouldBeEmpty(); // the factory's ParseOptions gate
o.Devices.Single().Host.ShouldBe("10.0.0.5");
o.Access.InspectPassword.ShouldBe("secret:heidenhain/inspect");
o.Polling.TimersEnabled.ShouldBeTrue();
JsonNode.Parse(json)!["Custom"]!.GetValue<int>().ShouldBe(42);
// no field is ever written as a number where the parser expects a name — there are no enums at all
HeidenhainDriverFormModel.FromJson(json).ToJson().ShouldBe(json);
}
[Fact]
public void Driver_form_refuses_literal_passwords_and_out_of_range_values()
{
var m = HeidenhainDriverFormModel.FromJson("{}");
m.DncPassword = "hunter2";
m.Validate()!.ShouldNotContain("hunter2");
m.DncPassword = null;
m.StatusMs = 10;
m.Validate().ShouldNotBeNull();
}
[Fact]
public void Device_form_round_trips_and_validates_with_the_driver_rule()
{
var d = HeidenhainDeviceModel.FromJson("""{"Host":"cnc-7","Note":"keep"}""");
d.Port.ShouldBe(19000);
d.Validate().ShouldBeNull();
d.DataPassword = "plain";
d.Validate().ShouldNotBeNull();
d.DataPassword = "env:OTOPCUA_HEIDENHAIN_DATA";
var json = d.ToJson();
JsonNode.Parse(json)!["Note"]!.GetValue<string>().ShouldBe("keep");
var opts = HeidenhainOptionsParser.Parse($$"""{"Devices":[{{json}}]}""");
opts.Devices.Single().DataPassword.ShouldBe("env:OTOPCUA_HEIDENHAIN_DATA");
HeidenhainDeviceModel.FromJson("{}").Validate().ShouldNotBeNull(); // host required
}
[Theory]
[InlineData("Axes/Positions")]
[InlineData("Alarms/Messages")]
[InlineData("Status/ExecutionState")]
[InlineData("Capabilities/DncLogin")]
public void Catalog_addresses_derive_type_and_rank_so_the_driver_accepts_them(string address)
{
var json = new HeidenhainTagConfigModel { Address = address }.ToJson();
HeidenhainTagDefinitionFactory.TryFromTagConfig("p", "d", json, out var def, out var error).ShouldBeTrue(error);
HeidenhainCatalog.TryGet(address, out var e).ShouldBeTrue();
def!.DataType.ShouldBe(e!.DataType);
def.IsArray.ShouldBe(e.IsArray);
TagConfigValidator.Validate(DriverTypeNames.Heidenhain, json).ShouldBeNull();
}
[Fact]
public void Editor_writes_the_data_type_as_a_name_and_drops_a_stray_writable_flag()
{
var m = HeidenhainTagConfigModel.FromJson("""{"address":"PLC:WORD:2","dataType":"Int64","writable":true,"isHistorized":true}""");
m.Validate().ShouldNotBeNull(); // as loaded: the driver refuses writable:true
var json = m.ToJson();
var o = JsonNode.Parse(json)!.AsObject();
o["dataType"]!.GetValue<string>().ShouldBe("Int64");
o.ContainsKey("writable").ShouldBeFalse();
o["isHistorized"]!.GetValue<bool>().ShouldBeTrue();
HeidenhainTagConfigModel.FromJson(json).Validate().ShouldBeNull();
}
[Fact]
public void Editor_refuses_a_narrowing_binding()
{
var m = new HeidenhainTagConfigModel { Address = "PLC:DWORD:0", DataType = DriverDataType.Int16 };
m.Validate().ShouldNotBeNull();
}
[Theory]
[InlineData("Status/ExecutionState")]
[InlineData("Axes/Names")]
public void Browse_committed_leaves_open_in_the_editor_and_pass_the_driver_gate(string address)
{
var json = RawBrowseCommitMapper.BuildTagConfig(DriverTypeNames.Heidenhain, address);
TagConfigValidator.Validate(DriverTypeNames.Heidenhain, json).ShouldBeNull();
HeidenhainTagConfigModel.FromJson(json).Address.ShouldBe(address);
}
}