Home | History | Annotate | Download | only in src
      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 "base/basictypes.h"
     31 #include "googleurl/src/url_parse.h"
     32 #include "testing/gtest/include/gtest/gtest.h"
     33 
     34 // Some implementations of base/basictypes.h may define ARRAYSIZE.
     35 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
     36 // which is in our version of basictypes.h.
     37 #ifndef ARRAYSIZE
     38 #define ARRAYSIZE ARRAYSIZE_UNSAFE
     39 #endif
     40 
     41 // Interesting IE file:isms...
     42 //
     43 //  file:/foo/bar              file:///foo/bar
     44 //      The result here seems totally invalid!?!? This isn't UNC.
     45 //
     46 //  file:/
     47 //  file:// or any other number of slashes
     48 //      IE6 doesn't do anything at all if you click on this link. No error:
     49 //      nothing. IE6's history system seems to always color this link, so I'm
     50 //      guessing that it maps internally to the empty URL.
     51 //
     52 //  C:\                        file:///C:/
     53 //  /                          file:///C:/
     54 //  /foo                       file:///C:/foo
     55 //      Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
     56 //      but is weird to think about on Windows.
     57 //
     58 //  file:foo/                  file:foo/  (invalid?!?!?)
     59 //  file:/foo/                 file:///foo/  (invalid?!?!?)
     60 //  file://foo/                file://foo/   (UNC to server "foo")
     61 //  file:///foo/               file:///foo/  (invalid)
     62 //  file:////foo/              file://foo/   (UNC to server "foo")
     63 //      Any more than four slashes is also treated as UNC.
     64 //
     65 //  file:C:/                   file://C:/
     66 //  file:/C:/                  file://C:/
     67 //      The number of slashes after "file:" don't matter if the thing following
     68 //      it looks like an absolute drive path. Also, slashes and backslashes are
     69 //      equally valid here.
     70 
     71 namespace {
     72 
     73 // Used for regular URL parse cases.
     74 struct URLParseCase {
     75   const char* input;
     76 
     77   const char* scheme;
     78   const char* username;
     79   const char* password;
     80   const char* host;
     81   int port;
     82   const char* path;
     83   const char* query;
     84   const char* ref;
     85 };
     86 
     87 // Simpler version of URLParseCase for testing path URLs.
     88 struct PathURLParseCase {
     89   const char* input;
     90 
     91   const char* scheme;
     92   const char* path;
     93 };
     94 
     95 // Simpler version of URLParseCase for testing mailto URLs.
     96 struct MailtoURLParseCase {
     97   const char* input;
     98 
     99   const char* scheme;
    100   const char* path;
    101   const char* query;
    102 };
    103 
    104 
    105 bool ComponentMatches(const char* input,
    106                       const char* reference,
    107                       const url_parse::Component& component) {
    108   // If the component is nonexistant (length == -1), it should begin at 0.
    109   EXPECT_TRUE(component.len >= 0 || component.len == -1);
    110 
    111   // Begin should be valid.
    112   EXPECT_LE(0, component.begin);
    113 
    114   // A NULL reference means the component should be nonexistant.
    115   if (!reference)
    116     return component.len == -1;
    117   if (component.len < 0)
    118     return false;  // Reference is not NULL but we don't have anything
    119 
    120   if (strlen(reference) != static_cast<size_t>(component.len))
    121     return false;  // Lengths don't match
    122 
    123   // Now check the actual characters.
    124   return strncmp(reference, &input[component.begin], component.len) == 0;
    125 }
    126 
    127 void ExpectInvalidComponent(const url_parse::Component& component) {
    128   EXPECT_EQ(0, component.begin);
    129   EXPECT_EQ(-1, component.len);
    130 }
    131 
    132 }  // namespace
    133 
    134 // Parsed ----------------------------------------------------------------------
    135 
    136 TEST(URLParser, Length) {
    137   const char* length_cases[] = {
    138       // One with everything in it.
    139     "http://user:pass@host:99/foo?bar#baz",
    140       // One with nothing in it.
    141     "",
    142       // Working backwards, let's start taking off stuff from the full one.
    143     "http://user:pass@host:99/foo?bar#",
    144     "http://user:pass@host:99/foo?bar",
    145     "http://user:pass@host:99/foo?",
    146     "http://user:pass@host:99/foo",
    147     "http://user:pass@host:99/",
    148     "http://user:pass@host:99",
    149     "http://user:pass@host:",
    150     "http://user:pass@host",
    151     "http://host",
    152     "http://user@",
    153     "http:",
    154   };
    155   for (size_t i = 0; i < arraysize(length_cases); i++) {
    156     int true_length = static_cast<int>(strlen(length_cases[i]));
    157 
    158     url_parse::Parsed parsed;
    159     url_parse::ParseStandardURL(length_cases[i], true_length, &parsed);
    160 
    161     EXPECT_EQ(true_length, parsed.Length());
    162   }
    163 }
    164 
    165 TEST(URLParser, CountCharactersBefore) {
    166   using namespace url_parse;
    167   struct CountCase {
    168     const char* url;
    169     Parsed::ComponentType component;
    170     bool include_delimiter;
    171     int expected_count;
    172   } count_cases[] = {
    173       // Test each possibility in the case where all components are present.
    174 //    0         1         2
    175 //    0123456789012345678901
    176     {"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
    177     {"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
    178     {"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
    179     {"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
    180     {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
    181     {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
    182     {"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
    183     {"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
    184     {"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
    185     {"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
    186     {"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
    187     {"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
    188     {"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
    189     {"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
    190     {"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
    191     {"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
    192       // Now test when the requested component is missing.
    193     {"http://u:p@h:8/p?", Parsed::REF, true, 17},
    194     {"http://u:p@h:8/p?q", Parsed::REF, true, 18},
    195     {"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
    196     {"http://u:p@h:8#r", Parsed::PATH, true, 14},
    197     {"http://u:p@h/", Parsed::PORT, true, 12},
    198     {"http://u:p@/", Parsed::HOST, true, 11},
    199       // This case is a little weird. It will report that the password would
    200       // start where the host begins. This is arguably correct, although you
    201       // could also argue that it should start at the '@' sign. Doing it
    202       // starting with the '@' sign is actually harder, so we don't bother.
    203     {"http://u@h/", Parsed::PASSWORD, true, 9},
    204     {"http://h/", Parsed::USERNAME, true, 7},
    205     {"http:", Parsed::USERNAME, true, 5},
    206     {"", Parsed::SCHEME, true, 0},
    207       // Make sure a random component still works when there's nothing there.
    208     {"", Parsed::REF, true, 0},
    209       // File URLs are special with no host, so we test those.
    210     {"file:///c:/foo", Parsed::USERNAME, true, 7},
    211     {"file:///c:/foo", Parsed::PASSWORD, true, 7},
    212     {"file:///c:/foo", Parsed::HOST, true, 7},
    213     {"file:///c:/foo", Parsed::PATH, true, 7},
    214   };
    215   for (size_t i = 0; i < ARRAYSIZE(count_cases); i++) {
    216     int length = static_cast<int>(strlen(count_cases[i].url));
    217 
    218     // Simple test to distinguish file and standard URLs.
    219     url_parse::Parsed parsed;
    220     if (length > 0 && count_cases[i].url[0] == 'f')
    221       url_parse::ParseFileURL(count_cases[i].url, length, &parsed);
    222     else
    223       url_parse::ParseStandardURL(count_cases[i].url, length, &parsed);
    224 
    225     int chars_before = parsed.CountCharactersBefore(
    226         count_cases[i].component, count_cases[i].include_delimiter);
    227     EXPECT_EQ(count_cases[i].expected_count, chars_before);
    228   }
    229 }
    230 
    231 // Standard --------------------------------------------------------------------
    232 
    233 // Input                               Scheme  Usrname Passwd     Host         Port Path       Query        Ref
    234 // ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
    235 static URLParseCase cases[] = {
    236   // Regular URL with all the parts
    237 {"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass",    "foo",       21, "/bar;par","b",          "c"},
    238 
    239   // Known schemes should lean towards authority identification
    240 {"http:foo.com",                        "http", NULL,  NULL,      "foo.com",    -1, NULL,      NULL,        NULL},
    241 
    242   // Spaces!
    243 {"\t   :foo.com   \n",                  "",     NULL,  NULL,      "foo.com",    -1, NULL,      NULL,        NULL},
    244 {" foo.com  ",                          NULL,   NULL,  NULL,      "foo.com",    -1, NULL,      NULL,        NULL},
    245 {"a:\t foo.com",                        "a",    NULL,  NULL,      "\t foo.com", -1, NULL,      NULL,        NULL},
    246 {"http://f:21/ b ? d # e ",             "http", NULL,  NULL,      "f",          21, "/ b ",    " d ",       " e"},
    247 
    248   // Invalid port numbers should be identified and turned into -2, empty port
    249   // numbers should be -1. Spaces aren't allowed in port numbers
    250 {"http://f:/c",                         "http", NULL,  NULL,      "f",          -1, "/c",      NULL,        NULL},
    251 {"http://f:0/c",                        "http", NULL,  NULL,      "f",           0, "/c",      NULL,        NULL},
    252 {"http://f:00000000000000/c",           "http", NULL,  NULL,      "f",           0, "/c",      NULL,        NULL},
    253 {"http://f:00000000000000000000080/c",  "http", NULL,  NULL,      "f",          80, "/c",      NULL,        NULL},
    254 {"http://f:b/c",                        "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
    255 {"http://f: /c",                        "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
    256 {"http://f:\n/c",                       "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
    257 {"http://f:fifty-two/c",                "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
    258 {"http://f:999999/c",                   "http", NULL,  NULL,      "f",          -2, "/c",      NULL,        NULL},
    259 {"http://f: 21 / b ? d # e ",           "http", NULL,  NULL,      "f",          -2, "/ b ",    " d ",       " e"},
    260 
    261   // Creative URLs missing key elements
    262 {"",                                    NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    263 {"  \t",                                NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    264 {":foo.com/",                           "",     NULL,  NULL,      "foo.com",    -1, "/",       NULL,        NULL},
    265 {":foo.com\\",                          "",     NULL,  NULL,      "foo.com",    -1, "\\",      NULL,        NULL},
    266 {":",                                   "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    267 {":a",                                  "",     NULL,  NULL,      "a",          -1, NULL,      NULL,        NULL},
    268 {":/",                                  "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    269 {":\\",                                 "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    270 {":#",                                  "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        ""},
    271 {"#",                                   NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        ""},
    272 {"#/",                                  NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        "/"},
    273 {"#\\",                                 NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        "\\"},
    274 {"#;?",                                 NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        ";?"},
    275 {"?",                                   NULL,   NULL,  NULL,      NULL,         -1, NULL,      "",          NULL},
    276 {"/",                                   NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    277 {":23",                                 "",     NULL,  NULL,      "23",         -1, NULL,      NULL,        NULL},
    278 {"/:23",                                "/",    NULL,  NULL,      "23",         -1, NULL,      NULL,        NULL},
    279 {"//",                                  NULL,   NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    280 {"::",                                  "",     NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    281 {"::23",                                "",     NULL,  NULL,      NULL,         23, NULL,      NULL,        NULL},
    282 {"foo://",                              "foo",  NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    283 
    284   // Username/passwords and things that look like them
    285 {"http://a:b@c:29/d",                   "http", "a",   "b",       "c",          29, "/d",      NULL,        NULL},
    286 {"http::@c:29",                         "http", "",    "",        "c",          29, NULL,      NULL,        NULL},
    287   // ... "]" in the password field isn't allowed, but we tolerate it here...
    288 {"http://&a:foo(b]c@d:2/",              "http", "&a",  "foo(b]c", "d",           2, "/",       NULL,        NULL},
    289 {"http://::@c@d:2",                     "http", "",    ":@c",     "d",           2, NULL,      NULL,        NULL},
    290 {"http://foo.com:b@d/",                 "http", "foo.com", "b",   "d",          -1, "/",       NULL,        NULL},
    291 
    292 {"http://foo.com/\\@",                  "http", NULL,  NULL,      "foo.com",    -1, "/\\@",    NULL,        NULL},
    293 {"http:\\\\foo.com\\",                  "http", NULL,  NULL,      "foo.com",    -1, "\\",      NULL,        NULL},
    294 {"http:\\\\a\\b:c\\d (at) foo.com\\",        "http", NULL,  NULL,      "a",          -1, "\\b:c\\d (at) foo.com\\", NULL,   NULL},
    295 
    296   // Tolerate different numbers of slashes.
    297 {"foo:/",                               "foo",  NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    298 {"foo:/bar.com/",                       "foo",  NULL,  NULL,      "bar.com",    -1, "/",       NULL,        NULL},
    299 {"foo://///////",                       "foo",  NULL,  NULL,      NULL,         -1, NULL,      NULL,        NULL},
    300 {"foo://///////bar.com/",               "foo",  NULL,  NULL,      "bar.com",    -1, "/",       NULL,        NULL},
    301 {"foo:////://///",                      "foo",  NULL,  NULL,      NULL,         -1, "/////",   NULL,        NULL},
    302 
    303   // Raw file paths on Windows aren't handled by the parser.
    304 {"c:/foo",                              "c",    NULL,  NULL,      "foo",        -1, NULL,      NULL,        NULL},
    305 {"//foo/bar",                           NULL,   NULL,  NULL,      "foo",        -1, "/bar",    NULL,        NULL},
    306 
    307   // Use the first question mark for the query and the ref.
    308 {"http://foo/path;a??e#f#g",            "http", NULL,  NULL,      "foo",        -1, "/path;a", "?e",      "f#g"},
    309 {"http://foo/abcd?efgh?ijkl",           "http", NULL,  NULL,      "foo",        -1, "/abcd",   "efgh?ijkl", NULL},
    310 {"http://foo/abcd#foo?bar",             "http", NULL,  NULL,      "foo",        -1, "/abcd",   NULL,        "foo?bar"},
    311 
    312   // IPv6, check also interesting uses of colons.
    313 {"[61:24:74]:98",                       "[61",  NULL,  NULL,      "24:74]",     98, NULL,      NULL,        NULL},
    314 {"http://[61:27]:98",                   "http", NULL,  NULL,      "[61:27]",    98, NULL,      NULL,        NULL},
    315 {"http:[61:27]/:foo",                   "http", NULL,  NULL,      "[61:27]",    -1, "/:foo",   NULL,        NULL},
    316 {"http://[1::2]:3:4",                   "http", NULL,  NULL,      "[1::2]:3",    4, NULL,      NULL,        NULL},
    317 
    318   // Partially-complete IPv6 literals, and related cases.
    319 {"http://2001::1",                      "http", NULL,  NULL,      "2001:",       1, NULL,      NULL,        NULL},
    320 {"http://[2001::1",                     "http", NULL,  NULL,      "[2001::1",   -1, NULL,      NULL,        NULL},
    321 {"http://2001::1]",                     "http", NULL,  NULL,      "2001::1]",   -1, NULL,      NULL,        NULL},
    322 {"http://2001::1]:80",                  "http", NULL,  NULL,      "2001::1]",   80, NULL,      NULL,        NULL},
    323 {"http://[2001::1]",                    "http", NULL,  NULL,      "[2001::1]",  -1, NULL,      NULL,        NULL},
    324 {"http://[2001::1]:80",                 "http", NULL,  NULL,      "[2001::1]",  80, NULL,      NULL,        NULL},
    325 {"http://[[::]]",                       "http", NULL,  NULL,      "[[::]]",     -1, NULL,      NULL,        NULL},
    326 
    327 };
    328 
    329 TEST(URLParser, Standard) {
    330   // Declared outside for loop to try to catch cases in init() where we forget
    331   // to reset something that is reset by the construtor.
    332   url_parse::Parsed parsed;
    333   for (size_t i = 0; i < arraysize(cases); i++) {
    334     const char* url = cases[i].input;
    335     url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
    336     int port = url_parse::ParsePort(url, parsed.port);
    337 
    338     EXPECT_TRUE(ComponentMatches(url, cases[i].scheme, parsed.scheme));
    339     EXPECT_TRUE(ComponentMatches(url, cases[i].username, parsed.username));
    340     EXPECT_TRUE(ComponentMatches(url, cases[i].password, parsed.password));
    341     EXPECT_TRUE(ComponentMatches(url, cases[i].host, parsed.host));
    342     EXPECT_EQ(cases[i].port, port);
    343     EXPECT_TRUE(ComponentMatches(url, cases[i].path, parsed.path));
    344     EXPECT_TRUE(ComponentMatches(url, cases[i].query, parsed.query));
    345     EXPECT_TRUE(ComponentMatches(url, cases[i].ref, parsed.ref));
    346   }
    347 }
    348 
    349 // PathURL --------------------------------------------------------------------
    350 
    351 // Various incarnations of path URLs.
    352 static PathURLParseCase path_cases[] = {
    353 {"",                                        NULL,          NULL},
    354 {":",                                       "",            NULL},
    355 {":/",                                      "",            "/"},
    356 {"/",                                       NULL,          "/"},
    357 {" This is \\interesting// \t",             NULL,          "This is \\interesting//"},
    358 {"about:",                                  "about",       NULL},
    359 {"about:blank",                             "about",       "blank"},
    360 {"  about: blank ",                         "about",       " blank"},
    361 {"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\");"},
    362 };
    363 
    364 TEST(URLParser, PathURL) {
    365   // Declared outside for loop to try to catch cases in init() where we forget
    366   // to reset something that is reset by the construtor.
    367   url_parse::Parsed parsed;
    368   for (size_t i = 0; i < arraysize(path_cases); i++) {
    369     const char* url = path_cases[i].input;
    370     url_parse::ParsePathURL(url, static_cast<int>(strlen(url)), &parsed);
    371 
    372     EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme));
    373     EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.path));
    374 
    375     // The remaining components are never used for path urls.
    376     ExpectInvalidComponent(parsed.username);
    377     ExpectInvalidComponent(parsed.password);
    378     ExpectInvalidComponent(parsed.host);
    379     ExpectInvalidComponent(parsed.port);
    380     ExpectInvalidComponent(parsed.query);
    381     ExpectInvalidComponent(parsed.ref);
    382   }
    383 }
    384 
    385 #ifdef WIN32
    386 
    387 // WindowsFile ----------------------------------------------------------------
    388 
    389 // Various incarnations of file URLs. These are for Windows only.
    390 static URLParseCase file_cases[] = {
    391 {"file:server",              "file", NULL, NULL, "server", -1, NULL,          NULL, NULL},
    392 {"  file: server  \t",       "file", NULL, NULL, " server",-1, NULL,          NULL, NULL},
    393 {"FiLe:c|",                  "FiLe", NULL, NULL, NULL,     -1, "c|",          NULL, NULL},
    394 {"FILE:/\\\\/server/file",   "FILE", NULL, NULL, "server", -1, "/file",       NULL, NULL},
    395 {"file://server/",           "file", NULL, NULL, "server", -1, "/",           NULL, NULL},
    396 {"file://localhost/c:/",     "file", NULL, NULL, NULL,     -1, "/c:/",        NULL, NULL},
    397 {"file://127.0.0.1/c|\\",    "file", NULL, NULL, NULL,     -1, "/c|\\",       NULL, NULL},
    398 {"file:/",                   "file", NULL, NULL, NULL,     -1, NULL,          NULL, NULL},
    399 {"file:",                    "file", NULL, NULL, NULL,     -1, NULL,          NULL, NULL},
    400   // If there is a Windows drive letter, treat any number of slashes as the
    401   // path part.
    402 {"file:c:\\fo\\b",           "file", NULL, NULL, NULL,     -1, "c:\\fo\\b",   NULL, NULL},
    403 {"file:/c:\\foo/bar",        "file", NULL, NULL, NULL,     -1, "/c:\\foo/bar",NULL, NULL},
    404 {"file://c:/f\\b",           "file", NULL, NULL, NULL,     -1, "/c:/f\\b",    NULL, NULL},
    405 {"file:///C:/foo",           "file", NULL, NULL, NULL,     -1, "/C:/foo",     NULL, NULL},
    406 {"file://///\\/\\/c:\\f\\b", "file", NULL, NULL, NULL,     -1, "/c:\\f\\b",   NULL, NULL},
    407   // If there is not a drive letter, we should treat is as UNC EXCEPT for
    408   // three slashes, which we treat as a Unix style path.
    409 {"file:server/file",         "file", NULL, NULL, "server", -1, "/file",       NULL, NULL},
    410 {"file:/server/file",        "file", NULL, NULL, "server", -1, "/file",       NULL, NULL},
    411 {"file://server/file",       "file", NULL, NULL, "server", -1, "/file",       NULL, NULL},
    412 {"file:///server/file",      "file", NULL, NULL, NULL,     -1, "/server/file",NULL, NULL},
    413 {"file://\\server/file",     "file", NULL, NULL, NULL,     -1, "\\server/file",NULL, NULL},
    414 {"file:////server/file",     "file", NULL, NULL, "server", -1, "/file",       NULL, NULL},
    415   // Queries and refs are valid for file URLs as well.
    416 {"file:///C:/foo.html?#",   "file", NULL, NULL,  NULL,     -1, "/C:/foo.html",  "",   ""},
    417 {"file:///C:/foo.html?query=yes#ref", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "query=yes", "ref"},
    418 };
    419 
    420 TEST(URLParser, WindowsFile) {
    421   // Declared outside for loop to try to catch cases in init() where we forget
    422   // to reset something that is reset by the construtor.
    423   url_parse::Parsed parsed;
    424   for (int i = 0; i < arraysize(file_cases); i++) {
    425     const char* url = file_cases[i].input;
    426     url_parse::ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
    427     int port = url_parse::ParsePort(url, parsed.port);
    428 
    429     EXPECT_TRUE(ComponentMatches(url, file_cases[i].scheme, parsed.scheme));
    430     EXPECT_TRUE(ComponentMatches(url, file_cases[i].username, parsed.username));
    431     EXPECT_TRUE(ComponentMatches(url, file_cases[i].password, parsed.password));
    432     EXPECT_TRUE(ComponentMatches(url, file_cases[i].host, parsed.host));
    433     EXPECT_EQ(file_cases[i].port, port);
    434     EXPECT_TRUE(ComponentMatches(url, file_cases[i].path, parsed.path));
    435     EXPECT_TRUE(ComponentMatches(url, file_cases[i].query, parsed.query));
    436     EXPECT_TRUE(ComponentMatches(url, file_cases[i].ref, parsed.ref));
    437   }
    438 }
    439 
    440 #endif  // WIN32
    441 
    442 TEST(URLParser, ExtractFileName) {
    443   struct FileCase {
    444     const char* input;
    445     const char* expected;
    446   } file_cases[] = {
    447     {"http://www.google.com", NULL},
    448     {"http://www.google.com/", ""},
    449     {"http://www.google.com/search", "search"},
    450     {"http://www.google.com/search/", ""},
    451     {"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
    452     {"http://www.google.com/foo/bar.html#ref", "bar.html"},
    453     {"http://www.google.com/search/;param", ""},
    454     {"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
    455     {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html;foo"},
    456     {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
    457   };
    458 
    459   for (size_t i = 0; i < ARRAYSIZE(file_cases); i++) {
    460     const char* url = file_cases[i].input;
    461     int len = static_cast<int>(strlen(url));
    462 
    463     url_parse::Parsed parsed;
    464     url_parse::ParseStandardURL(url, len, &parsed);
    465 
    466     url_parse::Component file_name;
    467     url_parse::ExtractFileName(url, parsed.path, &file_name);
    468 
    469     EXPECT_TRUE(ComponentMatches(url, file_cases[i].expected, file_name));
    470   }
    471 }
    472 
    473 // Returns true if the parameter with index |parameter| in the given URL's
    474 // query string. The expected key can be NULL to indicate no such key index
    475 // should exist. The parameter number is 1-based.
    476 static bool NthParameterIs(const char* url,
    477                            int parameter,
    478                            const char* expected_key,
    479                            const char* expected_value) {
    480   url_parse::Parsed parsed;
    481   url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
    482 
    483   url_parse::Component query = parsed.query;
    484 
    485   for (int i = 1; i <= parameter; i++) {
    486     url_parse::Component key, value;
    487     if (!url_parse::ExtractQueryKeyValue(url, &query, &key, &value)) {
    488       if (parameter >= i && !expected_key)
    489         return true;  // Expected nonexistant key, got one.
    490       return false;  // Not enough keys.
    491     }
    492 
    493     if (i == parameter) {
    494       if (!expected_key)
    495         return false;
    496 
    497       if (strncmp(&url[key.begin], expected_key, key.len) != 0)
    498         return false;
    499       if (strncmp(&url[value.begin], expected_value, value.len) != 0)
    500         return false;
    501       return true;
    502     }
    503   }
    504   return expected_key == NULL;  // We didn't find that many parameters.
    505 }
    506 
    507 TEST(URLParser, ExtractQueryKeyValue) {
    508   EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, NULL, NULL));
    509 
    510   // Basic case.
    511   char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
    512   EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
    513   EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
    514   EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
    515   EXPECT_TRUE(NthParameterIs(a, 4, NULL, NULL));
    516 
    517   // Empty param at the end.
    518   char b[] = "http://www.google.com?foo=bar&";
    519   EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
    520   EXPECT_TRUE(NthParameterIs(b, 2, NULL, NULL));
    521 
    522   // Empty param at the beginning.
    523   char c[] = "http://www.google.com?&foo=bar";
    524   EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
    525   EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
    526   EXPECT_TRUE(NthParameterIs(c, 3, NULL, NULL));
    527 
    528   // Empty key with value.
    529   char d[] = "http://www.google.com?=foo";
    530   EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
    531   EXPECT_TRUE(NthParameterIs(d, 2, NULL, NULL));
    532 
    533   // Empty value with key.
    534   char e[] = "http://www.google.com?foo=";
    535   EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
    536   EXPECT_TRUE(NthParameterIs(e, 2, NULL, NULL));
    537 
    538   // Empty key and values.
    539   char f[] = "http://www.google.com?&&==&=";
    540   EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
    541   EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
    542   EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
    543   EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
    544   EXPECT_TRUE(NthParameterIs(f, 5, NULL, NULL));
    545 }
    546 
    547 // MailtoURL --------------------------------------------------------------------
    548 
    549 static MailtoURLParseCase mailto_cases[] = {
    550 //|input                       |scheme   |path               |query
    551 {"mailto:foo (at) gmail.com",        "mailto", "foo (at) gmail.com",    NULL},
    552 {"  mailto: to  \t",            "mailto", " to",              NULL},
    553 {"mailto:addr1%2C%20addr2 ",    "mailto", "addr1%2C%20addr2", NULL},
    554 {"Mailto:addr1, addr2 ",        "Mailto", "addr1, addr2",     NULL},
    555 {"mailto:addr1:addr2 ",         "mailto", "addr1:addr2",      NULL},
    556 {"mailto:?to=addr1,addr2",      "mailto", NULL,               "to=addr1,addr2"},
    557 {"mailto:?to=addr1%2C%20addr2", "mailto", NULL,               "to=addr1%2C%20addr2"},
    558 {"mailto:addr1?to=addr2",       "mailto", "addr1",            "to=addr2"},
    559 {"mailto:?body=#foobar#",       "mailto", NULL,               "body=#foobar#",},
    560 {"mailto:#?body=#foobar#",      "mailto", "#",                "body=#foobar#"},
    561 };
    562 
    563 TEST(URLParser, MailtoUrl) {
    564   // Declared outside for loop to try to catch cases in init() where we forget
    565   // to reset something that is reset by the construtor.
    566   url_parse::Parsed parsed;
    567   for (size_t i = 0; i < arraysize(mailto_cases); ++i) {
    568     const char* url = mailto_cases[i].input;
    569     url_parse::ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
    570     int port = url_parse::ParsePort(url, parsed.port);
    571 
    572     EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].scheme, parsed.scheme));
    573     EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].path, parsed.path));
    574     EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].query, parsed.query));
    575     EXPECT_EQ(url_parse::PORT_UNSPECIFIED, port);
    576 
    577     // The remaining components are never used for mailto urls.
    578     ExpectInvalidComponent(parsed.username);
    579     ExpectInvalidComponent(parsed.password);
    580     ExpectInvalidComponent(parsed.port);
    581     ExpectInvalidComponent(parsed.ref);
    582   }
    583 }
    584