f79d13e2d8
Task 9. Adds MqttDriverFactoryExtensions (DriverTypeName = DriverTypeNames.Mqtt, direct deserialization of MqttDriverOptions — no intermediate DTO, mirroring OpcUaClientDriverFactoryExtensions), the DriverTypeNames.Mqtt constant, and both host wiring sites: the factory in DriverFactoryBootstrap.Register and the probe in AddOtOpcUaDriverProbes (the admin-node path Program.cs calls in its hasAdmin block — a probe wired only on driver nodes makes Test-connect silently dead). Carried-forward items: - Converges every MQTT config-parsing seam onto ONE JsonSerializerOptions — MqttJson.Options, in .Contracts alongside MqttDriverOptions. It replaces the probe's internal JsonOpts and the browser's separate private copy; the factory, the probe, MqttDriver.ParseOptions and MqttDriverBrowser now all parse through it. .Contracts is the only assembly all four consumers reference, and the browser's reference to the runtime .Driver project is a documented layering exception scheduled for removal — anchoring the options there would resurrect the duplicate the day it goes away. - Replaces the "Mqtt" literals in MqttDriverProbe, MqttDriverBrowser and MqttDriver with the constant (string value unchanged). - Tightens MqttDriverProbeTests.ProbeAsync_EnumAsName: it asserted only Ok == false + non-empty message, which is also exactly what a JSON-parse failure produces — so it stayed green under the very regression it names. It now asserts the probe got past the parse and reached the network. Falsifiability: deleting JsonStringEnumConverter from MqttJson.Options reddens 9 tests across 4 suites, including the tightened probe test (message becomes "Config JSON is invalid: The JSON value could not be converted to MqttProtocolVersion") — which the pre-fix assertions would have passed. Also references the MQTT driver from Core.Abstractions.Tests so DriverTypeNamesGuardTests' reflective bin scan discovers the new factory and the constant/factory parity check stays honest. Claude-Session: https://claude.ai/code/session_01GASWkNEi68FSCtvr6rLoEW
73 lines
2.7 KiB
C#
73 lines
2.7 KiB
C#
using System.Text.Json;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Mqtt;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Mqtt.Tests;
|
|
|
|
public sealed class MqttDriverOptionsTests
|
|
{
|
|
/// <summary>
|
|
/// The production instance, not a look-alike copy. A local clone would keep passing after
|
|
/// <see cref="MqttJson.Options"/> lost its <c>JsonStringEnumConverter</c> — testing the copy
|
|
/// instead of the thing every seam actually parses through.
|
|
/// </summary>
|
|
private static readonly JsonSerializerOptions J = MqttJson.Options;
|
|
|
|
[Fact]
|
|
public void Deserialize_SparkplugConfig_ReadsModeAndSubObject()
|
|
{
|
|
const string json = """
|
|
{ "host":"10.100.0.35","port":8883,"useTls":true,"mode":"SparkplugB",
|
|
"sparkplug":{"groupId":"Plant1","hostId":"h1","requestRebirthOnGap":true} }
|
|
""";
|
|
var o = JsonSerializer.Deserialize<MqttDriverOptions>(json, J)!;
|
|
o.Mode.ShouldBe(MqttMode.SparkplugB);
|
|
o.UseTls.ShouldBeTrue();
|
|
o.Sparkplug!.GroupId.ShouldBe("Plant1");
|
|
o.Plain.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Serialize_WritesEnumsAsNames_NotOrdinals()
|
|
{
|
|
var s = JsonSerializer.Serialize(new MqttDriverOptions { Mode = MqttMode.Plain }, J);
|
|
s.ShouldContain("\"Plain\"");
|
|
s.ShouldNotContain("\"mode\":0");
|
|
}
|
|
|
|
// Pins the plan's cross-cutting rule ("never ship anonymous/public-broker defaults") as an
|
|
// executable assertion — nothing else in this suite fails if UseTls / AllowUntrustedServerCertificate
|
|
// (or the §5.1 numeric defaults) get flipped by an unrelated edit.
|
|
[Fact]
|
|
public void Defaults_SecurityCriticalKnobs_MatchPlan()
|
|
{
|
|
var o = new MqttDriverOptions();
|
|
o.UseTls.ShouldBeTrue();
|
|
o.AllowUntrustedServerCertificate.ShouldBeFalse();
|
|
o.ConnectTimeoutSeconds.ShouldBe(15);
|
|
o.ReconnectMinBackoffSeconds.ShouldBe(1);
|
|
o.ReconnectMaxBackoffSeconds.ShouldBe(30);
|
|
}
|
|
|
|
// The production case: an operator-authored config blob that simply doesn't mention TLS must
|
|
// never silently deserialize into an insecure instance.
|
|
[Fact]
|
|
public void Deserialize_PartialJsonOmittingTlsKnobs_KeepsSecureDefaults()
|
|
{
|
|
const string json = """{ "host":"10.100.0.35","port":8883 }""";
|
|
var o = JsonSerializer.Deserialize<MqttDriverOptions>(json, J)!;
|
|
o.UseTls.ShouldBeTrue();
|
|
o.AllowUntrustedServerCertificate.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void ToString_DoesNotLeakPassword()
|
|
{
|
|
var o = new MqttDriverOptions { Password = "hunter2-supersecret" };
|
|
var s = o.ToString();
|
|
s.ShouldNotContain("hunter2-supersecret");
|
|
s.ShouldContain("***");
|
|
}
|
|
}
|