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 "base/basictypes.h" 6 #include "base/file_util.h" 7 #include "base/json/json_reader.h" 8 #include "base/json/json_writer.h" 9 #include "base/memory/scoped_temp_dir.h" 10 #include "base/path_service.h" 11 #include "base/string16.h" 12 #include "base/string_util.h" 13 #include "base/utf_string_conversions.h" 14 #include "base/values.h" 15 #include "chrome/common/chrome_paths.h" 16 #include "content/common/json_value_serializer.h" 17 #include "testing/gtest/include/gtest/gtest.h" 18 19 TEST(JSONValueSerializerTest, Roundtrip) { 20 const std::string original_serialization = 21 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; 22 JSONStringValueSerializer serializer(original_serialization); 23 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); 24 ASSERT_TRUE(root.get()); 25 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 26 27 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 28 29 Value* null_value = NULL; 30 ASSERT_TRUE(root_dict->Get("null", &null_value)); 31 ASSERT_TRUE(null_value); 32 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 33 34 bool bool_value = false; 35 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); 36 ASSERT_TRUE(bool_value); 37 38 int int_value = 0; 39 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 40 ASSERT_EQ(42, int_value); 41 42 double double_value = 0.0; 43 ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); 44 ASSERT_DOUBLE_EQ(3.14, double_value); 45 46 // We shouldn't be able to write using this serializer, since it was 47 // initialized with a const string. 48 ASSERT_FALSE(serializer.Serialize(*root_dict)); 49 50 std::string test_serialization = ""; 51 JSONStringValueSerializer mutable_serializer(&test_serialization); 52 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 53 ASSERT_EQ(original_serialization, test_serialization); 54 55 mutable_serializer.set_pretty_print(true); 56 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 57 // JSON output uses a different newline style on Windows than on other 58 // platforms. 59 #if defined(OS_WIN) 60 #define JSON_NEWLINE "\r\n" 61 #else 62 #define JSON_NEWLINE "\n" 63 #endif 64 const std::string pretty_serialization = 65 "{" JSON_NEWLINE 66 " \"bool\": true," JSON_NEWLINE 67 " \"double\": 3.14," JSON_NEWLINE 68 " \"int\": 42," JSON_NEWLINE 69 " \"list\": [ 1, 2 ]," JSON_NEWLINE 70 " \"null\": null" JSON_NEWLINE 71 "}" JSON_NEWLINE; 72 #undef JSON_NEWLINE 73 ASSERT_EQ(pretty_serialization, test_serialization); 74 } 75 76 TEST(JSONValueSerializerTest, StringEscape) { 77 string16 all_chars; 78 for (int i = 1; i < 256; ++i) { 79 all_chars += static_cast<char16>(i); 80 } 81 // Generated in in Firefox using the following js (with an extra backslash for 82 // double quote): 83 // var s = ''; 84 // for (var i = 1; i < 256; ++i) { s += String.fromCharCode(i); } 85 // uneval(s).replace(/\\/g, "\\\\"); 86 std::string all_chars_expected = 87 "\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r" 88 "\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" 89 "\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F !\\\"" 90 "#$%&'()*+,-./0123456789:;\\u003C=\\u003E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\" 91 "\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\u007F\\u0080\\u0081\\u0082\\u0083" 92 "\\u0084\\u0085\\u0086\\u0087\\u0088\\u0089\\u008A\\u008B\\u008C\\u008D" 93 "\\u008E\\u008F\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" 94 "\\u0098\\u0099\\u009A\\u009B\\u009C\\u009D\\u009E\\u009F\\u00A0\\u00A1" 95 "\\u00A2\\u00A3\\u00A4\\u00A5\\u00A6\\u00A7\\u00A8\\u00A9\\u00AA\\u00AB" 96 "\\u00AC\\u00AD\\u00AE\\u00AF\\u00B0\\u00B1\\u00B2\\u00B3\\u00B4\\u00B5" 97 "\\u00B6\\u00B7\\u00B8\\u00B9\\u00BA\\u00BB\\u00BC\\u00BD\\u00BE\\u00BF" 98 "\\u00C0\\u00C1\\u00C2\\u00C3\\u00C4\\u00C5\\u00C6\\u00C7\\u00C8\\u00C9" 99 "\\u00CA\\u00CB\\u00CC\\u00CD\\u00CE\\u00CF\\u00D0\\u00D1\\u00D2\\u00D3" 100 "\\u00D4\\u00D5\\u00D6\\u00D7\\u00D8\\u00D9\\u00DA\\u00DB\\u00DC\\u00DD" 101 "\\u00DE\\u00DF\\u00E0\\u00E1\\u00E2\\u00E3\\u00E4\\u00E5\\u00E6\\u00E7" 102 "\\u00E8\\u00E9\\u00EA\\u00EB\\u00EC\\u00ED\\u00EE\\u00EF\\u00F0\\u00F1" 103 "\\u00F2\\u00F3\\u00F4\\u00F5\\u00F6\\u00F7\\u00F8\\u00F9\\u00FA\\u00FB" 104 "\\u00FC\\u00FD\\u00FE\\u00FF"; 105 106 std::string expected_output = "{\"all_chars\":\"" + all_chars_expected + 107 "\"}"; 108 // Test JSONWriter interface 109 std::string output_js; 110 DictionaryValue valueRoot; 111 valueRoot.SetString("all_chars", all_chars); 112 base::JSONWriter::Write(&valueRoot, false, &output_js); 113 ASSERT_EQ(expected_output, output_js); 114 115 // Test JSONValueSerializer interface (uses JSONWriter). 116 JSONStringValueSerializer serializer(&output_js); 117 ASSERT_TRUE(serializer.Serialize(valueRoot)); 118 ASSERT_EQ(expected_output, output_js); 119 } 120 121 TEST(JSONValueSerializerTest, UnicodeStrings) { 122 // unicode string json -> escaped ascii text 123 DictionaryValue root; 124 string16 test(WideToUTF16(L"\x7F51\x9875")); 125 root.SetString("web", test); 126 127 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; 128 129 std::string actual; 130 JSONStringValueSerializer serializer(&actual); 131 ASSERT_TRUE(serializer.Serialize(root)); 132 ASSERT_EQ(expected, actual); 133 134 // escaped ascii text -> json 135 JSONStringValueSerializer deserializer(expected); 136 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 137 ASSERT_TRUE(deserial_root.get()); 138 DictionaryValue* dict_root = 139 static_cast<DictionaryValue*>(deserial_root.get()); 140 string16 web_value; 141 ASSERT_TRUE(dict_root->GetString("web", &web_value)); 142 ASSERT_EQ(test, web_value); 143 } 144 145 TEST(JSONValueSerializerTest, HexStrings) { 146 // hex string json -> escaped ascii text 147 DictionaryValue root; 148 string16 test(WideToUTF16(L"\x01\x02")); 149 root.SetString("test", test); 150 151 std::string expected = "{\"test\":\"\\u0001\\u0002\"}"; 152 153 std::string actual; 154 JSONStringValueSerializer serializer(&actual); 155 ASSERT_TRUE(serializer.Serialize(root)); 156 ASSERT_EQ(expected, actual); 157 158 // escaped ascii text -> json 159 JSONStringValueSerializer deserializer(expected); 160 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 161 ASSERT_TRUE(deserial_root.get()); 162 DictionaryValue* dict_root = 163 static_cast<DictionaryValue*>(deserial_root.get()); 164 string16 test_value; 165 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 166 ASSERT_EQ(test, test_value); 167 168 // Test converting escaped regular chars 169 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}"; 170 JSONStringValueSerializer deserializer2(escaped_chars); 171 deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); 172 ASSERT_TRUE(deserial_root.get()); 173 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); 174 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 175 ASSERT_EQ(ASCIIToUTF16("go"), test_value); 176 } 177 178 TEST(JSONValueSerializerTest, AllowTrailingComma) { 179 scoped_ptr<Value> root; 180 scoped_ptr<Value> root_expected; 181 std::string test_with_commas("{\"key\": [true,],}"); 182 std::string test_no_commas("{\"key\": [true]}"); 183 184 JSONStringValueSerializer serializer(test_with_commas); 185 serializer.set_allow_trailing_comma(true); 186 JSONStringValueSerializer serializer_expected(test_no_commas); 187 root.reset(serializer.Deserialize(NULL, NULL)); 188 ASSERT_TRUE(root.get()); 189 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); 190 ASSERT_TRUE(root_expected.get()); 191 ASSERT_TRUE(root->Equals(root_expected.get())); 192 } 193 194 namespace { 195 196 void ValidateJsonList(const std::string& json) { 197 scoped_ptr<Value> root(base::JSONReader::Read(json, false)); 198 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 199 ListValue* list = static_cast<ListValue*>(root.get()); 200 ASSERT_EQ(1U, list->GetSize()); 201 Value* elt = NULL; 202 ASSERT_TRUE(list->Get(0, &elt)); 203 int value = 0; 204 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); 205 ASSERT_EQ(1, value); 206 } 207 208 } // namespace 209 210 TEST(JSONValueSerializerTest, JSONReaderComments) { 211 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); 212 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); 213 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); 214 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); 215 ValidateJsonList("[ 1 /* one */ ] /* end */"); 216 ValidateJsonList("[ 1 //// ,2\r\n ]"); 217 218 scoped_ptr<Value> root; 219 220 // It's ok to have a comment in a string. 221 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); 222 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 223 ListValue* list = static_cast<ListValue*>(root.get()); 224 ASSERT_EQ(1U, list->GetSize()); 225 Value* elt = NULL; 226 ASSERT_TRUE(list->Get(0, &elt)); 227 std::string value; 228 ASSERT_TRUE(elt && elt->GetAsString(&value)); 229 ASSERT_EQ("// ok\n /* foo */ ", value); 230 231 // You can't nest comments. 232 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); 233 ASSERT_FALSE(root.get()); 234 235 // Not a open comment token. 236 root.reset(base::JSONReader::Read("/ * * / [1]", false)); 237 ASSERT_FALSE(root.get()); 238 } 239 240 class JSONFileValueSerializerTest : public testing::Test { 241 protected: 242 virtual void SetUp() { 243 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 244 } 245 246 ScopedTempDir temp_dir_; 247 }; 248 249 TEST_F(JSONFileValueSerializerTest, Roundtrip) { 250 FilePath original_file_path; 251 ASSERT_TRUE( 252 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); 253 original_file_path = 254 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js")); 255 256 ASSERT_TRUE(file_util::PathExists(original_file_path)); 257 258 JSONFileValueSerializer deserializer(original_file_path); 259 scoped_ptr<Value> root; 260 root.reset(deserializer.Deserialize(NULL, NULL)); 261 262 ASSERT_TRUE(root.get()); 263 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 264 265 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 266 267 Value* null_value = NULL; 268 ASSERT_TRUE(root_dict->Get("null", &null_value)); 269 ASSERT_TRUE(null_value); 270 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 271 272 bool bool_value = false; 273 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); 274 ASSERT_TRUE(bool_value); 275 276 int int_value = 0; 277 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 278 ASSERT_EQ(42, int_value); 279 280 std::string string_value; 281 ASSERT_TRUE(root_dict->GetString("string", &string_value)); 282 ASSERT_EQ("hello", string_value); 283 284 // Now try writing. 285 const FilePath written_file_path = 286 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); 287 288 ASSERT_FALSE(file_util::PathExists(written_file_path)); 289 JSONFileValueSerializer serializer(written_file_path); 290 ASSERT_TRUE(serializer.Serialize(*root)); 291 ASSERT_TRUE(file_util::PathExists(written_file_path)); 292 293 // Now compare file contents. 294 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, 295 written_file_path)); 296 EXPECT_TRUE(file_util::Delete(written_file_path, false)); 297 } 298 299 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { 300 FilePath original_file_path; 301 ASSERT_TRUE( 302 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); 303 original_file_path = 304 original_file_path.Append(FILE_PATH_LITERAL("serializer_nested_test.js")); 305 306 ASSERT_TRUE(file_util::PathExists(original_file_path)); 307 308 JSONFileValueSerializer deserializer(original_file_path); 309 scoped_ptr<Value> root; 310 root.reset(deserializer.Deserialize(NULL, NULL)); 311 ASSERT_TRUE(root.get()); 312 313 // Now try writing. 314 FilePath written_file_path = 315 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); 316 317 ASSERT_FALSE(file_util::PathExists(written_file_path)); 318 JSONFileValueSerializer serializer(written_file_path); 319 ASSERT_TRUE(serializer.Serialize(*root)); 320 ASSERT_TRUE(file_util::PathExists(written_file_path)); 321 322 // Now compare file contents. 323 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, 324 written_file_path)); 325 EXPECT_TRUE(file_util::Delete(written_file_path, false)); 326 } 327 328 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { 329 FilePath source_file_path; 330 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); 331 source_file_path = source_file_path.Append( 332 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); 333 ASSERT_TRUE(file_util::PathExists(source_file_path)); 334 JSONFileValueSerializer serializer(source_file_path); 335 scoped_ptr<Value> root; 336 root.reset(serializer.Deserialize(NULL, NULL)); 337 ASSERT_TRUE(root.get()); 338 } 339