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