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