Add abstract BluetoothProfile class
[chromium-blink-merge.git] / url / url_canon_unittest.cc
blob55b349fb5d47eca8b659284d79ea142222c571fc
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <errno.h>
31 #include <unicode/ucnv.h>
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "url/url_canon.h"
35 #include "url/url_canon_icu.h"
36 #include "url/url_canon_internal.h"
37 #include "url/url_canon_stdstring.h"
38 #include "url/url_parse.h"
39 #include "url/url_test_utils.h"
41 // Some implementations of base/basictypes.h may define ARRAYSIZE.
42 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
43 // which is in our version of basictypes.h.
44 #ifndef ARRAYSIZE
45 #define ARRAYSIZE ARRAYSIZE_UNSAFE
46 #endif
48 using url_test_utils::WStringToUTF16;
49 using url_test_utils::ConvertUTF8ToUTF16;
50 using url_test_utils::ConvertUTF16ToUTF8;
51 using url_canon::CanonHostInfo;
53 namespace {
55 struct ComponentCase {
56 const char* input;
57 const char* expected;
58 url_parse::Component expected_component;
59 bool expected_success;
62 // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
63 // treat each input as optional, and will only try processing if non-NULL.
64 // The output is always 8-bit.
65 struct DualComponentCase {
66 const char* input8;
67 const wchar_t* input16;
68 const char* expected;
69 url_parse::Component expected_component;
70 bool expected_success;
73 // Test cases for CanonicalizeIPAddress(). The inputs are identical to
74 // DualComponentCase, but the output has extra CanonHostInfo fields.
75 struct IPAddressCase {
76 const char* input8;
77 const wchar_t* input16;
78 const char* expected;
79 url_parse::Component expected_component;
81 // CanonHostInfo fields, for verbose output.
82 CanonHostInfo::Family expected_family;
83 int expected_num_ipv4_components;
84 const char* expected_address_hex; // Two hex chars per IP address byte.
87 std::string BytesToHexString(unsigned char bytes[16], int length) {
88 EXPECT_TRUE(length == 0 || length == 4 || length == 16)
89 << "Bad IP address length: " << length;
90 std::string result;
91 for (int i = 0; i < length; ++i) {
92 result.push_back(url_canon::kHexCharLookup[(bytes[i] >> 4) & 0xf]);
93 result.push_back(url_canon::kHexCharLookup[bytes[i] & 0xf]);
95 return result;
98 struct ReplaceCase {
99 const char* base;
100 const char* scheme;
101 const char* username;
102 const char* password;
103 const char* host;
104 const char* port;
105 const char* path;
106 const char* query;
107 const char* ref;
108 const char* expected;
111 // Wrapper around a UConverter object that managers creation and destruction.
112 class UConvScoper {
113 public:
114 explicit UConvScoper(const char* charset_name) {
115 UErrorCode err = U_ZERO_ERROR;
116 converter_ = ucnv_open(charset_name, &err);
119 ~UConvScoper() {
120 if (converter_)
121 ucnv_close(converter_);
124 // Returns the converter object, may be NULL.
125 UConverter* converter() const { return converter_; }
127 private:
128 UConverter* converter_;
131 // Magic string used in the replacements code that tells SetupReplComp to
132 // call the clear function.
133 const char kDeleteComp[] = "|";
135 // Sets up a replacement for a single component. This is given pointers to
136 // the set and clear function for the component being replaced, and will
137 // either set the component (if it exists) or clear it (if the replacement
138 // string matches kDeleteComp).
140 // This template is currently used only for the 8-bit case, and the strlen
141 // causes it to fail in other cases. It is left a template in case we have
142 // tests for wide replacements.
143 template<typename CHAR>
144 void SetupReplComp(
145 void (url_canon::Replacements<CHAR>::*set)(const CHAR*,
146 const url_parse::Component&),
147 void (url_canon::Replacements<CHAR>::*clear)(),
148 url_canon::Replacements<CHAR>* rep,
149 const CHAR* str) {
150 if (str && str[0] == kDeleteComp[0]) {
151 (rep->*clear)();
152 } else if (str) {
153 (rep->*set)(str, url_parse::Component(0, static_cast<int>(strlen(str))));
157 } // namespace
159 TEST(URLCanonTest, DoAppendUTF8) {
160 struct UTF8Case {
161 unsigned input;
162 const char* output;
163 } utf_cases[] = {
164 // Valid code points.
165 {0x24, "\x24"},
166 {0xA2, "\xC2\xA2"},
167 {0x20AC, "\xE2\x82\xAC"},
168 {0x24B62, "\xF0\xA4\xAD\xA2"},
169 {0x10FFFF, "\xF4\x8F\xBF\xBF"},
171 std::string out_str;
172 for (size_t i = 0; i < ARRAYSIZE(utf_cases); i++) {
173 out_str.clear();
174 url_canon::StdStringCanonOutput output(&out_str);
175 url_canon::AppendUTF8Value(utf_cases[i].input, &output);
176 output.Complete();
177 EXPECT_EQ(utf_cases[i].output, out_str);
181 // TODO(mattm): Can't run this in debug mode for now, since the DCHECK will
182 // cause the Chromium stacktrace dialog to appear and hang the test.
183 // See http://crbug.com/49580.
184 #if defined(GTEST_HAS_DEATH_TEST) && defined(NDEBUG)
185 TEST(URLCanonTest, DoAppendUTF8Invalid) {
186 std::string out_str;
187 url_canon::StdStringCanonOutput output(&out_str);
188 // Invalid code point (too large).
189 ASSERT_DEBUG_DEATH({
190 url_canon::AppendUTF8Value(0x110000, &output);
191 output.Complete();
192 EXPECT_EQ("", out_str);
193 }, "");
195 #endif
197 TEST(URLCanonTest, UTF) {
198 // Low-level test that we handle reading, canonicalization, and writing
199 // UTF-8/UTF-16 strings properly.
200 struct UTFCase {
201 const char* input8;
202 const wchar_t* input16;
203 bool expected_success;
204 const char* output;
205 } utf_cases[] = {
206 // Valid canonical input should get passed through & escaped.
207 {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
208 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
209 {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
210 // Non-shortest-form UTF-8 are invalid. The bad char should be replaced
211 // with the invalid character (EF BF DB in UTF-8).
212 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"},
213 // Invalid UTF-8 sequences should be marked as invalid (the first
214 // sequence is truncated).
215 {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
216 // Character going off the end.
217 {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
218 // ...same with low surrogates with no high surrogate.
219 {"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"},
220 // Test a UTF-8 encoded surrogate value is marked as invalid.
221 // ED A0 80 = U+D800
222 {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
225 std::string out_str;
226 for (size_t i = 0; i < ARRAYSIZE(utf_cases); i++) {
227 if (utf_cases[i].input8) {
228 out_str.clear();
229 url_canon::StdStringCanonOutput output(&out_str);
231 int input_len = static_cast<int>(strlen(utf_cases[i].input8));
232 bool success = true;
233 for (int ch = 0; ch < input_len; ch++) {
234 success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
235 &output);
237 output.Complete();
238 EXPECT_EQ(utf_cases[i].expected_success, success);
239 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
241 if (utf_cases[i].input16) {
242 out_str.clear();
243 url_canon::StdStringCanonOutput output(&out_str);
245 string16 input_str(WStringToUTF16(utf_cases[i].input16));
246 int input_len = static_cast<int>(input_str.length());
247 bool success = true;
248 for (int ch = 0; ch < input_len; ch++) {
249 success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
250 &output);
252 output.Complete();
253 EXPECT_EQ(utf_cases[i].expected_success, success);
254 EXPECT_EQ(std::string(utf_cases[i].output), out_str);
257 if (utf_cases[i].input8 && utf_cases[i].input16 &&
258 utf_cases[i].expected_success) {
259 // Check that the UTF-8 and UTF-16 inputs are equivalent.
261 // UTF-16 -> UTF-8
262 std::string input8_str(utf_cases[i].input8);
263 string16 input16_str(WStringToUTF16(utf_cases[i].input16));
264 EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str));
266 // UTF-8 -> UTF-16
267 EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
272 TEST(URLCanonTest, ICUCharsetConverter) {
273 struct ICUCase {
274 const wchar_t* input;
275 const char* encoding;
276 const char* expected;
277 } icu_cases[] = {
278 // UTF-8.
279 {L"Hello, world", "utf-8", "Hello, world"},
280 {L"\x4f60\x597d", "utf-8", "\xe4\xbd\xa0\xe5\xa5\xbd"},
281 // Non-BMP UTF-8.
282 {L"!\xd800\xdf00!", "utf-8", "!\xf0\x90\x8c\x80!"},
283 // Big5
284 {L"\x4f60\x597d", "big5", "\xa7\x41\xa6\x6e"},
285 // Unrepresentable character in the destination set.
286 {L"hello\x4f60\x06de\x597dworld", "big5", "hello\xa7\x41%26%231758%3B\xa6\x6eworld"},
289 for (size_t i = 0; i < ARRAYSIZE(icu_cases); i++) {
290 UConvScoper conv(icu_cases[i].encoding);
291 ASSERT_TRUE(conv.converter() != NULL);
292 url_canon::ICUCharsetConverter converter(conv.converter());
294 std::string str;
295 url_canon::StdStringCanonOutput output(&str);
297 string16 input_str(WStringToUTF16(icu_cases[i].input));
298 int input_len = static_cast<int>(input_str.length());
299 converter.ConvertFromUTF16(input_str.c_str(), input_len, &output);
300 output.Complete();
302 EXPECT_STREQ(icu_cases[i].expected, str.c_str());
305 // Test string sizes around the resize boundary for the output to make sure
306 // the converter resizes as needed.
307 const int static_size = 16;
308 UConvScoper conv("utf-8");
309 ASSERT_TRUE(conv.converter());
310 url_canon::ICUCharsetConverter converter(conv.converter());
311 for (int i = static_size - 2; i <= static_size + 2; i++) {
312 // Make a string with the appropriate length.
313 string16 input;
314 for (int ch = 0; ch < i; ch++)
315 input.push_back('a');
317 url_canon::RawCanonOutput<static_size> output;
318 converter.ConvertFromUTF16(input.c_str(), static_cast<int>(input.length()),
319 &output);
320 EXPECT_EQ(input.length(), static_cast<size_t>(output.length()));
324 TEST(URLCanonTest, Scheme) {
325 // Here, we're mostly testing that unusual characters are handled properly.
326 // The canonicalizer doesn't do any parsing or whitespace detection. It will
327 // also do its best on error, and will escape funny sequences (these won't be
328 // valid schemes and it will return error).
330 // Note that the canonicalizer will append a colon to the output to separate
331 // out the rest of the URL, which is not present in the input. We check,
332 // however, that the output range includes everything but the colon.
333 ComponentCase scheme_cases[] = {
334 {"http", "http:", url_parse::Component(0, 4), true},
335 {"HTTP", "http:", url_parse::Component(0, 4), true},
336 {" HTTP ", "%20http%20:", url_parse::Component(0, 10), false},
337 {"htt: ", "htt%3A%20:", url_parse::Component(0, 9), false},
338 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", url_parse::Component(0, 22), false},
339 // Don't re-escape something already escaped. Note that it will
340 // "canonicalize" the 'A' to 'a', but that's OK.
341 {"ht%3Atp", "ht%3atp:", url_parse::Component(0, 7), false},
344 std::string out_str;
346 for (size_t i = 0; i < arraysize(scheme_cases); i++) {
347 int url_len = static_cast<int>(strlen(scheme_cases[i].input));
348 url_parse::Component in_comp(0, url_len);
349 url_parse::Component out_comp;
351 out_str.clear();
352 url_canon::StdStringCanonOutput output1(&out_str);
353 bool success = url_canon::CanonicalizeScheme(scheme_cases[i].input,
354 in_comp, &output1, &out_comp);
355 output1.Complete();
357 EXPECT_EQ(scheme_cases[i].expected_success, success);
358 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
359 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
360 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
362 // Now try the wide version
363 out_str.clear();
364 url_canon::StdStringCanonOutput output2(&out_str);
366 string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input));
367 in_comp.len = static_cast<int>(wide_input.length());
368 success = url_canon::CanonicalizeScheme(wide_input.c_str(), in_comp,
369 &output2, &out_comp);
370 output2.Complete();
372 EXPECT_EQ(scheme_cases[i].expected_success, success);
373 EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
374 EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
375 EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
378 // Test the case where the scheme is declared nonexistant, it should be
379 // converted into an empty scheme.
380 url_parse::Component out_comp;
381 out_str.clear();
382 url_canon::StdStringCanonOutput output(&out_str);
384 EXPECT_TRUE(url_canon::CanonicalizeScheme("", url_parse::Component(0, -1),
385 &output, &out_comp));
386 output.Complete();
388 EXPECT_EQ(std::string(":"), out_str);
389 EXPECT_EQ(0, out_comp.begin);
390 EXPECT_EQ(0, out_comp.len);
393 TEST(URLCanonTest, Host) {
394 IPAddressCase host_cases[] = {
395 // Basic canonicalization, uppercase should be converted to lowercase.
396 {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", url_parse::Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
397 // Spaces and some other characters should be escaped.
398 {"Goo%20 goo%7C|.com", L"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", url_parse::Component(0, 22), CanonHostInfo::NEUTRAL, -1, ""},
399 // Exciting different types of spaces!
400 {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", url_parse::Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
401 // Other types of space (no-break, zero-width, zero-width-no-break) are
402 // name-prepped away to nothing.
403 {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", url_parse::Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
404 // Ideographic full stop (full-width period for Chinese, etc.) should be
405 // treated as a dot.
406 {NULL, L"www.foo\x3002"L"bar.com", "www.foo.bar.com", url_parse::Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
407 // Invalid unicode characters should fail...
408 // ...In wide input, ICU will barf and we'll end up with the input as
409 // escaped UTF-8 (the invalid character should be replaced with the
410 // replacement character).
411 {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", url_parse::Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
412 // ...This is the same as previous but with with escaped.
413 {"%ef%b7%90zyx.com", L"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", url_parse::Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
414 // Test name prepping, fullwidth input should be converted to ASCII and NOT
415 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
416 {"\xef\xbc\xa7\xef\xbd\x8f.com", L"\xff27\xff4f.com", "go.com", url_parse::Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
417 // Test that fullwidth escaped values are properly name-prepped,
418 // then converted or rejected.
419 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
420 {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", url_parse::Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
421 {"%ef%bc%85%ef%bc%94%ef%bc%91.com", L"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", url_parse::Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
422 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
423 {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", url_parse::Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
424 {"%ef%bc%85%ef%bc%90%ef%bc%90.com", L"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", url_parse::Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
425 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
426 {"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d\x4f60\x597d", "xn--6qqa088eba", url_parse::Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
427 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
428 // UTF-8 (wide case). The output should be equivalent to the true wide
429 // character input above).
430 {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd", L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba", url_parse::Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
431 // Invalid escaped characters should fail and the percents should be
432 // escaped.
433 {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", url_parse::Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
434 // If we get an invalid character that has been escaped.
435 {"%25", L"%25", "%25", url_parse::Component(0, 3), CanonHostInfo::BROKEN, -1, ""},
436 {"hello%00", L"hello%00", "hello%00", url_parse::Component(0, 8), CanonHostInfo::BROKEN, -1, ""},
437 // Escaped numbers should be treated like IP addresses if they are.
438 {"%30%78%63%30%2e%30%32%35%30.01", L"%30%78%63%30%2e%30%32%35%30.01", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
439 {"%30%78%63%30%2e%30%32%35%30.01%2e", L"%30%78%63%30%2e%30%32%35%30.01%2e", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
440 // Invalid escaping should trigger the regular host error handling.
441 {"%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", url_parse::Component(0, 17), CanonHostInfo::BROKEN, -1, ""},
442 // Something that isn't exactly an IP should get treated as a host and
443 // spaces escaped.
444 {"192.168.0.1 hello", L"192.168.0.1 hello", "192.168.0.1%20hello", url_parse::Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
445 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
446 // These are "0Xc0.0250.01" in fullwidth.
447 {"\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", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
448 // Broken IP addresses get marked as such.
449 {"192.168.0.257", L"192.168.0.257", "192.168.0.257", url_parse::Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
450 {"[google.com]", L"[google.com]", "[google.com]", url_parse::Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
451 // Cyrillic letter followed buy ( should return punicode for ( escaped before punicode string was created. I.e.
452 // if ( is escaped after punicode is created we would get xn--%28-8tb (incorrect).
453 {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", url_parse::Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
454 // Address with all hexidecimal characters with leading number of 1<<32
455 // or greater and should return NEUTRAL rather than BROKEN if not all
456 // components are numbers.
457 {"12345678912345.de", L"12345678912345.de", "12345678912345.de", url_parse::Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
458 {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", url_parse::Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
459 {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", url_parse::Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
460 {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", url_parse::Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
461 {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", url_parse::Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
464 // CanonicalizeHost() non-verbose.
465 std::string out_str;
466 for (size_t i = 0; i < arraysize(host_cases); i++) {
467 // Narrow version.
468 if (host_cases[i].input8) {
469 int host_len = static_cast<int>(strlen(host_cases[i].input8));
470 url_parse::Component in_comp(0, host_len);
471 url_parse::Component out_comp;
473 out_str.clear();
474 url_canon::StdStringCanonOutput output(&out_str);
476 bool success = url_canon::CanonicalizeHost(host_cases[i].input8, in_comp,
477 &output, &out_comp);
478 output.Complete();
480 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
481 success);
482 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
483 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
484 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
487 // Wide version.
488 if (host_cases[i].input16) {
489 string16 input16(WStringToUTF16(host_cases[i].input16));
490 int host_len = static_cast<int>(input16.length());
491 url_parse::Component in_comp(0, host_len);
492 url_parse::Component out_comp;
494 out_str.clear();
495 url_canon::StdStringCanonOutput output(&out_str);
497 bool success = url_canon::CanonicalizeHost(input16.c_str(), in_comp,
498 &output, &out_comp);
499 output.Complete();
501 EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
502 success);
503 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
504 EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
505 EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
509 // CanonicalizeHostVerbose()
510 for (size_t i = 0; i < arraysize(host_cases); i++) {
511 // Narrow version.
512 if (host_cases[i].input8) {
513 int host_len = static_cast<int>(strlen(host_cases[i].input8));
514 url_parse::Component in_comp(0, host_len);
516 out_str.clear();
517 url_canon::StdStringCanonOutput output(&out_str);
518 CanonHostInfo host_info;
520 url_canon::CanonicalizeHostVerbose(host_cases[i].input8, in_comp,
521 &output, &host_info);
522 output.Complete();
524 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
525 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
526 EXPECT_EQ(host_cases[i].expected_component.begin,
527 host_info.out_host.begin);
528 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
529 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
530 BytesToHexString(host_info.address, host_info.AddressLength()));
531 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
532 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
533 host_info.num_ipv4_components);
537 // Wide version.
538 if (host_cases[i].input16) {
539 string16 input16(WStringToUTF16(host_cases[i].input16));
540 int host_len = static_cast<int>(input16.length());
541 url_parse::Component in_comp(0, host_len);
543 out_str.clear();
544 url_canon::StdStringCanonOutput output(&out_str);
545 CanonHostInfo host_info;
547 url_canon::CanonicalizeHostVerbose(input16.c_str(), in_comp,
548 &output, &host_info);
549 output.Complete();
551 EXPECT_EQ(host_cases[i].expected_family, host_info.family);
552 EXPECT_EQ(std::string(host_cases[i].expected), out_str);
553 EXPECT_EQ(host_cases[i].expected_component.begin,
554 host_info.out_host.begin);
555 EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
556 EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
557 BytesToHexString(host_info.address, host_info.AddressLength()));
558 if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
559 EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
560 host_info.num_ipv4_components);
566 TEST(URLCanonTest, IPv4) {
567 IPAddressCase cases[] = {
568 // Empty is not an IP address.
569 {"", L"", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
570 {".", L".", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
571 // Regular IP addresses in different bases.
572 {"192.168.0.1", L"192.168.0.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
573 {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
574 {"0xC0.0Xa8.0x0.0x1", L"0xC0.0Xa8.0x0.0x1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
575 // Non-IP addresses due to invalid characters.
576 {"192.168.9.com", L"192.168.9.com", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
577 // Invalid characters for the base should be rejected.
578 {"19a.168.0.1", L"19a.168.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
579 {"0308.0250.00.01", L"0308.0250.00.01", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
580 {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
581 // If there are not enough components, the last one should fill them out.
582 {"192", L"192", "0.0.0.192", url_parse::Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
583 {"0xC0a80001", L"0xC0a80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
584 {"030052000001", L"030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
585 {"000030052000001", L"000030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
586 {"192.168", L"192.168", "192.0.0.168", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
587 {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
588 {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
589 {"192.168.1", L"192.168.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
590 // Too many components means not an IP address.
591 {"192.168.0.0.1", L"192.168.0.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
592 // We allow a single trailing dot.
593 {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
594 {"192.168.0.1. hello", L"192.168.0.1. hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
595 {"192.168.0.1..", L"192.168.0.1..", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
596 // Two dots in a row means not an IP address.
597 {"192.168..1", L"192.168..1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
598 // Any numerical overflow should be marked as BROKEN.
599 {"0x100.0", L"0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
600 {"0x100.0.0", L"0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
601 {"0x100.0.0.0", L"0x100.0.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
602 {"0.0x100.0.0", L"0.0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
603 {"0.0.0x100.0", L"0.0.0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
604 {"0.0.0.0x100", L"0.0.0.0x100", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
605 {"0.0.0x10000", L"0.0.0x10000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
606 {"0.0x1000000", L"0.0x1000000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
607 {"0x100000000", L"0x100000000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
608 // Repeat the previous tests, minus 1, to verify boundaries.
609 {"0xFF.0", L"0xFF.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
610 {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
611 {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
612 {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
613 {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
614 {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
615 {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
616 {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", url_parse::Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
617 {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", url_parse::Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
618 // Old trunctations tests. They're all "BROKEN" now.
619 {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
620 {"192.168.0.257", L"192.168.0.257", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
621 {"192.168.0xa20001", L"192.168.0xa20001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
622 {"192.015052000001", L"192.015052000001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
623 {"0X12C0a80001", L"0X12C0a80001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
624 {"276.1.2", L"276.1.2", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
625 // Spaces should be rejected.
626 {"192.168.0.1 hello", L"192.168.0.1 hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
627 // Very large numbers.
628 {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
629 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", url_parse::Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
630 // A number has no length limit, but long numbers can still overflow.
631 {"00000000000000000001", L"00000000000000000001", "0.0.0.1", url_parse::Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
632 {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
633 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
634 {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
635 {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
636 // Truncation of all zeros should still result in 0.
637 {"0.00.0x.0x0", L"0.00.0x.0x0", "0.0.0.0", url_parse::Component(0, 7), CanonHostInfo::IPV4, 4, "00000000"},
640 for (size_t i = 0; i < arraysize(cases); i++) {
641 // 8-bit version.
642 url_parse::Component component(0,
643 static_cast<int>(strlen(cases[i].input8)));
645 std::string out_str1;
646 url_canon::StdStringCanonOutput output1(&out_str1);
647 url_canon::CanonHostInfo host_info;
648 url_canon::CanonicalizeIPAddress(cases[i].input8, component, &output1,
649 &host_info);
650 output1.Complete();
652 EXPECT_EQ(cases[i].expected_family, host_info.family);
653 EXPECT_EQ(std::string(cases[i].expected_address_hex),
654 BytesToHexString(host_info.address, host_info.AddressLength()));
655 if (host_info.family == CanonHostInfo::IPV4) {
656 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
657 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
658 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
659 EXPECT_EQ(cases[i].expected_num_ipv4_components,
660 host_info.num_ipv4_components);
663 // 16-bit version.
664 string16 input16(WStringToUTF16(cases[i].input16));
665 component = url_parse::Component(0, static_cast<int>(input16.length()));
667 std::string out_str2;
668 url_canon::StdStringCanonOutput output2(&out_str2);
669 url_canon::CanonicalizeIPAddress(input16.c_str(), component, &output2,
670 &host_info);
671 output2.Complete();
673 EXPECT_EQ(cases[i].expected_family, host_info.family);
674 EXPECT_EQ(std::string(cases[i].expected_address_hex),
675 BytesToHexString(host_info.address, host_info.AddressLength()));
676 if (host_info.family == CanonHostInfo::IPV4) {
677 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
678 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
679 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
680 EXPECT_EQ(cases[i].expected_num_ipv4_components,
681 host_info.num_ipv4_components);
686 TEST(URLCanonTest, IPv6) {
687 IPAddressCase cases[] = {
688 // Empty is not an IP address.
689 {"", L"", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
690 // Non-IPs with [:] characters are marked BROKEN.
691 {":", L":", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
692 {"[", L"[", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
693 {"[:", L"[:", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
694 {"]", L"]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
695 {":]", L":]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
696 {"[]", L"[]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
697 {"[:]", L"[:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
698 // Regular IP address is invalid without bounding '[' and ']'.
699 {"2001:db8::1", L"2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
700 {"[2001:db8::1", L"[2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
701 {"2001:db8::1]", L"2001:db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
702 // Regular IP addresses.
703 {"[::]", L"[::]", "[::]", url_parse::Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
704 {"[::1]", L"[::1]", "[::1]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
705 {"[1::]", L"[1::]", "[1::]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
707 // Leading zeros should be stripped.
708 {"[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]", url_parse::Component(0,17), CanonHostInfo::IPV6, -1, "00000001000200030004000500060007"},
710 // Upper case letters should be lowercased.
711 {"[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]", url_parse::Component(0,20), CanonHostInfo::IPV6, -1, "000A000B000C00DE00FF0000000100AC"},
713 // The same address can be written with different contractions, but should
714 // get canonicalized to the same thing.
715 {"[1:0:0:2::3:0]", L"[1:0:0:2::3:0]", "[1::2:0:0:3:0]", url_parse::Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
716 {"[1::2:0:0:3:0]", L"[1::2:0:0:3:0]", "[1::2:0:0:3:0]", url_parse::Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
718 // Addresses with embedded IPv4.
719 {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", url_parse::Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
720 {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
721 {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", url_parse::Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
722 {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", url_parse::Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
723 {"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
725 // IPv4 with last component missing.
726 {"[::ffff:192.1.2]", L"[::ffff:192.1.2]", "[::ffff:c001:2]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0010002"},
728 // IPv4 using hex.
729 // TODO(eroman): Should this format be disallowed?
730 {"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
732 // There may be zeros surrounding the "::" contraction.
733 {"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000008"},
735 {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", url_parse::Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
737 // Can only have one "::" contraction in an IPv6 string literal.
738 {"[2001::db8::1]", L"[2001::db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
739 // No more than 2 consecutive ':'s.
740 {"[2001:db8:::1]", L"[2001:db8:::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
741 {"[:::]", L"[:::]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
742 // Non-IP addresses due to invalid characters.
743 {"[2001::.com]", L"[2001::.com]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
744 // If there are not enough components, the last one should fill them out.
745 // ... omitted at this time ...
746 // Too many components means not an IP address. Similarly with too few if using IPv4 compat or mapped addresses.
747 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
748 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
749 {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
750 // Too many bits (even though 8 comonents, the last one holds 32 bits).
751 {"[0:0:0:0:0:0:0:192.168.0.1]", L"[0:0:0:0:0:0:0:192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
753 // Too many bits specified -- the contraction would have to be zero-length
754 // to not exceed 128 bits.
755 {"[1:2:3:4:5:6::192.168.0.1]", L"[1:2:3:4:5:6::192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
757 // The contraction is for 16 bits of zero.
758 {"[1:2:3:4:5:6::8]", L"[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]", url_parse::Component(0,17), CanonHostInfo::IPV6, -1, "00010002000300040005000600000008"},
760 // Cannot have a trailing colon.
761 {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
762 {"[1:2:3:4:5:6:192.168.0.1:]", L"[1:2:3:4:5:6:192.168.0.1:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
764 // Cannot have negative numbers.
765 {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
767 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
768 // The scope_id should be included in the canonicalized URL, and is an
769 // unsigned decimal number.
771 // Invalid because no ID was given after the percent.
773 // Don't allow scope-id
774 {"[1::%1]", L"[1::%1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
775 {"[1::%eth0]", L"[1::%eth0]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
776 {"[1::%]", L"[1::%]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
777 {"[%]", L"[%]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
778 {"[::%:]", L"[::%:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
780 // Don't allow leading or trailing colons.
781 {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
782 {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
783 {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
785 // We allow a single trailing dot.
786 // ... omitted at this time ...
787 // Two dots in a row means not an IP address.
788 {"[::192.168..1]", L"[::192.168..1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
789 // Any non-first components get truncated to one byte.
790 // ... omitted at this time ...
791 // Spaces should be rejected.
792 {"[::1 hello]", L"[::1 hello]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
795 for (size_t i = 0; i < arraysize(cases); i++) {
796 // 8-bit version.
797 url_parse::Component component(0,
798 static_cast<int>(strlen(cases[i].input8)));
800 std::string out_str1;
801 url_canon::StdStringCanonOutput output1(&out_str1);
802 url_canon::CanonHostInfo host_info;
803 url_canon::CanonicalizeIPAddress(cases[i].input8, component, &output1,
804 &host_info);
805 output1.Complete();
807 EXPECT_EQ(cases[i].expected_family, host_info.family);
808 EXPECT_EQ(std::string(cases[i].expected_address_hex),
809 BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
810 if (host_info.family == CanonHostInfo::IPV6) {
811 EXPECT_STREQ(cases[i].expected, out_str1.c_str());
812 EXPECT_EQ(cases[i].expected_component.begin,
813 host_info.out_host.begin);
814 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
817 // 16-bit version.
818 string16 input16(WStringToUTF16(cases[i].input16));
819 component = url_parse::Component(0, static_cast<int>(input16.length()));
821 std::string out_str2;
822 url_canon::StdStringCanonOutput output2(&out_str2);
823 url_canon::CanonicalizeIPAddress(input16.c_str(), component, &output2,
824 &host_info);
825 output2.Complete();
827 EXPECT_EQ(cases[i].expected_family, host_info.family);
828 EXPECT_EQ(std::string(cases[i].expected_address_hex),
829 BytesToHexString(host_info.address, host_info.AddressLength()));
830 if (host_info.family == CanonHostInfo::IPV6) {
831 EXPECT_STREQ(cases[i].expected, out_str2.c_str());
832 EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
833 EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
838 TEST(URLCanonTest, IPEmpty) {
839 std::string out_str1;
840 url_canon::StdStringCanonOutput output1(&out_str1);
841 url_canon::CanonHostInfo host_info;
843 // This tests tests.
844 const char spec[] = "192.168.0.1";
845 url_canon::CanonicalizeIPAddress(spec, url_parse::Component(),
846 &output1, &host_info);
847 EXPECT_FALSE(host_info.IsIPAddress());
849 url_canon::CanonicalizeIPAddress(spec, url_parse::Component(0, 0),
850 &output1, &host_info);
851 EXPECT_FALSE(host_info.IsIPAddress());
854 TEST(URLCanonTest, UserInfo) {
855 // Note that the canonicalizer should escape and treat empty components as
856 // not being there.
858 // We actually parse a full input URL so we can get the initial components.
859 struct UserComponentCase {
860 const char* input;
861 const char* expected;
862 url_parse::Component expected_username;
863 url_parse::Component expected_password;
864 bool expected_success;
865 } user_info_cases[] = {
866 {"http://user:pass@host.com/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
867 {"http://@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
868 {"http://:@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
869 {"http://foo:@host.com/", "foo@", url_parse::Component(0, 3), url_parse::Component(0, -1), true},
870 {"http://:foo@host.com/", ":foo@", url_parse::Component(0, 0), url_parse::Component(1, 3), true},
871 {"http://^ :$\t@host.com/", "%5E%20:$%09@", url_parse::Component(0, 6), url_parse::Component(7, 4), true},
872 {"http://user:pass@/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
873 {"http://%2540:bar@domain.com/", "%2540:bar@", url_parse::Component(0, 5), url_parse::Component(6, 3), true },
875 // IE7 compatability: old versions allowed backslashes in usernames, but
876 // IE7 does not. We disallow it as well.
877 {"ftp://me\\mydomain:pass@foo.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
880 for (size_t i = 0; i < ARRAYSIZE(user_info_cases); i++) {
881 int url_len = static_cast<int>(strlen(user_info_cases[i].input));
882 url_parse::Parsed parsed;
883 url_parse::ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
884 url_parse::Component out_user, out_pass;
885 std::string out_str;
886 url_canon::StdStringCanonOutput output1(&out_str);
888 bool success = url_canon::CanonicalizeUserInfo(user_info_cases[i].input,
889 parsed.username,
890 user_info_cases[i].input,
891 parsed.password,
892 &output1, &out_user,
893 &out_pass);
894 output1.Complete();
896 EXPECT_EQ(user_info_cases[i].expected_success, success);
897 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
898 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
899 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
900 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
901 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
903 // Now try the wide version
904 out_str.clear();
905 url_canon::StdStringCanonOutput output2(&out_str);
906 string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
907 success = url_canon::CanonicalizeUserInfo(wide_input.c_str(),
908 parsed.username,
909 wide_input.c_str(),
910 parsed.password,
911 &output2, &out_user, &out_pass);
912 output2.Complete();
914 EXPECT_EQ(user_info_cases[i].expected_success, success);
915 EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
916 EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
917 EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
918 EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
919 EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
923 TEST(URLCanonTest, Port) {
924 // We only need to test that the number gets properly put into the output
925 // buffer. The parser unit tests will test scanning the number correctly.
927 // Note that the CanonicalizePort will always prepend a colon to the output
928 // to separate it from the colon that it assumes preceeds it.
929 struct PortCase {
930 const char* input;
931 int default_port;
932 const char* expected;
933 url_parse::Component expected_component;
934 bool expected_success;
935 } port_cases[] = {
936 // Invalid input should be copied w/ failure.
937 {"as df", 80, ":as%20df", url_parse::Component(1, 7), false},
938 {"-2", 80, ":-2", url_parse::Component(1, 2), false},
939 // Default port should be omitted.
940 {"80", 80, "", url_parse::Component(0, -1), true},
941 {"8080", 80, ":8080", url_parse::Component(1, 4), true},
942 // PORT_UNSPECIFIED should mean always keep the port.
943 {"80", url_parse::PORT_UNSPECIFIED, ":80", url_parse::Component(1, 2), true},
946 for (size_t i = 0; i < ARRAYSIZE(port_cases); i++) {
947 int url_len = static_cast<int>(strlen(port_cases[i].input));
948 url_parse::Component in_comp(0, url_len);
949 url_parse::Component out_comp;
950 std::string out_str;
951 url_canon::StdStringCanonOutput output1(&out_str);
952 bool success = url_canon::CanonicalizePort(port_cases[i].input, in_comp,
953 port_cases[i].default_port,
954 &output1, &out_comp);
955 output1.Complete();
957 EXPECT_EQ(port_cases[i].expected_success, success);
958 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
959 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
960 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
962 // Now try the wide version
963 out_str.clear();
964 url_canon::StdStringCanonOutput output2(&out_str);
965 string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
966 success = url_canon::CanonicalizePort(wide_input.c_str(), in_comp,
967 port_cases[i].default_port,
968 &output2, &out_comp);
969 output2.Complete();
971 EXPECT_EQ(port_cases[i].expected_success, success);
972 EXPECT_EQ(std::string(port_cases[i].expected), out_str);
973 EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
974 EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
978 TEST(URLCanonTest, Path) {
979 DualComponentCase path_cases[] = {
980 // ----- path collapsing tests -----
981 {"/././foo", L"/././foo", "/foo", url_parse::Component(0, 4), true},
982 {"/./.foo", L"/./.foo", "/.foo", url_parse::Component(0, 5), true},
983 {"/foo/.", L"/foo/.", "/foo/", url_parse::Component(0, 5), true},
984 {"/foo/./", L"/foo/./", "/foo/", url_parse::Component(0, 5), true},
985 // double dots followed by a slash or the end of the string count
986 {"/foo/bar/..", L"/foo/bar/..", "/foo/", url_parse::Component(0, 5), true},
987 {"/foo/bar/../", L"/foo/bar/../", "/foo/", url_parse::Component(0, 5), true},
988 // don't count double dots when they aren't followed by a slash
989 {"/foo/..bar", L"/foo/..bar", "/foo/..bar", url_parse::Component(0, 10), true},
990 // some in the middle
991 {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", url_parse::Component(0, 8), true},
992 {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", url_parse::Component(0, 2), true},
993 // we should not be able to go above the root
994 {"/foo/../../..", L"/foo/../../..", "/", url_parse::Component(0, 1), true},
995 {"/foo/../../../ton", L"/foo/../../../ton", "/ton", url_parse::Component(0, 4), true},
996 // escaped dots should be unescaped and treated the same as dots
997 {"/foo/%2e", L"/foo/%2e", "/foo/", url_parse::Component(0, 5), true},
998 {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", url_parse::Component(0, 8), true},
999 {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", url_parse::Component(0, 6), true},
1000 // Multiple slashes in a row should be preserved and treated like empty
1001 // directory names.
1002 {"////../..", L"////../..", "//", url_parse::Component(0, 2), true},
1004 // ----- escaping tests -----
1005 {"/foo", L"/foo", "/foo", url_parse::Component(0, 4), true},
1006 // Valid escape sequence
1007 {"/%20foo", L"/%20foo", "/%20foo", url_parse::Component(0, 7), true},
1008 // Invalid escape sequence we should pass through unchanged.
1009 {"/foo%", L"/foo%", "/foo%", url_parse::Component(0, 5), true},
1010 {"/foo%2", L"/foo%2", "/foo%2", url_parse::Component(0, 6), true},
1011 // Invalid escape sequence: bad characters should be treated the same as
1012 // the sourrounding text, not as escaped (in this case, UTF-8).
1013 {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", url_parse::Component(0, 10), true},
1014 {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", url_parse::Component(0, 16), true},
1015 {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", url_parse::Component(0, 22), true},
1016 // Regular characters that are escaped should be unescaped
1017 {"/foo%41%7a", L"/foo%41%7a", "/fooAz", url_parse::Component(0, 6), true},
1018 // Funny characters that are unescaped should be escaped
1019 {"/foo\x09\x91%91", NULL, "/foo%09%91%91", url_parse::Component(0, 13), true},
1020 {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", url_parse::Component(0, 16), true},
1021 // Invalid characters that are escaped should cause a failure.
1022 {"/foo%00%51", L"/foo%00%51", "/foo%00Q", url_parse::Component(0, 8), false},
1023 // Some characters should be passed through unchanged regardless of esc.
1024 {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", url_parse::Component(0, 13), true},
1025 // Characters that are properly escaped should not have the case changed
1026 // of hex letters.
1027 {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", url_parse::Component(0, 13), true},
1028 // Funny characters that are unescaped should be escaped
1029 {"/foo\tbar", L"/foo\tbar", "/foo%09bar", url_parse::Component(0, 10), true},
1030 // Backslashes should get converted to forward slashes
1031 {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", url_parse::Component(0, 8), true},
1032 // Hashes found in paths (possibly only when the caller explicitly sets
1033 // the path on an already-parsed URL) should be escaped.
1034 {"/foo#bar", L"/foo#bar", "/foo%23bar", url_parse::Component(0, 10), true},
1035 // %7f should be allowed and %3D should not be unescaped (these were wrong
1036 // in a previous version).
1037 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", url_parse::Component(0, 24), true},
1038 // @ should be passed through unchanged (escaped or unescaped).
1039 {"/@asdf%40", L"/@asdf%40", "/@asdf%40", url_parse::Component(0, 9), true},
1041 // ----- encoding tests -----
1042 // Basic conversions
1043 {"/\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", url_parse::Component(0, 37), true},
1044 // Invalid unicode characters should fail. We only do validation on
1045 // UTF-16 input, so this doesn't happen on 8-bit.
1046 {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", url_parse::Component(0, 13), true},
1047 {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", url_parse::Component(0, 13), false},
1050 for (size_t i = 0; i < arraysize(path_cases); i++) {
1051 if (path_cases[i].input8) {
1052 int len = static_cast<int>(strlen(path_cases[i].input8));
1053 url_parse::Component in_comp(0, len);
1054 url_parse::Component out_comp;
1055 std::string out_str;
1056 url_canon::StdStringCanonOutput output(&out_str);
1057 bool success = url_canon::CanonicalizePath(path_cases[i].input8, in_comp,
1058 &output, &out_comp);
1059 output.Complete();
1061 EXPECT_EQ(path_cases[i].expected_success, success);
1062 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1063 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1064 EXPECT_EQ(path_cases[i].expected, out_str);
1067 if (path_cases[i].input16) {
1068 string16 input16(WStringToUTF16(path_cases[i].input16));
1069 int len = static_cast<int>(input16.length());
1070 url_parse::Component in_comp(0, len);
1071 url_parse::Component out_comp;
1072 std::string out_str;
1073 url_canon::StdStringCanonOutput output(&out_str);
1075 bool success = url_canon::CanonicalizePath(input16.c_str(), in_comp,
1076 &output, &out_comp);
1077 output.Complete();
1079 EXPECT_EQ(path_cases[i].expected_success, success);
1080 EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
1081 EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
1082 EXPECT_EQ(path_cases[i].expected, out_str);
1086 // Manual test: embedded NULLs should be escaped and the URL should be marked
1087 // as invalid.
1088 const char path_with_null[] = "/ab\0c";
1089 url_parse::Component in_comp(0, 5);
1090 url_parse::Component out_comp;
1092 std::string out_str;
1093 url_canon::StdStringCanonOutput output(&out_str);
1094 bool success = url_canon::CanonicalizePath(path_with_null, in_comp,
1095 &output, &out_comp);
1096 output.Complete();
1097 EXPECT_FALSE(success);
1098 EXPECT_EQ("/ab%00c", out_str);
1101 TEST(URLCanonTest, Query) {
1102 struct QueryCase {
1103 const char* input8;
1104 const wchar_t* input16;
1105 const char* encoding;
1106 const char* expected;
1107 } query_cases[] = {
1108 // Regular ASCII case in some different encodings.
1109 {"foo=bar", L"foo=bar", NULL, "?foo=bar"},
1110 {"foo=bar", L"foo=bar", "utf-8", "?foo=bar"},
1111 {"foo=bar", L"foo=bar", "shift_jis", "?foo=bar"},
1112 {"foo=bar", L"foo=bar", "gb2312", "?foo=bar"},
1113 // Allow question marks in the query without escaping
1114 {"as?df", L"as?df", NULL, "?as?df"},
1115 // Always escape '#' since it would mark the ref.
1116 {"as#df", L"as#df", NULL, "?as%23df"},
1117 // Escape some questionable 8-bit characters, but never unescape.
1118 {"\x02hello\x7f bye", L"\x02hello\x7f bye", NULL, "?%02hello%7F%20bye"},
1119 {"%40%41123", L"%40%41123", NULL, "?%40%41123"},
1120 // Chinese input/output
1121 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", NULL, "?q=%E4%BD%A0%E5%A5%BD"},
1122 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "gb2312", "?q=%C4%E3%BA%C3"},
1123 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "big5", "?q=%A7A%A6n"},
1124 // Unencodable character in the destination character set should be
1125 // escaped. The escape sequence unescapes to be the entity name:
1126 // "?q=&#20320;"
1127 {"q=Chinese\xef\xbc\xa7", L"q=Chinese\xff27", "iso-8859-1", "?q=Chinese%26%2365319%3B"},
1128 // Invalid UTF-8/16 input should be replaced with invalid characters.
1129 {"q=\xed\xed", L"q=\xd800\xd800", NULL, "?q=%EF%BF%BD%EF%BF%BD"},
1130 // Don't allow < or > because sometimes they are used for XSS if the
1131 // URL is echoed in content. Firefox does this, IE doesn't.
1132 {"q=<asdf>", L"q=<asdf>", NULL, "?q=%3Casdf%3E"},
1133 // Escape double quotemarks in the query.
1134 {"q=\"asdf\"", L"q=\"asdf\"", NULL, "?q=%22asdf%22"},
1137 for (size_t i = 0; i < ARRAYSIZE(query_cases); i++) {
1138 url_parse::Component out_comp;
1140 UConvScoper conv(query_cases[i].encoding);
1141 ASSERT_TRUE(!query_cases[i].encoding || conv.converter());
1142 url_canon::ICUCharsetConverter converter(conv.converter());
1144 // Map NULL to a NULL converter pointer.
1145 url_canon::ICUCharsetConverter* conv_pointer = &converter;
1146 if (!query_cases[i].encoding)
1147 conv_pointer = NULL;
1149 if (query_cases[i].input8) {
1150 int len = static_cast<int>(strlen(query_cases[i].input8));
1151 url_parse::Component in_comp(0, len);
1152 std::string out_str;
1154 url_canon::StdStringCanonOutput output(&out_str);
1155 url_canon::CanonicalizeQuery(query_cases[i].input8, in_comp,
1156 conv_pointer, &output, &out_comp);
1157 output.Complete();
1159 EXPECT_EQ(query_cases[i].expected, out_str);
1162 if (query_cases[i].input16) {
1163 string16 input16(WStringToUTF16(query_cases[i].input16));
1164 int len = static_cast<int>(input16.length());
1165 url_parse::Component in_comp(0, len);
1166 std::string out_str;
1168 url_canon::StdStringCanonOutput output(&out_str);
1169 url_canon::CanonicalizeQuery(input16.c_str(), in_comp,
1170 conv_pointer, &output, &out_comp);
1171 output.Complete();
1173 EXPECT_EQ(query_cases[i].expected, out_str);
1177 // Extra test for input with embedded NULL;
1178 std::string out_str;
1179 url_canon::StdStringCanonOutput output(&out_str);
1180 url_parse::Component out_comp;
1181 url_canon::CanonicalizeQuery("a \x00z\x01", url_parse::Component(0, 5), NULL,
1182 &output, &out_comp);
1183 output.Complete();
1184 EXPECT_EQ("?a%20%00z%01", out_str);
1187 TEST(URLCanonTest, Ref) {
1188 // Refs are trivial, it just checks the encoding.
1189 DualComponentCase ref_cases[] = {
1190 // Regular one, we shouldn't escape spaces, et al.
1191 {"hello, world", L"hello, world", "#hello, world", url_parse::Component(1, 12), true},
1192 // UTF-8/wide input should be preserved
1193 {"\xc2\xa9", L"\xa9", "#\xc2\xa9", url_parse::Component(1, 2), true},
1194 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
1195 {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", url_parse::Component(1, 6), true},
1196 // Escaping should be preserved unchanged, even invalid ones
1197 {"%41%a", L"%41%a", "#%41%a", url_parse::Component(1, 5), true},
1198 // Invalid UTF-8/16 input should be flagged and the input made valid
1199 {"\xc2", NULL, "#\xef\xbf\xbd", url_parse::Component(1, 3), true},
1200 {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", url_parse::Component(1, 6), true},
1201 // Test a Unicode invalid character.
1202 {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", url_parse::Component(1, 4), true},
1203 // Refs can have # signs and we should preserve them.
1204 {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", url_parse::Component(1, 9), true},
1205 {"#asdf", L"#asdf", "##asdf", url_parse::Component(1, 5), true},
1208 for (size_t i = 0; i < arraysize(ref_cases); i++) {
1209 // 8-bit input
1210 if (ref_cases[i].input8) {
1211 int len = static_cast<int>(strlen(ref_cases[i].input8));
1212 url_parse::Component in_comp(0, len);
1213 url_parse::Component out_comp;
1215 std::string out_str;
1216 url_canon::StdStringCanonOutput output(&out_str);
1217 url_canon::CanonicalizeRef(ref_cases[i].input8, in_comp,
1218 &output, &out_comp);
1219 output.Complete();
1221 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1222 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1223 EXPECT_EQ(ref_cases[i].expected, out_str);
1226 // 16-bit input
1227 if (ref_cases[i].input16) {
1228 string16 input16(WStringToUTF16(ref_cases[i].input16));
1229 int len = static_cast<int>(input16.length());
1230 url_parse::Component in_comp(0, len);
1231 url_parse::Component out_comp;
1233 std::string out_str;
1234 url_canon::StdStringCanonOutput output(&out_str);
1235 url_canon::CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
1236 output.Complete();
1238 EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1239 EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1240 EXPECT_EQ(ref_cases[i].expected, out_str);
1244 // Try one with an embedded NULL. It should be stripped.
1245 const char null_input[5] = "ab\x00z";
1246 url_parse::Component null_input_component(0, 4);
1247 url_parse::Component out_comp;
1249 std::string out_str;
1250 url_canon::StdStringCanonOutput output(&out_str);
1251 url_canon::CanonicalizeRef(null_input, null_input_component,
1252 &output, &out_comp);
1253 output.Complete();
1255 EXPECT_EQ(1, out_comp.begin);
1256 EXPECT_EQ(3, out_comp.len);
1257 EXPECT_EQ("#abz", out_str);
1260 TEST(URLCanonTest, CanonicalizeStandardURL) {
1261 // The individual component canonicalize tests should have caught the cases
1262 // for each of those components. Here, we just need to test that the various
1263 // parts are included or excluded properly, and have the correct separators.
1264 struct URLCase {
1265 const char* input;
1266 const char* expected;
1267 bool expected_success;
1268 } cases[] = {
1269 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1270 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1271 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1272 {"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo", true},
1273 {"www.google.com", ":www.google.com/", true},
1274 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1275 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1276 {"http://user:pass@/", "http://user:pass@/", false},
1277 {"http://%25DOMAIN:foobar@foodomain.com/", "http://%25DOMAIN:foobar@foodomain.com/", true},
1279 // Backslashes should get converted to forward slashes.
1280 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1282 // Busted refs shouldn't make the whole thing fail.
1283 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1285 // Basic port tests.
1286 {"http://foo:80/", "http://foo/", true},
1287 {"http://foo:81/", "http://foo:81/", true},
1288 {"httpa://foo:80/", "httpa://foo:80/", true},
1289 {"http://foo:-80/", "http://foo:-80/", false},
1291 {"https://foo:443/", "https://foo/", true},
1292 {"https://foo:80/", "https://foo:80/", true},
1293 {"ftp://foo:21/", "ftp://foo/", true},
1294 {"ftp://foo:80/", "ftp://foo:80/", true},
1295 {"gopher://foo:70/", "gopher://foo/", true},
1296 {"gopher://foo:443/", "gopher://foo:443/", true},
1297 {"ws://foo:80/", "ws://foo/", true},
1298 {"ws://foo:81/", "ws://foo:81/", true},
1299 {"ws://foo:443/", "ws://foo:443/", true},
1300 {"ws://foo:815/", "ws://foo:815/", true},
1301 {"wss://foo:80/", "wss://foo:80/", true},
1302 {"wss://foo:81/", "wss://foo:81/", true},
1303 {"wss://foo:443/", "wss://foo/", true},
1304 {"wss://foo:815/", "wss://foo:815/", true},
1307 for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
1308 int url_len = static_cast<int>(strlen(cases[i].input));
1309 url_parse::Parsed parsed;
1310 url_parse::ParseStandardURL(cases[i].input, url_len, &parsed);
1312 url_parse::Parsed out_parsed;
1313 std::string out_str;
1314 url_canon::StdStringCanonOutput output(&out_str);
1315 bool success = url_canon::CanonicalizeStandardURL(
1316 cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
1317 output.Complete();
1319 EXPECT_EQ(cases[i].expected_success, success);
1320 EXPECT_EQ(cases[i].expected, out_str);
1324 // The codepath here is the same as for regular canonicalization, so we just
1325 // need to test that things are replaced or not correctly.
1326 TEST(URLCanonTest, ReplaceStandardURL) {
1327 ReplaceCase replace_cases[] = {
1328 // Common case of truncating the path.
1329 {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
1330 // Replace everything
1331 {"http://a:b@google.com:22/foo;bar?baz@cat", "https", "me", "pw", "host.com", "99", "/path", "query", "ref", "https://me:pw@host.com:99/path?query#ref"},
1332 // Replace nothing
1333 {"http://a:b@google.com:22/foo?baz@cat", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "http://a:b@google.com:22/foo?baz@cat"},
1334 // Replace scheme with filesystem. The result is garbage, but you asked
1335 // for it.
1336 {"http://a:b@google.com:22/foo?baz@cat", "filesystem", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem://a:b@google.com:22/foo?baz@cat"},
1339 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1340 const ReplaceCase& cur = replace_cases[i];
1341 int base_len = static_cast<int>(strlen(cur.base));
1342 url_parse::Parsed parsed;
1343 url_parse::ParseStandardURL(cur.base, base_len, &parsed);
1345 url_canon::Replacements<char> r;
1346 typedef url_canon::Replacements<char> R; // Clean up syntax.
1348 // Note that for the scheme we pass in a different clear function since
1349 // there is no function to clear the scheme.
1350 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1351 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1352 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1353 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1354 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1355 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1356 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1357 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1359 std::string out_str;
1360 url_canon::StdStringCanonOutput output(&out_str);
1361 url_parse::Parsed out_parsed;
1362 url_canon::ReplaceStandardURL(replace_cases[i].base, parsed,
1363 r, NULL, &output, &out_parsed);
1364 output.Complete();
1366 EXPECT_EQ(replace_cases[i].expected, out_str);
1369 // The path pointer should be ignored if the address is invalid.
1371 const char src[] = "http://www.google.com/here_is_the_path";
1372 int src_len = static_cast<int>(strlen(src));
1374 url_parse::Parsed parsed;
1375 url_parse::ParseStandardURL(src, src_len, &parsed);
1377 // Replace the path to 0 length string. By using 1 as the string address,
1378 // the test should get an access violation if it tries to dereference it.
1379 url_canon::Replacements<char> r;
1380 r.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component(0, 0));
1381 std::string out_str1;
1382 url_canon::StdStringCanonOutput output1(&out_str1);
1383 url_parse::Parsed new_parsed;
1384 url_canon::ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
1385 output1.Complete();
1386 EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
1388 // Same with an "invalid" path.
1389 r.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component());
1390 std::string out_str2;
1391 url_canon::StdStringCanonOutput output2(&out_str2);
1392 url_canon::ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
1393 output2.Complete();
1394 EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
1398 TEST(URLCanonTest, ReplaceFileURL) {
1399 ReplaceCase replace_cases[] = {
1400 // Replace everything
1401 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1402 // Replace nothing
1403 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1404 // Clear non-path components (common)
1405 {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
1406 // Replace path with something that doesn't begin with a slash and make
1407 // sure it gets added properly.
1408 {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1409 {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1410 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
1411 {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
1412 {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1413 // Replace scheme -- shouldn't do anything.
1414 {"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1417 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1418 const ReplaceCase& cur = replace_cases[i];
1419 int base_len = static_cast<int>(strlen(cur.base));
1420 url_parse::Parsed parsed;
1421 url_parse::ParseFileURL(cur.base, base_len, &parsed);
1423 url_canon::Replacements<char> r;
1424 typedef url_canon::Replacements<char> R; // Clean up syntax.
1425 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1426 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1427 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1428 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1429 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1430 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1431 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1432 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1434 std::string out_str;
1435 url_canon::StdStringCanonOutput output(&out_str);
1436 url_parse::Parsed out_parsed;
1437 url_canon::ReplaceFileURL(cur.base, parsed,
1438 r, NULL, &output, &out_parsed);
1439 output.Complete();
1441 EXPECT_EQ(replace_cases[i].expected, out_str);
1445 TEST(URLCanonTest, ReplaceFileSystemURL) {
1446 ReplaceCase replace_cases[] = {
1447 // Replace everything in the outer URL.
1448 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
1449 // Replace nothing
1450 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"},
1451 // Clear non-path components (common)
1452 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"},
1453 // Replace path with something that doesn't begin with a slash and make
1454 // sure it gets added properly.
1455 {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"},
1456 // Replace scheme -- shouldn't do anything.
1457 {"filesystem:http://u:p@bar.com/t/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
1458 // Replace username -- shouldn't do anything.
1459 {"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, "u2", NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
1460 // Replace password -- shouldn't do anything.
1461 {"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, NULL, "pw2", NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
1462 // Replace host -- shouldn't do anything.
1463 {"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, NULL, NULL, "foo.com", NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
1464 // Replace port -- shouldn't do anything.
1465 {"filesystem:http://u:p@bar.com:40/t/gaba?query#ref", NULL, NULL, NULL, NULL, "41", NULL, NULL, NULL, "filesystem:http://u:p@bar.com:40/t/gaba?query#ref"},
1468 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1469 const ReplaceCase& cur = replace_cases[i];
1470 int base_len = static_cast<int>(strlen(cur.base));
1471 url_parse::Parsed parsed;
1472 url_parse::ParseFileSystemURL(cur.base, base_len, &parsed);
1474 url_canon::Replacements<char> r;
1475 typedef url_canon::Replacements<char> R; // Clean up syntax.
1476 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1477 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1478 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1479 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1480 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1481 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1482 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1483 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1485 std::string out_str;
1486 url_canon::StdStringCanonOutput output(&out_str);
1487 url_parse::Parsed out_parsed;
1488 url_canon::ReplaceFileSystemURL(cur.base, parsed, r, NULL,
1489 &output, &out_parsed);
1490 output.Complete();
1492 EXPECT_EQ(replace_cases[i].expected, out_str);
1496 TEST(URLCanonTest, ReplacePathURL) {
1497 ReplaceCase replace_cases[] = {
1498 // Replace everything
1499 {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
1500 // Replace nothing
1501 {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
1502 // Replace one or the other
1503 {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
1504 {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
1505 {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
1508 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1509 const ReplaceCase& cur = replace_cases[i];
1510 int base_len = static_cast<int>(strlen(cur.base));
1511 url_parse::Parsed parsed;
1512 url_parse::ParsePathURL(cur.base, base_len, &parsed);
1514 url_canon::Replacements<char> r;
1515 typedef url_canon::Replacements<char> R; // Clean up syntax.
1516 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1517 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1518 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1519 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1520 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1521 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1522 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1523 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1525 std::string out_str;
1526 url_canon::StdStringCanonOutput output(&out_str);
1527 url_parse::Parsed out_parsed;
1528 url_canon::ReplacePathURL(cur.base, parsed,
1529 r, &output, &out_parsed);
1530 output.Complete();
1532 EXPECT_EQ(replace_cases[i].expected, out_str);
1536 TEST(URLCanonTest, ReplaceMailtoURL) {
1537 ReplaceCase replace_cases[] = {
1538 // Replace everything
1539 {"mailto:jon@foo.com?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
1540 // Replace nothing
1541 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:jon@foo.com?body=sup"},
1542 // Replace the path
1543 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
1544 // Replace the query
1545 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:jon@foo.com?custom=1"},
1546 // Replace the path and query
1547 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
1548 // Set the query to empty (should leave trailing question mark)
1549 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:jon@foo.com?"},
1550 // Clear the query
1551 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:jon@foo.com"},
1552 // Clear the path
1553 {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
1554 // Clear the path + query
1555 {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
1556 // Setting the ref should have no effect
1557 {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
1560 for (size_t i = 0; i < arraysize(replace_cases); i++) {
1561 const ReplaceCase& cur = replace_cases[i];
1562 int base_len = static_cast<int>(strlen(cur.base));
1563 url_parse::Parsed parsed;
1564 url_parse::ParseMailtoURL(cur.base, base_len, &parsed);
1566 url_canon::Replacements<char> r;
1567 typedef url_canon::Replacements<char> R;
1568 SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1569 SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1570 SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1571 SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1572 SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1573 SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1574 SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1575 SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1577 std::string out_str;
1578 url_canon::StdStringCanonOutput output(&out_str);
1579 url_parse::Parsed out_parsed;
1580 url_canon::ReplaceMailtoURL(cur.base, parsed,
1581 r, &output, &out_parsed);
1582 output.Complete();
1584 EXPECT_EQ(replace_cases[i].expected, out_str);
1588 TEST(URLCanonTest, CanonicalizeFileURL) {
1589 struct URLCase {
1590 const char* input;
1591 const char* expected;
1592 bool expected_success;
1593 url_parse::Component expected_host;
1594 url_parse::Component expected_path;
1595 } cases[] = {
1596 #ifdef _WIN32
1597 // Windows-style paths
1598 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1599 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1600 {"file:", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1601 {"file:UNChost/path", "file://unchost/path", true, url_parse::Component(7, 7), url_parse::Component(14, 5)},
1602 // CanonicalizeFileURL supports absolute Windows style paths for IE
1603 // compatability. Note that the caller must decide that this is a file
1604 // URL itself so it can call the file canonicalizer. This is usually
1605 // done automatically as part of relative URL resolving.
1606 {"c:\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1607 {"C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1608 {"/C|\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1609 {"//C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1610 {"//server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1611 {"\\\\server\\file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1612 {"/\\server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1613 // We should preserve the number of slashes after the colon for IE
1614 // compatability, except when there is none, in which case we should
1615 // add one.
1616 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1617 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1618 // Three slashes should be non-UNC, even if there is no drive spec (IE
1619 // does this, which makes the resulting request invalid).
1620 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, url_parse::Component(), url_parse::Component(7, 12)},
1621 // TODO(brettw) we should probably fail for invalid host names, which
1622 // would change the expected result on this test. We also currently allow
1623 // colon even though it's probably invalid, because its currently the
1624 // "natural" result of the way the canonicalizer is written. There doesn't
1625 // seem to be a strong argument for why allowing it here would be bad, so
1626 // we just tolerate it and the load will fail later.
1627 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, url_parse::Component(7, 2), url_parse::Component(9, 16)},
1628 {"file:filer/home\\me", "file://filer/home/me", true, url_parse::Component(7, 5), url_parse::Component(12, 8)},
1629 // Make sure relative paths can't go above the "C:"
1630 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, url_parse::Component(), url_parse::Component(7, 12)},
1631 // Busted refs shouldn't make the whole thing fail.
1632 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, url_parse::Component(), url_parse::Component(7, 8)},
1633 #else
1634 // Unix-style paths
1635 {"file:///home/me", "file:///home/me", true, url_parse::Component(), url_parse::Component(7, 8)},
1636 // Windowsy ones should get still treated as Unix-style.
1637 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1638 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1639 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
1640 {"//", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1641 {"///", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1642 {"///test", "file:///test", true, url_parse::Component(), url_parse::Component(7, 5)},
1643 {"file://test", "file://test/", true, url_parse::Component(7, 4), url_parse::Component(11, 1)},
1644 {"file://localhost", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
1645 {"file://localhost/", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
1646 {"file://localhost/test", "file://localhost/test", true, url_parse::Component(7, 9), url_parse::Component(16, 5)},
1647 #endif // _WIN32
1650 for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
1651 int url_len = static_cast<int>(strlen(cases[i].input));
1652 url_parse::Parsed parsed;
1653 url_parse::ParseFileURL(cases[i].input, url_len, &parsed);
1655 url_parse::Parsed out_parsed;
1656 std::string out_str;
1657 url_canon::StdStringCanonOutput output(&out_str);
1658 bool success = url_canon::CanonicalizeFileURL(cases[i].input, url_len,
1659 parsed, NULL, &output,
1660 &out_parsed);
1661 output.Complete();
1663 EXPECT_EQ(cases[i].expected_success, success);
1664 EXPECT_EQ(cases[i].expected, out_str);
1666 // Make sure the spec was properly identified, the file canonicalizer has
1667 // different code for writing the spec.
1668 EXPECT_EQ(0, out_parsed.scheme.begin);
1669 EXPECT_EQ(4, out_parsed.scheme.len);
1671 EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
1672 EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
1674 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1675 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1679 TEST(URLCanonTest, CanonicalizeFileSystemURL) {
1680 struct URLCase {
1681 const char* input;
1682 const char* expected;
1683 bool expected_success;
1684 } cases[] = {
1685 {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
1686 {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
1687 {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
1688 {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
1689 {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
1690 {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
1691 {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
1694 for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
1695 int url_len = static_cast<int>(strlen(cases[i].input));
1696 url_parse::Parsed parsed;
1697 url_parse::ParseFileSystemURL(cases[i].input, url_len, &parsed);
1699 url_parse::Parsed out_parsed;
1700 std::string out_str;
1701 url_canon::StdStringCanonOutput output(&out_str);
1702 bool success = url_canon::CanonicalizeFileSystemURL(cases[i].input, url_len,
1703 parsed, NULL, &output,
1704 &out_parsed);
1705 output.Complete();
1707 EXPECT_EQ(cases[i].expected_success, success);
1708 EXPECT_EQ(cases[i].expected, out_str);
1710 // Make sure the spec was properly identified, the filesystem canonicalizer
1711 // has different code for writing the spec.
1712 EXPECT_EQ(0, out_parsed.scheme.begin);
1713 EXPECT_EQ(10, out_parsed.scheme.len);
1714 if (success)
1715 EXPECT_GT(out_parsed.path.len, 0);
1719 TEST(URLCanonTest, CanonicalizePathURL) {
1720 // Path URLs should get canonicalized schemes but nothing else.
1721 struct PathCase {
1722 const char* input;
1723 const char* expected;
1724 } path_cases[] = {
1725 {"javascript:", "javascript:"},
1726 {"JavaScript:Foo", "javascript:Foo"},
1727 {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1730 for (size_t i = 0; i < ARRAYSIZE(path_cases); i++) {
1731 int url_len = static_cast<int>(strlen(path_cases[i].input));
1732 url_parse::Parsed parsed;
1733 url_parse::ParsePathURL(path_cases[i].input, url_len, &parsed);
1735 url_parse::Parsed out_parsed;
1736 std::string out_str;
1737 url_canon::StdStringCanonOutput output(&out_str);
1738 bool success = url_canon::CanonicalizePathURL(path_cases[i].input, url_len,
1739 parsed, &output,
1740 &out_parsed);
1741 output.Complete();
1743 EXPECT_TRUE(success);
1744 EXPECT_EQ(path_cases[i].expected, out_str);
1746 EXPECT_EQ(0, out_parsed.host.begin);
1747 EXPECT_EQ(-1, out_parsed.host.len);
1749 // When we end with a colon at the end, there should be no path.
1750 if (path_cases[i].input[url_len - 1] == ':') {
1751 EXPECT_EQ(0, out_parsed.path.begin);
1752 EXPECT_EQ(-1, out_parsed.path.len);
1757 TEST(URLCanonTest, CanonicalizeMailtoURL) {
1758 struct URLCase {
1759 const char* input;
1760 const char* expected;
1761 bool expected_success;
1762 url_parse::Component expected_path;
1763 url_parse::Component expected_query;
1764 } cases[] = {
1765 {"mailto:addr1", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
1766 {"mailto:addr1@foo.com", "mailto:addr1@foo.com", true, url_parse::Component(7, 13), url_parse::Component()},
1767 // Trailing whitespace is stripped.
1768 {"MaIlTo:addr1 \t ", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
1769 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, url_parse::Component(7, 5), url_parse::Component(13,6)},
1770 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, url_parse::Component(7, 11), url_parse::Component()},
1771 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, url_parse::Component(7, 12), url_parse::Component()},
1772 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, url_parse::Component(7, 13), url_parse::Component()},
1773 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, url_parse::Component(7, 12), url_parse::Component()},
1774 // Null character should be escaped to %00
1775 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, url_parse::Component(7, 13), url_parse::Component(21, 3)},
1776 // Invalid -- UTF-8 encoded surrogate value.
1777 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, url_parse::Component(7, 9), url_parse::Component()},
1778 {"mailto:addr1?", "mailto:addr1?", true, url_parse::Component(7, 5), url_parse::Component(13, 0)},
1781 // Define outside of loop to catch bugs where components aren't reset
1782 url_parse::Parsed parsed;
1783 url_parse::Parsed out_parsed;
1785 for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
1786 int url_len = static_cast<int>(strlen(cases[i].input));
1787 if (i == 8) {
1788 // The 9th test case purposely has a '\0' in it -- don't count it
1789 // as the string terminator.
1790 url_len = 22;
1792 url_parse::ParseMailtoURL(cases[i].input, url_len, &parsed);
1794 std::string out_str;
1795 url_canon::StdStringCanonOutput output(&out_str);
1796 bool success = url_canon::CanonicalizeMailtoURL(cases[i].input, url_len,
1797 parsed, &output,
1798 &out_parsed);
1799 output.Complete();
1801 EXPECT_EQ(cases[i].expected_success, success);
1802 EXPECT_EQ(cases[i].expected, out_str);
1804 // Make sure the spec was properly identified
1805 EXPECT_EQ(0, out_parsed.scheme.begin);
1806 EXPECT_EQ(6, out_parsed.scheme.len);
1808 EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1809 EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1811 EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
1812 EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
1816 #ifndef WIN32
1818 TEST(URLCanonTest, _itoa_s) {
1819 // We fill the buffer with 0xff to ensure that it's getting properly
1820 // null-terminated. We also allocate one byte more than what we tell
1821 // _itoa_s about, and ensure that the extra byte is untouched.
1822 char buf[6];
1823 memset(buf, 0xff, sizeof(buf));
1824 EXPECT_EQ(0, url_canon::_itoa_s(12, buf, sizeof(buf) - 1, 10));
1825 EXPECT_STREQ("12", buf);
1826 EXPECT_EQ('\xFF', buf[3]);
1828 // Test the edge cases - exactly the buffer size and one over
1829 memset(buf, 0xff, sizeof(buf));
1830 EXPECT_EQ(0, url_canon::_itoa_s(1234, buf, sizeof(buf) - 1, 10));
1831 EXPECT_STREQ("1234", buf);
1832 EXPECT_EQ('\xFF', buf[5]);
1834 memset(buf, 0xff, sizeof(buf));
1835 EXPECT_EQ(EINVAL, url_canon::_itoa_s(12345, buf, sizeof(buf) - 1, 10));
1836 EXPECT_EQ('\xFF', buf[5]); // should never write to this location
1838 // Test the template overload (note that this will see the full buffer)
1839 memset(buf, 0xff, sizeof(buf));
1840 EXPECT_EQ(0, url_canon::_itoa_s(12, buf, 10));
1841 EXPECT_STREQ("12", buf);
1842 EXPECT_EQ('\xFF', buf[3]);
1844 memset(buf, 0xff, sizeof(buf));
1845 EXPECT_EQ(0, url_canon::_itoa_s(12345, buf, 10));
1846 EXPECT_STREQ("12345", buf);
1848 EXPECT_EQ(EINVAL, url_canon::_itoa_s(123456, buf, 10));
1850 // Test that radix 16 is supported.
1851 memset(buf, 0xff, sizeof(buf));
1852 EXPECT_EQ(0, url_canon::_itoa_s(1234, buf, sizeof(buf) - 1, 16));
1853 EXPECT_STREQ("4d2", buf);
1854 EXPECT_EQ('\xFF', buf[5]);
1857 TEST(URLCanonTest, _itow_s) {
1858 // We fill the buffer with 0xff to ensure that it's getting properly
1859 // null-terminated. We also allocate one byte more than what we tell
1860 // _itoa_s about, and ensure that the extra byte is untouched.
1861 char16 buf[6];
1862 const char fill_mem = 0xff;
1863 const char16 fill_char = 0xffff;
1864 memset(buf, fill_mem, sizeof(buf));
1865 EXPECT_EQ(0, url_canon::_itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
1866 EXPECT_EQ(WStringToUTF16(L"12"), string16(buf));
1867 EXPECT_EQ(fill_char, buf[3]);
1869 // Test the edge cases - exactly the buffer size and one over
1870 EXPECT_EQ(0, url_canon::_itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
1871 EXPECT_EQ(WStringToUTF16(L"1234"), string16(buf));
1872 EXPECT_EQ(fill_char, buf[5]);
1874 memset(buf, fill_mem, sizeof(buf));
1875 EXPECT_EQ(EINVAL, url_canon::_itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
1876 EXPECT_EQ(fill_char, buf[5]); // should never write to this location
1878 // Test the template overload (note that this will see the full buffer)
1879 memset(buf, fill_mem, sizeof(buf));
1880 EXPECT_EQ(0, url_canon::_itow_s(12, buf, 10));
1881 EXPECT_EQ(WStringToUTF16(L"12"), string16(buf));
1882 EXPECT_EQ(fill_char, buf[3]);
1884 memset(buf, fill_mem, sizeof(buf));
1885 EXPECT_EQ(0, url_canon::_itow_s(12345, buf, 10));
1886 EXPECT_EQ(WStringToUTF16(L"12345"), string16(buf));
1888 EXPECT_EQ(EINVAL, url_canon::_itow_s(123456, buf, 10));
1891 #endif // !WIN32
1893 // Returns true if the given two structures are the same.
1894 static bool ParsedIsEqual(const url_parse::Parsed& a,
1895 const url_parse::Parsed& b) {
1896 return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
1897 a.username.begin == b.username.begin && a.username.len == b.username.len &&
1898 a.password.begin == b.password.begin && a.password.len == b.password.len &&
1899 a.host.begin == b.host.begin && a.host.len == b.host.len &&
1900 a.port.begin == b.port.begin && a.port.len == b.port.len &&
1901 a.path.begin == b.path.begin && a.path.len == b.path.len &&
1902 a.query.begin == b.query.begin && a.query.len == b.query.len &&
1903 a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
1906 TEST(URLCanonTest, ResolveRelativeURL) {
1907 struct RelativeCase {
1908 const char* base; // Input base URL: MUST BE CANONICAL
1909 bool is_base_hier; // Is the base URL hierarchical
1910 bool is_base_file; // Tells us if the base is a file URL.
1911 const char* test; // Input URL to test against.
1912 bool succeed_relative; // Whether we expect IsRelativeURL to succeed
1913 bool is_rel; // Whether we expect |test| to be relative or not.
1914 bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed.
1915 const char* resolved; // What we expect in the result when resolving.
1916 } rel_cases[] = {
1917 // Basic absolute input.
1918 {"http://host/a", true, false, "http://another/", true, false, false, NULL},
1919 {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
1920 // Empty relative URLs should only remove the ref part of the URL,
1921 // leaving the rest unchanged.
1922 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
1923 {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
1924 {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
1925 // Spaces at the ends of the relative path should be ignored.
1926 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
1927 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
1928 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
1929 // Matching schemes without two slashes are treated as relative.
1930 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
1931 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
1932 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
1933 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
1934 // Nonmatching schemes are absolute.
1935 {"http://host/a", true, false, "https:host2", true, false, false, NULL},
1936 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
1937 // Absolute path input
1938 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
1939 {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
1940 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
1941 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
1942 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
1943 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
1944 // Relative path input
1945 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
1946 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
1947 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
1948 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
1949 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
1950 {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
1951 {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
1952 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
1953 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
1954 // Query input
1955 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
1956 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
1957 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
1958 // Ref input
1959 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
1960 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
1961 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
1962 // Non-hierarchical base: no relative handling. Relative input should
1963 // error, and if a scheme is present, it should be treated as absolute.
1964 {"data:foobar", false, false, "baz.html", false, false, false, NULL},
1965 {"data:foobar", false, false, "data:baz", true, false, false, NULL},
1966 {"data:foobar", false, false, "data:/base", true, false, false, NULL},
1967 // Non-hierarchical base: absolute input should succeed.
1968 {"data:foobar", false, false, "http://host/", true, false, false, NULL},
1969 {"data:foobar", false, false, "http:host", true, false, false, NULL},
1970 // Invalid schemes should be treated as relative.
1971 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
1972 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
1973 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
1974 {"data:asdf", false, false, ":foo", false, false, false, NULL},
1975 // We should treat semicolons like any other character in URL resolving
1976 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
1977 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
1978 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
1979 // Relative URLs can also be written as "//foo/bar" which is relative to
1980 // the scheme. In this case, it would take the old scheme, so for http
1981 // the example would resolve to "http://foo/bar".
1982 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
1983 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
1984 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
1985 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
1986 {"http://host/a", true, false, "//", true, true, false, "http:"},
1987 // IE will also allow one or the other to be a backslash to get the same
1988 // behavior.
1989 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
1990 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
1991 #ifdef WIN32
1992 // Resolving against Windows file base URLs.
1993 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
1994 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
1995 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
1996 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
1997 // But two backslashes on Windows should be UNC so should be treated
1998 // as absolute.
1999 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
2000 // IE doesn't support drive specs starting with two slashes. It fails
2001 // immediately and doesn't even try to load. We fix it up to either
2002 // an absolute path or UNC depending on what it looks like.
2003 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
2004 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
2005 // Windows drive specs should be allowed and treated as absolute.
2006 {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
2007 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
2008 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
2009 // Relative paths with drive letters should be allowed when the base is
2010 // also a file.
2011 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
2012 // Treat absolute paths as being off of the drive.
2013 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
2014 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
2015 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
2016 // On Windows, two slashes without a drive letter when the base is a file
2017 // means that the path is UNC.
2018 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
2019 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
2020 #else
2021 // On Unix we fall back to relative behavior since there's nothing else
2022 // reasonable to do.
2023 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
2024 #endif
2025 // Even on Windows, we don't allow relative drive specs when the base
2026 // is not file.
2027 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
2028 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
2029 // Filesystem URL tests; filesystem URLs are only valid and relative if
2030 // they have no scheme, e.g. "./index.html". There's no valid equivalent
2031 // to http:index.html.
2032 {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2033 {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
2034 {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
2035 {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
2036 {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
2037 {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
2038 {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
2039 // Absolute URLs are still not relative to a non-standard base URL.
2040 {"about:blank", false, false, "http://X/A", true, false, true, ""},
2041 {"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
2044 for (size_t i = 0; i < ARRAYSIZE(rel_cases); i++) {
2045 const RelativeCase& cur_case = rel_cases[i];
2047 url_parse::Parsed parsed;
2048 int base_len = static_cast<int>(strlen(cur_case.base));
2049 if (cur_case.is_base_file)
2050 url_parse::ParseFileURL(cur_case.base, base_len, &parsed);
2051 else if (cur_case.is_base_hier)
2052 url_parse::ParseStandardURL(cur_case.base, base_len, &parsed);
2053 else
2054 url_parse::ParsePathURL(cur_case.base, base_len, &parsed);
2056 // First see if it is relative.
2057 int test_len = static_cast<int>(strlen(cur_case.test));
2058 bool is_relative;
2059 url_parse::Component relative_component;
2060 bool succeed_is_rel = url_canon::IsRelativeURL(
2061 cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
2062 &is_relative, &relative_component);
2064 EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
2065 "succeed is rel failure on " << cur_case.test;
2066 EXPECT_EQ(cur_case.is_rel, is_relative) <<
2067 "is rel failure on " << cur_case.test;
2068 // Now resolve it.
2069 if (succeed_is_rel && is_relative && cur_case.is_rel) {
2070 std::string resolved;
2071 url_canon::StdStringCanonOutput output(&resolved);
2072 url_parse::Parsed resolved_parsed;
2074 bool succeed_resolve = url_canon::ResolveRelativeURL(
2075 cur_case.base, parsed, cur_case.is_base_file,
2076 cur_case.test, relative_component, NULL, &output, &resolved_parsed);
2077 output.Complete();
2079 EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
2080 EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
2082 // Verify that the output parsed structure is the same as parsing a
2083 // the URL freshly.
2084 url_parse::Parsed ref_parsed;
2085 int resolved_len = static_cast<int>(resolved.size());
2086 if (cur_case.is_base_file)
2087 url_parse::ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
2088 else if (cur_case.is_base_hier)
2089 url_parse::ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
2090 else
2091 url_parse::ParsePathURL(resolved.c_str(), resolved_len, &ref_parsed);
2092 EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
2097 // It used to be when we did a replacement with a long buffer of UTF-16
2098 // characters, we would get invalid data in the URL. This is because the buffer
2099 // it used to hold the UTF-8 data was resized, while some pointers were still
2100 // kept to the old buffer that was removed.
2101 TEST(URLCanonTest, ReplacementOverflow) {
2102 const char src[] = "file:///C:/foo/bar";
2103 int src_len = static_cast<int>(strlen(src));
2104 url_parse::Parsed parsed;
2105 url_parse::ParseFileURL(src, src_len, &parsed);
2107 // Override two components, the path with something short, and the query with
2108 // sonething long enough to trigger the bug.
2109 url_canon::Replacements<char16> repl;
2110 string16 new_query;
2111 for (int i = 0; i < 4800; i++)
2112 new_query.push_back('a');
2114 string16 new_path(WStringToUTF16(L"/foo"));
2115 repl.SetPath(new_path.c_str(), url_parse::Component(0, 4));
2116 repl.SetQuery(new_query.c_str(),
2117 url_parse::Component(0, static_cast<int>(new_query.length())));
2119 // Call ReplaceComponents on the string. It doesn't matter if we call it for
2120 // standard URLs, file URLs, etc, since they will go to the same replacement
2121 // function that was buggy.
2122 url_parse::Parsed repl_parsed;
2123 std::string repl_str;
2124 url_canon::StdStringCanonOutput repl_output(&repl_str);
2125 url_canon::ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
2126 repl_output.Complete();
2128 // Generate the expected string and check.
2129 std::string expected("file:///foo?");
2130 for (size_t i = 0; i < new_query.length(); i++)
2131 expected.push_back('a');
2132 EXPECT_TRUE(expected == repl_str);