// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Tests for the parts that need Unicode data: the `uri_unicode` module, and //! the Punycode and IRI text form in `uri` that IDNA is built on. const std = @import("std"); const uri = @import("uri"); const unicode = @import("root.zig"); const vectors = @import("vectors.zig"); const Uri = uri.Uri; const idna = unicode.idna; const normalization = unicode.normalization; const punycode = uri.punycode; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectEqualSlices = std.testing.expectEqualSlices; const expectEqualStrings = std.testing.expectEqualStrings; const expectError = std.testing.expectError; // -- punycode -------------------------------------------------------------- // RFC 3492 §7.1, verbatim. test "punycode: RFC 3492 sample strings" { const a = std.testing.allocator; const Case = struct { []const u21, []const u8 }; for ([_]Case{ .{ &.{ 0x0644, 0x064A, 0x0647, 0x0645, 0x0627, 0x0628, 0x062A, 0x0643, 0x0644, 0x0645, 0x0648, 0x0634, 0x0639, 0x0631, 0x0628, 0x064A, 0x061F }, "egbpdaj6bu4bxfgehfvwxn" }, .{ &.{ 0x4ED6, 0x4EEC, 0x4E3A, 0x4EC0, 0x4E48, 0x4E0D, 0x8BF4, 0x4E2D, 0x6587 }, "ihqwcrb4cv8a8dqg056pqjye" }, .{ &.{ 0x4ED6, 0x5011, 0x7232, 0x4EC0, 0x9EBD, 0x4E0D, 0x8AAA, 0x4E2D, 0x6587 }, "ihqwctvzc91f659drss3x8bo0yb" }, .{ &.{ 0x0050, 0x0072, 0x006F, 0x010D, 0x0070, 0x0072, 0x006F, 0x0073, 0x0074, 0x011B, 0x006E, 0x0065, 0x006D, 0x006C, 0x0075, 0x0076, 0x00ED, 0x010D, 0x0065, 0x0073, 0x006B, 0x0079 }, "Proprostnemluvesky-uyb24dma41a" }, .{ &.{ 0x05DC, 0x05DE, 0x05D4, 0x05D4, 0x05DD, 0x05E4, 0x05E9, 0x05D5, 0x05D8, 0x05DC, 0x05D0, 0x05DE, 0x05D3, 0x05D1, 0x05E8, 0x05D9, 0x05DD, 0x05E2, 0x05D1, 0x05E8, 0x05D9, 0x05EA }, "4dbcagdahymbxekheh6e0a7fei0b" }, .{ &.{ 0x306A, 0x305C, 0x307F, 0x3093, 0x306A, 0x65E5, 0x672C, 0x8A9E, 0x3092, 0x8A71, 0x3057, 0x3066, 0x304F, 0x308C, 0x306A, 0x3044, 0x306E, 0x304B }, "n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa" }, // The "-> $1.00 <-" case, which exercises basic code points only. .{ &.{ 0x002D, 0x003E, 0x0020, 0x0024, 0x0031, 0x002E, 0x0030, 0x0030, 0x0020, 0x003C, 0x002D }, "-> $1.00 <--" }, }) |c| { const enc = try punycode.encode(a, c[0]); defer a.free(enc); try expectEqualStrings(c[1], enc); const dec = try punycode.decode(a, c[1]); defer a.free(dec); try expectEqualSlices(u21, c[0], dec); } } test "punycode: input past the Unicode range is refused" { const a = std.testing.allocator; try expectError(error.Overflow, punycode.decode(a, "999999999")); } // -- normalization --------------------------------------------------------- fn toCodePoints(a: std.mem.Allocator, s: []const u8) ![]u21 { var out: std.ArrayList(u21) = .empty; errdefer out.deinit(a); var it = (try std.unicode.Utf8View.init(s)).iterator(); while (it.nextCodepoint()) |cp| try out.append(a, cp); return out.toOwnedSlice(a); } test "NFC: Unicode conformance vectors" { const a = std.testing.allocator; for (vectors.nfc_cases) |c| { const src = try toCodePoints(a, c.source); defer a.free(src); const want = try toCodePoints(a, c.nfc); defer a.free(want); const got = try normalization.nfc(a, src); defer a.free(got); expectEqualSlices(u21, want, got) catch |err| { std.debug.print("\nNFC of {any}\n", .{src}); return err; }; } } test "NFC: composes and orders combining marks" { const a = std.testing.allocator; // a + combining acute composes to á. const composed = try normalization.nfc(a, &.{ 0x0061, 0x0301 }); defer a.free(composed); try expectEqualSlices(u21, &.{0x00E1}, composed); // Hangul composes arithmetically. const hangul = try normalization.nfc(a, &.{ 0x1100, 0x1161, 0x11A8 }); defer a.free(hangul); try expectEqualSlices(u21, &.{0xAC01}, hangul); } // -- IDNA ------------------------------------------------------------------ test "IDNA: Unicode conformance vectors" { const a = std.testing.allocator; // The vectors are generated with UseSTD3ASCIIRules, as UTS #46's test // file is. const opts: idna.Options = .{ .use_std3_ascii_rules = true }; for (vectors.idna_cases) |c| { if (idna.toUnicode(a, c.source, opts)) |got| { defer a.free(got); if (c.to_unicode_errors) { std.debug.print("\ntoUnicode({s}) should have failed, got '{s}'\n", .{ c.source, got }); return error.TestUnexpectedResult; } expectEqualStrings(c.to_unicode, got) catch |err| { std.debug.print("\ntoUnicode({s})\n", .{c.source}); return err; }; } else |err| { if (!c.to_unicode_errors) { std.debug.print("\ntoUnicode({s}) failed with {t}, wanted '{s}'\n", .{ c.source, err, c.to_unicode }); return error.TestUnexpectedResult; } } if (idna.toAscii(a, c.source, opts)) |got| { defer a.free(got); if (c.to_ascii_errors) { std.debug.print("\ntoAscii({s}) should have failed, got '{s}'\n", .{ c.source, got }); return error.TestUnexpectedResult; } expectEqualStrings(c.to_ascii, got) catch |err| { std.debug.print("\ntoAscii({s})\n", .{c.source}); return err; }; } else |err| { if (!c.to_ascii_errors) { std.debug.print("\ntoAscii({s}) failed with {t}, wanted '{s}'\n", .{ c.source, err, c.to_ascii }); return error.TestUnexpectedResult; } } } } test "IDNA: everyday names round-trip" { const a = std.testing.allocator; const Case = struct { []const u8, []const u8 }; for ([_]Case{ .{ "bücher.ch", "xn--bcher-kva.ch" }, .{ "Bücher.CH", "xn--bcher-kva.ch" }, // mapped, not rejected .{ "日本語.jp", "xn--wgv71a119e.jp" }, .{ "example.com", "example.com" }, // pure ASCII is untouched }) |c| { const ascii = try idna.toAscii(a, c[0], .{}); defer a.free(ascii); try expectEqualStrings(c[1], ascii); const back = try idna.toUnicode(a, ascii, .{}); defer a.free(back); const expected_unicode = try idna.toUnicode(a, c[0], .{}); defer a.free(expected_unicode); try expectEqualStrings(expected_unicode, back); } } test "IDNA: transitional processing is off by default" { const a = std.testing.allocator; // Non-transitional keeps the sharp s, which is what browsers do now. const nt = try idna.toAscii(a, "faß.de", .{}); defer a.free(nt); try expectEqualStrings("xn--fa-hia.de", nt); // Transitional folds it to "ss", giving a different name entirely. const t = try idna.toAscii(a, "faß.de", .{ .transitional = true }); defer a.free(t); try expectEqualStrings("fass.de", t); } test "IDNA: rejects what UTS #46 rejects" { const a = std.testing.allocator; try expectError(error.EmptyLabel, idna.toAscii(a, "a..c", .{})); try expectError(error.EmptyLabel, idna.toAscii(a, "", .{})); // An ACE label that decodes to pure ASCII would never re-encode to itself. try expectError(error.InvalidPunycode, idna.toAscii(a, "xn--ascii-", .{})); // STD3 is off by default, so this is accepted unless asked for. const ok = try idna.toAscii(a, "a_b.example", .{}); defer a.free(ok); try expectEqualStrings("a_b.example", ok); try expectError( error.DisallowedCodePoint, idna.toAscii(a, "a_b.example", .{ .use_std3_ascii_rules = true }), ); } // -- IRI and URI forms ----------------------------------------------------- test "toUri and toIri convert the host" { const a = std.testing.allocator; const u = try Uri.parse(a, "https://Bücher.example/straße?q=café#ü"); defer u.deinit(); const as_uri = try unicode.toUri(a, u); defer as_uri.deinit(); const ascii = try as_uri.toText(a, .{}); defer a.free(ascii); try expectEqualStrings( "https://xn--bcher-kva.example/stra%C3%9Fe?q=caf%C3%A9#%C3%BC", ascii, ); // Back to Unicode, and written as an IRI, nothing stays escaped. const as_iri = try unicode.toIri(a, as_uri); defer as_iri.deinit(); const iri = try as_iri.toText(a, .{ .form = .iri }); defer a.free(iri); try expectEqualStrings("https://bücher.example/straße?q=café#ü", iri); } test "IRI form escapes only what RFC 3987 requires" { const a = std.testing.allocator; const u = try Uri.parse(a, "http://example.com/a%20b/caf%C3%A9?x=%C3%A9#%C3%BC"); defer u.deinit(); const iri = try u.toText(a, .{ .form = .iri }); defer a.free(iri); // A space is not a ucschar, so it stays escaped; the accented letters do not. try expectEqualStrings("http://example.com/a%20b/café?x=é#ü", iri); // The URI form is unchanged from the default behaviour. const as_uri = try u.toText(a, .{}); defer a.free(as_uri); try expectEqualStrings("http://example.com/a%20b/caf%C3%A9?x=%C3%A9#%C3%BC", as_uri); } test "IRI form still escapes bytes that are not valid UTF-8" { const a = std.testing.allocator; const u = try Uri.parse(a, "http://example.com/%FF%FE"); defer u.deinit(); const iri = try u.toText(a, .{ .form = .iri }); defer a.free(iri); try expectEqualStrings("http://example.com/%FF%FE", iri); } test "toUri leaves address literals alone" { const a = std.testing.allocator; for ([_][]const u8{ "http://[::1]/", "http://192.0.2.16/" }) |text| { const u = try Uri.parse(a, text); defer u.deinit(); const converted = try unicode.toUri(a, u); defer converted.deinit(); const out = try converted.toText(a, .{}); defer a.free(out); try expectEqualStrings(text, out); } }