Issue #7673: Fix security vulnerability (CVE-2010-2089) in the audioop module,
[python/dscho.git] / Lib / json / tests / test_scanstring.py
blob53d0b6696e339e84a3308e06c5d863a1e922d208
1 import sys
2 import decimal
3 from unittest import TestCase
5 import json
6 import json.decoder
8 class TestScanString(TestCase):
9 def test_py_scanstring(self):
10 self._test_scanstring(json.decoder.py_scanstring)
12 def test_c_scanstring(self):
13 self._test_scanstring(json.decoder.c_scanstring)
15 def _test_scanstring(self, scanstring):
16 self.assertEquals(
17 scanstring('"z\\ud834\\udd20x"', 1, None, True),
18 (u'z\U0001d120x', 16))
20 if sys.maxunicode == 65535:
21 self.assertEquals(
22 scanstring(u'"z\U0001d120x"', 1, None, True),
23 (u'z\U0001d120x', 6))
24 else:
25 self.assertEquals(
26 scanstring(u'"z\U0001d120x"', 1, None, True),
27 (u'z\U0001d120x', 5))
29 self.assertEquals(
30 scanstring('"\\u007b"', 1, None, True),
31 (u'{', 8))
33 self.assertEquals(
34 scanstring('"A JSON payload should be an object or array, not a string."', 1, None, True),
35 (u'A JSON payload should be an object or array, not a string.', 60))
37 self.assertEquals(
38 scanstring('["Unclosed array"', 2, None, True),
39 (u'Unclosed array', 17))
41 self.assertEquals(
42 scanstring('["extra comma",]', 2, None, True),
43 (u'extra comma', 14))
45 self.assertEquals(
46 scanstring('["double extra comma",,]', 2, None, True),
47 (u'double extra comma', 21))
49 self.assertEquals(
50 scanstring('["Comma after the close"],', 2, None, True),
51 (u'Comma after the close', 24))
53 self.assertEquals(
54 scanstring('["Extra close"]]', 2, None, True),
55 (u'Extra close', 14))
57 self.assertEquals(
58 scanstring('{"Extra comma": true,}', 2, None, True),
59 (u'Extra comma', 14))
61 self.assertEquals(
62 scanstring('{"Extra value after close": true} "misplaced quoted value"', 2, None, True),
63 (u'Extra value after close', 26))
65 self.assertEquals(
66 scanstring('{"Illegal expression": 1 + 2}', 2, None, True),
67 (u'Illegal expression', 21))
69 self.assertEquals(
70 scanstring('{"Illegal invocation": alert()}', 2, None, True),
71 (u'Illegal invocation', 21))
73 self.assertEquals(
74 scanstring('{"Numbers cannot have leading zeroes": 013}', 2, None, True),
75 (u'Numbers cannot have leading zeroes', 37))
77 self.assertEquals(
78 scanstring('{"Numbers cannot be hex": 0x14}', 2, None, True),
79 (u'Numbers cannot be hex', 24))
81 self.assertEquals(
82 scanstring('[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', 21, None, True),
83 (u'Too deep', 30))
85 self.assertEquals(
86 scanstring('{"Missing colon" null}', 2, None, True),
87 (u'Missing colon', 16))
89 self.assertEquals(
90 scanstring('{"Double colon":: null}', 2, None, True),
91 (u'Double colon', 15))
93 self.assertEquals(
94 scanstring('{"Comma instead of colon", null}', 2, None, True),
95 (u'Comma instead of colon', 25))
97 self.assertEquals(
98 scanstring('["Colon instead of comma": false]', 2, None, True),
99 (u'Colon instead of comma', 25))
101 self.assertEquals(
102 scanstring('["Bad value", truth]', 2, None, True),
103 (u'Bad value', 12))
105 def test_issue3623(self):
106 self.assertRaises(ValueError, json.decoder.scanstring, b"xxx", 1,
107 "xxx")
108 self.assertRaises(UnicodeDecodeError,
109 json.encoder.encode_basestring_ascii, b"xx\xff")
111 def test_overflow(self):
112 self.assertRaises(OverflowError, json.decoder.scanstring, b"xxx", sys.maxsize+1)