Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/OpcUaApplicationHostTests.cs
T
Joseph Doherty f02067d295 test: retry OPC UA host start on a port bind race (TOCTOU in AllocateFreePort)
The tests probe a free port (bind 0, release) and start the SDK server later; under a
highly parallel run another test can take the port in between and the SDK fails with
'Failed to establish tcp listener sockets for Ipv4 and IPv6.'. OpcUaHostStart.StartAsync
builds a fresh host + server on a fresh port per attempt (up to 5) when the failure is a
bind failure; all 45 start sites in OpcUaServer.Tests, OpcUaServer.IntegrationTests and
Runtime.Tests use it. Per-attempt GUID ApplicationUris are hoisted out of the host factory
because a changed URI against a reused PKI store fails with 'Cannot access private key'.

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
2026-10-08 22:30:55 -04:00

95 lines
3.8 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
using Opc.Ua.Server;
using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
/// <summary>
/// Verifies the F13a cert auto-creation slice of <see cref="OpcUaApplicationHost"/>: the SDK
/// self-signs a certificate into <c>{PkiStoreRoot}/own/certs/</c> on first boot against a
/// fresh PKI tree, and the file persists for reuse on the next boot.
/// </summary>
public sealed class OpcUaApplicationHostTests : IDisposable
{
private static CancellationToken Ct => TestContext.Current.CancellationToken;
private readonly string _pkiRoot = Path.Combine(
Path.GetTempPath(),
$"otopcua-pki-{Guid.NewGuid():N}");
/// <summary>Verifies StartAsync creates a self-signed certificate in the PKI own store.</summary>
[Fact]
public async Task StartAsync_creates_application_certificate_in_pki_own()
{
var applicationUri = $"urn:OtOpcUa.Test:{Guid.NewGuid():N}";
var started = await OpcUaHostStart.StartAsync(
port => new OpcUaApplicationHost(
new OpcUaApplicationHostOptions
{
ApplicationName = "OtOpcUa.Test",
ApplicationUri = applicationUri,
OpcUaPort = port,
PublicHostname = "localhost",
PkiStoreRoot = _pkiRoot,
},
NullLogger<OpcUaApplicationHost>.Instance),
() => new StandardServer(), Ct);
await using var host = started.Host;
var ownCerts = Path.Combine(_pkiRoot, "own", "certs");
Directory.Exists(ownCerts).ShouldBeTrue($"expected SDK to create {ownCerts}");
Directory.EnumerateFiles(ownCerts).ShouldNotBeEmpty("expected a self-signed cert file in the own store");
}
/// <summary>Verifies StartAsync reuses an existing certificate on the second boot.</summary>
[Fact]
public async Task StartAsync_reuses_existing_certificate_on_second_boot()
{
var first = await OpcUaHostStart.StartAsync(
port => new OpcUaApplicationHost(
new OpcUaApplicationHostOptions
{
ApplicationName = "OtOpcUa.Reuse",
ApplicationUri = "urn:OtOpcUa.Reuse",
OpcUaPort = port,
PublicHostname = "localhost",
PkiStoreRoot = _pkiRoot,
},
NullLogger<OpcUaApplicationHost>.Instance),
() => new StandardServer(), Ct);
await first.Host.DisposeAsync();
var ownCerts = Path.Combine(_pkiRoot, "own", "certs");
var firstFiles = Directory.GetFiles(ownCerts).OrderBy(f => f).ToArray();
firstFiles.ShouldNotBeEmpty();
var second = await OpcUaHostStart.StartAsync(
port => new OpcUaApplicationHost(
new OpcUaApplicationHostOptions
{
ApplicationName = "OtOpcUa.Reuse",
ApplicationUri = "urn:OtOpcUa.Reuse",
OpcUaPort = port,
PublicHostname = "localhost",
PkiStoreRoot = _pkiRoot,
},
NullLogger<OpcUaApplicationHost>.Instance),
() => new StandardServer(), Ct);
await second.Host.DisposeAsync();
var secondFiles = Directory.GetFiles(ownCerts).OrderBy(f => f).ToArray();
secondFiles.ShouldBe(firstFiles, "expected the second boot to reuse the cert from the first, not create a new one");
}
/// <summary>Cleans up the temporary PKI directory.</summary>
public void Dispose()
{
if (Directory.Exists(_pkiRoot))
{
try { Directory.Delete(_pkiRoot, recursive: true); }
catch { /* best-effort cleanup */ }
}
}
}