Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: net/base/escape_unittest.cc

Issue 372017: Fix various problems with inline autocomplete and URLs that change length dur... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/escape.cc ('k') | net/base/net_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 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 char* input; 22 const char* input;
23 UnescapeRule::Type rules; 23 UnescapeRule::Type rules;
24 const char* output; 24 const char* output;
25 }; 25 };
26 26
27 struct UnescapeAndDecodeURLCase { 27 struct UnescapeAndDecodeCase {
28 const char* encoding;
29 const char* input; 28 const char* input;
30 29
31 // The expected output when run through UnescapeURL. 30 // The expected output when run through UnescapeURL.
32 const char* url_unescaped; 31 const char* url_unescaped;
33 32
34 // The expected output when run through UnescapeQuery. 33 // The expected output when run through UnescapeQuery.
35 const char* query_unescaped; 34 const char* query_unescaped;
36 35
37 // The expected output when run through UnescapeAndDecodeURLComponent. 36 // The expected output when run through UnescapeAndDecodeURLComponent.
38 const wchar_t* decoded; 37 const wchar_t* decoded;
39 }; 38 };
40 39
40 struct AdjustOffsetCase {
41 const char* input;
42 size_t input_offset;
43 size_t output_offset;
44 };
45
41 struct EscapeForHTMLCase { 46 struct EscapeForHTMLCase {
42 const char* input; 47 const char* input;
43 const char* expected_output; 48 const char* expected_output;
44 }; 49 };
45 50
46 } // namespace 51 } // namespace
47 52
48 TEST(Escape, EscapeTextForFormSubmission) { 53 TEST(EscapeTest, EscapeTextForFormSubmission) {
49 const EscapeCase escape_cases[] = { 54 const EscapeCase escape_cases[] = {
50 {L"foo", L"foo"}, 55 {L"foo", L"foo"},
51 {L"foo bar", L"foo+bar"}, 56 {L"foo bar", L"foo+bar"},
52 {L"foo++", L"foo%2B%2B"} 57 {L"foo++", L"foo%2B%2B"}
53 }; 58 };
54 for (size_t i = 0; i < arraysize(escape_cases); ++i) { 59 for (size_t i = 0; i < arraysize(escape_cases); ++i) {
55 EscapeCase value = escape_cases[i]; 60 EscapeCase value = escape_cases[i];
56 EXPECT_EQ(value.output, EscapeQueryParamValueUTF8(value.input)); 61 EXPECT_EQ(value.output, EscapeQueryParamValueUTF8(value.input));
57 } 62 }
58 63
(...skipping 27 matching lines...) Expand all
86 std::wstring test_str; 91 std::wstring test_str;
87 test_str.reserve(5000); 92 test_str.reserve(5000);
88 for (int i = 1; i < 5000; ++i) { 93 for (int i = 1; i < 5000; ++i) {
89 test_str.push_back(i); 94 test_str.push_back(i);
90 } 95 }
91 std::wstring wide; 96 std::wstring wide;
92 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, &wide)); 97 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, &wide));
93 EXPECT_EQ(wide, EscapeQueryParamValueUTF8(test_str)); 98 EXPECT_EQ(wide, EscapeQueryParamValueUTF8(test_str));
94 } 99 }
95 100
96 TEST(Escape, EscapePath) { 101 TEST(EscapeTest, EscapePath) {
97 ASSERT_EQ( 102 ASSERT_EQ(
98 // Most of the character space we care about, un-escaped 103 // Most of the character space we care about, un-escaped
99 EscapePath( 104 EscapePath(
100 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" 105 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;"
101 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" 106 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
102 "[\\]^_`abcdefghijklmnopqrstuvwxyz" 107 "[\\]^_`abcdefghijklmnopqrstuvwxyz"
103 "{|}~\x7f\x80\xff"), 108 "{|}~\x7f\x80\xff"),
104 // Escaped 109 // Escaped
105 "%02%0A%1D%20!%22%23$%25&'()*+,-./0123456789%3A;" 110 "%02%0A%1D%20!%22%23$%25&'()*+,-./0123456789%3A;"
106 "%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ" 111 "%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
107 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" 112 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
108 "%7B%7C%7D~%7F%80%FF"); 113 "%7B%7C%7D~%7F%80%FF");
109 } 114 }
110 115
111 TEST(Escape, EscapeUrlEncodedData) { 116 TEST(EscapeTest, EscapeUrlEncodedData) {
112 ASSERT_EQ( 117 ASSERT_EQ(
113 // Most of the character space we care about, un-escaped 118 // Most of the character space we care about, un-escaped
114 EscapeUrlEncodedData( 119 EscapeUrlEncodedData(
115 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" 120 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;"
116 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" 121 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
117 "[\\]^_`abcdefghijklmnopqrstuvwxyz" 122 "[\\]^_`abcdefghijklmnopqrstuvwxyz"
118 "{|}~\x7f\x80\xff"), 123 "{|}~\x7f\x80\xff"),
119 // Escaped 124 // Escaped
120 "%02%0A%1D+!%22%23%24%25%26%27()*%2B,-./0123456789:%3B" 125 "%02%0A%1D+!%22%23%24%25%26%27()*%2B,-./0123456789:%3B"
121 "%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ" 126 "%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ"
122 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" 127 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
123 "%7B%7C%7D~%7F%80%FF"); 128 "%7B%7C%7D~%7F%80%FF");
124 } 129 }
125 130
126 TEST(Escape, UnescapeURLComponent) { 131 TEST(EscapeTest, UnescapeURLComponent) {
127 const UnescapeURLCase unescape_cases[] = { 132 const UnescapeURLCase unescape_cases[] = {
128 {"", UnescapeRule::NORMAL, ""}, 133 {"", UnescapeRule::NORMAL, ""},
129 {"%2", UnescapeRule::NORMAL, "%2"}, 134 {"%2", UnescapeRule::NORMAL, "%2"},
130 {"%%%%%%", UnescapeRule::NORMAL, "%%%%%%"}, 135 {"%%%%%%", UnescapeRule::NORMAL, "%%%%%%"},
131 {"Don't escape anything", UnescapeRule::NORMAL, "Don't escape anything"}, 136 {"Don't escape anything", UnescapeRule::NORMAL, "Don't escape anything"},
132 {"Invalid %escape %2", UnescapeRule::NORMAL, "Invalid %escape %2"}, 137 {"Invalid %escape %2", UnescapeRule::NORMAL, "Invalid %escape %2"},
133 {"Some%20random text %25%3bOK", UnescapeRule::NONE, 138 {"Some%20random text %25%3bOK", UnescapeRule::NONE,
134 "Some%20random text %25%3bOK"}, 139 "Some%20random text %25%3bOK"},
135 {"Some%20random text %25%3bOK", UnescapeRule::NORMAL, 140 {"Some%20random text %25%3bOK", UnescapeRule::NORMAL,
136 "Some%20random text %25;OK"}, 141 "Some%20random text %25;OK"},
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 expected.append("9Test"); 182 expected.append("9Test");
178 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::CONTROL_CHARS)); 183 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::CONTROL_CHARS));
179 184
180 // When we're not unescaping NULLs. 185 // When we're not unescaping NULLs.
181 expected = "Null"; 186 expected = "Null";
182 expected.push_back(0); 187 expected.push_back(0);
183 expected.append("%009Test"); 188 expected.append("%009Test");
184 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); 189 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL));
185 } 190 }
186 191
187 TEST(Escape, UnescapeAndDecodeURLComponent) { 192 TEST(EscapeTest, UnescapeAndDecodeUTF8URLComponent) {
188 const UnescapeAndDecodeURLCase unescape_cases[] = { 193 const UnescapeAndDecodeCase unescape_cases[] = {
189 {"UTF8", "%", "%", "%", L"%"}, 194 { "%",
190 {"UTF8", "+", "+", " ", L"+"}, 195 "%",
191 {"UTF8", "%2+", "%2+", "%2 ", L"%2+"}, 196 "%",
192 {"UTF8", "+%%%+%%%", "+%%%+%%%", " %%% %%%", L"+%%%+%%%"}, 197 L"%"},
193 {"UTF8", "Don't escape anything", 198 { "+",
194 "Don't escape anything", 199 "+",
195 "Don't escape anything", 200 " ",
196 L"Don't escape anything"}, 201 L"+"},
197 {"UTF8", "+Invalid %escape %2+", 202 { "%2+",
198 "+Invalid %escape %2+", 203 "%2+",
199 " Invalid %escape %2 ", 204 "%2 ",
200 L"+Invalid %escape %2+"}, 205 L"%2+"},
201 {"UTF8", "Some random text %25%3bOK", 206 { "+%%%+%%%",
202 "Some random text %25;OK", 207 "+%%%+%%%",
203 "Some random text %25;OK", 208 " %%% %%%",
204 L"Some random text %25;OK"}, 209 L"+%%%+%%%"},
205 {"UTF8", "%01%02%03%04%05%06%07%08%09", 210 { "Don't escape anything",
206 "%01%02%03%04%05%06%07%08%09", 211 "Don't escape anything",
207 "%01%02%03%04%05%06%07%08%09", 212 "Don't escape anything",
208 L"%01%02%03%04%05%06%07%08%09"}, 213 L"Don't escape anything"},
209 {"UTF8", "%E4%BD%A0+%E5%A5%BD", 214 { "+Invalid %escape %2+",
210 "\xE4\xBD\xA0+\xE5\xA5\xBD", 215 "+Invalid %escape %2+",
211 "\xE4\xBD\xA0 \xE5\xA5\xBD", 216 " Invalid %escape %2 ",
212 L"\x4f60+\x597d"}, 217 L"+Invalid %escape %2+"},
213 {"BIG5", "%A7A%A6n", 218 { "Some random text %25%3BOK",
214 "\xA7\x41\xA6n", 219 "Some random text %25;OK",
215 "\xA7\x41\xA6n", 220 "Some random text %25;OK",
216 L"\x4f60\x597d"}, 221 L"Some random text %25;OK"},
217 {"UTF8", "%ED%ED", // Invalid UTF-8. 222 { "%01%02%03%04%05%06%07%08%09",
218 "\xED\xED", 223 "%01%02%03%04%05%06%07%08%09",
219 "\xED\xED", 224 "%01%02%03%04%05%06%07%08%09",
220 L"%ED%ED"}, // Invalid UTF-8 -> kept unescaped. 225 L"%01%02%03%04%05%06%07%08%09"},
226 { "%E4%BD%A0+%E5%A5%BD",
227 "\xE4\xBD\xA0+\xE5\xA5\xBD",
228 "\xE4\xBD\xA0 \xE5\xA5\xBD",
229 L"\x4f60+\x597d"},
230 { "%ED%ED", // Invalid UTF-8.
231 "\xED\xED",
232 "\xED\xED",
233 L"%ED%ED"}, // Invalid UTF-8 -> kept unescaped.
221 }; 234 };
222 235
223 for (size_t i = 0; i < arraysize(unescape_cases); i++) { 236 for (size_t i = 0; i < arraysize(unescape_cases); i++) {
224 std::string unescaped = UnescapeURLComponent(unescape_cases[i].input, 237 std::string unescaped = UnescapeURLComponent(unescape_cases[i].input,
225 UnescapeRule::NORMAL); 238 UnescapeRule::NORMAL);
226 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped); 239 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped);
227 240
228 unescaped = UnescapeURLComponent(unescape_cases[i].input, 241 unescaped = UnescapeURLComponent(unescape_cases[i].input,
229 UnescapeRule::REPLACE_PLUS_WITH_SPACE); 242 UnescapeRule::REPLACE_PLUS_WITH_SPACE);
230 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped); 243 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped);
231 244
232 // TODO: Need to test unescape_spaces and unescape_percent. 245 // TODO: Need to test unescape_spaces and unescape_percent.
233 std::wstring decoded = UnescapeAndDecodeURLComponent( 246 std::wstring decoded = UnescapeAndDecodeUTF8URLComponent(
234 unescape_cases[i].input, unescape_cases[i].encoding, 247 unescape_cases[i].input, UnescapeRule::NORMAL, NULL);
235 UnescapeRule::NORMAL);
236 EXPECT_EQ(std::wstring(unescape_cases[i].decoded), decoded); 248 EXPECT_EQ(std::wstring(unescape_cases[i].decoded), decoded);
237 } 249 }
238 } 250 }
239 251
240 TEST(Escape, EscapeForHTML) { 252 TEST(EscapeTest, AdjustOffset) {
253 const AdjustOffsetCase adjust_cases[] = {
254 {"", 0, std::wstring::npos},
255 {"test", 0, 0},
256 {"test", 2, 2},
257 {"test", 4, std::wstring::npos},
258 {"test", std::wstring::npos, std::wstring::npos},
259 {"%3Btest", 6, 4},
260 {"%3Btest", 2, std::wstring::npos},
261 {"test%3B", 2, 2},
262 {"%E4%BD%A0+%E5%A5%BD", 9, 1},
263 {"%E4%BD%A0+%E5%A5%BD", 6, std::wstring::npos},
264 {"%ED%B0%80+%E5%A5%BD", 6, 6},
265 };
266
267 for (size_t i = 0; i < arraysize(adjust_cases); i++) {
268 size_t offset = adjust_cases[i].input_offset;
269 UnescapeAndDecodeUTF8URLComponent(adjust_cases[i].input,
270 UnescapeRule::NORMAL, &offset);
271 EXPECT_EQ(adjust_cases[i].output_offset, offset);
272 }
273 }
274
275 TEST(EscapeTest, EscapeForHTML) {
241 const EscapeForHTMLCase tests[] = { 276 const EscapeForHTMLCase tests[] = {
242 { "hello", "hello" }, 277 { "hello", "hello" },
243 { "<hello>", "&lt;hello&gt;" }, 278 { "<hello>", "&lt;hello&gt;" },
244 { "don\'t mess with me", "don&#39;t mess with me" }, 279 { "don\'t mess with me", "don&#39;t mess with me" },
245 }; 280 };
246 for (size_t i = 0; i < arraysize(tests); ++i) { 281 for (size_t i = 0; i < arraysize(tests); ++i) {
247 std::string result = EscapeForHTML(std::string(tests[i].input)); 282 std::string result = EscapeForHTML(std::string(tests[i].input));
248 EXPECT_EQ(std::string(tests[i].expected_output), result); 283 EXPECT_EQ(std::string(tests[i].expected_output), result);
249 } 284 }
250 } 285 }
OLDNEW
« no previous file with comments | « net/base/escape.cc ('k') | net/base/net_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698