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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "testing/gtest/include/gtest/gtest.h"
#include "base/json_reader.h"
#include "base/values.h"
#include "build/build_config.h"
TEST(JSONReaderTest, Reading) {
// some whitespace checking
Value* root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue(" null ", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_NULL));
delete root;
// Invalid JSON string
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("nu", &root, false, false));
ASSERT_FALSE(root);
// Simple bool
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("true ", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
delete root;
// Test number formats
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("43", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
int int_val = 0;
ASSERT_TRUE(root->GetAsInteger(&int_val));
ASSERT_EQ(43, int_val);
delete root;
// According to RFC4627, oct, hex, and leading zeros are invalid JSON.
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("043", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("0x43", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("00", &root, false, false));
ASSERT_FALSE(root);
// Test 0 (which needs to be special cased because of the leading zero
// clause).
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("0", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
int_val = 1;
ASSERT_TRUE(root->GetAsInteger(&int_val));
ASSERT_EQ(0, int_val);
delete root;
// Numbers that overflow ints should succeed, being internally promoted to
// storage as doubles
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("2147483648", &root, false, false));
ASSERT_TRUE(root);
double real_val;
#ifdef ARCH_CPU_32_BITS
ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0;
ASSERT_TRUE(root->GetAsReal(&real_val));
ASSERT_DOUBLE_EQ(2147483648.0, real_val);
#else
ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
int_val = 0;
ASSERT_TRUE(root->GetAsInteger(&int_val));
ASSERT_EQ(2147483648, int_val);
#endif
delete root;
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("-2147483649", &root, false, false));
ASSERT_TRUE(root);
#ifdef ARCH_CPU_32_BITS
ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0;
ASSERT_TRUE(root->GetAsReal(&real_val));
ASSERT_DOUBLE_EQ(-2147483649.0, real_val);
#else
ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
int_val = 0;
ASSERT_TRUE(root->GetAsInteger(&int_val));
ASSERT_EQ(-2147483649, int_val);
#endif
delete root;
// Parse a double
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("43.1", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0;
ASSERT_TRUE(root->GetAsReal(&real_val));
ASSERT_DOUBLE_EQ(43.1, real_val);
delete root;
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("4.3e-1", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0;
ASSERT_TRUE(root->GetAsReal(&real_val));
ASSERT_DOUBLE_EQ(.43, real_val);
delete root;
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("2.1e0", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0;
ASSERT_TRUE(root->GetAsReal(&real_val));
ASSERT_DOUBLE_EQ(2.1, real_val);
delete root;
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("2.1e+0001", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0;
ASSERT_TRUE(root->GetAsReal(&real_val));
ASSERT_DOUBLE_EQ(21.0, real_val);
delete root;
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("0.01", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0;
ASSERT_TRUE(root->GetAsReal(&real_val));
ASSERT_DOUBLE_EQ(0.01, real_val);
delete root;
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("1.00", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
real_val = 0.0;
ASSERT_TRUE(root->GetAsReal(&real_val));
ASSERT_DOUBLE_EQ(1.0, real_val);
delete root;
// Fractional parts must have a digit before and after the decimal point.
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1.", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue(".1", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1.e10", &root, false, false));
ASSERT_FALSE(root);
// Exponent must have a digit following the 'e'.
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1e", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1E", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1e1.", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1e1.0", &root, false, false));
ASSERT_FALSE(root);
// INF/-INF/NaN are not valid
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("1e1000", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("-1e1000", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("NaN", &root, false, false));
ASSERT_FALSE(root);
// Invalid number formats
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("4.3.1", &root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("4e3.1", &root, false, false));
ASSERT_FALSE(root);
// Test string parser
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"hello world\"", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
std::wstring str_val;
ASSERT_TRUE(root->GetAsString(&str_val));
ASSERT_EQ(L"hello world", str_val);
delete root;
// Empty string
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"\"", &root, false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
ASSERT_TRUE(root->GetAsString(&str_val));
ASSERT_EQ(L"", str_val);
delete root;
// Test basic string escapes
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\"", &root,
false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
ASSERT_TRUE(root->GetAsString(&str_val));
ASSERT_EQ(L" \"\\/\b\f\n\r\t", str_val);
delete root;
// Test hex and unicode escapes including the null character.
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"\\x41\\x00\\u1234\"", &root, false,
false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
ASSERT_TRUE(root->GetAsString(&str_val));
ASSERT_EQ(std::wstring(L"A\0\x1234", 3), str_val);
delete root;
// Test invalid strings
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"no closing quote", &root, false,
false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"\\z invalid escape char\"", &root,
false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"\\xAQ invalid hex code\"", &root,
false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("not enough hex chars\\x1\"", &root,
false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"not enough escape chars\\u123\"",
&root, false, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::JsonToValue("\"extra backslash at end of input\\\"",
&root, false, false));
ASSERT_FALSE(root);
// Basic array
root = NULL;
ASSERT_TRUE(JSONReader::Read("[true, false, null]", &root, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
ListValue* list = static_cast<ListValue*>(root);
ASSERT_EQ(3U, list->GetSize());
// Test with trailing comma. Should be parsed the same as above.
Value* root2 = NULL;
ASSERT_TRUE(JSONReader::Read("[true, false, null, ]", &root2, true));
EXPECT_TRUE(root->Equals(root2));
delete root;
delete root2;
// Empty array
root = NULL;
ASSERT_TRUE(JSONReader::Read("[]", &root, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root);
ASSERT_EQ(0U, list->GetSize());
delete root;
// Nested arrays
root = NULL;
ASSERT_TRUE(JSONReader::Read("[[true], [], [false, [], [null]], null]", &root,
false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root);
ASSERT_EQ(4U, list->GetSize());
// Lots of trailing commas.
root2 = NULL;
ASSERT_TRUE(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]",
&root2, true));
EXPECT_TRUE(root->Equals(root2));
delete root;
delete root2;
// Invalid, missing close brace.
root = NULL;
ASSERT_FALSE(JSONReader::Read("[[true], [], [false, [], [null]], null", &root,
false));
ASSERT_FALSE(root);
// Invalid, too many commas
root = NULL;
ASSERT_FALSE(JSONReader::Read("[true,, null]", &root, false));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("[true,, null]", &root, true));
ASSERT_FALSE(root);
// Invalid, no commas
root = NULL;
ASSERT_FALSE(JSONReader::Read("[true null]", &root, false));
ASSERT_FALSE(root);
// Invalid, trailing comma
root = NULL;
ASSERT_FALSE(JSONReader::Read("[true,]", &root, false));
ASSERT_FALSE(root);
// Valid if we set |allow_trailing_comma| to true.
EXPECT_TRUE(JSONReader::Read("[true,]", &root, true));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root);
EXPECT_EQ(1U, list->GetSize());
Value* tmp_value = NULL;
ASSERT_TRUE(list->Get(0, &tmp_value));
EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN));
bool bool_value = false;
ASSERT_TRUE(tmp_value->GetAsBoolean(&bool_value));
EXPECT_TRUE(bool_value);
delete root;
// Don't allow empty elements, even if |allow_trailing_comma| is
// true.
root = NULL;
EXPECT_FALSE(JSONReader::Read("[,]", &root, true));
EXPECT_FALSE(root);
EXPECT_FALSE(JSONReader::Read("[true,,]", &root, true));
EXPECT_FALSE(root);
EXPECT_FALSE(JSONReader::Read("[,true,]", &root, true));
EXPECT_FALSE(root);
EXPECT_FALSE(JSONReader::Read("[true,,false]", &root, true));
EXPECT_FALSE(root);
// Test objects
root = NULL;
ASSERT_TRUE(JSONReader::Read("{}", &root, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
delete root;
root = NULL;
ASSERT_TRUE(JSONReader::Read(
"{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", &root,
false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
DictionaryValue* dict_val = static_cast<DictionaryValue*>(root);
real_val = 0.0;
ASSERT_TRUE(dict_val->GetReal(L"number", &real_val));
ASSERT_DOUBLE_EQ(9.87654321, real_val);
Value* null_val = NULL;
ASSERT_TRUE(dict_val->Get(L"null", &null_val));
ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL));
str_val.clear();
ASSERT_TRUE(dict_val->GetString(L"S", &str_val));
ASSERT_EQ(L"str", str_val);
root2 = NULL;
ASSERT_TRUE(JSONReader::Read(
"{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", &root2,
true));
EXPECT_TRUE(root->Equals(root2));
delete root;
delete root2;
// Test nesting
root = NULL;
ASSERT_TRUE(JSONReader::Read(
"{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", &root, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
dict_val = static_cast<DictionaryValue*>(root);
DictionaryValue* inner_dict = NULL;
ASSERT_TRUE(dict_val->GetDictionary(L"inner", &inner_dict));
ListValue* inner_array = NULL;
ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array));
ASSERT_EQ(1U, inner_array->GetSize());
bool_value = true;
ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value));
ASSERT_FALSE(bool_value);
inner_dict = NULL;
ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict));
root2 = NULL;
ASSERT_TRUE(JSONReader::Read(
"{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", &root2,
true));
EXPECT_TRUE(root->Equals(root2));
delete root;
delete root2;
// Invalid, no closing brace
root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\": true", &root, false));
ASSERT_FALSE(root);
// Invalid, keys must be quoted
root = NULL;
ASSERT_FALSE(JSONReader::Read("{foo:true}", &root, false));
ASSERT_FALSE(root);
// Invalid, trailing comma
root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\":true,}", &root, false));
ASSERT_FALSE(root);
// Invalid, too many commas
root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, false));
ASSERT_FALSE(root);
root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, true));
ASSERT_FALSE(root);
// Invalid, no separator
root = NULL;
ASSERT_FALSE(JSONReader::Read("{\"a\" \"b\"}", &root, false));
ASSERT_FALSE(root);
// Invalid, lone comma.
root = NULL;
ASSERT_FALSE(JSONReader::Read("{,}", &root, false));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("{,}", &root, true));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("{\"a\":true,,}", &root, true));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("{,\"a\":true}", &root, true));
ASSERT_FALSE(root);
ASSERT_FALSE(JSONReader::Read("{\"a\":true,,\"b\":false}", &root, true));
ASSERT_FALSE(root);
// Test stack overflow
root = NULL;
std::string evil(1000000, '[');
evil.append(std::string(1000000, ']'));
ASSERT_FALSE(JSONReader::Read(evil, &root, false));
ASSERT_FALSE(root);
// A few thousand adjacent lists is fine.
std::string not_evil("[");
not_evil.reserve(15010);
for (int i = 0; i < 5000; ++i) {
not_evil.append("[],");
}
not_evil.append("[]]");
ASSERT_TRUE(JSONReader::Read(not_evil, &root, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
list = static_cast<ListValue*>(root);
ASSERT_EQ(5001U, list->GetSize());
delete root;
// Test utf8 encoded input
root = NULL;
ASSERT_TRUE(JSONReader::JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", &root,
false, false));
ASSERT_TRUE(root);
ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
str_val.clear();
ASSERT_TRUE(root->GetAsString(&str_val));
ASSERT_EQ(L"\x7f51\x9875", str_val);
delete root;
// Test invalid root objects.
root = NULL;
ASSERT_FALSE(JSONReader::Read("null", &root, false));
ASSERT_FALSE(JSONReader::Read("true", &root, false));
ASSERT_FALSE(JSONReader::Read("10", &root, false));
ASSERT_FALSE(JSONReader::Read("\"root\"", &root, false));
}
|