Correctly handle problematic nested escapes in URL paths.
[chromium-blink-merge.git] / url / url_canon_unittest.cc
blobb382a7cda2aac175c77b4fef1e1a6e5ce52eca1e
1 // Copyright 2013 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 <errno.h>
7 #include "base/macros.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/third_party/mozilla/url_parse.h"
10 #include "url/url_canon.h"
11 #include "url/url_canon_internal.h"
12 #include "url/url_canon_stdstring.h"
13 #include "url/url_test_utils.h"
15 namespace url {
17 using test_utils::WStringToUTF16;
18 using test_utils::ConvertUTF8ToUTF16;
19 using test_utils::ConvertUTF16ToUTF8;
21 namespace {
23 struct ComponentCase {
24 const char* input;
25 const char* expected;
26 Component expected_component;
27 bool expected_success;
30 // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
31 // treat each input as optional, and will only try processing if non-NULL.
32 // The output is always 8-bit.
33 struct DualComponentCase {
34 const char* input8;
35 const wchar_t* input16;
36 const char* expected;
37 Component expected_component;
38 bool expected_success;
41 // Test cases for CanonicalizeIPAddress(). The inputs are identical to
42 // DualComponentCase, but the output has extra CanonHostInfo fields.
43 struct IPAddressCase {
44 const char* input8;
45 const wchar_t* input16;
46 const char* expected;
47 Component expected_component;
49 // CanonHostInfo fields, for verbose output.
50 CanonHostInfo::Family expected_family;
51 int expected_num_ipv4_components;
52 const char* expected_address_hex; // Two hex chars per IP address byte.
55 std::string BytesToHexString(unsigned char bytes[16], int length) {
56 EXPECT_TRUE(length == 0 || length == 4 || length == 16)
57 << "Bad IP address length: " << length;
58 std::string result;
59 for (int i = 0; i < length; ++i) {
60 result.push_back(kHexCharLookup[(bytes[i] >> 4) & 0xf]);
61 result.push_back(kHexCharLookup[bytes[i] & 0xf]);
63 return result;
66 struct ReplaceCase {
67 const char* base;
68 const char* scheme;
69 const char* username;
70 const char* password;
71 const char* host;
72 const char* port;
73 const char* path;
74 const char* query;
75 const char* ref;
76 const char* expected;
79 // Magic string used in the replacements code that tells SetupReplComp to
80 // call the clear function.
81 const char kDeleteComp[] = "|";
83 // Sets up a replacement for a single component. This is given pointers to
84 // the set and clear function for the component being replaced, and will
85 // either set the component (if it exists) or clear it (if the replacement
86 // string matches kDeleteComp).
88 // This template is currently used only for the 8-bit case, and the strlen
89 // causes it to fail in other cases. It is left a template in case we have
90 // tests for wide replacements.
91 template<typename CHAR>
92 void SetupReplComp(
93 void (Replacements<CHAR>::*set)(const CHAR*, const Component&),
94 void (Replacements<CHAR>::*clear)(),
95 Replacements<CHAR>* rep,
96 const CHAR* str) {
97 if (str && str[0] == kDeleteComp[0]) {
98 (rep->*clear)();
99 } else if (str) {
100 (rep->*set)(str, Component(0, static_cast<int>(strlen(str))));
104 } // namespace
106 TEST(URLCanonTest, DoAppendUTF8) {
107 struct UTF8Case {
108 unsigned input;
109 const char* output;
110 } utf_cases[] = {
111 // Valid code points.
112 {0x24, "\x24"},
113 {0xA2, "\xC2\xA2"},
114 {0x20AC, "\xE2\x82\xAC"},
115 {0x24B62, "\xF0\xA4\xAD\xA2"},
116 {0x10FFFF, "\xF4\x8F\xBF\xBF"},
118 std::string out_str;
119 for (size_t i = 0; i < arraysize(utf_cases); i++) {
120 out_str.clear();
121 StdStringCanonOutput output(&out_str);
122 AppendUTF8Value(utf_cases[i].input, &output);
123 output.Complete();
124 EXPECT_EQ(utf_cases[i].output, out_str);
128 #if defined(GTEST_HAS_DEATH_TEST)
129 // TODO(mattm): Can't run this in debug mode for now, since the DCHECK will
130 // cause the Chromium stack trace dialog to appear and hang the test.
131 // See http://crbug.com/49580.
132 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
133 #define MAYBE_DoAppendUTF8Invalid DoAppendUTF8Invalid
134 #else
135 #define MAYBE_DoAppendUTF8Invalid DISABLED_DoAppendUTF8Invalid
136 #endif
137 TEST(URLCanonTest, MAYBE_DoAppendUTF8Invalid) {
138 std::string out_str;
139 StdStringCanonOutput output(&out_str);
140 // Invalid code point (too large).
141 ASSERT_DEBUG_DEATH({
142 AppendUTF8Value(0x110000, &output);
143 output.Complete();
144 EXPECT_EQ("", out_str);
145 }, "");
147 #endif // defined(GTEST_HAS_DEATH_TEST)
149 TEST(URLCanonTest, UTF) {
150 // Low-level test that we handle reading, canonicalization, and writing
151 // UTF-8/UTF-16 strings properly.
152 struct UTFCase {
153 const char* input8;
154 const wchar_t* input16;
155 bool expected_success;
156 const char* output;
157 } utf_cases[] = {
158 // Valid canonical input should get passed through & escaped.
159 {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
160 // Test a character that takes > 16 bits (U+10300 = old italic letter A)
161 {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
162 // Non-shortest-form UTF-8 characters are invalid. The bad character
163 // should be replaced with the invalid character (EF BF DB in UTF-8).
164 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"},
165 // Invalid UTF-8 sequences should be marked as invalid (the first
166 // sequence is truncated).
167 {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
168 // Character going off the end.
169 {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
170 // ...same with low surrogates with no high surrogate.
171 {"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"},
172 // Test a UTF-8 encoded surrogate value is marked as invalid.
173 // ED A0 80 = U+D800
174 {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
177 std::string out_str;
178 for (size_t i = 0; i < arraysize(utf_cases); i++) {
179 if (utf_cases[i].input8) {
180 out_str.clear();
181 StdStringCanonOutput output(&out_str);
183 int input_len = static_cast<int>(strlen(utf_cases[i].input8));
184 bool success = true;
185 for (int ch = 0; ch < input_len; ch++) {
186 success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
187 &output);
189 output.Complete();
190 EXPECT_EQ(utf_cases[i].expected_success, success);
191 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
193 if (utf_cases[i].input16) {
194 out_str.clear();
195 StdStringCanonOutput output(&out_str);
197 base::string16 input_str(WStringToUTF16(utf_cases[i].input16));
198 int input_len = static_cast<int>(input_str.length());
199 bool success = true;
200 for (int ch = 0; ch < input_len; ch++) {
201 success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
202 &output);
204 output.Complete();
205 EXPECT_EQ(utf_cases[i].expected_success, success);
206 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
209 if (utf_cases[i].input8 && utf_cases[i].input16 &&
210 utf_cases[i].expected_success) {
211 // Check that the UTF-8 and UTF-16 inputs are equivalent.
213 // UTF-16 -> UTF-8
214 std::string input8_str(utf_cases[i].input8);
215 base::string16 input16_str(WStringToUTF16(utf_cases[i].input16));
216 EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str));
218 // UTF-8 -> UTF-16
219 EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
224 TEST(URLCanonTest, Scheme) {
225 // Here, we're mostly testing that unusual characters are handled properly.
226 // The canonicalizer doesn't do any parsing or whitespace detection. It will
227 // also do its best on error, and will escape funny sequences (these won't be
228 // valid schemes and it will return error).
230 // Note that the canonicalizer will append a colon to the output to separate
231 // out the rest of the URL, which is not present in the input. We check,
232 // however, that the output range includes everything but the colon.
233 ComponentCase scheme_cases[] = {
234 {"http", "http:", Component(0, 4), true},
235 {"HTTP", "http:", Component(0, 4), true},
236 {" HTTP ", "%20http%20:", Component(0, 10), false},
237 {"htt: ", "htt%3A%20:", Component(0, 9), false},
238 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false},
239 // Don't re-escape something already escaped. Note that it will
240 // "canonicalize" the 'A' to 'a', but that's OK.
241 {"ht%3Atp", "ht%3atp:", Component(0, 7), false},
244 std::string out_str;
246 for (size_t i = 0; i < arraysize(scheme_cases); i++) {
247 int url_len = static_cast<int>(strlen(scheme_cases[i].input));
248 Component in_comp(0, url_len);
249 Component out_comp;
251 out_str.clear();
252 StdStringCanonOutput output1(&out_str);
253 bool success = CanonicalizeScheme(scheme_cases[i].input, in_comp, &output1,
254 &out_comp);
255 output1.Complete();
257 EXPECT_EQ(scheme_cases[i].expected_success, success);
258 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
259 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
260 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
262 // Now try the wide version.
263 out_str.clear();
264 StdStringCanonOutput output2(&out_str);
266 base::string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input));
267 in_comp.len = static_cast<int>(wide_input.length());
268 success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2,
269 &out_comp);
270 output2.Complete();
272 EXPECT_EQ(scheme_cases[i].expected_success, success);
273 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
274 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
275 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
278 // Test the case where the scheme is declared nonexistent, it should be
279 // converted into an empty scheme.
280 Component out_comp;
281 out_str.clear();
282 StdStringCanonOutput output(&out_str);
284 EXPECT_TRUE(CanonicalizeScheme("", Component(0, -1), &output, &out_comp));
285 output.Complete();
287 EXPECT_EQ(std::string(":"), out_str);
288 EXPECT_EQ(0, out_comp.begin);
289 EXPECT_EQ(0, out_comp.len);
292 TEST(URLCanonTest, Host) {
293 IPAddressCase host_cases[] = {
294 // Basic canonicalization, uppercase should be converted to lowercase.
295 {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
296 // Spaces and some other characters should be escaped.
297 {"Goo%20 goo%7C|.com", L"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", Component(0, 22), CanonHostInfo::NEUTRAL, -1, ""},
298 // Exciting different types of spaces!
299 {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
300 // Other types of space (no-break, zero-width, zero-width-no-break) are
301 // name-prepped away to nothing.
302 {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
303 // Ideographic full stop (full-width period for Chinese, etc.) should be
304 // treated as a dot.
305 {NULL, L"www.foo\x3002" L"bar.com", "www.foo.bar.com", Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
306 // Invalid unicode characters should fail...
307 // ...In wide input, ICU will barf and we'll end up with the input as
308 // escaped UTF-8 (the invalid character should be replaced with the
309 // replacement character).
310 {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
311 // ...This is the same as previous but with with escaped.
312 {"%ef%b7%90zyx.com", L"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
313 // Test name prepping, fullwidth input should be converted to ASCII and NOT
314 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
315 {"\xef\xbc\xa7\xef\xbd\x8f.com", L"\xff27\xff4f.com", "go.com", Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
316 // Test that fullwidth escaped values are properly name-prepped,
317 // then converted or rejected.
318 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
319 {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
320 {"%ef%bc%85%ef%bc%94%ef%bc%91.com", L"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
321 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
322 {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
323 {"%ef%bc%85%ef%bc%90%ef%bc%90.com", L"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
324 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
325 {"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d\x4f60\x597d", "xn--6qqa088eba", Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
326 // See http://unicode.org/cldr/utility/idna.jsp for other
327 // examples/experiments and http://goo.gl/7yG11o
328 // for the full list of characters handled differently by
329 // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
331 // 4 Deviation characters are mapped/ignored in UTS 46 transitional
332 // mechansm. UTS 46, table 4 row (g).
333 // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
334 // Otherwise, it'd be "xn--fuball-cta.de".
335 {"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de",
336 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
337 // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
338 // Otherwise, it'd be "xn--wxaijb9b".
339 {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2",
340 "xn--wxaikc6b", Component(0, 12),
341 CanonHostInfo::NEUTRAL, -1, ""},
342 // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
343 // handling as well as in IDNA 2003.
344 {"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L"a\x200c" L"b\x200d" L"c", "abc",
345 Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""},
346 // ZWJ between Devanagari characters is still mapped away in UTS 46
347 // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
348 {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
349 L"\x915\x94d\x200d\x91c", "xn--11bo0m",
350 Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
351 // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
352 // However, we do allow this at the moment because we don't use
353 // STD3 rules and canonicalize full-width ASCII to ASCII.
354 {"wow\xef\xbc\x81", L"wow\xff01", "wow%21",
355 Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
356 // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
357 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
358 {"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo",
359 Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
360 // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
361 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
362 {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn",
363 "%F0%AF%A1%A8%E5%A7%BB.cn",
364 Component(0, 24), CanonHostInfo::BROKEN, -1, ""},
365 // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
366 {"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya",
367 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
368 // Symbol/punctuations are allowed in IDNA 2003/UTS46.
369 // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
370 {"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us",
371 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
372 // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
373 // We used to allow it because we passed through unassigned code points.
374 {"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com",
375 Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""},
376 // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
377 // Used to be allowed in INDA 2003.
378 {"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg",
379 Component(0, 9), CanonHostInfo::BROKEN, -1, ""},
380 // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
381 // on Unicode 3.2). We did allow it in the past because we let unassigned
382 // code point pass. We continue to allow it even though it's a
383 // "punctuation and symbol" blocked in IDNA 2008.
384 // UTS 46 table 4, row (j)
385 {"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com",
386 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
387 // Maps uppercase letters to lower case letters.
388 // In IDNA 2003, it's allowed without case-folding
389 // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
390 // (added in Unicode 4.1). UTS 46 table 4 row (k)
391 {"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com",
392 Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
393 // BiDi check test
394 // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
395 // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
396 {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
397 L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
398 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
399 // Disallowed in both IDNA 2003 and 2008 with BiDi check.
400 // Labels starting with a RTL character cannot end with a LTR character.
401 {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz",
402 "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21),
403 CanonHostInfo::BROKEN, -1, ""},
404 // Labels starting with a RTL character can end with BC=EN (European
405 // number). Disallowed in IDNA 2003 but now allowed.
406 {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2",
407 "xn--2-ymcov", Component(0, 11),
408 CanonHostInfo::NEUTRAL, -1, ""},
409 // Labels starting with a RTL character cannot have "L" characters
410 // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
411 {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2",
412 "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21),
413 CanonHostInfo::BROKEN, -1, ""},
414 // Labels starting with a RTL character can end with BC=AN (Arabic number)
415 // Disallowed in IDNA 2003, but now allowed.
416 {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662",
417 "xn--mgbjq0r", Component(0, 11),
418 CanonHostInfo::NEUTRAL, -1, ""},
419 // Labels starting with a RTL character cannot have "L" characters
420 // even if it ends with an BC=AN (Arabic number).
421 // Disallowed in both IDNA 2003/2008.
422 {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662",
423 "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26),
424 CanonHostInfo::BROKEN, -1, ""},
425 // Labels starting with a RTL character cannot mix BC=EN and BC=AN
426 {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662",
427 "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27),
428 CanonHostInfo::BROKEN, -1, ""},
429 // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
430 {"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com",
431 Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
432 // U+0080 is not allowed.
433 {"\xc2\x80.com", L"\x80.com", "%C2%80.com",
434 Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
435 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
436 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
437 // UTF-8 (wide case). The output should be equivalent to the true wide
438 // character input above).
439 {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
440 L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
441 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
442 // Invalid escaped characters should fail and the percents should be
443 // escaped.
444 {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", Component(0, 10),
445 CanonHostInfo::BROKEN, -1, ""},
446 // If we get an invalid character that has been escaped.
447 {"%25", L"%25", "%25", Component(0, 3),
448 CanonHostInfo::BROKEN, -1, ""},
449 {"hello%00", L"hello%00", "hello%00", Component(0, 8),
450 CanonHostInfo::BROKEN, -1, ""},
451 // Escaped numbers should be treated like IP addresses if they are.
452 {"%30%78%63%30%2e%30%32%35%30.01", L"%30%78%63%30%2e%30%32%35%30.01",
453 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
454 "C0A80001"},
455 {"%30%78%63%30%2e%30%32%35%30.01%2e", L"%30%78%63%30%2e%30%32%35%30.01%2e",
456 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
457 "C0A80001"},
458 // Invalid escaping should trigger the regular host error handling.
459 {"%3g%78%63%30%2e%30%32%35%30%2E.01", L"%3g%78%63%30%2e%30%32%35%30%2E.01", "%253gxc0.0250..01", Component(0, 17), CanonHostInfo::BROKEN, -1, ""},
460 // Something that isn't exactly an IP should get treated as a host and
461 // spaces escaped.
462 {"192.168.0.1 hello", L"192.168.0.1 hello", "192.168.0.1%20hello", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
463 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
464 // These are "0Xc0.0250.01" in fullwidth.
465 {"\xef\xbc\x90%Ef%bc\xb8%ef%Bd%83\xef\xbc\x90%EF%BC%8E\xef\xbc\x90\xef\xbc\x92\xef\xbc\x95\xef\xbc\x90\xef\xbc%8E\xef\xbc\x90\xef\xbc\x91", L"\xff10\xff38\xff43\xff10\xff0e\xff10\xff12\xff15\xff10\xff0e\xff10\xff11", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
466 // Broken IP addresses get marked as such.
467 {"192.168.0.257", L"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
468 {"[google.com]", L"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
469 // Cyrillic letter followed by '(' should return punycode for '(' escaped
470 // before punycode string was created. I.e.
471 // if '(' is escaped after punycode is created we would get xn--%28-8tb
472 // (incorrect).
473 {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", Component(0, 11),
474 CanonHostInfo::NEUTRAL, -1, ""},
475 // Address with all hexidecimal characters with leading number of 1<<32
476 // or greater and should return NEUTRAL rather than BROKEN if not all
477 // components are numbers.
478 {"12345678912345.de", L"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
479 {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
480 {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
481 {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
482 {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
485 // CanonicalizeHost() non-verbose.
486 std::string out_str;
487 for (size_t i = 0; i < arraysize(host_cases); i++) {
488 // Narrow version.
489 if (host_cases[i].input8) {
490 int host_len = static_cast<int>(strlen(host_cases[i].input8));
491 Component in_comp(0, host_len);
492 Component out_comp;
494 out_str.clear();
495 StdStringCanonOutput output(&out_str);
497 bool success = CanonicalizeHost(host_cases[i].input8, in_comp, &output,
498 &out_comp);
499 output.Complete();
501 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
502 success) << "for input: " << host_cases[i].input8;
503 EXPECT_EQ(std::string(host_cases[i].expected), out_str) <<
504 "for input: " << host_cases[i].input8;
505 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) <<
506 "for input: " << host_cases[i].input8;
507 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) <<
508 "for input: " << host_cases[i].input8;
511 // Wide version.
512 if (host_cases[i].input16) {
513 base::string16 input16(WStringToUTF16(host_cases[i].input16));
514 int host_len = static_cast<int>(input16.length());
515 Component in_comp(0, host_len);
516 Component out_comp;
518 out_str.clear();
519 StdStringCanonOutput output(&out_str);
521 bool success = CanonicalizeHost(input16.c_str(), in_comp, &output,
522 &out_comp);
523 output.Complete();
525 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
526 success);
527 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
528 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
529 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
533 // CanonicalizeHostVerbose()
534 for (size_t i = 0; i < arraysize(host_cases); i++) {
535 // Narrow version.
536 if (host_cases[i].input8) {
537 int host_len = static_cast<int>(strlen(host_cases[i].input8));
538 Component in_comp(0, host_len);
540 out_str.clear();
541 StdStringCanonOutput output(&out_str);
542 CanonHostInfo host_info;
544 CanonicalizeHostVerbose(host_cases[i].input8, in_comp, &output,
545 &host_info);
546 output.Complete();
548 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
549 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
550 EXPECT_EQ(host_cases[i].expected_component.begin,
551 host_info.out_host.begin);
552 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
553 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
554 BytesToHexString(host_info.address, host_info.AddressLength()));
555 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
556 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
557 host_info.num_ipv4_components);
561 // Wide version.
562 if (host_cases[i].input16) {
563 base::string16 input16(WStringToUTF16(host_cases[i].input16));
564 int host_len = static_cast<int>(input16.length());
565 Component in_comp(0, host_len);
567 out_str.clear();
568 StdStringCanonOutput output(&out_str);
569 CanonHostInfo host_info;
571 CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info);
572 output.Complete();
574 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
575 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
576 EXPECT_EQ(host_cases[i].expected_component.begin,
577 host_info.out_host.begin);
578 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
579 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
580 BytesToHexString(host_info.address, host_info.AddressLength()));
581 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
582 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
583 host_info.num_ipv4_components);
589 TEST(URLCanonTest, IPv4) {
590 IPAddressCase cases[] = {
591 // Empty is not an IP address.
592 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
593 {".", L".", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
594 // Regular IP addresses in different bases.
595 {"192.168.0.1", L"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
596 {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
597 {"0xC0.0Xa8.0x0.0x1", L"0xC0.0Xa8.0x0.0x1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
598 // Non-IP addresses due to invalid characters.
599 {"192.168.9.com", L"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
600 // Invalid characters for the base should be rejected.
601 {"19a.168.0.1", L"19a.168.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
602 {"0308.0250.00.01", L"0308.0250.00.01", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
603 {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
604 // If there are not enough components, the last one should fill them out.
605 {"192", L"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
606 {"0xC0a80001", L"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
607 {"030052000001", L"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
608 {"000030052000001", L"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
609 {"192.168", L"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
610 {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
611 {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
612 {"192.168.1", L"192.168.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
613 // Too many components means not an IP address.
614 {"192.168.0.0.1", L"192.168.0.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
615 // We allow a single trailing dot.
616 {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
617 {"192.168.0.1. hello", L"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
618 {"192.168.0.1..", L"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
619 // Two dots in a row means not an IP address.
620 {"192.168..1", L"192.168..1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
621 // Any numerical overflow should be marked as BROKEN.
622 {"0x100.0", L"0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
623 {"0x100.0.0", L"0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
624 {"0x100.0.0.0", L"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
625 {"0.0x100.0.0", L"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
626 {"0.0.0x100.0", L"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
627 {"0.0.0.0x100", L"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN, -1, ""},
628 {"0.0.0x10000", L"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
629 {"0.0x1000000", L"0.0x1000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
630 {"0x100000000", L"0x100000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
631 // Repeat the previous tests, minus 1, to verify boundaries.
632 {"0xFF.0", L"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
633 {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
634 {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
635 {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
636 {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
637 {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
638 {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
639 {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
640 {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
641 // Old trunctations tests. They're all "BROKEN" now.
642 {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN, -1, ""},
643 {"192.168.0.257", L"192.168.0.257", "", Component(), CanonHostInfo::BROKEN, -1, ""},
644 {"192.168.0xa20001", L"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
645 {"192.015052000001", L"192.015052000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
646 {"0X12C0a80001", L"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
647 {"276.1.2", L"276.1.2", "", Component(), CanonHostInfo::BROKEN, -1, ""},
648 // Spaces should be rejected.
649 {"192.168.0.1 hello", L"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
650 // Very large numbers.
651 {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
652 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
653 // A number has no length limit, but long numbers can still overflow.
654 {"00000000000000000001", L"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
655 {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
656 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
657 {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
658 {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
659 // Truncation of all zeros should still result in 0.
660 {"0.00.0x.0x0", L"0.00.0x.0x0", "0.0.0.0", Component(0, 7), CanonHostInfo::IPV4, 4, "00000000"},
663 for (size_t i = 0; i < arraysize(cases); i++) {
664 // 8-bit version.
665 Component component(0, static_cast<int>(strlen(cases[i].input8)));
667 std::string out_str1;
668 StdStringCanonOutput output1(&out_str1);
669 CanonHostInfo host_info;
670 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
671 output1.Complete();
673 EXPECT_EQ(cases[i].expected_family, host_info.family);
674 EXPECT_EQ(std::string(cases[i].expected_address_hex),
675 BytesToHexString(host_info.address, host_info.AddressLength()));
676 if (host_info.family == CanonHostInfo::IPV4) {
677 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
678 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
679 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
680 EXPECT_EQ(cases[i].expected_num_ipv4_components,
681 host_info.num_ipv4_components);
684 // 16-bit version.
685 base::string16 input16(WStringToUTF16(cases[i].input16));
686 component = Component(0, static_cast<int>(input16.length()));
688 std::string out_str2;
689 StdStringCanonOutput output2(&out_str2);
690 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
691 output2.Complete();
693 EXPECT_EQ(cases[i].expected_family, host_info.family);
694 EXPECT_EQ(std::string(cases[i].expected_address_hex),
695 BytesToHexString(host_info.address, host_info.AddressLength()));
696 if (host_info.family == CanonHostInfo::IPV4) {
697 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
698 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
699 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
700 EXPECT_EQ(cases[i].expected_num_ipv4_components,
701 host_info.num_ipv4_components);
706 TEST(URLCanonTest, IPv6) {
707 IPAddressCase cases[] = {
708 // Empty is not an IP address.
709 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
710 // Non-IPs with [:] characters are marked BROKEN.
711 {":", L":", "", Component(), CanonHostInfo::BROKEN, -1, ""},
712 {"[", L"[", "", Component(), CanonHostInfo::BROKEN, -1, ""},
713 {"[:", L"[:", "", Component(), CanonHostInfo::BROKEN, -1, ""},
714 {"]", L"]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
715 {":]", L":]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
716 {"[]", L"[]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
717 {"[:]", L"[:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
718 // Regular IP address is invalid without bounding '[' and ']'.
719 {"2001:db8::1", L"2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
720 {"[2001:db8::1", L"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
721 {"2001:db8::1]", L"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
722 // Regular IP addresses.
723 {"[::]", L"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
724 {"[::1]", L"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
725 {"[1::]", L"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
727 // Leading zeros should be stripped.
728 {"[000:01:02:003:004:5:6:007]", L"[000:01:02:003:004:5:6:007]", "[0:1:2:3:4:5:6:7]", Component(0,17), CanonHostInfo::IPV6, -1, "00000001000200030004000500060007"},
730 // Upper case letters should be lowercased.
731 {"[A:b:c:DE:fF:0:1:aC]", L"[A:b:c:DE:fF:0:1:aC]", "[a:b:c:de:ff:0:1:ac]", Component(0,20), CanonHostInfo::IPV6, -1, "000A000B000C00DE00FF0000000100AC"},
733 // The same address can be written with different contractions, but should
734 // get canonicalized to the same thing.
735 {"[1:0:0:2::3:0]", L"[1:0:0:2::3:0]", "[1::2:0:0:3:0]", Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
736 {"[1::2:0:0:3:0]", L"[1::2:0:0:3:0]", "[1::2:0:0:3:0]", Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
738 // Addresses with embedded IPv4.
739 {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
740 {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
741 {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
742 {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
743 {"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
745 // IPv4 with last component missing.
746 {"[::ffff:192.1.2]", L"[::ffff:192.1.2]", "[::ffff:c001:2]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0010002"},
748 // IPv4 using hex.
749 // TODO(eroman): Should this format be disallowed?
750 {"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
752 // There may be zeros surrounding the "::" contraction.
753 {"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000008"},
755 {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
757 // Can only have one "::" contraction in an IPv6 string literal.
758 {"[2001::db8::1]", L"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
759 // No more than 2 consecutive ':'s.
760 {"[2001:db8:::1]", L"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
761 {"[:::]", L"[:::]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
762 // Non-IP addresses due to invalid characters.
763 {"[2001::.com]", L"[2001::.com]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
764 // If there are not enough components, the last one should fill them out.
765 // ... omitted at this time ...
766 // Too many components means not an IP address. Similarly, with too few
767 // if using IPv4 compat or mapped addresses.
768 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
769 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
770 {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
771 // Too many bits (even though 8 comonents, the last one holds 32 bits).
772 {"[0:0:0:0:0:0:0:192.168.0.1]", L"[0:0:0:0:0:0:0:192.168.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
774 // Too many bits specified -- the contraction would have to be zero-length
775 // to not exceed 128 bits.
776 {"[1:2:3:4:5:6::192.168.0.1]", L"[1:2:3:4:5:6::192.168.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
778 // The contraction is for 16 bits of zero.
779 {"[1:2:3:4:5:6::8]", L"[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]", Component(0,17), CanonHostInfo::IPV6, -1, "00010002000300040005000600000008"},
781 // Cannot have a trailing colon.
782 {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
783 {"[1:2:3:4:5:6:192.168.0.1:]", L"[1:2:3:4:5:6:192.168.0.1:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
785 // Cannot have negative numbers.
786 {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
788 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
789 // The scope_id should be included in the canonicalized URL, and is an
790 // unsigned decimal number.
792 // Invalid because no ID was given after the percent.
794 // Don't allow scope-id
795 {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
796 {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
797 {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
798 {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
799 {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
801 // Don't allow leading or trailing colons.
802 {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
803 {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
804 {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
806 // We allow a single trailing dot.
807 // ... omitted at this time ...
808 // Two dots in a row means not an IP address.
809 {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
810 // Any non-first components get truncated to one byte.
811 // ... omitted at this time ...
812 // Spaces should be rejected.
813 {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
816 for (size_t i = 0; i < arraysize(cases); i++) {
817 // 8-bit version.
818 Component component(0, static_cast<int>(strlen(cases[i].input8)));
820 std::string out_str1;
821 StdStringCanonOutput output1(&out_str1);
822 CanonHostInfo host_info;
823 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
824 output1.Complete();
826 EXPECT_EQ(cases[i].expected_family, host_info.family);
827 EXPECT_EQ(std::string(cases[i].expected_address_hex),
828 BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
829 if (host_info.family == CanonHostInfo::IPV6) {
830 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
831 EXPECT_EQ(cases[i].expected_component.begin,
832 host_info.out_host.begin);
833 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
836 // 16-bit version.
837 base::string16 input16(WStringToUTF16(cases[i].input16));
838 component = Component(0, static_cast<int>(input16.length()));
840 std::string out_str2;
841 StdStringCanonOutput output2(&out_str2);
842 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
843 output2.Complete();
845 EXPECT_EQ(cases[i].expected_family, host_info.family);
846 EXPECT_EQ(std::string(cases[i].expected_address_hex),
847 BytesToHexString(host_info.address, host_info.AddressLength()));
848 if (host_info.family == CanonHostInfo::IPV6) {
849 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
850 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
851 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
856 TEST(URLCanonTest, IPEmpty) {
857 std::string out_str1;
858 StdStringCanonOutput output1(&out_str1);
859 CanonHostInfo host_info;
861 // This tests tests.
862 const char spec[] = "192.168.0.1";
863 CanonicalizeIPAddress(spec, Component(), &output1, &host_info);
864 EXPECT_FALSE(host_info.IsIPAddress());
866 CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info);
867 EXPECT_FALSE(host_info.IsIPAddress());
870 TEST(URLCanonTest, UserInfo) {
871 // Note that the canonicalizer should escape and treat empty components as
872 // not being there.
874 // We actually parse a full input URL so we can get the initial components.
875 struct UserComponentCase {
876 const char* input;
877 const char* expected;
878 Component expected_username;
879 Component expected_password;
880 bool expected_success;
881 } user_info_cases[] = {
882 {"http://user:pass@host.com/", "user:pass@", Component(0, 4), Component(5, 4), true},
883 {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
884 {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
885 {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
886 {"http://:foo@host.com/", ":foo@", Component(0, 0), Component(1, 3), true},
887 {"http://^ :$\t@host.com/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
888 {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
889 {"http://%2540:bar@domain.com/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
891 // IE7 compatibility: old versions allowed backslashes in usernames, but
892 // IE7 does not. We disallow it as well.
893 {"ftp://me\\mydomain:pass@foo.com/", "", Component(0, -1), Component(0, -1), true},
896 for (size_t i = 0; i < arraysize(user_info_cases); i++) {
897 int url_len = static_cast<int>(strlen(user_info_cases[i].input));
898 Parsed parsed;
899 ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
900 Component out_user, out_pass;
901 std::string out_str;
902 StdStringCanonOutput output1(&out_str);
904 bool success = CanonicalizeUserInfo(user_info_cases[i].input,
905 parsed.username,
906 user_info_cases[i].input,
907 parsed.password,
908 &output1,
909 &out_user,
910 &out_pass);
911 output1.Complete();
913 EXPECT_EQ(user_info_cases[i].expected_success, success);
914 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
915 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
916 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
917 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
918 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
920 // Now try the wide version
921 out_str.clear();
922 StdStringCanonOutput output2(&out_str);
923 base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
924 success = CanonicalizeUserInfo(wide_input.c_str(),
925 parsed.username,
926 wide_input.c_str(),
927 parsed.password,
928 &output2,
929 &out_user,
930 &out_pass);
931 output2.Complete();
933 EXPECT_EQ(user_info_cases[i].expected_success, success);
934 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
935 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
936 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
937 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
938 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
942 TEST(URLCanonTest, Port) {
943 // We only need to test that the number gets properly put into the output
944 // buffer. The parser unit tests will test scanning the number correctly.
946 // Note that the CanonicalizePort will always prepend a colon to the output
947 // to separate it from the colon that it assumes precedes it.
948 struct PortCase {
949 const char* input;
950 int default_port;
951 const char* expected;
952 Component expected_component;
953 bool expected_success;
954 } port_cases[] = {
955 // Invalid input should be copied w/ failure.
956 {"as df", 80, ":as%20df", Component(1, 7), false},
957 {"-2", 80, ":-2", Component(1, 2), false},
958 // Default port should be omitted.
959 {"80", 80, "", Component(0, -1), true},
960 {"8080", 80, ":8080", Component(1, 4), true},
961 // PORT_UNSPECIFIED should mean always keep the port.
962 {"80", PORT_UNSPECIFIED, ":80", Component(1, 2), true},
965 for (size_t i = 0; i < arraysize(port_cases); i++) {
966 int url_len = static_cast<int>(strlen(port_cases[i].input));
967 Component in_comp(0, url_len);
968 Component out_comp;
969 std::string out_str;
970 StdStringCanonOutput output1(&out_str);
971 bool success = CanonicalizePort(port_cases[i].input,
972 in_comp,
973 port_cases[i].default_port,
974 &output1,
975 &out_comp);
976 output1.Complete();
978 EXPECT_EQ(port_cases[i].expected_success, success);
979 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
980 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
981 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
983 // Now try the wide version
984 out_str.clear();
985 StdStringCanonOutput output2(&out_str);
986 base::string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
987 success = CanonicalizePort(wide_input.c_str(),
988 in_comp,
989 port_cases[i].default_port,
990 &output2,
991 &out_comp);
992 output2.Complete();
994 EXPECT_EQ(port_cases[i].expected_success, success);
995 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
996 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
997 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
1001 TEST(URLCanonTest, Path) {
1002 DualComponentCase path_cases[] = {
1003 // ----- path collapsing tests -----
1004 {"/././foo", L"/././foo", "/foo", Component(0, 4), true},
1005 {"/./.foo", L"/./.foo", "/.foo", Component(0, 5), true},
1006 {"/foo/.", L"/foo/.", "/foo/", Component(0, 5), true},
1007 {"/foo/./", L"/foo/./", "/foo/", Component(0, 5), true},
1008 // double dots followed by a slash or the end of the string count
1009 {"/foo/bar/..", L"/foo/bar/..", "/foo/", Component(0, 5), true},
1010 {"/foo/bar/../", L"/foo/bar/../", "/foo/", Component(0, 5), true},
1011 // don't count double dots when they aren't followed by a slash
1012 {"/foo/..bar", L"/foo/..bar", "/foo/..bar", Component(0, 10), true},
1013 // some in the middle
1014 {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", Component(0, 8), true},
1015 {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", Component(0, 2), true},
1016 // we should not be able to go above the root
1017 {"/foo/../../..", L"/foo/../../..", "/", Component(0, 1), true},
1018 {"/foo/../../../ton", L"/foo/../../../ton", "/ton", Component(0, 4), true},
1019 // escaped dots should be unescaped and treated the same as dots
1020 {"/foo/%2e", L"/foo/%2e", "/foo/", Component(0, 5), true},
1021 {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", Component(0, 8), true},
1022 {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", Component(0, 6), true},
1023 // Multiple slashes in a row should be preserved and treated like empty
1024 // directory names.
1025 {"////../..", L"////../..", "//", Component(0, 2), true},
1027 // ----- escaping tests -----
1028 {"/foo", L"/foo", "/foo", Component(0, 4), true},
1029 // Valid escape sequence
1030 {"/%20foo", L"/%20foo", "/%20foo", Component(0, 7), true},
1031 // Invalid escape sequence we should pass through unchanged.
1032 {"/foo%", L"/foo%", "/foo%", Component(0, 5), true},
1033 {"/foo%2", L"/foo%2", "/foo%2", Component(0, 6), true},
1034 // Invalid escape sequence: bad characters should be treated the same as
1035 // the sourrounding text, not as escaped (in this case, UTF-8).
1036 {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", Component(0, 10), true},
1037 {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", Component(0, 16), true},
1038 {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22), true},
1039 // Regular characters that are escaped should be unescaped
1040 {"/foo%41%7a", L"/foo%41%7a", "/fooAz", Component(0, 6), true},
1041 // Funny characters that are unescaped should be escaped
1042 {"/foo\x09\x91%91", NULL, "/foo%09%91%91", Component(0, 13), true},
1043 {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true},
1044 // Invalid characters that are escaped should cause a failure.
1045 {"/foo%00%51", L"/foo%00%51", "/foo%00Q", Component(0, 8), false},
1046 // Some characters should be passed through unchanged regardless of esc.
1047 {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13), true},
1048 // Characters that are properly escaped should not have the case changed
1049 // of hex letters.
1050 {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13), true},
1051 // Funny characters that are unescaped should be escaped
1052 {"/foo\tbar", L"/foo\tbar", "/foo%09bar", Component(0, 10), true},
1053 // Backslashes should get converted to forward slashes
1054 {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", Component(0, 8), true},
1055 // Hashes found in paths (possibly only when the caller explicitly sets
1056 // the path on an already-parsed URL) should be escaped.
1057 {"/foo#bar", L"/foo#bar", "/foo%23bar", Component(0, 10), true},
1058 // %7f should be allowed and %3D should not be unescaped (these were wrong
1059 // in a previous version).
1060 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true},
1061 // @ should be passed through unchanged (escaped or unescaped).
1062 {"/@asdf%40", L"/@asdf%40", "/@asdf%40", Component(0, 9), true},
1063 // Nested escape sequences should result in escaping the leading '%' if
1064 // unescaping would result in a new escape sequence.
1065 {"/%A%42", L"/%A%42", "/%25AB", Component(0, 6), true},
1066 {"/%%41B", L"/%%41B", "/%25AB", Component(0, 6), true},
1067 {"/%%41%42", L"/%%41%42", "/%25AB", Component(0, 6), true},
1068 // Make sure truncated "nested" escapes don't result in reading off the
1069 // string end.
1070 {"/%%41", L"/%%41", "/%A", Component(0, 3), true},
1071 // Don't unescape the leading '%' if unescaping doesn't result in a valid
1072 // new escape sequence.
1073 {"/%%470", L"/%%470", "/%G0", Component(0, 4), true},
1074 {"/%%2D%41", L"/%%2D%41", "/%-A", Component(0, 4), true},
1075 // Don't erroneously downcast a UTF-16 charater in a way that makes it
1076 // look like part of an escape sequence.
1077 {NULL, L"/%%41\x0130", "/%A%C4%B0", Component(0, 9), true},
1079 // ----- encoding tests -----
1080 // Basic conversions
1081 {"/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"/\x4f60\x597d\x4f60\x597d", "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD", Component(0, 37), true},
1082 // Invalid unicode characters should fail. We only do validation on
1083 // UTF-16 input, so this doesn't happen on 8-bit.
1084 {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", Component(0, 13), true},
1085 {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false},
1088 for (size_t i = 0; i < arraysize(path_cases); i++) {
1089 if (path_cases[i].input8) {
1090 int len = static_cast<int>(strlen(path_cases[i].input8));
1091 Component in_comp(0, len);
1092 Component out_comp;
1093 std::string out_str;
1094 StdStringCanonOutput output(&out_str);
1095 bool success =
1096 CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp);
1097 output.Complete();
1099 EXPECT_EQ(path_cases[i].expected_success, success);
1100 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1101 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1102 EXPECT_EQ(path_cases[i].expected, out_str);
1105 if (path_cases[i].input16) {
1106 base::string16 input16(WStringToUTF16(path_cases[i].input16));
1107 int len = static_cast<int>(input16.length());
1108 Component in_comp(0, len);
1109 Component out_comp;
1110 std::string out_str;
1111 StdStringCanonOutput output(&out_str);
1113 bool success =
1114 CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp);
1115 output.Complete();
1117 EXPECT_EQ(path_cases[i].expected_success, success);
1118 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1119 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1120 EXPECT_EQ(path_cases[i].expected, out_str);
1124 // Manual test: embedded NULLs should be escaped and the URL should be marked
1125 // as invalid.
1126 const char path_with_null[] = "/ab\0c";
1127 Component in_comp(0, 5);
1128 Component out_comp;
1130 std::string out_str;
1131 StdStringCanonOutput output(&out_str);
1132 bool success = CanonicalizePath(path_with_null, in_comp, &output, &out_comp);
1133 output.Complete();
1134 EXPECT_FALSE(success);
1135 EXPECT_EQ("/ab%00c", out_str);
1138 TEST(URLCanonTest, Query) {
1139 struct QueryCase {
1140 const char* input8;
1141 const wchar_t* input16;
1142 const char* expected;
1143 } query_cases[] = {
1144 // Regular ASCII case.
1145 {"foo=bar", L"foo=bar", "?foo=bar"},
1146 // Allow question marks in the query without escaping
1147 {"as?df", L"as?df", "?as?df"},
1148 // Always escape '#' since it would mark the ref.
1149 {"as#df", L"as#df", "?as%23df"},
1150 // Escape some questionable 8-bit characters, but never unescape.
1151 {"\x02hello\x7f bye", L"\x02hello\x7f bye", "?%02hello%7F%20bye"},
1152 {"%40%41123", L"%40%41123", "?%40%41123"},
1153 // Chinese input/output
1154 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "?q=%E4%BD%A0%E5%A5%BD"},
1155 // Invalid UTF-8/16 input should be replaced with invalid characters.
1156 {"q=\xed\xed", L"q=\xd800\xd800", "?q=%EF%BF%BD%EF%BF%BD"},
1157 // Don't allow < or > because sometimes they are used for XSS if the
1158 // URL is echoed in content. Firefox does this, IE doesn't.
1159 {"q=<asdf>", L"q=<asdf>", "?q=%3Casdf%3E"},
1160 // Escape double quotemarks in the query.
1161 {"q=\"asdf\"", L"q=\"asdf\"", "?q=%22asdf%22"},
1164 for (size_t i = 0; i < arraysize(query_cases); i++) {
1165 Component out_comp;
1167 if (query_cases[i].input8) {
1168 int len = static_cast<int>(strlen(query_cases[i].input8));
1169 Component in_comp(0, len);
1170 std::string out_str;
1172 StdStringCanonOutput output(&out_str);
1173 CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output,
1174 &out_comp);
1175 output.Complete();
1177 EXPECT_EQ(query_cases[i].expected, out_str);
1180 if (query_cases[i].input16) {
1181 base::string16 input16(WStringToUTF16(query_cases[i].input16));
1182 int len = static_cast<int>(input16.length());
1183 Component in_comp(0, len);
1184 std::string out_str;
1186 StdStringCanonOutput output(&out_str);
1187 CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp);
1188 output.Complete();
1190 EXPECT_EQ(query_cases[i].expected, out_str);
1194 // Extra test for input with embedded NULL;
1195 std::string out_str;
1196 StdStringCanonOutput output(&out_str);
1197 Component out_comp;
1198 CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL, &output, &out_comp);
1199 output.Complete();
1200 EXPECT_EQ("?a%20%00z%01", out_str);
1203 TEST(URLCanonTest, Ref) {
1204 // Refs are trivial, it just checks the encoding.
1205 DualComponentCase ref_cases[] = {
1206 // Regular one, we shouldn't escape spaces, et al.
1207 {"hello, world", L"hello, world", "#hello, world", Component(1, 12), true},
1208 // UTF-8/wide input should be preserved
1209 {"\xc2\xa9", L"\xa9", "#\xc2\xa9", Component(1, 2), true},
1210 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
1211 {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", Component(1, 6), true},
1212 // Escaping should be preserved unchanged, even invalid ones
1213 {"%41%a", L"%41%a", "#%41%a", Component(1, 5), true},
1214 // Invalid UTF-8/16 input should be flagged and the input made valid
1215 {"\xc2", NULL, "#\xef\xbf\xbd", Component(1, 3), true},
1216 {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", Component(1, 6), true},
1217 // Test a Unicode invalid character.
1218 {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", Component(1, 4), true},
1219 // Refs can have # signs and we should preserve them.
1220 {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", Component(1, 9), true},
1221 {"#asdf", L"#asdf", "##asdf", Component(1, 5), true},
1224 for (size_t i = 0; i < arraysize(ref_cases); i++) {
1225 // 8-bit input
1226 if (ref_cases[i].input8) {
1227 int len = static_cast<int>(strlen(ref_cases[i].input8));
1228 Component in_comp(0, len);
1229 Component out_comp;
1231 std::string out_str;
1232 StdStringCanonOutput output(&out_str);
1233 CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp);
1234 output.Complete();
1236 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1237 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1238 EXPECT_EQ(ref_cases[i].expected, out_str);
1241 // 16-bit input
1242 if (ref_cases[i].input16) {
1243 base::string16 input16(WStringToUTF16(ref_cases[i].input16));
1244 int len = static_cast<int>(input16.length());
1245 Component in_comp(0, len);
1246 Component out_comp;
1248 std::string out_str;
1249 StdStringCanonOutput output(&out_str);
1250 CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
1251 output.Complete();
1253 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1254 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1255 EXPECT_EQ(ref_cases[i].expected, out_str);
1259 // Try one with an embedded NULL. It should be stripped.
1260 const char null_input[5] = "ab\x00z";
1261 Component null_input_component(0, 4);
1262 Component out_comp;
1264 std::string out_str;
1265 StdStringCanonOutput output(&out_str);
1266 CanonicalizeRef(null_input, null_input_component, &output, &out_comp);
1267 output.Complete();
1269 EXPECT_EQ(1, out_comp.begin);
1270 EXPECT_EQ(3, out_comp.len);
1271 EXPECT_EQ("#abz", out_str);
1274 TEST(URLCanonTest, CanonicalizeStandardURL) {
1275 // The individual component canonicalize tests should have caught the cases
1276 // for each of those components. Here, we just need to test that the various
1277 // parts are included or excluded properly, and have the correct separators.
1278 struct URLCase {
1279 const char* input;
1280 const char* expected;
1281 bool expected_success;
1282 } cases[] = {
1283 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1284 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1285 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1286 {"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo", true},
1287 {"www.google.com", ":www.google.com/", true},
1288 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1289 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1290 {"http://user:pass@/", "http://user:pass@/", false},
1291 {"http://%25DOMAIN:foobar@foodomain.com/", "http://%25DOMAIN:foobar@foodomain.com/", true},
1293 // Backslashes should get converted to forward slashes.
1294 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1296 // Busted refs shouldn't make the whole thing fail.
1297 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1299 // Basic port tests.
1300 {"http://foo:80/", "http://foo/", true},
1301 {"http://foo:81/", "http://foo:81/", true},
1302 {"httpa://foo:80/", "httpa://foo:80/", true},
1303 {"http://foo:-80/", "http://foo:-80/", false},
1305 {"https://foo:443/", "https://foo/", true},
1306 {"https://foo:80/", "https://foo:80/", true},
1307 {"ftp://foo:21/", "ftp://foo/", true},
1308 {"ftp://foo:80/", "ftp://foo:80/", true},
1309 {"gopher://foo:70/", "gopher://foo/", true},
1310 {"gopher://foo:443/", "gopher://foo:443/", true},
1311 {"ws://foo:80/", "ws://foo/", true},
1312 {"ws://foo:81/", "ws://foo:81/", true},
1313 {"ws://foo:443/", "ws://foo:443/", true},
1314 {"ws://foo:815/", "ws://foo:815/", true},
1315 {"wss://foo:80/", "wss://foo:80/", true},
1316 {"wss://foo:81/", "wss://foo:81/", true},
1317 {"wss://foo:443/", "wss://foo/", true},
1318 {"wss://foo:815/", "wss://foo:815/", true},
1321 for (size_t i = 0; i < arraysize(cases); i++) {
1322 int url_len = static_cast<int>(strlen(cases[i].input));
1323 Parsed parsed;
1324 ParseStandardURL(cases[i].input, url_len, &parsed);
1326 Parsed out_parsed;
1327 std::string out_str;
1328 StdStringCanonOutput output(&out_str);
1329 bool success = CanonicalizeStandardURL(
1330 cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
1331 output.Complete();
1333 EXPECT_EQ(cases[i].expected_success, success);
1334 EXPECT_EQ(cases[i].expected, out_str);
1338 // The codepath here is the same as for regular canonicalization, so we just
1339 // need to test that things are replaced or not correctly.
1340 TEST(URLCanonTest, ReplaceStandardURL) {
1341 ReplaceCase replace_cases[] = {
1342 // Common case of truncating the path.
1343 {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
1344 // Replace everything
1345 {"http://a:b@google.com:22/foo;bar?baz@cat", "https", "me", "pw", "host.com", "99", "/path", "query", "ref", "https://me:pw@host.com:99/path?query#ref"},
1346 // Replace nothing
1347 {"http://a:b@google.com:22/foo?baz@cat", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "http://a:b@google.com:22/foo?baz@cat"},
1348 // Replace scheme with filesystem. The result is garbage, but you asked
1349 // for it.
1350 {"http://a:b@google.com:22/foo?baz@cat", "filesystem", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem://a:b@google.com:22/foo?baz@cat"},
1353 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1354 const ReplaceCase& cur = replace_cases[i];
1355 int base_len = static_cast<int>(strlen(cur.base));
1356 Parsed parsed;
1357 ParseStandardURL(cur.base, base_len, &parsed);
1359 Replacements<char> r;
1360 typedef Replacements<char> R; // Clean up syntax.
1362 // Note that for the scheme we pass in a different clear function since
1363 // there is no function to clear the scheme.
1364 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1365 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1366 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1367 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1368 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1369 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1370 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1371 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1373 std::string out_str;
1374 StdStringCanonOutput output(&out_str);
1375 Parsed out_parsed;
1376 ReplaceStandardURL(replace_cases[i].base, parsed, r, NULL, &output,
1377 &out_parsed);
1378 output.Complete();
1380 EXPECT_EQ(replace_cases[i].expected, out_str);
1383 // The path pointer should be ignored if the address is invalid.
1385 const char src[] = "http://www.google.com/here_is_the_path";
1386 int src_len = static_cast<int>(strlen(src));
1388 Parsed parsed;
1389 ParseStandardURL(src, src_len, &parsed);
1391 // Replace the path to 0 length string. By using 1 as the string address,
1392 // the test should get an access violation if it tries to dereference it.
1393 Replacements<char> r;
1394 r.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0));
1395 std::string out_str1;
1396 StdStringCanonOutput output1(&out_str1);
1397 Parsed new_parsed;
1398 ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
1399 output1.Complete();
1400 EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
1402 // Same with an "invalid" path.
1403 r.SetPath(reinterpret_cast<char*>(0x00000001), Component());
1404 std::string out_str2;
1405 StdStringCanonOutput output2(&out_str2);
1406 ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
1407 output2.Complete();
1408 EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
1412 TEST(URLCanonTest, ReplaceFileURL) {
1413 ReplaceCase replace_cases[] = {
1414 // Replace everything
1415 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1416 // Replace nothing
1417 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1418 // Clear non-path components (common)
1419 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
1420 // Replace path with something that doesn't begin with a slash and make
1421 // sure it gets added properly.
1422 {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1423 {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1424 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
1425 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
1426 {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1427 // Replace scheme -- shouldn't do anything.
1428 {"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1431 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1432 const ReplaceCase& cur = replace_cases[i];
1433 int base_len = static_cast<int>(strlen(cur.base));
1434 Parsed parsed;
1435 ParseFileURL(cur.base, base_len, &parsed);
1437 Replacements<char> r;
1438 typedef Replacements<char> R; // Clean up syntax.
1439 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1440 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1441 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1442 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1443 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1444 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1445 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1446 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1448 std::string out_str;
1449 StdStringCanonOutput output(&out_str);
1450 Parsed out_parsed;
1451 ReplaceFileURL(cur.base, parsed, r, NULL, &output, &out_parsed);
1452 output.Complete();
1454 EXPECT_EQ(replace_cases[i].expected, out_str);
1458 TEST(URLCanonTest, ReplaceFileSystemURL) {
1459 ReplaceCase replace_cases[] = {
1460 // Replace everything in the outer URL.
1461 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1462 // Replace nothing
1463 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"},
1464 // Clear non-path components (common)
1465 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"},
1466 // Replace path with something that doesn't begin with a slash and make
1467 // sure it gets added properly.
1468 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"},
1469 // Replace scheme -- shouldn't do anything.
1470 {"filesystem:http://u:p@bar.com/t/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
1471 // Replace username -- shouldn't do anything.
1472 {"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, "u2", NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
1473 // Replace password -- shouldn't do anything.
1474 {"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, NULL, "pw2", NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
1475 // Replace host -- shouldn't do anything.
1476 {"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, NULL, NULL, "foo.com", NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
1477 // Replace port -- shouldn't do anything.
1478 {"filesystem:http://u:p@bar.com:40/t/gaba?query#ref", NULL, NULL, NULL, NULL, "41", NULL, NULL, NULL, "filesystem:http://u:p@bar.com:40/t/gaba?query#ref"},
1481 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1482 const ReplaceCase& cur = replace_cases[i];
1483 int base_len = static_cast<int>(strlen(cur.base));
1484 Parsed parsed;
1485 ParseFileSystemURL(cur.base, base_len, &parsed);
1487 Replacements<char> r;
1488 typedef Replacements<char> R; // Clean up syntax.
1489 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1490 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1491 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1492 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1493 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1494 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1495 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1496 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1498 std::string out_str;
1499 StdStringCanonOutput output(&out_str);
1500 Parsed out_parsed;
1501 ReplaceFileSystemURL(cur.base, parsed, r, NULL, &output, &out_parsed);
1502 output.Complete();
1504 EXPECT_EQ(replace_cases[i].expected, out_str);
1508 TEST(URLCanonTest, ReplacePathURL) {
1509 ReplaceCase replace_cases[] = {
1510 // Replace everything
1511 {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
1512 // Replace nothing
1513 {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
1514 // Replace one or the other
1515 {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
1516 {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
1517 {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
1520 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1521 const ReplaceCase& cur = replace_cases[i];
1522 int base_len = static_cast<int>(strlen(cur.base));
1523 Parsed parsed;
1524 ParsePathURL(cur.base, base_len, false, &parsed);
1526 Replacements<char> r;
1527 typedef Replacements<char> R; // Clean up syntax.
1528 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1529 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1530 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1531 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1532 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1533 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1534 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1535 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1537 std::string out_str;
1538 StdStringCanonOutput output(&out_str);
1539 Parsed out_parsed;
1540 ReplacePathURL(cur.base, parsed, r, &output, &out_parsed);
1541 output.Complete();
1543 EXPECT_EQ(replace_cases[i].expected, out_str);
1547 TEST(URLCanonTest, ReplaceMailtoURL) {
1548 ReplaceCase replace_cases[] = {
1549 // Replace everything
1550 {"mailto:jon@foo.com?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
1551 // Replace nothing
1552 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:jon@foo.com?body=sup"},
1553 // Replace the path
1554 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
1555 // Replace the query
1556 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:jon@foo.com?custom=1"},
1557 // Replace the path and query
1558 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
1559 // Set the query to empty (should leave trailing question mark)
1560 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:jon@foo.com?"},
1561 // Clear the query
1562 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:jon@foo.com"},
1563 // Clear the path
1564 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
1565 // Clear the path + query
1566 {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
1567 // Setting the ref should have no effect
1568 {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
1571 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1572 const ReplaceCase& cur = replace_cases[i];
1573 int base_len = static_cast<int>(strlen(cur.base));
1574 Parsed parsed;
1575 ParseMailtoURL(cur.base, base_len, &parsed);
1577 Replacements<char> r;
1578 typedef Replacements<char> R;
1579 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1580 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1581 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1582 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1583 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1584 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1585 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1586 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1588 std::string out_str;
1589 StdStringCanonOutput output(&out_str);
1590 Parsed out_parsed;
1591 ReplaceMailtoURL(cur.base, parsed, r, &output, &out_parsed);
1592 output.Complete();
1594 EXPECT_EQ(replace_cases[i].expected, out_str);
1598 TEST(URLCanonTest, CanonicalizeFileURL) {
1599 struct URLCase {
1600 const char* input;
1601 const char* expected;
1602 bool expected_success;
1603 Component expected_host;
1604 Component expected_path;
1605 } cases[] = {
1606 #ifdef _WIN32
1607 // Windows-style paths
1608 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1609 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1610 {"file:", "file:///", true, Component(), Component(7, 1)},
1611 {"file:UNChost/path", "file://unchost/path", true, Component(7, 7), Component(14, 5)},
1612 // CanonicalizeFileURL supports absolute Windows style paths for IE
1613 // compatibility. Note that the caller must decide that this is a file
1614 // URL itself so it can call the file canonicalizer. This is usually
1615 // done automatically as part of relative URL resolving.
1616 {"c:\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1617 {"C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1618 {"/C|\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1619 {"//C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1620 {"//server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1621 {"\\\\server\\file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1622 {"/\\server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1623 // We should preserve the number of slashes after the colon for IE
1624 // compatibility, except when there is none, in which case we should
1625 // add one.
1626 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1627 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1628 // Three slashes should be non-UNC, even if there is no drive spec (IE
1629 // does this, which makes the resulting request invalid).
1630 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(), Component(7, 12)},
1631 // TODO(brettw) we should probably fail for invalid host names, which
1632 // would change the expected result on this test. We also currently allow
1633 // colon even though it's probably invalid, because its currently the
1634 // "natural" result of the way the canonicalizer is written. There doesn't
1635 // seem to be a strong argument for why allowing it here would be bad, so
1636 // we just tolerate it and the load will fail later.
1637 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, Component(7, 2), Component(9, 16)},
1638 {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5), Component(12, 8)},
1639 // Make sure relative paths can't go above the "C:"
1640 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, Component(), Component(7, 12)},
1641 // Busted refs shouldn't make the whole thing fail.
1642 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, Component(), Component(7, 8)},
1643 #else
1644 // Unix-style paths
1645 {"file:///home/me", "file:///home/me", true, Component(), Component(7, 8)},
1646 // Windowsy ones should get still treated as Unix-style.
1647 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(), Component(7, 16)},
1648 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, Component(), Component(7, 19)},
1649 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
1650 {"//", "file:///", true, Component(), Component(7, 1)},
1651 {"///", "file:///", true, Component(), Component(7, 1)},
1652 {"///test", "file:///test", true, Component(), Component(7, 5)},
1653 {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)},
1654 {"file://localhost", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1655 {"file://localhost/", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1656 {"file://localhost/test", "file://localhost/test", true, Component(7, 9), Component(16, 5)},
1657 #endif // _WIN32
1660 for (size_t i = 0; i < arraysize(cases); i++) {
1661 int url_len = static_cast<int>(strlen(cases[i].input));
1662 Parsed parsed;
1663 ParseFileURL(cases[i].input, url_len, &parsed);
1665 Parsed out_parsed;
1666 std::string out_str;
1667 StdStringCanonOutput output(&out_str);
1668 bool success = CanonicalizeFileURL(cases[i].input, url_len, parsed, NULL,
1669 &output, &out_parsed);
1670 output.Complete();
1672 EXPECT_EQ(cases[i].expected_success, success);
1673 EXPECT_EQ(cases[i].expected, out_str);
1675 // Make sure the spec was properly identified, the file canonicalizer has
1676 // different code for writing the spec.
1677 EXPECT_EQ(0, out_parsed.scheme.begin);
1678 EXPECT_EQ(4, out_parsed.scheme.len);
1680 EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
1681 EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
1683 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1684 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1688 TEST(URLCanonTest, CanonicalizeFileSystemURL) {
1689 struct URLCase {
1690 const char* input;
1691 const char* expected;
1692 bool expected_success;
1693 } cases[] = {
1694 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1695 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1696 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1697 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1698 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1699 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1700 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1703 for (size_t i = 0; i < arraysize(cases); i++) {
1704 int url_len = static_cast<int>(strlen(cases[i].input));
1705 Parsed parsed;
1706 ParseFileSystemURL(cases[i].input, url_len, &parsed);
1708 Parsed out_parsed;
1709 std::string out_str;
1710 StdStringCanonOutput output(&out_str);
1711 bool success = CanonicalizeFileSystemURL(cases[i].input, url_len, parsed,
1712 NULL, &output, &out_parsed);
1713 output.Complete();
1715 EXPECT_EQ(cases[i].expected_success, success);
1716 EXPECT_EQ(cases[i].expected, out_str);
1718 // Make sure the spec was properly identified, the filesystem canonicalizer
1719 // has different code for writing the spec.
1720 EXPECT_EQ(0, out_parsed.scheme.begin);
1721 EXPECT_EQ(10, out_parsed.scheme.len);
1722 if (success)
1723 EXPECT_GT(out_parsed.path.len, 0);
1727 TEST(URLCanonTest, CanonicalizePathURL) {
1728 // Path URLs should get canonicalized schemes but nothing else.
1729 struct PathCase {
1730 const char* input;
1731 const char* expected;
1732 } path_cases[] = {
1733 {"javascript:", "javascript:"},
1734 {"JavaScript:Foo", "javascript:Foo"},
1735 {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1738 for (size_t i = 0; i < arraysize(path_cases); i++) {
1739 int url_len = static_cast<int>(strlen(path_cases[i].input));
1740 Parsed parsed;
1741 ParsePathURL(path_cases[i].input, url_len, true, &parsed);
1743 Parsed out_parsed;
1744 std::string out_str;
1745 StdStringCanonOutput output(&out_str);
1746 bool success = CanonicalizePathURL(path_cases[i].input, url_len, parsed,
1747 &output, &out_parsed);
1748 output.Complete();
1750 EXPECT_TRUE(success);
1751 EXPECT_EQ(path_cases[i].expected, out_str);
1753 EXPECT_EQ(0, out_parsed.host.begin);
1754 EXPECT_EQ(-1, out_parsed.host.len);
1756 // When we end with a colon at the end, there should be no path.
1757 if (path_cases[i].input[url_len - 1] == ':') {
1758 EXPECT_EQ(0, out_parsed.GetContent().begin);
1759 EXPECT_EQ(-1, out_parsed.GetContent().len);
1764 TEST(URLCanonTest, CanonicalizeMailtoURL) {
1765 struct URLCase {
1766 const char* input;
1767 const char* expected;
1768 bool expected_success;
1769 Component expected_path;
1770 Component expected_query;
1771 } cases[] = {
1772 {"mailto:addr1", "mailto:addr1", true, Component(7, 5), Component()},
1773 {"mailto:addr1@foo.com", "mailto:addr1@foo.com", true, Component(7, 13), Component()},
1774 // Trailing whitespace is stripped.
1775 {"MaIlTo:addr1 \t ", "mailto:addr1", true, Component(7, 5), Component()},
1776 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, Component(7, 5), Component(13,6)},
1777 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, Component(7, 11), Component()},
1778 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, Component(7, 12), Component()},
1779 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, Component(7, 13), Component()},
1780 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, Component(7, 12), Component()},
1781 // Null character should be escaped to %00
1782 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, Component(7, 13), Component(21, 3)},
1783 // Invalid -- UTF-8 encoded surrogate value.
1784 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, Component(7, 9), Component()},
1785 {"mailto:addr1?", "mailto:addr1?", true, Component(7, 5), Component(13, 0)},
1788 // Define outside of loop to catch bugs where components aren't reset
1789 Parsed parsed;
1790 Parsed out_parsed;
1792 for (size_t i = 0; i < arraysize(cases); i++) {
1793 int url_len = static_cast<int>(strlen(cases[i].input));
1794 if (i == 8) {
1795 // The 9th test case purposely has a '\0' in it -- don't count it
1796 // as the string terminator.
1797 url_len = 22;
1799 ParseMailtoURL(cases[i].input, url_len, &parsed);
1801 std::string out_str;
1802 StdStringCanonOutput output(&out_str);
1803 bool success = CanonicalizeMailtoURL(cases[i].input, url_len, parsed,
1804 &output, &out_parsed);
1805 output.Complete();
1807 EXPECT_EQ(cases[i].expected_success, success);
1808 EXPECT_EQ(cases[i].expected, out_str);
1810 // Make sure the spec was properly identified
1811 EXPECT_EQ(0, out_parsed.scheme.begin);
1812 EXPECT_EQ(6, out_parsed.scheme.len);
1814 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1815 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1817 EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
1818 EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
1822 #ifndef WIN32
1824 TEST(URLCanonTest, _itoa_s) {
1825 // We fill the buffer with 0xff to ensure that it's getting properly
1826 // null-terminated. We also allocate one byte more than what we tell
1827 // _itoa_s about, and ensure that the extra byte is untouched.
1828 char buf[6];
1829 memset(buf, 0xff, sizeof(buf));
1830 EXPECT_EQ(0, _itoa_s(12, buf, sizeof(buf) - 1, 10));
1831 EXPECT_STREQ("12", buf);
1832 EXPECT_EQ('\xFF', buf[3]);
1834 // Test the edge cases - exactly the buffer size and one over
1835 memset(buf, 0xff, sizeof(buf));
1836 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 10));
1837 EXPECT_STREQ("1234", buf);
1838 EXPECT_EQ('\xFF', buf[5]);
1840 memset(buf, 0xff, sizeof(buf));
1841 EXPECT_EQ(EINVAL, _itoa_s(12345, buf, sizeof(buf) - 1, 10));
1842 EXPECT_EQ('\xFF', buf[5]); // should never write to this location
1844 // Test the template overload (note that this will see the full buffer)
1845 memset(buf, 0xff, sizeof(buf));
1846 EXPECT_EQ(0, _itoa_s(12, buf, 10));
1847 EXPECT_STREQ("12", buf);
1848 EXPECT_EQ('\xFF', buf[3]);
1850 memset(buf, 0xff, sizeof(buf));
1851 EXPECT_EQ(0, _itoa_s(12345, buf, 10));
1852 EXPECT_STREQ("12345", buf);
1854 EXPECT_EQ(EINVAL, _itoa_s(123456, buf, 10));
1856 // Test that radix 16 is supported.
1857 memset(buf, 0xff, sizeof(buf));
1858 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 16));
1859 EXPECT_STREQ("4d2", buf);
1860 EXPECT_EQ('\xFF', buf[5]);
1863 TEST(URLCanonTest, _itow_s) {
1864 // We fill the buffer with 0xff to ensure that it's getting properly
1865 // null-terminated. We also allocate one byte more than what we tell
1866 // _itoa_s about, and ensure that the extra byte is untouched.
1867 base::char16 buf[6];
1868 const char fill_mem = 0xff;
1869 const base::char16 fill_char = 0xffff;
1870 memset(buf, fill_mem, sizeof(buf));
1871 EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
1872 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
1873 EXPECT_EQ(fill_char, buf[3]);
1875 // Test the edge cases - exactly the buffer size and one over
1876 EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
1877 EXPECT_EQ(WStringToUTF16(L"1234"), base::string16(buf));
1878 EXPECT_EQ(fill_char, buf[5]);
1880 memset(buf, fill_mem, sizeof(buf));
1881 EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
1882 EXPECT_EQ(fill_char, buf[5]); // should never write to this location
1884 // Test the template overload (note that this will see the full buffer)
1885 memset(buf, fill_mem, sizeof(buf));
1886 EXPECT_EQ(0, _itow_s(12, buf, 10));
1887 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
1888 EXPECT_EQ(fill_char, buf[3]);
1890 memset(buf, fill_mem, sizeof(buf));
1891 EXPECT_EQ(0, _itow_s(12345, buf, 10));
1892 EXPECT_EQ(WStringToUTF16(L"12345"), base::string16(buf));
1894 EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10));
1897 #endif // !WIN32
1899 // Returns true if the given two structures are the same.
1900 static bool ParsedIsEqual(const Parsed& a, const Parsed& b) {
1901 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
1902 a.username.begin == b.username.begin && a.username.len == b.username.len &&
1903 a.password.begin == b.password.begin && a.password.len == b.password.len &&
1904 a.host.begin == b.host.begin && a.host.len == b.host.len &&
1905 a.port.begin == b.port.begin && a.port.len == b.port.len &&
1906 a.path.begin == b.path.begin && a.path.len == b.path.len &&
1907 a.query.begin == b.query.begin && a.query.len == b.query.len &&
1908 a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
1911 TEST(URLCanonTest, ResolveRelativeURL) {
1912 struct RelativeCase {
1913 const char* base; // Input base URL: MUST BE CANONICAL
1914 bool is_base_hier; // Is the base URL hierarchical
1915 bool is_base_file; // Tells us if the base is a file URL.
1916 const char* test; // Input URL to test against.
1917 bool succeed_relative; // Whether we expect IsRelativeURL to succeed
1918 bool is_rel; // Whether we expect |test| to be relative or not.
1919 bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed.
1920 const char* resolved; // What we expect in the result when resolving.
1921 } rel_cases[] = {
1922 // Basic absolute input.
1923 {"http://host/a", true, false, "http://another/", true, false, false, NULL},
1924 {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
1925 // Empty relative URLs should only remove the ref part of the URL,
1926 // leaving the rest unchanged.
1927 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
1928 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
1929 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
1930 // Spaces at the ends of the relative path should be ignored.
1931 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
1932 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
1933 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
1934 // Matching schemes without two slashes are treated as relative.
1935 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
1936 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
1937 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
1938 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
1939 // Nonmatching schemes are absolute.
1940 {"http://host/a", true, false, "https:host2", true, false, false, NULL},
1941 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
1942 // Absolute path input
1943 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
1944 {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
1945 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
1946 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
1947 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
1948 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
1949 // Relative path input
1950 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
1951 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
1952 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
1953 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
1954 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
1955 {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
1956 {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
1957 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
1958 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
1959 // Query input
1960 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
1961 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
1962 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
1963 // Ref input
1964 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
1965 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
1966 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
1967 // Non-hierarchical base: no relative handling. Relative input should
1968 // error, and if a scheme is present, it should be treated as absolute.
1969 {"data:foobar", false, false, "baz.html", false, false, false, NULL},
1970 {"data:foobar", false, false, "data:baz", true, false, false, NULL},
1971 {"data:foobar", false, false, "data:/base", true, false, false, NULL},
1972 // Non-hierarchical base: absolute input should succeed.
1973 {"data:foobar", false, false, "http://host/", true, false, false, NULL},
1974 {"data:foobar", false, false, "http:host", true, false, false, NULL},
1975 // Invalid schemes should be treated as relative.
1976 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
1977 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
1978 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
1979 {"data:asdf", false, false, ":foo", false, false, false, NULL},
1980 {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL},
1981 // We should treat semicolons like any other character in URL resolving
1982 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
1983 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
1984 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
1985 // Relative URLs can also be written as "//foo/bar" which is relative to
1986 // the scheme. In this case, it would take the old scheme, so for http
1987 // the example would resolve to "http://foo/bar".
1988 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
1989 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
1990 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
1991 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
1992 {"http://host/a", true, false, "//", true, true, false, "http:"},
1993 // IE will also allow one or the other to be a backslash to get the same
1994 // behavior.
1995 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
1996 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
1997 #ifdef WIN32
1998 // Resolving against Windows file base URLs.
1999 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
2000 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
2001 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
2002 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
2003 // But two backslashes on Windows should be UNC so should be treated
2004 // as absolute.
2005 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
2006 // IE doesn't support drive specs starting with two slashes. It fails
2007 // immediately and doesn't even try to load. We fix it up to either
2008 // an absolute path or UNC depending on what it looks like.
2009 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
2010 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
2011 // Windows drive specs should be allowed and treated as absolute.
2012 {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
2013 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
2014 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
2015 // Relative paths with drive letters should be allowed when the base is
2016 // also a file.
2017 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2018 // Treat absolute paths as being off of the drive.
2019 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2020 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2021 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2022 // On Windows, two slashes without a drive letter when the base is a file
2023 // means that the path is UNC.
2024 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2025 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2026 #else
2027 // On Unix we fall back to relative behavior since there's nothing else
2028 // reasonable to do.
2029 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2030 #endif
2031 // Even on Windows, we don't allow relative drive specs when the base
2032 // is not file.
2033 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2034 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
2035 // Ensure that ports aren't allowed for hosts relative to a file url.
2036 // Although the result string shows a host:port portion, the call to
2037 // resolve the relative URL returns false, indicating parse failure,
2038 // which is what is required.
2039 {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
2040 // Filesystem URL tests; filesystem URLs are only valid and relative if
2041 // they have no scheme, e.g. "./index.html". There's no valid equivalent
2042 // to http:index.html.
2043 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2044 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
2045 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
2046 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2047 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2048 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2049 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
2050 // Absolute URLs are still not relative to a non-standard base URL.
2051 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2052 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2055 for (size_t i = 0; i < arraysize(rel_cases); i++) {
2056 const RelativeCase& cur_case = rel_cases[i];
2058 Parsed parsed;
2059 int base_len = static_cast<int>(strlen(cur_case.base));
2060 if (cur_case.is_base_file)
2061 ParseFileURL(cur_case.base, base_len, &parsed);
2062 else if (cur_case.is_base_hier)
2063 ParseStandardURL(cur_case.base, base_len, &parsed);
2064 else
2065 ParsePathURL(cur_case.base, base_len, false, &parsed);
2067 // First see if it is relative.
2068 int test_len = static_cast<int>(strlen(cur_case.test));
2069 bool is_relative;
2070 Component relative_component;
2071 bool succeed_is_rel = IsRelativeURL(
2072 cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
2073 &is_relative, &relative_component);
2075 EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
2076 "succeed is rel failure on " << cur_case.test;
2077 EXPECT_EQ(cur_case.is_rel, is_relative) <<
2078 "is rel failure on " << cur_case.test;
2079 // Now resolve it.
2080 if (succeed_is_rel && is_relative && cur_case.is_rel) {
2081 std::string resolved;
2082 StdStringCanonOutput output(&resolved);
2083 Parsed resolved_parsed;
2085 bool succeed_resolve = ResolveRelativeURL(
2086 cur_case.base, parsed, cur_case.is_base_file, cur_case.test,
2087 relative_component, NULL, &output, &resolved_parsed);
2088 output.Complete();
2090 EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
2091 EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
2093 // Verify that the output parsed structure is the same as parsing a
2094 // the URL freshly.
2095 Parsed ref_parsed;
2096 int resolved_len = static_cast<int>(resolved.size());
2097 if (cur_case.is_base_file) {
2098 ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
2099 } else if (cur_case.is_base_hier) {
2100 ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
2101 } else {
2102 ParsePathURL(resolved.c_str(), resolved_len, false, &ref_parsed);
2104 EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
2109 // It used to be the case that when we did a replacement with a long buffer of
2110 // UTF-16 characters, we would get invalid data in the URL. This is because the
2111 // buffer that it used to hold the UTF-8 data was resized, while some pointers
2112 // were still kept to the old buffer that was removed.
2113 TEST(URLCanonTest, ReplacementOverflow) {
2114 const char src[] = "file:///C:/foo/bar";
2115 int src_len = static_cast<int>(strlen(src));
2116 Parsed parsed;
2117 ParseFileURL(src, src_len, &parsed);
2119 // Override two components, the path with something short, and the query with
2120 // something long enough to trigger the bug.
2121 Replacements<base::char16> repl;
2122 base::string16 new_query;
2123 for (int i = 0; i < 4800; i++)
2124 new_query.push_back('a');
2126 base::string16 new_path(WStringToUTF16(L"/foo"));
2127 repl.SetPath(new_path.c_str(), Component(0, 4));
2128 repl.SetQuery(new_query.c_str(),
2129 Component(0, static_cast<int>(new_query.length())));
2131 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2132 // standard URLs, file URLs, etc, since they will go to the same replacement
2133 // function that was buggy.
2134 Parsed repl_parsed;
2135 std::string repl_str;
2136 StdStringCanonOutput repl_output(&repl_str);
2137 ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
2138 repl_output.Complete();
2140 // Generate the expected string and check.
2141 std::string expected("file:///foo?");
2142 for (size_t i = 0; i < new_query.length(); i++)
2143 expected.push_back('a');
2144 EXPECT_TRUE(expected == repl_str);
2147 } // namespace url