punycode/fuzz_test.mbt1///|2/// Property-based fuzz tests for Punycode encoding/decoding3/// Tests RFC 3492 invariants45///|6/// Number of iterations for fuzz tests7let fuzz_iterations : Int32 = 100089///|10/// Seed for reproducibility (must be exactly 32 bytes for chacha8)11let fuzz_seed : Bytes = b"punycode-fuzz-seed-testing!!32!!"1213// =============================================================================14// Property 1: Roundtrip - decode(encode(s)) == s15// =============================================================================1617///|18test "fuzz: punycode roundtrip with random unicode" {19 let rng = @random.Rand::chacha8(seed=fuzz_seed)20 for _ in 0..<fuzz_iterations {21 let s = @fuzz.random_unicode_string(rng, max_len=30)22 try {23 let encoded = @punycode.encode(s)24 @punycode.decode(encoded)25 } catch {26 _ => () // Some strings may cause overflow, which is acceptable27 } noraise {28 decoded => assert_eq(decoded, s)29 }30 }31}3233///|34test "fuzz: punycode roundtrip with hangul" {35 let rng = @random.Rand::chacha8(seed=fuzz_seed)36 for _ in 0..<500 {37 let s = @fuzz.random_hangul_string(rng, max_len=20)38 try {39 let encoded = @punycode.encode(s)40 @punycode.decode(encoded)41 } catch {42 _ => () // Some strings may cause overflow43 } noraise {44 decoded => assert_eq(decoded, s)45 }46 }47}4849///|50test "fuzz: punycode roundtrip with latin extended" {51 let rng = @random.Rand::chacha8(seed=fuzz_seed)52 for _ in 0..<fuzz_iterations {53 // Generate strings with mostly Latin Extended characters54 let sb = StringBuilder()55 let len = rng.int(limit=30) + 156 for _ in 0..<len {57 let cp = @fuzz.random_codepoint(rng, LatinExtended)58 sb.write_char(cp)59 }60 let s = sb.to_string()61 try {62 let encoded = @punycode.encode(s)63 @punycode.decode(encoded)64 } catch {65 _ => ()66 } noraise {67 decoded => assert_eq(decoded, s)68 }69 }70}7172// =============================================================================73// Property 2: Encode produces ASCII-only output74// =============================================================================7576///|77fn is_ascii_string(s : String) -> Bool {78 for c in s {79 if c.to_int() >= 0x80 {80 return false81 }82 }83 true84}8586///|87test "fuzz: punycode encode produces ASCII" {88 let rng = @random.Rand::chacha8(seed=fuzz_seed)89 for _ in 0..<fuzz_iterations {90 let s = @fuzz.random_unicode_string(rng, max_len=30)91 try @punycode.encode(s) catch {92 _ => () // Overflow is acceptable93 } noraise {94 encoded => assert_true(is_ascii_string(encoded))95 }96 }97}9899// =============================================================================100// Property 3: Pure ASCII strings encode with trailing delimiter101// =============================================================================102103///|104test "fuzz: pure ASCII encodes with delimiter" {105 let rng = @random.Rand::chacha8(seed=fuzz_seed)106 for _ in 0..<500 {107 // Generate pure ASCII string108 let sb = StringBuilder()109 let len = rng.int(limit=20) + 1110 for _ in 0..<len {111 let cp = @fuzz.random_codepoint(rng, Ascii)112 sb.write_char(cp)113 }114 let s = sb.to_string()115 try @punycode.encode(s) catch {116 _ => ()117 } noraise {118 encoded =>119 // Pure ASCII input should produce: original + "-"120 assert_eq(encoded, s + "-")121 }122 }123}124125// =============================================================================126// Property 4: Idempotence of decode on valid Punycode127// =============================================================================128129///|130test "fuzz: valid punycode decode is stable" {131 let rng = @random.Rand::chacha8(seed=fuzz_seed)132 for _ in 0..<fuzz_iterations {133 let s = @fuzz.random_unicode_string(rng, max_len=20)134 try {135 let encoded = @punycode.encode(s)136 // Decode twice should give same result137 let decoded1 = @punycode.decode(encoded)138 let encoded2 = @punycode.encode(decoded1)139 let decoded2 = @punycode.decode(encoded2)140 (decoded1, decoded2)141 } catch {142 _ => ()143 } noraise {144 (d1, d2) => assert_eq(d1, d2)145 }146 }147}148149// =============================================================================150// Property 5: Mixed ASCII and non-ASCII151// =============================================================================152153///|154test "fuzz: mixed ascii and unicode roundtrip" {155 let rng = @random.Rand::chacha8(seed=fuzz_seed)156 for _ in 0..<fuzz_iterations {157 // Generate string with mix of ASCII and non-ASCII158 let sb = StringBuilder()159 let len = rng.int(limit=20) + 1160 for _ in 0..<len {161 let use_ascii = rng.int(limit=2) == 0162 let cp = if use_ascii {163 @fuzz.random_codepoint(rng, Ascii)164 } else {165 @fuzz.random_codepoint(rng, LatinExtended)166 }167 sb.write_char(cp)168 }169 let s = sb.to_string()170 try {171 let encoded = @punycode.encode(s)172 @punycode.decode(encoded)173 } catch {174 _ => ()175 } noraise {176 decoded => assert_eq(decoded, s)177 }178 }179}180181// =============================================================================182// Boundary cases183// =============================================================================184185///|186test "fuzz: empty string" {187 let encoded = @punycode.encode("")188 let decoded = @punycode.decode(encoded)189 assert_eq(decoded, "")190}191192///|193test "fuzz: single character roundtrip" {194 let rng = @random.Rand::chacha8(seed=fuzz_seed)195 for _ in 0..<500 {196 let cp = @fuzz.random_codepoint(rng, LatinExtended)197 let s = cp.to_string()198 try {199 let encoded = @punycode.encode(s)200 @punycode.decode(encoded)201 } catch {202 _ => ()203 } noraise {204 decoded => assert_eq(decoded, s)205 }206 }207}208209// =============================================================================210// Property: decoding has a single preimage211// =============================================================================212213///|214/// Whether `input` is the encoding of its own decoding, for lowercase input215/// that decodes: only a delimiter with nothing before it and no other216/// delimiter after it is not, as an encoding without basic code points has217/// no delimiter. IDNA relies on this to skip re-encoding A-labels.218fn is_canonical_encoding(input : String) -> Bool {219 !(input.has_prefix("-") && !input[1:].contains("-"))220}221222///|223test "fuzz: lowercase input that decodes encodes back to itself" {224 let rng = @random.Rand::chacha8(seed=fuzz_seed)225 let alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-----"226 let mut decoded_count = 0227 for _ in 0..<20000 {228 let sb = StringBuilder()229 let len = rng.int(limit=12) + 1230 for _ in 0..<len {231 sb.write_char(232 alphabet.get_char(rng.int(limit=alphabet.length())).unwrap(),233 )234 }235 let input = sb.to_string()236 let decoded = @punycode.decode(input) catch { _ => continue }237 decoded_count += 1238 let encoded = @punycode.encode(decoded)239 assert_eq(encoded == input, is_canonical_encoding(input))240 }241 // Most random inputs decode, so the property is not checked vacuously.242 assert_true(decoded_count > 5000)243}244
///|
/// Property-based fuzz tests for Punycode encoding/decoding
/// Tests RFC 3492 invariants
///|
/// Number of iterations for fuzz tests
let fuzz_iterations : Int32 = 1000
///|
/// Seed for reproducibility (must be exactly 32 bytes for chacha8)
let fuzz_seed : Bytes = b"punycode-fuzz-seed-testing!!32!!"
// =============================================================================
// Property 1: Roundtrip - decode(encode(s)) == s
// =============================================================================
///|
test "fuzz: punycode roundtrip with random unicode" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
for _ in 0..<fuzz_iterations {
let s = @fuzz.random_unicode_string(rng, max_len=30)
try {
let encoded = @punycode.encode(s)
@punycode.decode(encoded)
} catch {
_ => () // Some strings may cause overflow, which is acceptable
} noraise {
decoded => assert_eq(decoded, s)
}
}
}
///|
test "fuzz: punycode roundtrip with hangul" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
for _ in 0..<500 {
let s = @fuzz.random_hangul_string(rng, max_len=20)
try {
let encoded = @punycode.encode(s)
@punycode.decode(encoded)
} catch {
_ => () // Some strings may cause overflow
} noraise {
decoded => assert_eq(decoded, s)
}
}
}
///|
test "fuzz: punycode roundtrip with latin extended" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
for _ in 0..<fuzz_iterations {
// Generate strings with mostly Latin Extended characters
let sb = StringBuilder()
let len = rng.int(limit=30) + 1
for _ in 0..<len {
let cp = @fuzz.random_codepoint(rng, LatinExtended)
sb.write_char(cp)
}
let s = sb.to_string()
try {
let encoded = @punycode.encode(s)
@punycode.decode(encoded)
} catch {
_ => ()
} noraise {
decoded => assert_eq(decoded, s)
}
}
}
// =============================================================================
// Property 2: Encode produces ASCII-only output
// =============================================================================
///|
fn is_ascii_string(s : String) -> Bool {
for c in s {
if c.to_int() >= 0x80 {
return false
}
}
true
}
///|
test "fuzz: punycode encode produces ASCII" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
for _ in 0..<fuzz_iterations {
let s = @fuzz.random_unicode_string(rng, max_len=30)
try @punycode.encode(s) catch {
_ => () // Overflow is acceptable
} noraise {
encoded => assert_true(is_ascii_string(encoded))
}
}
}
// =============================================================================
// Property 3: Pure ASCII strings encode with trailing delimiter
// =============================================================================
///|
test "fuzz: pure ASCII encodes with delimiter" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
for _ in 0..<500 {
// Generate pure ASCII string
let sb = StringBuilder()
let len = rng.int(limit=20) + 1
for _ in 0..<len {
let cp = @fuzz.random_codepoint(rng, Ascii)
sb.write_char(cp)
}
let s = sb.to_string()
try @punycode.encode(s) catch {
_ => ()
} noraise {
encoded =>
// Pure ASCII input should produce: original + "-"
assert_eq(encoded, s + "-")
}
}
}
// =============================================================================
// Property 4: Idempotence of decode on valid Punycode
// =============================================================================
///|
test "fuzz: valid punycode decode is stable" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
for _ in 0..<fuzz_iterations {
let s = @fuzz.random_unicode_string(rng, max_len=20)
try {
let encoded = @punycode.encode(s)
// Decode twice should give same result
let decoded1 = @punycode.decode(encoded)
let encoded2 = @punycode.encode(decoded1)
let decoded2 = @punycode.decode(encoded2)
(decoded1, decoded2)
} catch {
_ => ()
} noraise {
(d1, d2) => assert_eq(d1, d2)
}
}
}
// =============================================================================
// Property 5: Mixed ASCII and non-ASCII
// =============================================================================
///|
test "fuzz: mixed ascii and unicode roundtrip" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
for _ in 0..<fuzz_iterations {
// Generate string with mix of ASCII and non-ASCII
let sb = StringBuilder()
let len = rng.int(limit=20) + 1
for _ in 0..<len {
let use_ascii = rng.int(limit=2) == 0
let cp = if use_ascii {
@fuzz.random_codepoint(rng, Ascii)
} else {
@fuzz.random_codepoint(rng, LatinExtended)
}
sb.write_char(cp)
}
let s = sb.to_string()
try {
let encoded = @punycode.encode(s)
@punycode.decode(encoded)
} catch {
_ => ()
} noraise {
decoded => assert_eq(decoded, s)
}
}
}
// =============================================================================
// Boundary cases
// =============================================================================
///|
test "fuzz: empty string" {
let encoded = @punycode.encode("")
let decoded = @punycode.decode(encoded)
assert_eq(decoded, "")
}
///|
test "fuzz: single character roundtrip" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
for _ in 0..<500 {
let cp = @fuzz.random_codepoint(rng, LatinExtended)
let s = cp.to_string()
try {
let encoded = @punycode.encode(s)
@punycode.decode(encoded)
} catch {
_ => ()
} noraise {
decoded => assert_eq(decoded, s)
}
}
}
// =============================================================================
// Property: decoding has a single preimage
// =============================================================================
///|
/// Whether `input` is the encoding of its own decoding, for lowercase input
/// that decodes: only a delimiter with nothing before it and no other
/// delimiter after it is not, as an encoding without basic code points has
/// no delimiter. IDNA relies on this to skip re-encoding A-labels.
fn is_canonical_encoding(input : String) -> Bool {
!(input.has_prefix("-") && !input[1:].contains("-"))
}
///|
test "fuzz: lowercase input that decodes encodes back to itself" {
let rng = @random.Rand::chacha8(seed=fuzz_seed)
let alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-----"
let mut decoded_count = 0
for _ in 0..<20000 {
let sb = StringBuilder()
let len = rng.int(limit=12) + 1
for _ in 0..<len {
sb.write_char(
alphabet.get_char(rng.int(limit=alphabet.length())).unwrap(),
)
}
let input = sb.to_string()
let decoded = @punycode.decode(input) catch { _ => continue }
decoded_count += 1
let encoded = @punycode.encode(decoded)
assert_eq(encoded == input, is_canonical_encoding(input))
}
// Most random inputs decode, so the property is not checked vacuously.
assert_true(decoded_count > 5000)
}