blob: dc23ef1f38f02bee944042db83e6bb2254b2a379 [file] [log] [blame]
[email protected]e7bba5f82013-04-10 20:10:521// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
[email protected]ecf0d742013-04-15 01:22:2930#include "url/url_parse.h"
31
[email protected]e7bba5f82013-04-10 20:10:5232#include "base/basictypes.h"
[email protected]e7bba5f82013-04-10 20:10:5233#include "testing/gtest/include/gtest/gtest.h"
[email protected]e7bba5f82013-04-10 20:10:5234
35// Some implementations of base/basictypes.h may define ARRAYSIZE.
36// If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
37// which is in our version of basictypes.h.
38#ifndef ARRAYSIZE
39#define ARRAYSIZE ARRAYSIZE_UNSAFE
40#endif
41
42// Interesting IE file:isms...
43//
44// file:/foo/bar file:///foo/bar
45// The result here seems totally invalid!?!? This isn't UNC.
46//
47// file:/
48// file:// or any other number of slashes
49// IE6 doesn't do anything at all if you click on this link. No error:
50// nothing. IE6's history system seems to always color this link, so I'm
51// guessing that it maps internally to the empty URL.
52//
53// C:\ file:///C:/
54// / file:///C:/
55// /foo file:///C:/foo
56// Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
57// but is weird to think about on Windows.
58//
59// file:foo/ file:foo/ (invalid?!?!?)
60// file:/foo/ file:///foo/ (invalid?!?!?)
61// file://foo/ file://foo/ (UNC to server "foo")
62// file:///foo/ file:///foo/ (invalid)
63// file:////foo/ file://foo/ (UNC to server "foo")
64// Any more than four slashes is also treated as UNC.
65//
66// file:C:/ file://C:/
67// file:/C:/ file://C:/
68// The number of slashes after "file:" don't matter if the thing following
69// it looks like an absolute drive path. Also, slashes and backslashes are
70// equally valid here.
71
[email protected]ecf0d742013-04-15 01:22:2972namespace url_parse {
[email protected]e7bba5f82013-04-10 20:10:5273namespace {
74
75// Used for regular URL parse cases.
76struct URLParseCase {
77 const char* input;
78
79 const char* scheme;
80 const char* username;
81 const char* password;
82 const char* host;
83 int port;
84 const char* path;
85 const char* query;
86 const char* ref;
87};
88
89// Simpler version of URLParseCase for testing path URLs.
90struct PathURLParseCase {
91 const char* input;
92
93 const char* scheme;
94 const char* path;
95};
96
97// Simpler version of URLParseCase for testing mailto URLs.
98struct MailtoURLParseCase {
99 const char* input;
100
101 const char* scheme;
102 const char* path;
103 const char* query;
104};
105
106// More complicated version of URLParseCase for testing filesystem URLs.
107struct FileSystemURLParseCase {
108 const char* input;
109
110 const char* inner_scheme;
111 const char* inner_username;
112 const char* inner_password;
113 const char* inner_host;
114 int inner_port;
115 const char* inner_path;
116 const char* path;
117 const char* query;
118 const char* ref;
119};
120
121bool ComponentMatches(const char* input,
122 const char* reference,
123 const url_parse::Component& component) {
124 // If the component is nonexistant (length == -1), it should begin at 0.
125 EXPECT_TRUE(component.len >= 0 || component.len == -1);
126
127 // Begin should be valid.
128 EXPECT_LE(0, component.begin);
129
130 // A NULL reference means the component should be nonexistant.
131 if (!reference)
132 return component.len == -1;
133 if (component.len < 0)
134 return false; // Reference is not NULL but we don't have anything
135
136 if (strlen(reference) != static_cast<size_t>(component.len))
137 return false; // Lengths don't match
138
139 // Now check the actual characters.
140 return strncmp(reference, &input[component.begin], component.len) == 0;
141}
142
143void ExpectInvalidComponent(const url_parse::Component& component) {
144 EXPECT_EQ(0, component.begin);
145 EXPECT_EQ(-1, component.len);
146}
147
[email protected]e7bba5f82013-04-10 20:10:52148// Parsed ----------------------------------------------------------------------
149
150TEST(URLParser, Length) {
151 const char* length_cases[] = {
152 // One with everything in it.
153 "http://user:pass@host:99/foo?bar#baz",
154 // One with nothing in it.
155 "",
156 // Working backwards, let's start taking off stuff from the full one.
157 "http://user:pass@host:99/foo?bar#",
158 "http://user:pass@host:99/foo?bar",
159 "http://user:pass@host:99/foo?",
160 "http://user:pass@host:99/foo",
161 "http://user:pass@host:99/",
162 "http://user:pass@host:99",
163 "http://user:pass@host:",
164 "http://user:pass@host",
165 "http://host",
166 "http://user@",
167 "http:",
168 };
169 for (size_t i = 0; i < arraysize(length_cases); i++) {
170 int true_length = static_cast<int>(strlen(length_cases[i]));
171
172 url_parse::Parsed parsed;
173 url_parse::ParseStandardURL(length_cases[i], true_length, &parsed);
174
175 EXPECT_EQ(true_length, parsed.Length());
176 }
177}
178
179TEST(URLParser, CountCharactersBefore) {
[email protected]e7bba5f82013-04-10 20:10:52180 struct CountCase {
181 const char* url;
182 Parsed::ComponentType component;
183 bool include_delimiter;
184 int expected_count;
185 } count_cases[] = {
[email protected]ecf0d742013-04-15 01:22:29186 // Test each possibility in the case where all components are present.
187 // 0 1 2
188 // 0123456789012345678901
[email protected]e7bba5f82013-04-10 20:10:52189 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
190 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
191 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
192 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
193 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
194 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
195 {"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
196 {"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
197 {"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
198 {"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
199 {"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
200 {"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
201 {"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
202 {"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
203 {"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
204 {"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
205 // Now test when the requested component is missing.
206 {"http://u:p@h:8/p?", Parsed::REF, true, 17},
207 {"http://u:p@h:8/p?q", Parsed::REF, true, 18},
208 {"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
209 {"http://u:p@h:8#r", Parsed::PATH, true, 14},
210 {"http://u:p@h/", Parsed::PORT, true, 12},
211 {"http://u:p@/", Parsed::HOST, true, 11},
212 // This case is a little weird. It will report that the password would
213 // start where the host begins. This is arguably correct, although you
214 // could also argue that it should start at the '@' sign. Doing it
215 // starting with the '@' sign is actually harder, so we don't bother.
216 {"http://u@h/", Parsed::PASSWORD, true, 9},
217 {"http://h/", Parsed::USERNAME, true, 7},
218 {"http:", Parsed::USERNAME, true, 5},
219 {"", Parsed::SCHEME, true, 0},
220 // Make sure a random component still works when there's nothing there.
221 {"", Parsed::REF, true, 0},
222 // File URLs are special with no host, so we test those.
223 {"file:///c:/foo", Parsed::USERNAME, true, 7},
224 {"file:///c:/foo", Parsed::PASSWORD, true, 7},
225 {"file:///c:/foo", Parsed::HOST, true, 7},
226 {"file:///c:/foo", Parsed::PATH, true, 7},
227 };
228 for (size_t i = 0; i < ARRAYSIZE(count_cases); i++) {
229 int length = static_cast<int>(strlen(count_cases[i].url));
230
231 // Simple test to distinguish file and standard URLs.
232 url_parse::Parsed parsed;
233 if (length > 0 && count_cases[i].url[0] == 'f')
234 url_parse::ParseFileURL(count_cases[i].url, length, &parsed);
235 else
236 url_parse::ParseStandardURL(count_cases[i].url, length, &parsed);
237
238 int chars_before = parsed.CountCharactersBefore(
239 count_cases[i].component, count_cases[i].include_delimiter);
240 EXPECT_EQ(count_cases[i].expected_count, chars_before);
241 }
242}
243
244// Standard --------------------------------------------------------------------
245
246// Input Scheme Usrname Passwd Host Port Path Query Ref
247// ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
248static URLParseCase cases[] = {
249 // Regular URL with all the parts
250{"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass", "foo", 21, "/bar;par","b", "c"},
251
252 // Known schemes should lean towards authority identification
253{"http:foo.com", "http", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
254
255 // Spaces!
256{"\t :foo.com \n", "", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
257{" foo.com ", NULL, NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
258{"a:\t foo.com", "a", NULL, NULL, "\t foo.com", -1, NULL, NULL, NULL},
259{"http://f:21/ b ? d # e ", "http", NULL, NULL, "f", 21, "/ b ", " d ", " e"},
260
261 // Invalid port numbers should be identified and turned into -2, empty port
262 // numbers should be -1. Spaces aren't allowed in port numbers
263{"http://f:/c", "http", NULL, NULL, "f", -1, "/c", NULL, NULL},
264{"http://f:0/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
265{"http://f:00000000000000/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
266{"http://f:00000000000000000000080/c", "http", NULL, NULL, "f", 80, "/c", NULL, NULL},
267{"http://f:b/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
268{"http://f: /c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
269{"http://f:\n/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
270{"http://f:fifty-two/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
271{"http://f:999999/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
272{"http://f: 21 / b ? d # e ", "http", NULL, NULL, "f", -2, "/ b ", " d ", " e"},
273
274 // Creative URLs missing key elements
275{"", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
276{" \t", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
277{":foo.com/", "", NULL, NULL, "foo.com", -1, "/", NULL, NULL},
278{":foo.com\\", "", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
279{":", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
280{":a", "", NULL, NULL, "a", -1, NULL, NULL, NULL},
281{":/", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
282{":\\", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
283{":#", "", NULL, NULL, NULL, -1, NULL, NULL, ""},
284{"#", NULL, NULL, NULL, NULL, -1, NULL, NULL, ""},
285{"#/", NULL, NULL, NULL, NULL, -1, NULL, NULL, "/"},
286{"#\\", NULL, NULL, NULL, NULL, -1, NULL, NULL, "\\"},
287{"#;?", NULL, NULL, NULL, NULL, -1, NULL, NULL, ";?"},
288{"?", NULL, NULL, NULL, NULL, -1, NULL, "", NULL},
289{"/", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
290{":23", "", NULL, NULL, "23", -1, NULL, NULL, NULL},
291{"/:23", "/", NULL, NULL, "23", -1, NULL, NULL, NULL},
292{"//", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
293{"::", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
294{"::23", "", NULL, NULL, NULL, 23, NULL, NULL, NULL},
295{"foo://", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
296
297 // Username/passwords and things that look like them
298{"http://a:b@c:29/d", "http", "a", "b", "c", 29, "/d", NULL, NULL},
299{"http::@c:29", "http", "", "", "c", 29, NULL, NULL, NULL},
300 // ... "]" in the password field isn't allowed, but we tolerate it here...
301{"http://&a:foo(b]c@d:2/", "http", "&a", "foo(b]c", "d", 2, "/", NULL, NULL},
302{"http://::@c@d:2", "http", "", ":@c", "d", 2, NULL, NULL, NULL},
303{"http://foo.com:b@d/", "http", "foo.com", "b", "d", -1, "/", NULL, NULL},
304
305{"http://foo.com/\\@", "http", NULL, NULL, "foo.com", -1, "/\\@", NULL, NULL},
306{"http:\\\\foo.com\\", "http", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
307{"http:\\\\a\\b:c\\[email protected]\\", "http", NULL, NULL, "a", -1, "\\b:c\\[email protected]\\", NULL, NULL},
308
309 // Tolerate different numbers of slashes.
310{"foo:/", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
311{"foo:/bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
312{"foo://///////", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
313{"foo://///////bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
314{"foo:////://///", "foo", NULL, NULL, NULL, -1, "/////", NULL, NULL},
315
316 // Raw file paths on Windows aren't handled by the parser.
317{"c:/foo", "c", NULL, NULL, "foo", -1, NULL, NULL, NULL},
318{"//foo/bar", NULL, NULL, NULL, "foo", -1, "/bar", NULL, NULL},
319
320 // Use the first question mark for the query and the ref.
321{"http://foo/path;a??e#f#g", "http", NULL, NULL, "foo", -1, "/path;a", "?e", "f#g"},
322{"http://foo/abcd?efgh?ijkl", "http", NULL, NULL, "foo", -1, "/abcd", "efgh?ijkl", NULL},
323{"http://foo/abcd#foo?bar", "http", NULL, NULL, "foo", -1, "/abcd", NULL, "foo?bar"},
324
325 // IPv6, check also interesting uses of colons.
326{"[61:24:74]:98", "[61", NULL, NULL, "24:74]", 98, NULL, NULL, NULL},
327{"http://[61:27]:98", "http", NULL, NULL, "[61:27]", 98, NULL, NULL, NULL},
328{"http:[61:27]/:foo", "http", NULL, NULL, "[61:27]", -1, "/:foo", NULL, NULL},
329{"http://[1::2]:3:4", "http", NULL, NULL, "[1::2]:3", 4, NULL, NULL, NULL},
330
331 // Partially-complete IPv6 literals, and related cases.
332{"http://2001::1", "http", NULL, NULL, "2001:", 1, NULL, NULL, NULL},
333{"http://[2001::1", "http", NULL, NULL, "[2001::1", -1, NULL, NULL, NULL},
334{"http://2001::1]", "http", NULL, NULL, "2001::1]", -1, NULL, NULL, NULL},
335{"http://2001::1]:80", "http", NULL, NULL, "2001::1]", 80, NULL, NULL, NULL},
336{"http://[2001::1]", "http", NULL, NULL, "[2001::1]", -1, NULL, NULL, NULL},
337{"http://[2001::1]:80", "http", NULL, NULL, "[2001::1]", 80, NULL, NULL, NULL},
338{"http://[[::]]", "http", NULL, NULL, "[[::]]", -1, NULL, NULL, NULL},
339
340};
341
342TEST(URLParser, Standard) {
343 // Declared outside for loop to try to catch cases in init() where we forget
344 // to reset something that is reset by the constructor.
345 url_parse::Parsed parsed;
346 for (size_t i = 0; i < arraysize(cases); i++) {
347 const char* url = cases[i].input;
348 url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
349 int port = url_parse::ParsePort(url, parsed.port);
350
351 EXPECT_TRUE(ComponentMatches(url, cases[i].scheme, parsed.scheme));
352 EXPECT_TRUE(ComponentMatches(url, cases[i].username, parsed.username));
353 EXPECT_TRUE(ComponentMatches(url, cases[i].password, parsed.password));
354 EXPECT_TRUE(ComponentMatches(url, cases[i].host, parsed.host));
355 EXPECT_EQ(cases[i].port, port);
356 EXPECT_TRUE(ComponentMatches(url, cases[i].path, parsed.path));
357 EXPECT_TRUE(ComponentMatches(url, cases[i].query, parsed.query));
358 EXPECT_TRUE(ComponentMatches(url, cases[i].ref, parsed.ref));
359 }
360}
361
362// PathURL --------------------------------------------------------------------
363
364// Various incarnations of path URLs.
365static PathURLParseCase path_cases[] = {
366{"", NULL, NULL},
367{":", "", NULL},
368{":/", "", "/"},
369{"/", NULL, "/"},
370{" This is \\interesting// \t", NULL, "This is \\interesting//"},
371{"about:", "about", NULL},
372{"about:blank", "about", "blank"},
373{" about: blank ", "about", " blank"},
374{"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?foo\");"},
375};
376
377TEST(URLParser, PathURL) {
378 // Declared outside for loop to try to catch cases in init() where we forget
379 // to reset something that is reset by the construtor.
380 url_parse::Parsed parsed;
381 for (size_t i = 0; i < arraysize(path_cases); i++) {
382 const char* url = path_cases[i].input;
383 url_parse::ParsePathURL(url, static_cast<int>(strlen(url)), &parsed);
384
385 EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme));
386 EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.path));
387
388 // The remaining components are never used for path urls.
389 ExpectInvalidComponent(parsed.username);
390 ExpectInvalidComponent(parsed.password);
391 ExpectInvalidComponent(parsed.host);
392 ExpectInvalidComponent(parsed.port);
393 ExpectInvalidComponent(parsed.query);
394 ExpectInvalidComponent(parsed.ref);
395 }
396}
397
398#ifdef WIN32
399
400// WindowsFile ----------------------------------------------------------------
401
402// Various incarnations of file URLs. These are for Windows only.
403static URLParseCase file_cases[] = {
404{"file:server", "file", NULL, NULL, "server", -1, NULL, NULL, NULL},
405{" file: server \t", "file", NULL, NULL, " server",-1, NULL, NULL, NULL},
406{"FiLe:c|", "FiLe", NULL, NULL, NULL, -1, "c|", NULL, NULL},
407{"FILE:/\\\\/server/file", "FILE", NULL, NULL, "server", -1, "/file", NULL, NULL},
408{"file://server/", "file", NULL, NULL, "server", -1, "/", NULL, NULL},
409{"file://localhost/c:/", "file", NULL, NULL, NULL, -1, "/c:/", NULL, NULL},
410{"file://127.0.0.1/c|\\", "file", NULL, NULL, NULL, -1, "/c|\\", NULL, NULL},
411{"file:/", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
412{"file:", "file", NULL, NULL, NULL, -1, NULL, NULL, NULL},
413 // If there is a Windows drive letter, treat any number of slashes as the
414 // path part.
415{"file:c:\\fo\\b", "file", NULL, NULL, NULL, -1, "c:\\fo\\b", NULL, NULL},
416{"file:/c:\\foo/bar", "file", NULL, NULL, NULL, -1, "/c:\\foo/bar",NULL, NULL},
417{"file://c:/f\\b", "file", NULL, NULL, NULL, -1, "/c:/f\\b", NULL, NULL},
418{"file:///C:/foo", "file", NULL, NULL, NULL, -1, "/C:/foo", NULL, NULL},
419{"file://///\\/\\/c:\\f\\b", "file", NULL, NULL, NULL, -1, "/c:\\f\\b", NULL, NULL},
420 // If there is not a drive letter, we should treat is as UNC EXCEPT for
421 // three slashes, which we treat as a Unix style path.
422{"file:server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
423{"file:/server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
424{"file://server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
425{"file:///server/file", "file", NULL, NULL, NULL, -1, "/server/file",NULL, NULL},
426{"file://\\server/file", "file", NULL, NULL, NULL, -1, "\\server/file",NULL, NULL},
427{"file:////server/file", "file", NULL, NULL, "server", -1, "/file", NULL, NULL},
428 // Queries and refs are valid for file URLs as well.
429{"file:///C:/foo.html?#", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "", ""},
430{"file:///C:/foo.html?query=yes#ref", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "query=yes", "ref"},
431};
432
433TEST(URLParser, WindowsFile) {
434 // Declared outside for loop to try to catch cases in init() where we forget
435 // to reset something that is reset by the construtor.
436 url_parse::Parsed parsed;
437 for (int i = 0; i < arraysize(file_cases); i++) {
438 const char* url = file_cases[i].input;
439 url_parse::ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
440 int port = url_parse::ParsePort(url, parsed.port);
441
442 EXPECT_TRUE(ComponentMatches(url, file_cases[i].scheme, parsed.scheme));
443 EXPECT_TRUE(ComponentMatches(url, file_cases[i].username, parsed.username));
444 EXPECT_TRUE(ComponentMatches(url, file_cases[i].password, parsed.password));
445 EXPECT_TRUE(ComponentMatches(url, file_cases[i].host, parsed.host));
446 EXPECT_EQ(file_cases[i].port, port);
447 EXPECT_TRUE(ComponentMatches(url, file_cases[i].path, parsed.path));
448 EXPECT_TRUE(ComponentMatches(url, file_cases[i].query, parsed.query));
449 EXPECT_TRUE(ComponentMatches(url, file_cases[i].ref, parsed.ref));
450 }
451}
452
453#endif // WIN32
454
455TEST(URLParser, ExtractFileName) {
456 struct FileCase {
457 const char* input;
458 const char* expected;
459 } file_cases[] = {
460 {"http://www.google.com", NULL},
461 {"http://www.google.com/", ""},
462 {"http://www.google.com/search", "search"},
463 {"http://www.google.com/search/", ""},
464 {"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
465 {"http://www.google.com/foo/bar.html#ref", "bar.html"},
466 {"http://www.google.com/search/;param", ""},
467 {"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
468 {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html;foo"},
469 {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
470 };
471
472 for (size_t i = 0; i < ARRAYSIZE(file_cases); i++) {
473 const char* url = file_cases[i].input;
474 int len = static_cast<int>(strlen(url));
475
476 url_parse::Parsed parsed;
477 url_parse::ParseStandardURL(url, len, &parsed);
478
479 url_parse::Component file_name;
480 url_parse::ExtractFileName(url, parsed.path, &file_name);
481
482 EXPECT_TRUE(ComponentMatches(url, file_cases[i].expected, file_name));
483 }
484}
485
486// Returns true if the parameter with index |parameter| in the given URL's
487// query string. The expected key can be NULL to indicate no such key index
488// should exist. The parameter number is 1-based.
489static bool NthParameterIs(const char* url,
490 int parameter,
491 const char* expected_key,
492 const char* expected_value) {
493 url_parse::Parsed parsed;
494 url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
495
496 url_parse::Component query = parsed.query;
497
498 for (int i = 1; i <= parameter; i++) {
499 url_parse::Component key, value;
500 if (!url_parse::ExtractQueryKeyValue(url, &query, &key, &value)) {
501 if (parameter >= i && !expected_key)
502 return true; // Expected nonexistant key, got one.
503 return false; // Not enough keys.
504 }
505
506 if (i == parameter) {
507 if (!expected_key)
508 return false;
509
510 if (strncmp(&url[key.begin], expected_key, key.len) != 0)
511 return false;
512 if (strncmp(&url[value.begin], expected_value, value.len) != 0)
513 return false;
514 return true;
515 }
516 }
517 return expected_key == NULL; // We didn't find that many parameters.
518}
519
520TEST(URLParser, ExtractQueryKeyValue) {
521 EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, NULL, NULL));
522
523 // Basic case.
524 char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
525 EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
526 EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
527 EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
528 EXPECT_TRUE(NthParameterIs(a, 4, NULL, NULL));
529
530 // Empty param at the end.
531 char b[] = "http://www.google.com?foo=bar&";
532 EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
533 EXPECT_TRUE(NthParameterIs(b, 2, NULL, NULL));
534
535 // Empty param at the beginning.
536 char c[] = "http://www.google.com?&foo=bar";
537 EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
538 EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
539 EXPECT_TRUE(NthParameterIs(c, 3, NULL, NULL));
540
541 // Empty key with value.
542 char d[] = "http://www.google.com?=foo";
543 EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
544 EXPECT_TRUE(NthParameterIs(d, 2, NULL, NULL));
545
546 // Empty value with key.
547 char e[] = "http://www.google.com?foo=";
548 EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
549 EXPECT_TRUE(NthParameterIs(e, 2, NULL, NULL));
550
551 // Empty key and values.
552 char f[] = "http://www.google.com?&&==&=";
553 EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
554 EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
555 EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
556 EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
557 EXPECT_TRUE(NthParameterIs(f, 5, NULL, NULL));
558}
559
560// MailtoURL --------------------------------------------------------------------
561
562static MailtoURLParseCase mailto_cases[] = {
563//|input |scheme |path |query
564{"mailto:[email protected]", "mailto", "[email protected]", NULL},
565{" mailto: to \t", "mailto", " to", NULL},
566{"mailto:addr1%2C%20addr2 ", "mailto", "addr1%2C%20addr2", NULL},
567{"Mailto:addr1, addr2 ", "Mailto", "addr1, addr2", NULL},
568{"mailto:addr1:addr2 ", "mailto", "addr1:addr2", NULL},
569{"mailto:?to=addr1,addr2", "mailto", NULL, "to=addr1,addr2"},
570{"mailto:?to=addr1%2C%20addr2", "mailto", NULL, "to=addr1%2C%20addr2"},
571{"mailto:addr1?to=addr2", "mailto", "addr1", "to=addr2"},
572{"mailto:?body=#foobar#", "mailto", NULL, "body=#foobar#",},
573{"mailto:#?body=#foobar#", "mailto", "#", "body=#foobar#"},
574};
575
576TEST(URLParser, MailtoUrl) {
577 // Declared outside for loop to try to catch cases in init() where we forget
578 // to reset something that is reset by the construtor.
579 url_parse::Parsed parsed;
580 for (size_t i = 0; i < arraysize(mailto_cases); ++i) {
581 const char* url = mailto_cases[i].input;
582 url_parse::ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
583 int port = url_parse::ParsePort(url, parsed.port);
584
585 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].scheme, parsed.scheme));
586 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].path, parsed.path));
587 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].query, parsed.query));
588 EXPECT_EQ(url_parse::PORT_UNSPECIFIED, port);
589
590 // The remaining components are never used for mailto urls.
591 ExpectInvalidComponent(parsed.username);
592 ExpectInvalidComponent(parsed.password);
593 ExpectInvalidComponent(parsed.port);
594 ExpectInvalidComponent(parsed.ref);
595 }
596}
597
598// Various incarnations of filesystem URLs.
599static FileSystemURLParseCase filesystem_cases[] = {
600 // Regular URL with all the parts
601{"filesystem:http://user:pass@foo:21/temporary/bar;par?b#c", "http", "user", "pass", "foo", 21, "/temporary", "/bar;par", "b", "c"},
602{"filesystem:https://foo/persistent/bar;par/", "https", NULL, NULL, "foo", -1, "/persistent", "/bar;par/", NULL, NULL},
603{"filesystem:file:///persistent/bar;par/", "file", NULL, NULL, NULL, -1, "/persistent", "/bar;par/", NULL, NULL},
604{"filesystem:file:///persistent/bar;par/?query#ref", "file", NULL, NULL, NULL, -1, "/persistent", "/bar;par/", "query", "ref"},
605{"filesystem:file:///persistent", "file", NULL, NULL, NULL, -1, "/persistent", "", NULL, NULL},
606};
607
608TEST(URLParser, FileSystemURL) {
609 // Declared outside for loop to try to catch cases in init() where we forget
610 // to reset something that is reset by the construtor.
611 url_parse::Parsed parsed;
612 for (size_t i = 0; i < arraysize(filesystem_cases); i++) {
613 const FileSystemURLParseCase* parsecase = &filesystem_cases[i];
614 const char* url = parsecase->input;
615 url_parse::ParseFileSystemURL(url, static_cast<int>(strlen(url)), &parsed);
616
617 EXPECT_TRUE(ComponentMatches(url, "filesystem", parsed.scheme));
618 EXPECT_EQ(!parsecase->inner_scheme, !parsed.inner_parsed());
619 // Only check the inner_parsed if there is one.
620 if (parsed.inner_parsed()) {
621 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_scheme,
622 parsed.inner_parsed()->scheme));
623 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_username,
624 parsed.inner_parsed()->username));
625 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_password,
626 parsed.inner_parsed()->password));
627 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_host,
628 parsed.inner_parsed()->host));
629 int port = url_parse::ParsePort(url, parsed.inner_parsed()->port);
630 EXPECT_EQ(parsecase->inner_port, port);
631
632 // The remaining components are never used for filesystem urls.
633 ExpectInvalidComponent(parsed.inner_parsed()->query);
634 ExpectInvalidComponent(parsed.inner_parsed()->ref);
635 }
636
637 EXPECT_TRUE(ComponentMatches(url, parsecase->path, parsed.path));
638 EXPECT_TRUE(ComponentMatches(url, parsecase->query, parsed.query));
639 EXPECT_TRUE(ComponentMatches(url, parsecase->ref, parsed.ref));
640
641 // The remaining components are never used for filesystem urls.
642 ExpectInvalidComponent(parsed.username);
643 ExpectInvalidComponent(parsed.password);
644 ExpectInvalidComponent(parsed.host);
645 ExpectInvalidComponent(parsed.port);
646 }
647}
648
[email protected]ecf0d742013-04-15 01:22:29649} // namespace
650} // namespace url_parse