Path: redb-route/tests/redb.Route.Tests.As4/As4Stand.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.As4/As4Stand.cs ---- using System.Security.Cryptography.X509Certificates; using System.Text.Json; using Xunit.Sdk; namespace redb.Route.Tests.As4; /// /// The AS4 interop harness: test keys, captured reference messages and the message folders shared with the Holodeck /// nodes of tests/stand/docker-compose.private.yml. No key material lives in the repository: the files are /// copied by hand into the git-ignored tests/stand/.generated/as4. /// internal static class As4Stand { /// The command that starts the Holodeck nodes and the recorder, for skip messages. public const string UpCommand = "docker compose -f redb.Route/docker-compose.tests.yml -f tests/stand/docker-compose.private.yml --profile as4 up -d --wait"; /// /// Root of the harness: AS4_STAND when set, otherwise tests/stand/.generated/as4 of the first folder /// above the test binaries that has tests/stand. /// public static string Root { get; } = Environment.GetEnvironmentVariable("AS4_STAND") ?? FindRoot(); private static string FindRoot() { for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) { if (Directory.Exists(Path.Combine(dir.FullName, "tests", "stand"))) return Path.Combine(dir.FullName, "tests", "stand", ".generated", "as4"); } // No tests/stand above the binaries: name the place it was looked for, so the skip message points at it. return Path.Combine(AppContext.BaseDirectory, "tests", "stand", ".generated", "as4"); } /// True when the run must not skip harness tests (REDB_AS4_INTEROP=1). public static bool Required => Environment.GetEnvironmentVariable("REDB_AS4_INTEROP") == "1"; /// A captured exchange: body bytes and the HTTP Content-Type it came with. public static (byte[] Body, string ContentType) Capture(string name, string side) { var dir = Path.Combine(Root, "captures", name); var body = File.ReadAllBytes(Path.Combine(dir, side + ".bin")); using var meta = JsonDocument.Parse(File.ReadAllText(Path.Combine(dir, side + ".json"))); var contentType = meta.RootElement.GetProperty("headers").GetProperty("content-type").GetString()!; return (body, contentType); } /// A file of a capture folder. public static byte[] CaptureFile(string name, string file) => File.ReadAllBytes(Path.Combine(Root, "captures", name, file)); /// A test key pair of the harness (keys/<name>.p12, password testpass). public static X509Certificate2 KeyPair(string name) { var path = Path.Combine(Root, "keys", name + ".p12"); return X509CertificateLoader.LoadPkcs12FromFile(path, "testpass", X509KeyStorageFlags.Exportable); } /// /// A lock shared by every test process on this machine — the three target frameworks run in parallel, and a /// test that Holodeck calls back on a fixed port can run in one of them at a time. A lock file rather than a /// named mutex: it is not bound to the acquiring thread, which an async test does not keep. /// public static async Task ExclusiveAsync(string name, TimeSpan timeout) { var path = Path.Combine(Path.GetTempPath(), $"redb-as4-{name}.lock"); var deadline = DateTime.UtcNow + timeout; while (true) { try { return new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1, FileOptions.DeleteOnClose); } catch (IOException) when (DateTime.UtcNow < deadline) { await Task.Delay(TimeSpan.FromMilliseconds(250)); // held by another test process } } } /// /// Reads a file the stand may still be writing: shared with the writer, retried while the writer holds it exclusively /// (Holodeck writes its msg_in files in place). /// public static string ReadShared(string path) { for (var attempt = 1; ; attempt++) { try { using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); using var reader = new StreamReader(stream); return reader.ReadToEnd(); } catch (IOException) when (attempt < 20) { Thread.Sleep(100); } } } /// Whether something accepts TCP connections on 127.0.0.1:. public static bool Listening(int port) { using var client = new System.Net.Sockets.TcpClient(); try { return client.ConnectAsync("127.0.0.1", port).Wait(TimeSpan.FromSeconds(1)) && client.Connected; } catch (AggregateException e) when (e.InnerException is System.Net.Sockets.SocketException) { return false; // refused: that is the answer "nothing listens" } } /// A test certificate of the harness without its key (keys/<name>.crt). public static X509Certificate2 Certificate(string name) { var path = Path.Combine(Root, "keys", name + ".crt"); return X509CertificateLoader.LoadCertificateFromFile(path); } } /// /// A fact that needs the AS4 harness files. Without them it is skipped — visibly, not passed — unless /// REDB_AS4_INTEROP=1, which makes a missing harness a failure. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] [XunitTestCaseDiscoverer("Xunit.Sdk.FactDiscoverer", "xunit.execution.{Platform}")] public sealed class As4StandFactAttribute : FactAttribute { /// Skips when is not present under the harness root. public As4StandFactAttribute(string relativePath) { if (!As4Stand.Required && !Path.Exists(Path.Combine(As4Stand.Root, relativePath))) Skip = $"AS4 harness not found ({Path.Combine(As4Stand.Root, relativePath)}); copy the stand files there (tests/stand/docker-compose.private.yml) or set AS4_STAND; REDB_AS4_INTEROP=1 requires it."; } } /// /// A fact that needs a live harness endpoint on 127.0.0.1:port (a Holodeck node or the recorder). /// Skipped when it does not answer — visibly — unless REDB_AS4_INTEROP=1 makes that a failure. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] [XunitTestCaseDiscoverer("Xunit.Sdk.FactDiscoverer", "xunit.execution.{Platform}")] public sealed class As4LiveFactAttribute : FactAttribute { /// Skips when nothing listens on . public As4LiveFactAttribute(int port) { if (!As4Stand.Required && !As4Stand.Listening(port)) Skip = $"AS4 harness endpoint 127.0.0.1:{port} is not up ({As4Stand.UpCommand}); REDB_AS4_INTEROP=1 requires it."; } } /// The theory form of . [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] [XunitTestCaseDiscoverer("Xunit.Sdk.TheoryDiscoverer", "xunit.execution.{Platform}")] public sealed class As4LiveTheoryAttribute : TheoryAttribute { /// Skips when nothing listens on . public As4LiveTheoryAttribute(int port) { if (!As4Stand.Required && !As4Stand.Listening(port)) Skip = $"AS4 harness endpoint 127.0.0.1:{port} is not up ({As4Stand.UpCommand}); REDB_AS4_INTEROP=1 requires it."; } }