blob: 8f71a911188f4bd2031a3686f09d3b2d91447b4f [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"
[email protected]e7bba5f82013-04-10 20:10:529#include "testing/gtest/include/gtest/gtest.h"
tfarina018de6e2015-05-26 17:41:2010#include "url/third_party/mozilla/url_parse.h"
[email protected]318076b2013-04-18 21:19:4511#include "url/url_canon.h"
[email protected]318076b2013-04-18 21:19:4512#include "url/url_canon_internal.h"
13#include "url/url_canon_stdstring.h"
[email protected]318076b2013-04-18 21:19:4514#include "url/url_test_utils.h"
[email protected]e7bba5f82013-04-10 20:10:5215
[email protected]0318f922014-04-22 00:09:2316namespace url {
17
18using test_utils::WStringToUTF16;
19using test_utils::ConvertUTF8ToUTF16;
20using test_utils::ConvertUTF16ToUTF8;
[email protected]e7bba5f82013-04-10 20:10:5221
22namespace {
23
24struct ComponentCase {
25 const char* input;
26 const char* expected;
[email protected]0318f922014-04-22 00:09:2327 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5228 bool expected_success;
29};
30
31// ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
32// treat each input as optional, and will only try processing if non-NULL.
33// The output is always 8-bit.
34struct DualComponentCase {
35 const char* input8;
36 const wchar_t* input16;
37 const char* expected;
[email protected]0318f922014-04-22 00:09:2338 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5239 bool expected_success;
40};
41
qyearsley2bc727d2015-08-14 20:17:1542// Test cases for CanonicalizeIPAddress(). The inputs are identical to
[email protected]e7bba5f82013-04-10 20:10:5243// DualComponentCase, but the output has extra CanonHostInfo fields.
44struct IPAddressCase {
45 const char* input8;
46 const wchar_t* input16;
47 const char* expected;
[email protected]0318f922014-04-22 00:09:2348 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5249
50 // CanonHostInfo fields, for verbose output.
51 CanonHostInfo::Family expected_family;
52 int expected_num_ipv4_components;
53 const char* expected_address_hex; // Two hex chars per IP address byte.
54};
55
56std::string BytesToHexString(unsigned char bytes[16], int length) {
57 EXPECT_TRUE(length == 0 || length == 4 || length == 16)
58 << "Bad IP address length: " << length;
59 std::string result;
60 for (int i = 0; i < length; ++i) {
[email protected]0318f922014-04-22 00:09:2361 result.push_back(kHexCharLookup[(bytes[i] >> 4) & 0xf]);
62 result.push_back(kHexCharLookup[bytes[i] & 0xf]);
[email protected]e7bba5f82013-04-10 20:10:5263 }
64 return result;
65}
66
67struct ReplaceCase {
68 const char* base;
69 const char* scheme;
70 const char* username;
71 const char* password;
72 const char* host;
73 const char* port;
74 const char* path;
75 const char* query;
76 const char* ref;
77 const char* expected;
78};
79
[email protected]e7bba5f82013-04-10 20:10:5280// Magic string used in the replacements code that tells SetupReplComp to
81// call the clear function.
82const char kDeleteComp[] = "|";
83
84// Sets up a replacement for a single component. This is given pointers to
85// the set and clear function for the component being replaced, and will
86// either set the component (if it exists) or clear it (if the replacement
87// string matches kDeleteComp).
88//
89// This template is currently used only for the 8-bit case, and the strlen
90// causes it to fail in other cases. It is left a template in case we have
91// tests for wide replacements.
92template<typename CHAR>
93void SetupReplComp(
[email protected]0318f922014-04-22 00:09:2394 void (Replacements<CHAR>::*set)(const CHAR*, const Component&),
95 void (Replacements<CHAR>::*clear)(),
96 Replacements<CHAR>* rep,
[email protected]e7bba5f82013-04-10 20:10:5297 const CHAR* str) {
98 if (str && str[0] == kDeleteComp[0]) {
99 (rep->*clear)();
100 } else if (str) {
[email protected]0318f922014-04-22 00:09:23101 (rep->*set)(str, Component(0, static_cast<int>(strlen(str))));
[email protected]e7bba5f82013-04-10 20:10:52102 }
103}
104
105} // namespace
106
107TEST(URLCanonTest, DoAppendUTF8) {
108 struct UTF8Case {
109 unsigned input;
110 const char* output;
111 } utf_cases[] = {
112 // Valid code points.
113 {0x24, "\x24"},
114 {0xA2, "\xC2\xA2"},
115 {0x20AC, "\xE2\x82\xAC"},
116 {0x24B62, "\xF0\xA4\xAD\xA2"},
117 {0x10FFFF, "\xF4\x8F\xBF\xBF"},
118 };
119 std::string out_str;
viettrungluu4b6915862014-10-16 03:42:49120 for (size_t i = 0; i < arraysize(utf_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52121 out_str.clear();
[email protected]0318f922014-04-22 00:09:23122 StdStringCanonOutput output(&out_str);
123 AppendUTF8Value(utf_cases[i].input, &output);
[email protected]e7bba5f82013-04-10 20:10:52124 output.Complete();
125 EXPECT_EQ(utf_cases[i].output, out_str);
126 }
127}
128
[email protected]7c0fccb32014-06-17 00:50:40129#if defined(GTEST_HAS_DEATH_TEST)
[email protected]e7bba5f82013-04-10 20:10:52130// TODO(mattm): Can't run this in debug mode for now, since the DCHECK will
qyearsley2bc727d2015-08-14 20:17:15131// cause the Chromium stack trace dialog to appear and hang the test.
[email protected]e7bba5f82013-04-10 20:10:52132// See http://crbug.com/49580.
[email protected]7c0fccb32014-06-17 00:50:40133#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
134#define MAYBE_DoAppendUTF8Invalid DoAppendUTF8Invalid
135#else
136#define MAYBE_DoAppendUTF8Invalid DISABLED_DoAppendUTF8Invalid
137#endif
138TEST(URLCanonTest, MAYBE_DoAppendUTF8Invalid) {
[email protected]e7bba5f82013-04-10 20:10:52139 std::string out_str;
[email protected]0318f922014-04-22 00:09:23140 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52141 // Invalid code point (too large).
142 ASSERT_DEBUG_DEATH({
[email protected]0318f922014-04-22 00:09:23143 AppendUTF8Value(0x110000, &output);
[email protected]e7bba5f82013-04-10 20:10:52144 output.Complete();
145 EXPECT_EQ("", out_str);
146 }, "");
147}
[email protected]7c0fccb32014-06-17 00:50:40148#endif // defined(GTEST_HAS_DEATH_TEST)
[email protected]e7bba5f82013-04-10 20:10:52149
150TEST(URLCanonTest, UTF) {
151 // Low-level test that we handle reading, canonicalization, and writing
152 // UTF-8/UTF-16 strings properly.
153 struct UTFCase {
154 const char* input8;
155 const wchar_t* input16;
156 bool expected_success;
157 const char* output;
158 } utf_cases[] = {
159 // Valid canonical input should get passed through & escaped.
160 {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
qyearsley2bc727d2015-08-14 20:17:15161 // Test a character that takes > 16 bits (U+10300 = old italic letter A)
[email protected]e7bba5f82013-04-10 20:10:52162 {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
qyearsley2bc727d2015-08-14 20:17:15163 // Non-shortest-form UTF-8 characters are invalid. The bad character
164 // should be replaced with the invalid character (EF BF DB in UTF-8).
[email protected]e7bba5f82013-04-10 20:10:52165 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"},
166 // Invalid UTF-8 sequences should be marked as invalid (the first
167 // sequence is truncated).
168 {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
169 // Character going off the end.
170 {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
171 // ...same with low surrogates with no high surrogate.
172 {"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"},
173 // Test a UTF-8 encoded surrogate value is marked as invalid.
174 // ED A0 80 = U+D800
175 {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
176 };
177
178 std::string out_str;
viettrungluu4b6915862014-10-16 03:42:49179 for (size_t i = 0; i < arraysize(utf_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52180 if (utf_cases[i].input8) {
181 out_str.clear();
[email protected]0318f922014-04-22 00:09:23182 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52183
184 int input_len = static_cast<int>(strlen(utf_cases[i].input8));
185 bool success = true;
186 for (int ch = 0; ch < input_len; ch++) {
187 success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
188 &output);
189 }
190 output.Complete();
191 EXPECT_EQ(utf_cases[i].expected_success, success);
192 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
193 }
194 if (utf_cases[i].input16) {
195 out_str.clear();
[email protected]0318f922014-04-22 00:09:23196 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52197
[email protected]3774f832013-06-11 21:21:57198 base::string16 input_str(WStringToUTF16(utf_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52199 int input_len = static_cast<int>(input_str.length());
200 bool success = true;
201 for (int ch = 0; ch < input_len; ch++) {
202 success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
203 &output);
204 }
205 output.Complete();
206 EXPECT_EQ(utf_cases[i].expected_success, success);
207 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
208 }
209
210 if (utf_cases[i].input8 && utf_cases[i].input16 &&
211 utf_cases[i].expected_success) {
212 // Check that the UTF-8 and UTF-16 inputs are equivalent.
213
214 // UTF-16 -> UTF-8
215 std::string input8_str(utf_cases[i].input8);
[email protected]3774f832013-06-11 21:21:57216 base::string16 input16_str(WStringToUTF16(utf_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52217 EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str));
218
219 // UTF-8 -> UTF-16
220 EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
221 }
222 }
223}
224
[email protected]e7bba5f82013-04-10 20:10:52225TEST(URLCanonTest, Scheme) {
226 // Here, we're mostly testing that unusual characters are handled properly.
227 // The canonicalizer doesn't do any parsing or whitespace detection. It will
228 // also do its best on error, and will escape funny sequences (these won't be
229 // valid schemes and it will return error).
230 //
231 // Note that the canonicalizer will append a colon to the output to separate
232 // out the rest of the URL, which is not present in the input. We check,
233 // however, that the output range includes everything but the colon.
234 ComponentCase scheme_cases[] = {
[email protected]0318f922014-04-22 00:09:23235 {"http", "http:", Component(0, 4), true},
236 {"HTTP", "http:", Component(0, 4), true},
237 {" HTTP ", "%20http%20:", Component(0, 10), false},
238 {"htt: ", "htt%3A%20:", Component(0, 9), false},
239 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false},
[email protected]e7bba5f82013-04-10 20:10:52240 // Don't re-escape something already escaped. Note that it will
241 // "canonicalize" the 'A' to 'a', but that's OK.
[email protected]0318f922014-04-22 00:09:23242 {"ht%3Atp", "ht%3atp:", Component(0, 7), false},
brettw46f9b832016-10-05 19:22:48243 {"", ":", Component(0, 0), false},
[email protected]e7bba5f82013-04-10 20:10:52244 };
245
246 std::string out_str;
247
248 for (size_t i = 0; i < arraysize(scheme_cases); i++) {
249 int url_len = static_cast<int>(strlen(scheme_cases[i].input));
[email protected]0318f922014-04-22 00:09:23250 Component in_comp(0, url_len);
251 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52252
253 out_str.clear();
[email protected]0318f922014-04-22 00:09:23254 StdStringCanonOutput output1(&out_str);
255 bool success = CanonicalizeScheme(scheme_cases[i].input, in_comp, &output1,
256 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52257 output1.Complete();
258
259 EXPECT_EQ(scheme_cases[i].expected_success, success);
260 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
261 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
262 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
263
qyearsley2bc727d2015-08-14 20:17:15264 // Now try the wide version.
[email protected]e7bba5f82013-04-10 20:10:52265 out_str.clear();
[email protected]0318f922014-04-22 00:09:23266 StdStringCanonOutput output2(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52267
[email protected]3774f832013-06-11 21:21:57268 base::string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input));
[email protected]e7bba5f82013-04-10 20:10:52269 in_comp.len = static_cast<int>(wide_input.length());
[email protected]0318f922014-04-22 00:09:23270 success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2,
271 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52272 output2.Complete();
273
274 EXPECT_EQ(scheme_cases[i].expected_success, success);
275 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
276 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
277 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
278 }
279
qyearsley2bc727d2015-08-14 20:17:15280 // Test the case where the scheme is declared nonexistent, it should be
[email protected]e7bba5f82013-04-10 20:10:52281 // converted into an empty scheme.
[email protected]0318f922014-04-22 00:09:23282 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52283 out_str.clear();
[email protected]0318f922014-04-22 00:09:23284 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52285
brettw46f9b832016-10-05 19:22:48286 EXPECT_FALSE(CanonicalizeScheme("", Component(0, -1), &output, &out_comp));
[email protected]e7bba5f82013-04-10 20:10:52287 output.Complete();
288
289 EXPECT_EQ(std::string(":"), out_str);
290 EXPECT_EQ(0, out_comp.begin);
291 EXPECT_EQ(0, out_comp.len);
292}
293
294TEST(URLCanonTest, Host) {
295 IPAddressCase host_cases[] = {
296 // Basic canonicalization, uppercase should be converted to lowercase.
[email protected]0318f922014-04-22 00:09:23297 {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52298 // Spaces and some other characters should be escaped.
[email protected]0318f922014-04-22 00:09:23299 {"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:52300 // Exciting different types of spaces!
[email protected]0318f922014-04-22 00:09:23301 {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52302 // Other types of space (no-break, zero-width, zero-width-no-break) are
303 // name-prepped away to nothing.
[email protected]0318f922014-04-22 00:09:23304 {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52305 // Ideographic full stop (full-width period for Chinese, etc.) should be
306 // treated as a dot.
[email protected]0318f922014-04-22 00:09:23307 {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:52308 // Invalid unicode characters should fail...
309 // ...In wide input, ICU will barf and we'll end up with the input as
310 // escaped UTF-8 (the invalid character should be replaced with the
311 // replacement character).
[email protected]0318f922014-04-22 00:09:23312 {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52313 // ...This is the same as previous but with with escaped.
[email protected]0318f922014-04-22 00:09:23314 {"%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:52315 // Test name prepping, fullwidth input should be converted to ASCII and NOT
316 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
[email protected]0318f922014-04-22 00:09:23317 {"\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:52318 // Test that fullwidth escaped values are properly name-prepped,
319 // then converted or rejected.
320 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
[email protected]0318f922014-04-22 00:09:23321 {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
322 {"%ef%bc%85%ef%bc%94%ef%bc%91.com", L"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52323 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
[email protected]0318f922014-04-22 00:09:23324 {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
325 {"%ef%bc%85%ef%bc%90%ef%bc%90.com", L"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
brettw1141951b2015-11-26 00:29:35326 // ICU will convert weird percents into ASCII percents, but not unescape
327 // further. A weird percent is U+FE6A (EF B9 AA in UTF-8) which is a
328 // "small percent". At this point we should be within our rights to mark
329 // anything as invalid since the URL is corrupt or malicious. The code
330 // happens to allow ASCII characters (%41 = "A" -> 'a') to be unescaped
331 // and kept as valid, so we validate that behavior here, but this level
332 // of fixing the input shouldn't be seen as required. "%81" is invalid.
333 {"\xef\xb9\xaa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
334 {"%ef%b9%aa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
335 {"\xef\xb9\xaa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
336 {"%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:52337 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
[email protected]0318f922014-04-22 00:09:23338 {"\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:50339 // See http://unicode.org/cldr/utility/idna.jsp for other
340 // examples/experiments and http://goo.gl/7yG11o
341 // for the full list of characters handled differently by
342 // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
343
344 // 4 Deviation characters are mapped/ignored in UTS 46 transitional
345 // mechansm. UTS 46, table 4 row (g).
346 // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
347 // Otherwise, it'd be "xn--fuball-cta.de".
348 {"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de",
[email protected]0318f922014-04-22 00:09:23349 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50350 // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
351 // Otherwise, it'd be "xn--wxaijb9b".
352 {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2",
[email protected]0318f922014-04-22 00:09:23353 "xn--wxaikc6b", Component(0, 12),
[email protected]18f00cd2013-09-29 10:52:50354 CanonHostInfo::NEUTRAL, -1, ""},
355 // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
356 // handling as well as in IDNA 2003.
357 {"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:23358 Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50359 // ZWJ between Devanagari characters is still mapped away in UTS 46
360 // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
361 {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
362 L"\x915\x94d\x200d\x91c", "xn--11bo0m",
[email protected]0318f922014-04-22 00:09:23363 Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50364 // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
365 // However, we do allow this at the moment because we don't use
366 // STD3 rules and canonicalize full-width ASCII to ASCII.
367 {"wow\xef\xbc\x81", L"wow\xff01", "wow%21",
[email protected]0318f922014-04-22 00:09:23368 Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50369 // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
370 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
371 {"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo",
[email protected]0318f922014-04-22 00:09:23372 Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50373 // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
374 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
375 {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn",
376 "%F0%AF%A1%A8%E5%A7%BB.cn",
[email protected]0318f922014-04-22 00:09:23377 Component(0, 24), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50378 // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
379 {"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya",
[email protected]0318f922014-04-22 00:09:23380 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50381 // Symbol/punctuations are allowed in IDNA 2003/UTS46.
382 // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
383 {"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us",
[email protected]0318f922014-04-22 00:09:23384 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50385 // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
386 // We used to allow it because we passed through unassigned code points.
387 {"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com",
[email protected]0318f922014-04-22 00:09:23388 Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50389 // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
390 // Used to be allowed in INDA 2003.
391 {"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg",
[email protected]0318f922014-04-22 00:09:23392 Component(0, 9), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50393 // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
394 // on Unicode 3.2). We did allow it in the past because we let unassigned
395 // code point pass. We continue to allow it even though it's a
396 // "punctuation and symbol" blocked in IDNA 2008.
397 // UTS 46 table 4, row (j)
398 {"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com",
[email protected]0318f922014-04-22 00:09:23399 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50400 // Maps uppercase letters to lower case letters.
401 // In IDNA 2003, it's allowed without case-folding
402 // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
403 // (added in Unicode 4.1). UTS 46 table 4 row (k)
404 {"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com",
[email protected]0318f922014-04-22 00:09:23405 Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
jshin62a92832016-03-18 18:42:39406 // Maps U+FF43 (Full Width Small Letter C) to 'c'.
407 {"ab\xef\xbd\x83.xyz", L"ab\xff43.xyz", "abc.xyz",
408 Component(0, 7), CanonHostInfo::NEUTRAL, -1, ""},
409 // Maps U+1D68C (Math Monospace Small C) to 'c'.
410 // U+1D68C = \xD835\xDE8C in UTF-16
411 {"ab\xf0\x9d\x9a\x8c.xyz", L"ab\xd835\xde8c.xyz", "abc.xyz",
412 Component(0, 7), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50413 // BiDi check test
414 // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
415 // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
416 {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
417 L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
[email protected]0318f922014-04-22 00:09:23418 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50419 // Disallowed in both IDNA 2003 and 2008 with BiDi check.
420 // Labels starting with a RTL character cannot end with a LTR character.
421 {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz",
[email protected]0318f922014-04-22 00:09:23422 "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21),
[email protected]18f00cd2013-09-29 10:52:50423 CanonHostInfo::BROKEN, -1, ""},
424 // Labels starting with a RTL character can end with BC=EN (European
425 // number). Disallowed in IDNA 2003 but now allowed.
426 {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2",
[email protected]0318f922014-04-22 00:09:23427 "xn--2-ymcov", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50428 CanonHostInfo::NEUTRAL, -1, ""},
429 // Labels starting with a RTL character cannot have "L" characters
430 // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
431 {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2",
[email protected]0318f922014-04-22 00:09:23432 "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21),
[email protected]18f00cd2013-09-29 10:52:50433 CanonHostInfo::BROKEN, -1, ""},
434 // Labels starting with a RTL character can end with BC=AN (Arabic number)
435 // Disallowed in IDNA 2003, but now allowed.
436 {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662",
[email protected]0318f922014-04-22 00:09:23437 "xn--mgbjq0r", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50438 CanonHostInfo::NEUTRAL, -1, ""},
439 // Labels starting with a RTL character cannot have "L" characters
440 // even if it ends with an BC=AN (Arabic number).
441 // Disallowed in both IDNA 2003/2008.
442 {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662",
[email protected]0318f922014-04-22 00:09:23443 "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26),
[email protected]18f00cd2013-09-29 10:52:50444 CanonHostInfo::BROKEN, -1, ""},
445 // Labels starting with a RTL character cannot mix BC=EN and BC=AN
446 {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662",
[email protected]0318f922014-04-22 00:09:23447 "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27),
[email protected]18f00cd2013-09-29 10:52:50448 CanonHostInfo::BROKEN, -1, ""},
449 // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
450 {"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com",
[email protected]0318f922014-04-22 00:09:23451 Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50452 // U+0080 is not allowed.
453 {"\xc2\x80.com", L"\x80.com", "%C2%80.com",
[email protected]0318f922014-04-22 00:09:23454 Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50455 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
[email protected]e7bba5f82013-04-10 20:10:52456 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
457 // UTF-8 (wide case). The output should be equivalent to the true wide
458 // character input above).
[email protected]18f00cd2013-09-29 10:52:50459 {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
460 L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
[email protected]0318f922014-04-22 00:09:23461 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52462 // Invalid escaped characters should fail and the percents should be
463 // escaped.
[email protected]0318f922014-04-22 00:09:23464 {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", Component(0, 10),
[email protected]18f00cd2013-09-29 10:52:50465 CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52466 // If we get an invalid character that has been escaped.
[email protected]0318f922014-04-22 00:09:23467 {"%25", L"%25", "%25", Component(0, 3),
[email protected]18f00cd2013-09-29 10:52:50468 CanonHostInfo::BROKEN, -1, ""},
[email protected]0318f922014-04-22 00:09:23469 {"hello%00", L"hello%00", "hello%00", Component(0, 8),
[email protected]18f00cd2013-09-29 10:52:50470 CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52471 // Escaped numbers should be treated like IP addresses if they are.
[email protected]18f00cd2013-09-29 10:52:50472 {"%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:23473 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
[email protected]18f00cd2013-09-29 10:52:50474 "C0A80001"},
475 {"%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:23476 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
[email protected]18f00cd2013-09-29 10:52:50477 "C0A80001"},
[email protected]e7bba5f82013-04-10 20:10:52478 // Invalid escaping should trigger the regular host error handling.
[email protected]0318f922014-04-22 00:09:23479 {"%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:52480 // Something that isn't exactly an IP should get treated as a host and
481 // spaces escaped.
[email protected]0318f922014-04-22 00:09:23482 {"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:52483 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
484 // These are "0Xc0.0250.01" in fullwidth.
[email protected]0318f922014-04-22 00:09:23485 {"\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:52486 // Broken IP addresses get marked as such.
[email protected]0318f922014-04-22 00:09:23487 {"192.168.0.257", L"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
488 {"[google.com]", L"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50489 // Cyrillic letter followed by '(' should return punycode for '(' escaped
490 // before punycode string was created. I.e.
491 // if '(' is escaped after punycode is created we would get xn--%28-8tb
492 // (incorrect).
[email protected]0318f922014-04-22 00:09:23493 {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50494 CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52495 // Address with all hexidecimal characters with leading number of 1<<32
496 // or greater and should return NEUTRAL rather than BROKEN if not all
497 // components are numbers.
[email protected]0318f922014-04-22 00:09:23498 {"12345678912345.de", L"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
499 {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
500 {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
501 {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
502 {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52503 };
504
505 // CanonicalizeHost() non-verbose.
506 std::string out_str;
507 for (size_t i = 0; i < arraysize(host_cases); i++) {
508 // Narrow version.
509 if (host_cases[i].input8) {
510 int host_len = static_cast<int>(strlen(host_cases[i].input8));
[email protected]0318f922014-04-22 00:09:23511 Component in_comp(0, host_len);
512 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52513
514 out_str.clear();
[email protected]0318f922014-04-22 00:09:23515 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52516
[email protected]0318f922014-04-22 00:09:23517 bool success = CanonicalizeHost(host_cases[i].input8, in_comp, &output,
518 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52519 output.Complete();
520
521 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
[email protected]18f00cd2013-09-29 10:52:50522 success) << "for input: " << host_cases[i].input8;
523 EXPECT_EQ(std::string(host_cases[i].expected), out_str) <<
524 "for input: " << host_cases[i].input8;
525 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) <<
526 "for input: " << host_cases[i].input8;
527 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) <<
528 "for input: " << host_cases[i].input8;
[email protected]e7bba5f82013-04-10 20:10:52529 }
530
531 // Wide version.
532 if (host_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:57533 base::string16 input16(WStringToUTF16(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) {
[email protected]3774f832013-06-11 21:21:57583 base::string16 input16(WStringToUTF16(host_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52584 int host_len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:23585 Component in_comp(0, host_len);
[email protected]e7bba5f82013-04-10 20:10:52586
587 out_str.clear();
[email protected]0318f922014-04-22 00:09:23588 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52589 CanonHostInfo host_info;
590
[email protected]0318f922014-04-22 00:09:23591 CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52592 output.Complete();
593
594 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
595 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
596 EXPECT_EQ(host_cases[i].expected_component.begin,
597 host_info.out_host.begin);
598 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
599 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
600 BytesToHexString(host_info.address, host_info.AddressLength()));
601 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
602 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
603 host_info.num_ipv4_components);
604 }
605 }
606 }
607}
608
609TEST(URLCanonTest, IPv4) {
610 IPAddressCase cases[] = {
611 // Empty is not an IP address.
[email protected]0318f922014-04-22 00:09:23612 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
613 {".", L".", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52614 // Regular IP addresses in different bases.
[email protected]0318f922014-04-22 00:09:23615 {"192.168.0.1", L"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
616 {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
617 {"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:52618 // Non-IP addresses due to invalid characters.
[email protected]0318f922014-04-22 00:09:23619 {"192.168.9.com", L"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52620 // Invalid characters for the base should be rejected.
[email protected]0318f922014-04-22 00:09:23621 {"19a.168.0.1", L"19a.168.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
622 {"0308.0250.00.01", L"0308.0250.00.01", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
623 {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52624 // If there are not enough components, the last one should fill them out.
[email protected]0318f922014-04-22 00:09:23625 {"192", L"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
626 {"0xC0a80001", L"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
627 {"030052000001", L"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
628 {"000030052000001", L"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
629 {"192.168", L"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
630 {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
631 {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
632 {"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:52633 // Too many components means not an IP address.
[email protected]0318f922014-04-22 00:09:23634 {"192.168.0.0.1", L"192.168.0.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52635 // We allow a single trailing dot.
[email protected]0318f922014-04-22 00:09:23636 {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
637 {"192.168.0.1. hello", L"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
638 {"192.168.0.1..", L"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52639 // Two dots in a row means not an IP address.
[email protected]0318f922014-04-22 00:09:23640 {"192.168..1", L"192.168..1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52641 // Any numerical overflow should be marked as BROKEN.
[email protected]0318f922014-04-22 00:09:23642 {"0x100.0", L"0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
643 {"0x100.0.0", L"0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
644 {"0x100.0.0.0", L"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
645 {"0.0x100.0.0", L"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
646 {"0.0.0x100.0", L"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
647 {"0.0.0.0x100", L"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN, -1, ""},
648 {"0.0.0x10000", L"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
649 {"0.0x1000000", L"0.0x1000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
650 {"0x100000000", L"0x100000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52651 // Repeat the previous tests, minus 1, to verify boundaries.
[email protected]0318f922014-04-22 00:09:23652 {"0xFF.0", L"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
653 {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
654 {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
655 {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
656 {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
657 {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
658 {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
659 {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
660 {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
qyearsley2bc727d2015-08-14 20:17:15661 // Old trunctations tests. They're all "BROKEN" now.
[email protected]0318f922014-04-22 00:09:23662 {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN, -1, ""},
663 {"192.168.0.257", L"192.168.0.257", "", Component(), CanonHostInfo::BROKEN, -1, ""},
664 {"192.168.0xa20001", L"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
665 {"192.015052000001", L"192.015052000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
666 {"0X12C0a80001", L"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
667 {"276.1.2", L"276.1.2", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52668 // Spaces should be rejected.
[email protected]0318f922014-04-22 00:09:23669 {"192.168.0.1 hello", L"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52670 // Very large numbers.
[email protected]0318f922014-04-22 00:09:23671 {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
672 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52673 // A number has no length limit, but long numbers can still overflow.
[email protected]0318f922014-04-22 00:09:23674 {"00000000000000000001", L"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
675 {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52676 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
[email protected]0318f922014-04-22 00:09:23677 {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
678 {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52679 // Truncation of all zeros should still result in 0.
[email protected]0318f922014-04-22 00:09:23680 {"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:52681 };
682
683 for (size_t i = 0; i < arraysize(cases); i++) {
684 // 8-bit version.
[email protected]0318f922014-04-22 00:09:23685 Component component(0, static_cast<int>(strlen(cases[i].input8)));
[email protected]e7bba5f82013-04-10 20:10:52686
687 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23688 StdStringCanonOutput output1(&out_str1);
689 CanonHostInfo host_info;
690 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52691 output1.Complete();
692
693 EXPECT_EQ(cases[i].expected_family, host_info.family);
694 EXPECT_EQ(std::string(cases[i].expected_address_hex),
695 BytesToHexString(host_info.address, host_info.AddressLength()));
696 if (host_info.family == CanonHostInfo::IPV4) {
697 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
698 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
699 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
700 EXPECT_EQ(cases[i].expected_num_ipv4_components,
701 host_info.num_ipv4_components);
702 }
703
704 // 16-bit version.
[email protected]3774f832013-06-11 21:21:57705 base::string16 input16(WStringToUTF16(cases[i].input16));
[email protected]0318f922014-04-22 00:09:23706 component = Component(0, static_cast<int>(input16.length()));
[email protected]e7bba5f82013-04-10 20:10:52707
708 std::string out_str2;
[email protected]0318f922014-04-22 00:09:23709 StdStringCanonOutput output2(&out_str2);
710 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52711 output2.Complete();
712
713 EXPECT_EQ(cases[i].expected_family, host_info.family);
714 EXPECT_EQ(std::string(cases[i].expected_address_hex),
715 BytesToHexString(host_info.address, host_info.AddressLength()));
716 if (host_info.family == CanonHostInfo::IPV4) {
717 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
718 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
719 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
720 EXPECT_EQ(cases[i].expected_num_ipv4_components,
721 host_info.num_ipv4_components);
722 }
723 }
724}
725
726TEST(URLCanonTest, IPv6) {
727 IPAddressCase cases[] = {
728 // Empty is not an IP address.
[email protected]0318f922014-04-22 00:09:23729 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52730 // Non-IPs with [:] characters are marked BROKEN.
[email protected]0318f922014-04-22 00:09:23731 {":", L":", "", Component(), CanonHostInfo::BROKEN, -1, ""},
732 {"[", L"[", "", Component(), CanonHostInfo::BROKEN, -1, ""},
733 {"[:", 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, ""},
[email protected]e7bba5f82013-04-10 20:10:52738 // Regular IP address is invalid without bounding '[' and ']'.
[email protected]0318f922014-04-22 00:09:23739 {"2001:db8::1", L"2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
740 {"[2001:db8::1", L"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
741 {"2001:db8::1]", L"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52742 // Regular IP addresses.
[email protected]0318f922014-04-22 00:09:23743 {"[::]", L"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
744 {"[::1]", L"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
745 {"[1::]", L"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
[email protected]e7bba5f82013-04-10 20:10:52746
747 // Leading zeros should be stripped.
[email protected]0318f922014-04-22 00:09:23748 {"[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:52749
750 // Upper case letters should be lowercased.
[email protected]0318f922014-04-22 00:09:23751 {"[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:52752
753 // The same address can be written with different contractions, but should
754 // get canonicalized to the same thing.
[email protected]0318f922014-04-22 00:09:23755 {"[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"},
756 {"[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:52757
758 // Addresses with embedded IPv4.
[email protected]0318f922014-04-22 00:09:23759 {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
760 {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
761 {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
762 {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
763 {"[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:52764
765 // IPv4 with last component missing.
[email protected]0318f922014-04-22 00:09:23766 {"[::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:52767
768 // IPv4 using hex.
769 // TODO(eroman): Should this format be disallowed?
[email protected]0318f922014-04-22 00:09:23770 {"[::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:52771
772 // There may be zeros surrounding the "::" contraction.
[email protected]0318f922014-04-22 00:09:23773 {"[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:52774
[email protected]0318f922014-04-22 00:09:23775 {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
[email protected]e7bba5f82013-04-10 20:10:52776
qyearsley2bc727d2015-08-14 20:17:15777 // Can only have one "::" contraction in an IPv6 string literal.
[email protected]0318f922014-04-22 00:09:23778 {"[2001::db8::1]", L"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15779 // No more than 2 consecutive ':'s.
[email protected]0318f922014-04-22 00:09:23780 {"[2001:db8:::1]", L"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
781 {"[:::]", L"[:::]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15782 // Non-IP addresses due to invalid characters.
[email protected]0318f922014-04-22 00:09:23783 {"[2001::.com]", L"[2001::.com]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15784 // If there are not enough components, the last one should fill them out.
[email protected]e7bba5f82013-04-10 20:10:52785 // ... omitted at this time ...
qyearsley2bc727d2015-08-14 20:17:15786 // Too many components means not an IP address. Similarly, with too few
787 // if using IPv4 compat or mapped addresses.
[email protected]0318f922014-04-22 00:09:23788 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
789 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
790 {"[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:52791 // Too many bits (even though 8 comonents, the last one holds 32 bits).
[email protected]0318f922014-04-22 00:09:23792 {"[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:52793
794 // Too many bits specified -- the contraction would have to be zero-length
795 // to not exceed 128 bits.
[email protected]0318f922014-04-22 00:09:23796 {"[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:52797
798 // The contraction is for 16 bits of zero.
[email protected]0318f922014-04-22 00:09:23799 {"[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:52800
801 // Cannot have a trailing colon.
[email protected]0318f922014-04-22 00:09:23802 {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
803 {"[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:52804
805 // Cannot have negative numbers.
[email protected]0318f922014-04-22 00:09:23806 {"[-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:52807
808 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
809 // The scope_id should be included in the canonicalized URL, and is an
810 // unsigned decimal number.
811
812 // Invalid because no ID was given after the percent.
813
814 // Don't allow scope-id
[email protected]0318f922014-04-22 00:09:23815 {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
816 {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
817 {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
818 {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
819 {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52820
821 // Don't allow leading or trailing colons.
[email protected]0318f922014-04-22 00:09:23822 {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
823 {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
824 {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52825
826 // We allow a single trailing dot.
827 // ... omitted at this time ...
828 // Two dots in a row means not an IP address.
[email protected]0318f922014-04-22 00:09:23829 {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52830 // Any non-first components get truncated to one byte.
831 // ... omitted at this time ...
832 // Spaces should be rejected.
[email protected]0318f922014-04-22 00:09:23833 {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52834 };
835
836 for (size_t i = 0; i < arraysize(cases); i++) {
837 // 8-bit version.
[email protected]0318f922014-04-22 00:09:23838 Component component(0, static_cast<int>(strlen(cases[i].input8)));
[email protected]e7bba5f82013-04-10 20:10:52839
840 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23841 StdStringCanonOutput output1(&out_str1);
842 CanonHostInfo host_info;
843 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52844 output1.Complete();
845
846 EXPECT_EQ(cases[i].expected_family, host_info.family);
847 EXPECT_EQ(std::string(cases[i].expected_address_hex),
848 BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
849 if (host_info.family == CanonHostInfo::IPV6) {
850 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
851 EXPECT_EQ(cases[i].expected_component.begin,
852 host_info.out_host.begin);
853 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
854 }
855
856 // 16-bit version.
[email protected]3774f832013-06-11 21:21:57857 base::string16 input16(WStringToUTF16(cases[i].input16));
[email protected]0318f922014-04-22 00:09:23858 component = Component(0, static_cast<int>(input16.length()));
[email protected]e7bba5f82013-04-10 20:10:52859
860 std::string out_str2;
[email protected]0318f922014-04-22 00:09:23861 StdStringCanonOutput output2(&out_str2);
862 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52863 output2.Complete();
864
865 EXPECT_EQ(cases[i].expected_family, host_info.family);
866 EXPECT_EQ(std::string(cases[i].expected_address_hex),
867 BytesToHexString(host_info.address, host_info.AddressLength()));
868 if (host_info.family == CanonHostInfo::IPV6) {
869 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
870 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
871 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
872 }
873 }
874}
875
876TEST(URLCanonTest, IPEmpty) {
877 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23878 StdStringCanonOutput output1(&out_str1);
879 CanonHostInfo host_info;
[email protected]e7bba5f82013-04-10 20:10:52880
881 // This tests tests.
882 const char spec[] = "192.168.0.1";
[email protected]0318f922014-04-22 00:09:23883 CanonicalizeIPAddress(spec, Component(), &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52884 EXPECT_FALSE(host_info.IsIPAddress());
885
[email protected]0318f922014-04-22 00:09:23886 CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52887 EXPECT_FALSE(host_info.IsIPAddress());
888}
889
890TEST(URLCanonTest, UserInfo) {
891 // Note that the canonicalizer should escape and treat empty components as
892 // not being there.
893
894 // We actually parse a full input URL so we can get the initial components.
895 struct UserComponentCase {
896 const char* input;
897 const char* expected;
[email protected]0318f922014-04-22 00:09:23898 Component expected_username;
899 Component expected_password;
[email protected]e7bba5f82013-04-10 20:10:52900 bool expected_success;
901 } user_info_cases[] = {
[email protected]0318f922014-04-22 00:09:23902 {"http://user:[email protected]/", "user:pass@", Component(0, 4), Component(5, 4), true},
903 {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
904 {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
905 {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
906 {"http://:[email protected]/", ":foo@", Component(0, 0), Component(1, 3), true},
907 {"http://^ :$\[email protected]/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
908 {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
909 {"http://%2540:[email protected]/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
[email protected]e7bba5f82013-04-10 20:10:52910
qyearsley2bc727d2015-08-14 20:17:15911 // IE7 compatibility: old versions allowed backslashes in usernames, but
[email protected]e7bba5f82013-04-10 20:10:52912 // IE7 does not. We disallow it as well.
[email protected]0318f922014-04-22 00:09:23913 {"ftp://me\\mydomain:[email protected]/", "", Component(0, -1), Component(0, -1), true},
[email protected]e7bba5f82013-04-10 20:10:52914 };
915
viettrungluu4b6915862014-10-16 03:42:49916 for (size_t i = 0; i < arraysize(user_info_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52917 int url_len = static_cast<int>(strlen(user_info_cases[i].input));
[email protected]0318f922014-04-22 00:09:23918 Parsed parsed;
919 ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
920 Component out_user, out_pass;
[email protected]e7bba5f82013-04-10 20:10:52921 std::string out_str;
[email protected]0318f922014-04-22 00:09:23922 StdStringCanonOutput output1(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52923
[email protected]0318f922014-04-22 00:09:23924 bool success = CanonicalizeUserInfo(user_info_cases[i].input,
925 parsed.username,
926 user_info_cases[i].input,
927 parsed.password,
928 &output1,
929 &out_user,
930 &out_pass);
[email protected]e7bba5f82013-04-10 20:10:52931 output1.Complete();
932
933 EXPECT_EQ(user_info_cases[i].expected_success, success);
934 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
935 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
936 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
937 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
938 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
939
940 // Now try the wide version
941 out_str.clear();
[email protected]0318f922014-04-22 00:09:23942 StdStringCanonOutput output2(&out_str);
[email protected]3774f832013-06-11 21:21:57943 base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
[email protected]0318f922014-04-22 00:09:23944 success = CanonicalizeUserInfo(wide_input.c_str(),
945 parsed.username,
946 wide_input.c_str(),
947 parsed.password,
948 &output2,
949 &out_user,
950 &out_pass);
[email protected]e7bba5f82013-04-10 20:10:52951 output2.Complete();
952
953 EXPECT_EQ(user_info_cases[i].expected_success, success);
954 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
955 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
956 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
957 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
958 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
959 }
960}
961
962TEST(URLCanonTest, Port) {
963 // We only need to test that the number gets properly put into the output
964 // buffer. The parser unit tests will test scanning the number correctly.
965 //
966 // Note that the CanonicalizePort will always prepend a colon to the output
qyearsley2bc727d2015-08-14 20:17:15967 // to separate it from the colon that it assumes precedes it.
[email protected]e7bba5f82013-04-10 20:10:52968 struct PortCase {
969 const char* input;
970 int default_port;
971 const char* expected;
[email protected]0318f922014-04-22 00:09:23972 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:52973 bool expected_success;
974 } port_cases[] = {
975 // Invalid input should be copied w/ failure.
[email protected]0318f922014-04-22 00:09:23976 {"as df", 80, ":as%20df", Component(1, 7), false},
977 {"-2", 80, ":-2", Component(1, 2), false},
[email protected]e7bba5f82013-04-10 20:10:52978 // Default port should be omitted.
[email protected]0318f922014-04-22 00:09:23979 {"80", 80, "", Component(0, -1), true},
980 {"8080", 80, ":8080", Component(1, 4), true},
[email protected]e7bba5f82013-04-10 20:10:52981 // PORT_UNSPECIFIED should mean always keep the port.
[email protected]0318f922014-04-22 00:09:23982 {"80", PORT_UNSPECIFIED, ":80", Component(1, 2), true},
[email protected]e7bba5f82013-04-10 20:10:52983 };
984
viettrungluu4b6915862014-10-16 03:42:49985 for (size_t i = 0; i < arraysize(port_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52986 int url_len = static_cast<int>(strlen(port_cases[i].input));
[email protected]0318f922014-04-22 00:09:23987 Component in_comp(0, url_len);
988 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52989 std::string out_str;
[email protected]0318f922014-04-22 00:09:23990 StdStringCanonOutput output1(&out_str);
991 bool success = CanonicalizePort(port_cases[i].input,
992 in_comp,
993 port_cases[i].default_port,
994 &output1,
995 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52996 output1.Complete();
997
998 EXPECT_EQ(port_cases[i].expected_success, success);
999 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
1000 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
1001 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
1002
1003 // Now try the wide version
1004 out_str.clear();
[email protected]0318f922014-04-22 00:09:231005 StdStringCanonOutput output2(&out_str);
[email protected]3774f832013-06-11 21:21:571006 base::string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
[email protected]0318f922014-04-22 00:09:231007 success = CanonicalizePort(wide_input.c_str(),
1008 in_comp,
1009 port_cases[i].default_port,
1010 &output2,
1011 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521012 output2.Complete();
1013
1014 EXPECT_EQ(port_cases[i].expected_success, success);
1015 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
1016 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
1017 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
1018 }
1019}
1020
1021TEST(URLCanonTest, Path) {
1022 DualComponentCase path_cases[] = {
1023 // ----- path collapsing tests -----
[email protected]0318f922014-04-22 00:09:231024 {"/././foo", L"/././foo", "/foo", Component(0, 4), true},
1025 {"/./.foo", L"/./.foo", "/.foo", Component(0, 5), true},
1026 {"/foo/.", L"/foo/.", "/foo/", Component(0, 5), true},
1027 {"/foo/./", L"/foo/./", "/foo/", Component(0, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521028 // double dots followed by a slash or the end of the string count
[email protected]0318f922014-04-22 00:09:231029 {"/foo/bar/..", L"/foo/bar/..", "/foo/", Component(0, 5), true},
1030 {"/foo/bar/../", L"/foo/bar/../", "/foo/", Component(0, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521031 // don't count double dots when they aren't followed by a slash
[email protected]0318f922014-04-22 00:09:231032 {"/foo/..bar", L"/foo/..bar", "/foo/..bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521033 // some in the middle
[email protected]0318f922014-04-22 00:09:231034 {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", Component(0, 8), true},
1035 {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", Component(0, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521036 // we should not be able to go above the root
[email protected]0318f922014-04-22 00:09:231037 {"/foo/../../..", L"/foo/../../..", "/", Component(0, 1), true},
1038 {"/foo/../../../ton", L"/foo/../../../ton", "/ton", Component(0, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521039 // escaped dots should be unescaped and treated the same as dots
[email protected]0318f922014-04-22 00:09:231040 {"/foo/%2e", L"/foo/%2e", "/foo/", Component(0, 5), true},
1041 {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", Component(0, 8), true},
1042 {"/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:521043 // Multiple slashes in a row should be preserved and treated like empty
1044 // directory names.
[email protected]0318f922014-04-22 00:09:231045 {"////../..", L"////../..", "//", Component(0, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521046
1047 // ----- escaping tests -----
[email protected]0318f922014-04-22 00:09:231048 {"/foo", L"/foo", "/foo", Component(0, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521049 // Valid escape sequence
[email protected]0318f922014-04-22 00:09:231050 {"/%20foo", L"/%20foo", "/%20foo", Component(0, 7), true},
[email protected]e7bba5f82013-04-10 20:10:521051 // Invalid escape sequence we should pass through unchanged.
[email protected]0318f922014-04-22 00:09:231052 {"/foo%", L"/foo%", "/foo%", Component(0, 5), true},
1053 {"/foo%2", L"/foo%2", "/foo%2", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521054 // Invalid escape sequence: bad characters should be treated the same as
1055 // the sourrounding text, not as escaped (in this case, UTF-8).
[email protected]0318f922014-04-22 00:09:231056 {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", Component(0, 10), true},
1057 {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", Component(0, 16), true},
1058 {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22), true},
[email protected]e7bba5f82013-04-10 20:10:521059 // Regular characters that are escaped should be unescaped
[email protected]0318f922014-04-22 00:09:231060 {"/foo%41%7a", L"/foo%41%7a", "/fooAz", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521061 // Funny characters that are unescaped should be escaped
[email protected]0318f922014-04-22 00:09:231062 {"/foo\x09\x91%91", NULL, "/foo%09%91%91", Component(0, 13), true},
1063 {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true},
[email protected]e7bba5f82013-04-10 20:10:521064 // Invalid characters that are escaped should cause a failure.
[email protected]0318f922014-04-22 00:09:231065 {"/foo%00%51", L"/foo%00%51", "/foo%00Q", Component(0, 8), false},
[email protected]e7bba5f82013-04-10 20:10:521066 // Some characters should be passed through unchanged regardless of esc.
[email protected]0318f922014-04-22 00:09:231067 {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13), true},
[email protected]e7bba5f82013-04-10 20:10:521068 // Characters that are properly escaped should not have the case changed
1069 // of hex letters.
[email protected]0318f922014-04-22 00:09:231070 {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13), true},
[email protected]e7bba5f82013-04-10 20:10:521071 // Funny characters that are unescaped should be escaped
[email protected]0318f922014-04-22 00:09:231072 {"/foo\tbar", L"/foo\tbar", "/foo%09bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521073 // Backslashes should get converted to forward slashes
[email protected]0318f922014-04-22 00:09:231074 {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", Component(0, 8), true},
[email protected]e7bba5f82013-04-10 20:10:521075 // Hashes found in paths (possibly only when the caller explicitly sets
1076 // the path on an already-parsed URL) should be escaped.
[email protected]0318f922014-04-22 00:09:231077 {"/foo#bar", L"/foo#bar", "/foo%23bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521078 // %7f should be allowed and %3D should not be unescaped (these were wrong
1079 // in a previous version).
[email protected]0318f922014-04-22 00:09:231080 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true},
[email protected]e7bba5f82013-04-10 20:10:521081 // @ should be passed through unchanged (escaped or unescaped).
[email protected]0318f922014-04-22 00:09:231082 {"/@asdf%40", L"/@asdf%40", "/@asdf%40", Component(0, 9), true},
pkasting69820362015-09-22 01:54:051083 // Nested escape sequences should result in escaping the leading '%' if
1084 // unescaping would result in a new escape sequence.
1085 {"/%A%42", L"/%A%42", "/%25AB", Component(0, 6), true},
1086 {"/%%41B", L"/%%41B", "/%25AB", Component(0, 6), true},
1087 {"/%%41%42", L"/%%41%42", "/%25AB", Component(0, 6), true},
1088 // Make sure truncated "nested" escapes don't result in reading off the
1089 // string end.
1090 {"/%%41", L"/%%41", "/%A", Component(0, 3), true},
1091 // Don't unescape the leading '%' if unescaping doesn't result in a valid
1092 // new escape sequence.
1093 {"/%%470", L"/%%470", "/%G0", Component(0, 4), true},
1094 {"/%%2D%41", L"/%%2D%41", "/%-A", Component(0, 4), true},
1095 // Don't erroneously downcast a UTF-16 charater in a way that makes it
1096 // look like part of an escape sequence.
1097 {NULL, L"/%%41\x0130", "/%A%C4%B0", Component(0, 9), true},
[email protected]e7bba5f82013-04-10 20:10:521098
1099 // ----- encoding tests -----
1100 // Basic conversions
[email protected]0318f922014-04-22 00:09:231101 {"/\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:521102 // Invalid unicode characters should fail. We only do validation on
1103 // UTF-16 input, so this doesn't happen on 8-bit.
[email protected]0318f922014-04-22 00:09:231104 {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", Component(0, 13), true},
1105 {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false},
[email protected]e7bba5f82013-04-10 20:10:521106 };
1107
1108 for (size_t i = 0; i < arraysize(path_cases); i++) {
1109 if (path_cases[i].input8) {
1110 int len = static_cast<int>(strlen(path_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231111 Component in_comp(0, len);
1112 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521113 std::string out_str;
[email protected]0318f922014-04-22 00:09:231114 StdStringCanonOutput output(&out_str);
1115 bool success =
1116 CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521117 output.Complete();
1118
1119 EXPECT_EQ(path_cases[i].expected_success, success);
1120 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1121 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1122 EXPECT_EQ(path_cases[i].expected, out_str);
1123 }
1124
1125 if (path_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571126 base::string16 input16(WStringToUTF16(path_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521127 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231128 Component in_comp(0, len);
1129 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521130 std::string out_str;
[email protected]0318f922014-04-22 00:09:231131 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:521132
[email protected]0318f922014-04-22 00:09:231133 bool success =
1134 CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521135 output.Complete();
1136
1137 EXPECT_EQ(path_cases[i].expected_success, success);
1138 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1139 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1140 EXPECT_EQ(path_cases[i].expected, out_str);
1141 }
1142 }
1143
1144 // Manual test: embedded NULLs should be escaped and the URL should be marked
1145 // as invalid.
1146 const char path_with_null[] = "/ab\0c";
[email protected]0318f922014-04-22 00:09:231147 Component in_comp(0, 5);
1148 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521149
1150 std::string out_str;
[email protected]0318f922014-04-22 00:09:231151 StdStringCanonOutput output(&out_str);
1152 bool success = CanonicalizePath(path_with_null, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521153 output.Complete();
1154 EXPECT_FALSE(success);
1155 EXPECT_EQ("/ab%00c", out_str);
1156}
1157
1158TEST(URLCanonTest, Query) {
1159 struct QueryCase {
1160 const char* input8;
1161 const wchar_t* input16;
[email protected]e7bba5f82013-04-10 20:10:521162 const char* expected;
1163 } query_cases[] = {
[email protected]847aaab82014-05-07 14:05:461164 // Regular ASCII case.
1165 {"foo=bar", L"foo=bar", "?foo=bar"},
[email protected]e7bba5f82013-04-10 20:10:521166 // Allow question marks in the query without escaping
[email protected]847aaab82014-05-07 14:05:461167 {"as?df", L"as?df", "?as?df"},
[email protected]e7bba5f82013-04-10 20:10:521168 // Always escape '#' since it would mark the ref.
[email protected]847aaab82014-05-07 14:05:461169 {"as#df", L"as#df", "?as%23df"},
[email protected]e7bba5f82013-04-10 20:10:521170 // Escape some questionable 8-bit characters, but never unescape.
[email protected]847aaab82014-05-07 14:05:461171 {"\x02hello\x7f bye", L"\x02hello\x7f bye", "?%02hello%7F%20bye"},
1172 {"%40%41123", L"%40%41123", "?%40%41123"},
[email protected]e7bba5f82013-04-10 20:10:521173 // Chinese input/output
[email protected]847aaab82014-05-07 14:05:461174 {"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:521175 // Invalid UTF-8/16 input should be replaced with invalid characters.
[email protected]847aaab82014-05-07 14:05:461176 {"q=\xed\xed", L"q=\xd800\xd800", "?q=%EF%BF%BD%EF%BF%BD"},
[email protected]e7bba5f82013-04-10 20:10:521177 // Don't allow < or > because sometimes they are used for XSS if the
1178 // URL is echoed in content. Firefox does this, IE doesn't.
[email protected]847aaab82014-05-07 14:05:461179 {"q=<asdf>", L"q=<asdf>", "?q=%3Casdf%3E"},
[email protected]e7bba5f82013-04-10 20:10:521180 // Escape double quotemarks in the query.
[email protected]847aaab82014-05-07 14:05:461181 {"q=\"asdf\"", L"q=\"asdf\"", "?q=%22asdf%22"},
[email protected]e7bba5f82013-04-10 20:10:521182 };
1183
viettrungluu4b6915862014-10-16 03:42:491184 for (size_t i = 0; i < arraysize(query_cases); i++) {
[email protected]0318f922014-04-22 00:09:231185 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521186
[email protected]e7bba5f82013-04-10 20:10:521187 if (query_cases[i].input8) {
1188 int len = static_cast<int>(strlen(query_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231189 Component in_comp(0, len);
[email protected]e7bba5f82013-04-10 20:10:521190 std::string out_str;
1191
[email protected]0318f922014-04-22 00:09:231192 StdStringCanonOutput output(&out_str);
[email protected]847aaab82014-05-07 14:05:461193 CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output,
[email protected]0318f922014-04-22 00:09:231194 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521195 output.Complete();
1196
1197 EXPECT_EQ(query_cases[i].expected, out_str);
1198 }
1199
1200 if (query_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571201 base::string16 input16(WStringToUTF16(query_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521202 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231203 Component in_comp(0, len);
[email protected]e7bba5f82013-04-10 20:10:521204 std::string out_str;
1205
[email protected]0318f922014-04-22 00:09:231206 StdStringCanonOutput output(&out_str);
[email protected]847aaab82014-05-07 14:05:461207 CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521208 output.Complete();
1209
1210 EXPECT_EQ(query_cases[i].expected, out_str);
1211 }
1212 }
1213
1214 // Extra test for input with embedded NULL;
1215 std::string out_str;
[email protected]0318f922014-04-22 00:09:231216 StdStringCanonOutput output(&out_str);
1217 Component out_comp;
1218 CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521219 output.Complete();
1220 EXPECT_EQ("?a%20%00z%01", out_str);
1221}
1222
1223TEST(URLCanonTest, Ref) {
1224 // Refs are trivial, it just checks the encoding.
1225 DualComponentCase ref_cases[] = {
1226 // Regular one, we shouldn't escape spaces, et al.
[email protected]0318f922014-04-22 00:09:231227 {"hello, world", L"hello, world", "#hello, world", Component(1, 12), true},
[email protected]e7bba5f82013-04-10 20:10:521228 // UTF-8/wide input should be preserved
[email protected]0318f922014-04-22 00:09:231229 {"\xc2\xa9", L"\xa9", "#\xc2\xa9", Component(1, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521230 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
[email protected]0318f922014-04-22 00:09:231231 {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", Component(1, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521232 // Escaping should be preserved unchanged, even invalid ones
[email protected]0318f922014-04-22 00:09:231233 {"%41%a", L"%41%a", "#%41%a", Component(1, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521234 // Invalid UTF-8/16 input should be flagged and the input made valid
[email protected]0318f922014-04-22 00:09:231235 {"\xc2", NULL, "#\xef\xbf\xbd", Component(1, 3), true},
1236 {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", Component(1, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521237 // Test a Unicode invalid character.
[email protected]0318f922014-04-22 00:09:231238 {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", Component(1, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521239 // Refs can have # signs and we should preserve them.
[email protected]0318f922014-04-22 00:09:231240 {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", Component(1, 9), true},
1241 {"#asdf", L"#asdf", "##asdf", Component(1, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521242 };
1243
1244 for (size_t i = 0; i < arraysize(ref_cases); i++) {
1245 // 8-bit input
1246 if (ref_cases[i].input8) {
1247 int len = static_cast<int>(strlen(ref_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231248 Component in_comp(0, len);
1249 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521250
1251 std::string out_str;
[email protected]0318f922014-04-22 00:09:231252 StdStringCanonOutput output(&out_str);
1253 CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521254 output.Complete();
1255
1256 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1257 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1258 EXPECT_EQ(ref_cases[i].expected, out_str);
1259 }
1260
1261 // 16-bit input
1262 if (ref_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571263 base::string16 input16(WStringToUTF16(ref_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521264 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231265 Component in_comp(0, len);
1266 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521267
1268 std::string out_str;
[email protected]0318f922014-04-22 00:09:231269 StdStringCanonOutput output(&out_str);
1270 CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521271 output.Complete();
1272
1273 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1274 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1275 EXPECT_EQ(ref_cases[i].expected, out_str);
1276 }
1277 }
1278
1279 // Try one with an embedded NULL. It should be stripped.
1280 const char null_input[5] = "ab\x00z";
[email protected]0318f922014-04-22 00:09:231281 Component null_input_component(0, 4);
1282 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521283
1284 std::string out_str;
[email protected]0318f922014-04-22 00:09:231285 StdStringCanonOutput output(&out_str);
1286 CanonicalizeRef(null_input, null_input_component, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521287 output.Complete();
1288
1289 EXPECT_EQ(1, out_comp.begin);
1290 EXPECT_EQ(3, out_comp.len);
1291 EXPECT_EQ("#abz", out_str);
1292}
1293
1294TEST(URLCanonTest, CanonicalizeStandardURL) {
1295 // The individual component canonicalize tests should have caught the cases
1296 // for each of those components. Here, we just need to test that the various
1297 // parts are included or excluded properly, and have the correct separators.
1298 struct URLCase {
1299 const char* input;
1300 const char* expected;
1301 bool expected_success;
1302 } cases[] = {
1303 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1304 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1305 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1306 {"http:////////user:@google.com:99?foo", "http://[email protected]:99/?foo", true},
brettw46f9b832016-10-05 19:22:481307 {"www.google.com", ":www.google.com/", false},
[email protected]e7bba5f82013-04-10 20:10:521308 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1309 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1310 {"http://user:pass@/", "http://user:pass@/", false},
1311 {"http://%25DOMAIN:[email protected]/", "http://%25DOMAIN:[email protected]/", true},
1312
1313 // Backslashes should get converted to forward slashes.
1314 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1315
1316 // Busted refs shouldn't make the whole thing fail.
1317 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1318
1319 // Basic port tests.
1320 {"http://foo:80/", "http://foo/", true},
1321 {"http://foo:81/", "http://foo:81/", true},
1322 {"httpa://foo:80/", "httpa://foo:80/", true},
1323 {"http://foo:-80/", "http://foo:-80/", false},
1324
1325 {"https://foo:443/", "https://foo/", true},
1326 {"https://foo:80/", "https://foo:80/", true},
1327 {"ftp://foo:21/", "ftp://foo/", true},
1328 {"ftp://foo:80/", "ftp://foo:80/", true},
1329 {"gopher://foo:70/", "gopher://foo/", true},
1330 {"gopher://foo:443/", "gopher://foo:443/", true},
1331 {"ws://foo:80/", "ws://foo/", true},
1332 {"ws://foo:81/", "ws://foo:81/", true},
1333 {"ws://foo:443/", "ws://foo:443/", true},
1334 {"ws://foo:815/", "ws://foo:815/", true},
1335 {"wss://foo:80/", "wss://foo:80/", true},
1336 {"wss://foo:81/", "wss://foo:81/", true},
1337 {"wss://foo:443/", "wss://foo/", true},
1338 {"wss://foo:815/", "wss://foo:815/", true},
brettw1141951b2015-11-26 00:29:351339
1340 // This particular code path ends up "backing up" to replace an invalid
1341 // host ICU generated with an escaped version. Test that in the context
1342 // of a full URL to make sure the backing up doesn't mess up the non-host
1343 // parts of the URL. "EF B9 AA" is U+FE6A which is a type of percent that
1344 // ICU will convert to an ASCII one, generating "%81".
1345 {"ws:)W\x1eW\xef\xb9\xaa""81:80/", "ws://%29w%1ew%81/", false},
[email protected]e7bba5f82013-04-10 20:10:521346 };
1347
viettrungluu4b6915862014-10-16 03:42:491348 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521349 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231350 Parsed parsed;
1351 ParseStandardURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521352
[email protected]0318f922014-04-22 00:09:231353 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521354 std::string out_str;
[email protected]0318f922014-04-22 00:09:231355 StdStringCanonOutput output(&out_str);
1356 bool success = CanonicalizeStandardURL(
[email protected]e7bba5f82013-04-10 20:10:521357 cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
1358 output.Complete();
1359
1360 EXPECT_EQ(cases[i].expected_success, success);
1361 EXPECT_EQ(cases[i].expected, out_str);
1362 }
1363}
1364
1365// The codepath here is the same as for regular canonicalization, so we just
1366// need to test that things are replaced or not correctly.
1367TEST(URLCanonTest, ReplaceStandardURL) {
1368 ReplaceCase replace_cases[] = {
1369 // Common case of truncating the path.
1370 {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
1371 // Replace everything
1372 {"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"},
1373 // Replace nothing
1374 {"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:151375 // Replace scheme with filesystem. The result is garbage, but you asked
[email protected]e7bba5f82013-04-10 20:10:521376 // for it.
1377 {"http://a:[email protected]:22/foo?baz@cat", "filesystem", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem://a:[email protected]:22/foo?baz@cat"},
1378 };
1379
1380 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1381 const ReplaceCase& cur = replace_cases[i];
1382 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231383 Parsed parsed;
1384 ParseStandardURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521385
[email protected]0318f922014-04-22 00:09:231386 Replacements<char> r;
1387 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521388
1389 // Note that for the scheme we pass in a different clear function since
1390 // there is no function to clear the scheme.
1391 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1392 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1393 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1394 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1395 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1396 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1397 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1398 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1399
1400 std::string out_str;
[email protected]0318f922014-04-22 00:09:231401 StdStringCanonOutput output(&out_str);
1402 Parsed out_parsed;
1403 ReplaceStandardURL(replace_cases[i].base, parsed, r, NULL, &output,
1404 &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521405 output.Complete();
1406
1407 EXPECT_EQ(replace_cases[i].expected, out_str);
1408 }
1409
1410 // The path pointer should be ignored if the address is invalid.
1411 {
1412 const char src[] = "http://www.google.com/here_is_the_path";
1413 int src_len = static_cast<int>(strlen(src));
1414
[email protected]0318f922014-04-22 00:09:231415 Parsed parsed;
1416 ParseStandardURL(src, src_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521417
1418 // Replace the path to 0 length string. By using 1 as the string address,
1419 // the test should get an access violation if it tries to dereference it.
[email protected]0318f922014-04-22 00:09:231420 Replacements<char> r;
1421 r.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0));
[email protected]e7bba5f82013-04-10 20:10:521422 std::string out_str1;
[email protected]0318f922014-04-22 00:09:231423 StdStringCanonOutput output1(&out_str1);
1424 Parsed new_parsed;
1425 ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
[email protected]e7bba5f82013-04-10 20:10:521426 output1.Complete();
1427 EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
1428
1429 // Same with an "invalid" path.
[email protected]0318f922014-04-22 00:09:231430 r.SetPath(reinterpret_cast<char*>(0x00000001), Component());
[email protected]e7bba5f82013-04-10 20:10:521431 std::string out_str2;
[email protected]0318f922014-04-22 00:09:231432 StdStringCanonOutput output2(&out_str2);
1433 ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
[email protected]e7bba5f82013-04-10 20:10:521434 output2.Complete();
1435 EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
1436 }
1437}
1438
1439TEST(URLCanonTest, ReplaceFileURL) {
1440 ReplaceCase replace_cases[] = {
1441 // Replace everything
1442 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1443 // Replace nothing
1444 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1445 // Clear non-path components (common)
1446 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
1447 // Replace path with something that doesn't begin with a slash and make
1448 // sure it gets added properly.
1449 {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1450 {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1451 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
1452 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
1453 {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1454 // Replace scheme -- shouldn't do anything.
1455 {"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1456 };
1457
1458 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1459 const ReplaceCase& cur = replace_cases[i];
1460 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231461 Parsed parsed;
1462 ParseFileURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521463
[email protected]0318f922014-04-22 00:09:231464 Replacements<char> r;
1465 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521466 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1467 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1468 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1469 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1470 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1471 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1472 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1473 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1474
1475 std::string out_str;
[email protected]0318f922014-04-22 00:09:231476 StdStringCanonOutput output(&out_str);
1477 Parsed out_parsed;
1478 ReplaceFileURL(cur.base, parsed, r, NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521479 output.Complete();
1480
1481 EXPECT_EQ(replace_cases[i].expected, out_str);
1482 }
1483}
1484
1485TEST(URLCanonTest, ReplaceFileSystemURL) {
1486 ReplaceCase replace_cases[] = {
1487 // Replace everything in the outer URL.
1488 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1489 // Replace nothing
1490 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"},
1491 // Clear non-path components (common)
1492 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"},
1493 // Replace path with something that doesn't begin with a slash and make
1494 // sure it gets added properly.
1495 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"},
1496 // Replace scheme -- shouldn't do anything.
1497 {"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"},
1498 // Replace username -- shouldn't do anything.
1499 {"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"},
1500 // Replace password -- shouldn't do anything.
1501 {"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"},
1502 // Replace host -- shouldn't do anything.
1503 {"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"},
1504 // Replace port -- shouldn't do anything.
1505 {"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"},
1506 };
1507
1508 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1509 const ReplaceCase& cur = replace_cases[i];
1510 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231511 Parsed parsed;
1512 ParseFileSystemURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521513
[email protected]0318f922014-04-22 00:09:231514 Replacements<char> r;
1515 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521516 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1517 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1518 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1519 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1520 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1521 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1522 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1523 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1524
1525 std::string out_str;
[email protected]0318f922014-04-22 00:09:231526 StdStringCanonOutput output(&out_str);
1527 Parsed out_parsed;
1528 ReplaceFileSystemURL(cur.base, parsed, r, NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521529 output.Complete();
1530
1531 EXPECT_EQ(replace_cases[i].expected, out_str);
1532 }
1533}
1534
1535TEST(URLCanonTest, ReplacePathURL) {
1536 ReplaceCase replace_cases[] = {
1537 // Replace everything
1538 {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
1539 // Replace nothing
1540 {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
1541 // Replace one or the other
1542 {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
1543 {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
1544 {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
1545 };
1546
1547 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1548 const ReplaceCase& cur = replace_cases[i];
1549 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231550 Parsed parsed;
1551 ParsePathURL(cur.base, base_len, false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521552
[email protected]0318f922014-04-22 00:09:231553 Replacements<char> r;
1554 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521555 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1556 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1557 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1558 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1559 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1560 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1561 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1562 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1563
1564 std::string out_str;
[email protected]0318f922014-04-22 00:09:231565 StdStringCanonOutput output(&out_str);
1566 Parsed out_parsed;
1567 ReplacePathURL(cur.base, parsed, r, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521568 output.Complete();
1569
1570 EXPECT_EQ(replace_cases[i].expected, out_str);
1571 }
1572}
1573
1574TEST(URLCanonTest, ReplaceMailtoURL) {
1575 ReplaceCase replace_cases[] = {
1576 // Replace everything
1577 {"mailto:[email protected]?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
1578 // Replace nothing
1579 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:[email protected]?body=sup"},
1580 // Replace the path
1581 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
1582 // Replace the query
1583 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:[email protected]?custom=1"},
1584 // Replace the path and query
1585 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
1586 // Set the query to empty (should leave trailing question mark)
1587 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:[email protected]?"},
1588 // Clear the query
1589 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:[email protected]"},
1590 // Clear the path
1591 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
1592 // Clear the path + query
1593 {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
1594 // Setting the ref should have no effect
1595 {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
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 ParseMailtoURL(cur.base, base_len, &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;
[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 ReplaceMailtoURL(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, CanonicalizeFileURL) {
1626 struct URLCase {
1627 const char* input;
1628 const char* expected;
1629 bool expected_success;
[email protected]0318f922014-04-22 00:09:231630 Component expected_host;
1631 Component expected_path;
[email protected]e7bba5f82013-04-10 20:10:521632 } cases[] = {
1633#ifdef _WIN32
1634 // Windows-style paths
[email protected]0318f922014-04-22 00:09:231635 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1636 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1637 {"file:", "file:///", true, Component(), Component(7, 1)},
1638 {"file:UNChost/path", "file://unchost/path", true, Component(7, 7), Component(14, 5)},
[email protected]e7bba5f82013-04-10 20:10:521639 // CanonicalizeFileURL supports absolute Windows style paths for IE
qyearsley2bc727d2015-08-14 20:17:151640 // compatibility. Note that the caller must decide that this is a file
[email protected]e7bba5f82013-04-10 20:10:521641 // URL itself so it can call the file canonicalizer. This is usually
1642 // done automatically as part of relative URL resolving.
[email protected]0318f922014-04-22 00:09:231643 {"c:\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1644 {"C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1645 {"/C|\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1646 {"//C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1647 {"//server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1648 {"\\\\server\\file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1649 {"/\\server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
[email protected]e7bba5f82013-04-10 20:10:521650 // We should preserve the number of slashes after the colon for IE
qyearsley2bc727d2015-08-14 20:17:151651 // compatibility, except when there is none, in which case we should
[email protected]e7bba5f82013-04-10 20:10:521652 // add one.
[email protected]0318f922014-04-22 00:09:231653 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1654 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
[email protected]e7bba5f82013-04-10 20:10:521655 // Three slashes should be non-UNC, even if there is no drive spec (IE
1656 // does this, which makes the resulting request invalid).
[email protected]0318f922014-04-22 00:09:231657 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(), Component(7, 12)},
[email protected]e7bba5f82013-04-10 20:10:521658 // TODO(brettw) we should probably fail for invalid host names, which
1659 // would change the expected result on this test. We also currently allow
1660 // colon even though it's probably invalid, because its currently the
1661 // "natural" result of the way the canonicalizer is written. There doesn't
1662 // seem to be a strong argument for why allowing it here would be bad, so
1663 // we just tolerate it and the load will fail later.
[email protected]0318f922014-04-22 00:09:231664 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, Component(7, 2), Component(9, 16)},
1665 {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5), Component(12, 8)},
[email protected]e7bba5f82013-04-10 20:10:521666 // Make sure relative paths can't go above the "C:"
[email protected]0318f922014-04-22 00:09:231667 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, Component(), Component(7, 12)},
[email protected]e7bba5f82013-04-10 20:10:521668 // Busted refs shouldn't make the whole thing fail.
[email protected]0318f922014-04-22 00:09:231669 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, Component(), Component(7, 8)},
[email protected]e7bba5f82013-04-10 20:10:521670#else
1671 // Unix-style paths
[email protected]0318f922014-04-22 00:09:231672 {"file:///home/me", "file:///home/me", true, Component(), Component(7, 8)},
[email protected]e7bba5f82013-04-10 20:10:521673 // Windowsy ones should get still treated as Unix-style.
[email protected]0318f922014-04-22 00:09:231674 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(), Component(7, 16)},
1675 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, Component(), Component(7, 19)},
[email protected]e7bba5f82013-04-10 20:10:521676 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
[email protected]0318f922014-04-22 00:09:231677 {"//", "file:///", true, Component(), Component(7, 1)},
1678 {"///", "file:///", true, Component(), Component(7, 1)},
1679 {"///test", "file:///test", true, Component(), Component(7, 5)},
1680 {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)},
1681 {"file://localhost", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1682 {"file://localhost/", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1683 {"file://localhost/test", "file://localhost/test", true, Component(7, 9), Component(16, 5)},
[email protected]e7bba5f82013-04-10 20:10:521684#endif // _WIN32
1685 };
1686
viettrungluu4b6915862014-10-16 03:42:491687 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521688 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231689 Parsed parsed;
1690 ParseFileURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521691
[email protected]0318f922014-04-22 00:09:231692 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521693 std::string out_str;
[email protected]0318f922014-04-22 00:09:231694 StdStringCanonOutput output(&out_str);
1695 bool success = CanonicalizeFileURL(cases[i].input, url_len, parsed, NULL,
1696 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521697 output.Complete();
1698
1699 EXPECT_EQ(cases[i].expected_success, success);
1700 EXPECT_EQ(cases[i].expected, out_str);
1701
1702 // Make sure the spec was properly identified, the file canonicalizer has
1703 // different code for writing the spec.
1704 EXPECT_EQ(0, out_parsed.scheme.begin);
1705 EXPECT_EQ(4, out_parsed.scheme.len);
1706
1707 EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
1708 EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
1709
1710 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1711 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1712 }
1713}
1714
1715TEST(URLCanonTest, CanonicalizeFileSystemURL) {
1716 struct URLCase {
1717 const char* input;
1718 const char* expected;
1719 bool expected_success;
1720 } cases[] = {
1721 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1722 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1723 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1724 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1725 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1726 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1727 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1728 };
1729
viettrungluu4b6915862014-10-16 03:42:491730 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521731 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231732 Parsed parsed;
1733 ParseFileSystemURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521734
[email protected]0318f922014-04-22 00:09:231735 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521736 std::string out_str;
[email protected]0318f922014-04-22 00:09:231737 StdStringCanonOutput output(&out_str);
1738 bool success = CanonicalizeFileSystemURL(cases[i].input, url_len, parsed,
1739 NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521740 output.Complete();
1741
1742 EXPECT_EQ(cases[i].expected_success, success);
1743 EXPECT_EQ(cases[i].expected, out_str);
1744
1745 // Make sure the spec was properly identified, the filesystem canonicalizer
1746 // has different code for writing the spec.
1747 EXPECT_EQ(0, out_parsed.scheme.begin);
1748 EXPECT_EQ(10, out_parsed.scheme.len);
1749 if (success)
1750 EXPECT_GT(out_parsed.path.len, 0);
1751 }
1752}
1753
1754TEST(URLCanonTest, CanonicalizePathURL) {
1755 // Path URLs should get canonicalized schemes but nothing else.
1756 struct PathCase {
1757 const char* input;
1758 const char* expected;
1759 } path_cases[] = {
1760 {"javascript:", "javascript:"},
1761 {"JavaScript:Foo", "javascript:Foo"},
brettw46f9b832016-10-05 19:22:481762 {"Foo:\":This /is interesting;?#", "foo:\":This /is interesting;?#"},
[email protected]e7bba5f82013-04-10 20:10:521763 };
1764
viettrungluu4b6915862014-10-16 03:42:491765 for (size_t i = 0; i < arraysize(path_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521766 int url_len = static_cast<int>(strlen(path_cases[i].input));
[email protected]0318f922014-04-22 00:09:231767 Parsed parsed;
1768 ParsePathURL(path_cases[i].input, url_len, true, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521769
[email protected]0318f922014-04-22 00:09:231770 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521771 std::string out_str;
[email protected]0318f922014-04-22 00:09:231772 StdStringCanonOutput output(&out_str);
1773 bool success = CanonicalizePathURL(path_cases[i].input, url_len, parsed,
1774 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521775 output.Complete();
1776
1777 EXPECT_TRUE(success);
1778 EXPECT_EQ(path_cases[i].expected, out_str);
1779
1780 EXPECT_EQ(0, out_parsed.host.begin);
1781 EXPECT_EQ(-1, out_parsed.host.len);
1782
1783 // When we end with a colon at the end, there should be no path.
1784 if (path_cases[i].input[url_len - 1] == ':') {
[email protected]369e84f72013-11-23 01:53:521785 EXPECT_EQ(0, out_parsed.GetContent().begin);
1786 EXPECT_EQ(-1, out_parsed.GetContent().len);
[email protected]e7bba5f82013-04-10 20:10:521787 }
1788 }
1789}
1790
1791TEST(URLCanonTest, CanonicalizeMailtoURL) {
1792 struct URLCase {
1793 const char* input;
1794 const char* expected;
1795 bool expected_success;
[email protected]0318f922014-04-22 00:09:231796 Component expected_path;
1797 Component expected_query;
[email protected]e7bba5f82013-04-10 20:10:521798 } cases[] = {
[email protected]0318f922014-04-22 00:09:231799 {"mailto:addr1", "mailto:addr1", true, Component(7, 5), Component()},
1800 {"mailto:[email protected]", "mailto:[email protected]", true, Component(7, 13), Component()},
[email protected]e7bba5f82013-04-10 20:10:521801 // Trailing whitespace is stripped.
[email protected]0318f922014-04-22 00:09:231802 {"MaIlTo:addr1 \t ", "mailto:addr1", true, Component(7, 5), Component()},
1803 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, Component(7, 5), Component(13,6)},
1804 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, Component(7, 11), Component()},
1805 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, Component(7, 12), Component()},
1806 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, Component(7, 13), Component()},
1807 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, Component(7, 12), Component()},
[email protected]e7bba5f82013-04-10 20:10:521808 // Null character should be escaped to %00
[email protected]0318f922014-04-22 00:09:231809 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, Component(7, 13), Component(21, 3)},
[email protected]e7bba5f82013-04-10 20:10:521810 // Invalid -- UTF-8 encoded surrogate value.
[email protected]0318f922014-04-22 00:09:231811 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, Component(7, 9), Component()},
1812 {"mailto:addr1?", "mailto:addr1?", true, Component(7, 5), Component(13, 0)},
[email protected]e7bba5f82013-04-10 20:10:521813 };
1814
1815 // Define outside of loop to catch bugs where components aren't reset
[email protected]0318f922014-04-22 00:09:231816 Parsed parsed;
1817 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521818
viettrungluu4b6915862014-10-16 03:42:491819 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521820 int url_len = static_cast<int>(strlen(cases[i].input));
1821 if (i == 8) {
1822 // The 9th test case purposely has a '\0' in it -- don't count it
1823 // as the string terminator.
1824 url_len = 22;
1825 }
[email protected]0318f922014-04-22 00:09:231826 ParseMailtoURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521827
1828 std::string out_str;
[email protected]0318f922014-04-22 00:09:231829 StdStringCanonOutput output(&out_str);
1830 bool success = CanonicalizeMailtoURL(cases[i].input, url_len, parsed,
1831 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521832 output.Complete();
1833
1834 EXPECT_EQ(cases[i].expected_success, success);
1835 EXPECT_EQ(cases[i].expected, out_str);
1836
1837 // Make sure the spec was properly identified
1838 EXPECT_EQ(0, out_parsed.scheme.begin);
1839 EXPECT_EQ(6, out_parsed.scheme.len);
1840
1841 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1842 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1843
1844 EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
1845 EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
1846 }
1847}
1848
1849#ifndef WIN32
1850
1851TEST(URLCanonTest, _itoa_s) {
1852 // We fill the buffer with 0xff to ensure that it's getting properly
qyearsley2bc727d2015-08-14 20:17:151853 // null-terminated. We also allocate one byte more than what we tell
[email protected]e7bba5f82013-04-10 20:10:521854 // _itoa_s about, and ensure that the extra byte is untouched.
1855 char buf[6];
1856 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231857 EXPECT_EQ(0, _itoa_s(12, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521858 EXPECT_STREQ("12", buf);
1859 EXPECT_EQ('\xFF', buf[3]);
1860
1861 // Test the edge cases - exactly the buffer size and one over
1862 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231863 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521864 EXPECT_STREQ("1234", buf);
1865 EXPECT_EQ('\xFF', buf[5]);
1866
1867 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231868 EXPECT_EQ(EINVAL, _itoa_s(12345, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521869 EXPECT_EQ('\xFF', buf[5]); // should never write to this location
1870
1871 // Test the template overload (note that this will see the full buffer)
1872 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231873 EXPECT_EQ(0, _itoa_s(12, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521874 EXPECT_STREQ("12", buf);
1875 EXPECT_EQ('\xFF', buf[3]);
1876
1877 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231878 EXPECT_EQ(0, _itoa_s(12345, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521879 EXPECT_STREQ("12345", buf);
1880
[email protected]0318f922014-04-22 00:09:231881 EXPECT_EQ(EINVAL, _itoa_s(123456, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521882
1883 // Test that radix 16 is supported.
1884 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231885 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 16));
[email protected]e7bba5f82013-04-10 20:10:521886 EXPECT_STREQ("4d2", buf);
1887 EXPECT_EQ('\xFF', buf[5]);
1888}
1889
1890TEST(URLCanonTest, _itow_s) {
1891 // We fill the buffer with 0xff to ensure that it's getting properly
qyearsley2bc727d2015-08-14 20:17:151892 // null-terminated. We also allocate one byte more than what we tell
[email protected]e7bba5f82013-04-10 20:10:521893 // _itoa_s about, and ensure that the extra byte is untouched.
[email protected]3774f832013-06-11 21:21:571894 base::char16 buf[6];
[email protected]e7bba5f82013-04-10 20:10:521895 const char fill_mem = 0xff;
[email protected]3774f832013-06-11 21:21:571896 const base::char16 fill_char = 0xffff;
[email protected]e7bba5f82013-04-10 20:10:521897 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231898 EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
[email protected]3774f832013-06-11 21:21:571899 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521900 EXPECT_EQ(fill_char, buf[3]);
1901
1902 // Test the edge cases - exactly the buffer size and one over
[email protected]0318f922014-04-22 00:09:231903 EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
[email protected]3774f832013-06-11 21:21:571904 EXPECT_EQ(WStringToUTF16(L"1234"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521905 EXPECT_EQ(fill_char, buf[5]);
1906
1907 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231908 EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521909 EXPECT_EQ(fill_char, buf[5]); // should never write to this location
1910
1911 // Test the template overload (note that this will see the full buffer)
1912 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231913 EXPECT_EQ(0, _itow_s(12, buf, 10));
[email protected]3774f832013-06-11 21:21:571914 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521915 EXPECT_EQ(fill_char, buf[3]);
1916
1917 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231918 EXPECT_EQ(0, _itow_s(12345, buf, 10));
[email protected]3774f832013-06-11 21:21:571919 EXPECT_EQ(WStringToUTF16(L"12345"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521920
[email protected]0318f922014-04-22 00:09:231921 EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521922}
1923
1924#endif // !WIN32
1925
1926// Returns true if the given two structures are the same.
[email protected]0318f922014-04-22 00:09:231927static bool ParsedIsEqual(const Parsed& a, const Parsed& b) {
[email protected]e7bba5f82013-04-10 20:10:521928 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
1929 a.username.begin == b.username.begin && a.username.len == b.username.len &&
1930 a.password.begin == b.password.begin && a.password.len == b.password.len &&
1931 a.host.begin == b.host.begin && a.host.len == b.host.len &&
1932 a.port.begin == b.port.begin && a.port.len == b.port.len &&
1933 a.path.begin == b.path.begin && a.path.len == b.path.len &&
1934 a.query.begin == b.query.begin && a.query.len == b.query.len &&
1935 a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
1936}
1937
1938TEST(URLCanonTest, ResolveRelativeURL) {
1939 struct RelativeCase {
1940 const char* base; // Input base URL: MUST BE CANONICAL
1941 bool is_base_hier; // Is the base URL hierarchical
1942 bool is_base_file; // Tells us if the base is a file URL.
1943 const char* test; // Input URL to test against.
1944 bool succeed_relative; // Whether we expect IsRelativeURL to succeed
1945 bool is_rel; // Whether we expect |test| to be relative or not.
1946 bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed.
1947 const char* resolved; // What we expect in the result when resolving.
1948 } rel_cases[] = {
1949 // Basic absolute input.
1950 {"http://host/a", true, false, "http://another/", true, false, false, NULL},
1951 {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
1952 // Empty relative URLs should only remove the ref part of the URL,
1953 // leaving the rest unchanged.
1954 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
1955 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
1956 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
1957 // Spaces at the ends of the relative path should be ignored.
1958 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
1959 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
1960 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
1961 // Matching schemes without two slashes are treated as relative.
1962 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
1963 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
1964 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
1965 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
1966 // Nonmatching schemes are absolute.
1967 {"http://host/a", true, false, "https:host2", true, false, false, NULL},
1968 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
1969 // Absolute path input
1970 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
1971 {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
1972 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
1973 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
1974 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
1975 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
1976 // Relative path input
1977 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
1978 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
1979 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
1980 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
1981 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
1982 {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
1983 {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
1984 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
1985 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
1986 // Query input
1987 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
1988 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
1989 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
1990 // Ref input
1991 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
1992 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
1993 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
1994 // Non-hierarchical base: no relative handling. Relative input should
1995 // error, and if a scheme is present, it should be treated as absolute.
1996 {"data:foobar", false, false, "baz.html", false, false, false, NULL},
1997 {"data:foobar", false, false, "data:baz", true, false, false, NULL},
1998 {"data:foobar", false, false, "data:/base", true, false, false, NULL},
1999 // Non-hierarchical base: absolute input should succeed.
2000 {"data:foobar", false, false, "http://host/", true, false, false, NULL},
2001 {"data:foobar", false, false, "http:host", true, false, false, NULL},
ramya.v56d422052015-12-02 02:24:462002 // Non-hierarchical base: empty URL should give error.
2003 {"data:foobar", false, false, "", false, false, false, NULL},
[email protected]e7bba5f82013-04-10 20:10:522004 // Invalid schemes should be treated as relative.
2005 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
2006 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
2007 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
2008 {"data:asdf", false, false, ":foo", false, false, false, NULL},
[email protected]45172e62014-03-03 21:21:352009 {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL},
[email protected]e7bba5f82013-04-10 20:10:522010 // We should treat semicolons like any other character in URL resolving
2011 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
2012 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
2013 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
2014 // Relative URLs can also be written as "//foo/bar" which is relative to
2015 // the scheme. In this case, it would take the old scheme, so for http
2016 // the example would resolve to "http://foo/bar".
2017 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
2018 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
2019 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
2020 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
2021 {"http://host/a", true, false, "//", true, true, false, "http:"},
2022 // IE will also allow one or the other to be a backslash to get the same
2023 // behavior.
2024 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
2025 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
2026#ifdef WIN32
2027 // Resolving against Windows file base URLs.
2028 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
2029 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
2030 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
2031 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
2032 // But two backslashes on Windows should be UNC so should be treated
2033 // as absolute.
2034 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
2035 // IE doesn't support drive specs starting with two slashes. It fails
2036 // immediately and doesn't even try to load. We fix it up to either
2037 // an absolute path or UNC depending on what it looks like.
2038 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
2039 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
2040 // Windows drive specs should be allowed and treated as absolute.
2041 {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
2042 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
2043 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
2044 // Relative paths with drive letters should be allowed when the base is
2045 // also a file.
2046 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2047 // Treat absolute paths as being off of the drive.
2048 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2049 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2050 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2051 // On Windows, two slashes without a drive letter when the base is a file
2052 // means that the path is UNC.
2053 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2054 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2055#else
2056 // On Unix we fall back to relative behavior since there's nothing else
2057 // reasonable to do.
2058 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2059#endif
2060 // Even on Windows, we don't allow relative drive specs when the base
2061 // is not file.
2062 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2063 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
[email protected]598c8382013-09-18 22:55:312064 // Ensure that ports aren't allowed for hosts relative to a file url.
2065 // Although the result string shows a host:port portion, the call to
2066 // resolve the relative URL returns false, indicating parse failure,
2067 // which is what is required.
2068 {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
[email protected]e7bba5f82013-04-10 20:10:522069 // Filesystem URL tests; filesystem URLs are only valid and relative if
qyearsley2bc727d2015-08-14 20:17:152070 // they have no scheme, e.g. "./index.html". There's no valid equivalent
[email protected]e7bba5f82013-04-10 20:10:522071 // to http:index.html.
2072 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2073 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
2074 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
2075 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2076 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2077 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2078 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
2079 // Absolute URLs are still not relative to a non-standard base URL.
2080 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2081 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2082 };
2083
viettrungluu4b6915862014-10-16 03:42:492084 for (size_t i = 0; i < arraysize(rel_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:522085 const RelativeCase& cur_case = rel_cases[i];
2086
[email protected]0318f922014-04-22 00:09:232087 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:522088 int base_len = static_cast<int>(strlen(cur_case.base));
2089 if (cur_case.is_base_file)
[email protected]0318f922014-04-22 00:09:232090 ParseFileURL(cur_case.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522091 else if (cur_case.is_base_hier)
[email protected]0318f922014-04-22 00:09:232092 ParseStandardURL(cur_case.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522093 else
[email protected]0318f922014-04-22 00:09:232094 ParsePathURL(cur_case.base, base_len, false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522095
2096 // First see if it is relative.
2097 int test_len = static_cast<int>(strlen(cur_case.test));
2098 bool is_relative;
[email protected]0318f922014-04-22 00:09:232099 Component relative_component;
2100 bool succeed_is_rel = IsRelativeURL(
[email protected]e7bba5f82013-04-10 20:10:522101 cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
2102 &is_relative, &relative_component);
2103
2104 EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
2105 "succeed is rel failure on " << cur_case.test;
2106 EXPECT_EQ(cur_case.is_rel, is_relative) <<
2107 "is rel failure on " << cur_case.test;
2108 // Now resolve it.
2109 if (succeed_is_rel && is_relative && cur_case.is_rel) {
2110 std::string resolved;
[email protected]0318f922014-04-22 00:09:232111 StdStringCanonOutput output(&resolved);
2112 Parsed resolved_parsed;
[email protected]e7bba5f82013-04-10 20:10:522113
[email protected]0318f922014-04-22 00:09:232114 bool succeed_resolve = ResolveRelativeURL(
2115 cur_case.base, parsed, cur_case.is_base_file, cur_case.test,
2116 relative_component, NULL, &output, &resolved_parsed);
[email protected]e7bba5f82013-04-10 20:10:522117 output.Complete();
2118
2119 EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
2120 EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
2121
2122 // Verify that the output parsed structure is the same as parsing a
2123 // the URL freshly.
[email protected]0318f922014-04-22 00:09:232124 Parsed ref_parsed;
[email protected]e7bba5f82013-04-10 20:10:522125 int resolved_len = static_cast<int>(resolved.size());
[email protected]369e84f72013-11-23 01:53:522126 if (cur_case.is_base_file) {
[email protected]0318f922014-04-22 00:09:232127 ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522128 } else if (cur_case.is_base_hier) {
[email protected]0318f922014-04-22 00:09:232129 ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522130 } else {
[email protected]0318f922014-04-22 00:09:232131 ParsePathURL(resolved.c_str(), resolved_len, false, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522132 }
[email protected]e7bba5f82013-04-10 20:10:522133 EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
2134 }
2135 }
2136}
2137
qyearsley2bc727d2015-08-14 20:17:152138// It used to be the case that when we did a replacement with a long buffer of
2139// UTF-16 characters, we would get invalid data in the URL. This is because the
2140// buffer that it used to hold the UTF-8 data was resized, while some pointers
2141// were still kept to the old buffer that was removed.
[email protected]e7bba5f82013-04-10 20:10:522142TEST(URLCanonTest, ReplacementOverflow) {
2143 const char src[] = "file:///C:/foo/bar";
2144 int src_len = static_cast<int>(strlen(src));
[email protected]0318f922014-04-22 00:09:232145 Parsed parsed;
2146 ParseFileURL(src, src_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522147
2148 // Override two components, the path with something short, and the query with
qyearsley2bc727d2015-08-14 20:17:152149 // something long enough to trigger the bug.
[email protected]0318f922014-04-22 00:09:232150 Replacements<base::char16> repl;
[email protected]3774f832013-06-11 21:21:572151 base::string16 new_query;
[email protected]e7bba5f82013-04-10 20:10:522152 for (int i = 0; i < 4800; i++)
2153 new_query.push_back('a');
2154
[email protected]3774f832013-06-11 21:21:572155 base::string16 new_path(WStringToUTF16(L"/foo"));
[email protected]0318f922014-04-22 00:09:232156 repl.SetPath(new_path.c_str(), Component(0, 4));
[email protected]e7bba5f82013-04-10 20:10:522157 repl.SetQuery(new_query.c_str(),
[email protected]0318f922014-04-22 00:09:232158 Component(0, static_cast<int>(new_query.length())));
[email protected]e7bba5f82013-04-10 20:10:522159
2160 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2161 // standard URLs, file URLs, etc, since they will go to the same replacement
2162 // function that was buggy.
[email protected]0318f922014-04-22 00:09:232163 Parsed repl_parsed;
[email protected]e7bba5f82013-04-10 20:10:522164 std::string repl_str;
[email protected]0318f922014-04-22 00:09:232165 StdStringCanonOutput repl_output(&repl_str);
2166 ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
[email protected]e7bba5f82013-04-10 20:10:522167 repl_output.Complete();
2168
2169 // Generate the expected string and check.
2170 std::string expected("file:///foo?");
2171 for (size_t i = 0; i < new_query.length(); i++)
2172 expected.push_back('a');
2173 EXPECT_TRUE(expected == repl_str);
2174}
[email protected]0318f922014-04-22 00:09:232175
jww036284092016-10-14 14:49:262176TEST(URLCanonTest, DefaultPortForScheme) {
2177 struct TestCases {
2178 const char* scheme;
2179 const int expected_port;
2180 } cases[]{
2181 {"http", 80},
2182 {"https", 443},
2183 {"ftp", 21},
2184 {"ws", 80},
2185 {"wss", 443},
2186 {"gopher", 70},
2187 {"fake-scheme", PORT_UNSPECIFIED},
2188 {"HTTP", PORT_UNSPECIFIED},
2189 {"HTTPS", PORT_UNSPECIFIED},
2190 {"FTP", PORT_UNSPECIFIED},
2191 {"WS", PORT_UNSPECIFIED},
2192 {"WSS", PORT_UNSPECIFIED},
2193 {"GOPHER", PORT_UNSPECIFIED},
2194 };
2195
2196 for (auto& test_case : cases) {
2197 SCOPED_TRACE(test_case.scheme);
2198 EXPECT_EQ(test_case.expected_port,
2199 DefaultPortForScheme(test_case.scheme, strlen(test_case.scheme)));
2200 }
2201}
2202
[email protected]0318f922014-04-22 00:09:232203} // namespace url