Debian Codegraph @

Lib/test/test_json/test_fail.py

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