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