Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: url/url_parse_unittest.cc

Issue 13821004: Move googleurl into the Chrome repo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « url/url_parse_internal.h ('k') | url/url_test_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // 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
30 #include "base/basictypes.h"
31 #include "googleurl/src/url_parse.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33
34 // Some implementations of base/basictypes.h may define ARRAYSIZE.
35 // If it's not defined, we define it to the ARRAYSIZE_UNSAFE macro
36 // which is in our version of basictypes.h.
37 #ifndef ARRAYSIZE
38 #define ARRAYSIZE ARRAYSIZE_UNSAFE
39 #endif
40
41 // Interesting IE file:isms...
42 //
43 // file:/foo/bar file:///foo/bar
44 // The result here seems totally invalid!?!? This isn't UNC.
45 //
46 // file:/
47 // file:// or any other number of slashes
48 // IE6 doesn't do anything at all if you click on this link. No error:
49 // nothing. IE6's history system seems to always color this link, so I'm
50 // guessing that it maps internally to the empty URL.
51 //
52 // C:\ file:///C:/
53 // / file:///C:/
54 // /foo file:///C:/foo
55 // Interestingly, IE treats "/" as an alias for "c:\", which makes sense,
56 // but is weird to think about on Windows.
57 //
58 // file:foo/ file:foo/ (invalid?!?!?)
59 // file:/foo/ file:///foo/ (invalid?!?!?)
60 // file://foo/ file://foo/ (UNC to server "foo")
61 // file:///foo/ file:///foo/ (invalid)
62 // file:////foo/ file://foo/ (UNC to server "foo")
63 // Any more than four slashes is also treated as UNC.
64 //
65 // file:C:/ file://C:/
66 // file:/C:/ file://C:/
67 // The number of slashes after "file:" don't matter if the thing following
68 // it looks like an absolute drive path. Also, slashes and backslashes are
69 // equally valid here.
70
71 namespace {
72
73 // Used for regular URL parse cases.
74 struct URLParseCase {
75 const char* input;
76
77 const char* scheme;
78 const char* username;
79 const char* password;
80 const char* host;
81 int port;
82 const char* path;
83 const char* query;
84 const char* ref;
85 };
86
87 // Simpler version of URLParseCase for testing path URLs.
88 struct PathURLParseCase {
89 const char* input;
90
91 const char* scheme;
92 const char* path;
93 };
94
95 // Simpler version of URLParseCase for testing mailto URLs.
96 struct MailtoURLParseCase {
97 const char* input;
98
99 const char* scheme;
100 const char* path;
101 const char* query;
102 };
103
104 // More complicated version of URLParseCase for testing filesystem URLs.
105 struct FileSystemURLParseCase {
106 const char* input;
107
108 const char* inner_scheme;
109 const char* inner_username;
110 const char* inner_password;
111 const char* inner_host;
112 int inner_port;
113 const char* inner_path;
114 const char* path;
115 const char* query;
116 const char* ref;
117 };
118
119 bool ComponentMatches(const char* input,
120 const char* reference,
121 const url_parse::Component& component) {
122 // If the component is nonexistant (length == -1), it should begin at 0.
123 EXPECT_TRUE(component.len >= 0 || component.len == -1);
124
125 // Begin should be valid.
126 EXPECT_LE(0, component.begin);
127
128 // A NULL reference means the component should be nonexistant.
129 if (!reference)
130 return component.len == -1;
131 if (component.len < 0)
132 return false; // Reference is not NULL but we don't have anything
133
134 if (strlen(reference) != static_cast<size_t>(component.len))
135 return false; // Lengths don't match
136
137 // Now check the actual characters.
138 return strncmp(reference, &input[component.begin], component.len) == 0;
139 }
140
141 void ExpectInvalidComponent(const url_parse::Component& component) {
142 EXPECT_EQ(0, component.begin);
143 EXPECT_EQ(-1, component.len);
144 }
145
146 } // namespace
147
148 // Parsed ----------------------------------------------------------------------
149
150 TEST(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
179 TEST(URLParser, CountCharactersBefore) {
180 using namespace url_parse;
181 struct CountCase {
182 const char* url;
183 Parsed::ComponentType component;
184 bool include_delimiter;
185 int expected_count;
186 } count_cases[] = {
187 // Test each possibility in the case where all components are present.
188 // 0 1 2
189 // 0123456789012345678901
190 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, true, 0},
191 {"http://u:p@h:8/p?q#r", Parsed::SCHEME, false, 0},
192 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, true, 7},
193 {"http://u:p@h:8/p?q#r", Parsed::USERNAME, false, 7},
194 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, true, 9},
195 {"http://u:p@h:8/p?q#r", Parsed::PASSWORD, false, 9},
196 {"http://u:p@h:8/p?q#r", Parsed::HOST, true, 11},
197 {"http://u:p@h:8/p?q#r", Parsed::HOST, false, 11},
198 {"http://u:p@h:8/p?q#r", Parsed::PORT, true, 12},
199 {"http://u:p@h:8/p?q#r", Parsed::PORT, false, 13},
200 {"http://u:p@h:8/p?q#r", Parsed::PATH, false, 14},
201 {"http://u:p@h:8/p?q#r", Parsed::PATH, true, 14},
202 {"http://u:p@h:8/p?q#r", Parsed::QUERY, true, 16},
203 {"http://u:p@h:8/p?q#r", Parsed::QUERY, false, 17},
204 {"http://u:p@h:8/p?q#r", Parsed::REF, true, 18},
205 {"http://u:p@h:8/p?q#r", Parsed::REF, false, 19},
206 // Now test when the requested component is missing.
207 {"http://u:p@h:8/p?", Parsed::REF, true, 17},
208 {"http://u:p@h:8/p?q", Parsed::REF, true, 18},
209 {"http://u:p@h:8/p#r", Parsed::QUERY, true, 16},
210 {"http://u:p@h:8#r", Parsed::PATH, true, 14},
211 {"http://u:p@h/", Parsed::PORT, true, 12},
212 {"http://u:p@/", Parsed::HOST, true, 11},
213 // This case is a little weird. It will report that the password would
214 // start where the host begins. This is arguably correct, although you
215 // could also argue that it should start at the '@' sign. Doing it
216 // starting with the '@' sign is actually harder, so we don't bother.
217 {"http://u@h/", Parsed::PASSWORD, true, 9},
218 {"http://h/", Parsed::USERNAME, true, 7},
219 {"http:", Parsed::USERNAME, true, 5},
220 {"", Parsed::SCHEME, true, 0},
221 // Make sure a random component still works when there's nothing there.
222 {"", Parsed::REF, true, 0},
223 // File URLs are special with no host, so we test those.
224 {"file:///c:/foo", Parsed::USERNAME, true, 7},
225 {"file:///c:/foo", Parsed::PASSWORD, true, 7},
226 {"file:///c:/foo", Parsed::HOST, true, 7},
227 {"file:///c:/foo", Parsed::PATH, true, 7},
228 };
229 for (size_t i = 0; i < ARRAYSIZE(count_cases); i++) {
230 int length = static_cast<int>(strlen(count_cases[i].url));
231
232 // Simple test to distinguish file and standard URLs.
233 url_parse::Parsed parsed;
234 if (length > 0 && count_cases[i].url[0] == 'f')
235 url_parse::ParseFileURL(count_cases[i].url, length, &parsed);
236 else
237 url_parse::ParseStandardURL(count_cases[i].url, length, &parsed);
238
239 int chars_before = parsed.CountCharactersBefore(
240 count_cases[i].component, count_cases[i].include_delimiter);
241 EXPECT_EQ(count_cases[i].expected_count, chars_before);
242 }
243 }
244
245 // Standard --------------------------------------------------------------------
246
247 // Input Scheme Usrname Passwd Host P ort Path Query Ref
248 // ------------------------------------ ------- ------- ---------- ------------ --- ---------- ------------ -----
249 static URLParseCase cases[] = {
250 // Regular URL with all the parts
251 {"http://user:pass@foo:21/bar;par?b#c", "http", "user", "pass", "foo", 21, "/bar;par","b", "c"},
252
253 // Known schemes should lean towards authority identification
254 {"http:foo.com", "http", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
255
256 // Spaces!
257 {"\t :foo.com \n", "", NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
258 {" foo.com ", NULL, NULL, NULL, "foo.com", -1, NULL, NULL, NULL},
259 {"a:\t foo.com", "a", NULL, NULL, "\t foo.com", -1, NULL, NULL, NULL},
260 {"http://f:21/ b ? d # e ", "http", NULL, NULL, "f", 21, "/ b ", " d ", " e"},
261
262 // Invalid port numbers should be identified and turned into -2, empty port
263 // numbers should be -1. Spaces aren't allowed in port numbers
264 {"http://f:/c", "http", NULL, NULL, "f", -1, "/c", NULL, NULL},
265 {"http://f:0/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
266 {"http://f:00000000000000/c", "http", NULL, NULL, "f", 0, "/c", NULL, NULL},
267 {"http://f:00000000000000000000080/c", "http", NULL, NULL, "f", 80, "/c", NULL, NULL},
268 {"http://f:b/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
269 {"http://f: /c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
270 {"http://f:\n/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
271 {"http://f:fifty-two/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
272 {"http://f:999999/c", "http", NULL, NULL, "f", -2, "/c", NULL, NULL},
273 {"http://f: 21 / b ? d # e ", "http", NULL, NULL, "f", -2, "/ b ", " d ", " e"},
274
275 // Creative URLs missing key elements
276 {"", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
277 {" \t", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
278 {":foo.com/", "", NULL, NULL, "foo.com", -1, "/", NULL, NULL},
279 {":foo.com\\", "", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
280 {":", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
281 {":a", "", NULL, NULL, "a", -1, NULL, NULL, NULL},
282 {":/", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
283 {":\\", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
284 {":#", "", 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},
290 {"/", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
291 {":23", "", NULL, NULL, "23", -1, NULL, NULL, NULL},
292 {"/:23", "/", NULL, NULL, "23", -1, NULL, NULL, NULL},
293 {"//", NULL, NULL, NULL, NULL, -1, NULL, NULL, NULL},
294 {"::", "", NULL, NULL, NULL, -1, NULL, NULL, NULL},
295 {"::23", "", NULL, NULL, NULL, 23, NULL, NULL, NULL},
296 {"foo://", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
297
298 // Username/passwords and things that look like them
299 {"http://a:b@c:29/d", "http", "a", "b", "c", 29, "/d", NULL, NULL},
300 {"http::@c:29", "http", "", "", "c", 29, NULL, NULL, NULL},
301 // ... "]" in the password field isn't allowed, but we tolerate it here...
302 {"http://&a:foo(b]c@d:2/", "http", "&a", "foo(b]c", "d", 2, "/", NULL, NULL},
303 {"http://::@c@d:2", "http", "", ":@c", "d", 2, NULL, NULL, NULL},
304 {"http://foo.com:b@d/", "http", "foo.com", "b", "d", -1, "/", NULL, NULL},
305
306 {"http://foo.com/\\@", "http", NULL, NULL, "foo.com", -1, "/\\@", NULL, NULL},
307 {"http:\\\\foo.com\\", "http", NULL, NULL, "foo.com", -1, "\\", NULL, NULL},
308 {"http:\\\\a\\b:c\\d@foo.com\\", "http", NULL, NULL, "a", -1, "\\b:c\\d@foo.com\\", NULL, NULL},
309
310 // Tolerate different numbers of slashes.
311 {"foo:/", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
312 {"foo:/bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
313 {"foo://///////", "foo", NULL, NULL, NULL, -1, NULL, NULL, NULL},
314 {"foo://///////bar.com/", "foo", NULL, NULL, "bar.com", -1, "/", NULL, NULL},
315 {"foo:////://///", "foo", NULL, NULL, NULL, -1, "/////", NULL, NULL},
316
317 // Raw file paths on Windows aren't handled by the parser.
318 {"c:/foo", "c", NULL, NULL, "foo", -1, NULL, NULL, NULL},
319 {"//foo/bar", NULL, NULL, NULL, "foo", -1, "/bar", NULL, NULL},
320
321 // Use the first question mark for the query and the ref.
322 {"http://foo/path;a??e#f#g", "http", NULL, NULL, "foo", -1, "/path;a", "?e", "f#g"},
323 {"http://foo/abcd?efgh?ijkl", "http", NULL, NULL, "foo", -1, "/abcd", "efgh?ijkl", NULL},
324 {"http://foo/abcd#foo?bar", "http", NULL, NULL, "foo", -1, "/abcd", NULL, "foo?bar"},
325
326 // IPv6, check also interesting uses of colons.
327 {"[61:24:74]:98", "[61", NULL, NULL, "24:74]", 98, NULL, NULL, NULL},
328 {"http://[61:27]:98", "http", NULL, NULL, "[61:27]", 98, NULL, NULL, NULL},
329 {"http:[61:27]/:foo", "http", NULL, NULL, "[61:27]", -1, "/:foo", NULL, NULL},
330 {"http://[1::2]:3:4", "http", NULL, NULL, "[1::2]:3", 4, NULL, NULL, NULL},
331
332 // Partially-complete IPv6 literals, and related cases.
333 {"http://2001::1", "http", NULL, NULL, "2001:", 1, NULL, NULL, NULL},
334 {"http://[2001::1", "http", NULL, NULL, "[2001::1", -1, NULL, NULL, NULL},
335 {"http://2001::1]", "http", NULL, NULL, "2001::1]", -1, NULL, NULL, NULL},
336 {"http://2001::1]:80", "http", NULL, NULL, "2001::1]", 80, NULL, NULL, NULL},
337 {"http://[2001::1]", "http", NULL, NULL, "[2001::1]", -1, NULL, NULL, NULL},
338 {"http://[2001::1]:80", "http", NULL, NULL, "[2001::1]", 80, NULL, NULL, NULL},
339 {"http://[[::]]", "http", NULL, NULL, "[[::]]", -1, NULL, NULL, NULL},
340
341 };
342
343 TEST(URLParser, Standard) {
344 // Declared outside for loop to try to catch cases in init() where we forget
345 // to reset something that is reset by the constructor.
346 url_parse::Parsed parsed;
347 for (size_t i = 0; i < arraysize(cases); i++) {
348 const char* url = cases[i].input;
349 url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
350 int port = url_parse::ParsePort(url, parsed.port);
351
352 EXPECT_TRUE(ComponentMatches(url, cases[i].scheme, parsed.scheme));
353 EXPECT_TRUE(ComponentMatches(url, cases[i].username, parsed.username));
354 EXPECT_TRUE(ComponentMatches(url, cases[i].password, parsed.password));
355 EXPECT_TRUE(ComponentMatches(url, cases[i].host, parsed.host));
356 EXPECT_EQ(cases[i].port, port);
357 EXPECT_TRUE(ComponentMatches(url, cases[i].path, parsed.path));
358 EXPECT_TRUE(ComponentMatches(url, cases[i].query, parsed.query));
359 EXPECT_TRUE(ComponentMatches(url, cases[i].ref, parsed.ref));
360 }
361 }
362
363 // PathURL --------------------------------------------------------------------
364
365 // Various incarnations of path URLs.
366 static PathURLParseCase path_cases[] = {
367 {"", NULL, NULL},
368 {":", "", NULL},
369 {":/", "", "/"},
370 {"/", NULL, "/"},
371 {" This is \\interesting// \t", NULL, "This is \\interestin g//"},
372 {"about:", "about", NULL},
373 {"about:blank", "about", "blank"},
374 {" about: blank ", "about", " blank"},
375 {"javascript :alert(\"He:/l\\l#o?foo\"); ", "javascript ", "alert(\"He:/l\\l#o?f oo\");"},
376 };
377
378 TEST(URLParser, PathURL) {
379 // Declared outside for loop to try to catch cases in init() where we forget
380 // to reset something that is reset by the construtor.
381 url_parse::Parsed parsed;
382 for (size_t i = 0; i < arraysize(path_cases); i++) {
383 const char* url = path_cases[i].input;
384 url_parse::ParsePathURL(url, static_cast<int>(strlen(url)), &parsed);
385
386 EXPECT_TRUE(ComponentMatches(url, path_cases[i].scheme, parsed.scheme));
387 EXPECT_TRUE(ComponentMatches(url, path_cases[i].path, parsed.path));
388
389 // The remaining components are never used for path urls.
390 ExpectInvalidComponent(parsed.username);
391 ExpectInvalidComponent(parsed.password);
392 ExpectInvalidComponent(parsed.host);
393 ExpectInvalidComponent(parsed.port);
394 ExpectInvalidComponent(parsed.query);
395 ExpectInvalidComponent(parsed.ref);
396 }
397 }
398
399 #ifdef WIN32
400
401 // WindowsFile ----------------------------------------------------------------
402
403 // Various incarnations of file URLs. These are for Windows only.
404 static URLParseCase file_cases[] = {
405 {"file:server", "file", NULL, NULL, "server", -1, NULL, NU LL, NULL},
406 {" file: server \t", "file", NULL, NULL, " server",-1, NULL, NU LL, NULL},
407 {"FiLe:c|", "FiLe", NULL, NULL, NULL, -1, "c|", NU LL, NULL},
408 {"FILE:/\\\\/server/file", "FILE", NULL, NULL, "server", -1, "/file", NU LL, NULL},
409 {"file://server/", "file", NULL, NULL, "server", -1, "/", NU LL, NULL},
410 {"file://localhost/c:/", "file", NULL, NULL, NULL, -1, "/c:/", NU LL, NULL},
411 {"file://127.0.0.1/c|\\", "file", NULL, NULL, NULL, -1, "/c|\\", NU LL, NULL},
412 {"file:/", "file", NULL, NULL, NULL, -1, NULL, NU LL, NULL},
413 {"file:", "file", NULL, NULL, NULL, -1, NULL, NU LL, NULL},
414 // If there is a Windows drive letter, treat any number of slashes as the
415 // path part.
416 {"file:c:\\fo\\b", "file", NULL, NULL, NULL, -1, "c:\\fo\\b", NU LL, NULL},
417 {"file:/c:\\foo/bar", "file", NULL, NULL, NULL, -1, "/c:\\foo/bar",NU LL, NULL},
418 {"file://c:/f\\b", "file", NULL, NULL, NULL, -1, "/c:/f\\b", NU LL, NULL},
419 {"file:///C:/foo", "file", NULL, NULL, NULL, -1, "/C:/foo", NU LL, NULL},
420 {"file://///\\/\\/c:\\f\\b", "file", NULL, NULL, NULL, -1, "/c:\\f\\b", NU LL, NULL},
421 // If there is not a drive letter, we should treat is as UNC EXCEPT for
422 // three slashes, which we treat as a Unix style path.
423 {"file:server/file", "file", NULL, NULL, "server", -1, "/file", NU LL, NULL},
424 {"file:/server/file", "file", NULL, NULL, "server", -1, "/file", NU LL, NULL},
425 {"file://server/file", "file", NULL, NULL, "server", -1, "/file", NU LL, NULL},
426 {"file:///server/file", "file", NULL, NULL, NULL, -1, "/server/file",NU LL, NULL},
427 {"file://\\server/file", "file", NULL, NULL, NULL, -1, "\\server/file",N ULL, NULL},
428 {"file:////server/file", "file", NULL, NULL, "server", -1, "/file", NU LL, NULL},
429 // Queries and refs are valid for file URLs as well.
430 {"file:///C:/foo.html?#", "file", NULL, NULL, NULL, -1, "/C:/foo.html", "", ""},
431 {"file:///C:/foo.html?query=yes#ref", "file", NULL, NULL, NULL, -1, "/C:/foo.htm l", "query=yes", "ref"},
432 };
433
434 TEST(URLParser, WindowsFile) {
435 // Declared outside for loop to try to catch cases in init() where we forget
436 // to reset something that is reset by the construtor.
437 url_parse::Parsed parsed;
438 for (int i = 0; i < arraysize(file_cases); i++) {
439 const char* url = file_cases[i].input;
440 url_parse::ParseFileURL(url, static_cast<int>(strlen(url)), &parsed);
441 int port = url_parse::ParsePort(url, parsed.port);
442
443 EXPECT_TRUE(ComponentMatches(url, file_cases[i].scheme, parsed.scheme));
444 EXPECT_TRUE(ComponentMatches(url, file_cases[i].username, parsed.username));
445 EXPECT_TRUE(ComponentMatches(url, file_cases[i].password, parsed.password));
446 EXPECT_TRUE(ComponentMatches(url, file_cases[i].host, parsed.host));
447 EXPECT_EQ(file_cases[i].port, port);
448 EXPECT_TRUE(ComponentMatches(url, file_cases[i].path, parsed.path));
449 EXPECT_TRUE(ComponentMatches(url, file_cases[i].query, parsed.query));
450 EXPECT_TRUE(ComponentMatches(url, file_cases[i].ref, parsed.ref));
451 }
452 }
453
454 #endif // WIN32
455
456 TEST(URLParser, ExtractFileName) {
457 struct FileCase {
458 const char* input;
459 const char* expected;
460 } file_cases[] = {
461 {"http://www.google.com", NULL},
462 {"http://www.google.com/", ""},
463 {"http://www.google.com/search", "search"},
464 {"http://www.google.com/search/", ""},
465 {"http://www.google.com/foo/bar.html?baz=22", "bar.html"},
466 {"http://www.google.com/foo/bar.html#ref", "bar.html"},
467 {"http://www.google.com/search/;param", ""},
468 {"http://www.google.com/foo/bar.html;param#ref", "bar.html"},
469 {"http://www.google.com/foo/bar.html;foo;param#ref", "bar.html;foo"},
470 {"http://www.google.com/foo/bar.html?query#ref", "bar.html"},
471 };
472
473 for (size_t i = 0; i < ARRAYSIZE(file_cases); i++) {
474 const char* url = file_cases[i].input;
475 int len = static_cast<int>(strlen(url));
476
477 url_parse::Parsed parsed;
478 url_parse::ParseStandardURL(url, len, &parsed);
479
480 url_parse::Component file_name;
481 url_parse::ExtractFileName(url, parsed.path, &file_name);
482
483 EXPECT_TRUE(ComponentMatches(url, file_cases[i].expected, file_name));
484 }
485 }
486
487 // Returns true if the parameter with index |parameter| in the given URL's
488 // query string. The expected key can be NULL to indicate no such key index
489 // should exist. The parameter number is 1-based.
490 static bool NthParameterIs(const char* url,
491 int parameter,
492 const char* expected_key,
493 const char* expected_value) {
494 url_parse::Parsed parsed;
495 url_parse::ParseStandardURL(url, static_cast<int>(strlen(url)), &parsed);
496
497 url_parse::Component query = parsed.query;
498
499 for (int i = 1; i <= parameter; i++) {
500 url_parse::Component key, value;
501 if (!url_parse::ExtractQueryKeyValue(url, &query, &key, &value)) {
502 if (parameter >= i && !expected_key)
503 return true; // Expected nonexistant key, got one.
504 return false; // Not enough keys.
505 }
506
507 if (i == parameter) {
508 if (!expected_key)
509 return false;
510
511 if (strncmp(&url[key.begin], expected_key, key.len) != 0)
512 return false;
513 if (strncmp(&url[value.begin], expected_value, value.len) != 0)
514 return false;
515 return true;
516 }
517 }
518 return expected_key == NULL; // We didn't find that many parameters.
519 }
520
521 TEST(URLParser, ExtractQueryKeyValue) {
522 EXPECT_TRUE(NthParameterIs("http://www.google.com", 1, NULL, NULL));
523
524 // Basic case.
525 char a[] = "http://www.google.com?arg1=1&arg2=2&bar";
526 EXPECT_TRUE(NthParameterIs(a, 1, "arg1", "1"));
527 EXPECT_TRUE(NthParameterIs(a, 2, "arg2", "2"));
528 EXPECT_TRUE(NthParameterIs(a, 3, "bar", ""));
529 EXPECT_TRUE(NthParameterIs(a, 4, NULL, NULL));
530
531 // Empty param at the end.
532 char b[] = "http://www.google.com?foo=bar&";
533 EXPECT_TRUE(NthParameterIs(b, 1, "foo", "bar"));
534 EXPECT_TRUE(NthParameterIs(b, 2, NULL, NULL));
535
536 // Empty param at the beginning.
537 char c[] = "http://www.google.com?&foo=bar";
538 EXPECT_TRUE(NthParameterIs(c, 1, "", ""));
539 EXPECT_TRUE(NthParameterIs(c, 2, "foo", "bar"));
540 EXPECT_TRUE(NthParameterIs(c, 3, NULL, NULL));
541
542 // Empty key with value.
543 char d[] = "http://www.google.com?=foo";
544 EXPECT_TRUE(NthParameterIs(d, 1, "", "foo"));
545 EXPECT_TRUE(NthParameterIs(d, 2, NULL, NULL));
546
547 // Empty value with key.
548 char e[] = "http://www.google.com?foo=";
549 EXPECT_TRUE(NthParameterIs(e, 1, "foo", ""));
550 EXPECT_TRUE(NthParameterIs(e, 2, NULL, NULL));
551
552 // Empty key and values.
553 char f[] = "http://www.google.com?&&==&=";
554 EXPECT_TRUE(NthParameterIs(f, 1, "", ""));
555 EXPECT_TRUE(NthParameterIs(f, 2, "", ""));
556 EXPECT_TRUE(NthParameterIs(f, 3, "", "="));
557 EXPECT_TRUE(NthParameterIs(f, 4, "", ""));
558 EXPECT_TRUE(NthParameterIs(f, 5, NULL, NULL));
559 }
560
561 // MailtoURL ------------------------------------------------------------------- -
562
563 static MailtoURLParseCase mailto_cases[] = {
564 //|input |scheme |path |query
565 {"mailto:foo@gmail.com", "mailto", "foo@gmail.com", NULL},
566 {" mailto: to \t", "mailto", " to", NULL},
567 {"mailto:addr1%2C%20addr2 ", "mailto", "addr1%2C%20addr2", NULL},
568 {"Mailto:addr1, addr2 ", "Mailto", "addr1, addr2", NULL},
569 {"mailto:addr1:addr2 ", "mailto", "addr1:addr2", NULL},
570 {"mailto:?to=addr1,addr2", "mailto", NULL, "to=addr1,addr2"},
571 {"mailto:?to=addr1%2C%20addr2", "mailto", NULL, "to=addr1%2C%20add r2"},
572 {"mailto:addr1?to=addr2", "mailto", "addr1", "to=addr2"},
573 {"mailto:?body=#foobar#", "mailto", NULL, "body=#foobar#",},
574 {"mailto:#?body=#foobar#", "mailto", "#", "body=#foobar#"},
575 };
576
577 TEST(URLParser, MailtoUrl) {
578 // Declared outside for loop to try to catch cases in init() where we forget
579 // to reset something that is reset by the construtor.
580 url_parse::Parsed parsed;
581 for (size_t i = 0; i < arraysize(mailto_cases); ++i) {
582 const char* url = mailto_cases[i].input;
583 url_parse::ParseMailtoURL(url, static_cast<int>(strlen(url)), &parsed);
584 int port = url_parse::ParsePort(url, parsed.port);
585
586 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].scheme, parsed.scheme));
587 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].path, parsed.path));
588 EXPECT_TRUE(ComponentMatches(url, mailto_cases[i].query, parsed.query));
589 EXPECT_EQ(url_parse::PORT_UNSPECIFIED, port);
590
591 // The remaining components are never used for mailto urls.
592 ExpectInvalidComponent(parsed.username);
593 ExpectInvalidComponent(parsed.password);
594 ExpectInvalidComponent(parsed.port);
595 ExpectInvalidComponent(parsed.ref);
596 }
597 }
598
599 // Various incarnations of filesystem URLs.
600 static FileSystemURLParseCase filesystem_cases[] = {
601 // Regular URL with all the parts
602 {"filesystem:http://user:pass@foo:21/temporary/bar;par?b#c", "http", "user", "p ass", "foo", 21, "/temporary", "/bar;par", "b", "c"},
603 {"filesystem:https://foo/persistent/bar;par/", "https", NULL, NU LL, "foo", -1, "/persistent", "/bar;par/", NULL, NULL},
604 {"filesystem:file:///persistent/bar;par/", "file", NULL, NU LL, NULL, -1, "/persistent", "/bar;par/", NULL, NULL},
605 {"filesystem:file:///persistent/bar;par/?query#ref", "file", N ULL, NULL, NULL, -1, "/persistent", "/bar;par/", "query", "ref"},
606 {"filesystem:file:///persistent", "file", NULL, NU LL, NULL, -1, "/persistent", "", NULL, NULL},
607 };
608
609 TEST(URLParser, FileSystemURL) {
610 // Declared outside for loop to try to catch cases in init() where we forget
611 // to reset something that is reset by the construtor.
612 url_parse::Parsed parsed;
613 for (size_t i = 0; i < arraysize(filesystem_cases); i++) {
614 const FileSystemURLParseCase* parsecase = &filesystem_cases[i];
615 const char* url = parsecase->input;
616 url_parse::ParseFileSystemURL(url, static_cast<int>(strlen(url)), &parsed);
617
618 EXPECT_TRUE(ComponentMatches(url, "filesystem", parsed.scheme));
619 EXPECT_EQ(!parsecase->inner_scheme, !parsed.inner_parsed());
620 // Only check the inner_parsed if there is one.
621 if (parsed.inner_parsed()) {
622 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_scheme,
623 parsed.inner_parsed()->scheme));
624 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_username,
625 parsed.inner_parsed()->username));
626 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_password,
627 parsed.inner_parsed()->password));
628 EXPECT_TRUE(ComponentMatches(url, parsecase->inner_host,
629 parsed.inner_parsed()->host));
630 int port = url_parse::ParsePort(url, parsed.inner_parsed()->port);
631 EXPECT_EQ(parsecase->inner_port, port);
632
633 // The remaining components are never used for filesystem urls.
634 ExpectInvalidComponent(parsed.inner_parsed()->query);
635 ExpectInvalidComponent(parsed.inner_parsed()->ref);
636 }
637
638 EXPECT_TRUE(ComponentMatches(url, parsecase->path, parsed.path));
639 EXPECT_TRUE(ComponentMatches(url, parsecase->query, parsed.query));
640 EXPECT_TRUE(ComponentMatches(url, parsecase->ref, parsed.ref));
641
642 // The remaining components are never used for filesystem urls.
643 ExpectInvalidComponent(parsed.username);
644 ExpectInvalidComponent(parsed.password);
645 ExpectInvalidComponent(parsed.host);
646 ExpectInvalidComponent(parsed.port);
647 }
648 }
649
OLDNEW
« no previous file with comments | « url/url_parse_internal.h ('k') | url/url_test_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698