blob: d82f6a8f45bb4cd7b6e950c01c2d0f0f397ec62b [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>
[email protected]e7bba5f82013-04-10 20:10:526
[email protected]847aaab82014-05-07 14:05:467#include "base/macros.h"
[email protected]e7bba5f82013-04-10 20:10:528#include "testing/gtest/include/gtest/gtest.h"
tfarina018de6e2015-05-26 17:41:209#include "url/third_party/mozilla/url_parse.h"
[email protected]318076b2013-04-18 21:19:4510#include "url/url_canon.h"
[email protected]318076b2013-04-18 21:19:4511#include "url/url_canon_internal.h"
12#include "url/url_canon_stdstring.h"
[email protected]318076b2013-04-18 21:19:4513#include "url/url_test_utils.h"
[email protected]e7bba5f82013-04-10 20:10:5214
[email protected]0318f922014-04-22 00:09:2315namespace url {
16
17using test_utils::WStringToUTF16;
18using test_utils::ConvertUTF8ToUTF16;
19using test_utils::ConvertUTF16ToUTF8;
[email protected]e7bba5f82013-04-10 20:10:5220
21namespace {
22
23struct ComponentCase {
24 const char* input;
25 const char* expected;
[email protected]0318f922014-04-22 00:09:2326 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5227 bool expected_success;
28};
29
30// ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
31// treat each input as optional, and will only try processing if non-NULL.
32// The output is always 8-bit.
33struct DualComponentCase {
34 const char* input8;
35 const wchar_t* input16;
36 const char* expected;
[email protected]0318f922014-04-22 00:09:2337 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5238 bool expected_success;
39};
40
qyearsley2bc727d2015-08-14 20:17:1541// Test cases for CanonicalizeIPAddress(). The inputs are identical to
[email protected]e7bba5f82013-04-10 20:10:5242// DualComponentCase, but the output has extra CanonHostInfo fields.
43struct IPAddressCase {
44 const char* input8;
45 const wchar_t* input16;
46 const char* expected;
[email protected]0318f922014-04-22 00:09:2347 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5248
49 // CanonHostInfo fields, for verbose output.
50 CanonHostInfo::Family expected_family;
51 int expected_num_ipv4_components;
52 const char* expected_address_hex; // Two hex chars per IP address byte.
53};
54
55std::string BytesToHexString(unsigned char bytes[16], int length) {
56 EXPECT_TRUE(length == 0 || length == 4 || length == 16)
57 << "Bad IP address length: " << length;
58 std::string result;
59 for (int i = 0; i < length; ++i) {
[email protected]0318f922014-04-22 00:09:2360 result.push_back(kHexCharLookup[(bytes[i] >> 4) & 0xf]);
61 result.push_back(kHexCharLookup[bytes[i] & 0xf]);
[email protected]e7bba5f82013-04-10 20:10:5262 }
63 return result;
64}
65
66struct ReplaceCase {
67 const char* base;
68 const char* scheme;
69 const char* username;
70 const char* password;
71 const char* host;
72 const char* port;
73 const char* path;
74 const char* query;
75 const char* ref;
76 const char* expected;
77};
78
[email protected]e7bba5f82013-04-10 20:10:5279// Magic string used in the replacements code that tells SetupReplComp to
80// call the clear function.
81const char kDeleteComp[] = "|";
82
83// Sets up a replacement for a single component. This is given pointers to
84// the set and clear function for the component being replaced, and will
85// either set the component (if it exists) or clear it (if the replacement
86// string matches kDeleteComp).
87//
88// This template is currently used only for the 8-bit case, and the strlen
89// causes it to fail in other cases. It is left a template in case we have
90// tests for wide replacements.
91template<typename CHAR>
92void SetupReplComp(
[email protected]0318f922014-04-22 00:09:2393 void (Replacements<CHAR>::*set)(const CHAR*, const Component&),
94 void (Replacements<CHAR>::*clear)(),
95 Replacements<CHAR>* rep,
[email protected]e7bba5f82013-04-10 20:10:5296 const CHAR* str) {
97 if (str && str[0] == kDeleteComp[0]) {
98 (rep->*clear)();
99 } else if (str) {
[email protected]0318f922014-04-22 00:09:23100 (rep->*set)(str, Component(0, static_cast<int>(strlen(str))));
[email protected]e7bba5f82013-04-10 20:10:52101 }
102}
103
104} // namespace
105
106TEST(URLCanonTest, DoAppendUTF8) {
107 struct UTF8Case {
108 unsigned input;
109 const char* output;
110 } utf_cases[] = {
111 // Valid code points.
112 {0x24, "\x24"},
113 {0xA2, "\xC2\xA2"},
114 {0x20AC, "\xE2\x82\xAC"},
115 {0x24B62, "\xF0\xA4\xAD\xA2"},
116 {0x10FFFF, "\xF4\x8F\xBF\xBF"},
117 };
118 std::string out_str;
viettrungluu4b6915862014-10-16 03:42:49119 for (size_t i = 0; i < arraysize(utf_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52120 out_str.clear();
[email protected]0318f922014-04-22 00:09:23121 StdStringCanonOutput output(&out_str);
122 AppendUTF8Value(utf_cases[i].input, &output);
[email protected]e7bba5f82013-04-10 20:10:52123 output.Complete();
124 EXPECT_EQ(utf_cases[i].output, out_str);
125 }
126}
127
[email protected]7c0fccb32014-06-17 00:50:40128#if defined(GTEST_HAS_DEATH_TEST)
[email protected]e7bba5f82013-04-10 20:10:52129// TODO(mattm): Can't run this in debug mode for now, since the DCHECK will
qyearsley2bc727d2015-08-14 20:17:15130// cause the Chromium stack trace dialog to appear and hang the test.
[email protected]e7bba5f82013-04-10 20:10:52131// See http://crbug.com/49580.
[email protected]7c0fccb32014-06-17 00:50:40132#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
133#define MAYBE_DoAppendUTF8Invalid DoAppendUTF8Invalid
134#else
135#define MAYBE_DoAppendUTF8Invalid DISABLED_DoAppendUTF8Invalid
136#endif
137TEST(URLCanonTest, MAYBE_DoAppendUTF8Invalid) {
[email protected]e7bba5f82013-04-10 20:10:52138 std::string out_str;
[email protected]0318f922014-04-22 00:09:23139 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52140 // Invalid code point (too large).
141 ASSERT_DEBUG_DEATH({
[email protected]0318f922014-04-22 00:09:23142 AppendUTF8Value(0x110000, &output);
[email protected]e7bba5f82013-04-10 20:10:52143 output.Complete();
144 EXPECT_EQ("", out_str);
145 }, "");
146}
[email protected]7c0fccb32014-06-17 00:50:40147#endif // defined(GTEST_HAS_DEATH_TEST)
[email protected]e7bba5f82013-04-10 20:10:52148
149TEST(URLCanonTest, UTF) {
150 // Low-level test that we handle reading, canonicalization, and writing
151 // UTF-8/UTF-16 strings properly.
152 struct UTFCase {
153 const char* input8;
154 const wchar_t* input16;
155 bool expected_success;
156 const char* output;
157 } utf_cases[] = {
158 // Valid canonical input should get passed through & escaped.
159 {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
qyearsley2bc727d2015-08-14 20:17:15160 // Test a character that takes > 16 bits (U+10300 = old italic letter A)
[email protected]e7bba5f82013-04-10 20:10:52161 {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
qyearsley2bc727d2015-08-14 20:17:15162 // Non-shortest-form UTF-8 characters are invalid. The bad character
163 // should be replaced with the invalid character (EF BF DB in UTF-8).
[email protected]e7bba5f82013-04-10 20:10:52164 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"},
165 // Invalid UTF-8 sequences should be marked as invalid (the first
166 // sequence is truncated).
167 {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
168 // Character going off the end.
169 {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
170 // ...same with low surrogates with no high surrogate.
171 {"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"},
172 // Test a UTF-8 encoded surrogate value is marked as invalid.
173 // ED A0 80 = U+D800
174 {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
175 };
176
177 std::string out_str;
viettrungluu4b6915862014-10-16 03:42:49178 for (size_t i = 0; i < arraysize(utf_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52179 if (utf_cases[i].input8) {
180 out_str.clear();
[email protected]0318f922014-04-22 00:09:23181 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52182
183 int input_len = static_cast<int>(strlen(utf_cases[i].input8));
184 bool success = true;
185 for (int ch = 0; ch < input_len; ch++) {
186 success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
187 &output);
188 }
189 output.Complete();
190 EXPECT_EQ(utf_cases[i].expected_success, success);
191 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
192 }
193 if (utf_cases[i].input16) {
194 out_str.clear();
[email protected]0318f922014-04-22 00:09:23195 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52196
[email protected]3774f832013-06-11 21:21:57197 base::string16 input_str(WStringToUTF16(utf_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52198 int input_len = static_cast<int>(input_str.length());
199 bool success = true;
200 for (int ch = 0; ch < input_len; ch++) {
201 success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
202 &output);
203 }
204 output.Complete();
205 EXPECT_EQ(utf_cases[i].expected_success, success);
206 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
207 }
208
209 if (utf_cases[i].input8 && utf_cases[i].input16 &&
210 utf_cases[i].expected_success) {
211 // Check that the UTF-8 and UTF-16 inputs are equivalent.
212
213 // UTF-16 -> UTF-8
214 std::string input8_str(utf_cases[i].input8);
[email protected]3774f832013-06-11 21:21:57215 base::string16 input16_str(WStringToUTF16(utf_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52216 EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str));
217
218 // UTF-8 -> UTF-16
219 EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
220 }
221 }
222}
223
[email protected]e7bba5f82013-04-10 20:10:52224TEST(URLCanonTest, Scheme) {
225 // Here, we're mostly testing that unusual characters are handled properly.
226 // The canonicalizer doesn't do any parsing or whitespace detection. It will
227 // also do its best on error, and will escape funny sequences (these won't be
228 // valid schemes and it will return error).
229 //
230 // Note that the canonicalizer will append a colon to the output to separate
231 // out the rest of the URL, which is not present in the input. We check,
232 // however, that the output range includes everything but the colon.
233 ComponentCase scheme_cases[] = {
[email protected]0318f922014-04-22 00:09:23234 {"http", "http:", Component(0, 4), true},
235 {"HTTP", "http:", Component(0, 4), true},
236 {" HTTP ", "%20http%20:", Component(0, 10), false},
237 {"htt: ", "htt%3A%20:", Component(0, 9), false},
238 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false},
[email protected]e7bba5f82013-04-10 20:10:52239 // Don't re-escape something already escaped. Note that it will
240 // "canonicalize" the 'A' to 'a', but that's OK.
[email protected]0318f922014-04-22 00:09:23241 {"ht%3Atp", "ht%3atp:", Component(0, 7), false},
[email protected]e7bba5f82013-04-10 20:10:52242 };
243
244 std::string out_str;
245
246 for (size_t i = 0; i < arraysize(scheme_cases); i++) {
247 int url_len = static_cast<int>(strlen(scheme_cases[i].input));
[email protected]0318f922014-04-22 00:09:23248 Component in_comp(0, url_len);
249 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52250
251 out_str.clear();
[email protected]0318f922014-04-22 00:09:23252 StdStringCanonOutput output1(&out_str);
253 bool success = CanonicalizeScheme(scheme_cases[i].input, in_comp, &output1,
254 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52255 output1.Complete();
256
257 EXPECT_EQ(scheme_cases[i].expected_success, success);
258 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
259 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
260 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
261
qyearsley2bc727d2015-08-14 20:17:15262 // Now try the wide version.
[email protected]e7bba5f82013-04-10 20:10:52263 out_str.clear();
[email protected]0318f922014-04-22 00:09:23264 StdStringCanonOutput output2(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52265
[email protected]3774f832013-06-11 21:21:57266 base::string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input));
[email protected]e7bba5f82013-04-10 20:10:52267 in_comp.len = static_cast<int>(wide_input.length());
[email protected]0318f922014-04-22 00:09:23268 success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2,
269 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52270 output2.Complete();
271
272 EXPECT_EQ(scheme_cases[i].expected_success, success);
273 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
274 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
275 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
276 }
277
qyearsley2bc727d2015-08-14 20:17:15278 // Test the case where the scheme is declared nonexistent, it should be
[email protected]e7bba5f82013-04-10 20:10:52279 // converted into an empty scheme.
[email protected]0318f922014-04-22 00:09:23280 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52281 out_str.clear();
[email protected]0318f922014-04-22 00:09:23282 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52283
[email protected]0318f922014-04-22 00:09:23284 EXPECT_TRUE(CanonicalizeScheme("", Component(0, -1), &output, &out_comp));
[email protected]e7bba5f82013-04-10 20:10:52285 output.Complete();
286
287 EXPECT_EQ(std::string(":"), out_str);
288 EXPECT_EQ(0, out_comp.begin);
289 EXPECT_EQ(0, out_comp.len);
290}
291
292TEST(URLCanonTest, Host) {
293 IPAddressCase host_cases[] = {
294 // Basic canonicalization, uppercase should be converted to lowercase.
[email protected]0318f922014-04-22 00:09:23295 {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52296 // Spaces and some other characters should be escaped.
[email protected]0318f922014-04-22 00:09:23297 {"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:52298 // Exciting different types of spaces!
[email protected]0318f922014-04-22 00:09:23299 {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52300 // Other types of space (no-break, zero-width, zero-width-no-break) are
301 // name-prepped away to nothing.
[email protected]0318f922014-04-22 00:09:23302 {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52303 // Ideographic full stop (full-width period for Chinese, etc.) should be
304 // treated as a dot.
[email protected]0318f922014-04-22 00:09:23305 {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:52306 // Invalid unicode characters should fail...
307 // ...In wide input, ICU will barf and we'll end up with the input as
308 // escaped UTF-8 (the invalid character should be replaced with the
309 // replacement character).
[email protected]0318f922014-04-22 00:09:23310 {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52311 // ...This is the same as previous but with with escaped.
[email protected]0318f922014-04-22 00:09:23312 {"%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:52313 // Test name prepping, fullwidth input should be converted to ASCII and NOT
314 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
[email protected]0318f922014-04-22 00:09:23315 {"\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:52316 // Test that fullwidth escaped values are properly name-prepped,
317 // then converted or rejected.
318 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
[email protected]0318f922014-04-22 00:09:23319 {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
320 {"%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:52321 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
[email protected]0318f922014-04-22 00:09:23322 {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
323 {"%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:35324 // ICU will convert weird percents into ASCII percents, but not unescape
325 // further. A weird percent is U+FE6A (EF B9 AA in UTF-8) which is a
326 // "small percent". At this point we should be within our rights to mark
327 // anything as invalid since the URL is corrupt or malicious. The code
328 // happens to allow ASCII characters (%41 = "A" -> 'a') to be unescaped
329 // and kept as valid, so we validate that behavior here, but this level
330 // of fixing the input shouldn't be seen as required. "%81" is invalid.
331 {"\xef\xb9\xaa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
332 {"%ef%b9%aa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
333 {"\xef\xb9\xaa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
334 {"%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:52335 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
[email protected]0318f922014-04-22 00:09:23336 {"\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:50337 // See http://unicode.org/cldr/utility/idna.jsp for other
338 // examples/experiments and http://goo.gl/7yG11o
339 // for the full list of characters handled differently by
340 // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
341
342 // 4 Deviation characters are mapped/ignored in UTS 46 transitional
343 // mechansm. UTS 46, table 4 row (g).
344 // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
345 // Otherwise, it'd be "xn--fuball-cta.de".
346 {"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de",
[email protected]0318f922014-04-22 00:09:23347 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50348 // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
349 // Otherwise, it'd be "xn--wxaijb9b".
350 {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2",
[email protected]0318f922014-04-22 00:09:23351 "xn--wxaikc6b", Component(0, 12),
[email protected]18f00cd2013-09-29 10:52:50352 CanonHostInfo::NEUTRAL, -1, ""},
353 // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
354 // handling as well as in IDNA 2003.
355 {"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:23356 Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50357 // ZWJ between Devanagari characters is still mapped away in UTS 46
358 // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
359 {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
360 L"\x915\x94d\x200d\x91c", "xn--11bo0m",
[email protected]0318f922014-04-22 00:09:23361 Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50362 // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
363 // However, we do allow this at the moment because we don't use
364 // STD3 rules and canonicalize full-width ASCII to ASCII.
365 {"wow\xef\xbc\x81", L"wow\xff01", "wow%21",
[email protected]0318f922014-04-22 00:09:23366 Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50367 // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
368 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
369 {"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo",
[email protected]0318f922014-04-22 00:09:23370 Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50371 // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
372 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
373 {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn",
374 "%F0%AF%A1%A8%E5%A7%BB.cn",
[email protected]0318f922014-04-22 00:09:23375 Component(0, 24), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50376 // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
377 {"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya",
[email protected]0318f922014-04-22 00:09:23378 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50379 // Symbol/punctuations are allowed in IDNA 2003/UTS46.
380 // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
381 {"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us",
[email protected]0318f922014-04-22 00:09:23382 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50383 // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
384 // We used to allow it because we passed through unassigned code points.
385 {"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com",
[email protected]0318f922014-04-22 00:09:23386 Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50387 // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
388 // Used to be allowed in INDA 2003.
389 {"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg",
[email protected]0318f922014-04-22 00:09:23390 Component(0, 9), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50391 // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
392 // on Unicode 3.2). We did allow it in the past because we let unassigned
393 // code point pass. We continue to allow it even though it's a
394 // "punctuation and symbol" blocked in IDNA 2008.
395 // UTS 46 table 4, row (j)
396 {"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com",
[email protected]0318f922014-04-22 00:09:23397 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50398 // Maps uppercase letters to lower case letters.
399 // In IDNA 2003, it's allowed without case-folding
400 // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
401 // (added in Unicode 4.1). UTS 46 table 4 row (k)
402 {"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com",
[email protected]0318f922014-04-22 00:09:23403 Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50404 // BiDi check test
405 // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
406 // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
407 {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
408 L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
[email protected]0318f922014-04-22 00:09:23409 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50410 // Disallowed in both IDNA 2003 and 2008 with BiDi check.
411 // Labels starting with a RTL character cannot end with a LTR character.
412 {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz",
[email protected]0318f922014-04-22 00:09:23413 "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21),
[email protected]18f00cd2013-09-29 10:52:50414 CanonHostInfo::BROKEN, -1, ""},
415 // Labels starting with a RTL character can end with BC=EN (European
416 // number). Disallowed in IDNA 2003 but now allowed.
417 {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2",
[email protected]0318f922014-04-22 00:09:23418 "xn--2-ymcov", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50419 CanonHostInfo::NEUTRAL, -1, ""},
420 // Labels starting with a RTL character cannot have "L" characters
421 // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
422 {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2",
[email protected]0318f922014-04-22 00:09:23423 "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21),
[email protected]18f00cd2013-09-29 10:52:50424 CanonHostInfo::BROKEN, -1, ""},
425 // Labels starting with a RTL character can end with BC=AN (Arabic number)
426 // Disallowed in IDNA 2003, but now allowed.
427 {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662",
[email protected]0318f922014-04-22 00:09:23428 "xn--mgbjq0r", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50429 CanonHostInfo::NEUTRAL, -1, ""},
430 // Labels starting with a RTL character cannot have "L" characters
431 // even if it ends with an BC=AN (Arabic number).
432 // Disallowed in both IDNA 2003/2008.
433 {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662",
[email protected]0318f922014-04-22 00:09:23434 "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26),
[email protected]18f00cd2013-09-29 10:52:50435 CanonHostInfo::BROKEN, -1, ""},
436 // Labels starting with a RTL character cannot mix BC=EN and BC=AN
437 {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662",
[email protected]0318f922014-04-22 00:09:23438 "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27),
[email protected]18f00cd2013-09-29 10:52:50439 CanonHostInfo::BROKEN, -1, ""},
440 // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
441 {"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com",
[email protected]0318f922014-04-22 00:09:23442 Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50443 // U+0080 is not allowed.
444 {"\xc2\x80.com", L"\x80.com", "%C2%80.com",
[email protected]0318f922014-04-22 00:09:23445 Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50446 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
[email protected]e7bba5f82013-04-10 20:10:52447 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
448 // UTF-8 (wide case). The output should be equivalent to the true wide
449 // character input above).
[email protected]18f00cd2013-09-29 10:52:50450 {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
451 L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
[email protected]0318f922014-04-22 00:09:23452 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52453 // Invalid escaped characters should fail and the percents should be
454 // escaped.
[email protected]0318f922014-04-22 00:09:23455 {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", Component(0, 10),
[email protected]18f00cd2013-09-29 10:52:50456 CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52457 // If we get an invalid character that has been escaped.
[email protected]0318f922014-04-22 00:09:23458 {"%25", L"%25", "%25", Component(0, 3),
[email protected]18f00cd2013-09-29 10:52:50459 CanonHostInfo::BROKEN, -1, ""},
[email protected]0318f922014-04-22 00:09:23460 {"hello%00", L"hello%00", "hello%00", Component(0, 8),
[email protected]18f00cd2013-09-29 10:52:50461 CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52462 // Escaped numbers should be treated like IP addresses if they are.
[email protected]18f00cd2013-09-29 10:52:50463 {"%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:23464 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
[email protected]18f00cd2013-09-29 10:52:50465 "C0A80001"},
466 {"%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:23467 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
[email protected]18f00cd2013-09-29 10:52:50468 "C0A80001"},
[email protected]e7bba5f82013-04-10 20:10:52469 // Invalid escaping should trigger the regular host error handling.
[email protected]0318f922014-04-22 00:09:23470 {"%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:52471 // Something that isn't exactly an IP should get treated as a host and
472 // spaces escaped.
[email protected]0318f922014-04-22 00:09:23473 {"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:52474 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
475 // These are "0Xc0.0250.01" in fullwidth.
[email protected]0318f922014-04-22 00:09:23476 {"\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:52477 // Broken IP addresses get marked as such.
[email protected]0318f922014-04-22 00:09:23478 {"192.168.0.257", L"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
479 {"[google.com]", L"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50480 // Cyrillic letter followed by '(' should return punycode for '(' escaped
481 // before punycode string was created. I.e.
482 // if '(' is escaped after punycode is created we would get xn--%28-8tb
483 // (incorrect).
[email protected]0318f922014-04-22 00:09:23484 {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50485 CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52486 // Address with all hexidecimal characters with leading number of 1<<32
487 // or greater and should return NEUTRAL rather than BROKEN if not all
488 // components are numbers.
[email protected]0318f922014-04-22 00:09:23489 {"12345678912345.de", L"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
490 {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
491 {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
492 {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
493 {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52494 };
495
496 // CanonicalizeHost() non-verbose.
497 std::string out_str;
498 for (size_t i = 0; i < arraysize(host_cases); i++) {
499 // Narrow version.
500 if (host_cases[i].input8) {
501 int host_len = static_cast<int>(strlen(host_cases[i].input8));
[email protected]0318f922014-04-22 00:09:23502 Component in_comp(0, host_len);
503 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52504
505 out_str.clear();
[email protected]0318f922014-04-22 00:09:23506 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52507
[email protected]0318f922014-04-22 00:09:23508 bool success = CanonicalizeHost(host_cases[i].input8, in_comp, &output,
509 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52510 output.Complete();
511
512 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
[email protected]18f00cd2013-09-29 10:52:50513 success) << "for input: " << host_cases[i].input8;
514 EXPECT_EQ(std::string(host_cases[i].expected), out_str) <<
515 "for input: " << host_cases[i].input8;
516 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) <<
517 "for input: " << host_cases[i].input8;
518 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) <<
519 "for input: " << host_cases[i].input8;
[email protected]e7bba5f82013-04-10 20:10:52520 }
521
522 // Wide version.
523 if (host_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:57524 base::string16 input16(WStringToUTF16(host_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52525 int host_len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:23526 Component in_comp(0, host_len);
527 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52528
529 out_str.clear();
[email protected]0318f922014-04-22 00:09:23530 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52531
[email protected]0318f922014-04-22 00:09:23532 bool success = CanonicalizeHost(input16.c_str(), in_comp, &output,
533 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52534 output.Complete();
535
536 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
537 success);
538 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
539 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
540 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
541 }
542 }
543
544 // CanonicalizeHostVerbose()
545 for (size_t i = 0; i < arraysize(host_cases); i++) {
546 // Narrow version.
547 if (host_cases[i].input8) {
548 int host_len = static_cast<int>(strlen(host_cases[i].input8));
[email protected]0318f922014-04-22 00:09:23549 Component in_comp(0, host_len);
[email protected]e7bba5f82013-04-10 20:10:52550
551 out_str.clear();
[email protected]0318f922014-04-22 00:09:23552 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52553 CanonHostInfo host_info;
554
[email protected]0318f922014-04-22 00:09:23555 CanonicalizeHostVerbose(host_cases[i].input8, in_comp, &output,
556 &host_info);
[email protected]e7bba5f82013-04-10 20:10:52557 output.Complete();
558
559 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
560 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
561 EXPECT_EQ(host_cases[i].expected_component.begin,
562 host_info.out_host.begin);
563 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
564 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
565 BytesToHexString(host_info.address, host_info.AddressLength()));
566 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
567 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
568 host_info.num_ipv4_components);
569 }
570 }
571
572 // Wide version.
573 if (host_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:57574 base::string16 input16(WStringToUTF16(host_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52575 int host_len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:23576 Component in_comp(0, host_len);
[email protected]e7bba5f82013-04-10 20:10:52577
578 out_str.clear();
[email protected]0318f922014-04-22 00:09:23579 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52580 CanonHostInfo host_info;
581
[email protected]0318f922014-04-22 00:09:23582 CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52583 output.Complete();
584
585 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
586 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
587 EXPECT_EQ(host_cases[i].expected_component.begin,
588 host_info.out_host.begin);
589 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
590 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
591 BytesToHexString(host_info.address, host_info.AddressLength()));
592 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
593 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
594 host_info.num_ipv4_components);
595 }
596 }
597 }
598}
599
600TEST(URLCanonTest, IPv4) {
601 IPAddressCase cases[] = {
602 // Empty is not an IP address.
[email protected]0318f922014-04-22 00:09:23603 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
604 {".", L".", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52605 // Regular IP addresses in different bases.
[email protected]0318f922014-04-22 00:09:23606 {"192.168.0.1", L"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
607 {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
608 {"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:52609 // Non-IP addresses due to invalid characters.
[email protected]0318f922014-04-22 00:09:23610 {"192.168.9.com", L"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52611 // Invalid characters for the base should be rejected.
[email protected]0318f922014-04-22 00:09:23612 {"19a.168.0.1", L"19a.168.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
613 {"0308.0250.00.01", L"0308.0250.00.01", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
614 {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52615 // If there are not enough components, the last one should fill them out.
[email protected]0318f922014-04-22 00:09:23616 {"192", L"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
617 {"0xC0a80001", L"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
618 {"030052000001", L"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
619 {"000030052000001", L"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
620 {"192.168", L"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
621 {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
622 {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
623 {"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:52624 // Too many components means not an IP address.
[email protected]0318f922014-04-22 00:09:23625 {"192.168.0.0.1", L"192.168.0.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52626 // We allow a single trailing dot.
[email protected]0318f922014-04-22 00:09:23627 {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
628 {"192.168.0.1. hello", L"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
629 {"192.168.0.1..", L"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52630 // Two dots in a row means not an IP address.
[email protected]0318f922014-04-22 00:09:23631 {"192.168..1", L"192.168..1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52632 // Any numerical overflow should be marked as BROKEN.
[email protected]0318f922014-04-22 00:09:23633 {"0x100.0", L"0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
634 {"0x100.0.0", L"0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
635 {"0x100.0.0.0", L"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
636 {"0.0x100.0.0", L"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
637 {"0.0.0x100.0", L"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
638 {"0.0.0.0x100", L"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN, -1, ""},
639 {"0.0.0x10000", L"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
640 {"0.0x1000000", L"0.0x1000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
641 {"0x100000000", L"0x100000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52642 // Repeat the previous tests, minus 1, to verify boundaries.
[email protected]0318f922014-04-22 00:09:23643 {"0xFF.0", L"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
644 {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
645 {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
646 {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
647 {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
648 {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
649 {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
650 {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
651 {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
qyearsley2bc727d2015-08-14 20:17:15652 // Old trunctations tests. They're all "BROKEN" now.
[email protected]0318f922014-04-22 00:09:23653 {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN, -1, ""},
654 {"192.168.0.257", L"192.168.0.257", "", Component(), CanonHostInfo::BROKEN, -1, ""},
655 {"192.168.0xa20001", L"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
656 {"192.015052000001", L"192.015052000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
657 {"0X12C0a80001", L"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
658 {"276.1.2", L"276.1.2", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52659 // Spaces should be rejected.
[email protected]0318f922014-04-22 00:09:23660 {"192.168.0.1 hello", L"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52661 // Very large numbers.
[email protected]0318f922014-04-22 00:09:23662 {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
663 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52664 // A number has no length limit, but long numbers can still overflow.
[email protected]0318f922014-04-22 00:09:23665 {"00000000000000000001", L"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
666 {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52667 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
[email protected]0318f922014-04-22 00:09:23668 {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
669 {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52670 // Truncation of all zeros should still result in 0.
[email protected]0318f922014-04-22 00:09:23671 {"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:52672 };
673
674 for (size_t i = 0; i < arraysize(cases); i++) {
675 // 8-bit version.
[email protected]0318f922014-04-22 00:09:23676 Component component(0, static_cast<int>(strlen(cases[i].input8)));
[email protected]e7bba5f82013-04-10 20:10:52677
678 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23679 StdStringCanonOutput output1(&out_str1);
680 CanonHostInfo host_info;
681 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52682 output1.Complete();
683
684 EXPECT_EQ(cases[i].expected_family, host_info.family);
685 EXPECT_EQ(std::string(cases[i].expected_address_hex),
686 BytesToHexString(host_info.address, host_info.AddressLength()));
687 if (host_info.family == CanonHostInfo::IPV4) {
688 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
689 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
690 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
691 EXPECT_EQ(cases[i].expected_num_ipv4_components,
692 host_info.num_ipv4_components);
693 }
694
695 // 16-bit version.
[email protected]3774f832013-06-11 21:21:57696 base::string16 input16(WStringToUTF16(cases[i].input16));
[email protected]0318f922014-04-22 00:09:23697 component = Component(0, static_cast<int>(input16.length()));
[email protected]e7bba5f82013-04-10 20:10:52698
699 std::string out_str2;
[email protected]0318f922014-04-22 00:09:23700 StdStringCanonOutput output2(&out_str2);
701 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52702 output2.Complete();
703
704 EXPECT_EQ(cases[i].expected_family, host_info.family);
705 EXPECT_EQ(std::string(cases[i].expected_address_hex),
706 BytesToHexString(host_info.address, host_info.AddressLength()));
707 if (host_info.family == CanonHostInfo::IPV4) {
708 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
709 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
710 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
711 EXPECT_EQ(cases[i].expected_num_ipv4_components,
712 host_info.num_ipv4_components);
713 }
714 }
715}
716
717TEST(URLCanonTest, IPv6) {
718 IPAddressCase cases[] = {
719 // Empty is not an IP address.
[email protected]0318f922014-04-22 00:09:23720 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52721 // Non-IPs with [:] characters are marked BROKEN.
[email protected]0318f922014-04-22 00:09:23722 {":", L":", "", Component(), CanonHostInfo::BROKEN, -1, ""},
723 {"[", L"[", "", Component(), CanonHostInfo::BROKEN, -1, ""},
724 {"[:", L"[:", "", Component(), CanonHostInfo::BROKEN, -1, ""},
725 {"]", L"]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
726 {":]", L":]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
727 {"[]", L"[]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
728 {"[:]", L"[:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52729 // Regular IP address is invalid without bounding '[' and ']'.
[email protected]0318f922014-04-22 00:09:23730 {"2001:db8::1", L"2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
731 {"[2001:db8::1", L"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
732 {"2001:db8::1]", L"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52733 // Regular IP addresses.
[email protected]0318f922014-04-22 00:09:23734 {"[::]", L"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
735 {"[::1]", L"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
736 {"[1::]", L"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
[email protected]e7bba5f82013-04-10 20:10:52737
738 // Leading zeros should be stripped.
[email protected]0318f922014-04-22 00:09:23739 {"[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:52740
741 // Upper case letters should be lowercased.
[email protected]0318f922014-04-22 00:09:23742 {"[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:52743
744 // The same address can be written with different contractions, but should
745 // get canonicalized to the same thing.
[email protected]0318f922014-04-22 00:09:23746 {"[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"},
747 {"[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:52748
749 // Addresses with embedded IPv4.
[email protected]0318f922014-04-22 00:09:23750 {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
751 {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
752 {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
753 {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
754 {"[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:52755
756 // IPv4 with last component missing.
[email protected]0318f922014-04-22 00:09:23757 {"[::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:52758
759 // IPv4 using hex.
760 // TODO(eroman): Should this format be disallowed?
[email protected]0318f922014-04-22 00:09:23761 {"[::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:52762
763 // There may be zeros surrounding the "::" contraction.
[email protected]0318f922014-04-22 00:09:23764 {"[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:52765
[email protected]0318f922014-04-22 00:09:23766 {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
[email protected]e7bba5f82013-04-10 20:10:52767
qyearsley2bc727d2015-08-14 20:17:15768 // Can only have one "::" contraction in an IPv6 string literal.
[email protected]0318f922014-04-22 00:09:23769 {"[2001::db8::1]", L"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15770 // No more than 2 consecutive ':'s.
[email protected]0318f922014-04-22 00:09:23771 {"[2001:db8:::1]", L"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
772 {"[:::]", L"[:::]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15773 // Non-IP addresses due to invalid characters.
[email protected]0318f922014-04-22 00:09:23774 {"[2001::.com]", L"[2001::.com]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15775 // If there are not enough components, the last one should fill them out.
[email protected]e7bba5f82013-04-10 20:10:52776 // ... omitted at this time ...
qyearsley2bc727d2015-08-14 20:17:15777 // Too many components means not an IP address. Similarly, with too few
778 // if using IPv4 compat or mapped addresses.
[email protected]0318f922014-04-22 00:09:23779 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
780 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
781 {"[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:52782 // Too many bits (even though 8 comonents, the last one holds 32 bits).
[email protected]0318f922014-04-22 00:09:23783 {"[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:52784
785 // Too many bits specified -- the contraction would have to be zero-length
786 // to not exceed 128 bits.
[email protected]0318f922014-04-22 00:09:23787 {"[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:52788
789 // The contraction is for 16 bits of zero.
[email protected]0318f922014-04-22 00:09:23790 {"[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:52791
792 // Cannot have a trailing colon.
[email protected]0318f922014-04-22 00:09:23793 {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
794 {"[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:52795
796 // Cannot have negative numbers.
[email protected]0318f922014-04-22 00:09:23797 {"[-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:52798
799 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
800 // The scope_id should be included in the canonicalized URL, and is an
801 // unsigned decimal number.
802
803 // Invalid because no ID was given after the percent.
804
805 // Don't allow scope-id
[email protected]0318f922014-04-22 00:09:23806 {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
807 {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
808 {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
809 {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
810 {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52811
812 // Don't allow leading or trailing colons.
[email protected]0318f922014-04-22 00:09:23813 {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
814 {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
815 {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52816
817 // We allow a single trailing dot.
818 // ... omitted at this time ...
819 // Two dots in a row means not an IP address.
[email protected]0318f922014-04-22 00:09:23820 {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52821 // Any non-first components get truncated to one byte.
822 // ... omitted at this time ...
823 // Spaces should be rejected.
[email protected]0318f922014-04-22 00:09:23824 {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52825 };
826
827 for (size_t i = 0; i < arraysize(cases); i++) {
828 // 8-bit version.
[email protected]0318f922014-04-22 00:09:23829 Component component(0, static_cast<int>(strlen(cases[i].input8)));
[email protected]e7bba5f82013-04-10 20:10:52830
831 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23832 StdStringCanonOutput output1(&out_str1);
833 CanonHostInfo host_info;
834 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52835 output1.Complete();
836
837 EXPECT_EQ(cases[i].expected_family, host_info.family);
838 EXPECT_EQ(std::string(cases[i].expected_address_hex),
839 BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
840 if (host_info.family == CanonHostInfo::IPV6) {
841 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
842 EXPECT_EQ(cases[i].expected_component.begin,
843 host_info.out_host.begin);
844 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
845 }
846
847 // 16-bit version.
[email protected]3774f832013-06-11 21:21:57848 base::string16 input16(WStringToUTF16(cases[i].input16));
[email protected]0318f922014-04-22 00:09:23849 component = Component(0, static_cast<int>(input16.length()));
[email protected]e7bba5f82013-04-10 20:10:52850
851 std::string out_str2;
[email protected]0318f922014-04-22 00:09:23852 StdStringCanonOutput output2(&out_str2);
853 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52854 output2.Complete();
855
856 EXPECT_EQ(cases[i].expected_family, host_info.family);
857 EXPECT_EQ(std::string(cases[i].expected_address_hex),
858 BytesToHexString(host_info.address, host_info.AddressLength()));
859 if (host_info.family == CanonHostInfo::IPV6) {
860 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
861 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
862 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
863 }
864 }
865}
866
867TEST(URLCanonTest, IPEmpty) {
868 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23869 StdStringCanonOutput output1(&out_str1);
870 CanonHostInfo host_info;
[email protected]e7bba5f82013-04-10 20:10:52871
872 // This tests tests.
873 const char spec[] = "192.168.0.1";
[email protected]0318f922014-04-22 00:09:23874 CanonicalizeIPAddress(spec, Component(), &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52875 EXPECT_FALSE(host_info.IsIPAddress());
876
[email protected]0318f922014-04-22 00:09:23877 CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52878 EXPECT_FALSE(host_info.IsIPAddress());
879}
880
881TEST(URLCanonTest, UserInfo) {
882 // Note that the canonicalizer should escape and treat empty components as
883 // not being there.
884
885 // We actually parse a full input URL so we can get the initial components.
886 struct UserComponentCase {
887 const char* input;
888 const char* expected;
[email protected]0318f922014-04-22 00:09:23889 Component expected_username;
890 Component expected_password;
[email protected]e7bba5f82013-04-10 20:10:52891 bool expected_success;
892 } user_info_cases[] = {
[email protected]0318f922014-04-22 00:09:23893 {"http://user:[email protected]/", "user:pass@", Component(0, 4), Component(5, 4), true},
894 {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
895 {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
896 {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
897 {"http://:[email protected]/", ":foo@", Component(0, 0), Component(1, 3), true},
898 {"http://^ :$\[email protected]/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
899 {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
900 {"http://%2540:[email protected]/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
[email protected]e7bba5f82013-04-10 20:10:52901
qyearsley2bc727d2015-08-14 20:17:15902 // IE7 compatibility: old versions allowed backslashes in usernames, but
[email protected]e7bba5f82013-04-10 20:10:52903 // IE7 does not. We disallow it as well.
[email protected]0318f922014-04-22 00:09:23904 {"ftp://me\\mydomain:[email protected]/", "", Component(0, -1), Component(0, -1), true},
[email protected]e7bba5f82013-04-10 20:10:52905 };
906
viettrungluu4b6915862014-10-16 03:42:49907 for (size_t i = 0; i < arraysize(user_info_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52908 int url_len = static_cast<int>(strlen(user_info_cases[i].input));
[email protected]0318f922014-04-22 00:09:23909 Parsed parsed;
910 ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
911 Component out_user, out_pass;
[email protected]e7bba5f82013-04-10 20:10:52912 std::string out_str;
[email protected]0318f922014-04-22 00:09:23913 StdStringCanonOutput output1(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52914
[email protected]0318f922014-04-22 00:09:23915 bool success = CanonicalizeUserInfo(user_info_cases[i].input,
916 parsed.username,
917 user_info_cases[i].input,
918 parsed.password,
919 &output1,
920 &out_user,
921 &out_pass);
[email protected]e7bba5f82013-04-10 20:10:52922 output1.Complete();
923
924 EXPECT_EQ(user_info_cases[i].expected_success, success);
925 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
926 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
927 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
928 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
929 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
930
931 // Now try the wide version
932 out_str.clear();
[email protected]0318f922014-04-22 00:09:23933 StdStringCanonOutput output2(&out_str);
[email protected]3774f832013-06-11 21:21:57934 base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
[email protected]0318f922014-04-22 00:09:23935 success = CanonicalizeUserInfo(wide_input.c_str(),
936 parsed.username,
937 wide_input.c_str(),
938 parsed.password,
939 &output2,
940 &out_user,
941 &out_pass);
[email protected]e7bba5f82013-04-10 20:10:52942 output2.Complete();
943
944 EXPECT_EQ(user_info_cases[i].expected_success, success);
945 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
946 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
947 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
948 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
949 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
950 }
951}
952
953TEST(URLCanonTest, Port) {
954 // We only need to test that the number gets properly put into the output
955 // buffer. The parser unit tests will test scanning the number correctly.
956 //
957 // Note that the CanonicalizePort will always prepend a colon to the output
qyearsley2bc727d2015-08-14 20:17:15958 // to separate it from the colon that it assumes precedes it.
[email protected]e7bba5f82013-04-10 20:10:52959 struct PortCase {
960 const char* input;
961 int default_port;
962 const char* expected;
[email protected]0318f922014-04-22 00:09:23963 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:52964 bool expected_success;
965 } port_cases[] = {
966 // Invalid input should be copied w/ failure.
[email protected]0318f922014-04-22 00:09:23967 {"as df", 80, ":as%20df", Component(1, 7), false},
968 {"-2", 80, ":-2", Component(1, 2), false},
[email protected]e7bba5f82013-04-10 20:10:52969 // Default port should be omitted.
[email protected]0318f922014-04-22 00:09:23970 {"80", 80, "", Component(0, -1), true},
971 {"8080", 80, ":8080", Component(1, 4), true},
[email protected]e7bba5f82013-04-10 20:10:52972 // PORT_UNSPECIFIED should mean always keep the port.
[email protected]0318f922014-04-22 00:09:23973 {"80", PORT_UNSPECIFIED, ":80", Component(1, 2), true},
[email protected]e7bba5f82013-04-10 20:10:52974 };
975
viettrungluu4b6915862014-10-16 03:42:49976 for (size_t i = 0; i < arraysize(port_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52977 int url_len = static_cast<int>(strlen(port_cases[i].input));
[email protected]0318f922014-04-22 00:09:23978 Component in_comp(0, url_len);
979 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52980 std::string out_str;
[email protected]0318f922014-04-22 00:09:23981 StdStringCanonOutput output1(&out_str);
982 bool success = CanonicalizePort(port_cases[i].input,
983 in_comp,
984 port_cases[i].default_port,
985 &output1,
986 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52987 output1.Complete();
988
989 EXPECT_EQ(port_cases[i].expected_success, success);
990 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
991 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
992 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
993
994 // Now try the wide version
995 out_str.clear();
[email protected]0318f922014-04-22 00:09:23996 StdStringCanonOutput output2(&out_str);
[email protected]3774f832013-06-11 21:21:57997 base::string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
[email protected]0318f922014-04-22 00:09:23998 success = CanonicalizePort(wide_input.c_str(),
999 in_comp,
1000 port_cases[i].default_port,
1001 &output2,
1002 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521003 output2.Complete();
1004
1005 EXPECT_EQ(port_cases[i].expected_success, success);
1006 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
1007 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
1008 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
1009 }
1010}
1011
1012TEST(URLCanonTest, Path) {
1013 DualComponentCase path_cases[] = {
1014 // ----- path collapsing tests -----
[email protected]0318f922014-04-22 00:09:231015 {"/././foo", L"/././foo", "/foo", Component(0, 4), true},
1016 {"/./.foo", L"/./.foo", "/.foo", Component(0, 5), true},
1017 {"/foo/.", L"/foo/.", "/foo/", Component(0, 5), true},
1018 {"/foo/./", L"/foo/./", "/foo/", Component(0, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521019 // double dots followed by a slash or the end of the string count
[email protected]0318f922014-04-22 00:09:231020 {"/foo/bar/..", L"/foo/bar/..", "/foo/", Component(0, 5), true},
1021 {"/foo/bar/../", L"/foo/bar/../", "/foo/", Component(0, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521022 // don't count double dots when they aren't followed by a slash
[email protected]0318f922014-04-22 00:09:231023 {"/foo/..bar", L"/foo/..bar", "/foo/..bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521024 // some in the middle
[email protected]0318f922014-04-22 00:09:231025 {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", Component(0, 8), true},
1026 {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", Component(0, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521027 // we should not be able to go above the root
[email protected]0318f922014-04-22 00:09:231028 {"/foo/../../..", L"/foo/../../..", "/", Component(0, 1), true},
1029 {"/foo/../../../ton", L"/foo/../../../ton", "/ton", Component(0, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521030 // escaped dots should be unescaped and treated the same as dots
[email protected]0318f922014-04-22 00:09:231031 {"/foo/%2e", L"/foo/%2e", "/foo/", Component(0, 5), true},
1032 {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", Component(0, 8), true},
1033 {"/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:521034 // Multiple slashes in a row should be preserved and treated like empty
1035 // directory names.
[email protected]0318f922014-04-22 00:09:231036 {"////../..", L"////../..", "//", Component(0, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521037
1038 // ----- escaping tests -----
[email protected]0318f922014-04-22 00:09:231039 {"/foo", L"/foo", "/foo", Component(0, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521040 // Valid escape sequence
[email protected]0318f922014-04-22 00:09:231041 {"/%20foo", L"/%20foo", "/%20foo", Component(0, 7), true},
[email protected]e7bba5f82013-04-10 20:10:521042 // Invalid escape sequence we should pass through unchanged.
[email protected]0318f922014-04-22 00:09:231043 {"/foo%", L"/foo%", "/foo%", Component(0, 5), true},
1044 {"/foo%2", L"/foo%2", "/foo%2", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521045 // Invalid escape sequence: bad characters should be treated the same as
1046 // the sourrounding text, not as escaped (in this case, UTF-8).
[email protected]0318f922014-04-22 00:09:231047 {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", Component(0, 10), true},
1048 {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", Component(0, 16), true},
1049 {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22), true},
[email protected]e7bba5f82013-04-10 20:10:521050 // Regular characters that are escaped should be unescaped
[email protected]0318f922014-04-22 00:09:231051 {"/foo%41%7a", L"/foo%41%7a", "/fooAz", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521052 // Funny characters that are unescaped should be escaped
[email protected]0318f922014-04-22 00:09:231053 {"/foo\x09\x91%91", NULL, "/foo%09%91%91", Component(0, 13), true},
1054 {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true},
[email protected]e7bba5f82013-04-10 20:10:521055 // Invalid characters that are escaped should cause a failure.
[email protected]0318f922014-04-22 00:09:231056 {"/foo%00%51", L"/foo%00%51", "/foo%00Q", Component(0, 8), false},
[email protected]e7bba5f82013-04-10 20:10:521057 // Some characters should be passed through unchanged regardless of esc.
[email protected]0318f922014-04-22 00:09:231058 {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13), true},
[email protected]e7bba5f82013-04-10 20:10:521059 // Characters that are properly escaped should not have the case changed
1060 // of hex letters.
[email protected]0318f922014-04-22 00:09:231061 {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13), true},
[email protected]e7bba5f82013-04-10 20:10:521062 // Funny characters that are unescaped should be escaped
[email protected]0318f922014-04-22 00:09:231063 {"/foo\tbar", L"/foo\tbar", "/foo%09bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521064 // Backslashes should get converted to forward slashes
[email protected]0318f922014-04-22 00:09:231065 {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", Component(0, 8), true},
[email protected]e7bba5f82013-04-10 20:10:521066 // Hashes found in paths (possibly only when the caller explicitly sets
1067 // the path on an already-parsed URL) should be escaped.
[email protected]0318f922014-04-22 00:09:231068 {"/foo#bar", L"/foo#bar", "/foo%23bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521069 // %7f should be allowed and %3D should not be unescaped (these were wrong
1070 // in a previous version).
[email protected]0318f922014-04-22 00:09:231071 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true},
[email protected]e7bba5f82013-04-10 20:10:521072 // @ should be passed through unchanged (escaped or unescaped).
[email protected]0318f922014-04-22 00:09:231073 {"/@asdf%40", L"/@asdf%40", "/@asdf%40", Component(0, 9), true},
pkasting69820362015-09-22 01:54:051074 // Nested escape sequences should result in escaping the leading '%' if
1075 // unescaping would result in a new escape sequence.
1076 {"/%A%42", L"/%A%42", "/%25AB", Component(0, 6), true},
1077 {"/%%41B", L"/%%41B", "/%25AB", Component(0, 6), true},
1078 {"/%%41%42", L"/%%41%42", "/%25AB", Component(0, 6), true},
1079 // Make sure truncated "nested" escapes don't result in reading off the
1080 // string end.
1081 {"/%%41", L"/%%41", "/%A", Component(0, 3), true},
1082 // Don't unescape the leading '%' if unescaping doesn't result in a valid
1083 // new escape sequence.
1084 {"/%%470", L"/%%470", "/%G0", Component(0, 4), true},
1085 {"/%%2D%41", L"/%%2D%41", "/%-A", Component(0, 4), true},
1086 // Don't erroneously downcast a UTF-16 charater in a way that makes it
1087 // look like part of an escape sequence.
1088 {NULL, L"/%%41\x0130", "/%A%C4%B0", Component(0, 9), true},
[email protected]e7bba5f82013-04-10 20:10:521089
1090 // ----- encoding tests -----
1091 // Basic conversions
[email protected]0318f922014-04-22 00:09:231092 {"/\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:521093 // Invalid unicode characters should fail. We only do validation on
1094 // UTF-16 input, so this doesn't happen on 8-bit.
[email protected]0318f922014-04-22 00:09:231095 {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", Component(0, 13), true},
1096 {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false},
[email protected]e7bba5f82013-04-10 20:10:521097 };
1098
1099 for (size_t i = 0; i < arraysize(path_cases); i++) {
1100 if (path_cases[i].input8) {
1101 int len = static_cast<int>(strlen(path_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231102 Component in_comp(0, len);
1103 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521104 std::string out_str;
[email protected]0318f922014-04-22 00:09:231105 StdStringCanonOutput output(&out_str);
1106 bool success =
1107 CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521108 output.Complete();
1109
1110 EXPECT_EQ(path_cases[i].expected_success, success);
1111 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1112 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1113 EXPECT_EQ(path_cases[i].expected, out_str);
1114 }
1115
1116 if (path_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571117 base::string16 input16(WStringToUTF16(path_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521118 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231119 Component in_comp(0, len);
1120 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521121 std::string out_str;
[email protected]0318f922014-04-22 00:09:231122 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:521123
[email protected]0318f922014-04-22 00:09:231124 bool success =
1125 CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521126 output.Complete();
1127
1128 EXPECT_EQ(path_cases[i].expected_success, success);
1129 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1130 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1131 EXPECT_EQ(path_cases[i].expected, out_str);
1132 }
1133 }
1134
1135 // Manual test: embedded NULLs should be escaped and the URL should be marked
1136 // as invalid.
1137 const char path_with_null[] = "/ab\0c";
[email protected]0318f922014-04-22 00:09:231138 Component in_comp(0, 5);
1139 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521140
1141 std::string out_str;
[email protected]0318f922014-04-22 00:09:231142 StdStringCanonOutput output(&out_str);
1143 bool success = CanonicalizePath(path_with_null, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521144 output.Complete();
1145 EXPECT_FALSE(success);
1146 EXPECT_EQ("/ab%00c", out_str);
1147}
1148
1149TEST(URLCanonTest, Query) {
1150 struct QueryCase {
1151 const char* input8;
1152 const wchar_t* input16;
[email protected]e7bba5f82013-04-10 20:10:521153 const char* expected;
1154 } query_cases[] = {
[email protected]847aaab82014-05-07 14:05:461155 // Regular ASCII case.
1156 {"foo=bar", L"foo=bar", "?foo=bar"},
[email protected]e7bba5f82013-04-10 20:10:521157 // Allow question marks in the query without escaping
[email protected]847aaab82014-05-07 14:05:461158 {"as?df", L"as?df", "?as?df"},
[email protected]e7bba5f82013-04-10 20:10:521159 // Always escape '#' since it would mark the ref.
[email protected]847aaab82014-05-07 14:05:461160 {"as#df", L"as#df", "?as%23df"},
[email protected]e7bba5f82013-04-10 20:10:521161 // Escape some questionable 8-bit characters, but never unescape.
[email protected]847aaab82014-05-07 14:05:461162 {"\x02hello\x7f bye", L"\x02hello\x7f bye", "?%02hello%7F%20bye"},
1163 {"%40%41123", L"%40%41123", "?%40%41123"},
[email protected]e7bba5f82013-04-10 20:10:521164 // Chinese input/output
[email protected]847aaab82014-05-07 14:05:461165 {"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:521166 // Invalid UTF-8/16 input should be replaced with invalid characters.
[email protected]847aaab82014-05-07 14:05:461167 {"q=\xed\xed", L"q=\xd800\xd800", "?q=%EF%BF%BD%EF%BF%BD"},
[email protected]e7bba5f82013-04-10 20:10:521168 // Don't allow < or > because sometimes they are used for XSS if the
1169 // URL is echoed in content. Firefox does this, IE doesn't.
[email protected]847aaab82014-05-07 14:05:461170 {"q=<asdf>", L"q=<asdf>", "?q=%3Casdf%3E"},
[email protected]e7bba5f82013-04-10 20:10:521171 // Escape double quotemarks in the query.
[email protected]847aaab82014-05-07 14:05:461172 {"q=\"asdf\"", L"q=\"asdf\"", "?q=%22asdf%22"},
[email protected]e7bba5f82013-04-10 20:10:521173 };
1174
viettrungluu4b6915862014-10-16 03:42:491175 for (size_t i = 0; i < arraysize(query_cases); i++) {
[email protected]0318f922014-04-22 00:09:231176 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521177
[email protected]e7bba5f82013-04-10 20:10:521178 if (query_cases[i].input8) {
1179 int len = static_cast<int>(strlen(query_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231180 Component in_comp(0, len);
[email protected]e7bba5f82013-04-10 20:10:521181 std::string out_str;
1182
[email protected]0318f922014-04-22 00:09:231183 StdStringCanonOutput output(&out_str);
[email protected]847aaab82014-05-07 14:05:461184 CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output,
[email protected]0318f922014-04-22 00:09:231185 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521186 output.Complete();
1187
1188 EXPECT_EQ(query_cases[i].expected, out_str);
1189 }
1190
1191 if (query_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571192 base::string16 input16(WStringToUTF16(query_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521193 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231194 Component in_comp(0, len);
[email protected]e7bba5f82013-04-10 20:10:521195 std::string out_str;
1196
[email protected]0318f922014-04-22 00:09:231197 StdStringCanonOutput output(&out_str);
[email protected]847aaab82014-05-07 14:05:461198 CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521199 output.Complete();
1200
1201 EXPECT_EQ(query_cases[i].expected, out_str);
1202 }
1203 }
1204
1205 // Extra test for input with embedded NULL;
1206 std::string out_str;
[email protected]0318f922014-04-22 00:09:231207 StdStringCanonOutput output(&out_str);
1208 Component out_comp;
1209 CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521210 output.Complete();
1211 EXPECT_EQ("?a%20%00z%01", out_str);
1212}
1213
1214TEST(URLCanonTest, Ref) {
1215 // Refs are trivial, it just checks the encoding.
1216 DualComponentCase ref_cases[] = {
1217 // Regular one, we shouldn't escape spaces, et al.
[email protected]0318f922014-04-22 00:09:231218 {"hello, world", L"hello, world", "#hello, world", Component(1, 12), true},
[email protected]e7bba5f82013-04-10 20:10:521219 // UTF-8/wide input should be preserved
[email protected]0318f922014-04-22 00:09:231220 {"\xc2\xa9", L"\xa9", "#\xc2\xa9", Component(1, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521221 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
[email protected]0318f922014-04-22 00:09:231222 {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", Component(1, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521223 // Escaping should be preserved unchanged, even invalid ones
[email protected]0318f922014-04-22 00:09:231224 {"%41%a", L"%41%a", "#%41%a", Component(1, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521225 // Invalid UTF-8/16 input should be flagged and the input made valid
[email protected]0318f922014-04-22 00:09:231226 {"\xc2", NULL, "#\xef\xbf\xbd", Component(1, 3), true},
1227 {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", Component(1, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521228 // Test a Unicode invalid character.
[email protected]0318f922014-04-22 00:09:231229 {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", Component(1, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521230 // Refs can have # signs and we should preserve them.
[email protected]0318f922014-04-22 00:09:231231 {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", Component(1, 9), true},
1232 {"#asdf", L"#asdf", "##asdf", Component(1, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521233 };
1234
1235 for (size_t i = 0; i < arraysize(ref_cases); i++) {
1236 // 8-bit input
1237 if (ref_cases[i].input8) {
1238 int len = static_cast<int>(strlen(ref_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231239 Component in_comp(0, len);
1240 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521241
1242 std::string out_str;
[email protected]0318f922014-04-22 00:09:231243 StdStringCanonOutput output(&out_str);
1244 CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521245 output.Complete();
1246
1247 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1248 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1249 EXPECT_EQ(ref_cases[i].expected, out_str);
1250 }
1251
1252 // 16-bit input
1253 if (ref_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571254 base::string16 input16(WStringToUTF16(ref_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521255 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231256 Component in_comp(0, len);
1257 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521258
1259 std::string out_str;
[email protected]0318f922014-04-22 00:09:231260 StdStringCanonOutput output(&out_str);
1261 CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521262 output.Complete();
1263
1264 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1265 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1266 EXPECT_EQ(ref_cases[i].expected, out_str);
1267 }
1268 }
1269
1270 // Try one with an embedded NULL. It should be stripped.
1271 const char null_input[5] = "ab\x00z";
[email protected]0318f922014-04-22 00:09:231272 Component null_input_component(0, 4);
1273 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521274
1275 std::string out_str;
[email protected]0318f922014-04-22 00:09:231276 StdStringCanonOutput output(&out_str);
1277 CanonicalizeRef(null_input, null_input_component, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521278 output.Complete();
1279
1280 EXPECT_EQ(1, out_comp.begin);
1281 EXPECT_EQ(3, out_comp.len);
1282 EXPECT_EQ("#abz", out_str);
1283}
1284
1285TEST(URLCanonTest, CanonicalizeStandardURL) {
1286 // The individual component canonicalize tests should have caught the cases
1287 // for each of those components. Here, we just need to test that the various
1288 // parts are included or excluded properly, and have the correct separators.
1289 struct URLCase {
1290 const char* input;
1291 const char* expected;
1292 bool expected_success;
1293 } cases[] = {
1294 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1295 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1296 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1297 {"http:////////user:@google.com:99?foo", "http://[email protected]:99/?foo", true},
1298 {"www.google.com", ":www.google.com/", true},
1299 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1300 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1301 {"http://user:pass@/", "http://user:pass@/", false},
1302 {"http://%25DOMAIN:[email protected]/", "http://%25DOMAIN:[email protected]/", true},
1303
1304 // Backslashes should get converted to forward slashes.
1305 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1306
1307 // Busted refs shouldn't make the whole thing fail.
1308 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1309
1310 // Basic port tests.
1311 {"http://foo:80/", "http://foo/", true},
1312 {"http://foo:81/", "http://foo:81/", true},
1313 {"httpa://foo:80/", "httpa://foo:80/", true},
1314 {"http://foo:-80/", "http://foo:-80/", false},
1315
1316 {"https://foo:443/", "https://foo/", true},
1317 {"https://foo:80/", "https://foo:80/", true},
1318 {"ftp://foo:21/", "ftp://foo/", true},
1319 {"ftp://foo:80/", "ftp://foo:80/", true},
1320 {"gopher://foo:70/", "gopher://foo/", true},
1321 {"gopher://foo:443/", "gopher://foo:443/", true},
1322 {"ws://foo:80/", "ws://foo/", true},
1323 {"ws://foo:81/", "ws://foo:81/", true},
1324 {"ws://foo:443/", "ws://foo:443/", true},
1325 {"ws://foo:815/", "ws://foo:815/", true},
1326 {"wss://foo:80/", "wss://foo:80/", true},
1327 {"wss://foo:81/", "wss://foo:81/", true},
1328 {"wss://foo:443/", "wss://foo/", true},
1329 {"wss://foo:815/", "wss://foo:815/", true},
brettw1141951b2015-11-26 00:29:351330
1331 // This particular code path ends up "backing up" to replace an invalid
1332 // host ICU generated with an escaped version. Test that in the context
1333 // of a full URL to make sure the backing up doesn't mess up the non-host
1334 // parts of the URL. "EF B9 AA" is U+FE6A which is a type of percent that
1335 // ICU will convert to an ASCII one, generating "%81".
1336 {"ws:)W\x1eW\xef\xb9\xaa""81:80/", "ws://%29w%1ew%81/", false},
[email protected]e7bba5f82013-04-10 20:10:521337 };
1338
viettrungluu4b6915862014-10-16 03:42:491339 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521340 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231341 Parsed parsed;
1342 ParseStandardURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521343
[email protected]0318f922014-04-22 00:09:231344 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521345 std::string out_str;
[email protected]0318f922014-04-22 00:09:231346 StdStringCanonOutput output(&out_str);
1347 bool success = CanonicalizeStandardURL(
[email protected]e7bba5f82013-04-10 20:10:521348 cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
1349 output.Complete();
1350
1351 EXPECT_EQ(cases[i].expected_success, success);
1352 EXPECT_EQ(cases[i].expected, out_str);
1353 }
1354}
1355
1356// The codepath here is the same as for regular canonicalization, so we just
1357// need to test that things are replaced or not correctly.
1358TEST(URLCanonTest, ReplaceStandardURL) {
1359 ReplaceCase replace_cases[] = {
1360 // Common case of truncating the path.
1361 {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
1362 // Replace everything
1363 {"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"},
1364 // Replace nothing
1365 {"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:151366 // Replace scheme with filesystem. The result is garbage, but you asked
[email protected]e7bba5f82013-04-10 20:10:521367 // for it.
1368 {"http://a:[email protected]:22/foo?baz@cat", "filesystem", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem://a:[email protected]:22/foo?baz@cat"},
1369 };
1370
1371 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1372 const ReplaceCase& cur = replace_cases[i];
1373 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231374 Parsed parsed;
1375 ParseStandardURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521376
[email protected]0318f922014-04-22 00:09:231377 Replacements<char> r;
1378 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521379
1380 // Note that for the scheme we pass in a different clear function since
1381 // there is no function to clear the scheme.
1382 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1383 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1384 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1385 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1386 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1387 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1388 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1389 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1390
1391 std::string out_str;
[email protected]0318f922014-04-22 00:09:231392 StdStringCanonOutput output(&out_str);
1393 Parsed out_parsed;
1394 ReplaceStandardURL(replace_cases[i].base, parsed, r, NULL, &output,
1395 &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521396 output.Complete();
1397
1398 EXPECT_EQ(replace_cases[i].expected, out_str);
1399 }
1400
1401 // The path pointer should be ignored if the address is invalid.
1402 {
1403 const char src[] = "http://www.google.com/here_is_the_path";
1404 int src_len = static_cast<int>(strlen(src));
1405
[email protected]0318f922014-04-22 00:09:231406 Parsed parsed;
1407 ParseStandardURL(src, src_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521408
1409 // Replace the path to 0 length string. By using 1 as the string address,
1410 // the test should get an access violation if it tries to dereference it.
[email protected]0318f922014-04-22 00:09:231411 Replacements<char> r;
1412 r.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0));
[email protected]e7bba5f82013-04-10 20:10:521413 std::string out_str1;
[email protected]0318f922014-04-22 00:09:231414 StdStringCanonOutput output1(&out_str1);
1415 Parsed new_parsed;
1416 ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
[email protected]e7bba5f82013-04-10 20:10:521417 output1.Complete();
1418 EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
1419
1420 // Same with an "invalid" path.
[email protected]0318f922014-04-22 00:09:231421 r.SetPath(reinterpret_cast<char*>(0x00000001), Component());
[email protected]e7bba5f82013-04-10 20:10:521422 std::string out_str2;
[email protected]0318f922014-04-22 00:09:231423 StdStringCanonOutput output2(&out_str2);
1424 ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
[email protected]e7bba5f82013-04-10 20:10:521425 output2.Complete();
1426 EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
1427 }
1428}
1429
1430TEST(URLCanonTest, ReplaceFileURL) {
1431 ReplaceCase replace_cases[] = {
1432 // Replace everything
1433 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1434 // Replace nothing
1435 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1436 // Clear non-path components (common)
1437 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
1438 // Replace path with something that doesn't begin with a slash and make
1439 // sure it gets added properly.
1440 {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1441 {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1442 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
1443 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
1444 {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1445 // Replace scheme -- shouldn't do anything.
1446 {"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1447 };
1448
1449 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1450 const ReplaceCase& cur = replace_cases[i];
1451 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231452 Parsed parsed;
1453 ParseFileURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521454
[email protected]0318f922014-04-22 00:09:231455 Replacements<char> r;
1456 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521457 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1458 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1459 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1460 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1461 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1462 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1463 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1464 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1465
1466 std::string out_str;
[email protected]0318f922014-04-22 00:09:231467 StdStringCanonOutput output(&out_str);
1468 Parsed out_parsed;
1469 ReplaceFileURL(cur.base, parsed, r, NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521470 output.Complete();
1471
1472 EXPECT_EQ(replace_cases[i].expected, out_str);
1473 }
1474}
1475
1476TEST(URLCanonTest, ReplaceFileSystemURL) {
1477 ReplaceCase replace_cases[] = {
1478 // Replace everything in the outer URL.
1479 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1480 // Replace nothing
1481 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"},
1482 // Clear non-path components (common)
1483 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"},
1484 // Replace path with something that doesn't begin with a slash and make
1485 // sure it gets added properly.
1486 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"},
1487 // Replace scheme -- shouldn't do anything.
1488 {"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"},
1489 // Replace username -- shouldn't do anything.
1490 {"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"},
1491 // Replace password -- shouldn't do anything.
1492 {"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"},
1493 // Replace host -- shouldn't do anything.
1494 {"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"},
1495 // Replace port -- shouldn't do anything.
1496 {"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"},
1497 };
1498
1499 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1500 const ReplaceCase& cur = replace_cases[i];
1501 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231502 Parsed parsed;
1503 ParseFileSystemURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521504
[email protected]0318f922014-04-22 00:09:231505 Replacements<char> r;
1506 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521507 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1508 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1509 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1510 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1511 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1512 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1513 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1514 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1515
1516 std::string out_str;
[email protected]0318f922014-04-22 00:09:231517 StdStringCanonOutput output(&out_str);
1518 Parsed out_parsed;
1519 ReplaceFileSystemURL(cur.base, parsed, r, NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521520 output.Complete();
1521
1522 EXPECT_EQ(replace_cases[i].expected, out_str);
1523 }
1524}
1525
1526TEST(URLCanonTest, ReplacePathURL) {
1527 ReplaceCase replace_cases[] = {
1528 // Replace everything
1529 {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
1530 // Replace nothing
1531 {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
1532 // Replace one or the other
1533 {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
1534 {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
1535 {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
1536 };
1537
1538 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1539 const ReplaceCase& cur = replace_cases[i];
1540 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231541 Parsed parsed;
1542 ParsePathURL(cur.base, base_len, false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521543
[email protected]0318f922014-04-22 00:09:231544 Replacements<char> r;
1545 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521546 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1547 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1548 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1549 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1550 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1551 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1552 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1553 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1554
1555 std::string out_str;
[email protected]0318f922014-04-22 00:09:231556 StdStringCanonOutput output(&out_str);
1557 Parsed out_parsed;
1558 ReplacePathURL(cur.base, parsed, r, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521559 output.Complete();
1560
1561 EXPECT_EQ(replace_cases[i].expected, out_str);
1562 }
1563}
1564
1565TEST(URLCanonTest, ReplaceMailtoURL) {
1566 ReplaceCase replace_cases[] = {
1567 // Replace everything
1568 {"mailto:[email protected]?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
1569 // Replace nothing
1570 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:[email protected]?body=sup"},
1571 // Replace the path
1572 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
1573 // Replace the query
1574 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:[email protected]?custom=1"},
1575 // Replace the path and query
1576 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
1577 // Set the query to empty (should leave trailing question mark)
1578 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:[email protected]?"},
1579 // Clear the query
1580 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:[email protected]"},
1581 // Clear the path
1582 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
1583 // Clear the path + query
1584 {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
1585 // Setting the ref should have no effect
1586 {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
1587 };
1588
1589 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1590 const ReplaceCase& cur = replace_cases[i];
1591 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231592 Parsed parsed;
1593 ParseMailtoURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521594
[email protected]0318f922014-04-22 00:09:231595 Replacements<char> r;
1596 typedef Replacements<char> R;
[email protected]e7bba5f82013-04-10 20:10:521597 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1598 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1599 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1600 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1601 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1602 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1603 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1604 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1605
1606 std::string out_str;
[email protected]0318f922014-04-22 00:09:231607 StdStringCanonOutput output(&out_str);
1608 Parsed out_parsed;
1609 ReplaceMailtoURL(cur.base, parsed, r, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521610 output.Complete();
1611
1612 EXPECT_EQ(replace_cases[i].expected, out_str);
1613 }
1614}
1615
1616TEST(URLCanonTest, CanonicalizeFileURL) {
1617 struct URLCase {
1618 const char* input;
1619 const char* expected;
1620 bool expected_success;
[email protected]0318f922014-04-22 00:09:231621 Component expected_host;
1622 Component expected_path;
[email protected]e7bba5f82013-04-10 20:10:521623 } cases[] = {
1624#ifdef _WIN32
1625 // Windows-style paths
[email protected]0318f922014-04-22 00:09:231626 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1627 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1628 {"file:", "file:///", true, Component(), Component(7, 1)},
1629 {"file:UNChost/path", "file://unchost/path", true, Component(7, 7), Component(14, 5)},
[email protected]e7bba5f82013-04-10 20:10:521630 // CanonicalizeFileURL supports absolute Windows style paths for IE
qyearsley2bc727d2015-08-14 20:17:151631 // compatibility. Note that the caller must decide that this is a file
[email protected]e7bba5f82013-04-10 20:10:521632 // URL itself so it can call the file canonicalizer. This is usually
1633 // done automatically as part of relative URL resolving.
[email protected]0318f922014-04-22 00:09:231634 {"c:\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1635 {"C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1636 {"/C|\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1637 {"//C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
1638 {"//server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1639 {"\\\\server\\file", "file://server/file", true, Component(7, 6), Component(13, 5)},
1640 {"/\\server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
[email protected]e7bba5f82013-04-10 20:10:521641 // We should preserve the number of slashes after the colon for IE
qyearsley2bc727d2015-08-14 20:17:151642 // compatibility, except when there is none, in which case we should
[email protected]e7bba5f82013-04-10 20:10:521643 // add one.
[email protected]0318f922014-04-22 00:09:231644 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1645 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
[email protected]e7bba5f82013-04-10 20:10:521646 // Three slashes should be non-UNC, even if there is no drive spec (IE
1647 // does this, which makes the resulting request invalid).
[email protected]0318f922014-04-22 00:09:231648 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(), Component(7, 12)},
[email protected]e7bba5f82013-04-10 20:10:521649 // TODO(brettw) we should probably fail for invalid host names, which
1650 // would change the expected result on this test. We also currently allow
1651 // colon even though it's probably invalid, because its currently the
1652 // "natural" result of the way the canonicalizer is written. There doesn't
1653 // seem to be a strong argument for why allowing it here would be bad, so
1654 // we just tolerate it and the load will fail later.
[email protected]0318f922014-04-22 00:09:231655 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, Component(7, 2), Component(9, 16)},
1656 {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5), Component(12, 8)},
[email protected]e7bba5f82013-04-10 20:10:521657 // Make sure relative paths can't go above the "C:"
[email protected]0318f922014-04-22 00:09:231658 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, Component(), Component(7, 12)},
[email protected]e7bba5f82013-04-10 20:10:521659 // Busted refs shouldn't make the whole thing fail.
[email protected]0318f922014-04-22 00:09:231660 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, Component(), Component(7, 8)},
[email protected]e7bba5f82013-04-10 20:10:521661#else
1662 // Unix-style paths
[email protected]0318f922014-04-22 00:09:231663 {"file:///home/me", "file:///home/me", true, Component(), Component(7, 8)},
[email protected]e7bba5f82013-04-10 20:10:521664 // Windowsy ones should get still treated as Unix-style.
[email protected]0318f922014-04-22 00:09:231665 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(), Component(7, 16)},
1666 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, Component(), Component(7, 19)},
[email protected]e7bba5f82013-04-10 20:10:521667 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
[email protected]0318f922014-04-22 00:09:231668 {"//", "file:///", true, Component(), Component(7, 1)},
1669 {"///", "file:///", true, Component(), Component(7, 1)},
1670 {"///test", "file:///test", true, Component(), Component(7, 5)},
1671 {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)},
1672 {"file://localhost", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1673 {"file://localhost/", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1674 {"file://localhost/test", "file://localhost/test", true, Component(7, 9), Component(16, 5)},
[email protected]e7bba5f82013-04-10 20:10:521675#endif // _WIN32
1676 };
1677
viettrungluu4b6915862014-10-16 03:42:491678 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521679 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231680 Parsed parsed;
1681 ParseFileURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521682
[email protected]0318f922014-04-22 00:09:231683 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521684 std::string out_str;
[email protected]0318f922014-04-22 00:09:231685 StdStringCanonOutput output(&out_str);
1686 bool success = CanonicalizeFileURL(cases[i].input, url_len, parsed, NULL,
1687 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521688 output.Complete();
1689
1690 EXPECT_EQ(cases[i].expected_success, success);
1691 EXPECT_EQ(cases[i].expected, out_str);
1692
1693 // Make sure the spec was properly identified, the file canonicalizer has
1694 // different code for writing the spec.
1695 EXPECT_EQ(0, out_parsed.scheme.begin);
1696 EXPECT_EQ(4, out_parsed.scheme.len);
1697
1698 EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
1699 EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
1700
1701 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1702 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1703 }
1704}
1705
1706TEST(URLCanonTest, CanonicalizeFileSystemURL) {
1707 struct URLCase {
1708 const char* input;
1709 const char* expected;
1710 bool expected_success;
1711 } cases[] = {
1712 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1713 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1714 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1715 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1716 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1717 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1718 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1719 };
1720
viettrungluu4b6915862014-10-16 03:42:491721 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521722 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231723 Parsed parsed;
1724 ParseFileSystemURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521725
[email protected]0318f922014-04-22 00:09:231726 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521727 std::string out_str;
[email protected]0318f922014-04-22 00:09:231728 StdStringCanonOutput output(&out_str);
1729 bool success = CanonicalizeFileSystemURL(cases[i].input, url_len, parsed,
1730 NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521731 output.Complete();
1732
1733 EXPECT_EQ(cases[i].expected_success, success);
1734 EXPECT_EQ(cases[i].expected, out_str);
1735
1736 // Make sure the spec was properly identified, the filesystem canonicalizer
1737 // has different code for writing the spec.
1738 EXPECT_EQ(0, out_parsed.scheme.begin);
1739 EXPECT_EQ(10, out_parsed.scheme.len);
1740 if (success)
1741 EXPECT_GT(out_parsed.path.len, 0);
1742 }
1743}
1744
1745TEST(URLCanonTest, CanonicalizePathURL) {
1746 // Path URLs should get canonicalized schemes but nothing else.
1747 struct PathCase {
1748 const char* input;
1749 const char* expected;
1750 } path_cases[] = {
1751 {"javascript:", "javascript:"},
1752 {"JavaScript:Foo", "javascript:Foo"},
1753 {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1754 };
1755
viettrungluu4b6915862014-10-16 03:42:491756 for (size_t i = 0; i < arraysize(path_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521757 int url_len = static_cast<int>(strlen(path_cases[i].input));
[email protected]0318f922014-04-22 00:09:231758 Parsed parsed;
1759 ParsePathURL(path_cases[i].input, url_len, true, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521760
[email protected]0318f922014-04-22 00:09:231761 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521762 std::string out_str;
[email protected]0318f922014-04-22 00:09:231763 StdStringCanonOutput output(&out_str);
1764 bool success = CanonicalizePathURL(path_cases[i].input, url_len, parsed,
1765 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521766 output.Complete();
1767
1768 EXPECT_TRUE(success);
1769 EXPECT_EQ(path_cases[i].expected, out_str);
1770
1771 EXPECT_EQ(0, out_parsed.host.begin);
1772 EXPECT_EQ(-1, out_parsed.host.len);
1773
1774 // When we end with a colon at the end, there should be no path.
1775 if (path_cases[i].input[url_len - 1] == ':') {
[email protected]369e84f72013-11-23 01:53:521776 EXPECT_EQ(0, out_parsed.GetContent().begin);
1777 EXPECT_EQ(-1, out_parsed.GetContent().len);
[email protected]e7bba5f82013-04-10 20:10:521778 }
1779 }
1780}
1781
1782TEST(URLCanonTest, CanonicalizeMailtoURL) {
1783 struct URLCase {
1784 const char* input;
1785 const char* expected;
1786 bool expected_success;
[email protected]0318f922014-04-22 00:09:231787 Component expected_path;
1788 Component expected_query;
[email protected]e7bba5f82013-04-10 20:10:521789 } cases[] = {
[email protected]0318f922014-04-22 00:09:231790 {"mailto:addr1", "mailto:addr1", true, Component(7, 5), Component()},
1791 {"mailto:[email protected]", "mailto:[email protected]", true, Component(7, 13), Component()},
[email protected]e7bba5f82013-04-10 20:10:521792 // Trailing whitespace is stripped.
[email protected]0318f922014-04-22 00:09:231793 {"MaIlTo:addr1 \t ", "mailto:addr1", true, Component(7, 5), Component()},
1794 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, Component(7, 5), Component(13,6)},
1795 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, Component(7, 11), Component()},
1796 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, Component(7, 12), Component()},
1797 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, Component(7, 13), Component()},
1798 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, Component(7, 12), Component()},
[email protected]e7bba5f82013-04-10 20:10:521799 // Null character should be escaped to %00
[email protected]0318f922014-04-22 00:09:231800 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, Component(7, 13), Component(21, 3)},
[email protected]e7bba5f82013-04-10 20:10:521801 // Invalid -- UTF-8 encoded surrogate value.
[email protected]0318f922014-04-22 00:09:231802 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, Component(7, 9), Component()},
1803 {"mailto:addr1?", "mailto:addr1?", true, Component(7, 5), Component(13, 0)},
[email protected]e7bba5f82013-04-10 20:10:521804 };
1805
1806 // Define outside of loop to catch bugs where components aren't reset
[email protected]0318f922014-04-22 00:09:231807 Parsed parsed;
1808 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521809
viettrungluu4b6915862014-10-16 03:42:491810 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521811 int url_len = static_cast<int>(strlen(cases[i].input));
1812 if (i == 8) {
1813 // The 9th test case purposely has a '\0' in it -- don't count it
1814 // as the string terminator.
1815 url_len = 22;
1816 }
[email protected]0318f922014-04-22 00:09:231817 ParseMailtoURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521818
1819 std::string out_str;
[email protected]0318f922014-04-22 00:09:231820 StdStringCanonOutput output(&out_str);
1821 bool success = CanonicalizeMailtoURL(cases[i].input, url_len, parsed,
1822 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521823 output.Complete();
1824
1825 EXPECT_EQ(cases[i].expected_success, success);
1826 EXPECT_EQ(cases[i].expected, out_str);
1827
1828 // Make sure the spec was properly identified
1829 EXPECT_EQ(0, out_parsed.scheme.begin);
1830 EXPECT_EQ(6, out_parsed.scheme.len);
1831
1832 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1833 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1834
1835 EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
1836 EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
1837 }
1838}
1839
1840#ifndef WIN32
1841
1842TEST(URLCanonTest, _itoa_s) {
1843 // We fill the buffer with 0xff to ensure that it's getting properly
qyearsley2bc727d2015-08-14 20:17:151844 // null-terminated. We also allocate one byte more than what we tell
[email protected]e7bba5f82013-04-10 20:10:521845 // _itoa_s about, and ensure that the extra byte is untouched.
1846 char buf[6];
1847 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231848 EXPECT_EQ(0, _itoa_s(12, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521849 EXPECT_STREQ("12", buf);
1850 EXPECT_EQ('\xFF', buf[3]);
1851
1852 // Test the edge cases - exactly the buffer size and one over
1853 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231854 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521855 EXPECT_STREQ("1234", buf);
1856 EXPECT_EQ('\xFF', buf[5]);
1857
1858 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231859 EXPECT_EQ(EINVAL, _itoa_s(12345, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521860 EXPECT_EQ('\xFF', buf[5]); // should never write to this location
1861
1862 // Test the template overload (note that this will see the full buffer)
1863 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231864 EXPECT_EQ(0, _itoa_s(12, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521865 EXPECT_STREQ("12", buf);
1866 EXPECT_EQ('\xFF', buf[3]);
1867
1868 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231869 EXPECT_EQ(0, _itoa_s(12345, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521870 EXPECT_STREQ("12345", buf);
1871
[email protected]0318f922014-04-22 00:09:231872 EXPECT_EQ(EINVAL, _itoa_s(123456, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521873
1874 // Test that radix 16 is supported.
1875 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231876 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 16));
[email protected]e7bba5f82013-04-10 20:10:521877 EXPECT_STREQ("4d2", buf);
1878 EXPECT_EQ('\xFF', buf[5]);
1879}
1880
1881TEST(URLCanonTest, _itow_s) {
1882 // We fill the buffer with 0xff to ensure that it's getting properly
qyearsley2bc727d2015-08-14 20:17:151883 // null-terminated. We also allocate one byte more than what we tell
[email protected]e7bba5f82013-04-10 20:10:521884 // _itoa_s about, and ensure that the extra byte is untouched.
[email protected]3774f832013-06-11 21:21:571885 base::char16 buf[6];
[email protected]e7bba5f82013-04-10 20:10:521886 const char fill_mem = 0xff;
[email protected]3774f832013-06-11 21:21:571887 const base::char16 fill_char = 0xffff;
[email protected]e7bba5f82013-04-10 20:10:521888 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231889 EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
[email protected]3774f832013-06-11 21:21:571890 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521891 EXPECT_EQ(fill_char, buf[3]);
1892
1893 // Test the edge cases - exactly the buffer size and one over
[email protected]0318f922014-04-22 00:09:231894 EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
[email protected]3774f832013-06-11 21:21:571895 EXPECT_EQ(WStringToUTF16(L"1234"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521896 EXPECT_EQ(fill_char, buf[5]);
1897
1898 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231899 EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521900 EXPECT_EQ(fill_char, buf[5]); // should never write to this location
1901
1902 // Test the template overload (note that this will see the full buffer)
1903 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231904 EXPECT_EQ(0, _itow_s(12, buf, 10));
[email protected]3774f832013-06-11 21:21:571905 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521906 EXPECT_EQ(fill_char, buf[3]);
1907
1908 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231909 EXPECT_EQ(0, _itow_s(12345, buf, 10));
[email protected]3774f832013-06-11 21:21:571910 EXPECT_EQ(WStringToUTF16(L"12345"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521911
[email protected]0318f922014-04-22 00:09:231912 EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521913}
1914
1915#endif // !WIN32
1916
1917// Returns true if the given two structures are the same.
[email protected]0318f922014-04-22 00:09:231918static bool ParsedIsEqual(const Parsed& a, const Parsed& b) {
[email protected]e7bba5f82013-04-10 20:10:521919 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
1920 a.username.begin == b.username.begin && a.username.len == b.username.len &&
1921 a.password.begin == b.password.begin && a.password.len == b.password.len &&
1922 a.host.begin == b.host.begin && a.host.len == b.host.len &&
1923 a.port.begin == b.port.begin && a.port.len == b.port.len &&
1924 a.path.begin == b.path.begin && a.path.len == b.path.len &&
1925 a.query.begin == b.query.begin && a.query.len == b.query.len &&
1926 a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
1927}
1928
1929TEST(URLCanonTest, ResolveRelativeURL) {
1930 struct RelativeCase {
1931 const char* base; // Input base URL: MUST BE CANONICAL
1932 bool is_base_hier; // Is the base URL hierarchical
1933 bool is_base_file; // Tells us if the base is a file URL.
1934 const char* test; // Input URL to test against.
1935 bool succeed_relative; // Whether we expect IsRelativeURL to succeed
1936 bool is_rel; // Whether we expect |test| to be relative or not.
1937 bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed.
1938 const char* resolved; // What we expect in the result when resolving.
1939 } rel_cases[] = {
1940 // Basic absolute input.
1941 {"http://host/a", true, false, "http://another/", true, false, false, NULL},
1942 {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
1943 // Empty relative URLs should only remove the ref part of the URL,
1944 // leaving the rest unchanged.
1945 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
1946 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
1947 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
1948 // Spaces at the ends of the relative path should be ignored.
1949 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
1950 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
1951 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
1952 // Matching schemes without two slashes are treated as relative.
1953 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
1954 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
1955 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
1956 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
1957 // Nonmatching schemes are absolute.
1958 {"http://host/a", true, false, "https:host2", true, false, false, NULL},
1959 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
1960 // Absolute path input
1961 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
1962 {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
1963 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
1964 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
1965 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
1966 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
1967 // Relative path input
1968 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
1969 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
1970 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
1971 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
1972 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
1973 {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
1974 {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
1975 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
1976 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
1977 // Query input
1978 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
1979 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
1980 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
1981 // Ref input
1982 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
1983 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
1984 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
1985 // Non-hierarchical base: no relative handling. Relative input should
1986 // error, and if a scheme is present, it should be treated as absolute.
1987 {"data:foobar", false, false, "baz.html", false, false, false, NULL},
1988 {"data:foobar", false, false, "data:baz", true, false, false, NULL},
1989 {"data:foobar", false, false, "data:/base", true, false, false, NULL},
1990 // Non-hierarchical base: absolute input should succeed.
1991 {"data:foobar", false, false, "http://host/", true, false, false, NULL},
1992 {"data:foobar", false, false, "http:host", true, false, false, NULL},
ramya.v56d422052015-12-02 02:24:461993 // Non-hierarchical base: empty URL should give error.
1994 {"data:foobar", false, false, "", false, false, false, NULL},
[email protected]e7bba5f82013-04-10 20:10:521995 // Invalid schemes should be treated as relative.
1996 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
1997 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
1998 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
1999 {"data:asdf", false, false, ":foo", false, false, false, NULL},
[email protected]45172e62014-03-03 21:21:352000 {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL},
[email protected]e7bba5f82013-04-10 20:10:522001 // We should treat semicolons like any other character in URL resolving
2002 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
2003 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
2004 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
2005 // Relative URLs can also be written as "//foo/bar" which is relative to
2006 // the scheme. In this case, it would take the old scheme, so for http
2007 // the example would resolve to "http://foo/bar".
2008 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
2009 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
2010 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
2011 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
2012 {"http://host/a", true, false, "//", true, true, false, "http:"},
2013 // IE will also allow one or the other to be a backslash to get the same
2014 // behavior.
2015 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
2016 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
2017#ifdef WIN32
2018 // Resolving against Windows file base URLs.
2019 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
2020 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
2021 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
2022 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
2023 // But two backslashes on Windows should be UNC so should be treated
2024 // as absolute.
2025 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
2026 // IE doesn't support drive specs starting with two slashes. It fails
2027 // immediately and doesn't even try to load. We fix it up to either
2028 // an absolute path or UNC depending on what it looks like.
2029 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
2030 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
2031 // Windows drive specs should be allowed and treated as absolute.
2032 {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
2033 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
2034 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
2035 // Relative paths with drive letters should be allowed when the base is
2036 // also a file.
2037 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2038 // Treat absolute paths as being off of the drive.
2039 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2040 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2041 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2042 // On Windows, two slashes without a drive letter when the base is a file
2043 // means that the path is UNC.
2044 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2045 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2046#else
2047 // On Unix we fall back to relative behavior since there's nothing else
2048 // reasonable to do.
2049 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2050#endif
2051 // Even on Windows, we don't allow relative drive specs when the base
2052 // is not file.
2053 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2054 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
[email protected]598c8382013-09-18 22:55:312055 // Ensure that ports aren't allowed for hosts relative to a file url.
2056 // Although the result string shows a host:port portion, the call to
2057 // resolve the relative URL returns false, indicating parse failure,
2058 // which is what is required.
2059 {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
[email protected]e7bba5f82013-04-10 20:10:522060 // Filesystem URL tests; filesystem URLs are only valid and relative if
qyearsley2bc727d2015-08-14 20:17:152061 // they have no scheme, e.g. "./index.html". There's no valid equivalent
[email protected]e7bba5f82013-04-10 20:10:522062 // to http:index.html.
2063 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2064 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
2065 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
2066 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2067 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2068 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2069 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
2070 // Absolute URLs are still not relative to a non-standard base URL.
2071 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2072 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2073 };
2074
viettrungluu4b6915862014-10-16 03:42:492075 for (size_t i = 0; i < arraysize(rel_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:522076 const RelativeCase& cur_case = rel_cases[i];
2077
[email protected]0318f922014-04-22 00:09:232078 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:522079 int base_len = static_cast<int>(strlen(cur_case.base));
2080 if (cur_case.is_base_file)
[email protected]0318f922014-04-22 00:09:232081 ParseFileURL(cur_case.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522082 else if (cur_case.is_base_hier)
[email protected]0318f922014-04-22 00:09:232083 ParseStandardURL(cur_case.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522084 else
[email protected]0318f922014-04-22 00:09:232085 ParsePathURL(cur_case.base, base_len, false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522086
2087 // First see if it is relative.
2088 int test_len = static_cast<int>(strlen(cur_case.test));
2089 bool is_relative;
[email protected]0318f922014-04-22 00:09:232090 Component relative_component;
2091 bool succeed_is_rel = IsRelativeURL(
[email protected]e7bba5f82013-04-10 20:10:522092 cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
2093 &is_relative, &relative_component);
2094
2095 EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
2096 "succeed is rel failure on " << cur_case.test;
2097 EXPECT_EQ(cur_case.is_rel, is_relative) <<
2098 "is rel failure on " << cur_case.test;
2099 // Now resolve it.
2100 if (succeed_is_rel && is_relative && cur_case.is_rel) {
2101 std::string resolved;
[email protected]0318f922014-04-22 00:09:232102 StdStringCanonOutput output(&resolved);
2103 Parsed resolved_parsed;
[email protected]e7bba5f82013-04-10 20:10:522104
[email protected]0318f922014-04-22 00:09:232105 bool succeed_resolve = ResolveRelativeURL(
2106 cur_case.base, parsed, cur_case.is_base_file, cur_case.test,
2107 relative_component, NULL, &output, &resolved_parsed);
[email protected]e7bba5f82013-04-10 20:10:522108 output.Complete();
2109
2110 EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
2111 EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
2112
2113 // Verify that the output parsed structure is the same as parsing a
2114 // the URL freshly.
[email protected]0318f922014-04-22 00:09:232115 Parsed ref_parsed;
[email protected]e7bba5f82013-04-10 20:10:522116 int resolved_len = static_cast<int>(resolved.size());
[email protected]369e84f72013-11-23 01:53:522117 if (cur_case.is_base_file) {
[email protected]0318f922014-04-22 00:09:232118 ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522119 } else if (cur_case.is_base_hier) {
[email protected]0318f922014-04-22 00:09:232120 ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522121 } else {
[email protected]0318f922014-04-22 00:09:232122 ParsePathURL(resolved.c_str(), resolved_len, false, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522123 }
[email protected]e7bba5f82013-04-10 20:10:522124 EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
2125 }
2126 }
2127}
2128
qyearsley2bc727d2015-08-14 20:17:152129// It used to be the case that when we did a replacement with a long buffer of
2130// UTF-16 characters, we would get invalid data in the URL. This is because the
2131// buffer that it used to hold the UTF-8 data was resized, while some pointers
2132// were still kept to the old buffer that was removed.
[email protected]e7bba5f82013-04-10 20:10:522133TEST(URLCanonTest, ReplacementOverflow) {
2134 const char src[] = "file:///C:/foo/bar";
2135 int src_len = static_cast<int>(strlen(src));
[email protected]0318f922014-04-22 00:09:232136 Parsed parsed;
2137 ParseFileURL(src, src_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522138
2139 // Override two components, the path with something short, and the query with
qyearsley2bc727d2015-08-14 20:17:152140 // something long enough to trigger the bug.
[email protected]0318f922014-04-22 00:09:232141 Replacements<base::char16> repl;
[email protected]3774f832013-06-11 21:21:572142 base::string16 new_query;
[email protected]e7bba5f82013-04-10 20:10:522143 for (int i = 0; i < 4800; i++)
2144 new_query.push_back('a');
2145
[email protected]3774f832013-06-11 21:21:572146 base::string16 new_path(WStringToUTF16(L"/foo"));
[email protected]0318f922014-04-22 00:09:232147 repl.SetPath(new_path.c_str(), Component(0, 4));
[email protected]e7bba5f82013-04-10 20:10:522148 repl.SetQuery(new_query.c_str(),
[email protected]0318f922014-04-22 00:09:232149 Component(0, static_cast<int>(new_query.length())));
[email protected]e7bba5f82013-04-10 20:10:522150
2151 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2152 // standard URLs, file URLs, etc, since they will go to the same replacement
2153 // function that was buggy.
[email protected]0318f922014-04-22 00:09:232154 Parsed repl_parsed;
[email protected]e7bba5f82013-04-10 20:10:522155 std::string repl_str;
[email protected]0318f922014-04-22 00:09:232156 StdStringCanonOutput repl_output(&repl_str);
2157 ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
[email protected]e7bba5f82013-04-10 20:10:522158 repl_output.Complete();
2159
2160 // Generate the expected string and check.
2161 std::string expected("file:///foo?");
2162 for (size_t i = 0; i < new_query.length(); i++)
2163 expected.push_back('a');
2164 EXPECT_TRUE(expected == repl_str);
2165}
[email protected]0318f922014-04-22 00:09:232166
2167} // namespace url