• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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/json/string_escape.h"
6 
7 #include <gtest/gtest.h>
8 #include <stddef.h>
9 
10 #include "base/macros.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversion_utils.h"
13 
14 namespace base {
15 
TEST(JSONStringEscapeTest,EscapeUTF8)16 TEST(JSONStringEscapeTest, EscapeUTF8) {
17   const struct {
18     const char* to_escape;
19     const char* escaped;
20   } cases[] = {
21     {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
22     {"a\b\f\n\r\t\v\1\\.\"z",
23         "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
24     {"b\x0f\x7f\xf0\xff!",  // \xf0\xff is not a valid UTF-8 unit.
25         "b\\u000F\x7F\xEF\xBF\xBD\xEF\xBF\xBD!"},
26     {"c<>d", "c\\u003C>d"},
27     {"Hello\xe2\x80\xa8world", "Hello\\u2028world"},
28     {"\xe2\x80\xa9purple", "\\u2029purple"},
29   };
30 
31   for (size_t i = 0; i < arraysize(cases); ++i) {
32     const char* in_ptr = cases[i].to_escape;
33     std::string in_str = in_ptr;
34 
35     std::string out;
36     EscapeJSONString(in_ptr, false, &out);
37     EXPECT_EQ(std::string(cases[i].escaped), out);
38     EXPECT_TRUE(IsStringUTF8(out));
39 
40     out.erase();
41     bool convert_ok = EscapeJSONString(in_str, false, &out);
42     EXPECT_EQ(std::string(cases[i].escaped), out);
43     EXPECT_TRUE(IsStringUTF8(out));
44 
45     if (convert_ok) {
46       std::string fooout = GetQuotedJSONString(in_str);
47       EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", fooout);
48       EXPECT_TRUE(IsStringUTF8(out));
49     }
50   }
51 
52   std::string in = cases[0].to_escape;
53   std::string out;
54   EscapeJSONString(in, false, &out);
55   EXPECT_TRUE(IsStringUTF8(out));
56 
57   // test quoting
58   std::string out_quoted;
59   EscapeJSONString(in, true, &out_quoted);
60   EXPECT_EQ(out.length() + 2, out_quoted.length());
61   EXPECT_EQ(out_quoted.find(out), 1U);
62   EXPECT_TRUE(IsStringUTF8(out_quoted));
63 
64   // now try with a NULL in the string
65   std::string null_prepend = "test";
66   null_prepend.push_back(0);
67   in = null_prepend + in;
68   std::string expected = "test\\u0000";
69   expected += cases[0].escaped;
70   out.clear();
71   EscapeJSONString(in, false, &out);
72   EXPECT_EQ(expected, out);
73   EXPECT_TRUE(IsStringUTF8(out));
74 }
75 
TEST(JSONStringEscapeTest,EscapeBytes)76 TEST(JSONStringEscapeTest, EscapeBytes) {
77   const struct {
78     const char* to_escape;
79     const char* escaped;
80   } cases[] = {
81     {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
82     {"\xe5\xc4\x4f\x05\xb6\xfd", "\\u00E5\\u00C4O\\u0005\\u00B6\\u00FD"},
83   };
84 
85   for (size_t i = 0; i < arraysize(cases); ++i) {
86     std::string in = std::string(cases[i].to_escape);
87     EXPECT_FALSE(IsStringUTF8(in));
88 
89     EXPECT_EQ(std::string(cases[i].escaped),
90         EscapeBytesAsInvalidJSONString(in, false));
91     EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"",
92         EscapeBytesAsInvalidJSONString(in, true));
93   }
94 
95   const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' };
96   std::string in(kEmbedNull, arraysize(kEmbedNull));
97   EXPECT_FALSE(IsStringUTF8(in));
98   EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"),
99             EscapeBytesAsInvalidJSONString(in, false));
100 }
101 
102 }  // namespace base
103