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