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