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)
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/macros.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "url/url_canon.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "url/url_canon_internal.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "url/url_canon_stdstring.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "url/url_parse.h"
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "url/url_test_utils.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Some implementations of base/basictypes.h may define ARRAYSIZE.
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// which is in our version of basictypes.h.
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef ARRAYSIZE
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define ARRAYSIZE ARRAYSIZE_UNSAFE
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnamespace url {
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochusing test_utils::WStringToUTF16;
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochusing test_utils::ConvertUTF8ToUTF16;
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochusing test_utils::ConvertUTF16ToUTF8;
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;
330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  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;
440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  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;
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  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) {
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    result.push_back(kHexCharLookup[(bytes[i] >> 4) & 0xf]);
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    result.push_back(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)// Magic string used in the replacements code that tells SetupReplComp to
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// call the clear function.
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kDeleteComp[] = "|";
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Sets up a replacement for a single component. This is given pointers to
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// the set and clear function for the component being replaced, and will
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// either set the component (if it exists) or clear it (if the replacement
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// string matches kDeleteComp).
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// This template is currently used only for the 8-bit case, and the strlen
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// causes it to fail in other cases. It is left a template in case we have
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// tests for wide replacements.
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)template<typename CHAR>
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void SetupReplComp(
1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    void (Replacements<CHAR>::*set)(const CHAR*, const Component&),
1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    void (Replacements<CHAR>::*clear)(),
1020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Replacements<CHAR>* rep,
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const CHAR* str) {
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (str && str[0] == kDeleteComp[0]) {
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    (rep->*clear)();
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (str) {
1070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    (rep->*set)(str, Component(0, static_cast<int>(strlen(str))));
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(URLCanonTest, DoAppendUTF8) {
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  struct UTF8Case {
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    unsigned input;
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const char* output;
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } utf_cases[] = {
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Valid code points.
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {0x24, "\x24"},
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {0xA2, "\xC2\xA2"},
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {0x20AC, "\xE2\x82\xAC"},
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {0x24B62, "\xF0\xA4\xAD\xA2"},
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {0x10FFFF, "\xF4\x8F\xBF\xBF"},
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string out_str;
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE(utf_cases); i++) {
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    out_str.clear();
1280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output(&out_str);
1290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    AppendUTF8Value(utf_cases[i].input, &output);
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output.Complete();
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(utf_cases[i].output, out_str);
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
135f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(GTEST_HAS_DEATH_TEST)
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// TODO(mattm): Can't run this in debug mode for now, since the DCHECK will
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// cause the Chromium stacktrace dialog to appear and hang the test.
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// See http://crbug.com/49580.
139f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
140f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#define MAYBE_DoAppendUTF8Invalid DoAppendUTF8Invalid
141f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#else
142f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#define MAYBE_DoAppendUTF8Invalid DISABLED_DoAppendUTF8Invalid
143f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
144f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TEST(URLCanonTest, MAYBE_DoAppendUTF8Invalid) {
145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string out_str;
1460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  StdStringCanonOutput output(&out_str);
147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Invalid code point (too large).
148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_DEBUG_DEATH({
1490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    AppendUTF8Value(0x110000, &output);
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output.Complete();
151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ("", out_str);
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }, "");
153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
154f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // defined(GTEST_HAS_DEATH_TEST)
155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(URLCanonTest, UTF) {
157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Low-level test that we handle reading, canonicalization, and writing
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // UTF-8/UTF-16 strings properly.
159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  struct UTFCase {
160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const char* input8;
161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const wchar_t* input16;
162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool expected_success;
163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const char* output;
164c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } utf_cases[] = {
165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Valid canonical input should get passed through & escaped.
166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Non-shortest-form UTF-8 are invalid. The bad char should be replaced
170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // with the invalid character (EF BF DB in UTF-8).
171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"},
172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Invalid UTF-8 sequences should be marked as invalid (the first
173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // sequence is truncated).
174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Character going off the end.
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // ...same with low surrogates with no high surrogate.
178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"},
179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Test a UTF-8 encoded surrogate value is marked as invalid.
180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // ED A0 80 = U+D800
181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string out_str;
185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE(utf_cases); i++) {
186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (utf_cases[i].input8) {
187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      out_str.clear();
1880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      StdStringCanonOutput output(&out_str);
189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int input_len = static_cast<int>(strlen(utf_cases[i].input8));
191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      bool success = true;
192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      for (int ch = 0; ch < input_len; ch++) {
193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                         &output);
195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      output.Complete();
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(utf_cases[i].expected_success, success);
198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(std::string(utf_cases[i].output), out_str);
199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (utf_cases[i].input16) {
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      out_str.clear();
2020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      StdStringCanonOutput output(&out_str);
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::string16 input_str(WStringToUTF16(utf_cases[i].input16));
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int input_len = static_cast<int>(input_str.length());
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      bool success = true;
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      for (int ch = 0; ch < input_len; ch++) {
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                         &output);
210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      output.Complete();
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(utf_cases[i].expected_success, success);
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(std::string(utf_cases[i].output), out_str);
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (utf_cases[i].input8 && utf_cases[i].input16 &&
217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        utf_cases[i].expected_success) {
218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Check that the UTF-8 and UTF-16 inputs are equivalent.
219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // UTF-16 -> UTF-8
221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string input8_str(utf_cases[i].input8);
2227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::string16 input16_str(WStringToUTF16(utf_cases[i].input16));
223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str));
224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // UTF-8 -> UTF-16
226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(URLCanonTest, Scheme) {
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Here, we're mostly testing that unusual characters are handled properly.
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The canonicalizer doesn't do any parsing or whitespace detection. It will
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // also do its best on error, and will escape funny sequences (these won't be
235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // valid schemes and it will return error).
236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Note that the canonicalizer will append a colon to the output to separate
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // out the rest of the URL, which is not present in the input. We check,
239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // however, that the output range includes everything but the colon.
240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ComponentCase scheme_cases[] = {
2410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http", "http:", Component(0, 4), true},
2420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"HTTP", "http:", Component(0, 4), true},
2430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {" HTTP ", "%20http%20:", Component(0, 10), false},
2440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"htt: ", "htt%3A%20:", Component(0, 9), false},
2450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false},
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Don't re-escape something already escaped. Note that it will
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // "canonicalize" the 'A' to 'a', but that's OK.
2480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"ht%3Atp", "ht%3atp:", Component(0, 7), false},
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string out_str;
252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < arraysize(scheme_cases); i++) {
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    int url_len = static_cast<int>(strlen(scheme_cases[i].input));
2550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Component in_comp(0, url_len);
2560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Component out_comp;
257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    out_str.clear();
2590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output1(&out_str);
2600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    bool success = CanonicalizeScheme(scheme_cases[i].input, in_comp, &output1,
2610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                      &out_comp);
262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output1.Complete();
263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(scheme_cases[i].expected_success, success);
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Now try the wide version
270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    out_str.clear();
2710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output2(&out_str);
272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input));
274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    in_comp.len = static_cast<int>(wide_input.length());
2750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2,
2760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                 &out_comp);
277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output2.Complete();
278c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
279c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(scheme_cases[i].expected_success, success);
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
281c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
283c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Test the case where the scheme is declared nonexistant, it should be
286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // converted into an empty scheme.
2870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  Component out_comp;
288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  out_str.clear();
2890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  StdStringCanonOutput output(&out_str);
290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_TRUE(CanonicalizeScheme("", Component(0, -1), &output, &out_comp));
292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  output.Complete();
293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(std::string(":"), out_str);
295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(0, out_comp.begin);
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(0, out_comp.len);
297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(URLCanonTest, Host) {
300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  IPAddressCase host_cases[] = {
301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       // Basic canonicalization, uppercase should be converted to lowercase.
3020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Spaces and some other characters should be escaped.
3040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"Goo%20 goo%7C|.com", L"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", Component(0, 22), CanonHostInfo::NEUTRAL, -1, ""},
305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Exciting different types of spaces!
3060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Other types of space (no-break, zero-width, zero-width-no-break) are
308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // name-prepped away to nothing.
3090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Ideographic full stop (full-width period for Chinese, etc.) should be
311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // treated as a dot.
3120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {NULL, L"www.foo\x3002" L"bar.com", "www.foo.bar.com", Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Invalid unicode characters should fail...
314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // ...In wide input, ICU will barf and we'll end up with the input as
315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    escaped UTF-8 (the invalid character should be replaced with the
316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      //    replacement character).
3170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // ...This is the same as previous but with with escaped.
3190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"%ef%b7%90zyx.com", L"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Test name prepping, fullwidth input should be converted to ASCII and NOT
321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"\xef\xbc\xa7\xef\xbd\x8f.com", L"\xff27\xff4f.com", "go.com", Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
323c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Test that fullwidth escaped values are properly name-prepped,
324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // then converted or rejected.
325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
3260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
3270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"%ef%bc%85%ef%bc%94%ef%bc%91.com", L"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
3290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
3300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"%ef%bc%85%ef%bc%90%ef%bc%90.com", L"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
3320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d\x4f60\x597d", "xn--6qqa088eba", Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
3334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // See http://unicode.org/cldr/utility/idna.jsp for other
3344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // examples/experiments and http://goo.gl/7yG11o
3354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // for the full list of characters handled differently by
3364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
3374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // 4 Deviation characters are mapped/ignored in UTS 46 transitional
3394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // mechansm. UTS 46, table 4 row (g).
3404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
3414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Otherwise, it'd be "xn--fuball-cta.de".
3424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de",
3430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
3444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
3454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Otherwise, it'd be "xn--wxaijb9b".
3464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2",
3470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "xn--wxaikc6b", Component(0, 12),
3484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CanonHostInfo::NEUTRAL, -1, ""},
3494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
3504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // handling as well as in IDNA 2003.
3514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L"a\x200c" L"b\x200d" L"c", "abc",
3520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""},
3534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // ZWJ between Devanagari characters is still mapped away in UTS 46
3544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
3554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
3564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     L"\x915\x94d\x200d\x91c", "xn--11bo0m",
3570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
3584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
3594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // However, we do allow this at the moment because we don't use
3604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // STD3 rules and canonicalize full-width ASCII to ASCII.
3614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"wow\xef\xbc\x81", L"wow\xff01", "wow%21",
3620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
3634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
3644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
3654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo",
3660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
3674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
3684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
3694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn",
3704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "%F0%AF%A1%A8%E5%A7%BB.cn",
3710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 24), CanonHostInfo::BROKEN, -1, ""},
3724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
3734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya",
3740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
3754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Symbol/punctuations are allowed in IDNA 2003/UTS46.
3764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
3774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us",
3780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
3794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
3804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // We used to allow it because we passed through unassigned code points.
3814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com",
3820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""},
3834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
3844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Used to be allowed in INDA 2003.
3854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg",
3860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 9), CanonHostInfo::BROKEN, -1, ""},
3874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
3884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // on Unicode 3.2). We did allow it in the past because we let unassigned
3894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // code point pass. We continue to allow it even though it's a
3904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // "punctuation and symbol" blocked in IDNA 2008.
3914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // UTS 46 table 4, row (j)
3924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com",
3930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
3944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Maps uppercase letters to lower case letters.
3954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // In IDNA 2003, it's allowed without case-folding
3964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
3974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // (added in Unicode 4.1). UTS 46 table 4 row (k)
3984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com",
3990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
4004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // BiDi check test
4014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
4024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
4034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
4044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
4050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
4064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Disallowed in both IDNA 2003 and 2008 with BiDi check.
4074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Labels starting with a RTL character cannot end with a LTR character.
4084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz",
4090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21),
4104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     CanonHostInfo::BROKEN, -1, ""},
4114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Labels starting with a RTL character can end with BC=EN (European
4124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // number). Disallowed in IDNA 2003 but now allowed.
4134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2",
4140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     "xn--2-ymcov", Component(0, 11),
4154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     CanonHostInfo::NEUTRAL, -1, ""},
4164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Labels starting with a RTL character cannot have "L" characters
4174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
4184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2",
4190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21),
4204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     CanonHostInfo::BROKEN, -1, ""},
4214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Labels starting with a RTL character can end with BC=AN (Arabic number)
4224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Disallowed in IDNA 2003, but now allowed.
4234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662",
4240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     "xn--mgbjq0r", Component(0, 11),
4254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     CanonHostInfo::NEUTRAL, -1, ""},
4264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Labels starting with a RTL character cannot have "L" characters
4274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // even if it ends with an BC=AN (Arabic number).
4284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Disallowed in both IDNA 2003/2008.
4294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662",
4300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26),
4314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     CanonHostInfo::BROKEN, -1, ""},
4324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Labels starting with a RTL character cannot mix BC=EN and BC=AN
4334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662",
4340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch     "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27),
4354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)     CanonHostInfo::BROKEN, -1, ""},
4364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
4374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com",
4380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
4394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // U+0080 is not allowed.
4404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"\xc2\x80.com", L"\x80.com", "%C2%80.com",
4410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
4424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
444c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // UTF-8 (wide case). The output should be equivalent to the true wide
445c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // character input above).
4464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
4474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
4480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Invalid escaped characters should fail and the percents should be
450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // escaped.
4510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", Component(0, 10),
4524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CanonHostInfo::BROKEN, -1, ""},
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // If we get an invalid character that has been escaped.
4540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"%25", L"%25", "%25", Component(0, 3),
4554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CanonHostInfo::BROKEN, -1, ""},
4560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"hello%00", L"hello%00", "hello%00", Component(0, 8),
4574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CanonHostInfo::BROKEN, -1, ""},
458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Escaped numbers should be treated like IP addresses if they are.
4594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    {"%30%78%63%30%2e%30%32%35%30.01", L"%30%78%63%30%2e%30%32%35%30.01",
4600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
4614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "C0A80001"},
4624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (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",
4630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
4644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "C0A80001"},
465c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Invalid escaping should trigger the regular host error handling.
4660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"%3g%78%63%30%2e%30%32%35%30%2E.01", L"%3g%78%63%30%2e%30%32%35%30%2E.01", "%253gxc0.0250..01", Component(0, 17), CanonHostInfo::BROKEN, -1, ""},
467c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Something that isn't exactly an IP should get treated as a host and
468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // spaces escaped.
4690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.1 hello", L"192.168.0.1 hello", "192.168.0.1%20hello", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // These are "0Xc0.0250.01" in fullwidth.
4720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"\xef\xbc\x90%Ef%bc\xb8%ef%Bd%83\xef\xbc\x90%EF%BC%8E\xef\xbc\x90\xef\xbc\x92\xef\xbc\x95\xef\xbc\x90\xef\xbc%8E\xef\xbc\x90\xef\xbc\x91", L"\xff10\xff38\xff43\xff10\xff0e\xff10\xff12\xff15\xff10\xff0e\xff10\xff11", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Broken IP addresses get marked as such.
4740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.257", L"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
4750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[google.com]", L"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
4764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // Cyrillic letter followed by '(' should return punycode for '(' escaped
4774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // before punycode string was created. I.e.
4784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // if '(' is escaped after punycode is created we would get xn--%28-8tb
4794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      // (incorrect).
4800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", Component(0, 11),
4814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CanonHostInfo::NEUTRAL, -1, ""},
482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Address with all hexidecimal characters with leading number of 1<<32
483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // or greater and should return NEUTRAL rather than BROKEN if not all
484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // components are numbers.
4850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"12345678912345.de", L"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
4860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
4870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
4880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
4890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // CanonicalizeHost() non-verbose.
493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string out_str;
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < arraysize(host_cases); i++) {
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Narrow version.
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (host_cases[i].input8) {
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int host_len = static_cast<int>(strlen(host_cases[i].input8));
4980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component in_comp(0, host_len);
4990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component out_comp;
500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      out_str.clear();
5020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      StdStringCanonOutput output(&out_str);
503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      bool success = CanonicalizeHost(host_cases[i].input8, in_comp, &output,
5050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                      &out_comp);
506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      output.Complete();
507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
5094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                success) << "for input: " << host_cases[i].input8;
5104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      EXPECT_EQ(std::string(host_cases[i].expected), out_str) <<
5114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                "for input: " << host_cases[i].input8;
5124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) <<
5134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                "for input: " << host_cases[i].input8;
5144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) <<
5154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                "for input: " << host_cases[i].input8;
516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Wide version.
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (host_cases[i].input16) {
5207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::string16 input16(WStringToUTF16(host_cases[i].input16));
521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int host_len = static_cast<int>(input16.length());
5220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component in_comp(0, host_len);
5230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component out_comp;
524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      out_str.clear();
5260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      StdStringCanonOutput output(&out_str);
527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      bool success = CanonicalizeHost(input16.c_str(), in_comp, &output,
5290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                      &out_comp);
530c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      output.Complete();
531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                success);
534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(std::string(host_cases[i].expected), out_str);
535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
538c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // CanonicalizeHostVerbose()
541c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < arraysize(host_cases); i++) {
542c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Narrow version.
543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (host_cases[i].input8) {
544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int host_len = static_cast<int>(strlen(host_cases[i].input8));
5450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component in_comp(0, host_len);
546c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
547c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      out_str.clear();
5480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      StdStringCanonOutput output(&out_str);
549c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CanonHostInfo host_info;
550c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      CanonicalizeHostVerbose(host_cases[i].input8, in_comp, &output,
5520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                              &host_info);
553c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      output.Complete();
554c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
555c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_family, host_info.family);
556c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(std::string(host_cases[i].expected), out_str);
557c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_component.begin,
558c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                host_info.out_host.begin);
559c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
560c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
561c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                BytesToHexString(host_info.address, host_info.AddressLength()));
562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
563c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
564c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  host_info.num_ipv4_components);
565c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
566c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
568c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Wide version.
569c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (host_cases[i].input16) {
5707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::string16 input16(WStringToUTF16(host_cases[i].input16));
571c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int host_len = static_cast<int>(input16.length());
5720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      Component in_comp(0, host_len);
573c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
574c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      out_str.clear();
5750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      StdStringCanonOutput output(&out_str);
576c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CanonHostInfo host_info;
577c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info);
579c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      output.Complete();
580c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
581c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_family, host_info.family);
582c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(std::string(host_cases[i].expected), out_str);
583c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_component.begin,
584c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                host_info.out_host.begin);
585c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
586c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
587c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                BytesToHexString(host_info.address, host_info.AddressLength()));
588c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
589c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
590c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  host_info.num_ipv4_components);
591c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
592c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
593c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
594c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
595c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
596c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(URLCanonTest, IPv4) {
597c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  IPAddressCase cases[] = {
598c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Empty is not an IP address.
5990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
6000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {".", L".", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
601c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Regular IP addresses in different bases.
6020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.1", L"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
6030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
6040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0xC0.0Xa8.0x0.0x1", L"0xC0.0Xa8.0x0.0x1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
605c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Non-IP addresses due to invalid characters.
6060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.9.com", L"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
607c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Invalid characters for the base should be rejected.
6080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"19a.168.0.1", L"19a.168.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
6090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0308.0250.00.01", L"0308.0250.00.01", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
6100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // If there are not enough components, the last one should fill them out.
6120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192", L"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
6130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0xC0a80001", L"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
6140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"030052000001", L"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
6150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"000030052000001", L"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
6160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168", L"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
6170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
6180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
6190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.1", L"192.168.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
620c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Too many components means not an IP address.
6210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.0.1", L"192.168.0.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
622c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // We allow a single trailing dot.
6230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
6240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.1. hello", L"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
6250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.1..", L"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
626c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Two dots in a row means not an IP address.
6270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168..1", L"192.168..1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
628c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Any numerical overflow should be marked as BROKEN.
6290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0x100.0", L"0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0x100.0.0", L"0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0x100.0.0.0", L"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0x100.0.0", L"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0.0x100.0", L"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0.0.0x100", L"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0.0x10000", L"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0x1000000", L"0.0x1000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0x100000000", L"0x100000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
638c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Repeat the previous tests, minus 1, to verify boundaries.
6390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0xFF.0", L"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
6400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
6410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
6420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
6430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
6440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
6450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
6460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
6470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
648c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Old trunctations tests.  They're all "BROKEN" now.
6490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.257", L"192.168.0.257", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0xa20001", L"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.015052000001", L"192.015052000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0X12C0a80001", L"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
6540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"276.1.2", L"276.1.2", "", Component(), CanonHostInfo::BROKEN, -1, ""},
655c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Spaces should be rejected.
6560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"192.168.0.1 hello", L"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
657c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Very large numbers.
6580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
6590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
660c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // A number has no length limit, but long numbers can still overflow.
6610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"00000000000000000001", L"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
6620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
663c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // If a long component is non-numeric, it's a hostname, *not* a broken IP.
6640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
6650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
666c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Truncation of all zeros should still result in 0.
6670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"0.00.0x.0x0", L"0.00.0x.0x0", "0.0.0.0", Component(0, 7), CanonHostInfo::IPV4, 4, "00000000"},
668c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
669c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
670c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < arraysize(cases); i++) {
671c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // 8-bit version.
6720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Component component(0, static_cast<int>(strlen(cases[i].input8)));
673c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
674c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string out_str1;
6750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output1(&out_str1);
6760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    CanonHostInfo host_info;
6770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
678c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output1.Complete();
679c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(cases[i].expected_family, host_info.family);
681c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(std::string(cases[i].expected_address_hex),
682c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              BytesToHexString(host_info.address, host_info.AddressLength()));
683c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (host_info.family == CanonHostInfo::IPV4) {
684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_STREQ(cases[i].expected, out_str1.c_str());
685c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
686c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
687c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_num_ipv4_components,
688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                host_info.num_ipv4_components);
689c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
690c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // 16-bit version.
6927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::string16 input16(WStringToUTF16(cases[i].input16));
6930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    component = Component(0, static_cast<int>(input16.length()));
694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string out_str2;
6960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output2(&out_str2);
6970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output2.Complete();
699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
700c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(cases[i].expected_family, host_info.family);
701c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(std::string(cases[i].expected_address_hex),
702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              BytesToHexString(host_info.address, host_info.AddressLength()));
703c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (host_info.family == CanonHostInfo::IPV4) {
704c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_STREQ(cases[i].expected, out_str2.c_str());
705c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_num_ipv4_components,
708c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                host_info.num_ipv4_components);
709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
711c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(URLCanonTest, IPv6) {
714c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  IPAddressCase cases[] = {
715c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Empty is not an IP address.
7160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
717c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Non-IPs with [:] characters are marked BROKEN.
7180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {":", L":", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[", L"[", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[:", L"[:", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"]", L"]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {":]", L":]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[]", L"[]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[:]", L"[:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
725c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Regular IP address is invalid without bounding '[' and ']'.
7260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"2001:db8::1", L"2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[2001:db8::1", L"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"2001:db8::1]", L"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
729c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Regular IP addresses.
7300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::]", L"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
7310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::1]", L"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
7320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1::]", L"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
733c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
734c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Leading zeros should be stripped.
7350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[000:01:02:003:004:5:6:007]", L"[000:01:02:003:004:5:6:007]", "[0:1:2:3:4:5:6:7]", Component(0,17), CanonHostInfo::IPV6, -1, "00000001000200030004000500060007"},
736c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
737c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Upper case letters should be lowercased.
7380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[A:b:c:DE:fF:0:1:aC]", L"[A:b:c:DE:fF:0:1:aC]", "[a:b:c:de:ff:0:1:ac]", Component(0,20), CanonHostInfo::IPV6, -1, "000A000B000C00DE00FF0000000100AC"},
739c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
740c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // The same address can be written with different contractions, but should
741c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // get canonicalized to the same thing.
7420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1:0:0:2::3:0]", L"[1:0:0:2::3:0]", "[1::2:0:0:3:0]", Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
7430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1::2:0:0:3:0]", L"[1::2:0:0:3:0]", "[1::2:0:0:3:0]", Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
744c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
745c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Addresses with embedded IPv4.
7460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
7470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
7480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
7490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
7500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
751c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
752c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // IPv4 with last component missing.
7530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::ffff:192.1.2]", L"[::ffff:192.1.2]", "[::ffff:c001:2]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0010002"},
754c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // IPv4 using hex.
756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // TODO(eroman): Should this format be disallowed?
7570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // There may be zeros surrounding the "::" contraction.
7600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000008"},
761c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
763c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Can only have one "::" contraction in an IPv6 string literal.
7650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[2001::db8::1]", L"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
766c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // No more than 2 consecutive ':'s.
7670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[2001:db8:::1]", L"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[:::]", L"[:::]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
769c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Non-IP addresses due to invalid characters.
7700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[2001::.com]", L"[2001::.com]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
771c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // If there are not enough components, the last one should fill them out.
772c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // ... omitted at this time ...
773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Too many components means not an IP address.  Similarly with too few if using IPv4 compat or mapped addresses.
7740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
777c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Too many bits (even though 8 comonents, the last one holds 32 bits).
7780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[0:0:0:0:0:0:0:192.168.0.1]", L"[0:0:0:0:0:0:0:192.168.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Too many bits specified -- the contraction would have to be zero-length
781c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // to not exceed 128 bits.
7820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1:2:3:4:5:6::192.168.0.1]", L"[1:2:3:4:5:6::192.168.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
783c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
784c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // The contraction is for 16 bits of zero.
7850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1:2:3:4:5:6::8]", L"[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]", Component(0,17), CanonHostInfo::IPV6, -1, "00010002000300040005000600000008"},
786c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
787c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Cannot have a trailing colon.
7880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
7890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1:2:3:4:5:6:192.168.0.1:]", L"[1:2:3:4:5:6:192.168.0.1:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
790c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
791c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Cannot have negative numbers.
7920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
793c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
794c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
795c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // The scope_id should be included in the canonicalized URL, and is an
796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // unsigned decimal number.
797c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
798c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Invalid because no ID was given after the percent.
799c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
800c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Don't allow scope-id
8010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
8020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
8030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
8040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
8050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
806c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
807c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Don't allow leading or trailing colons.
8080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
8090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
8100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
811c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
812c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // We allow a single trailing dot.
813c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // ... omitted at this time ...
814c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Two dots in a row means not an IP address.
8150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
816c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Any non-first components get truncated to one byte.
817c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // ... omitted at this time ...
818c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Spaces should be rejected.
8190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
820c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
821c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
822c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < arraysize(cases); i++) {
823c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // 8-bit version.
8240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Component component(0, static_cast<int>(strlen(cases[i].input8)));
825c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
826c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string out_str1;
8270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output1(&out_str1);
8280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    CanonHostInfo host_info;
8290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
830c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output1.Complete();
831c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
832c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(cases[i].expected_family, host_info.family);
833c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(std::string(cases[i].expected_address_hex),
834c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
835c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (host_info.family == CanonHostInfo::IPV6) {
836c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_STREQ(cases[i].expected, out_str1.c_str());
837c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_component.begin,
838c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                host_info.out_host.begin);
839c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
840c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
841c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
842c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // 16-bit version.
8437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::string16 input16(WStringToUTF16(cases[i].input16));
8440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    component = Component(0, static_cast<int>(input16.length()));
845c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
846c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string out_str2;
8470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output2(&out_str2);
8480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
849c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output2.Complete();
850c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
851c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(cases[i].expected_family, host_info.family);
852c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(std::string(cases[i].expected_address_hex),
853c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)              BytesToHexString(host_info.address, host_info.AddressLength()));
854c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (host_info.family == CanonHostInfo::IPV6) {
855c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_STREQ(cases[i].expected, out_str2.c_str());
856c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
857c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
858c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
859c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
860c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
861c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
862c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(URLCanonTest, IPEmpty) {
863c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string out_str1;
8640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  StdStringCanonOutput output1(&out_str1);
8650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  CanonHostInfo host_info;
866c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
867c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This tests tests.
868c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const char spec[] = "192.168.0.1";
8690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  CanonicalizeIPAddress(spec, Component(), &output1, &host_info);
870c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(host_info.IsIPAddress());
871c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info);
873c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(host_info.IsIPAddress());
874c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
875c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
876c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(URLCanonTest, UserInfo) {
877c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Note that the canonicalizer should escape and treat empty components as
878c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // not being there.
879c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
880c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // We actually parse a full input URL so we can get the initial components.
881c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  struct UserComponentCase {
882c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const char* input;
883c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const char* expected;
8840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Component expected_username;
8850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Component expected_password;
886c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool expected_success;
887c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } user_info_cases[] = {
8880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http://user:pass@host.com/", "user:pass@", Component(0, 4), Component(5, 4), true},
8890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
8900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
8910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
8920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http://:foo@host.com/", ":foo@", Component(0, 0), Component(1, 3), true},
8930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http://^ :$\t@host.com/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
8940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
8950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"http://%2540:bar@domain.com/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
896c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
897c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // IE7 compatability: old versions allowed backslashes in usernames, but
898c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // IE7 does not. We disallow it as well.
8990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    {"ftp://me\\mydomain:pass@foo.com/", "", Component(0, -1), Component(0, -1), true},
900c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
901c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
902c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE(user_info_cases); i++) {
903c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    int url_len = static_cast<int>(strlen(user_info_cases[i].input));
9040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Parsed parsed;
9050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
9060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    Component out_user, out_pass;
907c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string out_str;
9080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output1(&out_str);
9090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
9100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    bool success = CanonicalizeUserInfo(user_info_cases[i].input,
9110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                        parsed.username,
9120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                        user_info_cases[i].input,
9130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                        parsed.password,
9140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                        &output1,
9150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                        &out_user,
9160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                        &out_pass);
917c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output1.Complete();
918c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
919c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_success, success);
920c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
921c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
922c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
923c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
924c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
925c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
926c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Now try the wide version
927c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    out_str.clear();
9280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    StdStringCanonOutput output2(&out_str);
9297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
9300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    success = CanonicalizeUserInfo(wide_input.c_str(),
9310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                   parsed.username,
9320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                   wide_input.c_str(),
9330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                   parsed.password,
9340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                   &output2,
9350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                   &out_user,
9360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                   &out_pass);
937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    output2.Complete();
938c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_success, success);
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
944c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
945c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
946c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
947