// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! That a message this library produces can be signed, and that one it has //! parsed and written out again still verifies. //! //! This is the test the byte-for-byte round trip exists for. Everywhere else //! the property is asserted against itself — the bytes that went in are the //! bytes that came out — which proves the two halves of *this* library agree //! and nothing more. DKIM is the thing that actually cares: a signature covers //! the exact octets of a set of headers and a body, and a message that comes //! back from a parser one space different is a message whose signature no //! longer verifies. So the property is checked here against something that //! would notice. //! //! It is not built by default. [zig-dkim][] is a lazy dependency behind //! `-Ddkim`, so a program that only wants to read mail does not fetch a crypto //! library to do it; the workflow turns it on, which is where it matters. //! //! ```console //! $ zig build test -Ddkim //! ``` //! //! [zig-dkim]: https://git.jcollie.dev/jeff/zig-dkim const std = @import("std"); const testing = std.testing; const mime = @import("mime"); const dkim = @import("dkim"); test "a message this library built, signed and verified" { var prng: std.Random.DefaultPrng = .init(0xd1c17); const random = prng.random(); var message: mime.Message = try .init(testing.allocator); defer message.deinit(); try message.root.setAddresses("From", &.{ .{ .display_name = "Jeff Ollie", .local_part = "jeff", .domain = "example.com" }, }); try message.root.setAddresses("To", &.{.{ .local_part = "alice", .domain = "example.net" }}); try message.root.setSubject("Grüße"); try message.root.setMessageId("example.com", random); try message.setText("Schöne Grüße.\n", .{}); _ = try message.attach(random, "a,b\n", .{ .filename = "data.csv" }); const raw = try message.toBytesAlloc(testing.allocator); defer testing.allocator.free(raw); const seed: [32]u8 = @splat(7); const pair = try std.crypto.sign.Ed25519.KeyPair.generateDeterministic(seed); var buffer: [dkim.signing.max_field_len]u8 = undefined; const field = try dkim.sign(&buffer, raw, .{ .domain = "example.com", .selector = "ed", .key = .{ .ed25519 = pair }, .timestamp = 1757000000, }); // Put it where a caller would: at the front of the message it signs. // `prependRaw` rather than `prepend`, because the field was folded by // whatever produced it and the signature covers it as it stands. const colon = std.mem.findScalar(u8, field, ':').?; try message.root.headers.prependRaw( field[0..colon], std.mem.trim(u8, field[colon + 1 ..], " \t\r\n"), ); const signed = try message.toBytesAlloc(testing.allocator); defer testing.allocator.free(signed); // Which is exactly the field followed by the message that was signed -- // the property the whole thing rests on. try testing.expectEqualStrings(field, signed[0..field.len]); try testing.expectEqualStrings(raw, signed[field.len..]); var public: [64]u8 = undefined; const encoded = std.base64.standard.Encoder.encode(&public, &pair.public_key.toBytes()); var txt: [256]u8 = undefined; const record_txt = try std.fmt.bufPrint(&txt, "v=DKIM1; k=ed25519; p={s}", .{encoded}); const record = try dkim.Record.parse(record_txt); var it = dkim.signatures(signed); const signature = try it.next().?; const verified = try dkim.verify(signed, signature, record, .{}); try testing.expectEqualStrings("example.com", verified.domain); // And now the part that is this library's to prove: parsing the signed // message and writing it back out gives bytes that still verify. var parsed: mime.Message = try .parse(testing.allocator, signed, .{}); defer parsed.deinit(); const again = try parsed.toBytesAlloc(testing.allocator); defer testing.allocator.free(again); try testing.expectEqualStrings(signed, again); var it2 = dkim.signatures(again); const signature2 = try it2.next().?; _ = try dkim.verify(again, signature2, record, .{}); }