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