OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/json/string_escape.h" | 5 #include "base/json/string_escape.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
6 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 | 10 |
9 namespace base { | 11 namespace base { |
10 | 12 |
11 namespace { | 13 TEST(JSONStringEscapeTest, EscapeUTF8) { |
| 14 const struct { |
| 15 const char* to_escape; |
| 16 const char* escaped; |
| 17 } cases[] = { |
| 18 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
| 19 {"a\b\f\n\r\t\v\1\\.\"z", |
| 20 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, |
| 21 {"b\x0f\x7f\xf0\xff!", // \xf0\xff is not a valid UTF-8 unit. |
| 22 "b\\u000F\x7F\xEF\xBF\xBD\xEF\xBF\xBD!"}, |
| 23 {"c<>d", "c\\u003C>d"}, |
| 24 }; |
12 | 25 |
13 const struct json_narrow_test_data { | 26 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
14 const char* to_escape; | 27 const char* in_ptr = cases[i].to_escape; |
15 const char* escaped; | 28 std::string in_str = in_ptr; |
16 } json_narrow_cases[] = { | |
17 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | |
18 {"a\b\f\n\r\t\v\1\\.\"z", | |
19 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, | |
20 {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, | |
21 {"c<>d", "c\\u003C\\u003Ed"}, | |
22 }; | |
23 | 29 |
24 } // namespace | 30 std::string out; |
| 31 EscapeJSONString(in_ptr, false, &out); |
| 32 EXPECT_EQ(std::string(cases[i].escaped), out); |
| 33 EXPECT_TRUE(IsStringUTF8(out)); |
25 | 34 |
26 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) { | |
27 for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) { | |
28 const char* in_ptr = json_narrow_cases[i].to_escape; | |
29 std::string in_str = in_ptr; | |
30 std::string out; | |
31 JsonDoubleQuote(in_ptr, false, &out); | |
32 EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out); | |
33 out.erase(); | 35 out.erase(); |
34 JsonDoubleQuote(in_str, false, &out); | 36 bool convert_ok = EscapeJSONString(in_str, false, &out); |
35 EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out); | 37 EXPECT_EQ(std::string(cases[i].escaped), out); |
| 38 EXPECT_TRUE(IsStringUTF8(out)); |
| 39 |
| 40 if (convert_ok) { |
| 41 std::string fooout = GetQuotedJSONString(in_str); |
| 42 EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", fooout); |
| 43 EXPECT_TRUE(IsStringUTF8(out)); |
| 44 } |
36 } | 45 } |
37 | 46 |
38 std::string in = json_narrow_cases[0].to_escape; | 47 std::string in = cases[0].to_escape; |
39 std::string out; | 48 std::string out; |
40 JsonDoubleQuote(in, false, &out); | 49 EscapeJSONString(in, false, &out); |
| 50 EXPECT_TRUE(IsStringUTF8(out)); |
41 | 51 |
42 // test quoting | 52 // test quoting |
43 std::string out_quoted; | 53 std::string out_quoted; |
44 JsonDoubleQuote(in, true, &out_quoted); | 54 EscapeJSONString(in, true, &out_quoted); |
45 EXPECT_EQ(out.length() + 2, out_quoted.length()); | 55 EXPECT_EQ(out.length() + 2, out_quoted.length()); |
46 EXPECT_EQ(out_quoted.find(out), 1U); | 56 EXPECT_EQ(out_quoted.find(out), 1U); |
| 57 EXPECT_TRUE(IsStringUTF8(out_quoted)); |
47 | 58 |
48 // now try with a NULL in the string | 59 // now try with a NULL in the string |
49 std::string null_prepend = "test"; | 60 std::string null_prepend = "test"; |
50 null_prepend.push_back(0); | 61 null_prepend.push_back(0); |
51 in = null_prepend + in; | 62 in = null_prepend + in; |
52 std::string expected = "test\\u0000"; | 63 std::string expected = "test\\u0000"; |
53 expected += json_narrow_cases[0].escaped; | 64 expected += cases[0].escaped; |
54 out.clear(); | 65 out.clear(); |
55 JsonDoubleQuote(in, false, &out); | 66 EscapeJSONString(in, false, &out); |
56 EXPECT_EQ(expected, out); | 67 EXPECT_EQ(expected, out); |
| 68 EXPECT_TRUE(IsStringUTF8(out)); |
57 } | 69 } |
58 | 70 |
59 namespace { | 71 TEST(JSONStringEscapeTest, EscapeUTF16) { |
| 72 const struct { |
| 73 const wchar_t* to_escape; |
| 74 const char* escaped; |
| 75 } cases[] = { |
| 76 {L"b\uffb1\u00ff", "b\xEF\xBE\xB1\xC3\xBF"}, |
| 77 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
| 78 {L"a\b\f\n\r\t\v\1\\.\"z", |
| 79 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, |
| 80 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\x7F\xC3\xB0\xC3\xBF!"}, |
| 81 {L"c<>d", "c\\u003C>d"}, |
| 82 }; |
60 | 83 |
61 const struct json_wide_test_data { | 84 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
62 const wchar_t* to_escape; | 85 string16 in = WideToUTF16(cases[i].to_escape); |
63 const char* escaped; | |
64 } json_wide_cases[] = { | |
65 {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"}, | |
66 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | |
67 {L"a\b\f\n\r\t\v\1\\.\"z", | |
68 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, | |
69 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, | |
70 {L"c<>d", "c\\u003C\\u003Ed"}, | |
71 }; | |
72 | 86 |
73 } // namespace | 87 std::string out; |
| 88 EscapeJSONString(in, false, &out); |
| 89 EXPECT_EQ(std::string(cases[i].escaped), out); |
| 90 EXPECT_TRUE(IsStringUTF8(out)); |
74 | 91 |
75 TEST(StringEscapeTest, JsonDoubleQuoteWide) { | 92 out = GetQuotedJSONString(in); |
76 for (size_t i = 0; i < arraysize(json_wide_cases); ++i) { | 93 EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", out); |
77 std::string out; | 94 EXPECT_TRUE(IsStringUTF8(out)); |
78 string16 in = WideToUTF16(json_wide_cases[i].to_escape); | |
79 JsonDoubleQuote(in, false, &out); | |
80 EXPECT_EQ(std::string(json_wide_cases[i].escaped), out); | |
81 } | 95 } |
82 | 96 |
83 string16 in = WideToUTF16(json_wide_cases[0].to_escape); | 97 string16 in = WideToUTF16(cases[0].to_escape); |
84 std::string out; | 98 std::string out; |
85 JsonDoubleQuote(in, false, &out); | 99 EscapeJSONString(in, false, &out); |
| 100 EXPECT_TRUE(IsStringUTF8(out)); |
86 | 101 |
87 // test quoting | 102 // test quoting |
88 std::string out_quoted; | 103 std::string out_quoted; |
89 JsonDoubleQuote(in, true, &out_quoted); | 104 EscapeJSONString(in, true, &out_quoted); |
90 EXPECT_EQ(out.length() + 2, out_quoted.length()); | 105 EXPECT_EQ(out.length() + 2, out_quoted.length()); |
91 EXPECT_EQ(out_quoted.find(out), 1U); | 106 EXPECT_EQ(out_quoted.find(out), 1U); |
| 107 EXPECT_TRUE(IsStringUTF8(out)); |
92 | 108 |
93 // now try with a NULL in the string | 109 // now try with a NULL in the string |
94 string16 null_prepend = WideToUTF16(L"test"); | 110 string16 null_prepend = WideToUTF16(L"test"); |
95 null_prepend.push_back(0); | 111 null_prepend.push_back(0); |
96 in = null_prepend + in; | 112 in = null_prepend + in; |
97 std::string expected = "test\\u0000"; | 113 std::string expected = "test\\u0000"; |
98 expected += json_wide_cases[0].escaped; | 114 expected += cases[0].escaped; |
99 out.clear(); | 115 out.clear(); |
100 JsonDoubleQuote(in, false, &out); | 116 EscapeJSONString(in, false, &out); |
101 EXPECT_EQ(expected, out); | 117 EXPECT_EQ(expected, out); |
| 118 EXPECT_TRUE(IsStringUTF8(out)); |
| 119 } |
| 120 |
| 121 TEST(JSONStringEscapeTest, EscapeUTF16OutsideBMP) { |
| 122 { |
| 123 // {a, U+10300, !}, SMP. |
| 124 string16 test; |
| 125 test.push_back('a'); |
| 126 test.push_back(0xD800); |
| 127 test.push_back(0xDF00); |
| 128 test.push_back('!'); |
| 129 std::string actual; |
| 130 EXPECT_TRUE(EscapeJSONString(test, false, &actual)); |
| 131 EXPECT_EQ("a\xF0\x90\x8C\x80!", actual); |
| 132 } |
| 133 { |
| 134 // {U+20021, U+2002B}, SIP. |
| 135 string16 test; |
| 136 test.push_back(0xD840); |
| 137 test.push_back(0xDC21); |
| 138 test.push_back(0xD840); |
| 139 test.push_back(0xDC2B); |
| 140 std::string actual; |
| 141 EXPECT_TRUE(EscapeJSONString(test, false, &actual)); |
| 142 EXPECT_EQ("\xF0\xA0\x80\xA1\xF0\xA0\x80\xAB", actual); |
| 143 } |
| 144 { |
| 145 // {?, U+D800, @}, lone surrogate. |
| 146 string16 test; |
| 147 test.push_back('?'); |
| 148 test.push_back(0xD800); |
| 149 test.push_back('@'); |
| 150 std::string actual; |
| 151 EXPECT_FALSE(EscapeJSONString(test, false, &actual)); |
| 152 EXPECT_EQ("?\xEF\xBF\xBD@", actual); |
| 153 } |
| 154 } |
| 155 |
| 156 TEST(JSONStringEscapeTest, EscapeBytes) { |
| 157 const struct { |
| 158 const char* to_escape; |
| 159 const char* escaped; |
| 160 } cases[] = { |
| 161 {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, |
| 162 {"\xe5\xc4\x4f\x05\xb6\xfd\0", "\\u00E5\\u00C4O\\u0005\\u00B6\\u00FD"}, |
| 163 }; |
| 164 |
| 165 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 166 std::string in = std::string(cases[i].to_escape); |
| 167 EXPECT_FALSE(IsStringUTF8(in)); |
| 168 |
| 169 EXPECT_EQ(std::string(cases[i].escaped), |
| 170 EscapeBytesAsInvalidJSONString(in, false)); |
| 171 EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", |
| 172 EscapeBytesAsInvalidJSONString(in, true)); |
| 173 } |
| 174 |
| 175 const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' }; |
| 176 std::string in(kEmbedNull, ARRAYSIZE_UNSAFE(kEmbedNull)); |
| 177 EXPECT_FALSE(IsStringUTF8(in)); |
| 178 EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"), |
| 179 EscapeBytesAsInvalidJSONString(in, false)); |
102 } | 180 } |
103 | 181 |
104 } // namespace base | 182 } // namespace base |
OLD | NEW |