// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What the messages in `corpus.zig` must decode to, and the properties that //! must hold of any message at all. //! //! The messages themselves are next door because the fuzzer wants them too. //! What is here is the reading of them: that the `https` record really does //! offer h3 before h2, that the thirteen root servers really are a. through //! m., that the signed denial really is a NODATA and not an NXDOMAIN. const std = @import("std"); const dns = @import("dns"); const corpus = @import("corpus"); const testing = std.testing; test "every fixture parses, and nothing is left over" { inline for (corpus.all) |fixture| { const bytes = fixture.bytes; const message = dns.Message.parse(bytes) catch |err| { std.debug.print("{s}: {s}\n", .{ fixture.what, @errorName(err) }); return err; }; try testing.expectEqual(@as(usize, 0), message.trailing().len); // And every record's data decodes. `unknown` is a decode and not a // failure, so this is about the framing rather than about coverage. inline for ([_]dns.Section{ .answer, .authority, .additional }) |which| { var it = message.section(which); while (try it.next()) |record| _ = try record.data(); } } } test "every fixture survives being built again" { // Not byte for byte: this library compresses where it is allowed to and // the servers that sent these made their own choices, which is the point. // What must survive is every record, field for field, which is what // comparing the presentation form checks. var original_buf: [8192]u8 = undefined; var rebuilt_buf: [8192]u8 = undefined; inline for (corpus.all) |fixture| { const bytes = fixture.bytes; const message = try dns.Message.parse(bytes); var out: [8192]u8 = undefined; var b = try dns.Builder.init(&out, message.header, .{}); var questions = message.questions(); while (try questions.next()) |q| try b.question(q); inline for ([_]dns.Section{ .answer, .authority, .additional }) |which| { var it = message.section(which); while (try it.next()) |record| try b.record(which, .{ .name = record.name, .class = record.class, .ttl = record.ttl, .data = try record.data(), }); } const rebuilt = try dns.Message.parse(b.finish()); try testing.expectEqualStrings( try std.fmt.bufPrint(&original_buf, "{f}", .{message}), try std.fmt.bufPrint(&rebuilt_buf, "{f}", .{rebuilt}), ); } } test "the answer for example.com A" { const bytes = corpus.a_answer.bytes; const message = try dns.Message.parse(bytes); try testing.expect(message.header.response); try testing.expect(message.header.recursion_available); try testing.expectEqual(dns.ResponseCode.noerror, try message.responseCode()); try testing.expectFmt("example.com.\tIN\tA", "{f}", .{(try message.question()).?}); var it = message.answers(); try testing.expectFmt( "example.com.\t21\tIN\tA\t172.66.147.243", "{f}", .{(try it.next()).?}, ); try testing.expectFmt( "example.com.\t21\tIN\tA\t104.20.23.154", "{f}", .{(try it.next()).?}, ); try testing.expectEqual(@as(?dns.Record, null), try it.next()); // The OPT record is the one thing in the additional section. const opt = (try message.opt()).?; try testing.expectEqual(@as(u16, 1232), opt.udp_payload_size); try testing.expect(!opt.dnssec_ok); } test "a name compressed against the question" { const bytes = corpus.a_answer.bytes; const message = try dns.Message.parse(bytes); var it = message.answers(); const answer = (try it.next()).?; // The owner of the answer is two bytes -- a pointer to offset 12, where // the question's name is -- and it still reads as the whole name. try testing.expectEqual(@as(u16, 2), answer.name.wireLen()); try testing.expectEqual(@as(u16, 13), answer.name.encodedLen()); try testing.expectFmt("example.com.", "{f}", .{answer.name}); } test "an MX pointing at the root means there is no mail here" { const bytes = corpus.mx_at_the_root.bytes; const message = try dns.Message.parse(bytes); var it = message.answers(); const mx = (try it.next()).?; const data = (try mx.data()).mx; try testing.expectEqual(@as(u16, 0), data.preference); try testing.expect(data.exchange.isRoot()); } test "an SOA in the authority section, with names compressed into it" { const bytes = corpus.tlsa_nodata.bytes; const message = try dns.Message.parse(bytes); try testing.expectEqual(@as(u16, 0), message.header.counts.answer); var it = message.authority(); const soa = (try (try it.next()).?.data()).soa; try testing.expectFmt("elliott.ns.cloudflare.com.", "{f}", .{soa.mname}); try testing.expectFmt("dns.cloudflare.com.", "{f}", .{soa.rname}); try testing.expectEqual(@as(u32, 2413856909), soa.serial); try testing.expectEqual(@as(u32, 1800), soa.minimum); } test "an HTTPS record" { const bytes = corpus.https_record.bytes; const message = try dns.Message.parse(bytes); var it = message.answers(); const record = (try it.next()).?; const https = (try record.data()).https; try testing.expectEqual(@as(u16, 1), https.priority); // The target is the root, meaning "this record's own owner name". try testing.expect(https.target.isRoot()); var alpn = https.params.get(.alpn).?.stringList(); try testing.expectEqualStrings("h3", alpn.next().?); try testing.expectEqualStrings("h2", alpn.next().?); try testing.expectEqual(@as(?[]const u8, null), alpn.next()); try testing.expectFmt( "cloudflare.com.\t300\tIN\tHTTPS\t1 . alpn=\"h3,h2\" " ++ "ipv4hint=104.16.132.229,104.16.133.229 " ++ "ipv6hint=2606:4700::6810:84e5,2606:4700::6810:85e5", "{f}", .{record}, ); } test "DNSKEYs and the signature over them" { const bytes = corpus.dnskeys.bytes; const message = try dns.Message.parse(bytes); try testing.expect((try message.opt()).?.dnssec_ok); var tags: [4]u16 = undefined; var found: usize = 0; var signature: ?dns.rdata.Rrsig = null; var it = message.answers(); while (try it.next()) |record| { switch (try record.data()) { .dnskey => |key| { try testing.expectEqual(@as(u8, 3), key.protocol); try testing.expectEqual(@as(u8, 13), key.algorithm); // ECDSA P-256 try testing.expect(key.isZoneKey()); tags[found] = key.keyTag(); found += 1; }, .rrsig => |sig| signature = sig, else => unreachable, } } try testing.expectEqual(@as(usize, 4), found); const sig = signature.?; try testing.expectEqual(dns.Type.dnskey, sig.type_covered); try testing.expectFmt("example.com.", "{f}", .{sig.signer}); // The signature names one of the keys that came with it, which is the // whole point of a key tag. try testing.expect(std.mem.indexOfScalar(u16, &tags, sig.key_tag) != null); // And the key that signs the keys is the one flagged as the entry point. for (tags) |tag| { if (tag != sig.key_tag) continue; var again = message.answers(); while (try again.next()) |record| { const data = try record.data(); if (data != .dnskey) continue; if (data.dnskey.keyTag() != tag) continue; try testing.expect(data.dnskey.isSecureEntryPoint()); } } } test "a signed denial of a name that does not exist" { const bytes = corpus.signed_denial.bytes; const message = try dns.Message.parse(bytes); // NOERROR rather than NXDOMAIN, which is the first sign of what this // server is doing; see below. try testing.expectEqual(dns.ResponseCode.noerror, try message.responseCode()); try testing.expectEqual(@as(u16, 0), message.header.counts.answer); var nsec: ?dns.rdata.Nsec = null; var signatures: usize = 0; var it = message.authority(); while (try it.next()) |record| { switch (try record.data()) { .nsec => |n| nsec = n, .rrsig => signatures += 1, .soa => {}, else => unreachable, } } // One signature over the SOA and one over the NSEC. try testing.expectEqual(@as(usize, 2), signatures); // Cloudflare answers a denial with "black lies": rather than prove the // gap a missing name would fall in -- which would mean signing an NSEC // per gap, or revealing the names either side -- it signs an NSEC saying // that this exact name exists and holds nothing but an NSEC and its // signature. The answer is a NODATA rather than an NXDOMAIN, and the // proof is one record that can be generated on demand. const n = nsec.?; try testing.expect(n.types.contains(.rrsig)); try testing.expect(n.types.contains(.nsec)); try testing.expect(!n.types.contains(.a)); } test "the root zone's name servers, which are almost entirely pointers" { const bytes = corpus.root_servers.bytes; const message = try dns.Message.parse(bytes); try testing.expect((try message.question()).?.name.isRoot()); try testing.expectEqual(@as(u16, 13), message.header.counts.answer); var it = message.answers(); var letter: u8 = 'a'; while (try it.next()) |record| : (letter += 1) { try testing.expect(record.name.isRoot()); var text: dns.Name.TextBuffer = undefined; const expected = [_]u8{letter} ++ ".root-servers.net.".*; try testing.expectEqualStrings( &expected, try (try record.data()).ns.toText(&text), ); } try testing.expectEqual(@as(u8, 'n'), letter); // Twelve of the thirteen are one label and a pointer: four bytes of record // data for a name that is twenty bytes written out in full. var again = message.answers(); _ = try again.next(); const second = (try again.next()).?; try testing.expectEqual(@as(usize, 4), second.rdata.len); try testing.expectEqual(@as(u16, 20), (try second.data()).ns.encodedLen()); }