blob: b80e865661a44f1fcebdca18adb931a925a6b7c7 [file] [log] [blame]
[email protected]51bcc5d2013-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.
[email protected]e7bba5f82013-04-10 20:10:524
tfarina018de6e2015-05-26 17:41:205#include "url/third_party/mozilla/url_parse.h"
[email protected]ecf0d742013-04-15 01:22:296
avic0c60312015-12-21 21:03:507#include <stddef.h>
8
viettrungluu4b6915862014-10-16 03:42:499#include "base/macros.h"
[email protected]e7bba5f82013-04-10 20:10:5210#include "testing/gtest/include/gtest/gtest.h"
tfarina018de6e2015-05-26 17:41:2011#include "url/third_party/mozilla/url_parse.h"
[email protected]e7bba5f82013-04-10 20:10:5212
[email protected]e7bba5f82013-04-10 20:10:5213// 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
[email protected]0318f922014-04-22 00:09:2343namespace url {
[email protected]e7bba5f82013-04-10 20:10:5244namespace {
45
46// Used for regular URL parse cases.
47struct 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.
61struct 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.
69struct 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.
78struct 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
92bool ComponentMatches(const char* input,
93 const char* reference,
[email protected]0318f922014-04-22 00:09:2394 const Component& component) {
qyearsley2bc727d2015-08-14 20:17:1595 // If the component is nonexistent (length == -1), it should begin at 0.
[email protected]e7bba5f82013-04-10 20:10:5296 EXPECT_TRUE(component.len >= 0 || component.len == -1);
97
98 // Begin should be valid.
99 EXPECT_LE(0, component.begin);
100
qyearsley2bc727d2015-08-14 20:17:15101 // A NULL reference means the component should be nonexistent.
[email protected]e7bba5f82013-04-10 20:10:52102 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
[email protected]0318f922014-04-22 00:09:23114void ExpectInvalidComponent(const Component& component) {
[email protected]e7bba5f82013-04-10 20:10:52115 EXPECT_EQ(0, component.begin);
116 EXPECT_EQ(-1, component.len);
117}
118
[email protected]e7bba5f82013-04-10 20:10:52119// Parsed ----------------------------------------------------------------------
120
121TEST(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 < arraysize(length_cases); i++) {
141 int true_length = static_cast<int>(strlen(length_cases[i]));
142
[email protected]0318f922014-04-22 00:09:23143 Parsed parsed;
144 ParseStandardURL(length_cases[i], true_length, &parsed);
[email protected]e7bba5f82013-04-10 20:10:52145
146 EXPECT_EQ(true_length, parsed.Length());
147 }
148}
149
150TEST(URLParser, CountCharactersBefore) {
[email protected]e7bba5f82013-04-10 20:10:52151 struct CountCase {
152 const char* url;
153 Parsed::ComponentType component;
154 bool include_delimiter;
155 int expected_count;
156 } count_cases[] = {
[email protected]ecf0d742013-04-15 01:22:29157 // Test each possibility in the case where all components are present.
158 // 0 1 2
159 // 0123456789012345678901
[email protected]e7bba5f82013-04-10 20:10:52160 {"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 };
viettrungluu4b6915862014-10-16 03:42:49199 for (size_t i = 0; i < arraysize(count_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52200 int length = static_cast<int>(strlen(count_cases[i].url));
201
202 // Simple test to distinguish file and standard URLs.
[email protected]0318f922014-04-22 00:09:23203 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:52204 if (length > 0 && count_cases[i].url[0] == 'f')
[email protected]0318f922014-04-22 00:09:23205 ParseFileURL(count_cases[i].url, length, &parsed);
[email protected]e7bba5f82013-04-10 20:10:52206 else
[email protected]0318f922014-04-22 00:09:23207 ParseStandardURL(count_cases[i].url, length, &parsed);
[email protected]e7bba5f82013-04-10 20:10:52208
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// ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
219static 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\\[email protected]\\", "http", NULL, NULL, "a", -1, "\\b:c\\[email protected]\\", 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
313TEST(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.
[email protected]0318f922014-04-22 00:09:23316 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:52317 for (size_t i = 0; i < arraysize(cases); i++) {
318 const char* url = cases[i].input;
[email protected]0318f922014-04-22 00:09:23319 ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
320 int port = ParsePort(url, parsed.port);
[email protected]e7bba5f82013-04-10 20:10:52321
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.
336static PathURLParseCase path_cases[] = {
337{"", NULL, NULL},
338{":", "", NULL},
339{":/", "", "/"},
340{"/", NULL, "/"},
[email protected]369e84f72013-11-23 01:53:52341{" This is \\interesting// \t", NULL, "This is \\interesting// \t"},
[email protected]e7bba5f82013-04-10 20:10:52342{"about:", "about", NULL},
343{"about:blank", "about", "blank"},
[email protected]369e84f72013-11-23 01:53:52344{" about: blank ", "about", " blank "},
345{"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\"); "},
[email protected]e7bba5f82013-04-10 20:10:52346};
347
348TEST(URLParser, PathURL) {
349 // Declared outside for loop to try to catch cases in init() where we forget
qyearsley2bc727d2015-08-14 20:17:15350 // to reset something that is reset by the constructor.
[email protected]0318f922014-04-22 00:09:23351 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:52352 for (size_t i = 0; i < arraysize(path_cases); i++) {
353 const char* url = path_cases[i].input;
[email protected]0318f922014-04-22 00:09:23354 ParsePathURL(url, static_cast<int>(strlen(url)), false, &parsed);
[email protected]e7bba5f82013-04-10 20:10:52355
[email protected]369e84f72013-11-23 01:53:52356 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;
[email protected]e7bba5f82013-04-10 20:10:52360
qyearsley2bc727d2015-08-14 20:17:15361 // The remaining components are never used for path URLs.
[email protected]e7bba5f82013-04-10 20:10:52362 ExpectInvalidComponent(parsed.username);
363 ExpectInvalidComponent(parsed.password);
364 ExpectInvalidComponent(parsed.host);
365 ExpectInvalidComponent(parsed.port);
[email protected]e7bba5f82013-04-10 20:10:52366 }
367}
368
[email protected]598c8382013-09-18 22:55:31369// Various incarnations of file URLs.
[email protected]f80e6772013-09-18 20:55:09370static URLParseCase file_cases[] = {
[email protected]598c8382013-09-18 22:55:31371#ifdef WIN32
[email protected]e7bba5f82013-04-10 20:10:52372{"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"},
[email protected]598c8382013-09-18 22:55:31399#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
[email protected]e7bba5f82013-04-10 20:10:52445};
446
[email protected]598c8382013-09-18 22:55:31447TEST(URLParser, ParseFileURL) {
[email protected]e7bba5f82013-04-10 20:10:52448 // Declared outside for loop to try to catch cases in init() where we forget
449 // to reset something that is reset by the construtor.
[email protected]0318f922014-04-22 00:09:23450 Parsed parsed;
[email protected]598c8382013-09-18 22:55:31451 for (size_t i = 0; i < arraysize(file_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52452 const char* url = file_cases[i].input;
[email protected]0318f922014-04-22 00:09:23453 ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
454 int port = ParsePort(url, parsed.port);
[email protected]e7bba5f82013-04-10 20:10:52455
[email protected]598c8382013-09-18 22:55:31456 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;
[email protected]e7bba5f82013-04-10 20:10:52486 }
487}
488
[email protected]e7bba5f82013-04-10 20:10:52489
490TEST(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"},
arun87.kumardec80a62014-10-16 05:10:46503 {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html"},
[email protected]e7bba5f82013-04-10 20:10:52504 {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
arun87.kumardec80a62014-10-16 05:10:46505 {"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"},
[email protected]e7bba5f82013-04-10 20:10:52510 };
511
viettrungluu4b6915862014-10-16 03:42:49512 for (size_t i = 0; i < arraysize(file_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52513 const char* url = file_cases[i].input;
514 int len = static_cast<int>(strlen(url));
515
[email protected]0318f922014-04-22 00:09:23516 Parsed parsed;
517 ParseStandardURL(url, len, &parsed);
[email protected]e7bba5f82013-04-10 20:10:52518
[email protected]0318f922014-04-22 00:09:23519 Component file_name;
520 ExtractFileName(url, parsed.path, &file_name);
[email protected]e7bba5f82013-04-10 20:10:52521
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) {
[email protected]0318f922014-04-22 00:09:23533 Parsed parsed;
534 ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
[email protected]e7bba5f82013-04-10 20:10:52535
[email protected]0318f922014-04-22 00:09:23536 Component query = parsed.query;
[email protected]e7bba5f82013-04-10 20:10:52537
538 for (int i = 1; i <= parameter; i++) {
[email protected]0318f922014-04-22 00:09:23539 Component key, value;
540 if (!ExtractQueryKeyValue(url, &query, &key, &value)) {
[email protected]e7bba5f82013-04-10 20:10:52541 if (parameter >= i && !expected_key)
qyearsley2bc727d2015-08-14 20:17:15542 return true; // Expected nonexistent key, got one.
[email protected]e7bba5f82013-04-10 20:10:52543 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:[email protected]", "mailto", "[email protected]", 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
qyearsley2bc727d2015-08-14 20:17:15618 // to reset something that is reset by the constructor.
[email protected]0318f922014-04-22 00:09:23619 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:52620 for (size_t i = 0; i < arraysize(mailto_cases); ++i) {
621 const char* url = mailto_cases[i].input;
[email protected]0318f922014-04-22 00:09:23622 ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
623 int port = ParsePort(url, parsed.port);
[email protected]e7bba5f82013-04-10 20:10:52624
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));
[email protected]0318f922014-04-22 00:09:23628 EXPECT_EQ(PORT_UNSPECIFIED, port);
[email protected]e7bba5f82013-04-10 20:10:52629
qyearsley2bc727d2015-08-14 20:17:15630 // The remaining components are never used for mailto URLs.
[email protected]e7bba5f82013-04-10 20:10:52631 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
qyearsley2bc727d2015-08-14 20:17:15650 // to reset something that is reset by the constructor.
[email protected]0318f922014-04-22 00:09:23651 Parsed parsed;
[email protected]e7bba5f82013-04-10 20:10:52652 for (size_t i = 0; i < arraysize(filesystem_cases); i++) {
653 const FileSystemURLParseCase* parsecase = &filesystem_cases[i];
654 const char* url = parsecase->input;
[email protected]0318f922014-04-22 00:09:23655 ParseFileSystemURL(url, static_cast<int>(strlen(url)), &parsed);
[email protected]e7bba5f82013-04-10 20:10:52656
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));
[email protected]0318f922014-04-22 00:09:23669 int port = ParsePort(url, parsed.inner_parsed()->port);
[email protected]e7bba5f82013-04-10 20:10:52670 EXPECT_EQ(parsecase->inner_port, port);
671
qyearsley2bc727d2015-08-14 20:17:15672 // The remaining components are never used for filesystem URLs.
[email protected]e7bba5f82013-04-10 20:10:52673 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
qyearsley2bc727d2015-08-14 20:17:15681 // The remaining components are never used for filesystem URLs.
[email protected]e7bba5f82013-04-10 20:10:52682 ExpectInvalidComponent(parsed.username);
683 ExpectInvalidComponent(parsed.password);
684 ExpectInvalidComponent(parsed.host);
685 ExpectInvalidComponent(parsed.port);
686 }
687}
688
[email protected]ecf0d742013-04-15 01:22:29689} // namespace
[email protected]0318f922014-04-22 00:09:23690} // namespace url