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