// SPDX-FileCopyrightText: © 2026 Tim Culverhouse // SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-FileCopyrightText: © 2010-2011 Nokia Corporation // SPDX-FileCopyrightText: © 2022 Collabora Ltd. // SPDX-FileCopyrightText: © 2022 Evgeny Vereshchagin // SPDX-License-Identifier: MIT //! Wire format tests using vectors from the reference D-Bus implementation. //! //! The message blobs and syntax vectors below are transcribed from the test //! suite at , which carries the same //! MIT license as this project: `test/marshal.c` and `test/syntax.c` by Simon //! McVittie, and the annotated hex dumps under `test/data/valid-messages` and //! `test/data/invalid-messages`. Several of the invalid messages came out of a //! fuzzer, so they exercise cases that are unlikely to be reached by writing //! tests against the specification alone. //! //! Where this implementation deliberately differs from the reference — it is a //! client transport rather than a bus daemon, and refuses things a daemon has //! to tolerate — the test says so. const std = @import("std"); const wire = @import("wire.zig"); /// Parse `bytes`, which are copied so the message can own them. On failure the /// copy is released here, since `parseMessage` only takes ownership once it /// returns a message. fn parse(allocator: std.mem.Allocator, bytes: []const u8) !wire.Message { const data = try allocator.dupe(u8, bytes); errdefer allocator.free(data); const fds = try allocator.alloc(std.posix.fd_t, 0); errdefer allocator.free(fds); return wire.parseMessage(allocator, data, fds); } /// `test/marshal.c`, `le_blob`: a method return carrying one `u`, whose serial, /// reply serial, and body value are all distinctive enough to catch a byte /// order or offset mistake. const le_blob = [_]u8{ 'l', 2, 2, 1, // little-endian, method return, no auto-start, version 1 0x04, 0x00, 0x00, 0x00, // body is 4 bytes 0x78, 0x56, 0x34, 0x12, // serial 0x12345678 0x0f, 0x00, 0x00, 0x00, // header fields are 15 bytes 5, 1, 'u', 0, // in reply to, variant signature "u" 0x12, 0xef, 0xcd, 0xab, // reply serial 0xabcdef12 8, 1, 'g', 0, // signature field, variant signature "g" 1, 'u', 0, 0, // body signature "u", then padding 0xef, 0xbe, 0xad, 0xde, // body: 0xdeadbeef }; /// `test/marshal.c`, `be_blob`: the same message in the byte order almost no /// machine uses, which is exactly why it is worth decoding. const be_blob = [_]u8{ 'B', 2, 2, 1, 0x00, 0x00, 0x00, 0x04, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x00, 0x0f, 5, 1, 'u', 0, 0xab, 0xcd, 0xef, 0x12, 8, 1, 'g', 0, 1, 'u', 0, 0, 0xde, 0xad, 0xbe, 0xef, }; test "reference: decode a message in either byte order" { const a = std.testing.allocator; for ([_][]const u8{ &le_blob, &be_blob }) |blob| { var message = try parse(a, blob); defer message.deinit(); try std.testing.expectEqual(wire.MessageType.method_return, message.messageType()); try std.testing.expectEqual(@as(u32, 0x12345678), message.header.serial); try std.testing.expectEqual(@as(u32, 0xabcdef12), message.header.reply_serial.?); try std.testing.expectEqualStrings("u", message.bodySignature()); var decoder = message.bodyDecoder(); try std.testing.expectEqual(@as(u32, 0xdeadbeef), try decoder.uint32()); try decoder.end(); } } test "reference: bytes needed before a message can be decoded" { // `test/marshal.c`, test_needed. The fixed header is 16 bytes, and until // those have arrived the length cannot be known at all. for ([_][]const u8{ &le_blob, &be_blob }) |blob| { try std.testing.expect((try wire.messageLength(blob[0..0])) == null); try std.testing.expect((try wire.messageLength(blob[0..15])) == null); // The reference implementation reports the full length as soon as the // fixed header is complete. This one waits until the whole message is // buffered, because its only caller wants a message it can parse, not // a size to reserve. try std.testing.expect((try wire.messageLength(blob[0..16])) == null); try std.testing.expect((try wire.messageLength(blob[0..31])) == null); try std.testing.expect((try wire.messageLength(blob[0 .. blob.len - 1])) == null); try std.testing.expectEqual(@as(?usize, blob.len), try wire.messageLength(blob)); } // Trailing bytes belong to the next message, not this one. var two_messages: [le_blob.len + 4]u8 = @splat(0); @memcpy(two_messages[0..le_blob.len], &le_blob); try std.testing.expectEqual(@as(?usize, le_blob.len), try wire.messageLength(&two_messages)); } /// `test/data/valid-messages/minimal.message-raw`: the shortest message the /// protocol allows, a reply with no body at all. const minimal_message = [_]u8{ 'l', 2, 0, 1, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // serial 2 0x08, 0x00, 0x00, 0x00, // header fields are 8 bytes 5, 1, 'u', 0, // in reply to, variant signature "u" 0x01, 0x00, 0x00, 0x00, // reply serial 1 }; test "reference: minimal valid message" { const a = std.testing.allocator; var message = try parse(a, &minimal_message); defer message.deinit(); try std.testing.expectEqual(wire.MessageType.method_return, message.messageType()); try std.testing.expectEqual(@as(u32, 2), message.header.serial); try std.testing.expectEqual(@as(u32, 1), message.header.reply_serial.?); try std.testing.expectEqualStrings("", message.bodySignature()); try std.testing.expectEqual(@as(usize, 0), message.body().len); } test "reference: an unknown header field is skipped" { // `test/header-fields.c` inserts a field code that no version of the // specification defines; it has to be skipped rather than rejected, so // that a future field cannot break an old client. const message_bytes = [_]u8{ 'l', 2, 0, 1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // serial 1 0x14, 0x00, 0x00, 0x00, // header fields are 20 bytes 5, 1, 'u', 0, // in reply to, variant signature "u" 0x12, 0xef, 0xcd, 0xab, // reply serial 0xabcdef12 123, 1, 's', 0, // field code 123, which is not defined 0x03, 0x00, 0x00, 0x00, // its value: a 3-byte string 'a', 'b', 'c', 0, 0x00, 0x00, 0x00, 0x00, // padding to the body }; const a = std.testing.allocator; var message = try parse(a, &message_bytes); defer message.deinit(); try std.testing.expectEqual(@as(u32, 0xabcdef12), message.header.reply_serial.?); } test "reference: serial zero is rejected" { // `test/header-fields.c`: zero is reserved, so a message carrying it can // never be matched to a reply. var message_bytes = minimal_message; @memset(message_bytes[8..12], 0); const a = std.testing.allocator; try std.testing.expectError(error.InvalidMessage, parse(a, &message_bytes)); } test "reference: undefined message type" { // `test/data/valid-messages/byteswap-fd-index.message-raw`, a big-endian // fuzzer product filed under valid messages: the specification says an // unknown message type must be ignored, so it has to parse rather than // break the connection. `Connection` drops it once parsed. // // The body holds a handle naming the 0xf8280208th attached descriptor, // which no sender would mean; nothing here has to make sense of it, // because the type it belongs to is one this version does not implement. const message_bytes = [_]u8{ 'B', 0x2d, 0x31, 1, // big-endian, undefined type 0x2d 0x00, 0x00, 0x00, 0x0c, 0x97, 0xbc, 0x90, 0x23, 0x00, 0x00, 0x00, 0x08, 8, 1, 'g', 0, // signature field 2, 'h', 'v', 0, // body signature "hv" 0xf8, 0x28, 0x02, 0x08, // handle: descriptor index 0xf8280208 2, 'a', 'v', 0, // variant signature "av" 0x00, 0x00, 0x00, 0x00, // empty array }; const a = std.testing.allocator; var message = try parse(a, &message_bytes); defer message.deinit(); try std.testing.expectEqual(@as(u8, 0x2d), @intFromEnum(message.messageType())); try std.testing.expectEqualStrings("hv", message.bodySignature()); // Type zero is the one value that is never legitimate, so it stays an // error rather than something to ignore. var reserved_type = message_bytes; reserved_type[1] = 0; try std.testing.expectError(error.InvalidMessage, wire.messageLength(&reserved_type)); try std.testing.expectError(error.InvalidMessage, parse(a, &reserved_type)); } test "reference: truncated variant signature" { // `test/data/invalid-messages/truncated-variant-sig.message-raw`: a field // whose variant signature claims 255 bytes that the message does not have. const message_bytes = [_]u8{ 'l', 0xff, 0x00, 1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, // header fields are 4 bytes 0xff, 0xff, 0x00, 0x00, // field 0xff, signature length 0xff 0x00, 0x00, 0x00, 0x00, }; const a = std.testing.allocator; try std.testing.expectError(error.EndOfMessage, parse(a, &message_bytes)); } test "reference: zero-length variant signature" { // `test/data/invalid-messages/zero-length-variant-sig.message-raw`: a // variant must hold exactly one value, so its signature cannot be empty. const message_bytes = [_]u8{ 'l', 0xff, 0x00, 1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // header fields are 3 bytes 0xff, 0x00, 0x00, 0x00, // field 0xff, signature length 0 0x00, 0x00, 0x00, 0x00, }; const a = std.testing.allocator; try std.testing.expectError(error.InvalidSignature, parse(a, &message_bytes)); } test "reference: mis-nested signature" { // `test/data/invalid-messages/mis-nested-sig.message-raw`, simplified by // the reference suite from a fuzzer case: "(ta{ya{da{ya{yaq}}})}" closes // containers it never opened. const message_bytes = [_]u8{ 'l', 0xff, 0x00, 1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, // header fields are 24 bytes 0xff, 0x15, '(', 't', // field 0xff, 21-byte signature 'a', '{', 'y', 'a', '{', 'd', 'a', '{', 'y', 'a', '{', 'y', 'a', 'q', '}', '}', '}', ')', '}', 0, }; const a = std.testing.allocator; try std.testing.expectError(error.InvalidSignature, parse(a, &message_bytes)); } test "reference: issue 418" { // `test/data/invalid-messages/issue418.message-raw`: the fuzzer output the // three cases above were distilled from, invalid in all of those ways at // once. Whichever defect is noticed first, the message must not parse. const message_bytes = [_]u8{ 'l', 0x8f, 0x28, 1, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x22, 0x00, 0x08, 0x18, 0x00, 0x00, 0x00, 0xfd, 0x15, '(', 't', 'a', '{', 'y', 'a', '{', 'd', 'a', '{', 'y', 'a', '{', 'y', 'a', 'q', '}', '}', '}', ')', '}', 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, // field 0x0c, zero-length signature 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0x00, // field 0xfe, signature longer than the message }; const a = std.testing.allocator; // There is a fourth defect the reference suite does not name, and it is the // one noticed first: the header says its fields are 24 bytes, which puts // the end of the message at byte 40, so the last 28 bytes of the blob are // not part of it at all. try std.testing.expectError(error.TrailingData, parse(a, &message_bytes)); // Cut to the length the header declares, and the mis-nested signature in // the first field is what rejects it. try std.testing.expectError(error.InvalidSignature, parse(a, message_bytes[0..40])); } test "reference: fixed-size array whose length is not a whole number of items" { // `test/data/invalid-messages/fixed-array-not-divisible.message-raw` is // 296 bytes of fuzzer output whose defect is an array of int64 declared to // be 5 bytes long. That is the part worth keeping, so it is rebuilt here // rather than transcribed. const a = std.testing.allocator; var body: wire.Encoder = .init(a); defer body.deinit(); try body.uint32(5); // the array is 5 bytes long try body.structAlignment(); // int64 elements start on an 8-byte boundary for (0..5) |_| try body.byte(0); const bytes = try wire.encodeMessage(a, .{ .message_type = .method_call, .path = "/x", .member = "M", .signature = "at", }, 1, body.bytes(), 0); defer a.free(bytes); // The decoder skips a fixed-size-element array in one step, so a length // that is not a whole number of elements is caught as exactly that, // rather than by running off the end of the message mid-element. try std.testing.expectError(error.InvalidMessage, parse(a, bytes)); } test "reference: a boolean needs a value, and only two are valid" { // `test/data/invalid-messages/boolean-has-no-value.message-raw`: the body // signature promises a boolean that the empty body does not contain. const a = std.testing.allocator; const empty = try wire.encodeMessage(a, .{ .message_type = .method_call, .path = "/x", .member = "M", .signature = "b", }, 1, "", 0); defer a.free(empty); try std.testing.expectError(error.InvalidMessage, parse(a, empty)); // A boolean is carried in 32 bits but may only be 0 or 1; the // specification calls any other value invalid, so parsing rejects the // message rather than leaving the discovery to whichever read reaches // the value first. var body: wire.Encoder = .init(a); defer body.deinit(); try body.uint32(2); const bytes = try wire.encodeMessage(a, .{ .message_type = .method_call, .path = "/x", .member = "M", .signature = "b", }, 1, body.bytes(), 0); defer a.free(bytes); try std.testing.expectError(error.InvalidMessage, parse(a, bytes)); } test "reference: object path syntax" { // `test/syntax.c`, valid_paths and invalid_paths. const a = std.testing.allocator; var encoder: wire.Encoder = .init(a); defer encoder.deinit(); const valid = [_][]const u8{ "/", "/a", "/_", "/a/b/c", "/com/example/123", "/org/freedesktop/DBus", "/org/freedesktop/Telepathy/AccountManager", }; for (valid) |path| try encoder.objectPath(path); const invalid = [_][]const u8{ "", ".", "//", "/a/", "/-", "/com//example/MyApp", "/$", "/\xa9", // the copyright sign in Latin-1 "/\xc2\xa9", // and in UTF-8: valid text, but not a valid path }; for (invalid) |path| { try std.testing.expectError(error.InvalidObjectPath, encoder.objectPath(path)); } } test "reference: signature syntax" { // `test/syntax.c`, valid_signatures and invalid_signatures. A body // signature may be empty or hold several complete types. const a = std.testing.allocator; var encoder: wire.Encoder = .init(a); defer encoder.deinit(); const valid = [_][]const u8{ "", "a{sv}", "a{s(i)}", "a(sa{ii})" }; for (valid) |signature| try encoder.signature(signature); const invalid = [_][]const u8{ "a", // an array of nothing "a{s_}", "a{s(i}", "a{s(i})", "a{s(i)", "a(sa{ii)", "a(sa{ii)}", ")", "}", }; for (invalid) |signature| { try std.testing.expectError(error.InvalidSignature, encoder.signature(signature)); } } test "reference: single complete type" { // `test/syntax.c`, valid_single_signatures and invalid_single_signatures. // A variant holds one value, so its signature is held to a stricter rule // than a body's. const a = std.testing.allocator; var encoder: wire.Encoder = .init(a); defer encoder.deinit(); for ([_][]const u8{ "s", "a{sv}" }) |signature| try encoder.variantSignature(signature); const invalid = [_][]const u8{ "", // no type at all "a", // an array of nothing "sv", // two types "a{sv}as", // likewise }; for (invalid) |signature| { try std.testing.expectError(error.InvalidSignature, encoder.variantSignature(signature)); } } test "reference: strings must be valid UTF-8" { // `test/syntax.c`, valid_strings and invalid_strings. const a = std.testing.allocator; var encoder: wire.Encoder = .init(a); defer encoder.deinit(); const valid = [_][]const u8{ "", "\xc2\xa9", // the copyright sign "\xef\xbf\xbe", // U+FFFE, reserved but well-formed }; for (valid) |value| try encoder.string(value); const invalid = [_][]const u8{ "\xa9", // the copyright sign in Latin-1 "\xed\xa0\x80", // a UTF-16 surrogate, which UTF-8 may not encode "embedded\x00nul", // not in the reference suite, but equally invalid }; for (invalid) |value| { try std.testing.expectError(error.InvalidString, encoder.string(value)); } }