OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "base/json/json_reader.h" | 6 #include "base/json/json_reader.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"", | 215 root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"", |
216 false, false)); | 216 false, false)); |
217 ASSERT_FALSE(root.get()); | 217 ASSERT_FALSE(root.get()); |
218 root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"", | 218 root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"", |
219 false, false)); | 219 false, false)); |
220 ASSERT_FALSE(root.get()); | 220 ASSERT_FALSE(root.get()); |
221 | 221 |
222 // Basic array | 222 // Basic array |
223 root.reset(JSONReader::Read("[true, false, null]", false)); | 223 root.reset(JSONReader::Read("[true, false, null]", false)); |
224 ASSERT_TRUE(root.get()); | 224 ASSERT_TRUE(root.get()); |
225 ListValue* list = root->AsList(); | 225 ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
226 ASSERT_TRUE(list); | 226 ListValue* list = static_cast<ListValue*>(root.get()); |
227 ASSERT_EQ(3U, list->GetSize()); | 227 ASSERT_EQ(3U, list->GetSize()); |
228 | 228 |
229 // Test with trailing comma. Should be parsed the same as above. | 229 // Test with trailing comma. Should be parsed the same as above. |
230 scoped_ptr<Value> root2; | 230 scoped_ptr<Value> root2; |
231 root2.reset(JSONReader::Read("[true, false, null, ]", true)); | 231 root2.reset(JSONReader::Read("[true, false, null, ]", true)); |
232 EXPECT_TRUE(root->Equals(root2.get())); | 232 EXPECT_TRUE(root->Equals(root2.get())); |
233 | 233 |
234 // Empty array | 234 // Empty array |
235 root.reset(JSONReader::Read("[]", false)); | 235 root.reset(JSONReader::Read("[]", false)); |
236 ASSERT_TRUE(root.get()); | 236 ASSERT_TRUE(root.get()); |
237 list = root->AsList(); | 237 ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
238 ASSERT_TRUE(list); | 238 list = static_cast<ListValue*>(root.get()); |
239 ASSERT_EQ(0U, list->GetSize()); | 239 ASSERT_EQ(0U, list->GetSize()); |
240 | 240 |
241 // Nested arrays | 241 // Nested arrays |
242 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]", | 242 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]", |
243 false)); | 243 false)); |
244 ASSERT_TRUE(root.get()); | 244 ASSERT_TRUE(root.get()); |
245 list = root->AsList(); | 245 ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
246 ASSERT_TRUE(list); | 246 list = static_cast<ListValue*>(root.get()); |
247 ASSERT_EQ(4U, list->GetSize()); | 247 ASSERT_EQ(4U, list->GetSize()); |
248 | 248 |
249 // Lots of trailing commas. | 249 // Lots of trailing commas. |
250 root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]", | 250 root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]", |
251 true)); | 251 true)); |
252 EXPECT_TRUE(root->Equals(root2.get())); | 252 EXPECT_TRUE(root->Equals(root2.get())); |
253 | 253 |
254 // Invalid, missing close brace. | 254 // Invalid, missing close brace. |
255 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null", false)); | 255 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null", false)); |
256 ASSERT_FALSE(root.get()); | 256 ASSERT_FALSE(root.get()); |
257 | 257 |
258 // Invalid, too many commas | 258 // Invalid, too many commas |
259 root.reset(JSONReader::Read("[true,, null]", false)); | 259 root.reset(JSONReader::Read("[true,, null]", false)); |
260 ASSERT_FALSE(root.get()); | 260 ASSERT_FALSE(root.get()); |
261 root.reset(JSONReader::Read("[true,, null]", true)); | 261 root.reset(JSONReader::Read("[true,, null]", true)); |
262 ASSERT_FALSE(root.get()); | 262 ASSERT_FALSE(root.get()); |
263 | 263 |
264 // Invalid, no commas | 264 // Invalid, no commas |
265 root.reset(JSONReader::Read("[true null]", false)); | 265 root.reset(JSONReader::Read("[true null]", false)); |
266 ASSERT_FALSE(root.get()); | 266 ASSERT_FALSE(root.get()); |
267 | 267 |
268 // Invalid, trailing comma | 268 // Invalid, trailing comma |
269 root.reset(JSONReader::Read("[true,]", false)); | 269 root.reset(JSONReader::Read("[true,]", false)); |
270 ASSERT_FALSE(root.get()); | 270 ASSERT_FALSE(root.get()); |
271 | 271 |
272 // Valid if we set |allow_trailing_comma| to true. | 272 // Valid if we set |allow_trailing_comma| to true. |
273 root.reset(JSONReader::Read("[true,]", true)); | 273 root.reset(JSONReader::Read("[true,]", true)); |
274 ASSERT_TRUE(root.get()); | 274 ASSERT_TRUE(root.get()); |
275 list = root->AsList(); | 275 ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
276 ASSERT_TRUE(list); | 276 list = static_cast<ListValue*>(root.get()); |
277 EXPECT_EQ(1U, list->GetSize()); | 277 EXPECT_EQ(1U, list->GetSize()); |
278 Value* tmp_value = NULL; | 278 Value* tmp_value = NULL; |
279 ASSERT_TRUE(list->Get(0, &tmp_value)); | 279 ASSERT_TRUE(list->Get(0, &tmp_value)); |
280 EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN)); | 280 EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN)); |
281 bool bool_value = false; | 281 bool bool_value = false; |
282 ASSERT_TRUE(tmp_value->GetAsBoolean(&bool_value)); | 282 ASSERT_TRUE(tmp_value->GetAsBoolean(&bool_value)); |
283 EXPECT_TRUE(bool_value); | 283 EXPECT_TRUE(bool_value); |
284 | 284 |
285 // Don't allow empty elements, even if |allow_trailing_comma| is | 285 // Don't allow empty elements, even if |allow_trailing_comma| is |
286 // true. | 286 // true. |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 | 428 |
429 // A few thousand adjacent lists is fine. | 429 // A few thousand adjacent lists is fine. |
430 std::string not_evil("["); | 430 std::string not_evil("["); |
431 not_evil.reserve(15010); | 431 not_evil.reserve(15010); |
432 for (int i = 0; i < 5000; ++i) { | 432 for (int i = 0; i < 5000; ++i) { |
433 not_evil.append("[],"); | 433 not_evil.append("[],"); |
434 } | 434 } |
435 not_evil.append("[]]"); | 435 not_evil.append("[]]"); |
436 root.reset(JSONReader::Read(not_evil, false)); | 436 root.reset(JSONReader::Read(not_evil, false)); |
437 ASSERT_TRUE(root.get()); | 437 ASSERT_TRUE(root.get()); |
438 list = root->AsList(); | 438 ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
439 ASSERT_TRUE(list); | 439 list = static_cast<ListValue*>(root.get()); |
440 ASSERT_EQ(5001U, list->GetSize()); | 440 ASSERT_EQ(5001U, list->GetSize()); |
441 | 441 |
442 // Test utf8 encoded input | 442 // Test utf8 encoded input |
443 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", | 443 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", |
444 false, false)); | 444 false, false)); |
445 ASSERT_TRUE(root.get()); | 445 ASSERT_TRUE(root.get()); |
446 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); | 446 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
447 str_val.clear(); | 447 str_val.clear(); |
448 ASSERT_TRUE(root->GetAsString(&str_val)); | 448 ASSERT_TRUE(root->GetAsString(&str_val)); |
449 ASSERT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); | 449 ASSERT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 | 557 |
558 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, | 558 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, |
559 &error_code, &error_message)); | 559 &error_code, &error_message)); |
560 EXPECT_FALSE(root.get()); | 560 EXPECT_FALSE(root.get()); |
561 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), | 561 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
562 error_message); | 562 error_message); |
563 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); | 563 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
564 } | 564 } |
565 | 565 |
566 } // namespace base | 566 } // namespace base |
OLD | NEW |