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