Update V8 to version 4.7.42.
[chromium-blink-merge.git] / net / base / escape_unittest.cc
blobb6f022918ae6e639ad5ac93fd088efd859cf6443
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <algorithm>
6 #include <string>
8 #include "net/base/escape.h"
10 #include "base/basictypes.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace net {
17 namespace {
19 struct EscapeCase {
20 const char* input;
21 const char* output;
24 struct UnescapeURLCase {
25 const wchar_t* input;
26 UnescapeRule::Type rules;
27 const wchar_t* output;
30 struct UnescapeURLCaseASCII {
31 const char* input;
32 UnescapeRule::Type rules;
33 const char* output;
36 struct UnescapeAndDecodeCase {
37 const char* input;
39 // The expected output when run through UnescapeURL.
40 const char* url_unescaped;
42 // The expected output when run through UnescapeQuery.
43 const char* query_unescaped;
45 // The expected output when run through UnescapeAndDecodeURLComponent.
46 const wchar_t* decoded;
49 struct AdjustOffsetCase {
50 const char* input;
51 size_t input_offset;
52 size_t output_offset;
55 struct EscapeForHTMLCase {
56 const char* input;
57 const char* expected_output;
60 TEST(EscapeTest, EscapeTextForFormSubmission) {
61 const EscapeCase escape_cases[] = {
62 {"foo", "foo"},
63 {"foo bar", "foo+bar"},
64 {"foo++", "foo%2B%2B"}
66 for (size_t i = 0; i < arraysize(escape_cases); ++i) {
67 EscapeCase value = escape_cases[i];
68 EXPECT_EQ(value.output, EscapeQueryParamValue(value.input, true));
71 const EscapeCase escape_cases_no_plus[] = {
72 {"foo", "foo"},
73 {"foo bar", "foo%20bar"},
74 {"foo++", "foo%2B%2B"}
76 for (size_t i = 0; i < arraysize(escape_cases_no_plus); ++i) {
77 EscapeCase value = escape_cases_no_plus[i];
78 EXPECT_EQ(value.output, EscapeQueryParamValue(value.input, false));
81 // Test all the values in we're supposed to be escaping.
82 const std::string no_escape(
83 "abcdefghijklmnopqrstuvwxyz"
84 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
85 "0123456789"
86 "!'()*-._~");
87 for (int i = 0; i < 256; ++i) {
88 std::string in;
89 in.push_back(i);
90 std::string out = EscapeQueryParamValue(in, true);
91 if (0 == i) {
92 EXPECT_EQ(out, std::string("%00"));
93 } else if (32 == i) {
94 // Spaces are plus escaped like web forms.
95 EXPECT_EQ(out, std::string("+"));
96 } else if (no_escape.find(in) == std::string::npos) {
97 // Check %hex escaping
98 std::string expected = base::StringPrintf("%%%02X", i);
99 EXPECT_EQ(expected, out);
100 } else {
101 // No change for things in the no_escape list.
102 EXPECT_EQ(out, in);
107 TEST(EscapeTest, EscapePath) {
108 ASSERT_EQ(
109 // Most of the character space we care about, un-escaped
110 EscapePath(
111 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;"
112 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
113 "[\\]^_`abcdefghijklmnopqrstuvwxyz"
114 "{|}~\x7f\x80\xff"),
115 // Escaped
116 "%02%0A%1D%20!%22%23$%25&'()*+,-./0123456789%3A;"
117 "%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
118 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
119 "%7B%7C%7D~%7F%80%FF");
122 TEST(EscapeTest, DataURLWithAccentedCharacters) {
123 const std::string url =
124 "text/html;charset=utf-8,%3Chtml%3E%3Cbody%3ETonton,%20ton%20th%C3"
125 "%A9%20t'a-t-il%20%C3%B4t%C3%A9%20ta%20toux%20";
127 base::OffsetAdjuster::Adjustments adjustments;
128 UnescapeAndDecodeUTF8URLComponentWithAdjustments(url, UnescapeRule::SPACES,
129 &adjustments);
132 TEST(EscapeTest, EscapeUrlEncodedData) {
133 ASSERT_EQ(
134 // Most of the character space we care about, un-escaped
135 EscapeUrlEncodedData(
136 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;"
137 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
138 "[\\]^_`abcdefghijklmnopqrstuvwxyz"
139 "{|}~\x7f\x80\xff", true),
140 // Escaped
141 "%02%0A%1D+!%22%23%24%25%26%27()*%2B,-./0123456789:%3B"
142 "%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ"
143 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
144 "%7B%7C%7D~%7F%80%FF");
147 TEST(EscapeTest, EscapeUrlEncodedDataSpace) {
148 ASSERT_EQ(EscapeUrlEncodedData("a b", true), "a+b");
149 ASSERT_EQ(EscapeUrlEncodedData("a b", false), "a%20b");
152 TEST(EscapeTest, UnescapeURLComponentASCII) {
153 const UnescapeURLCaseASCII unescape_cases[] = {
154 {"", UnescapeRule::NORMAL, ""},
155 {"%2", UnescapeRule::NORMAL, "%2"},
156 {"%%%%%%", UnescapeRule::NORMAL, "%%%%%%"},
157 {"Don't escape anything", UnescapeRule::NORMAL, "Don't escape anything"},
158 {"Invalid %escape %2", UnescapeRule::NORMAL, "Invalid %escape %2"},
159 {"Some%20random text %25%2dOK", UnescapeRule::NONE,
160 "Some%20random text %25%2dOK"},
161 {"Some%20random text %25%2dOK", UnescapeRule::NORMAL,
162 "Some%20random text %25-OK"},
163 {"Some%20random text %25%2dOK", UnescapeRule::SPACES,
164 "Some random text %25-OK"},
165 {"Some%20random text %25%2dOK", UnescapeRule::URL_SPECIAL_CHARS,
166 "Some%20random text %-OK"},
167 {"Some%20random text %25%2dOK",
168 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS,
169 "Some random text %-OK"},
170 {"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, "\xA0\xB1\xC2\xD3\xE4\xF5"},
171 {"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, "\xAa\xBb\xCc\xDd\xEe\xFf"},
172 // Certain URL-sensitive characters should not be unescaped unless asked.
173 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES,
174 "Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"},
175 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+",
176 UnescapeRule::URL_SPECIAL_CHARS,
177 "Hello%20%13%10world ## ?? == && %% ++"},
178 // We can neither escape nor unescape '@' since some websites expect it to
179 // be preserved as either '@' or "%40".
180 // See http://b/996720 and http://crbug.com/23933 .
181 {"me@my%40example", UnescapeRule::NORMAL, "me@my%40example"},
182 // Control characters.
183 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS,
184 "%01%02%03%04%05%06%07%08%09 %"},
185 {"%01%02%03%04%05%06%07%08%09 %25",
186 UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
187 "\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"},
188 {"Hello%20%13%10%02", UnescapeRule::SPACES, "Hello %13%10%02"},
189 {"Hello%20%13%10%02", UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
190 "Hello%20\x13\x10\x02"},
193 for (size_t i = 0; i < arraysize(unescape_cases); i++) {
194 std::string str(unescape_cases[i].input);
195 EXPECT_EQ(std::string(unescape_cases[i].output),
196 UnescapeURLComponent(str, unescape_cases[i].rules));
199 // Test the NULL character unescaping (which wouldn't work above since those
200 // are just char pointers).
201 std::string input("Null");
202 input.push_back(0); // Also have a NULL in the input.
203 input.append("%00%39Test");
205 // When we're unescaping NULLs
206 std::string expected("Null");
207 expected.push_back(0);
208 expected.push_back(0);
209 expected.append("9Test");
210 EXPECT_EQ(expected, UnescapeURLComponent(
211 input, UnescapeRule::SPOOFING_AND_CONTROL_CHARS));
213 // When we're not unescaping NULLs.
214 expected = "Null";
215 expected.push_back(0);
216 expected.append("%009Test");
217 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL));
220 TEST(EscapeTest, UnescapeURLComponent) {
221 const UnescapeURLCase unescape_cases[] = {
222 {L"", UnescapeRule::NORMAL, L""},
223 {L"%2", UnescapeRule::NORMAL, L"%2"},
224 {L"%%%%%%", UnescapeRule::NORMAL, L"%%%%%%"},
225 {L"Don't escape anything", UnescapeRule::NORMAL, L"Don't escape anything"},
226 {L"Invalid %escape %2", UnescapeRule::NORMAL, L"Invalid %escape %2"},
227 {L"Some%20random text %25%2dOK", UnescapeRule::NONE,
228 L"Some%20random text %25%2dOK"},
229 {L"Some%20random text %25%2dOK", UnescapeRule::NORMAL,
230 L"Some%20random text %25-OK"},
231 {L"Some%20random text %25%E2%80", UnescapeRule::NORMAL,
232 L"Some%20random text %25\xE2\x80"},
233 {L"Some%20random text %25%E2%80OK", UnescapeRule::NORMAL,
234 L"Some%20random text %25\xE2\x80OK"},
235 {L"Some%20random text %25%E2%80%84OK", UnescapeRule::NORMAL,
236 L"Some%20random text %25\xE2\x80\x84OK"},
238 // BiDi Control characters should not be unescaped unless explicity told to
239 // do so with UnescapeRule::SPOOFING_AND_CONTROL_CHARS
240 {L"Some%20random text %25%D8%9COK", UnescapeRule::NORMAL,
241 L"Some%20random text %25%D8%9COK"},
242 {L"Some%20random text %25%E2%80%8EOK", UnescapeRule::NORMAL,
243 L"Some%20random text %25%E2%80%8EOK"},
244 {L"Some%20random text %25%E2%80%8FOK", UnescapeRule::NORMAL,
245 L"Some%20random text %25%E2%80%8FOK"},
246 {L"Some%20random text %25%E2%80%AAOK", UnescapeRule::NORMAL,
247 L"Some%20random text %25%E2%80%AAOK"},
248 {L"Some%20random text %25%E2%80%ABOK", UnescapeRule::NORMAL,
249 L"Some%20random text %25%E2%80%ABOK"},
250 {L"Some%20random text %25%E2%80%AEOK", UnescapeRule::NORMAL,
251 L"Some%20random text %25%E2%80%AEOK"},
252 {L"Some%20random text %25%E2%81%A6OK", UnescapeRule::NORMAL,
253 L"Some%20random text %25%E2%81%A6OK"},
254 {L"Some%20random text %25%E2%81%A9OK", UnescapeRule::NORMAL,
255 L"Some%20random text %25%E2%81%A9OK"},
256 // UnescapeRule::SPOOFING_AND_CONTROL_CHARS should unescape BiDi Control
257 // characters.
258 {L"Some%20random text %25%D8%9COK",
259 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
260 L"Some%20random text %25\xD8\x9COK"},
261 {L"Some%20random text %25%E2%80%8EOK",
262 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
263 L"Some%20random text %25\xE2\x80\x8EOK"},
264 {L"Some%20random text %25%E2%80%8FOK",
265 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
266 L"Some%20random text %25\xE2\x80\x8FOK"},
267 {L"Some%20random text %25%E2%80%AAOK",
268 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
269 L"Some%20random text %25\xE2\x80\xAAOK"},
270 {L"Some%20random text %25%E2%80%ABOK",
271 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
272 L"Some%20random text %25\xE2\x80\xABOK"},
273 {L"Some%20random text %25%E2%80%AEOK",
274 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
275 L"Some%20random text %25\xE2\x80\xAEOK"},
276 {L"Some%20random text %25%E2%81%A6OK",
277 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
278 L"Some%20random text %25\xE2\x81\xA6OK"},
279 {L"Some%20random text %25%E2%81%A9OK",
280 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
281 L"Some%20random text %25\xE2\x81\xA9OK"},
283 // Certain banned characters should not be unescaped unless explicitly told
284 // to do so with UnescapeRule::SPOOFING_AND_CONTROL_CHARS.
285 // U+1F50F LOCK WITH INK PEN
286 {L"Some%20random text %25%F0%9F%94%8FOK", UnescapeRule::NORMAL,
287 L"Some%20random text %25%F0%9F%94%8FOK"},
288 // U+1F510 CLOSED LOCK WITH KEY
289 {L"Some%20random text %25%F0%9F%94%90OK", UnescapeRule::NORMAL,
290 L"Some%20random text %25%F0%9F%94%90OK"},
291 // U+1F512 LOCK
292 {L"Some%20random text %25%F0%9F%94%92OK", UnescapeRule::NORMAL,
293 L"Some%20random text %25%F0%9F%94%92OK"},
294 // U+1F513 OPEN LOCK
295 {L"Some%20random text %25%F0%9F%94%93OK", UnescapeRule::NORMAL,
296 L"Some%20random text %25%F0%9F%94%93OK"},
297 // UnescapeRule::SPOOFING_AND_CONTROL_CHARS should unescape banned
298 // characters.
299 {L"Some%20random text %25%F0%9F%94%8FOK",
300 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
301 L"Some%20random text %25\xF0\x9F\x94\x8FOK"},
302 {L"Some%20random text %25%F0%9F%94%90OK",
303 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
304 L"Some%20random text %25\xF0\x9F\x94\x90OK"},
305 {L"Some%20random text %25%F0%9F%94%92OK",
306 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
307 L"Some%20random text %25\xF0\x9F\x94\x92OK"},
308 {L"Some%20random text %25%F0%9F%94%93OK",
309 UnescapeRule::NORMAL | UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
310 L"Some%20random text %25\xF0\x9F\x94\x93OK"},
312 {L"Some%20random text %25%2dOK", UnescapeRule::SPACES,
313 L"Some random text %25-OK"},
314 {L"Some%20random text %25%2dOK", UnescapeRule::URL_SPECIAL_CHARS,
315 L"Some%20random text %-OK"},
316 {L"Some%20random text %25%2dOK",
317 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS,
318 L"Some random text %-OK"},
319 {L"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, L"\xA0\xB1\xC2\xD3\xE4\xF5"},
320 {L"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, L"\xAa\xBb\xCc\xDd\xEe\xFf"},
321 // Certain URL-sensitive characters should not be unescaped unless asked.
322 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES,
323 L"Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"},
324 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+",
325 UnescapeRule::URL_SPECIAL_CHARS,
326 L"Hello%20%13%10world ## ?? == && %% ++"},
327 // We can neither escape nor unescape '@' since some websites expect it to
328 // be preserved as either '@' or "%40".
329 // See http://b/996720 and http://crbug.com/23933 .
330 {L"me@my%40example", UnescapeRule::NORMAL, L"me@my%40example"},
331 // Control characters.
332 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS,
333 L"%01%02%03%04%05%06%07%08%09 %"},
334 {L"%01%02%03%04%05%06%07%08%09 %25",
335 UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
336 L"\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"},
337 {L"Hello%20%13%10%02", UnescapeRule::SPACES, L"Hello %13%10%02"},
338 {L"Hello%20%13%10%02", UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
339 L"Hello%20\x13\x10\x02"},
340 {L"Hello\x9824\x9827", UnescapeRule::SPOOFING_AND_CONTROL_CHARS,
341 L"Hello\x9824\x9827"},
344 for (size_t i = 0; i < arraysize(unescape_cases); i++) {
345 base::string16 str(base::WideToUTF16(unescape_cases[i].input));
346 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].output),
347 UnescapeURLComponent(str, unescape_cases[i].rules));
350 // Test the NULL character unescaping (which wouldn't work above since those
351 // are just char pointers).
352 base::string16 input(base::WideToUTF16(L"Null"));
353 input.push_back(0); // Also have a NULL in the input.
354 input.append(base::WideToUTF16(L"%00%39Test"));
356 // When we're unescaping NULLs
357 base::string16 expected(base::WideToUTF16(L"Null"));
358 expected.push_back(0);
359 expected.push_back(0);
360 expected.append(base::ASCIIToUTF16("9Test"));
361 EXPECT_EQ(expected, UnescapeURLComponent(
362 input, UnescapeRule::SPOOFING_AND_CONTROL_CHARS));
364 // When we're not unescaping NULLs.
365 expected = base::WideToUTF16(L"Null");
366 expected.push_back(0);
367 expected.append(base::WideToUTF16(L"%009Test"));
368 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL));
371 TEST(EscapeTest, UnescapeAndDecodeUTF8URLComponent) {
372 const UnescapeAndDecodeCase unescape_cases[] = {
373 { "%",
374 "%",
375 "%",
376 L"%"},
377 { "+",
378 "+",
379 " ",
380 L"+"},
381 { "%2+",
382 "%2+",
383 "%2 ",
384 L"%2+"},
385 { "+%%%+%%%",
386 "+%%%+%%%",
387 " %%% %%%",
388 L"+%%%+%%%"},
389 { "Don't escape anything",
390 "Don't escape anything",
391 "Don't escape anything",
392 L"Don't escape anything"},
393 { "+Invalid %escape %2+",
394 "+Invalid %escape %2+",
395 " Invalid %escape %2 ",
396 L"+Invalid %escape %2+"},
397 { "Some random text %25%2dOK",
398 "Some random text %25-OK",
399 "Some random text %25-OK",
400 L"Some random text %25-OK"},
401 { "%01%02%03%04%05%06%07%08%09",
402 "%01%02%03%04%05%06%07%08%09",
403 "%01%02%03%04%05%06%07%08%09",
404 L"%01%02%03%04%05%06%07%08%09"},
405 { "%E4%BD%A0+%E5%A5%BD",
406 "\xE4\xBD\xA0+\xE5\xA5\xBD",
407 "\xE4\xBD\xA0 \xE5\xA5\xBD",
408 L"\x4f60+\x597d"},
409 { "%ED%ED", // Invalid UTF-8.
410 "\xED\xED",
411 "\xED\xED",
412 L"%ED%ED"}, // Invalid UTF-8 -> kept unescaped.
415 for (size_t i = 0; i < arraysize(unescape_cases); i++) {
416 std::string unescaped = UnescapeURLComponent(unescape_cases[i].input,
417 UnescapeRule::NORMAL);
418 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped);
420 unescaped = UnescapeURLComponent(unescape_cases[i].input,
421 UnescapeRule::REPLACE_PLUS_WITH_SPACE);
422 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped);
424 // TODO: Need to test unescape_spaces and unescape_percent.
425 base::string16 decoded = UnescapeAndDecodeUTF8URLComponent(
426 unescape_cases[i].input, UnescapeRule::NORMAL);
427 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].decoded), decoded);
431 TEST(EscapeTest, AdjustOffset) {
432 const AdjustOffsetCase adjust_cases[] = {
433 {"", 0, 0},
434 {"test", 0, 0},
435 {"test", 2, 2},
436 {"test", 4, 4},
437 {"test", std::string::npos, std::string::npos},
438 {"%2dtest", 6, 4},
439 {"%2dtest", 3, 1},
440 {"%2dtest", 2, std::string::npos},
441 {"%2dtest", 1, std::string::npos},
442 {"%2dtest", 0, 0},
443 {"test%2d", 2, 2},
444 {"%E4%BD%A0+%E5%A5%BD", 9, 1},
445 {"%E4%BD%A0+%E5%A5%BD", 6, std::string::npos},
446 {"%E4%BD%A0+%E5%A5%BD", 0, 0},
447 {"%E4%BD%A0+%E5%A5%BD", 10, 2},
448 {"%E4%BD%A0+%E5%A5%BD", 19, 3},
450 {"hi%41test%E4%BD%A0+%E5%A5%BD", 18, 8},
451 {"hi%41test%E4%BD%A0+%E5%A5%BD", 15, std::string::npos},
452 {"hi%41test%E4%BD%A0+%E5%A5%BD", 9, 7},
453 {"hi%41test%E4%BD%A0+%E5%A5%BD", 19, 9},
454 {"hi%41test%E4%BD%A0+%E5%A5%BD", 28, 10},
455 {"hi%41test%E4%BD%A0+%E5%A5%BD", 0, 0},
456 {"hi%41test%E4%BD%A0+%E5%A5%BD", 2, 2},
457 {"hi%41test%E4%BD%A0+%E5%A5%BD", 3, std::string::npos},
458 {"hi%41test%E4%BD%A0+%E5%A5%BD", 5, 3},
460 {"%E4%BD%A0+%E5%A5%BDhi%41test", 9, 1},
461 {"%E4%BD%A0+%E5%A5%BDhi%41test", 6, std::string::npos},
462 {"%E4%BD%A0+%E5%A5%BDhi%41test", 0, 0},
463 {"%E4%BD%A0+%E5%A5%BDhi%41test", 10, 2},
464 {"%E4%BD%A0+%E5%A5%BDhi%41test", 19, 3},
465 {"%E4%BD%A0+%E5%A5%BDhi%41test", 21, 5},
466 {"%E4%BD%A0+%E5%A5%BDhi%41test", 22, std::string::npos},
467 {"%E4%BD%A0+%E5%A5%BDhi%41test", 24, 6},
468 {"%E4%BD%A0+%E5%A5%BDhi%41test", 28, 10},
470 {"%ED%B0%80+%E5%A5%BD", 6, 6}, // not convertable to UTF-8
473 for (size_t i = 0; i < arraysize(adjust_cases); i++) {
474 size_t offset = adjust_cases[i].input_offset;
475 base::OffsetAdjuster::Adjustments adjustments;
476 UnescapeAndDecodeUTF8URLComponentWithAdjustments(
477 adjust_cases[i].input, UnescapeRule::NORMAL, &adjustments);
478 base::OffsetAdjuster::AdjustOffset(adjustments, &offset);
479 EXPECT_EQ(adjust_cases[i].output_offset, offset)
480 << "input=" << adjust_cases[i].input
481 << " offset=" << adjust_cases[i].input_offset;
485 TEST(EscapeTest, EscapeForHTML) {
486 const EscapeForHTMLCase tests[] = {
487 { "hello", "hello" },
488 { "<hello>", "&lt;hello&gt;" },
489 { "don\'t mess with me", "don&#39;t mess with me" },
491 for (size_t i = 0; i < arraysize(tests); ++i) {
492 std::string result = EscapeForHTML(std::string(tests[i].input));
493 EXPECT_EQ(std::string(tests[i].expected_output), result);
497 TEST(EscapeTest, UnescapeForHTML) {
498 const EscapeForHTMLCase tests[] = {
499 { "", "" },
500 { "&lt;hello&gt;", "<hello>" },
501 { "don&#39;t mess with me", "don\'t mess with me" },
502 { "&lt;&gt;&amp;&quot;&#39;", "<>&\"'" },
503 { "& lt; &amp ; &; '", "& lt; &amp ; &; '" },
504 { "&amp;", "&" },
505 { "&quot;", "\"" },
506 { "&#39;", "'" },
507 { "&lt;", "<" },
508 { "&gt;", ">" },
509 { "&amp; &", "& &" },
511 for (size_t i = 0; i < arraysize(tests); ++i) {
512 base::string16 result = UnescapeForHTML(base::ASCIIToUTF16(tests[i].input));
513 EXPECT_EQ(base::ASCIIToUTF16(tests[i].expected_output), result);
517 TEST(EscapeTest, EscapeExternalHandlerValue) {
518 ASSERT_EQ(
519 // Escaped
520 "%02%0A%1D%20!%22#$%25&'()*+,-./0123456789:;"
521 "%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
522 "[%5C]%5E_%60abcdefghijklmnopqrstuvwxyz"
523 "%7B%7C%7D~%7F%80%FF",
524 // Most of the character space we care about, un-escaped
525 EscapeExternalHandlerValue(
526 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;"
527 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
528 "[\\]^_`abcdefghijklmnopqrstuvwxyz"
529 "{|}~\x7f\x80\xff"));
531 ASSERT_EQ(
532 "!#$&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_"
533 "abcdefghijklmnopqrstuvwxyz~",
534 EscapeExternalHandlerValue(
535 "!#$&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_"
536 "abcdefghijklmnopqrstuvwxyz~"));
538 ASSERT_EQ("%258k", EscapeExternalHandlerValue("%8k"));
539 ASSERT_EQ("a%25", EscapeExternalHandlerValue("a%"));
540 ASSERT_EQ("%25a", EscapeExternalHandlerValue("%a"));
541 ASSERT_EQ("a%258", EscapeExternalHandlerValue("a%8"));
542 ASSERT_EQ("%ab", EscapeExternalHandlerValue("%ab"));
543 ASSERT_EQ("%AB", EscapeExternalHandlerValue("%AB"));
545 ASSERT_EQ("http://example.com/path/sub?q=a%7Cb%7Cc&q=1%7C2%7C3#ref%7C",
546 EscapeExternalHandlerValue(
547 "http://example.com/path/sub?q=a|b|c&q=1|2|3#ref|"));
548 ASSERT_EQ("http://example.com/path/sub?q=a%7Cb%7Cc&q=1%7C2%7C3#ref%7C",
549 EscapeExternalHandlerValue(
550 "http://example.com/path/sub?q=a%7Cb%7Cc&q=1%7C2%7C3#ref%7C"));
551 ASSERT_EQ("http://[2001:db8:0:1]:80",
552 EscapeExternalHandlerValue("http://[2001:db8:0:1]:80"));
555 } // namespace
556 } // namespace net