| 1 |
import unittest |
| 2 |
import warnings |
| 3 |
|
| 4 |
import idna |
| 5 |
|
| 6 |
|
| 7 |
class IDNATests(unittest.TestCase): |
| 8 |
def setUp(self): |
| 9 |
self.tld_strings: list[tuple[str, bytes]] = [ |
| 10 |
("\u6d4b\u8bd5", b"xn--0zwm56d"), |
| 11 |
("\u092a\u0930\u0940\u0915\u094d\u0937\u093e", b"xn--11b5bs3a9aj6g"), |
| 12 |
("\ud55c\uad6d", b"xn--3e0b707e"), |
| 13 |
("\u09ad\u09be\u09b0\u09a4", b"xn--45brj9c"), |
| 14 |
("\u09ac\u09be\u0982\u09b2\u09be", b"xn--54b7fta0cc"), |
| 15 |
( |
| 16 |
"\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435", |
| 17 |
b"xn--80akhbyknj4f", |
| 18 |
), |
| 19 |
("\u0441\u0440\u0431", b"xn--90a3ac"), |
| 20 |
("\ud14c\uc2a4\ud2b8", b"xn--9t4b11yi5a"), |
| 21 |
( |
| 22 |
"\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0bc2\u0bb0\u0bcd", |
| 23 |
b"xn--clchc0ea0b2g2a9gcd", |
| 24 |
), |
| 25 |
("\u05d8\u05e2\u05e1\u05d8", b"xn--deba0ad"), |
| 26 |
("\u4e2d\u56fd", b"xn--fiqs8s"), |
| 27 |
("\u4e2d\u570b", b"xn--fiqz9s"), |
| 28 |
("\u0c2d\u0c3e\u0c30\u0c24\u0c4d", b"xn--fpcrj9c3d"), |
| 29 |
("\u0dbd\u0d82\u0d9a\u0dcf", b"xn--fzc2c9e2c"), |
| 30 |
("\u6e2c\u8a66", b"xn--g6w251d"), |
| 31 |
("\u0aad\u0abe\u0ab0\u0aa4", b"xn--gecrj9c"), |
| 32 |
("\u092d\u093e\u0930\u0924", b"xn--h2brj9c"), |
| 33 |
("\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc", b"xn--hgbk6aj7f53bba"), |
| 34 |
("\u0baa\u0bb0\u0bbf\u0b9f\u0bcd\u0b9a\u0bc8", b"xn--hlcj6aya9esc7a"), |
| 35 |
("\u0443\u043a\u0440", b"xn--j1amh"), |
| 36 |
("\u9999\u6e2f", b"xn--j6w193g"), |
| 37 |
("\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae", b"xn--jxalpdlp"), |
| 38 |
("\u0625\u062e\u062a\u0628\u0627\u0631", b"xn--kgbechtv"), |
| 39 |
("\u53f0\u6e7e", b"xn--kprw13d"), |
| 40 |
("\u53f0\u7063", b"xn--kpry57d"), |
| 41 |
("\u0627\u0644\u062c\u0632\u0627\u0626\u0631", b"xn--lgbbat1ad8j"), |
| 42 |
("\u0639\u0645\u0627\u0646", b"xn--mgb9awbf"), |
| 43 |
("\u0627\u06cc\u0631\u0627\u0646", b"xn--mgba3a4f16a"), |
| 44 |
("\u0627\u0645\u0627\u0631\u0627\u062a", b"xn--mgbaam7a8h"), |
| 45 |
("\u067e\u0627\u06a9\u0633\u062a\u0627\u0646", b"xn--mgbai9azgqp6j"), |
| 46 |
("\u0627\u0644\u0627\u0631\u062f\u0646", b"xn--mgbayh7gpa"), |
| 47 |
("\u0628\u06be\u0627\u0631\u062a", b"xn--mgbbh1a71e"), |
| 48 |
("\u0627\u0644\u0645\u063a\u0631\u0628", b"xn--mgbc0a9azcg"), |
| 49 |
("\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629", b"xn--mgberp4a5d4ar"), |
| 50 |
("\u10d2\u10d4", b"xn--node"), |
| 51 |
("\u0e44\u0e17\u0e22", b"xn--o3cw4h"), |
| 52 |
("\u0633\u0648\u0631\u064a\u0629", b"xn--ogbpf8fl"), |
| 53 |
("\u0440\u0444", b"xn--p1ai"), |
| 54 |
("\u062a\u0648\u0646\u0633", b"xn--pgbs0dh"), |
| 55 |
("\u0a2d\u0a3e\u0a30\u0a24", b"xn--s9brj9c"), |
| 56 |
("\u0645\u0635\u0631", b"xn--wgbh1c"), |
| 57 |
("\u0642\u0637\u0631", b"xn--wgbl6a"), |
| 58 |
("\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8", b"xn--xkc2al3hye2a"), |
| 59 |
("\u0b87\u0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe", b"xn--xkc2dl3a5ee0h"), |
| 60 |
("\u65b0\u52a0\u5761", b"xn--yfro4i67o"), |
| 61 |
("\u0641\u0644\u0633\u0637\u064a\u0646", b"xn--ygbi2ammx"), |
| 62 |
("\u30c6\u30b9\u30c8", b"xn--zckzah"), |
| 63 |
("\u049b\u0430\u0437", b"xn--80ao21a"), |
| 64 |
("\u0645\u0644\u064a\u0633\u064a\u0627", b"xn--mgbx4cd0ab"), |
| 65 |
("\u043c\u043e\u043d", b"xn--l1acc"), |
| 66 |
("\u0633\u0648\u062f\u0627\u0646", b"xn--mgbpl2fh"), |
| 67 |
] |
| 68 |
|
| 69 |
def testIDNTLDALabels(self): |
| 70 |
for u, a in self.tld_strings: |
| 71 |
self.assertEqual(a, idna.alabel(u)) |
| 72 |
|
| 73 |
def testIDNTLDULabels(self): |
| 74 |
for u, a in self.tld_strings: |
| 75 |
self.assertEqual(u, idna.ulabel(a)) |
| 76 |
|
| 77 |
def test_valid_label_length(self): |
| 78 |
self.assertTrue(idna.valid_label_length("a" * 63)) |
| 79 |
self.assertFalse(idna.valid_label_length("a" * 64)) |
| 80 |
self.assertRaises(idna.IDNAError, idna.encode, "a" * 64) |
| 81 |
|
| 82 |
def test_oversized_input_rejected_promptly(self): |
| 83 |
# GHSA-65pc-fj4g-8rjx: encode/decode must reject inputs that |
| 84 |
# exceed the maximum DNS domain length before per-codepoint |
| 85 |
# validation runs, so labels dominated by CONTEXTO codepoints |
| 86 |
# cannot drive validation into quadratic time. |
| 87 |
import time |
| 88 |
|
| 89 |
for payload in ("٠" * 8000, "・" * 8000 + "漢"): |
| 90 |
start = time.perf_counter() |
| 91 |
self.assertRaises(idna.IDNAError, idna.encode, payload) |
| 92 |
self.assertRaises(idna.IDNAError, idna.decode, payload) |
| 93 |
self.assertLess(time.perf_counter() - start, 1.0) |
| 94 |
|
| 95 |
def test_oversized_label_rejected_promptly(self): |
| 96 |
# The whole-domain cap in encode()/decode() does not cover direct |
| 97 |
# callers of alabel/ulabel/check_label, nor the idna2008 |
| 98 |
# incremental codec which calls alabel/ulabel per label. Without a |
| 99 |
# per-label cap, a single oversized CONTEXTO-heavy label still |
| 100 |
# drives validation into quadratic time. |
| 101 |
import codecs |
| 102 |
import time |
| 103 |
|
| 104 |
import idna.codec # register the idna2008 codec |
| 105 |
|
| 106 |
payload = "・" * 8000 + "漢" |
| 107 |
start = time.perf_counter() |
| 108 |
self.assertRaises(idna.IDNAError, idna.check_label, payload) |
| 109 |
self.assertRaises(idna.IDNAError, idna.alabel, payload) |
| 110 |
self.assertRaises(idna.IDNAError, idna.ulabel, payload) |
| 111 |
self.assertRaises( |
| 112 |
idna.IDNAError, |
| 113 |
codecs.getincrementalencoder("idna2008")().encode, |
| 114 |
payload, |
| 115 |
True, |
| 116 |
) |
| 117 |
self.assertLess(time.perf_counter() - start, 1.0) |
| 118 |
|
| 119 |
def test_check_bidi(self): |
| 120 |
la = "\u0061" |
| 121 |
r = "\u05d0" |
| 122 |
al = "\u0627" |
| 123 |
an = "\u0660" |
| 124 |
en = "\u0030" |
| 125 |
es = "\u002d" |
| 126 |
cs = "\u002c" |
| 127 |
et = "\u0024" |
| 128 |
on = "\u0021" |
| 129 |
bn = "\u200c" |
| 130 |
nsm = "\u0610" |
| 131 |
ws = "\u0020" |
| 132 |
|
| 133 |
# RFC 5893 Rule 1 |
| 134 |
self.assertTrue(idna.check_bidi(la)) |
| 135 |
self.assertTrue(idna.check_bidi(r)) |
| 136 |
self.assertTrue(idna.check_bidi(al)) |
| 137 |
self.assertRaises(idna.IDNABidiError, idna.check_bidi, an) |
| 138 |
|
| 139 |
# RFC 5893 Rule 2 |
| 140 |
self.assertTrue(idna.check_bidi(r + al)) |
| 141 |
self.assertTrue(idna.check_bidi(r + an)) |
| 142 |
self.assertTrue(idna.check_bidi(r + en)) |
| 143 |
self.assertTrue(idna.check_bidi(r + es + al)) |
| 144 |
self.assertTrue(idna.check_bidi(r + cs + al)) |
| 145 |
self.assertTrue(idna.check_bidi(r + et + al)) |
| 146 |
self.assertTrue(idna.check_bidi(r + on + al)) |
| 147 |
self.assertTrue(idna.check_bidi(r + bn + al)) |
| 148 |
self.assertTrue(idna.check_bidi(r + nsm)) |
| 149 |
self.assertRaises(idna.IDNABidiError, idna.check_bidi, r + la) |
| 150 |
self.assertRaises(idna.IDNABidiError, idna.check_bidi, r + ws) |
| 151 |
|
| 152 |
# RFC 5893 Rule 3 |
| 153 |
self.assertTrue(idna.check_bidi(r + al)) |
| 154 |
self.assertTrue(idna.check_bidi(r + en)) |
| 155 |
self.assertTrue(idna.check_bidi(r + an)) |
| 156 |
self.assertTrue(idna.check_bidi(r + nsm)) |
| 157 |
self.assertTrue(idna.check_bidi(r + nsm + nsm)) |
| 158 |
self.assertRaises(idna.IDNABidiError, idna.check_bidi, r + on) |
| 159 |
|
| 160 |
# RFC 5893 Rule 4 |
| 161 |
self.assertTrue(idna.check_bidi(r + en)) |
| 162 |
self.assertTrue(idna.check_bidi(r + an)) |
| 163 |
self.assertRaises(idna.IDNABidiError, idna.check_bidi, r + en + an) |
| 164 |
self.assertRaises(idna.IDNABidiError, idna.check_bidi, r + an + en) |
| 165 |
|
| 166 |
# RFC 5893 Rule 5 |
| 167 |
self.assertTrue(idna.check_bidi(la + en, check_ltr=True)) |
| 168 |
self.assertTrue(idna.check_bidi(la + es + la, check_ltr=True)) |
| 169 |
self.assertTrue(idna.check_bidi(la + cs + la, check_ltr=True)) |
| 170 |
self.assertTrue(idna.check_bidi(la + et + la, check_ltr=True)) |
| 171 |
self.assertTrue(idna.check_bidi(la + on + la, check_ltr=True)) |
| 172 |
self.assertTrue(idna.check_bidi(la + bn + la, check_ltr=True)) |
| 173 |
self.assertTrue(idna.check_bidi(la + nsm, check_ltr=True)) |
| 174 |
|
| 175 |
# RFC 5893 Rule 6 |
| 176 |
self.assertTrue(idna.check_bidi(la + la, check_ltr=True)) |
| 177 |
self.assertTrue(idna.check_bidi(la + en, check_ltr=True)) |
| 178 |
self.assertTrue(idna.check_bidi(la + en + nsm, check_ltr=True)) |
| 179 |
self.assertTrue(idna.check_bidi(la + en + nsm + nsm, check_ltr=True)) |
| 180 |
self.assertRaises(idna.IDNABidiError, idna.check_bidi, la + cs, check_ltr=True) |
| 181 |
|
| 182 |
def test_check_initial_combiner(self): |
| 183 |
m = "\u0300" |
| 184 |
a = "\u0061" |
| 185 |
|
| 186 |
self.assertTrue(idna.check_initial_combiner(a)) |
| 187 |
self.assertTrue(idna.check_initial_combiner(a + m)) |
| 188 |
self.assertRaises(idna.IDNAError, idna.check_initial_combiner, m + a) |
| 189 |
self.assertTrue(idna.check_initial_combiner("")) |
| 190 |
|
| 191 |
def test_check_hyphen_ok(self): |
| 192 |
self.assertTrue(idna.check_hyphen_ok("abc")) |
| 193 |
self.assertTrue(idna.check_hyphen_ok("a--b")) |
| 194 |
self.assertRaises(idna.IDNAError, idna.check_hyphen_ok, "aa--") |
| 195 |
self.assertRaises(idna.IDNAError, idna.check_hyphen_ok, "a-") |
| 196 |
self.assertRaises(idna.IDNAError, idna.check_hyphen_ok, "-a") |
| 197 |
self.assertRaises(idna.IDNAError, idna.check_hyphen_ok, "-") |
| 198 |
self.assertTrue(idna.check_hyphen_ok("")) |
| 199 |
|
| 200 |
def test_valid_contextj(self): |
| 201 |
zwnj = "\u200c" |
| 202 |
zwj = "\u200d" |
| 203 |
virama = "\u094d" |
| 204 |
latin = "\u0061" |
| 205 |
|
| 206 |
# RFC 5892 Appendix A.1 (Zero Width Non-Joiner) |
| 207 |
self.assertFalse(idna.valid_contextj(zwnj, 0)) |
| 208 |
self.assertFalse(idna.valid_contextj(latin + zwnj, 1)) # No preceding Virama |
| 209 |
self.assertTrue(idna.valid_contextj(virama + zwnj, 1)) # Preceding Virama |
| 210 |
|
| 211 |
# RFC 5892 Appendix A.2 (Zero Width Joiner) |
| 212 |
self.assertFalse(idna.valid_contextj(zwj, 0)) |
| 213 |
self.assertFalse(idna.valid_contextj(latin + zwj, 1)) # No preceding Virama |
| 214 |
self.assertTrue(idna.valid_contextj(virama + zwj, 1)) # Preceding Virama |
| 215 |
|
| 216 |
def test_check_label_contextj_violation(self): |
| 217 |
zwnj = "\u200c" |
| 218 |
zwj = "\u200d" |
| 219 |
virama = "\u094d" |
| 220 |
latin = "\u0061" |
| 221 |
|
| 222 |
for joiner in (zwnj, zwj): |
| 223 |
with self.assertRaises(idna.InvalidCodepointContext) as context: |
| 224 |
idna.check_label(latin + joiner + latin) |
| 225 |
self.assertIn("not allowed at position 2", str(context.exception)) |
| 226 |
self.assertRaises(idna.InvalidCodepointContext, idna.encode, latin + joiner + latin) |
| 227 |
|
| 228 |
# Valid joiner contexts are still accepted |
| 229 |
idna.check_label(latin + virama + zwj + latin) |
| 230 |
idna.check_label(latin + virama + zwnj + latin) |
| 231 |
|
| 232 |
def test_check_label_contextj_unknown_codepoint(self): |
| 233 |
# An adjacent codepoint unknown to unicodedata (e.g. in a |
| 234 |
# newer Unicode version than the Python build supports) must still |
| 235 |
# surface as an IDNAError. Need to mock this to test. |
| 236 |
from unittest import mock |
| 237 |
|
| 238 |
with ( |
| 239 |
mock.patch("idna.core._combining_class", side_effect=ValueError("Unknown character in unicodedata")), |
| 240 |
self.assertRaises(idna.IDNAError) as context, |
| 241 |
): |
| 242 |
idna.check_label("\u0061\u200d\u0061") |
| 243 |
self.assertNotIsInstance(context.exception, idna.InvalidCodepointContext) |
| 244 |
self.assertIn("Unknown codepoint adjacent to joiner", str(context.exception)) |
| 245 |
|
| 246 |
def test_valid_contexto(self): |
| 247 |
latin = "\u0061" |
| 248 |
latin_l = "\u006c" |
| 249 |
greek = "\u03b1" |
| 250 |
hebrew = "\u05d0" |
| 251 |
katakana = "\u30a1" |
| 252 |
hiragana = "\u3041" |
| 253 |
han = "\u6f22" |
| 254 |
arabic_digit = "\u0660" |
| 255 |
ext_arabic_digit = "\u06f0" |
| 256 |
|
| 257 |
# RFC 5892 Rule A.3 (Middle Dot) |
| 258 |
latin_middle_dot = "\u00b7" |
| 259 |
self.assertTrue(idna.valid_contexto(latin_l + latin_middle_dot + latin_l, 1)) |
| 260 |
self.assertFalse(idna.valid_contexto(latin_middle_dot + latin_l, 1)) |
| 261 |
self.assertFalse(idna.valid_contexto(latin_l + latin_middle_dot, 0)) |
| 262 |
self.assertFalse(idna.valid_contexto(latin_middle_dot, 0)) |
| 263 |
self.assertFalse(idna.valid_contexto(latin_l + latin_middle_dot + latin, 1)) |
| 264 |
|
| 265 |
# RFC 5892 Rule A.4 (Greek Lower Numeral Sign) |
| 266 |
glns = "\u0375" |
| 267 |
self.assertTrue(idna.valid_contexto(glns + greek, 0)) |
| 268 |
self.assertFalse(idna.valid_contexto(glns + latin, 0)) |
| 269 |
self.assertFalse(idna.valid_contexto(glns, 0)) |
| 270 |
self.assertFalse(idna.valid_contexto(greek + glns, 1)) |
| 271 |
|
| 272 |
# RFC 5892 Rule A.5 (Hebrew Punctuation Geresh) |
| 273 |
geresh = "\u05f3" |
| 274 |
self.assertTrue(idna.valid_contexto(hebrew + geresh, 1)) |
| 275 |
self.assertFalse(idna.valid_contexto(latin + geresh, 1)) |
| 276 |
|
| 277 |
# RFC 5892 Rule A.6 (Hebrew Punctuation Gershayim) |
| 278 |
gershayim = "\u05f4" |
| 279 |
self.assertTrue(idna.valid_contexto(hebrew + gershayim, 1)) |
| 280 |
self.assertFalse(idna.valid_contexto(latin + gershayim, 1)) |
| 281 |
|
| 282 |
# RFC 5892 Rule A.7 (Katakana Middle Dot) |
| 283 |
ja_middle_dot = "\u30fb" |
| 284 |
self.assertTrue(idna.valid_contexto(katakana + ja_middle_dot + katakana, 1)) |
| 285 |
self.assertTrue(idna.valid_contexto(hiragana + ja_middle_dot + hiragana, 1)) |
| 286 |
self.assertTrue(idna.valid_contexto(han + ja_middle_dot + han, 1)) |
| 287 |
self.assertTrue(idna.valid_contexto(han + ja_middle_dot + latin, 1)) |
| 288 |
self.assertTrue(idna.valid_contexto("\u6f22\u30fb\u5b57", 1)) |
| 289 |
self.assertFalse(idna.valid_contexto("\u0061\u30fb\u0061", 1)) |
| 290 |
|
| 291 |
# RFC 5892 Rule A.8 (Arabic-Indic Digits) |
| 292 |
self.assertTrue(idna.valid_contexto(arabic_digit + arabic_digit, 0)) |
| 293 |
self.assertFalse(idna.valid_contexto(arabic_digit + ext_arabic_digit, 0)) |
| 294 |
|
| 295 |
# RFC 5892 Rule A.9 (Extended Arabic-Indic Digits) |
| 296 |
self.assertTrue(idna.valid_contexto(ext_arabic_digit + ext_arabic_digit, 0)) |
| 297 |
self.assertFalse(idna.valid_contexto(ext_arabic_digit + arabic_digit, 0)) |
| 298 |
|
| 299 |
def test_encode(self, encode=None, skip_bytes=False): |
| 300 |
if encode is None: |
| 301 |
encode = idna.encode |
| 302 |
|
| 303 |
self.assertEqual(encode("xn--zckzah.xn--zckzah"), b"xn--zckzah.xn--zckzah") |
| 304 |
self.assertEqual(encode("\u30c6\u30b9\u30c8.xn--zckzah"), b"xn--zckzah.xn--zckzah") |
| 305 |
self.assertEqual(encode("\u30c6\u30b9\u30c8.\u30c6\u30b9\u30c8"), b"xn--zckzah.xn--zckzah") |
| 306 |
self.assertEqual(encode("abc.abc"), b"abc.abc") |
| 307 |
self.assertEqual(encode("xn--zckzah.abc"), b"xn--zckzah.abc") |
| 308 |
self.assertEqual(encode("\u30c6\u30b9\u30c8.abc"), b"xn--zckzah.abc") |
| 309 |
self.assertEqual( |
| 310 |
encode("\u0521\u0525\u0523-\u0523\u0523-----\u0521\u0523\u0523\u0523.aa"), |
| 311 |
b"xn---------90gglbagaar.aa", |
| 312 |
) |
| 313 |
if encode is idna.encode: |
| 314 |
self.assertRaises( |
| 315 |
idna.IDNAError, |
| 316 |
encode, |
| 317 |
"\u0521\u0524\u0523-\u0523\u0523-----\u0521\u0523\u0523\u0523.aa", |
| 318 |
uts46=False, |
| 319 |
) |
| 320 |
self.assertEqual(encode("a" * 63), b"a" * 63) |
| 321 |
self.assertRaises(idna.IDNAError, encode, "a" * 64) |
| 322 |
self.assertRaises(idna.core.InvalidCodepoint, encode, "*") |
| 323 |
if not skip_bytes: |
| 324 |
self.assertRaises(idna.IDNAError, encode, b"\x0a\x33\x81") |
| 325 |
|
| 326 |
def test_decode(self, decode=None, skip_str=False): |
| 327 |
if decode is None: |
| 328 |
decode = idna.decode |
| 329 |
self.assertEqual(decode(b"xn--zckzah.xn--zckzah"), "\u30c6\u30b9\u30c8.\u30c6\u30b9\u30c8") |
| 330 |
self.assertEqual( |
| 331 |
decode(b"xn--d1acufc.xn--80akhbyknj4f"), |
| 332 |
"\u0434\u043e\u043c\u0435\u043d.\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435", |
| 333 |
) |
| 334 |
if not skip_str: |
| 335 |
self.assertEqual( |
| 336 |
decode("\u30c6\u30b9\u30c8.xn--zckzah"), |
| 337 |
"\u30c6\u30b9\u30c8.\u30c6\u30b9\u30c8", |
| 338 |
) |
| 339 |
self.assertEqual( |
| 340 |
decode("\u30c6\u30b9\u30c8.\u30c6\u30b9\u30c8"), |
| 341 |
"\u30c6\u30b9\u30c8.\u30c6\u30b9\u30c8", |
| 342 |
) |
| 343 |
self.assertEqual(decode("abc.abc"), "abc.abc") |
| 344 |
self.assertEqual( |
| 345 |
decode(b"xn---------90gglbagaar.aa"), |
| 346 |
"\u0521\u0525\u0523-\u0523\u0523-----\u0521\u0523\u0523\u0523.aa", |
| 347 |
) |
| 348 |
self.assertRaises(idna.IDNAError, decode, b"XN---------90GGLBAGAAC.AA") |
| 349 |
self.assertRaises(idna.IDNAError, decode, b"xn---------90gglbagaac.aa") |
| 350 |
self.assertRaises(idna.IDNAError, decode, b"xn--") |
| 351 |
self.assertRaises(idna.IDNAError, decode, b"\x8d\xd2") |
| 352 |
self.assertRaises( |
| 353 |
idna.IDNAError, |
| 354 |
decode, |
| 355 |
b"A.A.0.a.a.A.0.a.A.A.0.a.A.0A.2.a.A.A.0.a.A.0.A.a.A0.a.a.A.0.a.fB.A.A.a.A.A.B.A.A.a.A.A.B.A.A.a.A.A.0.a.A.a.a.A.A.0.a.A.0.A.a.A0.a.a.A.0.a.fB.A.A.a.A.A.B.0A.A.a.A.A.B.A.A.a.A.A.a.A.A.B.A.A.a.A.0.a.B.A.A.a.A.B.A.a.A.A.5.a.A.0.a.Ba.A.B.A.A.a.A.0.a.Xn--B.A.A.A.a", |
| 356 |
) |
| 357 |
self.assertRaises(idna.IDNAError, decode, b"xn--ukba655qaaaa14431eeaaba.c") |
| 358 |
|
| 359 |
def test_encode_transitional_deprecation_warning(self): |
| 360 |
with warnings.catch_warnings(record=True) as w: |
| 361 |
warnings.simplefilter("always") |
| 362 |
idna.encode("example.com", uts46=True, transitional=True) |
| 363 |
self.assertEqual(len(w), 1) |
| 364 |
self.assertTrue(issubclass(w[0].category, DeprecationWarning)) |
| 365 |
self.assertIn("transitional", str(w[0].message).lower()) |
| 366 |
|
| 367 |
def test_uts46_remap_transitional_deprecation_warning(self): |
| 368 |
with warnings.catch_warnings(record=True) as w: |
| 369 |
warnings.simplefilter("always") |
| 370 |
idna.uts46_remap("example.com", transitional=True) |
| 371 |
self.assertEqual(len(w), 1) |
| 372 |
self.assertTrue(issubclass(w[0].category, DeprecationWarning)) |
| 373 |
self.assertIn("transitional", str(w[0].message).lower()) |
| 374 |
with warnings.catch_warnings(record=True) as w: |
| 375 |
warnings.simplefilter("always") |
| 376 |
idna.uts46_remap("example.com") |
| 377 |
self.assertEqual(len(w), 0) |
| 378 |
|
| 379 |
def test_encode_no_transitional_no_warning(self): |
| 380 |
with warnings.catch_warnings(record=True) as w: |
| 381 |
warnings.simplefilter("always") |
| 382 |
idna.encode("example.com", uts46=True) |
| 383 |
self.assertEqual(len(w), 0) |
| 384 |
|
| 385 |
def test_encode_decode_invalid_input_type(self): |
| 386 |
# encode() and decode() are documented to raise IDNAError on bad |
| 387 |
# input. Inputs that are not str, bytes, or bytes-like used to leak |
| 388 |
# a raw TypeError out of str(s, "ascii"); they should be wrapped in |
| 389 |
# IDNAError just like UnicodeDecodeError already is. |
| 390 |
for value in (42, None, 1.5, ["a", "b"], {"a": 1}): |
| 391 |
self.assertRaises(idna.IDNAError, idna.encode, value) |
| 392 |
self.assertRaises(idna.IDNAError, idna.decode, value) |
| 393 |
|
| 394 |
def test_uts46_remap(self): |
| 395 |
remap = idna.uts46_remap |
| 396 |
# Empty and unchanged input, including the no-copy path for a |
| 397 |
# non-ASCII string that needs no mapping. |
| 398 |
self.assertEqual(remap(""), "") |
| 399 |
self.assertEqual(remap("\u0431\u0443\u043a\u0432\u044b"), "\u0431\u0443\u043a\u0432\u044b") |
| 400 |
# Mapped (M) characters interleaved with runs of valid ones: case |
| 401 |
# folding, fullwidth forms, and the alternative label separators. |
| 402 |
self.assertEqual(remap("\u0411\u0443\u041a\u0432\u042b"), "\u0431\u0443\u043a\u0432\u044b") |
| 403 |
self.assertEqual(remap("ab\uff23\uff24ef"), "abcdef") |
| 404 |
self.assertEqual(remap("a\u3002b\uff0ec\uff61d"), "a.b.c.d") |
| 405 |
# Ignored (I) characters are dropped, wherever they fall. |
| 406 |
self.assertEqual(remap("\u00ada\u00adb\u00ad"), "ab") |
| 407 |
# Deviation (D) characters are kept. Transitional processing, which |
| 408 |
# mapped them, is deprecated in UTS #46 and the flag has no effect. |
| 409 |
self.assertEqual(remap("a\u00dfb"), "a\u00dfb") |
| 410 |
self.assertEqual(remap("a\u03c2b"), "a\u03c2b") |
| 411 |
self.assertEqual(remap("a\u200cb"), "a\u200cb") |
| 412 |
with warnings.catch_warnings(): |
| 413 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 414 |
self.assertEqual(remap("a\u00dfb", transitional=True), "a\u00dfb") |
| 415 |
self.assertEqual(remap("a\u03c2b", transitional=True), "a\u03c2b") |
| 416 |
self.assertEqual(remap("a\u200cb", transitional=True), "a\u200cb") |
| 417 |
self.assertEqual(remap("\u1e9e", transitional=True), "\u00df") |
| 418 |
# Output is NFC even when the input is not. |
| 419 |
self.assertEqual(remap("e\u0301"), "\u00e9") |
| 420 |
# Disallowed (X) characters raise, reporting the 1-based position in |
| 421 |
# the input. |
| 422 |
with self.assertRaises(idna.InvalidCodepoint) as cm: |
| 423 |
remap("ab\ufffdc") |
| 424 |
self.assertIn("position 3", str(cm.exception)) |
| 425 |
self.assertRaises(idna.IDNAError, remap, "a" * 1025) |
| 426 |
|
| 427 |
def test_uts46_remap_ascii(self): |
| 428 |
# uts46_remap() takes a shortcut for pure-ASCII input on the basis |
| 429 |
# that the only ASCII mapping in UTS #46 is upper- to lowercase and |
| 430 |
# every other ASCII codepoint has status V. Pin that against the |
| 431 |
# table so a future table change cannot silently invalidate it. |
| 432 |
from idna.uts46data import uts46_replacements, uts46_statuses |
| 433 |
|
| 434 |
for cp in range(128): |
| 435 |
char = chr(cp) |
| 436 |
if "A" <= char <= "Z": |
| 437 |
self.assertEqual(chr(uts46_statuses[cp]), "M", char) |
| 438 |
self.assertEqual(uts46_replacements[cp], char.lower(), char) |
| 439 |
else: |
| 440 |
self.assertEqual(chr(uts46_statuses[cp]), "V", char) |
| 441 |
self.assertIsNone(uts46_replacements[cp], char) |
| 442 |
for std3_rules in (True, False): |
| 443 |
self.assertEqual(idna.uts46_remap("WWW.Example.COM", std3_rules=std3_rules), "www.example.com") |
| 444 |
self.assertEqual(idna.uts46_remap("a-b_c d~", std3_rules=False), "a-b_c d~") |
| 445 |
self.assertRaises(idna.InvalidCodepoint, idna.uts46_remap, "a-b_c d~", std3_rules=True) |
| 446 |
self.assertRaises(idna.IDNAError, idna.uts46_remap, "A" * 1025) |
| 447 |
|
| 448 |
def test_uts46_remap_std3(self): |
| 449 |
# UTS #46 §4.1: with UseSTD3ASCIIRules, an ASCII character in the |
| 450 |
# mapped output must be a lowercase letter, digit or hyphen (or the |
| 451 |
# label separator). The offending codepoint and position reported are |
| 452 |
# those of the *input* character, including when the ASCII character |
| 453 |
# was produced by a mapping. |
| 454 |
remap = idna.uts46_remap |
| 455 |
for domain, expected, cp, position in ( |
| 456 |
("a_b", "a_b", "U+005F", 2), # ASCII fast path |
| 457 |
("A B", "a b", "U+0020", 2), |
| 458 |
("a/b.c", "a/b.c", "U+002F", 2), |
| 459 |
("a\uff01b", "a!b", "U+FF01", 2), # fullwidth ! maps to ! |
| 460 |
("\u00a0x", " x", "U+00A0", 1), # NBSP maps to space |
| 461 |
("a\u2100b", "aa/cb", "U+2100", 2), # ACCOUNT OF maps to a/c |
| 462 |
("a\u03b2_", "a\u03b2_", "U+005F", 3), # in a run after a non-ASCII char |
| 463 |
("\u03b2\u0391a~", "\u03b2\u03b1a~", "U+007E", 4), # in the tail run |
| 464 |
("~\u0391", "~\u03b1", "U+007E", 1), # in a run before a mapped char |
| 465 |
): |
| 466 |
with self.subTest(domain=domain): |
| 467 |
self.assertEqual(remap(domain, std3_rules=False), expected) |
| 468 |
with self.assertRaises(idna.InvalidCodepoint) as cm: |
| 469 |
remap(domain, std3_rules=True) |
| 470 |
self.assertEqual(str(cm.exception), f"Codepoint {cp} not allowed at position {position} in {domain!r}") |
| 471 |
# Letters, digits, hyphens and label separators (including the ones |
| 472 |
# mapped to U+002E) are fine either way. |
| 473 |
for domain, expected in ( |
| 474 |
("a-b.c9", "a-b.c9"), |
| 475 |
("XN--80AK6AA92E.COM", "xn--80ak6aa92e.com"), |
| 476 |
("\u0431\uff0e\u0432\u3002\u0433\uff61", "\u0431.\u0432.\u0433."), |
| 477 |
("\uff21\uff22\uff23", "abc"), |
| 478 |
): |
| 479 |
for std3_rules in (True, False): |
| 480 |
self.assertEqual(remap(domain, std3_rules=std3_rules), expected) |
| 481 |
# encode() forwards the flag (default off): with it on the mapping |
| 482 |
# step rejects the character; with it off the character survives |
| 483 |
# mapping and is only rejected later by IDNA 2008 label validation. |
| 484 |
with self.assertRaises(idna.InvalidCodepoint) as cm: |
| 485 |
idna.encode("a_b", uts46=True, std3_rules=True) |
| 486 |
self.assertEqual(str(cm.exception), "Codepoint U+005F not allowed at position 2 in 'a_b'") |
| 487 |
with self.assertRaises(idna.InvalidCodepoint) as cm: |
| 488 |
idna.encode("a_b", uts46=True) |
| 489 |
self.assertEqual(str(cm.exception), "Codepoint U+005F at position 2 of 'a_b' not allowed") |
| 490 |
self.assertEqual(idna.encode("a\u00adb", uts46=True, std3_rules=True), b"ab") |
| 491 |
|
| 492 |
def test_bytes_input_errors_are_idnaerror(self): |
| 493 |
# bytes given to the label helpers must fail with IDNAError, not leak |
| 494 |
# UnicodeDecodeError: check_label() decodes bytes as UTF-8, ulabel() |
| 495 |
# treats bytes as ASCII. |
| 496 |
self.assertRaises(idna.IDNAError, idna.check_label, b"\xff") |
| 497 |
self.assertRaises(idna.IDNAError, idna.ulabel, b"\xff") |
| 498 |
self.assertRaises(idna.IDNAError, idna.ulabel, b"\xc3\x9f") # valid UTF-8 for U+00DF, still not ASCII |
| 499 |
self.assertRaises(idna.IDNAError, idna.ulabel, bytearray(b"xn--\xff")) |
| 500 |
self.assertEqual(idna.ulabel(b"xn--e1afmkfd"), "\u043f\u0440\u0438\u043c\u0435\u0440") |
| 501 |
|
| 502 |
def test_non_canonical_alabel(self): |
| 503 |
# RFC 5891 §5.3: an A-label must re-encode to itself. "xn---bbk" is |
| 504 |
# a non-canonical Punycode spelling of "xn--bbk" (RFC 3492 permits |
| 505 |
# a delimiter before an empty basic-code-point run, so both decode |
| 506 |
# to the same U-label); accepting it would let two different |
| 507 |
# wire-format names display identically. |
| 508 |
self.assertEqual(idna.ulabel("xn--bbk"), "\u307e") |
| 509 |
self.assertEqual(idna.encode("xn--bbk"), b"xn--bbk") |
| 510 |
for label in ("xn---bbk", b"xn---bbk", "XN---BBK"): |
| 511 |
with self.subTest(label=label): |
| 512 |
with self.assertRaises(idna.IDNAError) as ctx: |
| 513 |
idna.ulabel(label) |
| 514 |
self.assertEqual(ctx.exception.code, "non_canonical_alabel") |
| 515 |
self.assertRaises(idna.IDNAError, idna.alabel, "xn---bbk") |
| 516 |
self.assertRaises(idna.IDNAError, idna.encode, "xn---bbk.example") |
| 517 |
self.assertRaises(idna.IDNAError, idna.decode, "xn---bbk.example") |
| 518 |
# display decoding keeps the wire form rather than the misleading U-label |
| 519 |
self.assertEqual(idna.decode("XN---BBK.example", display=True), "xn---bbk.example") |
| 520 |
# ASCII case in the input is not a canonicality violation |
| 521 |
self.assertEqual(idna.ulabel("XN--MNCHEN-3YA"), "m\xfcnchen") |
| 522 |
self.assertEqual(idna.ulabel("xn--Mnchen-3ya"), "m\xfcnchen") |
| 523 |
|
| 524 |
def test_decode_display(self): |
| 525 |
# A label whose Punycode decode succeeds but contains disallowed |
| 526 |
# codepoints — under display decoding, the original A-label is kept. |
| 527 |
self.assertRaises(idna.IDNAError, idna.decode, "a.b.c.xn--pokxncvks") |
| 528 |
self.assertEqual( |
| 529 |
idna.decode("a.b.c.xn--pokxncvks", display=True), |
| 530 |
"a.b.c.xn--pokxncvks", |
| 531 |
) |
| 532 |
|
| 533 |
# Mixed valid/invalid labels: the valid label still decodes, the |
| 534 |
# invalid xn-- label is preserved verbatim. |
| 535 |
self.assertEqual( |
| 536 |
idna.decode("xn--zckzah.xn--pokxncvks", display=True), |
| 537 |
"テスト.xn--pokxncvks", |
| 538 |
) |
| 539 |
|
| 540 |
# A label whose Punycode itself is malformed. |
| 541 |
self.assertEqual( |
| 542 |
idna.decode("xn--.example", display=True), |
| 543 |
"xn--.example", |
| 544 |
) |
| 545 |
|
| 546 |
# Uppercase A-label prefix: the kept label is lowercased to match |
| 547 |
# what a successful ulabel() call would have returned. |
| 548 |
self.assertEqual( |
| 549 |
idna.decode("XN--POKXNCVKS.example", display=True), |
| 550 |
"xn--pokxncvks.example", |
| 551 |
) |
| 552 |
|
| 553 |
# display must not swallow errors for non-xn-- labels. |
| 554 |
self.assertRaises( |
| 555 |
idna.IDNAError, |
| 556 |
idna.decode, |
| 557 |
"-bad.example", |
| 558 |
display=True, |
| 559 |
) |
| 560 |
|
| 561 |
# display should be a no-op for fully valid input. |
| 562 |
self.assertEqual( |
| 563 |
idna.decode("xn--zckzah.xn--zckzah", display=True), |
| 564 |
"テスト.テスト", |
| 565 |
) |
| 566 |
|
| 567 |
# Trailing dot preserved under display recovery. |
| 568 |
self.assertEqual( |
| 569 |
idna.decode("xn--pokxncvks.", display=True), |
| 570 |
"xn--pokxncvks.", |
| 571 |
) |
| 572 |
|
| 573 |
# Bytes input is supported, matching decode()'s normal contract. |
| 574 |
self.assertEqual( |
| 575 |
idna.decode(b"a.b.c.xn--pokxncvks", display=True), |
| 576 |
"a.b.c.xn--pokxncvks", |
| 577 |
) |
| 578 |
|
| 579 |
|
| 580 |
class UnicodeVersionTests(unittest.TestCase): |
| 581 |
def test_unicode_version_is_exported_and_consistent(self): |
| 582 |
import idna.idnadata |
| 583 |
import idna.uts46data |
| 584 |
|
| 585 |
self.assertIn("unicode_version", idna.__all__) |
| 586 |
self.assertRegex(idna.unicode_version, r"^\d+\.\d+\.\d+$") |
| 587 |
# The two generated tables must always be regenerated together. |
| 588 |
self.assertEqual(idna.unicode_version, idna.idnadata.__version__) |
| 589 |
self.assertEqual(idna.unicode_version, idna.uts46data.__version__) |
| 590 |
|
| 591 |
|
| 592 |
if __name__ == "__main__": |
| 593 |
unittest.main() |