// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Make the seeds the fuzz target in `tests/fuzz.zig` starts from, and write //! them out as a Zig source file. //! //! fuzz-corpus //! //! A seed is a byte saying which signal, then a body of it as the daemon //! sends one: an item found on each protocol, services resolved with and //! without TXT, a group's states, the daemon coming and going. A mutation of //! one gets into the strings and the arrays; random bytes almost never //! decode at all. const std = @import("std"); const avahi = @import("avahi"); const wire = avahi.wire; const Encoder = wire.Encoder; pub fn main(init: std.process.Init) !void { const io = init.io; const gpa = init.gpa; var args: std.process.Args.Iterator = .init(init.minimal.args); _ = args.skip(); const path = args.next() orelse return error.MissingArgument; var seeds: std.ArrayList([]u8) = .empty; defer { for (seeds.items) |s| gpa.free(s); seeds.deinit(gpa); } for ([_]i32{ 0, 1 }) |protocol| { var e: Encoder = .init(gpa); defer e.deinit(); try e.int32(2); try e.int32(protocol); try e.string("Kitchen"); try e.string("_sendspin._tcp"); try e.string("local"); try e.uint32(if (protocol == 0) 0 else wire.lookup_result_our_own); try seeds.append(gpa, try seed(gpa, 0, e.bytes())); } const services = [_]struct { address: []const u8, protocol: i32, txt: []const []const u8 }{ .{ .address = "192.168.1.20", .protocol = 0, .txt = &.{ "name=Kitchen", "path=/sendspin" } }, .{ .address = "fe80::1", .protocol = 1, .txt = &.{} }, .{ .address = "10.0.0.2", .protocol = 0, .txt = &.{ "a", "=", "b=c=d", "" } }, }; for (services) |s| { var e: Encoder = .init(gpa); defer e.deinit(); try e.int32(2); try e.int32(s.protocol); try e.string("Kitchen"); try e.string("_sendspin._tcp"); try e.string("local"); try e.string("kitchen.local"); try e.int32(s.protocol); try e.string(s.address); try e.uint16(8928); try wire.encodeTxt(&e, s.txt); try e.uint32(wire.lookup_result_local); try seeds.append(gpa, try seed(gpa, 1, e.bytes())); } for ([_]struct { i32, []const u8 }{ .{ 2, "" }, .{ 3, "" }, .{ 4, "Local name collision" } }) |st| { var e: Encoder = .init(gpa); defer e.deinit(); try e.int32(st[0]); try e.string(st[1]); try seeds.append(gpa, try seed(gpa, 2, e.bytes())); } for ([_][2][]const u8{ .{ ":1.8", "" }, .{ "", ":1.42" } }) |owners| { var e: Encoder = .init(gpa); defer e.deinit(); try e.string("org.freedesktop.Avahi"); try e.string(owners[0]); try e.string(owners[1]); try seeds.append(gpa, try seed(gpa, 3, e.bytes())); } var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); const w = &out.writer; try w.writeAll("// Generated by tools/fuzz_corpus.zig; see there.\n\npub const signals = [_][]const u8{\n"); for (seeds.items) |s| { try w.writeAll(" \""); for (s) |b| try w.print("\\x{x:0>2}", .{b}); try w.writeAll("\",\n"); } try w.writeAll("};\n"); // The same seeds as `Smith` reads an input: a four-byte little-endian // length before each, which is what `std.testing.fuzz` takes as its // corpus. try w.writeAll("\npub const signal_inputs = [_][]const u8{\n"); for (seeds.items) |s| { try w.writeAll(" \""); var length: [4]u8 = undefined; std.mem.writeInt(u32, &length, @intCast(s.len), .little); for (length) |b| try w.print("\\x{x:0>2}", .{b}); for (s) |b| try w.print("\\x{x:0>2}", .{b}); try w.writeAll("\",\n"); } try w.writeAll("};\n"); try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = out.written() }); } fn seed(gpa: std.mem.Allocator, which: u8, body: []const u8) ![]u8 { const s = try gpa.alloc(u8, 1 + body.len); s[0] = which; @memcpy(s[1..], body); return s; }