[go: nahoru, domu]

blob: bbd6e49a54fc7fa5bcfd05321bfa8c1d9c80fade [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2013 The Chromium Authors
tfarina@chromium.org51bcc5d2013-04-24 01:41:372// 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
avic0c60312015-12-21 21:03:505#include <stddef.h>
6
brettw@chromium.orge7bba5f2013-04-10 20:10:527#include "testing/gtest/include/gtest/gtest.h"
tfarina018de6e2015-05-26 17:41:208#include "url/third_party/mozilla/url_parse.h"
brettw@chromium.orge7bba5f2013-04-10 20:10:529
brettw@chromium.orge7bba5f2013-04-10 20:10:5210// Interesting IE file:isms...
11//
12// file:/foo/bar file:///foo/bar
13// The result here seems totally invalid!?!? This isn't UNC.
14//
15// file:/
16// file:// or any other number of slashes
17// IE6 doesn't do anything at all if you click on this link. No error:
18// nothing. IE6's history system seems to always color this link, so I'm
19// guessing that it maps internally to the empty URL.
20//
21// C:\ file:///C:/
22// / file:///C:/
23// /foo file:///C:/foo
24// Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
25// but is weird to think about on Windows.
26//
27// file:foo/ file:foo/ (invalid?!?!?)
28// file:/foo/ file:///foo/ (invalid?!?!?)
29// file://foo/ file://foo/ (UNC to server "foo")
30// file:///foo/ file:///foo/ (invalid)
31// file:////foo/ file://foo/ (UNC to server "foo")
32// Any more than four slashes is also treated as UNC.
33//
34// file:C:/ file://C:/
35// file:/C:/ file://C:/
36// The number of slashes after "file:" don't matter if the thing following
37// it looks like an absolute drive path. Also, slashes and backslashes are
38// equally valid here.
39
vitalybuka@chromium.org0318f922014-04-22 00:09:2340namespace url {
Hayato Itob37956b2023-12-04 05:01:5241
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) {
Tom Sepez2a0390182022-11-15 02:25:3893 // Check that the -1 sentinel is the only allowed negative value.
94 EXPECT_TRUE(component.is_valid() || component.len == -1);
brettw@chromium.orge7bba5f2013-04-10 20:10:5295
96 // Begin should be valid.
97 EXPECT_LE(0, component.begin);
98
qyearsley2bc727d2015-08-14 20:17:1599 // A NULL reference means the component should be nonexistent.
brettw@chromium.orge7bba5f2013-04-10 20:10:52100 if (!reference)
101 return component.len == -1;
Tom Sepez2a0390182022-11-15 02:25:38102 if (!component.is_valid())
brettw@chromium.orge7bba5f2013-04-10 20:10:52103 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
Hayato Itob37956b2023-12-04 05:01:52117void URLParseCaseMatches(const URLParseCase& expected, const Parsed& parsed) {
118 const char* url = expected.input;
119 SCOPED_TRACE(testing::Message()
120 << "url: \"" << url << "\", parsed: " << parsed);
121 int port = ParsePort(url, parsed.port);
122 EXPECT_TRUE(ComponentMatches(url, expected.scheme, parsed.scheme));
123 EXPECT_TRUE(ComponentMatches(url, expected.username, parsed.username));
124 EXPECT_TRUE(ComponentMatches(url, expected.password, parsed.password));
125 EXPECT_TRUE(ComponentMatches(url, expected.host, parsed.host));
126 EXPECT_EQ(expected.port, port);
127 EXPECT_TRUE(ComponentMatches(url, expected.path, parsed.path));
128 EXPECT_TRUE(ComponentMatches(url, expected.query, parsed.query));
129 EXPECT_TRUE(ComponentMatches(url, expected.ref, parsed.ref));
130}
131
brettw@chromium.orge7bba5f2013-04-10 20:10:52132// Parsed ----------------------------------------------------------------------
133
134TEST(URLParser, Length) {
135 const char* length_cases[] = {
136 // One with everything in it.
137 "http://user:pass@host:99/foo?bar#baz",
138 // One with nothing in it.
139 "",
140 // Working backwards, let's start taking off stuff from the full one.
141 "http://user:pass@host:99/foo?bar#",
142 "http://user:pass@host:99/foo?bar",
143 "http://user:pass@host:99/foo?",
144 "http://user:pass@host:99/foo",
145 "http://user:pass@host:99/",
146 "http://user:pass@host:99",
147 "http://user:pass@host:",
148 "http://user:pass@host",
149 "http://host",
150 "http://user@",
151 "http:",
152 };
Hayato Ito67985d682023-10-26 01:11:29153 for (const char* length_case : length_cases) {
154 int true_length = static_cast<int>(strlen(length_case));
brettw@chromium.orge7bba5f2013-04-10 20:10:52155
vitalybuka@chromium.org0318f922014-04-22 00:09:23156 Parsed parsed;
Hayato Ito67985d682023-10-26 01:11:29157 ParseStandardURL(length_case, true_length, &parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52158
159 EXPECT_EQ(true_length, parsed.Length());
160 }
161}
162
163TEST(URLParser, CountCharactersBefore) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52164 struct CountCase {
165 const char* url;
166 Parsed::ComponentType component;
167 bool include_delimiter;
168 int expected_count;
169 } count_cases[] = {
tfarina@chromium.orgecf0d742013-04-15 01:22:29170 // Test each possibility in the case where all components are present.
171 // 0 1 2
172 // 0123456789012345678901
brettw@chromium.orge7bba5f2013-04-10 20:10:52173 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
174 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
175 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
176 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
177 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
178 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
179 {"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
180 {"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
181 {"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
182 {"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
183 {"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
184 {"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
185 {"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
186 {"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
187 {"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
188 {"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
189 // Now test when the requested component is missing.
190 {"http://u:p@h:8/p?", Parsed::REF, true, 17},
191 {"http://u:p@h:8/p?q", Parsed::REF, true, 18},
192 {"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
193 {"http://u:p@h:8#r", Parsed::PATH, true, 14},
194 {"http://u:p@h/", Parsed::PORT, true, 12},
195 {"http://u:p@/", Parsed::HOST, true, 11},
196 // This case is a little weird. It will report that the password would
197 // start where the host begins. This is arguably correct, although you
198 // could also argue that it should start at the '@' sign. Doing it
199 // starting with the '@' sign is actually harder, so we don't bother.
200 {"http://u@h/", Parsed::PASSWORD, true, 9},
201 {"http://h/", Parsed::USERNAME, true, 7},
202 {"http:", Parsed::USERNAME, true, 5},
203 {"", Parsed::SCHEME, true, 0},
204 // Make sure a random component still works when there's nothing there.
205 {"", Parsed::REF, true, 0},
206 // File URLs are special with no host, so we test those.
207 {"file:///c:/foo", Parsed::USERNAME, true, 7},
208 {"file:///c:/foo", Parsed::PASSWORD, true, 7},
209 {"file:///c:/foo", Parsed::HOST, true, 7},
210 {"file:///c:/foo", Parsed::PATH, true, 7},
211 };
Hayato Ito67985d682023-10-26 01:11:29212 for (const auto& count_case : count_cases) {
213 int length = static_cast<int>(strlen(count_case.url));
brettw@chromium.orge7bba5f2013-04-10 20:10:52214
215 // Simple test to distinguish file and standard URLs.
vitalybuka@chromium.org0318f922014-04-22 00:09:23216 Parsed parsed;
Hayato Ito67985d682023-10-26 01:11:29217 if (length > 0 && count_case.url[0] == 'f') {
218 ParseFileURL(count_case.url, length, &parsed);
219 } else {
220 ParseStandardURL(count_case.url, length, &parsed);
221 }
brettw@chromium.orge7bba5f2013-04-10 20:10:52222
223 int chars_before = parsed.CountCharactersBefore(
Hayato Ito67985d682023-10-26 01:11:29224 count_case.component, count_case.include_delimiter);
225 EXPECT_EQ(count_case.expected_count, chars_before);
brettw@chromium.orge7bba5f2013-04-10 20:10:52226 }
227}
228
229// Standard --------------------------------------------------------------------
230
Hayato Ito67985d682023-10-26 01:11:29231// clang-format off
232// Input Scheme Usrname Passwd Host Port Path Query Ref
233// ------------------------------------ ------- -------- ---------- ------------ --- ---------- ------------ -----
brettw@chromium.orge7bba5f2013-04-10 20:10:52234static URLParseCase cases[] = {
235 // Regular URL with all the parts
Hayato Ito67985d682023-10-26 01:11:29236{"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass", "foo", 21, "/bar;par","b", "c"},
brettw@chromium.orge7bba5f2013-04-10 20:10:52237
238 // Known schemes should lean towards authority identification
Hayato Ito67985d682023-10-26 01:11:29239{"http:foo.com", "http", nullptr, nullptr, "foo.com", -1, nullptr, nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52240
241 // Spaces!
Hayato Ito67985d682023-10-26 01:11:29242{"\t :foo.com \n", "", nullptr, nullptr, "foo.com", -1, nullptr, nullptr, nullptr},
243{" foo.com ", nullptr,nullptr, nullptr, "foo.com", -1, nullptr, nullptr, nullptr},
244{"a:\t foo.com", "a", nullptr, nullptr, "\t foo.com", -1, nullptr, nullptr, nullptr},
245{"http://f:21/ b ? d # e ", "http", nullptr, nullptr, "f", 21, "/ b ", " d ", " e"},
brettw@chromium.orge7bba5f2013-04-10 20:10:52246
247 // Invalid port numbers should be identified and turned into -2, empty port
248 // numbers should be -1. Spaces aren't allowed in port numbers
Hayato Ito67985d682023-10-26 01:11:29249{"http://f:/c", "http", nullptr, nullptr, "f", -1, "/c", nullptr, nullptr},
250{"http://f:0/c", "http", nullptr, nullptr, "f", 0, "/c", nullptr, nullptr},
251{"http://f:00000000000000/c", "http", nullptr, nullptr, "f", 0, "/c", nullptr, nullptr},
252{"http://f:00000000000000000000080/c", "http", nullptr, nullptr, "f", 80, "/c", nullptr, nullptr},
253{"http://f:b/c", "http", nullptr, nullptr, "f", -2, "/c", nullptr, nullptr},
254{"http://f: /c", "http", nullptr, nullptr, "f", -2, "/c", nullptr, nullptr},
255{"http://f:\n/c", "http", nullptr, nullptr, "f", -2, "/c", nullptr, nullptr},
256{"http://f:fifty-two/c", "http", nullptr, nullptr, "f", -2, "/c", nullptr, nullptr},
257{"http://f:999999/c", "http", nullptr, nullptr, "f", -2, "/c", nullptr, nullptr},
258{"http://f: 21 / b ? d # e ", "http", nullptr, nullptr, "f", -2, "/ b ", " d ", " e"},
brettw@chromium.orge7bba5f2013-04-10 20:10:52259
260 // Creative URLs missing key elements
Hayato Ito67985d682023-10-26 01:11:29261{"", nullptr,nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
262{" \t", nullptr,nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
263{":foo.com/", "", nullptr, nullptr, "foo.com", -1, "/", nullptr, nullptr},
264{":foo.com\\", "", nullptr, nullptr, "foo.com", -1, "\\", nullptr, nullptr},
265{":", "", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
266{":a", "", nullptr, nullptr, "a", -1, nullptr, nullptr, nullptr},
267{":/", "", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
268{":\\", "", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
269{":#", "", nullptr, nullptr, nullptr, -1, nullptr, nullptr, ""},
270{"#", nullptr,nullptr, nullptr, nullptr, -1, nullptr, nullptr, ""},
271{"#/", nullptr,nullptr, nullptr, nullptr, -1, nullptr, nullptr, "/"},
272{"#\\", nullptr,nullptr, nullptr, nullptr, -1, nullptr, nullptr, "\\"},
273{"#;?", nullptr,nullptr, nullptr, nullptr, -1, nullptr, nullptr, ";?"},
274{"?", nullptr,nullptr, nullptr, nullptr, -1, nullptr, "", nullptr},
275{"/", nullptr,nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
276{":23", "", nullptr, nullptr, "23", -1, nullptr, nullptr, nullptr},
277{"/:23", "/", nullptr, nullptr, "23", -1, nullptr, nullptr, nullptr},
278{"//", nullptr,nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
279{"::", "", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
280{"::23", "", nullptr, nullptr, nullptr, 23, nullptr, nullptr, nullptr},
281{"foo://", "foo", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52282
283 // Username/passwords and things that look like them
Hayato Ito67985d682023-10-26 01:11:29284{"http://a:b@c:29/d", "http", "a", "b", "c", 29, "/d", nullptr, nullptr},
285{"http::@c:29", "http", "", "", "c", 29, nullptr, nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52286 // ... "]" in the password field isn't allowed, but we tolerate it here...
Hayato Ito67985d682023-10-26 01:11:29287{"http://&a:foo(b]c@d:2/", "http", "&a", "foo(b]c", "d", 2, "/", nullptr, nullptr},
288{"http://::@c@d:2", "http", "", ":@c", "d", 2, nullptr, nullptr, nullptr},
289{"http://foo.com:b@d/", "http", "foo.com","b", "d", -1, "/", nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52290
Hayato Ito67985d682023-10-26 01:11:29291{"http://foo.com/\\@", "http", nullptr, nullptr, "foo.com", -1, "/\\@", nullptr, nullptr},
292{"http:\\\\foo.com\\", "http", nullptr, nullptr, "foo.com", -1, "\\", nullptr, nullptr},
293{"http:\\\\a\\b:c\\d@foo.com\\", "http", nullptr, nullptr, "a", -1, "\\b:c\\d@foo.com\\", nullptr,nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52294
295 // Tolerate different numbers of slashes.
Hayato Ito67985d682023-10-26 01:11:29296{"foo:/", "foo", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
297{"foo:/bar.com/", "foo", nullptr, nullptr, "bar.com", -1, "/", nullptr, nullptr},
298{"foo://///////", "foo", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
299{"foo://///////bar.com/", "foo", nullptr, nullptr, "bar.com", -1, "/", nullptr, nullptr},
300{"foo:////://///", "foo", nullptr, nullptr, nullptr, -1, "/////", nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52301
302 // Raw file paths on Windows aren't handled by the parser.
Hayato Ito67985d682023-10-26 01:11:29303{"c:/foo", "c", nullptr, nullptr, "foo", -1, nullptr, nullptr, nullptr},
304{"//foo/bar", nullptr,nullptr, nullptr, "foo", -1, "/bar", nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52305
306 // Use the first question mark for the query and the ref.
Hayato Ito67985d682023-10-26 01:11:29307{"http://foo/path;a??e#f#g", "http", nullptr, nullptr, "foo", -1, "/path;a", "?e", "f#g"},
308{"http://foo/abcd?efgh?ijkl", "http", nullptr, nullptr, "foo", -1, "/abcd", "efgh?ijkl", nullptr},
309{"http://foo/abcd#foo?bar", "http", nullptr, nullptr, "foo", -1, "/abcd", nullptr, "foo?bar"},
brettw@chromium.orge7bba5f2013-04-10 20:10:52310
311 // IPv6, check also interesting uses of colons.
Hayato Ito67985d682023-10-26 01:11:29312{"[61:24:74]:98", "[61", nullptr, nullptr, "24:74]", 98, nullptr, nullptr, nullptr},
313{"http://[61:27]:98", "http", nullptr, nullptr, "[61:27]", 98, nullptr, nullptr, nullptr},
314{"http:[61:27]/:foo", "http", nullptr, nullptr, "[61:27]", -1, "/:foo", nullptr, nullptr},
315{"http://[1::2]:3:4", "http", nullptr, nullptr, "[1::2]:3", 4, nullptr, nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52316
317 // Partially-complete IPv6 literals, and related cases.
Hayato Ito67985d682023-10-26 01:11:29318{"http://2001::1", "http", nullptr, nullptr, "2001:", 1, nullptr, nullptr, nullptr},
319{"http://[2001::1", "http", nullptr, nullptr, "[2001::1", -1, nullptr, nullptr, nullptr},
320{"http://2001::1]", "http", nullptr, nullptr, "2001::1]", -1, nullptr, nullptr, nullptr},
321{"http://2001::1]:80", "http", nullptr, nullptr, "2001::1]", 80, nullptr, nullptr, nullptr},
322{"http://[2001::1]", "http", nullptr, nullptr, "[2001::1]", -1, nullptr, nullptr, nullptr},
323{"http://[2001::1]:80", "http", nullptr, nullptr, "[2001::1]", 80, nullptr, nullptr, nullptr},
324{"http://[[::]]", "http", nullptr, nullptr, "[[::]]", -1, nullptr, nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52325
326};
Hayato Ito67985d682023-10-26 01:11:29327// clang-format on
brettw@chromium.orge7bba5f2013-04-10 20:10:52328
329TEST(URLParser, Standard) {
330 // Declared outside for loop to try to catch cases in init() where we forget
331 // to reset something that is reset by the constructor.
vitalybuka@chromium.org0318f922014-04-22 00:09:23332 Parsed parsed;
Hayato Ito67985d682023-10-26 01:11:29333 for (const auto& i : cases) {
334 const char* url = i.input;
vitalybuka@chromium.org0318f922014-04-22 00:09:23335 ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
Hayato Itob37956b2023-12-04 05:01:52336 URLParseCaseMatches(i, parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52337 }
338}
339
340// PathURL --------------------------------------------------------------------
341
342// Various incarnations of path URLs.
Hayato Ito67985d682023-10-26 01:11:29343// clang-format off
brettw@chromium.orge7bba5f2013-04-10 20:10:52344static PathURLParseCase path_cases[] = {
Hayato Ito67985d682023-10-26 01:11:29345{"", nullptr, nullptr},
346{":", "", nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52347{":/", "", "/"},
Hayato Ito67985d682023-10-26 01:11:29348{"/", nullptr, "/"},
349{" This is \\interesting// \t", nullptr, "This is \\interesting// \t"},
350{"about:", "about", nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52351{"about:blank", "about", "blank"},
joth@chromium.org369e84f72013-11-23 01:53:52352{" about: blank ", "about", " blank "},
353{"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\"); "},
brettw@chromium.orge7bba5f2013-04-10 20:10:52354};
Hayato Ito67985d682023-10-26 01:11:29355// clang-format on
brettw@chromium.orge7bba5f2013-04-10 20:10:52356
357TEST(URLParser, PathURL) {
358 // Declared outside for loop to try to catch cases in init() where we forget
qyearsley2bc727d2015-08-14 20:17:15359 // to reset something that is reset by the constructor.
vitalybuka@chromium.org0318f922014-04-22 00:09:23360 Parsed parsed;
Daniel Cheng47705c22022-02-28 04:14:42361 for (size_t i = 0; i < std::size(path_cases); i++) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52362 const char* url = path_cases[i].input;
vitalybuka@chromium.org0318f922014-04-22 00:09:23363 ParsePathURL(url, static_cast<int>(strlen(url)), false, &parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52364
joth@chromium.org369e84f72013-11-23 01:53:52365 EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme))
366 << i;
367 EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.GetContent()))
368 << i;
brettw@chromium.orge7bba5f2013-04-10 20:10:52369
qyearsley2bc727d2015-08-14 20:17:15370 // The remaining components are never used for path URLs.
brettw@chromium.orge7bba5f2013-04-10 20:10:52371 ExpectInvalidComponent(parsed.username);
372 ExpectInvalidComponent(parsed.password);
373 ExpectInvalidComponent(parsed.host);
374 ExpectInvalidComponent(parsed.port);
brettw@chromium.orge7bba5f2013-04-10 20:10:52375 }
376}
377
tsepez@chromium.org598c8382013-09-18 22:55:31378// Various incarnations of file URLs.
Hayato Ito67985d682023-10-26 01:11:29379// clang-format off
tsepez@chromium.orgf80e6772013-09-18 20:55:09380static URLParseCase file_cases[] = {
tsepez@chromium.org598c8382013-09-18 22:55:31381#ifdef WIN32
Hayato Ito67985d682023-10-26 01:11:29382{"file:server", "file", nullptr, nullptr, "server", -1, nullptr, nullptr, nullptr},
383{" file: server \t", "file", nullptr, nullptr, " server",-1, nullptr, nullptr, nullptr},
384{"FiLe:c|", "FiLe", nullptr, nullptr, nullptr, -1, "c|", nullptr, nullptr},
385{"FILE:/\\\\/server/file", "FILE", nullptr, nullptr, "server", -1, "/file", nullptr, nullptr},
386{"file://server/", "file", nullptr, nullptr, "server", -1, "/", nullptr, nullptr},
387{"file://localhost/c:/", "file", nullptr, nullptr, "localhost", -1, "/c:/", nullptr, nullptr},
388{"file://127.0.0.1/c|\\", "file", nullptr, nullptr, "127.0.0.1", -1, "/c|\\", nullptr, nullptr},
389{"file:/", "file", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
390{"file:", "file", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52391 // If there is a Windows drive letter, treat any number of slashes as the
392 // path part.
Hayato Ito67985d682023-10-26 01:11:29393{"file:c:\\fo\\b", "file", nullptr, nullptr, nullptr, -1, "c:\\fo\\b", nullptr, nullptr},
394{"file:/c:\\foo/bar", "file", nullptr, nullptr, nullptr, -1, "/c:\\foo/bar",nullptr, nullptr},
395{"file://c:/f\\b", "file", nullptr, nullptr, nullptr, -1, "/c:/f\\b", nullptr, nullptr},
396{"file:///C:/foo", "file", nullptr, nullptr, nullptr, -1, "/C:/foo", nullptr, nullptr},
397{"file://///\\/\\/c:\\f\\b", "file", nullptr, nullptr, nullptr, -1, "/c:\\f\\b", nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52398 // If there is not a drive letter, we should treat is as UNC EXCEPT for
399 // three slashes, which we treat as a Unix style path.
Hayato Ito67985d682023-10-26 01:11:29400{"file:server/file", "file", nullptr, nullptr, "server", -1, "/file", nullptr, nullptr},
401{"file:/server/file", "file", nullptr, nullptr, "server", -1, "/file", nullptr, nullptr},
402{"file://server/file", "file", nullptr, nullptr, "server", -1, "/file", nullptr, nullptr},
403{"file:///server/file", "file", nullptr, nullptr, nullptr, -1, "/server/file",nullptr, nullptr},
404{"file://\\server/file", "file", nullptr, nullptr, nullptr, -1, "\\server/file",nullptr, nullptr},
405{"file:////server/file", "file", nullptr, nullptr, "server", -1, "/file", nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52406 // Queries and refs are valid for file URLs as well.
Hayato Ito67985d682023-10-26 01:11:29407{"file:///C:/foo.html?#", "file", nullptr, nullptr, nullptr, -1, "/C:/foo.html", "", ""},
408{"file:///C:/foo.html?query=yes#ref", "file", nullptr, nullptr, nullptr, -1, "/C:/foo.html", "query=yes", "ref"},
tsepez@chromium.org598c8382013-09-18 22:55:31409#else // WIN32
410 // No slashes.
Hayato Ito67985d682023-10-26 01:11:29411 {"file:", "file", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
412 {"file:path", "file", nullptr, nullptr, nullptr, -1, "path", nullptr, nullptr},
413 {"file:path/", "file", nullptr, nullptr, nullptr, -1, "path/", nullptr, nullptr},
414 {"file:path/f.txt", "file", nullptr, nullptr, nullptr, -1, "path/f.txt", nullptr, nullptr},
tsepez@chromium.org598c8382013-09-18 22:55:31415 // One slash.
Hayato Ito67985d682023-10-26 01:11:29416 {"file:/", "file", nullptr, nullptr, nullptr, -1, "/", nullptr, nullptr},
417 {"file:/path", "file", nullptr, nullptr, nullptr, -1, "/path", nullptr, nullptr},
418 {"file:/path/", "file", nullptr, nullptr, nullptr, -1, "/path/", nullptr, nullptr},
419 {"file:/path/f.txt", "file", nullptr, nullptr, nullptr, -1, "/path/f.txt", nullptr, nullptr},
tsepez@chromium.org598c8382013-09-18 22:55:31420 // Two slashes.
Hayato Ito67985d682023-10-26 01:11:29421 {"file://", "file", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
422 {"file://server", "file", nullptr, nullptr, "server", -1, nullptr, nullptr, nullptr},
423 {"file://server/", "file", nullptr, nullptr, "server", -1, "/", nullptr, nullptr},
424 {"file://server/f.txt", "file", nullptr, nullptr, "server", -1, "/f.txt", nullptr, nullptr},
tsepez@chromium.org598c8382013-09-18 22:55:31425 // Three slashes.
Hayato Ito67985d682023-10-26 01:11:29426 {"file:///", "file", nullptr, nullptr, nullptr, -1, "/", nullptr, nullptr},
427 {"file:///path", "file", nullptr, nullptr, nullptr, -1, "/path", nullptr, nullptr},
428 {"file:///path/", "file", nullptr, nullptr, nullptr, -1, "/path/", nullptr, nullptr},
429 {"file:///path/f.txt", "file", nullptr, nullptr, nullptr, -1, "/path/f.txt", nullptr, nullptr},
tsepez@chromium.org598c8382013-09-18 22:55:31430 // More than three slashes.
Hayato Ito67985d682023-10-26 01:11:29431 {"file:////", "file", nullptr, nullptr, nullptr, -1, "/", nullptr, nullptr},
432 {"file:////path", "file", nullptr, nullptr, nullptr, -1, "/path", nullptr, nullptr},
433 {"file:////path/", "file", nullptr, nullptr, nullptr, -1, "/path/", nullptr, nullptr},
434 {"file:////path/f.txt", "file", nullptr, nullptr, nullptr, -1, "/path/f.txt", nullptr, nullptr},
tsepez@chromium.org598c8382013-09-18 22:55:31435 // Schemeless URLs
Hayato Ito67985d682023-10-26 01:11:29436 {"path/f.txt", nullptr,nullptr, nullptr, nullptr, -1, "path/f.txt", nullptr, nullptr},
437 {"path:80/f.txt", "path", nullptr, nullptr, nullptr, -1, "80/f.txt", nullptr, nullptr},
438 {"path/f.txt:80", "path/f.txt",nullptr, nullptr, nullptr,-1,"80", nullptr, nullptr}, // Wrong.
439 {"/path/f.txt", nullptr,nullptr, nullptr, nullptr, -1, "/path/f.txt", nullptr, nullptr},
440 {"/path:80/f.txt", nullptr,nullptr, nullptr, nullptr, -1, "/path:80/f.txt",nullptr, nullptr},
441 {"/path/f.txt:80", nullptr,nullptr, nullptr, nullptr, -1, "/path/f.txt:80",nullptr, nullptr},
442 {"//server/f.txt", nullptr,nullptr, nullptr, "server", -1, "/f.txt", nullptr, nullptr},
443 {"//server:80/f.txt", nullptr,nullptr, nullptr, "server:80",-1, "/f.txt", nullptr, nullptr},
444 {"//server/f.txt:80", nullptr,nullptr, nullptr, "server", -1, "/f.txt:80", nullptr, nullptr},
445 {"///path/f.txt", nullptr,nullptr, nullptr, nullptr, -1, "/path/f.txt", nullptr, nullptr},
446 {"///path:80/f.txt", nullptr,nullptr, nullptr, nullptr, -1, "/path:80/f.txt",nullptr, nullptr},
447 {"///path/f.txt:80", nullptr,nullptr, nullptr, nullptr, -1, "/path/f.txt:80",nullptr, nullptr},
448 {"////path/f.txt", nullptr,nullptr, nullptr, nullptr, -1, "/path/f.txt", nullptr, nullptr},
449 {"////path:80/f.txt", nullptr,nullptr, nullptr, nullptr, -1, "/path:80/f.txt",nullptr, nullptr},
450 {"////path/f.txt:80", nullptr,nullptr, nullptr, nullptr, -1, "/path/f.txt:80",nullptr, nullptr},
tsepez@chromium.org598c8382013-09-18 22:55:31451 // Queries and refs are valid for file URLs as well.
Hayato Ito67985d682023-10-26 01:11:29452 {"file:///foo.html?#", "file", nullptr, nullptr, nullptr, -1, "/foo.html", "", ""},
453 {"file:///foo.html?q=y#ref", "file", nullptr, nullptr, nullptr, -1, "/foo.html", "q=y", "ref"},
tsepez@chromium.org598c8382013-09-18 22:55:31454#endif // WIN32
brettw@chromium.orge7bba5f2013-04-10 20:10:52455};
Hayato Ito67985d682023-10-26 01:11:29456// clang-format on
brettw@chromium.orge7bba5f2013-04-10 20:10:52457
tsepez@chromium.org598c8382013-09-18 22:55:31458TEST(URLParser, ParseFileURL) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52459 // Declared outside for loop to try to catch cases in init() where we forget
460 // to reset something that is reset by the construtor.
vitalybuka@chromium.org0318f922014-04-22 00:09:23461 Parsed parsed;
Hayato Itob37956b2023-12-04 05:01:52462 for (const auto& file_case : file_cases) {
463 ParseFileURL(file_case.input, static_cast<int>(strlen(file_case.input)),
464 &parsed);
465 URLParseCaseMatches(file_case, parsed);
466 EXPECT_FALSE(parsed.has_opaque_path);
brettw@chromium.orge7bba5f2013-04-10 20:10:52467 }
468}
469
brettw@chromium.orge7bba5f2013-04-10 20:10:52470TEST(URLParser, ExtractFileName) {
471 struct FileCase {
472 const char* input;
473 const char* expected;
Peter Kastinga45490c2021-08-17 22:45:40474 } extract_cases[] = {
Keishi Hattori27f19de2020-11-17 05:53:12475 {"http://www.google.com", nullptr},
476 {"http://www.google.com/", ""},
477 {"http://www.google.com/search", "search"},
478 {"http://www.google.com/search/", ""},
479 {"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
480 {"http://www.google.com/foo/bar.html#ref", "bar.html"},
481 {"http://www.google.com/search/;param", ""},
482 {"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
483 {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html"},
484 {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
485 {"http://www.google.com/foo;/bar.html", "bar.html"},
486 {"http://www.google.com/foo;/", ""},
487 {"http://www.google.com/foo;", "foo"},
488 {"http://www.google.com/;", ""},
489 {"http://www.google.com/foo;bar;html", "foo"},
brettw@chromium.orge7bba5f2013-04-10 20:10:52490 };
491
Hayato Ito67985d682023-10-26 01:11:29492 for (const auto& extract_case : extract_cases) {
493 const char* url = extract_case.input;
brettw@chromium.orge7bba5f2013-04-10 20:10:52494 int len = static_cast<int>(strlen(url));
495
vitalybuka@chromium.org0318f922014-04-22 00:09:23496 Parsed parsed;
497 ParseStandardURL(url, len, &parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52498
vitalybuka@chromium.org0318f922014-04-22 00:09:23499 Component file_name;
500 ExtractFileName(url, parsed.path, &file_name);
brettw@chromium.orge7bba5f2013-04-10 20:10:52501
Hayato Ito67985d682023-10-26 01:11:29502 EXPECT_TRUE(ComponentMatches(url, extract_case.expected, file_name));
brettw@chromium.orge7bba5f2013-04-10 20:10:52503 }
504}
505
506// Returns true if the parameter with index |parameter| in the given URL's
507// query string. The expected key can be NULL to indicate no such key index
508// should exist. The parameter number is 1-based.
509static bool NthParameterIs(const char* url,
510 int parameter,
511 const char* expected_key,
512 const char* expected_value) {
vitalybuka@chromium.org0318f922014-04-22 00:09:23513 Parsed parsed;
514 ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52515
vitalybuka@chromium.org0318f922014-04-22 00:09:23516 Component query = parsed.query;
brettw@chromium.orge7bba5f2013-04-10 20:10:52517
518 for (int i = 1; i <= parameter; i++) {
vitalybuka@chromium.org0318f922014-04-22 00:09:23519 Component key, value;
520 if (!ExtractQueryKeyValue(url, &query, &key, &value)) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52521 if (parameter >= i && !expected_key)
qyearsley2bc727d2015-08-14 20:17:15522 return true; // Expected nonexistent key, got one.
brettw@chromium.orge7bba5f2013-04-10 20:10:52523 return false; // Not enough keys.
524 }
525
526 if (i == parameter) {
527 if (!expected_key)
528 return false;
529
530 if (strncmp(&url[key.begin], expected_key, key.len) != 0)
531 return false;
532 if (strncmp(&url[value.begin], expected_value, value.len) != 0)
533 return false;
534 return true;
535 }
536 }
Hayato Ito67985d682023-10-26 01:11:29537 return expected_key == nullptr; // We didn't find that many parameters.
brettw@chromium.orge7bba5f2013-04-10 20:10:52538}
539
540TEST(URLParser, ExtractQueryKeyValue) {
Hayato Ito67985d682023-10-26 01:11:29541 EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, nullptr, nullptr));
brettw@chromium.orge7bba5f2013-04-10 20:10:52542
543 // Basic case.
544 char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
545 EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
546 EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
547 EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
Hayato Ito67985d682023-10-26 01:11:29548 EXPECT_TRUE(NthParameterIs(a, 4, nullptr, nullptr));
brettw@chromium.orge7bba5f2013-04-10 20:10:52549
550 // Empty param at the end.
551 char b[] = "http://www.google.com?foo=bar&";
552 EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
Hayato Ito67985d682023-10-26 01:11:29553 EXPECT_TRUE(NthParameterIs(b, 2, nullptr, nullptr));
brettw@chromium.orge7bba5f2013-04-10 20:10:52554
555 // Empty param at the beginning.
556 char c[] = "http://www.google.com?&foo=bar";
557 EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
558 EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
Hayato Ito67985d682023-10-26 01:11:29559 EXPECT_TRUE(NthParameterIs(c, 3, nullptr, nullptr));
brettw@chromium.orge7bba5f2013-04-10 20:10:52560
561 // Empty key with value.
562 char d[] = "http://www.google.com?=foo";
563 EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
Hayato Ito67985d682023-10-26 01:11:29564 EXPECT_TRUE(NthParameterIs(d, 2, nullptr, nullptr));
brettw@chromium.orge7bba5f2013-04-10 20:10:52565
566 // Empty value with key.
567 char e[] = "http://www.google.com?foo=";
568 EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
Hayato Ito67985d682023-10-26 01:11:29569 EXPECT_TRUE(NthParameterIs(e, 2, nullptr, nullptr));
brettw@chromium.orge7bba5f2013-04-10 20:10:52570
571 // Empty key and values.
572 char f[] = "http://www.google.com?&&==&=";
573 EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
574 EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
575 EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
576 EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
Hayato Ito67985d682023-10-26 01:11:29577 EXPECT_TRUE(NthParameterIs(f, 5, nullptr, nullptr));
brettw@chromium.orge7bba5f2013-04-10 20:10:52578}
579
580// MailtoURL --------------------------------------------------------------------
581
Hayato Ito67985d682023-10-26 01:11:29582// clang-format off
brettw@chromium.orge7bba5f2013-04-10 20:10:52583static MailtoURLParseCase mailto_cases[] = {
584//|input |scheme |path |query
Hayato Ito67985d682023-10-26 01:11:29585{"mailto:foo@gmail.com", "mailto", "foo@gmail.com", nullptr},
586{" mailto: to \t", "mailto", " to", nullptr},
587{"mailto:addr1%2C%20addr2 ", "mailto", "addr1%2C%20addr2", nullptr},
588{"Mailto:addr1, addr2 ", "Mailto", "addr1, addr2", nullptr},
589{"mailto:addr1:addr2 ", "mailto", "addr1:addr2", nullptr},
590{"mailto:?to=addr1,addr2", "mailto", nullptr, "to=addr1,addr2"},
591{"mailto:?to=addr1%2C%20addr2", "mailto", nullptr, "to=addr1%2C%20addr2"},
brettw@chromium.orge7bba5f2013-04-10 20:10:52592{"mailto:addr1?to=addr2", "mailto", "addr1", "to=addr2"},
Hayato Ito67985d682023-10-26 01:11:29593{"mailto:?body=#foobar#", "mailto", nullptr, "body=#foobar#",},
brettw@chromium.orge7bba5f2013-04-10 20:10:52594{"mailto:#?body=#foobar#", "mailto", "#", "body=#foobar#"},
595};
Hayato Ito67985d682023-10-26 01:11:29596// clang-format on
brettw@chromium.orge7bba5f2013-04-10 20:10:52597
598TEST(URLParser, MailtoUrl) {
599 // Declared outside for loop to try to catch cases in init() where we forget
qyearsley2bc727d2015-08-14 20:17:15600 // to reset something that is reset by the constructor.
vitalybuka@chromium.org0318f922014-04-22 00:09:23601 Parsed parsed;
Hayato Ito67985d682023-10-26 01:11:29602 for (const auto& mailto_case : mailto_cases) {
603 const char* url = mailto_case.input;
vitalybuka@chromium.org0318f922014-04-22 00:09:23604 ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
605 int port = ParsePort(url, parsed.port);
brettw@chromium.orge7bba5f2013-04-10 20:10:52606
Hayato Ito67985d682023-10-26 01:11:29607 EXPECT_TRUE(ComponentMatches(url, mailto_case.scheme, parsed.scheme));
608 EXPECT_TRUE(ComponentMatches(url, mailto_case.path, parsed.path));
609 EXPECT_TRUE(ComponentMatches(url, mailto_case.query, parsed.query));
vitalybuka@chromium.org0318f922014-04-22 00:09:23610 EXPECT_EQ(PORT_UNSPECIFIED, port);
Hayato Itob37956b2023-12-04 05:01:52611 EXPECT_FALSE(parsed.has_opaque_path);
brettw@chromium.orge7bba5f2013-04-10 20:10:52612
qyearsley2bc727d2015-08-14 20:17:15613 // The remaining components are never used for mailto URLs.
brettw@chromium.orge7bba5f2013-04-10 20:10:52614 ExpectInvalidComponent(parsed.username);
615 ExpectInvalidComponent(parsed.password);
616 ExpectInvalidComponent(parsed.port);
617 ExpectInvalidComponent(parsed.ref);
618 }
619}
620
621// Various incarnations of filesystem URLs.
622static FileSystemURLParseCase filesystem_cases[] = {
Hayato Ito67985d682023-10-26 01:11:29623 // Regular URL with all the parts
624 {"filesystem:http://user:pass@foo:21/temporary/bar;par?b#c", "http", "user",
625 "pass", "foo", 21, "/temporary", "/bar;par", "b", "c"},
626 {"filesystem:https://foo/persistent/bar;par/", "https", nullptr, nullptr,
627 "foo", -1, "/persistent", "/bar;par/", nullptr, nullptr},
628 {"filesystem:file:///persistent/bar;par/", "file", nullptr, nullptr,
629 nullptr, -1, "/persistent", "/bar;par/", nullptr, nullptr},
630 {"filesystem:file:///persistent/bar;par/?query#ref", "file", nullptr,
631 nullptr, nullptr, -1, "/persistent", "/bar;par/", "query", "ref"},
632 {"filesystem:file:///persistent", "file", nullptr, nullptr, nullptr, -1,
633 "/persistent", "", nullptr, nullptr},
brettw@chromium.orge7bba5f2013-04-10 20:10:52634};
635
636TEST(URLParser, FileSystemURL) {
637 // Declared outside for loop to try to catch cases in init() where we forget
qyearsley2bc727d2015-08-14 20:17:15638 // to reset something that is reset by the constructor.
vitalybuka@chromium.org0318f922014-04-22 00:09:23639 Parsed parsed;
Hayato Ito67985d682023-10-26 01:11:29640 for (const auto& filesystem_case : filesystem_cases) {
641 const char* url = filesystem_case.input;
vitalybuka@chromium.org0318f922014-04-22 00:09:23642 ParseFileSystemURL(url, static_cast<int>(strlen(url)), &parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52643
644 EXPECT_TRUE(ComponentMatches(url, "filesystem", parsed.scheme));
Hayato Ito67985d682023-10-26 01:11:29645 EXPECT_EQ(!filesystem_case.inner_scheme, !parsed.inner_parsed());
brettw@chromium.orge7bba5f2013-04-10 20:10:52646 // Only check the inner_parsed if there is one.
647 if (parsed.inner_parsed()) {
Hayato Ito67985d682023-10-26 01:11:29648 EXPECT_TRUE(ComponentMatches(url, filesystem_case.inner_scheme,
brettw@chromium.orge7bba5f2013-04-10 20:10:52649 parsed.inner_parsed()->scheme));
Hayato Ito67985d682023-10-26 01:11:29650 EXPECT_TRUE(ComponentMatches(url, filesystem_case.inner_username,
brettw@chromium.orge7bba5f2013-04-10 20:10:52651 parsed.inner_parsed()->username));
Hayato Ito67985d682023-10-26 01:11:29652 EXPECT_TRUE(ComponentMatches(url, filesystem_case.inner_password,
brettw@chromium.orge7bba5f2013-04-10 20:10:52653 parsed.inner_parsed()->password));
Hayato Ito67985d682023-10-26 01:11:29654 EXPECT_TRUE(ComponentMatches(url, filesystem_case.inner_host,
brettw@chromium.orge7bba5f2013-04-10 20:10:52655 parsed.inner_parsed()->host));
vitalybuka@chromium.org0318f922014-04-22 00:09:23656 int port = ParsePort(url, parsed.inner_parsed()->port);
Hayato Ito67985d682023-10-26 01:11:29657 EXPECT_EQ(filesystem_case.inner_port, port);
brettw@chromium.orge7bba5f2013-04-10 20:10:52658
qyearsley2bc727d2015-08-14 20:17:15659 // The remaining components are never used for filesystem URLs.
brettw@chromium.orge7bba5f2013-04-10 20:10:52660 ExpectInvalidComponent(parsed.inner_parsed()->query);
661 ExpectInvalidComponent(parsed.inner_parsed()->ref);
662 }
663
Hayato Ito67985d682023-10-26 01:11:29664 EXPECT_TRUE(ComponentMatches(url, filesystem_case.path, parsed.path));
665 EXPECT_TRUE(ComponentMatches(url, filesystem_case.query, parsed.query));
666 EXPECT_TRUE(ComponentMatches(url, filesystem_case.ref, parsed.ref));
Hayato Itob37956b2023-12-04 05:01:52667 EXPECT_FALSE(parsed.has_opaque_path);
brettw@chromium.orge7bba5f2013-04-10 20:10:52668
qyearsley2bc727d2015-08-14 20:17:15669 // The remaining components are never used for filesystem URLs.
brettw@chromium.orge7bba5f2013-04-10 20:10:52670 ExpectInvalidComponent(parsed.username);
671 ExpectInvalidComponent(parsed.password);
672 ExpectInvalidComponent(parsed.host);
673 ExpectInvalidComponent(parsed.port);
674 }
675}
676
Hayato Itob37956b2023-12-04 05:01:52677// Non-special URLs which don't have an opaque path.
678static URLParseCase non_special_cases[] = {
679 {"git://user:pass@foo:21/bar;par?b#c", "git", "user", "pass", "foo", 21,
680 "/bar;par", "b", "c"},
681 {"git://host", "git", nullptr, nullptr, "host", -1, nullptr, nullptr,
682 nullptr},
683 {"git://host/a/../b", "git", nullptr, nullptr, "host", -1, "/a/../b",
684 nullptr, nullptr},
685 {"git://host/a b", "git", nullptr, nullptr, "host", -1, "/a b", nullptr,
686 nullptr},
687 {"git://ho\\st/", "git", nullptr, nullptr, "ho\\st", -1, "/", nullptr,
688 nullptr},
Hayato Ito735a5cbc2023-12-13 02:49:38689 // Empty users
690 {"git://@host", "git", "", nullptr, "host", -1, nullptr, nullptr, nullptr},
691 // Empty user and invalid host. "git://@" is an invalid URL.
692 {"git://@", "git", "", nullptr, nullptr, -1, nullptr, nullptr, nullptr},
Hayato Itoa5bb75e2024-02-09 07:19:04693 // Invalid host and non-empty port. "git://:80" is an invalid URL.
694 {"git://:80", "git", nullptr, nullptr, nullptr, 80, nullptr, nullptr,
695 nullptr},
Hayato Itob37956b2023-12-04 05:01:52696 // Empty host cases
697 {"git://", "git", nullptr, nullptr, "", -1, nullptr, nullptr, nullptr},
698 {"git:///", "git", nullptr, nullptr, "", -1, "/", nullptr, nullptr},
699 {"git:////", "git", nullptr, nullptr, "", -1, "//", nullptr, nullptr},
700 // Null host cases
701 {"git:/", "git", nullptr, nullptr, nullptr, -1, "/", nullptr, nullptr},
702 {"git:/trailing-space ", "git", nullptr, nullptr, nullptr, -1,
703 "/trailing-space", nullptr, nullptr},
704};
705
706TEST(URLParser, NonSpecial) {
707 // Declared outside for loop to try to catch cases in init() where we forget
708 // to reset something that is reset by the constructor.
709 Parsed parsed;
710 for (const auto& i : non_special_cases) {
711 const char* url = i.input;
712 ParseNonSpecialURL(url, static_cast<int>(strlen(url)), &parsed);
713 URLParseCaseMatches(i, parsed);
714 EXPECT_FALSE(parsed.has_opaque_path) << "url: " << url;
715 }
716}
717
718// Non-special URLs which have an opaque path.
719static URLParseCase non_special_opaque_path_cases[] = {
720 {"git:", "git", nullptr, nullptr, nullptr, -1, nullptr, nullptr, nullptr},
721 {"git:opaque", "git", nullptr, nullptr, nullptr, -1, "opaque", nullptr,
722 nullptr},
723 {"git:opaque?a=b#c", "git", nullptr, nullptr, nullptr, -1, "opaque", "a=b",
724 "c"},
725 {"git: o p a q u e ", "git", nullptr, nullptr, nullptr, -1, " o p a q u e",
726 nullptr, nullptr},
727 {"git:opa\\que", "git", nullptr, nullptr, nullptr, -1, "opa\\que", nullptr,
728 nullptr},
729};
730
731TEST(URLParser, NonSpecialOpaquePath) {
732 // Declared outside for loop to try to catch cases in init() where we forget
733 // to reset something that is reset by the constructor.
734 Parsed parsed;
735 for (const auto& i : non_special_opaque_path_cases) {
736 const char* url = i.input;
737 ParseNonSpecialURL(url, static_cast<int>(strlen(url)), &parsed);
738 URLParseCaseMatches(i, parsed);
739 EXPECT_TRUE(parsed.has_opaque_path) << "url: " << url;
740 }
741}
742
tfarina@chromium.orgecf0d742013-04-15 01:22:29743} // namespace
vitalybuka@chromium.org0318f922014-04-22 00:09:23744} // namespace url