1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "base/json/json_reader.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/string_piece.h"
9 #include "base/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "build/build_config.h"
12
13 namespace base {
14
TEST(JSONReaderTest,Reading)15 TEST(JSONReaderTest, Reading) {
16 // some whitespace checking
17 scoped_ptr<Value> root;
18 root.reset(JSONReader().JsonToValue(" null ", false, false));
19 ASSERT_TRUE(root.get());
20 ASSERT_TRUE(root->IsType(Value::TYPE_NULL));
21
22 // Invalid JSON string
23 root.reset(JSONReader().JsonToValue("nu", false, false));
24 ASSERT_FALSE(root.get());
25
26 // Simple bool
27 root.reset(JSONReader().JsonToValue("true ", false, false));
28 ASSERT_TRUE(root.get());
29 ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
30
31 // Embedded comment
32 root.reset(JSONReader().JsonToValue("/* comment */null", false, false));
33 ASSERT_TRUE(root.get());
34 ASSERT_TRUE(root->IsType(Value::TYPE_NULL));
35 root.reset(JSONReader().JsonToValue("40 /* comment */", false, false));
36 ASSERT_TRUE(root.get());
37 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
38 root.reset(JSONReader().JsonToValue("true // comment", false, false));
39 ASSERT_TRUE(root.get());
40 ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
41 root.reset(JSONReader().JsonToValue("/* comment */\"sample string\"",
42 false, false));
43 ASSERT_TRUE(root.get());
44 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
45 std::string value;
46 ASSERT_TRUE(root->GetAsString(&value));
47 ASSERT_EQ("sample string", value);
48
49 // Test number formats
50 root.reset(JSONReader().JsonToValue("43", false, false));
51 ASSERT_TRUE(root.get());
52 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
53 int int_val = 0;
54 ASSERT_TRUE(root->GetAsInteger(&int_val));
55 ASSERT_EQ(43, int_val);
56
57 // According to RFC4627, oct, hex, and leading zeros are invalid JSON.
58 root.reset(JSONReader().JsonToValue("043", false, false));
59 ASSERT_FALSE(root.get());
60 root.reset(JSONReader().JsonToValue("0x43", false, false));
61 ASSERT_FALSE(root.get());
62 root.reset(JSONReader().JsonToValue("00", false, false));
63 ASSERT_FALSE(root.get());
64
65 // Test 0 (which needs to be special cased because of the leading zero
66 // clause).
67 root.reset(JSONReader().JsonToValue("0", false, false));
68 ASSERT_TRUE(root.get());
69 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
70 int_val = 1;
71 ASSERT_TRUE(root->GetAsInteger(&int_val));
72 ASSERT_EQ(0, int_val);
73
74 // Numbers that overflow ints should succeed, being internally promoted to
75 // storage as doubles
76 root.reset(JSONReader().JsonToValue("2147483648", false, false));
77 ASSERT_TRUE(root.get());
78 double double_val;
79 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE));
80 double_val = 0.0;
81 ASSERT_TRUE(root->GetAsDouble(&double_val));
82 ASSERT_DOUBLE_EQ(2147483648.0, double_val);
83 root.reset(JSONReader().JsonToValue("-2147483649", false, false));
84 ASSERT_TRUE(root.get());
85 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE));
86 double_val = 0.0;
87 ASSERT_TRUE(root->GetAsDouble(&double_val));
88 ASSERT_DOUBLE_EQ(-2147483649.0, double_val);
89
90 // Parse a double
91 root.reset(JSONReader().JsonToValue("43.1", false, false));
92 ASSERT_TRUE(root.get());
93 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE));
94 double_val = 0.0;
95 ASSERT_TRUE(root->GetAsDouble(&double_val));
96 ASSERT_DOUBLE_EQ(43.1, double_val);
97
98 root.reset(JSONReader().JsonToValue("4.3e-1", false, false));
99 ASSERT_TRUE(root.get());
100 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE));
101 double_val = 0.0;
102 ASSERT_TRUE(root->GetAsDouble(&double_val));
103 ASSERT_DOUBLE_EQ(.43, double_val);
104
105 root.reset(JSONReader().JsonToValue("2.1e0", false, false));
106 ASSERT_TRUE(root.get());
107 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE));
108 double_val = 0.0;
109 ASSERT_TRUE(root->GetAsDouble(&double_val));
110 ASSERT_DOUBLE_EQ(2.1, double_val);
111
112 root.reset(JSONReader().JsonToValue("2.1e+0001", false, false));
113 ASSERT_TRUE(root.get());
114 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE));
115 double_val = 0.0;
116 ASSERT_TRUE(root->GetAsDouble(&double_val));
117 ASSERT_DOUBLE_EQ(21.0, double_val);
118
119 root.reset(JSONReader().JsonToValue("0.01", false, false));
120 ASSERT_TRUE(root.get());
121 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE));
122 double_val = 0.0;
123 ASSERT_TRUE(root->GetAsDouble(&double_val));
124 ASSERT_DOUBLE_EQ(0.01, double_val);
125
126 root.reset(JSONReader().JsonToValue("1.00", false, false));
127 ASSERT_TRUE(root.get());
128 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE));
129 double_val = 0.0;
130 ASSERT_TRUE(root->GetAsDouble(&double_val));
131 ASSERT_DOUBLE_EQ(1.0, double_val);
132
133 // Fractional parts must have a digit before and after the decimal point.
134 root.reset(JSONReader().JsonToValue("1.", false, false));
135 ASSERT_FALSE(root.get());
136 root.reset(JSONReader().JsonToValue(".1", false, false));
137 ASSERT_FALSE(root.get());
138 root.reset(JSONReader().JsonToValue("1.e10", false, false));
139 ASSERT_FALSE(root.get());
140
141 // Exponent must have a digit following the 'e'.
142 root.reset(JSONReader().JsonToValue("1e", false, false));
143 ASSERT_FALSE(root.get());
144 root.reset(JSONReader().JsonToValue("1E", false, false));
145 ASSERT_FALSE(root.get());
146 root.reset(JSONReader().JsonToValue("1e1.", false, false));
147 ASSERT_FALSE(root.get());
148 root.reset(JSONReader().JsonToValue("1e1.0", false, false));
149 ASSERT_FALSE(root.get());
150
151 // INF/-INF/NaN are not valid
152 root.reset(JSONReader().JsonToValue("1e1000", false, false));
153 ASSERT_FALSE(root.get());
154 root.reset(JSONReader().JsonToValue("-1e1000", false, false));
155 ASSERT_FALSE(root.get());
156 root.reset(JSONReader().JsonToValue("NaN", false, false));
157 ASSERT_FALSE(root.get());
158 root.reset(JSONReader().JsonToValue("nan", false, false));
159 ASSERT_FALSE(root.get());
160 root.reset(JSONReader().JsonToValue("inf", false, false));
161 ASSERT_FALSE(root.get());
162
163 // Invalid number formats
164 root.reset(JSONReader().JsonToValue("4.3.1", false, false));
165 ASSERT_FALSE(root.get());
166 root.reset(JSONReader().JsonToValue("4e3.1", false, false));
167 ASSERT_FALSE(root.get());
168
169 // Test string parser
170 root.reset(JSONReader().JsonToValue("\"hello world\"", false, false));
171 ASSERT_TRUE(root.get());
172 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
173 std::string str_val;
174 ASSERT_TRUE(root->GetAsString(&str_val));
175 ASSERT_EQ("hello world", str_val);
176
177 // Empty string
178 root.reset(JSONReader().JsonToValue("\"\"", false, false));
179 ASSERT_TRUE(root.get());
180 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
181 str_val.clear();
182 ASSERT_TRUE(root->GetAsString(&str_val));
183 ASSERT_EQ("", str_val);
184
185 // Test basic string escapes
186 root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"",
187 false, false));
188 ASSERT_TRUE(root.get());
189 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
190 str_val.clear();
191 ASSERT_TRUE(root->GetAsString(&str_val));
192 ASSERT_EQ(" \"\\/\b\f\n\r\t\v", str_val);
193
194 // Test hex and unicode escapes including the null character.
195 root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false,
196 false));
197 ASSERT_TRUE(root.get());
198 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
199 str_val.clear();
200 ASSERT_TRUE(root->GetAsString(&str_val));
201 ASSERT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val));
202
203 // Test invalid strings
204 root.reset(JSONReader().JsonToValue("\"no closing quote", false, false));
205 ASSERT_FALSE(root.get());
206 root.reset(JSONReader().JsonToValue("\"\\z invalid escape char\"", false,
207 false));
208 ASSERT_FALSE(root.get());
209 root.reset(JSONReader().JsonToValue("\"\\xAQ invalid hex code\"", false,
210 false));
211 ASSERT_FALSE(root.get());
212 root.reset(JSONReader().JsonToValue("not enough hex chars\\x1\"", false,
213 false));
214 ASSERT_FALSE(root.get());
215 root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"",
216 false, false));
217 ASSERT_FALSE(root.get());
218 root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"",
219 false, false));
220 ASSERT_FALSE(root.get());
221
222 // Basic array
223 root.reset(JSONReader::Read("[true, false, null]", false));
224 ASSERT_TRUE(root.get());
225 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
226 ListValue* list = static_cast<ListValue*>(root.get());
227 ASSERT_EQ(3U, list->GetSize());
228
229 // Test with trailing comma. Should be parsed the same as above.
230 scoped_ptr<Value> root2;
231 root2.reset(JSONReader::Read("[true, false, null, ]", true));
232 EXPECT_TRUE(root->Equals(root2.get()));
233
234 // Empty array
235 root.reset(JSONReader::Read("[]", false));
236 ASSERT_TRUE(root.get());
237 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
238 list = static_cast<ListValue*>(root.get());
239 ASSERT_EQ(0U, list->GetSize());
240
241 // Nested arrays
242 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]",
243 false));
244 ASSERT_TRUE(root.get());
245 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
246 list = static_cast<ListValue*>(root.get());
247 ASSERT_EQ(4U, list->GetSize());
248
249 // Lots of trailing commas.
250 root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]",
251 true));
252 EXPECT_TRUE(root->Equals(root2.get()));
253
254 // Invalid, missing close brace.
255 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null", false));
256 ASSERT_FALSE(root.get());
257
258 // Invalid, too many commas
259 root.reset(JSONReader::Read("[true,, null]", false));
260 ASSERT_FALSE(root.get());
261 root.reset(JSONReader::Read("[true,, null]", true));
262 ASSERT_FALSE(root.get());
263
264 // Invalid, no commas
265 root.reset(JSONReader::Read("[true null]", false));
266 ASSERT_FALSE(root.get());
267
268 // Invalid, trailing comma
269 root.reset(JSONReader::Read("[true,]", false));
270 ASSERT_FALSE(root.get());
271
272 // Valid if we set |allow_trailing_comma| to true.
273 root.reset(JSONReader::Read("[true,]", true));
274 ASSERT_TRUE(root.get());
275 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
276 list = static_cast<ListValue*>(root.get());
277 EXPECT_EQ(1U, list->GetSize());
278 Value* tmp_value = NULL;
279 ASSERT_TRUE(list->Get(0, &tmp_value));
280 EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN));
281 bool bool_value = false;
282 ASSERT_TRUE(tmp_value->GetAsBoolean(&bool_value));
283 EXPECT_TRUE(bool_value);
284
285 // Don't allow empty elements, even if |allow_trailing_comma| is
286 // true.
287 root.reset(JSONReader::Read("[,]", true));
288 EXPECT_FALSE(root.get());
289 root.reset(JSONReader::Read("[true,,]", true));
290 EXPECT_FALSE(root.get());
291 root.reset(JSONReader::Read("[,true,]", true));
292 EXPECT_FALSE(root.get());
293 root.reset(JSONReader::Read("[true,,false]", true));
294 EXPECT_FALSE(root.get());
295
296 // Test objects
297 root.reset(JSONReader::Read("{}", false));
298 ASSERT_TRUE(root.get());
299 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
300
301 root.reset(JSONReader::Read(
302 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false));
303 ASSERT_TRUE(root.get());
304 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
305 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get());
306 double_val = 0.0;
307 ASSERT_TRUE(dict_val->GetDouble("number", &double_val));
308 ASSERT_DOUBLE_EQ(9.87654321, double_val);
309 Value* null_val = NULL;
310 ASSERT_TRUE(dict_val->Get("null", &null_val));
311 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL));
312 str_val.clear();
313 ASSERT_TRUE(dict_val->GetString("S", &str_val));
314 ASSERT_EQ("str", str_val);
315
316 root2.reset(JSONReader::Read(
317 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true));
318 ASSERT_TRUE(root2.get());
319 EXPECT_TRUE(root->Equals(root2.get()));
320
321 // Test newline equivalence.
322 root2.reset(JSONReader::Read(
323 "{\n"
324 " \"number\":9.87654321,\n"
325 " \"null\":null,\n"
326 " \"\\x53\":\"str\",\n"
327 "}\n", true));
328 ASSERT_TRUE(root2.get());
329 EXPECT_TRUE(root->Equals(root2.get()));
330
331 root2.reset(JSONReader::Read(
332 "{\r\n"
333 " \"number\":9.87654321,\r\n"
334 " \"null\":null,\r\n"
335 " \"\\x53\":\"str\",\r\n"
336 "}\r\n", true));
337 ASSERT_TRUE(root2.get());
338 EXPECT_TRUE(root->Equals(root2.get()));
339
340 // Test nesting
341 root.reset(JSONReader::Read(
342 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false));
343 ASSERT_TRUE(root.get());
344 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
345 dict_val = static_cast<DictionaryValue*>(root.get());
346 DictionaryValue* inner_dict = NULL;
347 ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict));
348 ListValue* inner_array = NULL;
349 ASSERT_TRUE(inner_dict->GetList("array", &inner_array));
350 ASSERT_EQ(1U, inner_array->GetSize());
351 bool_value = true;
352 ASSERT_TRUE(dict_val->GetBoolean("false", &bool_value));
353 ASSERT_FALSE(bool_value);
354 inner_dict = NULL;
355 ASSERT_TRUE(dict_val->GetDictionary("d", &inner_dict));
356
357 root2.reset(JSONReader::Read(
358 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true));
359 EXPECT_TRUE(root->Equals(root2.get()));
360
361 // Test keys with periods
362 root.reset(JSONReader::Read(
363 "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", false));
364 ASSERT_TRUE(root.get());
365 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
366 dict_val = static_cast<DictionaryValue*>(root.get());
367 int integer_value = 0;
368 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
369 EXPECT_EQ(3, integer_value);
370 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value));
371 EXPECT_EQ(2, integer_value);
372 inner_dict = NULL;
373 ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion("d.e.f",
374 &inner_dict));
375 ASSERT_EQ(1U, inner_dict->size());
376 EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j",
377 &integer_value));
378 EXPECT_EQ(1, integer_value);
379
380 root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}", false));
381 ASSERT_TRUE(root.get());
382 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
383 dict_val = static_cast<DictionaryValue*>(root.get());
384 EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value));
385 EXPECT_EQ(2, integer_value);
386 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
387 EXPECT_EQ(1, integer_value);
388
389 // Invalid, no closing brace
390 root.reset(JSONReader::Read("{\"a\": true", false));
391 ASSERT_FALSE(root.get());
392
393 // Invalid, keys must be quoted
394 root.reset(JSONReader::Read("{foo:true}", false));
395 ASSERT_FALSE(root.get());
396
397 // Invalid, trailing comma
398 root.reset(JSONReader::Read("{\"a\":true,}", false));
399 ASSERT_FALSE(root.get());
400
401 // Invalid, too many commas
402 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", false));
403 ASSERT_FALSE(root.get());
404 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true));
405 ASSERT_FALSE(root.get());
406
407 // Invalid, no separator
408 root.reset(JSONReader::Read("{\"a\" \"b\"}", false));
409 ASSERT_FALSE(root.get());
410
411 // Invalid, lone comma.
412 root.reset(JSONReader::Read("{,}", false));
413 ASSERT_FALSE(root.get());
414 root.reset(JSONReader::Read("{,}", true));
415 ASSERT_FALSE(root.get());
416 root.reset(JSONReader::Read("{\"a\":true,,}", true));
417 ASSERT_FALSE(root.get());
418 root.reset(JSONReader::Read("{,\"a\":true}", true));
419 ASSERT_FALSE(root.get());
420 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true));
421 ASSERT_FALSE(root.get());
422
423 // Test stack overflow
424 std::string evil(1000000, '[');
425 evil.append(std::string(1000000, ']'));
426 root.reset(JSONReader::Read(evil, false));
427 ASSERT_FALSE(root.get());
428
429 // A few thousand adjacent lists is fine.
430 std::string not_evil("[");
431 not_evil.reserve(15010);
432 for (int i = 0; i < 5000; ++i) {
433 not_evil.append("[],");
434 }
435 not_evil.append("[]]");
436 root.reset(JSONReader::Read(not_evil, false));
437 ASSERT_TRUE(root.get());
438 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
439 list = static_cast<ListValue*>(root.get());
440 ASSERT_EQ(5001U, list->GetSize());
441
442 // Test utf8 encoded input
443 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"",
444 false, false));
445 ASSERT_TRUE(root.get());
446 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
447 str_val.clear();
448 ASSERT_TRUE(root->GetAsString(&str_val));
449 ASSERT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val));
450
451 // Test invalid utf8 encoded input
452 root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"",
453 false, false));
454 ASSERT_FALSE(root.get());
455 root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"",
456 false, false));
457 ASSERT_FALSE(root.get());
458
459 // Test invalid root objects.
460 root.reset(JSONReader::Read("null", false));
461 ASSERT_FALSE(root.get());
462 root.reset(JSONReader::Read("true", false));
463 ASSERT_FALSE(root.get());
464 root.reset(JSONReader::Read("10", false));
465 ASSERT_FALSE(root.get());
466 root.reset(JSONReader::Read("\"root\"", false));
467 ASSERT_FALSE(root.get());
468 }
469
TEST(JSONReaderTest,ErrorMessages)470 TEST(JSONReaderTest, ErrorMessages) {
471 // Error strings should not be modified in case of success.
472 std::string error_message;
473 int error_code = 0;
474 scoped_ptr<Value> root;
475 root.reset(JSONReader::ReadAndReturnError("[42]", false,
476 &error_code, &error_message));
477 EXPECT_TRUE(error_message.empty());
478 EXPECT_EQ(0, error_code);
479
480 // Test line and column counting
481 const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
482 // error here --------------------------------^
483 root.reset(JSONReader::ReadAndReturnError(big_json, false,
484 &error_code, &error_message));
485 EXPECT_FALSE(root.get());
486 EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError),
487 error_message);
488 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
489
490 // Test each of the error conditions
491 root.reset(JSONReader::ReadAndReturnError("{},{}", false,
492 &error_code, &error_message));
493 EXPECT_FALSE(root.get());
494 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3,
495 JSONReader::kUnexpectedDataAfterRoot), error_message);
496 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
497
498 std::string nested_json;
499 for (int i = 0; i < 101; ++i) {
500 nested_json.insert(nested_json.begin(), '[');
501 nested_json.append(1, ']');
502 }
503 root.reset(JSONReader::ReadAndReturnError(nested_json, false,
504 &error_code, &error_message));
505 EXPECT_FALSE(root.get());
506 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting),
507 error_message);
508 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
509
510 root.reset(JSONReader::ReadAndReturnError("42", false,
511 &error_code, &error_message));
512 EXPECT_FALSE(root.get());
513 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 1,
514 JSONReader::kBadRootElementType), error_message);
515 EXPECT_EQ(JSONReader::JSON_BAD_ROOT_ELEMENT_TYPE, error_code);
516
517 root.reset(JSONReader::ReadAndReturnError("[1,]", false,
518 &error_code, &error_message));
519 EXPECT_FALSE(root.get());
520 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
521 error_message);
522 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
523
524 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", false,
525 &error_code, &error_message));
526 EXPECT_FALSE(root.get());
527 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2,
528 JSONReader::kUnquotedDictionaryKey), error_message);
529 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
530
531 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", false,
532 &error_code, &error_message));
533 EXPECT_FALSE(root.get());
534 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
535 error_message);
536
537 root.reset(JSONReader::ReadAndReturnError("[nu]", false,
538 &error_code, &error_message));
539 EXPECT_FALSE(root.get());
540 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
541 error_message);
542 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
543
544 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", false,
545 &error_code, &error_message));
546 EXPECT_FALSE(root.get());
547 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
548 error_message);
549 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
550
551 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", false,
552 &error_code, &error_message));
553 EXPECT_FALSE(root.get());
554 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
555 error_message);
556 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
557
558 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false,
559 &error_code, &error_message));
560 EXPECT_FALSE(root.get());
561 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
562 error_message);
563 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
564 }
565
566 } // namespace base
567