OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "net/base/escape.h" | 7 #include "net/base/escape.h" |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 struct EscapeCase { | 16 struct EscapeCase { |
17 const wchar_t* input; | 17 const wchar_t* input; |
18 const wchar_t* output; | 18 const wchar_t* output; |
19 }; | 19 }; |
20 | 20 |
21 struct UnescapeURLCase { | 21 struct UnescapeURLCase { |
| 22 const wchar_t* input; |
| 23 UnescapeRule::Type rules; |
| 24 const wchar_t* output; |
| 25 }; |
| 26 |
| 27 struct UnescapeURLCaseASCII { |
22 const char* input; | 28 const char* input; |
23 UnescapeRule::Type rules; | 29 UnescapeRule::Type rules; |
24 const char* output; | 30 const char* output; |
25 }; | 31 }; |
26 | 32 |
27 struct UnescapeAndDecodeCase { | 33 struct UnescapeAndDecodeCase { |
28 const char* input; | 34 const char* input; |
29 | 35 |
30 // The expected output when run through UnescapeURL. | 36 // The expected output when run through UnescapeURL. |
31 const char* url_unescaped; | 37 const char* url_unescaped; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 128 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
123 "[\\]^_`abcdefghijklmnopqrstuvwxyz" | 129 "[\\]^_`abcdefghijklmnopqrstuvwxyz" |
124 "{|}~\x7f\x80\xff"), | 130 "{|}~\x7f\x80\xff"), |
125 // Escaped | 131 // Escaped |
126 "%02%0A%1D+!%22%23%24%25%26%27()*%2B,-./0123456789:%3B" | 132 "%02%0A%1D+!%22%23%24%25%26%27()*%2B,-./0123456789:%3B" |
127 "%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 133 "%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
128 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" | 134 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" |
129 "%7B%7C%7D~%7F%80%FF"); | 135 "%7B%7C%7D~%7F%80%FF"); |
130 } | 136 } |
131 | 137 |
132 TEST(EscapeTest, UnescapeURLComponent) { | 138 TEST(EscapeTest, UnescapeURLComponentASCII) { |
133 const UnescapeURLCase unescape_cases[] = { | 139 const UnescapeURLCaseASCII unescape_cases[] = { |
134 {"", UnescapeRule::NORMAL, ""}, | 140 {"", UnescapeRule::NORMAL, ""}, |
135 {"%2", UnescapeRule::NORMAL, "%2"}, | 141 {"%2", UnescapeRule::NORMAL, "%2"}, |
136 {"%%%%%%", UnescapeRule::NORMAL, "%%%%%%"}, | 142 {"%%%%%%", UnescapeRule::NORMAL, "%%%%%%"}, |
137 {"Don't escape anything", UnescapeRule::NORMAL, "Don't escape anything"}, | 143 {"Don't escape anything", UnescapeRule::NORMAL, "Don't escape anything"}, |
138 {"Invalid %escape %2", UnescapeRule::NORMAL, "Invalid %escape %2"}, | 144 {"Invalid %escape %2", UnescapeRule::NORMAL, "Invalid %escape %2"}, |
139 {"Some%20random text %25%3bOK", UnescapeRule::NONE, | 145 {"Some%20random text %25%3bOK", UnescapeRule::NONE, |
140 "Some%20random text %25%3bOK"}, | 146 "Some%20random text %25%3bOK"}, |
141 {"Some%20random text %25%3bOK", UnescapeRule::NORMAL, | 147 {"Some%20random text %25%3bOK", UnescapeRule::NORMAL, |
142 "Some%20random text %25;OK"}, | 148 "Some%20random text %25;OK"}, |
143 {"Some%20random text %25%3bOK", UnescapeRule::SPACES, | 149 {"Some%20random text %25%3bOK", UnescapeRule::SPACES, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 expected.append("9Test"); | 189 expected.append("9Test"); |
184 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::CONTROL_CHARS)); | 190 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::CONTROL_CHARS)); |
185 | 191 |
186 // When we're not unescaping NULLs. | 192 // When we're not unescaping NULLs. |
187 expected = "Null"; | 193 expected = "Null"; |
188 expected.push_back(0); | 194 expected.push_back(0); |
189 expected.append("%009Test"); | 195 expected.append("%009Test"); |
190 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); | 196 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); |
191 } | 197 } |
192 | 198 |
| 199 TEST(EscapeTest, UnescapeURLComponent) { |
| 200 const UnescapeURLCase unescape_cases[] = { |
| 201 {L"", UnescapeRule::NORMAL, L""}, |
| 202 {L"%2", UnescapeRule::NORMAL, L"%2"}, |
| 203 {L"%%%%%%", UnescapeRule::NORMAL, L"%%%%%%"}, |
| 204 {L"Don't escape anything", UnescapeRule::NORMAL, L"Don't escape anything"}, |
| 205 {L"Invalid %escape %2", UnescapeRule::NORMAL, L"Invalid %escape %2"}, |
| 206 {L"Some%20random text %25%3bOK", UnescapeRule::NONE, |
| 207 L"Some%20random text %25%3bOK"}, |
| 208 {L"Some%20random text %25%3bOK", UnescapeRule::NORMAL, |
| 209 L"Some%20random text %25;OK"}, |
| 210 {L"Some%20random text %25%3bOK", UnescapeRule::SPACES, |
| 211 L"Some random text %25;OK"}, |
| 212 {L"Some%20random text %25%3bOK", UnescapeRule::URL_SPECIAL_CHARS, |
| 213 L"Some%20random text %;OK"}, |
| 214 {L"Some%20random text %25%3bOK", |
| 215 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, |
| 216 L"Some random text %;OK"}, |
| 217 {L"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, L"\xA0\xB1\xC2\xD3\xE4\xF5"}, |
| 218 {L"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, L"\xAa\xBb\xCc\xDd\xEe\xFf"}, |
| 219 // Certain URL-sensitive characters should not be unescaped unless asked. |
| 220 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES, |
| 221 L"Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, |
| 222 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 223 UnescapeRule::URL_SPECIAL_CHARS, |
| 224 L"Hello%20%13%10world ## ?? == && %% ++"}, |
| 225 // Control characters. |
| 226 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, |
| 227 L"%01%02%03%04%05%06%07%08%09 %"}, |
| 228 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, |
| 229 L"\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, |
| 230 {L"Hello%20%13%10%02", UnescapeRule::SPACES, L"Hello %13%10%02"}, |
| 231 {L"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, |
| 232 L"Hello%20\x13\x10\x02"}, |
| 233 {L"Hello\x9824\x9827", UnescapeRule::CONTROL_CHARS, |
| 234 L"Hello\x9824\x9827"}, |
| 235 }; |
| 236 |
| 237 for (size_t i = 0; i < arraysize(unescape_cases); i++) { |
| 238 string16 str(WideToUTF16(unescape_cases[i].input)); |
| 239 EXPECT_EQ(WideToUTF16(unescape_cases[i].output), |
| 240 UnescapeURLComponent(str, unescape_cases[i].rules)); |
| 241 } |
| 242 |
| 243 // Test the NULL character unescaping (which wouldn't work above since those |
| 244 // are just char pointers). |
| 245 string16 input(WideToUTF16(L"Null")); |
| 246 input.push_back(0); // Also have a NULL in the input. |
| 247 input.append(WideToUTF16(L"%00%39Test")); |
| 248 |
| 249 // When we're unescaping NULLs |
| 250 string16 expected(WideToUTF16(L"Null")); |
| 251 expected.push_back(0); |
| 252 expected.push_back(0); |
| 253 expected.append(ASCIIToUTF16("9Test")); |
| 254 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::CONTROL_CHARS)); |
| 255 |
| 256 // When we're not unescaping NULLs. |
| 257 expected = WideToUTF16(L"Null"); |
| 258 expected.push_back(0); |
| 259 expected.append(WideToUTF16(L"%009Test")); |
| 260 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); |
| 261 } |
| 262 |
193 TEST(EscapeTest, UnescapeAndDecodeUTF8URLComponent) { | 263 TEST(EscapeTest, UnescapeAndDecodeUTF8URLComponent) { |
194 const UnescapeAndDecodeCase unescape_cases[] = { | 264 const UnescapeAndDecodeCase unescape_cases[] = { |
195 { "%", | 265 { "%", |
196 "%", | 266 "%", |
197 "%", | 267 "%", |
198 L"%"}, | 268 L"%"}, |
199 { "+", | 269 { "+", |
200 "+", | 270 "+", |
201 " ", | 271 " ", |
202 L"+"}, | 272 L"+"}, |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 const EscapeForHTMLCase tests[] = { | 348 const EscapeForHTMLCase tests[] = { |
279 { "hello", "hello" }, | 349 { "hello", "hello" }, |
280 { "<hello>", "<hello>" }, | 350 { "<hello>", "<hello>" }, |
281 { "don\'t mess with me", "don't mess with me" }, | 351 { "don\'t mess with me", "don't mess with me" }, |
282 }; | 352 }; |
283 for (size_t i = 0; i < arraysize(tests); ++i) { | 353 for (size_t i = 0; i < arraysize(tests); ++i) { |
284 std::string result = EscapeForHTML(std::string(tests[i].input)); | 354 std::string result = EscapeForHTML(std::string(tests[i].input)); |
285 EXPECT_EQ(std::string(tests[i].expected_output), result); | 355 EXPECT_EQ(std::string(tests[i].expected_output), result); |
286 } | 356 } |
287 } | 357 } |
| 358 |
| 359 TEST(EscapeTest, UnescapeForHTML) { |
| 360 const EscapeForHTMLCase tests[] = { |
| 361 { "", "" }, |
| 362 { "<hello>", "<hello>" }, |
| 363 { "don't mess with me", "don\'t mess with me" }, |
| 364 { "<>&"'", "<>&\"'" }, |
| 365 { "& lt; & ; &; '", "& lt; & ; &; '" }, |
| 366 { "&", "&" }, |
| 367 { """, "\"" }, |
| 368 { "'", "'" }, |
| 369 { "<", "<" }, |
| 370 { ">", ">" }, |
| 371 { "& &", "& &" }, |
| 372 }; |
| 373 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 374 string16 result = UnescapeForHTML(ASCIIToUTF16(tests[i].input)); |
| 375 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_output), result); |
| 376 } |
| 377 } |
OLD | NEW |