Path: redb-route/tests/redb.Route.Tests.As4/As4CanonicalizationTests.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/As4CanonicalizationTests.cs ---- using System.Security.Cryptography; using System.Text; using redb.Route.As4.Mime; using redb.Route.As4.Security; namespace redb.Route.Tests.As4; /// /// The digest input of the Attachment-Content-Signature-Transform (SwA 1.1 ยง5.4.2, the rules of WSS4J): /// Exclusive C14N for XML media types, CRLF line endings for other text, the bytes as they are for the rest. /// Interop with Holodeck over these rules is As4HolodeckLiveTests.UncompressedTextPayload_*. /// public class As4CanonicalizationTests { private static byte[] Digest(string contentType, string content) => AttachmentCanonicalizer.Sha256(new SwaPart("p", contentType, Encoding.UTF8.GetBytes(content))); private static byte[] Sha(string canonical) => SHA256.HashData(Encoding.UTF8.GetBytes(canonical)); [Theory] [InlineData("text/xml")] [InlineData("text/xml; charset=UTF-8")] [InlineData("application/xml")] [InlineData("application/soap+xml")] [InlineData("image/svg+xml")] public void XmlMediaTypes_AreExclusiveC14N(string contentType) { Digest(contentType, "") .Should().Equal(Sha("")); } [Theory] [InlineData("a\nb", "a\r\nb")] [InlineData("a\rb", "a\r\nb")] [InlineData("a\r\nb", "a\r\nb")] [InlineData("a\n\nb\r\r", "a\r\n\r\nb\r\n\r\n")] public void OtherText_HasCrlfLineEndings(string content, string canonical) { Digest("text/plain", content).Should().Equal(Sha(canonical)); } [Fact] public void CrlfSplitAcrossReads_IsOneLineEnding() { // The reader works in 80 KB blocks: a CR that ends one block and the LF that starts the next are one CRLF. var head = new string('x', 81919); Digest("text/csv", head + "\r\n" + "tail").Should().Equal(Sha(head + "\r\n" + "tail")); } [Theory] [InlineData("application/octet-stream")] [InlineData("application/gzip")] [InlineData("application/pdf")] public void OtherMediaTypes_AreTheBytesAsTheyAre(string contentType) { Digest(contentType, "\n").Should().Equal(Sha("\n")); } [Fact] public void XmlAttachmentWithADoctype_IsRefused_AsAFailedSignature() { var act = () => Digest("text/xml", "]>&e;"); act.Should().Throw().WithMessage("*cannot be read as XML*"); } [Fact] public void XmlAttachmentOverTheDefaultLimits_IsRefusedBeforeCanonicalization() { // The payload is the partner's document, read with the generous default limits: 100 000 levels stop at 257. var deep = string.Concat(Enumerable.Repeat("", 100_000)) + string.Concat(Enumerable.Repeat("", 100_000)); var act = () => Digest("application/xml", deep); act.Should().Throw().WithMessage("*MaxDepth*") .WithInnerException(); } [Fact] public void XmlAttachmentWithinTheDefaultLimits_IsCanonicalizedAsBefore() { // A deep but ordinary payload (60 levels, more than any envelope) keeps its digest path. Deeper than about 65 // levels the patched System.Security.Cryptography.Xml (10.0.13) refuses to canonicalize on its own. var deep = string.Concat(Enumerable.Repeat("", 60)) + string.Concat(Enumerable.Repeat("", 60)); Digest("application/xml", deep).Should().HaveCount(32); } }