Merge fix/816-opcuaclient-cert-host

This commit is contained in:
Joseph Doherty
2026-11-11 01:53:35 -05:00
35 changed files with 832 additions and 63 deletions
@@ -38,6 +38,22 @@ public class CommandBaseTests
settings.AutoAcceptCertificates.ShouldBeTrue();
}
/// <summary>Gitea #816: --validate-host-name maps to the server host-name check, which is off without it.</summary>
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ValidateHostNameOption_MapsToConnectionSettings(bool flag)
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ConnectCommand(factory) { Url = "opc.tcp://localhost:4840", ValidateHostName = flag };
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.LastConnectionSettings!.ValidateServerHostName.ShouldBe(flag);
}
/// <summary>Verifies that encrypt option maps to SignAndEncrypt.</summary>
[Fact]
public async Task SecurityOption_Encrypt_MapsToSignAndEncrypt()
@@ -42,8 +42,10 @@ public sealed class EndpointSelectorRewriteParityTests
[InlineData("opc.tcp://plc-a:4840/Server", "opc.tcp://PLC-A:4840/", "opc.tcp://plc-a:4840/Server")]
// A real rewrite keeps the advertised path's percent-escaping intact.
[InlineData("opc.tcp://a:4840/x%20y?q=1", "opc.tcp://b:1/", "opc.tcp://b:1/x%20y?q=1")]
// An IPv6 zone index on the configured host is dropped (UriBuilder behaviour, same in both copies).
[InlineData("opc.tcp://internal-host:4840/OtOpcUa", "opc.tcp://[fe80::1%25eth0]:4841", "opc.tcp://[fe80::1]:4841/OtOpcUa")]
// #819: an IPv6 zone index on the configured host is KEPT, verbatim (it used to be dropped — UriBuilder behaviour,
// preserved deliberately through #812 — leaving link-local endpoints unroutable).
[InlineData("opc.tcp://internal-host:4840/OtOpcUa", "opc.tcp://[fe80::1%25eth0]:4841", "opc.tcp://[fe80::1%25eth0]:4841/OtOpcUa")]
[InlineData("opc.tcp://internal-host:4840/OtOpcUa", "opc.tcp://[fe80::1%eth0]:4841", "opc.tcp://[fe80::1%eth0]:4841/OtOpcUa")]
public void SelectBest_rewrites_exactly_as_before(string advertised, string configured, string expected)
{
Select(advertised, configured).ShouldBe(expected);
@@ -14,6 +14,8 @@ internal sealed class FakeSessionFactory : ISessionFactory
public bool ThrowOnCreate { get; set; }
/// <summary>Gets the endpoint URL from the last CreateSessionAsync call.</summary>
public string? LastEndpointUrl { get; private set; }
/// <summary>Gets the host-name-validation flag from the last CreateSessionAsync call (#816).</summary>
public bool? LastValidateServerHostName { get; private set; }
/// <summary>
/// Optional gate that, when set, blocks <see cref="CreateSessionAsync" /> until completed.
@@ -33,14 +35,16 @@ internal sealed class FakeSessionFactory : ISessionFactory
/// <param name="sessionName">The session name.</param>
/// <param name="sessionTimeoutMs">The session timeout in milliseconds.</param>
/// <param name="identity">The user identity.</param>
/// <param name="validateServerHostName">The SDK checkDomain flag (#816).</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A task containing the session adapter.</returns>
public async Task<ISessionAdapter> CreateSessionAsync(
ApplicationConfiguration config, EndpointDescription endpoint, string sessionName,
uint sessionTimeoutMs, UserIdentity identity, CancellationToken ct)
uint sessionTimeoutMs, UserIdentity identity, bool validateServerHostName, CancellationToken ct)
{
CreateCallCount++;
LastEndpointUrl = endpoint.EndpointUrl ?? string.Empty;
LastValidateServerHostName = validateServerHostName;
// One-shot: the gate holds only the call that observed it, so a later call (e.g. a user's Connect while a
// failover is held) runs freely.
@@ -55,6 +55,32 @@ public class OpcUaClientServiceTests : IDisposable
_service.CurrentConnectionInfo.ShouldBe(info);
}
/// <summary>
/// Gitea #816: the host-name check reaches the session factory (the SDK's checkDomain) on a SECURED endpoint — off
/// by default, the pre-#816 behaviour — and survives the per-endpoint settings copy the connect path makes. On a
/// None endpoint it is never passed: SDK 2.0.1 would check the GetEndpoints certificate at channel creation even
/// there (review of #816, reproduced against opc-plc).
/// </summary>
[Theory]
[InlineData(SecurityMode.Sign, null, false)]
[InlineData(SecurityMode.Sign, true, true)]
[InlineData(SecurityMode.SignAndEncrypt, true, true)]
[InlineData(SecurityMode.Sign, false, false)]
[InlineData(SecurityMode.None, true, false)]
[InlineData(SecurityMode.None, null, false)]
public async Task ConnectAsync_threads_ValidateServerHostName_to_secured_sessions_only(
SecurityMode mode, bool? validate, bool expectedCheckDomain)
{
var settings = ValidSettings();
settings.SecurityMode = mode;
if (validate is { } v) settings.ValidateServerHostName = v;
await _service.ConnectAsync(settings, TestContext.Current.CancellationToken);
_sessionFactory.LastValidateServerHostName.ShouldBe(expectedCheckDomain);
_configFactory.LastSettings!.ValidateServerHostName.ShouldBe(validate ?? false);
}
/// <summary>
/// Verifies that invalid connection settings fail validation before any OPC UA session is created.
/// </summary>