blob: 9ca51aaaf37e8d818c27dcd056f124b47f5fac85 [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]e7bba5f82013-04-10 20:10:524
5#include <errno.h>
avic0c60312015-12-21 21:03:506#include <stddef.h>
[email protected]e7bba5f82013-04-10 20:10:527
[email protected]847aaab82014-05-07 14:05:468#include "base/macros.h"
brettw1b8582f2016-11-03 20:37:179#include "base/strings/utf_string_conversions.h"
[email protected]e7bba5f82013-04-10 20:10:5210#include "testing/gtest/include/gtest/gtest.h"
tfarina018de6e2015-05-26 17:41:2011#include "url/third_party/mozilla/url_parse.h"
[email protected]318076b2013-04-18 21:19:4512#include "url/url_canon.h"
[email protected]318076b2013-04-18 21:19:4513#include "url/url_canon_internal.h"
14#include "url/url_canon_stdstring.h"
[email protected]318076b2013-04-18 21:19:4515#include "url/url_test_utils.h"
[email protected]e7bba5f82013-04-10 20:10:5216
[email protected]0318f922014-04-22 00:09:2317namespace url {
18
[email protected]e7bba5f82013-04-10 20:10:5219namespace {
20
21struct ComponentCase {
22 const char* input;
23 const char* expected;
[email protected]0318f922014-04-22 00:09:2324 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5225 bool expected_success;
26};
27
28// ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
29// treat each input as optional, and will only try processing if non-NULL.
30// The output is always 8-bit.
31struct DualComponentCase {
32 const char* input8;
33 const wchar_t* input16;
34 const char* expected;
[email protected]0318f922014-04-22 00:09:2335 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5236 bool expected_success;
37};
38
qyearsley2bc727d2015-08-14 20:17:1539// Test cases for CanonicalizeIPAddress(). The inputs are identical to
[email protected]e7bba5f82013-04-10 20:10:5240// DualComponentCase, but the output has extra CanonHostInfo fields.
41struct IPAddressCase {
42 const char* input8;
43 const wchar_t* input16;
44 const char* expected;
[email protected]0318f922014-04-22 00:09:2345 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5246
47 // CanonHostInfo fields, for verbose output.
48 CanonHostInfo::Family expected_family;
49 int expected_num_ipv4_components;
50 const char* expected_address_hex; // Two hex chars per IP address byte.
51};
52
53std::string BytesToHexString(unsigned char bytes[16], int length) {
54 EXPECT_TRUE(length == 0 || length == 4 || length == 16)
55 << "Bad IP address length: " << length;
56 std::string result;
57 for (int i = 0; i < length; ++i) {
[email protected]0318f922014-04-22 00:09:2358 result.push_back(kHexCharLookup[(bytes[i] >> 4) & 0xf]);
59 result.push_back(kHexCharLookup[bytes[i] & 0xf]);
[email protected]e7bba5f82013-04-10 20:10:5260 }
61 return result;
62}
63
64struct ReplaceCase {
65 const char* base;
66 const char* scheme;
67 const char* username;
68 const char* password;
69 const char* host;
70 const char* port;
71 const char* path;
72 const char* query;
73 const char* ref;
74 const char* expected;
75};
76
[email protected]e7bba5f82013-04-10 20:10:5277// Magic string used in the replacements code that tells SetupReplComp to
78// call the clear function.
79const char kDeleteComp[] = "|";
80
81// Sets up a replacement for a single component. This is given pointers to
82// the set and clear function for the component being replaced, and will
83// either set the component (if it exists) or clear it (if the replacement
84// string matches kDeleteComp).
85//
86// This template is currently used only for the 8-bit case, and the strlen
87// causes it to fail in other cases. It is left a template in case we have
88// tests for wide replacements.
89template<typename CHAR>
90void SetupReplComp(
[email protected]0318f922014-04-22 00:09:2391 void (Replacements<CHAR>::*set)(const CHAR*, const Component&),
92 void (Replacements<CHAR>::*clear)(),
93 Replacements<CHAR>* rep,
[email protected]e7bba5f82013-04-10 20:10:5294 const CHAR* str) {
95 if (str && str[0] == kDeleteComp[0]) {
96 (rep->*clear)();
97 } else if (str) {
[email protected]0318f922014-04-22 00:09:2398 (rep->*set)(str, Component(0, static_cast<int>(strlen(str))));
[email protected]e7bba5f82013-04-10 20:10:5299 }
100}
101
102} // namespace
103
104TEST(URLCanonTest, DoAppendUTF8) {
105 struct UTF8Case {
106 unsigned input;
107 const char* output;
108 } utf_cases[] = {
109 // Valid code points.
110 {0x24, "\x24"},
111 {0xA2, "\xC2\xA2"},
112 {0x20AC, "\xE2\x82\xAC"},
113 {0x24B62, "\xF0\xA4\xAD\xA2"},
114 {0x10FFFF, "\xF4\x8F\xBF\xBF"},
115 };
116 std::string out_str;
viettrungluu4b6915862014-10-16 03:42:49117 for (size_t i = 0; i < arraysize(utf_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52118 out_str.clear();
[email protected]0318f922014-04-22 00:09:23119 StdStringCanonOutput output(&out_str);
120 AppendUTF8Value(utf_cases[i].input, &output);
[email protected]e7bba5f82013-04-10 20:10:52121 output.Complete();
122 EXPECT_EQ(utf_cases[i].output, out_str);
123 }
124}
125
[email protected]7c0fccb32014-06-17 00:50:40126#if defined(GTEST_HAS_DEATH_TEST)
[email protected]e7bba5f82013-04-10 20:10:52127// TODO(mattm): Can't run this in debug mode for now, since the DCHECK will
qyearsley2bc727d2015-08-14 20:17:15128// cause the Chromium stack trace dialog to appear and hang the test.
[email protected]e7bba5f82013-04-10 20:10:52129// See http://crbug.com/49580.
[email protected]7c0fccb32014-06-17 00:50:40130#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
131#define MAYBE_DoAppendUTF8Invalid DoAppendUTF8Invalid
132#else
133#define MAYBE_DoAppendUTF8Invalid DISABLED_DoAppendUTF8Invalid
134#endif
135TEST(URLCanonTest, MAYBE_DoAppendUTF8Invalid) {
[email protected]e7bba5f82013-04-10 20:10:52136 std::string out_str;
[email protected]0318f922014-04-22 00:09:23137 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52138 // Invalid code point (too large).
139 ASSERT_DEBUG_DEATH({
[email protected]0318f922014-04-22 00:09:23140 AppendUTF8Value(0x110000, &output);
[email protected]e7bba5f82013-04-10 20:10:52141 output.Complete();
142 EXPECT_EQ("", out_str);
143 }, "");
144}
[email protected]7c0fccb32014-06-17 00:50:40145#endif // defined(GTEST_HAS_DEATH_TEST)
[email protected]e7bba5f82013-04-10 20:10:52146
147TEST(URLCanonTest, UTF) {
148 // Low-level test that we handle reading, canonicalization, and writing
149 // UTF-8/UTF-16 strings properly.
150 struct UTFCase {
151 const char* input8;
152 const wchar_t* input16;
153 bool expected_success;
154 const char* output;
155 } utf_cases[] = {
156 // Valid canonical input should get passed through & escaped.
157 {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
qyearsley2bc727d2015-08-14 20:17:15158 // Test a character that takes > 16 bits (U+10300 = old italic letter A)
[email protected]e7bba5f82013-04-10 20:10:52159 {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
qyearsley2bc727d2015-08-14 20:17:15160 // Non-shortest-form UTF-8 characters are invalid. The bad character
161 // should be replaced with the invalid character (EF BF DB in UTF-8).
[email protected]e7bba5f82013-04-10 20:10:52162 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"},
163 // Invalid UTF-8 sequences should be marked as invalid (the first
164 // sequence is truncated).
165 {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
166 // Character going off the end.
167 {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
168 // ...same with low surrogates with no high surrogate.
169 {"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"},
170 // Test a UTF-8 encoded surrogate value is marked as invalid.
171 // ED A0 80 = U+D800
172 {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
173 };
174
175 std::string out_str;
viettrungluu4b6915862014-10-16 03:42:49176 for (size_t i = 0; i < arraysize(utf_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52177 if (utf_cases[i].input8) {
178 out_str.clear();
[email protected]0318f922014-04-22 00:09:23179 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52180
181 int input_len = static_cast<int>(strlen(utf_cases[i].input8));
182 bool success = true;
183 for (int ch = 0; ch < input_len; ch++) {
184 success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
185 &output);
186 }
187 output.Complete();
188 EXPECT_EQ(utf_cases[i].expected_success, success);
189 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
190 }
191 if (utf_cases[i].input16) {
192 out_str.clear();
[email protected]0318f922014-04-22 00:09:23193 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52194
brettw1b8582f2016-11-03 20:37:17195 base::string16 input_str(
196 test_utils::TruncateWStringToUTF16(utf_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52197 int input_len = static_cast<int>(input_str.length());
198 bool success = true;
199 for (int ch = 0; ch < input_len; ch++) {
200 success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
201 &output);
202 }
203 output.Complete();
204 EXPECT_EQ(utf_cases[i].expected_success, success);
205 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
206 }
207
208 if (utf_cases[i].input8 && utf_cases[i].input16 &&
209 utf_cases[i].expected_success) {
210 // Check that the UTF-8 and UTF-16 inputs are equivalent.
211
212 // UTF-16 -> UTF-8
213 std::string input8_str(utf_cases[i].input8);
brettw1b8582f2016-11-03 20:37:17214 base::string16 input16_str(
215 test_utils::TruncateWStringToUTF16(utf_cases[i].input16));
216 EXPECT_EQ(input8_str, base::UTF16ToUTF8(input16_str));
[email protected]e7bba5f82013-04-10 20:10:52217
218 // UTF-8 -> UTF-16
brettw1b8582f2016-11-03 20:37:17219 EXPECT_EQ(input16_str, base::UTF8ToUTF16(input8_str));
[email protected]e7bba5f82013-04-10 20:10:52220 }
221 }
222}
223
[email protected]e7bba5f82013-04-10 20:10:52224TEST(URLCanonTest, Scheme) {
225 // Here, we're mostly testing that unusual characters are handled properly.
226 // The canonicalizer doesn't do any parsing or whitespace detection. It will
227 // also do its best on error, and will escape funny sequences (these won't be
228 // valid schemes and it will return error).
229 //
230 // Note that the canonicalizer will append a colon to the output to separate
231 // out the rest of the URL, which is not present in the input. We check,
232 // however, that the output range includes everything but the colon.
233 ComponentCase scheme_cases[] = {
[email protected]0318f922014-04-22 00:09:23234 {"http", "http:", Component(0, 4), true},
235 {"HTTP", "http:", Component(0, 4), true},
236 {" HTTP ", "%20http%20:", Component(0, 10), false},
237 {"htt: ", "htt%3A%20:", Component(0, 9), false},
238 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false},
[email protected]e7bba5f82013-04-10 20:10:52239 // Don't re-escape something already escaped. Note that it will
240 // "canonicalize" the 'A' to 'a', but that's OK.
[email protected]0318f922014-04-22 00:09:23241 {"ht%3Atp", "ht%3atp:", Component(0, 7), false},
brettw46f9b832016-10-05 19:22:48242 {"", ":", Component(0, 0), false},
[email protected]e7bba5f82013-04-10 20:10:52243 };
244
245 std::string out_str;
246
247 for (size_t i = 0; i < arraysize(scheme_cases); i++) {
248 int url_len = static_cast<int>(strlen(scheme_cases[i].input));
[email protected]0318f922014-04-22 00:09:23249 Component in_comp(0, url_len);
250 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52251
252 out_str.clear();
[email protected]0318f922014-04-22 00:09:23253 StdStringCanonOutput output1(&out_str);
254 bool success = CanonicalizeScheme(scheme_cases[i].input, in_comp, &output1,
255 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52256 output1.Complete();
257
258 EXPECT_EQ(scheme_cases[i].expected_success, success);
259 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
260 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
261 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
262
qyearsley2bc727d2015-08-14 20:17:15263 // Now try the wide version.
[email protected]e7bba5f82013-04-10 20:10:52264 out_str.clear();
[email protected]0318f922014-04-22 00:09:23265 StdStringCanonOutput output2(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52266
brettw1b8582f2016-11-03 20:37:17267 base::string16 wide_input(base::UTF8ToUTF16(scheme_cases[i].input));
[email protected]e7bba5f82013-04-10 20:10:52268 in_comp.len = static_cast<int>(wide_input.length());
[email protected]0318f922014-04-22 00:09:23269 success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2,
270 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52271 output2.Complete();
272
273 EXPECT_EQ(scheme_cases[i].expected_success, success);
274 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
275 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
276 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
277 }
278
qyearsley2bc727d2015-08-14 20:17:15279 // Test the case where the scheme is declared nonexistent, it should be
[email protected]e7bba5f82013-04-10 20:10:52280 // converted into an empty scheme.
[email protected]0318f922014-04-22 00:09:23281 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52282 out_str.clear();
[email protected]0318f922014-04-22 00:09:23283 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52284
brettw46f9b832016-10-05 19:22:48285 EXPECT_FALSE(CanonicalizeScheme("", Component(0, -1), &output, &out_comp));
[email protected]e7bba5f82013-04-10 20:10:52286 output.Complete();
287
288 EXPECT_EQ(std::string(":"), out_str);
289 EXPECT_EQ(0, out_comp.begin);
290 EXPECT_EQ(0, out_comp.len);
291}
292
293TEST(URLCanonTest, Host) {
294 IPAddressCase host_cases[] = {
295 // Basic canonicalization, uppercase should be converted to lowercase.
[email protected]0318f922014-04-22 00:09:23296 {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52297 // Spaces and some other characters should be escaped.
[email protected]0318f922014-04-22 00:09:23298 {"Goo%20 goo%7C|.com", L"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", Component(0, 22), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52299 // Exciting different types of spaces!
[email protected]0318f922014-04-22 00:09:23300 {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52301 // Other types of space (no-break, zero-width, zero-width-no-break) are
302 // name-prepped away to nothing.
[email protected]0318f922014-04-22 00:09:23303 {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52304 // Ideographic full stop (full-width period for Chinese, etc.) should be
305 // treated as a dot.
[email protected]0318f922014-04-22 00:09:23306 {NULL, L"www.foo\x3002" L"bar.com", "www.foo.bar.com", Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52307 // Invalid unicode characters should fail...
308 // ...In wide input, ICU will barf and we'll end up with the input as
309 // escaped UTF-8 (the invalid character should be replaced with the
310 // replacement character).
[email protected]0318f922014-04-22 00:09:23311 {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52312 // ...This is the same as previous but with with escaped.
[email protected]0318f922014-04-22 00:09:23313 {"%ef%b7%90zyx.com", L"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52314 // Test name prepping, fullwidth input should be converted to ASCII and NOT
315 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
[email protected]0318f922014-04-22 00:09:23316 {"\xef\xbc\xa7\xef\xbd\x8f.com", L"\xff27\xff4f.com", "go.com", Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52317 // Test that fullwidth escaped values are properly name-prepped,
318 // then converted or rejected.
319 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
[email protected]0318f922014-04-22 00:09:23320 {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
321 {"%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, ""},
[email protected]e7bba5f82013-04-10 20:10:52322 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
[email protected]0318f922014-04-22 00:09:23323 {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
324 {"%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, ""},
brettw1141951b2015-11-26 00:29:35325 // ICU will convert weird percents into ASCII percents, but not unescape
326 // further. A weird percent is U+FE6A (EF B9 AA in UTF-8) which is a
327 // "small percent". At this point we should be within our rights to mark
328 // anything as invalid since the URL is corrupt or malicious. The code
329 // happens to allow ASCII characters (%41 = "A" -> 'a') to be unescaped
330 // and kept as valid, so we validate that behavior here, but this level
331 // of fixing the input shouldn't be seen as required. "%81" is invalid.
332 {"\xef\xb9\xaa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
333 {"%ef%b9%aa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
334 {"\xef\xb9\xaa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
335 {"%ef%b9%aa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52336 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
[email protected]0318f922014-04-22 00:09:23337 {"\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, ""},
[email protected]18f00cd2013-09-29 10:52:50338 // See http://unicode.org/cldr/utility/idna.jsp for other
339 // examples/experiments and http://goo.gl/7yG11o
340 // for the full list of characters handled differently by
341 // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
342
343 // 4 Deviation characters are mapped/ignored in UTS 46 transitional
344 // mechansm. UTS 46, table 4 row (g).
345 // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
346 // Otherwise, it'd be "xn--fuball-cta.de".
347 {"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de",
[email protected]0318f922014-04-22 00:09:23348 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50349 // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
350 // Otherwise, it'd be "xn--wxaijb9b".
351 {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2",
[email protected]0318f922014-04-22 00:09:23352 "xn--wxaikc6b", Component(0, 12),
[email protected]18f00cd2013-09-29 10:52:50353 CanonHostInfo::NEUTRAL, -1, ""},
354 // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
355 // handling as well as in IDNA 2003.
356 {"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L"a\x200c" L"b\x200d" L"c", "abc",
[email protected]0318f922014-04-22 00:09:23357 Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50358 // ZWJ between Devanagari characters is still mapped away in UTS 46
359 // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
360 {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
361 L"\x915\x94d\x200d\x91c", "xn--11bo0m",
[email protected]0318f922014-04-22 00:09:23362 Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50363 // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
364 // However, we do allow this at the moment because we don't use
365 // STD3 rules and canonicalize full-width ASCII to ASCII.
366 {"wow\xef\xbc\x81", L"wow\xff01", "wow%21",
[email protected]0318f922014-04-22 00:09:23367 Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50368 // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
369 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
370 {"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo",
[email protected]0318f922014-04-22 00:09:23371 Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50372 // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
373 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
374 {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn",
375 "%F0%AF%A1%A8%E5%A7%BB.cn",
[email protected]0318f922014-04-22 00:09:23376 Component(0, 24), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50377 // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
378 {"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya",
[email protected]0318f922014-04-22 00:09:23379 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50380 // Symbol/punctuations are allowed in IDNA 2003/UTS46.
381 // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
382 {"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us",
[email protected]0318f922014-04-22 00:09:23383 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50384 // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
385 // We used to allow it because we passed through unassigned code points.
386 {"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com",
[email protected]0318f922014-04-22 00:09:23387 Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50388 // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
389 // Used to be allowed in INDA 2003.
390 {"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg",
[email protected]0318f922014-04-22 00:09:23391 Component(0, 9), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50392 // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
393 // on Unicode 3.2). We did allow it in the past because we let unassigned
394 // code point pass. We continue to allow it even though it's a
395 // "punctuation and symbol" blocked in IDNA 2008.
396 // UTS 46 table 4, row (j)
397 {"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com",
[email protected]0318f922014-04-22 00:09:23398 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50399 // Maps uppercase letters to lower case letters.
400 // In IDNA 2003, it's allowed without case-folding
401 // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
402 // (added in Unicode 4.1). UTS 46 table 4 row (k)
403 {"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com",
[email protected]0318f922014-04-22 00:09:23404 Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
jshin62a92832016-03-18 18:42:39405 // Maps U+FF43 (Full Width Small Letter C) to 'c'.
406 {"ab\xef\xbd\x83.xyz", L"ab\xff43.xyz", "abc.xyz",
407 Component(0, 7), CanonHostInfo::NEUTRAL, -1, ""},
408 // Maps U+1D68C (Math Monospace Small C) to 'c'.
409 // U+1D68C = \xD835\xDE8C in UTF-16
410 {"ab\xf0\x9d\x9a\x8c.xyz", L"ab\xd835\xde8c.xyz", "abc.xyz",
411 Component(0, 7), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50412 // BiDi check test
413 // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
414 // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
415 {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
416 L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
[email protected]0318f922014-04-22 00:09:23417 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50418 // Disallowed in both IDNA 2003 and 2008 with BiDi check.
419 // Labels starting with a RTL character cannot end with a LTR character.
420 {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz",
[email protected]0318f922014-04-22 00:09:23421 "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21),
[email protected]18f00cd2013-09-29 10:52:50422 CanonHostInfo::BROKEN, -1, ""},
423 // Labels starting with a RTL character can end with BC=EN (European
424 // number). Disallowed in IDNA 2003 but now allowed.
425 {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2",
[email protected]0318f922014-04-22 00:09:23426 "xn--2-ymcov", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50427 CanonHostInfo::NEUTRAL, -1, ""},
428 // Labels starting with a RTL character cannot have "L" characters
429 // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
430 {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2",
[email protected]0318f922014-04-22 00:09:23431 "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21),
[email protected]18f00cd2013-09-29 10:52:50432 CanonHostInfo::BROKEN, -1, ""},
433 // Labels starting with a RTL character can end with BC=AN (Arabic number)
434 // Disallowed in IDNA 2003, but now allowed.
435 {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662",
[email protected]0318f922014-04-22 00:09:23436 "xn--mgbjq0r", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50437 CanonHostInfo::NEUTRAL, -1, ""},
438 // Labels starting with a RTL character cannot have "L" characters
439 // even if it ends with an BC=AN (Arabic number).
440 // Disallowed in both IDNA 2003/2008.
441 {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662",
[email protected]0318f922014-04-22 00:09:23442 "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26),
[email protected]18f00cd2013-09-29 10:52:50443 CanonHostInfo::BROKEN, -1, ""},
444 // Labels starting with a RTL character cannot mix BC=EN and BC=AN
445 {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662",
[email protected]0318f922014-04-22 00:09:23446 "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27),
[email protected]18f00cd2013-09-29 10:52:50447 CanonHostInfo::BROKEN, -1, ""},
448 // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
449 {"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com",
[email protected]0318f922014-04-22 00:09:23450 Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50451 // U+0080 is not allowed.
452 {"\xc2\x80.com", L"\x80.com", "%C2%80.com",
[email protected]0318f922014-04-22 00:09:23453 Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50454 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
[email protected]e7bba5f82013-04-10 20:10:52455 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
456 // UTF-8 (wide case). The output should be equivalent to the true wide
457 // character input above).
[email protected]18f00cd2013-09-29 10:52:50458 {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
459 L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
[email protected]0318f922014-04-22 00:09:23460 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52461 // Invalid escaped characters should fail and the percents should be
462 // escaped.
[email protected]0318f922014-04-22 00:09:23463 {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", Component(0, 10),
[email protected]18f00cd2013-09-29 10:52:50464 CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52465 // If we get an invalid character that has been escaped.
[email protected]0318f922014-04-22 00:09:23466 {"%25", L"%25", "%25", Component(0, 3),
[email protected]18f00cd2013-09-29 10:52:50467 CanonHostInfo::BROKEN, -1, ""},
[email protected]0318f922014-04-22 00:09:23468 {"hello%00", L"hello%00", "hello%00", Component(0, 8),
[email protected]18f00cd2013-09-29 10:52:50469 CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52470 // Escaped numbers should be treated like IP addresses if they are.
[email protected]18f00cd2013-09-29 10:52:50471 {"%30%78%63%30%2e%30%32%35%30.01", L"%30%78%63%30%2e%30%32%35%30.01",
[email protected]0318f922014-04-22 00:09:23472 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
[email protected]18f00cd2013-09-29 10:52:50473 "C0A80001"},
474 {"%30%78%63%30%2e%30%32%35%30.01%2e", L"%30%78%63%30%2e%30%32%35%30.01%2e",
[email protected]0318f922014-04-22 00:09:23475 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
[email protected]18f00cd2013-09-29 10:52:50476 "C0A80001"},
[email protected]e7bba5f82013-04-10 20:10:52477 // Invalid escaping should trigger the regular host error handling.
[email protected]0318f922014-04-22 00:09:23478 {"%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, ""},
[email protected]e7bba5f82013-04-10 20:10:52479 // Something that isn't exactly an IP should get treated as a host and
480 // spaces escaped.
[email protected]0318f922014-04-22 00:09:23481 {"192.168.0.1 hello", L"192.168.0.1 hello", "192.168.0.1%20hello", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52482 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
483 // These are "0Xc0.0250.01" in fullwidth.
[email protected]0318f922014-04-22 00:09:23484 {"\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"},
[email protected]e7bba5f82013-04-10 20:10:52485 // Broken IP addresses get marked as such.
[email protected]0318f922014-04-22 00:09:23486 {"192.168.0.257", L"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
487 {"[google.com]", L"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50488 // Cyrillic letter followed by '(' should return punycode for '(' escaped
489 // before punycode string was created. I.e.
490 // if '(' is escaped after punycode is created we would get xn--%28-8tb
491 // (incorrect).
[email protected]0318f922014-04-22 00:09:23492 {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50493 CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52494 // Address with all hexidecimal characters with leading number of 1<<32
495 // or greater and should return NEUTRAL rather than BROKEN if not all
496 // components are numbers.
[email protected]0318f922014-04-22 00:09:23497 {"12345678912345.de", L"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
498 {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
499 {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
500 {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
501 {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52502 };
503
504 // CanonicalizeHost() non-verbose.
505 std::string out_str;
506 for (size_t i = 0; i < arraysize(host_cases); i++) {
507 // Narrow version.
508 if (host_cases[i].input8) {
509 int host_len = static_cast<int>(strlen(host_cases[i].input8));
[email protected]0318f922014-04-22 00:09:23510 Component in_comp(0, host_len);
511 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52512
513 out_str.clear();
[email protected]0318f922014-04-22 00:09:23514 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52515
[email protected]0318f922014-04-22 00:09:23516 bool success = CanonicalizeHost(host_cases[i].input8, in_comp, &output,
517 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52518 output.Complete();
519
520 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
[email protected]18f00cd2013-09-29 10:52:50521 success) << "for input: " << host_cases[i].input8;
522 EXPECT_EQ(std::string(host_cases[i].expected), out_str) <<
523 "for input: " << host_cases[i].input8;
524 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) <<
525 "for input: " << host_cases[i].input8;
526 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) <<
527 "for input: " << host_cases[i].input8;
[email protected]e7bba5f82013-04-10 20:10:52528 }
529
530 // Wide version.
531 if (host_cases[i].input16) {
brettw1b8582f2016-11-03 20:37:17532 base::string16 input16(
533 test_utils::TruncateWStringToUTF16(host_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52534 int host_len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:23535 Component in_comp(0, host_len);
536 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52537
538 out_str.clear();
[email protected]0318f922014-04-22 00:09:23539 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52540
[email protected]0318f922014-04-22 00:09:23541 bool success = CanonicalizeHost(input16.c_str(), in_comp, &output,
542 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52543 output.Complete();
544
545 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
546 success);
547 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
548 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
549 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
550 }
551 }
552
553 // CanonicalizeHostVerbose()
554 for (size_t i = 0; i < arraysize(host_cases); i++) {
555 // Narrow version.
556 if (host_cases[i].input8) {
557 int host_len = static_cast<int>(strlen(host_cases[i].input8));
[email protected]0318f922014-04-22 00:09:23558 Component in_comp(0, host_len);
[email protected]e7bba5f82013-04-10 20:10:52559
560 out_str.clear();
[email protected]0318f922014-04-22 00:09:23561 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52562 CanonHostInfo host_info;
563
[email protected]0318f922014-04-22 00:09:23564 CanonicalizeHostVerbose(host_cases[i].input8, in_comp, &output,
565 &host_info);
[email protected]e7bba5f82013-04-10 20:10:52566 output.Complete();
567
568 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
569 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
570 EXPECT_EQ(host_cases[i].expected_component.begin,
571 host_info.out_host.begin);
572 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
573 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
574 BytesToHexString(host_info.address, host_info.AddressLength()));
575 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
576 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
577 host_info.num_ipv4_components);
578 }
579 }
580
581 // Wide version.
582 if (host_cases[i].input16) {
brettw1b8582f2016-11-03 20:37:17583 base::string16 input16(
584 test_utils::TruncateWStringToUTF16(host_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52585 int host_len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:23586 Component in_comp(0, host_len);
[email protected]e7bba5f82013-04-10 20:10:52587
588 out_str.clear();
[email protected]0318f922014-04-22 00:09:23589 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52590 CanonHostInfo host_info;
591
[email protected]0318f922014-04-22 00:09:23592 CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52593 output.Complete();
594
595 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
596 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
597 EXPECT_EQ(host_cases[i].expected_component.begin,
598 host_info.out_host.begin);
599 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
600 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
601 BytesToHexString(host_info.address, host_info.AddressLength()));
602 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
603 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
604 host_info.num_ipv4_components);
605 }
606 }
607 }
608}
609
610TEST(URLCanonTest, IPv4) {
611 IPAddressCase cases[] = {
612 // Empty is not an IP address.
[email protected]0318f922014-04-22 00:09:23613 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
614 {".", L".", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52615 // Regular IP addresses in different bases.
[email protected]0318f922014-04-22 00:09:23616 {"192.168.0.1", L"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
617 {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
618 {"0xC0.0Xa8.0x0.0x1", L"0xC0.0Xa8.0x0.0x1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
[email protected]e7bba5f82013-04-10 20:10:52619 // Non-IP addresses due to invalid characters.
[email protected]0318f922014-04-22 00:09:23620 {"192.168.9.com", L"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52621 // Invalid characters for the base should be rejected.
[email protected]0318f922014-04-22 00:09:23622 {"19a.168.0.1", L"19a.168.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
623 {"0308.0250.00.01", L"0308.0250.00.01", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
624 {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52625 // If there are not enough components, the last one should fill them out.
[email protected]0318f922014-04-22 00:09:23626 {"192", L"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
627 {"0xC0a80001", L"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
628 {"030052000001", L"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
629 {"000030052000001", L"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
630 {"192.168", L"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
631 {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
632 {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
633 {"192.168.1", L"192.168.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
[email protected]e7bba5f82013-04-10 20:10:52634 // Too many components means not an IP address.
[email protected]0318f922014-04-22 00:09:23635 {"192.168.0.0.1", L"192.168.0.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52636 // We allow a single trailing dot.
[email protected]0318f922014-04-22 00:09:23637 {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
638 {"192.168.0.1. hello", L"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
639 {"192.168.0.1..", L"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52640 // Two dots in a row means not an IP address.
[email protected]0318f922014-04-22 00:09:23641 {"192.168..1", L"192.168..1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52642 // Any numerical overflow should be marked as BROKEN.
[email protected]0318f922014-04-22 00:09:23643 {"0x100.0", L"0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
644 {"0x100.0.0", L"0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
645 {"0x100.0.0.0", L"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
646 {"0.0x100.0.0", L"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
647 {"0.0.0x100.0", L"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
648 {"0.0.0.0x100", L"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN, -1, ""},
649 {"0.0.0x10000", L"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
650 {"0.0x1000000", L"0.0x1000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
651 {"0x100000000", L"0x100000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52652 // Repeat the previous tests, minus 1, to verify boundaries.
[email protected]0318f922014-04-22 00:09:23653 {"0xFF.0", L"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
654 {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
655 {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
656 {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
657 {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
658 {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
659 {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
660 {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
661 {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
qyearsley2bc727d2015-08-14 20:17:15662 // Old trunctations tests. They're all "BROKEN" now.
[email protected]0318f922014-04-22 00:09:23663 {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN, -1, ""},
664 {"192.168.0.257", L"192.168.0.257", "", Component(), CanonHostInfo::BROKEN, -1, ""},
665 {"192.168.0xa20001", L"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
666 {"192.015052000001", L"192.015052000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
667 {"0X12C0a80001", L"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
668 {"276.1.2", L"276.1.2", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52669 // Spaces should be rejected.
[email protected]0318f922014-04-22 00:09:23670 {"192.168.0.1 hello", L"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52671 // Very large numbers.
[email protected]0318f922014-04-22 00:09:23672 {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
673 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52674 // A number has no length limit, but long numbers can still overflow.
[email protected]0318f922014-04-22 00:09:23675 {"00000000000000000001", L"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
676 {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52677 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
[email protected]0318f922014-04-22 00:09:23678 {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
679 {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52680 // Truncation of all zeros should still result in 0.
[email protected]0318f922014-04-22 00:09:23681 {"0.00.0x.0x0", L"0.00.0x.0x0", "0.0.0.0", Component(0, 7), CanonHostInfo::IPV4, 4, "00000000"},
[email protected]e7bba5f82013-04-10 20:10:52682 };
683
684 for (size_t i = 0; i < arraysize(cases); i++) {
685 // 8-bit version.
[email protected]0318f922014-04-22 00:09:23686 Component component(0, static_cast<int>(strlen(cases[i].input8)));
[email protected]e7bba5f82013-04-10 20:10:52687
688 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23689 StdStringCanonOutput output1(&out_str1);
690 CanonHostInfo host_info;
691 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52692 output1.Complete();
693
694 EXPECT_EQ(cases[i].expected_family, host_info.family);
695 EXPECT_EQ(std::string(cases[i].expected_address_hex),
696 BytesToHexString(host_info.address, host_info.AddressLength()));
697 if (host_info.family == CanonHostInfo::IPV4) {
698 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
699 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
700 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
701 EXPECT_EQ(cases[i].expected_num_ipv4_components,
702 host_info.num_ipv4_components);
703 }
704
705 // 16-bit version.
brettw1b8582f2016-11-03 20:37:17706 base::string16 input16(
707 test_utils::TruncateWStringToUTF16(cases[i].input16));
[email protected]0318f922014-04-22 00:09:23708 component = Component(0, static_cast<int>(input16.length()));
[email protected]e7bba5f82013-04-10 20:10:52709
710 std::string out_str2;
[email protected]0318f922014-04-22 00:09:23711 StdStringCanonOutput output2(&out_str2);
712 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52713 output2.Complete();
714
715 EXPECT_EQ(cases[i].expected_family, host_info.family);
716 EXPECT_EQ(std::string(cases[i].expected_address_hex),
717 BytesToHexString(host_info.address, host_info.AddressLength()));
718 if (host_info.family == CanonHostInfo::IPV4) {
719 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
720 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
721 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
722 EXPECT_EQ(cases[i].expected_num_ipv4_components,
723 host_info.num_ipv4_components);
724 }
725 }
726}
727
728TEST(URLCanonTest, IPv6) {
729 IPAddressCase cases[] = {
730 // Empty is not an IP address.
[email protected]0318f922014-04-22 00:09:23731 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52732 // Non-IPs with [:] characters are marked BROKEN.
[email protected]0318f922014-04-22 00:09:23733 {":", L":", "", Component(), CanonHostInfo::BROKEN, -1, ""},
734 {"[", L"[", "", Component(), CanonHostInfo::BROKEN, -1, ""},
735 {"[:", L"[:", "", Component(), CanonHostInfo::BROKEN, -1, ""},
736 {"]", L"]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
737 {":]", L":]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
738 {"[]", L"[]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
739 {"[:]", L"[:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52740 // Regular IP address is invalid without bounding '[' and ']'.
[email protected]0318f922014-04-22 00:09:23741 {"2001:db8::1", L"2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
742 {"[2001:db8::1", L"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
743 {"2001:db8::1]", L"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52744 // Regular IP addresses.
[email protected]0318f922014-04-22 00:09:23745 {"[::]", L"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
746 {"[::1]", L"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
747 {"[1::]", L"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
[email protected]e7bba5f82013-04-10 20:10:52748
749 // Leading zeros should be stripped.
[email protected]0318f922014-04-22 00:09:23750 {"[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"},
[email protected]e7bba5f82013-04-10 20:10:52751
752 // Upper case letters should be lowercased.
[email protected]0318f922014-04-22 00:09:23753 {"[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"},
[email protected]e7bba5f82013-04-10 20:10:52754
755 // The same address can be written with different contractions, but should
756 // get canonicalized to the same thing.
[email protected]0318f922014-04-22 00:09:23757 {"[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"},
758 {"[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"},
[email protected]e7bba5f82013-04-10 20:10:52759
760 // Addresses with embedded IPv4.
[email protected]0318f922014-04-22 00:09:23761 {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
762 {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
763 {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
764 {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
765 {"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52766
767 // IPv4 with last component missing.
[email protected]0318f922014-04-22 00:09:23768 {"[::ffff:192.1.2]", L"[::ffff:192.1.2]", "[::ffff:c001:2]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0010002"},
[email protected]e7bba5f82013-04-10 20:10:52769
770 // IPv4 using hex.
771 // TODO(eroman): Should this format be disallowed?
[email protected]0318f922014-04-22 00:09:23772 {"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
[email protected]e7bba5f82013-04-10 20:10:52773
774 // There may be zeros surrounding the "::" contraction.
[email protected]0318f922014-04-22 00:09:23775 {"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000008"},
[email protected]e7bba5f82013-04-10 20:10:52776
[email protected]0318f922014-04-22 00:09:23777 {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
[email protected]e7bba5f82013-04-10 20:10:52778
qyearsley2bc727d2015-08-14 20:17:15779 // Can only have one "::" contraction in an IPv6 string literal.
[email protected]0318f922014-04-22 00:09:23780 {"[2001::db8::1]", L"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15781 // No more than 2 consecutive ':'s.
[email protected]0318f922014-04-22 00:09:23782 {"[2001:db8:::1]", L"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
783 {"[:::]", L"[:::]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15784 // Non-IP addresses due to invalid characters.
[email protected]0318f922014-04-22 00:09:23785 {"[2001::.com]", L"[2001::.com]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15786 // If there are not enough components, the last one should fill them out.
[email protected]e7bba5f82013-04-10 20:10:52787 // ... omitted at this time ...
qyearsley2bc727d2015-08-14 20:17:15788 // Too many components means not an IP address. Similarly, with too few
789 // if using IPv4 compat or mapped addresses.
[email protected]0318f922014-04-22 00:09:23790 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
791 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
792 {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52793 // Too many bits (even though 8 comonents, the last one holds 32 bits).
[email protected]0318f922014-04-22 00:09:23794 {"[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, ""},
[email protected]e7bba5f82013-04-10 20:10:52795
796 // Too many bits specified -- the contraction would have to be zero-length
797 // to not exceed 128 bits.
[email protected]0318f922014-04-22 00:09:23798 {"[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, ""},
[email protected]e7bba5f82013-04-10 20:10:52799
800 // The contraction is for 16 bits of zero.
[email protected]0318f922014-04-22 00:09:23801 {"[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"},
[email protected]e7bba5f82013-04-10 20:10:52802
803 // Cannot have a trailing colon.
[email protected]0318f922014-04-22 00:09:23804 {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
805 {"[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, ""},
[email protected]e7bba5f82013-04-10 20:10:52806
807 // Cannot have negative numbers.
[email protected]0318f922014-04-22 00:09:23808 {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52809
810 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
811 // The scope_id should be included in the canonicalized URL, and is an
812 // unsigned decimal number.
813
814 // Invalid because no ID was given after the percent.
815
816 // Don't allow scope-id
[email protected]0318f922014-04-22 00:09:23817 {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
818 {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
819 {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
820 {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
821 {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52822
823 // Don't allow leading or trailing colons.
[email protected]0318f922014-04-22 00:09:23824 {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
825 {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
826 {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52827
828 // We allow a single trailing dot.
829 // ... omitted at this time ...
830 // Two dots in a row means not an IP address.
[email protected]0318f922014-04-22 00:09:23831 {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52832 // Any non-first components get truncated to one byte.
833 // ... omitted at this time ...
834 // Spaces should be rejected.
[email protected]0318f922014-04-22 00:09:23835 {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52836 };
837
838 for (size_t i = 0; i < arraysize(cases); i++) {
839 // 8-bit version.
[email protected]0318f922014-04-22 00:09:23840 Component component(0, static_cast<int>(strlen(cases[i].input8)));
[email protected]e7bba5f82013-04-10 20:10:52841
842 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23843 StdStringCanonOutput output1(&out_str1);
844 CanonHostInfo host_info;
845 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52846 output1.Complete();
847
848 EXPECT_EQ(cases[i].expected_family, host_info.family);
849 EXPECT_EQ(std::string(cases[i].expected_address_hex),
850 BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
851 if (host_info.family == CanonHostInfo::IPV6) {
852 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
853 EXPECT_EQ(cases[i].expected_component.begin,
854 host_info.out_host.begin);
855 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
856 }
857
858 // 16-bit version.
brettw1b8582f2016-11-03 20:37:17859 base::string16 input16(
860 test_utils::TruncateWStringToUTF16(cases[i].input16));
[email protected]0318f922014-04-22 00:09:23861 component = Component(0, static_cast<int>(input16.length()));
[email protected]e7bba5f82013-04-10 20:10:52862
863 std::string out_str2;
[email protected]0318f922014-04-22 00:09:23864 StdStringCanonOutput output2(&out_str2);
865 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52866 output2.Complete();
867
868 EXPECT_EQ(cases[i].expected_family, host_info.family);
869 EXPECT_EQ(std::string(cases[i].expected_address_hex),
870 BytesToHexString(host_info.address, host_info.AddressLength()));
871 if (host_info.family == CanonHostInfo::IPV6) {
872 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
873 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
874 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
875 }
876 }
877}
878
879TEST(URLCanonTest, IPEmpty) {
880 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23881 StdStringCanonOutput output1(&out_str1);
882 CanonHostInfo host_info;
[email protected]e7bba5f82013-04-10 20:10:52883
884 // This tests tests.
885 const char spec[] = "192.168.0.1";
[email protected]0318f922014-04-22 00:09:23886 CanonicalizeIPAddress(spec, Component(), &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52887 EXPECT_FALSE(host_info.IsIPAddress());
888
[email protected]0318f922014-04-22 00:09:23889 CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52890 EXPECT_FALSE(host_info.IsIPAddress());
891}
892
brettw5a36380ef2016-10-27 19:51:56893// Verifies that CanonicalizeHostSubstring produces the expected output and
894// does not "fix" IP addresses. Because this code is a subset of
895// CanonicalizeHost, the shared functionality is not tested.
896TEST(URLCanonTest, CanonicalizeHostSubstring) {
897 // Basic sanity check.
898 {
899 std::string out_str;
900 StdStringCanonOutput output(&out_str);
901 EXPECT_TRUE(CanonicalizeHostSubstring("M\xc3\x9cNCHEN.com",
902 Component(0, 12), &output));
903 output.Complete();
904 EXPECT_EQ("xn--mnchen-3ya.com", out_str);
905 }
906
907 // Failure case.
908 {
909 std::string out_str;
910 StdStringCanonOutput output(&out_str);
911 EXPECT_FALSE(CanonicalizeHostSubstring(
brettw1b8582f2016-11-03 20:37:17912 test_utils::TruncateWStringToUTF16(L"\xfdd0zyx.com").c_str(),
913 Component(0, 8), &output));
brettw5a36380ef2016-10-27 19:51:56914 output.Complete();
915 EXPECT_EQ("%EF%BF%BDzyx.com", out_str);
916 }
917
918 // Should return true for empty input strings.
919 {
920 std::string out_str;
921 StdStringCanonOutput output(&out_str);
922 EXPECT_TRUE(CanonicalizeHostSubstring("", Component(0, 0), &output));
923 output.Complete();
924 EXPECT_EQ(std::string(), out_str);
925 }
926
927 // Numbers that look like IP addresses should not be changed.
928 {
929 std::string out_str;
930 StdStringCanonOutput output(&out_str);
931 EXPECT_TRUE(
932 CanonicalizeHostSubstring("01.02.03.04", Component(0, 11), &output));
933 output.Complete();
934 EXPECT_EQ("01.02.03.04", out_str);
935 }
936}
937
[email protected]e7bba5f82013-04-10 20:10:52938TEST(URLCanonTest, UserInfo) {
939 // Note that the canonicalizer should escape and treat empty components as
940 // not being there.
941
942 // We actually parse a full input URL so we can get the initial components.
943 struct UserComponentCase {
944 const char* input;
945 const char* expected;
[email protected]0318f922014-04-22 00:09:23946 Component expected_username;
947 Component expected_password;
[email protected]e7bba5f82013-04-10 20:10:52948 bool expected_success;
949 } user_info_cases[] = {
[email protected]0318f922014-04-22 00:09:23950 {"http://user:[email protected]/", "user:pass@", Component(0, 4), Component(5, 4), true},
951 {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
952 {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
953 {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
954 {"http://:[email protected]/", ":foo@", Component(0, 0), Component(1, 3), true},
955 {"http://^ :$\[email protected]/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
956 {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
957 {"http://%2540:[email protected]/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
[email protected]e7bba5f82013-04-10 20:10:52958
qyearsley2bc727d2015-08-14 20:17:15959 // IE7 compatibility: old versions allowed backslashes in usernames, but
[email protected]e7bba5f82013-04-10 20:10:52960 // IE7 does not. We disallow it as well.
[email protected]0318f922014-04-22 00:09:23961 {"ftp://me\\mydomain:[email protected]/", "", Component(0, -1), Component(0, -1), true},
[email protected]e7bba5f82013-04-10 20:10:52962 };
963
viettrungluu4b6915862014-10-16 03:42:49964 for (size_t i = 0; i < arraysize(user_info_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52965 int url_len = static_cast<int>(strlen(user_info_cases[i].input));
[email protected]0318f922014-04-22 00:09:23966 Parsed parsed;
967 ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
968 Component out_user, out_pass;
[email protected]e7bba5f82013-04-10 20:10:52969 std::string out_str;
[email protected]0318f922014-04-22 00:09:23970 StdStringCanonOutput output1(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52971
[email protected]0318f922014-04-22 00:09:23972 bool success = CanonicalizeUserInfo(user_info_cases[i].input,
973 parsed.username,
974 user_info_cases[i].input,
975 parsed.password,
976 &output1,
977 &out_user,
978 &out_pass);
[email protected]e7bba5f82013-04-10 20:10:52979 output1.Complete();
980
981 EXPECT_EQ(user_info_cases[i].expected_success, success);
982 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
983 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
984 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
985 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
986 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
987
988 // Now try the wide version
989 out_str.clear();
[email protected]0318f922014-04-22 00:09:23990 StdStringCanonOutput output2(&out_str);
brettw1b8582f2016-11-03 20:37:17991 base::string16 wide_input(base::UTF8ToUTF16(user_info_cases[i].input));
[email protected]0318f922014-04-22 00:09:23992 success = CanonicalizeUserInfo(wide_input.c_str(),
993 parsed.username,
994 wide_input.c_str(),
995 parsed.password,
996 &output2,
997 &out_user,
998 &out_pass);
[email protected]e7bba5f82013-04-10 20:10:52999 output2.Complete();
1000
1001 EXPECT_EQ(user_info_cases[i].expected_success, success);
1002 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
1003 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
1004 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
1005 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
1006 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
1007 }
1008}
1009
1010TEST(URLCanonTest, Port) {
1011 // We only need to test that the number gets properly put into the output
1012 // buffer. The parser unit tests will test scanning the number correctly.
1013 //
1014 // Note that the CanonicalizePort will always prepend a colon to the output
qyearsley2bc727d2015-08-14 20:17:151015 // to separate it from the colon that it assumes precedes it.
[email protected]e7bba5f82013-04-10 20:10:521016 struct PortCase {
1017 const char* input;
1018 int default_port;
1019 const char* expected;
[email protected]0318f922014-04-22 00:09:231020 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:521021 bool expected_success;
1022 } port_cases[] = {
1023 // Invalid input should be copied w/ failure.
[email protected]0318f922014-04-22 00:09:231024 {"as df", 80, ":as%20df", Component(1, 7), false},
1025 {"-2", 80, ":-2", Component(1, 2), false},
[email protected]e7bba5f82013-04-10 20:10:521026 // Default port should be omitted.
[email protected]0318f922014-04-22 00:09:231027 {"80", 80, "", Component(0, -1), true},
1028 {"8080", 80, ":8080", Component(1, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521029 // PORT_UNSPECIFIED should mean always keep the port.
[email protected]0318f922014-04-22 00:09:231030 {"80", PORT_UNSPECIFIED, ":80", Component(1, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521031 };
1032
viettrungluu4b6915862014-10-16 03:42:491033 for (size_t i = 0; i < arraysize(port_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521034 int url_len = static_cast<int>(strlen(port_cases[i].input));
[email protected]0318f922014-04-22 00:09:231035 Component in_comp(0, url_len);
1036 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521037 std::string out_str;
[email protected]0318f922014-04-22 00:09:231038 StdStringCanonOutput output1(&out_str);
1039 bool success = CanonicalizePort(port_cases[i].input,
1040 in_comp,
1041 port_cases[i].default_port,
1042 &output1,
1043 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521044 output1.Complete();
1045
1046 EXPECT_EQ(port_cases[i].expected_success, success);
1047 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
1048 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
1049 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
1050
1051 // Now try the wide version
1052 out_str.clear();
[email protected]0318f922014-04-22 00:09:231053 StdStringCanonOutput output2(&out_str);
brettw1b8582f2016-11-03 20:37:171054 base::string16 wide_input(base::UTF8ToUTF16(port_cases[i].input));
[email protected]0318f922014-04-22 00:09:231055 success = CanonicalizePort(wide_input.c_str(),
1056 in_comp,
1057 port_cases[i].default_port,
1058 &output2,
1059 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521060 output2.Complete();
1061
1062 EXPECT_EQ(port_cases[i].expected_success, success);
1063 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
1064 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
1065 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
1066 }
1067}
1068
1069TEST(URLCanonTest, Path) {
1070 DualComponentCase path_cases[] = {
1071 // ----- path collapsing tests -----
[email protected]0318f922014-04-22 00:09:231072 {"/././foo", L"/././foo", "/foo", Component(0, 4), true},
1073 {"/./.foo", L"/./.foo", "/.foo", Component(0, 5), true},
1074 {"/foo/.", L"/foo/.", "/foo/", Component(0, 5), true},
1075 {"/foo/./", L"/foo/./", "/foo/", Component(0, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521076 // double dots followed by a slash or the end of the string count
[email protected]0318f922014-04-22 00:09:231077 {"/foo/bar/..", L"/foo/bar/..", "/foo/", Component(0, 5), true},
1078 {"/foo/bar/../", L"/foo/bar/../", "/foo/", Component(0, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521079 // don't count double dots when they aren't followed by a slash
[email protected]0318f922014-04-22 00:09:231080 {"/foo/..bar", L"/foo/..bar", "/foo/..bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521081 // some in the middle
[email protected]0318f922014-04-22 00:09:231082 {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", Component(0, 8), true},
1083 {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", Component(0, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521084 // we should not be able to go above the root
[email protected]0318f922014-04-22 00:09:231085 {"/foo/../../..", L"/foo/../../..", "/", Component(0, 1), true},
1086 {"/foo/../../../ton", L"/foo/../../../ton", "/ton", Component(0, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521087 // escaped dots should be unescaped and treated the same as dots
[email protected]0318f922014-04-22 00:09:231088 {"/foo/%2e", L"/foo/%2e", "/foo/", Component(0, 5), true},
1089 {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", Component(0, 8), true},
1090 {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521091 // Multiple slashes in a row should be preserved and treated like empty
1092 // directory names.
[email protected]0318f922014-04-22 00:09:231093 {"////../..", L"////../..", "//", Component(0, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521094
1095 // ----- escaping tests -----
[email protected]0318f922014-04-22 00:09:231096 {"/foo", L"/foo", "/foo", Component(0, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521097 // Valid escape sequence
[email protected]0318f922014-04-22 00:09:231098 {"/%20foo", L"/%20foo", "/%20foo", Component(0, 7), true},
[email protected]e7bba5f82013-04-10 20:10:521099 // Invalid escape sequence we should pass through unchanged.
[email protected]0318f922014-04-22 00:09:231100 {"/foo%", L"/foo%", "/foo%", Component(0, 5), true},
1101 {"/foo%2", L"/foo%2", "/foo%2", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521102 // Invalid escape sequence: bad characters should be treated the same as
1103 // the sourrounding text, not as escaped (in this case, UTF-8).
[email protected]0318f922014-04-22 00:09:231104 {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", Component(0, 10), true},
1105 {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", Component(0, 16), true},
1106 {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22), true},
[email protected]e7bba5f82013-04-10 20:10:521107 // Regular characters that are escaped should be unescaped
[email protected]0318f922014-04-22 00:09:231108 {"/foo%41%7a", L"/foo%41%7a", "/fooAz", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521109 // Funny characters that are unescaped should be escaped
[email protected]0318f922014-04-22 00:09:231110 {"/foo\x09\x91%91", NULL, "/foo%09%91%91", Component(0, 13), true},
1111 {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true},
[email protected]e7bba5f82013-04-10 20:10:521112 // Invalid characters that are escaped should cause a failure.
[email protected]0318f922014-04-22 00:09:231113 {"/foo%00%51", L"/foo%00%51", "/foo%00Q", Component(0, 8), false},
[email protected]e7bba5f82013-04-10 20:10:521114 // Some characters should be passed through unchanged regardless of esc.
[email protected]0318f922014-04-22 00:09:231115 {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13), true},
[email protected]e7bba5f82013-04-10 20:10:521116 // Characters that are properly escaped should not have the case changed
1117 // of hex letters.
[email protected]0318f922014-04-22 00:09:231118 {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13), true},
[email protected]e7bba5f82013-04-10 20:10:521119 // Funny characters that are unescaped should be escaped
[email protected]0318f922014-04-22 00:09:231120 {"/foo\tbar", L"/foo\tbar", "/foo%09bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521121 // Backslashes should get converted to forward slashes
[email protected]0318f922014-04-22 00:09:231122 {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", Component(0, 8), true},
[email protected]e7bba5f82013-04-10 20:10:521123 // Hashes found in paths (possibly only when the caller explicitly sets
1124 // the path on an already-parsed URL) should be escaped.
[email protected]0318f922014-04-22 00:09:231125 {"/foo#bar", L"/foo#bar", "/foo%23bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521126 // %7f should be allowed and %3D should not be unescaped (these were wrong
1127 // in a previous version).
[email protected]0318f922014-04-22 00:09:231128 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true},
[email protected]e7bba5f82013-04-10 20:10:521129 // @ should be passed through unchanged (escaped or unescaped).
[email protected]0318f922014-04-22 00:09:231130 {"/@asdf%40", L"/@asdf%40", "/@asdf%40", Component(0, 9), true},
pkasting69820362015-09-22 01:54:051131 // Nested escape sequences should result in escaping the leading '%' if
1132 // unescaping would result in a new escape sequence.
1133 {"/%A%42", L"/%A%42", "/%25AB", Component(0, 6), true},
1134 {"/%%41B", L"/%%41B", "/%25AB", Component(0, 6), true},
1135 {"/%%41%42", L"/%%41%42", "/%25AB", Component(0, 6), true},
1136 // Make sure truncated "nested" escapes don't result in reading off the
1137 // string end.
1138 {"/%%41", L"/%%41", "/%A", Component(0, 3), true},
1139 // Don't unescape the leading '%' if unescaping doesn't result in a valid
1140 // new escape sequence.
1141 {"/%%470", L"/%%470", "/%G0", Component(0, 4), true},
1142 {"/%%2D%41", L"/%%2D%41", "/%-A", Component(0, 4), true},
1143 // Don't erroneously downcast a UTF-16 charater in a way that makes it
1144 // look like part of an escape sequence.
1145 {NULL, L"/%%41\x0130", "/%A%C4%B0", Component(0, 9), true},
[email protected]e7bba5f82013-04-10 20:10:521146
1147 // ----- encoding tests -----
1148 // Basic conversions
[email protected]0318f922014-04-22 00:09:231149 {"/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"/\x4f60\x597d\x4f60\x597d", "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD", Component(0, 37), true},
[email protected]e7bba5f82013-04-10 20:10:521150 // Invalid unicode characters should fail. We only do validation on
1151 // UTF-16 input, so this doesn't happen on 8-bit.
[email protected]0318f922014-04-22 00:09:231152 {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", Component(0, 13), true},
1153 {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false},
[email protected]e7bba5f82013-04-10 20:10:521154 };
1155
1156 for (size_t i = 0; i < arraysize(path_cases); i++) {
1157 if (path_cases[i].input8) {
1158 int len = static_cast<int>(strlen(path_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231159 Component in_comp(0, len);
1160 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521161 std::string out_str;
[email protected]0318f922014-04-22 00:09:231162 StdStringCanonOutput output(&out_str);
1163 bool success =
1164 CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521165 output.Complete();
1166
1167 EXPECT_EQ(path_cases[i].expected_success, success);
1168 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1169 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1170 EXPECT_EQ(path_cases[i].expected, out_str);
1171 }
1172
1173 if (path_cases[i].input16) {
brettw1b8582f2016-11-03 20:37:171174 base::string16 input16(
1175 test_utils::TruncateWStringToUTF16(path_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521176 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231177 Component in_comp(0, len);
1178 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521179 std::string out_str;
[email protected]0318f922014-04-22 00:09:231180 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:521181
[email protected]0318f922014-04-22 00:09:231182 bool success =
1183 CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521184 output.Complete();
1185
1186 EXPECT_EQ(path_cases[i].expected_success, success);
1187 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1188 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1189 EXPECT_EQ(path_cases[i].expected, out_str);
1190 }
1191 }
1192
1193 // Manual test: embedded NULLs should be escaped and the URL should be marked
1194 // as invalid.
1195 const char path_with_null[] = "/ab\0c";
[email protected]0318f922014-04-22 00:09:231196 Component in_comp(0, 5);
1197 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521198
1199 std::string out_str;
[email protected]0318f922014-04-22 00:09:231200 StdStringCanonOutput output(&out_str);
1201 bool success = CanonicalizePath(path_with_null, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521202 output.Complete();
1203 EXPECT_FALSE(success);
1204 EXPECT_EQ("/ab%00c", out_str);
1205}
1206
1207TEST(URLCanonTest, Query) {
1208 struct QueryCase {
1209 const char* input8;
1210 const wchar_t* input16;
[email protected]e7bba5f82013-04-10 20:10:521211 const char* expected;
1212 } query_cases[] = {
[email protected]847aaab82014-05-07 14:05:461213 // Regular ASCII case.
1214 {"foo=bar", L"foo=bar", "?foo=bar"},
[email protected]e7bba5f82013-04-10 20:10:521215 // Allow question marks in the query without escaping
[email protected]847aaab82014-05-07 14:05:461216 {"as?df", L"as?df", "?as?df"},
[email protected]e7bba5f82013-04-10 20:10:521217 // Always escape '#' since it would mark the ref.
[email protected]847aaab82014-05-07 14:05:461218 {"as#df", L"as#df", "?as%23df"},
[email protected]e7bba5f82013-04-10 20:10:521219 // Escape some questionable 8-bit characters, but never unescape.
[email protected]847aaab82014-05-07 14:05:461220 {"\x02hello\x7f bye", L"\x02hello\x7f bye", "?%02hello%7F%20bye"},
1221 {"%40%41123", L"%40%41123", "?%40%41123"},
[email protected]e7bba5f82013-04-10 20:10:521222 // Chinese input/output
[email protected]847aaab82014-05-07 14:05:461223 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "?q=%E4%BD%A0%E5%A5%BD"},
[email protected]e7bba5f82013-04-10 20:10:521224 // Invalid UTF-8/16 input should be replaced with invalid characters.
[email protected]847aaab82014-05-07 14:05:461225 {"q=\xed\xed", L"q=\xd800\xd800", "?q=%EF%BF%BD%EF%BF%BD"},
[email protected]e7bba5f82013-04-10 20:10:521226 // Don't allow < or > because sometimes they are used for XSS if the
1227 // URL is echoed in content. Firefox does this, IE doesn't.
[email protected]847aaab82014-05-07 14:05:461228 {"q=<asdf>", L"q=<asdf>", "?q=%3Casdf%3E"},
[email protected]e7bba5f82013-04-10 20:10:521229 // Escape double quotemarks in the query.
[email protected]847aaab82014-05-07 14:05:461230 {"q=\"asdf\"", L"q=\"asdf\"", "?q=%22asdf%22"},
[email protected]e7bba5f82013-04-10 20:10:521231 };
1232
viettrungluu4b6915862014-10-16 03:42:491233 for (size_t i = 0; i < arraysize(query_cases); i++) {
[email protected]0318f922014-04-22 00:09:231234 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521235
[email protected]e7bba5f82013-04-10 20:10:521236 if (query_cases[i].input8) {
1237 int len = static_cast<int>(strlen(query_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231238 Component in_comp(0, len);
[email protected]e7bba5f82013-04-10 20:10:521239 std::string out_str;
1240
[email protected]0318f922014-04-22 00:09:231241 StdStringCanonOutput output(&out_str);
[email protected]847aaab82014-05-07 14:05:461242 CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output,
[email protected]0318f922014-04-22 00:09:231243 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521244 output.Complete();
1245
1246 EXPECT_EQ(query_cases[i].expected, out_str);
1247 }
1248
1249 if (query_cases[i].input16) {
brettw1b8582f2016-11-03 20:37:171250 base::string16 input16(
1251 test_utils::TruncateWStringToUTF16(query_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521252 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231253 Component in_comp(0, len);
[email protected]e7bba5f82013-04-10 20:10:521254 std::string out_str;
1255
[email protected]0318f922014-04-22 00:09:231256 StdStringCanonOutput output(&out_str);
[email protected]847aaab82014-05-07 14:05:461257 CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521258 output.Complete();
1259
1260 EXPECT_EQ(query_cases[i].expected, out_str);
1261 }
1262 }
1263
1264 // Extra test for input with embedded NULL;
1265 std::string out_str;
[email protected]0318f922014-04-22 00:09:231266 StdStringCanonOutput output(&out_str);
1267 Component out_comp;
1268 CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521269 output.Complete();
1270 EXPECT_EQ("?a%20%00z%01", out_str);
1271}
1272
1273TEST(URLCanonTest, Ref) {
1274 // Refs are trivial, it just checks the encoding.
1275 DualComponentCase ref_cases[] = {
1276 // Regular one, we shouldn't escape spaces, et al.
[email protected]0318f922014-04-22 00:09:231277 {"hello, world", L"hello, world", "#hello, world", Component(1, 12), true},
[email protected]e7bba5f82013-04-10 20:10:521278 // UTF-8/wide input should be preserved
[email protected]0318f922014-04-22 00:09:231279 {"\xc2\xa9", L"\xa9", "#\xc2\xa9", Component(1, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521280 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
[email protected]0318f922014-04-22 00:09:231281 {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", Component(1, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521282 // Escaping should be preserved unchanged, even invalid ones
[email protected]0318f922014-04-22 00:09:231283 {"%41%a", L"%41%a", "#%41%a", Component(1, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521284 // Invalid UTF-8/16 input should be flagged and the input made valid
[email protected]0318f922014-04-22 00:09:231285 {"\xc2", NULL, "#\xef\xbf\xbd", Component(1, 3), true},
1286 {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", Component(1, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521287 // Test a Unicode invalid character.
[email protected]0318f922014-04-22 00:09:231288 {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", Component(1, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521289 // Refs can have # signs and we should preserve them.
[email protected]0318f922014-04-22 00:09:231290 {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", Component(1, 9), true},
1291 {"#asdf", L"#asdf", "##asdf", Component(1, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521292 };
1293
1294 for (size_t i = 0; i < arraysize(ref_cases); i++) {
1295 // 8-bit input
1296 if (ref_cases[i].input8) {
1297 int len = static_cast<int>(strlen(ref_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231298 Component in_comp(0, len);
1299 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521300
1301 std::string out_str;
[email protected]0318f922014-04-22 00:09:231302 StdStringCanonOutput output(&out_str);
1303 CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521304 output.Complete();
1305
1306 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1307 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1308 EXPECT_EQ(ref_cases[i].expected, out_str);
1309 }
1310
1311 // 16-bit input
1312 if (ref_cases[i].input16) {
brettw1b8582f2016-11-03 20:37:171313 base::string16 input16(
1314 test_utils::TruncateWStringToUTF16(ref_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521315 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231316 Component in_comp(0, len);
1317 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521318
1319 std::string out_str;
[email protected]0318f922014-04-22 00:09:231320 StdStringCanonOutput output(&out_str);
1321 CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521322 output.Complete();
1323
1324 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1325 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1326 EXPECT_EQ(ref_cases[i].expected, out_str);
1327 }
1328 }
1329
1330 // Try one with an embedded NULL. It should be stripped.
1331 const char null_input[5] = "ab\x00z";
[email protected]0318f922014-04-22 00:09:231332 Component null_input_component(0, 4);
1333 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521334
1335 std::string out_str;
[email protected]0318f922014-04-22 00:09:231336 StdStringCanonOutput output(&out_str);
1337 CanonicalizeRef(null_input, null_input_component, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521338 output.Complete();
1339
1340 EXPECT_EQ(1, out_comp.begin);
1341 EXPECT_EQ(3, out_comp.len);
1342 EXPECT_EQ("#abz", out_str);
1343}
1344
1345TEST(URLCanonTest, CanonicalizeStandardURL) {
1346 // The individual component canonicalize tests should have caught the cases
1347 // for each of those components. Here, we just need to test that the various
1348 // parts are included or excluded properly, and have the correct separators.
1349 struct URLCase {
1350 const char* input;
1351 const char* expected;
1352 bool expected_success;
1353 } cases[] = {
1354 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1355 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1356 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1357 {"http:////////user:@google.com:99?foo", "http://[email protected]:99/?foo", true},
brettw46f9b832016-10-05 19:22:481358 {"www.google.com", ":www.google.com/", false},
[email protected]e7bba5f82013-04-10 20:10:521359 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1360 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1361 {"http://user:pass@/", "http://user:pass@/", false},
1362 {"http://%25DOMAIN:[email protected]/", "http://%25DOMAIN:[email protected]/", true},
1363
1364 // Backslashes should get converted to forward slashes.
1365 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1366
1367 // Busted refs shouldn't make the whole thing fail.
1368 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1369
1370 // Basic port tests.
1371 {"http://foo:80/", "http://foo/", true},
1372 {"http://foo:81/", "http://foo:81/", true},
1373 {"httpa://foo:80/", "httpa://foo:80/", true},
1374 {"http://foo:-80/", "http://foo:-80/", false},
1375
1376 {"https://foo:443/", "https://foo/", true},
1377 {"https://foo:80/", "https://foo:80/", true},
1378 {"ftp://foo:21/", "ftp://foo/", true},
1379 {"ftp://foo:80/", "ftp://foo:80/", true},
1380 {"gopher://foo:70/", "gopher://foo/", true},
1381 {"gopher://foo:443/", "gopher://foo:443/", true},
1382 {"ws://foo:80/", "ws://foo/", true},
1383 {"ws://foo:81/", "ws://foo:81/", true},
1384 {"ws://foo:443/", "ws://foo:443/", true},
1385 {"ws://foo:815/", "ws://foo:815/", true},
1386 {"wss://foo:80/", "wss://foo:80/", true},
1387 {"wss://foo:81/", "wss://foo:81/", true},
1388 {"wss://foo:443/", "wss://foo/", true},
1389 {"wss://foo:815/", "wss://foo:815/", true},
brettw1141951b2015-11-26 00:29:351390
1391 // This particular code path ends up "backing up" to replace an invalid
1392 // host ICU generated with an escaped version. Test that in the context
1393 // of a full URL to make sure the backing up doesn't mess up the non-host
1394 // parts of the URL. "EF B9 AA" is U+FE6A which is a type of percent that
1395 // ICU will convert to an ASCII one, generating "%81".
1396 {"ws:)W\x1eW\xef\xb9\xaa""81:80/", "ws://%29w%1ew%81/", false},
[email protected]e7bba5f82013-04-10 20:10:521397 };
1398
viettrungluu4b6915862014-10-16 03:42:491399 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521400 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231401 Parsed parsed;
1402 ParseStandardURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521403
[email protected]0318f922014-04-22 00:09:231404 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521405 std::string out_str;
[email protected]0318f922014-04-22 00:09:231406 StdStringCanonOutput output(&out_str);
1407 bool success = CanonicalizeStandardURL(
[email protected]e7bba5f82013-04-10 20:10:521408 cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
1409 output.Complete();
1410
1411 EXPECT_EQ(cases[i].expected_success, success);
1412 EXPECT_EQ(cases[i].expected, out_str);
1413 }
1414}
1415
1416// The codepath here is the same as for regular canonicalization, so we just
1417// need to test that things are replaced or not correctly.
1418TEST(URLCanonTest, ReplaceStandardURL) {
1419 ReplaceCase replace_cases[] = {
1420 // Common case of truncating the path.
1421 {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
1422 // Replace everything
1423 {"http://a:[email protected]:22/foo;bar?baz@cat", "https", "me", "pw", "host.com", "99", "/path", "query", "ref", "https://me:[email protected]:99/path?query#ref"},
1424 // Replace nothing
1425 {"http://a:[email protected]:22/foo?baz@cat", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "http://a:[email protected]:22/foo?baz@cat"},
qyearsley2bc727d2015-08-14 20:17:151426 // Replace scheme with filesystem. The result is garbage, but you asked
[email protected]e7bba5f82013-04-10 20:10:521427 // for it.
1428 {"http://a:[email protected]:22/foo?baz@cat", "filesystem", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem://a:[email protected]:22/foo?baz@cat"},
1429 };
1430
1431 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1432 const ReplaceCase& cur = replace_cases[i];
1433 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231434 Parsed parsed;
1435 ParseStandardURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521436
[email protected]0318f922014-04-22 00:09:231437 Replacements<char> r;
1438 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521439
1440 // Note that for the scheme we pass in a different clear function since
1441 // there is no function to clear the scheme.
1442 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1443 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1444 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1445 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1446 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1447 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1448 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1449 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1450
1451 std::string out_str;
[email protected]0318f922014-04-22 00:09:231452 StdStringCanonOutput output(&out_str);
1453 Parsed out_parsed;
1454 ReplaceStandardURL(replace_cases[i].base, parsed, r, NULL, &output,
1455 &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521456 output.Complete();
1457
1458 EXPECT_EQ(replace_cases[i].expected, out_str);
1459 }
1460
1461 // The path pointer should be ignored if the address is invalid.
1462 {
1463 const char src[] = "http://www.google.com/here_is_the_path";
1464 int src_len = static_cast<int>(strlen(src));
1465
[email protected]0318f922014-04-22 00:09:231466 Parsed parsed;
1467 ParseStandardURL(src, src_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521468
1469 // Replace the path to 0 length string. By using 1 as the string address,
1470 // the test should get an access violation if it tries to dereference it.
[email protected]0318f922014-04-22 00:09:231471 Replacements<char> r;
1472 r.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0));
[email protected]e7bba5f82013-04-10 20:10:521473 std::string out_str1;
[email protected]0318f922014-04-22 00:09:231474 StdStringCanonOutput output1(&out_str1);
1475 Parsed new_parsed;
1476 ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
[email protected]e7bba5f82013-04-10 20:10:521477 output1.Complete();
1478 EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
1479
1480 // Same with an "invalid" path.
[email protected]0318f922014-04-22 00:09:231481 r.SetPath(reinterpret_cast<char*>(0x00000001), Component());
[email protected]e7bba5f82013-04-10 20:10:521482 std::string out_str2;
[email protected]0318f922014-04-22 00:09:231483 StdStringCanonOutput output2(&out_str2);
1484 ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
[email protected]e7bba5f82013-04-10 20:10:521485 output2.Complete();
1486 EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
1487 }
1488}
1489
1490TEST(URLCanonTest, ReplaceFileURL) {
1491 ReplaceCase replace_cases[] = {
1492 // Replace everything
1493 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1494 // Replace nothing
1495 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1496 // Clear non-path components (common)
1497 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
1498 // Replace path with something that doesn't begin with a slash and make
1499 // sure it gets added properly.
1500 {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1501 {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1502 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
1503 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
1504 {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1505 // Replace scheme -- shouldn't do anything.
1506 {"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1507 };
1508
1509 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1510 const ReplaceCase& cur = replace_cases[i];
1511 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231512 Parsed parsed;
1513 ParseFileURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521514
[email protected]0318f922014-04-22 00:09:231515 Replacements<char> r;
1516 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521517 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1518 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1519 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1520 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1521 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1522 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1523 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1524 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1525
1526 std::string out_str;
[email protected]0318f922014-04-22 00:09:231527 StdStringCanonOutput output(&out_str);
1528 Parsed out_parsed;
1529 ReplaceFileURL(cur.base, parsed, r, NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521530 output.Complete();
1531
1532 EXPECT_EQ(replace_cases[i].expected, out_str);
1533 }
1534}
1535
1536TEST(URLCanonTest, ReplaceFileSystemURL) {
1537 ReplaceCase replace_cases[] = {
1538 // Replace everything in the outer URL.
1539 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1540 // Replace nothing
1541 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"},
1542 // Clear non-path components (common)
1543 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"},
1544 // Replace path with something that doesn't begin with a slash and make
1545 // sure it gets added properly.
1546 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"},
1547 // Replace scheme -- shouldn't do anything.
1548 {"filesystem:http://u:[email protected]/t/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:[email protected]/t/gaba?query#ref"},
1549 // Replace username -- shouldn't do anything.
1550 {"filesystem:http://u:[email protected]/t/gaba?query#ref", NULL, "u2", NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:[email protected]/t/gaba?query#ref"},
1551 // Replace password -- shouldn't do anything.
1552 {"filesystem:http://u:[email protected]/t/gaba?query#ref", NULL, NULL, "pw2", NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:[email protected]/t/gaba?query#ref"},
1553 // Replace host -- shouldn't do anything.
1554 {"filesystem:http://u:[email protected]/t/gaba?query#ref", NULL, NULL, NULL, "foo.com", NULL, NULL, NULL, NULL, "filesystem:http://u:[email protected]/t/gaba?query#ref"},
1555 // Replace port -- shouldn't do anything.
1556 {"filesystem:http://u:[email protected]:40/t/gaba?query#ref", NULL, NULL, NULL, NULL, "41", NULL, NULL, NULL, "filesystem:http://u:[email protected]:40/t/gaba?query#ref"},
1557 };
1558
1559 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1560 const ReplaceCase& cur = replace_cases[i];
1561 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231562 Parsed parsed;
1563 ParseFileSystemURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521564
[email protected]0318f922014-04-22 00:09:231565 Replacements<char> r;
1566 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521567 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1568 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1569 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1570 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1571 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1572 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1573 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1574 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1575
1576 std::string out_str;
[email protected]0318f922014-04-22 00:09:231577 StdStringCanonOutput output(&out_str);
1578 Parsed out_parsed;
1579 ReplaceFileSystemURL(cur.base, parsed, r, NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521580 output.Complete();
1581
1582 EXPECT_EQ(replace_cases[i].expected, out_str);
1583 }
1584}
1585
1586TEST(URLCanonTest, ReplacePathURL) {
1587 ReplaceCase replace_cases[] = {
1588 // Replace everything
1589 {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
1590 // Replace nothing
1591 {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
1592 // Replace one or the other
1593 {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
1594 {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
1595 {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
1596 };
1597
1598 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1599 const ReplaceCase& cur = replace_cases[i];
1600 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231601 Parsed parsed;
1602 ParsePathURL(cur.base, base_len, false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521603
[email protected]0318f922014-04-22 00:09:231604 Replacements<char> r;
1605 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521606 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1607 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1608 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1609 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1610 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1611 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1612 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1613 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1614
1615 std::string out_str;
[email protected]0318f922014-04-22 00:09:231616 StdStringCanonOutput output(&out_str);
1617 Parsed out_parsed;
1618 ReplacePathURL(cur.base, parsed, r, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521619 output.Complete();
1620
1621 EXPECT_EQ(replace_cases[i].expected, out_str);
1622 }
1623}
1624
1625TEST(URLCanonTest, ReplaceMailtoURL) {
1626 ReplaceCase replace_cases[] = {
1627 // Replace everything
1628 {"mailto:[email protected]?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
1629 // Replace nothing
1630 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:[email protected]?body=sup"},
1631 // Replace the path
1632 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
1633 // Replace the query
1634 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:[email protected]?custom=1"},
1635 // Replace the path and query
1636 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
1637 // Set the query to empty (should leave trailing question mark)
1638 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:[email protected]?"},
1639 // Clear the query
1640 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:[email protected]"},
1641 // Clear the path
1642 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
1643 // Clear the path + query
1644 {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
1645 // Setting the ref should have no effect
1646 {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
1647 };
1648
1649 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1650 const ReplaceCase& cur = replace_cases[i];
1651 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231652 Parsed parsed;
1653 ParseMailtoURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521654
[email protected]0318f922014-04-22 00:09:231655 Replacements<char> r;
1656 typedef Replacements<char> R;
[email protected]e7bba5f82013-04-10 20:10:521657 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1658 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1659 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1660 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1661 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1662 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1663 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1664 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1665
1666 std::string out_str;
[email protected]0318f922014-04-22 00:09:231667 StdStringCanonOutput output(&out_str);
1668 Parsed out_parsed;
1669 ReplaceMailtoURL(cur.base, parsed, r, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521670 output.Complete();
1671
1672 EXPECT_EQ(replace_cases[i].expected, out_str);
1673 }
1674}
1675
1676TEST(URLCanonTest, CanonicalizeFileURL) {
1677 struct URLCase {
1678 const char* input;
1679 const char* expected;
1680 bool expected_success;
[email protected]0318f922014-04-22 00:09:231681 Component expected_host;
1682 Component expected_path;
[email protected]e7bba5f82013-04-10 20:10:521683 } cases[] = {
1684#ifdef _WIN32
1685 // Windows-style paths
[email protected]0318f922014-04-22 00:09:231686 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1687 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1688 {"file:", "file:///", true, Component(), Component(7, 1)},
1689 {"file:UNChost/path", "file://unchost/path", true, Component(7, 7), Component(14, 5)},
[email protected]e7bba5f82013-04-10 20:10:521690 // CanonicalizeFileURL supports absolute Windows style paths for IE
qyearsley2bc727d2015-08-14 20:17:151691 // compatibility. Note that the caller must decide that this is a file
[email protected]e7bba5f82013-04-10 20:10:521692 // URL itself so it can call the file canonicalizer. This is usually
1693 // done automatically as part of relative URL resolving.
[email protected]0318f922014-04-22 00:09:231694 {"c:\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1695 {"C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1696 {"/C|\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1697 {"//C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1698 {"//server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1699 {"\\\\server\\file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1700 {"/\\server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
[email protected]e7bba5f82013-04-10 20:10:521701 // We should preserve the number of slashes after the colon for IE
qyearsley2bc727d2015-08-14 20:17:151702 // compatibility, except when there is none, in which case we should
[email protected]e7bba5f82013-04-10 20:10:521703 // add one.
[email protected]0318f922014-04-22 00:09:231704 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1705 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
[email protected]e7bba5f82013-04-10 20:10:521706 // Three slashes should be non-UNC, even if there is no drive spec (IE
1707 // does this, which makes the resulting request invalid).
[email protected]0318f922014-04-22 00:09:231708 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(), Component(7, 12)},
[email protected]e7bba5f82013-04-10 20:10:521709 // TODO(brettw) we should probably fail for invalid host names, which
1710 // would change the expected result on this test. We also currently allow
1711 // colon even though it's probably invalid, because its currently the
1712 // "natural" result of the way the canonicalizer is written. There doesn't
1713 // seem to be a strong argument for why allowing it here would be bad, so
1714 // we just tolerate it and the load will fail later.
[email protected]0318f922014-04-22 00:09:231715 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, Component(7, 2), Component(9, 16)},
1716 {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5), Component(12, 8)},
[email protected]e7bba5f82013-04-10 20:10:521717 // Make sure relative paths can't go above the "C:"
[email protected]0318f922014-04-22 00:09:231718 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, Component(), Component(7, 12)},
[email protected]e7bba5f82013-04-10 20:10:521719 // Busted refs shouldn't make the whole thing fail.
[email protected]0318f922014-04-22 00:09:231720 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, Component(), Component(7, 8)},
[email protected]e7bba5f82013-04-10 20:10:521721#else
1722 // Unix-style paths
[email protected]0318f922014-04-22 00:09:231723 {"file:///home/me", "file:///home/me", true, Component(), Component(7, 8)},
[email protected]e7bba5f82013-04-10 20:10:521724 // Windowsy ones should get still treated as Unix-style.
[email protected]0318f922014-04-22 00:09:231725 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(), Component(7, 16)},
1726 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, Component(), Component(7, 19)},
[email protected]e7bba5f82013-04-10 20:10:521727 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
[email protected]0318f922014-04-22 00:09:231728 {"//", "file:///", true, Component(), Component(7, 1)},
1729 {"///", "file:///", true, Component(), Component(7, 1)},
1730 {"///test", "file:///test", true, Component(), Component(7, 5)},
1731 {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)},
1732 {"file://localhost", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1733 {"file://localhost/", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1734 {"file://localhost/test", "file://localhost/test", true, Component(7, 9), Component(16, 5)},
[email protected]e7bba5f82013-04-10 20:10:521735#endif // _WIN32
1736 };
1737
viettrungluu4b6915862014-10-16 03:42:491738 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521739 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231740 Parsed parsed;
1741 ParseFileURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521742
[email protected]0318f922014-04-22 00:09:231743 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521744 std::string out_str;
[email protected]0318f922014-04-22 00:09:231745 StdStringCanonOutput output(&out_str);
1746 bool success = CanonicalizeFileURL(cases[i].input, url_len, parsed, NULL,
1747 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521748 output.Complete();
1749
1750 EXPECT_EQ(cases[i].expected_success, success);
1751 EXPECT_EQ(cases[i].expected, out_str);
1752
1753 // Make sure the spec was properly identified, the file canonicalizer has
1754 // different code for writing the spec.
1755 EXPECT_EQ(0, out_parsed.scheme.begin);
1756 EXPECT_EQ(4, out_parsed.scheme.len);
1757
1758 EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
1759 EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
1760
1761 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1762 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1763 }
1764}
1765
1766TEST(URLCanonTest, CanonicalizeFileSystemURL) {
1767 struct URLCase {
1768 const char* input;
1769 const char* expected;
1770 bool expected_success;
1771 } cases[] = {
1772 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1773 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1774 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1775 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1776 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1777 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1778 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1779 };
1780
viettrungluu4b6915862014-10-16 03:42:491781 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521782 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231783 Parsed parsed;
1784 ParseFileSystemURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521785
[email protected]0318f922014-04-22 00:09:231786 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521787 std::string out_str;
[email protected]0318f922014-04-22 00:09:231788 StdStringCanonOutput output(&out_str);
1789 bool success = CanonicalizeFileSystemURL(cases[i].input, url_len, parsed,
1790 NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521791 output.Complete();
1792
1793 EXPECT_EQ(cases[i].expected_success, success);
1794 EXPECT_EQ(cases[i].expected, out_str);
1795
1796 // Make sure the spec was properly identified, the filesystem canonicalizer
1797 // has different code for writing the spec.
1798 EXPECT_EQ(0, out_parsed.scheme.begin);
1799 EXPECT_EQ(10, out_parsed.scheme.len);
1800 if (success)
1801 EXPECT_GT(out_parsed.path.len, 0);
1802 }
1803}
1804
1805TEST(URLCanonTest, CanonicalizePathURL) {
1806 // Path URLs should get canonicalized schemes but nothing else.
1807 struct PathCase {
1808 const char* input;
1809 const char* expected;
1810 } path_cases[] = {
1811 {"javascript:", "javascript:"},
1812 {"JavaScript:Foo", "javascript:Foo"},
brettw46f9b832016-10-05 19:22:481813 {"Foo:\":This /is interesting;?#", "foo:\":This /is interesting;?#"},
[email protected]e7bba5f82013-04-10 20:10:521814 };
1815
viettrungluu4b6915862014-10-16 03:42:491816 for (size_t i = 0; i < arraysize(path_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521817 int url_len = static_cast<int>(strlen(path_cases[i].input));
[email protected]0318f922014-04-22 00:09:231818 Parsed parsed;
1819 ParsePathURL(path_cases[i].input, url_len, true, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521820
[email protected]0318f922014-04-22 00:09:231821 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521822 std::string out_str;
[email protected]0318f922014-04-22 00:09:231823 StdStringCanonOutput output(&out_str);
1824 bool success = CanonicalizePathURL(path_cases[i].input, url_len, parsed,
1825 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521826 output.Complete();
1827
1828 EXPECT_TRUE(success);
1829 EXPECT_EQ(path_cases[i].expected, out_str);
1830
1831 EXPECT_EQ(0, out_parsed.host.begin);
1832 EXPECT_EQ(-1, out_parsed.host.len);
1833
1834 // When we end with a colon at the end, there should be no path.
1835 if (path_cases[i].input[url_len - 1] == ':') {
[email protected]369e84f72013-11-23 01:53:521836 EXPECT_EQ(0, out_parsed.GetContent().begin);
1837 EXPECT_EQ(-1, out_parsed.GetContent().len);
[email protected]e7bba5f82013-04-10 20:10:521838 }
1839 }
1840}
1841
1842TEST(URLCanonTest, CanonicalizeMailtoURL) {
1843 struct URLCase {
1844 const char* input;
1845 const char* expected;
1846 bool expected_success;
[email protected]0318f922014-04-22 00:09:231847 Component expected_path;
1848 Component expected_query;
[email protected]e7bba5f82013-04-10 20:10:521849 } cases[] = {
elawrenced75485092017-04-18 20:44:201850 // Null character should be escaped to %00.
1851 // Keep this test first in the list as it is handled specially below.
1852 {"mailto:addr1\0addr2?foo",
1853 "mailto:addr1%00addr2?foo",
1854 true, Component(7, 13), Component(21, 3)},
1855 {"mailto:addr1",
1856 "mailto:addr1",
1857 true, Component(7, 5), Component()},
1858 {"mailto:[email protected]",
1859 "mailto:[email protected]",
1860 true, Component(7, 13), Component()},
[email protected]e7bba5f82013-04-10 20:10:521861 // Trailing whitespace is stripped.
elawrenced75485092017-04-18 20:44:201862 {"MaIlTo:addr1 \t ",
1863 "mailto:addr1",
1864 true, Component(7, 5), Component()},
1865 {"MaIlTo:addr1?to=jon",
1866 "mailto:addr1?to=jon",
1867 true, Component(7, 5), Component(13,6)},
1868 {"mailto:addr1,addr2",
1869 "mailto:addr1,addr2",
1870 true, Component(7, 11), Component()},
1871 // Embedded spaces must be encoded.
1872 {"mailto:addr1, addr2",
1873 "mailto:addr1,%20addr2",
1874 true, Component(7, 14), Component()},
1875 {"mailto:addr1, addr2?subject=one two ",
1876 "mailto:addr1,%20addr2?subject=one%20two",
1877 true, Component(7, 14), Component(22, 17)},
1878 {"mailto:addr1%2caddr2",
1879 "mailto:addr1%2caddr2",
1880 true, Component(7, 13), Component()},
1881 {"mailto:\xF0\x90\x8C\x80",
1882 "mailto:%F0%90%8C%80",
1883 true, Component(7, 12), Component()},
[email protected]e7bba5f82013-04-10 20:10:521884 // Invalid -- UTF-8 encoded surrogate value.
elawrenced75485092017-04-18 20:44:201885 {"mailto:\xed\xa0\x80",
1886 "mailto:%EF%BF%BD",
1887 false, Component(7, 9), Component()},
1888 {"mailto:addr1?",
1889 "mailto:addr1?",
1890 true, Component(7, 5), Component(13, 0)},
1891 // Certain characters have special meanings and must be encoded.
1892 {"mailto:! \x22$&()+,-./09:;<=>@AZ[\\]&_`az{|}~\x7f?Query! \x22$&()+,-./09:;<=>@AZ[\\]&_`az{|}~",
1893 "mailto:!%20%22$&()+,-./09:;%3C=%3E@AZ[\\]&_%60az%7B%7C%7D~%7F?Query!%20%22$&()+,-./09:;%3C=%3E@AZ[\\]&_`az{|}~",
1894 true, Component(7, 53), Component(61, 47)},
[email protected]e7bba5f82013-04-10 20:10:521895 };
1896
1897 // Define outside of loop to catch bugs where components aren't reset
[email protected]0318f922014-04-22 00:09:231898 Parsed parsed;
1899 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521900
viettrungluu4b6915862014-10-16 03:42:491901 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521902 int url_len = static_cast<int>(strlen(cases[i].input));
elawrenced75485092017-04-18 20:44:201903 if (i == 0) {
1904 // The first test case purposely has a '\0' in it -- don't count it
[email protected]e7bba5f82013-04-10 20:10:521905 // as the string terminator.
1906 url_len = 22;
1907 }
[email protected]0318f922014-04-22 00:09:231908 ParseMailtoURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521909
1910 std::string out_str;
[email protected]0318f922014-04-22 00:09:231911 StdStringCanonOutput output(&out_str);
1912 bool success = CanonicalizeMailtoURL(cases[i].input, url_len, parsed,
1913 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521914 output.Complete();
1915
1916 EXPECT_EQ(cases[i].expected_success, success);
1917 EXPECT_EQ(cases[i].expected, out_str);
1918
1919 // Make sure the spec was properly identified
1920 EXPECT_EQ(0, out_parsed.scheme.begin);
1921 EXPECT_EQ(6, out_parsed.scheme.len);
1922
1923 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1924 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1925
1926 EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
1927 EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
1928 }
1929}
1930
1931#ifndef WIN32
1932
1933TEST(URLCanonTest, _itoa_s) {
1934 // We fill the buffer with 0xff to ensure that it's getting properly
qyearsley2bc727d2015-08-14 20:17:151935 // null-terminated. We also allocate one byte more than what we tell
[email protected]e7bba5f82013-04-10 20:10:521936 // _itoa_s about, and ensure that the extra byte is untouched.
1937 char buf[6];
1938 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231939 EXPECT_EQ(0, _itoa_s(12, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521940 EXPECT_STREQ("12", buf);
1941 EXPECT_EQ('\xFF', buf[3]);
1942
1943 // Test the edge cases - exactly the buffer size and one over
1944 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231945 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521946 EXPECT_STREQ("1234", buf);
1947 EXPECT_EQ('\xFF', buf[5]);
1948
1949 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231950 EXPECT_EQ(EINVAL, _itoa_s(12345, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521951 EXPECT_EQ('\xFF', buf[5]); // should never write to this location
1952
1953 // Test the template overload (note that this will see the full buffer)
1954 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231955 EXPECT_EQ(0, _itoa_s(12, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521956 EXPECT_STREQ("12", buf);
1957 EXPECT_EQ('\xFF', buf[3]);
1958
1959 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231960 EXPECT_EQ(0, _itoa_s(12345, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521961 EXPECT_STREQ("12345", buf);
1962
[email protected]0318f922014-04-22 00:09:231963 EXPECT_EQ(EINVAL, _itoa_s(123456, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521964
1965 // Test that radix 16 is supported.
1966 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231967 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 16));
[email protected]e7bba5f82013-04-10 20:10:521968 EXPECT_STREQ("4d2", buf);
1969 EXPECT_EQ('\xFF', buf[5]);
1970}
1971
1972TEST(URLCanonTest, _itow_s) {
1973 // We fill the buffer with 0xff to ensure that it's getting properly
qyearsley2bc727d2015-08-14 20:17:151974 // null-terminated. We also allocate one byte more than what we tell
[email protected]e7bba5f82013-04-10 20:10:521975 // _itoa_s about, and ensure that the extra byte is untouched.
[email protected]3774f832013-06-11 21:21:571976 base::char16 buf[6];
[email protected]e7bba5f82013-04-10 20:10:521977 const char fill_mem = 0xff;
[email protected]3774f832013-06-11 21:21:571978 const base::char16 fill_char = 0xffff;
[email protected]e7bba5f82013-04-10 20:10:521979 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231980 EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
brettw1b8582f2016-11-03 20:37:171981 EXPECT_EQ(base::UTF8ToUTF16("12"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521982 EXPECT_EQ(fill_char, buf[3]);
1983
1984 // Test the edge cases - exactly the buffer size and one over
[email protected]0318f922014-04-22 00:09:231985 EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
brettw1b8582f2016-11-03 20:37:171986 EXPECT_EQ(base::UTF8ToUTF16("1234"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521987 EXPECT_EQ(fill_char, buf[5]);
1988
1989 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231990 EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521991 EXPECT_EQ(fill_char, buf[5]); // should never write to this location
1992
1993 // Test the template overload (note that this will see the full buffer)
1994 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231995 EXPECT_EQ(0, _itow_s(12, buf, 10));
brettw1b8582f2016-11-03 20:37:171996 EXPECT_EQ(base::UTF8ToUTF16("12"),
1997 base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521998 EXPECT_EQ(fill_char, buf[3]);
1999
2000 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:232001 EXPECT_EQ(0, _itow_s(12345, buf, 10));
brettw1b8582f2016-11-03 20:37:172002 EXPECT_EQ(base::UTF8ToUTF16("12345"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:522003
[email protected]0318f922014-04-22 00:09:232004 EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:522005}
2006
2007#endif // !WIN32
2008
2009// Returns true if the given two structures are the same.
[email protected]0318f922014-04-22 00:09:232010static bool ParsedIsEqual(const Parsed& a, const Parsed& b) {
[email protected]e7bba5f82013-04-10 20:10:522011 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
2012 a.username.begin == b.username.begin && a.username.len == b.username.len &&
2013 a.password.begin == b.password.begin && a.password.len == b.password.len &&
2014 a.host.begin == b.host.begin && a.host.len == b.host.len &&
2015 a.port.begin == b.port.begin && a.port.len == b.port.len &&
2016 a.path.begin == b.path.begin && a.path.len == b.path.len &&
2017 a.query.begin == b.query.begin && a.query.len == b.query.len &&
2018 a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
2019}
2020
2021TEST(URLCanonTest, ResolveRelativeURL) {
2022 struct RelativeCase {
2023 const char* base; // Input base URL: MUST BE CANONICAL
2024 bool is_base_hier; // Is the base URL hierarchical
2025 bool is_base_file; // Tells us if the base is a file URL.
2026 const char* test; // Input URL to test against.
2027 bool succeed_relative; // Whether we expect IsRelativeURL to succeed
2028 bool is_rel; // Whether we expect |test| to be relative or not.
2029 bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed.
2030 const char* resolved; // What we expect in the result when resolving.
2031 } rel_cases[] = {
2032 // Basic absolute input.
2033 {"http://host/a", true, false, "http://another/", true, false, false, NULL},
2034 {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
2035 // Empty relative URLs should only remove the ref part of the URL,
2036 // leaving the rest unchanged.
2037 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
2038 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
2039 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
2040 // Spaces at the ends of the relative path should be ignored.
2041 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
2042 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
2043 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
2044 // Matching schemes without two slashes are treated as relative.
2045 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
2046 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
2047 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
2048 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
2049 // Nonmatching schemes are absolute.
2050 {"http://host/a", true, false, "https:host2", true, false, false, NULL},
2051 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
2052 // Absolute path input
2053 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
2054 {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
2055 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
2056 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
2057 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
2058 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
2059 // Relative path input
2060 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
2061 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
2062 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
2063 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
2064 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
2065 {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
2066 {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
2067 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
2068 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
2069 // Query input
2070 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
2071 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
2072 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
2073 // Ref input
2074 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
2075 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
2076 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
2077 // Non-hierarchical base: no relative handling. Relative input should
2078 // error, and if a scheme is present, it should be treated as absolute.
2079 {"data:foobar", false, false, "baz.html", false, false, false, NULL},
2080 {"data:foobar", false, false, "data:baz", true, false, false, NULL},
2081 {"data:foobar", false, false, "data:/base", true, false, false, NULL},
2082 // Non-hierarchical base: absolute input should succeed.
2083 {"data:foobar", false, false, "http://host/", true, false, false, NULL},
2084 {"data:foobar", false, false, "http:host", true, false, false, NULL},
ramya.v56d422052015-12-02 02:24:462085 // Non-hierarchical base: empty URL should give error.
2086 {"data:foobar", false, false, "", false, false, false, NULL},
[email protected]e7bba5f82013-04-10 20:10:522087 // Invalid schemes should be treated as relative.
2088 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
2089 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
2090 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
2091 {"data:asdf", false, false, ":foo", false, false, false, NULL},
[email protected]45172e62014-03-03 21:21:352092 {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL},
[email protected]e7bba5f82013-04-10 20:10:522093 // We should treat semicolons like any other character in URL resolving
2094 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
2095 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
2096 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
2097 // Relative URLs can also be written as "//foo/bar" which is relative to
2098 // the scheme. In this case, it would take the old scheme, so for http
2099 // the example would resolve to "http://foo/bar".
2100 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
2101 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
2102 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
2103 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
2104 {"http://host/a", true, false, "//", true, true, false, "http:"},
2105 // IE will also allow one or the other to be a backslash to get the same
2106 // behavior.
2107 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
2108 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
2109#ifdef WIN32
2110 // Resolving against Windows file base URLs.
2111 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
2112 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
2113 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
2114 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
2115 // But two backslashes on Windows should be UNC so should be treated
2116 // as absolute.
2117 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
2118 // IE doesn't support drive specs starting with two slashes. It fails
2119 // immediately and doesn't even try to load. We fix it up to either
2120 // an absolute path or UNC depending on what it looks like.
2121 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
2122 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
2123 // Windows drive specs should be allowed and treated as absolute.
2124 {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
2125 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
2126 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
2127 // Relative paths with drive letters should be allowed when the base is
2128 // also a file.
2129 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2130 // Treat absolute paths as being off of the drive.
2131 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2132 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2133 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2134 // On Windows, two slashes without a drive letter when the base is a file
2135 // means that the path is UNC.
2136 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2137 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2138#else
2139 // On Unix we fall back to relative behavior since there's nothing else
2140 // reasonable to do.
2141 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2142#endif
2143 // Even on Windows, we don't allow relative drive specs when the base
2144 // is not file.
2145 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2146 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
[email protected]598c8382013-09-18 22:55:312147 // Ensure that ports aren't allowed for hosts relative to a file url.
2148 // Although the result string shows a host:port portion, the call to
2149 // resolve the relative URL returns false, indicating parse failure,
2150 // which is what is required.
2151 {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
[email protected]e7bba5f82013-04-10 20:10:522152 // Filesystem URL tests; filesystem URLs are only valid and relative if
qyearsley2bc727d2015-08-14 20:17:152153 // they have no scheme, e.g. "./index.html". There's no valid equivalent
[email protected]e7bba5f82013-04-10 20:10:522154 // to http:index.html.
2155 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2156 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
2157 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
2158 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2159 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2160 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2161 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
2162 // Absolute URLs are still not relative to a non-standard base URL.
2163 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2164 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2165 };
2166
viettrungluu4b6915862014-10-16 03:42:492167 for (size_t i = 0; i < arraysize(rel_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:522168 const RelativeCase& cur_case = rel_cases[i];
2169
[email protected]0318f922014-04-22 00:09:232170 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:522171 int base_len = static_cast<int>(strlen(cur_case.base));
2172 if (cur_case.is_base_file)
[email protected]0318f922014-04-22 00:09:232173 ParseFileURL(cur_case.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522174 else if (cur_case.is_base_hier)
[email protected]0318f922014-04-22 00:09:232175 ParseStandardURL(cur_case.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522176 else
[email protected]0318f922014-04-22 00:09:232177 ParsePathURL(cur_case.base, base_len, false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522178
2179 // First see if it is relative.
2180 int test_len = static_cast<int>(strlen(cur_case.test));
2181 bool is_relative;
[email protected]0318f922014-04-22 00:09:232182 Component relative_component;
2183 bool succeed_is_rel = IsRelativeURL(
[email protected]e7bba5f82013-04-10 20:10:522184 cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
2185 &is_relative, &relative_component);
2186
2187 EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
2188 "succeed is rel failure on " << cur_case.test;
2189 EXPECT_EQ(cur_case.is_rel, is_relative) <<
2190 "is rel failure on " << cur_case.test;
2191 // Now resolve it.
2192 if (succeed_is_rel && is_relative && cur_case.is_rel) {
2193 std::string resolved;
[email protected]0318f922014-04-22 00:09:232194 StdStringCanonOutput output(&resolved);
2195 Parsed resolved_parsed;
[email protected]e7bba5f82013-04-10 20:10:522196
[email protected]0318f922014-04-22 00:09:232197 bool succeed_resolve = ResolveRelativeURL(
2198 cur_case.base, parsed, cur_case.is_base_file, cur_case.test,
2199 relative_component, NULL, &output, &resolved_parsed);
[email protected]e7bba5f82013-04-10 20:10:522200 output.Complete();
2201
2202 EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
2203 EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
2204
2205 // Verify that the output parsed structure is the same as parsing a
2206 // the URL freshly.
[email protected]0318f922014-04-22 00:09:232207 Parsed ref_parsed;
[email protected]e7bba5f82013-04-10 20:10:522208 int resolved_len = static_cast<int>(resolved.size());
[email protected]369e84f72013-11-23 01:53:522209 if (cur_case.is_base_file) {
[email protected]0318f922014-04-22 00:09:232210 ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522211 } else if (cur_case.is_base_hier) {
[email protected]0318f922014-04-22 00:09:232212 ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522213 } else {
[email protected]0318f922014-04-22 00:09:232214 ParsePathURL(resolved.c_str(), resolved_len, false, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522215 }
[email protected]e7bba5f82013-04-10 20:10:522216 EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
2217 }
2218 }
2219}
2220
qyearsley2bc727d2015-08-14 20:17:152221// It used to be the case that when we did a replacement with a long buffer of
2222// UTF-16 characters, we would get invalid data in the URL. This is because the
2223// buffer that it used to hold the UTF-8 data was resized, while some pointers
2224// were still kept to the old buffer that was removed.
[email protected]e7bba5f82013-04-10 20:10:522225TEST(URLCanonTest, ReplacementOverflow) {
2226 const char src[] = "file:///C:/foo/bar";
2227 int src_len = static_cast<int>(strlen(src));
[email protected]0318f922014-04-22 00:09:232228 Parsed parsed;
2229 ParseFileURL(src, src_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522230
2231 // Override two components, the path with something short, and the query with
qyearsley2bc727d2015-08-14 20:17:152232 // something long enough to trigger the bug.
[email protected]0318f922014-04-22 00:09:232233 Replacements<base::char16> repl;
[email protected]3774f832013-06-11 21:21:572234 base::string16 new_query;
[email protected]e7bba5f82013-04-10 20:10:522235 for (int i = 0; i < 4800; i++)
2236 new_query.push_back('a');
2237
brettw1b8582f2016-11-03 20:37:172238 base::string16 new_path(test_utils::TruncateWStringToUTF16(L"/foo"));
[email protected]0318f922014-04-22 00:09:232239 repl.SetPath(new_path.c_str(), Component(0, 4));
[email protected]e7bba5f82013-04-10 20:10:522240 repl.SetQuery(new_query.c_str(),
[email protected]0318f922014-04-22 00:09:232241 Component(0, static_cast<int>(new_query.length())));
[email protected]e7bba5f82013-04-10 20:10:522242
2243 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2244 // standard URLs, file URLs, etc, since they will go to the same replacement
2245 // function that was buggy.
[email protected]0318f922014-04-22 00:09:232246 Parsed repl_parsed;
[email protected]e7bba5f82013-04-10 20:10:522247 std::string repl_str;
[email protected]0318f922014-04-22 00:09:232248 StdStringCanonOutput repl_output(&repl_str);
2249 ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
[email protected]e7bba5f82013-04-10 20:10:522250 repl_output.Complete();
2251
2252 // Generate the expected string and check.
2253 std::string expected("file:///foo?");
2254 for (size_t i = 0; i < new_query.length(); i++)
2255 expected.push_back('a');
2256 EXPECT_TRUE(expected == repl_str);
2257}
[email protected]0318f922014-04-22 00:09:232258
jww036284092016-10-14 14:49:262259TEST(URLCanonTest, DefaultPortForScheme) {
2260 struct TestCases {
2261 const char* scheme;
2262 const int expected_port;
2263 } cases[]{
2264 {"http", 80},
2265 {"https", 443},
2266 {"ftp", 21},
2267 {"ws", 80},
2268 {"wss", 443},
2269 {"gopher", 70},
jww04480402016-10-25 02:50:332270 {"http-so", 80},
2271 {"https-so", 443},
jww036284092016-10-14 14:49:262272 {"fake-scheme", PORT_UNSPECIFIED},
2273 {"HTTP", PORT_UNSPECIFIED},
2274 {"HTTPS", PORT_UNSPECIFIED},
2275 {"FTP", PORT_UNSPECIFIED},
2276 {"WS", PORT_UNSPECIFIED},
2277 {"WSS", PORT_UNSPECIFIED},
2278 {"GOPHER", PORT_UNSPECIFIED},
jww04480402016-10-25 02:50:332279 {"HTTP-SO", PORT_UNSPECIFIED},
2280 {"HTTPS-SO", PORT_UNSPECIFIED},
jww036284092016-10-14 14:49:262281 };
2282
2283 for (auto& test_case : cases) {
2284 SCOPED_TRACE(test_case.scheme);
2285 EXPECT_EQ(test_case.expected_port,
2286 DefaultPortForScheme(test_case.scheme, strlen(test_case.scheme)));
2287 }
2288}
2289
[email protected]0318f922014-04-22 00:09:232290} // namespace url