Something went wrong. Try again.
A URI parser and formatter for Zig, modelled on Python's hyperlink package, with LDAP URLs, file: URIs and sdns:// DNS stamps beside it.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Fuzz tests for the parts that need Unicode tables, compiled only with//! `-Dunicode=true`. See `fuzz.zig` for how these are run.//!//! The properties here are the ones UTS #46 and UAX #15 state about their own//! algorithms: normalization is idempotent, and both directions of IDNA are//! fixed points that agree with each other.
const std = @import("std");
/// For `fuzz.backing`, which is where every target here gets its memory; that/// file says why it is a variable rather than `std.testing.allocator`.const fuzz = @import("fuzz.zig");
const idna = @import("idna.zig");const normalization = @import("normalization.zig");
const Smith = std.testing.Smith;const Weight = Smith.Weight;
const expect = std.testing.expect;const expectEqual = std.testing.expectEqual;const expectEqualSlices = std.testing.expectEqualSlices;const expectEqualStrings = std.testing.expectEqualStrings;
/// Code points, weighted towards the ones the IDNA and normalization rules/// single out. The ranges have to be disjoint, and they deliberately leave out/// the surrogates, which are not scalar values and cannot appear in UTF-8.const code_points: []const Weight = &.{ .rangeAtMost(u21, 0x0000, 0x001f, 1), .rangeAtMost(u21, 0x0020, 0x007e, 5), // ASCII, including `.` and `-` .rangeAtMost(u21, 0x007f, 0x009f, 1), .rangeAtMost(u21, 0x00a0, 0x02ff, 3), // Latin-1 and friends, which map .rangeAtMost(u21, 0x0300, 0x036f, 4), // combining marks, for reordering .rangeAtMost(u21, 0x0370, 0x05cf, 1), .rangeAtMost(u21, 0x05d0, 0x05ea, 3), // Hebrew, for the Bidi rule .rangeAtMost(u21, 0x05eb, 0x05ff, 1), .rangeAtMost(u21, 0x0600, 0x06ff, 3), // Arabic, for Bidi and CONTEXTO .rangeAtMost(u21, 0x0700, 0x10ff, 1), .rangeAtMost(u21, 0x1100, 0x11ff, 3), // Hangul jamo, for composition .rangeAtMost(u21, 0x1200, 0x200b, 1), .rangeAtMost(u21, 0x200c, 0x200d, 4), // ZWNJ and ZWJ, for CONTEXTJ .rangeAtMost(u21, 0x200e, 0xabff, 1), .rangeAtMost(u21, 0xac00, 0xd7a3, 3), // Hangul syllables, which decompose .rangeAtMost(u21, 0xd7a4, 0xd7ff, 1), // 0xd800..0xdfff are surrogates and are skipped. .rangeAtMost(u21, 0xe000, 0xf8ff, 1), .rangeAtMost(u21, 0xf900, 0xfaff, 2), // CJK compatibility, which maps .rangeAtMost(u21, 0xfb00, 0xffff, 1), .rangeAtMost(u21, 0x10000, 0x10ffff, 1),};
const max_cps = 12;const max_name = 64;
fn genCodePoints(s: *Smith, out: *[max_cps]u21) []const u21 { const n = s.index(max_cps + 1); for (out[0..n]) |*cp| cp.* = s.valueWeighted(u21, code_points); return out[0..n];}
/// Build a domain name out of generated code points, with the ASCII that IDNA/// gives special meaning -- the label separator, the hyphen, and the `xn--`/// prefix -- mixed in often enough to reach the label-shape rules.fn genName(s: *Smith, buf: *[max_name]u8) []const u8 { const specials = ".-xn--"; var len: usize = 0; const n = s.index(max_cps + 1); for (0..n) |_| { if (s.boolWeighted(2, 1)) { if (len == buf.len) break; buf[len] = specials[s.index(specials.len)]; len += 1; } else { var utf8: [4]u8 = undefined; const w = std.unicode.utf8Encode(s.valueWeighted(u21, code_points), &utf8) catch continue; if (len + w > buf.len) break; @memcpy(buf[len..][0..w], utf8[0..w]); len += w; } } return buf[0..len];}
test "fuzz: NFC is idempotent" { try std.testing.fuzz({}, fuzzNfc, .{});}
fn fuzzNfc(_: void, s: *Smith) anyerror!void { const alloc = fuzz.backing;
var storage: [max_cps]u21 = undefined; const input = genCodePoints(s, &storage);
const once = try normalization.nfc(alloc, input); defer alloc.free(once);
const twice = try normalization.nfc(alloc, once); defer alloc.free(twice);
try expectEqualSlices(u21, once, twice); try expect(try normalization.isNfc(alloc, once));}
test "fuzz: IDNA conversion is a fixed point in both directions" { try std.testing.fuzz({}, fuzzIdna, .{});}
fn fuzzIdna(_: void, s: *Smith) anyerror!void { const alloc = fuzz.backing;
var buf: [max_name]u8 = undefined; const name = genName(s, &buf);
// Every error is a documented rejection of a name that cannot be // converted; only what converts has anything to prove. const ascii = idna.toAscii(alloc, name, .{}) catch return; defer alloc.free(ascii);
for (ascii) |c| { expect(c < 0x80) catch |err| { std.debug.print("\ntoAscii({s}) is not ASCII: {s}\n", .{ name, ascii }); return err; }; }
const again = idna.toAscii(alloc, ascii, .{}) catch |err| { std.debug.print("\ntoAscii({s}) rejects its own output {s}\n", .{ name, ascii }); return err; }; defer alloc.free(again); expectEqualStrings(ascii, again) catch |err| { std.debug.print("\ntoAscii is not a fixed point for {s}\n", .{name}); return err; };
const unicode = idna.toUnicode(alloc, name, .{}) catch return; defer alloc.free(unicode);
const unicode_again = idna.toUnicode(alloc, unicode, .{}) catch |err| { std.debug.print("\ntoUnicode({s}) rejects its own output {s}\n", .{ name, unicode }); return err; }; defer alloc.free(unicode_again); expectEqualStrings(unicode, unicode_again) catch |err| { std.debug.print("\ntoUnicode is not a fixed point for {s}\n", .{name}); return err; };}
test "fuzz: the ASCII and Unicode forms of a name agree" { try std.testing.fuzz({}, fuzzIdnaForms, .{});}
// UTS #46 §4.3: the Unicode form of a name is what the ASCII form decodes// to, so encoding it again has to give that same ASCII form back.fn fuzzIdnaForms(_: void, s: *Smith) anyerror!void { const alloc = fuzz.backing;
var buf: [max_name]u8 = undefined; const name = genName(s, &buf);
const ascii = idna.toAscii(alloc, name, .{}) catch return; defer alloc.free(ascii);
const unicode = idna.toUnicode(alloc, ascii, .{}) catch |err| { std.debug.print("\ntoUnicode rejects the ASCII form of {s}: {s}\n", .{ name, ascii }); return err; }; defer alloc.free(unicode);
const round_tripped = idna.toAscii(alloc, unicode, .{}) catch |err| { std.debug.print( "\ntoAscii rejects the Unicode form of {s}: {s} -> {s}\n", .{ name, ascii, unicode }, ); return err; }; defer alloc.free(round_tripped);
expectEqualStrings(ascii, round_tripped) catch |err| { std.debug.print("\n{s} does not round-trip through its Unicode form\n", .{name}); return err; };}
test "fuzz: IDNA rejects rather than misbehaves" { try std.testing.fuzz({}, fuzzIdnaReject, .{});}
// Arbitrary bytes, most of which are not well-formed UTF-8 at all.fn fuzzIdnaReject(_: void, s: *Smith) anyerror!void { const alloc = fuzz.backing;
var buf: [max_name]u8 = undefined; const name = buf[0..s.slice(&buf)];
const opts: idna.Options = .{ .transitional = s.value(bool), .use_std3_ascii_rules = s.value(bool), .check_hyphens = s.value(bool), .check_joiners = s.value(bool), .check_bidi = s.value(bool), .verify_dns_length = s.value(bool), };
const ascii = idna.toAscii(alloc, name, opts) catch |err| switch (err) { error.OutOfMemory => return err, // Everything else is a documented rejection. else => return, }; defer alloc.free(ascii);
for (ascii) |c| try expect(c < 0x80);}
/// What `tools/fuzz.zig` runs, appended to `fuzz.zig`'s own table when this/// file is compiled at all.////// Every one of these asks for a count and then for code points, so none reads/// a length-prefixed chunk; the names are what the driver's `--target` takes.pub const targets = [_]fuzz.Target{ .{ .name = "nfc", .run = Driven(fuzzNfc).run, .slices = 0 }, .{ .name = "idna", .run = Driven(fuzzIdna).run, .slices = 0 }, .{ .name = "idna-forms", .run = Driven(fuzzIdnaForms).run, .slices = 0 }, .{ .name = "idna-reject", .run = Driven(fuzzIdnaReject).run, .slices = 0 },};
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); } };}