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