• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
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.
29 
30 #include <errno.h>
31 #include <unicode/ucnv.h>
32 
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"
40 
41 // Some implementations of base/basictypes.h may define ARRAYSIZE.
42 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
43 // which is in our version of basictypes.h.
44 #ifndef ARRAYSIZE
45 #define ARRAYSIZE ARRAYSIZE_UNSAFE
46 #endif
47 
48 using url_test_utils::WStringToUTF16;
49 using url_test_utils::ConvertUTF8ToUTF16;
50 using url_test_utils::ConvertUTF16ToUTF8;
51 using url_canon::CanonHostInfo;
52 
53 namespace {
54 
55 struct ComponentCase {
56   const char* input;
57   const char* expected;
58   url_parse::Component expected_component;
59   bool expected_success;
60 };
61 
62 // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests
63 // treat each input as optional, and will only try processing if non-NULL.
64 // The output is always 8-bit.
65 struct DualComponentCase {
66   const char* input8;
67   const wchar_t* input16;
68   const char* expected;
69   url_parse::Component expected_component;
70   bool expected_success;
71 };
72 
73 // Test cases for CanonicalizeIPAddress().  The inputs are identical to
74 // DualComponentCase, but the output has extra CanonHostInfo fields.
75 struct IPAddressCase {
76   const char* input8;
77   const wchar_t* input16;
78   const char* expected;
79   url_parse::Component expected_component;
80 
81   // CanonHostInfo fields, for verbose output.
82   CanonHostInfo::Family expected_family;
83   int expected_num_ipv4_components;
84 };
85 
86 struct ReplaceCase {
87   const char* base;
88   const char* scheme;
89   const char* username;
90   const char* password;
91   const char* host;
92   const char* port;
93   const char* path;
94   const char* query;
95   const char* ref;
96   const char* expected;
97 };
98 
99 // Wrapper around a UConverter object that managers creation and destruction.
100 class UConvScoper {
101  public:
UConvScoper(const char * charset_name)102   explicit UConvScoper(const char* charset_name) {
103     UErrorCode err = U_ZERO_ERROR;
104     converter_ = ucnv_open(charset_name, &err);
105   }
106 
~UConvScoper()107   ~UConvScoper() {
108     if (converter_)
109       ucnv_close(converter_);
110   }
111 
112   // Returns the converter object, may be NULL.
converter() const113   UConverter* converter() const { return converter_; }
114 
115  private:
116   UConverter* converter_;
117 };
118 
119 // Magic string used in the replacements code that tells SetupReplComp to
120 // call the clear function.
121 const char kDeleteComp[] = "|";
122 
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).
127 //
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>
SetupReplComp(void (url_canon::Replacements<CHAR>::* set)(const CHAR *,const url_parse::Component &),void (url_canon::Replacements<CHAR>::* clear)(),url_canon::Replacements<CHAR> * rep,const CHAR * str)132 void SetupReplComp(
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,
137     const CHAR* str) {
138   if (str && str[0] == kDeleteComp[0]) {
139     (rep->*clear)();
140   } else if (str) {
141     (rep->*set)(str, url_parse::Component(0, static_cast<int>(strlen(str))));
142   }
143 }
144 
145 }  // namespace
146 
TEST(URLCanonTest,UTF)147 TEST(URLCanonTest, UTF) {
148   // Low-level test that we handle reading, canonicalization, and writing
149   // UTF-8/UTF-16 strings properly.
150   struct UTFCase {
151     const char* input8;
152     const wchar_t* input16;
153     bool expected_success;
154     const char* output;
155   } utf_cases[] = {
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.
171       // ED A0 80 = U+D800
172     {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
173   };
174 
175   std::string out_str;
176   for (size_t i = 0; i < ARRAYSIZE(utf_cases); i++) {
177     if (utf_cases[i].input8) {
178       out_str.clear();
179       url_canon::StdStringCanonOutput output(&out_str);
180 
181       int input_len = static_cast<int>(strlen(utf_cases[i].input8));
182       bool success = true;
183       for (int ch = 0; ch < input_len; ch++) {
184         success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
185                                          &output);
186       }
187       output.Complete();
188       EXPECT_EQ(utf_cases[i].expected_success, success);
189       EXPECT_EQ(std::string(utf_cases[i].output), out_str);
190     }
191     if (utf_cases[i].input16) {
192       out_str.clear();
193       url_canon::StdStringCanonOutput output(&out_str);
194 
195       string16 input_str(WStringToUTF16(utf_cases[i].input16));
196       int input_len = static_cast<int>(input_str.length());
197       bool success = true;
198       for (int ch = 0; ch < input_len; ch++) {
199         success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
200                                          &output);
201       }
202       output.Complete();
203       EXPECT_EQ(utf_cases[i].expected_success, success);
204       EXPECT_EQ(std::string(utf_cases[i].output), out_str);
205     }
206 
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.
210 
211       // UTF-16 -> UTF-8
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));
215 
216       // UTF-8 -> UTF-16
217       EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
218     }
219   }
220 }
221 
TEST(URLCanonTest,ICUCharsetConverter)222 TEST(URLCanonTest, ICUCharsetConverter) {
223   struct ICUCase {
224     const wchar_t* input;
225     const char* encoding;
226     const char* expected;
227   } icu_cases[] = {
228       // UTF-8.
229     {L"Hello, world", "utf-8", "Hello, world"},
230     {L"\x4f60\x597d", "utf-8", "\xe4\xbd\xa0\xe5\xa5\xbd"},
231       // Non-BMP UTF-8.
232     {L"!\xd800\xdf00!", "utf-8", "!\xf0\x90\x8c\x80!"},
233       // Big5
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"},
237   };
238 
239   for (size_t i = 0; i < ARRAYSIZE(icu_cases); i++) {
240     UConvScoper conv(icu_cases[i].encoding);
241     ASSERT_TRUE(conv.converter() != NULL);
242     url_canon::ICUCharsetConverter converter(conv.converter());
243 
244     std::string str;
245     url_canon::StdStringCanonOutput output(&str);
246 
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);
250     output.Complete();
251 
252     EXPECT_STREQ(icu_cases[i].expected, str.c_str());
253   }
254 
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.
263     string16 input;
264     for (int ch = 0; ch < i; ch++)
265       input.push_back('a');
266 
267     url_canon::RawCanonOutput<static_size> output;
268     converter.ConvertFromUTF16(input.c_str(), static_cast<int>(input.length()),
269                                &output);
270     EXPECT_EQ(input.length(), static_cast<size_t>(output.length()));
271   }
272 }
273 
TEST(URLCanonTest,Scheme)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).
279   //
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},
292   };
293 
294   std::string out_str;
295 
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;
300 
301     out_str.clear();
302     url_canon::StdStringCanonOutput output1(&out_str);
303     bool success = url_canon::CanonicalizeScheme(scheme_cases[i].input,
304                                                  in_comp, &output1, &out_comp);
305     output1.Complete();
306 
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);
311 
312     // Now try the wide version
313     out_str.clear();
314     url_canon::StdStringCanonOutput output2(&out_str);
315 
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);
320     output2.Complete();
321 
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);
326   }
327 
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;
331   out_str.clear();
332   url_canon::StdStringCanonOutput output(&out_str);
333 
334   EXPECT_TRUE(url_canon::CanonicalizeScheme("", url_parse::Component(0, -1),
335                                             &output, &out_comp));
336   output.Complete();
337 
338   EXPECT_EQ(std::string(":"), out_str);
339   EXPECT_EQ(0, out_comp.begin);
340   EXPECT_EQ(0, out_comp.len);
341 }
342 
TEST(URLCanonTest,Host)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
355       // treated as a dot.
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
382       // escaped.
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
393       // spaces escaped.
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},
401       // Cyrillic letter followed buy ( should return punicode for ( escaped before punicode string was created. I.e.
402       // if ( is escaped after punicode is created we would get xn--%28-8tb (incorrect).
403     {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", url_parse::Component(0, 11), CanonHostInfo::NEUTRAL, -1},
404   };
405 
406   // CanonicalizeHost() non-verbose.
407   std::string out_str;
408   for (size_t i = 0; i < arraysize(host_cases); i++) {
409     // Narrow version.
410     if (host_cases[i].input8) {
411       int host_len = static_cast<int>(strlen(host_cases[i].input8));
412       url_parse::Component in_comp(0, host_len);
413       url_parse::Component out_comp;
414 
415       out_str.clear();
416       url_canon::StdStringCanonOutput output(&out_str);
417 
418       bool success = url_canon::CanonicalizeHost(host_cases[i].input8, in_comp,
419                                                  &output, &out_comp);
420       output.Complete();
421 
422       EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
423                 success);
424       EXPECT_EQ(std::string(host_cases[i].expected), out_str);
425       EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
426       EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
427     }
428 
429     // Wide version.
430     if (host_cases[i].input16) {
431       string16 input16(WStringToUTF16(host_cases[i].input16));
432       int host_len = static_cast<int>(input16.length());
433       url_parse::Component in_comp(0, host_len);
434       url_parse::Component out_comp;
435 
436       out_str.clear();
437       url_canon::StdStringCanonOutput output(&out_str);
438 
439       bool success = url_canon::CanonicalizeHost(input16.c_str(), in_comp,
440                                                  &output, &out_comp);
441       output.Complete();
442 
443       EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
444                 success);
445       EXPECT_EQ(std::string(host_cases[i].expected), out_str);
446       EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
447       EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
448     }
449   }
450 
451   // CanonicalizeHostVerbose()
452   for (size_t i = 0; i < arraysize(host_cases); i++) {
453     // Narrow version.
454     if (host_cases[i].input8) {
455       int host_len = static_cast<int>(strlen(host_cases[i].input8));
456       url_parse::Component in_comp(0, host_len);
457 
458       out_str.clear();
459       url_canon::StdStringCanonOutput output(&out_str);
460       CanonHostInfo host_info;
461 
462       url_canon::CanonicalizeHostVerbose(host_cases[i].input8, in_comp,
463                                          &output, &host_info);
464       output.Complete();
465 
466       EXPECT_EQ(host_cases[i].expected_family, host_info.family);
467       EXPECT_EQ(std::string(host_cases[i].expected), out_str);
468       EXPECT_EQ(host_cases[i].expected_component.begin,
469                 host_info.out_host.begin);
470       EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
471       if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
472         EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
473                   host_info.num_ipv4_components);
474       }
475     }
476 
477     // Wide version.
478     if (host_cases[i].input16) {
479       string16 input16(WStringToUTF16(host_cases[i].input16));
480       int host_len = static_cast<int>(input16.length());
481       url_parse::Component in_comp(0, host_len);
482 
483       out_str.clear();
484       url_canon::StdStringCanonOutput output(&out_str);
485       CanonHostInfo host_info;
486 
487       url_canon::CanonicalizeHostVerbose(input16.c_str(), in_comp,
488                                          &output, &host_info);
489       output.Complete();
490 
491       EXPECT_EQ(host_cases[i].expected_family, host_info.family);
492       EXPECT_EQ(std::string(host_cases[i].expected), out_str);
493       EXPECT_EQ(host_cases[i].expected_component.begin,
494                 host_info.out_host.begin);
495       EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
496       if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
497         EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
498                   host_info.num_ipv4_components);
499       }
500     }
501   }
502 }
503 
TEST(URLCanonTest,IPv4)504 TEST(URLCanonTest, IPv4) {
505   IPAddressCase cases[] = {
506       // Empty is not an IP address.
507     {"", L"", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
508     {".", L".", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
509       // Regular IP addresses in different bases.
510     {"192.168.0.1", L"192.168.0.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4},
511     {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4},
512     {"0xC0.0Xa8.0x0.0x1", L"0xC0.0Xa8.0x0.0x1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4},
513       // Non-IP addresses due to invalid characters.
514     {"192.168.9.com", L"192.168.9.com", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
515       // Invalid characters for the base should be rejected.
516     {"19a.168.0.1", L"19a.168.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
517     {"0308.0250.00.01", L"0308.0250.00.01", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
518     {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
519       // If there are not enough components, the last one should fill them out.
520     {"192", L"192", "0.0.0.192", url_parse::Component(0, 9), CanonHostInfo::IPV4, 1},
521     {"0xC0a80001", L"0xC0a80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1},
522     {"030052000001", L"030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1},
523     {"000030052000001", L"000030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1},
524     {"192.168", L"192.168", "192.0.0.168", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2},
525     {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2},
526     {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2},
527     {"192.168.1", L"192.168.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3},
528       // Too many components means not an IP address.
529     {"192.168.0.0.1", L"192.168.0.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
530       // We allow a single trailing dot.
531     {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4},
532     {"192.168.0.1. hello", L"192.168.0.1. hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
533     {"192.168.0.1..", L"192.168.0.1..", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
534       // Two dots in a row means not an IP address.
535     {"192.168..1", L"192.168..1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
536       // Any numerical overflow should be marked as BROKEN.
537     {"0x100.0", L"0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
538     {"0x100.0.0", L"0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
539     {"0x100.0.0.0", L"0x100.0.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
540     {"0.0x100.0.0", L"0.0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
541     {"0.0.0x100.0", L"0.0.0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
542     {"0.0.0.0x100", L"0.0.0.0x100", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
543     {"0.0.0x10000", L"0.0.0x10000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
544     {"0.0x1000000", L"0.0x1000000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
545     {"0x100000000", L"0x100000000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
546       // Repeat the previous tests, minus 1, to verify boundaries.
547     {"0xFF.0", L"0xFF.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 2},
548     {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 3},
549     {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4},
550     {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4},
551     {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4},
552     {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4},
553     {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3},
554     {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", url_parse::Component(0, 13), CanonHostInfo::IPV4, 2},
555     {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", url_parse::Component(0, 15), CanonHostInfo::IPV4, 1},
556       // Old trunctations tests.  They're all "BROKEN" now.
557     {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
558     {"192.168.0.257", L"192.168.0.257", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
559     {"192.168.0xa20001", L"192.168.0xa20001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
560     {"192.015052000001", L"192.015052000001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
561     {"0X12C0a80001", L"0X12C0a80001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
562     {"276.1.2", L"276.1.2", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
563       // Spaces should be rejected.
564     {"192.168.0.1 hello", L"192.168.0.1 hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
565       // Very large numbers.
566     {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3},
567     {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", url_parse::Component(0, 11), CanonHostInfo::BROKEN, -1},
568       // A number has no length limit, but long numbers can still overflow.
569     {"00000000000000000001", L"00000000000000000001", "0.0.0.1", url_parse::Component(0, 7), CanonHostInfo::IPV4, 1},
570     {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
571       // If a long component is non-numeric, it's a hostname, *not* a broken IP.
572     {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
573     {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
574       // Truncation of all zeros should still result in 0.
575     {"0.00.0x.0x0", L"0.00.0x.0x0", "0.0.0.0", url_parse::Component(0, 7), CanonHostInfo::IPV4, 4},
576   };
577 
578   for (size_t i = 0; i < arraysize(cases); i++) {
579     // 8-bit version.
580     url_parse::Component component(0,
581                                    static_cast<int>(strlen(cases[i].input8)));
582 
583     std::string out_str1;
584     url_canon::StdStringCanonOutput output1(&out_str1);
585     url_canon::CanonHostInfo host_info;
586     url_canon::CanonicalizeIPAddress(cases[i].input8, component, &output1,
587                                      &host_info);
588     output1.Complete();
589 
590     EXPECT_EQ(cases[i].expected_family, host_info.family);
591     if (host_info.family == CanonHostInfo::IPV4) {
592       EXPECT_STREQ(cases[i].expected, out_str1.c_str());
593       EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
594       EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
595       EXPECT_EQ(cases[i].expected_num_ipv4_components,
596                 host_info.num_ipv4_components);
597     }
598 
599     // 16-bit version.
600     string16 input16(WStringToUTF16(cases[i].input16));
601     component = url_parse::Component(0, static_cast<int>(input16.length()));
602 
603     std::string out_str2;
604     url_canon::StdStringCanonOutput output2(&out_str2);
605     url_canon::CanonicalizeIPAddress(input16.c_str(), component, &output2,
606                                      &host_info);
607     output2.Complete();
608 
609     EXPECT_EQ(cases[i].expected_family, host_info.family);
610     if (host_info.family == CanonHostInfo::IPV4) {
611       EXPECT_STREQ(cases[i].expected, out_str2.c_str());
612       EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
613       EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
614       EXPECT_EQ(cases[i].expected_num_ipv4_components,
615                 host_info.num_ipv4_components);
616     }
617   }
618 }
619 
TEST(URLCanonTest,IPv6)620 TEST(URLCanonTest, IPv6) {
621   IPAddressCase cases[] = {
622       // Empty is not an IP address.
623     {"", L"", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1},
624       // Non-IPs with [:] characters are marked BROKEN.
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     {":]", L":]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
630     {"[]", L"[]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
631     {"[:]", L"[:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
632       // Regular IP address is invalid without bounding '[' and ']'.
633     {"2001:db8::1", L"2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
634     {"[2001:db8::1", L"[2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
635     {"2001:db8::1]", L"2001:db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
636       // Regular IP addresses.
637     {"[::]", L"[::]", "[::]", url_parse::Component(0,4), CanonHostInfo::IPV6, -1},
638     {"[::1]", L"[::1]", "[::1]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1},
639     {"[1::]", L"[1::]", "[1::]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1},
640     {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", url_parse::Component(0,10), CanonHostInfo::IPV6, -1},
641     {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1},
642 
643     // Leading zeros should be stripped.
644     {"[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},
645 
646     // Upper case letters should be lowercased.
647     {"[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},
648 
649     // The same address can be written with different contractions, but should
650     // get canonicalized to the same thing.
651     {"[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},
652     {"[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},
653 
654     // IPv4 addresses
655     // Only mapped and compat addresses can have IPv4 syntax embedded.
656     {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
657     {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
658     {"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
659 
660     // IPv4 with last component missing.
661     {"[::ffff:192.1.2]", L"[::ffff:192.1.2]", "[::ffff:c001:2]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1},
662 
663     // IPv4 using hex.
664     // TODO(eroman): Should this format be disallowed?
665     {"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1},
666 
667     // There may be zeros surrounding the "::" contraction.
668     {"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1},
669 
670     {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", url_parse::Component(0,13), CanonHostInfo::IPV6, -1},
671 
672       // Can only have one "::" contraction in an IPv6 string literal.
673     {"[2001::db8::1]", L"[2001::db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
674       // No more than 2 consecutive ':'s.
675     {"[2001:db8:::1]", L"[2001:db8:::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
676     {"[:::]", L"[:::]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
677       // Non-IP addresses due to invalid characters.
678     {"[2001::.com]", L"[2001::.com]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
679       // If there are not enough components, the last one should fill them out.
680     // ... omitted at this time ...
681       // Too many components means not an IP address.  Similarly with too few if using IPv4 compat or mapped addresses.
682     {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
683     {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
684     {"[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},
685     // Too many bits (even though 8 comonents, the last one holds 32 bits).
686     {"[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},
687 
688     // Too many bits specified -- the contraction would have to be zero-length
689     // to not exceed 128 bits.
690     {"[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},
691 
692     // The contraction is for 16 bits of zero.
693     {"[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},
694 
695     // Cannot have a trailing colon.
696     {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
697     {"[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},
698 
699     // Cannot have negative numbers.
700     {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
701 
702     // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
703     // The scope_id should be included in the canonicalized URL, and is an
704     // unsigned decimal number.
705 
706     // Invalid because no ID was given after the percent.
707 
708     // Don't allow scope-id
709     {"[1::%1]", L"[1::%1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
710     {"[1::%eth0]", L"[1::%eth0]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
711     {"[1::%]", L"[1::%]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
712     {"[%]", L"[%]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
713     {"[::%:]", L"[::%:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
714 
715     // Don't allow leading or trailing colons.
716     {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
717     {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
718     {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
719 
720       // We allow a single trailing dot.
721     // ... omitted at this time ...
722       // Two dots in a row means not an IP address.
723     {"[::192.168..1]", L"[::192.168..1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
724       // Any non-first components get truncated to one byte.
725     // ... omitted at this time ...
726       // Spaces should be rejected.
727     {"[::1 hello]", L"[::1 hello]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1},
728   };
729 
730   for (size_t i = 0; i < arraysize(cases); i++) {
731     // 8-bit version.
732     url_parse::Component component(0,
733                                    static_cast<int>(strlen(cases[i].input8)));
734 
735     std::string out_str1;
736     url_canon::StdStringCanonOutput output1(&out_str1);
737     url_canon::CanonHostInfo host_info;
738     url_canon::CanonicalizeIPAddress(cases[i].input8, component, &output1,
739                                      &host_info);
740     output1.Complete();
741 
742     EXPECT_EQ(cases[i].expected_family, host_info.family);
743     if (host_info.family == CanonHostInfo::IPV6) {
744       EXPECT_STREQ(cases[i].expected, out_str1.c_str());
745       EXPECT_EQ(cases[i].expected_component.begin,
746                 host_info.out_host.begin);
747       EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
748     }
749 
750     // 16-bit version.
751     string16 input16(WStringToUTF16(cases[i].input16));
752     component = url_parse::Component(0, static_cast<int>(input16.length()));
753 
754     std::string out_str2;
755     url_canon::StdStringCanonOutput output2(&out_str2);
756     url_canon::CanonicalizeIPAddress(input16.c_str(), component, &output2,
757                                      &host_info);
758     output2.Complete();
759 
760     EXPECT_EQ(cases[i].expected_family, host_info.family);
761     if (host_info.family == CanonHostInfo::IPV6) {
762       EXPECT_STREQ(cases[i].expected, out_str2.c_str());
763       EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
764       EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
765     }
766   }
767 }
768 
TEST(URLCanonTest,UserInfo)769 TEST(URLCanonTest, UserInfo) {
770   // Note that the canonicalizer should escape and treat empty components as
771   // not being there.
772 
773   // We actually parse a full input URL so we can get the initial components.
774   struct UserComponentCase {
775     const char* input;
776     const char* expected;
777     url_parse::Component expected_username;
778     url_parse::Component expected_password;
779     bool expected_success;
780   } user_info_cases[] = {
781     {"http://user:pass@host.com/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
782     {"http://@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
783     {"http://:@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
784     {"http://foo:@host.com/", "foo@", url_parse::Component(0, 3), url_parse::Component(0, -1), true},
785     {"http://:foo@host.com/", ":foo@", url_parse::Component(0, 0), url_parse::Component(1, 3), true},
786     {"http://^ :$\t@host.com/", "%5E%20:$%09@", url_parse::Component(0, 6), url_parse::Component(7, 4), true},
787     {"http://user:pass@/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
788     {"http://%2540:bar@domain.com/", "%2540:bar@", url_parse::Component(0, 5), url_parse::Component(6, 3), true },
789 
790       // IE7 compatability: old versions allowed backslashes in usernames, but
791       // IE7 does not. We disallow it as well.
792     {"ftp://me\\mydomain:pass@foo.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
793   };
794 
795   for (size_t i = 0; i < ARRAYSIZE(user_info_cases); i++) {
796     int url_len = static_cast<int>(strlen(user_info_cases[i].input));
797     url_parse::Parsed parsed;
798     url_parse::ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
799     url_parse::Component out_user, out_pass;
800     std::string out_str;
801     url_canon::StdStringCanonOutput output1(&out_str);
802 
803     bool success = url_canon::CanonicalizeUserInfo(user_info_cases[i].input,
804                                                    parsed.username,
805                                                    user_info_cases[i].input,
806                                                    parsed.password,
807                                                    &output1, &out_user,
808                                                    &out_pass);
809     output1.Complete();
810 
811     EXPECT_EQ(user_info_cases[i].expected_success, success);
812     EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
813     EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
814     EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
815     EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
816     EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
817 
818     // Now try the wide version
819     out_str.clear();
820     url_canon::StdStringCanonOutput output2(&out_str);
821     string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
822     success = url_canon::CanonicalizeUserInfo(wide_input.c_str(),
823                                               parsed.username,
824                                               wide_input.c_str(),
825                                               parsed.password,
826                                               &output2, &out_user, &out_pass);
827     output2.Complete();
828 
829     EXPECT_EQ(user_info_cases[i].expected_success, success);
830     EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
831     EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
832     EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
833     EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
834     EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
835   }
836 }
837 
TEST(URLCanonTest,Port)838 TEST(URLCanonTest, Port) {
839   // We only need to test that the number gets properly put into the output
840   // buffer. The parser unit tests will test scanning the number correctly.
841   //
842   // Note that the CanonicalizePort will always prepend a colon to the output
843   // to separate it from the colon that it assumes preceeds it.
844   struct PortCase {
845     const char* input;
846     int default_port;
847     const char* expected;
848     url_parse::Component expected_component;
849     bool expected_success;
850   } port_cases[] = {
851       // Invalid input should be copied w/ failure.
852     {"as df", 80, ":as%20df", url_parse::Component(1, 7), false},
853     {"-2", 80, ":-2", url_parse::Component(1, 2), false},
854       // Default port should be omitted.
855     {"80", 80, "", url_parse::Component(0, -1), true},
856     {"8080", 80, ":8080", url_parse::Component(1, 4), true},
857       // PORT_UNSPECIFIED should mean always keep the port.
858     {"80", url_parse::PORT_UNSPECIFIED, ":80", url_parse::Component(1, 2), true},
859   };
860 
861   for (size_t i = 0; i < ARRAYSIZE(port_cases); i++) {
862     int url_len = static_cast<int>(strlen(port_cases[i].input));
863     url_parse::Component in_comp(0, url_len);
864     url_parse::Component out_comp;
865     std::string out_str;
866     url_canon::StdStringCanonOutput output1(&out_str);
867     bool success = url_canon::CanonicalizePort(port_cases[i].input, in_comp,
868                                                port_cases[i].default_port,
869                                                &output1, &out_comp);
870     output1.Complete();
871 
872     EXPECT_EQ(port_cases[i].expected_success, success);
873     EXPECT_EQ(std::string(port_cases[i].expected), out_str);
874     EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
875     EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
876 
877     // Now try the wide version
878     out_str.clear();
879     url_canon::StdStringCanonOutput output2(&out_str);
880     string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
881     success = url_canon::CanonicalizePort(wide_input.c_str(), in_comp,
882                                           port_cases[i].default_port,
883                                           &output2, &out_comp);
884     output2.Complete();
885 
886     EXPECT_EQ(port_cases[i].expected_success, success);
887     EXPECT_EQ(std::string(port_cases[i].expected), out_str);
888     EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
889     EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
890   }
891 }
892 
TEST(URLCanonTest,Path)893 TEST(URLCanonTest, Path) {
894   DualComponentCase path_cases[] = {
895     // ----- path collapsing tests -----
896     {"/././foo", L"/././foo", "/foo", url_parse::Component(0, 4), true},
897     {"/./.foo", L"/./.foo", "/.foo", url_parse::Component(0, 5), true},
898     {"/foo/.", L"/foo/.", "/foo/", url_parse::Component(0, 5), true},
899     {"/foo/./", L"/foo/./", "/foo/", url_parse::Component(0, 5), true},
900       // double dots followed by a slash or the end of the string count
901     {"/foo/bar/..", L"/foo/bar/..", "/foo/", url_parse::Component(0, 5), true},
902     {"/foo/bar/../", L"/foo/bar/../", "/foo/", url_parse::Component(0, 5), true},
903       // don't count double dots when they aren't followed by a slash
904     {"/foo/..bar", L"/foo/..bar", "/foo/..bar", url_parse::Component(0, 10), true},
905       // some in the middle
906     {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", url_parse::Component(0, 8), true},
907     {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", url_parse::Component(0, 2), true},
908       // we should not be able to go above the root
909     {"/foo/../../..", L"/foo/../../..", "/", url_parse::Component(0, 1), true},
910     {"/foo/../../../ton", L"/foo/../../../ton", "/ton", url_parse::Component(0, 4), true},
911       // escaped dots should be unescaped and treated the same as dots
912     {"/foo/%2e", L"/foo/%2e", "/foo/", url_parse::Component(0, 5), true},
913     {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", url_parse::Component(0, 8), true},
914     {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", url_parse::Component(0, 6), true},
915       // Multiple slashes in a row should be preserved and treated like empty
916       // directory names.
917     {"////../..", L"////../..", "//", url_parse::Component(0, 2), true},
918 
919     // ----- escaping tests -----
920     {"/foo", L"/foo", "/foo", url_parse::Component(0, 4), true},
921       // Valid escape sequence
922     {"/%20foo", L"/%20foo", "/%20foo", url_parse::Component(0, 7), true},
923       // Invalid escape sequence we should pass through unchanged.
924     {"/foo%", L"/foo%", "/foo%", url_parse::Component(0, 5), true},
925     {"/foo%2", L"/foo%2", "/foo%2", url_parse::Component(0, 6), true},
926       // Invalid escape sequence: bad characters should be treated the same as
927       // the sourrounding text, not as escaped (in this case, UTF-8).
928     {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", url_parse::Component(0, 10), true},
929     {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", url_parse::Component(0, 16), true},
930     {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", url_parse::Component(0, 22), true},
931       // Regular characters that are escaped should be unescaped
932     {"/foo%41%7a", L"/foo%41%7a", "/fooAz", url_parse::Component(0, 6), true},
933       // Funny characters that are unescaped should be escaped
934     {"/foo\x09\x91%91", NULL, "/foo%09%91%91", url_parse::Component(0, 13), true},
935     {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", url_parse::Component(0, 16), true},
936       // Invalid characters that are escaped should cause a failure.
937     {"/foo%00%51", L"/foo%00%51", "/foo%00Q", url_parse::Component(0, 8), false},
938       // Some characters should be passed through unchanged regardless of esc.
939     {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", url_parse::Component(0, 13), true},
940       // Characters that are properly escaped should not have the case changed
941       // of hex letters.
942     {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", url_parse::Component(0, 13), true},
943       // Funny characters that are unescaped should be escaped
944     {"/foo\tbar", L"/foo\tbar", "/foo%09bar", url_parse::Component(0, 10), true},
945       // Backslashes should get converted to forward slashes
946     {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", url_parse::Component(0, 8), true},
947       // Hashes found in paths (possibly only when the caller explicitly sets
948       // the path on an already-parsed URL) should be escaped.
949     {"/foo#bar", L"/foo#bar", "/foo%23bar", url_parse::Component(0, 10), true},
950       // %7f should be allowed and %3D should not be unescaped (these were wrong
951       // in a previous version).
952     {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", url_parse::Component(0, 24), true},
953       // @ should be unescaped.
954     {"/@asdf%40", L"/@asdf%40", "/@asdf@", url_parse::Component(0, 7), true},
955 
956     // ----- encoding tests -----
957       // Basic conversions
958     {"/\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},
959       // Invalid unicode characters should fail. We only do validation on
960       // UTF-16 input, so this doesn't happen on 8-bit.
961     {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", url_parse::Component(0, 13), true},
962     {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", url_parse::Component(0, 13), false},
963   };
964 
965   for (size_t i = 0; i < arraysize(path_cases); i++) {
966     if (path_cases[i].input8) {
967       int len = static_cast<int>(strlen(path_cases[i].input8));
968       url_parse::Component in_comp(0, len);
969       url_parse::Component out_comp;
970       std::string out_str;
971       url_canon::StdStringCanonOutput output(&out_str);
972       bool success = url_canon::CanonicalizePath(path_cases[i].input8, in_comp,
973                                                  &output, &out_comp);
974       output.Complete();
975 
976       EXPECT_EQ(path_cases[i].expected_success, success);
977       EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
978       EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
979       EXPECT_EQ(path_cases[i].expected, out_str);
980     }
981 
982     if (path_cases[i].input16) {
983       string16 input16(WStringToUTF16(path_cases[i].input16));
984       int len = static_cast<int>(input16.length());
985       url_parse::Component in_comp(0, len);
986       url_parse::Component out_comp;
987       std::string out_str;
988       url_canon::StdStringCanonOutput output(&out_str);
989 
990       bool success = url_canon::CanonicalizePath(input16.c_str(), in_comp,
991                                                  &output, &out_comp);
992       output.Complete();
993 
994       EXPECT_EQ(path_cases[i].expected_success, success);
995       EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
996       EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
997       EXPECT_EQ(path_cases[i].expected, out_str);
998     }
999   }
1000 
1001   // Manual test: embedded NULLs should be escaped and the URL should be marked
1002   // as invalid.
1003   const char path_with_null[] = "/ab\0c";
1004   url_parse::Component in_comp(0, 5);
1005   url_parse::Component out_comp;
1006 
1007   std::string out_str;
1008   url_canon::StdStringCanonOutput output(&out_str);
1009   bool success = url_canon::CanonicalizePath(path_with_null, in_comp,
1010                                              &output, &out_comp);
1011   output.Complete();
1012   EXPECT_FALSE(success);
1013   EXPECT_EQ("/ab%00c", out_str);
1014 }
1015 
TEST(URLCanonTest,Query)1016 TEST(URLCanonTest, Query) {
1017   struct QueryCase {
1018     const char* input8;
1019     const wchar_t* input16;
1020     const char* encoding;
1021     const char* expected;
1022   } query_cases[] = {
1023       // Regular ASCII case in some different encodings.
1024     {"foo=bar", L"foo=bar", NULL, "?foo=bar"},
1025     {"foo=bar", L"foo=bar", "utf-8", "?foo=bar"},
1026     {"foo=bar", L"foo=bar", "shift_jis", "?foo=bar"},
1027     {"foo=bar", L"foo=bar", "gb2312", "?foo=bar"},
1028       // Allow question marks in the query without escaping
1029     {"as?df", L"as?df", NULL, "?as?df"},
1030       // Always escape '#' since it would mark the ref.
1031     {"as#df", L"as#df", NULL, "?as%23df"},
1032       // Escape some questionable 8-bit characters, but never unescape.
1033     {"\x02hello\x7f bye", L"\x02hello\x7f bye", NULL, "?%02hello%7F%20bye"},
1034     {"%40%41123", L"%40%41123", NULL, "?%40%41123"},
1035       // Chinese input/output
1036     {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", NULL, "?q=%E4%BD%A0%E5%A5%BD"},
1037     {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "gb2312", "?q=%C4%E3%BA%C3"},
1038     {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "big5", "?q=%A7A%A6n"},
1039       // Unencodable character in the destination character set should be
1040       // escaped. The escape sequence unescapes to be the entity name:
1041       // "?q=&#20320;"
1042     {"q=Chinese\xef\xbc\xa7", L"q=Chinese\xff27", "iso-8859-1", "?q=Chinese%26%2365319%3B"},
1043       // Invalid UTF-8/16 input should be replaced with invalid characters.
1044     {"q=\xed\xed", L"q=\xd800\xd800", NULL, "?q=%EF%BF%BD%EF%BF%BD"},
1045       // Don't allow < or > because sometimes they are used for XSS if the
1046       // URL is echoed in content. Firefox does this, IE doesn't.
1047     {"q=<asdf>", L"q=<asdf>", NULL, "?q=%3Casdf%3E"},
1048       // Escape double quotemarks in the query.
1049     {"q=\"asdf\"", L"q=\"asdf\"", NULL, "?q=%22asdf%22"},
1050   };
1051 
1052   for (size_t i = 0; i < ARRAYSIZE(query_cases); i++) {
1053     url_parse::Component out_comp;
1054 
1055     UConvScoper conv(query_cases[i].encoding);
1056     ASSERT_TRUE(!query_cases[i].encoding || conv.converter());
1057     url_canon::ICUCharsetConverter converter(conv.converter());
1058 
1059     // Map NULL to a NULL converter pointer.
1060     url_canon::ICUCharsetConverter* conv_pointer = &converter;
1061     if (!query_cases[i].encoding)
1062       conv_pointer = NULL;
1063 
1064     if (query_cases[i].input8) {
1065       int len = static_cast<int>(strlen(query_cases[i].input8));
1066       url_parse::Component in_comp(0, len);
1067       std::string out_str;
1068 
1069       url_canon::StdStringCanonOutput output(&out_str);
1070       url_canon::CanonicalizeQuery(query_cases[i].input8, in_comp,
1071                                    conv_pointer, &output, &out_comp);
1072       output.Complete();
1073 
1074       EXPECT_EQ(query_cases[i].expected, out_str);
1075     }
1076 
1077     if (query_cases[i].input16) {
1078       string16 input16(WStringToUTF16(query_cases[i].input16));
1079       int len = static_cast<int>(input16.length());
1080       url_parse::Component in_comp(0, len);
1081       std::string out_str;
1082 
1083       url_canon::StdStringCanonOutput output(&out_str);
1084       url_canon::CanonicalizeQuery(input16.c_str(), in_comp,
1085                                    conv_pointer, &output, &out_comp);
1086       output.Complete();
1087 
1088       EXPECT_EQ(query_cases[i].expected, out_str);
1089     }
1090   }
1091 
1092   // Extra test for input with embedded NULL;
1093   std::string out_str;
1094   url_canon::StdStringCanonOutput output(&out_str);
1095   url_parse::Component out_comp;
1096   url_canon::CanonicalizeQuery("a \x00z\x01", url_parse::Component(0, 5), NULL,
1097                                &output, &out_comp);
1098   output.Complete();
1099   EXPECT_EQ("?a%20%00z%01", out_str);
1100 }
1101 
TEST(URLCanonTest,Ref)1102 TEST(URLCanonTest, Ref) {
1103   // Refs are trivial, it just checks the encoding.
1104   DualComponentCase ref_cases[] = {
1105       // Regular one, we shouldn't escape spaces, et al.
1106     {"hello, world", L"hello, world", "#hello, world", url_parse::Component(1, 12), true},
1107       // UTF-8/wide input should be preserved
1108     {"\xc2\xa9", L"\xa9", "#\xc2\xa9", url_parse::Component(1, 2), true},
1109       // Test a characer that takes > 16 bits (U+10300 = old italic letter A)
1110     {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", url_parse::Component(1, 6), true},
1111       // Escaping should be preserved unchanged, even invalid ones
1112     {"%41%a", L"%41%a", "#%41%a", url_parse::Component(1, 5), true},
1113       // Invalid UTF-8/16 input should be flagged and the input made valid
1114     {"\xc2", NULL, "#\xef\xbf\xbd", url_parse::Component(1, 3), true},
1115     {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", url_parse::Component(1, 6), true},
1116       // Test a Unicode invalid character.
1117     {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", url_parse::Component(1, 4), true},
1118       // Refs can have # signs and we should preserve them.
1119     {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", url_parse::Component(1, 9), true},
1120     {"#asdf", L"#asdf", "##asdf", url_parse::Component(1, 5), true},
1121   };
1122 
1123   for (size_t i = 0; i < arraysize(ref_cases); i++) {
1124     // 8-bit input
1125     if (ref_cases[i].input8) {
1126       int len = static_cast<int>(strlen(ref_cases[i].input8));
1127       url_parse::Component in_comp(0, len);
1128       url_parse::Component out_comp;
1129 
1130       std::string out_str;
1131       url_canon::StdStringCanonOutput output(&out_str);
1132       url_canon::CanonicalizeRef(ref_cases[i].input8, in_comp,
1133                                  &output, &out_comp);
1134       output.Complete();
1135 
1136       EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1137       EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1138       EXPECT_EQ(ref_cases[i].expected, out_str);
1139     }
1140 
1141     // 16-bit input
1142     if (ref_cases[i].input16) {
1143       string16 input16(WStringToUTF16(ref_cases[i].input16));
1144       int len = static_cast<int>(input16.length());
1145       url_parse::Component in_comp(0, len);
1146       url_parse::Component out_comp;
1147 
1148       std::string out_str;
1149       url_canon::StdStringCanonOutput output(&out_str);
1150       url_canon::CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
1151       output.Complete();
1152 
1153       EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
1154       EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
1155       EXPECT_EQ(ref_cases[i].expected, out_str);
1156     }
1157   }
1158 
1159   // Try one with an embedded NULL. It should be stripped.
1160   const char null_input[5] = "ab\x00z";
1161   url_parse::Component null_input_component(0, 4);
1162   url_parse::Component out_comp;
1163 
1164   std::string out_str;
1165   url_canon::StdStringCanonOutput output(&out_str);
1166   url_canon::CanonicalizeRef(null_input, null_input_component,
1167                              &output, &out_comp);
1168   output.Complete();
1169 
1170   EXPECT_EQ(1, out_comp.begin);
1171   EXPECT_EQ(3, out_comp.len);
1172   EXPECT_EQ("#abz", out_str);
1173 }
1174 
TEST(URLCanonTest,CanonicalizeStandardURL)1175 TEST(URLCanonTest, CanonicalizeStandardURL) {
1176   // The individual component canonicalize tests should have caught the cases
1177   // for each of those components. Here, we just need to test that the various
1178   // parts are included or excluded properly, and have the correct separators.
1179   struct URLCase {
1180     const char* input;
1181     const char* expected;
1182     bool expected_success;
1183   } cases[] = {
1184     {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
1185     {"http://[www.google.com]/", "http://[www.google.com]/", false},
1186     {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
1187     {"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo", true},
1188     {"www.google.com", ":www.google.com/", true},
1189     {"http://192.0x00A80001", "http://192.168.0.1/", true},
1190     {"http://www/foo%2Ehtml", "http://www/foo.html", true},
1191     {"http://user:pass@/", "http://user:pass@/", false},
1192     {"http://%25DOMAIN:foobar@foodomain.com/", "http://%25DOMAIN:foobar@foodomain.com/", true},
1193 
1194       // Backslashes should get converted to forward slashes.
1195     {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
1196 
1197       // Busted refs shouldn't make the whole thing fail.
1198     {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
1199 
1200       // Basic port tests.
1201     {"http://foo:80/", "http://foo/", true},
1202     {"http://foo:81/", "http://foo:81/", true},
1203     {"httpa://foo:80/", "httpa://foo:80/", true},
1204     {"http://foo:-80/", "http://foo:-80/", false},
1205 
1206     {"https://foo:443/", "https://foo/", true},
1207     {"https://foo:80/", "https://foo:80/", true},
1208     {"ftp://foo:21/", "ftp://foo/", true},
1209     {"ftp://foo:80/", "ftp://foo:80/", true},
1210     {"gopher://foo:70/", "gopher://foo/", true},
1211     {"gopher://foo:443/", "gopher://foo:443/", true},
1212     {"ws://foo:80/", "ws://foo/", true},
1213     {"ws://foo:81/", "ws://foo:81/", true},
1214     {"ws://foo:443/", "ws://foo:443/", true},
1215     {"ws://foo:815/", "ws://foo:815/", true},
1216     {"wss://foo:80/", "wss://foo:80/", true},
1217     {"wss://foo:81/", "wss://foo:81/", true},
1218     {"wss://foo:443/", "wss://foo/", true},
1219     {"wss://foo:815/", "wss://foo:815/", true},
1220   };
1221 
1222   for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
1223     int url_len = static_cast<int>(strlen(cases[i].input));
1224     url_parse::Parsed parsed;
1225     url_parse::ParseStandardURL(cases[i].input, url_len, &parsed);
1226 
1227     url_parse::Parsed out_parsed;
1228     std::string out_str;
1229     url_canon::StdStringCanonOutput output(&out_str);
1230     bool success = url_canon::CanonicalizeStandardURL(
1231         cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
1232     output.Complete();
1233 
1234     EXPECT_EQ(cases[i].expected_success, success);
1235     EXPECT_EQ(cases[i].expected, out_str);
1236   }
1237 }
1238 
1239 // The codepath here is the same as for regular canonicalization, so we just
1240 // need to test that things are replaced or not correctly.
TEST(URLCanonTest,ReplaceStandardURL)1241 TEST(URLCanonTest, ReplaceStandardURL) {
1242   ReplaceCase replace_cases[] = {
1243       // Common case of truncating the path.
1244     {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
1245       // Replace everything
1246     {"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"},
1247       // Replace nothing
1248     {"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"},
1249   };
1250 
1251   for (size_t i = 0; i < arraysize(replace_cases); i++) {
1252     const ReplaceCase& cur = replace_cases[i];
1253     int base_len = static_cast<int>(strlen(cur.base));
1254     url_parse::Parsed parsed;
1255     url_parse::ParseStandardURL(cur.base, base_len, &parsed);
1256 
1257     url_canon::Replacements<char> r;
1258     typedef url_canon::Replacements<char> R;  // Clean up syntax.
1259 
1260     // Note that for the scheme we pass in a different clear function since
1261     // there is no function to clear the scheme.
1262     SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1263     SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1264     SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1265     SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1266     SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1267     SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1268     SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1269     SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1270 
1271     std::string out_str;
1272     url_canon::StdStringCanonOutput output(&out_str);
1273     url_parse::Parsed out_parsed;
1274     url_canon::ReplaceStandardURL(replace_cases[i].base, parsed,
1275                                   r, NULL, &output, &out_parsed);
1276     output.Complete();
1277 
1278     EXPECT_EQ(replace_cases[i].expected, out_str);
1279   }
1280 
1281   // The path pointer should be ignored if the address is invalid.
1282   {
1283     const char src[] = "http://www.google.com/here_is_the_path";
1284     int src_len = static_cast<int>(strlen(src));
1285 
1286     url_parse::Parsed parsed;
1287     url_parse::ParseStandardURL(src, src_len, &parsed);
1288 
1289     // Replace the path to 0 length string. By using 1 as the string address,
1290     // the test should get an access violation if it tries to dereference it.
1291     url_canon::Replacements<char> r;
1292     r.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component(0, 0));
1293     std::string out_str1;
1294     url_canon::StdStringCanonOutput output1(&out_str1);
1295     url_parse::Parsed new_parsed;
1296     url_canon::ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
1297     output1.Complete();
1298     EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
1299 
1300     // Same with an "invalid" path.
1301     r.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component());
1302     std::string out_str2;
1303     url_canon::StdStringCanonOutput output2(&out_str2);
1304     url_canon::ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
1305     output2.Complete();
1306     EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
1307   }
1308 }
1309 
TEST(URLCanonTest,ReplaceFileURL)1310 TEST(URLCanonTest, ReplaceFileURL) {
1311   ReplaceCase replace_cases[] = {
1312       // Replace everything
1313     {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1314       // Replace nothing
1315     {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
1316       // Clear non-path components (common)
1317     {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
1318       // Replace path with something that doesn't begin with a slash and make
1319       // sure it get added properly.
1320     {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1321     {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
1322     {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
1323     {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
1324     {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
1325   };
1326 
1327   for (size_t i = 0; i < arraysize(replace_cases); i++) {
1328     const ReplaceCase& cur = replace_cases[i];
1329     int base_len = static_cast<int>(strlen(cur.base));
1330     url_parse::Parsed parsed;
1331     url_parse::ParseFileURL(cur.base, base_len, &parsed);
1332 
1333     url_canon::Replacements<char> r;
1334     typedef url_canon::Replacements<char> R;  // Clean up syntax.
1335     SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1336     SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1337     SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1338     SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1339     SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1340     SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1341     SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1342     SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1343 
1344     std::string out_str;
1345     url_canon::StdStringCanonOutput output(&out_str);
1346     url_parse::Parsed out_parsed;
1347     url_canon::ReplaceFileURL(cur.base, parsed,
1348                               r, NULL, &output, &out_parsed);
1349     output.Complete();
1350 
1351     EXPECT_EQ(replace_cases[i].expected, out_str);
1352   }
1353 }
1354 
TEST(URLCanonTest,ReplacePathURL)1355 TEST(URLCanonTest, ReplacePathURL) {
1356   ReplaceCase replace_cases[] = {
1357       // Replace everything
1358     {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
1359       // Replace nothing
1360     {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
1361       // Replace one or the other
1362     {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
1363     {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
1364     {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
1365   };
1366 
1367   for (size_t i = 0; i < arraysize(replace_cases); i++) {
1368     const ReplaceCase& cur = replace_cases[i];
1369     int base_len = static_cast<int>(strlen(cur.base));
1370     url_parse::Parsed parsed;
1371     url_parse::ParsePathURL(cur.base, base_len, &parsed);
1372 
1373     url_canon::Replacements<char> r;
1374     typedef url_canon::Replacements<char> R;  // Clean up syntax.
1375     SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1376     SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1377     SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1378     SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1379     SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1380     SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1381     SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1382     SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1383 
1384     std::string out_str;
1385     url_canon::StdStringCanonOutput output(&out_str);
1386     url_parse::Parsed out_parsed;
1387     url_canon::ReplacePathURL(cur.base, parsed,
1388                               r, &output, &out_parsed);
1389     output.Complete();
1390 
1391     EXPECT_EQ(replace_cases[i].expected, out_str);
1392   }
1393 }
1394 
TEST(URLCanonTest,ReplaceMailtoURL)1395 TEST(URLCanonTest, ReplaceMailtoURL) {
1396   ReplaceCase replace_cases[] = {
1397       // Replace everything
1398     {"mailto:jon@foo.com?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
1399       // Replace nothing
1400     {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:jon@foo.com?body=sup"},
1401       // Replace the path
1402     {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
1403       // Replace the query
1404     {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:jon@foo.com?custom=1"},
1405       // Replace the path and query
1406     {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
1407       // Set the query to empty (should leave trailing question mark)
1408     {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:jon@foo.com?"},
1409       // Clear the query
1410     {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:jon@foo.com"},
1411       // Clear the path
1412     {"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
1413       // Clear the path + query
1414     {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
1415       // Setting the ref should have no effect
1416     {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
1417   };
1418 
1419   for (size_t i = 0; i < arraysize(replace_cases); i++) {
1420     const ReplaceCase& cur = replace_cases[i];
1421     int base_len = static_cast<int>(strlen(cur.base));
1422     url_parse::Parsed parsed;
1423     url_parse::ParseMailtoURL(cur.base, base_len, &parsed);
1424 
1425     url_canon::Replacements<char> r;
1426     typedef url_canon::Replacements<char> R;
1427     SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
1428     SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
1429     SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
1430     SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
1431     SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
1432     SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
1433     SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
1434     SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
1435 
1436     std::string out_str;
1437     url_canon::StdStringCanonOutput output(&out_str);
1438     url_parse::Parsed out_parsed;
1439     url_canon::ReplaceMailtoURL(cur.base, parsed,
1440                                 r, &output, &out_parsed);
1441     output.Complete();
1442 
1443     EXPECT_EQ(replace_cases[i].expected, out_str);
1444   }
1445 }
1446 
TEST(URLCanonTest,CanonicalizeFileURL)1447 TEST(URLCanonTest, CanonicalizeFileURL) {
1448   struct URLCase {
1449     const char* input;
1450     const char* expected;
1451     bool expected_success;
1452     url_parse::Component expected_host;
1453     url_parse::Component expected_path;
1454   } cases[] = {
1455 #ifdef _WIN32
1456       // Windows-style paths
1457     {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1458     {"  File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1459     {"file:", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1460     {"file:UNChost/path", "file://unchost/path", true, url_parse::Component(7, 7), url_parse::Component(14, 5)},
1461       // CanonicalizeFileURL supports absolute Windows style paths for IE
1462       // compatability. Note that the caller must decide that this is a file
1463       // URL itself so it can call the file canonicalizer. This is usually
1464       // done automatically as part of relative URL resolving.
1465     {"c:\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1466     {"C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1467     {"/C|\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1468     {"//C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
1469     {"//server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1470     {"\\\\server\\file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1471     {"/\\server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
1472       // We should preserve the number of slashes after the colon for IE
1473       // compatability, except when there is none, in which case we should
1474       // add one.
1475     {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1476     {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1477       // Three slashes should be non-UNC, even if there is no drive spec (IE
1478       // does this, which makes the resulting request invalid).
1479     {"file:///foo/bar.txt", "file:///foo/bar.txt", true, url_parse::Component(), url_parse::Component(7, 12)},
1480       // TODO(brettw) we should probably fail for invalid host names, which
1481       // would change the expected result on this test. We also currently allow
1482       // colon even though it's probably invalid, because its currently the
1483       // "natural" result of the way the canonicalizer is written. There doesn't
1484       // seem to be a strong argument for why allowing it here would be bad, so
1485       // we just tolerate it and the load will fail later.
1486     {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, url_parse::Component(7, 2), url_parse::Component(9, 16)},
1487     {"file:filer/home\\me", "file://filer/home/me", true, url_parse::Component(7, 5), url_parse::Component(12, 8)},
1488       // Make sure relative paths can't go above the "C:"
1489     {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, url_parse::Component(), url_parse::Component(7, 12)},
1490       // Busted refs shouldn't make the whole thing fail.
1491     {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, url_parse::Component(), url_parse::Component(7, 8)},
1492 #else
1493       // Unix-style paths
1494     {"file:///home/me", "file:///home/me", true, url_parse::Component(), url_parse::Component(7, 8)},
1495       // Windowsy ones should get still treated as Unix-style.
1496     {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
1497     {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
1498       // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html)
1499     {"//", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1500     {"///", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
1501     {"///test", "file:///test", true, url_parse::Component(), url_parse::Component(7, 5)},
1502     {"file://test", "file://test/", true, url_parse::Component(7, 4), url_parse::Component(11, 1)},
1503     {"file://localhost",  "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
1504     {"file://localhost/", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
1505     {"file://localhost/test", "file://localhost/test", true, url_parse::Component(7, 9), url_parse::Component(16, 5)},
1506 #endif  // _WIN32
1507   };
1508 
1509   for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
1510     int url_len = static_cast<int>(strlen(cases[i].input));
1511     url_parse::Parsed parsed;
1512     url_parse::ParseFileURL(cases[i].input, url_len, &parsed);
1513 
1514     url_parse::Parsed out_parsed;
1515     std::string out_str;
1516     url_canon::StdStringCanonOutput output(&out_str);
1517     bool success = url_canon::CanonicalizeFileURL(cases[i].input, url_len,
1518                                                   parsed, NULL, &output,
1519                                                   &out_parsed);
1520     output.Complete();
1521 
1522     EXPECT_EQ(cases[i].expected_success, success);
1523     EXPECT_EQ(cases[i].expected, out_str);
1524 
1525     // Make sure the spec was properly identified, the file canonicalizer has
1526     // different code for writing the spec.
1527     EXPECT_EQ(0, out_parsed.scheme.begin);
1528     EXPECT_EQ(4, out_parsed.scheme.len);
1529 
1530     EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
1531     EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
1532 
1533     EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1534     EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1535   }
1536 }
1537 
TEST(URLCanonTest,CanonicalizePathURL)1538 TEST(URLCanonTest, CanonicalizePathURL) {
1539   // Path URLs should get canonicalized schemes but nothing else.
1540   struct PathCase {
1541     const char* input;
1542     const char* expected;
1543   } path_cases[] = {
1544     {"javascript:", "javascript:"},
1545     {"JavaScript:Foo", "javascript:Foo"},
1546     {":\":This /is interesting;?#", ":\":This /is interesting;?#"},
1547   };
1548 
1549   for (size_t i = 0; i < ARRAYSIZE(path_cases); i++) {
1550     int url_len = static_cast<int>(strlen(path_cases[i].input));
1551     url_parse::Parsed parsed;
1552     url_parse::ParsePathURL(path_cases[i].input, url_len, &parsed);
1553 
1554     url_parse::Parsed out_parsed;
1555     std::string out_str;
1556     url_canon::StdStringCanonOutput output(&out_str);
1557     bool success = url_canon::CanonicalizePathURL(path_cases[i].input, url_len,
1558                                                   parsed, &output,
1559                                                   &out_parsed);
1560     output.Complete();
1561 
1562     EXPECT_TRUE(success);
1563     EXPECT_EQ(path_cases[i].expected, out_str);
1564 
1565     EXPECT_EQ(0, out_parsed.host.begin);
1566     EXPECT_EQ(-1, out_parsed.host.len);
1567 
1568     // When we end with a colon at the end, there should be no path.
1569     if (path_cases[i].input[url_len - 1] == ':') {
1570       EXPECT_EQ(0, out_parsed.path.begin);
1571       EXPECT_EQ(-1, out_parsed.path.len);
1572     }
1573   }
1574 }
1575 
TEST(URLCanonTest,CanonicalizeMailtoURL)1576 TEST(URLCanonTest, CanonicalizeMailtoURL) {
1577   struct URLCase {
1578     const char* input;
1579     const char* expected;
1580     bool expected_success;
1581     url_parse::Component expected_path;
1582     url_parse::Component expected_query;
1583   } cases[] = {
1584     {"mailto:addr1", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
1585     {"mailto:addr1@foo.com", "mailto:addr1@foo.com", true, url_parse::Component(7, 13), url_parse::Component()},
1586     // Trailing whitespace is stripped.
1587     {"MaIlTo:addr1 \t ", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
1588     {"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, url_parse::Component(7, 5), url_parse::Component(13,6)},
1589     {"mailto:addr1,addr2", "mailto:addr1,addr2", true, url_parse::Component(7, 11), url_parse::Component()},
1590     {"mailto:addr1, addr2", "mailto:addr1, addr2", true, url_parse::Component(7, 12), url_parse::Component()},
1591     {"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, url_parse::Component(7, 13), url_parse::Component()},
1592     {"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, url_parse::Component(7, 12), url_parse::Component()},
1593     // Null character should be escaped to %00
1594     {"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, url_parse::Component(7, 13), url_parse::Component(21, 3)},
1595     // Invalid -- UTF-8 encoded surrogate value.
1596     {"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, url_parse::Component(7, 9), url_parse::Component()},
1597     {"mailto:addr1?", "mailto:addr1?", true, url_parse::Component(7, 5), url_parse::Component(13, 0)},
1598   };
1599 
1600   // Define outside of loop to catch bugs where components aren't reset
1601   url_parse::Parsed parsed;
1602   url_parse::Parsed out_parsed;
1603 
1604   for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
1605     int url_len = static_cast<int>(strlen(cases[i].input));
1606     if (i == 8) {
1607       // The 9th test case purposely has a '\0' in it -- don't count it
1608       // as the string terminator.
1609       url_len = 22;
1610     }
1611     url_parse::ParseMailtoURL(cases[i].input, url_len, &parsed);
1612 
1613     std::string out_str;
1614     url_canon::StdStringCanonOutput output(&out_str);
1615     bool success = url_canon::CanonicalizeMailtoURL(cases[i].input, url_len,
1616                                                     parsed, &output,
1617                                                     &out_parsed);
1618     output.Complete();
1619 
1620     EXPECT_EQ(cases[i].expected_success, success);
1621     EXPECT_EQ(cases[i].expected, out_str);
1622 
1623     // Make sure the spec was properly identified
1624     EXPECT_EQ(0, out_parsed.scheme.begin);
1625     EXPECT_EQ(6, out_parsed.scheme.len);
1626 
1627     EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
1628     EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
1629 
1630     EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
1631     EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
1632   }
1633 }
1634 
1635 #ifndef WIN32
1636 
TEST(URLCanonTest,_itoa_s)1637 TEST(URLCanonTest, _itoa_s) {
1638   // We fill the buffer with 0xff to ensure that it's getting properly
1639   // null-terminated.  We also allocate one byte more than what we tell
1640   // _itoa_s about, and ensure that the extra byte is untouched.
1641   char buf[6];
1642   memset(buf, 0xff, sizeof(buf));
1643   EXPECT_EQ(0, url_canon::_itoa_s(12, buf, sizeof(buf) - 1, 10));
1644   EXPECT_STREQ("12", buf);
1645   EXPECT_EQ('\xFF', buf[3]);
1646 
1647   // Test the edge cases - exactly the buffer size and one over
1648   memset(buf, 0xff, sizeof(buf));
1649   EXPECT_EQ(0, url_canon::_itoa_s(1234, buf, sizeof(buf) - 1, 10));
1650   EXPECT_STREQ("1234", buf);
1651   EXPECT_EQ('\xFF', buf[5]);
1652 
1653   memset(buf, 0xff, sizeof(buf));
1654   EXPECT_EQ(EINVAL, url_canon::_itoa_s(12345, buf, sizeof(buf) - 1, 10));
1655   EXPECT_EQ('\xFF', buf[5]);  // should never write to this location
1656 
1657   // Test the template overload (note that this will see the full buffer)
1658   memset(buf, 0xff, sizeof(buf));
1659   EXPECT_EQ(0, url_canon::_itoa_s(12, buf, 10));
1660   EXPECT_STREQ("12", buf);
1661   EXPECT_EQ('\xFF', buf[3]);
1662 
1663   memset(buf, 0xff, sizeof(buf));
1664   EXPECT_EQ(0, url_canon::_itoa_s(12345, buf, 10));
1665   EXPECT_STREQ("12345", buf);
1666 
1667   EXPECT_EQ(EINVAL, url_canon::_itoa_s(123456, buf, 10));
1668 
1669   // Test that radix 16 is supported.
1670   memset(buf, 0xff, sizeof(buf));
1671   EXPECT_EQ(0, url_canon::_itoa_s(1234, buf, sizeof(buf) - 1, 16));
1672   EXPECT_STREQ("4d2", buf);
1673   EXPECT_EQ('\xFF', buf[5]);
1674 }
1675 
TEST(URLCanonTest,_itow_s)1676 TEST(URLCanonTest, _itow_s) {
1677   // We fill the buffer with 0xff to ensure that it's getting properly
1678   // null-terminated.  We also allocate one byte more than what we tell
1679   // _itoa_s about, and ensure that the extra byte is untouched.
1680   char16 buf[6];
1681   const char fill_mem = 0xff;
1682   const char16 fill_char = 0xffff;
1683   memset(buf, fill_mem, sizeof(buf));
1684   EXPECT_EQ(0, url_canon::_itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
1685   EXPECT_EQ(WStringToUTF16(L"12"), string16(buf));
1686   EXPECT_EQ(fill_char, buf[3]);
1687 
1688   // Test the edge cases - exactly the buffer size and one over
1689   EXPECT_EQ(0, url_canon::_itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
1690   EXPECT_EQ(WStringToUTF16(L"1234"), string16(buf));
1691   EXPECT_EQ(fill_char, buf[5]);
1692 
1693   memset(buf, fill_mem, sizeof(buf));
1694   EXPECT_EQ(EINVAL, url_canon::_itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
1695   EXPECT_EQ(fill_char, buf[5]);  // should never write to this location
1696 
1697   // Test the template overload (note that this will see the full buffer)
1698   memset(buf, fill_mem, sizeof(buf));
1699   EXPECT_EQ(0, url_canon::_itow_s(12, buf, 10));
1700   EXPECT_EQ(WStringToUTF16(L"12"), string16(buf));
1701   EXPECT_EQ(fill_char, buf[3]);
1702 
1703   memset(buf, fill_mem, sizeof(buf));
1704   EXPECT_EQ(0, url_canon::_itow_s(12345, buf, 10));
1705   EXPECT_EQ(WStringToUTF16(L"12345"), string16(buf));
1706 
1707   EXPECT_EQ(EINVAL, url_canon::_itow_s(123456, buf, 10));
1708 }
1709 
1710 #endif  // !WIN32
1711 
1712 // Returns true if the given two structures are the same.
ParsedIsEqual(const url_parse::Parsed & a,const url_parse::Parsed & b)1713 static bool ParsedIsEqual(const url_parse::Parsed& a,
1714                           const url_parse::Parsed& b) {
1715   return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
1716          a.username.begin == b.username.begin && a.username.len == b.username.len &&
1717          a.password.begin == b.password.begin && a.password.len == b.password.len &&
1718          a.host.begin == b.host.begin && a.host.len == b.host.len &&
1719          a.port.begin == b.port.begin && a.port.len == b.port.len &&
1720          a.path.begin == b.path.begin && a.path.len == b.path.len &&
1721          a.query.begin == b.query.begin && a.query.len == b.query.len &&
1722          a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
1723 }
1724 
TEST(URLCanonTest,ResolveRelativeURL)1725 TEST(URLCanonTest, ResolveRelativeURL) {
1726   struct RelativeCase {
1727     const char* base;      // Input base URL: MUST BE CANONICAL
1728     bool is_base_hier;     // Is the base URL hierarchical
1729     bool is_base_file;     // Tells us if the base is a file URL.
1730     const char* test;      // Input URL to test against.
1731     bool succeed_relative; // Whether we expect IsRelativeURL to succeed
1732     bool is_rel;           // Whether we expect |test| to be relative or not.
1733     bool succeed_resolve;  // Whether we expect ResolveRelativeURL to succeed.
1734     const char* resolved;  // What we expect in the result when resolving.
1735   } rel_cases[] = {
1736       // Basic absolute input.
1737     {"http://host/a", true, false, "http://another/", true, false, false, NULL},
1738     {"http://host/a", true, false, "http:////another/", true, false, false, NULL},
1739       // Empty relative URLs shouldn't change the input.
1740     {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
1741       // Spaces at the ends of the relative path should be ignored.
1742     {"http://foo/bar", true, false, "  another  ", true, true, true, "http://foo/another"},
1743     {"http://foo/bar", true, false, "  .  ", true, true, true, "http://foo/"},
1744     {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
1745       // Matching schemes without two slashes are treated as relative.
1746     {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
1747     {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
1748     {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
1749     {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
1750       // Nonmatching schemes are absolute.
1751     {"http://host/a", true, false, "https:host2", true, false, false, NULL},
1752     {"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
1753       // Absolute path input
1754     {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
1755     {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
1756     {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
1757     {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
1758     {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
1759     {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
1760       // Relative path input
1761     {"http://host/a", true, false, "b", true, true, true, "http://host/b"},
1762     {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
1763     {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
1764     {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
1765     {"http://host/a/", true, false, "..", true, true, true, "http://host/"},
1766     {"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
1767     {"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
1768     {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
1769     {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
1770       // Query input
1771     {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
1772     {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
1773     {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
1774       // Ref input
1775     {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
1776     {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
1777     {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
1778       // Non-hierarchical base: no relative handling. Relative input should
1779       // error, and if a scheme is present, it should be treated as absolute.
1780     {"data:foobar", false, false, "baz.html", false, false, false, NULL},
1781     {"data:foobar", false, false, "data:baz", true, false, false, NULL},
1782     {"data:foobar", false, false, "data:/base", true, false, false, NULL},
1783       // Non-hierarchical base: absolute input should succeed.
1784     {"data:foobar", false, false, "http://host/", true, false, false, NULL},
1785     {"data:foobar", false, false, "http:host", true, false, false, NULL},
1786       // Invalid schemes should be treated as relative.
1787     {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
1788     {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
1789     {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
1790     {"data:asdf", false, false, ":foo", false, false, false, NULL},
1791       // We should treat semicolons like any other character in URL resolving
1792     {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
1793     {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
1794     {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
1795       // Relative URLs can also be written as "//foo/bar" which is relative to
1796       // the scheme. In this case, it would take the old scheme, so for http
1797       // the example would resolve to "http://foo/bar".
1798     {"http://host/a", true, false, "//another", true, true, true, "http://another/"},
1799     {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
1800     {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
1801     {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
1802     {"http://host/a", true, false, "//", true, true, false, "http:"},
1803       // IE will also allow one or the other to be a backslash to get the same
1804       // behavior.
1805     {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
1806     {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
1807 #ifdef WIN32
1808       // Resolving against Windows file base URLs.
1809     {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
1810     {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
1811     {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
1812     {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
1813       // But two backslashes on Windows should be UNC so should be treated
1814       // as absolute.
1815     {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
1816       // IE doesn't support drive specs starting with two slashes. It fails
1817       // immediately and doesn't even try to load. We fix it up to either
1818       // an absolute path or UNC depending on what it looks like.
1819     {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
1820     {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
1821       // Windows drive specs should be allowed and treated as absolute.
1822     {"file:///C:/foo", true, true, "c:", true, false, false, NULL},
1823     {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
1824     {"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
1825       // Relative paths with drive letters should be allowed when the base is
1826       // also a file.
1827     {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
1828       // Treat absolute paths as being off of the drive.
1829     {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
1830     {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
1831     {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
1832       // On Windows, two slashes without a drive letter when the base is a file
1833       // means that the path is UNC.
1834     {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
1835     {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
1836 #else
1837       // On Unix we fall back to relative behavior since there's nothing else
1838       // reasonable to do.
1839     {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
1840 #endif
1841       // Even on Windows, we don't allow relative drive specs when the base
1842       // is not file.
1843     {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
1844     {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
1845   };
1846 
1847   for (size_t i = 0; i < ARRAYSIZE(rel_cases); i++) {
1848     const RelativeCase& cur_case = rel_cases[i];
1849 
1850     url_parse::Parsed parsed;
1851     int base_len = static_cast<int>(strlen(cur_case.base));
1852     if (cur_case.is_base_file)
1853       url_parse::ParseFileURL(cur_case.base, base_len, &parsed);
1854     else if (cur_case.is_base_hier)
1855       url_parse::ParseStandardURL(cur_case.base, base_len, &parsed);
1856     else
1857       url_parse::ParsePathURL(cur_case.base, base_len, &parsed);
1858 
1859     // First see if it is relative.
1860     int test_len = static_cast<int>(strlen(cur_case.test));
1861     bool is_relative;
1862     url_parse::Component relative_component;
1863     bool succeed_is_rel = url_canon::IsRelativeURL(
1864         cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
1865         &is_relative, &relative_component);
1866 
1867     EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
1868         "succeed is rel failure on " << cur_case.test;
1869     EXPECT_EQ(cur_case.is_rel, is_relative) <<
1870         "is rel failure on " << cur_case.test;
1871     // Now resolve it.
1872     if (succeed_is_rel && is_relative && cur_case.is_rel) {
1873       std::string resolved;
1874       url_canon::StdStringCanonOutput output(&resolved);
1875       url_parse::Parsed resolved_parsed;
1876 
1877       bool succeed_resolve = url_canon::ResolveRelativeURL(
1878           cur_case.base, parsed, cur_case.is_base_file,
1879           cur_case.test, relative_component, NULL, &output, &resolved_parsed);
1880       output.Complete();
1881 
1882       EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
1883       EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
1884 
1885       // Verify that the output parsed structure is the same as parsing a
1886       // the URL freshly.
1887       url_parse::Parsed ref_parsed;
1888       int resolved_len = static_cast<int>(resolved.size());
1889       if (cur_case.is_base_file)
1890         url_parse::ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
1891       else if (cur_case.is_base_hier)
1892         url_parse::ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed);
1893       else
1894         url_parse::ParsePathURL(resolved.c_str(), resolved_len, &ref_parsed);
1895       EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
1896     }
1897   }
1898 }
1899 
1900 // It used to be when we did a replacement with a long buffer of UTF-16
1901 // characters, we would get invalid data in the URL. This is because the buffer
1902 // it used to hold the UTF-8 data was resized, while some pointers were still
1903 // kept to the old buffer that was removed.
TEST(URLCanonTest,ReplacementOverflow)1904 TEST(URLCanonTest, ReplacementOverflow) {
1905   const char src[] = "file:///C:/foo/bar";
1906   int src_len = static_cast<int>(strlen(src));
1907   url_parse::Parsed parsed;
1908   url_parse::ParseFileURL(src, src_len, &parsed);
1909 
1910   // Override two components, the path with something short, and the query with
1911   // sonething long enough to trigger the bug.
1912   url_canon::Replacements<char16> repl;
1913   string16 new_query;
1914   for (int i = 0; i < 4800; i++)
1915     new_query.push_back('a');
1916 
1917   string16 new_path(WStringToUTF16(L"/foo"));
1918   repl.SetPath(new_path.c_str(), url_parse::Component(0, 4));
1919   repl.SetQuery(new_query.c_str(),
1920                 url_parse::Component(0, static_cast<int>(new_query.length())));
1921 
1922   // Call ReplaceComponents on the string. It doesn't matter if we call it for
1923   // standard URLs, file URLs, etc, since they will go to the same replacement
1924   // function that was buggy.
1925   url_parse::Parsed repl_parsed;
1926   std::string repl_str;
1927   url_canon::StdStringCanonOutput repl_output(&repl_str);
1928   url_canon::ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
1929   repl_output.Complete();
1930 
1931   // Generate the expected string and check.
1932   std::string expected("file:///foo?");
1933   for (size_t i = 0; i < new_query.length(); i++)
1934     expected.push_back('a');
1935   EXPECT_TRUE(expected == repl_str);
1936 }
1937