Update V8 to version 4.5.82.
[chromium-blink-merge.git] / url / url_canon_unittest.cc
blob67cced24607052a941b6cf71bfcf0831110400cb
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 stacktrace 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 characer 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 are invalid. The bad char should be replaced
163 // 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 nonexistant, 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 if using IPv4 compat or mapped addresses.
767 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
768 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
769 {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
770 // Too many bits (even though 8 comonents, the last one holds 32 bits).
771 {"[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, ""},
773 // Too many bits specified -- the contraction would have to be zero-length
774 // to not exceed 128 bits.
775 {"[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, ""},
777 // The contraction is for 16 bits of zero.
778 {"[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"},
780 // Cannot have a trailing colon.
781 {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
782 {"[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, ""},
784 // Cannot have negative numbers.
785 {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
787 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
788 // The scope_id should be included in the canonicalized URL, and is an
789 // unsigned decimal number.
791 // Invalid because no ID was given after the percent.
793 // Don't allow scope-id
794 {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
795 {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
796 {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
797 {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
798 {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
800 // Don't allow leading or trailing colons.
801 {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
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, ""},
805 // We allow a single trailing dot.
806 // ... omitted at this time ...
807 // Two dots in a row means not an IP address.
808 {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
809 // Any non-first components get truncated to one byte.
810 // ... omitted at this time ...
811 // Spaces should be rejected.
812 {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
815 for (size_t i = 0; i < arraysize(cases); i++) {
816 // 8-bit version.
817 Component component(0, static_cast<int>(strlen(cases[i].input8)));
819 std::string out_str1;
820 StdStringCanonOutput output1(&out_str1);
821 CanonHostInfo host_info;
822 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
823 output1.Complete();
825 EXPECT_EQ(cases[i].expected_family, host_info.family);
826 EXPECT_EQ(std::string(cases[i].expected_address_hex),
827 BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
828 if (host_info.family == CanonHostInfo::IPV6) {
829 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
830 EXPECT_EQ(cases[i].expected_component.begin,
831 host_info.out_host.begin);
832 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
835 // 16-bit version.
836 base::string16 input16(WStringToUTF16(cases[i].input16));
837 component = Component(0, static_cast<int>(input16.length()));
839 std::string out_str2;
840 StdStringCanonOutput output2(&out_str2);
841 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
842 output2.Complete();
844 EXPECT_EQ(cases[i].expected_family, host_info.family);
845 EXPECT_EQ(std::string(cases[i].expected_address_hex),
846 BytesToHexString(host_info.address, host_info.AddressLength()));
847 if (host_info.family == CanonHostInfo::IPV6) {
848 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
849 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
850 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
855 TEST(URLCanonTest, IPEmpty) {
856 std::string out_str1;
857 StdStringCanonOutput output1(&out_str1);
858 CanonHostInfo host_info;
860 // This tests tests.
861 const char spec[] = "192.168.0.1";
862 CanonicalizeIPAddress(spec, Component(), &output1, &host_info);
863 EXPECT_FALSE(host_info.IsIPAddress());
865 CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info);
866 EXPECT_FALSE(host_info.IsIPAddress());
869 TEST(URLCanonTest, UserInfo) {
870 // Note that the canonicalizer should escape and treat empty components as
871 // not being there.
873 // We actually parse a full input URL so we can get the initial components.
874 struct UserComponentCase {
875 const char* input;
876 const char* expected;
877 Component expected_username;
878 Component expected_password;
879 bool expected_success;
880 } user_info_cases[] = {
881 {"http://user:pass@host.com/", "user:pass@", Component(0, 4), Component(5, 4), true},
882 {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
883 {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
884 {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
885 {"http://:foo@host.com/", ":foo@", Component(0, 0), Component(1, 3), true},
886 {"http://^ :$\t@host.com/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
887 {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
888 {"http://%2540:bar@domain.com/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
890 // IE7 compatability: old versions allowed backslashes in usernames, but
891 // IE7 does not. We disallow it as well.
892 {"ftp://me\\mydomain:pass@foo.com/", "", Component(0, -1), Component(0, -1), true},
895 for (size_t i = 0; i < arraysize(user_info_cases); i++) {
896 int url_len = static_cast<int>(strlen(user_info_cases[i].input));
897 Parsed parsed;
898 ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
899 Component out_user, out_pass;
900 std::string out_str;
901 StdStringCanonOutput output1(&out_str);
903 bool success = CanonicalizeUserInfo(user_info_cases[i].input,
904 parsed.username,
905 user_info_cases[i].input,
906 parsed.password,
907 &output1,
908 &out_user,
909 &out_pass);
910 output1.Complete();
912 EXPECT_EQ(user_info_cases[i].expected_success, success);
913 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
914 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
915 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
916 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
917 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
919 // Now try the wide version
920 out_str.clear();
921 StdStringCanonOutput output2(&out_str);
922 base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
923 success = CanonicalizeUserInfo(wide_input.c_str(),
924 parsed.username,
925 wide_input.c_str(),
926 parsed.password,
927 &output2,
928 &out_user,
929 &out_pass);
930 output2.Complete();
932 EXPECT_EQ(user_info_cases[i].expected_success, success);
933 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
934 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
935 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
936 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
937 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
941 TEST(URLCanonTest, Port) {
942 // We only need to test that the number gets properly put into the output
943 // buffer. The parser unit tests will test scanning the number correctly.
945 // Note that the CanonicalizePort will always prepend a colon to the output
946 // to separate it from the colon that it assumes preceeds it.
947 struct PortCase {
948 const char* input;
949 int default_port;
950 const char* expected;
951 Component expected_component;
952 bool expected_success;
953 } port_cases[] = {
954 // Invalid input should be copied w/ failure.
955 {"as df", 80, ":as%20df", Component(1, 7), false},
956 {"-2", 80, ":-2", Component(1, 2), false},
957 // Default port should be omitted.
958 {"80", 80, "", Component(0, -1), true},
959 {"8080", 80, ":8080", Component(1, 4), true},
960 // PORT_UNSPECIFIED should mean always keep the port.
961 {"80", PORT_UNSPECIFIED, ":80", Component(1, 2), true},
964 for (size_t i = 0; i < arraysize(port_cases); i++) {
965 int url_len = static_cast<int>(strlen(port_cases[i].input));
966 Component in_comp(0, url_len);
967 Component out_comp;
968 std::string out_str;
969 StdStringCanonOutput output1(&out_str);
970 bool success = CanonicalizePort(port_cases[i].input,
971 in_comp,
972 port_cases[i].default_port,
973 &output1,
974 &out_comp);
975 output1.Complete();
977 EXPECT_EQ(port_cases[i].expected_success, success);
978 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
979 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
980 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
982 // Now try the wide version
983 out_str.clear();
984 StdStringCanonOutput output2(&out_str);
985 base::string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
986 success = CanonicalizePort(wide_input.c_str(),
987 in_comp,
988 port_cases[i].default_port,
989 &output2,
990 &out_comp);
991 output2.Complete();
993 EXPECT_EQ(port_cases[i].expected_success, success);
994 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
995 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
996 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
1000 TEST(URLCanonTest, Path) {
1001 DualComponentCase path_cases[] = {
1002 // ----- path collapsing tests -----
1003 {"/././foo", L"/././foo", "/foo", Component(0, 4), true},
1004 {"/./.foo", L"/./.foo", "/.foo", Component(0, 5), true},
1005 {"/foo/.", L"/foo/.", "/foo/", Component(0, 5), true},
1006 {"/foo/./", L"/foo/./", "/foo/", Component(0, 5), true},
1007 // double dots followed by a slash or the end of the string count
1008 {"/foo/bar/..", L"/foo/bar/..", "/foo/", Component(0, 5), true},
1009 {"/foo/bar/../", L"/foo/bar/../", "/foo/", Component(0, 5), true},
1010 // don't count double dots when they aren't followed by a slash
1011 {"/foo/..bar", L"/foo/..bar", "/foo/..bar", Component(0, 10), true},
1012 // some in the middle
1013 {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", Component(0, 8), true},
1014 {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", Component(0, 2), true},
1015 // we should not be able to go above the root
1016 {"/foo/../../..", L"/foo/../../..", "/", Component(0, 1), true},
1017 {"/foo/../../../ton", L"/foo/../../../ton", "/ton", Component(0, 4), true},
1018 // escaped dots should be unescaped and treated the same as dots
1019 {"/foo/%2e", L"/foo/%2e", "/foo/", Component(0, 5), true},
1020 {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", Component(0, 8), true},
1021 {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", Component(0, 6), true},
1022 // Multiple slashes in a row should be preserved and treated like empty
1023 // directory names.
1024 {"////../..", L"////../..", "//", Component(0, 2), true},
1026 // ----- escaping tests -----
1027 {"/foo", L"/foo", "/foo", Component(0, 4), true},
1028 // Valid escape sequence
1029 {"/%20foo", L"/%20foo", "/%20foo", Component(0, 7), true},
1030 // Invalid escape sequence we should pass through unchanged.
1031 {"/foo%", L"/foo%", "/foo%", Component(0, 5), true},
1032 {"/foo%2", L"/foo%2", "/foo%2", Component(0, 6), true},
1033 // Invalid escape sequence: bad characters should be treated the same as
1034 // the sourrounding text, not as escaped (in this case, UTF-8).
1035 {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", Component(0, 10), true},
1036 {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", Component(0, 16), true},
1037 {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22), true},
1038 // Regular characters that are escaped should be unescaped
1039 {"/foo%41%7a", L"/foo%41%7a", "/fooAz", Component(0, 6), true},
1040 // Funny characters that are unescaped should be escaped
1041 {"/foo\x09\x91%91", NULL, "/foo%09%91%91", Component(0, 13), true},
1042 {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true},
1043 // Invalid characters that are escaped should cause a failure.
1044 {"/foo%00%51", L"/foo%00%51", "/foo%00Q", Component(0, 8), false},
1045 // Some characters should be passed through unchanged regardless of esc.
1046 {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13), true},
1047 // Characters that are properly escaped should not have the case changed
1048 // of hex letters.
1049 {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13), true},
1050 // Funny characters that are unescaped should be escaped
1051 {"/foo\tbar", L"/foo\tbar", "/foo%09bar", Component(0, 10), true},
1052 // Backslashes should get converted to forward slashes
1053 {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", Component(0, 8), true},
1054 // Hashes found in paths (possibly only when the caller explicitly sets
1055 // the path on an already-parsed URL) should be escaped.
1056 {"/foo#bar", L"/foo#bar", "/foo%23bar", Component(0, 10), true},
1057 // %7f should be allowed and %3D should not be unescaped (these were wrong
1058 // in a previous version).
1059 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true},
1060 // @ should be passed through unchanged (escaped or unescaped).
1061 {"/@asdf%40", L"/@asdf%40", "/@asdf%40", Component(0, 9), true},
1063 // ----- encoding tests -----
1064 // Basic conversions
1065 {"/\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},
1066 // Invalid unicode characters should fail. We only do validation on
1067 // UTF-16 input, so this doesn't happen on 8-bit.
1068 {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", Component(0, 13), true},
1069 {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false},
1072 for (size_t i = 0; i < arraysize(path_cases); i++) {
1073 if (path_cases[i].input8) {
1074 int len = static_cast<int>(strlen(path_cases[i].input8));
1075 Component in_comp(0, len);
1076 Component out_comp;
1077 std::string out_str;
1078 StdStringCanonOutput output(&out_str);
1079 bool success =
1080 CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp);
1081 output.Complete();
1083 EXPECT_EQ(path_cases[i].expected_success, success);
1084 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1085 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1086 EXPECT_EQ(path_cases[i].expected, out_str);
1089 if (path_cases[i].input16) {
1090 base::string16 input16(WStringToUTF16(path_cases[i].input16));
1091 int len = static_cast<int>(input16.length());
1092 Component in_comp(0, len);
1093 Component out_comp;
1094 std::string out_str;
1095 StdStringCanonOutput output(&out_str);
1097 bool success =
1098 CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp);
1099 output.Complete();
1101 EXPECT_EQ(path_cases[i].expected_success, success);
1102 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1103 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1104 EXPECT_EQ(path_cases[i].expected, out_str);
1108 // Manual test: embedded NULLs should be escaped and the URL should be marked
1109 // as invalid.
1110 const char path_with_null[] = "/ab\0c";
1111 Component in_comp(0, 5);
1112 Component out_comp;
1114 std::string out_str;
1115 StdStringCanonOutput output(&out_str);
1116 bool success = CanonicalizePath(path_with_null, in_comp, &output, &out_comp);
1117 output.Complete();
1118 EXPECT_FALSE(success);
1119 EXPECT_EQ("/ab%00c", out_str);
1122 TEST(URLCanonTest, Query) {
1123 struct QueryCase {
1124 const char* input8;
1125 const wchar_t* input16;
1126 const char* expected;
1127 } query_cases[] = {
1128 // Regular ASCII case.
1129 {"foo=bar", L"foo=bar", "?foo=bar"},
1130 // Allow question marks in the query without escaping
1131 {"as?df", L"as?df", "?as?df"},
1132 // Always escape '#' since it would mark the ref.
1133 {"as#df", L"as#df", "?as%23df"},
1134 // Escape some questionable 8-bit characters, but never unescape.
1135 {"\x02hello\x7f bye", L"\x02hello\x7f bye", "?%02hello%7F%20bye"},
1136 {"%40%41123", L"%40%41123", "?%40%41123"},
1137 // Chinese input/output
1138 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "?q=%E4%BD%A0%E5%A5%BD"},
1139 // Invalid UTF-8/16 input should be replaced with invalid characters.
1140 {"q=\xed\xed", L"q=\xd800\xd800", "?q=%EF%BF%BD%EF%BF%BD"},
1141 // Don't allow < or > because sometimes they are used for XSS if the
1142 // URL is echoed in content. Firefox does this, IE doesn't.
1143 {"q=<asdf>", L"q=<asdf>", "?q=%3Casdf%3E"},
1144 // Escape double quotemarks in the query.
1145 {"q=\"asdf\"", L"q=\"asdf\"", "?q=%22asdf%22"},
1148 for (size_t i = 0; i < arraysize(query_cases); i++) {
1149 Component out_comp;
1151 if (query_cases[i].input8) {
1152 int len = static_cast<int>(strlen(query_cases[i].input8));
1153 Component in_comp(0, len);
1154 std::string out_str;
1156 StdStringCanonOutput output(&out_str);
1157 CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output,
1158 &out_comp);
1159 output.Complete();
1161 EXPECT_EQ(query_cases[i].expected, out_str);
1164 if (query_cases[i].input16) {
1165 base::string16 input16(WStringToUTF16(query_cases[i].input16));
1166 int len = static_cast<int>(input16.length());
1167 Component in_comp(0, len);
1168 std::string out_str;
1170 StdStringCanonOutput output(&out_str);
1171 CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp);
1172 output.Complete();
1174 EXPECT_EQ(query_cases[i].expected, out_str);
1178 // Extra test for input with embedded NULL;
1179 std::string out_str;
1180 StdStringCanonOutput output(&out_str);
1181 Component out_comp;
1182 CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL, &output, &out_comp);
1183 output.Complete();
1184 EXPECT_EQ("?a%20%00z%01", out_str);
1187 TEST(URLCanonTest, Ref) {
1188 // Refs are trivial, it just checks the encoding.
1189 DualComponentCase ref_cases[] = {
1190 // Regular one, we shouldn't escape spaces, et al.
1191 {"hello, world", L"hello, world", "#hello, world", Component(1, 12), true},
1192 // UTF-8/wide input should be preserved
1193 {"\xc2\xa9", L"\xa9", "#\xc2\xa9", Component(1, 2), true},
1194 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
1195 {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", Component(1, 6), true},
1196 // Escaping should be preserved unchanged, even invalid ones
1197 {"%41%a", L"%41%a", "#%41%a", Component(1, 5), true},
1198 // Invalid UTF-8/16 input should be flagged and the input made valid
1199 {"\xc2", NULL, "#\xef\xbf\xbd", Component(1, 3), true},
1200 {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", Component(1, 6), true},
1201 // Test a Unicode invalid character.
1202 {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", Component(1, 4), true},
1203 // Refs can have # signs and we should preserve them.
1204 {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", Component(1, 9), true},
1205 {"#asdf", L"#asdf", "##asdf", Component(1, 5), true},
1208 for (size_t i = 0; i < arraysize(ref_cases); i++) {
1209 // 8-bit input
1210 if (ref_cases[i].input8) {
1211 int len = static_cast<int>(strlen(ref_cases[i].input8));
1212 Component in_comp(0, len);
1213 Component out_comp;
1215 std::string out_str;
1216 StdStringCanonOutput output(&out_str);
1217 CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp);
1218 output.Complete();
1220 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1221 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1222 EXPECT_EQ(ref_cases[i].expected, out_str);
1225 // 16-bit input
1226 if (ref_cases[i].input16) {
1227 base::string16 input16(WStringToUTF16(ref_cases[i].input16));
1228 int len = static_cast<int>(input16.length());
1229 Component in_comp(0, len);
1230 Component out_comp;
1232 std::string out_str;
1233 StdStringCanonOutput output(&out_str);
1234 CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
1235 output.Complete();
1237 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1238 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1239 EXPECT_EQ(ref_cases[i].expected, out_str);
1243 // Try one with an embedded NULL. It should be stripped.
1244 const char null_input[5] = "ab\x00z";
1245 Component null_input_component(0, 4);
1246 Component out_comp;
1248 std::string out_str;
1249 StdStringCanonOutput output(&out_str);
1250 CanonicalizeRef(null_input, null_input_component, &output, &out_comp);
1251 output.Complete();
1253 EXPECT_EQ(1, out_comp.begin);
1254 EXPECT_EQ(3, out_comp.len);
1255 EXPECT_EQ("#abz", out_str);
1258 TEST(URLCanonTest, CanonicalizeStandardURL) {
1259 // The individual component canonicalize tests should have caught the cases
1260 // for each of those components. Here, we just need to test that the various
1261 // parts are included or excluded properly, and have the correct separators.
1262 struct URLCase {
1263 const char* input;
1264 const char* expected;
1265 bool expected_success;
1266 } cases[] = {
1267 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1268 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1269 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1270 {"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo", true},
1271 {"www.google.com", ":www.google.com/", true},
1272 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1273 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1274 {"http://user:pass@/", "http://user:pass@/", false},
1275 {"http://%25DOMAIN:foobar@foodomain.com/", "http://%25DOMAIN:foobar@foodomain.com/", true},
1277 // Backslashes should get converted to forward slashes.
1278 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1280 // Busted refs shouldn't make the whole thing fail.
1281 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1283 // Basic port tests.
1284 {"http://foo:80/", "http://foo/", true},
1285 {"http://foo:81/", "http://foo:81/", true},
1286 {"httpa://foo:80/", "httpa://foo:80/", true},
1287 {"http://foo:-80/", "http://foo:-80/", false},
1289 {"https://foo:443/", "https://foo/", true},
1290 {"https://foo:80/", "https://foo:80/", true},
1291 {"ftp://foo:21/", "ftp://foo/", true},
1292 {"ftp://foo:80/", "ftp://foo:80/", true},
1293 {"gopher://foo:70/", "gopher://foo/", true},
1294 {"gopher://foo:443/", "gopher://foo:443/", true},
1295 {"ws://foo:80/", "ws://foo/", true},
1296 {"ws://foo:81/", "ws://foo:81/", true},
1297 {"ws://foo:443/", "ws://foo:443/", true},
1298 {"ws://foo:815/", "ws://foo:815/", true},
1299 {"wss://foo:80/", "wss://foo:80/", true},
1300 {"wss://foo:81/", "wss://foo:81/", true},
1301 {"wss://foo:443/", "wss://foo/", true},
1302 {"wss://foo:815/", "wss://foo:815/", true},
1305 for (size_t i = 0; i < arraysize(cases); i++) {
1306 int url_len = static_cast<int>(strlen(cases[i].input));
1307 Parsed parsed;
1308 ParseStandardURL(cases[i].input, url_len, &parsed);
1310 Parsed out_parsed;
1311 std::string out_str;
1312 StdStringCanonOutput output(&out_str);
1313 bool success = CanonicalizeStandardURL(
1314 cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
1315 output.Complete();
1317 EXPECT_EQ(cases[i].expected_success, success);
1318 EXPECT_EQ(cases[i].expected, out_str);
1322 // The codepath here is the same as for regular canonicalization, so we just
1323 // need to test that things are replaced or not correctly.
1324 TEST(URLCanonTest, ReplaceStandardURL) {
1325 ReplaceCase replace_cases[] = {
1326 // Common case of truncating the path.
1327 {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
1328 // Replace everything
1329 {"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"},
1330 // Replace nothing
1331 {"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"},
1332 // Replace scheme with filesystem. The result is garbage, but you asked
1333 // for it.
1334 {"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"},
1337 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1338 const ReplaceCase& cur = replace_cases[i];
1339 int base_len = static_cast<int>(strlen(cur.base));
1340 Parsed parsed;
1341 ParseStandardURL(cur.base, base_len, &parsed);
1343 Replacements<char> r;
1344 typedef Replacements<char> R; // Clean up syntax.
1346 // Note that for the scheme we pass in a different clear function since
1347 // there is no function to clear the scheme.
1348 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1349 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1350 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1351 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1352 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1353 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1354 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1355 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1357 std::string out_str;
1358 StdStringCanonOutput output(&out_str);
1359 Parsed out_parsed;
1360 ReplaceStandardURL(replace_cases[i].base, parsed, r, NULL, &output,
1361 &out_parsed);
1362 output.Complete();
1364 EXPECT_EQ(replace_cases[i].expected, out_str);
1367 // The path pointer should be ignored if the address is invalid.
1369 const char src[] = "http://www.google.com/here_is_the_path";
1370 int src_len = static_cast<int>(strlen(src));
1372 Parsed parsed;
1373 ParseStandardURL(src, src_len, &parsed);
1375 // Replace the path to 0 length string. By using 1 as the string address,
1376 // the test should get an access violation if it tries to dereference it.
1377 Replacements<char> r;
1378 r.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0));
1379 std::string out_str1;
1380 StdStringCanonOutput output1(&out_str1);
1381 Parsed new_parsed;
1382 ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
1383 output1.Complete();
1384 EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
1386 // Same with an "invalid" path.
1387 r.SetPath(reinterpret_cast<char*>(0x00000001), Component());
1388 std::string out_str2;
1389 StdStringCanonOutput output2(&out_str2);
1390 ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
1391 output2.Complete();
1392 EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
1396 TEST(URLCanonTest, ReplaceFileURL) {
1397 ReplaceCase replace_cases[] = {
1398 // Replace everything
1399 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1400 // Replace nothing
1401 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1402 // Clear non-path components (common)
1403 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
1404 // Replace path with something that doesn't begin with a slash and make
1405 // sure it gets added properly.
1406 {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1407 {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1408 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
1409 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
1410 {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1411 // Replace scheme -- shouldn't do anything.
1412 {"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1415 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1416 const ReplaceCase& cur = replace_cases[i];
1417 int base_len = static_cast<int>(strlen(cur.base));
1418 Parsed parsed;
1419 ParseFileURL(cur.base, base_len, &parsed);
1421 Replacements<char> r;
1422 typedef Replacements<char> R; // Clean up syntax.
1423 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1424 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1425 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1426 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1427 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1428 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1429 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1430 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1432 std::string out_str;
1433 StdStringCanonOutput output(&out_str);
1434 Parsed out_parsed;
1435 ReplaceFileURL(cur.base, parsed, r, NULL, &output, &out_parsed);
1436 output.Complete();
1438 EXPECT_EQ(replace_cases[i].expected, out_str);
1442 TEST(URLCanonTest, ReplaceFileSystemURL) {
1443 ReplaceCase replace_cases[] = {
1444 // Replace everything in the outer URL.
1445 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1446 // Replace nothing
1447 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"},
1448 // Clear non-path components (common)
1449 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"},
1450 // Replace path with something that doesn't begin with a slash and make
1451 // sure it gets added properly.
1452 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"},
1453 // Replace scheme -- shouldn't do anything.
1454 {"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"},
1455 // Replace username -- shouldn't do anything.
1456 {"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"},
1457 // Replace password -- shouldn't do anything.
1458 {"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"},
1459 // Replace host -- shouldn't do anything.
1460 {"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"},
1461 // Replace port -- shouldn't do anything.
1462 {"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"},
1465 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1466 const ReplaceCase& cur = replace_cases[i];
1467 int base_len = static_cast<int>(strlen(cur.base));
1468 Parsed parsed;
1469 ParseFileSystemURL(cur.base, base_len, &parsed);
1471 Replacements<char> r;
1472 typedef Replacements<char> R; // Clean up syntax.
1473 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1474 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1475 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1476 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1477 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1478 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1479 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1480 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1482 std::string out_str;
1483 StdStringCanonOutput output(&out_str);
1484 Parsed out_parsed;
1485 ReplaceFileSystemURL(cur.base, parsed, r, NULL, &output, &out_parsed);
1486 output.Complete();
1488 EXPECT_EQ(replace_cases[i].expected, out_str);
1492 TEST(URLCanonTest, ReplacePathURL) {
1493 ReplaceCase replace_cases[] = {
1494 // Replace everything
1495 {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
1496 // Replace nothing
1497 {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
1498 // Replace one or the other
1499 {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
1500 {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
1501 {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
1504 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1505 const ReplaceCase& cur = replace_cases[i];
1506 int base_len = static_cast<int>(strlen(cur.base));
1507 Parsed parsed;
1508 ParsePathURL(cur.base, base_len, false, &parsed);
1510 Replacements<char> r;
1511 typedef Replacements<char> R; // Clean up syntax.
1512 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1513 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1514 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1515 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1516 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1517 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1518 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1519 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1521 std::string out_str;
1522 StdStringCanonOutput output(&out_str);
1523 Parsed out_parsed;
1524 ReplacePathURL(cur.base, parsed, r, &output, &out_parsed);
1525 output.Complete();
1527 EXPECT_EQ(replace_cases[i].expected, out_str);
1531 TEST(URLCanonTest, ReplaceMailtoURL) {
1532 ReplaceCase replace_cases[] = {
1533 // Replace everything
1534 {"mailto:jon@foo.com?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
1535 // Replace nothing
1536 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:jon@foo.com?body=sup"},
1537 // Replace the path
1538 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
1539 // Replace the query
1540 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:jon@foo.com?custom=1"},
1541 // Replace the path and query
1542 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
1543 // Set the query to empty (should leave trailing question mark)
1544 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:jon@foo.com?"},
1545 // Clear the query
1546 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:jon@foo.com"},
1547 // Clear the path
1548 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
1549 // Clear the path + query
1550 {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
1551 // Setting the ref should have no effect
1552 {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
1555 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1556 const ReplaceCase& cur = replace_cases[i];
1557 int base_len = static_cast<int>(strlen(cur.base));
1558 Parsed parsed;
1559 ParseMailtoURL(cur.base, base_len, &parsed);
1561 Replacements<char> r;
1562 typedef Replacements<char> R;
1563 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1564 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1565 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1566 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1567 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1568 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1569 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1570 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1572 std::string out_str;
1573 StdStringCanonOutput output(&out_str);
1574 Parsed out_parsed;
1575 ReplaceMailtoURL(cur.base, parsed, r, &output, &out_parsed);
1576 output.Complete();
1578 EXPECT_EQ(replace_cases[i].expected, out_str);
1582 TEST(URLCanonTest, CanonicalizeFileURL) {
1583 struct URLCase {
1584 const char* input;
1585 const char* expected;
1586 bool expected_success;
1587 Component expected_host;
1588 Component expected_path;
1589 } cases[] = {
1590 #ifdef _WIN32
1591 // Windows-style paths
1592 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1593 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1594 {"file:", "file:///", true, Component(), Component(7, 1)},
1595 {"file:UNChost/path", "file://unchost/path", true, Component(7, 7), Component(14, 5)},
1596 // CanonicalizeFileURL supports absolute Windows style paths for IE
1597 // compatability. Note that the caller must decide that this is a file
1598 // URL itself so it can call the file canonicalizer. This is usually
1599 // done automatically as part of relative URL resolving.
1600 {"c:\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
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 {"//server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
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 // We should preserve the number of slashes after the colon for IE
1608 // compatability, except when there is none, in which case we should
1609 // add one.
1610 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1611 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1612 // Three slashes should be non-UNC, even if there is no drive spec (IE
1613 // does this, which makes the resulting request invalid).
1614 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(), Component(7, 12)},
1615 // TODO(brettw) we should probably fail for invalid host names, which
1616 // would change the expected result on this test. We also currently allow
1617 // colon even though it's probably invalid, because its currently the
1618 // "natural" result of the way the canonicalizer is written. There doesn't
1619 // seem to be a strong argument for why allowing it here would be bad, so
1620 // we just tolerate it and the load will fail later.
1621 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, Component(7, 2), Component(9, 16)},
1622 {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5), Component(12, 8)},
1623 // Make sure relative paths can't go above the "C:"
1624 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, Component(), Component(7, 12)},
1625 // Busted refs shouldn't make the whole thing fail.
1626 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, Component(), Component(7, 8)},
1627 #else
1628 // Unix-style paths
1629 {"file:///home/me", "file:///home/me", true, Component(), Component(7, 8)},
1630 // Windowsy ones should get still treated as Unix-style.
1631 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(), Component(7, 16)},
1632 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, Component(), Component(7, 19)},
1633 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
1634 {"//", "file:///", true, Component(), Component(7, 1)},
1635 {"///", "file:///", true, Component(), Component(7, 1)},
1636 {"///test", "file:///test", true, Component(), Component(7, 5)},
1637 {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)},
1638 {"file://localhost", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1639 {"file://localhost/", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1640 {"file://localhost/test", "file://localhost/test", true, Component(7, 9), Component(16, 5)},
1641 #endif // _WIN32
1644 for (size_t i = 0; i < arraysize(cases); i++) {
1645 int url_len = static_cast<int>(strlen(cases[i].input));
1646 Parsed parsed;
1647 ParseFileURL(cases[i].input, url_len, &parsed);
1649 Parsed out_parsed;
1650 std::string out_str;
1651 StdStringCanonOutput output(&out_str);
1652 bool success = CanonicalizeFileURL(cases[i].input, url_len, parsed, NULL,
1653 &output, &out_parsed);
1654 output.Complete();
1656 EXPECT_EQ(cases[i].expected_success, success);
1657 EXPECT_EQ(cases[i].expected, out_str);
1659 // Make sure the spec was properly identified, the file canonicalizer has
1660 // different code for writing the spec.
1661 EXPECT_EQ(0, out_parsed.scheme.begin);
1662 EXPECT_EQ(4, out_parsed.scheme.len);
1664 EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
1665 EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
1667 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1668 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1672 TEST(URLCanonTest, CanonicalizeFileSystemURL) {
1673 struct URLCase {
1674 const char* input;
1675 const char* expected;
1676 bool expected_success;
1677 } cases[] = {
1678 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1679 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1680 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1681 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1682 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1683 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1684 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1687 for (size_t i = 0; i < arraysize(cases); i++) {
1688 int url_len = static_cast<int>(strlen(cases[i].input));
1689 Parsed parsed;
1690 ParseFileSystemURL(cases[i].input, url_len, &parsed);
1692 Parsed out_parsed;
1693 std::string out_str;
1694 StdStringCanonOutput output(&out_str);
1695 bool success = CanonicalizeFileSystemURL(cases[i].input, url_len, parsed,
1696 NULL, &output, &out_parsed);
1697 output.Complete();
1699 EXPECT_EQ(cases[i].expected_success, success);
1700 EXPECT_EQ(cases[i].expected, out_str);
1702 // Make sure the spec was properly identified, the filesystem canonicalizer
1703 // has different code for writing the spec.
1704 EXPECT_EQ(0, out_parsed.scheme.begin);
1705 EXPECT_EQ(10, out_parsed.scheme.len);
1706 if (success)
1707 EXPECT_GT(out_parsed.path.len, 0);
1711 TEST(URLCanonTest, CanonicalizePathURL) {
1712 // Path URLs should get canonicalized schemes but nothing else.
1713 struct PathCase {
1714 const char* input;
1715 const char* expected;
1716 } path_cases[] = {
1717 {"javascript:", "javascript:"},
1718 {"JavaScript:Foo", "javascript:Foo"},
1719 {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1722 for (size_t i = 0; i < arraysize(path_cases); i++) {
1723 int url_len = static_cast<int>(strlen(path_cases[i].input));
1724 Parsed parsed;
1725 ParsePathURL(path_cases[i].input, url_len, true, &parsed);
1727 Parsed out_parsed;
1728 std::string out_str;
1729 StdStringCanonOutput output(&out_str);
1730 bool success = CanonicalizePathURL(path_cases[i].input, url_len, parsed,
1731 &output, &out_parsed);
1732 output.Complete();
1734 EXPECT_TRUE(success);
1735 EXPECT_EQ(path_cases[i].expected, out_str);
1737 EXPECT_EQ(0, out_parsed.host.begin);
1738 EXPECT_EQ(-1, out_parsed.host.len);
1740 // When we end with a colon at the end, there should be no path.
1741 if (path_cases[i].input[url_len - 1] == ':') {
1742 EXPECT_EQ(0, out_parsed.GetContent().begin);
1743 EXPECT_EQ(-1, out_parsed.GetContent().len);
1748 TEST(URLCanonTest, CanonicalizeMailtoURL) {
1749 struct URLCase {
1750 const char* input;
1751 const char* expected;
1752 bool expected_success;
1753 Component expected_path;
1754 Component expected_query;
1755 } cases[] = {
1756 {"mailto:addr1", "mailto:addr1", true, Component(7, 5), Component()},
1757 {"mailto:addr1@foo.com", "mailto:addr1@foo.com", true, Component(7, 13), Component()},
1758 // Trailing whitespace is stripped.
1759 {"MaIlTo:addr1 \t ", "mailto:addr1", true, Component(7, 5), Component()},
1760 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, Component(7, 5), Component(13,6)},
1761 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, Component(7, 11), Component()},
1762 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, Component(7, 12), Component()},
1763 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, Component(7, 13), Component()},
1764 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, Component(7, 12), Component()},
1765 // Null character should be escaped to %00
1766 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, Component(7, 13), Component(21, 3)},
1767 // Invalid -- UTF-8 encoded surrogate value.
1768 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, Component(7, 9), Component()},
1769 {"mailto:addr1?", "mailto:addr1?", true, Component(7, 5), Component(13, 0)},
1772 // Define outside of loop to catch bugs where components aren't reset
1773 Parsed parsed;
1774 Parsed out_parsed;
1776 for (size_t i = 0; i < arraysize(cases); i++) {
1777 int url_len = static_cast<int>(strlen(cases[i].input));
1778 if (i == 8) {
1779 // The 9th test case purposely has a '\0' in it -- don't count it
1780 // as the string terminator.
1781 url_len = 22;
1783 ParseMailtoURL(cases[i].input, url_len, &parsed);
1785 std::string out_str;
1786 StdStringCanonOutput output(&out_str);
1787 bool success = CanonicalizeMailtoURL(cases[i].input, url_len, parsed,
1788 &output, &out_parsed);
1789 output.Complete();
1791 EXPECT_EQ(cases[i].expected_success, success);
1792 EXPECT_EQ(cases[i].expected, out_str);
1794 // Make sure the spec was properly identified
1795 EXPECT_EQ(0, out_parsed.scheme.begin);
1796 EXPECT_EQ(6, out_parsed.scheme.len);
1798 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1799 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1801 EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
1802 EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
1806 #ifndef WIN32
1808 TEST(URLCanonTest, _itoa_s) {
1809 // We fill the buffer with 0xff to ensure that it's getting properly
1810 // null-terminated. We also allocate one byte more than what we tell
1811 // _itoa_s about, and ensure that the extra byte is untouched.
1812 char buf[6];
1813 memset(buf, 0xff, sizeof(buf));
1814 EXPECT_EQ(0, _itoa_s(12, buf, sizeof(buf) - 1, 10));
1815 EXPECT_STREQ("12", buf);
1816 EXPECT_EQ('\xFF', buf[3]);
1818 // Test the edge cases - exactly the buffer size and one over
1819 memset(buf, 0xff, sizeof(buf));
1820 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 10));
1821 EXPECT_STREQ("1234", buf);
1822 EXPECT_EQ('\xFF', buf[5]);
1824 memset(buf, 0xff, sizeof(buf));
1825 EXPECT_EQ(EINVAL, _itoa_s(12345, buf, sizeof(buf) - 1, 10));
1826 EXPECT_EQ('\xFF', buf[5]); // should never write to this location
1828 // Test the template overload (note that this will see the full buffer)
1829 memset(buf, 0xff, sizeof(buf));
1830 EXPECT_EQ(0, _itoa_s(12, buf, 10));
1831 EXPECT_STREQ("12", buf);
1832 EXPECT_EQ('\xFF', buf[3]);
1834 memset(buf, 0xff, sizeof(buf));
1835 EXPECT_EQ(0, _itoa_s(12345, buf, 10));
1836 EXPECT_STREQ("12345", buf);
1838 EXPECT_EQ(EINVAL, _itoa_s(123456, buf, 10));
1840 // Test that radix 16 is supported.
1841 memset(buf, 0xff, sizeof(buf));
1842 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 16));
1843 EXPECT_STREQ("4d2", buf);
1844 EXPECT_EQ('\xFF', buf[5]);
1847 TEST(URLCanonTest, _itow_s) {
1848 // We fill the buffer with 0xff to ensure that it's getting properly
1849 // null-terminated. We also allocate one byte more than what we tell
1850 // _itoa_s about, and ensure that the extra byte is untouched.
1851 base::char16 buf[6];
1852 const char fill_mem = 0xff;
1853 const base::char16 fill_char = 0xffff;
1854 memset(buf, fill_mem, sizeof(buf));
1855 EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
1856 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
1857 EXPECT_EQ(fill_char, buf[3]);
1859 // Test the edge cases - exactly the buffer size and one over
1860 EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
1861 EXPECT_EQ(WStringToUTF16(L"1234"), base::string16(buf));
1862 EXPECT_EQ(fill_char, buf[5]);
1864 memset(buf, fill_mem, sizeof(buf));
1865 EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
1866 EXPECT_EQ(fill_char, buf[5]); // should never write to this location
1868 // Test the template overload (note that this will see the full buffer)
1869 memset(buf, fill_mem, sizeof(buf));
1870 EXPECT_EQ(0, _itow_s(12, buf, 10));
1871 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
1872 EXPECT_EQ(fill_char, buf[3]);
1874 memset(buf, fill_mem, sizeof(buf));
1875 EXPECT_EQ(0, _itow_s(12345, buf, 10));
1876 EXPECT_EQ(WStringToUTF16(L"12345"), base::string16(buf));
1878 EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10));
1881 #endif // !WIN32
1883 // Returns true if the given two structures are the same.
1884 static bool ParsedIsEqual(const Parsed& a, const Parsed& b) {
1885 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
1886 a.username.begin == b.username.begin && a.username.len == b.username.len &&
1887 a.password.begin == b.password.begin && a.password.len == b.password.len &&
1888 a.host.begin == b.host.begin && a.host.len == b.host.len &&
1889 a.port.begin == b.port.begin && a.port.len == b.port.len &&
1890 a.path.begin == b.path.begin && a.path.len == b.path.len &&
1891 a.query.begin == b.query.begin && a.query.len == b.query.len &&
1892 a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
1895 TEST(URLCanonTest, ResolveRelativeURL) {
1896 struct RelativeCase {
1897 const char* base; // Input base URL: MUST BE CANONICAL
1898 bool is_base_hier; // Is the base URL hierarchical
1899 bool is_base_file; // Tells us if the base is a file URL.
1900 const char* test; // Input URL to test against.
1901 bool succeed_relative; // Whether we expect IsRelativeURL to succeed
1902 bool is_rel; // Whether we expect |test| to be relative or not.
1903 bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed.
1904 const char* resolved; // What we expect in the result when resolving.
1905 } rel_cases[] = {
1906 // Basic absolute input.
1907 {"http://host/a", true, false, "http://another/", true, false, false, NULL},
1908 {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
1909 // Empty relative URLs should only remove the ref part of the URL,
1910 // leaving the rest unchanged.
1911 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
1912 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
1913 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
1914 // Spaces at the ends of the relative path should be ignored.
1915 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
1916 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
1917 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
1918 // Matching schemes without two slashes are treated as relative.
1919 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
1920 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
1921 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
1922 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
1923 // Nonmatching schemes are absolute.
1924 {"http://host/a", true, false, "https:host2", true, false, false, NULL},
1925 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
1926 // Absolute path input
1927 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
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", true, true, true, "http://host/c"},
1930 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
1931 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
1932 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
1933 // Relative path input
1934 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
1935 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
1936 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
1937 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
1938 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
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/a/"},
1942 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
1943 // Query input
1944 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
1945 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
1946 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
1947 // Ref input
1948 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
1949 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
1950 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
1951 // Non-hierarchical base: no relative handling. Relative input should
1952 // error, and if a scheme is present, it should be treated as absolute.
1953 {"data:foobar", false, false, "baz.html", false, false, false, NULL},
1954 {"data:foobar", false, false, "data:baz", true, false, false, NULL},
1955 {"data:foobar", false, false, "data:/base", true, false, false, NULL},
1956 // Non-hierarchical base: absolute input should succeed.
1957 {"data:foobar", false, false, "http://host/", true, false, false, NULL},
1958 {"data:foobar", false, false, "http:host", true, false, false, NULL},
1959 // Invalid schemes should be treated as relative.
1960 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
1961 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
1962 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
1963 {"data:asdf", false, false, ":foo", false, false, false, NULL},
1964 {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL},
1965 // We should treat semicolons like any other character in URL resolving
1966 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
1967 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
1968 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
1969 // Relative URLs can also be written as "//foo/bar" which is relative to
1970 // the scheme. In this case, it would take the old scheme, so for http
1971 // the example would resolve to "http://foo/bar".
1972 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
1973 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
1974 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
1975 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
1976 {"http://host/a", true, false, "//", true, true, false, "http:"},
1977 // IE will also allow one or the other to be a backslash to get the same
1978 // behavior.
1979 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
1980 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
1981 #ifdef WIN32
1982 // Resolving against Windows file base URLs.
1983 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
1984 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
1985 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
1986 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
1987 // But two backslashes on Windows should be UNC so should be treated
1988 // as absolute.
1989 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
1990 // IE doesn't support drive specs starting with two slashes. It fails
1991 // immediately and doesn't even try to load. We fix it up to either
1992 // an absolute path or UNC depending on what it looks like.
1993 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
1994 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
1995 // Windows drive specs should be allowed and treated as absolute.
1996 {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
1997 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
1998 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
1999 // Relative paths with drive letters should be allowed when the base is
2000 // also a file.
2001 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2002 // Treat absolute paths as being off of the drive.
2003 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2004 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2005 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2006 // On Windows, two slashes without a drive letter when the base is a file
2007 // means that the path is UNC.
2008 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2009 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2010 #else
2011 // On Unix we fall back to relative behavior since there's nothing else
2012 // reasonable to do.
2013 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2014 #endif
2015 // Even on Windows, we don't allow relative drive specs when the base
2016 // is not file.
2017 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2018 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
2019 // Ensure that ports aren't allowed for hosts relative to a file url.
2020 // Although the result string shows a host:port portion, the call to
2021 // resolve the relative URL returns false, indicating parse failure,
2022 // which is what is required.
2023 {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
2024 // Filesystem URL tests; filesystem URLs are only valid and relative if
2025 // they have no scheme, e.g. "./index.html". There's no valid equivalent
2026 // to http:index.html.
2027 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2028 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
2029 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
2030 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2031 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2032 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2033 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
2034 // Absolute URLs are still not relative to a non-standard base URL.
2035 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2036 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2039 for (size_t i = 0; i < arraysize(rel_cases); i++) {
2040 const RelativeCase& cur_case = rel_cases[i];
2042 Parsed parsed;
2043 int base_len = static_cast<int>(strlen(cur_case.base));
2044 if (cur_case.is_base_file)
2045 ParseFileURL(cur_case.base, base_len, &parsed);
2046 else if (cur_case.is_base_hier)
2047 ParseStandardURL(cur_case.base, base_len, &parsed);
2048 else
2049 ParsePathURL(cur_case.base, base_len, false, &parsed);
2051 // First see if it is relative.
2052 int test_len = static_cast<int>(strlen(cur_case.test));
2053 bool is_relative;
2054 Component relative_component;
2055 bool succeed_is_rel = IsRelativeURL(
2056 cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
2057 &is_relative, &relative_component);
2059 EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
2060 "succeed is rel failure on " << cur_case.test;
2061 EXPECT_EQ(cur_case.is_rel, is_relative) <<
2062 "is rel failure on " << cur_case.test;
2063 // Now resolve it.
2064 if (succeed_is_rel && is_relative && cur_case.is_rel) {
2065 std::string resolved;
2066 StdStringCanonOutput output(&resolved);
2067 Parsed resolved_parsed;
2069 bool succeed_resolve = ResolveRelativeURL(
2070 cur_case.base, parsed, cur_case.is_base_file, cur_case.test,
2071 relative_component, NULL, &output, &resolved_parsed);
2072 output.Complete();
2074 EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
2075 EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
2077 // Verify that the output parsed structure is the same as parsing a
2078 // the URL freshly.
2079 Parsed ref_parsed;
2080 int resolved_len = static_cast<int>(resolved.size());
2081 if (cur_case.is_base_file) {
2082 ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
2083 } else if (cur_case.is_base_hier) {
2084 ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
2085 } else {
2086 ParsePathURL(resolved.c_str(), resolved_len, false, &ref_parsed);
2088 EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
2093 // It used to be when we did a replacement with a long buffer of UTF-16
2094 // characters, we would get invalid data in the URL. This is because the buffer
2095 // it used to hold the UTF-8 data was resized, while some pointers were still
2096 // kept to the old buffer that was removed.
2097 TEST(URLCanonTest, ReplacementOverflow) {
2098 const char src[] = "file:///C:/foo/bar";
2099 int src_len = static_cast<int>(strlen(src));
2100 Parsed parsed;
2101 ParseFileURL(src, src_len, &parsed);
2103 // Override two components, the path with something short, and the query with
2104 // sonething long enough to trigger the bug.
2105 Replacements<base::char16> repl;
2106 base::string16 new_query;
2107 for (int i = 0; i < 4800; i++)
2108 new_query.push_back('a');
2110 base::string16 new_path(WStringToUTF16(L"/foo"));
2111 repl.SetPath(new_path.c_str(), Component(0, 4));
2112 repl.SetQuery(new_query.c_str(),
2113 Component(0, static_cast<int>(new_query.length())));
2115 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2116 // standard URLs, file URLs, etc, since they will go to the same replacement
2117 // function that was buggy.
2118 Parsed repl_parsed;
2119 std::string repl_str;
2120 StdStringCanonOutput repl_output(&repl_str);
2121 ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
2122 repl_output.Complete();
2124 // Generate the expected string and check.
2125 std::string expected("file:///foo?");
2126 for (size_t i = 0; i < new_query.length(); i++)
2127 expected.push_back('a');
2128 EXPECT_TRUE(expected == repl_str);
2131 } // namespace url