| 1 |
"""Tests for the machine-readable attributes carried by IDNAError.""" |
| 2 |
|
| 3 |
import codecs |
| 4 |
import pickle |
| 5 |
import re |
| 6 |
import unittest |
| 7 |
from pathlib import Path |
| 8 |
from typing import get_args |
| 9 |
from unittest import mock |
| 10 |
|
| 11 |
import idna |
| 12 |
import idna.codec |
| 13 |
import idna.core |
| 14 |
|
| 15 |
|
| 16 |
class ErrorAttributeTests(unittest.TestCase): |
| 17 |
def _cases(self): |
| 18 |
"""(trigger, expected class, expected text, codepoint, position)""" |
| 19 |
r = "\u05d0" # R |
| 20 |
an = "\u0660" # AN |
| 21 |
nsm = "\u0610" # NSM |
| 22 |
return [ |
| 23 |
(lambda: idna.alabel("abc\u0141"), idna.InvalidCodepoint, "abc\u0141", 0x141, 4), |
| 24 |
(lambda: idna.alabel("a\u200cb"), idna.InvalidCodepointContext, "a\u200cb", 0x200C, 2), |
| 25 |
(lambda: idna.alabel("a\xb7b"), idna.InvalidCodepointContext, "a\xb7b", 0xB7, 2), |
| 26 |
(lambda: idna.alabel("\u0301abc"), idna.IDNAError, "\u0301abc", 0x301, 1), |
| 27 |
(lambda: self._unknown_direction("ab"), idna.IDNABidiError, "ab", 0x61, 1), |
| 28 |
(lambda: idna.check_bidi(an + r), idna.IDNABidiError, an + r, 0x660, 1), # rule 1 |
| 29 |
(lambda: idna.check_bidi(r + "a"), idna.IDNABidiError, r + "a", 0x61, 2), # rule 2 |
| 30 |
(lambda: idna.check_bidi(r + "-" + nsm), idna.IDNABidiError, r + "-" + nsm, 0x2D, 2), # rule 3 |
| 31 |
(lambda: idna.check_bidi(r + an + "0"), idna.IDNABidiError, r + an + "0", 0x30, 3), # rule 4 |
| 32 |
(lambda: idna.alabel("a" + r), idna.IDNABidiError, "a" + r, 0x5D0, 2), # rule 5 |
| 33 |
(lambda: idna.check_bidi("a-", check_ltr=True), idna.IDNABidiError, "a-", 0x2D, 2), # rule 6 |
| 34 |
(lambda: idna.uts46_remap("a\x80"), idna.InvalidCodepoint, "a\x80", 0x80, 2), |
| 35 |
(lambda: idna.uts46_remap("a_b"), idna.InvalidCodepoint, "a_b", 0x5F, 2), |
| 36 |
(lambda: idna.uts46_remap("a\uff3fb"), idna.InvalidCodepoint, "a\uff3fb", 0xFF3F, 2), |
| 37 |
(lambda: idna.encode("a_b", uts46=True, std3_rules=True), idna.InvalidCodepoint, "a_b", 0x5F, 2), |
| 38 |
] |
| 39 |
|
| 40 |
def test_attributes_are_populated(self): |
| 41 |
for trigger, exc_class, text, codepoint, position in self._cases(): |
| 42 |
with self.subTest(text=text): |
| 43 |
with self.assertRaises(exc_class) as ctx: |
| 44 |
trigger() |
| 45 |
err = ctx.exception |
| 46 |
self.assertEqual(err.text, text) |
| 47 |
self.assertEqual(err.codepoint, codepoint) |
| 48 |
self.assertEqual(err.position, position) |
| 49 |
# position indexes text and names the codepoint ... |
| 50 |
self.assertEqual(ord(err.text[err.position - 1]), err.codepoint) |
| 51 |
# ... and agrees with the message where the message quotes one |
| 52 |
match = re.search(r"position (\d+)", str(err)) |
| 53 |
if match: |
| 54 |
self.assertEqual(int(match.group(1)), err.position) |
| 55 |
|
| 56 |
def test_every_error_code_is_documented_and_raisable(self): |
| 57 |
"""Each code has an input that produces it, and vice versa; the README |
| 58 |
table must list exactly this set.""" |
| 59 |
r = "\u05d0" |
| 60 |
an = "\u0660" |
| 61 |
triggers = { |
| 62 |
"input_too_long": lambda: idna.encode("a" * 1025), |
| 63 |
"label_too_long": lambda: idna.alabel("a" * 64), |
| 64 |
"domain_too_long": lambda: idna.encode(".".join(["a" * 63] * 4)), |
| 65 |
"empty_label": lambda: idna.encode("a..b"), |
| 66 |
"empty_domain": lambda: idna.encode(""), |
| 67 |
"not_nfc": lambda: idna.alabel("e\u0301xample"), |
| 68 |
"hyphen_3_4": lambda: idna.alabel("ab--cd"), |
| 69 |
"hyphen_start_end": lambda: idna.alabel("-abc"), |
| 70 |
"leading_combiner": lambda: idna.alabel("\u0301abc"), |
| 71 |
"disallowed_codepoint": lambda: idna.alabel("abc\u0141"), |
| 72 |
"contextj": lambda: idna.alabel("a\u200cb"), |
| 73 |
"contexto": lambda: idna.alabel("a\xb7b"), |
| 74 |
"unknown_codepoint": self._unknown_codepoint, |
| 75 |
"bidi_rule_1": lambda: idna.check_bidi(an + r), |
| 76 |
"bidi_rule_2": lambda: idna.check_bidi(r + "a"), |
| 77 |
"bidi_rule_3": lambda: idna.check_bidi(r + "-"), |
| 78 |
"bidi_rule_4": lambda: idna.check_bidi(r + an + "0"), |
| 79 |
"bidi_rule_5": lambda: idna.alabel("a" + r), |
| 80 |
"bidi_rule_6": lambda: idna.check_bidi("a-", check_ltr=True), |
| 81 |
"bidi_unknown_direction": lambda: self._unknown_direction("ab"), |
| 82 |
"invalid_alabel": lambda: idna.ulabel("xn--"), |
| 83 |
"non_canonical_alabel": lambda: idna.ulabel("xn---bbk"), |
| 84 |
"invalid_ascii": lambda: idna.encode(b"\xff"), |
| 85 |
"invalid_utf8": lambda: idna.check_label(b"\xff"), |
| 86 |
"uts46_disallowed": lambda: idna.uts46_remap("a\x80"), |
| 87 |
"uts46_std3": lambda: idna.uts46_remap("a_b"), |
| 88 |
"unsupported_errors": lambda: codecs.encode("a", "idna2008", errors="ignore"), |
| 89 |
} |
| 90 |
codes = set(get_args(idna.core._ErrorCode)) |
| 91 |
self.assertEqual(set(triggers), codes) |
| 92 |
for code, trigger in triggers.items(): |
| 93 |
with self.subTest(code=code): |
| 94 |
with self.assertRaises(idna.IDNAError) as ctx: |
| 95 |
trigger() |
| 96 |
self.assertEqual(ctx.exception.code, code) |
| 97 |
readme = Path(__file__).resolve().parent.parent / "README.md" |
| 98 |
if not readme.is_file(): |
| 99 |
self.skipTest("README.md not present") |
| 100 |
documented = set() |
| 101 |
for first, last in re.findall(r"^\| `([a-z0-9_]+)`(?: \u2026 `([a-z0-9_]+)`)? \|", readme.read_text(), re.MULTILINE): |
| 102 |
if last: # a `x_1` … `x_6` range row |
| 103 |
stem, lo = first.rsplit("_", 1) |
| 104 |
documented.update(f"{stem}_{i}" for i in range(int(lo), int(last.rsplit("_", 1)[1]) + 1)) |
| 105 |
else: |
| 106 |
documented.add(first) |
| 107 |
documented.discard("code") # the table header |
| 108 |
self.assertEqual(documented, codes) |
| 109 |
|
| 110 |
# Conditions that depend on the host's Unicode database being older than |
| 111 |
# the input cannot be triggered portably (from Python 3.15, |
| 112 |
# ``unicodedata.bidirectional`` returns a default class for every |
| 113 |
# codepoint), so simulate them. |
| 114 |
def _unknown_codepoint(self): |
| 115 |
with mock.patch("idna.core._combining_class", side_effect=ValueError): |
| 116 |
idna.check_label("a\u200cb") |
| 117 |
|
| 118 |
def _unknown_direction(self, label): |
| 119 |
with mock.patch("idna.core.unicodedata.bidirectional", return_value=""): |
| 120 |
idna.check_bidi(label) |
| 121 |
|
| 122 |
def test_unknown_codepoint_adjacent_to_joiner(self): |
| 123 |
unknown = mock.patch("idna.core._combining_class", side_effect=ValueError) |
| 124 |
with unknown, self.assertRaises(idna.IDNAError) as ctx: |
| 125 |
idna.check_label("a\u200cb") |
| 126 |
err = ctx.exception |
| 127 |
self.assertNotIsInstance(err, idna.InvalidCodepointContext) |
| 128 |
self.assertEqual(err.code, "unknown_codepoint") |
| 129 |
self.assertEqual((err.text, err.codepoint, err.position), ("a\u200cb", 0x200C, 2)) |
| 130 |
|
| 131 |
def test_positional_attributes_default_to_none(self): |
| 132 |
for trigger in ( |
| 133 |
lambda: idna.encode("a..b"), |
| 134 |
lambda: idna.encode(""), |
| 135 |
lambda: idna.alabel("a" * 64), |
| 136 |
lambda: idna.alabel("ab--cd"), |
| 137 |
lambda: idna.alabel("-abc"), |
| 138 |
lambda: idna.ulabel("xn--"), |
| 139 |
lambda: idna.encode(b"\xff"), |
| 140 |
): |
| 141 |
with self.assertRaises(idna.IDNAError) as ctx: |
| 142 |
trigger() |
| 143 |
err = ctx.exception |
| 144 |
self.assertIsNotNone(err.code) |
| 145 |
self.assertIsNone(err.text) |
| 146 |
self.assertIsNone(err.codepoint) |
| 147 |
self.assertIsNone(err.position) |
| 148 |
|
| 149 |
def test_construction_is_backwards_compatible(self): |
| 150 |
err = idna.IDNAError("just a message") |
| 151 |
self.assertEqual(str(err), "just a message") |
| 152 |
self.assertEqual(err.args, ("just a message",)) |
| 153 |
self.assertIsNone(err.code) |
| 154 |
self.assertIsNone(err.text) |
| 155 |
self.assertEqual(idna.IDNAError().args, ()) |
| 156 |
self.assertEqual(idna.InvalidCodepoint("a", "b").args, ("a", "b")) |
| 157 |
|
| 158 |
def test_message_remains_sole_positional_argument(self): |
| 159 |
with self.assertRaises(idna.InvalidCodepoint) as ctx: |
| 160 |
idna.alabel("abc\u0141") |
| 161 |
self.assertEqual(ctx.exception.args, ("Codepoint U+0141 at position 4 of 'abc\u0141' not allowed",)) |
| 162 |
|
| 163 |
def test_attributes_survive_pickle(self): |
| 164 |
with self.assertRaises(idna.InvalidCodepoint) as ctx: |
| 165 |
idna.alabel("abc\u0141") |
| 166 |
err = pickle.loads(pickle.dumps(ctx.exception)) |
| 167 |
self.assertIsInstance(err, idna.InvalidCodepoint) |
| 168 |
self.assertEqual(str(err), str(ctx.exception)) |
| 169 |
self.assertEqual(err.code, "disallowed_codepoint") |
| 170 |
self.assertEqual((err.text, err.codepoint, err.position), ("abc\u0141", 0x141, 4)) |
| 171 |
|
| 172 |
|
| 173 |
if __name__ == "__main__": |
| 174 |
unittest.main() |