OLD | NEW |
1 // Copyright (c) 2013 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 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 TEST(JSONStringEscapeTest, EscapeUTF8) { | 13 TEST(JSONStringEscapeTest, EscapeUTF8) { |
14 const struct { | 14 const struct { |
15 const char* to_escape; | 15 const char* to_escape; |
16 const char* escaped; | 16 const char* escaped; |
17 } cases[] = { | 17 } cases[] = { |
18 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | 18 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
19 {"a\b\f\n\r\t\v\1\\.\"z", | 19 {"a\b\f\n\r\t\v\1\\.\"z", |
20 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"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. | 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!"}, | 22 "b\\u000F\x7F\xEF\xBF\xBD\xEF\xBF\xBD!"}, |
23 {"c<>d", "c\\u003C>d"}, | 23 {"c<>d", "c\\u003C>d"}, |
24 }; | 24 }; |
25 | 25 |
26 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 26 for (size_t i = 0; i < arraysize(cases); ++i) { |
27 const char* in_ptr = cases[i].to_escape; | 27 const char* in_ptr = cases[i].to_escape; |
28 std::string in_str = in_ptr; | 28 std::string in_str = in_ptr; |
29 | 29 |
30 std::string out; | 30 std::string out; |
31 EscapeJSONString(in_ptr, false, &out); | 31 EscapeJSONString(in_ptr, false, &out); |
32 EXPECT_EQ(std::string(cases[i].escaped), out); | 32 EXPECT_EQ(std::string(cases[i].escaped), out); |
33 EXPECT_TRUE(IsStringUTF8(out)); | 33 EXPECT_TRUE(IsStringUTF8(out)); |
34 | 34 |
35 out.erase(); | 35 out.erase(); |
36 bool convert_ok = EscapeJSONString(in_str, false, &out); | 36 bool convert_ok = EscapeJSONString(in_str, false, &out); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 const char* escaped; | 74 const char* escaped; |
75 } cases[] = { | 75 } cases[] = { |
76 {L"b\uffb1\u00ff", "b\xEF\xBE\xB1\xC3\xBF"}, | 76 {L"b\uffb1\u00ff", "b\xEF\xBE\xB1\xC3\xBF"}, |
77 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | 77 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
78 {L"a\b\f\n\r\t\v\1\\.\"z", | 78 {L"a\b\f\n\r\t\v\1\\.\"z", |
79 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"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!"}, | 80 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\x7F\xC3\xB0\xC3\xBF!"}, |
81 {L"c<>d", "c\\u003C>d"}, | 81 {L"c<>d", "c\\u003C>d"}, |
82 }; | 82 }; |
83 | 83 |
84 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 84 for (size_t i = 0; i < arraysize(cases); ++i) { |
85 string16 in = WideToUTF16(cases[i].to_escape); | 85 string16 in = WideToUTF16(cases[i].to_escape); |
86 | 86 |
87 std::string out; | 87 std::string out; |
88 EscapeJSONString(in, false, &out); | 88 EscapeJSONString(in, false, &out); |
89 EXPECT_EQ(std::string(cases[i].escaped), out); | 89 EXPECT_EQ(std::string(cases[i].escaped), out); |
90 EXPECT_TRUE(IsStringUTF8(out)); | 90 EXPECT_TRUE(IsStringUTF8(out)); |
91 | 91 |
92 out = GetQuotedJSONString(in); | 92 out = GetQuotedJSONString(in); |
93 EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", out); | 93 EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", out); |
94 EXPECT_TRUE(IsStringUTF8(out)); | 94 EXPECT_TRUE(IsStringUTF8(out)); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 155 |
156 TEST(JSONStringEscapeTest, EscapeBytes) { | 156 TEST(JSONStringEscapeTest, EscapeBytes) { |
157 const struct { | 157 const struct { |
158 const char* to_escape; | 158 const char* to_escape; |
159 const char* escaped; | 159 const char* escaped; |
160 } cases[] = { | 160 } cases[] = { |
161 {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, | 161 {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, |
162 {"\xe5\xc4\x4f\x05\xb6\xfd\0", "\\u00E5\\u00C4O\\u0005\\u00B6\\u00FD"}, | 162 {"\xe5\xc4\x4f\x05\xb6\xfd\0", "\\u00E5\\u00C4O\\u0005\\u00B6\\u00FD"}, |
163 }; | 163 }; |
164 | 164 |
165 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 165 for (size_t i = 0; i < arraysize(cases); ++i) { |
166 std::string in = std::string(cases[i].to_escape); | 166 std::string in = std::string(cases[i].to_escape); |
167 EXPECT_FALSE(IsStringUTF8(in)); | 167 EXPECT_FALSE(IsStringUTF8(in)); |
168 | 168 |
169 EXPECT_EQ(std::string(cases[i].escaped), | 169 EXPECT_EQ(std::string(cases[i].escaped), |
170 EscapeBytesAsInvalidJSONString(in, false)); | 170 EscapeBytesAsInvalidJSONString(in, false)); |
171 EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", | 171 EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", |
172 EscapeBytesAsInvalidJSONString(in, true)); | 172 EscapeBytesAsInvalidJSONString(in, true)); |
173 } | 173 } |
174 | 174 |
175 const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' }; | 175 const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' }; |
176 std::string in(kEmbedNull, ARRAYSIZE_UNSAFE(kEmbedNull)); | 176 std::string in(kEmbedNull, arraysize(kEmbedNull)); |
177 EXPECT_FALSE(IsStringUTF8(in)); | 177 EXPECT_FALSE(IsStringUTF8(in)); |
178 EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"), | 178 EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"), |
179 EscapeBytesAsInvalidJSONString(in, false)); | 179 EscapeBytesAsInvalidJSONString(in, false)); |
180 } | 180 } |
181 | 181 |
182 } // namespace base | 182 } // namespace base |
OLD | NEW |