blob: 82edd0e6221cb2f9623726d14fdd6fedc12a28a5 [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]e7bba5f82013-04-10 20:10:524
5#include <errno.h>
avic0c60312015-12-21 21:03:506#include <stddef.h>
[email protected]e7bba5f82013-04-10 20:10:527
[email protected]847aaab82014-05-07 14:05:468#include "base/macros.h"
[email protected]e7bba5f82013-04-10 20:10:529#include "testing/gtest/include/gtest/gtest.h"
tfarina018de6e2015-05-26 17:41:2010#include "url/third_party/mozilla/url_parse.h"
[email protected]318076b2013-04-18 21:19:4511#include "url/url_canon.h"
[email protected]318076b2013-04-18 21:19:4512#include "url/url_canon_internal.h"
13#include "url/url_canon_stdstring.h"
[email protected]318076b2013-04-18 21:19:4514#include "url/url_test_utils.h"
[email protected]e7bba5f82013-04-10 20:10:5215
[email protected]0318f922014-04-22 00:09:2316namespace url {
17
18using test_utils::WStringToUTF16;
19using test_utils::ConvertUTF8ToUTF16;
20using test_utils::ConvertUTF16ToUTF8;
[email protected]e7bba5f82013-04-10 20:10:5221
22namespace {
23
24struct ComponentCase {
25 const char* input;
26 const char* expected;
[email protected]0318f922014-04-22 00:09:2327 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5228 bool expected_success;
29};
30
31// ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
32// treat each input as optional, and will only try processing if non-NULL.
33// The output is always 8-bit.
34struct DualComponentCase {
35 const char* input8;
36 const wchar_t* input16;
37 const char* expected;
[email protected]0318f922014-04-22 00:09:2338 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5239 bool expected_success;
40};
41
qyearsley2bc727d2015-08-14 20:17:1542// Test cases for CanonicalizeIPAddress(). The inputs are identical to
[email protected]e7bba5f82013-04-10 20:10:5243// DualComponentCase, but the output has extra CanonHostInfo fields.
44struct IPAddressCase {
45 const char* input8;
46 const wchar_t* input16;
47 const char* expected;
[email protected]0318f922014-04-22 00:09:2348 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:5249
50 // CanonHostInfo fields, for verbose output.
51 CanonHostInfo::Family expected_family;
52 int expected_num_ipv4_components;
53 const char* expected_address_hex; // Two hex chars per IP address byte.
54};
55
56std::string BytesToHexString(unsigned char bytes[16], int length) {
57 EXPECT_TRUE(length == 0 || length == 4 || length == 16)
58 << "Bad IP address length: " << length;
59 std::string result;
60 for (int i = 0; i < length; ++i) {
[email protected]0318f922014-04-22 00:09:2361 result.push_back(kHexCharLookup[(bytes[i] >> 4) & 0xf]);
62 result.push_back(kHexCharLookup[bytes[i] & 0xf]);
[email protected]e7bba5f82013-04-10 20:10:5263 }
64 return result;
65}
66
67struct ReplaceCase {
68 const char* base;
69 const char* scheme;
70 const char* username;
71 const char* password;
72 const char* host;
73 const char* port;
74 const char* path;
75 const char* query;
76 const char* ref;
77 const char* expected;
78};
79
[email protected]e7bba5f82013-04-10 20:10:5280// Magic string used in the replacements code that tells SetupReplComp to
81// call the clear function.
82const char kDeleteComp[] = "|";
83
84// Sets up a replacement for a single component. This is given pointers to
85// the set and clear function for the component being replaced, and will
86// either set the component (if it exists) or clear it (if the replacement
87// string matches kDeleteComp).
88//
89// This template is currently used only for the 8-bit case, and the strlen
90// causes it to fail in other cases. It is left a template in case we have
91// tests for wide replacements.
92template<typename CHAR>
93void SetupReplComp(
[email protected]0318f922014-04-22 00:09:2394 void (Replacements<CHAR>::*set)(const CHAR*, const Component&),
95 void (Replacements<CHAR>::*clear)(),
96 Replacements<CHAR>* rep,
[email protected]e7bba5f82013-04-10 20:10:5297 const CHAR* str) {
98 if (str && str[0] == kDeleteComp[0]) {
99 (rep->*clear)();
100 } else if (str) {
[email protected]0318f922014-04-22 00:09:23101 (rep->*set)(str, Component(0, static_cast<int>(strlen(str))));
[email protected]e7bba5f82013-04-10 20:10:52102 }
103}
104
105} // namespace
106
107TEST(URLCanonTest, DoAppendUTF8) {
108 struct UTF8Case {
109 unsigned input;
110 const char* output;
111 } utf_cases[] = {
112 // Valid code points.
113 {0x24, "\x24"},
114 {0xA2, "\xC2\xA2"},
115 {0x20AC, "\xE2\x82\xAC"},
116 {0x24B62, "\xF0\xA4\xAD\xA2"},
117 {0x10FFFF, "\xF4\x8F\xBF\xBF"},
118 };
119 std::string out_str;
viettrungluu4b6915862014-10-16 03:42:49120 for (size_t i = 0; i < arraysize(utf_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52121 out_str.clear();
[email protected]0318f922014-04-22 00:09:23122 StdStringCanonOutput output(&out_str);
123 AppendUTF8Value(utf_cases[i].input, &output);
[email protected]e7bba5f82013-04-10 20:10:52124 output.Complete();
125 EXPECT_EQ(utf_cases[i].output, out_str);
126 }
127}
128
[email protected]7c0fccb32014-06-17 00:50:40129#if defined(GTEST_HAS_DEATH_TEST)
[email protected]e7bba5f82013-04-10 20:10:52130// TODO(mattm): Can't run this in debug mode for now, since the DCHECK will
qyearsley2bc727d2015-08-14 20:17:15131// cause the Chromium stack trace dialog to appear and hang the test.
[email protected]e7bba5f82013-04-10 20:10:52132// See http://crbug.com/49580.
[email protected]7c0fccb32014-06-17 00:50:40133#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
134#define MAYBE_DoAppendUTF8Invalid DoAppendUTF8Invalid
135#else
136#define MAYBE_DoAppendUTF8Invalid DISABLED_DoAppendUTF8Invalid
137#endif
138TEST(URLCanonTest, MAYBE_DoAppendUTF8Invalid) {
[email protected]e7bba5f82013-04-10 20:10:52139 std::string out_str;
[email protected]0318f922014-04-22 00:09:23140 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52141 // Invalid code point (too large).
142 ASSERT_DEBUG_DEATH({
[email protected]0318f922014-04-22 00:09:23143 AppendUTF8Value(0x110000, &output);
[email protected]e7bba5f82013-04-10 20:10:52144 output.Complete();
145 EXPECT_EQ("", out_str);
146 }, "");
147}
[email protected]7c0fccb32014-06-17 00:50:40148#endif // defined(GTEST_HAS_DEATH_TEST)
[email protected]e7bba5f82013-04-10 20:10:52149
150TEST(URLCanonTest, UTF) {
151 // Low-level test that we handle reading, canonicalization, and writing
152 // UTF-8/UTF-16 strings properly.
153 struct UTFCase {
154 const char* input8;
155 const wchar_t* input16;
156 bool expected_success;
157 const char* output;
158 } utf_cases[] = {
159 // Valid canonical input should get passed through & escaped.
160 {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
qyearsley2bc727d2015-08-14 20:17:15161 // Test a character that takes > 16 bits (U+10300 = old italic letter A)
[email protected]e7bba5f82013-04-10 20:10:52162 {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
qyearsley2bc727d2015-08-14 20:17:15163 // Non-shortest-form UTF-8 characters are invalid. The bad character
164 // should be replaced with the invalid character (EF BF DB in UTF-8).
[email protected]e7bba5f82013-04-10 20:10:52165 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"},
166 // Invalid UTF-8 sequences should be marked as invalid (the first
167 // sequence is truncated).
168 {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
169 // Character going off the end.
170 {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
171 // ...same with low surrogates with no high surrogate.
172 {"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"},
173 // Test a UTF-8 encoded surrogate value is marked as invalid.
174 // ED A0 80 = U+D800
175 {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
176 };
177
178 std::string out_str;
viettrungluu4b6915862014-10-16 03:42:49179 for (size_t i = 0; i < arraysize(utf_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52180 if (utf_cases[i].input8) {
181 out_str.clear();
[email protected]0318f922014-04-22 00:09:23182 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52183
184 int input_len = static_cast<int>(strlen(utf_cases[i].input8));
185 bool success = true;
186 for (int ch = 0; ch < input_len; ch++) {
187 success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
188 &output);
189 }
190 output.Complete();
191 EXPECT_EQ(utf_cases[i].expected_success, success);
192 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
193 }
194 if (utf_cases[i].input16) {
195 out_str.clear();
[email protected]0318f922014-04-22 00:09:23196 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52197
[email protected]3774f832013-06-11 21:21:57198 base::string16 input_str(WStringToUTF16(utf_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52199 int input_len = static_cast<int>(input_str.length());
200 bool success = true;
201 for (int ch = 0; ch < input_len; ch++) {
202 success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
203 &output);
204 }
205 output.Complete();
206 EXPECT_EQ(utf_cases[i].expected_success, success);
207 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
208 }
209
210 if (utf_cases[i].input8 && utf_cases[i].input16 &&
211 utf_cases[i].expected_success) {
212 // Check that the UTF-8 and UTF-16 inputs are equivalent.
213
214 // UTF-16 -> UTF-8
215 std::string input8_str(utf_cases[i].input8);
[email protected]3774f832013-06-11 21:21:57216 base::string16 input16_str(WStringToUTF16(utf_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52217 EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str));
218
219 // UTF-8 -> UTF-16
220 EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
221 }
222 }
223}
224
[email protected]e7bba5f82013-04-10 20:10:52225TEST(URLCanonTest, Scheme) {
226 // Here, we're mostly testing that unusual characters are handled properly.
227 // The canonicalizer doesn't do any parsing or whitespace detection. It will
228 // also do its best on error, and will escape funny sequences (these won't be
229 // valid schemes and it will return error).
230 //
231 // Note that the canonicalizer will append a colon to the output to separate
232 // out the rest of the URL, which is not present in the input. We check,
233 // however, that the output range includes everything but the colon.
234 ComponentCase scheme_cases[] = {
[email protected]0318f922014-04-22 00:09:23235 {"http", "http:", Component(0, 4), true},
236 {"HTTP", "http:", Component(0, 4), true},
237 {" HTTP ", "%20http%20:", Component(0, 10), false},
238 {"htt: ", "htt%3A%20:", Component(0, 9), false},
239 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false},
[email protected]e7bba5f82013-04-10 20:10:52240 // Don't re-escape something already escaped. Note that it will
241 // "canonicalize" the 'A' to 'a', but that's OK.
[email protected]0318f922014-04-22 00:09:23242 {"ht%3Atp", "ht%3atp:", Component(0, 7), false},
[email protected]e7bba5f82013-04-10 20:10:52243 };
244
245 std::string out_str;
246
247 for (size_t i = 0; i < arraysize(scheme_cases); i++) {
248 int url_len = static_cast<int>(strlen(scheme_cases[i].input));
[email protected]0318f922014-04-22 00:09:23249 Component in_comp(0, url_len);
250 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52251
252 out_str.clear();
[email protected]0318f922014-04-22 00:09:23253 StdStringCanonOutput output1(&out_str);
254 bool success = CanonicalizeScheme(scheme_cases[i].input, in_comp, &output1,
255 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52256 output1.Complete();
257
258 EXPECT_EQ(scheme_cases[i].expected_success, success);
259 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
260 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
261 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
262
qyearsley2bc727d2015-08-14 20:17:15263 // Now try the wide version.
[email protected]e7bba5f82013-04-10 20:10:52264 out_str.clear();
[email protected]0318f922014-04-22 00:09:23265 StdStringCanonOutput output2(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52266
[email protected]3774f832013-06-11 21:21:57267 base::string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input));
[email protected]e7bba5f82013-04-10 20:10:52268 in_comp.len = static_cast<int>(wide_input.length());
[email protected]0318f922014-04-22 00:09:23269 success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2,
270 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52271 output2.Complete();
272
273 EXPECT_EQ(scheme_cases[i].expected_success, success);
274 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
275 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
276 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
277 }
278
qyearsley2bc727d2015-08-14 20:17:15279 // Test the case where the scheme is declared nonexistent, it should be
[email protected]e7bba5f82013-04-10 20:10:52280 // converted into an empty scheme.
[email protected]0318f922014-04-22 00:09:23281 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52282 out_str.clear();
[email protected]0318f922014-04-22 00:09:23283 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52284
[email protected]0318f922014-04-22 00:09:23285 EXPECT_TRUE(CanonicalizeScheme("", Component(0, -1), &output, &out_comp));
[email protected]e7bba5f82013-04-10 20:10:52286 output.Complete();
287
288 EXPECT_EQ(std::string(":"), out_str);
289 EXPECT_EQ(0, out_comp.begin);
290 EXPECT_EQ(0, out_comp.len);
291}
292
293TEST(URLCanonTest, Host) {
294 IPAddressCase host_cases[] = {
295 // Basic canonicalization, uppercase should be converted to lowercase.
[email protected]0318f922014-04-22 00:09:23296 {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52297 // Spaces and some other characters should be escaped.
[email protected]0318f922014-04-22 00:09:23298 {"Goo%20 goo%7C|.com", L"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", Component(0, 22), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52299 // Exciting different types of spaces!
[email protected]0318f922014-04-22 00:09:23300 {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52301 // Other types of space (no-break, zero-width, zero-width-no-break) are
302 // name-prepped away to nothing.
[email protected]0318f922014-04-22 00:09:23303 {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52304 // Ideographic full stop (full-width period for Chinese, etc.) should be
305 // treated as a dot.
[email protected]0318f922014-04-22 00:09:23306 {NULL, L"www.foo\x3002" L"bar.com", "www.foo.bar.com", Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52307 // Invalid unicode characters should fail...
308 // ...In wide input, ICU will barf and we'll end up with the input as
309 // escaped UTF-8 (the invalid character should be replaced with the
310 // replacement character).
[email protected]0318f922014-04-22 00:09:23311 {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52312 // ...This is the same as previous but with with escaped.
[email protected]0318f922014-04-22 00:09:23313 {"%ef%b7%90zyx.com", L"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52314 // Test name prepping, fullwidth input should be converted to ASCII and NOT
315 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
[email protected]0318f922014-04-22 00:09:23316 {"\xef\xbc\xa7\xef\xbd\x8f.com", L"\xff27\xff4f.com", "go.com", Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52317 // Test that fullwidth escaped values are properly name-prepped,
318 // then converted or rejected.
319 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
[email protected]0318f922014-04-22 00:09:23320 {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
321 {"%ef%bc%85%ef%bc%94%ef%bc%91.com", L"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52322 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
[email protected]0318f922014-04-22 00:09:23323 {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
324 {"%ef%bc%85%ef%bc%90%ef%bc%90.com", L"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
brettw1141951b2015-11-26 00:29:35325 // ICU will convert weird percents into ASCII percents, but not unescape
326 // further. A weird percent is U+FE6A (EF B9 AA in UTF-8) which is a
327 // "small percent". At this point we should be within our rights to mark
328 // anything as invalid since the URL is corrupt or malicious. The code
329 // happens to allow ASCII characters (%41 = "A" -> 'a') to be unescaped
330 // and kept as valid, so we validate that behavior here, but this level
331 // of fixing the input shouldn't be seen as required. "%81" is invalid.
332 {"\xef\xb9\xaa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
333 {"%ef%b9%aa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
334 {"\xef\xb9\xaa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
335 {"%ef%b9%aa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52336 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
[email protected]0318f922014-04-22 00:09:23337 {"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d\x4f60\x597d", "xn--6qqa088eba", Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50338 // See http://unicode.org/cldr/utility/idna.jsp for other
339 // examples/experiments and http://goo.gl/7yG11o
340 // for the full list of characters handled differently by
341 // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008.
342
343 // 4 Deviation characters are mapped/ignored in UTS 46 transitional
344 // mechansm. UTS 46, table 4 row (g).
345 // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003.
346 // Otherwise, it'd be "xn--fuball-cta.de".
347 {"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de",
[email protected]0318f922014-04-22 00:09:23348 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50349 // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2).
350 // Otherwise, it'd be "xn--wxaijb9b".
351 {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2",
[email protected]0318f922014-04-22 00:09:23352 "xn--wxaikc6b", Component(0, 12),
[email protected]18f00cd2013-09-29 10:52:50353 CanonHostInfo::NEUTRAL, -1, ""},
354 // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional
355 // handling as well as in IDNA 2003.
356 {"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L"a\x200c" L"b\x200d" L"c", "abc",
[email protected]0318f922014-04-22 00:09:23357 Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50358 // ZWJ between Devanagari characters is still mapped away in UTS 46
359 // transitional handling. IDNA 2008 would give xn--11bo0mv54g.
360 {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
361 L"\x915\x94d\x200d\x91c", "xn--11bo0m",
[email protected]0318f922014-04-22 00:09:23362 Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50363 // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b)
364 // However, we do allow this at the moment because we don't use
365 // STD3 rules and canonicalize full-width ASCII to ASCII.
366 {"wow\xef\xbc\x81", L"wow\xff01", "wow%21",
[email protected]0318f922014-04-22 00:09:23367 Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50368 // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c)
369 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
370 {"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo",
[email protected]0318f922014-04-22 00:09:23371 Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50372 // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d)
373 // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2
374 {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn",
375 "%F0%AF%A1%A8%E5%A7%BB.cn",
[email protected]0318f922014-04-22 00:09:23376 Component(0, 24), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50377 // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e)
378 {"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya",
[email protected]0318f922014-04-22 00:09:23379 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50380 // Symbol/punctuations are allowed in IDNA 2003/UTS46.
381 // Not allowed in IDNA 2008. UTS 46 table 4 row (f).
382 {"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us",
[email protected]0318f922014-04-22 00:09:23383 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50384 // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h)
385 // We used to allow it because we passed through unassigned code points.
386 {"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com",
[email protected]0318f922014-04-22 00:09:23387 Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50388 // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i)
389 // Used to be allowed in INDA 2003.
390 {"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg",
[email protected]0318f922014-04-22 00:09:23391 Component(0, 9), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50392 // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based
393 // on Unicode 3.2). We did allow it in the past because we let unassigned
394 // code point pass. We continue to allow it even though it's a
395 // "punctuation and symbol" blocked in IDNA 2008.
396 // UTS 46 table 4, row (j)
397 {"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com",
[email protected]0318f922014-04-22 00:09:23398 Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50399 // Maps uppercase letters to lower case letters.
400 // In IDNA 2003, it's allowed without case-folding
401 // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2
402 // (added in Unicode 4.1). UTS 46 table 4 row (k)
403 {"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com",
[email protected]0318f922014-04-22 00:09:23404 Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50405 // BiDi check test
406 // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM.
407 // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008.
408 {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
409 L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
[email protected]0318f922014-04-22 00:09:23410 Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50411 // Disallowed in both IDNA 2003 and 2008 with BiDi check.
412 // Labels starting with a RTL character cannot end with a LTR character.
413 {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz",
[email protected]0318f922014-04-22 00:09:23414 "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21),
[email protected]18f00cd2013-09-29 10:52:50415 CanonHostInfo::BROKEN, -1, ""},
416 // Labels starting with a RTL character can end with BC=EN (European
417 // number). Disallowed in IDNA 2003 but now allowed.
418 {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2",
[email protected]0318f922014-04-22 00:09:23419 "xn--2-ymcov", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50420 CanonHostInfo::NEUTRAL, -1, ""},
421 // Labels starting with a RTL character cannot have "L" characters
422 // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008.
423 {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2",
[email protected]0318f922014-04-22 00:09:23424 "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21),
[email protected]18f00cd2013-09-29 10:52:50425 CanonHostInfo::BROKEN, -1, ""},
426 // Labels starting with a RTL character can end with BC=AN (Arabic number)
427 // Disallowed in IDNA 2003, but now allowed.
428 {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662",
[email protected]0318f922014-04-22 00:09:23429 "xn--mgbjq0r", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50430 CanonHostInfo::NEUTRAL, -1, ""},
431 // Labels starting with a RTL character cannot have "L" characters
432 // even if it ends with an BC=AN (Arabic number).
433 // Disallowed in both IDNA 2003/2008.
434 {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662",
[email protected]0318f922014-04-22 00:09:23435 "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26),
[email protected]18f00cd2013-09-29 10:52:50436 CanonHostInfo::BROKEN, -1, ""},
437 // Labels starting with a RTL character cannot mix BC=EN and BC=AN
438 {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662",
[email protected]0318f922014-04-22 00:09:23439 "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27),
[email protected]18f00cd2013-09-29 10:52:50440 CanonHostInfo::BROKEN, -1, ""},
441 // As of Unicode 6.2, U+20CF is not assigned. We do not allow it.
442 {"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com",
[email protected]0318f922014-04-22 00:09:23443 Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50444 // U+0080 is not allowed.
445 {"\xc2\x80.com", L"\x80.com", "%C2%80.com",
[email protected]0318f922014-04-22 00:09:23446 Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50447 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
[email protected]e7bba5f82013-04-10 20:10:52448 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
449 // UTF-8 (wide case). The output should be equivalent to the true wide
450 // character input above).
[email protected]18f00cd2013-09-29 10:52:50451 {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
452 L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
[email protected]0318f922014-04-22 00:09:23453 Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52454 // Invalid escaped characters should fail and the percents should be
455 // escaped.
[email protected]0318f922014-04-22 00:09:23456 {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", Component(0, 10),
[email protected]18f00cd2013-09-29 10:52:50457 CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52458 // If we get an invalid character that has been escaped.
[email protected]0318f922014-04-22 00:09:23459 {"%25", L"%25", "%25", Component(0, 3),
[email protected]18f00cd2013-09-29 10:52:50460 CanonHostInfo::BROKEN, -1, ""},
[email protected]0318f922014-04-22 00:09:23461 {"hello%00", L"hello%00", "hello%00", Component(0, 8),
[email protected]18f00cd2013-09-29 10:52:50462 CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52463 // Escaped numbers should be treated like IP addresses if they are.
[email protected]18f00cd2013-09-29 10:52:50464 {"%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:23465 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
[email protected]18f00cd2013-09-29 10:52:50466 "C0A80001"},
467 {"%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:23468 "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3,
[email protected]18f00cd2013-09-29 10:52:50469 "C0A80001"},
[email protected]e7bba5f82013-04-10 20:10:52470 // Invalid escaping should trigger the regular host error handling.
[email protected]0318f922014-04-22 00:09:23471 {"%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:52472 // Something that isn't exactly an IP should get treated as a host and
473 // spaces escaped.
[email protected]0318f922014-04-22 00:09:23474 {"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:52475 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
476 // These are "0Xc0.0250.01" in fullwidth.
[email protected]0318f922014-04-22 00:09:23477 {"\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:52478 // Broken IP addresses get marked as such.
[email protected]0318f922014-04-22 00:09:23479 {"192.168.0.257", L"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
480 {"[google.com]", L"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
[email protected]18f00cd2013-09-29 10:52:50481 // Cyrillic letter followed by '(' should return punycode for '(' escaped
482 // before punycode string was created. I.e.
483 // if '(' is escaped after punycode is created we would get xn--%28-8tb
484 // (incorrect).
[email protected]0318f922014-04-22 00:09:23485 {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", Component(0, 11),
[email protected]18f00cd2013-09-29 10:52:50486 CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52487 // Address with all hexidecimal characters with leading number of 1<<32
488 // or greater and should return NEUTRAL rather than BROKEN if not all
489 // components are numbers.
[email protected]0318f922014-04-22 00:09:23490 {"12345678912345.de", L"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
491 {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
492 {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
493 {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
494 {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52495 };
496
497 // CanonicalizeHost() non-verbose.
498 std::string out_str;
499 for (size_t i = 0; i < arraysize(host_cases); i++) {
500 // Narrow version.
501 if (host_cases[i].input8) {
502 int host_len = static_cast<int>(strlen(host_cases[i].input8));
[email protected]0318f922014-04-22 00:09:23503 Component in_comp(0, host_len);
504 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52505
506 out_str.clear();
[email protected]0318f922014-04-22 00:09:23507 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52508
[email protected]0318f922014-04-22 00:09:23509 bool success = CanonicalizeHost(host_cases[i].input8, in_comp, &output,
510 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52511 output.Complete();
512
513 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
[email protected]18f00cd2013-09-29 10:52:50514 success) << "for input: " << host_cases[i].input8;
515 EXPECT_EQ(std::string(host_cases[i].expected), out_str) <<
516 "for input: " << host_cases[i].input8;
517 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) <<
518 "for input: " << host_cases[i].input8;
519 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) <<
520 "for input: " << host_cases[i].input8;
[email protected]e7bba5f82013-04-10 20:10:52521 }
522
523 // Wide version.
524 if (host_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:57525 base::string16 input16(WStringToUTF16(host_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52526 int host_len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:23527 Component in_comp(0, host_len);
528 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52529
530 out_str.clear();
[email protected]0318f922014-04-22 00:09:23531 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52532
[email protected]0318f922014-04-22 00:09:23533 bool success = CanonicalizeHost(input16.c_str(), in_comp, &output,
534 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52535 output.Complete();
536
537 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
538 success);
539 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
540 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
541 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
542 }
543 }
544
545 // CanonicalizeHostVerbose()
546 for (size_t i = 0; i < arraysize(host_cases); i++) {
547 // Narrow version.
548 if (host_cases[i].input8) {
549 int host_len = static_cast<int>(strlen(host_cases[i].input8));
[email protected]0318f922014-04-22 00:09:23550 Component in_comp(0, host_len);
[email protected]e7bba5f82013-04-10 20:10:52551
552 out_str.clear();
[email protected]0318f922014-04-22 00:09:23553 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52554 CanonHostInfo host_info;
555
[email protected]0318f922014-04-22 00:09:23556 CanonicalizeHostVerbose(host_cases[i].input8, in_comp, &output,
557 &host_info);
[email protected]e7bba5f82013-04-10 20:10:52558 output.Complete();
559
560 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
561 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
562 EXPECT_EQ(host_cases[i].expected_component.begin,
563 host_info.out_host.begin);
564 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
565 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
566 BytesToHexString(host_info.address, host_info.AddressLength()));
567 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
568 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
569 host_info.num_ipv4_components);
570 }
571 }
572
573 // Wide version.
574 if (host_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:57575 base::string16 input16(WStringToUTF16(host_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:52576 int host_len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:23577 Component in_comp(0, host_len);
[email protected]e7bba5f82013-04-10 20:10:52578
579 out_str.clear();
[email protected]0318f922014-04-22 00:09:23580 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52581 CanonHostInfo host_info;
582
[email protected]0318f922014-04-22 00:09:23583 CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52584 output.Complete();
585
586 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
587 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
588 EXPECT_EQ(host_cases[i].expected_component.begin,
589 host_info.out_host.begin);
590 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
591 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
592 BytesToHexString(host_info.address, host_info.AddressLength()));
593 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
594 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
595 host_info.num_ipv4_components);
596 }
597 }
598 }
599}
600
601TEST(URLCanonTest, IPv4) {
602 IPAddressCase cases[] = {
603 // Empty is not an IP address.
[email protected]0318f922014-04-22 00:09:23604 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
605 {".", L".", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52606 // Regular IP addresses in different bases.
[email protected]0318f922014-04-22 00:09:23607 {"192.168.0.1", L"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
608 {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
609 {"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:52610 // Non-IP addresses due to invalid characters.
[email protected]0318f922014-04-22 00:09:23611 {"192.168.9.com", L"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52612 // Invalid characters for the base should be rejected.
[email protected]0318f922014-04-22 00:09:23613 {"19a.168.0.1", L"19a.168.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
614 {"0308.0250.00.01", L"0308.0250.00.01", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
615 {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52616 // If there are not enough components, the last one should fill them out.
[email protected]0318f922014-04-22 00:09:23617 {"192", L"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
618 {"0xC0a80001", L"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
619 {"030052000001", L"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
620 {"000030052000001", L"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
621 {"192.168", L"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
622 {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
623 {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
624 {"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:52625 // Too many components means not an IP address.
[email protected]0318f922014-04-22 00:09:23626 {"192.168.0.0.1", L"192.168.0.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52627 // We allow a single trailing dot.
[email protected]0318f922014-04-22 00:09:23628 {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
629 {"192.168.0.1. hello", L"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
630 {"192.168.0.1..", L"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52631 // Two dots in a row means not an IP address.
[email protected]0318f922014-04-22 00:09:23632 {"192.168..1", L"192.168..1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52633 // Any numerical overflow should be marked as BROKEN.
[email protected]0318f922014-04-22 00:09:23634 {"0x100.0", L"0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
635 {"0x100.0.0", L"0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
636 {"0x100.0.0.0", L"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
637 {"0.0x100.0.0", L"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
638 {"0.0.0x100.0", L"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""},
639 {"0.0.0.0x100", L"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN, -1, ""},
640 {"0.0.0x10000", L"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
641 {"0.0x1000000", L"0.0x1000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
642 {"0x100000000", L"0x100000000", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52643 // Repeat the previous tests, minus 1, to verify boundaries.
[email protected]0318f922014-04-22 00:09:23644 {"0xFF.0", L"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
645 {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
646 {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
647 {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
648 {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
649 {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
650 {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
651 {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
652 {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
qyearsley2bc727d2015-08-14 20:17:15653 // Old trunctations tests. They're all "BROKEN" now.
[email protected]0318f922014-04-22 00:09:23654 {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN, -1, ""},
655 {"192.168.0.257", L"192.168.0.257", "", Component(), CanonHostInfo::BROKEN, -1, ""},
656 {"192.168.0xa20001", L"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
657 {"192.015052000001", L"192.015052000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
658 {"0X12C0a80001", L"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
659 {"276.1.2", L"276.1.2", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52660 // Spaces should be rejected.
[email protected]0318f922014-04-22 00:09:23661 {"192.168.0.1 hello", L"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52662 // Very large numbers.
[email protected]0318f922014-04-22 00:09:23663 {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
664 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52665 // A number has no length limit, but long numbers can still overflow.
[email protected]0318f922014-04-22 00:09:23666 {"00000000000000000001", L"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
667 {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52668 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
[email protected]0318f922014-04-22 00:09:23669 {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
670 {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52671 // Truncation of all zeros should still result in 0.
[email protected]0318f922014-04-22 00:09:23672 {"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:52673 };
674
675 for (size_t i = 0; i < arraysize(cases); i++) {
676 // 8-bit version.
[email protected]0318f922014-04-22 00:09:23677 Component component(0, static_cast<int>(strlen(cases[i].input8)));
[email protected]e7bba5f82013-04-10 20:10:52678
679 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23680 StdStringCanonOutput output1(&out_str1);
681 CanonHostInfo host_info;
682 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52683 output1.Complete();
684
685 EXPECT_EQ(cases[i].expected_family, host_info.family);
686 EXPECT_EQ(std::string(cases[i].expected_address_hex),
687 BytesToHexString(host_info.address, host_info.AddressLength()));
688 if (host_info.family == CanonHostInfo::IPV4) {
689 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
690 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
691 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
692 EXPECT_EQ(cases[i].expected_num_ipv4_components,
693 host_info.num_ipv4_components);
694 }
695
696 // 16-bit version.
[email protected]3774f832013-06-11 21:21:57697 base::string16 input16(WStringToUTF16(cases[i].input16));
[email protected]0318f922014-04-22 00:09:23698 component = Component(0, static_cast<int>(input16.length()));
[email protected]e7bba5f82013-04-10 20:10:52699
700 std::string out_str2;
[email protected]0318f922014-04-22 00:09:23701 StdStringCanonOutput output2(&out_str2);
702 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52703 output2.Complete();
704
705 EXPECT_EQ(cases[i].expected_family, host_info.family);
706 EXPECT_EQ(std::string(cases[i].expected_address_hex),
707 BytesToHexString(host_info.address, host_info.AddressLength()));
708 if (host_info.family == CanonHostInfo::IPV4) {
709 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
710 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
711 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
712 EXPECT_EQ(cases[i].expected_num_ipv4_components,
713 host_info.num_ipv4_components);
714 }
715 }
716}
717
718TEST(URLCanonTest, IPv6) {
719 IPAddressCase cases[] = {
720 // Empty is not an IP address.
[email protected]0318f922014-04-22 00:09:23721 {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52722 // Non-IPs with [:] characters are marked BROKEN.
[email protected]0318f922014-04-22 00:09:23723 {":", 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, ""},
729 {"[:]", L"[:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52730 // Regular IP address is invalid without bounding '[' and ']'.
[email protected]0318f922014-04-22 00:09:23731 {"2001:db8::1", L"2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
732 {"[2001:db8::1", L"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""},
733 {"2001:db8::1]", L"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52734 // Regular IP addresses.
[email protected]0318f922014-04-22 00:09:23735 {"[::]", L"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
736 {"[::1]", L"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
737 {"[1::]", L"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
[email protected]e7bba5f82013-04-10 20:10:52738
739 // Leading zeros should be stripped.
[email protected]0318f922014-04-22 00:09:23740 {"[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:52741
742 // Upper case letters should be lowercased.
[email protected]0318f922014-04-22 00:09:23743 {"[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:52744
745 // The same address can be written with different contractions, but should
746 // get canonicalized to the same thing.
[email protected]0318f922014-04-22 00:09:23747 {"[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"},
748 {"[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:52749
750 // Addresses with embedded IPv4.
[email protected]0318f922014-04-22 00:09:23751 {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
752 {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
753 {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
754 {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
755 {"[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:52756
757 // IPv4 with last component missing.
[email protected]0318f922014-04-22 00:09:23758 {"[::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:52759
760 // IPv4 using hex.
761 // TODO(eroman): Should this format be disallowed?
[email protected]0318f922014-04-22 00:09:23762 {"[::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:52763
764 // There may be zeros surrounding the "::" contraction.
[email protected]0318f922014-04-22 00:09:23765 {"[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:52766
[email protected]0318f922014-04-22 00:09:23767 {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
[email protected]e7bba5f82013-04-10 20:10:52768
qyearsley2bc727d2015-08-14 20:17:15769 // Can only have one "::" contraction in an IPv6 string literal.
[email protected]0318f922014-04-22 00:09:23770 {"[2001::db8::1]", L"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15771 // No more than 2 consecutive ':'s.
[email protected]0318f922014-04-22 00:09:23772 {"[2001:db8:::1]", L"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
773 {"[:::]", L"[:::]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15774 // Non-IP addresses due to invalid characters.
[email protected]0318f922014-04-22 00:09:23775 {"[2001::.com]", L"[2001::.com]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
qyearsley2bc727d2015-08-14 20:17:15776 // If there are not enough components, the last one should fill them out.
[email protected]e7bba5f82013-04-10 20:10:52777 // ... omitted at this time ...
qyearsley2bc727d2015-08-14 20:17:15778 // Too many components means not an IP address. Similarly, with too few
779 // if using IPv4 compat or mapped addresses.
[email protected]0318f922014-04-22 00:09:23780 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
781 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
782 {"[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:52783 // Too many bits (even though 8 comonents, the last one holds 32 bits).
[email protected]0318f922014-04-22 00:09:23784 {"[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:52785
786 // Too many bits specified -- the contraction would have to be zero-length
787 // to not exceed 128 bits.
[email protected]0318f922014-04-22 00:09:23788 {"[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:52789
790 // The contraction is for 16 bits of zero.
[email protected]0318f922014-04-22 00:09:23791 {"[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:52792
793 // Cannot have a trailing colon.
[email protected]0318f922014-04-22 00:09:23794 {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
795 {"[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:52796
797 // Cannot have negative numbers.
[email protected]0318f922014-04-22 00:09:23798 {"[-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:52799
800 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
801 // The scope_id should be included in the canonicalized URL, and is an
802 // unsigned decimal number.
803
804 // Invalid because no ID was given after the percent.
805
806 // Don't allow scope-id
[email protected]0318f922014-04-22 00:09:23807 {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
808 {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
809 {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
810 {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
811 {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52812
813 // Don't allow leading or trailing colons.
[email protected]0318f922014-04-22 00:09:23814 {"[: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, ""},
816 {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52817
818 // We allow a single trailing dot.
819 // ... omitted at this time ...
820 // Two dots in a row means not an IP address.
[email protected]0318f922014-04-22 00:09:23821 {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52822 // Any non-first components get truncated to one byte.
823 // ... omitted at this time ...
824 // Spaces should be rejected.
[email protected]0318f922014-04-22 00:09:23825 {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""},
[email protected]e7bba5f82013-04-10 20:10:52826 };
827
828 for (size_t i = 0; i < arraysize(cases); i++) {
829 // 8-bit version.
[email protected]0318f922014-04-22 00:09:23830 Component component(0, static_cast<int>(strlen(cases[i].input8)));
[email protected]e7bba5f82013-04-10 20:10:52831
832 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23833 StdStringCanonOutput output1(&out_str1);
834 CanonHostInfo host_info;
835 CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52836 output1.Complete();
837
838 EXPECT_EQ(cases[i].expected_family, host_info.family);
839 EXPECT_EQ(std::string(cases[i].expected_address_hex),
840 BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
841 if (host_info.family == CanonHostInfo::IPV6) {
842 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
843 EXPECT_EQ(cases[i].expected_component.begin,
844 host_info.out_host.begin);
845 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
846 }
847
848 // 16-bit version.
[email protected]3774f832013-06-11 21:21:57849 base::string16 input16(WStringToUTF16(cases[i].input16));
[email protected]0318f922014-04-22 00:09:23850 component = Component(0, static_cast<int>(input16.length()));
[email protected]e7bba5f82013-04-10 20:10:52851
852 std::string out_str2;
[email protected]0318f922014-04-22 00:09:23853 StdStringCanonOutput output2(&out_str2);
854 CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52855 output2.Complete();
856
857 EXPECT_EQ(cases[i].expected_family, host_info.family);
858 EXPECT_EQ(std::string(cases[i].expected_address_hex),
859 BytesToHexString(host_info.address, host_info.AddressLength()));
860 if (host_info.family == CanonHostInfo::IPV6) {
861 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
862 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
863 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
864 }
865 }
866}
867
868TEST(URLCanonTest, IPEmpty) {
869 std::string out_str1;
[email protected]0318f922014-04-22 00:09:23870 StdStringCanonOutput output1(&out_str1);
871 CanonHostInfo host_info;
[email protected]e7bba5f82013-04-10 20:10:52872
873 // This tests tests.
874 const char spec[] = "192.168.0.1";
[email protected]0318f922014-04-22 00:09:23875 CanonicalizeIPAddress(spec, Component(), &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52876 EXPECT_FALSE(host_info.IsIPAddress());
877
[email protected]0318f922014-04-22 00:09:23878 CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info);
[email protected]e7bba5f82013-04-10 20:10:52879 EXPECT_FALSE(host_info.IsIPAddress());
880}
881
882TEST(URLCanonTest, UserInfo) {
883 // Note that the canonicalizer should escape and treat empty components as
884 // not being there.
885
886 // We actually parse a full input URL so we can get the initial components.
887 struct UserComponentCase {
888 const char* input;
889 const char* expected;
[email protected]0318f922014-04-22 00:09:23890 Component expected_username;
891 Component expected_password;
[email protected]e7bba5f82013-04-10 20:10:52892 bool expected_success;
893 } user_info_cases[] = {
[email protected]0318f922014-04-22 00:09:23894 {"http://user:[email protected]/", "user:pass@", Component(0, 4), Component(5, 4), true},
895 {"http://@host.com/", "", Component(0, -1), Component(0, -1), true},
896 {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true},
897 {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true},
898 {"http://:[email protected]/", ":foo@", Component(0, 0), Component(1, 3), true},
899 {"http://^ :$\[email protected]/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true},
900 {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true},
901 {"http://%2540:[email protected]/", "%2540:bar@", Component(0, 5), Component(6, 3), true },
[email protected]e7bba5f82013-04-10 20:10:52902
qyearsley2bc727d2015-08-14 20:17:15903 // IE7 compatibility: old versions allowed backslashes in usernames, but
[email protected]e7bba5f82013-04-10 20:10:52904 // IE7 does not. We disallow it as well.
[email protected]0318f922014-04-22 00:09:23905 {"ftp://me\\mydomain:[email protected]/", "", Component(0, -1), Component(0, -1), true},
[email protected]e7bba5f82013-04-10 20:10:52906 };
907
viettrungluu4b6915862014-10-16 03:42:49908 for (size_t i = 0; i < arraysize(user_info_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52909 int url_len = static_cast<int>(strlen(user_info_cases[i].input));
[email protected]0318f922014-04-22 00:09:23910 Parsed parsed;
911 ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
912 Component out_user, out_pass;
[email protected]e7bba5f82013-04-10 20:10:52913 std::string out_str;
[email protected]0318f922014-04-22 00:09:23914 StdStringCanonOutput output1(&out_str);
[email protected]e7bba5f82013-04-10 20:10:52915
[email protected]0318f922014-04-22 00:09:23916 bool success = CanonicalizeUserInfo(user_info_cases[i].input,
917 parsed.username,
918 user_info_cases[i].input,
919 parsed.password,
920 &output1,
921 &out_user,
922 &out_pass);
[email protected]e7bba5f82013-04-10 20:10:52923 output1.Complete();
924
925 EXPECT_EQ(user_info_cases[i].expected_success, success);
926 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
927 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
928 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
929 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
930 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
931
932 // Now try the wide version
933 out_str.clear();
[email protected]0318f922014-04-22 00:09:23934 StdStringCanonOutput output2(&out_str);
[email protected]3774f832013-06-11 21:21:57935 base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
[email protected]0318f922014-04-22 00:09:23936 success = CanonicalizeUserInfo(wide_input.c_str(),
937 parsed.username,
938 wide_input.c_str(),
939 parsed.password,
940 &output2,
941 &out_user,
942 &out_pass);
[email protected]e7bba5f82013-04-10 20:10:52943 output2.Complete();
944
945 EXPECT_EQ(user_info_cases[i].expected_success, success);
946 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
947 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
948 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
949 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
950 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
951 }
952}
953
954TEST(URLCanonTest, Port) {
955 // We only need to test that the number gets properly put into the output
956 // buffer. The parser unit tests will test scanning the number correctly.
957 //
958 // Note that the CanonicalizePort will always prepend a colon to the output
qyearsley2bc727d2015-08-14 20:17:15959 // to separate it from the colon that it assumes precedes it.
[email protected]e7bba5f82013-04-10 20:10:52960 struct PortCase {
961 const char* input;
962 int default_port;
963 const char* expected;
[email protected]0318f922014-04-22 00:09:23964 Component expected_component;
[email protected]e7bba5f82013-04-10 20:10:52965 bool expected_success;
966 } port_cases[] = {
967 // Invalid input should be copied w/ failure.
[email protected]0318f922014-04-22 00:09:23968 {"as df", 80, ":as%20df", Component(1, 7), false},
969 {"-2", 80, ":-2", Component(1, 2), false},
[email protected]e7bba5f82013-04-10 20:10:52970 // Default port should be omitted.
[email protected]0318f922014-04-22 00:09:23971 {"80", 80, "", Component(0, -1), true},
972 {"8080", 80, ":8080", Component(1, 4), true},
[email protected]e7bba5f82013-04-10 20:10:52973 // PORT_UNSPECIFIED should mean always keep the port.
[email protected]0318f922014-04-22 00:09:23974 {"80", PORT_UNSPECIFIED, ":80", Component(1, 2), true},
[email protected]e7bba5f82013-04-10 20:10:52975 };
976
viettrungluu4b6915862014-10-16 03:42:49977 for (size_t i = 0; i < arraysize(port_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52978 int url_len = static_cast<int>(strlen(port_cases[i].input));
[email protected]0318f922014-04-22 00:09:23979 Component in_comp(0, url_len);
980 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:52981 std::string out_str;
[email protected]0318f922014-04-22 00:09:23982 StdStringCanonOutput output1(&out_str);
983 bool success = CanonicalizePort(port_cases[i].input,
984 in_comp,
985 port_cases[i].default_port,
986 &output1,
987 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:52988 output1.Complete();
989
990 EXPECT_EQ(port_cases[i].expected_success, success);
991 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
992 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
993 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
994
995 // Now try the wide version
996 out_str.clear();
[email protected]0318f922014-04-22 00:09:23997 StdStringCanonOutput output2(&out_str);
[email protected]3774f832013-06-11 21:21:57998 base::string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
[email protected]0318f922014-04-22 00:09:23999 success = CanonicalizePort(wide_input.c_str(),
1000 in_comp,
1001 port_cases[i].default_port,
1002 &output2,
1003 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521004 output2.Complete();
1005
1006 EXPECT_EQ(port_cases[i].expected_success, success);
1007 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
1008 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
1009 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
1010 }
1011}
1012
1013TEST(URLCanonTest, Path) {
1014 DualComponentCase path_cases[] = {
1015 // ----- path collapsing tests -----
[email protected]0318f922014-04-22 00:09:231016 {"/././foo", L"/././foo", "/foo", Component(0, 4), true},
1017 {"/./.foo", L"/./.foo", "/.foo", Component(0, 5), true},
1018 {"/foo/.", L"/foo/.", "/foo/", Component(0, 5), true},
1019 {"/foo/./", L"/foo/./", "/foo/", Component(0, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521020 // double dots followed by a slash or the end of the string count
[email protected]0318f922014-04-22 00:09:231021 {"/foo/bar/..", L"/foo/bar/..", "/foo/", Component(0, 5), true},
1022 {"/foo/bar/../", L"/foo/bar/../", "/foo/", Component(0, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521023 // don't count double dots when they aren't followed by a slash
[email protected]0318f922014-04-22 00:09:231024 {"/foo/..bar", L"/foo/..bar", "/foo/..bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521025 // some in the middle
[email protected]0318f922014-04-22 00:09:231026 {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", Component(0, 8), true},
1027 {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", Component(0, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521028 // we should not be able to go above the root
[email protected]0318f922014-04-22 00:09:231029 {"/foo/../../..", L"/foo/../../..", "/", Component(0, 1), true},
1030 {"/foo/../../../ton", L"/foo/../../../ton", "/ton", Component(0, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521031 // escaped dots should be unescaped and treated the same as dots
[email protected]0318f922014-04-22 00:09:231032 {"/foo/%2e", L"/foo/%2e", "/foo/", Component(0, 5), true},
1033 {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", Component(0, 8), true},
1034 {"/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:521035 // Multiple slashes in a row should be preserved and treated like empty
1036 // directory names.
[email protected]0318f922014-04-22 00:09:231037 {"////../..", L"////../..", "//", Component(0, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521038
1039 // ----- escaping tests -----
[email protected]0318f922014-04-22 00:09:231040 {"/foo", L"/foo", "/foo", Component(0, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521041 // Valid escape sequence
[email protected]0318f922014-04-22 00:09:231042 {"/%20foo", L"/%20foo", "/%20foo", Component(0, 7), true},
[email protected]e7bba5f82013-04-10 20:10:521043 // Invalid escape sequence we should pass through unchanged.
[email protected]0318f922014-04-22 00:09:231044 {"/foo%", L"/foo%", "/foo%", Component(0, 5), true},
1045 {"/foo%2", L"/foo%2", "/foo%2", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521046 // Invalid escape sequence: bad characters should be treated the same as
1047 // the sourrounding text, not as escaped (in this case, UTF-8).
[email protected]0318f922014-04-22 00:09:231048 {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", Component(0, 10), true},
1049 {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", Component(0, 16), true},
1050 {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22), true},
[email protected]e7bba5f82013-04-10 20:10:521051 // Regular characters that are escaped should be unescaped
[email protected]0318f922014-04-22 00:09:231052 {"/foo%41%7a", L"/foo%41%7a", "/fooAz", Component(0, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521053 // Funny characters that are unescaped should be escaped
[email protected]0318f922014-04-22 00:09:231054 {"/foo\x09\x91%91", NULL, "/foo%09%91%91", Component(0, 13), true},
1055 {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true},
[email protected]e7bba5f82013-04-10 20:10:521056 // Invalid characters that are escaped should cause a failure.
[email protected]0318f922014-04-22 00:09:231057 {"/foo%00%51", L"/foo%00%51", "/foo%00Q", Component(0, 8), false},
[email protected]e7bba5f82013-04-10 20:10:521058 // Some characters should be passed through unchanged regardless of esc.
[email protected]0318f922014-04-22 00:09:231059 {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13), true},
[email protected]e7bba5f82013-04-10 20:10:521060 // Characters that are properly escaped should not have the case changed
1061 // of hex letters.
[email protected]0318f922014-04-22 00:09:231062 {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13), true},
[email protected]e7bba5f82013-04-10 20:10:521063 // Funny characters that are unescaped should be escaped
[email protected]0318f922014-04-22 00:09:231064 {"/foo\tbar", L"/foo\tbar", "/foo%09bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521065 // Backslashes should get converted to forward slashes
[email protected]0318f922014-04-22 00:09:231066 {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", Component(0, 8), true},
[email protected]e7bba5f82013-04-10 20:10:521067 // Hashes found in paths (possibly only when the caller explicitly sets
1068 // the path on an already-parsed URL) should be escaped.
[email protected]0318f922014-04-22 00:09:231069 {"/foo#bar", L"/foo#bar", "/foo%23bar", Component(0, 10), true},
[email protected]e7bba5f82013-04-10 20:10:521070 // %7f should be allowed and %3D should not be unescaped (these were wrong
1071 // in a previous version).
[email protected]0318f922014-04-22 00:09:231072 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true},
[email protected]e7bba5f82013-04-10 20:10:521073 // @ should be passed through unchanged (escaped or unescaped).
[email protected]0318f922014-04-22 00:09:231074 {"/@asdf%40", L"/@asdf%40", "/@asdf%40", Component(0, 9), true},
pkasting69820362015-09-22 01:54:051075 // Nested escape sequences should result in escaping the leading '%' if
1076 // unescaping would result in a new escape sequence.
1077 {"/%A%42", L"/%A%42", "/%25AB", Component(0, 6), true},
1078 {"/%%41B", L"/%%41B", "/%25AB", Component(0, 6), true},
1079 {"/%%41%42", L"/%%41%42", "/%25AB", Component(0, 6), true},
1080 // Make sure truncated "nested" escapes don't result in reading off the
1081 // string end.
1082 {"/%%41", L"/%%41", "/%A", Component(0, 3), true},
1083 // Don't unescape the leading '%' if unescaping doesn't result in a valid
1084 // new escape sequence.
1085 {"/%%470", L"/%%470", "/%G0", Component(0, 4), true},
1086 {"/%%2D%41", L"/%%2D%41", "/%-A", Component(0, 4), true},
1087 // Don't erroneously downcast a UTF-16 charater in a way that makes it
1088 // look like part of an escape sequence.
1089 {NULL, L"/%%41\x0130", "/%A%C4%B0", Component(0, 9), true},
[email protected]e7bba5f82013-04-10 20:10:521090
1091 // ----- encoding tests -----
1092 // Basic conversions
[email protected]0318f922014-04-22 00:09:231093 {"/\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:521094 // Invalid unicode characters should fail. We only do validation on
1095 // UTF-16 input, so this doesn't happen on 8-bit.
[email protected]0318f922014-04-22 00:09:231096 {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", Component(0, 13), true},
1097 {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false},
[email protected]e7bba5f82013-04-10 20:10:521098 };
1099
1100 for (size_t i = 0; i < arraysize(path_cases); i++) {
1101 if (path_cases[i].input8) {
1102 int len = static_cast<int>(strlen(path_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231103 Component in_comp(0, len);
1104 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521105 std::string out_str;
[email protected]0318f922014-04-22 00:09:231106 StdStringCanonOutput output(&out_str);
1107 bool success =
1108 CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521109 output.Complete();
1110
1111 EXPECT_EQ(path_cases[i].expected_success, success);
1112 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1113 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1114 EXPECT_EQ(path_cases[i].expected, out_str);
1115 }
1116
1117 if (path_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571118 base::string16 input16(WStringToUTF16(path_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521119 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231120 Component in_comp(0, len);
1121 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521122 std::string out_str;
[email protected]0318f922014-04-22 00:09:231123 StdStringCanonOutput output(&out_str);
[email protected]e7bba5f82013-04-10 20:10:521124
[email protected]0318f922014-04-22 00:09:231125 bool success =
1126 CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521127 output.Complete();
1128
1129 EXPECT_EQ(path_cases[i].expected_success, success);
1130 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1131 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1132 EXPECT_EQ(path_cases[i].expected, out_str);
1133 }
1134 }
1135
1136 // Manual test: embedded NULLs should be escaped and the URL should be marked
1137 // as invalid.
1138 const char path_with_null[] = "/ab\0c";
[email protected]0318f922014-04-22 00:09:231139 Component in_comp(0, 5);
1140 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521141
1142 std::string out_str;
[email protected]0318f922014-04-22 00:09:231143 StdStringCanonOutput output(&out_str);
1144 bool success = CanonicalizePath(path_with_null, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521145 output.Complete();
1146 EXPECT_FALSE(success);
1147 EXPECT_EQ("/ab%00c", out_str);
1148}
1149
1150TEST(URLCanonTest, Query) {
1151 struct QueryCase {
1152 const char* input8;
1153 const wchar_t* input16;
[email protected]e7bba5f82013-04-10 20:10:521154 const char* expected;
1155 } query_cases[] = {
[email protected]847aaab82014-05-07 14:05:461156 // Regular ASCII case.
1157 {"foo=bar", L"foo=bar", "?foo=bar"},
[email protected]e7bba5f82013-04-10 20:10:521158 // Allow question marks in the query without escaping
[email protected]847aaab82014-05-07 14:05:461159 {"as?df", L"as?df", "?as?df"},
[email protected]e7bba5f82013-04-10 20:10:521160 // Always escape '#' since it would mark the ref.
[email protected]847aaab82014-05-07 14:05:461161 {"as#df", L"as#df", "?as%23df"},
[email protected]e7bba5f82013-04-10 20:10:521162 // Escape some questionable 8-bit characters, but never unescape.
[email protected]847aaab82014-05-07 14:05:461163 {"\x02hello\x7f bye", L"\x02hello\x7f bye", "?%02hello%7F%20bye"},
1164 {"%40%41123", L"%40%41123", "?%40%41123"},
[email protected]e7bba5f82013-04-10 20:10:521165 // Chinese input/output
[email protected]847aaab82014-05-07 14:05:461166 {"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:521167 // Invalid UTF-8/16 input should be replaced with invalid characters.
[email protected]847aaab82014-05-07 14:05:461168 {"q=\xed\xed", L"q=\xd800\xd800", "?q=%EF%BF%BD%EF%BF%BD"},
[email protected]e7bba5f82013-04-10 20:10:521169 // Don't allow < or > because sometimes they are used for XSS if the
1170 // URL is echoed in content. Firefox does this, IE doesn't.
[email protected]847aaab82014-05-07 14:05:461171 {"q=<asdf>", L"q=<asdf>", "?q=%3Casdf%3E"},
[email protected]e7bba5f82013-04-10 20:10:521172 // Escape double quotemarks in the query.
[email protected]847aaab82014-05-07 14:05:461173 {"q=\"asdf\"", L"q=\"asdf\"", "?q=%22asdf%22"},
[email protected]e7bba5f82013-04-10 20:10:521174 };
1175
viettrungluu4b6915862014-10-16 03:42:491176 for (size_t i = 0; i < arraysize(query_cases); i++) {
[email protected]0318f922014-04-22 00:09:231177 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521178
[email protected]e7bba5f82013-04-10 20:10:521179 if (query_cases[i].input8) {
1180 int len = static_cast<int>(strlen(query_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231181 Component in_comp(0, len);
[email protected]e7bba5f82013-04-10 20:10:521182 std::string out_str;
1183
[email protected]0318f922014-04-22 00:09:231184 StdStringCanonOutput output(&out_str);
[email protected]847aaab82014-05-07 14:05:461185 CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output,
[email protected]0318f922014-04-22 00:09:231186 &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521187 output.Complete();
1188
1189 EXPECT_EQ(query_cases[i].expected, out_str);
1190 }
1191
1192 if (query_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571193 base::string16 input16(WStringToUTF16(query_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521194 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231195 Component in_comp(0, len);
[email protected]e7bba5f82013-04-10 20:10:521196 std::string out_str;
1197
[email protected]0318f922014-04-22 00:09:231198 StdStringCanonOutput output(&out_str);
[email protected]847aaab82014-05-07 14:05:461199 CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521200 output.Complete();
1201
1202 EXPECT_EQ(query_cases[i].expected, out_str);
1203 }
1204 }
1205
1206 // Extra test for input with embedded NULL;
1207 std::string out_str;
[email protected]0318f922014-04-22 00:09:231208 StdStringCanonOutput output(&out_str);
1209 Component out_comp;
1210 CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521211 output.Complete();
1212 EXPECT_EQ("?a%20%00z%01", out_str);
1213}
1214
1215TEST(URLCanonTest, Ref) {
1216 // Refs are trivial, it just checks the encoding.
1217 DualComponentCase ref_cases[] = {
1218 // Regular one, we shouldn't escape spaces, et al.
[email protected]0318f922014-04-22 00:09:231219 {"hello, world", L"hello, world", "#hello, world", Component(1, 12), true},
[email protected]e7bba5f82013-04-10 20:10:521220 // UTF-8/wide input should be preserved
[email protected]0318f922014-04-22 00:09:231221 {"\xc2\xa9", L"\xa9", "#\xc2\xa9", Component(1, 2), true},
[email protected]e7bba5f82013-04-10 20:10:521222 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
[email protected]0318f922014-04-22 00:09:231223 {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", Component(1, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521224 // Escaping should be preserved unchanged, even invalid ones
[email protected]0318f922014-04-22 00:09:231225 {"%41%a", L"%41%a", "#%41%a", Component(1, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521226 // Invalid UTF-8/16 input should be flagged and the input made valid
[email protected]0318f922014-04-22 00:09:231227 {"\xc2", NULL, "#\xef\xbf\xbd", Component(1, 3), true},
1228 {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", Component(1, 6), true},
[email protected]e7bba5f82013-04-10 20:10:521229 // Test a Unicode invalid character.
[email protected]0318f922014-04-22 00:09:231230 {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", Component(1, 4), true},
[email protected]e7bba5f82013-04-10 20:10:521231 // Refs can have # signs and we should preserve them.
[email protected]0318f922014-04-22 00:09:231232 {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", Component(1, 9), true},
1233 {"#asdf", L"#asdf", "##asdf", Component(1, 5), true},
[email protected]e7bba5f82013-04-10 20:10:521234 };
1235
1236 for (size_t i = 0; i < arraysize(ref_cases); i++) {
1237 // 8-bit input
1238 if (ref_cases[i].input8) {
1239 int len = static_cast<int>(strlen(ref_cases[i].input8));
[email protected]0318f922014-04-22 00:09:231240 Component in_comp(0, len);
1241 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521242
1243 std::string out_str;
[email protected]0318f922014-04-22 00:09:231244 StdStringCanonOutput output(&out_str);
1245 CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521246 output.Complete();
1247
1248 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1249 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1250 EXPECT_EQ(ref_cases[i].expected, out_str);
1251 }
1252
1253 // 16-bit input
1254 if (ref_cases[i].input16) {
[email protected]3774f832013-06-11 21:21:571255 base::string16 input16(WStringToUTF16(ref_cases[i].input16));
[email protected]e7bba5f82013-04-10 20:10:521256 int len = static_cast<int>(input16.length());
[email protected]0318f922014-04-22 00:09:231257 Component in_comp(0, len);
1258 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521259
1260 std::string out_str;
[email protected]0318f922014-04-22 00:09:231261 StdStringCanonOutput output(&out_str);
1262 CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521263 output.Complete();
1264
1265 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1266 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1267 EXPECT_EQ(ref_cases[i].expected, out_str);
1268 }
1269 }
1270
1271 // Try one with an embedded NULL. It should be stripped.
1272 const char null_input[5] = "ab\x00z";
[email protected]0318f922014-04-22 00:09:231273 Component null_input_component(0, 4);
1274 Component out_comp;
[email protected]e7bba5f82013-04-10 20:10:521275
1276 std::string out_str;
[email protected]0318f922014-04-22 00:09:231277 StdStringCanonOutput output(&out_str);
1278 CanonicalizeRef(null_input, null_input_component, &output, &out_comp);
[email protected]e7bba5f82013-04-10 20:10:521279 output.Complete();
1280
1281 EXPECT_EQ(1, out_comp.begin);
1282 EXPECT_EQ(3, out_comp.len);
1283 EXPECT_EQ("#abz", out_str);
1284}
1285
1286TEST(URLCanonTest, CanonicalizeStandardURL) {
1287 // The individual component canonicalize tests should have caught the cases
1288 // for each of those components. Here, we just need to test that the various
1289 // parts are included or excluded properly, and have the correct separators.
1290 struct URLCase {
1291 const char* input;
1292 const char* expected;
1293 bool expected_success;
1294 } cases[] = {
1295 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1296 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1297 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1298 {"http:////////user:@google.com:99?foo", "http://[email protected]:99/?foo", true},
1299 {"www.google.com", ":www.google.com/", true},
1300 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1301 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1302 {"http://user:pass@/", "http://user:pass@/", false},
1303 {"http://%25DOMAIN:[email protected]/", "http://%25DOMAIN:[email protected]/", true},
1304
1305 // Backslashes should get converted to forward slashes.
1306 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1307
1308 // Busted refs shouldn't make the whole thing fail.
1309 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1310
1311 // Basic port tests.
1312 {"http://foo:80/", "http://foo/", true},
1313 {"http://foo:81/", "http://foo:81/", true},
1314 {"httpa://foo:80/", "httpa://foo:80/", true},
1315 {"http://foo:-80/", "http://foo:-80/", false},
1316
1317 {"https://foo:443/", "https://foo/", true},
1318 {"https://foo:80/", "https://foo:80/", true},
1319 {"ftp://foo:21/", "ftp://foo/", true},
1320 {"ftp://foo:80/", "ftp://foo:80/", true},
1321 {"gopher://foo:70/", "gopher://foo/", true},
1322 {"gopher://foo:443/", "gopher://foo:443/", true},
1323 {"ws://foo:80/", "ws://foo/", true},
1324 {"ws://foo:81/", "ws://foo:81/", true},
1325 {"ws://foo:443/", "ws://foo:443/", true},
1326 {"ws://foo:815/", "ws://foo:815/", true},
1327 {"wss://foo:80/", "wss://foo:80/", true},
1328 {"wss://foo:81/", "wss://foo:81/", true},
1329 {"wss://foo:443/", "wss://foo/", true},
1330 {"wss://foo:815/", "wss://foo:815/", true},
brettw1141951b2015-11-26 00:29:351331
1332 // This particular code path ends up "backing up" to replace an invalid
1333 // host ICU generated with an escaped version. Test that in the context
1334 // of a full URL to make sure the backing up doesn't mess up the non-host
1335 // parts of the URL. "EF B9 AA" is U+FE6A which is a type of percent that
1336 // ICU will convert to an ASCII one, generating "%81".
1337 {"ws:)W\x1eW\xef\xb9\xaa""81:80/", "ws://%29w%1ew%81/", false},
[email protected]e7bba5f82013-04-10 20:10:521338 };
1339
viettrungluu4b6915862014-10-16 03:42:491340 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521341 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231342 Parsed parsed;
1343 ParseStandardURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521344
[email protected]0318f922014-04-22 00:09:231345 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521346 std::string out_str;
[email protected]0318f922014-04-22 00:09:231347 StdStringCanonOutput output(&out_str);
1348 bool success = CanonicalizeStandardURL(
[email protected]e7bba5f82013-04-10 20:10:521349 cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
1350 output.Complete();
1351
1352 EXPECT_EQ(cases[i].expected_success, success);
1353 EXPECT_EQ(cases[i].expected, out_str);
1354 }
1355}
1356
1357// The codepath here is the same as for regular canonicalization, so we just
1358// need to test that things are replaced or not correctly.
1359TEST(URLCanonTest, ReplaceStandardURL) {
1360 ReplaceCase replace_cases[] = {
1361 // Common case of truncating the path.
1362 {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
1363 // Replace everything
1364 {"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"},
1365 // Replace nothing
1366 {"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:151367 // Replace scheme with filesystem. The result is garbage, but you asked
[email protected]e7bba5f82013-04-10 20:10:521368 // for it.
1369 {"http://a:[email protected]:22/foo?baz@cat", "filesystem", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem://a:[email protected]:22/foo?baz@cat"},
1370 };
1371
1372 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1373 const ReplaceCase& cur = replace_cases[i];
1374 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231375 Parsed parsed;
1376 ParseStandardURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521377
[email protected]0318f922014-04-22 00:09:231378 Replacements<char> r;
1379 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521380
1381 // Note that for the scheme we pass in a different clear function since
1382 // there is no function to clear the scheme.
1383 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1384 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1385 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1386 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1387 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1388 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1389 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1390 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1391
1392 std::string out_str;
[email protected]0318f922014-04-22 00:09:231393 StdStringCanonOutput output(&out_str);
1394 Parsed out_parsed;
1395 ReplaceStandardURL(replace_cases[i].base, parsed, r, NULL, &output,
1396 &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521397 output.Complete();
1398
1399 EXPECT_EQ(replace_cases[i].expected, out_str);
1400 }
1401
1402 // The path pointer should be ignored if the address is invalid.
1403 {
1404 const char src[] = "http://www.google.com/here_is_the_path";
1405 int src_len = static_cast<int>(strlen(src));
1406
[email protected]0318f922014-04-22 00:09:231407 Parsed parsed;
1408 ParseStandardURL(src, src_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521409
1410 // Replace the path to 0 length string. By using 1 as the string address,
1411 // the test should get an access violation if it tries to dereference it.
[email protected]0318f922014-04-22 00:09:231412 Replacements<char> r;
1413 r.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0));
[email protected]e7bba5f82013-04-10 20:10:521414 std::string out_str1;
[email protected]0318f922014-04-22 00:09:231415 StdStringCanonOutput output1(&out_str1);
1416 Parsed new_parsed;
1417 ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
[email protected]e7bba5f82013-04-10 20:10:521418 output1.Complete();
1419 EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
1420
1421 // Same with an "invalid" path.
[email protected]0318f922014-04-22 00:09:231422 r.SetPath(reinterpret_cast<char*>(0x00000001), Component());
[email protected]e7bba5f82013-04-10 20:10:521423 std::string out_str2;
[email protected]0318f922014-04-22 00:09:231424 StdStringCanonOutput output2(&out_str2);
1425 ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
[email protected]e7bba5f82013-04-10 20:10:521426 output2.Complete();
1427 EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
1428 }
1429}
1430
1431TEST(URLCanonTest, ReplaceFileURL) {
1432 ReplaceCase replace_cases[] = {
1433 // Replace everything
1434 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1435 // Replace nothing
1436 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1437 // Clear non-path components (common)
1438 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
1439 // Replace path with something that doesn't begin with a slash and make
1440 // sure it gets added properly.
1441 {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1442 {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1443 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
1444 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
1445 {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1446 // Replace scheme -- shouldn't do anything.
1447 {"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1448 };
1449
1450 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1451 const ReplaceCase& cur = replace_cases[i];
1452 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231453 Parsed parsed;
1454 ParseFileURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521455
[email protected]0318f922014-04-22 00:09:231456 Replacements<char> r;
1457 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521458 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1459 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1460 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1461 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1462 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1463 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1464 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1465 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1466
1467 std::string out_str;
[email protected]0318f922014-04-22 00:09:231468 StdStringCanonOutput output(&out_str);
1469 Parsed out_parsed;
1470 ReplaceFileURL(cur.base, parsed, r, NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521471 output.Complete();
1472
1473 EXPECT_EQ(replace_cases[i].expected, out_str);
1474 }
1475}
1476
1477TEST(URLCanonTest, ReplaceFileSystemURL) {
1478 ReplaceCase replace_cases[] = {
1479 // Replace everything in the outer URL.
1480 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1481 // Replace nothing
1482 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"},
1483 // Clear non-path components (common)
1484 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"},
1485 // Replace path with something that doesn't begin with a slash and make
1486 // sure it gets added properly.
1487 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"},
1488 // Replace scheme -- shouldn't do anything.
1489 {"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"},
1490 // Replace username -- shouldn't do anything.
1491 {"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"},
1492 // Replace password -- shouldn't do anything.
1493 {"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"},
1494 // Replace host -- shouldn't do anything.
1495 {"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"},
1496 // Replace port -- shouldn't do anything.
1497 {"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"},
1498 };
1499
1500 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1501 const ReplaceCase& cur = replace_cases[i];
1502 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231503 Parsed parsed;
1504 ParseFileSystemURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521505
[email protected]0318f922014-04-22 00:09:231506 Replacements<char> r;
1507 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521508 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1509 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1510 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1511 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1512 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1513 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1514 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1515 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1516
1517 std::string out_str;
[email protected]0318f922014-04-22 00:09:231518 StdStringCanonOutput output(&out_str);
1519 Parsed out_parsed;
1520 ReplaceFileSystemURL(cur.base, parsed, r, NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521521 output.Complete();
1522
1523 EXPECT_EQ(replace_cases[i].expected, out_str);
1524 }
1525}
1526
1527TEST(URLCanonTest, ReplacePathURL) {
1528 ReplaceCase replace_cases[] = {
1529 // Replace everything
1530 {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
1531 // Replace nothing
1532 {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
1533 // Replace one or the other
1534 {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
1535 {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
1536 {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
1537 };
1538
1539 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1540 const ReplaceCase& cur = replace_cases[i];
1541 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231542 Parsed parsed;
1543 ParsePathURL(cur.base, base_len, false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521544
[email protected]0318f922014-04-22 00:09:231545 Replacements<char> r;
1546 typedef Replacements<char> R; // Clean up syntax.
[email protected]e7bba5f82013-04-10 20:10:521547 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1548 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1549 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1550 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1551 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1552 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1553 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1554 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1555
1556 std::string out_str;
[email protected]0318f922014-04-22 00:09:231557 StdStringCanonOutput output(&out_str);
1558 Parsed out_parsed;
1559 ReplacePathURL(cur.base, parsed, r, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521560 output.Complete();
1561
1562 EXPECT_EQ(replace_cases[i].expected, out_str);
1563 }
1564}
1565
1566TEST(URLCanonTest, ReplaceMailtoURL) {
1567 ReplaceCase replace_cases[] = {
1568 // Replace everything
1569 {"mailto:[email protected]?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
1570 // Replace nothing
1571 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:[email protected]?body=sup"},
1572 // Replace the path
1573 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
1574 // Replace the query
1575 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:[email protected]?custom=1"},
1576 // Replace the path and query
1577 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
1578 // Set the query to empty (should leave trailing question mark)
1579 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:[email protected]?"},
1580 // Clear the query
1581 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:[email protected]"},
1582 // Clear the path
1583 {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
1584 // Clear the path + query
1585 {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
1586 // Setting the ref should have no effect
1587 {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
1588 };
1589
1590 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1591 const ReplaceCase& cur = replace_cases[i];
1592 int base_len = static_cast<int>(strlen(cur.base));
[email protected]0318f922014-04-22 00:09:231593 Parsed parsed;
1594 ParseMailtoURL(cur.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521595
[email protected]0318f922014-04-22 00:09:231596 Replacements<char> r;
1597 typedef Replacements<char> R;
[email protected]e7bba5f82013-04-10 20:10:521598 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1599 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1600 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1601 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1602 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1603 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1604 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1605 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1606
1607 std::string out_str;
[email protected]0318f922014-04-22 00:09:231608 StdStringCanonOutput output(&out_str);
1609 Parsed out_parsed;
1610 ReplaceMailtoURL(cur.base, parsed, r, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521611 output.Complete();
1612
1613 EXPECT_EQ(replace_cases[i].expected, out_str);
1614 }
1615}
1616
1617TEST(URLCanonTest, CanonicalizeFileURL) {
1618 struct URLCase {
1619 const char* input;
1620 const char* expected;
1621 bool expected_success;
[email protected]0318f922014-04-22 00:09:231622 Component expected_host;
1623 Component expected_path;
[email protected]e7bba5f82013-04-10 20:10:521624 } cases[] = {
1625#ifdef _WIN32
1626 // Windows-style paths
[email protected]0318f922014-04-22 00:09:231627 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1628 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
1629 {"file:", "file:///", true, Component(), Component(7, 1)},
1630 {"file:UNChost/path", "file://unchost/path", true, Component(7, 7), Component(14, 5)},
[email protected]e7bba5f82013-04-10 20:10:521631 // CanonicalizeFileURL supports absolute Windows style paths for IE
qyearsley2bc727d2015-08-14 20:17:151632 // compatibility. Note that the caller must decide that this is a file
[email protected]e7bba5f82013-04-10 20:10:521633 // URL itself so it can call the file canonicalizer. This is usually
1634 // done automatically as part of relative URL resolving.
[email protected]0318f922014-04-22 00:09:231635 {"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 {"//C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)},
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)},
1641 {"/\\server/file", "file://server/file", true, Component(7, 6), Component(13, 5)},
[email protected]e7bba5f82013-04-10 20:10:521642 // We should preserve the number of slashes after the colon for IE
qyearsley2bc727d2015-08-14 20:17:151643 // compatibility, except when there is none, in which case we should
[email protected]e7bba5f82013-04-10 20:10:521644 // add one.
[email protected]0318f922014-04-22 00:09:231645 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)},
1646 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)},
[email protected]e7bba5f82013-04-10 20:10:521647 // Three slashes should be non-UNC, even if there is no drive spec (IE
1648 // does this, which makes the resulting request invalid).
[email protected]0318f922014-04-22 00:09:231649 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(), Component(7, 12)},
[email protected]e7bba5f82013-04-10 20:10:521650 // TODO(brettw) we should probably fail for invalid host names, which
1651 // would change the expected result on this test. We also currently allow
1652 // colon even though it's probably invalid, because its currently the
1653 // "natural" result of the way the canonicalizer is written. There doesn't
1654 // seem to be a strong argument for why allowing it here would be bad, so
1655 // we just tolerate it and the load will fail later.
[email protected]0318f922014-04-22 00:09:231656 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, Component(7, 2), Component(9, 16)},
1657 {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5), Component(12, 8)},
[email protected]e7bba5f82013-04-10 20:10:521658 // Make sure relative paths can't go above the "C:"
[email protected]0318f922014-04-22 00:09:231659 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, Component(), Component(7, 12)},
[email protected]e7bba5f82013-04-10 20:10:521660 // Busted refs shouldn't make the whole thing fail.
[email protected]0318f922014-04-22 00:09:231661 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, Component(), Component(7, 8)},
[email protected]e7bba5f82013-04-10 20:10:521662#else
1663 // Unix-style paths
[email protected]0318f922014-04-22 00:09:231664 {"file:///home/me", "file:///home/me", true, Component(), Component(7, 8)},
[email protected]e7bba5f82013-04-10 20:10:521665 // Windowsy ones should get still treated as Unix-style.
[email protected]0318f922014-04-22 00:09:231666 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(), Component(7, 16)},
1667 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, Component(), Component(7, 19)},
[email protected]e7bba5f82013-04-10 20:10:521668 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
[email protected]0318f922014-04-22 00:09:231669 {"//", "file:///", true, Component(), Component(7, 1)},
1670 {"///", "file:///", true, Component(), Component(7, 1)},
1671 {"///test", "file:///test", true, Component(), Component(7, 5)},
1672 {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)},
1673 {"file://localhost", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1674 {"file://localhost/", "file://localhost/", true, Component(7, 9), Component(16, 1)},
1675 {"file://localhost/test", "file://localhost/test", true, Component(7, 9), Component(16, 5)},
[email protected]e7bba5f82013-04-10 20:10:521676#endif // _WIN32
1677 };
1678
viettrungluu4b6915862014-10-16 03:42:491679 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521680 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231681 Parsed parsed;
1682 ParseFileURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521683
[email protected]0318f922014-04-22 00:09:231684 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521685 std::string out_str;
[email protected]0318f922014-04-22 00:09:231686 StdStringCanonOutput output(&out_str);
1687 bool success = CanonicalizeFileURL(cases[i].input, url_len, parsed, NULL,
1688 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521689 output.Complete();
1690
1691 EXPECT_EQ(cases[i].expected_success, success);
1692 EXPECT_EQ(cases[i].expected, out_str);
1693
1694 // Make sure the spec was properly identified, the file canonicalizer has
1695 // different code for writing the spec.
1696 EXPECT_EQ(0, out_parsed.scheme.begin);
1697 EXPECT_EQ(4, out_parsed.scheme.len);
1698
1699 EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
1700 EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
1701
1702 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1703 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1704 }
1705}
1706
1707TEST(URLCanonTest, CanonicalizeFileSystemURL) {
1708 struct URLCase {
1709 const char* input;
1710 const char* expected;
1711 bool expected_success;
1712 } cases[] = {
1713 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1714 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1715 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1716 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1717 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1718 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1719 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1720 };
1721
viettrungluu4b6915862014-10-16 03:42:491722 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521723 int url_len = static_cast<int>(strlen(cases[i].input));
[email protected]0318f922014-04-22 00:09:231724 Parsed parsed;
1725 ParseFileSystemURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521726
[email protected]0318f922014-04-22 00:09:231727 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521728 std::string out_str;
[email protected]0318f922014-04-22 00:09:231729 StdStringCanonOutput output(&out_str);
1730 bool success = CanonicalizeFileSystemURL(cases[i].input, url_len, parsed,
1731 NULL, &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521732 output.Complete();
1733
1734 EXPECT_EQ(cases[i].expected_success, success);
1735 EXPECT_EQ(cases[i].expected, out_str);
1736
1737 // Make sure the spec was properly identified, the filesystem canonicalizer
1738 // has different code for writing the spec.
1739 EXPECT_EQ(0, out_parsed.scheme.begin);
1740 EXPECT_EQ(10, out_parsed.scheme.len);
1741 if (success)
1742 EXPECT_GT(out_parsed.path.len, 0);
1743 }
1744}
1745
1746TEST(URLCanonTest, CanonicalizePathURL) {
1747 // Path URLs should get canonicalized schemes but nothing else.
1748 struct PathCase {
1749 const char* input;
1750 const char* expected;
1751 } path_cases[] = {
1752 {"javascript:", "javascript:"},
1753 {"JavaScript:Foo", "javascript:Foo"},
1754 {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1755 };
1756
viettrungluu4b6915862014-10-16 03:42:491757 for (size_t i = 0; i < arraysize(path_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521758 int url_len = static_cast<int>(strlen(path_cases[i].input));
[email protected]0318f922014-04-22 00:09:231759 Parsed parsed;
1760 ParsePathURL(path_cases[i].input, url_len, true, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521761
[email protected]0318f922014-04-22 00:09:231762 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521763 std::string out_str;
[email protected]0318f922014-04-22 00:09:231764 StdStringCanonOutput output(&out_str);
1765 bool success = CanonicalizePathURL(path_cases[i].input, url_len, parsed,
1766 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521767 output.Complete();
1768
1769 EXPECT_TRUE(success);
1770 EXPECT_EQ(path_cases[i].expected, out_str);
1771
1772 EXPECT_EQ(0, out_parsed.host.begin);
1773 EXPECT_EQ(-1, out_parsed.host.len);
1774
1775 // When we end with a colon at the end, there should be no path.
1776 if (path_cases[i].input[url_len - 1] == ':') {
[email protected]369e84f72013-11-23 01:53:521777 EXPECT_EQ(0, out_parsed.GetContent().begin);
1778 EXPECT_EQ(-1, out_parsed.GetContent().len);
[email protected]e7bba5f82013-04-10 20:10:521779 }
1780 }
1781}
1782
1783TEST(URLCanonTest, CanonicalizeMailtoURL) {
1784 struct URLCase {
1785 const char* input;
1786 const char* expected;
1787 bool expected_success;
[email protected]0318f922014-04-22 00:09:231788 Component expected_path;
1789 Component expected_query;
[email protected]e7bba5f82013-04-10 20:10:521790 } cases[] = {
[email protected]0318f922014-04-22 00:09:231791 {"mailto:addr1", "mailto:addr1", true, Component(7, 5), Component()},
1792 {"mailto:[email protected]", "mailto:[email protected]", true, Component(7, 13), Component()},
[email protected]e7bba5f82013-04-10 20:10:521793 // Trailing whitespace is stripped.
[email protected]0318f922014-04-22 00:09:231794 {"MaIlTo:addr1 \t ", "mailto:addr1", true, Component(7, 5), Component()},
1795 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, Component(7, 5), Component(13,6)},
1796 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, Component(7, 11), Component()},
1797 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, Component(7, 12), Component()},
1798 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, Component(7, 13), Component()},
1799 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, Component(7, 12), Component()},
[email protected]e7bba5f82013-04-10 20:10:521800 // Null character should be escaped to %00
[email protected]0318f922014-04-22 00:09:231801 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, Component(7, 13), Component(21, 3)},
[email protected]e7bba5f82013-04-10 20:10:521802 // Invalid -- UTF-8 encoded surrogate value.
[email protected]0318f922014-04-22 00:09:231803 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, Component(7, 9), Component()},
1804 {"mailto:addr1?", "mailto:addr1?", true, Component(7, 5), Component(13, 0)},
[email protected]e7bba5f82013-04-10 20:10:521805 };
1806
1807 // Define outside of loop to catch bugs where components aren't reset
[email protected]0318f922014-04-22 00:09:231808 Parsed parsed;
1809 Parsed out_parsed;
[email protected]e7bba5f82013-04-10 20:10:521810
viettrungluu4b6915862014-10-16 03:42:491811 for (size_t i = 0; i < arraysize(cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:521812 int url_len = static_cast<int>(strlen(cases[i].input));
1813 if (i == 8) {
1814 // The 9th test case purposely has a '\0' in it -- don't count it
1815 // as the string terminator.
1816 url_len = 22;
1817 }
[email protected]0318f922014-04-22 00:09:231818 ParseMailtoURL(cases[i].input, url_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:521819
1820 std::string out_str;
[email protected]0318f922014-04-22 00:09:231821 StdStringCanonOutput output(&out_str);
1822 bool success = CanonicalizeMailtoURL(cases[i].input, url_len, parsed,
1823 &output, &out_parsed);
[email protected]e7bba5f82013-04-10 20:10:521824 output.Complete();
1825
1826 EXPECT_EQ(cases[i].expected_success, success);
1827 EXPECT_EQ(cases[i].expected, out_str);
1828
1829 // Make sure the spec was properly identified
1830 EXPECT_EQ(0, out_parsed.scheme.begin);
1831 EXPECT_EQ(6, out_parsed.scheme.len);
1832
1833 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1834 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1835
1836 EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
1837 EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
1838 }
1839}
1840
1841#ifndef WIN32
1842
1843TEST(URLCanonTest, _itoa_s) {
1844 // We fill the buffer with 0xff to ensure that it's getting properly
qyearsley2bc727d2015-08-14 20:17:151845 // null-terminated. We also allocate one byte more than what we tell
[email protected]e7bba5f82013-04-10 20:10:521846 // _itoa_s about, and ensure that the extra byte is untouched.
1847 char buf[6];
1848 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231849 EXPECT_EQ(0, _itoa_s(12, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521850 EXPECT_STREQ("12", buf);
1851 EXPECT_EQ('\xFF', buf[3]);
1852
1853 // Test the edge cases - exactly the buffer size and one over
1854 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231855 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521856 EXPECT_STREQ("1234", buf);
1857 EXPECT_EQ('\xFF', buf[5]);
1858
1859 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231860 EXPECT_EQ(EINVAL, _itoa_s(12345, buf, sizeof(buf) - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521861 EXPECT_EQ('\xFF', buf[5]); // should never write to this location
1862
1863 // Test the template overload (note that this will see the full buffer)
1864 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231865 EXPECT_EQ(0, _itoa_s(12, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521866 EXPECT_STREQ("12", buf);
1867 EXPECT_EQ('\xFF', buf[3]);
1868
1869 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231870 EXPECT_EQ(0, _itoa_s(12345, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521871 EXPECT_STREQ("12345", buf);
1872
[email protected]0318f922014-04-22 00:09:231873 EXPECT_EQ(EINVAL, _itoa_s(123456, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521874
1875 // Test that radix 16 is supported.
1876 memset(buf, 0xff, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231877 EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 16));
[email protected]e7bba5f82013-04-10 20:10:521878 EXPECT_STREQ("4d2", buf);
1879 EXPECT_EQ('\xFF', buf[5]);
1880}
1881
1882TEST(URLCanonTest, _itow_s) {
1883 // We fill the buffer with 0xff to ensure that it's getting properly
qyearsley2bc727d2015-08-14 20:17:151884 // null-terminated. We also allocate one byte more than what we tell
[email protected]e7bba5f82013-04-10 20:10:521885 // _itoa_s about, and ensure that the extra byte is untouched.
[email protected]3774f832013-06-11 21:21:571886 base::char16 buf[6];
[email protected]e7bba5f82013-04-10 20:10:521887 const char fill_mem = 0xff;
[email protected]3774f832013-06-11 21:21:571888 const base::char16 fill_char = 0xffff;
[email protected]e7bba5f82013-04-10 20:10:521889 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231890 EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
[email protected]3774f832013-06-11 21:21:571891 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521892 EXPECT_EQ(fill_char, buf[3]);
1893
1894 // Test the edge cases - exactly the buffer size and one over
[email protected]0318f922014-04-22 00:09:231895 EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
[email protected]3774f832013-06-11 21:21:571896 EXPECT_EQ(WStringToUTF16(L"1234"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521897 EXPECT_EQ(fill_char, buf[5]);
1898
1899 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231900 EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
[email protected]e7bba5f82013-04-10 20:10:521901 EXPECT_EQ(fill_char, buf[5]); // should never write to this location
1902
1903 // Test the template overload (note that this will see the full buffer)
1904 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231905 EXPECT_EQ(0, _itow_s(12, buf, 10));
[email protected]3774f832013-06-11 21:21:571906 EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521907 EXPECT_EQ(fill_char, buf[3]);
1908
1909 memset(buf, fill_mem, sizeof(buf));
[email protected]0318f922014-04-22 00:09:231910 EXPECT_EQ(0, _itow_s(12345, buf, 10));
[email protected]3774f832013-06-11 21:21:571911 EXPECT_EQ(WStringToUTF16(L"12345"), base::string16(buf));
[email protected]e7bba5f82013-04-10 20:10:521912
[email protected]0318f922014-04-22 00:09:231913 EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10));
[email protected]e7bba5f82013-04-10 20:10:521914}
1915
1916#endif // !WIN32
1917
1918// Returns true if the given two structures are the same.
[email protected]0318f922014-04-22 00:09:231919static bool ParsedIsEqual(const Parsed& a, const Parsed& b) {
[email protected]e7bba5f82013-04-10 20:10:521920 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
1921 a.username.begin == b.username.begin && a.username.len == b.username.len &&
1922 a.password.begin == b.password.begin && a.password.len == b.password.len &&
1923 a.host.begin == b.host.begin && a.host.len == b.host.len &&
1924 a.port.begin == b.port.begin && a.port.len == b.port.len &&
1925 a.path.begin == b.path.begin && a.path.len == b.path.len &&
1926 a.query.begin == b.query.begin && a.query.len == b.query.len &&
1927 a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
1928}
1929
1930TEST(URLCanonTest, ResolveRelativeURL) {
1931 struct RelativeCase {
1932 const char* base; // Input base URL: MUST BE CANONICAL
1933 bool is_base_hier; // Is the base URL hierarchical
1934 bool is_base_file; // Tells us if the base is a file URL.
1935 const char* test; // Input URL to test against.
1936 bool succeed_relative; // Whether we expect IsRelativeURL to succeed
1937 bool is_rel; // Whether we expect |test| to be relative or not.
1938 bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed.
1939 const char* resolved; // What we expect in the result when resolving.
1940 } rel_cases[] = {
1941 // Basic absolute input.
1942 {"http://host/a", true, false, "http://another/", true, false, false, NULL},
1943 {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
1944 // Empty relative URLs should only remove the ref part of the URL,
1945 // leaving the rest unchanged.
1946 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
1947 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
1948 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
1949 // Spaces at the ends of the relative path should be ignored.
1950 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
1951 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
1952 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
1953 // Matching schemes without two slashes are treated as relative.
1954 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
1955 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
1956 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
1957 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
1958 // Nonmatching schemes are absolute.
1959 {"http://host/a", true, false, "https:host2", true, false, false, NULL},
1960 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
1961 // Absolute path input
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\\d", true, true, true, "http://host/b/c/d"},
1964 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
1965 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
1966 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
1967 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
1968 // Relative path input
1969 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
1970 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
1971 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
1972 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
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/"},
1976 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
1977 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
1978 // Query input
1979 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
1980 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
1981 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
1982 // Ref input
1983 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
1984 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
1985 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
1986 // Non-hierarchical base: no relative handling. Relative input should
1987 // error, and if a scheme is present, it should be treated as absolute.
1988 {"data:foobar", false, false, "baz.html", false, false, false, NULL},
1989 {"data:foobar", false, false, "data:baz", true, false, false, NULL},
1990 {"data:foobar", false, false, "data:/base", true, false, false, NULL},
1991 // Non-hierarchical base: absolute input should succeed.
1992 {"data:foobar", false, false, "http://host/", true, false, false, NULL},
1993 {"data:foobar", false, false, "http:host", true, false, false, NULL},
ramya.v56d422052015-12-02 02:24:461994 // Non-hierarchical base: empty URL should give error.
1995 {"data:foobar", false, false, "", false, false, false, NULL},
[email protected]e7bba5f82013-04-10 20:10:521996 // Invalid schemes should be treated as relative.
1997 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
1998 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
1999 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
2000 {"data:asdf", false, false, ":foo", false, false, false, NULL},
[email protected]45172e62014-03-03 21:21:352001 {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL},
[email protected]e7bba5f82013-04-10 20:10:522002 // We should treat semicolons like any other character in URL resolving
2003 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
2004 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
2005 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
2006 // Relative URLs can also be written as "//foo/bar" which is relative to
2007 // the scheme. In this case, it would take the old scheme, so for http
2008 // the example would resolve to "http://foo/bar".
2009 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
2010 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
2011 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
2012 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
2013 {"http://host/a", true, false, "//", true, true, false, "http:"},
2014 // IE will also allow one or the other to be a backslash to get the same
2015 // behavior.
2016 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
2017 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
2018#ifdef WIN32
2019 // Resolving against Windows file base URLs.
2020 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
2021 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
2022 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
2023 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
2024 // But two backslashes on Windows should be UNC so should be treated
2025 // as absolute.
2026 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
2027 // IE doesn't support drive specs starting with two slashes. It fails
2028 // immediately and doesn't even try to load. We fix it up to either
2029 // an absolute path or UNC depending on what it looks like.
2030 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
2031 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
2032 // Windows drive specs should be allowed and treated as absolute.
2033 {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
2034 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
2035 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
2036 // Relative paths with drive letters should be allowed when the base is
2037 // also a file.
2038 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2039 // Treat absolute paths as being off of the drive.
2040 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2041 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2042 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2043 // On Windows, two slashes without a drive letter when the base is a file
2044 // means that the path is UNC.
2045 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2046 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2047#else
2048 // On Unix we fall back to relative behavior since there's nothing else
2049 // reasonable to do.
2050 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2051#endif
2052 // Even on Windows, we don't allow relative drive specs when the base
2053 // is not file.
2054 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2055 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
[email protected]598c8382013-09-18 22:55:312056 // Ensure that ports aren't allowed for hosts relative to a file url.
2057 // Although the result string shows a host:port portion, the call to
2058 // resolve the relative URL returns false, indicating parse failure,
2059 // which is what is required.
2060 {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
[email protected]e7bba5f82013-04-10 20:10:522061 // Filesystem URL tests; filesystem URLs are only valid and relative if
qyearsley2bc727d2015-08-14 20:17:152062 // they have no scheme, e.g. "./index.html". There's no valid equivalent
[email protected]e7bba5f82013-04-10 20:10:522063 // to http:index.html.
2064 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2065 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
2066 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
2067 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2068 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2069 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2070 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
2071 // Absolute URLs are still not relative to a non-standard base URL.
2072 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2073 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2074 };
2075
viettrungluu4b6915862014-10-16 03:42:492076 for (size_t i = 0; i < arraysize(rel_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:522077 const RelativeCase& cur_case = rel_cases[i];
2078
[email protected]0318f922014-04-22 00:09:232079 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:522080 int base_len = static_cast<int>(strlen(cur_case.base));
2081 if (cur_case.is_base_file)
[email protected]0318f922014-04-22 00:09:232082 ParseFileURL(cur_case.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522083 else if (cur_case.is_base_hier)
[email protected]0318f922014-04-22 00:09:232084 ParseStandardURL(cur_case.base, base_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522085 else
[email protected]0318f922014-04-22 00:09:232086 ParsePathURL(cur_case.base, base_len, false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522087
2088 // First see if it is relative.
2089 int test_len = static_cast<int>(strlen(cur_case.test));
2090 bool is_relative;
[email protected]0318f922014-04-22 00:09:232091 Component relative_component;
2092 bool succeed_is_rel = IsRelativeURL(
[email protected]e7bba5f82013-04-10 20:10:522093 cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
2094 &is_relative, &relative_component);
2095
2096 EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
2097 "succeed is rel failure on " << cur_case.test;
2098 EXPECT_EQ(cur_case.is_rel, is_relative) <<
2099 "is rel failure on " << cur_case.test;
2100 // Now resolve it.
2101 if (succeed_is_rel && is_relative && cur_case.is_rel) {
2102 std::string resolved;
[email protected]0318f922014-04-22 00:09:232103 StdStringCanonOutput output(&resolved);
2104 Parsed resolved_parsed;
[email protected]e7bba5f82013-04-10 20:10:522105
[email protected]0318f922014-04-22 00:09:232106 bool succeed_resolve = ResolveRelativeURL(
2107 cur_case.base, parsed, cur_case.is_base_file, cur_case.test,
2108 relative_component, NULL, &output, &resolved_parsed);
[email protected]e7bba5f82013-04-10 20:10:522109 output.Complete();
2110
2111 EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
2112 EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
2113
2114 // Verify that the output parsed structure is the same as parsing a
2115 // the URL freshly.
[email protected]0318f922014-04-22 00:09:232116 Parsed ref_parsed;
[email protected]e7bba5f82013-04-10 20:10:522117 int resolved_len = static_cast<int>(resolved.size());
[email protected]369e84f72013-11-23 01:53:522118 if (cur_case.is_base_file) {
[email protected]0318f922014-04-22 00:09:232119 ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522120 } else if (cur_case.is_base_hier) {
[email protected]0318f922014-04-22 00:09:232121 ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522122 } else {
[email protected]0318f922014-04-22 00:09:232123 ParsePathURL(resolved.c_str(), resolved_len, false, &ref_parsed);
[email protected]369e84f72013-11-23 01:53:522124 }
[email protected]e7bba5f82013-04-10 20:10:522125 EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
2126 }
2127 }
2128}
2129
qyearsley2bc727d2015-08-14 20:17:152130// It used to be the case that when we did a replacement with a long buffer of
2131// UTF-16 characters, we would get invalid data in the URL. This is because the
2132// buffer that it used to hold the UTF-8 data was resized, while some pointers
2133// were still kept to the old buffer that was removed.
[email protected]e7bba5f82013-04-10 20:10:522134TEST(URLCanonTest, ReplacementOverflow) {
2135 const char src[] = "file:///C:/foo/bar";
2136 int src_len = static_cast<int>(strlen(src));
[email protected]0318f922014-04-22 00:09:232137 Parsed parsed;
2138 ParseFileURL(src, src_len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:522139
2140 // Override two components, the path with something short, and the query with
qyearsley2bc727d2015-08-14 20:17:152141 // something long enough to trigger the bug.
[email protected]0318f922014-04-22 00:09:232142 Replacements<base::char16> repl;
[email protected]3774f832013-06-11 21:21:572143 base::string16 new_query;
[email protected]e7bba5f82013-04-10 20:10:522144 for (int i = 0; i < 4800; i++)
2145 new_query.push_back('a');
2146
[email protected]3774f832013-06-11 21:21:572147 base::string16 new_path(WStringToUTF16(L"/foo"));
[email protected]0318f922014-04-22 00:09:232148 repl.SetPath(new_path.c_str(), Component(0, 4));
[email protected]e7bba5f82013-04-10 20:10:522149 repl.SetQuery(new_query.c_str(),
[email protected]0318f922014-04-22 00:09:232150 Component(0, static_cast<int>(new_query.length())));
[email protected]e7bba5f82013-04-10 20:10:522151
2152 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2153 // standard URLs, file URLs, etc, since they will go to the same replacement
2154 // function that was buggy.
[email protected]0318f922014-04-22 00:09:232155 Parsed repl_parsed;
[email protected]e7bba5f82013-04-10 20:10:522156 std::string repl_str;
[email protected]0318f922014-04-22 00:09:232157 StdStringCanonOutput repl_output(&repl_str);
2158 ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
[email protected]e7bba5f82013-04-10 20:10:522159 repl_output.Complete();
2160
2161 // Generate the expected string and check.
2162 std::string expected("file:///foo?");
2163 for (size_t i = 0; i < new_query.length(); i++)
2164 expected.push_back('a');
2165 EXPECT_TRUE(expected == repl_str);
2166}
[email protected]0318f922014-04-22 00:09:232167
2168} // namespace url