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