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

Side by Side Diff: chrome/common/net/url_fixer_upper_unittest.cc

Issue 320253004: Componentize URLFixerUpper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Win64 fix Created 6 years, 6 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
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include <stdlib.h>
6
7 #include "base/basictypes.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/net/url_fixer_upper.h"
14 #include "net/base/filename_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h"
17 #include "url/url_parse.h"
18
19 namespace url {
20
21 std::ostream& operator<<(std::ostream& os, const Component& part) {
22 return os << "(begin=" << part.begin << ", len=" << part.len << ")";
23 }
24
25 } // namespace url
26
27 struct SegmentCase {
28 const std::string input;
29 const std::string result;
30 const url::Component scheme;
31 const url::Component username;
32 const url::Component password;
33 const url::Component host;
34 const url::Component port;
35 const url::Component path;
36 const url::Component query;
37 const url::Component ref;
38 };
39
40 static const SegmentCase segment_cases[] = {
41 { "http://www.google.com/", "http",
42 url::Component(0, 4), // scheme
43 url::Component(), // username
44 url::Component(), // password
45 url::Component(7, 14), // host
46 url::Component(), // port
47 url::Component(21, 1), // path
48 url::Component(), // query
49 url::Component(), // ref
50 },
51 { "aBoUt:vErSiOn", "about",
52 url::Component(0, 5), // scheme
53 url::Component(), // username
54 url::Component(), // password
55 url::Component(6, 7), // host
56 url::Component(), // port
57 url::Component(), // path
58 url::Component(), // query
59 url::Component(), // ref
60 },
61 { "about:host/path?query#ref", "about",
62 url::Component(0, 5), // scheme
63 url::Component(), // username
64 url::Component(), // password
65 url::Component(6, 4), // host
66 url::Component(), // port
67 url::Component(10, 5), // path
68 url::Component(16, 5), // query
69 url::Component(22, 3), // ref
70 },
71 { "about://host/path?query#ref", "about",
72 url::Component(0, 5), // scheme
73 url::Component(), // username
74 url::Component(), // password
75 url::Component(8, 4), // host
76 url::Component(), // port
77 url::Component(12, 5), // path
78 url::Component(18, 5), // query
79 url::Component(24, 3), // ref
80 },
81 { "chrome:host/path?query#ref", "chrome",
82 url::Component(0, 6), // scheme
83 url::Component(), // username
84 url::Component(), // password
85 url::Component(7, 4), // host
86 url::Component(), // port
87 url::Component(11, 5), // path
88 url::Component(17, 5), // query
89 url::Component(23, 3), // ref
90 },
91 { "chrome://host/path?query#ref", "chrome",
92 url::Component(0, 6), // scheme
93 url::Component(), // username
94 url::Component(), // password
95 url::Component(9, 4), // host
96 url::Component(), // port
97 url::Component(13, 5), // path
98 url::Component(19, 5), // query
99 url::Component(25, 3), // ref
100 },
101 { " www.google.com:124?foo#", "http",
102 url::Component(), // scheme
103 url::Component(), // username
104 url::Component(), // password
105 url::Component(4, 14), // host
106 url::Component(19, 3), // port
107 url::Component(), // path
108 url::Component(23, 3), // query
109 url::Component(27, 0), // ref
110 },
111 { "user@www.google.com", "http",
112 url::Component(), // scheme
113 url::Component(0, 4), // username
114 url::Component(), // password
115 url::Component(5, 14), // host
116 url::Component(), // port
117 url::Component(), // path
118 url::Component(), // query
119 url::Component(), // ref
120 },
121 { "ftp:/user:P:a$$Wd@..ftp.google.com...::23///pub?foo#bar", "ftp",
122 url::Component(0, 3), // scheme
123 url::Component(5, 4), // username
124 url::Component(10, 7), // password
125 url::Component(18, 20), // host
126 url::Component(39, 2), // port
127 url::Component(41, 6), // path
128 url::Component(48, 3), // query
129 url::Component(52, 3), // ref
130 },
131 { "[2001:db8::1]/path", "http",
132 url::Component(), // scheme
133 url::Component(), // username
134 url::Component(), // password
135 url::Component(0, 13), // host
136 url::Component(), // port
137 url::Component(13, 5), // path
138 url::Component(), // query
139 url::Component(), // ref
140 },
141 { "[::1]", "http",
142 url::Component(), // scheme
143 url::Component(), // username
144 url::Component(), // password
145 url::Component(0, 5), // host
146 url::Component(), // port
147 url::Component(), // path
148 url::Component(), // query
149 url::Component(), // ref
150 },
151 // Incomplete IPv6 addresses (will not canonicalize).
152 { "[2001:4860:", "http",
153 url::Component(), // scheme
154 url::Component(), // username
155 url::Component(), // password
156 url::Component(0, 11), // host
157 url::Component(), // port
158 url::Component(), // path
159 url::Component(), // query
160 url::Component(), // ref
161 },
162 { "[2001:4860:/foo", "http",
163 url::Component(), // scheme
164 url::Component(), // username
165 url::Component(), // password
166 url::Component(0, 11), // host
167 url::Component(), // port
168 url::Component(11, 4), // path
169 url::Component(), // query
170 url::Component(), // ref
171 },
172 { "http://:b005::68]", "http",
173 url::Component(0, 4), // scheme
174 url::Component(), // username
175 url::Component(), // password
176 url::Component(7, 10), // host
177 url::Component(), // port
178 url::Component(), // path
179 url::Component(), // query
180 url::Component(), // ref
181 },
182 // Can't do anything useful with this.
183 { ":b005::68]", "",
184 url::Component(0, 0), // scheme
185 url::Component(), // username
186 url::Component(), // password
187 url::Component(), // host
188 url::Component(), // port
189 url::Component(), // path
190 url::Component(), // query
191 url::Component(), // ref
192 },
193 };
194
195 typedef testing::Test URLFixerUpperTest;
196
197 TEST(URLFixerUpperTest, SegmentURL) {
198 std::string result;
199 url::Parsed parts;
200
201 for (size_t i = 0; i < arraysize(segment_cases); ++i) {
202 SegmentCase value = segment_cases[i];
203 result = URLFixerUpper::SegmentURL(value.input, &parts);
204 EXPECT_EQ(value.result, result);
205 EXPECT_EQ(value.scheme, parts.scheme);
206 EXPECT_EQ(value.username, parts.username);
207 EXPECT_EQ(value.password, parts.password);
208 EXPECT_EQ(value.host, parts.host);
209 EXPECT_EQ(value.port, parts.port);
210 EXPECT_EQ(value.path, parts.path);
211 EXPECT_EQ(value.query, parts.query);
212 EXPECT_EQ(value.ref, parts.ref);
213 }
214 }
215
216 // Creates a file and returns its full name as well as the decomposed
217 // version. Example:
218 // full_path = "c:\foo\bar.txt"
219 // dir = "c:\foo"
220 // file_name = "bar.txt"
221 static bool MakeTempFile(const base::FilePath& dir,
222 const base::FilePath& file_name,
223 base::FilePath* full_path) {
224 *full_path = dir.Append(file_name);
225 return base::WriteFile(*full_path, "", 0) == 0;
226 }
227
228 // Returns true if the given URL is a file: URL that matches the given file
229 static bool IsMatchingFileURL(const std::string& url,
230 const base::FilePath& full_file_path) {
231 if (url.length() <= 8)
232 return false;
233 if (std::string("file:///") != url.substr(0, 8))
234 return false; // no file:/// prefix
235 if (url.find('\\') != std::string::npos)
236 return false; // contains backslashes
237
238 base::FilePath derived_path;
239 net::FileURLToFilePath(GURL(url), &derived_path);
240
241 return base::FilePath::CompareEqualIgnoreCase(derived_path.value(),
242 full_file_path.value());
243 }
244
245 struct FixupCase {
246 const std::string input;
247 const std::string output;
248 } fixup_cases[] = {
249 {"www.google.com", "http://www.google.com/"},
250 {" www.google.com ", "http://www.google.com/"},
251 {" foo.com/asdf bar", "http://foo.com/asdf%20%20bar"},
252 {"..www.google.com..", "http://www.google.com./"},
253 {"http://......", "http://....../"},
254 {"http://host.com:ninety-two/", "http://host.com:ninety-two/"},
255 {"http://host.com:ninety-two?foo", "http://host.com:ninety-two/?foo"},
256 {"google.com:123", "http://google.com:123/"},
257 {"about:", "chrome://version/"},
258 {"about:foo", "chrome://foo/"},
259 {"about:version", "chrome://version/"},
260 {"about:blank", "about:blank"},
261 {"about:usr:pwd@hst/pth?qry#ref", "chrome://usr:pwd@hst/pth?qry#ref"},
262 {"about://usr:pwd@hst/pth?qry#ref", "chrome://usr:pwd@hst/pth?qry#ref"},
263 {"chrome:usr:pwd@hst/pth?qry#ref", "chrome://usr:pwd@hst/pth?qry#ref"},
264 {"chrome://usr:pwd@hst/pth?qry#ref", "chrome://usr:pwd@hst/pth?qry#ref"},
265 {"www:123", "http://www:123/"},
266 {" www:123", "http://www:123/"},
267 {"www.google.com?foo", "http://www.google.com/?foo"},
268 {"www.google.com#foo", "http://www.google.com/#foo"},
269 {"www.google.com?", "http://www.google.com/?"},
270 {"www.google.com#", "http://www.google.com/#"},
271 {"www.google.com:123?foo#bar", "http://www.google.com:123/?foo#bar"},
272 {"user@www.google.com", "http://user@www.google.com/"},
273 {"\xE6\xB0\xB4.com", "http://xn--1rw.com/"},
274 // It would be better if this next case got treated as http, but I don't see
275 // a clean way to guess this isn't the new-and-exciting "user" scheme.
276 {"user:passwd@www.google.com:8080/", "user:passwd@www.google.com:8080/"},
277 // {"file:///c:/foo/bar%20baz.txt", "file:///C:/foo/bar%20baz.txt"},
278 {"ftp.google.com", "ftp://ftp.google.com/"},
279 {" ftp.google.com", "ftp://ftp.google.com/"},
280 {"FTP.GooGle.com", "ftp://ftp.google.com/"},
281 {"ftpblah.google.com", "http://ftpblah.google.com/"},
282 {"ftp", "http://ftp/"},
283 {"google.ftp.com", "http://google.ftp.com/"},
284 // URLs which end with 0x85 (NEL in ISO-8859).
285 {"http://foo.com/s?q=\xd0\x85", "http://foo.com/s?q=%D0%85"},
286 {"http://foo.com/s?q=\xec\x97\x85", "http://foo.com/s?q=%EC%97%85"},
287 {"http://foo.com/s?q=\xf0\x90\x80\x85", "http://foo.com/s?q=%F0%90%80%85"},
288 // URLs which end with 0xA0 (non-break space in ISO-8859).
289 {"http://foo.com/s?q=\xd0\xa0", "http://foo.com/s?q=%D0%A0"},
290 {"http://foo.com/s?q=\xec\x97\xa0", "http://foo.com/s?q=%EC%97%A0"},
291 {"http://foo.com/s?q=\xf0\x90\x80\xa0", "http://foo.com/s?q=%F0%90%80%A0"},
292 // URLs containing IPv6 literals.
293 {"[2001:db8::2]", "http://[2001:db8::2]/"},
294 {"[::]:80", "http://[::]/"},
295 {"[::]:80/path", "http://[::]/path"},
296 {"[::]:180/path", "http://[::]:180/path"},
297 // TODO(pmarks): Maybe we should parse bare IPv6 literals someday.
298 {"::1", "::1"},
299 // Semicolon as scheme separator for standard schemes.
300 {"http;//www.google.com/", "http://www.google.com/"},
301 {"about;chrome", "chrome://chrome/"},
302 // Semicolon left as-is for non-standard schemes.
303 {"whatsup;//fool", "whatsup://fool"},
304 // Semicolon left as-is in URL itself.
305 {"http://host/port?query;moar", "http://host/port?query;moar"},
306 // Fewer slashes than expected.
307 {"http;www.google.com/", "http://www.google.com/"},
308 {"http;/www.google.com/", "http://www.google.com/"},
309 // Semicolon at start.
310 {";http://www.google.com/", "http://%3Bhttp//www.google.com/"},
311 };
312
313 TEST(URLFixerUpperTest, FixupURL) {
314 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
315 FixupCase value = fixup_cases[i];
316 EXPECT_EQ(value.output,
317 URLFixerUpper::FixupURL(value.input, "").possibly_invalid_spec())
318 << "input: " << value.input;
319 }
320
321 // Check the TLD-appending functionality.
322 FixupCase tld_cases[] = {
323 {"google", "http://www.google.com/"},
324 {"google.", "http://www.google.com/"},
325 {"google..", "http://www.google.com/"},
326 {".google", "http://www.google.com/"},
327 {"www.google", "http://www.google.com/"},
328 {"google.com", "http://google.com/"},
329 {"http://google", "http://www.google.com/"},
330 {"..google..", "http://www.google.com/"},
331 {"http://www.google", "http://www.google.com/"},
332 {"9999999999999999", "http://www.9999999999999999.com/"},
333 {"google/foo", "http://www.google.com/foo"},
334 {"google.com/foo", "http://google.com/foo"},
335 {"google/?foo=.com", "http://www.google.com/?foo=.com"},
336 {"www.google/?foo=www.", "http://www.google.com/?foo=www."},
337 {"google.com/?foo=.com", "http://google.com/?foo=.com"},
338 {"http://www.google.com", "http://www.google.com/"},
339 {"google:123", "http://www.google.com:123/"},
340 {"http://google:123", "http://www.google.com:123/"},
341 };
342 for (size_t i = 0; i < arraysize(tld_cases); ++i) {
343 FixupCase value = tld_cases[i];
344 EXPECT_EQ(value.output,
345 URLFixerUpper::FixupURL(value.input, "com").possibly_invalid_spec());
346 }
347 }
348
349 // Test different types of file inputs to URIFixerUpper::FixupURL. This
350 // doesn't go into the nice array of fixups above since the file input
351 // has to exist.
352 TEST(URLFixerUpperTest, FixupFile) {
353 // this "original" filename is the one we tweak to get all the variations
354 base::FilePath dir;
355 base::FilePath original;
356 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir));
357 ASSERT_TRUE(MakeTempFile(
358 dir,
359 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
360 &original));
361
362 // reference path
363 GURL golden(net::FilePathToFileURL(original));
364
365 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic)
366 GURL fixedup(URLFixerUpper::FixupURL(original.AsUTF8Unsafe(), std::string()));
367 EXPECT_EQ(golden, fixedup);
368
369 // TODO(port): Make some equivalent tests for posix.
370 #if defined(OS_WIN)
371 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon)
372 std::string cur(base::WideToUTF8(original.value()));
373 EXPECT_EQ(':', cur[1]);
374 cur[1] = '|';
375 EXPECT_EQ(golden, URLFixerUpper::FixupURL(cur, std::string()));
376
377 FixupCase cases[] = {
378 {"c:\\Non-existent%20file.txt", "file:///C:/Non-existent%2520file.txt"},
379
380 // \\foo\bar.txt -> file://foo/bar.txt
381 // UNC paths, this file won't exist, but since there are no escapes, it
382 // should be returned just converted to a file: URL.
383 {"\\\\NonexistentHost\\foo\\bar.txt", "file://nonexistenthost/foo/bar.txt"},
384 // We do this strictly, like IE8, which only accepts this form using
385 // backslashes and not forward ones. Turning "//foo" into "http" matches
386 // Firefox and IE, silly though it may seem (it falls out of adding "http"
387 // as the default protocol if you haven't entered one).
388 {"//NonexistentHost\\foo/bar.txt", "http://nonexistenthost/foo/bar.txt"},
389 {"file:///C:/foo/bar", "file:///C:/foo/bar"},
390
391 // Much of the work here comes from GURL's canonicalization stage.
392 {"file://C:/foo/bar", "file:///C:/foo/bar"},
393 {"file:c:", "file:///C:/"},
394 {"file:c:WINDOWS", "file:///C:/WINDOWS"},
395 {"file:c|Program Files", "file:///C:/Program%20Files"},
396 {"file:/file", "file://file/"},
397 {"file:////////c:\\foo", "file:///C:/foo"},
398 {"file://server/folder/file", "file://server/folder/file"},
399
400 // These are fixups we don't do, but could consider:
401 // {"file:///foo:/bar", "file://foo/bar"},
402 // {"file:/\\/server\\folder/file", "file://server/folder/file"},
403 };
404 #elif defined(OS_POSIX)
405
406 #if defined(OS_MACOSX)
407 #define HOME "/Users/"
408 #else
409 #define HOME "/home/"
410 #endif
411 URLFixerUpper::home_directory_override = "/foo";
412 FixupCase cases[] = {
413 // File URLs go through GURL, which tries to escape intelligently.
414 {"/A%20non-existent file.txt", "file:///A%2520non-existent%20file.txt"},
415 // A plain "/" refers to the root.
416 {"/", "file:///"},
417
418 // These rely on the above home_directory_override.
419 {"~", "file:///foo"},
420 {"~/bar", "file:///foo/bar"},
421
422 // References to other users' homedirs.
423 {"~foo", "file://" HOME "foo"},
424 {"~x/blah", "file://" HOME "x/blah"},
425 };
426 #endif
427
428 for (size_t i = 0; i < arraysize(cases); i++) {
429 EXPECT_EQ(cases[i].output,
430 URLFixerUpper::FixupURL(cases[i].input, "").possibly_invalid_spec());
431 }
432
433 EXPECT_TRUE(base::DeleteFile(original, false));
434 }
435
436 TEST(URLFixerUpperTest, FixupRelativeFile) {
437 base::FilePath full_path, dir;
438 base::FilePath file_part(
439 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
440 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir));
441 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path));
442 full_path = base::MakeAbsoluteFilePath(full_path);
443 ASSERT_FALSE(full_path.empty());
444
445 // make sure we pass through good URLs
446 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
447 FixupCase value = fixup_cases[i];
448 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input);
449 EXPECT_EQ(value.output,
450 URLFixerUpper::FixupRelativeFile(dir, input).possibly_invalid_spec());
451 }
452
453 // make sure the existing file got fixed-up to a file URL, and that there
454 // are no backslashes
455 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir,
456 file_part).possibly_invalid_spec(), full_path));
457 EXPECT_TRUE(base::DeleteFile(full_path, false));
458
459 // create a filename we know doesn't exist and make sure it doesn't get
460 // fixed up to a file URL
461 base::FilePath nonexistent_file(
462 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
463 std::string fixedup(URLFixerUpper::FixupRelativeFile(dir,
464 nonexistent_file).possibly_invalid_spec());
465 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
466 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
467
468 // make a subdir to make sure relative paths with directories work, also
469 // test spaces:
470 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt"
471 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
472 base::FilePath sub_file(
473 FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
474 base::FilePath new_dir = dir.Append(sub_dir);
475 base::CreateDirectory(new_dir);
476 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
477 full_path = base::MakeAbsoluteFilePath(full_path);
478 ASSERT_FALSE(full_path.empty());
479
480 // test file in the subdir
481 base::FilePath relative_file = sub_dir.Append(sub_file);
482 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir,
483 relative_file).possibly_invalid_spec(), full_path));
484
485 // test file in the subdir with different slashes and escaping.
486 base::FilePath::StringType relative_file_str = sub_dir.value() +
487 FILE_PATH_LITERAL("/") + sub_file.value();
488 ReplaceSubstringsAfterOffset(&relative_file_str, 0,
489 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
490 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir,
491 base::FilePath(relative_file_str)).possibly_invalid_spec(), full_path));
492
493 // test relative directories and duplicate slashes
494 // (should resolve to the same file as above)
495 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
496 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
497 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir,
498 base::FilePath(relative_file_str)).possibly_invalid_spec(), full_path));
499
500 // done with the subdir
501 EXPECT_TRUE(base::DeleteFile(full_path, false));
502 EXPECT_TRUE(base::DeleteFile(new_dir, true));
503
504 // Test that an obvious HTTP URL isn't accidentally treated as an absolute
505 // file path (on account of system-specific craziness).
506 base::FilePath empty_path;
507 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../"));
508 EXPECT_TRUE(URLFixerUpper::FixupRelativeFile(
509 empty_path, http_url_path).SchemeIs("http"));
510 }
OLDNEW
« no previous file with comments | « chrome/common/net/url_fixer_upper.cc ('k') | chrome/utility/importer/bookmarks_file_importer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698