blob: c05fcdadebf37b1ca0c574378deb41f9727a1a1f [file] [log] [blame]
[email protected]4dad9ad82009-11-25 20:47:521// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include "testing/gtest/include/gtest/gtest.h"
[email protected]93d49d72009-10-23 20:00:206#include "base/json/json_reader.h"
[email protected]b4cebf82008-12-29 19:59:087#include "base/scoped_ptr.h"
initial.commitd7cae122008-07-26 21:49:388#include "base/values.h"
[email protected]b509f7a2008-08-08 17:26:429#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3810
[email protected]93d49d72009-10-23 20:00:2011namespace base {
12
initial.commitd7cae122008-07-26 21:49:3813TEST(JSONReaderTest, Reading) {
14 // some whitespace checking
[email protected]b4cebf82008-12-29 19:59:0815 scoped_ptr<Value> root;
16 root.reset(JSONReader().JsonToValue(" null ", false, false));
17 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:3818 ASSERT_TRUE(root->IsType(Value::TYPE_NULL));
initial.commitd7cae122008-07-26 21:49:3819
20 // Invalid JSON string
[email protected]b4cebf82008-12-29 19:59:0821 root.reset(JSONReader().JsonToValue("nu", false, false));
22 ASSERT_FALSE(root.get());
initial.commitd7cae122008-07-26 21:49:3823
24 // Simple bool
[email protected]b4cebf82008-12-29 19:59:0825 root.reset(JSONReader().JsonToValue("true ", false, false));
26 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:3827 ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
initial.commitd7cae122008-07-26 21:49:3828
[email protected]5b0bc252009-07-23 21:34:4029 // 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
initial.commitd7cae122008-07-26 21:49:3847 // Test number formats
[email protected]b4cebf82008-12-29 19:59:0848 root.reset(JSONReader().JsonToValue("43", false, false));
49 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:3850 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);
initial.commitd7cae122008-07-26 21:49:3854
55 // According to RFC4627, oct, hex, and leading zeros are invalid JSON.
[email protected]b4cebf82008-12-29 19:59:0856 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());
initial.commitd7cae122008-07-26 21:49:3862
63 // Test 0 (which needs to be special cased because of the leading zero
64 // clause).
[email protected]b4cebf82008-12-29 19:59:0865 root.reset(JSONReader().JsonToValue("0", false, false));
66 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:3867 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER));
68 int_val = 1;
69 ASSERT_TRUE(root->GetAsInteger(&int_val));
70 ASSERT_EQ(0, int_val);
initial.commitd7cae122008-07-26 21:49:3871
[email protected]d9023ab2008-08-07 17:15:4172 // Numbers that overflow ints should succeed, being internally promoted to
73 // storage as doubles
[email protected]b4cebf82008-12-29 19:59:0874 root.reset(JSONReader().JsonToValue("2147483648", false, false));
75 ASSERT_TRUE(root.get());
[email protected]b509f7a2008-08-08 17:26:4276 double real_val;
[email protected]b509f7a2008-08-08 17:26:4277 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);
[email protected]b4cebf82008-12-29 19:59:0881 root.reset(JSONReader().JsonToValue("-2147483649", false, false));
82 ASSERT_TRUE(root.get());
[email protected]b509f7a2008-08-08 17:26:4283 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);
initial.commitd7cae122008-07-26 21:49:3887
88 // Parse a double
[email protected]b4cebf82008-12-29 19:59:0889 root.reset(JSONReader().JsonToValue("43.1", false, false));
90 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:3891 ASSERT_TRUE(root->IsType(Value::TYPE_REAL));
[email protected]b509f7a2008-08-08 17:26:4292 real_val = 0.0;
initial.commitd7cae122008-07-26 21:49:3893 ASSERT_TRUE(root->GetAsReal(&real_val));
94 ASSERT_DOUBLE_EQ(43.1, real_val);
initial.commitd7cae122008-07-26 21:49:3895
[email protected]b4cebf82008-12-29 19:59:0896 root.reset(JSONReader().JsonToValue("4.3e-1", false, false));
97 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:3898 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);
initial.commitd7cae122008-07-26 21:49:38102
[email protected]b4cebf82008-12-29 19:59:08103 root.reset(JSONReader().JsonToValue("2.1e0", false, false));
104 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38105 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);
initial.commitd7cae122008-07-26 21:49:38109
[email protected]b4cebf82008-12-29 19:59:08110 root.reset(JSONReader().JsonToValue("2.1e+0001", false, false));
111 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38112 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);
initial.commitd7cae122008-07-26 21:49:38116
[email protected]b4cebf82008-12-29 19:59:08117 root.reset(JSONReader().JsonToValue("0.01", false, false));
118 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38119 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);
initial.commitd7cae122008-07-26 21:49:38123
[email protected]b4cebf82008-12-29 19:59:08124 root.reset(JSONReader().JsonToValue("1.00", false, false));
125 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38126 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);
initial.commitd7cae122008-07-26 21:49:38130
131 // Fractional parts must have a digit before and after the decimal point.
[email protected]b4cebf82008-12-29 19:59:08132 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());
initial.commitd7cae122008-07-26 21:49:38138
139 // Exponent must have a digit following the 'e'.
[email protected]b4cebf82008-12-29 19:59:08140 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());
initial.commitd7cae122008-07-26 21:49:38148
149 // INF/-INF/NaN are not valid
[email protected]b4cebf82008-12-29 19:59:08150 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());
initial.commitd7cae122008-07-26 21:49:38160
161 // Invalid number formats
[email protected]b4cebf82008-12-29 19:59:08162 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());
initial.commitd7cae122008-07-26 21:49:38166
167 // Test string parser
[email protected]b4cebf82008-12-29 19:59:08168 root.reset(JSONReader().JsonToValue("\"hello world\"", false, false));
169 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38170 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);
initial.commitd7cae122008-07-26 21:49:38174
175 // Empty string
[email protected]b4cebf82008-12-29 19:59:08176 root.reset(JSONReader().JsonToValue("\"\"", false, false));
177 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38178 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
179 str_val.clear();
180 ASSERT_TRUE(root->GetAsString(&str_val));
181 ASSERT_EQ(L"", str_val);
initial.commitd7cae122008-07-26 21:49:38182
183 // Test basic string escapes
[email protected]b4cebf82008-12-29 19:59:08184 root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"",
185 false, false));
186 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38187 ASSERT_TRUE(root->IsType(Value::TYPE_STRING));
188 str_val.clear();
189 ASSERT_TRUE(root->GetAsString(&str_val));
[email protected]d46d6f3b2008-10-01 17:26:06190 ASSERT_EQ(L" \"\\/\b\f\n\r\t\v", str_val);
initial.commitd7cae122008-07-26 21:49:38191
192 // Test hex and unicode escapes including the null character.
[email protected]b4cebf82008-12-29 19:59:08193 root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false,
[email protected]e7245992008-07-29 00:01:31194 false));
[email protected]b4cebf82008-12-29 19:59:08195 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38196 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);
initial.commitd7cae122008-07-26 21:49:38200
201 // Test invalid strings
[email protected]b4cebf82008-12-29 19:59:08202 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());
initial.commitd7cae122008-07-26 21:49:38219
220 // Basic array
[email protected]b4cebf82008-12-29 19:59:08221 root.reset(JSONReader::Read("[true, false, null]", false));
222 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38223 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
[email protected]b4cebf82008-12-29 19:59:08224 ListValue* list = static_cast<ListValue*>(root.get());
[email protected]cb2f3632008-08-14 20:27:29225 ASSERT_EQ(3U, list->GetSize());
[email protected]e7245992008-07-29 00:01:31226
227 // Test with trailing comma. Should be parsed the same as above.
[email protected]b4cebf82008-12-29 19:59:08228 scoped_ptr<Value> root2;
229 root2.reset(JSONReader::Read("[true, false, null, ]", true));
230 EXPECT_TRUE(root->Equals(root2.get()));
initial.commitd7cae122008-07-26 21:49:38231
232 // Empty array
[email protected]b4cebf82008-12-29 19:59:08233 root.reset(JSONReader::Read("[]", false));
234 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38235 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
[email protected]b4cebf82008-12-29 19:59:08236 list = static_cast<ListValue*>(root.get());
[email protected]cb2f3632008-08-14 20:27:29237 ASSERT_EQ(0U, list->GetSize());
initial.commitd7cae122008-07-26 21:49:38238
239 // Nested arrays
[email protected]b4cebf82008-12-29 19:59:08240 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]",
241 false));
242 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38243 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
[email protected]b4cebf82008-12-29 19:59:08244 list = static_cast<ListValue*>(root.get());
[email protected]cb2f3632008-08-14 20:27:29245 ASSERT_EQ(4U, list->GetSize());
[email protected]e7245992008-07-29 00:01:31246
247 // Lots of trailing commas.
[email protected]b4cebf82008-12-29 19:59:08248 root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]",
249 true));
250 EXPECT_TRUE(root->Equals(root2.get()));
initial.commitd7cae122008-07-26 21:49:38251
252 // Invalid, missing close brace.
[email protected]b4cebf82008-12-29 19:59:08253 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null", false));
254 ASSERT_FALSE(root.get());
initial.commitd7cae122008-07-26 21:49:38255
256 // Invalid, too many commas
[email protected]b4cebf82008-12-29 19:59:08257 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());
initial.commitd7cae122008-07-26 21:49:38261
262 // Invalid, no commas
[email protected]b4cebf82008-12-29 19:59:08263 root.reset(JSONReader::Read("[true null]", false));
264 ASSERT_FALSE(root.get());
initial.commitd7cae122008-07-26 21:49:38265
266 // Invalid, trailing comma
[email protected]b4cebf82008-12-29 19:59:08267 root.reset(JSONReader::Read("[true,]", false));
268 ASSERT_FALSE(root.get());
initial.commitd7cae122008-07-26 21:49:38269
[email protected]e7245992008-07-29 00:01:31270 // Valid if we set |allow_trailing_comma| to true.
[email protected]b4cebf82008-12-29 19:59:08271 root.reset(JSONReader::Read("[true,]", true));
272 ASSERT_TRUE(root.get());
[email protected]e7245992008-07-29 00:01:31273 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
[email protected]b4cebf82008-12-29 19:59:08274 list = static_cast<ListValue*>(root.get());
[email protected]cb2f3632008-08-14 20:27:29275 EXPECT_EQ(1U, list->GetSize());
[email protected]e7245992008-07-29 00:01:31276 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);
[email protected]e7245992008-07-29 00:01:31282
283 // Don't allow empty elements, even if |allow_trailing_comma| is
284 // true.
[email protected]b4cebf82008-12-29 19:59:08285 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());
[email protected]e7245992008-07-29 00:01:31293
initial.commitd7cae122008-07-26 21:49:38294 // Test objects
[email protected]b4cebf82008-12-29 19:59:08295 root.reset(JSONReader::Read("{}", false));
296 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38297 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
initial.commitd7cae122008-07-26 21:49:38298
[email protected]b4cebf82008-12-29 19:59:08299 root.reset(JSONReader::Read(
[email protected]4dad9ad82009-11-25 20:47:52300 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false));
[email protected]b4cebf82008-12-29 19:59:08301 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38302 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
[email protected]b4cebf82008-12-29 19:59:08303 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get());
initial.commitd7cae122008-07-26 21:49:38304 real_val = 0.0;
[email protected]90018542010-08-14 01:49:14305 ASSERT_TRUE(dict_val->GetReal("number", &real_val));
initial.commitd7cae122008-07-26 21:49:38306 ASSERT_DOUBLE_EQ(9.87654321, real_val);
307 Value* null_val = NULL;
[email protected]90018542010-08-14 01:49:14308 ASSERT_TRUE(dict_val->Get("null", &null_val));
initial.commitd7cae122008-07-26 21:49:38309 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL));
[email protected]8e50b602009-03-03 22:59:43310 str_val.clear();
311 ASSERT_TRUE(dict_val->GetString(L"S", &str_val));
312 ASSERT_EQ(L"str", str_val);
[email protected]e7245992008-07-29 00:01:31313
[email protected]b4cebf82008-12-29 19:59:08314 root2.reset(JSONReader::Read(
[email protected]4dad9ad82009-11-25 20:47:52315 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true));
[email protected]b81637c32009-06-26 21:17:24316 ASSERT_TRUE(root2.get());
317 EXPECT_TRUE(root->Equals(root2.get()));
318
319 // Test newline equivalence.
320 root2.reset(JSONReader::Read(
[email protected]4dad9ad82009-11-25 20:47:52321 "{\n"
322 " \"number\":9.87654321,\n"
323 " \"null\":null,\n"
324 " \"\\x53\":\"str\",\n"
325 "}\n", true));
[email protected]b81637c32009-06-26 21:17:24326 ASSERT_TRUE(root2.get());
327 EXPECT_TRUE(root->Equals(root2.get()));
328
329 root2.reset(JSONReader::Read(
[email protected]4dad9ad82009-11-25 20:47:52330 "{\r\n"
331 " \"number\":9.87654321,\r\n"
332 " \"null\":null,\r\n"
333 " \"\\x53\":\"str\",\r\n"
334 "}\r\n", true));
[email protected]b81637c32009-06-26 21:17:24335 ASSERT_TRUE(root2.get());
[email protected]b4cebf82008-12-29 19:59:08336 EXPECT_TRUE(root->Equals(root2.get()));
initial.commitd7cae122008-07-26 21:49:38337
338 // Test nesting
[email protected]b4cebf82008-12-29 19:59:08339 root.reset(JSONReader::Read(
[email protected]4dad9ad82009-11-25 20:47:52340 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false));
[email protected]b4cebf82008-12-29 19:59:08341 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38342 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
[email protected]b4cebf82008-12-29 19:59:08343 dict_val = static_cast<DictionaryValue*>(root.get());
initial.commitd7cae122008-07-26 21:49:38344 DictionaryValue* inner_dict = NULL;
[email protected]90018542010-08-14 01:49:14345 ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict));
initial.commitd7cae122008-07-26 21:49:38346 ListValue* inner_array = NULL;
[email protected]90018542010-08-14 01:49:14347 ASSERT_TRUE(inner_dict->GetList("array", &inner_array));
[email protected]cb2f3632008-08-14 20:27:29348 ASSERT_EQ(1U, inner_array->GetSize());
[email protected]e7245992008-07-29 00:01:31349 bool_value = true;
[email protected]90018542010-08-14 01:49:14350 ASSERT_TRUE(dict_val->GetBoolean("false", &bool_value));
initial.commitd7cae122008-07-26 21:49:38351 ASSERT_FALSE(bool_value);
352 inner_dict = NULL;
[email protected]90018542010-08-14 01:49:14353 ASSERT_TRUE(dict_val->GetDictionary("d", &inner_dict));
[email protected]e7245992008-07-29 00:01:31354
[email protected]b4cebf82008-12-29 19:59:08355 root2.reset(JSONReader::Read(
[email protected]4dad9ad82009-11-25 20:47:52356 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true));
[email protected]b4cebf82008-12-29 19:59:08357 EXPECT_TRUE(root->Equals(root2.get()));
initial.commitd7cae122008-07-26 21:49:38358
[email protected]4dad9ad82009-11-25 20:47:52359 // 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;
[email protected]90018542010-08-14 01:49:14366 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
[email protected]4dad9ad82009-11-25 20:47:52367 EXPECT_EQ(3, integer_value);
[email protected]90018542010-08-14 01:49:14368 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value));
[email protected]4dad9ad82009-11-25 20:47:52369 EXPECT_EQ(2, integer_value);
370 inner_dict = NULL;
[email protected]90018542010-08-14 01:49:14371 ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion("d.e.f",
[email protected]4dad9ad82009-11-25 20:47:52372 &inner_dict));
373 ASSERT_EQ(1U, inner_dict->size());
[email protected]90018542010-08-14 01:49:14374 EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j",
[email protected]4dad9ad82009-11-25 20:47:52375 &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());
[email protected]90018542010-08-14 01:49:14382 EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value));
[email protected]4dad9ad82009-11-25 20:47:52383 EXPECT_EQ(2, integer_value);
[email protected]90018542010-08-14 01:49:14384 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
[email protected]4dad9ad82009-11-25 20:47:52385 EXPECT_EQ(1, integer_value);
386
initial.commitd7cae122008-07-26 21:49:38387 // Invalid, no closing brace
[email protected]b4cebf82008-12-29 19:59:08388 root.reset(JSONReader::Read("{\"a\": true", false));
389 ASSERT_FALSE(root.get());
initial.commitd7cae122008-07-26 21:49:38390
391 // Invalid, keys must be quoted
[email protected]b4cebf82008-12-29 19:59:08392 root.reset(JSONReader::Read("{foo:true}", false));
393 ASSERT_FALSE(root.get());
initial.commitd7cae122008-07-26 21:49:38394
395 // Invalid, trailing comma
[email protected]b4cebf82008-12-29 19:59:08396 root.reset(JSONReader::Read("{\"a\":true,}", false));
397 ASSERT_FALSE(root.get());
initial.commitd7cae122008-07-26 21:49:38398
399 // Invalid, too many commas
[email protected]b4cebf82008-12-29 19:59:08400 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());
initial.commitd7cae122008-07-26 21:49:38404
405 // Invalid, no separator
[email protected]b4cebf82008-12-29 19:59:08406 root.reset(JSONReader::Read("{\"a\" \"b\"}", false));
407 ASSERT_FALSE(root.get());
[email protected]e7245992008-07-29 00:01:31408
409 // Invalid, lone comma.
[email protected]b4cebf82008-12-29 19:59:08410 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());
initial.commitd7cae122008-07-26 21:49:38420
421 // Test stack overflow
initial.commitd7cae122008-07-26 21:49:38422 std::string evil(1000000, '[');
423 evil.append(std::string(1000000, ']'));
[email protected]b4cebf82008-12-29 19:59:08424 root.reset(JSONReader::Read(evil, false));
425 ASSERT_FALSE(root.get());
initial.commitd7cae122008-07-26 21:49:38426
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("[]]");
[email protected]b4cebf82008-12-29 19:59:08434 root.reset(JSONReader::Read(not_evil, false));
435 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38436 ASSERT_TRUE(root->IsType(Value::TYPE_LIST));
[email protected]b4cebf82008-12-29 19:59:08437 list = static_cast<ListValue*>(root.get());
[email protected]cb2f3632008-08-14 20:27:29438 ASSERT_EQ(5001U, list->GetSize());
initial.commitd7cae122008-07-26 21:49:38439
440 // Test utf8 encoded input
[email protected]b4cebf82008-12-29 19:59:08441 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"",
[email protected]e7245992008-07-29 00:01:31442 false, false));
[email protected]b4cebf82008-12-29 19:59:08443 ASSERT_TRUE(root.get());
initial.commitd7cae122008-07-26 21:49:38444 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);
initial.commitd7cae122008-07-26 21:49:38448
[email protected]c9ec4542008-09-25 21:42:00449 // Test invalid utf8 encoded input
[email protected]b4cebf82008-12-29 19:59:08450 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());
[email protected]c9ec4542008-09-25 21:42:00456
initial.commitd7cae122008-07-26 21:49:38457 // Test invalid root objects.
[email protected]b4cebf82008-12-29 19:59:08458 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());
initial.commitd7cae122008-07-26 21:49:38466}
[email protected]88e728452008-12-05 22:14:46467
468TEST(JSONReaderTest, ErrorMessages) {
469 // Error strings should not be modified in case of success.
470 std::string error_message;
[email protected]ba399672010-04-06 15:42:39471 int error_code = 0;
[email protected]b4cebf82008-12-29 19:59:08472 scoped_ptr<Value> root;
[email protected]ba399672010-04-06 15:42:39473 root.reset(JSONReader::ReadAndReturnError("[42]", false,
474 &error_code, &error_message));
[email protected]6160649ec2008-12-24 02:07:48475 EXPECT_TRUE(error_message.empty());
[email protected]ba399672010-04-06 15:42:39476 EXPECT_EQ(0, error_code);
[email protected]88e728452008-12-05 22:14:46477
478 // Test line and column counting
479 const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
480 // error here --------------------------------^
[email protected]ba399672010-04-06 15:42:39481 root.reset(JSONReader::ReadAndReturnError(big_json, false,
482 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08483 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48484 EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError),
[email protected]88e728452008-12-05 22:14:46485 error_message);
[email protected]ba399672010-04-06 15:42:39486 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
[email protected]88e728452008-12-05 22:14:46487
488 // Test each of the error conditions
[email protected]ba399672010-04-06 15:42:39489 root.reset(JSONReader::ReadAndReturnError("{},{}", false,
490 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08491 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48492 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3,
[email protected]88e728452008-12-05 22:14:46493 JSONReader::kUnexpectedDataAfterRoot), error_message);
[email protected]ba399672010-04-06 15:42:39494 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
[email protected]88e728452008-12-05 22:14:46495
496 std::string nested_json;
497 for (int i = 0; i < 101; ++i) {
498 nested_json.insert(nested_json.begin(), '[');
499 nested_json.append(1, ']');
500 }
[email protected]b4cebf82008-12-29 19:59:08501 root.reset(JSONReader::ReadAndReturnError(nested_json, false,
[email protected]ba399672010-04-06 15:42:39502 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08503 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48504 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting),
[email protected]88e728452008-12-05 22:14:46505 error_message);
[email protected]ba399672010-04-06 15:42:39506 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
[email protected]88e728452008-12-05 22:14:46507
[email protected]ba399672010-04-06 15:42:39508 root.reset(JSONReader::ReadAndReturnError("42", false,
509 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08510 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48511 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 1,
[email protected]88e728452008-12-05 22:14:46512 JSONReader::kBadRootElementType), error_message);
[email protected]ba399672010-04-06 15:42:39513 EXPECT_EQ(JSONReader::JSON_BAD_ROOT_ELEMENT_TYPE, error_code);
[email protected]88e728452008-12-05 22:14:46514
[email protected]ba399672010-04-06 15:42:39515 root.reset(JSONReader::ReadAndReturnError("[1,]", false,
516 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08517 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48518 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
[email protected]88e728452008-12-05 22:14:46519 error_message);
[email protected]ba399672010-04-06 15:42:39520 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
[email protected]88e728452008-12-05 22:14:46521
[email protected]b4cebf82008-12-29 19:59:08522 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", false,
[email protected]ba399672010-04-06 15:42:39523 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08524 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48525 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2,
[email protected]88e728452008-12-05 22:14:46526 JSONReader::kUnquotedDictionaryKey), error_message);
[email protected]ba399672010-04-06 15:42:39527 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
[email protected]88e728452008-12-05 22:14:46528
[email protected]b4cebf82008-12-29 19:59:08529 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", false,
[email protected]ba399672010-04-06 15:42:39530 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08531 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48532 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
[email protected]88e728452008-12-05 22:14:46533 error_message);
534
[email protected]ba399672010-04-06 15:42:39535 root.reset(JSONReader::ReadAndReturnError("[nu]", false,
536 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08537 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48538 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
[email protected]88e728452008-12-05 22:14:46539 error_message);
[email protected]ba399672010-04-06 15:42:39540 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
[email protected]88e728452008-12-05 22:14:46541
[email protected]b4cebf82008-12-29 19:59:08542 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", false,
[email protected]ba399672010-04-06 15:42:39543 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08544 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48545 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
[email protected]88e728452008-12-05 22:14:46546 error_message);
[email protected]ba399672010-04-06 15:42:39547 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
[email protected]88e728452008-12-05 22:14:46548
[email protected]b4cebf82008-12-29 19:59:08549 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", false,
[email protected]ba399672010-04-06 15:42:39550 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08551 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48552 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
[email protected]88e728452008-12-05 22:14:46553 error_message);
[email protected]ba399672010-04-06 15:42:39554 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
[email protected]88e728452008-12-05 22:14:46555
[email protected]b4cebf82008-12-29 19:59:08556 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false,
[email protected]ba399672010-04-06 15:42:39557 &error_code, &error_message));
[email protected]b4cebf82008-12-29 19:59:08558 EXPECT_FALSE(root.get());
[email protected]6160649ec2008-12-24 02:07:48559 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
[email protected]88e728452008-12-05 22:14:46560 error_message);
[email protected]ba399672010-04-06 15:42:39561 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
[email protected]88e728452008-12-05 22:14:46562}
[email protected]93d49d72009-10-23 20:00:20563
564} // namespace base