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
23 using url_test_utils::WStringToUTF16
;
24 using url_test_utils::ConvertUTF8ToUTF16
;
25 using url_test_utils::ConvertUTF16ToUTF8
;
26 using url_canon::CanonHostInfo
;
30 struct ComponentCase
{
33 url_parse::Component expected_component
;
34 bool expected_success
;
37 // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
38 // treat each input as optional, and will only try processing if non-NULL.
39 // The output is always 8-bit.
40 struct DualComponentCase
{
42 const wchar_t* input16
;
44 url_parse::Component expected_component
;
45 bool expected_success
;
48 // Test cases for CanonicalizeIPAddress(). The inputs are identical to
49 // DualComponentCase, but the output has extra CanonHostInfo fields.
50 struct IPAddressCase
{
52 const wchar_t* input16
;
54 url_parse::Component expected_component
;
56 // CanonHostInfo fields, for verbose output.
57 CanonHostInfo::Family expected_family
;
58 int expected_num_ipv4_components
;
59 const char* expected_address_hex
; // Two hex chars per IP address byte.
62 std::string
BytesToHexString(unsigned char bytes
[16], int length
) {
63 EXPECT_TRUE(length
== 0 || length
== 4 || length
== 16)
64 << "Bad IP address length: " << length
;
66 for (int i
= 0; i
< length
; ++i
) {
67 result
.push_back(url_canon::kHexCharLookup
[(bytes
[i
] >> 4) & 0xf]);
68 result
.push_back(url_canon::kHexCharLookup
[bytes
[i
] & 0xf]);
86 // Wrapper around a UConverter object that managers creation and destruction.
89 explicit UConvScoper(const char* charset_name
) {
90 UErrorCode err
= U_ZERO_ERROR
;
91 converter_
= ucnv_open(charset_name
, &err
);
96 ucnv_close(converter_
);
99 // Returns the converter object, may be NULL.
100 UConverter
* converter() const { return converter_
; }
103 UConverter
* converter_
;
106 // Magic string used in the replacements code that tells SetupReplComp to
107 // call the clear function.
108 const char kDeleteComp
[] = "|";
110 // Sets up a replacement for a single component. This is given pointers to
111 // the set and clear function for the component being replaced, and will
112 // either set the component (if it exists) or clear it (if the replacement
113 // string matches kDeleteComp).
115 // This template is currently used only for the 8-bit case, and the strlen
116 // causes it to fail in other cases. It is left a template in case we have
117 // tests for wide replacements.
118 template<typename CHAR
>
120 void (url_canon::Replacements
<CHAR
>::*set
)(const CHAR
*,
121 const url_parse::Component
&),
122 void (url_canon::Replacements
<CHAR
>::*clear
)(),
123 url_canon::Replacements
<CHAR
>* rep
,
125 if (str
&& str
[0] == kDeleteComp
[0]) {
128 (rep
->*set
)(str
, url_parse::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 url_canon::StdStringCanonOutput
output(&out_str
);
150 url_canon::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 url_canon::StdStringCanonOutput
output(&out_str
);
163 // Invalid code point (too large).
165 url_canon::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 url_canon::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 url_canon::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 url_canon::ICUCharsetConverter
converter(conv
.converter());
270 url_canon::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 url_canon::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 url_canon::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:", url_parse::Component(0, 4), true},
310 {"HTTP", "http:", url_parse::Component(0, 4), true},
311 {" HTTP ", "%20http%20:", url_parse::Component(0, 10), false},
312 {"htt: ", "htt%3A%20:", url_parse::Component(0, 9), false},
313 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", url_parse::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:", url_parse::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 url_parse::Component
in_comp(0, url_len
);
324 url_parse::Component out_comp
;
327 url_canon::StdStringCanonOutput
output1(&out_str
);
328 bool success
= url_canon::CanonicalizeScheme(scheme_cases
[i
].input
,
329 in_comp
, &output1
, &out_comp
);
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 url_canon::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
= url_canon::CanonicalizeScheme(wide_input
.c_str(), in_comp
,
344 &output2
, &out_comp
);
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.
355 url_parse::Component out_comp
;
357 url_canon::StdStringCanonOutput
output(&out_str
);
359 EXPECT_TRUE(url_canon::CanonicalizeScheme("", url_parse::Component(0, -1),
360 &output
, &out_comp
));
363 EXPECT_EQ(std::string(":"), out_str
);
364 EXPECT_EQ(0, out_comp
.begin
);
365 EXPECT_EQ(0, out_comp
.len
);
368 TEST(URLCanonTest
, Host
) {
369 IPAddressCase host_cases
[] = {
370 // Basic canonicalization, uppercase should be converted to lowercase.
371 {"GoOgLe.CoM", L
"GoOgLe.CoM", "google.com", url_parse::Component(0, 10), CanonHostInfo::NEUTRAL
, -1, ""},
372 // Spaces and some other characters should be escaped.
373 {"Goo%20 goo%7C|.com", L
"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", url_parse::Component(0, 22), CanonHostInfo::NEUTRAL
, -1, ""},
374 // Exciting different types of spaces!
375 {NULL
, L
"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", url_parse::Component(0, 16), CanonHostInfo::NEUTRAL
, -1, ""},
376 // Other types of space (no-break, zero-width, zero-width-no-break) are
377 // name-prepped away to nothing.
378 {NULL
, L
"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", url_parse::Component(0, 10), CanonHostInfo::NEUTRAL
, -1, ""},
379 // Ideographic full stop (full-width period for Chinese, etc.) should be
381 {NULL
, L
"www.foo\x3002" L
"bar.com", "www.foo.bar.com", url_parse::Component(0, 15), CanonHostInfo::NEUTRAL
, -1, ""},
382 // Invalid unicode characters should fail...
383 // ...In wide input, ICU will barf and we'll end up with the input as
384 // escaped UTF-8 (the invalid character should be replaced with the
385 // replacement character).
386 {"\xef\xb7\x90zyx.com", L
"\xfdd0zyx.com", "%EF%BF%BDzyx.com", url_parse::Component(0, 16), CanonHostInfo::BROKEN
, -1, ""},
387 // ...This is the same as previous but with with escaped.
388 {"%ef%b7%90zyx.com", L
"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", url_parse::Component(0, 16), CanonHostInfo::BROKEN
, -1, ""},
389 // Test name prepping, fullwidth input should be converted to ASCII and NOT
390 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
391 {"\xef\xbc\xa7\xef\xbd\x8f.com", L
"\xff27\xff4f.com", "go.com", url_parse::Component(0, 6), CanonHostInfo::NEUTRAL
, -1, ""},
392 // Test that fullwidth escaped values are properly name-prepped,
393 // then converted or rejected.
394 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
395 {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L
"\xff05\xff14\xff11.com", "a.com", url_parse::Component(0, 5), CanonHostInfo::NEUTRAL
, -1, ""},
396 {"%ef%bc%85%ef%bc%94%ef%bc%91.com", L
"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", url_parse::Component(0, 5), CanonHostInfo::NEUTRAL
, -1, ""},
397 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
398 {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L
"\xff05\xff10\xff10.com", "%00.com", url_parse::Component(0, 7), CanonHostInfo::BROKEN
, -1, ""},
399 {"%ef%bc%85%ef%bc%90%ef%bc%90.com", L
"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", url_parse::Component(0, 7), CanonHostInfo::BROKEN
, -1, ""},
400 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
401 {"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L
"\x4f60\x597d\x4f60\x597d", "xn--6qqa088eba", url_parse::Component(0, 14), CanonHostInfo::NEUTRAL
, -1, ""},
402 // See http://unicode.org/cldr/utility/idna.jsp for other
403 // examples/experiments and http://goo.gl/7yG11o
404 // for the full list of characters handled differently by
405 // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
407 // 4 Deviation characters are mapped/ignored in UTS 46 transitional
408 // mechansm. UTS 46, table 4 row (g).
409 // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
410 // Otherwise, it'd be "xn--fuball-cta.de".
411 {"fu\xc3\x9f" "ball.de", L
"fu\x00df" L
"ball.de", "fussball.de",
412 url_parse::Component(0, 11), CanonHostInfo::NEUTRAL
, -1, ""},
413 // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
414 // Otherwise, it'd be "xn--wxaijb9b".
415 {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L
"\x3c3\x3cc\x3bb\x3bf\x3c2",
416 "xn--wxaikc6b", url_parse::Component(0, 12),
417 CanonHostInfo::NEUTRAL
, -1, ""},
418 // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
419 // handling as well as in IDNA 2003.
420 {"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L
"a\x200c" L
"b\x200d" L
"c", "abc",
421 url_parse::Component(0, 3), CanonHostInfo::NEUTRAL
, -1, ""},
422 // ZWJ between Devanagari characters is still mapped away in UTS 46
423 // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
424 {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
425 L
"\x915\x94d\x200d\x91c", "xn--11bo0m",
426 url_parse::Component(0, 10), CanonHostInfo::NEUTRAL
, -1, ""},
427 // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
428 // However, we do allow this at the moment because we don't use
429 // STD3 rules and canonicalize full-width ASCII to ASCII.
430 {"wow\xef\xbc\x81", L
"wow\xff01", "wow%21",
431 url_parse::Component(0, 6), CanonHostInfo::NEUTRAL
, -1, ""},
432 // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
433 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
434 {"\xe2\x84\xb2oo", L
"\x2132oo", "%E2%84%B2oo",
435 url_parse::Component(0, 11), CanonHostInfo::BROKEN
, -1, ""},
436 // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
437 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
438 {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L
"\xd87e\xdc68\x59fb.cn",
439 "%F0%AF%A1%A8%E5%A7%BB.cn",
440 url_parse::Component(0, 24), CanonHostInfo::BROKEN
, -1, ""},
441 // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
442 {"M\xc3\x9cNCHEN", L
"M\xdcNCHEN", "xn--mnchen-3ya",
443 url_parse::Component(0, 14), CanonHostInfo::NEUTRAL
, -1, ""},
444 // Symbol/punctuations are allowed in IDNA 2003/UTS46.
445 // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
446 {"\xe2\x99\xa5ny.us", L
"\x2665ny.us", "xn--ny-s0x.us",
447 url_parse::Component(0, 13), CanonHostInfo::NEUTRAL
, -1, ""},
448 // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
449 // We used to allow it because we passed through unassigned code points.
450 {"\xf0\x91\x80\x93.com", L
"\xd804\xdc13.com", "xn--n00d.com",
451 url_parse::Component(0, 12), CanonHostInfo::NEUTRAL
, -1, ""},
452 // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
453 // Used to be allowed in INDA 2003.
454 {"\xd8\x82.eg", L
"\x602.eg", "%D8%82.eg",
455 url_parse::Component(0, 9), CanonHostInfo::BROKEN
, -1, ""},
456 // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
457 // on Unicode 3.2). We did allow it in the past because we let unassigned
458 // code point pass. We continue to allow it even though it's a
459 // "punctuation and symbol" blocked in IDNA 2008.
460 // UTS 46 table 4, row (j)
461 {"\xe2\x82\xb7.com", L
"\x20b7.com", "xn--wzg.com",
462 url_parse::Component(0, 11), CanonHostInfo::NEUTRAL
, -1, ""},
463 // Maps uppercase letters to lower case letters.
464 // In IDNA 2003, it's allowed without case-folding
465 // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
466 // (added in Unicode 4.1). UTS 46 table 4 row (k)
467 {"bc\xc8\xba.com", L
"bc\x23a.com", "xn--bc-is1a.com",
468 url_parse::Component(0, 15), CanonHostInfo::NEUTRAL
, -1, ""},
470 // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
471 // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
472 {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
473 L
"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
474 url_parse::Component(0, 13), CanonHostInfo::NEUTRAL
, -1, ""},
475 // Disallowed in both IDNA 2003 and 2008 with BiDi check.
476 // Labels starting with a RTL character cannot end with a LTR character.
477 {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L
"\x62c\x627\x631xyz",
478 "%D8%AC%D8%A7%D8%B1xyz", url_parse::Component(0, 21),
479 CanonHostInfo::BROKEN
, -1, ""},
480 // Labels starting with a RTL character can end with BC=EN (European
481 // number). Disallowed in IDNA 2003 but now allowed.
482 {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L
"\x62c\x627\x631" L
"2",
483 "xn--2-ymcov", url_parse::Component(0, 11),
484 CanonHostInfo::NEUTRAL
, -1, ""},
485 // Labels starting with a RTL character cannot have "L" characters
486 // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
487 {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L
"\x62c\x627\x631xy2",
488 "%D8%AC%D8%A7%D8%B1xy2", url_parse::Component(0, 21),
489 CanonHostInfo::BROKEN
, -1, ""},
490 // Labels starting with a RTL character can end with BC=AN (Arabic number)
491 // Disallowed in IDNA 2003, but now allowed.
492 {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L
"\x62c\x627\x631\x662",
493 "xn--mgbjq0r", url_parse::Component(0, 11),
494 CanonHostInfo::NEUTRAL
, -1, ""},
495 // Labels starting with a RTL character cannot have "L" characters
496 // even if it ends with an BC=AN (Arabic number).
497 // Disallowed in both IDNA 2003/2008.
498 {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L
"\x62c\x627\x631xy\x662",
499 "%D8%AC%D8%A7%D8%B1xy%D9%A2", url_parse::Component(0, 26),
500 CanonHostInfo::BROKEN
, -1, ""},
501 // Labels starting with a RTL character cannot mix BC=EN and BC=AN
502 {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L
"\x62c\x627\x631xy2\x662",
503 "%D8%AC%D8%A7%D8%B1xy2%D9%A2", url_parse::Component(0, 27),
504 CanonHostInfo::BROKEN
, -1, ""},
505 // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
506 {"\xe2\x83\x8f.com", L
"\x20cf.com", "%E2%83%8F.com",
507 url_parse::Component(0, 13), CanonHostInfo::BROKEN
, -1, ""},
508 // U+0080 is not allowed.
509 {"\xc2\x80.com", L
"\x80.com", "%C2%80.com",
510 url_parse::Component(0, 10), CanonHostInfo::BROKEN
, -1, ""},
511 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
512 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
513 // UTF-8 (wide case). The output should be equivalent to the true wide
514 // character input above).
515 {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
516 L
"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
517 url_parse::Component(0, 14), CanonHostInfo::NEUTRAL
, -1, ""},
518 // Invalid escaped characters should fail and the percents should be
520 {"%zz%66%a", L
"%zz%66%a", "%25zzf%25a", url_parse::Component(0, 10),
521 CanonHostInfo::BROKEN
, -1, ""},
522 // If we get an invalid character that has been escaped.
523 {"%25", L
"%25", "%25", url_parse::Component(0, 3),
524 CanonHostInfo::BROKEN
, -1, ""},
525 {"hello%00", L
"hello%00", "hello%00", url_parse::Component(0, 8),
526 CanonHostInfo::BROKEN
, -1, ""},
527 // Escaped numbers should be treated like IP addresses if they are.
528 {"%30%78%63%30%2e%30%32%35%30.01", L
"%30%78%63%30%2e%30%32%35%30.01",
529 "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3,
531 {"%30%78%63%30%2e%30%32%35%30.01%2e", L
"%30%78%63%30%2e%30%32%35%30.01%2e",
532 "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3,
534 // Invalid escaping should trigger the regular host error handling.
535 {"%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", url_parse::Component(0, 17), CanonHostInfo::BROKEN
, -1, ""},
536 // Something that isn't exactly an IP should get treated as a host and
538 {"192.168.0.1 hello", L
"192.168.0.1 hello", "192.168.0.1%20hello", url_parse::Component(0, 19), CanonHostInfo::NEUTRAL
, -1, ""},
539 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
540 // These are "0Xc0.0250.01" in fullwidth.
541 {"\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", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3, "C0A80001"},
542 // Broken IP addresses get marked as such.
543 {"192.168.0.257", L
"192.168.0.257", "192.168.0.257", url_parse::Component(0, 13), CanonHostInfo::BROKEN
, -1, ""},
544 {"[google.com]", L
"[google.com]", "[google.com]", url_parse::Component(0, 12), CanonHostInfo::BROKEN
, -1, ""},
545 // Cyrillic letter followed by '(' should return punycode for '(' escaped
546 // before punycode string was created. I.e.
547 // if '(' is escaped after punycode is created we would get xn--%28-8tb
549 {"\xd1\x82(", L
"\x0442(", "xn--%28-7ed", url_parse::Component(0, 11),
550 CanonHostInfo::NEUTRAL
, -1, ""},
551 // Address with all hexidecimal characters with leading number of 1<<32
552 // or greater and should return NEUTRAL rather than BROKEN if not all
553 // components are numbers.
554 {"12345678912345.de", L
"12345678912345.de", "12345678912345.de", url_parse::Component(0, 17), CanonHostInfo::NEUTRAL
, -1, ""},
555 {"1.12345678912345.de", L
"1.12345678912345.de", "1.12345678912345.de", url_parse::Component(0, 19), CanonHostInfo::NEUTRAL
, -1, ""},
556 {"12345678912345.12345678912345.de", L
"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", url_parse::Component(0, 32), CanonHostInfo::NEUTRAL
, -1, ""},
557 {"1.2.0xB3A73CE5B59.de", L
"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", url_parse::Component(0, 20), CanonHostInfo::NEUTRAL
, -1, ""},
558 {"12345678912345.0xde", L
"12345678912345.0xde", "12345678912345.0xde", url_parse::Component(0, 19), CanonHostInfo::BROKEN
, -1, ""},
561 // CanonicalizeHost() non-verbose.
563 for (size_t i
= 0; i
< arraysize(host_cases
); i
++) {
565 if (host_cases
[i
].input8
) {
566 int host_len
= static_cast<int>(strlen(host_cases
[i
].input8
));
567 url_parse::Component
in_comp(0, host_len
);
568 url_parse::Component out_comp
;
571 url_canon::StdStringCanonOutput
output(&out_str
);
573 bool success
= url_canon::CanonicalizeHost(host_cases
[i
].input8
, in_comp
,
577 EXPECT_EQ(host_cases
[i
].expected_family
!= CanonHostInfo::BROKEN
,
578 success
) << "for input: " << host_cases
[i
].input8
;
579 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
) <<
580 "for input: " << host_cases
[i
].input8
;
581 EXPECT_EQ(host_cases
[i
].expected_component
.begin
, out_comp
.begin
) <<
582 "for input: " << host_cases
[i
].input8
;
583 EXPECT_EQ(host_cases
[i
].expected_component
.len
, out_comp
.len
) <<
584 "for input: " << host_cases
[i
].input8
;
588 if (host_cases
[i
].input16
) {
589 base::string16
input16(WStringToUTF16(host_cases
[i
].input16
));
590 int host_len
= static_cast<int>(input16
.length());
591 url_parse::Component
in_comp(0, host_len
);
592 url_parse::Component out_comp
;
595 url_canon::StdStringCanonOutput
output(&out_str
);
597 bool success
= url_canon::CanonicalizeHost(input16
.c_str(), in_comp
,
601 EXPECT_EQ(host_cases
[i
].expected_family
!= CanonHostInfo::BROKEN
,
603 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
604 EXPECT_EQ(host_cases
[i
].expected_component
.begin
, out_comp
.begin
);
605 EXPECT_EQ(host_cases
[i
].expected_component
.len
, out_comp
.len
);
609 // CanonicalizeHostVerbose()
610 for (size_t i
= 0; i
< arraysize(host_cases
); i
++) {
612 if (host_cases
[i
].input8
) {
613 int host_len
= static_cast<int>(strlen(host_cases
[i
].input8
));
614 url_parse::Component
in_comp(0, host_len
);
617 url_canon::StdStringCanonOutput
output(&out_str
);
618 CanonHostInfo host_info
;
620 url_canon::CanonicalizeHostVerbose(host_cases
[i
].input8
, in_comp
,
621 &output
, &host_info
);
624 EXPECT_EQ(host_cases
[i
].expected_family
, host_info
.family
);
625 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
626 EXPECT_EQ(host_cases
[i
].expected_component
.begin
,
627 host_info
.out_host
.begin
);
628 EXPECT_EQ(host_cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
629 EXPECT_EQ(std::string(host_cases
[i
].expected_address_hex
),
630 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
631 if (host_cases
[i
].expected_family
== CanonHostInfo::IPV4
) {
632 EXPECT_EQ(host_cases
[i
].expected_num_ipv4_components
,
633 host_info
.num_ipv4_components
);
638 if (host_cases
[i
].input16
) {
639 base::string16
input16(WStringToUTF16(host_cases
[i
].input16
));
640 int host_len
= static_cast<int>(input16
.length());
641 url_parse::Component
in_comp(0, host_len
);
644 url_canon::StdStringCanonOutput
output(&out_str
);
645 CanonHostInfo host_info
;
647 url_canon::CanonicalizeHostVerbose(input16
.c_str(), in_comp
,
648 &output
, &host_info
);
651 EXPECT_EQ(host_cases
[i
].expected_family
, host_info
.family
);
652 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
653 EXPECT_EQ(host_cases
[i
].expected_component
.begin
,
654 host_info
.out_host
.begin
);
655 EXPECT_EQ(host_cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
656 EXPECT_EQ(std::string(host_cases
[i
].expected_address_hex
),
657 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
658 if (host_cases
[i
].expected_family
== CanonHostInfo::IPV4
) {
659 EXPECT_EQ(host_cases
[i
].expected_num_ipv4_components
,
660 host_info
.num_ipv4_components
);
666 TEST(URLCanonTest
, IPv4
) {
667 IPAddressCase cases
[] = {
668 // Empty is not an IP address.
669 {"", L
"", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
670 {".", L
".", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
671 // Regular IP addresses in different bases.
672 {"192.168.0.1", L
"192.168.0.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 4, "C0A80001"},
673 {"0300.0250.00.01", L
"0300.0250.00.01", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 4, "C0A80001"},
674 {"0xC0.0Xa8.0x0.0x1", L
"0xC0.0Xa8.0x0.0x1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 4, "C0A80001"},
675 // Non-IP addresses due to invalid characters.
676 {"192.168.9.com", L
"192.168.9.com", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
677 // Invalid characters for the base should be rejected.
678 {"19a.168.0.1", L
"19a.168.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
679 {"0308.0250.00.01", L
"0308.0250.00.01", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
680 {"0xCG.0xA8.0x0.0x1", L
"0xCG.0xA8.0x0.0x1", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
681 // If there are not enough components, the last one should fill them out.
682 {"192", L
"192", "0.0.0.192", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 1, "000000C0"},
683 {"0xC0a80001", L
"0xC0a80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 1, "C0A80001"},
684 {"030052000001", L
"030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 1, "C0A80001"},
685 {"000030052000001", L
"000030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 1, "C0A80001"},
686 {"192.168", L
"192.168", "192.0.0.168", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 2, "C00000A8"},
687 {"192.0x00A80001", L
"192.0x000A80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 2, "C0A80001"},
688 {"0xc0.052000001", L
"0xc0.052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 2, "C0A80001"},
689 {"192.168.1", L
"192.168.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3, "C0A80001"},
690 // Too many components means not an IP address.
691 {"192.168.0.0.1", L
"192.168.0.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
692 // We allow a single trailing dot.
693 {"192.168.0.1.", L
"192.168.0.1.", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 4, "C0A80001"},
694 {"192.168.0.1. hello", L
"192.168.0.1. hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
695 {"192.168.0.1..", L
"192.168.0.1..", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
696 // Two dots in a row means not an IP address.
697 {"192.168..1", L
"192.168..1", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
698 // Any numerical overflow should be marked as BROKEN.
699 {"0x100.0", L
"0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
700 {"0x100.0.0", L
"0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
701 {"0x100.0.0.0", L
"0x100.0.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
702 {"0.0x100.0.0", L
"0.0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
703 {"0.0.0x100.0", L
"0.0.0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
704 {"0.0.0.0x100", L
"0.0.0.0x100", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
705 {"0.0.0x10000", L
"0.0.0x10000", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
706 {"0.0x1000000", L
"0.0x1000000", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
707 {"0x100000000", L
"0x100000000", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
708 // Repeat the previous tests, minus 1, to verify boundaries.
709 {"0xFF.0", L
"0xFF.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 2, "FF000000"},
710 {"0xFF.0.0", L
"0xFF.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 3, "FF000000"},
711 {"0xFF.0.0.0", L
"0xFF.0.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 4, "FF000000"},
712 {"0.0xFF.0.0", L
"0.0xFF.0.0", "0.255.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 4, "00FF0000"},
713 {"0.0.0xFF.0", L
"0.0.0xFF.0", "0.0.255.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 4, "0000FF00"},
714 {"0.0.0.0xFF", L
"0.0.0.0xFF", "0.0.0.255", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 4, "000000FF"},
715 {"0.0.0xFFFF", L
"0.0.0xFFFF", "0.0.255.255", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3, "0000FFFF"},
716 {"0.0xFFFFFF", L
"0.0xFFFFFF", "0.255.255.255", url_parse::Component(0, 13), CanonHostInfo::IPV4
, 2, "00FFFFFF"},
717 {"0xFFFFFFFF", L
"0xFFFFFFFF", "255.255.255.255", url_parse::Component(0, 15), CanonHostInfo::IPV4
, 1, "FFFFFFFF"},
718 // Old trunctations tests. They're all "BROKEN" now.
719 {"276.256.0xf1a2.077777", L
"276.256.0xf1a2.077777", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
720 {"192.168.0.257", L
"192.168.0.257", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
721 {"192.168.0xa20001", L
"192.168.0xa20001", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
722 {"192.015052000001", L
"192.015052000001", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
723 {"0X12C0a80001", L
"0X12C0a80001", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
724 {"276.1.2", L
"276.1.2", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
725 // Spaces should be rejected.
726 {"192.168.0.1 hello", L
"192.168.0.1 hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
727 // Very large numbers.
728 {"0000000000000300.0x00000000000000fF.00000000000000001", L
"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3, "C0FF0001"},
729 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L
"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", url_parse::Component(0, 11), CanonHostInfo::BROKEN
, -1, ""},
730 // A number has no length limit, but long numbers can still overflow.
731 {"00000000000000000001", L
"00000000000000000001", "0.0.0.1", url_parse::Component(0, 7), CanonHostInfo::IPV4
, 1, "00000001"},
732 {"0000000000000000100000000000000001", L
"0000000000000000100000000000000001", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
733 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
734 {"0.0.0.000000000000000000z", L
"0.0.0.000000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
735 {"0.0.0.100000000000000000z", L
"0.0.0.100000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
736 // Truncation of all zeros should still result in 0.
737 {"0.00.0x.0x0", L
"0.00.0x.0x0", "0.0.0.0", url_parse::Component(0, 7), CanonHostInfo::IPV4
, 4, "00000000"},
740 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
742 url_parse::Component
component(0,
743 static_cast<int>(strlen(cases
[i
].input8
)));
745 std::string out_str1
;
746 url_canon::StdStringCanonOutput
output1(&out_str1
);
747 url_canon::CanonHostInfo host_info
;
748 url_canon::CanonicalizeIPAddress(cases
[i
].input8
, component
, &output1
,
752 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
753 EXPECT_EQ(std::string(cases
[i
].expected_address_hex
),
754 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
755 if (host_info
.family
== CanonHostInfo::IPV4
) {
756 EXPECT_STREQ(cases
[i
].expected
, out_str1
.c_str());
757 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
758 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
759 EXPECT_EQ(cases
[i
].expected_num_ipv4_components
,
760 host_info
.num_ipv4_components
);
764 base::string16
input16(WStringToUTF16(cases
[i
].input16
));
765 component
= url_parse::Component(0, static_cast<int>(input16
.length()));
767 std::string out_str2
;
768 url_canon::StdStringCanonOutput
output2(&out_str2
);
769 url_canon::CanonicalizeIPAddress(input16
.c_str(), component
, &output2
,
773 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
774 EXPECT_EQ(std::string(cases
[i
].expected_address_hex
),
775 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
776 if (host_info
.family
== CanonHostInfo::IPV4
) {
777 EXPECT_STREQ(cases
[i
].expected
, out_str2
.c_str());
778 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
779 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
780 EXPECT_EQ(cases
[i
].expected_num_ipv4_components
,
781 host_info
.num_ipv4_components
);
786 TEST(URLCanonTest
, IPv6
) {
787 IPAddressCase cases
[] = {
788 // Empty is not an IP address.
789 {"", L
"", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1, ""},
790 // Non-IPs with [:] characters are marked BROKEN.
791 {":", L
":", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
792 {"[", L
"[", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
793 {"[:", L
"[:", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
794 {"]", L
"]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
795 {":]", L
":]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
796 {"[]", L
"[]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
797 {"[:]", L
"[:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
798 // Regular IP address is invalid without bounding '[' and ']'.
799 {"2001:db8::1", L
"2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
800 {"[2001:db8::1", L
"[2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
801 {"2001:db8::1]", L
"2001:db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
802 // Regular IP addresses.
803 {"[::]", L
"[::]", "[::]", url_parse::Component(0,4), CanonHostInfo::IPV6
, -1, "00000000000000000000000000000000"},
804 {"[::1]", L
"[::1]", "[::1]", url_parse::Component(0,5), CanonHostInfo::IPV6
, -1, "00000000000000000000000000000001"},
805 {"[1::]", L
"[1::]", "[1::]", url_parse::Component(0,5), CanonHostInfo::IPV6
, -1, "00010000000000000000000000000000"},
807 // Leading zeros should be stripped.
808 {"[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]", url_parse::Component(0,17), CanonHostInfo::IPV6
, -1, "00000001000200030004000500060007"},
810 // Upper case letters should be lowercased.
811 {"[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]", url_parse::Component(0,20), CanonHostInfo::IPV6
, -1, "000A000B000C00DE00FF0000000100AC"},
813 // The same address can be written with different contractions, but should
814 // get canonicalized to the same thing.
815 {"[1:0:0:2::3:0]", L
"[1:0:0:2::3:0]", "[1::2:0:0:3:0]", url_parse::Component(0,14), CanonHostInfo::IPV6
, -1, "00010000000000020000000000030000"},
816 {"[1::2:0:0:3:0]", L
"[1::2:0:0:3:0]", "[1::2:0:0:3:0]", url_parse::Component(0,14), CanonHostInfo::IPV6
, -1, "00010000000000020000000000030000"},
818 // Addresses with embedded IPv4.
819 {"[::192.168.0.1]", L
"[::192.168.0.1]", "[::c0a8:1]", url_parse::Component(0,10), CanonHostInfo::IPV6
, -1, "000000000000000000000000C0A80001"},
820 {"[::ffff:192.168.0.1]", L
"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6
, -1, "00000000000000000000FFFFC0A80001"},
821 {"[::eeee:192.168.0.1]", L
"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", url_parse::Component(0, 15), CanonHostInfo::IPV6
, -1, "00000000000000000000EEEEC0A80001"},
822 {"[2001::192.168.0.1]", L
"[2001::192.168.0.1]", "[2001::c0a8:1]", url_parse::Component(0, 14), CanonHostInfo::IPV6
, -1, "200100000000000000000000C0A80001"},
823 {"[1:2:192.168.0.1:5:6]", L
"[1:2:192.168.0.1:5:6]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
825 // IPv4 with last component missing.
826 {"[::ffff:192.1.2]", L
"[::ffff:192.1.2]", "[::ffff:c001:2]", url_parse::Component(0,15), CanonHostInfo::IPV6
, -1, "00000000000000000000FFFFC0010002"},
829 // TODO(eroman): Should this format be disallowed?
830 {"[::ffff:0xC0.0Xa8.0x0.0x1]", L
"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6
, -1, "00000000000000000000FFFFC0A80001"},
832 // There may be zeros surrounding the "::" contraction.
833 {"[0:0::0:0:8]", L
"[0:0::0:0:8]", "[::8]", url_parse::Component(0,5), CanonHostInfo::IPV6
, -1, "00000000000000000000000000000008"},
835 {"[2001:db8::1]", L
"[2001:db8::1]", "[2001:db8::1]", url_parse::Component(0,13), CanonHostInfo::IPV6
, -1, "20010DB8000000000000000000000001"},
837 // Can only have one "::" contraction in an IPv6 string literal.
838 {"[2001::db8::1]", L
"[2001::db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
839 // No more than 2 consecutive ':'s.
840 {"[2001:db8:::1]", L
"[2001:db8:::1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
841 {"[:::]", L
"[:::]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
842 // Non-IP addresses due to invalid characters.
843 {"[2001::.com]", L
"[2001::.com]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
844 // If there are not enough components, the last one should fill them out.
845 // ... omitted at this time ...
846 // Too many components means not an IP address. Similarly with too few if using IPv4 compat or mapped addresses.
847 {"[::192.168.0.0.1]", L
"[::192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
848 {"[::ffff:192.168.0.0.1]", L
"[::ffff:192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
849 {"[1:2:3:4:5:6:7:8:9]", L
"[1:2:3:4:5:6:7:8:9]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
850 // Too many bits (even though 8 comonents, the last one holds 32 bits).
851 {"[0:0:0:0:0:0:0:192.168.0.1]", L
"[0:0:0:0:0:0:0:192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
853 // Too many bits specified -- the contraction would have to be zero-length
854 // to not exceed 128 bits.
855 {"[1:2:3:4:5:6::192.168.0.1]", L
"[1:2:3:4:5:6::192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
857 // The contraction is for 16 bits of zero.
858 {"[1:2:3:4:5:6::8]", L
"[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]", url_parse::Component(0,17), CanonHostInfo::IPV6
, -1, "00010002000300040005000600000008"},
860 // Cannot have a trailing colon.
861 {"[1:2:3:4:5:6:7:8:]", L
"[1:2:3:4:5:6:7:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
862 {"[1:2:3:4:5:6:192.168.0.1:]", L
"[1:2:3:4:5:6:192.168.0.1:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
864 // Cannot have negative numbers.
865 {"[-1:2:3:4:5:6:7:8]", L
"[-1:2:3:4:5:6:7:8]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
867 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
868 // The scope_id should be included in the canonicalized URL, and is an
869 // unsigned decimal number.
871 // Invalid because no ID was given after the percent.
873 // Don't allow scope-id
874 {"[1::%1]", L
"[1::%1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
875 {"[1::%eth0]", L
"[1::%eth0]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
876 {"[1::%]", L
"[1::%]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
877 {"[%]", L
"[%]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
878 {"[::%:]", L
"[::%:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
880 // Don't allow leading or trailing colons.
881 {"[:0:0::0:0:8]", L
"[:0:0::0:0:8]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
882 {"[0:0::0:0:8:]", L
"[0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
883 {"[:0:0::0:0:8:]", L
"[:0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
885 // We allow a single trailing dot.
886 // ... omitted at this time ...
887 // Two dots in a row means not an IP address.
888 {"[::192.168..1]", L
"[::192.168..1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
889 // Any non-first components get truncated to one byte.
890 // ... omitted at this time ...
891 // Spaces should be rejected.
892 {"[::1 hello]", L
"[::1 hello]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1, ""},
895 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
897 url_parse::Component
component(0,
898 static_cast<int>(strlen(cases
[i
].input8
)));
900 std::string out_str1
;
901 url_canon::StdStringCanonOutput
output1(&out_str1
);
902 url_canon::CanonHostInfo host_info
;
903 url_canon::CanonicalizeIPAddress(cases
[i
].input8
, component
, &output1
,
907 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
908 EXPECT_EQ(std::string(cases
[i
].expected_address_hex
),
909 BytesToHexString(host_info
.address
, host_info
.AddressLength())) << "iter " << i
<< " host " << cases
[i
].input8
;
910 if (host_info
.family
== CanonHostInfo::IPV6
) {
911 EXPECT_STREQ(cases
[i
].expected
, out_str1
.c_str());
912 EXPECT_EQ(cases
[i
].expected_component
.begin
,
913 host_info
.out_host
.begin
);
914 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
918 base::string16
input16(WStringToUTF16(cases
[i
].input16
));
919 component
= url_parse::Component(0, static_cast<int>(input16
.length()));
921 std::string out_str2
;
922 url_canon::StdStringCanonOutput
output2(&out_str2
);
923 url_canon::CanonicalizeIPAddress(input16
.c_str(), component
, &output2
,
927 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
928 EXPECT_EQ(std::string(cases
[i
].expected_address_hex
),
929 BytesToHexString(host_info
.address
, host_info
.AddressLength()));
930 if (host_info
.family
== CanonHostInfo::IPV6
) {
931 EXPECT_STREQ(cases
[i
].expected
, out_str2
.c_str());
932 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
933 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
938 TEST(URLCanonTest
, IPEmpty
) {
939 std::string out_str1
;
940 url_canon::StdStringCanonOutput
output1(&out_str1
);
941 url_canon::CanonHostInfo host_info
;
944 const char spec
[] = "192.168.0.1";
945 url_canon::CanonicalizeIPAddress(spec
, url_parse::Component(),
946 &output1
, &host_info
);
947 EXPECT_FALSE(host_info
.IsIPAddress());
949 url_canon::CanonicalizeIPAddress(spec
, url_parse::Component(0, 0),
950 &output1
, &host_info
);
951 EXPECT_FALSE(host_info
.IsIPAddress());
954 TEST(URLCanonTest
, UserInfo
) {
955 // Note that the canonicalizer should escape and treat empty components as
958 // We actually parse a full input URL so we can get the initial components.
959 struct UserComponentCase
{
961 const char* expected
;
962 url_parse::Component expected_username
;
963 url_parse::Component expected_password
;
964 bool expected_success
;
965 } user_info_cases
[] = {
966 {"http://user:pass@host.com/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
967 {"http://@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
968 {"http://:@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
969 {"http://foo:@host.com/", "foo@", url_parse::Component(0, 3), url_parse::Component(0, -1), true},
970 {"http://:foo@host.com/", ":foo@", url_parse::Component(0, 0), url_parse::Component(1, 3), true},
971 {"http://^ :$\t@host.com/", "%5E%20:$%09@", url_parse::Component(0, 6), url_parse::Component(7, 4), true},
972 {"http://user:pass@/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
973 {"http://%2540:bar@domain.com/", "%2540:bar@", url_parse::Component(0, 5), url_parse::Component(6, 3), true },
975 // IE7 compatability: old versions allowed backslashes in usernames, but
976 // IE7 does not. We disallow it as well.
977 {"ftp://me\\mydomain:pass@foo.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
980 for (size_t i
= 0; i
< ARRAYSIZE(user_info_cases
); i
++) {
981 int url_len
= static_cast<int>(strlen(user_info_cases
[i
].input
));
982 url_parse::Parsed parsed
;
983 url_parse::ParseStandardURL(user_info_cases
[i
].input
, url_len
, &parsed
);
984 url_parse::Component out_user
, out_pass
;
986 url_canon::StdStringCanonOutput
output1(&out_str
);
988 bool success
= url_canon::CanonicalizeUserInfo(user_info_cases
[i
].input
,
990 user_info_cases
[i
].input
,
996 EXPECT_EQ(user_info_cases
[i
].expected_success
, success
);
997 EXPECT_EQ(std::string(user_info_cases
[i
].expected
), out_str
);
998 EXPECT_EQ(user_info_cases
[i
].expected_username
.begin
, out_user
.begin
);
999 EXPECT_EQ(user_info_cases
[i
].expected_username
.len
, out_user
.len
);
1000 EXPECT_EQ(user_info_cases
[i
].expected_password
.begin
, out_pass
.begin
);
1001 EXPECT_EQ(user_info_cases
[i
].expected_password
.len
, out_pass
.len
);
1003 // Now try the wide version
1005 url_canon::StdStringCanonOutput
output2(&out_str
);
1006 base::string16
wide_input(ConvertUTF8ToUTF16(user_info_cases
[i
].input
));
1007 success
= url_canon::CanonicalizeUserInfo(wide_input
.c_str(),
1011 &output2
, &out_user
, &out_pass
);
1014 EXPECT_EQ(user_info_cases
[i
].expected_success
, success
);
1015 EXPECT_EQ(std::string(user_info_cases
[i
].expected
), out_str
);
1016 EXPECT_EQ(user_info_cases
[i
].expected_username
.begin
, out_user
.begin
);
1017 EXPECT_EQ(user_info_cases
[i
].expected_username
.len
, out_user
.len
);
1018 EXPECT_EQ(user_info_cases
[i
].expected_password
.begin
, out_pass
.begin
);
1019 EXPECT_EQ(user_info_cases
[i
].expected_password
.len
, out_pass
.len
);
1023 TEST(URLCanonTest
, Port
) {
1024 // We only need to test that the number gets properly put into the output
1025 // buffer. The parser unit tests will test scanning the number correctly.
1027 // Note that the CanonicalizePort will always prepend a colon to the output
1028 // to separate it from the colon that it assumes preceeds it.
1032 const char* expected
;
1033 url_parse::Component expected_component
;
1034 bool expected_success
;
1036 // Invalid input should be copied w/ failure.
1037 {"as df", 80, ":as%20df", url_parse::Component(1, 7), false},
1038 {"-2", 80, ":-2", url_parse::Component(1, 2), false},
1039 // Default port should be omitted.
1040 {"80", 80, "", url_parse::Component(0, -1), true},
1041 {"8080", 80, ":8080", url_parse::Component(1, 4), true},
1042 // PORT_UNSPECIFIED should mean always keep the port.
1043 {"80", url_parse::PORT_UNSPECIFIED
, ":80", url_parse::Component(1, 2), true},
1046 for (size_t i
= 0; i
< ARRAYSIZE(port_cases
); i
++) {
1047 int url_len
= static_cast<int>(strlen(port_cases
[i
].input
));
1048 url_parse::Component
in_comp(0, url_len
);
1049 url_parse::Component out_comp
;
1050 std::string out_str
;
1051 url_canon::StdStringCanonOutput
output1(&out_str
);
1052 bool success
= url_canon::CanonicalizePort(port_cases
[i
].input
, in_comp
,
1053 port_cases
[i
].default_port
,
1054 &output1
, &out_comp
);
1057 EXPECT_EQ(port_cases
[i
].expected_success
, success
);
1058 EXPECT_EQ(std::string(port_cases
[i
].expected
), out_str
);
1059 EXPECT_EQ(port_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1060 EXPECT_EQ(port_cases
[i
].expected_component
.len
, out_comp
.len
);
1062 // Now try the wide version
1064 url_canon::StdStringCanonOutput
output2(&out_str
);
1065 base::string16
wide_input(ConvertUTF8ToUTF16(port_cases
[i
].input
));
1066 success
= url_canon::CanonicalizePort(wide_input
.c_str(), in_comp
,
1067 port_cases
[i
].default_port
,
1068 &output2
, &out_comp
);
1071 EXPECT_EQ(port_cases
[i
].expected_success
, success
);
1072 EXPECT_EQ(std::string(port_cases
[i
].expected
), out_str
);
1073 EXPECT_EQ(port_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1074 EXPECT_EQ(port_cases
[i
].expected_component
.len
, out_comp
.len
);
1078 TEST(URLCanonTest
, Path
) {
1079 DualComponentCase path_cases
[] = {
1080 // ----- path collapsing tests -----
1081 {"/././foo", L
"/././foo", "/foo", url_parse::Component(0, 4), true},
1082 {"/./.foo", L
"/./.foo", "/.foo", url_parse::Component(0, 5), true},
1083 {"/foo/.", L
"/foo/.", "/foo/", url_parse::Component(0, 5), true},
1084 {"/foo/./", L
"/foo/./", "/foo/", url_parse::Component(0, 5), true},
1085 // double dots followed by a slash or the end of the string count
1086 {"/foo/bar/..", L
"/foo/bar/..", "/foo/", url_parse::Component(0, 5), true},
1087 {"/foo/bar/../", L
"/foo/bar/../", "/foo/", url_parse::Component(0, 5), true},
1088 // don't count double dots when they aren't followed by a slash
1089 {"/foo/..bar", L
"/foo/..bar", "/foo/..bar", url_parse::Component(0, 10), true},
1090 // some in the middle
1091 {"/foo/bar/../ton", L
"/foo/bar/../ton", "/foo/ton", url_parse::Component(0, 8), true},
1092 {"/foo/bar/../ton/../../a", L
"/foo/bar/../ton/../../a", "/a", url_parse::Component(0, 2), true},
1093 // we should not be able to go above the root
1094 {"/foo/../../..", L
"/foo/../../..", "/", url_parse::Component(0, 1), true},
1095 {"/foo/../../../ton", L
"/foo/../../../ton", "/ton", url_parse::Component(0, 4), true},
1096 // escaped dots should be unescaped and treated the same as dots
1097 {"/foo/%2e", L
"/foo/%2e", "/foo/", url_parse::Component(0, 5), true},
1098 {"/foo/%2e%2", L
"/foo/%2e%2", "/foo/.%2", url_parse::Component(0, 8), true},
1099 {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L
"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", url_parse::Component(0, 6), true},
1100 // Multiple slashes in a row should be preserved and treated like empty
1102 {"////../..", L
"////../..", "//", url_parse::Component(0, 2), true},
1104 // ----- escaping tests -----
1105 {"/foo", L
"/foo", "/foo", url_parse::Component(0, 4), true},
1106 // Valid escape sequence
1107 {"/%20foo", L
"/%20foo", "/%20foo", url_parse::Component(0, 7), true},
1108 // Invalid escape sequence we should pass through unchanged.
1109 {"/foo%", L
"/foo%", "/foo%", url_parse::Component(0, 5), true},
1110 {"/foo%2", L
"/foo%2", "/foo%2", url_parse::Component(0, 6), true},
1111 // Invalid escape sequence: bad characters should be treated the same as
1112 // the sourrounding text, not as escaped (in this case, UTF-8).
1113 {"/foo%2zbar", L
"/foo%2zbar", "/foo%2zbar", url_parse::Component(0, 10), true},
1114 {"/foo%2\xc2\xa9zbar", NULL
, "/foo%2%C2%A9zbar", url_parse::Component(0, 16), true},
1115 {NULL
, L
"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", url_parse::Component(0, 22), true},
1116 // Regular characters that are escaped should be unescaped
1117 {"/foo%41%7a", L
"/foo%41%7a", "/fooAz", url_parse::Component(0, 6), true},
1118 // Funny characters that are unescaped should be escaped
1119 {"/foo\x09\x91%91", NULL
, "/foo%09%91%91", url_parse::Component(0, 13), true},
1120 {NULL
, L
"/foo\x09\x91%91", "/foo%09%C2%91%91", url_parse::Component(0, 16), true},
1121 // Invalid characters that are escaped should cause a failure.
1122 {"/foo%00%51", L
"/foo%00%51", "/foo%00Q", url_parse::Component(0, 8), false},
1123 // Some characters should be passed through unchanged regardless of esc.
1124 {"/(%28:%3A%29)", L
"/(%28:%3A%29)", "/(%28:%3A%29)", url_parse::Component(0, 13), true},
1125 // Characters that are properly escaped should not have the case changed
1127 {"/%3A%3a%3C%3c", L
"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", url_parse::Component(0, 13), true},
1128 // Funny characters that are unescaped should be escaped
1129 {"/foo\tbar", L
"/foo\tbar", "/foo%09bar", url_parse::Component(0, 10), true},
1130 // Backslashes should get converted to forward slashes
1131 {"\\foo\\bar", L
"\\foo\\bar", "/foo/bar", url_parse::Component(0, 8), true},
1132 // Hashes found in paths (possibly only when the caller explicitly sets
1133 // the path on an already-parsed URL) should be escaped.
1134 {"/foo#bar", L
"/foo#bar", "/foo%23bar", url_parse::Component(0, 10), true},
1135 // %7f should be allowed and %3D should not be unescaped (these were wrong
1136 // in a previous version).
1137 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L
"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", url_parse::Component(0, 24), true},
1138 // @ should be passed through unchanged (escaped or unescaped).
1139 {"/@asdf%40", L
"/@asdf%40", "/@asdf%40", url_parse::Component(0, 9), true},
1141 // ----- encoding tests -----
1142 // Basic conversions
1143 {"/\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", url_parse::Component(0, 37), true},
1144 // Invalid unicode characters should fail. We only do validation on
1145 // UTF-16 input, so this doesn't happen on 8-bit.
1146 {"/\xef\xb7\x90zyx", NULL
, "/%EF%B7%90zyx", url_parse::Component(0, 13), true},
1147 {NULL
, L
"/\xfdd0zyx", "/%EF%BF%BDzyx", url_parse::Component(0, 13), false},
1150 for (size_t i
= 0; i
< arraysize(path_cases
); i
++) {
1151 if (path_cases
[i
].input8
) {
1152 int len
= static_cast<int>(strlen(path_cases
[i
].input8
));
1153 url_parse::Component
in_comp(0, len
);
1154 url_parse::Component out_comp
;
1155 std::string out_str
;
1156 url_canon::StdStringCanonOutput
output(&out_str
);
1157 bool success
= url_canon::CanonicalizePath(path_cases
[i
].input8
, in_comp
,
1158 &output
, &out_comp
);
1161 EXPECT_EQ(path_cases
[i
].expected_success
, success
);
1162 EXPECT_EQ(path_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1163 EXPECT_EQ(path_cases
[i
].expected_component
.len
, out_comp
.len
);
1164 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
1167 if (path_cases
[i
].input16
) {
1168 base::string16
input16(WStringToUTF16(path_cases
[i
].input16
));
1169 int len
= static_cast<int>(input16
.length());
1170 url_parse::Component
in_comp(0, len
);
1171 url_parse::Component out_comp
;
1172 std::string out_str
;
1173 url_canon::StdStringCanonOutput
output(&out_str
);
1175 bool success
= url_canon::CanonicalizePath(input16
.c_str(), in_comp
,
1176 &output
, &out_comp
);
1179 EXPECT_EQ(path_cases
[i
].expected_success
, success
);
1180 EXPECT_EQ(path_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1181 EXPECT_EQ(path_cases
[i
].expected_component
.len
, out_comp
.len
);
1182 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
1186 // Manual test: embedded NULLs should be escaped and the URL should be marked
1188 const char path_with_null
[] = "/ab\0c";
1189 url_parse::Component
in_comp(0, 5);
1190 url_parse::Component out_comp
;
1192 std::string out_str
;
1193 url_canon::StdStringCanonOutput
output(&out_str
);
1194 bool success
= url_canon::CanonicalizePath(path_with_null
, in_comp
,
1195 &output
, &out_comp
);
1197 EXPECT_FALSE(success
);
1198 EXPECT_EQ("/ab%00c", out_str
);
1201 TEST(URLCanonTest
, Query
) {
1204 const wchar_t* input16
;
1205 const char* encoding
;
1206 const char* expected
;
1208 // Regular ASCII case in some different encodings.
1209 {"foo=bar", L
"foo=bar", NULL
, "?foo=bar"},
1210 {"foo=bar", L
"foo=bar", "utf-8", "?foo=bar"},
1211 {"foo=bar", L
"foo=bar", "shift_jis", "?foo=bar"},
1212 {"foo=bar", L
"foo=bar", "gb2312", "?foo=bar"},
1213 // Allow question marks in the query without escaping
1214 {"as?df", L
"as?df", NULL
, "?as?df"},
1215 // Always escape '#' since it would mark the ref.
1216 {"as#df", L
"as#df", NULL
, "?as%23df"},
1217 // Escape some questionable 8-bit characters, but never unescape.
1218 {"\x02hello\x7f bye", L
"\x02hello\x7f bye", NULL
, "?%02hello%7F%20bye"},
1219 {"%40%41123", L
"%40%41123", NULL
, "?%40%41123"},
1220 // Chinese input/output
1221 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", NULL
, "?q=%E4%BD%A0%E5%A5%BD"},
1222 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", "gb2312", "?q=%C4%E3%BA%C3"},
1223 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", "big5", "?q=%A7A%A6n"},
1224 // Unencodable character in the destination character set should be
1225 // escaped. The escape sequence unescapes to be the entity name:
1227 {"q=Chinese\xef\xbc\xa7", L
"q=Chinese\xff27", "iso-8859-1", "?q=Chinese%26%2365319%3B"},
1228 // Invalid UTF-8/16 input should be replaced with invalid characters.
1229 {"q=\xed\xed", L
"q=\xd800\xd800", NULL
, "?q=%EF%BF%BD%EF%BF%BD"},
1230 // Don't allow < or > because sometimes they are used for XSS if the
1231 // URL is echoed in content. Firefox does this, IE doesn't.
1232 {"q=<asdf>", L
"q=<asdf>", NULL
, "?q=%3Casdf%3E"},
1233 // Escape double quotemarks in the query.
1234 {"q=\"asdf\"", L
"q=\"asdf\"", NULL
, "?q=%22asdf%22"},
1237 for (size_t i
= 0; i
< ARRAYSIZE(query_cases
); i
++) {
1238 url_parse::Component out_comp
;
1240 UConvScoper
conv(query_cases
[i
].encoding
);
1241 ASSERT_TRUE(!query_cases
[i
].encoding
|| conv
.converter());
1242 url_canon::ICUCharsetConverter
converter(conv
.converter());
1244 // Map NULL to a NULL converter pointer.
1245 url_canon::ICUCharsetConverter
* conv_pointer
= &converter
;
1246 if (!query_cases
[i
].encoding
)
1247 conv_pointer
= NULL
;
1249 if (query_cases
[i
].input8
) {
1250 int len
= static_cast<int>(strlen(query_cases
[i
].input8
));
1251 url_parse::Component
in_comp(0, len
);
1252 std::string out_str
;
1254 url_canon::StdStringCanonOutput
output(&out_str
);
1255 url_canon::CanonicalizeQuery(query_cases
[i
].input8
, in_comp
,
1256 conv_pointer
, &output
, &out_comp
);
1259 EXPECT_EQ(query_cases
[i
].expected
, out_str
);
1262 if (query_cases
[i
].input16
) {
1263 base::string16
input16(WStringToUTF16(query_cases
[i
].input16
));
1264 int len
= static_cast<int>(input16
.length());
1265 url_parse::Component
in_comp(0, len
);
1266 std::string out_str
;
1268 url_canon::StdStringCanonOutput
output(&out_str
);
1269 url_canon::CanonicalizeQuery(input16
.c_str(), in_comp
,
1270 conv_pointer
, &output
, &out_comp
);
1273 EXPECT_EQ(query_cases
[i
].expected
, out_str
);
1277 // Extra test for input with embedded NULL;
1278 std::string out_str
;
1279 url_canon::StdStringCanonOutput
output(&out_str
);
1280 url_parse::Component out_comp
;
1281 url_canon::CanonicalizeQuery("a \x00z\x01", url_parse::Component(0, 5), NULL
,
1282 &output
, &out_comp
);
1284 EXPECT_EQ("?a%20%00z%01", out_str
);
1287 TEST(URLCanonTest
, Ref
) {
1288 // Refs are trivial, it just checks the encoding.
1289 DualComponentCase ref_cases
[] = {
1290 // Regular one, we shouldn't escape spaces, et al.
1291 {"hello, world", L
"hello, world", "#hello, world", url_parse::Component(1, 12), true},
1292 // UTF-8/wide input should be preserved
1293 {"\xc2\xa9", L
"\xa9", "#\xc2\xa9", url_parse::Component(1, 2), true},
1294 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
1295 {"\xF0\x90\x8C\x80ss", L
"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", url_parse::Component(1, 6), true},
1296 // Escaping should be preserved unchanged, even invalid ones
1297 {"%41%a", L
"%41%a", "#%41%a", url_parse::Component(1, 5), true},
1298 // Invalid UTF-8/16 input should be flagged and the input made valid
1299 {"\xc2", NULL
, "#\xef\xbf\xbd", url_parse::Component(1, 3), true},
1300 {NULL
, L
"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", url_parse::Component(1, 6), true},
1301 // Test a Unicode invalid character.
1302 {"a\xef\xb7\x90", L
"a\xfdd0", "#a\xef\xbf\xbd", url_parse::Component(1, 4), true},
1303 // Refs can have # signs and we should preserve them.
1304 {"asdf#qwer", L
"asdf#qwer", "#asdf#qwer", url_parse::Component(1, 9), true},
1305 {"#asdf", L
"#asdf", "##asdf", url_parse::Component(1, 5), true},
1308 for (size_t i
= 0; i
< arraysize(ref_cases
); i
++) {
1310 if (ref_cases
[i
].input8
) {
1311 int len
= static_cast<int>(strlen(ref_cases
[i
].input8
));
1312 url_parse::Component
in_comp(0, len
);
1313 url_parse::Component out_comp
;
1315 std::string out_str
;
1316 url_canon::StdStringCanonOutput
output(&out_str
);
1317 url_canon::CanonicalizeRef(ref_cases
[i
].input8
, in_comp
,
1318 &output
, &out_comp
);
1321 EXPECT_EQ(ref_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1322 EXPECT_EQ(ref_cases
[i
].expected_component
.len
, out_comp
.len
);
1323 EXPECT_EQ(ref_cases
[i
].expected
, out_str
);
1327 if (ref_cases
[i
].input16
) {
1328 base::string16
input16(WStringToUTF16(ref_cases
[i
].input16
));
1329 int len
= static_cast<int>(input16
.length());
1330 url_parse::Component
in_comp(0, len
);
1331 url_parse::Component out_comp
;
1333 std::string out_str
;
1334 url_canon::StdStringCanonOutput
output(&out_str
);
1335 url_canon::CanonicalizeRef(input16
.c_str(), in_comp
, &output
, &out_comp
);
1338 EXPECT_EQ(ref_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1339 EXPECT_EQ(ref_cases
[i
].expected_component
.len
, out_comp
.len
);
1340 EXPECT_EQ(ref_cases
[i
].expected
, out_str
);
1344 // Try one with an embedded NULL. It should be stripped.
1345 const char null_input
[5] = "ab\x00z";
1346 url_parse::Component
null_input_component(0, 4);
1347 url_parse::Component out_comp
;
1349 std::string out_str
;
1350 url_canon::StdStringCanonOutput
output(&out_str
);
1351 url_canon::CanonicalizeRef(null_input
, null_input_component
,
1352 &output
, &out_comp
);
1355 EXPECT_EQ(1, out_comp
.begin
);
1356 EXPECT_EQ(3, out_comp
.len
);
1357 EXPECT_EQ("#abz", out_str
);
1360 TEST(URLCanonTest
, CanonicalizeStandardURL
) {
1361 // The individual component canonicalize tests should have caught the cases
1362 // for each of those components. Here, we just need to test that the various
1363 // parts are included or excluded properly, and have the correct separators.
1366 const char* expected
;
1367 bool expected_success
;
1369 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1370 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1371 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1372 {"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo", true},
1373 {"www.google.com", ":www.google.com/", true},
1374 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1375 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1376 {"http://user:pass@/", "http://user:pass@/", false},
1377 {"http://%25DOMAIN:foobar@foodomain.com/", "http://%25DOMAIN:foobar@foodomain.com/", true},
1379 // Backslashes should get converted to forward slashes.
1380 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1382 // Busted refs shouldn't make the whole thing fail.
1383 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1385 // Basic port tests.
1386 {"http://foo:80/", "http://foo/", true},
1387 {"http://foo:81/", "http://foo:81/", true},
1388 {"httpa://foo:80/", "httpa://foo:80/", true},
1389 {"http://foo:-80/", "http://foo:-80/", false},
1391 {"https://foo:443/", "https://foo/", true},
1392 {"https://foo:80/", "https://foo:80/", true},
1393 {"ftp://foo:21/", "ftp://foo/", true},
1394 {"ftp://foo:80/", "ftp://foo:80/", true},
1395 {"gopher://foo:70/", "gopher://foo/", true},
1396 {"gopher://foo:443/", "gopher://foo:443/", true},
1397 {"ws://foo:80/", "ws://foo/", true},
1398 {"ws://foo:81/", "ws://foo:81/", true},
1399 {"ws://foo:443/", "ws://foo:443/", true},
1400 {"ws://foo:815/", "ws://foo:815/", true},
1401 {"wss://foo:80/", "wss://foo:80/", true},
1402 {"wss://foo:81/", "wss://foo:81/", true},
1403 {"wss://foo:443/", "wss://foo/", true},
1404 {"wss://foo:815/", "wss://foo:815/", true},
1407 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1408 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1409 url_parse::Parsed parsed
;
1410 url_parse::ParseStandardURL(cases
[i
].input
, url_len
, &parsed
);
1412 url_parse::Parsed out_parsed
;
1413 std::string out_str
;
1414 url_canon::StdStringCanonOutput
output(&out_str
);
1415 bool success
= url_canon::CanonicalizeStandardURL(
1416 cases
[i
].input
, url_len
, parsed
, NULL
, &output
, &out_parsed
);
1419 EXPECT_EQ(cases
[i
].expected_success
, success
);
1420 EXPECT_EQ(cases
[i
].expected
, out_str
);
1424 // The codepath here is the same as for regular canonicalization, so we just
1425 // need to test that things are replaced or not correctly.
1426 TEST(URLCanonTest
, ReplaceStandardURL
) {
1427 ReplaceCase replace_cases
[] = {
1428 // Common case of truncating the path.
1429 {"http://www.google.com/foo?bar=baz#ref", NULL
, NULL
, NULL
, NULL
, NULL
, "/", kDeleteComp
, kDeleteComp
, "http://www.google.com/"},
1430 // Replace everything
1431 {"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"},
1433 {"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"},
1434 // Replace scheme with filesystem. The result is garbage, but you asked
1436 {"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"},
1439 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1440 const ReplaceCase
& cur
= replace_cases
[i
];
1441 int base_len
= static_cast<int>(strlen(cur
.base
));
1442 url_parse::Parsed parsed
;
1443 url_parse::ParseStandardURL(cur
.base
, base_len
, &parsed
);
1445 url_canon::Replacements
<char> r
;
1446 typedef url_canon::Replacements
<char> R
; // Clean up syntax.
1448 // Note that for the scheme we pass in a different clear function since
1449 // there is no function to clear the scheme.
1450 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1451 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1452 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1453 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1454 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1455 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1456 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1457 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1459 std::string out_str
;
1460 url_canon::StdStringCanonOutput
output(&out_str
);
1461 url_parse::Parsed out_parsed
;
1462 url_canon::ReplaceStandardURL(replace_cases
[i
].base
, parsed
,
1463 r
, NULL
, &output
, &out_parsed
);
1466 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1469 // The path pointer should be ignored if the address is invalid.
1471 const char src
[] = "http://www.google.com/here_is_the_path";
1472 int src_len
= static_cast<int>(strlen(src
));
1474 url_parse::Parsed parsed
;
1475 url_parse::ParseStandardURL(src
, src_len
, &parsed
);
1477 // Replace the path to 0 length string. By using 1 as the string address,
1478 // the test should get an access violation if it tries to dereference it.
1479 url_canon::Replacements
<char> r
;
1480 r
.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component(0, 0));
1481 std::string out_str1
;
1482 url_canon::StdStringCanonOutput
output1(&out_str1
);
1483 url_parse::Parsed new_parsed
;
1484 url_canon::ReplaceStandardURL(src
, parsed
, r
, NULL
, &output1
, &new_parsed
);
1486 EXPECT_STREQ("http://www.google.com/", out_str1
.c_str());
1488 // Same with an "invalid" path.
1489 r
.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component());
1490 std::string out_str2
;
1491 url_canon::StdStringCanonOutput
output2(&out_str2
);
1492 url_canon::ReplaceStandardURL(src
, parsed
, r
, NULL
, &output2
, &new_parsed
);
1494 EXPECT_STREQ("http://www.google.com/", out_str2
.c_str());
1498 TEST(URLCanonTest
, ReplaceFileURL
) {
1499 ReplaceCase replace_cases
[] = {
1500 // Replace everything
1501 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, "filer", NULL
, "/foo", "b", "c", "file://filer/foo?b#c"},
1503 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "file:///C:/gaba?query#ref"},
1504 // Clear non-path components (common)
1505 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, kDeleteComp
, "file:///C:/gaba"},
1506 // Replace path with something that doesn't begin with a slash and make
1507 // sure it gets added properly.
1508 {"file:///C:/gaba", NULL
, NULL
, NULL
, NULL
, NULL
, "interesting/", NULL
, NULL
, "file:///interesting/"},
1509 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, "filer", NULL
, "/foo", "b", "c", "file://filer/foo?b#c"},
1510 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "file:///home/gaba?query#ref"},
1511 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, kDeleteComp
, "file:///home/gaba"},
1512 {"file:///home/gaba", NULL
, NULL
, NULL
, NULL
, NULL
, "interesting/", NULL
, NULL
, "file:///interesting/"},
1513 // Replace scheme -- shouldn't do anything.
1514 {"file:///C:/gaba?query#ref", "http", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "file:///C:/gaba?query#ref"},
1517 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1518 const ReplaceCase
& cur
= replace_cases
[i
];
1519 int base_len
= static_cast<int>(strlen(cur
.base
));
1520 url_parse::Parsed parsed
;
1521 url_parse::ParseFileURL(cur
.base
, base_len
, &parsed
);
1523 url_canon::Replacements
<char> r
;
1524 typedef url_canon::Replacements
<char> R
; // Clean up syntax.
1525 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1526 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1527 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1528 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1529 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1530 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1531 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1532 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1534 std::string out_str
;
1535 url_canon::StdStringCanonOutput
output(&out_str
);
1536 url_parse::Parsed out_parsed
;
1537 url_canon::ReplaceFileURL(cur
.base
, parsed
,
1538 r
, NULL
, &output
, &out_parsed
);
1541 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1545 TEST(URLCanonTest
, ReplaceFileSystemURL
) {
1546 ReplaceCase replace_cases
[] = {
1547 // Replace everything in the outer URL.
1548 {"filesystem:file:///temporary/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1550 {"filesystem:file:///temporary/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "filesystem:file:///temporary/gaba?query#ref"},
1551 // Clear non-path components (common)
1552 {"filesystem:file:///temporary/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, kDeleteComp
, "filesystem:file:///temporary/gaba"},
1553 // Replace path with something that doesn't begin with a slash and make
1554 // sure it gets added properly.
1555 {"filesystem:file:///temporary/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, "interesting/", NULL
, NULL
, "filesystem:file:///temporary/interesting/?query#ref"},
1556 // Replace scheme -- shouldn't do anything.
1557 {"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"},
1558 // Replace username -- shouldn't do anything.
1559 {"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"},
1560 // Replace password -- shouldn't do anything.
1561 {"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"},
1562 // Replace host -- shouldn't do anything.
1563 {"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"},
1564 // Replace port -- shouldn't do anything.
1565 {"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"},
1568 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1569 const ReplaceCase
& cur
= replace_cases
[i
];
1570 int base_len
= static_cast<int>(strlen(cur
.base
));
1571 url_parse::Parsed parsed
;
1572 url_parse::ParseFileSystemURL(cur
.base
, base_len
, &parsed
);
1574 url_canon::Replacements
<char> r
;
1575 typedef url_canon::Replacements
<char> R
; // Clean up syntax.
1576 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1577 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1578 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1579 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1580 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1581 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1582 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1583 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1585 std::string out_str
;
1586 url_canon::StdStringCanonOutput
output(&out_str
);
1587 url_parse::Parsed out_parsed
;
1588 url_canon::ReplaceFileSystemURL(cur
.base
, parsed
, r
, NULL
,
1589 &output
, &out_parsed
);
1592 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1596 TEST(URLCanonTest
, ReplacePathURL
) {
1597 ReplaceCase replace_cases
[] = {
1598 // Replace everything
1599 {"data:foo", "javascript", NULL
, NULL
, NULL
, NULL
, "alert('foo?');", NULL
, NULL
, "javascript:alert('foo?');"},
1601 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "data:foo"},
1602 // Replace one or the other
1603 {"data:foo", "javascript", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "javascript:foo"},
1604 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, "bar", NULL
, NULL
, "data:bar"},
1605 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, NULL
, NULL
, "data:"},
1608 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1609 const ReplaceCase
& cur
= replace_cases
[i
];
1610 int base_len
= static_cast<int>(strlen(cur
.base
));
1611 url_parse::Parsed parsed
;
1612 url_parse::ParsePathURL(cur
.base
, base_len
, false, &parsed
);
1614 url_canon::Replacements
<char> r
;
1615 typedef url_canon::Replacements
<char> R
; // Clean up syntax.
1616 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1617 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1618 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1619 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1620 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1621 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1622 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1623 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1625 std::string out_str
;
1626 url_canon::StdStringCanonOutput
output(&out_str
);
1627 url_parse::Parsed out_parsed
;
1628 url_canon::ReplacePathURL(cur
.base
, parsed
,
1629 r
, &output
, &out_parsed
);
1632 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1636 TEST(URLCanonTest
, ReplaceMailtoURL
) {
1637 ReplaceCase replace_cases
[] = {
1638 // Replace everything
1639 {"mailto:jon@foo.com?body=sup", "mailto", NULL
, NULL
, NULL
, NULL
, "addr1", "to=tony", NULL
, "mailto:addr1?to=tony"},
1641 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "mailto:jon@foo.com?body=sup"},
1643 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "jason", NULL
, NULL
, "mailto:jason?body=sup"},
1644 // Replace the query
1645 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "custom=1", NULL
, "mailto:jon@foo.com?custom=1"},
1646 // Replace the path and query
1647 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "jason", "custom=1", NULL
, "mailto:jason?custom=1"},
1648 // Set the query to empty (should leave trailing question mark)
1649 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "", NULL
, "mailto:jon@foo.com?"},
1651 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "|", NULL
, "mailto:jon@foo.com"},
1653 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "|", NULL
, NULL
, "mailto:?body=sup"},
1654 // Clear the path + query
1655 {"mailto:", NULL
, NULL
, NULL
, NULL
, NULL
, "|", "|", NULL
, "mailto:"},
1656 // Setting the ref should have no effect
1657 {"mailto:addr1", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "BLAH", "mailto:addr1"},
1660 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1661 const ReplaceCase
& cur
= replace_cases
[i
];
1662 int base_len
= static_cast<int>(strlen(cur
.base
));
1663 url_parse::Parsed parsed
;
1664 url_parse::ParseMailtoURL(cur
.base
, base_len
, &parsed
);
1666 url_canon::Replacements
<char> r
;
1667 typedef url_canon::Replacements
<char> R
;
1668 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1669 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1670 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1671 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1672 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1673 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1674 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1675 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1677 std::string out_str
;
1678 url_canon::StdStringCanonOutput
output(&out_str
);
1679 url_parse::Parsed out_parsed
;
1680 url_canon::ReplaceMailtoURL(cur
.base
, parsed
,
1681 r
, &output
, &out_parsed
);
1684 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1688 TEST(URLCanonTest
, CanonicalizeFileURL
) {
1691 const char* expected
;
1692 bool expected_success
;
1693 url_parse::Component expected_host
;
1694 url_parse::Component expected_path
;
1697 // Windows-style paths
1698 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1699 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1700 {"file:", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1701 {"file:UNChost/path", "file://unchost/path", true, url_parse::Component(7, 7), url_parse::Component(14, 5)},
1702 // CanonicalizeFileURL supports absolute Windows style paths for IE
1703 // compatability. Note that the caller must decide that this is a file
1704 // URL itself so it can call the file canonicalizer. This is usually
1705 // done automatically as part of relative URL resolving.
1706 {"c:\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1707 {"C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1708 {"/C|\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1709 {"//C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1710 {"//server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1711 {"\\\\server\\file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1712 {"/\\server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1713 // We should preserve the number of slashes after the colon for IE
1714 // compatability, except when there is none, in which case we should
1716 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1717 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1718 // Three slashes should be non-UNC, even if there is no drive spec (IE
1719 // does this, which makes the resulting request invalid).
1720 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, url_parse::Component(), url_parse::Component(7, 12)},
1721 // TODO(brettw) we should probably fail for invalid host names, which
1722 // would change the expected result on this test. We also currently allow
1723 // colon even though it's probably invalid, because its currently the
1724 // "natural" result of the way the canonicalizer is written. There doesn't
1725 // seem to be a strong argument for why allowing it here would be bad, so
1726 // we just tolerate it and the load will fail later.
1727 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, url_parse::Component(7, 2), url_parse::Component(9, 16)},
1728 {"file:filer/home\\me", "file://filer/home/me", true, url_parse::Component(7, 5), url_parse::Component(12, 8)},
1729 // Make sure relative paths can't go above the "C:"
1730 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, url_parse::Component(), url_parse::Component(7, 12)},
1731 // Busted refs shouldn't make the whole thing fail.
1732 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, url_parse::Component(), url_parse::Component(7, 8)},
1735 {"file:///home/me", "file:///home/me", true, url_parse::Component(), url_parse::Component(7, 8)},
1736 // Windowsy ones should get still treated as Unix-style.
1737 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1738 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1739 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
1740 {"//", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1741 {"///", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1742 {"///test", "file:///test", true, url_parse::Component(), url_parse::Component(7, 5)},
1743 {"file://test", "file://test/", true, url_parse::Component(7, 4), url_parse::Component(11, 1)},
1744 {"file://localhost", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
1745 {"file://localhost/", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
1746 {"file://localhost/test", "file://localhost/test", true, url_parse::Component(7, 9), url_parse::Component(16, 5)},
1750 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1751 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1752 url_parse::Parsed parsed
;
1753 url_parse::ParseFileURL(cases
[i
].input
, url_len
, &parsed
);
1755 url_parse::Parsed out_parsed
;
1756 std::string out_str
;
1757 url_canon::StdStringCanonOutput
output(&out_str
);
1758 bool success
= url_canon::CanonicalizeFileURL(cases
[i
].input
, url_len
,
1759 parsed
, NULL
, &output
,
1763 EXPECT_EQ(cases
[i
].expected_success
, success
);
1764 EXPECT_EQ(cases
[i
].expected
, out_str
);
1766 // Make sure the spec was properly identified, the file canonicalizer has
1767 // different code for writing the spec.
1768 EXPECT_EQ(0, out_parsed
.scheme
.begin
);
1769 EXPECT_EQ(4, out_parsed
.scheme
.len
);
1771 EXPECT_EQ(cases
[i
].expected_host
.begin
, out_parsed
.host
.begin
);
1772 EXPECT_EQ(cases
[i
].expected_host
.len
, out_parsed
.host
.len
);
1774 EXPECT_EQ(cases
[i
].expected_path
.begin
, out_parsed
.path
.begin
);
1775 EXPECT_EQ(cases
[i
].expected_path
.len
, out_parsed
.path
.len
);
1779 TEST(URLCanonTest
, CanonicalizeFileSystemURL
) {
1782 const char* expected
;
1783 bool expected_success
;
1785 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1786 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1787 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1788 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1789 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1790 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1791 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1794 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1795 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1796 url_parse::Parsed parsed
;
1797 url_parse::ParseFileSystemURL(cases
[i
].input
, url_len
, &parsed
);
1799 url_parse::Parsed out_parsed
;
1800 std::string out_str
;
1801 url_canon::StdStringCanonOutput
output(&out_str
);
1802 bool success
= url_canon::CanonicalizeFileSystemURL(cases
[i
].input
, url_len
,
1803 parsed
, NULL
, &output
,
1807 EXPECT_EQ(cases
[i
].expected_success
, success
);
1808 EXPECT_EQ(cases
[i
].expected
, out_str
);
1810 // Make sure the spec was properly identified, the filesystem canonicalizer
1811 // has different code for writing the spec.
1812 EXPECT_EQ(0, out_parsed
.scheme
.begin
);
1813 EXPECT_EQ(10, out_parsed
.scheme
.len
);
1815 EXPECT_GT(out_parsed
.path
.len
, 0);
1819 TEST(URLCanonTest
, CanonicalizePathURL
) {
1820 // Path URLs should get canonicalized schemes but nothing else.
1823 const char* expected
;
1825 {"javascript:", "javascript:"},
1826 {"JavaScript:Foo", "javascript:Foo"},
1827 {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1830 for (size_t i
= 0; i
< ARRAYSIZE(path_cases
); i
++) {
1831 int url_len
= static_cast<int>(strlen(path_cases
[i
].input
));
1832 url_parse::Parsed parsed
;
1833 url_parse::ParsePathURL(path_cases
[i
].input
, url_len
, true, &parsed
);
1835 url_parse::Parsed out_parsed
;
1836 std::string out_str
;
1837 url_canon::StdStringCanonOutput
output(&out_str
);
1838 bool success
= url_canon::CanonicalizePathURL(path_cases
[i
].input
, url_len
,
1843 EXPECT_TRUE(success
);
1844 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
1846 EXPECT_EQ(0, out_parsed
.host
.begin
);
1847 EXPECT_EQ(-1, out_parsed
.host
.len
);
1849 // When we end with a colon at the end, there should be no path.
1850 if (path_cases
[i
].input
[url_len
- 1] == ':') {
1851 EXPECT_EQ(0, out_parsed
.GetContent().begin
);
1852 EXPECT_EQ(-1, out_parsed
.GetContent().len
);
1857 TEST(URLCanonTest
, CanonicalizeMailtoURL
) {
1860 const char* expected
;
1861 bool expected_success
;
1862 url_parse::Component expected_path
;
1863 url_parse::Component expected_query
;
1865 {"mailto:addr1", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
1866 {"mailto:addr1@foo.com", "mailto:addr1@foo.com", true, url_parse::Component(7, 13), url_parse::Component()},
1867 // Trailing whitespace is stripped.
1868 {"MaIlTo:addr1 \t ", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
1869 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, url_parse::Component(7, 5), url_parse::Component(13,6)},
1870 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, url_parse::Component(7, 11), url_parse::Component()},
1871 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, url_parse::Component(7, 12), url_parse::Component()},
1872 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, url_parse::Component(7, 13), url_parse::Component()},
1873 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, url_parse::Component(7, 12), url_parse::Component()},
1874 // Null character should be escaped to %00
1875 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, url_parse::Component(7, 13), url_parse::Component(21, 3)},
1876 // Invalid -- UTF-8 encoded surrogate value.
1877 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, url_parse::Component(7, 9), url_parse::Component()},
1878 {"mailto:addr1?", "mailto:addr1?", true, url_parse::Component(7, 5), url_parse::Component(13, 0)},
1881 // Define outside of loop to catch bugs where components aren't reset
1882 url_parse::Parsed parsed
;
1883 url_parse::Parsed out_parsed
;
1885 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1886 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1888 // The 9th test case purposely has a '\0' in it -- don't count it
1889 // as the string terminator.
1892 url_parse::ParseMailtoURL(cases
[i
].input
, url_len
, &parsed
);
1894 std::string out_str
;
1895 url_canon::StdStringCanonOutput
output(&out_str
);
1896 bool success
= url_canon::CanonicalizeMailtoURL(cases
[i
].input
, url_len
,
1901 EXPECT_EQ(cases
[i
].expected_success
, success
);
1902 EXPECT_EQ(cases
[i
].expected
, out_str
);
1904 // Make sure the spec was properly identified
1905 EXPECT_EQ(0, out_parsed
.scheme
.begin
);
1906 EXPECT_EQ(6, out_parsed
.scheme
.len
);
1908 EXPECT_EQ(cases
[i
].expected_path
.begin
, out_parsed
.path
.begin
);
1909 EXPECT_EQ(cases
[i
].expected_path
.len
, out_parsed
.path
.len
);
1911 EXPECT_EQ(cases
[i
].expected_query
.begin
, out_parsed
.query
.begin
);
1912 EXPECT_EQ(cases
[i
].expected_query
.len
, out_parsed
.query
.len
);
1918 TEST(URLCanonTest
, _itoa_s
) {
1919 // We fill the buffer with 0xff to ensure that it's getting properly
1920 // null-terminated. We also allocate one byte more than what we tell
1921 // _itoa_s about, and ensure that the extra byte is untouched.
1923 memset(buf
, 0xff, sizeof(buf
));
1924 EXPECT_EQ(0, url_canon::_itoa_s(12, buf
, sizeof(buf
) - 1, 10));
1925 EXPECT_STREQ("12", buf
);
1926 EXPECT_EQ('\xFF', buf
[3]);
1928 // Test the edge cases - exactly the buffer size and one over
1929 memset(buf
, 0xff, sizeof(buf
));
1930 EXPECT_EQ(0, url_canon::_itoa_s(1234, buf
, sizeof(buf
) - 1, 10));
1931 EXPECT_STREQ("1234", buf
);
1932 EXPECT_EQ('\xFF', buf
[5]);
1934 memset(buf
, 0xff, sizeof(buf
));
1935 EXPECT_EQ(EINVAL
, url_canon::_itoa_s(12345, buf
, sizeof(buf
) - 1, 10));
1936 EXPECT_EQ('\xFF', buf
[5]); // should never write to this location
1938 // Test the template overload (note that this will see the full buffer)
1939 memset(buf
, 0xff, sizeof(buf
));
1940 EXPECT_EQ(0, url_canon::_itoa_s(12, buf
, 10));
1941 EXPECT_STREQ("12", buf
);
1942 EXPECT_EQ('\xFF', buf
[3]);
1944 memset(buf
, 0xff, sizeof(buf
));
1945 EXPECT_EQ(0, url_canon::_itoa_s(12345, buf
, 10));
1946 EXPECT_STREQ("12345", buf
);
1948 EXPECT_EQ(EINVAL
, url_canon::_itoa_s(123456, buf
, 10));
1950 // Test that radix 16 is supported.
1951 memset(buf
, 0xff, sizeof(buf
));
1952 EXPECT_EQ(0, url_canon::_itoa_s(1234, buf
, sizeof(buf
) - 1, 16));
1953 EXPECT_STREQ("4d2", buf
);
1954 EXPECT_EQ('\xFF', buf
[5]);
1957 TEST(URLCanonTest
, _itow_s
) {
1958 // We fill the buffer with 0xff to ensure that it's getting properly
1959 // null-terminated. We also allocate one byte more than what we tell
1960 // _itoa_s about, and ensure that the extra byte is untouched.
1961 base::char16 buf
[6];
1962 const char fill_mem
= 0xff;
1963 const base::char16 fill_char
= 0xffff;
1964 memset(buf
, fill_mem
, sizeof(buf
));
1965 EXPECT_EQ(0, url_canon::_itow_s(12, buf
, sizeof(buf
) / 2 - 1, 10));
1966 EXPECT_EQ(WStringToUTF16(L
"12"), base::string16(buf
));
1967 EXPECT_EQ(fill_char
, buf
[3]);
1969 // Test the edge cases - exactly the buffer size and one over
1970 EXPECT_EQ(0, url_canon::_itow_s(1234, buf
, sizeof(buf
) / 2 - 1, 10));
1971 EXPECT_EQ(WStringToUTF16(L
"1234"), base::string16(buf
));
1972 EXPECT_EQ(fill_char
, buf
[5]);
1974 memset(buf
, fill_mem
, sizeof(buf
));
1975 EXPECT_EQ(EINVAL
, url_canon::_itow_s(12345, buf
, sizeof(buf
) / 2 - 1, 10));
1976 EXPECT_EQ(fill_char
, buf
[5]); // should never write to this location
1978 // Test the template overload (note that this will see the full buffer)
1979 memset(buf
, fill_mem
, sizeof(buf
));
1980 EXPECT_EQ(0, url_canon::_itow_s(12, buf
, 10));
1981 EXPECT_EQ(WStringToUTF16(L
"12"), base::string16(buf
));
1982 EXPECT_EQ(fill_char
, buf
[3]);
1984 memset(buf
, fill_mem
, sizeof(buf
));
1985 EXPECT_EQ(0, url_canon::_itow_s(12345, buf
, 10));
1986 EXPECT_EQ(WStringToUTF16(L
"12345"), base::string16(buf
));
1988 EXPECT_EQ(EINVAL
, url_canon::_itow_s(123456, buf
, 10));
1993 // Returns true if the given two structures are the same.
1994 static bool ParsedIsEqual(const url_parse::Parsed
& a
,
1995 const url_parse::Parsed
& b
) {
1996 return a
.scheme
.begin
== b
.scheme
.begin
&& a
.scheme
.len
== b
.scheme
.len
&&
1997 a
.username
.begin
== b
.username
.begin
&& a
.username
.len
== b
.username
.len
&&
1998 a
.password
.begin
== b
.password
.begin
&& a
.password
.len
== b
.password
.len
&&
1999 a
.host
.begin
== b
.host
.begin
&& a
.host
.len
== b
.host
.len
&&
2000 a
.port
.begin
== b
.port
.begin
&& a
.port
.len
== b
.port
.len
&&
2001 a
.path
.begin
== b
.path
.begin
&& a
.path
.len
== b
.path
.len
&&
2002 a
.query
.begin
== b
.query
.begin
&& a
.query
.len
== b
.query
.len
&&
2003 a
.ref
.begin
== b
.ref
.begin
&& a
.ref
.len
== b
.ref
.len
;
2006 TEST(URLCanonTest
, ResolveRelativeURL
) {
2007 struct RelativeCase
{
2008 const char* base
; // Input base URL: MUST BE CANONICAL
2009 bool is_base_hier
; // Is the base URL hierarchical
2010 bool is_base_file
; // Tells us if the base is a file URL.
2011 const char* test
; // Input URL to test against.
2012 bool succeed_relative
; // Whether we expect IsRelativeURL to succeed
2013 bool is_rel
; // Whether we expect |test| to be relative or not.
2014 bool succeed_resolve
; // Whether we expect ResolveRelativeURL to succeed.
2015 const char* resolved
; // What we expect in the result when resolving.
2017 // Basic absolute input.
2018 {"http://host/a", true, false, "http://another/", true, false, false, NULL
},
2019 {"http://host/a", true, false, "http:////another/", true, false, false, NULL
},
2020 // Empty relative URLs should only remove the ref part of the URL,
2021 // leaving the rest unchanged.
2022 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
2023 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
2024 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
2025 // Spaces at the ends of the relative path should be ignored.
2026 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
2027 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
2028 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
2029 // Matching schemes without two slashes are treated as relative.
2030 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
2031 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
2032 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
2033 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
2034 // Nonmatching schemes are absolute.
2035 {"http://host/a", true, false, "https:host2", true, false, false, NULL
},
2036 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL
},
2037 // Absolute path input
2038 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
2039 {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
2040 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
2041 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
2042 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
2043 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
2044 // Relative path input
2045 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
2046 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
2047 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
2048 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
2049 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
2050 {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
2051 {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
2052 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
2053 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
2055 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
2056 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
2057 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
2059 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
2060 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
2061 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
2062 // Non-hierarchical base: no relative handling. Relative input should
2063 // error, and if a scheme is present, it should be treated as absolute.
2064 {"data:foobar", false, false, "baz.html", false, false, false, NULL
},
2065 {"data:foobar", false, false, "data:baz", true, false, false, NULL
},
2066 {"data:foobar", false, false, "data:/base", true, false, false, NULL
},
2067 // Non-hierarchical base: absolute input should succeed.
2068 {"data:foobar", false, false, "http://host/", true, false, false, NULL
},
2069 {"data:foobar", false, false, "http:host", true, false, false, NULL
},
2070 // Invalid schemes should be treated as relative.
2071 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
2072 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
2073 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
2074 {"data:asdf", false, false, ":foo", false, false, false, NULL
},
2075 {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL
},
2076 // We should treat semicolons like any other character in URL resolving
2077 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
2078 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
2079 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
2080 // Relative URLs can also be written as "//foo/bar" which is relative to
2081 // the scheme. In this case, it would take the old scheme, so for http
2082 // the example would resolve to "http://foo/bar".
2083 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
2084 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
2085 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
2086 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
2087 {"http://host/a", true, false, "//", true, true, false, "http:"},
2088 // IE will also allow one or the other to be a backslash to get the same
2090 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
2091 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
2093 // Resolving against Windows file base URLs.
2094 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL
},
2095 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
2096 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
2097 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
2098 // But two backslashes on Windows should be UNC so should be treated
2100 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL
},
2101 // IE doesn't support drive specs starting with two slashes. It fails
2102 // immediately and doesn't even try to load. We fix it up to either
2103 // an absolute path or UNC depending on what it looks like.
2104 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
2105 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
2106 // Windows drive specs should be allowed and treated as absolute.
2107 {"file:///C:/foo", true, true, "c:", true, false, false, NULL
},
2108 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL
},
2109 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL
},
2110 // Relative paths with drive letters should be allowed when the base is
2112 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2113 // Treat absolute paths as being off of the drive.
2114 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2115 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2116 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2117 // On Windows, two slashes without a drive letter when the base is a file
2118 // means that the path is UNC.
2119 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2120 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2122 // On Unix we fall back to relative behavior since there's nothing else
2123 // reasonable to do.
2124 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2126 // Even on Windows, we don't allow relative drive specs when the base
2128 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2129 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
2130 // Ensure that ports aren't allowed for hosts relative to a file url.
2131 // Although the result string shows a host:port portion, the call to
2132 // resolve the relative URL returns false, indicating parse failure,
2133 // which is what is required.
2134 {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
2135 // Filesystem URL tests; filesystem URLs are only valid and relative if
2136 // they have no scheme, e.g. "./index.html". There's no valid equivalent
2137 // to http:index.html.
2138 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL
},
2139 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL
},
2140 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL
},
2141 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL
},
2142 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2143 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2144 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL
},
2145 // Absolute URLs are still not relative to a non-standard base URL.
2146 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2147 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2150 for (size_t i
= 0; i
< ARRAYSIZE(rel_cases
); i
++) {
2151 const RelativeCase
& cur_case
= rel_cases
[i
];
2153 url_parse::Parsed parsed
;
2154 int base_len
= static_cast<int>(strlen(cur_case
.base
));
2155 if (cur_case
.is_base_file
)
2156 url_parse::ParseFileURL(cur_case
.base
, base_len
, &parsed
);
2157 else if (cur_case
.is_base_hier
)
2158 url_parse::ParseStandardURL(cur_case
.base
, base_len
, &parsed
);
2160 url_parse::ParsePathURL(cur_case
.base
, base_len
, false, &parsed
);
2162 // First see if it is relative.
2163 int test_len
= static_cast<int>(strlen(cur_case
.test
));
2165 url_parse::Component relative_component
;
2166 bool succeed_is_rel
= url_canon::IsRelativeURL(
2167 cur_case
.base
, parsed
, cur_case
.test
, test_len
, cur_case
.is_base_hier
,
2168 &is_relative
, &relative_component
);
2170 EXPECT_EQ(cur_case
.succeed_relative
, succeed_is_rel
) <<
2171 "succeed is rel failure on " << cur_case
.test
;
2172 EXPECT_EQ(cur_case
.is_rel
, is_relative
) <<
2173 "is rel failure on " << cur_case
.test
;
2175 if (succeed_is_rel
&& is_relative
&& cur_case
.is_rel
) {
2176 std::string resolved
;
2177 url_canon::StdStringCanonOutput
output(&resolved
);
2178 url_parse::Parsed resolved_parsed
;
2180 bool succeed_resolve
= url_canon::ResolveRelativeURL(
2181 cur_case
.base
, parsed
, cur_case
.is_base_file
,
2182 cur_case
.test
, relative_component
, NULL
, &output
, &resolved_parsed
);
2185 EXPECT_EQ(cur_case
.succeed_resolve
, succeed_resolve
);
2186 EXPECT_EQ(cur_case
.resolved
, resolved
) << " on " << cur_case
.test
;
2188 // Verify that the output parsed structure is the same as parsing a
2190 url_parse::Parsed ref_parsed
;
2191 int resolved_len
= static_cast<int>(resolved
.size());
2192 if (cur_case
.is_base_file
) {
2193 url_parse::ParseFileURL(resolved
.c_str(), resolved_len
, &ref_parsed
);
2194 } else if (cur_case
.is_base_hier
) {
2195 url_parse::ParseStandardURL(resolved
.c_str(), resolved_len
,
2198 url_parse::ParsePathURL(resolved
.c_str(), resolved_len
, false,
2201 EXPECT_TRUE(ParsedIsEqual(ref_parsed
, resolved_parsed
));
2206 // It used to be when we did a replacement with a long buffer of UTF-16
2207 // characters, we would get invalid data in the URL. This is because the buffer
2208 // it used to hold the UTF-8 data was resized, while some pointers were still
2209 // kept to the old buffer that was removed.
2210 TEST(URLCanonTest
, ReplacementOverflow
) {
2211 const char src
[] = "file:///C:/foo/bar";
2212 int src_len
= static_cast<int>(strlen(src
));
2213 url_parse::Parsed parsed
;
2214 url_parse::ParseFileURL(src
, src_len
, &parsed
);
2216 // Override two components, the path with something short, and the query with
2217 // sonething long enough to trigger the bug.
2218 url_canon::Replacements
<base::char16
> repl
;
2219 base::string16 new_query
;
2220 for (int i
= 0; i
< 4800; i
++)
2221 new_query
.push_back('a');
2223 base::string16
new_path(WStringToUTF16(L
"/foo"));
2224 repl
.SetPath(new_path
.c_str(), url_parse::Component(0, 4));
2225 repl
.SetQuery(new_query
.c_str(),
2226 url_parse::Component(0, static_cast<int>(new_query
.length())));
2228 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2229 // standard URLs, file URLs, etc, since they will go to the same replacement
2230 // function that was buggy.
2231 url_parse::Parsed repl_parsed
;
2232 std::string repl_str
;
2233 url_canon::StdStringCanonOutput
repl_output(&repl_str
);
2234 url_canon::ReplaceFileURL(src
, parsed
, repl
, NULL
, &repl_output
, &repl_parsed
);
2235 repl_output
.Complete();
2237 // Generate the expected string and check.
2238 std::string
expected("file:///foo?");
2239 for (size_t i
= 0; i
< new_query
.length(); i
++)
2240 expected
.push_back('a');
2241 EXPECT_TRUE(expected
== repl_str
);