Debian Codegraph @

lib-python/3/test/test_json/test_fail.py

raw symbols SCIP
1 from test.test_json import PyTest, CTest
2 from test import support
3
4 # 2007-10-05
5 JSONDOCS = [
6 # https://json.org/JSON_checker/test/fail1.json
7 '"A JSON payload should be an object or array, not a string."',
8 # https://json.org/JSON_checker/test/fail2.json
9 '["Unclosed array"',
10 # https://json.org/JSON_checker/test/fail3.json
11 '{unquoted_key: "keys must be quoted"}',
12 # https://json.org/JSON_checker/test/fail4.json
13 '["extra comma",]',
14 # https://json.org/JSON_checker/test/fail5.json
15 '["double extra comma",,]',
16 # https://json.org/JSON_checker/test/fail6.json
17 '[ , "<-- missing value"]',
18 # https://json.org/JSON_checker/test/fail7.json
19 '["Comma after the close"],',
20 # https://json.org/JSON_checker/test/fail8.json
21 '["Extra close"]]',
22 # https://json.org/JSON_checker/test/fail9.json
23 '{"Extra comma": true,}',
24 # https://json.org/JSON_checker/test/fail10.json
25 '{"Extra value after close": true} "misplaced quoted value"',
26 # https://json.org/JSON_checker/test/fail11.json
27 '{"Illegal expression": 1 + 2}',
28 # https://json.org/JSON_checker/test/fail12.json
29 '{"Illegal invocation": alert()}',
30 # https://json.org/JSON_checker/test/fail13.json
31 '{"Numbers cannot have leading zeroes": 013}',
32 # https://json.org/JSON_checker/test/fail14.json
33 '{"Numbers cannot be hex": 0x14}',
34 # https://json.org/JSON_checker/test/fail15.json
35 '["Illegal backslash escape: \\x15"]',
36 # https://json.org/JSON_checker/test/fail16.json
37 '[\\naked]',
38 # https://json.org/JSON_checker/test/fail17.json
39 '["Illegal backslash escape: \\017"]',
40 # https://json.org/JSON_checker/test/fail18.json
41 '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]',
42 # https://json.org/JSON_checker/test/fail19.json
43 '{"Missing colon" null}',
44 # https://json.org/JSON_checker/test/fail20.json
45 '{"Double colon":: null}',
46 # https://json.org/JSON_checker/test/fail21.json
47 '{"Comma instead of colon", null}',
48 # https://json.org/JSON_checker/test/fail22.json
49 '["Colon instead of comma": false]',
50 # https://json.org/JSON_checker/test/fail23.json
51 '["Bad value", truth]',
52 # https://json.org/JSON_checker/test/fail24.json
53 "['single quote']",
54 # https://json.org/JSON_checker/test/fail25.json
55 '["\ttab\tcharacter\tin\tstring\t"]',
56 # https://json.org/JSON_checker/test/fail26.json
57 '["tab\\ character\\ in\\ string\\ "]',
58 # https://json.org/JSON_checker/test/fail27.json
59 '["line\nbreak"]',
60 # https://json.org/JSON_checker/test/fail28.json
61 '["line\\\nbreak"]',
62 # https://json.org/JSON_checker/test/fail29.json
63 '[0e]',
64 # https://json.org/JSON_checker/test/fail30.json
65 '[0e+]',
66 # https://json.org/JSON_checker/test/fail31.json
67 '[0e+-1]',
68 # https://json.org/JSON_checker/test/fail32.json
69 '{"Comma instead if closing brace": true,',
70 # https://json.org/JSON_checker/test/fail33.json
71 '["mismatch"}',
72 # https://code.google.com/archive/p/simplejson/issues/3
73 '["A\u001FZ control characters in string"]',
74 ]
75
76 SKIPS = {
77 1: "why not have a string payload?",
78 18: "spec doesn't specify any nesting limitations",
79 }
80
81 class TestFail:
82 def test_failures(self):
83 for idx, doc in enumerate(JSONDOCS):
84 idx = idx + 1
85 if idx in SKIPS:
86 self.loads(doc)
87 continue
88 try:
89 self.loads(doc)
90 except self.JSONDecodeError:
91 pass
92 else:
93 self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
94
95 def test_non_string_keys_dict(self):
96 data = {'a' : 1, (1, 2) : 2}
97 with self.assertRaisesRegex(TypeError,
98 'keys must be str, int, float, bool or None, not tuple'):
99 self.dumps(data)
100
101 def test_not_serializable(self):
102 import sys
103 with self.assertRaisesRegex(TypeError,
104 'Object of type module is not JSON serializable'):
105 self.dumps(sys)
106
107 def test_truncated_input(self):
108 test_cases = [
109 ('', 'Expecting value', 0),
110 ('[', 'Expecting value', 1),
111 ('[42', "Expecting ',' delimiter", 3),
112 ('[42,', 'Expecting value', 4),
113 ('["', 'Unterminated string starting at', 1),
114 ('["spam', 'Unterminated string starting at', 1),
115 ('["spam"', "Expecting ',' delimiter", 7),
116 ('["spam",', 'Expecting value', 8),
117 ('{', 'Expecting property name enclosed in double quotes', 1),
118 ('{"', 'Unterminated string starting at', 1),
119 ('{"spam', 'Unterminated string starting at', 1),
120 ('{"spam"', "Expecting ':' delimiter", 7),
121 ('{"spam":', 'Expecting value', 8),
122 ('{"spam":42', "Expecting ',' delimiter", 10),
123 ('{"spam":42,', 'Expecting property name enclosed in double quotes', 11),
124 ]
125 test_cases += [
126 ('"', 'Unterminated string starting at', 0),
127 ('"spam', 'Unterminated string starting at', 0),
128 ]
129 for data, msg, idx in test_cases:
130 with self.assertRaises(self.JSONDecodeError) as cm:
131 self.loads(data)
132 err = cm.exception
133 if support.check_impl_detail():
134 self.assertEqual(err.msg, msg)
135 else:
136 if data in ['[42', # skip these tests, PyPy gets
137 '["spam"', # another error which makes sense
138 '{"spam":42', # too: unterminated array
139 ]:
140 continue
141 msg = err.msg # ignore the message provided in the test,
142 # only check for position
143 self.assertEqual(err.pos, idx)
144 self.assertEqual(err.lineno, 1)
145 self.assertEqual(err.colno, idx + 1)
146 self.assertEqual(str(err),
147 '%s: line 1 column %d (char %d)' %
148 (msg, idx + 1, idx))
149
150 def test_unexpected_data(self):
151 test_cases = [
152 ('[,', 'Expecting value', 1),
153 ('{"spam":[}', 'Expecting value', 9),
154 ('[42:', "Expecting ',' delimiter", 3),
155 ('[42 "spam"', "Expecting ',' delimiter", 4),
156 ('[42,]', 'Expecting value', 4),
157 ('{"spam":[42}', "Expecting ',' delimiter", 11),
158 ('["]', 'Unterminated string starting at', 1),
159 ('["spam":', "Expecting ',' delimiter", 7),
160 ('["spam",]', 'Expecting value', 8),
161 ('{:', 'Expecting property name enclosed in double quotes', 1),
162 ('{,', 'Expecting property name enclosed in double quotes', 1),
163 ('{42', 'Expecting property name enclosed in double quotes', 1),
164 ('[{]', 'Expecting property name enclosed in double quotes', 2),
165 ('{"spam",', "Expecting ':' delimiter", 7),
166 ('{"spam"}', "Expecting ':' delimiter", 7),
167 ('[{"spam"]', "Expecting ':' delimiter", 8),
168 ('{"spam":}', 'Expecting value', 8),
169 ('[{"spam":]', 'Expecting value', 9),
170 ('{"spam":42 "ham"', "Expecting ',' delimiter", 11),
171 ('[{"spam":42]', "Expecting ',' delimiter", 11),
172 ('{"spam":42,}', 'Expecting property name enclosed in double quotes', 11),
173 ]
174 for data, msg, idx in test_cases:
175 with self.assertRaises(self.JSONDecodeError) as cm:
176 self.loads(data)
177 err = cm.exception
178 if support.check_impl_detail():
179 self.assertEqual(err.msg, msg)
180 else:
181 msg = err.msg # ignore the message provided in the test,
182 # only check for position
183 self.assertEqual(err.pos, idx)
184 self.assertEqual(err.lineno, 1)
185 self.assertEqual(err.colno, idx + 1)
186 self.assertEqual(str(err),
187 '%s: line 1 column %d (char %d)' %
188 (msg, idx + 1, idx))
189
190 def test_extra_data(self):
191 test_cases = [
192 ('[]]', 'Extra data', 2),
193 ('{}}', 'Extra data', 2),
194 ('[],[]', 'Extra data', 2),
195 ('{},{}', 'Extra data', 2),
196 ]
197 test_cases += [
198 ('42,"spam"', 'Extra data', 2),
199 ('"spam",42', 'Extra data', 6),
200 ]
201 for data, msg, idx in test_cases:
202 with self.assertRaises(self.JSONDecodeError) as cm:
203 self.loads(data)
204 err = cm.exception
205 self.assertEqual(err.msg, msg)
206 self.assertEqual(err.pos, idx)
207 self.assertEqual(err.lineno, 1)
208 self.assertEqual(err.colno, idx + 1)
209 self.assertEqual(str(err),
210 '%s: line 1 column %d (char %d)' %
211 (msg, idx + 1, idx))
212
213 def test_linecol(self):
214 test_cases = [
215 ('!', 1, 1, 0),
216 (' !', 1, 2, 1),
217 ('\n!', 2, 1, 1),
218 ('\n \n\n !', 4, 6, 10),
219 ]
220 for data, line, col, idx in test_cases:
221 with self.assertRaises(self.JSONDecodeError) as cm:
222 self.loads(data)
223 err = cm.exception
224 if support.check_impl_detail():
225 msg = 'Expecting value'
226 self.assertEqual(err.msg, msg)
227 else:
228 msg = err.msg # ignore the message provided in the test,
229 # only check for position
230 self.assertEqual(err.pos, idx)
231 self.assertEqual(err.lineno, line)
232 self.assertEqual(err.colno, col)
233 self.assertEqual(str(err),
234 '%s: line %s column %d (char %d)' %
235 (msg, line, col, idx))
236
237 class TestPyFail(TestFail, PyTest): pass
238 class TestCFail(TestFail, CTest): pass