Path: redb-route/tests/redb.Route.Tests.As2/As2MicInteropTests.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.As2/As2MicInteropTests.cs
----
using System.Text;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.As2;
using redb.Route.Core;
using As2Dsl = redb.Route.As2.Fluent.As2;
namespace redb.Route.Tests.As2;
///
/// The Received-Content-MIC of the unsigned and the sign-then-compress profiles against OpenAS2 (profile as2 of
/// docker-compose.tests.yml, partnerships redb-enc, redb-zip, redb-zipenc and
/// redb-signzip). Both directions: OpenAS2 confirms the MIC redb sent (our producer sees matched), and
/// OpenAS2 closes the message it sent once the MIC in our MDN is its own (a mismatch makes it resend). OpenAS2 4.9.0
/// hashes an encrypted unsigned message over the (uncompressed) part with its MIME headers and a compressed unencrypted
/// one over the content alone.
///
[Trait("Category", "Interop")]
public class As2MicInteropTests
{
[As2InteropTheory]
[InlineData("redb-enc", true, false)]
[InlineData("redb-zip", false, true)]
[InlineData("redb-zipenc", true, true)]
public async Task Redb_To_OpenAs2_UnsignedProfile_TheMicIsConfirmed(string ourId, bool encrypt, bool compress)
{
var ourCert = As2InteropTests.LoadPkcs12(Path.Combine(As2InteropTests.CertsDir, "redb.p12"), "testpass");
var partnerCert = As2InteropTests.LoadCertificate(Path.Combine(As2InteropTests.CertsDir, "openas2.crt"));
await using var context = new RouteContext();
context.AddComponent(new As2Component());
context.AddToRegistry("openas2", new As2ConnectionFactory
{
OurCertificate = ourCert, PartnerCertificate = partnerCert,
As2From = ourId, As2To = "openas2",
Sign = false, Encrypt = encrypt, Compress = compress, EncryptAlg = "aes-128-cbc",
SignedMdn = true, RequireValidMdn = true, MdnMode = As2MdnMode.Sync,
});
await context.Start();
var producer = context.GetEndpoint(As2Dsl.Send("http://127.0.0.1:14080/").ConnectionFactory("openas2")).CreateProducer();
await producer.Start();
var exchange = new Exchange(new Message($"ISA*00*REDB*MIC*{ourId}*{Guid.NewGuid():N}~") { ContentType = "application/edi-x12" });
await producer.Process(exchange);
exchange.Out!.GetHeader(As2Headers.MdnMicStatus).Should().Be("matched", "OpenAS2 computed the MIC redb sent");
exchange.Out!.GetHeader(As2Headers.MdnConfirmed).Should().BeTrue();
}
[As2InteropTheory]
[InlineData("redb-enc", 15083, false, true, false)]
[InlineData("redb-zip", 15084, false, false, true)]
[InlineData("redb-zipenc", 15085, false, true, true)]
[InlineData("redb-signzip", 15086, true, true, true)]
public async Task OpenAs2_To_Redb_Profile_IsReceived_AndOpenAs2MatchesOurMic(string ourId, int port, bool sign, bool encrypt, bool compress)
{
var ourCert = As2InteropTests.LoadPkcs12(Path.Combine(As2InteropTests.CertsDir, "redb.p12"), "testpass");
var partnerCert = As2InteropTests.LoadCertificate(Path.Combine(As2InteropTests.CertsDir, "openas2.crt"));
await using var context = new RouteContext();
context.AddComponent(new As2Component());
context.AddToRegistry(ourId, new As2ConnectionFactory
{
OurCertificate = ourCert, PartnerCertificate = partnerCert,
As2From = ourId, As2To = "openas2",
Sign = sign, Encrypt = encrypt, Compress = compress, SignedMdn = true, MdnMode = As2MdnMode.Sync,
});
var received = new List<(string Body, bool SignatureValid)>();
context.AddRoutes(r => r.From(As2Dsl.Receive("/inbound").Host("0.0.0.0").Port(port).ConnectionFactory(ourId))
.Process(e =>
{
lock (received) received.Add((Encoding.UTF8.GetString((byte[])e.In.Body!), e.In.GetHeader(As2Headers.SignatureValid)));
}));
await context.Start();
// Dropped into OpenAS2's outbox for this partner (tests/docker/gen-certs.sh creates it writable); the poller
// sends it with the partnership's profile and keeps the message pending until our MDN is accepted.
var data = Path.Combine(Path.GetDirectoryName(As2InteropTests.CertsDir.TrimEnd('\\', '/'))!, "data");
var name = $"mic-{Guid.NewGuid():N}";
var payload = $"ISA*00*OPENAS2*MIC*{ourId}*{name}~";
await File.WriteAllTextAsync(Path.Combine(data, "outbox", ourId, name + ".edi"), payload);
var deadline = DateTime.UtcNow.AddSeconds(45);
while (Arrived() is null && DateTime.UtcNow < deadline)
await Task.Delay(250);
var arrived = Arrived();
arrived.Should().NotBeNull("OpenAS2 delivers the dropped file to our receiver");
if (sign) arrived!.Value.SignatureValid.Should().BeTrue("the signature over the part that was then compressed verifies");
// A MIC in our MDN other than OpenAS2's own makes it keep the message for a resend: it is closed only when they
// are the same.
while (Mentioned("pendinginfoMDN3") && DateTime.UtcNow < deadline)
await Task.Delay(250);
Mentioned("pendinginfoMDN3").Should().BeFalse("OpenAS2 accepted our MDN and closed the message");
await Task.Delay(TimeSpan.FromSeconds(2));
Mentioned("resend").Should().BeFalse("a MIC mismatch would have queued the message for a resend");
lock (received) received.Count(m => m.Body == payload).Should().Be(1);
(string Body, bool SignatureValid)? Arrived()
{
lock (received) return received.Where(m => m.Body == payload).Select(m => ((string, bool)?)m).FirstOrDefault();
}
bool Mentioned(string folder)
{
var dir = Path.Combine(data, folder);
if (!Directory.Exists(dir)) return false;
foreach (var file in Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories))
{
if (file.Contains(name, StringComparison.Ordinal)) return true;
try
{
if (Encoding.Latin1.GetString(File.ReadAllBytes(file)).Contains(name, StringComparison.Ordinal)) return true;
}
catch (IOException) { }
catch (UnauthorizedAccessException) { }
}
return false;
}
}
}