unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

idna/fuzz_test.mbt
1///|2/// Property-based fuzz tests for IDNA processing3/// Tests UTS #46 invariants45///|6/// Number of iterations for fuzz tests7let fuzz_iterations : Int32 = 5000089///|10/// Seed for reproducibility (must be exactly 32 bytes for chacha8)11let fuzz_seed : Bytes = b"idna-fuzz-seed-for-testing!32!!!"1213// =============================================================================14// Property 1: to_ascii produces ASCII-only output15// =============================================================================1617///|18test "fuzz: to_ascii produces ASCII" {19  let rng = @random.Rand::chacha8(seed=fuzz_seed)20  for _ in 0..<fuzz_iterations {21    let domain = @idna_fuzz.random_domain(rng)22    try @idna.to_ascii(domain, check_bidi=false, check_joiners=false) catch {23      _ => () // Some domains may be invalid24    } noraise {25      ascii => assert_true(@idna_fuzz.is_ascii_string(ascii))26    }27  }28}2930// =============================================================================31// Property 2: Pure ASCII domains pass through32// =============================================================================3334///|35test "fuzz: pure ASCII domain passthrough" {36  let rng = @random.Rand::chacha8(seed=fuzz_seed)37  for _ in 0..<fuzz_iterations {38    // Generate pure ASCII domain39    let num_labels = rng.int(limit=3) + 140    let domain = [41      for _ in 0..<num_labels => @idna_fuzz.random_ascii_label(rng)42    ].join(".")43    try44      @idna.to_ascii(45        domain,46        use_std3_ascii_rules=false,47        check_hyphens=false,48        check_bidi=false,49        check_joiners=false,50      )51    catch {52      _ => ()53    } noraise {54      ascii =>55        // ASCII domain should lowercase but otherwise stay similar56        assert_eq(ascii, domain.to_lower())57    }58  }59}6061// =============================================================================62// Property 3: Roundtrip for valid domains63// =============================================================================6465///|66test "fuzz: idna roundtrip" {67  let rng = @random.Rand::chacha8(seed=fuzz_seed)68  for _ in 0..<fuzz_iterations {69    let domain = @idna_fuzz.random_domain(rng)70    try {71      let ascii = @idna.to_ascii(72        domain,73        check_bidi=false,74        check_joiners=false,75        verify_dns_length=false,76      )77      let unicode = @idna.to_unicode(78        ascii,79        check_bidi=false,80        check_joiners=false,81      )82      let ascii2 = @idna.to_ascii(83        unicode,84        check_bidi=false,85        check_joiners=false,86        verify_dns_length=false,87      )88      (ascii, ascii2)89    } catch {90      _ => ()91    } noraise {92      (ascii, ascii2) => assert_eq(ascii, ascii2)93    }94  }95}9697// =============================================================================98// Property 4: to_unicode is idempotent99// =============================================================================100101///|102test "fuzz: to_unicode idempotence" {103  let rng = @random.Rand::chacha8(seed=fuzz_seed)104  for _ in 0..<fuzz_iterations {105    let domain = @idna_fuzz.random_domain(rng)106    try {107      let unicode1 = @idna.to_unicode(108        domain,109        check_bidi=false,110        check_joiners=false,111      )112      let unicode2 = @idna.to_unicode(113        unicode1,114        check_bidi=false,115        check_joiners=false,116      )117      (unicode1, unicode2)118    } catch {119      _ => ()120    } noraise {121      (u1, u2) => assert_eq(u1, u2)122    }123  }124}125126// =============================================================================127// Property 5: to_ascii produces pure ASCII for all Unicode input128// =============================================================================129130///|131test "fuzz: to_ascii produces pure ASCII for any Unicode input" {132  let rng = @random.Rand::chacha8(seed=fuzz_seed)133  for _ in 0..<fuzz_iterations {134    // Generate label with random Unicode code points (full range U+0000 to U+10FFFF)135    let sb = StringBuilder()136    let len = rng.int(limit=10) + 1137    for _ in 0..<len {138      let cp = @fuzz.random_any_codepoint(rng)139      sb.write_char(cp)140    }141    let label = sb.to_string()142    try143      @idna.to_ascii(144        label,145        check_bidi=false,146        check_joiners=false,147        verify_dns_length=false,148      )149    catch {150      _ => () // Disallowed chars, bidi errors, etc. are expected151    } noraise {152      ascii =>153        // DETERMINISTIC ASSERTION:154        // to_ascii output must be pure ASCII (code points < 0x80)155        assert_true(@idna_fuzz.is_ascii_string(ascii))156    }157  }158}159160// =============================================================================161// Property 6: Hangul domain handling162// =============================================================================163164///|165test "fuzz: hangul domain roundtrip" {166  let rng = @random.Rand::chacha8(seed=fuzz_seed)167  for _ in 0..<300 {168    // Generate Hangul label169    let label = @fuzz.random_hangul_string(rng, max_len=10)170    try {171      let ascii = @idna.to_ascii(172        label,173        check_bidi=false,174        check_joiners=false,175        verify_dns_length=false,176      )177      let unicode = @idna.to_unicode(178        ascii,179        check_bidi=false,180        check_joiners=false,181      )182      (ascii, unicode)183    } catch {184      _ => ()185    } noraise {186      (ascii, _unicode) =>187        // Should have xn-- prefix188        assert_true(ascii.has_prefix("xn--"))189    }190  }191}192193// =============================================================================194// Boundary cases195// =============================================================================196197///|198test "fuzz: empty label handling" {199  // Empty domain should fail200  @test.assert_raise(() => @idna.to_ascii(""))201}202203///|204test "fuzz: single character domains" {205  let rng = @random.Rand::chacha8(seed=fuzz_seed)206  for _ in 0..<200 {207    let cp = @fuzz.random_codepoint(rng, Ascii)208    let s = cp.to_string()209    try210      @idna.to_ascii(211        s,212        use_std3_ascii_rules=false,213        check_hyphens=false,214        check_bidi=false,215        check_joiners=false,216      )217    catch {218      _ => ()219    } noraise {220      ascii => assert_true(@idna_fuzz.is_ascii_string(ascii))221    }222  }223}224225///|226test "fuzz: multiple dot handling" {227  let rng = @random.Rand::chacha8(seed=fuzz_seed)228  for _ in 0..<200 {229    // Generate domain with various separators230    let label1 = @idna_fuzz.random_ascii_label(rng)231    let label2 = @idna_fuzz.random_ascii_label(rng)232    let domain = "\{label1}.\{label2}"233    try234      @idna.to_ascii(235        domain,236        use_std3_ascii_rules=false,237        check_hyphens=false,238        check_bidi=false,239        check_joiners=false,240      )241    catch {242      _ => ()243    } noraise {244      ascii =>245        // Should preserve dot structure246        assert_true(ascii.contains("."))247    }248  }249}250251// =============================================================================252// Property 7: Bidi checking253// =============================================================================254255///|256test "fuzz: arabic rtl domains with bidi check" {257  let rng = @random.Rand::chacha8(seed=fuzz_seed)258  for _ in 0..<fuzz_iterations {259    let label = @idna_fuzz.random_arabic_label(rng)260    // We're mainly testing that the code path runs without panicking.261    // Both success and expected errors are acceptable.262    try263      @idna.to_ascii(264        label,265        check_bidi=true,266        check_joiners=false,267        verify_dns_length=false,268      )269    catch {270      _ => ()271    } noraise {272      ascii => assert_true(@idna_fuzz.is_ascii_string(ascii))273    }274  }275}276277///|278test "fuzz: hebrew rtl domains with bidi check" {279  let rng = @random.Rand::chacha8(seed=fuzz_seed)280  for _ in 0..<fuzz_iterations {281    let label = @idna_fuzz.random_hebrew_label(rng)282    try283      @idna.to_ascii(284        label,285        check_bidi=true,286        check_joiners=false,287        verify_dns_length=false,288      )289    catch {290      _ => ()291    } noraise {292      ascii => assert_true(@idna_fuzz.is_ascii_string(ascii))293    }294  }295}296297///|298test "fuzz: mixed bidi labels rejected" {299  let rng = @random.Rand::chacha8(seed=fuzz_seed)300  for _ in 0..<fuzz_iterations {301    let label = @idna_fuzz.random_mixed_bidi_label(rng)302    let error = @test.expect_error(() => {303      @idna.to_ascii(304        label,305        check_bidi=true,306        check_joiners=false,307        verify_dns_length=false,308      )309    })310    guard error is InvalidBidi else {311      fail("expected InvalidBidi for mixed LTR/RTL label")312    }313  }314}315316// =============================================================================317// Property 8: Joiner checking318// =============================================================================319320///|321test "fuzz: joiners at boundary rejected" {322  let rng = @random.Rand::chacha8(seed=fuzz_seed)323  for _ in 0..<fuzz_iterations {324    let label = @idna_fuzz.random_label_with_joiners(rng, at_boundary=true)325    let error = @test.expect_error(() => {326      @idna.to_ascii(327        label,328        check_bidi=false,329        check_joiners=true,330        verify_dns_length=false,331      )332    })333    guard error is InvalidJoiner else {334      fail("expected InvalidJoiner for joiner at label boundary")335    }336  }337}338339///|340test "fuzz: joiners after virama accepted" {341  let rng = @random.Rand::chacha8(seed=fuzz_seed)342  for _ in 0..<fuzz_iterations {343    let label = @idna_fuzz.random_label_with_joiners(rng, at_boundary=false)344    let ascii = @idna.to_ascii(345      label,346      check_bidi=false,347      check_joiners=true,348      verify_dns_length=false,349    )350    assert_true(@idna_fuzz.is_ascii_string(ascii))351  }352}353354// =============================================================================355// Property 9: IDNA Mapping356// =============================================================================357358///|359test "fuzz: ignored characters removed" {360  let rng = @random.Rand::chacha8(seed=fuzz_seed)361  for _ in 0..<fuzz_iterations {362    let label = @idna_fuzz.random_label_with_ignored(rng)363    try364      @idna.to_ascii(365        label,366        check_bidi=false,367        check_joiners=false,368        verify_dns_length=false,369      )370    catch {371      _ => ()372    } noraise {373      ascii =>374        // Result should not contain soft hyphen (U+00AD) etc.375        for c in ascii {376          let cp = c.to_int()377          assert_true(378            cp != 0x00AD && cp != 0x034F && cp != 0x180B && cp != 0xFE00,379          )380        }381    }382  }383}384385///|386test "fuzz: mapped characters transformed" {387  let rng = @random.Rand::chacha8(seed=fuzz_seed)388  for _ in 0..<fuzz_iterations {389    let label = @idna_fuzz.random_label_with_mapped(rng)390    try391      @idna.to_ascii(392        label,393        check_bidi=false,394        check_joiners=false,395        verify_dns_length=false,396      )397    catch {398      _ => ()399    } noraise {400      ascii =>401        // Result should be lowercase ASCII (for fullwidth/uppercase input)402        // and should not contain fullwidth characters403        for c in ascii {404          let cp = c.to_int()405          // No fullwidth ASCII (FF00-FF5E)406          assert_true(cp < 0xFF00 || cp > 0xFF5E)407          // No uppercase A-Z in output (should be lowercase)408          // (except in xn-- prefix which is lowercase anyway)409        }410    }411  }412}413414// =============================================================================415// Property 10: Error case validation416// =============================================================================417418///|419test "fuzz: disallowed characters rejected" {420  let rng = @random.Rand::chacha8(seed=fuzz_seed)421  for _ in 0..<fuzz_iterations {422    let label = @idna_fuzz.random_label_with_disallowed(rng)423    let error = @test.expect_error(() => {424      @idna.to_ascii(label, check_bidi=false, check_joiners=false)425    })426    guard error is DisallowedCodePoint(_) else {427      fail("expected DisallowedCodePoint")428    }429  }430}431432///|433test "fuzz: label too long rejected" {434  let rng = @random.Rand::chacha8(seed=fuzz_seed)435  for _ in 0..<100 {436    let label = @idna_fuzz.random_long_label(rng)437    let error = @test.expect_error(() => {438      @idna.to_ascii(439        label,440        check_bidi=false,441        check_joiners=false,442        verify_dns_length=true,443      )444    })445    guard error is LabelTooLong else { fail("expected LabelTooLong") }446  }447}448449///|450test "fuzz: leading combining mark rejected" {451  let rng = @random.Rand::chacha8(seed=fuzz_seed)452  for _ in 0..<fuzz_iterations {453    let label = @idna_fuzz.random_label_leading_combining(rng)454    let error = @test.expect_error(() => {455      @idna.to_ascii(label, check_bidi=false, check_joiners=false)456    })457    guard error is LeadingCombiningMark else {458      fail("expected LeadingCombiningMark")459    }460  }461}462463// =============================================================================464// Property 11: STD3 rules465// =============================================================================466467///|468test "fuzz: std3 hyphen at start rejected" {469  let rng = @random.Rand::chacha8(seed=fuzz_seed)470  for _ in 0..<200 {471    let label = @idna_fuzz.random_label_boundary_hyphen(rng, at_start=true)472    let error = @test.expect_error(() => {473      @idna.to_ascii(474        label,475        use_std3_ascii_rules=true,476        check_hyphens=true,477        check_bidi=false,478        check_joiners=false,479      )480    })481    guard error is InvalidHyphen else {482      fail("expected InvalidHyphen at label start")483    }484  }485}486487///|488test "fuzz: std3 hyphen at end rejected" {489  let rng = @random.Rand::chacha8(seed=fuzz_seed)490  for _ in 0..<200 {491    let label = @idna_fuzz.random_label_boundary_hyphen(rng, at_start=false)492    let error = @test.expect_error(() => {493      @idna.to_ascii(494        label,495        use_std3_ascii_rules=true,496        check_hyphens=true,497        check_bidi=false,498        check_joiners=false,499      )500    })501    guard error is InvalidHyphen else {502      fail("expected InvalidHyphen at label end")503    }504  }505}506507// =============================================================================508// Property 12: Cross-library - NFC normalization509// =============================================================================510511///|512test "fuzz: idna output is nfc normalized" {513  let rng = @random.Rand::chacha8(seed=fuzz_seed)514  for _ in 0..<fuzz_iterations {515    let domain = @idna_fuzz.random_domain(rng)516    try @idna.to_unicode(domain, check_bidi=false, check_joiners=false) catch {517      _ => ()518    } noraise {519      unicode => {520        // to_unicode output should be NFC normalized521        // NFC(output) == output522        let nfc_result = @normalization.nfc(unicode)523        assert_eq(unicode, nfc_result)524      }525    }526  }527}528