[go: nahoru, domu]

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