unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

punycode/fuzz_test.mbt
1///|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