1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
31 #include <unicode/ucnv.h>
33 #include "googleurl/src/url_canon.h"
34 #include "googleurl/src/url_canon_icu.h"
35 #include "googleurl/src/url_canon_internal.h"
36 #include "googleurl/src/url_canon_stdstring.h"
37 #include "googleurl/src/url_parse.h"
38 #include "googleurl/src/url_test_utils.h"
39 #include "testing/gtest/include/gtest/gtest.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.
45 #define ARRAYSIZE ARRAYSIZE_UNSAFE
48 using url_test_utils::WStringToUTF16
;
49 using url_test_utils::ConvertUTF8ToUTF16
;
50 using url_test_utils::ConvertUTF16ToUTF8
;
51 using url_canon::CanonHostInfo
;
55 struct ComponentCase
{
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
{
67 const wchar_t* input16
;
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
{
77 const wchar_t* input16
;
79 url_parse::Component expected_component
;
81 // CanonHostInfo fields, for verbose output.
82 CanonHostInfo::Family expected_family
;
83 int expected_num_ipv4_components
;
99 // Wrapper around a UConverter object that managers creation and destruction.
102 UConvScoper(const char* charset_name
) {
103 UErrorCode err
= U_ZERO_ERROR
;
104 converter_
= ucnv_open(charset_name
, &err
);
109 ucnv_close(converter_
);
112 // Returns the converter object, may be NULL.
113 UConverter
* converter() const { return converter_
; }
116 UConverter
* converter_
;
119 // Magic string used in the replacements code that tells SetupReplComp to
120 // call the clear function.
121 const char kDeleteComp
[] = "|";
123 // Sets up a replacement for a single component. This is given pointers to
124 // the set and clear function for the component being replaced, and will
125 // either set the component (if it exists) or clear it (if the replacement
126 // string matches kDeleteComp).
128 // This template is currently used only for the 8-bit case, and the strlen
129 // causes it to fail in other cases. It is left a template in case we have
130 // tests for wide replacements.
131 template<typename CHAR
>
133 void (url_canon::Replacements
<CHAR
>::*set
)(const CHAR
*,
134 const url_parse::Component
&),
135 void (url_canon::Replacements
<CHAR
>::*clear
)(),
136 url_canon::Replacements
<CHAR
>* rep
,
138 if (str
&& str
[0] == kDeleteComp
[0]) {
141 (rep
->*set
)(str
, url_parse::Component(0, static_cast<int>(strlen(str
))));
147 TEST(URLCanonTest
, UTF
) {
148 // Low-level test that we handle reading, canonicalization, and writing
149 // UTF-8/UTF-16 strings properly.
152 const wchar_t* input16
;
153 bool expected_success
;
156 // Valid canonical input should get passed through & escaped.
157 {"\xe4\xbd\xa0\xe5\xa5\xbd", L
"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
158 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
159 {"\xF0\x90\x8C\x80", L
"\xd800\xdf00", true, "%F0%90%8C%80"},
160 // Non-shortest-form UTF-8 are invalid. The bad char should be replaced
161 // with the invalid character (EF BF DB in UTF-8).
162 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL
, false, "%EF%BF%BD%E5%A5%BD"},
163 // Invalid UTF-8 sequences should be marked as invalid (the first
164 // sequence is truncated).
165 {"\xe4\xa0\xe5\xa5\xbd", L
"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
166 // Character going off the end.
167 {"\xe4\xbd\xa0\xe5\xa5", L
"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
168 // ...same with low surrogates with no high surrogate.
169 {"\xed\xb0\x80", L
"\xdc00", false, "%EF%BF%BD"},
170 // Test a UTF-8 encoded surrogate value is marked as invalid.
172 {"\xed\xa0\x80", NULL
, false, "%EF%BF%BD"},
176 for (size_t i
= 0; i
< ARRAYSIZE(utf_cases
); i
++) {
177 if (utf_cases
[i
].input8
) {
179 url_canon::StdStringCanonOutput
output(&out_str
);
181 int input_len
= static_cast<int>(strlen(utf_cases
[i
].input8
));
183 for (int ch
= 0; ch
< input_len
; ch
++) {
184 success
&= AppendUTF8EscapedChar(utf_cases
[i
].input8
, &ch
, input_len
,
188 EXPECT_EQ(utf_cases
[i
].expected_success
, success
);
189 EXPECT_EQ(std::string(utf_cases
[i
].output
), out_str
);
191 if (utf_cases
[i
].input16
) {
193 url_canon::StdStringCanonOutput
output(&out_str
);
195 string16
input_str(WStringToUTF16(utf_cases
[i
].input16
));
196 int input_len
= static_cast<int>(input_str
.length());
198 for (int ch
= 0; ch
< input_len
; ch
++) {
199 success
&= AppendUTF8EscapedChar(input_str
.c_str(), &ch
, input_len
,
203 EXPECT_EQ(utf_cases
[i
].expected_success
, success
);
204 EXPECT_EQ(std::string(utf_cases
[i
].output
), out_str
);
207 if (utf_cases
[i
].input8
&& utf_cases
[i
].input16
&&
208 utf_cases
[i
].expected_success
) {
209 // Check that the UTF-8 and UTF-16 inputs are equivalent.
212 std::string
input8_str(utf_cases
[i
].input8
);
213 string16
input16_str(WStringToUTF16(utf_cases
[i
].input16
));
214 EXPECT_EQ(input8_str
, ConvertUTF16ToUTF8(input16_str
));
217 EXPECT_EQ(input16_str
, ConvertUTF8ToUTF16(input8_str
));
222 TEST(URLCanonTest
, ICUCharsetConverter
) {
224 const wchar_t* input
;
225 const char* encoding
;
226 const char* expected
;
229 {L
"Hello, world", "utf-8", "Hello, world"},
230 {L
"\x4f60\x597d", "utf-8", "\xe4\xbd\xa0\xe5\xa5\xbd"},
232 {L
"!\xd800\xdf00!", "utf-8", "!\xf0\x90\x8c\x80!"},
234 {L
"\x4f60\x597d", "big5", "\xa7\x41\xa6\x6e"},
235 // Unrepresentable character in the destination set.
236 {L
"hello\x4f60\x06de\x597dworld", "big5", "hello\xa7\x41%26%231758%3B\xa6\x6eworld"},
239 for (size_t i
= 0; i
< ARRAYSIZE(icu_cases
); i
++) {
240 UConvScoper
conv(icu_cases
[i
].encoding
);
241 ASSERT_TRUE(conv
.converter());
242 url_canon::ICUCharsetConverter
converter(conv
.converter());
245 url_canon::StdStringCanonOutput
output(&str
);
247 string16
input_str(WStringToUTF16(icu_cases
[i
].input
));
248 int input_len
= static_cast<int>(input_str
.length());
249 converter
.ConvertFromUTF16(input_str
.c_str(), input_len
, &output
);
252 EXPECT_STREQ(icu_cases
[i
].expected
, str
.c_str());
255 // Test string sizes around the resize boundary for the output to make sure
256 // the converter resizes as needed.
257 const int static_size
= 16;
258 UConvScoper
conv("utf-8");
259 ASSERT_TRUE(conv
.converter());
260 url_canon::ICUCharsetConverter
converter(conv
.converter());
261 for (int i
= static_size
- 2; i
<= static_size
+ 2; i
++) {
262 // Make a string with the appropriate length.
264 for (int ch
= 0; ch
< i
; ch
++)
265 input
.push_back('a');
267 url_canon::RawCanonOutput
<static_size
> output
;
268 converter
.ConvertFromUTF16(input
.c_str(), static_cast<int>(input
.length()),
270 EXPECT_EQ(input
.length(), static_cast<size_t>(output
.length()));
274 TEST(URLCanonTest
, Scheme
) {
275 // Here, we're mostly testing that unusual characters are handled properly.
276 // The canonicalizer doesn't do any parsing or whitespace detection. It will
277 // also do its best on error, and will escape funny sequences (these won't be
278 // valid schemes and it will return error).
280 // Note that the canonicalizer will append a colon to the output to separate
281 // out the rest of the URL, which is not present in the input. We check,
282 // however, that the output range includes everything but the colon.
283 ComponentCase scheme_cases
[] = {
284 {"http", "http:", url_parse::Component(0, 4), true},
285 {"HTTP", "http:", url_parse::Component(0, 4), true},
286 {" HTTP ", "%20http%20:", url_parse::Component(0, 10), false},
287 {"htt: ", "htt%3A%20:", url_parse::Component(0, 9), false},
288 {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", url_parse::Component(0, 22), false},
289 // Don't re-escape something already escaped. Note that it will
290 // "canonicalize" the 'A' to 'a', but that's OK.
291 {"ht%3Atp", "ht%3atp:", url_parse::Component(0, 7), false},
296 for (size_t i
= 0; i
< arraysize(scheme_cases
); i
++) {
297 int url_len
= static_cast<int>(strlen(scheme_cases
[i
].input
));
298 url_parse::Component
in_comp(0, url_len
);
299 url_parse::Component out_comp
;
302 url_canon::StdStringCanonOutput
output1(&out_str
);
303 bool success
= url_canon::CanonicalizeScheme(scheme_cases
[i
].input
,
304 in_comp
, &output1
, &out_comp
);
307 EXPECT_EQ(scheme_cases
[i
].expected_success
, success
);
308 EXPECT_EQ(std::string(scheme_cases
[i
].expected
), out_str
);
309 EXPECT_EQ(scheme_cases
[i
].expected_component
.begin
, out_comp
.begin
);
310 EXPECT_EQ(scheme_cases
[i
].expected_component
.len
, out_comp
.len
);
312 // Now try the wide version
314 url_canon::StdStringCanonOutput
output2(&out_str
);
316 string16
wide_input(ConvertUTF8ToUTF16(scheme_cases
[i
].input
));
317 in_comp
.len
= static_cast<int>(wide_input
.length());
318 success
= url_canon::CanonicalizeScheme(wide_input
.c_str(), in_comp
,
319 &output2
, &out_comp
);
322 EXPECT_EQ(scheme_cases
[i
].expected_success
, success
);
323 EXPECT_EQ(std::string(scheme_cases
[i
].expected
), out_str
);
324 EXPECT_EQ(scheme_cases
[i
].expected_component
.begin
, out_comp
.begin
);
325 EXPECT_EQ(scheme_cases
[i
].expected_component
.len
, out_comp
.len
);
328 // Test the case where the scheme is declared nonexistant, it should be
329 // converted into an empty scheme.
330 url_parse::Component out_comp
;
332 url_canon::StdStringCanonOutput
output(&out_str
);
334 EXPECT_TRUE(url_canon::CanonicalizeScheme("", url_parse::Component(0, -1),
335 &output
, &out_comp
));
338 EXPECT_EQ(std::string(":"), out_str
);
339 EXPECT_EQ(0, out_comp
.begin
);
340 EXPECT_EQ(0, out_comp
.len
);
343 TEST(URLCanonTest
, Host
) {
344 IPAddressCase host_cases
[] = {
345 // Basic canonicalization, uppercase should be converted to lowercase.
346 {"GoOgLe.CoM", L
"GoOgLe.CoM", "google.com", url_parse::Component(0, 10), CanonHostInfo::NEUTRAL
, -1},
347 // Spaces and some other characters should be escaped.
348 {"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},
349 // Exciting different types of spaces!
350 {NULL
, L
"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", url_parse::Component(0, 16), CanonHostInfo::NEUTRAL
, -1},
351 // Other types of space (no-break, zero-width, zero-width-no-break) are
352 // name-prepped away to nothing.
353 {NULL
, L
"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", url_parse::Component(0, 10), CanonHostInfo::NEUTRAL
, -1},
354 // Ideographic full stop (full-width period for Chinese, etc.) should be
356 {NULL
, L
"www.foo\x3002"L
"bar.com", "www.foo.bar.com", url_parse::Component(0, 15), CanonHostInfo::NEUTRAL
, -1},
357 // Invalid unicode characters should fail...
358 // ...In wide input, ICU will barf and we'll end up with the input as
359 // escaped UTF-8 (the invalid character should be replaced with the
360 // replacement character).
361 {"\xef\xb7\x90zyx.com", L
"\xfdd0zyx.com", "%EF%BF%BDzyx.com", url_parse::Component(0, 16), CanonHostInfo::BROKEN
, -1},
362 // ...This is the same as previous but with with escaped.
363 {"%ef%b7%90zyx.com", L
"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", url_parse::Component(0, 16), CanonHostInfo::BROKEN
, -1},
364 // Test name prepping, fullwidth input should be converted to ASCII and NOT
365 // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16.
366 {"\xef\xbc\xa7\xef\xbd\x8f.com", L
"\xff27\xff4f.com", "go.com", url_parse::Component(0, 6), CanonHostInfo::NEUTRAL
, -1},
367 // Test that fullwidth escaped values are properly name-prepped,
368 // then converted or rejected.
369 // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input)
370 {"\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},
371 {"%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},
372 // ...%00 in fullwidth should fail (also as escaped UTF-8 input)
373 {"\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},
374 {"%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},
375 // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN
376 {"\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},
377 // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped
378 // UTF-8 (wide case). The output should be equivalent to the true wide
379 // character input above).
380 {"%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},
381 // Invalid escaped characters should fail and the percents should be
383 {"%zz%66%a", L
"%zz%66%a", "%25zzf%25a", url_parse::Component(0, 10), CanonHostInfo::BROKEN
, -1},
384 // If we get an invalid character that has been escaped.
385 {"%25", L
"%25", "%25", url_parse::Component(0, 3), CanonHostInfo::BROKEN
, -1},
386 {"hello%00", L
"hello%00", "hello%00", url_parse::Component(0, 8), CanonHostInfo::BROKEN
, -1},
387 // Escaped numbers should be treated like IP addresses if they are.
388 {"%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},
389 {"%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},
390 // Invalid escaping should trigger the regular host error handling.
391 {"%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},
392 // Something that isn't exactly an IP should get treated as a host and
394 {"192.168.0.1 hello", L
"192.168.0.1 hello", "192.168.0.1%20hello", url_parse::Component(0, 19), CanonHostInfo::NEUTRAL
, -1},
395 // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP.
396 // These are "0Xc0.0250.01" in fullwidth.
397 {"\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},
398 // Broken IP addresses get marked as such.
399 {"192.168.0.257", L
"192.168.0.257", "192.168.0.257", url_parse::Component(0, 13), CanonHostInfo::BROKEN
, -1},
400 {"[google.com]", L
"[google.com]", "[google.com]", url_parse::Component(0, 12), CanonHostInfo::BROKEN
, -1},
403 // CanonicalizeHost() non-verbose.
405 for (size_t i
= 0; i
< arraysize(host_cases
); i
++) {
407 if (host_cases
[i
].input8
) {
408 int host_len
= static_cast<int>(strlen(host_cases
[i
].input8
));
409 url_parse::Component
in_comp(0, host_len
);
410 url_parse::Component out_comp
;
413 url_canon::StdStringCanonOutput
output(&out_str
);
415 bool success
= url_canon::CanonicalizeHost(host_cases
[i
].input8
, in_comp
,
419 EXPECT_EQ(host_cases
[i
].expected_family
!= CanonHostInfo::BROKEN
,
421 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
422 EXPECT_EQ(host_cases
[i
].expected_component
.begin
, out_comp
.begin
);
423 EXPECT_EQ(host_cases
[i
].expected_component
.len
, out_comp
.len
);
427 if (host_cases
[i
].input16
) {
428 string16
input16(WStringToUTF16(host_cases
[i
].input16
));
429 int host_len
= static_cast<int>(input16
.length());
430 url_parse::Component
in_comp(0, host_len
);
431 url_parse::Component out_comp
;
434 url_canon::StdStringCanonOutput
output(&out_str
);
436 bool success
= url_canon::CanonicalizeHost(input16
.c_str(), in_comp
,
440 EXPECT_EQ(host_cases
[i
].expected_family
!= CanonHostInfo::BROKEN
,
442 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
443 EXPECT_EQ(host_cases
[i
].expected_component
.begin
, out_comp
.begin
);
444 EXPECT_EQ(host_cases
[i
].expected_component
.len
, out_comp
.len
);
448 // CanonicalizeHostVerbose()
449 for (size_t i
= 0; i
< arraysize(host_cases
); i
++) {
451 if (host_cases
[i
].input8
) {
452 int host_len
= static_cast<int>(strlen(host_cases
[i
].input8
));
453 url_parse::Component
in_comp(0, host_len
);
456 url_canon::StdStringCanonOutput
output(&out_str
);
457 CanonHostInfo host_info
;
459 url_canon::CanonicalizeHostVerbose(host_cases
[i
].input8
, in_comp
,
460 &output
, &host_info
);
463 EXPECT_EQ(host_cases
[i
].expected_family
, host_info
.family
);
464 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
465 EXPECT_EQ(host_cases
[i
].expected_component
.begin
,
466 host_info
.out_host
.begin
);
467 EXPECT_EQ(host_cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
468 if (host_cases
[i
].expected_family
== CanonHostInfo::IPV4
) {
469 EXPECT_EQ(host_cases
[i
].expected_num_ipv4_components
,
470 host_info
.num_ipv4_components
);
475 if (host_cases
[i
].input16
) {
476 string16
input16(WStringToUTF16(host_cases
[i
].input16
));
477 int host_len
= static_cast<int>(input16
.length());
478 url_parse::Component
in_comp(0, host_len
);
481 url_canon::StdStringCanonOutput
output(&out_str
);
482 CanonHostInfo host_info
;
484 url_canon::CanonicalizeHostVerbose(input16
.c_str(), in_comp
,
485 &output
, &host_info
);
488 EXPECT_EQ(host_cases
[i
].expected_family
, host_info
.family
);
489 EXPECT_EQ(std::string(host_cases
[i
].expected
), out_str
);
490 EXPECT_EQ(host_cases
[i
].expected_component
.begin
,
491 host_info
.out_host
.begin
);
492 EXPECT_EQ(host_cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
493 if (host_cases
[i
].expected_family
== CanonHostInfo::IPV4
) {
494 EXPECT_EQ(host_cases
[i
].expected_num_ipv4_components
,
495 host_info
.num_ipv4_components
);
501 TEST(URLCanonTest
, IPv4
) {
502 IPAddressCase cases
[] = {
503 // Empty is not an IP address.
504 {"", L
"", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
505 {".", L
".", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
506 // Regular IP addresses in different bases.
507 {"192.168.0.1", L
"192.168.0.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 4},
508 {"0300.0250.00.01", L
"0300.0250.00.01", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 4},
509 {"0xC0.0Xa8.0x0.0x1", L
"0xC0.0Xa8.0x0.0x1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 4},
510 // Non-IP addresses due to invalid characters.
511 {"192.168.9.com", L
"192.168.9.com", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
512 // Invalid characters for the base should be rejected.
513 {"19a.168.0.1", L
"19a.168.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
514 {"0308.0250.00.01", L
"0308.0250.00.01", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
515 {"0xCG.0xA8.0x0.0x1", L
"0xCG.0xA8.0x0.0x1", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
516 // If there are not enough components, the last one should fill them out.
517 {"192", L
"192", "0.0.0.192", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 1},
518 {"0xC0a80001", L
"0xC0a80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 1},
519 {"030052000001", L
"030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 1},
520 {"000030052000001", L
"000030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 1},
521 {"192.168", L
"192.168", "192.0.0.168", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 2},
522 {"192.0x00A80001", L
"192.0x000A80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 2},
523 {"0xc0.052000001", L
"0xc0.052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 2},
524 {"192.168.1", L
"192.168.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3},
525 // Too many components means not an IP address.
526 {"192.168.0.0.1", L
"192.168.0.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
527 // We allow a single trailing dot.
528 {"192.168.0.1.", L
"192.168.0.1.", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 4},
529 {"192.168.0.1. hello", L
"192.168.0.1. hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
530 {"192.168.0.1..", L
"192.168.0.1..", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
531 // Two dots in a row means not an IP address.
532 {"192.168..1", L
"192.168..1", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
533 // Any numerical overflow should be marked as BROKEN.
534 {"0x100.0", L
"0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
535 {"0x100.0.0", L
"0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
536 {"0x100.0.0.0", L
"0x100.0.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
537 {"0.0x100.0.0", L
"0.0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
538 {"0.0.0x100.0", L
"0.0.0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
539 {"0.0.0.0x100", L
"0.0.0.0x100", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
540 {"0.0.0x10000", L
"0.0.0x10000", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
541 {"0.0x1000000", L
"0.0x1000000", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
542 {"0x100000000", L
"0x100000000", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
543 // Repeat the previous tests, minus 1, to verify boundaries.
544 {"0xFF.0", L
"0xFF.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 2},
545 {"0xFF.0.0", L
"0xFF.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 3},
546 {"0xFF.0.0.0", L
"0xFF.0.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 4},
547 {"0.0xFF.0.0", L
"0.0xFF.0.0", "0.255.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 4},
548 {"0.0.0xFF.0", L
"0.0.0xFF.0", "0.0.255.0", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 4},
549 {"0.0.0.0xFF", L
"0.0.0.0xFF", "0.0.0.255", url_parse::Component(0, 9), CanonHostInfo::IPV4
, 4},
550 {"0.0.0xFFFF", L
"0.0.0xFFFF", "0.0.255.255", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3},
551 {"0.0xFFFFFF", L
"0.0xFFFFFF", "0.255.255.255", url_parse::Component(0, 13), CanonHostInfo::IPV4
, 2},
552 {"0xFFFFFFFF", L
"0xFFFFFFFF", "255.255.255.255", url_parse::Component(0, 15), CanonHostInfo::IPV4
, 1},
553 // Old trunctations tests. They're all "BROKEN" now.
554 {"276.256.0xf1a2.077777", L
"276.256.0xf1a2.077777", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
555 {"192.168.0.257", L
"192.168.0.257", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
556 {"192.168.0xa20001", L
"192.168.0xa20001", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
557 {"192.015052000001", L
"192.015052000001", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
558 {"0X12C0a80001", L
"0X12C0a80001", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
559 {"276.1.2", L
"276.1.2", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
560 // Spaces should be rejected.
561 {"192.168.0.1 hello", L
"192.168.0.1 hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
562 // Very large numbers.
563 {"0000000000000300.0x00000000000000fF.00000000000000001", L
"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4
, 3},
564 {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L
"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", url_parse::Component(0, 11), CanonHostInfo::BROKEN
, -1},
565 // A number has no length limit, but long numbers can still overflow.
566 {"00000000000000000001", L
"00000000000000000001", "0.0.0.1", url_parse::Component(0, 7), CanonHostInfo::IPV4
, 1},
567 {"0000000000000000100000000000000001", L
"0000000000000000100000000000000001", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
568 // If a long component is non-numeric, it's a hostname, *not* a broken IP.
569 {"0.0.0.000000000000000000z", L
"0.0.0.000000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
570 {"0.0.0.100000000000000000z", L
"0.0.0.100000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
571 // Truncation of all zeros should still result in 0.
572 {"0.00.0x.0x0", L
"0.00.0x.0x0", "0.0.0.0", url_parse::Component(0, 7), CanonHostInfo::IPV4
, 4},
575 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
577 url_parse::Component
component(0,
578 static_cast<int>(strlen(cases
[i
].input8
)));
580 std::string out_str1
;
581 url_canon::StdStringCanonOutput
output1(&out_str1
);
582 url_canon::CanonHostInfo host_info
;
583 url_canon::CanonicalizeIPAddress(cases
[i
].input8
, component
, &output1
,
587 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
588 if (host_info
.family
== CanonHostInfo::IPV4
) {
589 EXPECT_STREQ(cases
[i
].expected
, out_str1
.c_str());
590 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
591 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
592 EXPECT_EQ(cases
[i
].expected_num_ipv4_components
,
593 host_info
.num_ipv4_components
);
597 string16
input16(WStringToUTF16(cases
[i
].input16
));
598 component
= url_parse::Component(0, static_cast<int>(input16
.length()));
600 std::string out_str2
;
601 url_canon::StdStringCanonOutput
output2(&out_str2
);
602 url_canon::CanonicalizeIPAddress(input16
.c_str(), component
, &output2
,
606 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
607 if (host_info
.family
== CanonHostInfo::IPV4
) {
608 EXPECT_STREQ(cases
[i
].expected
, out_str2
.c_str());
609 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
610 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
611 EXPECT_EQ(cases
[i
].expected_num_ipv4_components
,
612 host_info
.num_ipv4_components
);
617 TEST(URLCanonTest
, IPv6
) {
618 IPAddressCase cases
[] = {
619 // Empty is not an IP address.
620 {"", L
"", "", url_parse::Component(), CanonHostInfo::NEUTRAL
, -1},
621 // Non-IPs with [:] characters are marked BROKEN.
622 {":", L
":", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
623 {"[", L
"[", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
624 {"[:", L
"[:", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
625 {"]", L
"]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
626 {":]", L
":]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
627 {"[]", L
"[]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
628 {"[:]", L
"[:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
629 // Regular IP address is invalid without bounding '[' and ']'.
630 {"2001:db8::1", L
"2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
631 {"[2001:db8::1", L
"[2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
632 {"2001:db8::1]", L
"2001:db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
633 // Regular IP addresses.
634 {"[::]", L
"[::]", "[::]", url_parse::Component(0,4), CanonHostInfo::IPV6
, -1},
635 {"[::1]", L
"[::1]", "[::1]", url_parse::Component(0,5), CanonHostInfo::IPV6
, -1},
636 {"[1::]", L
"[1::]", "[1::]", url_parse::Component(0,5), CanonHostInfo::IPV6
, -1},
637 {"[::192.168.0.1]", L
"[::192.168.0.1]", "[::c0a8:1]", url_parse::Component(0,10), CanonHostInfo::IPV6
, -1},
638 {"[::ffff:192.168.0.1]", L
"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6
, -1},
640 // Leading zeros should be stripped.
641 {"[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},
643 // Upper case letters should be lowercased.
644 {"[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},
646 // The same address can be written with different contractions, but should
647 // get canonicalized to the same thing.
648 {"[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},
649 {"[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},
652 // Only mapped and compat addresses can have IPv4 syntax embedded.
653 {"[::eeee:192.168.0.1]", L
"[::eeee:192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
654 {"[2001::192.168.0.1]", L
"[2001::192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
655 {"[1:2:192.168.0.1:5:6]", L
"[1:2:192.168.0.1:5:6]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
657 // IPv4 with last component missing.
658 {"[::ffff:192.1.2]", L
"[::ffff:192.1.2]", "[::ffff:c001:2]", url_parse::Component(0,15), CanonHostInfo::IPV6
, -1},
661 // TODO(eroman): Should this format be disallowed?
662 {"[::ffff:0xC0.0Xa8.0x0.0x1]", L
"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6
, -1},
664 // There may be zeros surrounding the "::" contraction.
665 {"[0:0::0:0:8]", L
"[0:0::0:0:8]", "[::8]", url_parse::Component(0,5), CanonHostInfo::IPV6
, -1},
667 {"[2001:db8::1]", L
"[2001:db8::1]", "[2001:db8::1]", url_parse::Component(0,13), CanonHostInfo::IPV6
, -1},
669 // Can only have one "::" contraction in an IPv6 string literal.
670 {"[2001::db8::1]", L
"[2001::db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
671 // No more than 2 consecutive ':'s.
672 {"[2001:db8:::1]", L
"[2001:db8:::1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
673 {"[:::]", L
"[:::]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
674 // Non-IP addresses due to invalid characters.
675 {"[2001::.com]", L
"[2001::.com]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
676 // If there are not enough components, the last one should fill them out.
677 // ... omitted at this time ...
678 // Too many components means not an IP address. Similarly with too few if using IPv4 compat or mapped addresses.
679 {"[::192.168.0.0.1]", L
"[::192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
680 {"[::ffff:192.168.0.0.1]", L
"[::ffff:192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
681 {"[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},
682 // Too many bits (even though 8 comonents, the last one holds 32 bits).
683 {"[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},
685 // Too many bits specified -- the contraction would have to be zero-length
686 // to not exceed 128 bits.
687 {"[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},
689 // The contraction is for 16 bits of zero.
690 {"[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},
692 // Cannot have a trailing colon.
693 {"[1:2:3:4:5:6:7:8:]", L
"[1:2:3:4:5:6:7:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
694 {"[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},
696 // Cannot have negative numbers.
697 {"[-1:2:3:4:5:6:7:8]", L
"[-1:2:3:4:5:6:7:8]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
699 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
700 // The scope_id should be included in the canonicalized URL, and is an
701 // unsigned decimal number.
703 // Invalid because no ID was given after the percent.
705 // Don't allow scope-id
706 {"[1::%1]", L
"[1::%1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
707 {"[1::%eth0]", L
"[1::%eth0]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
708 {"[1::%]", L
"[1::%]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
709 {"[%]", L
"[%]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
710 {"[::%:]", L
"[::%:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
712 // Don't allow leading or trailing colons.
713 {"[:0:0::0:0:8]", L
"[:0:0::0:0:8]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
714 {"[0:0::0:0:8:]", L
"[0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
715 {"[:0:0::0:0:8:]", L
"[:0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
717 // We allow a single trailing dot.
718 // ... omitted at this time ...
719 // Two dots in a row means not an IP address.
720 {"[::192.168..1]", L
"[::192.168..1]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
721 // Any non-first components get truncated to one byte.
722 // ... omitted at this time ...
723 // Spaces should be rejected.
724 {"[::1 hello]", L
"[::1 hello]", "", url_parse::Component(), CanonHostInfo::BROKEN
, -1},
727 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
729 url_parse::Component
component(0,
730 static_cast<int>(strlen(cases
[i
].input8
)));
732 std::string out_str1
;
733 url_canon::StdStringCanonOutput
output1(&out_str1
);
734 url_canon::CanonHostInfo host_info
;
735 url_canon::CanonicalizeIPAddress(cases
[i
].input8
, component
, &output1
,
739 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
740 if (host_info
.family
== CanonHostInfo::IPV6
) {
741 EXPECT_STREQ(cases
[i
].expected
, out_str1
.c_str());
742 EXPECT_EQ(cases
[i
].expected_component
.begin
,
743 host_info
.out_host
.begin
);
744 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
748 string16
input16(WStringToUTF16(cases
[i
].input16
));
749 component
= url_parse::Component(0, static_cast<int>(input16
.length()));
751 std::string out_str2
;
752 url_canon::StdStringCanonOutput
output2(&out_str2
);
753 url_canon::CanonicalizeIPAddress(input16
.c_str(), component
, &output2
,
757 EXPECT_EQ(cases
[i
].expected_family
, host_info
.family
);
758 if (host_info
.family
== CanonHostInfo::IPV6
) {
759 EXPECT_STREQ(cases
[i
].expected
, out_str2
.c_str());
760 EXPECT_EQ(cases
[i
].expected_component
.begin
, host_info
.out_host
.begin
);
761 EXPECT_EQ(cases
[i
].expected_component
.len
, host_info
.out_host
.len
);
766 TEST(URLCanonTest
, UserInfo
) {
767 // Note that the canonicalizer should escape and treat empty components as
770 // We actually parse a full input URL so we can get the initial components.
771 struct UserComponentCase
{
773 const char* expected
;
774 url_parse::Component expected_username
;
775 url_parse::Component expected_password
;
776 bool expected_success
;
777 } user_info_cases
[] = {
778 {"http://user:pass@host.com/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
779 {"http://@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
780 {"http://:@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
781 {"http://foo:@host.com/", "foo@", url_parse::Component(0, 3), url_parse::Component(0, -1), true},
782 {"http://:foo@host.com/", ":foo@", url_parse::Component(0, 0), url_parse::Component(1, 3), true},
783 {"http://^ :$\t@host.com/", "%5E%20:$%09@", url_parse::Component(0, 6), url_parse::Component(7, 4), true},
784 {"http://user:pass@/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
785 {"http://%2540:bar@domain.com/", "%2540:bar@", url_parse::Component(0, 5), url_parse::Component(6, 3), true },
787 // IE7 compatability: old versions allowed backslashes in usernames, but
788 // IE7 does not. We disallow it as well.
789 {"ftp://me\\mydomain:pass@foo.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
792 for (size_t i
= 0; i
< ARRAYSIZE(user_info_cases
); i
++) {
793 int url_len
= static_cast<int>(strlen(user_info_cases
[i
].input
));
794 url_parse::Parsed parsed
;
795 url_parse::ParseStandardURL(user_info_cases
[i
].input
, url_len
, &parsed
);
796 url_parse::Component out_user
, out_pass
;
798 url_canon::StdStringCanonOutput
output1(&out_str
);
800 bool success
= url_canon::CanonicalizeUserInfo(user_info_cases
[i
].input
,
802 user_info_cases
[i
].input
,
808 EXPECT_EQ(user_info_cases
[i
].expected_success
, success
);
809 EXPECT_EQ(std::string(user_info_cases
[i
].expected
), out_str
);
810 EXPECT_EQ(user_info_cases
[i
].expected_username
.begin
, out_user
.begin
);
811 EXPECT_EQ(user_info_cases
[i
].expected_username
.len
, out_user
.len
);
812 EXPECT_EQ(user_info_cases
[i
].expected_password
.begin
, out_pass
.begin
);
813 EXPECT_EQ(user_info_cases
[i
].expected_password
.len
, out_pass
.len
);
815 // Now try the wide version
817 url_canon::StdStringCanonOutput
output2(&out_str
);
818 string16
wide_input(ConvertUTF8ToUTF16(user_info_cases
[i
].input
));
819 success
= url_canon::CanonicalizeUserInfo(wide_input
.c_str(),
823 &output2
, &out_user
, &out_pass
);
826 EXPECT_EQ(user_info_cases
[i
].expected_success
, success
);
827 EXPECT_EQ(std::string(user_info_cases
[i
].expected
), out_str
);
828 EXPECT_EQ(user_info_cases
[i
].expected_username
.begin
, out_user
.begin
);
829 EXPECT_EQ(user_info_cases
[i
].expected_username
.len
, out_user
.len
);
830 EXPECT_EQ(user_info_cases
[i
].expected_password
.begin
, out_pass
.begin
);
831 EXPECT_EQ(user_info_cases
[i
].expected_password
.len
, out_pass
.len
);
835 TEST(URLCanonTest
, Port
) {
836 // We only need to test that the number gets properly put into the output
837 // buffer. The parser unit tests will test scanning the number correctly.
839 // Note that the CanonicalizePort will always prepend a colon to the output
840 // to separate it from the colon that it assumes preceeds it.
844 const char* expected
;
845 url_parse::Component expected_component
;
846 bool expected_success
;
848 // Invalid input should be copied w/ failure.
849 {"as df", 80, ":as%20df", url_parse::Component(1, 7), false},
850 {"-2", 80, ":-2", url_parse::Component(1, 2), false},
851 // Default port should be omitted.
852 {"80", 80, "", url_parse::Component(0, -1), true},
853 {"8080", 80, ":8080", url_parse::Component(1, 4), true},
854 // PORT_UNSPECIFIED should mean always keep the port.
855 {"80", url_parse::PORT_UNSPECIFIED
, ":80", url_parse::Component(1, 2), true},
858 for (size_t i
= 0; i
< ARRAYSIZE(port_cases
); i
++) {
859 int url_len
= static_cast<int>(strlen(port_cases
[i
].input
));
860 url_parse::Component
in_comp(0, url_len
);
861 url_parse::Component out_comp
;
863 url_canon::StdStringCanonOutput
output1(&out_str
);
864 bool success
= url_canon::CanonicalizePort(port_cases
[i
].input
, in_comp
,
865 port_cases
[i
].default_port
,
866 &output1
, &out_comp
);
869 EXPECT_EQ(port_cases
[i
].expected_success
, success
);
870 EXPECT_EQ(std::string(port_cases
[i
].expected
), out_str
);
871 EXPECT_EQ(port_cases
[i
].expected_component
.begin
, out_comp
.begin
);
872 EXPECT_EQ(port_cases
[i
].expected_component
.len
, out_comp
.len
);
874 // Now try the wide version
876 url_canon::StdStringCanonOutput
output2(&out_str
);
877 string16
wide_input(ConvertUTF8ToUTF16(port_cases
[i
].input
));
878 success
= url_canon::CanonicalizePort(wide_input
.c_str(), in_comp
,
879 port_cases
[i
].default_port
,
880 &output2
, &out_comp
);
883 EXPECT_EQ(port_cases
[i
].expected_success
, success
);
884 EXPECT_EQ(std::string(port_cases
[i
].expected
), out_str
);
885 EXPECT_EQ(port_cases
[i
].expected_component
.begin
, out_comp
.begin
);
886 EXPECT_EQ(port_cases
[i
].expected_component
.len
, out_comp
.len
);
890 TEST(URLCanonTest
, Path
) {
891 DualComponentCase path_cases
[] = {
892 // ----- path collapsing tests -----
893 {"/././foo", L
"/././foo", "/foo", url_parse::Component(0, 4), true},
894 {"/./.foo", L
"/./.foo", "/.foo", url_parse::Component(0, 5), true},
895 {"/foo/.", L
"/foo/.", "/foo/", url_parse::Component(0, 5), true},
896 {"/foo/./", L
"/foo/./", "/foo/", url_parse::Component(0, 5), true},
897 // double dots followed by a slash or the end of the string count
898 {"/foo/bar/..", L
"/foo/bar/..", "/foo/", url_parse::Component(0, 5), true},
899 {"/foo/bar/../", L
"/foo/bar/../", "/foo/", url_parse::Component(0, 5), true},
900 // don't count double dots when they aren't followed by a slash
901 {"/foo/..bar", L
"/foo/..bar", "/foo/..bar", url_parse::Component(0, 10), true},
902 // some in the middle
903 {"/foo/bar/../ton", L
"/foo/bar/../ton", "/foo/ton", url_parse::Component(0, 8), true},
904 {"/foo/bar/../ton/../../a", L
"/foo/bar/../ton/../../a", "/a", url_parse::Component(0, 2), true},
905 // we should not be able to go above the root
906 {"/foo/../../..", L
"/foo/../../..", "/", url_parse::Component(0, 1), true},
907 {"/foo/../../../ton", L
"/foo/../../../ton", "/ton", url_parse::Component(0, 4), true},
908 // escaped dots should be unescaped and treated the same as dots
909 {"/foo/%2e", L
"/foo/%2e", "/foo/", url_parse::Component(0, 5), true},
910 {"/foo/%2e%2", L
"/foo/%2e%2", "/foo/.%2", url_parse::Component(0, 8), true},
911 {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L
"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", url_parse::Component(0, 6), true},
912 // Multiple slashes in a row should be preserved and treated like empty
914 {"////../..", L
"////../..", "//", url_parse::Component(0, 2), true},
916 // ----- escaping tests -----
917 {"/foo", L
"/foo", "/foo", url_parse::Component(0, 4), true},
918 // Valid escape sequence
919 {"/%20foo", L
"/%20foo", "/%20foo", url_parse::Component(0, 7), true},
920 // Invalid escape sequence we should pass through unchanged.
921 {"/foo%", L
"/foo%", "/foo%", url_parse::Component(0, 5), true},
922 {"/foo%2", L
"/foo%2", "/foo%2", url_parse::Component(0, 6), true},
923 // Invalid escape sequence: bad characters should be treated the same as
924 // the sourrounding text, not as escaped (in this case, UTF-8).
925 {"/foo%2zbar", L
"/foo%2zbar", "/foo%2zbar", url_parse::Component(0, 10), true},
926 {"/foo%2\xc2\xa9zbar", NULL
, "/foo%2%C2%A9zbar", url_parse::Component(0, 16), true},
927 {NULL
, L
"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", url_parse::Component(0, 22), true},
928 // Regular characters that are escaped should be unescaped
929 {"/foo%41%7a", L
"/foo%41%7a", "/fooAz", url_parse::Component(0, 6), true},
930 // Funny characters that are unescaped should be escaped
931 {"/foo\x09\x91%91", NULL
, "/foo%09%91%91", url_parse::Component(0, 13), true},
932 {NULL
, L
"/foo\x09\x91%91", "/foo%09%C2%91%91", url_parse::Component(0, 16), true},
933 // Invalid characters that are escaped should cause a failure.
934 {"/foo%00%51", L
"/foo%00%51", "/foo%00Q", url_parse::Component(0, 8), false},
935 // Some characters should be passed through unchanged regardless of esc.
936 {"/(%28:%3A%29)", L
"/(%28:%3A%29)", "/(%28:%3A%29)", url_parse::Component(0, 13), true},
937 // Characters that are properly escaped should not have the case changed
939 {"/%3A%3a%3C%3c", L
"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", url_parse::Component(0, 13), true},
940 // Funny characters that are unescaped should be escaped
941 {"/foo\tbar", L
"/foo\tbar", "/foo%09bar", url_parse::Component(0, 10), true},
942 // Backslashes should get converted to forward slashes
943 {"\\foo\\bar", L
"\\foo\\bar", "/foo/bar", url_parse::Component(0, 8), true},
944 // Hashes found in paths (possibly only when the caller explicitly sets
945 // the path on an already-parsed URL) should be escaped.
946 {"/foo#bar", L
"/foo#bar", "/foo%23bar", url_parse::Component(0, 10), true},
947 // %7f should be allowed and %3D should not be unescaped (these were wrong
948 // in a previous version).
949 {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L
"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", url_parse::Component(0, 24), true},
950 // @ should be unescaped.
951 {"/@asdf%40", L
"/@asdf%40", "/@asdf@", url_parse::Component(0, 7), true},
953 // ----- encoding tests -----
955 {"/\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},
956 // Invalid unicode characters should fail. We only do validation on
957 // UTF-16 input, so this doesn't happen on 8-bit.
958 {"/\xef\xb7\x90zyx", NULL
, "/%EF%B7%90zyx", url_parse::Component(0, 13), true},
959 {NULL
, L
"/\xfdd0zyx", "/%EF%BF%BDzyx", url_parse::Component(0, 13), false},
962 for (size_t i
= 0; i
< arraysize(path_cases
); i
++) {
963 if (path_cases
[i
].input8
) {
964 int len
= static_cast<int>(strlen(path_cases
[i
].input8
));
965 url_parse::Component
in_comp(0, len
);
966 url_parse::Component out_comp
;
968 url_canon::StdStringCanonOutput
output(&out_str
);
969 bool success
= url_canon::CanonicalizePath(path_cases
[i
].input8
, in_comp
,
973 EXPECT_EQ(path_cases
[i
].expected_success
, success
);
974 EXPECT_EQ(path_cases
[i
].expected_component
.begin
, out_comp
.begin
);
975 EXPECT_EQ(path_cases
[i
].expected_component
.len
, out_comp
.len
);
976 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
979 if (path_cases
[i
].input16
) {
980 string16
input16(WStringToUTF16(path_cases
[i
].input16
));
981 int len
= static_cast<int>(input16
.length());
982 url_parse::Component
in_comp(0, len
);
983 url_parse::Component out_comp
;
985 url_canon::StdStringCanonOutput
output(&out_str
);
987 bool success
= url_canon::CanonicalizePath(input16
.c_str(), in_comp
,
991 EXPECT_EQ(path_cases
[i
].expected_success
, success
);
992 EXPECT_EQ(path_cases
[i
].expected_component
.begin
, out_comp
.begin
);
993 EXPECT_EQ(path_cases
[i
].expected_component
.len
, out_comp
.len
);
994 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
998 // Manual test: embedded NULLs should be escaped and the URL should be marked
1000 const char path_with_null
[] = "/ab\0c";
1001 url_parse::Component
in_comp(0, 5);
1002 url_parse::Component out_comp
;
1004 std::string out_str
;
1005 url_canon::StdStringCanonOutput
output(&out_str
);
1006 bool success
= url_canon::CanonicalizePath(path_with_null
, in_comp
,
1007 &output
, &out_comp
);
1009 EXPECT_FALSE(success
);
1010 EXPECT_EQ("/ab%00c", out_str
);
1013 TEST(URLCanonTest
, Query
) {
1016 const wchar_t* input16
;
1017 const char* encoding
;
1018 const char* expected
;
1020 // Regular ASCII case in some different encodings.
1021 {"foo=bar", L
"foo=bar", NULL
, "?foo=bar"},
1022 {"foo=bar", L
"foo=bar", "utf-8", "?foo=bar"},
1023 {"foo=bar", L
"foo=bar", "shift_jis", "?foo=bar"},
1024 {"foo=bar", L
"foo=bar", "gb2312", "?foo=bar"},
1025 // Allow question marks in the query without escaping
1026 {"as?df", L
"as?df", NULL
, "?as?df"},
1027 // Always escape '#' since it would mark the ref.
1028 {"as#df", L
"as#df", NULL
, "?as%23df"},
1029 // Escape some questionable 8-bit characters, but never unescape.
1030 {"\x02hello\x7f bye", L
"\x02hello\x7f bye", NULL
, "?%02hello%7F%20bye"},
1031 {"%40%41123", L
"%40%41123", NULL
, "?%40%41123"},
1032 // Chinese input/output
1033 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", NULL
, "?q=%E4%BD%A0%E5%A5%BD"},
1034 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", "gb2312", "?q=%C4%E3%BA%C3"},
1035 {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L
"q=\x4f60\x597d", "big5", "?q=%A7A%A6n"},
1036 // Unencodable character in the destination character set should be
1037 // escaped. The escape sequence unescapes to be the entity name:
1039 {"q=Chinese\xef\xbc\xa7", L
"q=Chinese\xff27", "iso-8859-1", "?q=Chinese%26%2365319%3B"},
1040 // Invalid UTF-8/16 input should be replaced with invalid characters.
1041 {"q=\xed\xed", L
"q=\xd800\xd800", NULL
, "?q=%EF%BF%BD%EF%BF%BD"},
1042 // Don't allow < or > because sometimes they are used for XSS if the
1043 // URL is echoed in content. Firefox does this, IE doesn't.
1044 {"q=<asdf>", L
"q=<asdf>", NULL
, "?q=%3Casdf%3E"},
1045 // Escape double quotemarks in the query.
1046 {"q=\"asdf\"", L
"q=\"asdf\"", NULL
, "?q=%22asdf%22"},
1049 for (size_t i
= 0; i
< ARRAYSIZE(query_cases
); i
++) {
1050 url_parse::Component out_comp
;
1052 UConvScoper
conv(query_cases
[i
].encoding
);
1053 ASSERT_TRUE(!query_cases
[i
].encoding
|| conv
.converter());
1054 url_canon::ICUCharsetConverter
converter(conv
.converter());
1056 // Map NULL to a NULL converter pointer.
1057 url_canon::ICUCharsetConverter
* conv_pointer
= &converter
;
1058 if (!query_cases
[i
].encoding
)
1059 conv_pointer
= NULL
;
1061 if (query_cases
[i
].input8
) {
1062 int len
= static_cast<int>(strlen(query_cases
[i
].input8
));
1063 url_parse::Component
in_comp(0, len
);
1064 std::string out_str
;
1066 url_canon::StdStringCanonOutput
output(&out_str
);
1067 url_canon::CanonicalizeQuery(query_cases
[i
].input8
, in_comp
,
1068 conv_pointer
, &output
, &out_comp
);
1071 EXPECT_EQ(query_cases
[i
].expected
, out_str
);
1074 if (query_cases
[i
].input16
) {
1075 string16
input16(WStringToUTF16(query_cases
[i
].input16
));
1076 int len
= static_cast<int>(input16
.length());
1077 url_parse::Component
in_comp(0, len
);
1078 std::string out_str
;
1080 url_canon::StdStringCanonOutput
output(&out_str
);
1081 url_canon::CanonicalizeQuery(input16
.c_str(), in_comp
,
1082 conv_pointer
, &output
, &out_comp
);
1085 EXPECT_EQ(query_cases
[i
].expected
, out_str
);
1089 // Extra test for input with embedded NULL;
1090 std::string out_str
;
1091 url_canon::StdStringCanonOutput
output(&out_str
);
1092 url_parse::Component out_comp
;
1093 url_canon::CanonicalizeQuery("a \x00z\x01", url_parse::Component(0, 5), NULL
,
1094 &output
, &out_comp
);
1096 EXPECT_EQ("?a%20%00z%01", out_str
);
1099 TEST(URLCanonTest
, Ref
) {
1100 // Refs are trivial, it just checks the encoding.
1101 DualComponentCase ref_cases
[] = {
1102 // Regular one, we shouldn't escape spaces, et al.
1103 {"hello, world", L
"hello, world", "#hello, world", url_parse::Component(1, 12), true},
1104 // UTF-8/wide input should be preserved
1105 {"\xc2\xa9", L
"\xa9", "#\xc2\xa9", url_parse::Component(1, 2), true},
1106 // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
1107 {"\xF0\x90\x8C\x80ss", L
"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", url_parse::Component(1, 6), true},
1108 // Escaping should be preserved unchanged, even invalid ones
1109 {"%41%a", L
"%41%a", "#%41%a", url_parse::Component(1, 5), true},
1110 // Invalid UTF-8/16 input should be flagged and the input made valid
1111 {"\xc2", NULL
, "#\xef\xbf\xbd", url_parse::Component(1, 3), true},
1112 {NULL
, L
"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", url_parse::Component(1, 6), true},
1113 // Test a Unicode invalid character.
1114 {"a\xef\xb7\x90", L
"a\xfdd0", "#a\xef\xbf\xbd", url_parse::Component(1, 4), true},
1115 // Refs can have # signs and we should preserve them.
1116 {"asdf#qwer", L
"asdf#qwer", "#asdf#qwer", url_parse::Component(1, 9), true},
1117 {"#asdf", L
"#asdf", "##asdf", url_parse::Component(1, 5), true},
1120 for (size_t i
= 0; i
< arraysize(ref_cases
); i
++) {
1122 if (ref_cases
[i
].input8
) {
1123 int len
= static_cast<int>(strlen(ref_cases
[i
].input8
));
1124 url_parse::Component
in_comp(0, len
);
1125 url_parse::Component out_comp
;
1127 std::string out_str
;
1128 url_canon::StdStringCanonOutput
output(&out_str
);
1129 url_canon::CanonicalizeRef(ref_cases
[i
].input8
, in_comp
,
1130 &output
, &out_comp
);
1133 EXPECT_EQ(ref_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1134 EXPECT_EQ(ref_cases
[i
].expected_component
.len
, out_comp
.len
);
1135 EXPECT_EQ(ref_cases
[i
].expected
, out_str
);
1139 if (ref_cases
[i
].input16
) {
1140 string16
input16(WStringToUTF16(ref_cases
[i
].input16
));
1141 int len
= static_cast<int>(input16
.length());
1142 url_parse::Component
in_comp(0, len
);
1143 url_parse::Component out_comp
;
1145 std::string out_str
;
1146 url_canon::StdStringCanonOutput
output(&out_str
);
1147 url_canon::CanonicalizeRef(input16
.c_str(), in_comp
, &output
, &out_comp
);
1150 EXPECT_EQ(ref_cases
[i
].expected_component
.begin
, out_comp
.begin
);
1151 EXPECT_EQ(ref_cases
[i
].expected_component
.len
, out_comp
.len
);
1152 EXPECT_EQ(ref_cases
[i
].expected
, out_str
);
1156 // Try one with an embedded NULL. It should be stripped.
1157 const char null_input
[5] = "ab\x00z";
1158 url_parse::Component
null_input_component(0, 4);
1159 url_parse::Component out_comp
;
1161 std::string out_str
;
1162 url_canon::StdStringCanonOutput
output(&out_str
);
1163 url_canon::CanonicalizeRef(null_input
, null_input_component
,
1164 &output
, &out_comp
);
1167 EXPECT_EQ(1, out_comp
.begin
);
1168 EXPECT_EQ(3, out_comp
.len
);
1169 EXPECT_EQ("#abz", out_str
);
1172 TEST(URLCanonTest
, CanonicalizeStandardURL
) {
1173 // The individual component canonicalize tests should have caught the cases
1174 // for each of those components. Here, we just need to test that the various
1175 // parts are included or excluded properly, and have the correct separators.
1178 const char* expected
;
1179 bool expected_success
;
1181 {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1182 {"http://[www.google.com]/", "http://[www.google.com]/", false},
1183 {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1184 {"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo", true},
1185 {"www.google.com", ":www.google.com/", true},
1186 {"http://192.0x00A80001", "http://192.168.0.1/", true},
1187 {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1188 {"http://user:pass@/", "http://user:pass@/", false},
1189 {"http://%25DOMAIN:foobar@foodomain.com/", "http://%25DOMAIN:foobar@foodomain.com/", true},
1191 // Backslashes should get converted to forward slashes.
1192 {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1194 // Busted refs shouldn't make the whole thing fail.
1195 {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1197 // Basic port tests.
1198 {"http://foo:80/", "http://foo/", true},
1199 {"http://foo:81/", "http://foo:81/", true},
1200 {"httpa://foo:80/", "httpa://foo:80/", true},
1201 {"http://foo:-80/", "http://foo:-80/", false},
1203 {"https://foo:443/", "https://foo/", true},
1204 {"https://foo:80/", "https://foo:80/", true},
1205 {"ftp://foo:21/", "ftp://foo/", true},
1206 {"ftp://foo:80/", "ftp://foo:80/", true},
1207 {"gopher://foo:70/", "gopher://foo/", true},
1208 {"gopher://foo:443/", "gopher://foo:443/", true},
1211 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1212 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1213 url_parse::Parsed parsed
;
1214 url_parse::ParseStandardURL(cases
[i
].input
, url_len
, &parsed
);
1216 url_parse::Parsed out_parsed
;
1217 std::string out_str
;
1218 url_canon::StdStringCanonOutput
output(&out_str
);
1219 bool success
= url_canon::CanonicalizeStandardURL(
1220 cases
[i
].input
, url_len
, parsed
, NULL
, &output
, &out_parsed
);
1223 EXPECT_EQ(cases
[i
].expected_success
, success
);
1224 EXPECT_EQ(cases
[i
].expected
, out_str
);
1228 // The codepath here is the same as for regular canonicalization, so we just
1229 // need to test that things are replaced or not correctly.
1230 TEST(URLCanonTest
, ReplaceStandardURL
) {
1231 ReplaceCase replace_cases
[] = {
1232 // Common case of truncating the path.
1233 {"http://www.google.com/foo?bar=baz#ref", NULL
, NULL
, NULL
, NULL
, NULL
, "/", kDeleteComp
, kDeleteComp
, "http://www.google.com/"},
1234 // Replace everything
1235 {"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"},
1237 {"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"},
1240 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1241 const ReplaceCase
& cur
= replace_cases
[i
];
1242 int base_len
= static_cast<int>(strlen(cur
.base
));
1243 url_parse::Parsed parsed
;
1244 url_parse::ParseStandardURL(cur
.base
, base_len
, &parsed
);
1246 url_canon::Replacements
<char> r
;
1247 typedef url_canon::Replacements
<char> R
; // Clean up syntax.
1249 // Note that for the scheme we pass in a different clear function since
1250 // there is no function to clear the scheme.
1251 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1252 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1253 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1254 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1255 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1256 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1257 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1258 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1260 std::string out_str
;
1261 url_canon::StdStringCanonOutput
output(&out_str
);
1262 url_parse::Parsed out_parsed
;
1263 url_canon::ReplaceStandardURL(replace_cases
[i
].base
, parsed
,
1264 r
, NULL
, &output
, &out_parsed
);
1267 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1270 // The path pointer should be ignored if the address is invalid.
1272 const char src
[] = "http://www.google.com/here_is_the_path";
1273 int src_len
= static_cast<int>(strlen(src
));
1275 url_parse::Parsed parsed
;
1276 url_parse::ParseStandardURL(src
, src_len
, &parsed
);
1278 // Replace the path to 0 length string. By using 1 as the string address,
1279 // the test should get an access violation if it tries to dereference it.
1280 url_canon::Replacements
<char> r
;
1281 r
.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component(0, 0));
1282 std::string out_str1
;
1283 url_canon::StdStringCanonOutput
output1(&out_str1
);
1284 url_parse::Parsed new_parsed
;
1285 url_canon::ReplaceStandardURL(src
, parsed
, r
, NULL
, &output1
, &new_parsed
);
1287 EXPECT_STREQ("http://www.google.com/", out_str1
.c_str());
1289 // Same with an "invalid" path.
1290 r
.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component());
1291 std::string out_str2
;
1292 url_canon::StdStringCanonOutput
output2(&out_str2
);
1293 url_canon::ReplaceStandardURL(src
, parsed
, r
, NULL
, &output2
, &new_parsed
);
1295 EXPECT_STREQ("http://www.google.com/", out_str2
.c_str());
1299 TEST(URLCanonTest
, ReplaceFileURL
) {
1300 ReplaceCase replace_cases
[] = {
1301 // Replace everything
1302 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, "filer", NULL
, "/foo", "b", "c", "file://filer/foo?b#c"},
1304 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "file:///C:/gaba?query#ref"},
1305 // Clear non-path components (common)
1306 {"file:///C:/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, kDeleteComp
, "file:///C:/gaba"},
1307 // Replace path with something that doesn't begin with a slash and make
1308 // sure it get added properly.
1309 {"file:///C:/gaba", NULL
, NULL
, NULL
, NULL
, NULL
, "interesting/", NULL
, NULL
, "file:///interesting/"},
1310 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, "filer", NULL
, "/foo", "b", "c", "file://filer/foo?b#c"},
1311 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "file:///home/gaba?query#ref"},
1312 {"file:///home/gaba?query#ref", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, kDeleteComp
, "file:///home/gaba"},
1313 {"file:///home/gaba", NULL
, NULL
, NULL
, NULL
, NULL
, "interesting/", NULL
, NULL
, "file:///interesting/"},
1316 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1317 const ReplaceCase
& cur
= replace_cases
[i
];
1318 int base_len
= static_cast<int>(strlen(cur
.base
));
1319 url_parse::Parsed parsed
;
1320 url_parse::ParseFileURL(cur
.base
, base_len
, &parsed
);
1322 url_canon::Replacements
<char> r
;
1323 typedef url_canon::Replacements
<char> R
; // Clean up syntax.
1324 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1325 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1326 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1327 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1328 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1329 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1330 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1331 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1333 std::string out_str
;
1334 url_canon::StdStringCanonOutput
output(&out_str
);
1335 url_parse::Parsed out_parsed
;
1336 url_canon::ReplaceFileURL(cur
.base
, parsed
,
1337 r
, NULL
, &output
, &out_parsed
);
1340 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1344 TEST(URLCanonTest
, ReplacePathURL
) {
1345 ReplaceCase replace_cases
[] = {
1346 // Replace everything
1347 {"data:foo", "javascript", NULL
, NULL
, NULL
, NULL
, "alert('foo?');", NULL
, NULL
, "javascript:alert('foo?');"},
1349 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "data:foo"},
1350 // Replace one or the other
1351 {"data:foo", "javascript", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "javascript:foo"},
1352 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, "bar", NULL
, NULL
, "data:bar"},
1353 {"data:foo", NULL
, NULL
, NULL
, NULL
, NULL
, kDeleteComp
, NULL
, NULL
, "data:"},
1356 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1357 const ReplaceCase
& cur
= replace_cases
[i
];
1358 int base_len
= static_cast<int>(strlen(cur
.base
));
1359 url_parse::Parsed parsed
;
1360 url_parse::ParsePathURL(cur
.base
, base_len
, &parsed
);
1362 url_canon::Replacements
<char> r
;
1363 typedef url_canon::Replacements
<char> R
; // Clean up syntax.
1364 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1365 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1366 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1367 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1368 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1369 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1370 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1371 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1373 std::string out_str
;
1374 url_canon::StdStringCanonOutput
output(&out_str
);
1375 url_parse::Parsed out_parsed
;
1376 url_canon::ReplacePathURL(cur
.base
, parsed
,
1377 r
, &output
, &out_parsed
);
1380 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1384 TEST(URLCanonTest
, ReplaceMailtoURL
) {
1385 ReplaceCase replace_cases
[] = {
1386 // Replace everything
1387 {"mailto:jon@foo.com?body=sup", "mailto", NULL
, NULL
, NULL
, NULL
, "addr1", "to=tony", NULL
, "mailto:addr1?to=tony"},
1389 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "mailto:jon@foo.com?body=sup"},
1391 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "jason", NULL
, NULL
, "mailto:jason?body=sup"},
1392 // Replace the query
1393 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "custom=1", NULL
, "mailto:jon@foo.com?custom=1"},
1394 // Replace the path and query
1395 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "jason", "custom=1", NULL
, "mailto:jason?custom=1"},
1396 // Set the query to empty (should leave trailing question mark)
1397 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "", NULL
, "mailto:jon@foo.com?"},
1399 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "|", NULL
, "mailto:jon@foo.com"},
1401 {"mailto:jon@foo.com?body=sup", NULL
, NULL
, NULL
, NULL
, NULL
, "|", NULL
, NULL
, "mailto:?body=sup"},
1402 // Clear the path + query
1403 {"mailto:", NULL
, NULL
, NULL
, NULL
, NULL
, "|", "|", NULL
, "mailto:"},
1404 // Setting the ref should have no effect
1405 {"mailto:addr1", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "BLAH", "mailto:addr1"},
1408 for (size_t i
= 0; i
< arraysize(replace_cases
); i
++) {
1409 const ReplaceCase
& cur
= replace_cases
[i
];
1410 int base_len
= static_cast<int>(strlen(cur
.base
));
1411 url_parse::Parsed parsed
;
1412 url_parse::ParseMailtoURL(cur
.base
, base_len
, &parsed
);
1414 url_canon::Replacements
<char> r
;
1415 typedef url_canon::Replacements
<char> R
;
1416 SetupReplComp(&R::SetScheme
, &R::ClearRef
, &r
, cur
.scheme
);
1417 SetupReplComp(&R::SetUsername
, &R::ClearUsername
, &r
, cur
.username
);
1418 SetupReplComp(&R::SetPassword
, &R::ClearPassword
, &r
, cur
.password
);
1419 SetupReplComp(&R::SetHost
, &R::ClearHost
, &r
, cur
.host
);
1420 SetupReplComp(&R::SetPort
, &R::ClearPort
, &r
, cur
.port
);
1421 SetupReplComp(&R::SetPath
, &R::ClearPath
, &r
, cur
.path
);
1422 SetupReplComp(&R::SetQuery
, &R::ClearQuery
, &r
, cur
.query
);
1423 SetupReplComp(&R::SetRef
, &R::ClearRef
, &r
, cur
.ref
);
1425 std::string out_str
;
1426 url_canon::StdStringCanonOutput
output(&out_str
);
1427 url_parse::Parsed out_parsed
;
1428 url_canon::ReplaceMailtoURL(cur
.base
, parsed
,
1429 r
, &output
, &out_parsed
);
1432 EXPECT_EQ(replace_cases
[i
].expected
, out_str
);
1436 TEST(URLCanonTest
, CanonicalizeFileURL
) {
1439 const char* expected
;
1440 bool expected_success
;
1441 url_parse::Component expected_host
;
1442 url_parse::Component expected_path
;
1445 // Windows-style paths
1446 {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1447 {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1448 {"file:", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1449 {"file:UNChost/path", "file://unchost/path", true, url_parse::Component(7, 7), url_parse::Component(14, 5)},
1450 // CanonicalizeFileURL supports absolute Windows style paths for IE
1451 // compatability. Note that the caller must decide that this is a file
1452 // URL itself so it can call the file canonicalizer. This is usually
1453 // done automatically as part of relative URL resolving.
1454 {"c:\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1455 {"C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1456 {"/C|\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1457 {"//C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1458 {"//server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1459 {"\\\\server\\file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1460 {"/\\server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1461 // We should preserve the number of slashes after the colon for IE
1462 // compatability, except when there is none, in which case we should
1464 {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1465 {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1466 // Three slashes should be non-UNC, even if there is no drive spec (IE
1467 // does this, which makes the resulting request invalid).
1468 {"file:///foo/bar.txt", "file:///foo/bar.txt", true, url_parse::Component(), url_parse::Component(7, 12)},
1469 // TODO(brettw) we should probably fail for invalid host names, which
1470 // would change the expected result on this test.
1471 {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7%3A////foo/bar.html", false, url_parse::Component(7, 4), url_parse::Component(11, 16)},
1472 {"file:filer/home\\me", "file://filer/home/me", true, url_parse::Component(7, 5), url_parse::Component(12, 8)},
1473 // Make sure relative paths can't go above the "C:"
1474 {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, url_parse::Component(), url_parse::Component(7, 12)},
1475 // Busted refs shouldn't make the whole thing fail.
1476 {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, url_parse::Component(), url_parse::Component(7, 8)},
1479 {"file:///home/me", "file:///home/me", true, url_parse::Component(), url_parse::Component(7, 8)},
1480 // Windowsy ones should get still treated as Unix-style.
1481 {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1482 {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1483 // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
1484 {"//", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1485 {"///", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1486 {"///test", "file:///test", true, url_parse::Component(), url_parse::Component(7, 5)},
1487 {"file://test", "file://test/", true, url_parse::Component(7, 4), url_parse::Component(11, 1)},
1488 {"file://localhost", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
1489 {"file://localhost/", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
1490 {"file://localhost/test", "file://localhost/test", true, url_parse::Component(7, 9), url_parse::Component(16, 5)},
1494 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1495 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1496 url_parse::Parsed parsed
;
1497 url_parse::ParseFileURL(cases
[i
].input
, url_len
, &parsed
);
1499 url_parse::Parsed out_parsed
;
1500 std::string out_str
;
1501 url_canon::StdStringCanonOutput
output(&out_str
);
1502 bool success
= url_canon::CanonicalizeFileURL(cases
[i
].input
, url_len
,
1503 parsed
, NULL
, &output
,
1507 EXPECT_EQ(cases
[i
].expected_success
, success
);
1508 EXPECT_EQ(cases
[i
].expected
, out_str
);
1510 // Make sure the spec was properly identified, the file canonicalizer has
1511 // different code for writing the spec.
1512 EXPECT_EQ(0, out_parsed
.scheme
.begin
);
1513 EXPECT_EQ(4, out_parsed
.scheme
.len
);
1515 EXPECT_EQ(cases
[i
].expected_host
.begin
, out_parsed
.host
.begin
);
1516 EXPECT_EQ(cases
[i
].expected_host
.len
, out_parsed
.host
.len
);
1518 EXPECT_EQ(cases
[i
].expected_path
.begin
, out_parsed
.path
.begin
);
1519 EXPECT_EQ(cases
[i
].expected_path
.len
, out_parsed
.path
.len
);
1523 TEST(URLCanonTest
, CanonicalizePathURL
) {
1524 // Path URLs should get canonicalized schemes but nothing else.
1527 const char* expected
;
1529 {"javascript:", "javascript:"},
1530 {"JavaScript:Foo", "javascript:Foo"},
1531 {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1534 for (size_t i
= 0; i
< ARRAYSIZE(path_cases
); i
++) {
1535 int url_len
= static_cast<int>(strlen(path_cases
[i
].input
));
1536 url_parse::Parsed parsed
;
1537 url_parse::ParsePathURL(path_cases
[i
].input
, url_len
, &parsed
);
1539 url_parse::Parsed out_parsed
;
1540 std::string out_str
;
1541 url_canon::StdStringCanonOutput
output(&out_str
);
1542 bool success
= url_canon::CanonicalizePathURL(path_cases
[i
].input
, url_len
,
1547 EXPECT_TRUE(success
);
1548 EXPECT_EQ(path_cases
[i
].expected
, out_str
);
1550 EXPECT_EQ(0, out_parsed
.host
.begin
);
1551 EXPECT_EQ(-1, out_parsed
.host
.len
);
1553 // When we end with a colon at the end, there should be no path.
1554 if (path_cases
[i
].input
[url_len
- 1] == ':') {
1555 EXPECT_EQ(0, out_parsed
.path
.begin
);
1556 EXPECT_EQ(-1, out_parsed
.path
.len
);
1561 TEST(URLCanonTest
, CanonicalizeMailtoURL
) {
1564 const char* expected
;
1565 bool expected_success
;
1566 url_parse::Component expected_path
;
1567 url_parse::Component expected_query
;
1569 {"mailto:addr1", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
1570 {"mailto:addr1@foo.com", "mailto:addr1@foo.com", true, url_parse::Component(7, 13), url_parse::Component()},
1571 // Trailing whitespace is stripped.
1572 {"MaIlTo:addr1 \t ", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
1573 {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, url_parse::Component(7, 5), url_parse::Component(13,6)},
1574 {"mailto:addr1,addr2", "mailto:addr1,addr2", true, url_parse::Component(7, 11), url_parse::Component()},
1575 {"mailto:addr1, addr2", "mailto:addr1, addr2", true, url_parse::Component(7, 12), url_parse::Component()},
1576 {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, url_parse::Component(7, 13), url_parse::Component()},
1577 {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, url_parse::Component(7, 12), url_parse::Component()},
1578 // Null character should be escaped to %00
1579 {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, url_parse::Component(7, 13), url_parse::Component(21, 3)},
1580 // Invalid -- UTF-8 encoded surrogate value.
1581 {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, url_parse::Component(7, 9), url_parse::Component()},
1582 {"mailto:addr1?", "mailto:addr1?", true, url_parse::Component(7, 5), url_parse::Component(13, 0)},
1585 // Define outside of loop to catch bugs where components aren't reset
1586 url_parse::Parsed parsed
;
1587 url_parse::Parsed out_parsed
;
1589 for (size_t i
= 0; i
< ARRAYSIZE(cases
); i
++) {
1590 int url_len
= static_cast<int>(strlen(cases
[i
].input
));
1592 // The 9th test case purposely has a '\0' in it -- don't count it
1593 // as the string terminator.
1596 url_parse::ParseMailtoURL(cases
[i
].input
, url_len
, &parsed
);
1598 std::string out_str
;
1599 url_canon::StdStringCanonOutput
output(&out_str
);
1600 bool success
= url_canon::CanonicalizeMailtoURL(cases
[i
].input
, url_len
,
1605 EXPECT_EQ(cases
[i
].expected_success
, success
);
1606 EXPECT_EQ(cases
[i
].expected
, out_str
);
1608 // Make sure the spec was properly identified
1609 EXPECT_EQ(0, out_parsed
.scheme
.begin
);
1610 EXPECT_EQ(6, out_parsed
.scheme
.len
);
1612 EXPECT_EQ(cases
[i
].expected_path
.begin
, out_parsed
.path
.begin
);
1613 EXPECT_EQ(cases
[i
].expected_path
.len
, out_parsed
.path
.len
);
1615 EXPECT_EQ(cases
[i
].expected_query
.begin
, out_parsed
.query
.begin
);
1616 EXPECT_EQ(cases
[i
].expected_query
.len
, out_parsed
.query
.len
);
1622 TEST(URLCanonTest
, _itoa_s
) {
1623 // We fill the buffer with 0xff to ensure that it's getting properly
1624 // null-terminated. We also allocate one byte more than what we tell
1625 // _itoa_s about, and ensure that the extra byte is untouched.
1627 memset(buf
, 0xff, sizeof(buf
));
1628 EXPECT_EQ(0, url_canon::_itoa_s(12, buf
, sizeof(buf
) - 1, 10));
1629 EXPECT_STREQ("12", buf
);
1630 EXPECT_EQ('\xFF', buf
[3]);
1632 // Test the edge cases - exactly the buffer size and one over
1633 memset(buf
, 0xff, sizeof(buf
));
1634 EXPECT_EQ(0, url_canon::_itoa_s(1234, buf
, sizeof(buf
) - 1, 10));
1635 EXPECT_STREQ("1234", buf
);
1636 EXPECT_EQ('\xFF', buf
[5]);
1638 memset(buf
, 0xff, sizeof(buf
));
1639 EXPECT_EQ(EINVAL
, url_canon::_itoa_s(12345, buf
, sizeof(buf
) - 1, 10));
1640 EXPECT_EQ('\xFF', buf
[5]); // should never write to this location
1642 // Test the template overload (note that this will see the full buffer)
1643 memset(buf
, 0xff, sizeof(buf
));
1644 EXPECT_EQ(0, url_canon::_itoa_s(12, buf
, 10));
1645 EXPECT_STREQ("12", buf
);
1646 EXPECT_EQ('\xFF', buf
[3]);
1648 memset(buf
, 0xff, sizeof(buf
));
1649 EXPECT_EQ(0, url_canon::_itoa_s(12345, buf
, 10));
1650 EXPECT_STREQ("12345", buf
);
1652 EXPECT_EQ(EINVAL
, url_canon::_itoa_s(123456, buf
, 10));
1654 // Test that radix 16 is supported.
1655 memset(buf
, 0xff, sizeof(buf
));
1656 EXPECT_EQ(0, url_canon::_itoa_s(1234, buf
, sizeof(buf
) - 1, 16));
1657 EXPECT_STREQ("4d2", buf
);
1658 EXPECT_EQ('\xFF', buf
[5]);
1661 TEST(URLCanonTest
, _itow_s
) {
1662 // We fill the buffer with 0xff to ensure that it's getting properly
1663 // null-terminated. We also allocate one byte more than what we tell
1664 // _itoa_s about, and ensure that the extra byte is untouched.
1666 const char fill_mem
= 0xff;
1667 const char16 fill_char
= 0xffff;
1668 memset(buf
, fill_mem
, sizeof(buf
));
1669 EXPECT_EQ(0, url_canon::_itow_s(12, buf
, sizeof(buf
) / 2 - 1, 10));
1670 EXPECT_EQ(WStringToUTF16(L
"12"), string16(buf
));
1671 EXPECT_EQ(fill_char
, buf
[3]);
1673 // Test the edge cases - exactly the buffer size and one over
1674 EXPECT_EQ(0, url_canon::_itow_s(1234, buf
, sizeof(buf
) / 2 - 1, 10));
1675 EXPECT_EQ(WStringToUTF16(L
"1234"), string16(buf
));
1676 EXPECT_EQ(fill_char
, buf
[5]);
1678 memset(buf
, fill_mem
, sizeof(buf
));
1679 EXPECT_EQ(EINVAL
, url_canon::_itow_s(12345, buf
, sizeof(buf
) / 2 - 1, 10));
1680 EXPECT_EQ(fill_char
, buf
[5]); // should never write to this location
1682 // Test the template overload (note that this will see the full buffer)
1683 memset(buf
, fill_mem
, sizeof(buf
));
1684 EXPECT_EQ(0, url_canon::_itow_s(12, buf
, 10));
1685 EXPECT_EQ(WStringToUTF16(L
"12"), string16(buf
));
1686 EXPECT_EQ(fill_char
, buf
[3]);
1688 memset(buf
, fill_mem
, sizeof(buf
));
1689 EXPECT_EQ(0, url_canon::_itow_s(12345, buf
, 10));
1690 EXPECT_EQ(WStringToUTF16(L
"12345"), string16(buf
));
1692 EXPECT_EQ(EINVAL
, url_canon::_itow_s(123456, buf
, 10));
1697 // Returns true if the given two structures are the same.
1698 static bool ParsedIsEqual(const url_parse::Parsed
& a
,
1699 const url_parse::Parsed
& b
) {
1700 return a
.scheme
.begin
== b
.scheme
.begin
&& a
.scheme
.len
== b
.scheme
.len
&&
1701 a
.username
.begin
== b
.username
.begin
&& a
.username
.len
== b
.username
.len
&&
1702 a
.password
.begin
== b
.password
.begin
&& a
.password
.len
== b
.password
.len
&&
1703 a
.host
.begin
== b
.host
.begin
&& a
.host
.len
== b
.host
.len
&&
1704 a
.port
.begin
== b
.port
.begin
&& a
.port
.len
== b
.port
.len
&&
1705 a
.path
.begin
== b
.path
.begin
&& a
.path
.len
== b
.path
.len
&&
1706 a
.query
.begin
== b
.query
.begin
&& a
.query
.len
== b
.query
.len
&&
1707 a
.ref
.begin
== b
.ref
.begin
&& a
.ref
.len
== b
.ref
.len
;
1710 TEST(URLCanonTest
, ResolveRelativeURL
) {
1711 struct RelativeCase
{
1712 const char* base
; // Input base URL: MUST BE CANONICAL
1713 bool is_base_hier
; // Is the base URL hierarchical
1714 bool is_base_file
; // Tells us if the base is a file URL.
1715 const char* test
; // Input URL to test against.
1716 bool succeed_relative
; // Whether we expect IsRelativeURL to succeed
1717 bool is_rel
; // Whether we expect |test| to be relative or not.
1718 bool succeed_resolve
; // Whether we expect ResolveRelativeURL to succeed.
1719 const char* resolved
; // What we expect in the result when resolving.
1721 // Basic absolute input.
1722 {"http://host/a", true, false, "http://another/", true, false, false, NULL
},
1723 {"http://host/a", true, false, "http:////another/", true, false, false, NULL
},
1724 // Empty relative URLs shouldn't change the input.
1725 {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
1726 // Spaces at the ends of the relative path should be ignored.
1727 {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
1728 {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
1729 {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
1730 // Matching schemes without two slashes are treated as relative.
1731 {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
1732 {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
1733 {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
1734 {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
1735 // Nonmatching schemes are absolute.
1736 {"http://host/a", true, false, "https:host2", true, false, false, NULL
},
1737 {"http://host/a", true, false, "htto:/host2", true, false, false, NULL
},
1738 // Absolute path input
1739 {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
1740 {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
1741 {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
1742 {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
1743 {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
1744 {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
1745 // Relative path input
1746 {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
1747 {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
1748 {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
1749 {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
1750 {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
1751 {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
1752 {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
1753 {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
1754 {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
1756 {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
1757 {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
1758 {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
1760 {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
1761 {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
1762 {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
1763 // Non-hierarchical base: no relative handling. Relative input should
1764 // error, and if a scheme is present, it should be treated as absolute.
1765 {"data:foobar", false, false, "baz.html", false, false, false, NULL
},
1766 {"data:foobar", false, false, "data:baz", true, false, false, NULL
},
1767 {"data:foobar", false, false, "data:/base", true, false, false, NULL
},
1768 // Non-hierarchical base: absolute input should succeed.
1769 {"data:foobar", false, false, "http://host/", true, false, false, NULL
},
1770 {"data:foobar", false, false, "http:host", true, false, false, NULL
},
1771 // Invalid schemes should be treated as relative.
1772 {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
1773 {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
1774 {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
1775 {"data:asdf", false, false, ":foo", false, false, false, NULL
},
1776 // We should treat semicolons like any other character in URL resolving
1777 {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
1778 {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
1779 {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
1780 // Relative URLs can also be written as "//foo/bar" which is relative to
1781 // the scheme. In this case, it would take the old scheme, so for http
1782 // the example would resolve to "http://foo/bar".
1783 {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
1784 {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
1785 {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
1786 {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
1787 {"http://host/a", true, false, "//", true, true, false, "http:"},
1788 // IE will also allow one or the other to be a backslash to get the same
1790 {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
1791 {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
1793 // Resolving against Windows file base URLs.
1794 {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL
},
1795 {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
1796 {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
1797 {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
1798 // But two backslashes on Windows should be UNC so should be treated
1800 {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL
},
1801 // IE doesn't support drive specs starting with two slashes. It fails
1802 // immediately and doesn't even try to load. We fix it up to either
1803 // an absolute path or UNC depending on what it looks like.
1804 {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
1805 {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
1806 // Windows drive specs should be allowed and treated as absolute.
1807 {"file:///C:/foo", true, true, "c:", true, false, false, NULL
},
1808 {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL
},
1809 {"http://host/a", true, false, "c:\\foo", true, false, false, NULL
},
1810 // Relative paths with drive letters should be allowed when the base is
1812 {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
1813 // Treat absolute paths as being off of the drive.
1814 {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
1815 {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
1816 {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
1817 // On Windows, two slashes without a drive letter when the base is a file
1818 // means that the path is UNC.
1819 {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
1820 {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
1822 // On Unix we fall back to relative behavior since there's nothing else
1823 // reasonable to do.
1824 {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
1826 // Even on Windows, we don't allow relative drive specs when the base
1828 {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
1829 {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
1832 for (size_t i
= 0; i
< ARRAYSIZE(rel_cases
); i
++) {
1833 const RelativeCase
& cur_case
= rel_cases
[i
];
1835 url_parse::Parsed parsed
;
1836 int base_len
= static_cast<int>(strlen(cur_case
.base
));
1837 if (cur_case
.is_base_file
)
1838 url_parse::ParseFileURL(cur_case
.base
, base_len
, &parsed
);
1839 else if (cur_case
.is_base_hier
)
1840 url_parse::ParseStandardURL(cur_case
.base
, base_len
, &parsed
);
1842 url_parse::ParsePathURL(cur_case
.base
, base_len
, &parsed
);
1844 // First see if it is relative.
1845 int test_len
= static_cast<int>(strlen(cur_case
.test
));
1847 url_parse::Component relative_component
;
1848 bool succeed_is_rel
= url_canon::IsRelativeURL(
1849 cur_case
.base
, parsed
, cur_case
.test
, test_len
, cur_case
.is_base_hier
,
1850 &is_relative
, &relative_component
);
1852 EXPECT_EQ(cur_case
.succeed_relative
, succeed_is_rel
) <<
1853 "succeed is rel failure on " << cur_case
.test
;
1854 EXPECT_EQ(cur_case
.is_rel
, is_relative
) <<
1855 "is rel failure on " << cur_case
.test
;
1857 if (succeed_is_rel
&& is_relative
&& cur_case
.is_rel
) {
1858 std::string resolved
;
1859 url_canon::StdStringCanonOutput
output(&resolved
);
1860 url_parse::Parsed resolved_parsed
;
1862 bool succeed_resolve
= url_canon::ResolveRelativeURL(
1863 cur_case
.base
, parsed
, cur_case
.is_base_file
,
1864 cur_case
.test
, relative_component
, NULL
, &output
, &resolved_parsed
);
1867 EXPECT_EQ(cur_case
.succeed_resolve
, succeed_resolve
);
1868 EXPECT_EQ(cur_case
.resolved
, resolved
) << " on " << cur_case
.test
;
1870 // Verify that the output parsed structure is the same as parsing a
1872 url_parse::Parsed ref_parsed
;
1873 int resolved_len
= static_cast<int>(resolved
.size());
1874 if (cur_case
.is_base_file
)
1875 url_parse::ParseFileURL(resolved
.c_str(), resolved_len
, &ref_parsed
);
1876 else if (cur_case
.is_base_hier
)
1877 url_parse::ParseStandardURL(resolved
.c_str(), resolved_len
, &ref_parsed
);
1879 url_parse::ParsePathURL(resolved
.c_str(), resolved_len
, &ref_parsed
);
1880 EXPECT_TRUE(ParsedIsEqual(ref_parsed
, resolved_parsed
));
1885 // It used to be when we did a replacement with a long buffer of UTF-16
1886 // characters, we would get invalid data in the URL. This is because the buffer
1887 // it used to hold the UTF-8 data was resized, while some pointers were still
1888 // kept to the old buffer that was removed.
1889 TEST(URLCanonTest
, ReplacementOverflow
) {
1890 const char src
[] = "file:///C:/foo/bar";
1891 int src_len
= static_cast<int>(strlen(src
));
1892 url_parse::Parsed parsed
;
1893 url_parse::ParseFileURL(src
, src_len
, &parsed
);
1895 // Override two components, the path with something short, and the query with
1896 // sonething long enough to trigger the bug.
1897 url_canon::Replacements
<char16
> repl
;
1899 for (int i
= 0; i
< 4800; i
++)
1900 new_query
.push_back('a');
1902 string16
new_path(WStringToUTF16(L
"/foo"));
1903 repl
.SetPath(new_path
.c_str(), url_parse::Component(0, 4));
1904 repl
.SetQuery(new_query
.c_str(),
1905 url_parse::Component(0, static_cast<int>(new_query
.length())));
1907 // Call ReplaceComponents on the string. It doesn't matter if we call it for
1908 // standard URLs, file URLs, etc, since they will go to the same replacement
1909 // function that was buggy.
1910 url_parse::Parsed repl_parsed
;
1911 std::string repl_str
;
1912 url_canon::StdStringCanonOutput
repl_output(&repl_str
);
1913 url_canon::ReplaceFileURL(src
, parsed
, repl
, NULL
, &repl_output
, &repl_parsed
);
1914 repl_output
.Complete();
1916 // Generate the expected string and check.
1917 std::string
expected("file:///foo?");
1918 for (size_t i
= 0; i
< new_query
.length(); i
++)
1919 expected
.push_back('a');
1920 EXPECT_TRUE(expected
== repl_str
);