OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 5 #include <algorithm> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "net/base/escape.h" | 8 #include "net/base/escape.h" |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/i18n/icu_string_conversions.h" | 11 #include "base/i18n/icu_string_conversions.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 static const size_t kNpos = string16::npos; | 19 static const size_t kNpos = string16::npos; |
20 | 20 |
21 struct EscapeCase { | 21 struct EscapeCase { |
22 const wchar_t* input; | 22 const char* input; |
23 const wchar_t* output; | 23 const char* output; |
24 }; | 24 }; |
25 | 25 |
26 struct UnescapeURLCase { | 26 struct UnescapeURLCase { |
27 const wchar_t* input; | 27 const wchar_t* input; |
28 UnescapeRule::Type rules; | 28 UnescapeRule::Type rules; |
29 const wchar_t* output; | 29 const wchar_t* output; |
30 }; | 30 }; |
31 | 31 |
32 struct UnescapeURLCaseASCII { | 32 struct UnescapeURLCaseASCII { |
33 const char* input; | 33 const char* input; |
(...skipping 22 matching lines...) Expand all Loading... |
56 | 56 |
57 struct EscapeForHTMLCase { | 57 struct EscapeForHTMLCase { |
58 const char* input; | 58 const char* input; |
59 const char* expected_output; | 59 const char* expected_output; |
60 }; | 60 }; |
61 | 61 |
62 } // namespace | 62 } // namespace |
63 | 63 |
64 TEST(EscapeTest, EscapeTextForFormSubmission) { | 64 TEST(EscapeTest, EscapeTextForFormSubmission) { |
65 const EscapeCase escape_cases[] = { | 65 const EscapeCase escape_cases[] = { |
66 {L"foo", L"foo"}, | 66 {"foo", "foo"}, |
67 {L"foo bar", L"foo+bar"}, | 67 {"foo bar", "foo+bar"}, |
68 {L"foo++", L"foo%2B%2B"} | 68 {"foo++", "foo%2B%2B"} |
69 }; | 69 }; |
70 for (size_t i = 0; i < arraysize(escape_cases); ++i) { | 70 for (size_t i = 0; i < arraysize(escape_cases); ++i) { |
71 EscapeCase value = escape_cases[i]; | 71 EscapeCase value = escape_cases[i]; |
72 EXPECT_EQ(WideToUTF16Hack(value.output), | 72 EXPECT_EQ(UTF8ToUTF16(value.output), |
73 EscapeQueryParamValueUTF8(WideToUTF16Hack(value.input), true)); | 73 EscapeQueryParamValueUTF8(UTF8ToUTF16(value.input), true)); |
74 } | 74 } |
75 | 75 |
76 const EscapeCase escape_cases_no_plus[] = { | 76 const EscapeCase escape_cases_no_plus[] = { |
77 {L"foo", L"foo"}, | 77 {"foo", "foo"}, |
78 {L"foo bar", L"foo%20bar"}, | 78 {"foo bar", "foo%20bar"}, |
79 {L"foo++", L"foo%2B%2B"} | 79 {"foo++", "foo%2B%2B"} |
80 }; | 80 }; |
81 for (size_t i = 0; i < arraysize(escape_cases_no_plus); ++i) { | 81 for (size_t i = 0; i < arraysize(escape_cases_no_plus); ++i) { |
82 EscapeCase value = escape_cases_no_plus[i]; | 82 EscapeCase value = escape_cases_no_plus[i]; |
83 EXPECT_EQ(WideToUTF16Hack(value.output), | 83 EXPECT_EQ(ASCIIToUTF16(value.output), |
84 EscapeQueryParamValueUTF8(WideToUTF16Hack(value.input), false)); | 84 EscapeQueryParamValueUTF8(ASCIIToUTF16(value.input), false)); |
85 } | 85 } |
86 | 86 |
87 // Test all the values in we're supposed to be escaping. | 87 // Test all the values in we're supposed to be escaping. |
88 const std::string no_escape( | 88 const std::string no_escape( |
89 "abcdefghijklmnopqrstuvwxyz" | 89 "abcdefghijklmnopqrstuvwxyz" |
90 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 90 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
91 "0123456789" | 91 "0123456789" |
92 "!'()*-._~"); | 92 "!'()*-._~"); |
93 for (int i = 0; i < 256; ++i) { | 93 for (int i = 0; i < 256; ++i) { |
94 std::string in; | 94 std::string in; |
(...skipping 14 matching lines...) Expand all Loading... |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 // Check to see if EscapeQueryParamValueUTF8 is the same as | 112 // Check to see if EscapeQueryParamValueUTF8 is the same as |
113 // EscapeQueryParamValue(..., kCodepageUTF8,) | 113 // EscapeQueryParamValue(..., kCodepageUTF8,) |
114 string16 test_str; | 114 string16 test_str; |
115 test_str.reserve(5000); | 115 test_str.reserve(5000); |
116 for (int i = 1; i < 5000; ++i) { | 116 for (int i = 1; i < 5000; ++i) { |
117 test_str.push_back(i); | 117 test_str.push_back(i); |
118 } | 118 } |
119 string16 wide; | 119 string16 utf16; |
120 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, true, | 120 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, true, |
121 &wide)); | 121 &utf16)); |
122 EXPECT_EQ(wide, EscapeQueryParamValueUTF8(test_str, true)); | 122 EXPECT_EQ(utf16, EscapeQueryParamValueUTF8(test_str, true)); |
123 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, false, | 123 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, false, |
124 &wide)); | 124 &utf16)); |
125 EXPECT_EQ(wide, EscapeQueryParamValueUTF8(test_str, false)); | 125 EXPECT_EQ(utf16, EscapeQueryParamValueUTF8(test_str, false)); |
126 } | 126 } |
127 | 127 |
128 TEST(EscapeTest, EscapePath) { | 128 TEST(EscapeTest, EscapePath) { |
129 ASSERT_EQ( | 129 ASSERT_EQ( |
130 // Most of the character space we care about, un-escaped | 130 // Most of the character space we care about, un-escaped |
131 EscapePath( | 131 EscapePath( |
132 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" | 132 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" |
133 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 133 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
134 "[\\]^_`abcdefghijklmnopqrstuvwxyz" | 134 "[\\]^_`abcdefghijklmnopqrstuvwxyz" |
135 "{|}~\x7f\x80\xff"), | 135 "{|}~\x7f\x80\xff"), |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, | 174 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, |
175 "Some random text %-OK"}, | 175 "Some random text %-OK"}, |
176 {"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, "\xA0\xB1\xC2\xD3\xE4\xF5"}, | 176 {"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, "\xA0\xB1\xC2\xD3\xE4\xF5"}, |
177 {"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, "\xAa\xBb\xCc\xDd\xEe\xFf"}, | 177 {"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, "\xAa\xBb\xCc\xDd\xEe\xFf"}, |
178 // Certain URL-sensitive characters should not be unescaped unless asked. | 178 // Certain URL-sensitive characters should not be unescaped unless asked. |
179 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES, | 179 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES, |
180 "Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, | 180 "Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, |
181 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", | 181 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
182 UnescapeRule::URL_SPECIAL_CHARS, | 182 UnescapeRule::URL_SPECIAL_CHARS, |
183 "Hello%20%13%10world ## ?? == && %% ++"}, | 183 "Hello%20%13%10world ## ?? == && %% ++"}, |
| 184 // We can neither escape nor unescape '@' since some websites expect it to |
| 185 // be preserved as either '@' or "%40". |
| 186 // See http://b/996720 and http://crbug.com/23933 . |
| 187 {"me@my%40example", UnescapeRule::NORMAL, "me@my%40example"}, |
184 // Control characters. | 188 // Control characters. |
185 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, | 189 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, |
186 "%01%02%03%04%05%06%07%08%09 %"}, | 190 "%01%02%03%04%05%06%07%08%09 %"}, |
187 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, | 191 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, |
188 "\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, | 192 "\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, |
189 {"Hello%20%13%10%02", UnescapeRule::SPACES, "Hello %13%10%02"}, | 193 {"Hello%20%13%10%02", UnescapeRule::SPACES, "Hello %13%10%02"}, |
190 {"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, "Hello%20\x13\x10\x02"}, | 194 {"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, "Hello%20\x13\x10\x02"}, |
191 }; | 195 }; |
192 | 196 |
193 for (size_t i = 0; i < arraysize(unescape_cases); i++) { | 197 for (size_t i = 0; i < arraysize(unescape_cases); i++) { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 UnescapeRule::NORMAL); | 337 UnescapeRule::NORMAL); |
334 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped); | 338 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped); |
335 | 339 |
336 unescaped = UnescapeURLComponent(unescape_cases[i].input, | 340 unescaped = UnescapeURLComponent(unescape_cases[i].input, |
337 UnescapeRule::REPLACE_PLUS_WITH_SPACE); | 341 UnescapeRule::REPLACE_PLUS_WITH_SPACE); |
338 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped); | 342 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped); |
339 | 343 |
340 // TODO: Need to test unescape_spaces and unescape_percent. | 344 // TODO: Need to test unescape_spaces and unescape_percent. |
341 string16 decoded = UnescapeAndDecodeUTF8URLComponent( | 345 string16 decoded = UnescapeAndDecodeUTF8URLComponent( |
342 unescape_cases[i].input, UnescapeRule::NORMAL, NULL); | 346 unescape_cases[i].input, UnescapeRule::NORMAL, NULL); |
343 EXPECT_EQ(WideToUTF16Hack(std::wstring(unescape_cases[i].decoded)), | 347 EXPECT_EQ(WideToUTF16(unescape_cases[i].decoded), decoded); |
344 decoded); | |
345 } | 348 } |
346 } | 349 } |
347 | 350 |
348 TEST(EscapeTest, AdjustOffset) { | 351 TEST(EscapeTest, AdjustOffset) { |
349 const AdjustOffsetCase adjust_cases[] = { | 352 const AdjustOffsetCase adjust_cases[] = { |
350 {"", 0, std::wstring::npos}, | 353 {"", 0, std::string::npos}, |
351 {"test", 0, 0}, | 354 {"test", 0, 0}, |
352 {"test", 2, 2}, | 355 {"test", 2, 2}, |
353 {"test", 4, std::wstring::npos}, | 356 {"test", 4, std::string::npos}, |
354 {"test", std::wstring::npos, std::wstring::npos}, | 357 {"test", std::string::npos, std::string::npos}, |
355 {"%2dtest", 6, 4}, | 358 {"%2dtest", 6, 4}, |
356 {"%2dtest", 2, std::wstring::npos}, | 359 {"%2dtest", 2, std::string::npos}, |
357 {"test%2d", 2, 2}, | 360 {"test%2d", 2, 2}, |
358 {"%E4%BD%A0+%E5%A5%BD", 9, 1}, | 361 {"%E4%BD%A0+%E5%A5%BD", 9, 1}, |
359 {"%E4%BD%A0+%E5%A5%BD", 6, std::wstring::npos}, | 362 {"%E4%BD%A0+%E5%A5%BD", 6, std::string::npos}, |
360 {"%ED%B0%80+%E5%A5%BD", 6, 6}, | 363 {"%ED%B0%80+%E5%A5%BD", 6, 6}, |
361 }; | 364 }; |
362 | 365 |
363 for (size_t i = 0; i < arraysize(adjust_cases); i++) { | 366 for (size_t i = 0; i < arraysize(adjust_cases); i++) { |
364 size_t offset = adjust_cases[i].input_offset; | 367 size_t offset = adjust_cases[i].input_offset; |
365 UnescapeAndDecodeUTF8URLComponent(adjust_cases[i].input, | 368 UnescapeAndDecodeUTF8URLComponent(adjust_cases[i].input, |
366 UnescapeRule::NORMAL, &offset); | 369 UnescapeRule::NORMAL, &offset); |
367 EXPECT_EQ(adjust_cases[i].output_offset, offset); | 370 EXPECT_EQ(adjust_cases[i].output_offset, offset); |
368 } | 371 } |
369 } | 372 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 adjustments.push_back(9); | 431 adjustments.push_back(9); |
429 adjustments.push_back(15); | 432 adjustments.push_back(15); |
430 std::for_each(offsets.begin(), offsets.end(), | 433 std::for_each(offsets.begin(), offsets.end(), |
431 AdjustEncodingOffset(adjustments)); | 434 AdjustEncodingOffset(adjustments)); |
432 size_t expected_2[] = {0, kNpos, kNpos, 1, 2, 3, 4, kNpos, kNpos, 5, kNpos, | 435 size_t expected_2[] = {0, kNpos, kNpos, 1, 2, 3, 4, kNpos, kNpos, 5, kNpos, |
433 kNpos, 6, 7, 8, 9, kNpos, kNpos}; | 436 kNpos, 6, 7, 8, 9, kNpos, kNpos}; |
434 EXPECT_EQ(offsets.size(), arraysize(expected_2)); | 437 EXPECT_EQ(offsets.size(), arraysize(expected_2)); |
435 for (size_t i = 0; i < arraysize(expected_2); ++i) | 438 for (size_t i = 0; i < arraysize(expected_2); ++i) |
436 EXPECT_EQ(expected_2[i], offsets[i]); | 439 EXPECT_EQ(expected_2[i], offsets[i]); |
437 } | 440 } |
OLD | NEW |