Whitelist window states to persist on ASH app windows.
[chromium-blink-merge.git] / url / url_parse_unittest.cc
bloba65ff858b460149fd5dba2271991c130b78a3a69
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/url_parse.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/url_parse.h"
11 // Some implementations of base/basictypes.h may define ARRAYSIZE.
12 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
13 // which is in our version of basictypes.h.
14 #ifndef ARRAYSIZE
15 #define ARRAYSIZE ARRAYSIZE_UNSAFE
16 #endif
18 // Interesting IE file:isms...
20 // file:/foo/bar file:///foo/bar
21 // The result here seems totally invalid!?!? This isn't UNC.
23 // file:/
24 // file:// or any other number of slashes
25 // IE6 doesn't do anything at all if you click on this link. No error:
26 // nothing. IE6's history system seems to always color this link, so I'm
27 // guessing that it maps internally to the empty URL.
29 // C:\ file:///C:/
30 // / file:///C:/
31 // /foo file:///C:/foo
32 // Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
33 // but is weird to think about on Windows.
35 // file:foo/ file:foo/ (invalid?!?!?)
36 // file:/foo/ file:///foo/ (invalid?!?!?)
37 // file://foo/ file://foo/ (UNC to server "foo")
38 // file:///foo/ file:///foo/ (invalid)
39 // file:////foo/ file://foo/ (UNC to server "foo")
40 // Any more than four slashes is also treated as UNC.
42 // file:C:/ file://C:/
43 // file:/C:/ file://C:/
44 // The number of slashes after "file:" don't matter if the thing following
45 // it looks like an absolute drive path. Also, slashes and backslashes are
46 // equally valid here.
48 namespace url_parse {
49 namespace {
51 // Used for regular URL parse cases.
52 struct URLParseCase {
53 const char* input;
55 const char* scheme;
56 const char* username;
57 const char* password;
58 const char* host;
59 int port;
60 const char* path;
61 const char* query;
62 const char* ref;
65 // Simpler version of URLParseCase for testing path URLs.
66 struct PathURLParseCase {
67 const char* input;
69 const char* scheme;
70 const char* path;
73 // Simpler version of URLParseCase for testing mailto URLs.
74 struct MailtoURLParseCase {
75 const char* input;
77 const char* scheme;
78 const char* path;
79 const char* query;
82 // More complicated version of URLParseCase for testing filesystem URLs.
83 struct FileSystemURLParseCase {
84 const char* input;
86 const char* inner_scheme;
87 const char* inner_username;
88 const char* inner_password;
89 const char* inner_host;
90 int inner_port;
91 const char* inner_path;
92 const char* path;
93 const char* query;
94 const char* ref;
97 bool ComponentMatches(const char* input,
98 const char* reference,
99 const url_parse::Component& component) {
100 // If the component is nonexistant (length == -1), it should begin at 0.
101 EXPECT_TRUE(component.len >= 0 || component.len == -1);
103 // Begin should be valid.
104 EXPECT_LE(0, component.begin);
106 // A NULL reference means the component should be nonexistant.
107 if (!reference)
108 return component.len == -1;
109 if (component.len < 0)
110 return false; // Reference is not NULL but we don't have anything
112 if (strlen(reference) != static_cast<size_t>(component.len))
113 return false; // Lengths don't match
115 // Now check the actual characters.
116 return strncmp(reference, &input[component.begin], component.len) == 0;
119 void ExpectInvalidComponent(const url_parse::Component& component) {
120 EXPECT_EQ(0, component.begin);
121 EXPECT_EQ(-1, component.len);
124 // Parsed ----------------------------------------------------------------------
126 TEST(URLParser, Length) {
127 const char* length_cases[] = {
128 // One with everything in it.
129 "http://user:pass@host:99/foo?bar#baz",
130 // One with nothing in it.
132 // Working backwards, let's start taking off stuff from the full one.
133 "http://user:pass@host:99/foo?bar#",
134 "http://user:pass@host:99/foo?bar",
135 "http://user:pass@host:99/foo?",
136 "http://user:pass@host:99/foo",
137 "http://user:pass@host:99/",
138 "http://user:pass@host:99",
139 "http://user:pass@host:",
140 "http://user:pass@host",
141 "http://host",
142 "http://user@",
143 "http:",
145 for (size_t i = 0; i < arraysize(length_cases); i++) {
146 int true_length = static_cast<int>(strlen(length_cases[i]));
148 url_parse::Parsed parsed;
149 url_parse::ParseStandardURL(length_cases[i], true_length, &parsed);
151 EXPECT_EQ(true_length, parsed.Length());
155 TEST(URLParser, CountCharactersBefore) {
156 struct CountCase {
157 const char* url;
158 Parsed::ComponentType component;
159 bool include_delimiter;
160 int expected_count;
161 } count_cases[] = {
162 // Test each possibility in the case where all components are present.
163 // 0 1 2
164 // 0123456789012345678901
165 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
166 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
167 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
168 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
169 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
170 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
171 {"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
172 {"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
173 {"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
174 {"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
175 {"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
176 {"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
177 {"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
178 {"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
179 {"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
180 {"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
181 // Now test when the requested component is missing.
182 {"http://u:p@h:8/p?", Parsed::REF, true, 17},
183 {"http://u:p@h:8/p?q", Parsed::REF, true, 18},
184 {"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
185 {"http://u:p@h:8#r", Parsed::PATH, true, 14},
186 {"http://u:p@h/", Parsed::PORT, true, 12},
187 {"http://u:p@/", Parsed::HOST, true, 11},
188 // This case is a little weird. It will report that the password would
189 // start where the host begins. This is arguably correct, although you
190 // could also argue that it should start at the '@' sign. Doing it
191 // starting with the '@' sign is actually harder, so we don't bother.
192 {"http://u@h/", Parsed::PASSWORD, true, 9},
193 {"http://h/", Parsed::USERNAME, true, 7},
194 {"http:", Parsed::USERNAME, true, 5},
195 {"", Parsed::SCHEME, true, 0},
196 // Make sure a random component still works when there's nothing there.
197 {"", Parsed::REF, true, 0},
198 // File URLs are special with no host, so we test those.
199 {"file:///c:/foo", Parsed::USERNAME, true, 7},
200 {"file:///c:/foo", Parsed::PASSWORD, true, 7},
201 {"file:///c:/foo", Parsed::HOST, true, 7},
202 {"file:///c:/foo", Parsed::PATH, true, 7},
204 for (size_t i = 0; i < ARRAYSIZE(count_cases); i++) {
205 int length = static_cast<int>(strlen(count_cases[i].url));
207 // Simple test to distinguish file and standard URLs.
208 url_parse::Parsed parsed;
209 if (length > 0 && count_cases[i].url[0] == 'f')
210 url_parse::ParseFileURL(count_cases[i].url, length, &parsed);
211 else
212 url_parse::ParseStandardURL(count_cases[i].url, length, &parsed);
214 int chars_before = parsed.CountCharactersBefore(
215 count_cases[i].component, count_cases[i].include_delimiter);
216 EXPECT_EQ(count_cases[i].expected_count, chars_before);
220 // Standard --------------------------------------------------------------------
222 // Input Scheme Usrname Passwd Host Port Path Query Ref
223 // ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
224 static URLParseCase cases[] = {
225 // Regular URL with all the parts
226 {"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass", "foo", 21, "/bar;par","b", "c"},
228 // Known schemes should lean towards authority identification
229 {"http:foo.com", "http", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
231 // Spaces!
232 {"\t :foo.com \n", "", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
233 {" foo.com ", NULL, NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
234 {"a:\t foo.com", "a", NULL, NULL, "\t foo.com", -1, NULL, NULL, NULL},
235 {"http://f:21/ b ? d # e ", "http", NULL, NULL, "f", 21, "/ b ", " d ", " e"},
237 // Invalid port numbers should be identified and turned into -2, empty port
238 // numbers should be -1. Spaces aren't allowed in port numbers
239 {"http://f:/c", "http", NULL, NULL, "f", -1, "/c", NULL, NULL},
240 {"http://f:0/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
241 {"http://f:00000000000000/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
242 {"http://f:00000000000000000000080/c", "http", NULL, NULL, "f", 80, "/c", NULL, NULL},
243 {"http://f:b/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
244 {"http://f: /c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
245 {"http://f:\n/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
246 {"http://f:fifty-two/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
247 {"http://f:999999/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
248 {"http://f: 21 / b ? d # e ", "http", NULL, NULL, "f", -2, "/ b ", " d ", " e"},
250 // Creative URLs missing key elements
251 {"", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
252 {" \t", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
253 {":foo.com/", "", NULL, NULL, "foo.com", -1, "/", NULL, NULL},
254 {":foo.com\\", "", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
255 {":", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
256 {":a", "", NULL, NULL, "a", -1, NULL, NULL, NULL},
257 {":/", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
258 {":\\", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
259 {":#", "", NULL, NULL, NULL, -1, NULL, NULL, ""},
260 {"#", NULL, NULL, NULL, NULL, -1, NULL, NULL, ""},
261 {"#/", NULL, NULL, NULL, NULL, -1, NULL, NULL, "/"},
262 {"#\\", NULL, NULL, NULL, NULL, -1, NULL, NULL, "\\"},
263 {"#;?", NULL, NULL, NULL, NULL, -1, NULL, NULL, ";?"},
264 {"?", NULL, NULL, NULL, NULL, -1, NULL, "", NULL},
265 {"/", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
266 {":23", "", NULL, NULL, "23", -1, NULL, NULL, NULL},
267 {"/:23", "/", NULL, NULL, "23", -1, NULL, NULL, NULL},
268 {"//", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
269 {"::", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
270 {"::23", "", NULL, NULL, NULL, 23, NULL, NULL, NULL},
271 {"foo://", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
273 // Username/passwords and things that look like them
274 {"http://a:b@c:29/d", "http", "a", "b", "c", 29, "/d", NULL, NULL},
275 {"http::@c:29", "http", "", "", "c", 29, NULL, NULL, NULL},
276 // ... "]" in the password field isn't allowed, but we tolerate it here...
277 {"http://&a:foo(b]c@d:2/", "http", "&a", "foo(b]c", "d", 2, "/", NULL, NULL},
278 {"http://::@c@d:2", "http", "", ":@c", "d", 2, NULL, NULL, NULL},
279 {"http://foo.com:b@d/", "http", "foo.com", "b", "d", -1, "/", NULL, NULL},
281 {"http://foo.com/\\@", "http", NULL, NULL, "foo.com", -1, "/\\@", NULL, NULL},
282 {"http:\\\\foo.com\\", "http", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
283 {"http:\\\\a\\b:c\\d@foo.com\\", "http", NULL, NULL, "a", -1, "\\b:c\\d@foo.com\\", NULL, NULL},
285 // Tolerate different numbers of slashes.
286 {"foo:/", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
287 {"foo:/bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
288 {"foo://///////", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
289 {"foo://///////bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
290 {"foo:////://///", "foo", NULL, NULL, NULL, -1, "/////", NULL, NULL},
292 // Raw file paths on Windows aren't handled by the parser.
293 {"c:/foo", "c", NULL, NULL, "foo", -1, NULL, NULL, NULL},
294 {"//foo/bar", NULL, NULL, NULL, "foo", -1, "/bar", NULL, NULL},
296 // Use the first question mark for the query and the ref.
297 {"http://foo/path;a??e#f#g", "http", NULL, NULL, "foo", -1, "/path;a", "?e", "f#g"},
298 {"http://foo/abcd?efgh?ijkl", "http", NULL, NULL, "foo", -1, "/abcd", "efgh?ijkl", NULL},
299 {"http://foo/abcd#foo?bar", "http", NULL, NULL, "foo", -1, "/abcd", NULL, "foo?bar"},
301 // IPv6, check also interesting uses of colons.
302 {"[61:24:74]:98", "[61", NULL, NULL, "24:74]", 98, NULL, NULL, NULL},
303 {"http://[61:27]:98", "http", NULL, NULL, "[61:27]", 98, NULL, NULL, NULL},
304 {"http:[61:27]/:foo", "http", NULL, NULL, "[61:27]", -1, "/:foo", NULL, NULL},
305 {"http://[1::2]:3:4", "http", NULL, NULL, "[1::2]:3", 4, NULL, NULL, NULL},
307 // Partially-complete IPv6 literals, and related cases.
308 {"http://2001::1", "http", NULL, NULL, "2001:", 1, NULL, NULL, NULL},
309 {"http://[2001::1", "http", NULL, NULL, "[2001::1", -1, NULL, NULL, NULL},
310 {"http://2001::1]", "http", NULL, NULL, "2001::1]", -1, NULL, NULL, NULL},
311 {"http://2001::1]:80", "http", NULL, NULL, "2001::1]", 80, NULL, NULL, NULL},
312 {"http://[2001::1]", "http", NULL, NULL, "[2001::1]", -1, NULL, NULL, NULL},
313 {"http://[2001::1]:80", "http", NULL, NULL, "[2001::1]", 80, NULL, NULL, NULL},
314 {"http://[[::]]", "http", NULL, NULL, "[[::]]", -1, NULL, NULL, NULL},
318 TEST(URLParser, Standard) {
319 // Declared outside for loop to try to catch cases in init() where we forget
320 // to reset something that is reset by the constructor.
321 url_parse::Parsed parsed;
322 for (size_t i = 0; i < arraysize(cases); i++) {
323 const char* url = cases[i].input;
324 url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
325 int port = url_parse::ParsePort(url, parsed.port);
327 EXPECT_TRUE(ComponentMatches(url, cases[i].scheme, parsed.scheme));
328 EXPECT_TRUE(ComponentMatches(url, cases[i].username, parsed.username));
329 EXPECT_TRUE(ComponentMatches(url, cases[i].password, parsed.password));
330 EXPECT_TRUE(ComponentMatches(url, cases[i].host, parsed.host));
331 EXPECT_EQ(cases[i].port, port);
332 EXPECT_TRUE(ComponentMatches(url, cases[i].path, parsed.path));
333 EXPECT_TRUE(ComponentMatches(url, cases[i].query, parsed.query));
334 EXPECT_TRUE(ComponentMatches(url, cases[i].ref, parsed.ref));
338 // PathURL --------------------------------------------------------------------
340 // Various incarnations of path URLs.
341 static PathURLParseCase path_cases[] = {
342 {"", NULL, NULL},
343 {":", "", NULL},
344 {":/", "", "/"},
345 {"/", NULL, "/"},
346 {" This is \\interesting// \t", NULL, "This is \\interesting//"},
347 {"about:", "about", NULL},
348 {"about:blank", "about", "blank"},
349 {" about: blank ", "about", " blank"},
350 {"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\");"},
353 TEST(URLParser, PathURL) {
354 // Declared outside for loop to try to catch cases in init() where we forget
355 // to reset something that is reset by the construtor.
356 url_parse::Parsed parsed;
357 for (size_t i = 0; i < arraysize(path_cases); i++) {
358 const char* url = path_cases[i].input;
359 url_parse::ParsePathURL(url, static_cast<int>(strlen(url)), &parsed);
361 EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme));
362 EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.path));
364 // The remaining components are never used for path urls.
365 ExpectInvalidComponent(parsed.username);
366 ExpectInvalidComponent(parsed.password);
367 ExpectInvalidComponent(parsed.host);
368 ExpectInvalidComponent(parsed.port);
369 ExpectInvalidComponent(parsed.query);
370 ExpectInvalidComponent(parsed.ref);
374 #ifdef WIN32
376 // WindowsFile ----------------------------------------------------------------
378 // Various incarnations of file URLs. These are for Windows only.
379 static URLParseCase file_cases[] = {
380 {"file:server", "file", NULL, NULL, "server", -1, NULL, NULL, NULL},
381 {" file: server \t", "file", NULL, NULL, " server",-1, NULL, NULL, NULL},
382 {"FiLe:c|", "FiLe", NULL, NULL, NULL, -1, "c|", NULL, NULL},
383 {"FILE:/\\\\/server/file", "FILE", NULL, NULL, "server", -1, "/file", NULL, NULL},
384 {"file://server/", "file", NULL, NULL, "server", -1, "/", NULL, NULL},
385 {"file://localhost/c:/", "file", NULL, NULL, NULL, -1, "/c:/", NULL, NULL},
386 {"file://127.0.0.1/c|\\", "file", NULL, NULL, NULL, -1, "/c|\\", NULL, NULL},
387 {"file:/", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
388 {"file:", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
389 // If there is a Windows drive letter, treat any number of slashes as the
390 // path part.
391 {"file:c:\\fo\\b", "file", NULL, NULL, NULL, -1, "c:\\fo\\b", NULL, NULL},
392 {"file:/c:\\foo/bar", "file", NULL, NULL, NULL, -1, "/c:\\foo/bar",NULL, NULL},
393 {"file://c:/f\\b", "file", NULL, NULL, NULL, -1, "/c:/f\\b", NULL, NULL},
394 {"file:///C:/foo", "file", NULL, NULL, NULL, -1, "/C:/foo", NULL, NULL},
395 {"file://///\\/\\/c:\\f\\b", "file", NULL, NULL, NULL, -1, "/c:\\f\\b", NULL, NULL},
396 // If there is not a drive letter, we should treat is as UNC EXCEPT for
397 // three slashes, which we treat as a Unix style path.
398 {"file:server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
399 {"file:/server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
400 {"file://server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
401 {"file:///server/file", "file", NULL, NULL, NULL, -1, "/server/file",NULL, NULL},
402 {"file://\\server/file", "file", NULL, NULL, NULL, -1, "\\server/file",NULL, NULL},
403 {"file:////server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
404 // Queries and refs are valid for file URLs as well.
405 {"file:///C:/foo.html?#", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "", ""},
406 {"file:///C:/foo.html?query=yes#ref", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "query=yes", "ref"},
409 TEST(URLParser, WindowsFile) {
410 // Declared outside for loop to try to catch cases in init() where we forget
411 // to reset something that is reset by the construtor.
412 url_parse::Parsed parsed;
413 for (int i = 0; i < arraysize(file_cases); i++) {
414 const char* url = file_cases[i].input;
415 url_parse::ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
416 int port = url_parse::ParsePort(url, parsed.port);
418 EXPECT_TRUE(ComponentMatches(url, file_cases[i].scheme, parsed.scheme));
419 EXPECT_TRUE(ComponentMatches(url, file_cases[i].username, parsed.username));
420 EXPECT_TRUE(ComponentMatches(url, file_cases[i].password, parsed.password));
421 EXPECT_TRUE(ComponentMatches(url, file_cases[i].host, parsed.host));
422 EXPECT_EQ(file_cases[i].port, port);
423 EXPECT_TRUE(ComponentMatches(url, file_cases[i].path, parsed.path));
424 EXPECT_TRUE(ComponentMatches(url, file_cases[i].query, parsed.query));
425 EXPECT_TRUE(ComponentMatches(url, file_cases[i].ref, parsed.ref));
429 #endif // WIN32
431 TEST(URLParser, ExtractFileName) {
432 struct FileCase {
433 const char* input;
434 const char* expected;
435 } file_cases[] = {
436 {"http://www.google.com", NULL},
437 {"http://www.google.com/", ""},
438 {"http://www.google.com/search", "search"},
439 {"http://www.google.com/search/", ""},
440 {"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
441 {"http://www.google.com/foo/bar.html#ref", "bar.html"},
442 {"http://www.google.com/search/;param", ""},
443 {"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
444 {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html;foo"},
445 {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
448 for (size_t i = 0; i < ARRAYSIZE(file_cases); i++) {
449 const char* url = file_cases[i].input;
450 int len = static_cast<int>(strlen(url));
452 url_parse::Parsed parsed;
453 url_parse::ParseStandardURL(url, len, &parsed);
455 url_parse::Component file_name;
456 url_parse::ExtractFileName(url, parsed.path, &file_name);
458 EXPECT_TRUE(ComponentMatches(url, file_cases[i].expected, file_name));
462 // Returns true if the parameter with index |parameter| in the given URL's
463 // query string. The expected key can be NULL to indicate no such key index
464 // should exist. The parameter number is 1-based.
465 static bool NthParameterIs(const char* url,
466 int parameter,
467 const char* expected_key,
468 const char* expected_value) {
469 url_parse::Parsed parsed;
470 url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
472 url_parse::Component query = parsed.query;
474 for (int i = 1; i <= parameter; i++) {
475 url_parse::Component key, value;
476 if (!url_parse::ExtractQueryKeyValue(url, &query, &key, &value)) {
477 if (parameter >= i && !expected_key)
478 return true; // Expected nonexistant key, got one.
479 return false; // Not enough keys.
482 if (i == parameter) {
483 if (!expected_key)
484 return false;
486 if (strncmp(&url[key.begin], expected_key, key.len) != 0)
487 return false;
488 if (strncmp(&url[value.begin], expected_value, value.len) != 0)
489 return false;
490 return true;
493 return expected_key == NULL; // We didn't find that many parameters.
496 TEST(URLParser, ExtractQueryKeyValue) {
497 EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, NULL, NULL));
499 // Basic case.
500 char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
501 EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
502 EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
503 EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
504 EXPECT_TRUE(NthParameterIs(a, 4, NULL, NULL));
506 // Empty param at the end.
507 char b[] = "http://www.google.com?foo=bar&";
508 EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
509 EXPECT_TRUE(NthParameterIs(b, 2, NULL, NULL));
511 // Empty param at the beginning.
512 char c[] = "http://www.google.com?&foo=bar";
513 EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
514 EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
515 EXPECT_TRUE(NthParameterIs(c, 3, NULL, NULL));
517 // Empty key with value.
518 char d[] = "http://www.google.com?=foo";
519 EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
520 EXPECT_TRUE(NthParameterIs(d, 2, NULL, NULL));
522 // Empty value with key.
523 char e[] = "http://www.google.com?foo=";
524 EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
525 EXPECT_TRUE(NthParameterIs(e, 2, NULL, NULL));
527 // Empty key and values.
528 char f[] = "http://www.google.com?&&==&=";
529 EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
530 EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
531 EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
532 EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
533 EXPECT_TRUE(NthParameterIs(f, 5, NULL, NULL));
536 // MailtoURL --------------------------------------------------------------------
538 static MailtoURLParseCase mailto_cases[] = {
539 //|input |scheme |path |query
540 {"mailto:foo@gmail.com", "mailto", "foo@gmail.com", NULL},
541 {" mailto: to \t", "mailto", " to", NULL},
542 {"mailto:addr1%2C%20addr2 ", "mailto", "addr1%2C%20addr2", NULL},
543 {"Mailto:addr1, addr2 ", "Mailto", "addr1, addr2", NULL},
544 {"mailto:addr1:addr2 ", "mailto", "addr1:addr2", NULL},
545 {"mailto:?to=addr1,addr2", "mailto", NULL, "to=addr1,addr2"},
546 {"mailto:?to=addr1%2C%20addr2", "mailto", NULL, "to=addr1%2C%20addr2"},
547 {"mailto:addr1?to=addr2", "mailto", "addr1", "to=addr2"},
548 {"mailto:?body=#foobar#", "mailto", NULL, "body=#foobar#",},
549 {"mailto:#?body=#foobar#", "mailto", "#", "body=#foobar#"},
552 TEST(URLParser, MailtoUrl) {
553 // Declared outside for loop to try to catch cases in init() where we forget
554 // to reset something that is reset by the construtor.
555 url_parse::Parsed parsed;
556 for (size_t i = 0; i < arraysize(mailto_cases); ++i) {
557 const char* url = mailto_cases[i].input;
558 url_parse::ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
559 int port = url_parse::ParsePort(url, parsed.port);
561 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].scheme, parsed.scheme));
562 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].path, parsed.path));
563 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].query, parsed.query));
564 EXPECT_EQ(url_parse::PORT_UNSPECIFIED, port);
566 // The remaining components are never used for mailto urls.
567 ExpectInvalidComponent(parsed.username);
568 ExpectInvalidComponent(parsed.password);
569 ExpectInvalidComponent(parsed.port);
570 ExpectInvalidComponent(parsed.ref);
574 // Various incarnations of filesystem URLs.
575 static FileSystemURLParseCase filesystem_cases[] = {
576 // Regular URL with all the parts
577 {"filesystem:http://user:pass@foo:21/temporary/bar;par?b#c", "http", "user", "pass", "foo", 21, "/temporary", "/bar;par", "b", "c"},
578 {"filesystem:https://foo/persistent/bar;par/", "https", NULL, NULL, "foo", -1, "/persistent", "/bar;par/", NULL, NULL},
579 {"filesystem:file:///persistent/bar;par/", "file", NULL, NULL, NULL, -1, "/persistent", "/bar;par/", NULL, NULL},
580 {"filesystem:file:///persistent/bar;par/?query#ref", "file", NULL, NULL, NULL, -1, "/persistent", "/bar;par/", "query", "ref"},
581 {"filesystem:file:///persistent", "file", NULL, NULL, NULL, -1, "/persistent", "", NULL, NULL},
584 TEST(URLParser, FileSystemURL) {
585 // Declared outside for loop to try to catch cases in init() where we forget
586 // to reset something that is reset by the construtor.
587 url_parse::Parsed parsed;
588 for (size_t i = 0; i < arraysize(filesystem_cases); i++) {
589 const FileSystemURLParseCase* parsecase = &filesystem_cases[i];
590 const char* url = parsecase->input;
591 url_parse::ParseFileSystemURL(url, static_cast<int>(strlen(url)), &parsed);
593 EXPECT_TRUE(ComponentMatches(url, "filesystem", parsed.scheme));
594 EXPECT_EQ(!parsecase->inner_scheme, !parsed.inner_parsed());
595 // Only check the inner_parsed if there is one.
596 if (parsed.inner_parsed()) {
597 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_scheme,
598 parsed.inner_parsed()->scheme));
599 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_username,
600 parsed.inner_parsed()->username));
601 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_password,
602 parsed.inner_parsed()->password));
603 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_host,
604 parsed.inner_parsed()->host));
605 int port = url_parse::ParsePort(url, parsed.inner_parsed()->port);
606 EXPECT_EQ(parsecase->inner_port, port);
608 // The remaining components are never used for filesystem urls.
609 ExpectInvalidComponent(parsed.inner_parsed()->query);
610 ExpectInvalidComponent(parsed.inner_parsed()->ref);
613 EXPECT_TRUE(ComponentMatches(url, parsecase->path, parsed.path));
614 EXPECT_TRUE(ComponentMatches(url, parsecase->query, parsed.query));
615 EXPECT_TRUE(ComponentMatches(url, parsecase->ref, parsed.ref));
617 // The remaining components are never used for filesystem urls.
618 ExpectInvalidComponent(parsed.username);
619 ExpectInvalidComponent(parsed.password);
620 ExpectInvalidComponent(parsed.host);
621 ExpectInvalidComponent(parsed.port);
625 } // namespace
626 } // namespace url_parse