Something went wrong. Try again.
DNS message encoding and decoding for Zig, with no I/O and no allocation.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Record types that carry keys, certificates and the addresses of things//! that hold them.//!//! Nothing here is DNSSEC -- these are keys published *in* the DNS for use//! somewhere else, rather than keys that sign the DNS itself. The shapes have//! one idea in common: a small header saying what kind of thing follows, and//! then a blob this library does not open. `Gateway` is the other recurring//! idea, an address that may be IPv4, IPv6, a domain name, or deliberately//! absent, with a type byte beside it saying which.
const std = @import("std");const Writer = std.Io.Writer;const z46 = @import("z46");
const Builder = @import("../Builder.zig");const Name = @import("../Name.zig");const presentation = @import("../presentation.zig");const types = @import("../types.zig");const wire = @import("../wire.zig");
const Decoder = wire.Decoder;
pub const Error = wire.Error || error{ /// The record data is not the shape its type requires. MalformedRdata,};
/// Bytes with no structure this library knows, written as base64: `dhcid`,/// whose contents are a hash a DHCP server made, `openpgpkey`, whose contents/// are an OpenPGP transferable public key, and `hhit` and `brid`, whose/// contents are CBOR describing a drone (RFC 9886).////// All are opaque on purpose. The record says only that this name is/// associated with that blob, and whoever can read the blob is not the DNS.pub const Blob = struct { bytes: []const u8,
/// Takes the rest of the record data as it is, without looking inside. pub fn parse(d: *Decoder) Error!Blob { return .{ .bytes = d.takeRest() }; }
test parse { var d = try Decoder.init("\x01\x02\x03"); const blob = try Blob.parse(&d); try std.testing.expectEqualSlices(u8, "\x01\x02\x03", blob.bytes); }
/// Writes the bytes exactly as they are. pub fn writeTo(self: Blob, b: *Builder) Builder.Error!void { try b.writeAll(self.bytes); }
test writeTo { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{ .compression = false }); try (Blob{ .bytes = "\x01\x02\x03" }).writeTo(&b); try std.testing.expectEqualSlices(u8, "\x01\x02\x03", b.buffer[12..b.pos]); }
/// Prints the bytes as one unbroken run of base64. pub fn format(self: Blob, w: *Writer) Writer.Error!void { try presentation.writeBase64(w, self.bytes); }
test format { try std.testing.expectFmt("AQID", "{f}", .{Blob{ .bytes = "\x01\x02\x03" }}); }};
/// A certificate, or a pointer to one (RFC 4398).pub const Cert = struct { /// What `certificate` holds. type: CertType, /// Which key, when the certificate is one this zone also publishes as a /// `dnskey`. Zero when that does not apply. key_tag: u16, /// The signing algorithm, from the same registry `dnskey` uses. algorithm: u8, certificate: []const u8,
pub const CertType = enum(u16) { /// An X.509 certificate, as PKIX uses them. pkix = 1, /// An SPKI certificate. spki = 2, /// An OpenPGP packet. pgp = 3, /// A URL pointing at an X.509 certificate. ipkix = 4, /// A URL pointing at an SPKI certificate. ispki = 5, /// A fingerprint and a URL pointing at an OpenPGP packet. ipgp = 6, /// An attribute certificate. acpkix = 7, /// A URL pointing at an attribute certificate. iacpkix = 8, /// A URI, as a private type. uri = 253, /// An OID, as a private type. oid = 254, _,
/// The mnemonic RFC 4398 §2.1 gives the type, in capitals, or `null` /// for a number that has none. pub fn name(self: CertType) ?[]const u8 { return switch (self) { _ => null, inline else => |t| blk: { const frozen = comptime up(@tagName(t)); break :blk &frozen; }, }; }
test name { try std.testing.expectEqualStrings("PKIX", CertType.pkix.name().?); try std.testing.expectEqual(@as(?[]const u8, null), @as(CertType, @fromBackingInt(100)).name()); }
/// Prints the mnemonic, or the number for a type without one. pub fn format(self: CertType, w: *Writer) Writer.Error!void { if (self.name()) |n| return w.writeAll(n); try w.print("{d}", .{@backingInt(self)}); } };
fn up(comptime lowercase: []const u8) [lowercase.len]u8 { var buf: [lowercase.len]u8 = undefined; for (lowercase, 0..) |c, i| buf[i] = std.ascii.toUpper(c); return buf; }
/// Reads the fixed five bytes and takes the rest as the certificate. A /// certificate type or algorithm nobody has assigned is carried, as a /// number. pub fn parse(d: *Decoder) Error!Cert { return .{ .type = try d.enumeration(CertType), .key_tag = try d.int(u16), .algorithm = try d.int(u8), .certificate = d.takeRest(), }; }
test parse { var d = try Decoder.init("\x00\x03\x00\x00\x00" ++ "\x01\x02\x03"); const cert = try Cert.parse(&d); try std.testing.expectEqual(Cert.CertType.pgp, cert.type); try std.testing.expectEqual(@as(usize, 3), cert.certificate.len); }
/// Writes the type, key tag and algorithm, then the certificate as it is. pub fn writeTo(self: Cert, b: *Builder) Builder.Error!void { try b.int(u16, @backingInt(self.type)); try b.int(u16, self.key_tag); try b.int(u8, self.algorithm); try b.writeAll(self.certificate); }
test writeTo { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{ .compression = false }); const cert: Cert = .{ .type = .pkix, .key_tag = 0, .algorithm = 0, .certificate = "\x01\x02\x03" }; try cert.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x00\x01\x00\x00\x00\x01\x02\x03", b.buffer[12..b.pos]); }
/// Prints the type by mnemonic where it has one, the key tag and /// algorithm as numbers, and the certificate as base64. pub fn format(self: Cert, w: *Writer) Writer.Error!void { try w.print("{f} {d} {d} ", .{ self.type, self.key_tag, self.algorithm }); try presentation.writeBase64(w, self.certificate); }
test format { const cert: Cert = .{ .type = .pgp, .key_tag = 0, .algorithm = 0, .certificate = "\x01\x02\x03" }; try std.testing.expectFmt("PGP 0 0 AQID", "{f}", .{cert}); // A type with no mnemonic is written as its number. const private: Cert = .{ .type = @fromBackingInt(65535), .key_tag = 0, .algorithm = 0, .certificate = "" }; try std.testing.expectFmt("65535 0 0 ", "{f}", .{private}); }};
/// Something to talk to, which may be an address of either family, a name, or/// nothing at all. `ipseckey` and `amtrelay` both carry one, with a type byte/// beside it saying which of the four it is.pub const Gateway = union(enum) { /// No gateway. The record is saying "here is a key" and nothing about /// where to use it. none, ipv4: z46.Ipv4Address, ipv6: z46.Ipv6Address, /// A domain name, never compressed: these types are newer than RFC 1035 /// and RFC 3597 §4 forbids it. name: Name,
/// The type byte that goes beside it. pub fn kind(self: Gateway) u8 { return switch (self) { .none => 0, .ipv4 => 1, .ipv6 => 2, .name => 3, }; }
test kind { const gateway: Gateway = .{ .ipv4 = .{ .octets = .{ 192, 0, 2, 1 } } }; try std.testing.expectEqual(@as(u8, 1), gateway.kind()); try std.testing.expectEqual(@as(u8, 0), (Gateway{ .none = {} }).kind()); }
/// Reads the gateway `kind_byte` says follows, which the caller has already /// read from elsewhere in the record. A kind other than 0 to 3 is /// `error.MalformedRdata`, since there is no telling how long the gateway /// is, and a name may not be compressed. pub fn parse(d: *Decoder, kind_byte: u8) Error!Gateway { return switch (kind_byte) { 0 => .none, 1 => .{ .ipv4 = .{ .octets = (try d.take(4))[0..4].* } }, 2 => .{ .ipv6 = .{ .octets = (try d.take(16))[0..16].* } }, 3 => .{ .name = try d.uncompressedName() }, else => error.MalformedRdata, }; }
test parse { var d = try Decoder.init("\xc0\x00\x02\x26"); const gateway = try Gateway.parse(&d, 1); try std.testing.expectFmt("192.0.2.38", "{f}", .{gateway}); var unknown = try Decoder.init("\xc0\x00\x02\x26"); try std.testing.expectError(error.MalformedRdata, Gateway.parse(&unknown, 4)); }
/// Writes the gateway alone, never compressing a name. Its kind byte is /// not written here, because each record puts it somewhere else. pub fn writeTo(self: Gateway, b: *Builder) Builder.Error!void { switch (self) { .none => {}, .ipv4 => |address| try b.writeAll(&address.octets), .ipv6 => |address| try b.writeAll(&address.octets), .name => |value| try b.name(value, .uncompressed), } }
test writeTo { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{ .compression = false }); const gateway: Gateway = .{ .name = .literal("gw.ex") }; try gateway.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x02gw\x02ex\x00", b.buffer[12..b.pos]); }
/// Prints the address or the name, and `.` for no gateway at all. pub fn format(self: Gateway, w: *Writer) Writer.Error!void { switch (self) { // The root, which is what a zone file writes for "no gateway" -- // and is also how a gateway that *is* the root would be written, // which is a collision RFC 4025 lives with. .none => try w.writeByte('.'), .ipv4 => |address| try w.print("{f}", .{address}), .ipv6 => |address| try w.print("{f}", .{address}), .name => |value| try w.print("{f}", .{value}), } }
test format { try std.testing.expectFmt(".", "{f}", .{Gateway{ .none = {} }}); try std.testing.expectFmt("gw.ex.", "{f}", .{Gateway{ .name = .literal("gw.ex") }}); }};
/// A public key for IPsec, and where to use it (RFC 4025).pub const Ipseckey = struct { /// Lower is tried first, as with `mx`. precedence: u8, /// 0 none, 1 DSA, 2 RSA, 3 ECDSA. algorithm: u8, gateway: Gateway, public_key: []const u8,
/// Reads the precedence, gateway type and algorithm, then the gateway that /// type says follows, refusing a type other than 0 to 3, and takes the rest /// as the public key. pub fn parse(d: *Decoder) Error!Ipseckey { const precedence = try d.int(u8); const gateway_type = try d.int(u8); const algorithm = try d.int(u8); return .{ .precedence = precedence, .algorithm = algorithm, .gateway = try Gateway.parse(d, gateway_type), .public_key = d.takeRest(), }; }
test parse { var d = try Decoder.init("\x0a\x01\x02\xc0\x00\x02\x26\x01\x02\x03"); const key = try Ipseckey.parse(&d); try std.testing.expectEqual(@as(u8, 1), key.gateway.kind()); try std.testing.expectEqualSlices(u8, "\x01\x02\x03", key.public_key); }
/// Writes the fixed fields with the gateway's kind among them, the /// gateway, and the public key. A gateway name is never compressed. pub fn writeTo(self: Ipseckey, b: *Builder) Builder.Error!void { try b.int(u8, self.precedence); try b.int(u8, self.gateway.kind()); try b.int(u8, self.algorithm); try self.gateway.writeTo(b); try b.writeAll(self.public_key); }
test writeTo { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{ .compression = false }); const key: Ipseckey = .{ .precedence = 10, .algorithm = 2, .gateway = .none, .public_key = "\x01\x02\x03" }; try key.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x0a\x00\x02\x01\x02\x03", b.buffer[12..b.pos]); }
/// Prints precedence, gateway type, algorithm, the gateway and the public /// key in base64, the order RFC 4025 §3 gives them. pub fn format(self: Ipseckey, w: *Writer) Writer.Error!void { try w.print("{d} {d} {d} {f} ", .{ self.precedence, self.gateway.kind(), self.algorithm, self.gateway, }); try presentation.writeBase64(w, self.public_key); }
test format { const key: Ipseckey = .{ .precedence = 10, .algorithm = 2, .gateway = .{ .ipv4 = .{ .octets = .{ 192, 0, 2, 38 } } }, .public_key = "\x01\x02\x03", }; try std.testing.expectFmt("10 1 2 192.0.2.38 AQID", "{f}", .{key}); }};
/// Where to find a relay for automatic multicast tunnelling (RFC 8777).pub const Amtrelay = struct { precedence: u8, /// The "discovery optional" bit: the client may use this relay rather than /// going looking for one. discovery_optional: bool, relay: Gateway,
/// Reads the precedence and the flags byte, whose top bit is the discovery /// bit and whose other seven are the relay type, then the relay that type /// says follows. A type other than 0 to 3 is refused. pub fn parse(d: *Decoder) Error!Amtrelay { const precedence = try d.int(u8); const flags = try d.int(u8); return .{ .precedence = precedence, .discovery_optional = flags & 0x80 != 0, .relay = try Gateway.parse(d, flags & 0x7f), }; }
test parse { var d = try Decoder.init("\x0a\x81\xc0\x00\x02\x01"); const relay = try Amtrelay.parse(&d); try std.testing.expect(relay.discovery_optional); try std.testing.expectFmt("192.0.2.1", "{f}", .{relay.relay}); }
/// Writes the precedence, the discovery bit and relay type packed into one /// byte, and the relay, a name never compressed. pub fn writeTo(self: Amtrelay, b: *Builder) Builder.Error!void { try b.int(u8, self.precedence); try b.int(u8, @as(u8, @intFromBool(self.discovery_optional)) << 7 | self.relay.kind()); try self.relay.writeTo(b); }
test writeTo { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{ .compression = false }); const relay: Amtrelay = .{ .precedence = 10, .discovery_optional = true, .relay = .{ .ipv4 = .{ .octets = .{ 192, 0, 2, 1 } } }, }; try relay.writeTo(&b); try std.testing.expectEqualSlices(u8, "\x0a\x81\xc0\x00\x02\x01", b.buffer[12..b.pos]); }
/// Prints the precedence, the discovery bit as 0 or 1, the relay type and /// the relay, which is `.` when there is none. pub fn format(self: Amtrelay, w: *Writer) Writer.Error!void { try w.print("{d} {d} {d} {f}", .{ self.precedence, @intFromBool(self.discovery_optional), self.relay.kind(), self.relay, }); }
test format { const relay: Amtrelay = .{ .precedence = 10, .discovery_optional = false, .relay = .none }; try std.testing.expectFmt("10 0 0 .", "{f}", .{relay}); }};
/// A host identity tag, its public key, and the rendezvous servers that will/// forward to whatever address the host currently has (RFC 8005).pub const Hip = struct { /// 1 DSA, 2 RSA, 3 ECDSA, 5 EdDSA. Zero when there is no key. algorithm: u8, /// The host identity tag: a hash of the public key, which is what the host /// is called in a protocol that does not use addresses as names. hit: []const u8, public_key: []const u8, /// The message the rendezvous server names are read out of, cut off at the /// end of this record. message: []const u8, servers_offset: u16,
/// Reads the tag and key by their lengths, then every rendezvous server to /// the end of the record data, none of which may be compressed. pub fn parse(d: *Decoder) Error!Hip { const hit_len = try d.int(u8); const algorithm = try d.int(u8); const key_len = try d.int(u16); const hit = try d.take(hit_len); const public_key = try d.take(key_len); const servers_offset = d.pos; // Walked now so that a malformed name is this record's problem rather // than a surprise the first time somebody iterates. while (!d.atEnd()) _ = try d.uncompressedName(); return .{ .algorithm = algorithm, .hit = hit, .public_key = public_key, .message = d.message, .servers_offset = servers_offset, }; }
test parse { var d = try Decoder.init("\x04\x02\x00\x03" ++ "\x01\x02\x03\x04" ++ "\xaa\xbb\xcc" ++ "\x04rvs1\x02ex\x00"); const hip = try Hip.parse(&d); try std.testing.expectEqual(@as(u8, 2), hip.algorithm); try std.testing.expectEqualSlices(u8, "\x01\x02\x03\x04", hip.hit); }
/// The rendezvous servers, in the order they were given. pub const ServerIterator = struct { decoder: Decoder,
/// The next server, or `null` after the last one. pub fn next(self: *ServerIterator) ?Name { if (self.decoder.atEnd()) return null; return self.decoder.uncompressedName() catch null; }
test next { var d = try Decoder.init("\x01\x00\x00\x00" ++ "\xaa" ++ "\x04rvs1\x02ex\x00"); var it = (try Hip.parse(&d)).servers(); try std.testing.expectFmt("rvs1.ex.", "{f}", .{it.next().?}); try std.testing.expectEqual(@as(?Name, null), it.next()); } };
/// The rendezvous servers, read out of the message as they are asked for. /// `parse` has already walked them, so for a parsed record none can fail. pub fn servers(self: Hip) ServerIterator { return .{ .decoder = .{ .message = self.message, .pos = self.servers_offset } }; }
test servers { var d = try Decoder.init("\x01\x00\x00\x00" ++ "\xaa" ++ "\x04rvs1\x02ex\x00" ++ "\x04rvs2\x02ex\x00"); const hip = try Hip.parse(&d); var count: usize = 0; var it = hip.servers(); while (it.next()) |_| count += 1; try std.testing.expectEqual(@as(usize, 2), count); }
/// Writes the lengths, the tag, the key and the servers, none of them /// compressed. A tag over 255 bytes or a key over 65535 is /// `error.RdataTooLong`. pub fn writeTo(self: Hip, b: *Builder) Builder.Error!void { if (self.hit.len > std.math.maxInt(u8)) return error.RdataTooLong; if (self.public_key.len > std.math.maxInt(u16)) return error.RdataTooLong; try b.int(u8, @intCast(self.hit.len)); try b.int(u8, self.algorithm); try b.int(u16, @intCast(self.public_key.len)); try b.writeAll(self.hit); try b.writeAll(self.public_key); var it = self.servers(); while (it.next()) |server| try b.name(server, .uncompressed); }
test writeTo { const bytes = "\x01\x02\x00\x01" ++ "\xaa" ++ "\xbb" ++ "\x04rvs1\x02ex\x00"; var d = try Decoder.init(bytes); const hip = try Hip.parse(&d); var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{ .compression = false }); try hip.writeTo(&b); try std.testing.expectEqualSlices(u8, bytes, b.buffer[12..b.pos]); }
/// Prints the algorithm, the tag in uppercase hexadecimal, the key in /// base64, and then each rendezvous server. pub fn format(self: Hip, w: *Writer) Writer.Error!void { try w.print("{d} ", .{self.algorithm}); try presentation.writeHexUpper(w, self.hit); try w.writeByte(' '); try presentation.writeBase64(w, self.public_key); var it = self.servers(); while (it.next()) |server| try w.print(" {f}", .{server}); }
test format { var d = try Decoder.init("\x02\x02\x00\x03" ++ "\xab\xcd" ++ "\xaa\xbb\xcc" ++ "\x04rvs1\x02ex\x00"); try std.testing.expectFmt("2 ABCD qrvM rvs1.ex.", "{f}", .{try Hip.parse(&d)}); }};
/// A key established for a transaction rather than published in a zone/// (RFC 2930): the other half of `tsig`, used to agree on a secret before/// anything is signed with it.pub const Tkey = struct { /// The keyed hash the resulting key is for, as a name. algorithm: Name, /// When the key starts being valid, in seconds since the Unix epoch. inception: u32, expiration: u32, /// How the key is being established: 3 is Diffie-Hellman, 5 is a key /// handed over under an existing `tsig`. mode: u16, /// Why it failed, using the same numbering as a response code extended /// for `tsig`. error_code: u16, /// Whatever the mode says this is. key: []const u8, other: []const u8,
/// Reads the algorithm name, which may not be compressed, the fixed /// fields, and the key and other data by their lengths. pub fn parse(d: *Decoder) Error!Tkey { const algorithm = try d.uncompressedName(); const inception = try d.int(u32); const expiration = try d.int(u32); const mode = try d.int(u16); const error_code = try d.int(u16); const key_len = try d.int(u16); const key = try d.take(key_len); const other_len = try d.int(u16); return .{ .algorithm = algorithm, .inception = inception, .expiration = expiration, .mode = mode, .error_code = error_code, .key = key, .other = try d.take(other_len), }; }
test parse { var d = try Decoder.init("\x0bhmac-sha256\x00" ++ "\x6a\xa8\x68\x80" ++ "\x6a\xa8\x6c\x68" ++ "\x00\x05" ++ "\x00\x00" ++ "\x00\x03\x01\x02\x03" ++ "\x00\x00"); const tkey = try Tkey.parse(&d); try std.testing.expectEqual(@as(u16, 5), tkey.mode); try std.testing.expectEqualSlices(u8, "\x01\x02\x03", tkey.key); }
/// Writes the algorithm name, never compressed, the fixed fields, and the /// key and other data each after its length. Either one over 65535 bytes /// is `error.RdataTooLong`. pub fn writeTo(self: Tkey, b: *Builder) Builder.Error!void { try b.name(self.algorithm, .uncompressed); try b.int(u32, self.inception); try b.int(u32, self.expiration); try b.int(u16, self.mode); try b.int(u16, self.error_code); if (self.key.len > std.math.maxInt(u16)) return error.RdataTooLong; try b.int(u16, @intCast(self.key.len)); try b.writeAll(self.key); if (self.other.len > std.math.maxInt(u16)) return error.RdataTooLong; try b.int(u16, @intCast(self.other.len)); try b.writeAll(self.other); }
test writeTo { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 0 }, .{ .compression = false }); const tkey: Tkey = .{ .algorithm = .literal("x"), .inception = 1, .expiration = 2, .mode = 3, .error_code = 0, .key = "\xff", .other = "", }; try tkey.writeTo(&b); try std.testing.expectEqualSlices( u8, "\x01x\x00" ++ "\x00\x00\x00\x01" ++ "\x00\x00\x00\x02" ++ "\x00\x03\x00\x00" ++ "\x00\x01\xff" ++ "\x00\x00", b.buffer[12..b.pos], ); }
/// Prints the algorithm, the times as seconds, the mode and error as /// numbers, and the key and other data each as its length and then base64. /// Empty other data is written as its length alone. pub fn format(self: Tkey, w: *Writer) Writer.Error!void { try w.print("{f} {d} {d} {d} {d} {d} ", .{ self.algorithm, self.inception, self.expiration, self.mode, self.error_code, self.key.len, }); try presentation.writeBase64(w, self.key); try w.print(" {d}", .{self.other.len}); if (self.other.len > 0) { try w.writeByte(' '); try presentation.writeBase64(w, self.other); } }
test format { const tkey: Tkey = .{ .algorithm = .literal("hmac-sha256"), .inception = 1789421696, .expiration = 1789422696, .mode = 5, .error_code = 0, .key = "\x01\x02\x03", .other = "", }; try std.testing.expectFmt("hmac-sha256. 1789421696 1789422696 5 0 3 AQID 0", "{f}", .{tkey}); }};
const testing = std.testing;
fn expectParses(comptime T: type, bytes: []const u8, expected: []const u8) !T { var d = try Decoder.init(bytes); const value = try T.parse(&d); try testing.expect(d.atEnd()); try testing.expectFmt(expected, "{f}", .{value}); return value;}
test "a certificate" { const cert = try expectParses( Cert, "\x00\x03\x00\x00\x00" ++ "\x01\x02\x03", "PGP 0 0 AQID", ); try testing.expectEqual(Cert.CertType.pgp, cert.type); _ = try expectParses(Cert, "\xff\xff\x00\x00\x00", "65535 0 0 ");}
test "a gateway is any of four things" { _ = try expectParses(Ipseckey, "\x0a\x00\x02\x01\x02\x03", "10 0 2 . AQID"); _ = try expectParses( Ipseckey, "\x0a\x01\x02\xc0\x00\x02\x26\x01\x02\x03", "10 1 2 192.0.2.38 AQID", ); _ = try expectParses( Ipseckey, "\x0a\x02\x02" ++ "\x20\x01\x0d\xb8" ++ @as([11]u8, @splat(0)) ++ "\x01" ++ "\x01\x02\x03", "10 2 2 2001:db8::1 AQID", ); const named = try expectParses( Ipseckey, "\x0a\x03\x02\x02gw\x02ex\x00\x01\x02\x03", "10 3 2 gw.ex. AQID", ); try testing.expectFmt("gw.ex.", "{f}", .{named.gateway.name});
// And a gateway type nobody defined is not a gateway. var d = try Decoder.init("\x0a\x04\x02"); try testing.expectError(error.MalformedRdata, Ipseckey.parse(&d));}
test "a relay, with its discovery bit" { const relay = try expectParses(Amtrelay, "\x0a\x81\xc0\x00\x02\x01", "10 1 1 192.0.2.1"); try testing.expect(relay.discovery_optional); _ = try expectParses(Amtrelay, "\x0a\x00", "10 0 0 .");}
test "a host identity, with rendezvous servers" { const hip = try expectParses( Hip, "\x04\x02\x00\x03" ++ "\x01\x02\x03\x04" ++ "\xaa\xbb\xcc" ++ "\x04rvs1\x02ex\x00" ++ "\x04rvs2\x02ex\x00", "2 01020304 qrvM rvs1.ex. rvs2.ex.", ); var it = hip.servers(); try testing.expectFmt("rvs1.ex.", "{f}", .{it.next().?}); try testing.expectFmt("rvs2.ex.", "{f}", .{it.next().?}); try testing.expectEqual(@as(?Name, null), it.next());}
test "a transaction key" { _ = try expectParses( Tkey, "\x0bhmac-sha256\x00" ++ "\x6a\xa8\x68\x80" ++ "\x6a\xa8\x6c\x68" ++ "\x00\x05" ++ "\x00\x00" ++ "\x00\x03\x01\x02\x03" ++ "\x00\x00", "hmac-sha256. 1789421696 1789422696 5 0 3 AQID 0", );}
test "the opaque blobs" { _ = try expectParses(Blob, "\x01\x02\x03", "AQID");}