File: test_stringprep.py

package info (click to toggle)
python-aioxmpp 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 6,244 kB
  • sloc: python: 97,761; xml: 215; makefile: 155; sh: 63
file content (231 lines) | stat: -rw-r--r-- 7,175 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
########################################################################
# File name: test_stringprep.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import unittest

from aioxmpp.stringprep import (
    nodeprep, resourceprep, nameprep,
    check_bidi, unicodedata
)


class TestUnicodeVersion(unittest.TestCase):

    def test_version(self):
        self.assertEqual(unicodedata.unidata_version, "3.2.0")


class Testcheck_bidi(unittest.TestCase):
    # some test cases which are not covered by the other tests
    def test_empty_string(self):
        check_bidi("")

    def test_L_RAL_violation(self):
        with self.assertRaises(ValueError):
            check_bidi("\u05be\u0041")


class TestNodeprep(unittest.TestCase):
    def test_map_to_nothing(self):
        self.assertEqual(
            "ix",
            nodeprep("I\u00ADX"),
            "Nodeprep requirement: map SOFT HYPHEN to nothing")

    def test_case_fold(self):
        self.assertEqual(
            "ssa",
            nodeprep("ßA"),
            "Nodeprep requirement: map ß to ss, A to a")

    def test_nfkc(self):
        self.assertEqual(
            "a",
            nodeprep("\u00AA"),
            "Nodeprep requirement: NFKC")
        self.assertEqual(
            "ix",
            nodeprep("\u2168"),
            "Nodeprep requirement: NFKC")

    def test_prohibited_character(self):
        with self.assertRaisesRegex(
                ValueError,
                r"U\+0007",
                msg="Nodeprep requirement: prohibited character (C.2.1)"):
            nodeprep("\u0007")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+200e",
                msg="Nodeprep requirement: prohibited character (C.8)"):
            nodeprep("\u200E")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+003e",
                msg="Nodeprep requirement: prohibited character (custom)"):
            nodeprep(">")

    def test_unassigned(self):
        with self.assertRaises(
                ValueError,
                msg="Nodeprep requirement: unassigned"):
            nodeprep("\u0221", allow_unassigned=False)

        with self.assertRaises(
                ValueError,
                msg="enforce no unassigned by default"):
            nodeprep("\u0221")

        self.assertEqual(
            "\u0221",
            nodeprep("\u0221", allow_unassigned=True))


class TestNameprep(unittest.TestCase):
    def test_map_to_nothing(self):
        self.assertEqual(
            "ix",
            nameprep("I\u00ADX"),
            "Nameprep requirement: map SOFT HYPHEN to nothing")

    def test_case_fold(self):
        self.assertEqual(
            "ssa",
            nameprep("ßA"),
            "Nameprep requirement: map ß to ss, A to a")

    def test_nfkc(self):
        self.assertEqual(
            "a",
            nodeprep("\u00AA"),
            "Nameprep requirement: NFKC")
        self.assertEqual(
            "ix",
            nodeprep("\u2168"),
            "Nameprep requirement: NFKC")

    def test_prohibited_character(self):
        with self.assertRaisesRegex(
                ValueError,
                r"U\+06dd",
                msg="Nameprep requirement: prohibited character (C.2.2)"):
            nameprep("\u06DD")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+e000",
                msg="Nameprep requirement: prohibited character (C.3)"):
            nameprep("\uE000")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+1fffe",
                msg="Nameprep requirement: prohibited character (C.4)"):
            nameprep("\U0001FFFE")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+d800",
                msg="Nameprep requirement: prohibited character (C.5)"):
            nameprep("\uD800")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+fff9",
                msg="Nameprep requirement: prohibited character (C.6)"):
            nameprep("\uFFF9")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+2ff0",
                msg="Nameprep requirement: prohibited character (C.7)"):
            nameprep("\u2FF0")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+e0001",
                msg="Nameprep requirement: prohibited character (C.9)"):
            nameprep("\U000E0001")

    def test_unassigned(self):
        with self.assertRaises(
                ValueError,
                msg="Nameprep requirement: unassigned"):
            nameprep("\u0221", allow_unassigned=False)

        with self.assertRaises(
                ValueError,
                msg="enforce no unassigned by default"):
            nameprep("\u0221")

        self.assertEqual(
            "\u0221",
            nameprep("\u0221", allow_unassigned=True))


class TestResourceprep(unittest.TestCase):
    def test_map_to_nothing(self):
        self.assertEqual(
            "IX",
            resourceprep("I\u00ADX"),
            "Resourceprep requirement: map SOFT HYPHEN to nothing")

    def test_nfkc(self):
        self.assertEqual(
            "a",
            resourceprep("\u00AA"),
            "Resourceprep requirement: NFKC")
        self.assertEqual(
            "IX",
            resourceprep("\u2168"),
            "Resourceprep requirement: NFKC")

    def test_prohibited_character(self):
        with self.assertRaisesRegex(
                ValueError,
                r"U\+0007",
                msg="Resourceprep requirement: "
                    "prohibited character (C.2.1)"):
            resourceprep("\u0007")

        with self.assertRaisesRegex(
                ValueError,
                r"U\+200e",
                msg="Resourceprep requirement: "
                    "prohibited character (C.8)"):
            resourceprep("\u200E")

    def test_unassigned(self):
        with self.assertRaises(
                ValueError,
                msg="Resourceprep requirement: unassigned"):
            resourceprep("\u0221", allow_unassigned=False)

        with self.assertRaises(
                ValueError,
                msg="enforce no unassigned by default"):
            resourceprep("\u0221")

        self.assertEqual(
            "\u0221",
            resourceprep("\u0221", allow_unassigned=True))