forked from davfsa/orjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ujson.py
346 lines (276 loc) · 10.5 KB
/
test_ujson.py
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import json
import pytest
import orjson
class TestUltraJSON:
def test_doubleLongIssue(self):
sut = {"a": -4342969734183514}
encoded = orjson.dumps(sut)
decoded = orjson.loads(encoded)
assert sut == decoded
encoded = orjson.dumps(sut)
decoded = orjson.loads(encoded)
assert sut == decoded
def test_doubleLongDecimalIssue(self):
sut = {"a": -12345678901234.56789012}
encoded = orjson.dumps(sut)
decoded = orjson.loads(encoded)
assert sut == decoded
encoded = orjson.dumps(sut)
decoded = orjson.loads(encoded)
assert sut == decoded
def test_encodeDecodeLongDecimal(self):
sut = {"a": -528656961.4399388}
encoded = orjson.dumps(sut)
orjson.loads(encoded)
def test_decimalDecodeTest(self):
sut = {"a": 4.56}
encoded = orjson.dumps(sut)
decoded = orjson.loads(encoded)
pytest.approx(sut["a"], decoded["a"])
def test_encodeDictWithUnicodeKeys(self):
val = {
"key1": "value1",
"key1": "value1",
"key1": "value1",
"key1": "value1",
"key1": "value1",
"key1": "value1",
}
orjson.dumps(val)
val = {
"بن": "value1",
"بن": "value1",
"بن": "value1",
"بن": "value1",
"بن": "value1",
"بن": "value1",
"بن": "value1",
}
orjson.dumps(val)
def test_encodeArrayOfNestedArrays(self):
val = [[[[]]]] * 20 # type: ignore
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert val == orjson.loads(output)
def test_encodeArrayOfDoubles(self):
val = [31337.31337, 31337.31337, 31337.31337, 31337.31337] * 10
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert val == orjson.loads(output)
def test_encodeStringConversion2(self):
val = "A string \\ / \b \f \n \r \t"
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == b'"A string \\\\ / \\b \\f \\n \\r \\t"'
assert val == orjson.loads(output)
def test_decodeUnicodeConversion(self):
pass
def test_encodeUnicodeConversion1(self):
val = "Räksmörgås اسامة بن محمد بن عوض بن لادن"
enc = orjson.dumps(val)
dec = orjson.loads(enc)
assert enc == orjson.dumps(val)
assert dec == orjson.loads(enc)
def test_encodeControlEscaping(self):
val = "\x19"
enc = orjson.dumps(val)
dec = orjson.loads(enc)
assert val == dec
assert enc == orjson.dumps(val)
def test_encodeUnicodeConversion2(self):
val = "\xe6\x97\xa5\xd1\x88"
enc = orjson.dumps(val)
dec = orjson.loads(enc)
assert enc == orjson.dumps(val)
assert dec == orjson.loads(enc)
def test_encodeUnicodeSurrogatePair(self):
val = "\xf0\x90\x8d\x86"
enc = orjson.dumps(val)
dec = orjson.loads(enc)
assert enc == orjson.dumps(val)
assert dec == orjson.loads(enc)
def test_encodeUnicode4BytesUTF8(self):
val = "\xf0\x91\x80\xb0TRAILINGNORMAL"
enc = orjson.dumps(val)
dec = orjson.loads(enc)
assert enc == orjson.dumps(val)
assert dec == orjson.loads(enc)
def test_encodeUnicode4BytesUTF8Highest(self):
val = "\xf3\xbf\xbf\xbfTRAILINGNORMAL"
enc = orjson.dumps(val)
dec = orjson.loads(enc)
assert enc == orjson.dumps(val)
assert dec == orjson.loads(enc)
def testEncodeUnicodeBMP(self):
s = "\U0001f42e\U0001f42e\U0001f42d\U0001f42d" # 🐮🐮🐭🐭
orjson.dumps(s)
json.dumps(s)
assert json.loads(json.dumps(s)) == s
assert orjson.loads(orjson.dumps(s)) == s
def testEncodeSymbols(self):
s = "\u273f\u2661\u273f" # ✿♡✿
encoded = orjson.dumps(s)
encoded_json = json.dumps(s)
decoded = orjson.loads(encoded)
assert s == decoded
encoded = orjson.dumps(s)
# json outputs an unicode object
encoded_json = json.dumps(s, ensure_ascii=False)
assert encoded == encoded_json.encode("utf-8")
decoded = orjson.loads(encoded)
assert s == decoded
def test_encodeArrayInArray(self):
val = [[[[]]]] # type: ignore
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
def test_encodeIntConversion(self):
val = 31337
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
def test_encodeIntNegConversion(self):
val = -31337
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
def test_encodeLongNegConversion(self):
val = -9223372036854775808
output = orjson.dumps(val)
orjson.loads(output)
orjson.loads(output)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
def test_encodeListConversion(self):
val = [1, 2, 3, 4]
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert val == orjson.loads(output)
def test_encodeDictConversion(self):
val = {"k1": 1, "k2": 2, "k3": 3, "k4": 4}
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert val == orjson.loads(output)
assert val == orjson.loads(output)
def test_encodeNoneConversion(self):
val = None
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
def test_encodeTrueConversion(self):
val = True
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
def test_encodeFalseConversion(self):
val = False
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
def test_encodeToUTF8(self):
val = b"\xe6\x97\xa5\xd1\x88".decode("utf-8")
enc = orjson.dumps(val)
dec = orjson.loads(enc)
assert enc == orjson.dumps(val)
assert dec == orjson.loads(enc)
def test_decodeFromUnicode(self):
val = '{"obj": 31337}'
dec1 = orjson.loads(val)
dec2 = orjson.loads(str(val))
assert dec1 == dec2
def test_decodeJibberish(self):
val = "fdsa sda v9sa fdsa"
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeBrokenArrayStart(self):
val = "["
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeBrokenObjectStart(self):
val = "{"
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeBrokenArrayEnd(self):
val = "]"
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeBrokenObjectEnd(self):
val = "}"
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeObjectDepthTooBig(self):
val = "{" * (1024 * 1024)
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeStringUnterminated(self):
val = '"TESTING'
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeStringUntermEscapeSequence(self):
val = '"TESTING\\"'
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeStringBadEscape(self):
val = '"TESTING\\"'
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeTrueBroken(self):
val = "tru"
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeFalseBroken(self):
val = "fa"
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeNullBroken(self):
val = "n"
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeBrokenDictKeyTypeLeakTest(self):
val = '{{1337:""}}'
for _ in range(1000):
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeBrokenDictLeakTest(self):
val = '{{"key":"}'
for _ in range(1000):
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeBrokenListLeakTest(self):
val = "[[[true"
for _ in range(1000):
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeDictWithNoKey(self):
val = "{{{{31337}}}}"
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeDictWithNoColonOrValue(self):
val = '{{{{"key"}}}}'
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeDictWithNoValue(self):
val = '{{{{"key":}}}}'
pytest.raises(orjson.JSONDecodeError, orjson.loads, val)
def test_decodeNumericIntPos(self):
val = "31337"
assert 31337 == orjson.loads(val)
def test_decodeNumericIntNeg(self):
assert -31337 == orjson.loads("-31337")
def test_encodeNullCharacter(self):
val = "31337 \x00 1337"
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
val = "\x00"
output = orjson.dumps(val)
assert val == orjson.loads(output)
assert output == orjson.dumps(val)
assert val == orjson.loads(output)
assert b'" \\u0000\\r\\n "' == orjson.dumps(" \u0000\r\n ")
def test_decodeNullCharacter(self):
val = '"31337 \\u0000 31337"'
assert orjson.loads(val) == json.loads(val)
def test_decodeEscape(self):
base = "\u00e5".encode()
quote = b'"'
val = quote + base + quote
assert json.loads(val) == orjson.loads(val)
def test_decodeBigEscape(self):
for _ in range(10):
base = "\u00e5".encode()
quote = b'"'
val = quote + (base * 1024 * 1024 * 2) + quote
assert json.loads(val) == orjson.loads(val)