// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What the client must do with whatever the Avahi daemon's signals carry. //! //! The daemon is trusted, but what it relays is not: a service's name, host //! and TXT come from whatever answered on the network. So each signal body //! the client decodes -- an item found or lost, a resolved service, a //! state, the daemon's owner changing -- is fed any bytes, and whatever //! arrives, decoding returns, reads only inside the body, and what it keeps //! of a TXT record is bounded. //! //! The target is an ordinary test as well as a fuzz target. Without //! `--fuzz` it runs the seeds `tools/fuzz_corpus.zig` makes -- a body of //! every signal -- so `zig build test` checks the same property on real //! ones. `zig build fuzz-run` mutates those seeds with the loop in //! `tools/fuzz.zig`, since Zig 0.16.0's own fuzzer cannot run here: that //! file says why. const builtin = @import("builtin"); const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const testing = std.testing; const Smith = std.testing.Smith; const avahi = @import("avahi"); const corpus = @import("fuzz_corpus"); const wire = avahi.wire; /// The allocator and the `Io` the target runs against: the testing ones /// under `zig build test`, and `tools/fuzz.zig`'s own otherwise. pub var backing: Allocator = if (builtin.is_test) testing.allocator else undefined; pub var io: Io = if (builtin.is_test) testing.io else undefined; /// Where a body lies, which every string decoded from it must lie inside. const Span = struct { lo: usize, hi: usize, fn of(outer: []const u8) Span { const lo = @intFromPtr(outer.ptr); return .{ .lo = lo, .hi = lo + outer.len }; } fn holds(self: Span, inner: []const u8) bool { if (inner.len == 0) return true; const i = @intFromPtr(inner.ptr); return i >= self.lo and i <= self.hi and inner.len <= self.hi - i; } }; /// The first byte says which signal the rest is the body of. fn signalProperty(input: []const u8) !void { if (input.len == 0) return; const body = input[1..]; const span: Span = .of(body); var d: wire.Decoder = .{ .data = body, .endian = if (input[0] & 0x80 != 0) .big else .little }; switch (input[0] % 4) { 0 => { const item = wire.Item.decode(&d) catch return; for ([_][]const u8{ item.name, item.type, item.domain }) |s| try testing.expect(span.holds(s)); }, 1 => { const r = wire.Resolved.decode(&d) catch return; for ([_][]const u8{ r.name, r.type, r.domain, r.host, r.address }) |s| try testing.expect(span.holds(s)); try testing.expect(r.txt().len <= wire.max_txt); for (r.txt()) |t| try testing.expect(span.holds(t)); _ = r.txtValue("path"); }, 2 => { const s = wire.StateChanged.decode(&d) catch return; try testing.expect(span.holds(s.@"error")); }, else => { const n = wire.NameOwnerChanged.decode(&d) catch return; for ([_][]const u8{ n.name, n.old_owner, n.new_owner }) |s| try testing.expect(span.holds(s)); }, } } test "fuzz the daemon's signals" { for (corpus.signals) |seed| try signalProperty(seed); try testing.fuzz({}, fuzzSignals, .{}); } fn fuzzSignals(_: void, smith: *Smith) !void { var buffer: [4096]u8 = undefined; try signalProperty(buffer[0..smith.slice(&buffer)]); } test "a TXT key found without regard to case, and not by prefix" { const txt = [_][]const u8{ "pathology=no", "PATH=/sendspin", "=x", "empty" }; try testing.expectEqualStrings("/sendspin", avahi.findTxt(&txt, "path").?); try testing.expectEqualStrings("", avahi.findTxt(&txt, "EMPTY").?); try testing.expect(avahi.findTxt(&txt, "") == null); } /// A target, as `tools/fuzz.zig` drives it. pub const Target = struct { name: []const u8, run: *const fn (input: []const u8) anyerror!void, /// Inputs worth mutating, which stand in for the coverage feedback a /// real fuzzer would have. corpus: []const []const u8, /// The buffer this target reads its input into. `Smith.slice` yields /// an *empty* slice for a length larger than that, not a truncated /// one, so a generator that does not know it feeds the target nothing. content_max: usize, }; fn Driven(comptime one: fn (void, *Smith) anyerror!void) type { return struct { fn run(input: []const u8) anyerror!void { var smith: Smith = .{ .in = input }; return one({}, &smith); } }; } pub const all = [_]Target{ .{ .name = "signals", .run = Driven(fuzzSignals).run, .corpus = &corpus.signals, .content_max = 4096 }, };