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