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.
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/icu/source/common/unicode/ucnv.h"
9 #include "url/url_canon.h"
10 #include "url/url_canon_icu.h"
11 #include "url/url_canon_internal.h"
12 #include "url/url_canon_stdstring.h"
13 #include "url/url_parse.h"
14 #include "url/url_test_utils.h"
16 // Some implementations of base/basictypes.h may define ARRAYSIZE.
17 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
18 // which is in our version of basictypes.h.
20 #define ARRAYSIZE ARRAYSIZE_UNSAFE
25 using test_utils::WStringToUTF16
;
26 using test_utils::ConvertUTF8ToUTF16
;
27 using test_utils::ConvertUTF16ToUTF8
;
31 struct ComponentCase
{
34 Component expected_component
;
35 bool expected_success
;
38 // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
39 // treat each input as optional, and will only try processing if non-NULL.
40 // The output is always 8-bit.
41 struct DualComponentCase
{
43 const wchar_t* input16
;
45 Component expected_component
;
46 bool expected_success
;
49 // Test cases for CanonicalizeIPAddress(). The inputs are identical to
50 // DualComponentCase, but the output has extra CanonHostInfo fields.
51 struct IPAddressCase
{
53 const wchar_t* input16
;
55 Component expected_component
;
57 // CanonHostInfo fields, for verbose output.
58 CanonHostInfo::Family expected_family
;
59 int expected_num_ipv4_components
;
60 const char* expected_address_hex
; // Two hex chars per IP address byte.
63 std::string
BytesToHexString(unsigned char bytes
[16], int length
) {
64 EXPECT_TRUE(length
== 0 || length
== 4 || length
== 16)
65 << "Bad IP address length: " << length
;
67 for (int i
= 0; i
< length
; ++i
) {
68 result
.push_back(kHexCharLookup
[(bytes
[i
] >> 4) & 0xf]);
69 result
.push_back(kHexCharLookup
[bytes
[i
] & 0xf]);
87 // Wrapper around a UConverter object that managers creation and destruction.
90 explicit UConvScoper(const char* charset_name
) {
91 UErrorCode err
= U_ZERO_ERROR
;
92 converter_
= ucnv_open(charset_name
, &err
);
97 ucnv_close(converter_
);
100 // Returns the converter object, may be NULL.
101 UConverter
* converter() const { return converter_
; }
104 UConverter
* converter_
;
107 // Magic string used in the replacements code that tells SetupReplComp to
108 // call the clear function.
109 const char kDeleteComp
[] = "|";
111 // Sets up a replacement for a single component. This is given pointers to
112 // the set and clear function for the component being replaced, and will
113 // either set the component (if it exists) or clear it (if the replacement
114 // string matches kDeleteComp).
116 // This template is currently used only for the 8-bit case, and the strlen
117 // causes it to fail in other cases. It is left a template in case we have
118 // tests for wide replacements.
119 template<typename CHAR
>
121 void (Replacements
<CHAR
>::*set
)(const CHAR
*, const Component
&),
122 void (Replacements
<CHAR
>::*clear
)(),
123 Replacements
<CHAR
>* rep
,
125 if (str
&& str
[0] == kDeleteComp
[0]) {
128 (rep
->*set
)(str
, Component(0, static_cast<int>(strlen(str
))));
134 TEST(URLCanonTest
, DoAppendUTF8
) {
139 // Valid code points.
142 {0x20AC, "\xE2\x82\xAC"},
143 {0x24B62, "\xF0\xA4\xAD\xA2"},
144 {0x10FFFF, "\xF4\x8F\xBF\xBF"},
147 for (size_t i
= 0; i
< ARRAYSIZE(utf_cases
); i
++) {
149 StdStringCanonOutput
output(&out_str
);
150 AppendUTF8Value(utf_cases
[i
].input
, &output
);
152 EXPECT_EQ(utf_cases
[i
].output
, out_str
);
156 // TODO(mattm): Can't run this in debug mode for now, since the DCHECK will
157 // cause the Chromium stacktrace dialog to appear and hang the test.
158 // See http://crbug.com/49580.
159 #if defined(GTEST_HAS_DEATH_TEST) && defined(NDEBUG)
160 TEST(URLCanonTest
, DoAppendUTF8Invalid
) {
162 StdStringCanonOutput
output(&out_str
);
163 // Invalid code point (too large).
165 AppendUTF8Value(0x110000, &output
);
167 EXPECT_EQ("", out_str
);
172 TEST(URLCanonTest
, UTF
) {
173 // Low-level test that we handle reading, canonicalization, and writing
174 // UTF-8/UTF-16 strings properly.
177 const wchar_t* input16
;
178 bool expected_success
;
181 // Valid canonical input should get passed through & escaped.
182 {"\xe4\xbd\xa0\xe5\xa5\xbd", L
"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
183 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
184 {"\xF0\x90\x8C\x80", L
"\xd800\xdf00", true, "%F0%90%8C%80"},
185 // Non-shortest-form UTF-8 are invalid. The bad char should be replaced
186 // with the invalid character (EF BF DB in UTF-8).
187 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL
, false, "%EF%BF%BD%E5%A5%BD"},
188 // Invalid UTF-8 sequences should be marked as invalid (the first
189 // sequence is truncated).
190 {"\xe4\xa0\xe5\xa5\xbd", L
"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
191 // Character going off the end.
192 {"\xe4\xbd\xa0\xe5\xa5", L
"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
193 // ...same with low surrogates with no high surrogate.
194 {"\xed\xb0\x80", L
"\xdc00", false, "%EF%BF%BD"},
195 // Test a UTF-8 encoded surrogate value is marked as invalid.
197 {"\xed\xa0\x80", NULL
, false, "%EF%BF%BD"},
201 for (size_t i
= 0; i
< ARRAYSIZE(utf_cases
); i
++) {
202 if (utf_cases
[i
].input8
) {
204 StdStringCanonOutput
output(&out_str
);
206 int input_len
= static_cast<int>(strlen(utf_cases
[i
].input8
));
208 for (int ch
= 0; ch
< input_len
; ch
++) {
209 success
&= AppendUTF8EscapedChar(utf_cases
[i
].input8
, &ch
, input_len
,
213 EXPECT_EQ(utf_cases
[i
].expected_success
, success
);
214 EXPECT_EQ(std::string(utf_cases
[i
].output
), out_str
);
216 if (utf_cases
[i
].input16
) {
218 StdStringCanonOutput
output(&out_str
);
220 base::string16
input_str(WStringToUTF16(utf_cases
[i
].input16
));
221 int input_len
= static_cast<int>(input_str
.length());
223 for (int ch
= 0; ch
< input_len
; ch
++) {
224 success
&= AppendUTF8EscapedChar(input_str
.c_str(), &ch
, input_len
,
228 EXPECT_EQ(utf_cases
[i
].expected_success
, success
);
229 EXPECT_EQ(std::string(utf_cases
[i
].output
), out_str
);
232 if (utf_cases
[i
].input8
&& utf_cases
[i
].input16
&&
233 utf_cases
[i
].expected_success
) {
234 // Check that the UTF-8 and UTF-16 inputs are equivalent.
237 std::string
input8_str(utf_cases
[i
].input8
);
238 base::string16
input16_str(WStringToUTF16(utf_cases
[i
].input16
));
239 EXPECT_EQ(input8_str
, ConvertUTF16ToUTF8(input16_str
));
242 EXPECT_EQ(input16_str
, ConvertUTF8ToUTF16(input8_str
));
247 TEST(URLCanonTest
, ICUCharsetConverter
) {
249 const wchar_t* input
;
250 const char* encoding
;
251 const char* expected
;
254 {L
"Hello, world", "utf-8", "Hello, world"},
255 {L
"\x4f60\x597d", "utf-8", "\xe4\xbd\xa0\xe5\xa5\xbd"},
257 {L
"!\xd800\xdf00!", "utf-8", "!\xf0\x90\x8c\x80!"},
259 {L
"\x4f60\x597d", "big5", "\xa7\x41\xa6\x6e"},
260 // Unrepresentable character in the destination set.
261 {L
"hello\x4f60\x06de\x597dworld", "big5", "hello\xa7\x41%26%231758%3B\xa6\x6eworld"},
264 for (size_t i
= 0; i
< ARRAYSIZE(icu_cases
); i
++) {
265 UConvScoper
conv(icu_cases
[i
].encoding
);
266 ASSERT_TRUE(conv
.converter() != NULL
);
267 ICUCharsetConverter
converter(conv
.converter());
270 StdStringCanonOutput
output(&str
);
272 base::string16
input_str(WStringToUTF16(icu_cases
[i
].input
));
273 int input_len
= static_cast<int>(input_str
.length());
274 converter
.ConvertFromUTF16(input_str
.c_str(), input_len
, &output
);
277 EXPECT_STREQ(icu_cases
[i
].expected
, str
.c_str());
280 // Test string sizes around the resize boundary for the output to make sure
281 // the converter resizes as needed.
282 const int static_size
= 16;
283 UConvScoper
conv("utf-8");
284 ASSERT_TRUE(conv
.converter());
285 ICUCharsetConverter
converter(conv
.converter());
286 for (int i
= static_size
- 2; i
<= static_size
+ 2; i
++) {
287 // Make a string with the appropriate length.
288 base::string16 input
;
289 for (int ch
= 0; ch
< i
; ch
++)
290 input
.push_back('a');
292 RawCanonOutput
<static_size
> output
;
293 converter
.ConvertFromUTF16(input
.c_str(), static_cast<int>(input
.length()),
295 EXPECT_EQ(input
.length(), static_cast<size_t>(output
.length()));
299 TEST(URLCanonTest
, Scheme
) {
300 // Here, we're mostly testing that unusual characters are handled properly.
301 // The canonicalizer doesn't do any parsing or whitespace detection. It will
302 // also do its best on error, and will escape funny sequences (these won't be
303 // valid schemes and it will return error).
305 // Note that the canonicalizer will append a colon to the output to separate
306 // out the rest of the URL, which is not present in the input. We check,
307 // however, that the output range includes everything but the colon.
308 ComponentCase scheme_cases
[] = {
309 {"http", "http:", Component(0, 4), true},
310 {"HTTP", "http:", Component(0, 4), true},
311 {" HTTP ", "%20http%20:", Component(0, 10), false},
312 {"htt: ", "htt%3A%20:", Component(0, 9), false},
313 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false},
314 // Don't re-escape something already escaped. Note that it will
315 // "canonicalize" the 'A' to 'a', but that's OK.
316 {"ht%3Atp", "ht%3atp:", Component(0, 7), false},
321 for (size_t i
= 0; i
< arraysize(scheme_cases
); i
++) {
322 int url_len
= static_cast<int>(strlen(scheme_cases
[i
].input
));
323 Component
in_comp(0, url_len
);
327 StdStringCanonOutput
output1(&out_str
);
328 bool success
= CanonicalizeScheme(scheme_cases
[i
].input
, in_comp
, &output1
,
332 EXPECT_EQ(scheme_cases
[i
].expected_success
, success
);
333 EXPECT_EQ(std::string(scheme_cases
[i
].expected
), out_str
);
334 EXPECT_EQ(scheme_cases
[i
].expected_component
.begin
, out_comp
.begin
);
335 EXPECT_EQ(scheme_cases
[i
].expected_component
.len
, out_comp
.len
);
337 // Now try the wide version
339 StdStringCanonOutput
output2(&out_str
);
341 base::string16
wide_input(ConvertUTF8ToUTF16(scheme_cases
[i
].input
));
342 in_comp
.len
= static_cast<int>(wide_input
.length());
343 success
= CanonicalizeScheme(wide_input
.c_str(), in_comp
, &output2
,
347 EXPECT_EQ(scheme_cases
[i
].expected_success
, success
);
348 EXPECT_EQ(std::string(scheme_cases
[i
].expected
), out_str
);
349 EXPECT_EQ(scheme_cases
[i
].expected_component
.begin
, out_comp
.begin
);
350 EXPECT_EQ(scheme_cases
[i
].expected_component
.len
, out_comp
.len
);
353 // Test the case where the scheme is declared nonexistant, it should be
354 // converted into an empty scheme.
357 StdStringCanonOutput
output(&out_str
);
359 EXPECT_TRUE(CanonicalizeScheme("", Component(0, -1), &output
, &out_comp
));
362 EXPECT_EQ(std::string(":"), out_str
);
363 EXPECT_EQ(0, out_comp
.begin
);
364 EXPECT_EQ(0, out_comp
.len
);
367 TEST(URLCanonTest
, Host
) {
368 IPAddressCase host_cases
[] = {
369 // Basic canonicalization, uppercase should be converted to lowercase.
370 {"GoOgLe.CoM", L
"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL
, -1, ""},
371 // Spaces and some other characters should be escaped.
372 {"Goo%20 goo%7C|.com", L
"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", Component(0, 22), CanonHostInfo::NEUTRAL
, -1, ""},
373 // Exciting different types of spaces!
374 {NULL
, L
"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL
, -1, ""},
375 // Other types of space (no-break, zero-width, zero-width-no-break) are
376 // name-prepped away to nothing.
377 {NULL
, L
"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL
, -1, ""},
378 // Ideographic full stop (full-width period for Chinese, etc.) should be
380 {NULL
, L
"www.foo\x3002" L
"bar.com", "www.foo.bar.com", Component(0, 15), CanonHostInfo::NEUTRAL
, -1, ""},
381 // Invalid unicode characters should fail...
382 // ...In wide input, ICU will barf and we'll end up with the input as
383 // escaped UTF-8 (the invalid character should be replaced with the
384 // replacement character).
385 {"\xef\xb7\x90zyx.com", L
"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN
, -1, ""},
386 // ...This is the same as previous but with with escaped.
387 {"%ef%b7%90zyx.com", L
"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN
, -1, ""},
388 // Test name prepping, fullwidth input should be converted to ASCII and NOT
389 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
390 {"\xef\xbc\xa7\xef\xbd\x8f.com", L
"\xff27\xff4f.com", "go.com", Component(0, 6), CanonHostInfo::NEUTRAL
, -1, ""},
391 // Test that fullwidth escaped values are properly name-prepped,
392 // then converted or rejected.
393 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
394 {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L
"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL
, -1, ""},
395 {"%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, ""},
396 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
397 {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L
"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN
, -1, ""},
398 {"%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, ""},
399 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
400 {"\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, ""},
401 // See http://unicode.org/cldr/utility/idna.jsp for other
402 // examples/experiments and http://goo.gl/7yG11o
403 // for the full list of characters handled differently by
404 // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
406 // 4 Deviation characters are mapped/ignored in UTS 46 transitional
407 // mechansm. UTS 46, table 4 row (g).
408 // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
409 // Otherwise, it'd be "xn--fuball-cta.de".
410 {"fu\xc3\x9f" "ball.de", L
"fu\x00df" L
"ball.de", "fussball.de",
411 Component(0, 11), CanonHostInfo::NEUTRAL
, -1, ""},
412 // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
413 // Otherwise, it'd be "xn--wxaijb9b".
414 {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L
"\x3c3\x3cc\x3bb\x3bf\x3c2",
415 "xn--wxaikc6b", Component(0, 12),
416 CanonHostInfo::NEUTRAL
, -1, ""},
417 // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
418 // handling as well as in IDNA 2003.
419 {"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L
"a\x200c" L
"b\x200d" L
"c", "abc",
420 Component(0, 3), CanonHostInfo::NEUTRAL
, -1, ""},
421 // ZWJ between Devanagari characters is still mapped away in UTS 46
422 // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
423 {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
424 L
"\x915\x94d\x200d\x91c", "xn--11bo0m",
425 Component(0, 10), CanonHostInfo::NEUTRAL
, -1, ""},
426 // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
427 // However, we do allow this at the moment because we don't use
428 // STD3 rules and canonicalize full-width ASCII to ASCII.
429 {"wow\xef\xbc\x81", L
"wow\xff01", "wow%21",
430 Component(0, 6), CanonHostInfo::NEUTRAL
, -1, ""},
431 // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
432 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
433 {"\xe2\x84\xb2oo", L
"\x2132oo", "%E2%84%B2oo",
434 Component(0, 11), CanonHostInfo::BROKEN
, -1, ""},
435 // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
436 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
437 {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L
"\xd87e\xdc68\x59fb.cn",
438 "%F0%AF%A1%A8%E5%A7%BB.cn",
439 Component(0, 24), CanonHostInfo::BROKEN
, -1, ""},
440 // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
441 {"M\xc3\x9cNCHEN", L
"M\xdcNCHEN", "xn--mnchen-3ya",
442 Component(0, 14), CanonHostInfo::NEUTRAL
, -1, ""},
443 // Symbol/punctuations are allowed in IDNA 2003/UTS46.
444 // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
445 {"\xe2\x99\xa5ny.us", L
"\x2665ny.us", "xn--ny-s0x.us",
446 Component(0, 13), CanonHostInfo::NEUTRAL
, -1, ""},
447 // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
448 // We used to allow it because we passed through unassigned code points.
449 {"\xf0\x91\x80\x93.com", L
"\xd804\xdc13.com", "xn--n00d.com",
450 Component(0, 12), CanonHostInfo::NEUTRAL
, -1, ""},
451 // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
452 // Used to be allowed in INDA 2003.
453 {"\xd8\x82.eg", L
"\x602.eg", "%D8%82.eg",
454 Component(0, 9), CanonHostInfo::BROKEN
, -1, ""},
455 // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
456 // on Unicode 3.2). We did allow it in the past because we let unassigned
457 // code point pass. We continue to allow it even though it's a
458 // "punctuation and symbol" blocked in IDNA 2008.
459 // UTS 46 table 4, row (j)
460 {"\xe2\x82\xb7.com", L
"\x20b7.com", "xn--wzg.com",
461 Component(0, 11), CanonHostInfo::NEUTRAL
, -1, ""},
462 // Maps uppercase letters to lower case letters.
463 // In IDNA 2003, it's allowed without case-folding
464 // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
465 // (added in Unicode 4.1). UTS 46 table 4 row (k)
466 {"bc\xc8\xba.com", L
"bc\x23a.com", "xn--bc-is1a.com",
467 Component(0, 15), CanonHostInfo::NEUTRAL
, -1, ""},
469 // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
470 // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
471 {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
472 L
"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
473 Component(0, 13), CanonHostInfo::NEUTRAL
, -1, ""},
474 // Disallowed in both IDNA 2003 and 2008 with BiDi check.
475 // Labels starting with a RTL character cannot end with a LTR character.
476 {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L
"\x62c\x627\x631xyz",
477 "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21),
478 CanonHostInfo::BROKEN
, -1, ""},
479 // Labels starting with a RTL character can end with BC=EN (European
480 // number). Disallowed in IDNA 2003 but now allowed.
481 {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L
"\x62c\x627\x631" L
"2",
482 "xn--2-ymcov", Component(0, 11),
483 CanonHostInfo::NEUTRAL
, -1, ""},
484 // Labels starting with a RTL character cannot have "L" characters
485 // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
486 {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L
"\x62c\x627\x631xy2",
487 "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21),
488 CanonHostInfo::BROKEN
, -1, ""},
489 // Labels starting with a RTL character can end with BC=AN (Arabic number)
490 // Disallowed in IDNA 2003, but now allowed.
491 {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L
"\x62c\x627\x631\x662",
492 "xn--mgbjq0r", Component(0, 11),
493 CanonHostInfo::NEUTRAL
, -1, ""},
494 // Labels starting with a RTL character cannot have "L" characters
495 // even if it ends with an BC=AN (Arabic number).
496 // Disallowed in both IDNA 2003/2008.
497 {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L
"\x62c\x627\x631xy\x662",
498 "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26),
499 CanonHostInfo::BROKEN
, -1, ""},
500 // Labels starting with a RTL character cannot mix BC=EN and BC=AN
501 {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L
"\x62c\x627\x631xy2\x662",
502 "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27),
503 CanonHostInfo::BROKEN
, -1, ""},
504 // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
505 {"\xe2\x83\x8f.com", L
"\x20cf.com", "%E2%83%8F.com",
506 Component(0, 13), CanonHostInfo::BROKEN
, -1, ""},
507 // U+0080 is not allowed.
508 {"\xc2\x80.com", L
"\x80.com", "%C2%80.com",
509 Component(0, 10), CanonHostInfo::BROKEN
, -1, ""},
510 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
511 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
512 // UTF-8 (wide case). The output should be equivalent to the true wide
513 // character input above).
514 {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
515 L
"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
516 Component(0, 14), CanonHostInfo::NEUTRAL
, -1, ""},
517 // Invalid escaped characters should fail and the percents should be
519 {"%zz%66%a", L
"%zz%66%a", "%25zzf%25a", Component(0, 10),
520 CanonHostInfo::BROKEN
, -1, ""},
521 // If we get an invalid character that has been escaped.
522 {"%25", L
"%25", "%25", Component(0, 3),
523 CanonHostInfo::BROKEN
, -1, ""},
524 {"hello%00", L
"hello%00", "hello%00", Component(0, 8),
525 CanonHostInfo::BROKEN
, -1, ""},
526 // Escaped numbers should be treated like IP addresses if they are.
527 {"%30%78%63%30%2e%30%32%35%30.01", L
"%30%78%63%30%2e%30%32%35%30.01",
528 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 3,
530 {"%30%78%63%30%2e%30%32%35%30.01%2e", L
"%30%78%63%30%2e%30%32%35%30.01%2e",
531 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 3,
533 // Invalid escaping should trigger the regular host error handling.
534 {"%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, ""},
535 // Something that isn't exactly an IP should get treated as a host and
537 {"192.168.0.1 hello", L
"192.168.0.1 hello", "192.168.0.1%20hello", Component(0, 19), CanonHostInfo::NEUTRAL
, -1, ""},
538 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
539 // These are "0Xc0.0250.01" in fullwidth.
540 {"\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"},
541 // Broken IP addresses get marked as such.
542 {"192.168.0.257", L
"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN
, -1, ""},
543 {"[google.com]", L
"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN
, -1, ""},
544 // Cyrillic letter followed by '(' should return punycode for '(' escaped
545 // before punycode string was created. I.e.
546 // if '(' is escaped after punycode is created we would get xn--%28-8tb
548 {"\xd1\x82(", L
"\x0442(", "xn--%28-7ed", Component(0, 11),
549 CanonHostInfo::NEUTRAL
, -1, ""},
550 // Address with all hexidecimal characters with leading number of 1<<32
551 // or greater and should return NEUTRAL rather than BROKEN if not all
552 // components are numbers.
553 {"12345678912345.de", L
"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL
, -1, ""},
554 {"1.12345678912345.de", L
"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL
, -1, ""},
555 {"12345678912345.12345678912345.de", L
"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL
, -1, ""},
556 {"1.2.0xB3A73CE5B59.de", L
"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL
, -1, ""},
557 {"12345678912345.0xde", L
"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN
, -1, ""},
560 // CanonicalizeHost() non-verbose.
562 for (size_t i
= 0; i
< arraysize(host_cases
); i
++) {
564 if (host_cases
[i
].input8
) {
565 int host_len
= static_cast<int>(strlen(host_cases
[i
].input8
));
566 Component
in_comp(0, host_len
);
570 StdStringCanonOutput
output(&out_str
);
572 bool success
= CanonicalizeHost(host_cases
[i
].input8
, in_comp
, &output
,
576 EXPECT_EQ(host_cases
[i
].expected_family
!= CanonHostInfo::BROKEN
,
577 success
) << "for input: " << host_cases
[i
].input8
;
578 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
) <<
579 "for input: " << host_cases
[i
].input8
;
580 EXPECT_EQ(host_cases
[i
].expected_component
.begin
, out_comp
.begin
) <<
581 "for input: " << host_cases
[i
].input8
;
582 EXPECT_EQ(host_cases
[i
].expected_component
.len
, out_comp
.len
) <<
583 "for input: " << host_cases
[i
].input8
;
587 if (host_cases
[i
].input16
) {
588 base::string16
input16(WStringToUTF16(host_cases
[i
].input16
));
589 int host_len
= static_cast<int>(input16
.length());
590 Component
in_comp(0, host_len
);
594 StdStringCanonOutput
output(&out_str
);
596 bool success
= CanonicalizeHost(input16
.c_str(), in_comp
, &output
,
600 EXPECT_EQ(host_cases
[i
].expected_family
!= CanonHostInfo::BROKEN
,
602 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
603 EXPECT_EQ(host_cases
[i
].expected_component
.begin
, out_comp
.begin
);
604 EXPECT_EQ(host_cases
[i
].expected_component
.len
, out_comp
.len
);
608 // CanonicalizeHostVerbose()
609 for (size_t i
= 0; i
< arraysize(host_cases
); i
++) {
611 if (host_cases
[i
].input8
) {
612 int host_len
= static_cast<int>(strlen(host_cases
[i
].input8
));
613 Component
in_comp(0, host_len
);
616 StdStringCanonOutput
output(&out_str
);
617 CanonHostInfo host_info
;
619 CanonicalizeHostVerbose(host_cases
[i
].input8
, in_comp
, &output
,
623 EXPECT_EQ(host_cases
[i
].expected_family
, host_info
.family
);
624 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
625 EXPECT_EQ(host_cases
[i
].expected_component
.begin
,
626 host_info
.out_host
.begin
);
627 EXPECT_EQ(host_cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
628 EXPECT_EQ(std::string(host_cases
[i
].expected_address_hex
),
629 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
630 if (host_cases
[i
].expected_family
== CanonHostInfo::IPV4
) {
631 EXPECT_EQ(host_cases
[i
].expected_num_ipv4_components
,
632 host_info
.num_ipv4_components
);
637 if (host_cases
[i
].input16
) {
638 base::string16
input16(WStringToUTF16(host_cases
[i
].input16
));
639 int host_len
= static_cast<int>(input16
.length());
640 Component
in_comp(0, host_len
);
643 StdStringCanonOutput
output(&out_str
);
644 CanonHostInfo host_info
;
646 CanonicalizeHostVerbose(input16
.c_str(), in_comp
, &output
, &host_info
);
649 EXPECT_EQ(host_cases
[i
].expected_family
, host_info
.family
);
650 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
651 EXPECT_EQ(host_cases
[i
].expected_component
.begin
,
652 host_info
.out_host
.begin
);
653 EXPECT_EQ(host_cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
654 EXPECT_EQ(std::string(host_cases
[i
].expected_address_hex
),
655 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
656 if (host_cases
[i
].expected_family
== CanonHostInfo::IPV4
) {
657 EXPECT_EQ(host_cases
[i
].expected_num_ipv4_components
,
658 host_info
.num_ipv4_components
);
664 TEST(URLCanonTest
, IPv4
) {
665 IPAddressCase cases
[] = {
666 // Empty is not an IP address.
667 {"", L
"", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
668 {".", L
".", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
669 // Regular IP addresses in different bases.
670 {"192.168.0.1", L
"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 4, "C0A80001"},
671 {"0300.0250.00.01", L
"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 4, "C0A80001"},
672 {"0xC0.0Xa8.0x0.0x1", L
"0xC0.0Xa8.0x0.0x1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 4, "C0A80001"},
673 // Non-IP addresses due to invalid characters.
674 {"192.168.9.com", L
"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
675 // Invalid characters for the base should be rejected.
676 {"19a.168.0.1", L
"19a.168.0.1", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
677 {"0308.0250.00.01", L
"0308.0250.00.01", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
678 {"0xCG.0xA8.0x0.0x1", L
"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
679 // If there are not enough components, the last one should fill them out.
680 {"192", L
"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4
, 1, "000000C0"},
681 {"0xC0a80001", L
"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 1, "C0A80001"},
682 {"030052000001", L
"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 1, "C0A80001"},
683 {"000030052000001", L
"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 1, "C0A80001"},
684 {"192.168", L
"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4
, 2, "C00000A8"},
685 {"192.0x00A80001", L
"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 2, "C0A80001"},
686 {"0xc0.052000001", L
"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 2, "C0A80001"},
687 {"192.168.1", L
"192.168.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 3, "C0A80001"},
688 // Too many components means not an IP address.
689 {"192.168.0.0.1", L
"192.168.0.0.1", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
690 // We allow a single trailing dot.
691 {"192.168.0.1.", L
"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4
, 4, "C0A80001"},
692 {"192.168.0.1. hello", L
"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
693 {"192.168.0.1..", L
"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
694 // Two dots in a row means not an IP address.
695 {"192.168..1", L
"192.168..1", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
696 // Any numerical overflow should be marked as BROKEN.
697 {"0x100.0", L
"0x100.0", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
698 {"0x100.0.0", L
"0x100.0.0", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
699 {"0x100.0.0.0", L
"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
700 {"0.0x100.0.0", L
"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
701 {"0.0.0x100.0", L
"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
702 {"0.0.0.0x100", L
"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
703 {"0.0.0x10000", L
"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
704 {"0.0x1000000", L
"0.0x1000000", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
705 {"0x100000000", L
"0x100000000", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
706 // Repeat the previous tests, minus 1, to verify boundaries.
707 {"0xFF.0", L
"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4
, 2, "FF000000"},
708 {"0xFF.0.0", L
"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4
, 3, "FF000000"},
709 {"0xFF.0.0.0", L
"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4
, 4, "FF000000"},
710 {"0.0xFF.0.0", L
"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4
, 4, "00FF0000"},
711 {"0.0.0xFF.0", L
"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4
, 4, "0000FF00"},
712 {"0.0.0.0xFF", L
"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4
, 4, "000000FF"},
713 {"0.0.0xFFFF", L
"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4
, 3, "0000FFFF"},
714 {"0.0xFFFFFF", L
"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4
, 2, "00FFFFFF"},
715 {"0xFFFFFFFF", L
"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4
, 1, "FFFFFFFF"},
716 // Old trunctations tests. They're all "BROKEN" now.
717 {"276.256.0xf1a2.077777", L
"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
718 {"192.168.0.257", L
"192.168.0.257", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
719 {"192.168.0xa20001", L
"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
720 {"192.015052000001", L
"192.015052000001", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
721 {"0X12C0a80001", L
"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
722 {"276.1.2", L
"276.1.2", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
723 // Spaces should be rejected.
724 {"192.168.0.1 hello", L
"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
725 // Very large numbers.
726 {"0000000000000300.0x00000000000000fF.00000000000000001", L
"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4
, 3, "C0FF0001"},
727 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L
"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN
, -1, ""},
728 // A number has no length limit, but long numbers can still overflow.
729 {"00000000000000000001", L
"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4
, 1, "00000001"},
730 {"0000000000000000100000000000000001", L
"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
731 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
732 {"0.0.0.000000000000000000z", L
"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
733 {"0.0.0.100000000000000000z", L
"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
734 // Truncation of all zeros should still result in 0.
735 {"0.00.0x.0x0", L
"0.00.0x.0x0", "0.0.0.0", Component(0, 7), CanonHostInfo::IPV4
, 4, "00000000"},
738 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
740 Component
component(0, static_cast<int>(strlen(cases
[i
].input8
)));
742 std::string out_str1
;
743 StdStringCanonOutput
output1(&out_str1
);
744 CanonHostInfo host_info
;
745 CanonicalizeIPAddress(cases
[i
].input8
, component
, &output1
, &host_info
);
748 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
749 EXPECT_EQ(std::string(cases
[i
].expected_address_hex
),
750 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
751 if (host_info
.family
== CanonHostInfo::IPV4
) {
752 EXPECT_STREQ(cases
[i
].expected
, out_str1
.c_str());
753 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
754 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
755 EXPECT_EQ(cases
[i
].expected_num_ipv4_components
,
756 host_info
.num_ipv4_components
);
760 base::string16
input16(WStringToUTF16(cases
[i
].input16
));
761 component
= Component(0, static_cast<int>(input16
.length()));
763 std::string out_str2
;
764 StdStringCanonOutput
output2(&out_str2
);
765 CanonicalizeIPAddress(input16
.c_str(), component
, &output2
, &host_info
);
768 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
769 EXPECT_EQ(std::string(cases
[i
].expected_address_hex
),
770 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
771 if (host_info
.family
== CanonHostInfo::IPV4
) {
772 EXPECT_STREQ(cases
[i
].expected
, out_str2
.c_str());
773 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
774 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
775 EXPECT_EQ(cases
[i
].expected_num_ipv4_components
,
776 host_info
.num_ipv4_components
);
781 TEST(URLCanonTest
, IPv6
) {
782 IPAddressCase cases
[] = {
783 // Empty is not an IP address.
784 {"", L
"", "", Component(), CanonHostInfo::NEUTRAL
, -1, ""},
785 // Non-IPs with [:] characters are marked BROKEN.
786 {":", L
":", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
787 {"[", L
"[", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
788 {"[:", L
"[:", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
789 {"]", L
"]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
790 {":]", L
":]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
791 {"[]", L
"[]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
792 {"[:]", L
"[:]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
793 // Regular IP address is invalid without bounding '[' and ']'.
794 {"2001:db8::1", L
"2001:db8::1", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
795 {"[2001:db8::1", L
"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
796 {"2001:db8::1]", L
"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
797 // Regular IP addresses.
798 {"[::]", L
"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6
, -1, "00000000000000000000000000000000"},
799 {"[::1]", L
"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6
, -1, "00000000000000000000000000000001"},
800 {"[1::]", L
"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6
, -1, "00010000000000000000000000000000"},
802 // Leading zeros should be stripped.
803 {"[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"},
805 // Upper case letters should be lowercased.
806 {"[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"},
808 // The same address can be written with different contractions, but should
809 // get canonicalized to the same thing.
810 {"[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"},
811 {"[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"},
813 // Addresses with embedded IPv4.
814 {"[::192.168.0.1]", L
"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6
, -1, "000000000000000000000000C0A80001"},
815 {"[::ffff:192.168.0.1]", L
"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6
, -1, "00000000000000000000FFFFC0A80001"},
816 {"[::eeee:192.168.0.1]", L
"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6
, -1, "00000000000000000000EEEEC0A80001"},
817 {"[2001::192.168.0.1]", L
"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6
, -1, "200100000000000000000000C0A80001"},
818 {"[1:2:192.168.0.1:5:6]", L
"[1:2:192.168.0.1:5:6]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
820 // IPv4 with last component missing.
821 {"[::ffff:192.1.2]", L
"[::ffff:192.1.2]", "[::ffff:c001:2]", Component(0,15), CanonHostInfo::IPV6
, -1, "00000000000000000000FFFFC0010002"},
824 // TODO(eroman): Should this format be disallowed?
825 {"[::ffff:0xC0.0Xa8.0x0.0x1]", L
"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6
, -1, "00000000000000000000FFFFC0A80001"},
827 // There may be zeros surrounding the "::" contraction.
828 {"[0:0::0:0:8]", L
"[0:0::0:0:8]", "[::8]", Component(0,5), CanonHostInfo::IPV6
, -1, "00000000000000000000000000000008"},
830 {"[2001:db8::1]", L
"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6
, -1, "20010DB8000000000000000000000001"},
832 // Can only have one "::" contraction in an IPv6 string literal.
833 {"[2001::db8::1]", L
"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
834 // No more than 2 consecutive ':'s.
835 {"[2001:db8:::1]", L
"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
836 {"[:::]", L
"[:::]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
837 // Non-IP addresses due to invalid characters.
838 {"[2001::.com]", L
"[2001::.com]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
839 // If there are not enough components, the last one should fill them out.
840 // ... omitted at this time ...
841 // Too many components means not an IP address. Similarly with too few if using IPv4 compat or mapped addresses.
842 {"[::192.168.0.0.1]", L
"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
843 {"[::ffff:192.168.0.0.1]", L
"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
844 {"[1:2:3:4:5:6:7:8:9]", L
"[1:2:3:4:5:6:7:8:9]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
845 // Too many bits (even though 8 comonents, the last one holds 32 bits).
846 {"[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, ""},
848 // Too many bits specified -- the contraction would have to be zero-length
849 // to not exceed 128 bits.
850 {"[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, ""},
852 // The contraction is for 16 bits of zero.
853 {"[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"},
855 // Cannot have a trailing colon.
856 {"[1:2:3:4:5:6:7:8:]", L
"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
857 {"[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, ""},
859 // Cannot have negative numbers.
860 {"[-1:2:3:4:5:6:7:8]", L
"[-1:2:3:4:5:6:7:8]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
862 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
863 // The scope_id should be included in the canonicalized URL, and is an
864 // unsigned decimal number.
866 // Invalid because no ID was given after the percent.
868 // Don't allow scope-id
869 {"[1::%1]", L
"[1::%1]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
870 {"[1::%eth0]", L
"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
871 {"[1::%]", L
"[1::%]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
872 {"[%]", L
"[%]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
873 {"[::%:]", L
"[::%:]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
875 // Don't allow leading or trailing colons.
876 {"[:0:0::0:0:8]", L
"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
877 {"[0:0::0:0:8:]", L
"[0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
878 {"[:0:0::0:0:8:]", L
"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
880 // We allow a single trailing dot.
881 // ... omitted at this time ...
882 // Two dots in a row means not an IP address.
883 {"[::192.168..1]", L
"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
884 // Any non-first components get truncated to one byte.
885 // ... omitted at this time ...
886 // Spaces should be rejected.
887 {"[::1 hello]", L
"[::1 hello]", "", Component(), CanonHostInfo::BROKEN
, -1, ""},
890 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
892 Component
component(0, static_cast<int>(strlen(cases
[i
].input8
)));
894 std::string out_str1
;
895 StdStringCanonOutput
output1(&out_str1
);
896 CanonHostInfo host_info
;
897 CanonicalizeIPAddress(cases
[i
].input8
, component
, &output1
, &host_info
);
900 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
901 EXPECT_EQ(std::string(cases
[i
].expected_address_hex
),
902 BytesToHexString(host_info
.address
, host_info
.AddressLength())) << "iter " << i
<< " host " << cases
[i
].input8
;
903 if (host_info
.family
== CanonHostInfo::IPV6
) {
904 EXPECT_STREQ(cases
[i
].expected
, out_str1
.c_str());
905 EXPECT_EQ(cases
[i
].expected_component
.begin
,
906 host_info
.out_host
.begin
);
907 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
911 base::string16
input16(WStringToUTF16(cases
[i
].input16
));
912 component
= Component(0, static_cast<int>(input16
.length()));
914 std::string out_str2
;
915 StdStringCanonOutput
output2(&out_str2
);
916 CanonicalizeIPAddress(input16
.c_str(), component
, &output2
, &host_info
);
919 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
920 EXPECT_EQ(std::string(cases
[i
].expected_address_hex
),
921 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
922 if (host_info
.family
== CanonHostInfo::IPV6
) {
923 EXPECT_STREQ(cases
[i
].expected
, out_str2
.c_str());
924 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
925 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
930 TEST(URLCanonTest
, IPEmpty
) {
931 std::string out_str1
;
932 StdStringCanonOutput
output1(&out_str1
);
933 CanonHostInfo host_info
;
936 const char spec
[] = "192.168.0.1";
937 CanonicalizeIPAddress(spec
, Component(), &output1
, &host_info
);
938 EXPECT_FALSE(host_info
.IsIPAddress());
940 CanonicalizeIPAddress(spec
, Component(0, 0), &output1
, &host_info
);
941 EXPECT_FALSE(host_info
.IsIPAddress());
944 TEST(URLCanonTest
, UserInfo
) {
945 // Note that the canonicalizer should escape and treat empty components as
948 // We actually parse a full input URL so we can get the initial components.
949 struct UserComponentCase
{
951 const char* expected
;
952 Component expected_username
;
953 Component expected_password
;
954 bool expected_success
;
955 } user_info_cases
[] = {
956 {"http://user:pass@host.com/", "user:pass@", Component(0, 4), Component(5, 4), true},
957 {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
958 {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
959 {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
960 {"http://:foo@host.com/", ":foo@", Component(0, 0), Component(1, 3), true},
961 {"http://^ :$\t@host.com/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
962 {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
963 {"http://%2540:bar@domain.com/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
965 // IE7 compatability: old versions allowed backslashes in usernames, but
966 // IE7 does not. We disallow it as well.
967 {"ftp://me\\mydomain:pass@foo.com/", "", Component(0, -1), Component(0, -1), true},
970 for (size_t i
= 0; i
< ARRAYSIZE(user_info_cases
); i
++) {
971 int url_len
= static_cast<int>(strlen(user_info_cases
[i
].input
));
973 ParseStandardURL(user_info_cases
[i
].input
, url_len
, &parsed
);
974 Component out_user
, out_pass
;
976 StdStringCanonOutput
output1(&out_str
);
978 bool success
= CanonicalizeUserInfo(user_info_cases
[i
].input
,
980 user_info_cases
[i
].input
,
987 EXPECT_EQ(user_info_cases
[i
].expected_success
, success
);
988 EXPECT_EQ(std::string(user_info_cases
[i
].expected
), out_str
);
989 EXPECT_EQ(user_info_cases
[i
].expected_username
.begin
, out_user
.begin
);
990 EXPECT_EQ(user_info_cases
[i
].expected_username
.len
, out_user
.len
);
991 EXPECT_EQ(user_info_cases
[i
].expected_password
.begin
, out_pass
.begin
);
992 EXPECT_EQ(user_info_cases
[i
].expected_password
.len
, out_pass
.len
);
994 // Now try the wide version
996 StdStringCanonOutput
output2(&out_str
);
997 base::string16
wide_input(ConvertUTF8ToUTF16(user_info_cases
[i
].input
));
998 success
= CanonicalizeUserInfo(wide_input
.c_str(),
1007 EXPECT_EQ(user_info_cases
[i
].expected_success
, success
);
1008 EXPECT_EQ(std::string(user_info_cases
[i
].expected
), out_str
);
1009 EXPECT_EQ(user_info_cases
[i
].expected_username
.begin
, out_user
.begin
);
1010 EXPECT_EQ(user_info_cases
[i
].expected_username
.len
, out_user
.len
);
1011 EXPECT_EQ(user_info_cases
[i
].expected_password
.begin
, out_pass
.begin
);
1012 EXPECT_EQ(user_info_cases
[i
].expected_password
.len
, out_pass
.len
);
1016 TEST(URLCanonTest
, Port
) {
1017 // We only need to test that the number gets properly put into the output
1018 // buffer. The parser unit tests will test scanning the number correctly.
1020 // Note that the CanonicalizePort will always prepend a colon to the output
1021 // to separate it from the colon that it assumes preceeds it.
1025 const char* expected
;
1026 Component expected_component
;
1027 bool expected_success
;
1029 // Invalid input should be copied w/ failure.
1030 {"as df", 80, ":as%20df", Component(1, 7), false},
1031 {"-2", 80, ":-2", Component(1, 2), false},
1032 // Default port should be omitted.
1033 {"80", 80, "", Component(0, -1), true},
1034 {"8080", 80, ":8080", Component(1, 4), true},
1035 // PORT_UNSPECIFIED should mean always keep the port.
1036 {"80", PORT_UNSPECIFIED
, ":80", Component(1, 2), true},
1039 for (size_t i
= 0; i
< ARRAYSIZE(port_cases
); i
++) {
1040 int url_len
= static_cast<int>(strlen(port_cases
[i
].input
));
1041 Component
in_comp(0, url_len
);
1043 std::string out_str
;
1044 StdStringCanonOutput
output1(&out_str
);
1045 bool success
= CanonicalizePort(port_cases
[i
].input
,
1047 port_cases
[i
].default_port
,
1052 EXPECT_EQ(port_cases
[i
].expected_success
, success
);
1053 EXPECT_EQ(std::string(port_cases
[i
].expected
), out_str
);
1054 EXPECT_EQ(port_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1055 EXPECT_EQ(port_cases
[i
].expected_component
.len
, out_comp
.len
);
1057 // Now try the wide version
1059 StdStringCanonOutput
output2(&out_str
);
1060 base::string16
wide_input(ConvertUTF8ToUTF16(port_cases
[i
].input
));
1061 success
= CanonicalizePort(wide_input
.c_str(),
1063 port_cases
[i
].default_port
,
1068 EXPECT_EQ(port_cases
[i
].expected_success
, success
);
1069 EXPECT_EQ(std::string(port_cases
[i
].expected
), out_str
);
1070 EXPECT_EQ(port_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1071 EXPECT_EQ(port_cases
[i
].expected_component
.len
, out_comp
.len
);
1075 TEST(URLCanonTest
, Path
) {
1076 DualComponentCase path_cases
[] = {
1077 // ----- path collapsing tests -----
1078 {"/././foo", L
"/././foo", "/foo", Component(0, 4), true},
1079 {"/./.foo", L
"/./.foo", "/.foo", Component(0, 5), true},
1080 {"/foo/.", L
"/foo/.", "/foo/", Component(0, 5), true},
1081 {"/foo/./", L
"/foo/./", "/foo/", Component(0, 5), true},
1082 // double dots followed by a slash or the end of the string count
1083 {"/foo/bar/..", L
"/foo/bar/..", "/foo/", Component(0, 5), true},
1084 {"/foo/bar/../", L
"/foo/bar/../", "/foo/", Component(0, 5), true},
1085 // don't count double dots when they aren't followed by a slash
1086 {"/foo/..bar", L
"/foo/..bar", "/foo/..bar", Component(0, 10), true},
1087 // some in the middle
1088 {"/foo/bar/../ton", L
"/foo/bar/../ton", "/foo/ton", Component(0, 8), true},
1089 {"/foo/bar/../ton/../../a", L
"/foo/bar/../ton/../../a", "/a", Component(0, 2), true},
1090 // we should not be able to go above the root
1091 {"/foo/../../..", L
"/foo/../../..", "/", Component(0, 1), true},
1092 {"/foo/../../../ton", L
"/foo/../../../ton", "/ton", Component(0, 4), true},
1093 // escaped dots should be unescaped and treated the same as dots
1094 {"/foo/%2e", L
"/foo/%2e", "/foo/", Component(0, 5), true},
1095 {"/foo/%2e%2", L
"/foo/%2e%2", "/foo/.%2", Component(0, 8), true},
1096 {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L
"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", Component(0, 6), true},
1097 // Multiple slashes in a row should be preserved and treated like empty
1099 {"////../..", L
"////../..", "//", Component(0, 2), true},
1101 // ----- escaping tests -----
1102 {"/foo", L
"/foo", "/foo", Component(0, 4), true},
1103 // Valid escape sequence
1104 {"/%20foo", L
"/%20foo", "/%20foo", Component(0, 7), true},
1105 // Invalid escape sequence we should pass through unchanged.
1106 {"/foo%", L
"/foo%", "/foo%", Component(0, 5), true},
1107 {"/foo%2", L
"/foo%2", "/foo%2", Component(0, 6), true},
1108 // Invalid escape sequence: bad characters should be treated the same as
1109 // the sourrounding text, not as escaped (in this case, UTF-8).
1110 {"/foo%2zbar", L
"/foo%2zbar", "/foo%2zbar", Component(0, 10), true},
1111 {"/foo%2\xc2\xa9zbar", NULL
, "/foo%2%C2%A9zbar", Component(0, 16), true},
1112 {NULL
, L
"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22), true},
1113 // Regular characters that are escaped should be unescaped
1114 {"/foo%41%7a", L
"/foo%41%7a", "/fooAz", Component(0, 6), true},
1115 // Funny characters that are unescaped should be escaped
1116 {"/foo\x09\x91%91", NULL
, "/foo%09%91%91", Component(0, 13), true},
1117 {NULL
, L
"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true},
1118 // Invalid characters that are escaped should cause a failure.
1119 {"/foo%00%51", L
"/foo%00%51", "/foo%00Q", Component(0, 8), false},
1120 // Some characters should be passed through unchanged regardless of esc.
1121 {"/(%28:%3A%29)", L
"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13), true},
1122 // Characters that are properly escaped should not have the case changed
1124 {"/%3A%3a%3C%3c", L
"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13), true},
1125 // Funny characters that are unescaped should be escaped
1126 {"/foo\tbar", L
"/foo\tbar", "/foo%09bar", Component(0, 10), true},
1127 // Backslashes should get converted to forward slashes
1128 {"\\foo\\bar", L
"\\foo\\bar", "/foo/bar", Component(0, 8), true},
1129 // Hashes found in paths (possibly only when the caller explicitly sets
1130 // the path on an already-parsed URL) should be escaped.
1131 {"/foo#bar", L
"/foo#bar", "/foo%23bar", Component(0, 10), true},
1132 // %7f should be allowed and %3D should not be unescaped (these were wrong
1133 // in a previous version).
1134 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L
"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true},
1135 // @ should be passed through unchanged (escaped or unescaped).
1136 {"/@asdf%40", L
"/@asdf%40", "/@asdf%40", Component(0, 9), true},
1138 // ----- encoding tests -----
1139 // Basic conversions
1140 {"/\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},
1141 // Invalid unicode characters should fail. We only do validation on
1142 // UTF-16 input, so this doesn't happen on 8-bit.
1143 {"/\xef\xb7\x90zyx", NULL
, "/%EF%B7%90zyx", Component(0, 13), true},
1144 {NULL
, L
"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false},
1147 for (size_t i
= 0; i
< arraysize(path_cases
); i
++) {
1148 if (path_cases
[i
].input8
) {
1149 int len
= static_cast<int>(strlen(path_cases
[i
].input8
));
1150 Component
in_comp(0, len
);
1152 std::string out_str
;
1153 StdStringCanonOutput
output(&out_str
);
1155 CanonicalizePath(path_cases
[i
].input8
, in_comp
, &output
, &out_comp
);
1158 EXPECT_EQ(path_cases
[i
].expected_success
, success
);
1159 EXPECT_EQ(path_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1160 EXPECT_EQ(path_cases
[i
].expected_component
.len
, out_comp
.len
);
1161 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
1164 if (path_cases
[i
].input16
) {
1165 base::string16
input16(WStringToUTF16(path_cases
[i
].input16
));
1166 int len
= static_cast<int>(input16
.length());
1167 Component
in_comp(0, len
);
1169 std::string out_str
;
1170 StdStringCanonOutput
output(&out_str
);
1173 CanonicalizePath(input16
.c_str(), in_comp
, &output
, &out_comp
);
1176 EXPECT_EQ(path_cases
[i
].expected_success
, success
);
1177 EXPECT_EQ(path_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1178 EXPECT_EQ(path_cases
[i
].expected_component
.len
, out_comp
.len
);
1179 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
1183 // Manual test: embedded NULLs should be escaped and the URL should be marked
1185 const char path_with_null
[] = "/ab\0c";
1186 Component
in_comp(0, 5);
1189 std::string out_str
;
1190 StdStringCanonOutput
output(&out_str
);
1191 bool success
= CanonicalizePath(path_with_null
, in_comp
, &output
, &out_comp
);
1193 EXPECT_FALSE(success
);
1194 EXPECT_EQ("/ab%00c", out_str
);
1197 TEST(URLCanonTest
, Query
) {
1200 const wchar_t* input16
;
1201 const char* encoding
;
1202 const char* expected
;
1204 // Regular ASCII case in some different encodings.
1205 {"foo=bar", L
"foo=bar", NULL
, "?foo=bar"},
1206 {"foo=bar", L
"foo=bar", "utf-8", "?foo=bar"},
1207 {"foo=bar", L
"foo=bar", "shift_jis", "?foo=bar"},
1208 {"foo=bar", L
"foo=bar", "gb2312", "?foo=bar"},
1209 // Allow question marks in the query without escaping
1210 {"as?df", L
"as?df", NULL
, "?as?df"},
1211 // Always escape '#' since it would mark the ref.
1212 {"as#df", L
"as#df", NULL
, "?as%23df"},
1213 // Escape some questionable 8-bit characters, but never unescape.
1214 {"\x02hello\x7f bye", L
"\x02hello\x7f bye", NULL
, "?%02hello%7F%20bye"},
1215 {"%40%41123", L
"%40%41123", NULL
, "?%40%41123"},
1216 // Chinese input/output
1217 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", NULL
, "?q=%E4%BD%A0%E5%A5%BD"},
1218 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", "gb2312", "?q=%C4%E3%BA%C3"},
1219 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", "big5", "?q=%A7A%A6n"},
1220 // Unencodable character in the destination character set should be
1221 // escaped. The escape sequence unescapes to be the entity name:
1223 {"q=Chinese\xef\xbc\xa7", L
"q=Chinese\xff27", "iso-8859-1", "?q=Chinese%26%2365319%3B"},
1224 // Invalid UTF-8/16 input should be replaced with invalid characters.
1225 {"q=\xed\xed", L
"q=\xd800\xd800", NULL
, "?q=%EF%BF%BD%EF%BF%BD"},
1226 // Don't allow < or > because sometimes they are used for XSS if the
1227 // URL is echoed in content. Firefox does this, IE doesn't.
1228 {"q=<asdf>", L
"q=<asdf>", NULL
, "?q=%3Casdf%3E"},
1229 // Escape double quotemarks in the query.
1230 {"q=\"asdf\"", L
"q=\"asdf\"", NULL
, "?q=%22asdf%22"},
1233 for (size_t i
= 0; i
< ARRAYSIZE(query_cases
); i
++) {
1236 UConvScoper
conv(query_cases
[i
].encoding
);
1237 ASSERT_TRUE(!query_cases
[i
].encoding
|| conv
.converter());
1238 ICUCharsetConverter
converter(conv
.converter());
1240 // Map NULL to a NULL converter pointer.
1241 ICUCharsetConverter
* conv_pointer
= &converter
;
1242 if (!query_cases
[i
].encoding
)
1243 conv_pointer
= NULL
;
1245 if (query_cases
[i
].input8
) {
1246 int len
= static_cast<int>(strlen(query_cases
[i
].input8
));
1247 Component
in_comp(0, len
);
1248 std::string out_str
;
1250 StdStringCanonOutput
output(&out_str
);
1251 CanonicalizeQuery(query_cases
[i
].input8
, in_comp
, conv_pointer
, &output
,
1255 EXPECT_EQ(query_cases
[i
].expected
, out_str
);
1258 if (query_cases
[i
].input16
) {
1259 base::string16
input16(WStringToUTF16(query_cases
[i
].input16
));
1260 int len
= static_cast<int>(input16
.length());
1261 Component
in_comp(0, len
);
1262 std::string out_str
;
1264 StdStringCanonOutput
output(&out_str
);
1265 CanonicalizeQuery(input16
.c_str(), in_comp
, conv_pointer
, &output
,
1269 EXPECT_EQ(query_cases
[i
].expected
, out_str
);
1273 // Extra test for input with embedded NULL;
1274 std::string out_str
;
1275 StdStringCanonOutput
output(&out_str
);
1277 CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL
, &output
, &out_comp
);
1279 EXPECT_EQ("?a%20%00z%01", out_str
);
1282 TEST(URLCanonTest
, Ref
) {
1283 // Refs are trivial, it just checks the encoding.
1284 DualComponentCase ref_cases
[] = {
1285 // Regular one, we shouldn't escape spaces, et al.
1286 {"hello, world", L
"hello, world", "#hello, world", Component(1, 12), true},
1287 // UTF-8/wide input should be preserved
1288 {"\xc2\xa9", L
"\xa9", "#\xc2\xa9", Component(1, 2), true},
1289 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
1290 {"\xF0\x90\x8C\x80ss", L
"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", Component(1, 6), true},
1291 // Escaping should be preserved unchanged, even invalid ones
1292 {"%41%a", L
"%41%a", "#%41%a", Component(1, 5), true},
1293 // Invalid UTF-8/16 input should be flagged and the input made valid
1294 {"\xc2", NULL
, "#\xef\xbf\xbd", Component(1, 3), true},
1295 {NULL
, L
"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", Component(1, 6), true},
1296 // Test a Unicode invalid character.
1297 {"a\xef\xb7\x90", L
"a\xfdd0", "#a\xef\xbf\xbd", Component(1, 4), true},
1298 // Refs can have # signs and we should preserve them.
1299 {"asdf#qwer", L
"asdf#qwer", "#asdf#qwer", Component(1, 9), true},
1300 {"#asdf", L
"#asdf", "##asdf", Component(1, 5), true},
1303 for (size_t i
= 0; i
< arraysize(ref_cases
); i
++) {
1305 if (ref_cases
[i
].input8
) {
1306 int len
= static_cast<int>(strlen(ref_cases
[i
].input8
));
1307 Component
in_comp(0, len
);
1310 std::string out_str
;
1311 StdStringCanonOutput
output(&out_str
);
1312 CanonicalizeRef(ref_cases
[i
].input8
, in_comp
, &output
, &out_comp
);
1315 EXPECT_EQ(ref_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1316 EXPECT_EQ(ref_cases
[i
].expected_component
.len
, out_comp
.len
);
1317 EXPECT_EQ(ref_cases
[i
].expected
, out_str
);
1321 if (ref_cases
[i
].input16
) {
1322 base::string16
input16(WStringToUTF16(ref_cases
[i
].input16
));
1323 int len
= static_cast<int>(input16
.length());
1324 Component
in_comp(0, len
);
1327 std::string out_str
;
1328 StdStringCanonOutput
output(&out_str
);
1329 CanonicalizeRef(input16
.c_str(), in_comp
, &output
, &out_comp
);
1332 EXPECT_EQ(ref_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1333 EXPECT_EQ(ref_cases
[i
].expected_component
.len
, out_comp
.len
);
1334 EXPECT_EQ(ref_cases
[i
].expected
, out_str
);
1338 // Try one with an embedded NULL. It should be stripped.
1339 const char null_input
[5] = "ab\x00z";
1340 Component
null_input_component(0, 4);
1343 std::string out_str
;
1344 StdStringCanonOutput
output(&out_str
);
1345 CanonicalizeRef(null_input
, null_input_component
, &output
, &out_comp
);
1348 EXPECT_EQ(1, out_comp
.begin
);
1349 EXPECT_EQ(3, out_comp
.len
);
1350 EXPECT_EQ("#abz", out_str
);
1353 TEST(URLCanonTest
, CanonicalizeStandardURL
) {
1354 // The individual component canonicalize tests should have caught the cases
1355 // for each of those components. Here, we just need to test that the various
1356 // parts are included or excluded properly, and have the correct separators.
1359 const char* expected
;
1360 bool expected_success
;
1362 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1363 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1364 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1365 {"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo", true},
1366 {"www.google.com", ":www.google.com/", true},
1367 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1368 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1369 {"http://user:pass@/", "http://user:pass@/", false},
1370 {"http://%25DOMAIN:foobar@foodomain.com/", "http://%25DOMAIN:foobar@foodomain.com/", true},
1372 // Backslashes should get converted to forward slashes.
1373 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1375 // Busted refs shouldn't make the whole thing fail.
1376 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1378 // Basic port tests.
1379 {"http://foo:80/", "http://foo/", true},
1380 {"http://foo:81/", "http://foo:81/", true},
1381 {"httpa://foo:80/", "httpa://foo:80/", true},
1382 {"http://foo:-80/", "http://foo:-80/", false},
1384 {"https://foo:443/", "https://foo/", true},
1385 {"https://foo:80/", "https://foo:80/", true},
1386 {"ftp://foo:21/", "ftp://foo/", true},
1387 {"ftp://foo:80/", "ftp://foo:80/", true},
1388 {"gopher://foo:70/", "gopher://foo/", true},
1389 {"gopher://foo:443/", "gopher://foo:443/", true},
1390 {"ws://foo:80/", "ws://foo/", true},
1391 {"ws://foo:81/", "ws://foo:81/", true},
1392 {"ws://foo:443/", "ws://foo:443/", true},
1393 {"ws://foo:815/", "ws://foo:815/", true},
1394 {"wss://foo:80/", "wss://foo:80/", true},
1395 {"wss://foo:81/", "wss://foo:81/", true},
1396 {"wss://foo:443/", "wss://foo/", true},
1397 {"wss://foo:815/", "wss://foo:815/", true},
1400 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1401 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1403 ParseStandardURL(cases
[i
].input
, url_len
, &parsed
);
1406 std::string out_str
;
1407 StdStringCanonOutput
output(&out_str
);
1408 bool success
= CanonicalizeStandardURL(
1409 cases
[i
].input
, url_len
, parsed
, NULL
, &output
, &out_parsed
);
1412 EXPECT_EQ(cases
[i
].expected_success
, success
);
1413 EXPECT_EQ(cases
[i
].expected
, out_str
);
1417 // The codepath here is the same as for regular canonicalization, so we just
1418 // need to test that things are replaced or not correctly.
1419 TEST(URLCanonTest
, ReplaceStandardURL
) {
1420 ReplaceCase replace_cases
[] = {
1421 // Common case of truncating the path.
1422 {"http://www.google.com/foo?bar=baz#ref", NULL
, NULL
, NULL
, NULL
, NULL
, "/", kDeleteComp
, kDeleteComp
, "http://www.google.com/"},
1423 // Replace everything
1424 {"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"},
1426 {"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"},
1427 // Replace scheme with filesystem. The result is garbage, but you asked
1429 {"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"},
1432 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1433 const ReplaceCase
& cur
= replace_cases
[i
];
1434 int base_len
= static_cast<int>(strlen(cur
.base
));
1436 ParseStandardURL(cur
.base
, base_len
, &parsed
);
1438 Replacements
<char> r
;
1439 typedef Replacements
<char> R
; // Clean up syntax.
1441 // Note that for the scheme we pass in a different clear function since
1442 // there is no function to clear the scheme.
1443 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1444 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1445 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1446 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1447 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1448 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1449 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1450 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1452 std::string out_str
;
1453 StdStringCanonOutput
output(&out_str
);
1455 ReplaceStandardURL(replace_cases
[i
].base
, parsed
, r
, NULL
, &output
,
1459 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1462 // The path pointer should be ignored if the address is invalid.
1464 const char src
[] = "http://www.google.com/here_is_the_path";
1465 int src_len
= static_cast<int>(strlen(src
));
1468 ParseStandardURL(src
, src_len
, &parsed
);
1470 // Replace the path to 0 length string. By using 1 as the string address,
1471 // the test should get an access violation if it tries to dereference it.
1472 Replacements
<char> r
;
1473 r
.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0));
1474 std::string out_str1
;
1475 StdStringCanonOutput
output1(&out_str1
);
1477 ReplaceStandardURL(src
, parsed
, r
, NULL
, &output1
, &new_parsed
);
1479 EXPECT_STREQ("http://www.google.com/", out_str1
.c_str());
1481 // Same with an "invalid" path.
1482 r
.SetPath(reinterpret_cast<char*>(0x00000001), Component());
1483 std::string out_str2
;
1484 StdStringCanonOutput
output2(&out_str2
);
1485 ReplaceStandardURL(src
, parsed
, r
, NULL
, &output2
, &new_parsed
);
1487 EXPECT_STREQ("http://www.google.com/", out_str2
.c_str());
1491 TEST(URLCanonTest
, ReplaceFileURL
) {
1492 ReplaceCase replace_cases
[] = {
1493 // Replace everything
1494 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, "filer", NULL
, "/foo", "b", "c", "file://filer/foo?b#c"},
1496 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "file:///C:/gaba?query#ref"},
1497 // Clear non-path components (common)
1498 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, kDeleteComp
, "file:///C:/gaba"},
1499 // Replace path with something that doesn't begin with a slash and make
1500 // sure it gets added properly.
1501 {"file:///C:/gaba", NULL
, NULL
, NULL
, NULL
, NULL
, "interesting/", NULL
, NULL
, "file:///interesting/"},
1502 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, "filer", NULL
, "/foo", "b", "c", "file://filer/foo?b#c"},
1503 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "file:///home/gaba?query#ref"},
1504 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, kDeleteComp
, "file:///home/gaba"},
1505 {"file:///home/gaba", NULL
, NULL
, NULL
, NULL
, NULL
, "interesting/", NULL
, NULL
, "file:///interesting/"},
1506 // Replace scheme -- shouldn't do anything.
1507 {"file:///C:/gaba?query#ref", "http", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "file:///C:/gaba?query#ref"},
1510 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1511 const ReplaceCase
& cur
= replace_cases
[i
];
1512 int base_len
= static_cast<int>(strlen(cur
.base
));
1514 ParseFileURL(cur
.base
, base_len
, &parsed
);
1516 Replacements
<char> r
;
1517 typedef Replacements
<char> R
; // Clean up syntax.
1518 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1519 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1520 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1521 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1522 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1523 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1524 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1525 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1527 std::string out_str
;
1528 StdStringCanonOutput
output(&out_str
);
1530 ReplaceFileURL(cur
.base
, parsed
, r
, NULL
, &output
, &out_parsed
);
1533 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1537 TEST(URLCanonTest
, ReplaceFileSystemURL
) {
1538 ReplaceCase replace_cases
[] = {
1539 // Replace everything in the outer URL.
1540 {"filesystem:file:///temporary/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1542 {"filesystem:file:///temporary/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "filesystem:file:///temporary/gaba?query#ref"},
1543 // Clear non-path components (common)
1544 {"filesystem:file:///temporary/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, kDeleteComp
, "filesystem:file:///temporary/gaba"},
1545 // Replace path with something that doesn't begin with a slash and make
1546 // sure it gets added properly.
1547 {"filesystem:file:///temporary/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, "interesting/", NULL
, NULL
, "filesystem:file:///temporary/interesting/?query#ref"},
1548 // Replace scheme -- shouldn't do anything.
1549 {"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"},
1550 // Replace username -- shouldn't do anything.
1551 {"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"},
1552 // Replace password -- shouldn't do anything.
1553 {"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"},
1554 // Replace host -- shouldn't do anything.
1555 {"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"},
1556 // Replace port -- shouldn't do anything.
1557 {"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"},
1560 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1561 const ReplaceCase
& cur
= replace_cases
[i
];
1562 int base_len
= static_cast<int>(strlen(cur
.base
));
1564 ParseFileSystemURL(cur
.base
, base_len
, &parsed
);
1566 Replacements
<char> r
;
1567 typedef Replacements
<char> R
; // Clean up syntax.
1568 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1569 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1570 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1571 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1572 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1573 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1574 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1575 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1577 std::string out_str
;
1578 StdStringCanonOutput
output(&out_str
);
1580 ReplaceFileSystemURL(cur
.base
, parsed
, r
, NULL
, &output
, &out_parsed
);
1583 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1587 TEST(URLCanonTest
, ReplacePathURL
) {
1588 ReplaceCase replace_cases
[] = {
1589 // Replace everything
1590 {"data:foo", "javascript", NULL
, NULL
, NULL
, NULL
, "alert('foo?');", NULL
, NULL
, "javascript:alert('foo?');"},
1592 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "data:foo"},
1593 // Replace one or the other
1594 {"data:foo", "javascript", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "javascript:foo"},
1595 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, "bar", NULL
, NULL
, "data:bar"},
1596 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, NULL
, NULL
, "data:"},
1599 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1600 const ReplaceCase
& cur
= replace_cases
[i
];
1601 int base_len
= static_cast<int>(strlen(cur
.base
));
1603 ParsePathURL(cur
.base
, base_len
, false, &parsed
);
1605 Replacements
<char> r
;
1606 typedef Replacements
<char> R
; // Clean up syntax.
1607 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1608 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1609 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1610 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1611 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1612 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1613 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1614 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1616 std::string out_str
;
1617 StdStringCanonOutput
output(&out_str
);
1619 ReplacePathURL(cur
.base
, parsed
, r
, &output
, &out_parsed
);
1622 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1626 TEST(URLCanonTest
, ReplaceMailtoURL
) {
1627 ReplaceCase replace_cases
[] = {
1628 // Replace everything
1629 {"mailto:jon@foo.com?body=sup", "mailto", NULL
, NULL
, NULL
, NULL
, "addr1", "to=tony", NULL
, "mailto:addr1?to=tony"},
1631 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "mailto:jon@foo.com?body=sup"},
1633 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "jason", NULL
, NULL
, "mailto:jason?body=sup"},
1634 // Replace the query
1635 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "custom=1", NULL
, "mailto:jon@foo.com?custom=1"},
1636 // Replace the path and query
1637 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "jason", "custom=1", NULL
, "mailto:jason?custom=1"},
1638 // Set the query to empty (should leave trailing question mark)
1639 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "", NULL
, "mailto:jon@foo.com?"},
1641 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "|", NULL
, "mailto:jon@foo.com"},
1643 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "|", NULL
, NULL
, "mailto:?body=sup"},
1644 // Clear the path + query
1645 {"mailto:", NULL
, NULL
, NULL
, NULL
, NULL
, "|", "|", NULL
, "mailto:"},
1646 // Setting the ref should have no effect
1647 {"mailto:addr1", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "BLAH", "mailto:addr1"},
1650 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1651 const ReplaceCase
& cur
= replace_cases
[i
];
1652 int base_len
= static_cast<int>(strlen(cur
.base
));
1654 ParseMailtoURL(cur
.base
, base_len
, &parsed
);
1656 Replacements
<char> r
;
1657 typedef Replacements
<char> R
;
1658 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1659 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1660 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1661 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1662 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1663 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1664 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1665 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1667 std::string out_str
;
1668 StdStringCanonOutput
output(&out_str
);
1670 ReplaceMailtoURL(cur
.base
, parsed
, r
, &output
, &out_parsed
);
1673 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1677 TEST(URLCanonTest
, CanonicalizeFileURL
) {
1680 const char* expected
;
1681 bool expected_success
;
1682 Component expected_host
;
1683 Component expected_path
;
1686 // Windows-style paths
1687 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1688 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1689 {"file:", "file:///", true, Component(), Component(7, 1)},
1690 {"file:UNChost/path", "file://unchost/path", true, Component(7, 7), Component(14, 5)},
1691 // CanonicalizeFileURL supports absolute Windows style paths for IE
1692 // compatability. Note that the caller must decide that this is a file
1693 // URL itself so it can call the file canonicalizer. This is usually
1694 // done automatically as part of relative URL resolving.
1695 {"c:\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1696 {"C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1697 {"/C|\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1698 {"//C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1699 {"//server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1700 {"\\\\server\\file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1701 {"/\\server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1702 // We should preserve the number of slashes after the colon for IE
1703 // compatability, except when there is none, in which case we should
1705 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1706 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1707 // Three slashes should be non-UNC, even if there is no drive spec (IE
1708 // does this, which makes the resulting request invalid).
1709 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(), Component(7, 12)},
1710 // TODO(brettw) we should probably fail for invalid host names, which
1711 // would change the expected result on this test. We also currently allow
1712 // colon even though it's probably invalid, because its currently the
1713 // "natural" result of the way the canonicalizer is written. There doesn't
1714 // seem to be a strong argument for why allowing it here would be bad, so
1715 // we just tolerate it and the load will fail later.
1716 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, Component(7, 2), Component(9, 16)},
1717 {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5), Component(12, 8)},
1718 // Make sure relative paths can't go above the "C:"
1719 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, Component(), Component(7, 12)},
1720 // Busted refs shouldn't make the whole thing fail.
1721 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, Component(), Component(7, 8)},
1724 {"file:///home/me", "file:///home/me", true, Component(), Component(7, 8)},
1725 // Windowsy ones should get still treated as Unix-style.
1726 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(), Component(7, 16)},
1727 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, Component(), Component(7, 19)},
1728 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
1729 {"//", "file:///", true, Component(), Component(7, 1)},
1730 {"///", "file:///", true, Component(), Component(7, 1)},
1731 {"///test", "file:///test", true, Component(), Component(7, 5)},
1732 {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)},
1733 {"file://localhost", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1734 {"file://localhost/", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1735 {"file://localhost/test", "file://localhost/test", true, Component(7, 9), Component(16, 5)},
1739 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1740 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1742 ParseFileURL(cases
[i
].input
, url_len
, &parsed
);
1745 std::string out_str
;
1746 StdStringCanonOutput
output(&out_str
);
1747 bool success
= CanonicalizeFileURL(cases
[i
].input
, url_len
, parsed
, NULL
,
1748 &output
, &out_parsed
);
1751 EXPECT_EQ(cases
[i
].expected_success
, success
);
1752 EXPECT_EQ(cases
[i
].expected
, out_str
);
1754 // Make sure the spec was properly identified, the file canonicalizer has
1755 // different code for writing the spec.
1756 EXPECT_EQ(0, out_parsed
.scheme
.begin
);
1757 EXPECT_EQ(4, out_parsed
.scheme
.len
);
1759 EXPECT_EQ(cases
[i
].expected_host
.begin
, out_parsed
.host
.begin
);
1760 EXPECT_EQ(cases
[i
].expected_host
.len
, out_parsed
.host
.len
);
1762 EXPECT_EQ(cases
[i
].expected_path
.begin
, out_parsed
.path
.begin
);
1763 EXPECT_EQ(cases
[i
].expected_path
.len
, out_parsed
.path
.len
);
1767 TEST(URLCanonTest
, CanonicalizeFileSystemURL
) {
1770 const char* expected
;
1771 bool expected_success
;
1773 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1774 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1775 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1776 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1777 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1778 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1779 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1782 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1783 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1785 ParseFileSystemURL(cases
[i
].input
, url_len
, &parsed
);
1788 std::string out_str
;
1789 StdStringCanonOutput
output(&out_str
);
1790 bool success
= CanonicalizeFileSystemURL(cases
[i
].input
, url_len
, parsed
,
1791 NULL
, &output
, &out_parsed
);
1794 EXPECT_EQ(cases
[i
].expected_success
, success
);
1795 EXPECT_EQ(cases
[i
].expected
, out_str
);
1797 // Make sure the spec was properly identified, the filesystem canonicalizer
1798 // has different code for writing the spec.
1799 EXPECT_EQ(0, out_parsed
.scheme
.begin
);
1800 EXPECT_EQ(10, out_parsed
.scheme
.len
);
1802 EXPECT_GT(out_parsed
.path
.len
, 0);
1806 TEST(URLCanonTest
, CanonicalizePathURL
) {
1807 // Path URLs should get canonicalized schemes but nothing else.
1810 const char* expected
;
1812 {"javascript:", "javascript:"},
1813 {"JavaScript:Foo", "javascript:Foo"},
1814 {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1817 for (size_t i
= 0; i
< ARRAYSIZE(path_cases
); i
++) {
1818 int url_len
= static_cast<int>(strlen(path_cases
[i
].input
));
1820 ParsePathURL(path_cases
[i
].input
, url_len
, true, &parsed
);
1823 std::string out_str
;
1824 StdStringCanonOutput
output(&out_str
);
1825 bool success
= CanonicalizePathURL(path_cases
[i
].input
, url_len
, parsed
,
1826 &output
, &out_parsed
);
1829 EXPECT_TRUE(success
);
1830 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
1832 EXPECT_EQ(0, out_parsed
.host
.begin
);
1833 EXPECT_EQ(-1, out_parsed
.host
.len
);
1835 // When we end with a colon at the end, there should be no path.
1836 if (path_cases
[i
].input
[url_len
- 1] == ':') {
1837 EXPECT_EQ(0, out_parsed
.GetContent().begin
);
1838 EXPECT_EQ(-1, out_parsed
.GetContent().len
);
1843 TEST(URLCanonTest
, CanonicalizeMailtoURL
) {
1846 const char* expected
;
1847 bool expected_success
;
1848 Component expected_path
;
1849 Component expected_query
;
1851 {"mailto:addr1", "mailto:addr1", true, Component(7, 5), Component()},
1852 {"mailto:addr1@foo.com", "mailto:addr1@foo.com", true, Component(7, 13), Component()},
1853 // Trailing whitespace is stripped.
1854 {"MaIlTo:addr1 \t ", "mailto:addr1", true, Component(7, 5), Component()},
1855 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, Component(7, 5), Component(13,6)},
1856 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, Component(7, 11), Component()},
1857 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, Component(7, 12), Component()},
1858 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, Component(7, 13), Component()},
1859 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, Component(7, 12), Component()},
1860 // Null character should be escaped to %00
1861 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, Component(7, 13), Component(21, 3)},
1862 // Invalid -- UTF-8 encoded surrogate value.
1863 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, Component(7, 9), Component()},
1864 {"mailto:addr1?", "mailto:addr1?", true, Component(7, 5), Component(13, 0)},
1867 // Define outside of loop to catch bugs where components aren't reset
1871 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1872 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1874 // The 9th test case purposely has a '\0' in it -- don't count it
1875 // as the string terminator.
1878 ParseMailtoURL(cases
[i
].input
, url_len
, &parsed
);
1880 std::string out_str
;
1881 StdStringCanonOutput
output(&out_str
);
1882 bool success
= CanonicalizeMailtoURL(cases
[i
].input
, url_len
, parsed
,
1883 &output
, &out_parsed
);
1886 EXPECT_EQ(cases
[i
].expected_success
, success
);
1887 EXPECT_EQ(cases
[i
].expected
, out_str
);
1889 // Make sure the spec was properly identified
1890 EXPECT_EQ(0, out_parsed
.scheme
.begin
);
1891 EXPECT_EQ(6, out_parsed
.scheme
.len
);
1893 EXPECT_EQ(cases
[i
].expected_path
.begin
, out_parsed
.path
.begin
);
1894 EXPECT_EQ(cases
[i
].expected_path
.len
, out_parsed
.path
.len
);
1896 EXPECT_EQ(cases
[i
].expected_query
.begin
, out_parsed
.query
.begin
);
1897 EXPECT_EQ(cases
[i
].expected_query
.len
, out_parsed
.query
.len
);
1903 TEST(URLCanonTest
, _itoa_s
) {
1904 // We fill the buffer with 0xff to ensure that it's getting properly
1905 // null-terminated. We also allocate one byte more than what we tell
1906 // _itoa_s about, and ensure that the extra byte is untouched.
1908 memset(buf
, 0xff, sizeof(buf
));
1909 EXPECT_EQ(0, _itoa_s(12, buf
, sizeof(buf
) - 1, 10));
1910 EXPECT_STREQ("12", buf
);
1911 EXPECT_EQ('\xFF', buf
[3]);
1913 // Test the edge cases - exactly the buffer size and one over
1914 memset(buf
, 0xff, sizeof(buf
));
1915 EXPECT_EQ(0, _itoa_s(1234, buf
, sizeof(buf
) - 1, 10));
1916 EXPECT_STREQ("1234", buf
);
1917 EXPECT_EQ('\xFF', buf
[5]);
1919 memset(buf
, 0xff, sizeof(buf
));
1920 EXPECT_EQ(EINVAL
, _itoa_s(12345, buf
, sizeof(buf
) - 1, 10));
1921 EXPECT_EQ('\xFF', buf
[5]); // should never write to this location
1923 // Test the template overload (note that this will see the full buffer)
1924 memset(buf
, 0xff, sizeof(buf
));
1925 EXPECT_EQ(0, _itoa_s(12, buf
, 10));
1926 EXPECT_STREQ("12", buf
);
1927 EXPECT_EQ('\xFF', buf
[3]);
1929 memset(buf
, 0xff, sizeof(buf
));
1930 EXPECT_EQ(0, _itoa_s(12345, buf
, 10));
1931 EXPECT_STREQ("12345", buf
);
1933 EXPECT_EQ(EINVAL
, _itoa_s(123456, buf
, 10));
1935 // Test that radix 16 is supported.
1936 memset(buf
, 0xff, sizeof(buf
));
1937 EXPECT_EQ(0, _itoa_s(1234, buf
, sizeof(buf
) - 1, 16));
1938 EXPECT_STREQ("4d2", buf
);
1939 EXPECT_EQ('\xFF', buf
[5]);
1942 TEST(URLCanonTest
, _itow_s
) {
1943 // We fill the buffer with 0xff to ensure that it's getting properly
1944 // null-terminated. We also allocate one byte more than what we tell
1945 // _itoa_s about, and ensure that the extra byte is untouched.
1946 base::char16 buf
[6];
1947 const char fill_mem
= 0xff;
1948 const base::char16 fill_char
= 0xffff;
1949 memset(buf
, fill_mem
, sizeof(buf
));
1950 EXPECT_EQ(0, _itow_s(12, buf
, sizeof(buf
) / 2 - 1, 10));
1951 EXPECT_EQ(WStringToUTF16(L
"12"), base::string16(buf
));
1952 EXPECT_EQ(fill_char
, buf
[3]);
1954 // Test the edge cases - exactly the buffer size and one over
1955 EXPECT_EQ(0, _itow_s(1234, buf
, sizeof(buf
) / 2 - 1, 10));
1956 EXPECT_EQ(WStringToUTF16(L
"1234"), base::string16(buf
));
1957 EXPECT_EQ(fill_char
, buf
[5]);
1959 memset(buf
, fill_mem
, sizeof(buf
));
1960 EXPECT_EQ(EINVAL
, _itow_s(12345, buf
, sizeof(buf
) / 2 - 1, 10));
1961 EXPECT_EQ(fill_char
, buf
[5]); // should never write to this location
1963 // Test the template overload (note that this will see the full buffer)
1964 memset(buf
, fill_mem
, sizeof(buf
));
1965 EXPECT_EQ(0, _itow_s(12, buf
, 10));
1966 EXPECT_EQ(WStringToUTF16(L
"12"), base::string16(buf
));
1967 EXPECT_EQ(fill_char
, buf
[3]);
1969 memset(buf
, fill_mem
, sizeof(buf
));
1970 EXPECT_EQ(0, _itow_s(12345, buf
, 10));
1971 EXPECT_EQ(WStringToUTF16(L
"12345"), base::string16(buf
));
1973 EXPECT_EQ(EINVAL
, _itow_s(123456, buf
, 10));
1978 // Returns true if the given two structures are the same.
1979 static bool ParsedIsEqual(const Parsed
& a
, const Parsed
& b
) {
1980 return a
.scheme
.begin
== b
.scheme
.begin
&& a
.scheme
.len
== b
.scheme
.len
&&
1981 a
.username
.begin
== b
.username
.begin
&& a
.username
.len
== b
.username
.len
&&
1982 a
.password
.begin
== b
.password
.begin
&& a
.password
.len
== b
.password
.len
&&
1983 a
.host
.begin
== b
.host
.begin
&& a
.host
.len
== b
.host
.len
&&
1984 a
.port
.begin
== b
.port
.begin
&& a
.port
.len
== b
.port
.len
&&
1985 a
.path
.begin
== b
.path
.begin
&& a
.path
.len
== b
.path
.len
&&
1986 a
.query
.begin
== b
.query
.begin
&& a
.query
.len
== b
.query
.len
&&
1987 a
.ref
.begin
== b
.ref
.begin
&& a
.ref
.len
== b
.ref
.len
;
1990 TEST(URLCanonTest
, ResolveRelativeURL
) {
1991 struct RelativeCase
{
1992 const char* base
; // Input base URL: MUST BE CANONICAL
1993 bool is_base_hier
; // Is the base URL hierarchical
1994 bool is_base_file
; // Tells us if the base is a file URL.
1995 const char* test
; // Input URL to test against.
1996 bool succeed_relative
; // Whether we expect IsRelativeURL to succeed
1997 bool is_rel
; // Whether we expect |test| to be relative or not.
1998 bool succeed_resolve
; // Whether we expect ResolveRelativeURL to succeed.
1999 const char* resolved
; // What we expect in the result when resolving.
2001 // Basic absolute input.
2002 {"http://host/a", true, false, "http://another/", true, false, false, NULL
},
2003 {"http://host/a", true, false, "http:////another/", true, false, false, NULL
},
2004 // Empty relative URLs should only remove the ref part of the URL,
2005 // leaving the rest unchanged.
2006 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
2007 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
2008 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
2009 // Spaces at the ends of the relative path should be ignored.
2010 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
2011 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
2012 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
2013 // Matching schemes without two slashes are treated as relative.
2014 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
2015 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
2016 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
2017 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
2018 // Nonmatching schemes are absolute.
2019 {"http://host/a", true, false, "https:host2", true, false, false, NULL
},
2020 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL
},
2021 // Absolute path input
2022 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
2023 {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
2024 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
2025 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
2026 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
2027 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
2028 // Relative path input
2029 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
2030 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
2031 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
2032 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
2033 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
2034 {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
2035 {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
2036 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
2037 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
2039 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
2040 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
2041 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
2043 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
2044 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
2045 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
2046 // Non-hierarchical base: no relative handling. Relative input should
2047 // error, and if a scheme is present, it should be treated as absolute.
2048 {"data:foobar", false, false, "baz.html", false, false, false, NULL
},
2049 {"data:foobar", false, false, "data:baz", true, false, false, NULL
},
2050 {"data:foobar", false, false, "data:/base", true, false, false, NULL
},
2051 // Non-hierarchical base: absolute input should succeed.
2052 {"data:foobar", false, false, "http://host/", true, false, false, NULL
},
2053 {"data:foobar", false, false, "http:host", true, false, false, NULL
},
2054 // Invalid schemes should be treated as relative.
2055 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
2056 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
2057 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
2058 {"data:asdf", false, false, ":foo", false, false, false, NULL
},
2059 {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL
},
2060 // We should treat semicolons like any other character in URL resolving
2061 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
2062 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
2063 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
2064 // Relative URLs can also be written as "//foo/bar" which is relative to
2065 // the scheme. In this case, it would take the old scheme, so for http
2066 // the example would resolve to "http://foo/bar".
2067 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
2068 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
2069 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
2070 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
2071 {"http://host/a", true, false, "//", true, true, false, "http:"},
2072 // IE will also allow one or the other to be a backslash to get the same
2074 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
2075 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
2077 // Resolving against Windows file base URLs.
2078 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL
},
2079 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
2080 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
2081 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
2082 // But two backslashes on Windows should be UNC so should be treated
2084 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL
},
2085 // IE doesn't support drive specs starting with two slashes. It fails
2086 // immediately and doesn't even try to load. We fix it up to either
2087 // an absolute path or UNC depending on what it looks like.
2088 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
2089 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
2090 // Windows drive specs should be allowed and treated as absolute.
2091 {"file:///C:/foo", true, true, "c:", true, false, false, NULL
},
2092 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL
},
2093 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL
},
2094 // Relative paths with drive letters should be allowed when the base is
2096 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2097 // Treat absolute paths as being off of the drive.
2098 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2099 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2100 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2101 // On Windows, two slashes without a drive letter when the base is a file
2102 // means that the path is UNC.
2103 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2104 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2106 // On Unix we fall back to relative behavior since there's nothing else
2107 // reasonable to do.
2108 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2110 // Even on Windows, we don't allow relative drive specs when the base
2112 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2113 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
2114 // Ensure that ports aren't allowed for hosts relative to a file url.
2115 // Although the result string shows a host:port portion, the call to
2116 // resolve the relative URL returns false, indicating parse failure,
2117 // which is what is required.
2118 {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
2119 // Filesystem URL tests; filesystem URLs are only valid and relative if
2120 // they have no scheme, e.g. "./index.html". There's no valid equivalent
2121 // to http:index.html.
2122 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL
},
2123 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL
},
2124 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL
},
2125 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL
},
2126 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2127 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2128 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL
},
2129 // Absolute URLs are still not relative to a non-standard base URL.
2130 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2131 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2134 for (size_t i
= 0; i
< ARRAYSIZE(rel_cases
); i
++) {
2135 const RelativeCase
& cur_case
= rel_cases
[i
];
2138 int base_len
= static_cast<int>(strlen(cur_case
.base
));
2139 if (cur_case
.is_base_file
)
2140 ParseFileURL(cur_case
.base
, base_len
, &parsed
);
2141 else if (cur_case
.is_base_hier
)
2142 ParseStandardURL(cur_case
.base
, base_len
, &parsed
);
2144 ParsePathURL(cur_case
.base
, base_len
, false, &parsed
);
2146 // First see if it is relative.
2147 int test_len
= static_cast<int>(strlen(cur_case
.test
));
2149 Component relative_component
;
2150 bool succeed_is_rel
= IsRelativeURL(
2151 cur_case
.base
, parsed
, cur_case
.test
, test_len
, cur_case
.is_base_hier
,
2152 &is_relative
, &relative_component
);
2154 EXPECT_EQ(cur_case
.succeed_relative
, succeed_is_rel
) <<
2155 "succeed is rel failure on " << cur_case
.test
;
2156 EXPECT_EQ(cur_case
.is_rel
, is_relative
) <<
2157 "is rel failure on " << cur_case
.test
;
2159 if (succeed_is_rel
&& is_relative
&& cur_case
.is_rel
) {
2160 std::string resolved
;
2161 StdStringCanonOutput
output(&resolved
);
2162 Parsed resolved_parsed
;
2164 bool succeed_resolve
= ResolveRelativeURL(
2165 cur_case
.base
, parsed
, cur_case
.is_base_file
, cur_case
.test
,
2166 relative_component
, NULL
, &output
, &resolved_parsed
);
2169 EXPECT_EQ(cur_case
.succeed_resolve
, succeed_resolve
);
2170 EXPECT_EQ(cur_case
.resolved
, resolved
) << " on " << cur_case
.test
;
2172 // Verify that the output parsed structure is the same as parsing a
2175 int resolved_len
= static_cast<int>(resolved
.size());
2176 if (cur_case
.is_base_file
) {
2177 ParseFileURL(resolved
.c_str(), resolved_len
, &ref_parsed
);
2178 } else if (cur_case
.is_base_hier
) {
2179 ParseStandardURL(resolved
.c_str(), resolved_len
, &ref_parsed
);
2181 ParsePathURL(resolved
.c_str(), resolved_len
, false, &ref_parsed
);
2183 EXPECT_TRUE(ParsedIsEqual(ref_parsed
, resolved_parsed
));
2188 // It used to be when we did a replacement with a long buffer of UTF-16
2189 // characters, we would get invalid data in the URL. This is because the buffer
2190 // it used to hold the UTF-8 data was resized, while some pointers were still
2191 // kept to the old buffer that was removed.
2192 TEST(URLCanonTest
, ReplacementOverflow
) {
2193 const char src
[] = "file:///C:/foo/bar";
2194 int src_len
= static_cast<int>(strlen(src
));
2196 ParseFileURL(src
, src_len
, &parsed
);
2198 // Override two components, the path with something short, and the query with
2199 // sonething long enough to trigger the bug.
2200 Replacements
<base::char16
> repl
;
2201 base::string16 new_query
;
2202 for (int i
= 0; i
< 4800; i
++)
2203 new_query
.push_back('a');
2205 base::string16
new_path(WStringToUTF16(L
"/foo"));
2206 repl
.SetPath(new_path
.c_str(), Component(0, 4));
2207 repl
.SetQuery(new_query
.c_str(),
2208 Component(0, static_cast<int>(new_query
.length())));
2210 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2211 // standard URLs, file URLs, etc, since they will go to the same replacement
2212 // function that was buggy.
2214 std::string repl_str
;
2215 StdStringCanonOutput
repl_output(&repl_str
);
2216 ReplaceFileURL(src
, parsed
, repl
, NULL
, &repl_output
, &repl_parsed
);
2217 repl_output
.Complete();
2219 // Generate the expected string and check.
2220 std::string
expected("file:///foo?");
2221 for (size_t i
= 0; i
< new_query
.length(); i
++)
2222 expected
.push_back('a');
2223 EXPECT_TRUE(expected
== repl_str
);