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
This commit is contained in:
@@ -22,18 +22,20 @@ public sealed class OpcUaApplicationHostTests : IDisposable
|
||||
[Fact]
|
||||
public async Task StartAsync_creates_application_certificate_in_pki_own()
|
||||
{
|
||||
await using var host = new OpcUaApplicationHost(
|
||||
new OpcUaApplicationHostOptions
|
||||
{
|
||||
ApplicationName = "OtOpcUa.Test",
|
||||
ApplicationUri = $"urn:OtOpcUa.Test:{Guid.NewGuid():N}",
|
||||
OpcUaPort = AllocateFreePort(),
|
||||
PublicHostname = "localhost",
|
||||
PkiStoreRoot = _pkiRoot,
|
||||
},
|
||||
NullLogger<OpcUaApplicationHost>.Instance);
|
||||
|
||||
await host.StartAsync(new StandardServer(), Ct);
|
||||
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}");
|
||||
@@ -44,53 +46,42 @@ public sealed class OpcUaApplicationHostTests : IDisposable
|
||||
[Fact]
|
||||
public async Task StartAsync_reuses_existing_certificate_on_second_boot()
|
||||
{
|
||||
var port1 = AllocateFreePort();
|
||||
await using (var first = new OpcUaApplicationHost(
|
||||
new OpcUaApplicationHostOptions
|
||||
{
|
||||
ApplicationName = "OtOpcUa.Reuse",
|
||||
ApplicationUri = "urn:OtOpcUa.Reuse",
|
||||
OpcUaPort = port1,
|
||||
PublicHostname = "localhost",
|
||||
PkiStoreRoot = _pkiRoot,
|
||||
},
|
||||
NullLogger<OpcUaApplicationHost>.Instance))
|
||||
{
|
||||
await first.StartAsync(new StandardServer(), Ct);
|
||||
}
|
||||
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 port2 = AllocateFreePort();
|
||||
await using (var second = new OpcUaApplicationHost(
|
||||
new OpcUaApplicationHostOptions
|
||||
{
|
||||
ApplicationName = "OtOpcUa.Reuse",
|
||||
ApplicationUri = "urn:OtOpcUa.Reuse",
|
||||
OpcUaPort = port2,
|
||||
PublicHostname = "localhost",
|
||||
PkiStoreRoot = _pkiRoot,
|
||||
},
|
||||
NullLogger<OpcUaApplicationHost>.Instance))
|
||||
{
|
||||
await second.StartAsync(new StandardServer(), Ct);
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
private static int AllocateFreePort()
|
||||
{
|
||||
using var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
var port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
|
||||
listener.Stop();
|
||||
return port;
|
||||
}
|
||||
|
||||
/// <summary>Cleans up the temporary PKI directory.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user