1 // Copyright (c) 2012 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 <string> 6 7 #include "base/file_util.h" 8 #include "base/files/scoped_temp_dir.h" 9 #include "base/json/json_file_value_serializer.h" 10 #include "base/json/json_reader.h" 11 #include "base/json/json_string_value_serializer.h" 12 #include "base/json/json_writer.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/path_service.h" 15 #include "base/strings/string_util.h" 16 #include "base/strings/utf_string_conversions.h" 17 #include "base/values.h" 18 #include "testing/gtest/include/gtest/gtest.h" 19 20 namespace base { 21 22 namespace { 23 24 // Some proper JSON to test with: 25 const char kProperJSON[] = 26 "{\n" 27 " \"compound\": {\n" 28 " \"a\": 1,\n" 29 " \"b\": 2\n" 30 " },\n" 31 " \"some_String\": \"1337\",\n" 32 " \"some_int\": 42,\n" 33 " \"the_list\": [ \"val1\", \"val2\" ]\n" 34 "}\n"; 35 36 // Some proper JSON with trailing commas: 37 const char kProperJSONWithCommas[] = 38 "{\n" 39 "\t\"some_int\": 42,\n" 40 "\t\"some_String\": \"1337\",\n" 41 "\t\"the_list\": [\"val1\", \"val2\", ],\n" 42 "\t\"compound\": { \"a\": 1, \"b\": 2, },\n" 43 "}\n"; 44 45 const char kWinLineEnds[] = "\r\n"; 46 const char kLinuxLineEnds[] = "\n"; 47 48 // Verifies the generated JSON against the expected output. 49 void CheckJSONIsStillTheSame(Value& value) { 50 // Serialize back the output. 51 std::string serialized_json; 52 JSONStringValueSerializer str_serializer(&serialized_json); 53 str_serializer.set_pretty_print(true); 54 ASSERT_TRUE(str_serializer.Serialize(value)); 55 // Unify line endings between platforms. 56 ReplaceSubstringsAfterOffset(&serialized_json, 0, 57 kWinLineEnds, kLinuxLineEnds); 58 // Now compare the input with the output. 59 ASSERT_EQ(kProperJSON, serialized_json); 60 } 61 62 void ValidateJsonList(const std::string& json) { 63 scoped_ptr<Value> root(JSONReader::Read(json)); 64 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 65 ListValue* list = static_cast<ListValue*>(root.get()); 66 ASSERT_EQ(1U, list->GetSize()); 67 Value* elt = NULL; 68 ASSERT_TRUE(list->Get(0, &elt)); 69 int value = 0; 70 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); 71 ASSERT_EQ(1, value); 72 } 73 74 // Test proper JSON [de]serialization from string is working. 75 TEST(JSONValueSerializerTest, ReadProperJSONFromString) { 76 // Try to deserialize it through the serializer. 77 std::string proper_json(kProperJSON); 78 JSONStringValueSerializer str_deserializer(proper_json); 79 80 int error_code = 0; 81 std::string error_message; 82 scoped_ptr<Value> value( 83 str_deserializer.Deserialize(&error_code, &error_message)); 84 ASSERT_TRUE(value.get()); 85 ASSERT_EQ(0, error_code); 86 ASSERT_TRUE(error_message.empty()); 87 // Verify if the same JSON is still there. 88 CheckJSONIsStillTheSame(*value); 89 } 90 91 // Test that trialing commas are only properly deserialized from string when 92 // the proper flag for that is set. 93 TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) { 94 // Try to deserialize it through the serializer. 95 std::string proper_json(kProperJSONWithCommas); 96 JSONStringValueSerializer str_deserializer(proper_json); 97 98 int error_code = 0; 99 std::string error_message; 100 scoped_ptr<Value> value( 101 str_deserializer.Deserialize(&error_code, &error_message)); 102 ASSERT_FALSE(value.get()); 103 ASSERT_NE(0, error_code); 104 ASSERT_FALSE(error_message.empty()); 105 // Now the flag is set and it must pass. 106 str_deserializer.set_allow_trailing_comma(true); 107 value.reset(str_deserializer.Deserialize(&error_code, &error_message)); 108 ASSERT_TRUE(value.get()); 109 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 110 // Verify if the same JSON is still there. 111 CheckJSONIsStillTheSame(*value); 112 } 113 114 // Test proper JSON [de]serialization from file is working. 115 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { 116 ScopedTempDir tempdir; 117 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 118 // Write it down in the file. 119 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 120 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), 121 WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); 122 123 // Try to deserialize it through the serializer. 124 JSONFileValueSerializer file_deserializer(temp_file); 125 126 int error_code = 0; 127 std::string error_message; 128 scoped_ptr<Value> value( 129 file_deserializer.Deserialize(&error_code, &error_message)); 130 ASSERT_TRUE(value.get()); 131 ASSERT_EQ(0, error_code); 132 ASSERT_TRUE(error_message.empty()); 133 // Verify if the same JSON is still there. 134 CheckJSONIsStillTheSame(*value); 135 } 136 137 // Test that trialing commas are only properly deserialized from file when 138 // the proper flag for that is set. 139 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { 140 ScopedTempDir tempdir; 141 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 142 // Write it down in the file. 143 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 144 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), 145 WriteFile(temp_file, kProperJSONWithCommas, 146 strlen(kProperJSONWithCommas))); 147 148 // Try to deserialize it through the serializer. 149 JSONFileValueSerializer file_deserializer(temp_file); 150 // This must fail without the proper flag. 151 int error_code = 0; 152 std::string error_message; 153 scoped_ptr<Value> value( 154 file_deserializer.Deserialize(&error_code, &error_message)); 155 ASSERT_FALSE(value.get()); 156 ASSERT_NE(0, error_code); 157 ASSERT_FALSE(error_message.empty()); 158 // Now the flag is set and it must pass. 159 file_deserializer.set_allow_trailing_comma(true); 160 value.reset(file_deserializer.Deserialize(&error_code, &error_message)); 161 ASSERT_TRUE(value.get()); 162 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 163 // Verify if the same JSON is still there. 164 CheckJSONIsStillTheSame(*value); 165 } 166 167 TEST(JSONValueSerializerTest, Roundtrip) { 168 const std::string original_serialization = 169 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; 170 JSONStringValueSerializer serializer(original_serialization); 171 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); 172 ASSERT_TRUE(root.get()); 173 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 174 175 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 176 177 Value* null_value = NULL; 178 ASSERT_TRUE(root_dict->Get("null", &null_value)); 179 ASSERT_TRUE(null_value); 180 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 181 182 bool bool_value = false; 183 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); 184 ASSERT_TRUE(bool_value); 185 186 int int_value = 0; 187 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 188 ASSERT_EQ(42, int_value); 189 190 double double_value = 0.0; 191 ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); 192 ASSERT_DOUBLE_EQ(3.14, double_value); 193 194 // We shouldn't be able to write using this serializer, since it was 195 // initialized with a const string. 196 ASSERT_FALSE(serializer.Serialize(*root_dict)); 197 198 std::string test_serialization; 199 JSONStringValueSerializer mutable_serializer(&test_serialization); 200 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 201 ASSERT_EQ(original_serialization, test_serialization); 202 203 mutable_serializer.set_pretty_print(true); 204 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 205 // JSON output uses a different newline style on Windows than on other 206 // platforms. 207 #if defined(OS_WIN) 208 #define JSON_NEWLINE "\r\n" 209 #else 210 #define JSON_NEWLINE "\n" 211 #endif 212 const std::string pretty_serialization = 213 "{" JSON_NEWLINE 214 " \"bool\": true," JSON_NEWLINE 215 " \"double\": 3.14," JSON_NEWLINE 216 " \"int\": 42," JSON_NEWLINE 217 " \"list\": [ 1, 2 ]," JSON_NEWLINE 218 " \"null\": null" JSON_NEWLINE 219 "}" JSON_NEWLINE; 220 #undef JSON_NEWLINE 221 ASSERT_EQ(pretty_serialization, test_serialization); 222 } 223 224 TEST(JSONValueSerializerTest, StringEscape) { 225 string16 all_chars; 226 for (int i = 1; i < 256; ++i) { 227 all_chars += static_cast<char16>(i); 228 } 229 // Generated in in Firefox using the following js (with an extra backslash for 230 // double quote): 231 // var s = ''; 232 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); } 233 // uneval(s).replace(/\\/g, "\\\\"); 234 std::string all_chars_expected = 235 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r" 236 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" 237 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"#$%&'()*+," 238 "-./0123456789:;\\u003C=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcde" 239 "fghijklmnopqrstuvwxyz{|}~\x7F\xC2\x80\xC2\x81\xC2\x82\xC2\x83\xC2\x84" 240 "\xC2\x85\xC2\x86\xC2\x87\xC2\x88\xC2\x89\xC2\x8A\xC2\x8B\xC2\x8C\xC2\x8D" 241 "\xC2\x8E\xC2\x8F\xC2\x90\xC2\x91\xC2\x92\xC2\x93\xC2\x94\xC2\x95\xC2\x96" 242 "\xC2\x97\xC2\x98\xC2\x99\xC2\x9A\xC2\x9B\xC2\x9C\xC2\x9D\xC2\x9E\xC2\x9F" 243 "\xC2\xA0\xC2\xA1\xC2\xA2\xC2\xA3\xC2\xA4\xC2\xA5\xC2\xA6\xC2\xA7\xC2\xA8" 244 "\xC2\xA9\xC2\xAA\xC2\xAB\xC2\xAC\xC2\xAD\xC2\xAE\xC2\xAF\xC2\xB0\xC2\xB1" 245 "\xC2\xB2\xC2\xB3\xC2\xB4\xC2\xB5\xC2\xB6\xC2\xB7\xC2\xB8\xC2\xB9\xC2\xBA" 246 "\xC2\xBB\xC2\xBC\xC2\xBD\xC2\xBE\xC2\xBF\xC3\x80\xC3\x81\xC3\x82\xC3\x83" 247 "\xC3\x84\xC3\x85\xC3\x86\xC3\x87\xC3\x88\xC3\x89\xC3\x8A\xC3\x8B\xC3\x8C" 248 "\xC3\x8D\xC3\x8E\xC3\x8F\xC3\x90\xC3\x91\xC3\x92\xC3\x93\xC3\x94\xC3\x95" 249 "\xC3\x96\xC3\x97\xC3\x98\xC3\x99\xC3\x9A\xC3\x9B\xC3\x9C\xC3\x9D\xC3\x9E" 250 "\xC3\x9F\xC3\xA0\xC3\xA1\xC3\xA2\xC3\xA3\xC3\xA4\xC3\xA5\xC3\xA6\xC3\xA7" 251 "\xC3\xA8\xC3\xA9\xC3\xAA\xC3\xAB\xC3\xAC\xC3\xAD\xC3\xAE\xC3\xAF\xC3\xB0" 252 "\xC3\xB1\xC3\xB2\xC3\xB3\xC3\xB4\xC3\xB5\xC3\xB6\xC3\xB7\xC3\xB8\xC3\xB9" 253 "\xC3\xBA\xC3\xBB\xC3\xBC\xC3\xBD\xC3\xBE\xC3\xBF"; 254 255 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + 256 "\"}"; 257 // Test JSONWriter interface 258 std::string output_js; 259 DictionaryValue valueRoot; 260 valueRoot.SetString("all_chars", all_chars); 261 JSONWriter::Write(&valueRoot, &output_js); 262 ASSERT_EQ(expected_output, output_js); 263 264 // Test JSONValueSerializer interface (uses JSONWriter). 265 JSONStringValueSerializer serializer(&output_js); 266 ASSERT_TRUE(serializer.Serialize(valueRoot)); 267 ASSERT_EQ(expected_output, output_js); 268 } 269 270 TEST(JSONValueSerializerTest, UnicodeStrings) { 271 // unicode string json -> escaped ascii text 272 DictionaryValue root; 273 string16 test(WideToUTF16(L"\x7F51\x9875")); 274 root.SetString("web", test); 275 276 std::string expected = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}"; 277 278 std::string actual; 279 JSONStringValueSerializer serializer(&actual); 280 ASSERT_TRUE(serializer.Serialize(root)); 281 ASSERT_EQ(expected, actual); 282 283 // escaped ascii text -> json 284 JSONStringValueSerializer deserializer(expected); 285 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 286 ASSERT_TRUE(deserial_root.get()); 287 DictionaryValue* dict_root = 288 static_cast<DictionaryValue*>(deserial_root.get()); 289 string16 web_value; 290 ASSERT_TRUE(dict_root->GetString("web", &web_value)); 291 ASSERT_EQ(test, web_value); 292 } 293 294 TEST(JSONValueSerializerTest, HexStrings) { 295 // hex string json -> escaped ascii text 296 DictionaryValue root; 297 string16 test(WideToUTF16(L"\x01\x02")); 298 root.SetString("test", test); 299 300 std::string expected = "{\"test\":\"\\u0001\\u0002\"}"; 301 302 std::string actual; 303 JSONStringValueSerializer serializer(&actual); 304 ASSERT_TRUE(serializer.Serialize(root)); 305 ASSERT_EQ(expected, actual); 306 307 // escaped ascii text -> json 308 JSONStringValueSerializer deserializer(expected); 309 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 310 ASSERT_TRUE(deserial_root.get()); 311 DictionaryValue* dict_root = 312 static_cast<DictionaryValue*>(deserial_root.get()); 313 string16 test_value; 314 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 315 ASSERT_EQ(test, test_value); 316 317 // Test converting escaped regular chars 318 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}"; 319 JSONStringValueSerializer deserializer2(escaped_chars); 320 deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); 321 ASSERT_TRUE(deserial_root.get()); 322 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); 323 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 324 ASSERT_EQ(ASCIIToUTF16("go"), test_value); 325 } 326 327 TEST(JSONValueSerializerTest, AllowTrailingComma) { 328 scoped_ptr<Value> root; 329 scoped_ptr<Value> root_expected; 330 std::string test_with_commas("{\"key\": [true,],}"); 331 std::string test_no_commas("{\"key\": [true]}"); 332 333 JSONStringValueSerializer serializer(test_with_commas); 334 serializer.set_allow_trailing_comma(true); 335 JSONStringValueSerializer serializer_expected(test_no_commas); 336 root.reset(serializer.Deserialize(NULL, NULL)); 337 ASSERT_TRUE(root.get()); 338 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); 339 ASSERT_TRUE(root_expected.get()); 340 ASSERT_TRUE(root->Equals(root_expected.get())); 341 } 342 343 TEST(JSONValueSerializerTest, JSONReaderComments) { 344 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); 345 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); 346 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); 347 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); 348 ValidateJsonList("[ 1 /* one */ ] /* end */"); 349 ValidateJsonList("[ 1 //// ,2\r\n ]"); 350 351 scoped_ptr<Value> root; 352 353 // It's ok to have a comment in a string. 354 root.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]")); 355 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 356 ListValue* list = static_cast<ListValue*>(root.get()); 357 ASSERT_EQ(1U, list->GetSize()); 358 Value* elt = NULL; 359 ASSERT_TRUE(list->Get(0, &elt)); 360 std::string value; 361 ASSERT_TRUE(elt && elt->GetAsString(&value)); 362 ASSERT_EQ("// ok\n /* foo */ ", value); 363 364 // You can't nest comments. 365 root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]")); 366 ASSERT_FALSE(root.get()); 367 368 // Not a open comment token. 369 root.reset(JSONReader::Read("/ * * / [1]")); 370 ASSERT_FALSE(root.get()); 371 } 372 373 class JSONFileValueSerializerTest : public testing::Test { 374 protected: 375 virtual void SetUp() OVERRIDE { 376 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 377 } 378 379 base::ScopedTempDir temp_dir_; 380 }; 381 382 TEST_F(JSONFileValueSerializerTest, Roundtrip) { 383 base::FilePath original_file_path; 384 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 385 original_file_path = 386 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); 387 388 ASSERT_TRUE(PathExists(original_file_path)); 389 390 JSONFileValueSerializer deserializer(original_file_path); 391 scoped_ptr<Value> root; 392 root.reset(deserializer.Deserialize(NULL, NULL)); 393 394 ASSERT_TRUE(root.get()); 395 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 396 397 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 398 399 Value* null_value = NULL; 400 ASSERT_TRUE(root_dict->Get("null", &null_value)); 401 ASSERT_TRUE(null_value); 402 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 403 404 bool bool_value = false; 405 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); 406 ASSERT_TRUE(bool_value); 407 408 int int_value = 0; 409 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 410 ASSERT_EQ(42, int_value); 411 412 std::string string_value; 413 ASSERT_TRUE(root_dict->GetString("string", &string_value)); 414 ASSERT_EQ("hello", string_value); 415 416 // Now try writing. 417 const base::FilePath written_file_path = 418 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); 419 420 ASSERT_FALSE(PathExists(written_file_path)); 421 JSONFileValueSerializer serializer(written_file_path); 422 ASSERT_TRUE(serializer.Serialize(*root)); 423 ASSERT_TRUE(PathExists(written_file_path)); 424 425 // Now compare file contents. 426 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); 427 EXPECT_TRUE(base::DeleteFile(written_file_path, false)); 428 } 429 430 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { 431 base::FilePath original_file_path; 432 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 433 original_file_path = original_file_path.Append( 434 FILE_PATH_LITERAL("serializer_nested_test.json")); 435 436 ASSERT_TRUE(PathExists(original_file_path)); 437 438 JSONFileValueSerializer deserializer(original_file_path); 439 scoped_ptr<Value> root; 440 root.reset(deserializer.Deserialize(NULL, NULL)); 441 ASSERT_TRUE(root.get()); 442 443 // Now try writing. 444 base::FilePath written_file_path = temp_dir_.path().Append( 445 FILE_PATH_LITERAL("test_output.json")); 446 447 ASSERT_FALSE(PathExists(written_file_path)); 448 JSONFileValueSerializer serializer(written_file_path); 449 ASSERT_TRUE(serializer.Serialize(*root)); 450 ASSERT_TRUE(PathExists(written_file_path)); 451 452 // Now compare file contents. 453 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); 454 EXPECT_TRUE(base::DeleteFile(written_file_path, false)); 455 } 456 457 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { 458 base::FilePath source_file_path; 459 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); 460 source_file_path = source_file_path.Append( 461 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); 462 ASSERT_TRUE(PathExists(source_file_path)); 463 JSONFileValueSerializer serializer(source_file_path); 464 scoped_ptr<Value> root; 465 root.reset(serializer.Deserialize(NULL, NULL)); 466 ASSERT_TRUE(root.get()); 467 } 468 469 } // namespace 470 471 } // namespace base 472