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