blob: c2b3e263480646d5bfd667b3c315aa355822e800 [file] [log] [blame]
[email protected]89f550b2011-06-08 18:34:031// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include <stdlib.h>
initial.commit09911bf2008-07-26 23:55:296
[email protected]9b5b1d602014-06-12 14:29:027#include "base/base_paths.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/basictypes.h"
[email protected]9b5b1d602014-06-12 14:29:029#include "base/files/file_path.h"
thestig819adcc82014-09-10 22:24:5310#include "base/files/file_util.h"
[email protected]97803e1b2014-07-25 01:52:1411#include "base/files/scoped_temp_dir.h"
[email protected]f9b294362013-06-10 20:22:3112#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1813#include "base/strings/utf_string_conversions.h"
[email protected]9b5b1d602014-06-12 14:29:0214#include "components/url_fixer/url_fixer.h"
[email protected]d96cf752014-04-09 04:05:2815#include "net/base/filename_util.h"
initial.commit09911bf2008-07-26 23:55:2916#include "testing/gtest/include/gtest/gtest.h"
[email protected]761fa4702013-07-02 15:25:1517#include "url/gurl.h"
tfarina018de6e2015-05-26 17:41:2018#include "url/third_party/mozilla/url_parse.h"
initial.commit09911bf2008-07-26 23:55:2919
[email protected]0318f922014-04-22 00:09:2320namespace url {
[email protected]46cf4be92010-10-01 11:23:2021
22std::ostream& operator<<(std::ostream& os, const Component& part) {
initial.commit09911bf2008-07-26 23:55:2923 return os << "(begin=" << part.begin << ", len=" << part.len << ")";
24}
25
[email protected]0318f922014-04-22 00:09:2326} // namespace url
[email protected]46cf4be92010-10-01 11:23:2027
[email protected]dc7d3052013-05-31 21:23:2828struct SegmentCase {
[email protected]b1c33f82009-01-23 01:51:2329 const std::string input;
30 const std::string result;
[email protected]b45334502014-04-30 19:44:0531 const url::Component scheme;
32 const url::Component username;
33 const url::Component password;
34 const url::Component host;
35 const url::Component port;
36 const url::Component path;
37 const url::Component query;
38 const url::Component ref;
initial.commit09911bf2008-07-26 23:55:2939};
40
[email protected]dc7d3052013-05-31 21:23:2841static const SegmentCase segment_cases[] = {
[email protected]b1c33f82009-01-23 01:51:2342 { "http://www.google.com/", "http",
[email protected]b45334502014-04-30 19:44:0543 url::Component(0, 4), // scheme
44 url::Component(), // username
45 url::Component(), // password
46 url::Component(7, 14), // host
47 url::Component(), // port
48 url::Component(21, 1), // path
49 url::Component(), // query
50 url::Component(), // ref
initial.commit09911bf2008-07-26 23:55:2951 },
[email protected]b1c33f82009-01-23 01:51:2352 { "aBoUt:vErSiOn", "about",
[email protected]b45334502014-04-30 19:44:0553 url::Component(0, 5), // scheme
54 url::Component(), // username
55 url::Component(), // password
56 url::Component(6, 7), // host
57 url::Component(), // port
58 url::Component(), // path
59 url::Component(), // query
60 url::Component(), // ref
initial.commit09911bf2008-07-26 23:55:2961 },
[email protected]89f550b2011-06-08 18:34:0362 { "about:host/path?query#ref", "about",
[email protected]b45334502014-04-30 19:44:0563 url::Component(0, 5), // scheme
64 url::Component(), // username
65 url::Component(), // password
66 url::Component(6, 4), // host
67 url::Component(), // port
68 url::Component(10, 5), // path
69 url::Component(16, 5), // query
70 url::Component(22, 3), // ref
[email protected]89f550b2011-06-08 18:34:0371 },
72 { "about://host/path?query#ref", "about",
[email protected]b45334502014-04-30 19:44:0573 url::Component(0, 5), // scheme
74 url::Component(), // username
75 url::Component(), // password
76 url::Component(8, 4), // host
77 url::Component(), // port
78 url::Component(12, 5), // path
79 url::Component(18, 5), // query
80 url::Component(24, 3), // ref
[email protected]89f550b2011-06-08 18:34:0381 },
82 { "chrome:host/path?query#ref", "chrome",
[email protected]b45334502014-04-30 19:44:0583 url::Component(0, 6), // scheme
84 url::Component(), // username
85 url::Component(), // password
86 url::Component(7, 4), // host
87 url::Component(), // port
88 url::Component(11, 5), // path
89 url::Component(17, 5), // query
90 url::Component(23, 3), // ref
[email protected]89f550b2011-06-08 18:34:0391 },
92 { "chrome://host/path?query#ref", "chrome",
[email protected]b45334502014-04-30 19:44:0593 url::Component(0, 6), // scheme
94 url::Component(), // username
95 url::Component(), // password
96 url::Component(9, 4), // host
97 url::Component(), // port
98 url::Component(13, 5), // path
99 url::Component(19, 5), // query
100 url::Component(25, 3), // ref
[email protected]89f550b2011-06-08 18:34:03101 },
[email protected]b1c33f82009-01-23 01:51:23102 { " www.google.com:124?foo#", "http",
[email protected]b45334502014-04-30 19:44:05103 url::Component(), // scheme
104 url::Component(), // username
105 url::Component(), // password
106 url::Component(4, 14), // host
107 url::Component(19, 3), // port
108 url::Component(), // path
109 url::Component(23, 3), // query
110 url::Component(27, 0), // ref
initial.commit09911bf2008-07-26 23:55:29111 },
[email protected]b1c33f82009-01-23 01:51:23112 { "[email protected]", "http",
[email protected]b45334502014-04-30 19:44:05113 url::Component(), // scheme
114 url::Component(0, 4), // username
115 url::Component(), // password
116 url::Component(5, 14), // host
117 url::Component(), // port
118 url::Component(), // path
119 url::Component(), // query
120 url::Component(), // ref
initial.commit09911bf2008-07-26 23:55:29121 },
[email protected]b1c33f82009-01-23 01:51:23122 { "ftp:/user:P:[email protected]...::23///pub?foo#bar", "ftp",
[email protected]b45334502014-04-30 19:44:05123 url::Component(0, 3), // scheme
124 url::Component(5, 4), // username
125 url::Component(10, 7), // password
126 url::Component(18, 20), // host
127 url::Component(39, 2), // port
128 url::Component(41, 6), // path
129 url::Component(48, 3), // query
130 url::Component(52, 3), // ref
initial.commit09911bf2008-07-26 23:55:29131 },
[email protected]818071ce2009-05-18 01:25:25132 { "[2001:db8::1]/path", "http",
[email protected]b45334502014-04-30 19:44:05133 url::Component(), // scheme
134 url::Component(), // username
135 url::Component(), // password
136 url::Component(0, 13), // host
137 url::Component(), // port
138 url::Component(13, 5), // path
139 url::Component(), // query
140 url::Component(), // ref
[email protected]818071ce2009-05-18 01:25:25141 },
142 { "[::1]", "http",
[email protected]b45334502014-04-30 19:44:05143 url::Component(), // scheme
144 url::Component(), // username
145 url::Component(), // password
146 url::Component(0, 5), // host
147 url::Component(), // port
148 url::Component(), // path
149 url::Component(), // query
150 url::Component(), // ref
[email protected]818071ce2009-05-18 01:25:25151 },
[email protected]c1991302009-06-04 03:57:39152 // Incomplete IPv6 addresses (will not canonicalize).
153 { "[2001:4860:", "http",
[email protected]b45334502014-04-30 19:44:05154 url::Component(), // scheme
155 url::Component(), // username
156 url::Component(), // password
157 url::Component(0, 11), // host
158 url::Component(), // port
159 url::Component(), // path
160 url::Component(), // query
161 url::Component(), // ref
[email protected]c1991302009-06-04 03:57:39162 },
163 { "[2001:4860:/foo", "http",
[email protected]b45334502014-04-30 19:44:05164 url::Component(), // scheme
165 url::Component(), // username
166 url::Component(), // password
167 url::Component(0, 11), // host
168 url::Component(), // port
169 url::Component(11, 4), // path
170 url::Component(), // query
171 url::Component(), // ref
[email protected]c1991302009-06-04 03:57:39172 },
173 { "http://:b005::68]", "http",
[email protected]b45334502014-04-30 19:44:05174 url::Component(0, 4), // scheme
175 url::Component(), // username
176 url::Component(), // password
177 url::Component(7, 10), // host
178 url::Component(), // port
179 url::Component(), // path
180 url::Component(), // query
181 url::Component(), // ref
[email protected]c1991302009-06-04 03:57:39182 },
183 // Can't do anything useful with this.
184 { ":b005::68]", "",
[email protected]b45334502014-04-30 19:44:05185 url::Component(0, 0), // scheme
186 url::Component(), // username
187 url::Component(), // password
188 url::Component(), // host
189 url::Component(), // port
190 url::Component(), // path
191 url::Component(), // query
192 url::Component(), // ref
[email protected]c1991302009-06-04 03:57:39193 },
initial.commit09911bf2008-07-26 23:55:29194};
195
[email protected]9b5b1d602014-06-12 14:29:02196typedef testing::Test URLFixerTest;
[email protected]78977682014-05-10 18:42:25197
[email protected]9b5b1d602014-06-12 14:29:02198TEST(URLFixerTest, SegmentURL) {
[email protected]b1c33f82009-01-23 01:51:23199 std::string result;
[email protected]b45334502014-04-30 19:44:05200 url::Parsed parts;
initial.commit09911bf2008-07-26 23:55:29201
[email protected]b1c33f82009-01-23 01:51:23202 for (size_t i = 0; i < arraysize(segment_cases); ++i) {
[email protected]dc7d3052013-05-31 21:23:28203 SegmentCase value = segment_cases[i];
[email protected]9b5b1d602014-06-12 14:29:02204 result = url_fixer::SegmentURL(value.input, &parts);
initial.commit09911bf2008-07-26 23:55:29205 EXPECT_EQ(value.result, result);
206 EXPECT_EQ(value.scheme, parts.scheme);
207 EXPECT_EQ(value.username, parts.username);
208 EXPECT_EQ(value.password, parts.password);
209 EXPECT_EQ(value.host, parts.host);
210 EXPECT_EQ(value.port, parts.port);
211 EXPECT_EQ(value.path, parts.path);
212 EXPECT_EQ(value.query, parts.query);
213 EXPECT_EQ(value.ref, parts.ref);
214 }
215}
216
217// Creates a file and returns its full name as well as the decomposed
218// version. Example:
219// full_path = "c:\foo\bar.txt"
220// dir = "c:\foo"
221// file_name = "bar.txt"
[email protected]650b2d52013-02-10 03:41:45222static bool MakeTempFile(const base::FilePath& dir,
223 const base::FilePath& file_name,
224 base::FilePath* full_path) {
[email protected]b1c33f82009-01-23 01:51:23225 *full_path = dir.Append(file_name);
[email protected]e5c2a22e2014-03-06 20:42:30226 return base::WriteFile(*full_path, "", 0) == 0;
initial.commit09911bf2008-07-26 23:55:29227}
228
229// Returns true if the given URL is a file: URL that matches the given file
[email protected]b1c33f82009-01-23 01:51:23230static bool IsMatchingFileURL(const std::string& url,
[email protected]650b2d52013-02-10 03:41:45231 const base::FilePath& full_file_path) {
initial.commit09911bf2008-07-26 23:55:29232 if (url.length() <= 8)
233 return false;
[email protected]b1c33f82009-01-23 01:51:23234 if (std::string("file:///") != url.substr(0, 8))
initial.commit09911bf2008-07-26 23:55:29235 return false; // no file:/// prefix
[email protected]b1c33f82009-01-23 01:51:23236 if (url.find('\\') != std::string::npos)
initial.commit09911bf2008-07-26 23:55:29237 return false; // contains backslashes
238
[email protected]650b2d52013-02-10 03:41:45239 base::FilePath derived_path;
[email protected]56741852008-12-17 19:04:50240 net::FileURLToFilePath(GURL(url), &derived_path);
[email protected]b1c33f82009-01-23 01:51:23241
[email protected]650b2d52013-02-10 03:41:45242 return base::FilePath::CompareEqualIgnoreCase(derived_path.value(),
[email protected]eccb9d12009-10-28 05:40:09243 full_file_path.value());
initial.commit09911bf2008-07-26 23:55:29244}
245
[email protected]dc7d3052013-05-31 21:23:28246struct FixupCase {
[email protected]b1c33f82009-01-23 01:51:23247 const std::string input;
[email protected]b1c33f82009-01-23 01:51:23248 const std::string output;
initial.commit09911bf2008-07-26 23:55:29249} fixup_cases[] = {
[email protected]78977682014-05-10 18:42:25250 {"www.google.com", "http://www.google.com/"},
251 {" www.google.com ", "http://www.google.com/"},
252 {" foo.com/asdf bar", "http://foo.com/asdf%20%20bar"},
253 {"..www.google.com..", "http://www.google.com./"},
254 {"http://......", "http://....../"},
255 {"http://host.com:ninety-two/", "http://host.com:ninety-two/"},
256 {"http://host.com:ninety-two?foo", "http://host.com:ninety-two/?foo"},
257 {"google.com:123", "http://google.com:123/"},
258 {"about:", "chrome://version/"},
259 {"about:foo", "chrome://foo/"},
260 {"about:version", "chrome://version/"},
261 {"about:blank", "about:blank"},
262 {"about:usr:pwd@hst/pth?qry#ref", "chrome://usr:pwd@hst/pth?qry#ref"},
263 {"about://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 {"chrome://usr:pwd@hst/pth?qry#ref", "chrome://usr:pwd@hst/pth?qry#ref"},
266 {"www:123", "http://www:123/"},
267 {" www:123", "http://www:123/"},
268 {"www.google.com?foo", "http://www.google.com/?foo"},
269 {"www.google.com#foo", "http://www.google.com/#foo"},
270 {"www.google.com?", "http://www.google.com/?"},
271 {"www.google.com#", "http://www.google.com/#"},
272 {"www.google.com:123?foo#bar", "http://www.google.com:123/?foo#bar"},
273 {"[email protected]", "http://[email protected]/"},
274 {"\xE6\xB0\xB4.com", "http://xn--1rw.com/"},
initial.commit09911bf2008-07-26 23:55:29275 // It would be better if this next case got treated as http, but I don't see
276 // a clean way to guess this isn't the new-and-exciting "user" scheme.
[email protected]78977682014-05-10 18:42:25277 {"user:[email protected]:8080/", "user:[email protected]:8080/"},
278 // {"file:///c:/foo/bar%20baz.txt", "file:///C:/foo/bar%20baz.txt"},
279 {"ftp.google.com", "ftp://ftp.google.com/"},
280 {" ftp.google.com", "ftp://ftp.google.com/"},
281 {"FTP.GooGle.com", "ftp://ftp.google.com/"},
282 {"ftpblah.google.com", "http://ftpblah.google.com/"},
283 {"ftp", "http://ftp/"},
284 {"google.ftp.com", "http://google.ftp.com/"},
[email protected]90f933a2009-03-05 03:41:51285 // URLs which end with 0x85 (NEL in ISO-8859).
[email protected]78977682014-05-10 18:42:25286 {"http://foo.com/s?q=\xd0\x85", "http://foo.com/s?q=%D0%85"},
287 {"http://foo.com/s?q=\xec\x97\x85", "http://foo.com/s?q=%EC%97%85"},
288 {"http://foo.com/s?q=\xf0\x90\x80\x85", "http://foo.com/s?q=%F0%90%80%85"},
[email protected]90f933a2009-03-05 03:41:51289 // URLs which end with 0xA0 (non-break space in ISO-8859).
[email protected]78977682014-05-10 18:42:25290 {"http://foo.com/s?q=\xd0\xa0", "http://foo.com/s?q=%D0%A0"},
291 {"http://foo.com/s?q=\xec\x97\xa0", "http://foo.com/s?q=%EC%97%A0"},
292 {"http://foo.com/s?q=\xf0\x90\x80\xa0", "http://foo.com/s?q=%F0%90%80%A0"},
[email protected]818071ce2009-05-18 01:25:25293 // URLs containing IPv6 literals.
[email protected]78977682014-05-10 18:42:25294 {"[2001:db8::2]", "http://[2001:db8::2]/"},
295 {"[::]:80", "http://[::]/"},
296 {"[::]:80/path", "http://[::]/path"},
297 {"[::]:180/path", "http://[::]:180/path"},
[email protected]818071ce2009-05-18 01:25:25298 // TODO(pmarks): Maybe we should parse bare IPv6 literals someday.
[email protected]78977682014-05-10 18:42:25299 {"::1", "::1"},
[email protected]ae8e3672013-03-20 09:00:08300 // Semicolon as scheme separator for standard schemes.
[email protected]78977682014-05-10 18:42:25301 {"http;//www.google.com/", "http://www.google.com/"},
302 {"about;chrome", "chrome://chrome/"},
[email protected]ae8e3672013-03-20 09:00:08303 // Semicolon left as-is for non-standard schemes.
[email protected]78977682014-05-10 18:42:25304 {"whatsup;//fool", "whatsup://fool"},
[email protected]ae8e3672013-03-20 09:00:08305 // Semicolon left as-is in URL itself.
[email protected]78977682014-05-10 18:42:25306 {"http://host/port?query;moar", "http://host/port?query;moar"},
[email protected]ae8e3672013-03-20 09:00:08307 // Fewer slashes than expected.
[email protected]78977682014-05-10 18:42:25308 {"http;www.google.com/", "http://www.google.com/"},
309 {"http;/www.google.com/", "http://www.google.com/"},
[email protected]ae8e3672013-03-20 09:00:08310 // Semicolon at start.
[email protected]78977682014-05-10 18:42:25311 {";http://www.google.com/", "http://%3Bhttp//www.google.com/"},
initial.commit09911bf2008-07-26 23:55:29312};
313
[email protected]9b5b1d602014-06-12 14:29:02314TEST(URLFixerTest, FixupURL) {
[email protected]b1c33f82009-01-23 01:51:23315 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
[email protected]dc7d3052013-05-31 21:23:28316 FixupCase value = fixup_cases[i];
[email protected]78977682014-05-10 18:42:25317 EXPECT_EQ(value.output,
[email protected]9b5b1d602014-06-12 14:29:02318 url_fixer::FixupURL(value.input, "").possibly_invalid_spec())
[email protected]ae8e3672013-03-20 09:00:08319 << "input: " << value.input;
initial.commit09911bf2008-07-26 23:55:29320 }
321
[email protected]78977682014-05-10 18:42:25322 // Check the TLD-appending functionality.
[email protected]dc7d3052013-05-31 21:23:28323 FixupCase tld_cases[] = {
[email protected]91bf0c12014-08-12 14:49:24324 {"somedomainthatwillnotbeagtld",
325 "http://www.somedomainthatwillnotbeagtld.com/"},
326 {"somedomainthatwillnotbeagtld.",
327 "http://www.somedomainthatwillnotbeagtld.com/"},
328 {"somedomainthatwillnotbeagtld..",
329 "http://www.somedomainthatwillnotbeagtld.com/"},
330 {".somedomainthatwillnotbeagtld",
331 "http://www.somedomainthatwillnotbeagtld.com/"},
332 {"www.somedomainthatwillnotbeagtld",
333 "http://www.somedomainthatwillnotbeagtld.com/"},
334 {"somedomainthatwillnotbeagtld.com",
335 "http://somedomainthatwillnotbeagtld.com/"},
336 {"http://somedomainthatwillnotbeagtld",
337 "http://www.somedomainthatwillnotbeagtld.com/"},
338 {"..somedomainthatwillnotbeagtld..",
339 "http://www.somedomainthatwillnotbeagtld.com/"},
340 {"http://www.somedomainthatwillnotbeagtld",
341 "http://www.somedomainthatwillnotbeagtld.com/"},
342 {"9999999999999999", "http://www.9999999999999999.com/"},
343 {"somedomainthatwillnotbeagtld/foo",
344 "http://www.somedomainthatwillnotbeagtld.com/foo"},
345 {"somedomainthatwillnotbeagtld.com/foo",
346 "http://somedomainthatwillnotbeagtld.com/foo"},
347 {"somedomainthatwillnotbeagtld/?foo=.com",
348 "http://www.somedomainthatwillnotbeagtld.com/?foo=.com"},
349 {"www.somedomainthatwillnotbeagtld/?foo=www.",
350 "http://www.somedomainthatwillnotbeagtld.com/?foo=www."},
351 {"somedomainthatwillnotbeagtld.com/?foo=.com",
352 "http://somedomainthatwillnotbeagtld.com/?foo=.com"},
353 {"http://www.somedomainthatwillnotbeagtld.com",
354 "http://www.somedomainthatwillnotbeagtld.com/"},
355 {"somedomainthatwillnotbeagtld:123",
356 "http://www.somedomainthatwillnotbeagtld.com:123/"},
357 {"http://somedomainthatwillnotbeagtld:123",
358 "http://www.somedomainthatwillnotbeagtld.com:123/"},
initial.commit09911bf2008-07-26 23:55:29359 };
[email protected]b1c33f82009-01-23 01:51:23360 for (size_t i = 0; i < arraysize(tld_cases); ++i) {
[email protected]dc7d3052013-05-31 21:23:28361 FixupCase value = tld_cases[i];
[email protected]78977682014-05-10 18:42:25362 EXPECT_EQ(value.output,
[email protected]9b5b1d602014-06-12 14:29:02363 url_fixer::FixupURL(value.input, "com").possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29364 }
365}
366
367// Test different types of file inputs to URIFixerUpper::FixupURL. This
368// doesn't go into the nice array of fixups above since the file input
369// has to exist.
[email protected]9b5b1d602014-06-12 14:29:02370TEST(URLFixerTest, FixupFile) {
initial.commit09911bf2008-07-26 23:55:29371 // this "original" filename is the one we tweak to get all the variations
[email protected]97803e1b2014-07-25 01:52:14372 base::ScopedTempDir temp_dir_;
373 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:45374 base::FilePath original;
[email protected]d3216442009-03-05 21:07:27375 ASSERT_TRUE(MakeTempFile(
[email protected]97803e1b2014-07-25 01:52:14376 temp_dir_.path(),
[email protected]650b2d52013-02-10 03:41:45377 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
[email protected]d3216442009-03-05 21:07:27378 &original));
initial.commit09911bf2008-07-26 23:55:29379
380 // reference path
[email protected]76e7da22010-06-18 22:44:49381 GURL golden(net::FilePathToFileURL(original));
initial.commit09911bf2008-07-26 23:55:29382
383 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic)
[email protected]9b5b1d602014-06-12 14:29:02384 GURL fixedup(url_fixer::FixupURL(original.AsUTF8Unsafe(), std::string()));
initial.commit09911bf2008-07-26 23:55:29385 EXPECT_EQ(golden, fixedup);
386
[email protected]b1c33f82009-01-23 01:51:23387 // TODO(port): Make some equivalent tests for posix.
388#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29389 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon)
[email protected]036a5f32013-12-25 00:26:11390 std::string cur(base::WideToUTF8(original.value()));
initial.commit09911bf2008-07-26 23:55:29391 EXPECT_EQ(':', cur[1]);
392 cur[1] = '|';
[email protected]9b5b1d602014-06-12 14:29:02393 EXPECT_EQ(golden, url_fixer::FixupURL(cur, std::string()));
initial.commit09911bf2008-07-26 23:55:29394
[email protected]78977682014-05-10 18:42:25395 FixupCase cases[] = {
396 {"c:\\Non-existent%20file.txt", "file:///C:/Non-existent%2520file.txt"},
initial.commit09911bf2008-07-26 23:55:29397
398 // \\foo\bar.txt -> file://foo/bar.txt
399 // UNC paths, this file won't exist, but since there are no escapes, it
400 // should be returned just converted to a file: URL.
[email protected]78977682014-05-10 18:42:25401 {"\\\\NonexistentHost\\foo\\bar.txt", "file://nonexistenthost/foo/bar.txt"},
[email protected]7fc13ed2010-03-06 05:06:20402 // We do this strictly, like IE8, which only accepts this form using
[email protected]76e7da22010-06-18 22:44:49403 // backslashes and not forward ones. Turning "//foo" into "http" matches
404 // Firefox and IE, silly though it may seem (it falls out of adding "http"
405 // as the default protocol if you haven't entered one).
[email protected]78977682014-05-10 18:42:25406 {"//NonexistentHost\\foo/bar.txt", "http://nonexistenthost/foo/bar.txt"},
407 {"file:///C:/foo/bar", "file:///C:/foo/bar"},
initial.commit09911bf2008-07-26 23:55:29408
[email protected]76e7da22010-06-18 22:44:49409 // Much of the work here comes from GURL's canonicalization stage.
[email protected]78977682014-05-10 18:42:25410 {"file://C:/foo/bar", "file:///C:/foo/bar"},
411 {"file:c:", "file:///C:/"},
412 {"file:c:WINDOWS", "file:///C:/WINDOWS"},
413 {"file:c|Program Files", "file:///C:/Program%20Files"},
414 {"file:/file", "file://file/"},
415 {"file:////////c:\\foo", "file:///C:/foo"},
416 {"file://server/folder/file", "file://server/folder/file"},
[email protected]76e7da22010-06-18 22:44:49417
initial.commit09911bf2008-07-26 23:55:29418 // These are fixups we don't do, but could consider:
[email protected]78977682014-05-10 18:42:25419 // {"file:///foo:/bar", "file://foo/bar"},
420 // {"file:/\\/server\\folder/file", "file://server/folder/file"},
initial.commit09911bf2008-07-26 23:55:29421 };
[email protected]ba1321d12009-04-21 22:42:29422#elif defined(OS_POSIX)
[email protected]762c5542009-10-21 16:45:38423
424#if defined(OS_MACOSX)
425#define HOME "/Users/"
426#else
427#define HOME "/home/"
428#endif
[email protected]9b5b1d602014-06-12 14:29:02429 url_fixer::home_directory_override = "/foo";
[email protected]78977682014-05-10 18:42:25430 FixupCase cases[] = {
[email protected]ba1321d12009-04-21 22:42:29431 // File URLs go through GURL, which tries to escape intelligently.
[email protected]78977682014-05-10 18:42:25432 {"/A%20non-existent file.txt", "file:///A%2520non-existent%20file.txt"},
[email protected]ba1321d12009-04-21 22:42:29433 // A plain "/" refers to the root.
[email protected]78977682014-05-10 18:42:25434 {"/", "file:///"},
[email protected]762c5542009-10-21 16:45:38435
436 // These rely on the above home_directory_override.
[email protected]78977682014-05-10 18:42:25437 {"~", "file:///foo"},
438 {"~/bar", "file:///foo/bar"},
[email protected]762c5542009-10-21 16:45:38439
440 // References to other users' homedirs.
[email protected]78977682014-05-10 18:42:25441 {"~foo", "file://" HOME "foo"},
442 {"~x/blah", "file://" HOME "x/blah"},
[email protected]ba1321d12009-04-21 22:42:29443 };
444#endif
[email protected]78977682014-05-10 18:42:25445
446 for (size_t i = 0; i < arraysize(cases); i++) {
447 EXPECT_EQ(cases[i].output,
[email protected]9b5b1d602014-06-12 14:29:02448 url_fixer::FixupURL(cases[i].input, "").possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29449 }
450
[email protected]dd3aa792013-07-16 19:10:23451 EXPECT_TRUE(base::DeleteFile(original, false));
initial.commit09911bf2008-07-26 23:55:29452}
453
[email protected]9b5b1d602014-06-12 14:29:02454TEST(URLFixerTest, FixupRelativeFile) {
[email protected]97803e1b2014-07-25 01:52:14455 base::FilePath full_path;
[email protected]650b2d52013-02-10 03:41:45456 base::FilePath file_part(
457 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
[email protected]97803e1b2014-07-25 01:52:14458 base::ScopedTempDir temp_dir_;
459 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
460 ASSERT_TRUE(MakeTempFile(temp_dir_.path(), file_part, &full_path));
[email protected]15476932013-04-12 05:17:15461 full_path = base::MakeAbsoluteFilePath(full_path);
462 ASSERT_FALSE(full_path.empty());
initial.commit09911bf2008-07-26 23:55:29463
464 // make sure we pass through good URLs
[email protected]b1c33f82009-01-23 01:51:23465 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
[email protected]dc7d3052013-05-31 21:23:28466 FixupCase value = fixup_cases[i];
[email protected]0b58a812013-05-31 01:15:17467 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input);
[email protected]76e7da22010-06-18 22:44:49468 EXPECT_EQ(value.output,
[email protected]97803e1b2014-07-25 01:52:14469 url_fixer::FixupRelativeFile(temp_dir_.path(),
470 input).possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29471 }
472
473 // make sure the existing file got fixed-up to a file URL, and that there
474 // are no backslashes
[email protected]9b5b1d602014-06-12 14:29:02475 EXPECT_TRUE(IsMatchingFileURL(
[email protected]97803e1b2014-07-25 01:52:14476 url_fixer::FixupRelativeFile(temp_dir_.path(),
477 file_part).possibly_invalid_spec(), full_path));
[email protected]dd3aa792013-07-16 19:10:23478 EXPECT_TRUE(base::DeleteFile(full_path, false));
initial.commit09911bf2008-07-26 23:55:29479
480 // create a filename we know doesn't exist and make sure it doesn't get
481 // fixed up to a file URL
[email protected]650b2d52013-02-10 03:41:45482 base::FilePath nonexistent_file(
[email protected]d3216442009-03-05 21:07:27483 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
[email protected]97803e1b2014-07-25 01:52:14484 std::string fixedup(url_fixer::FixupRelativeFile(
485 temp_dir_.path(), nonexistent_file).possibly_invalid_spec());
[email protected]b1c33f82009-01-23 01:51:23486 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
initial.commit09911bf2008-07-26 23:55:29487 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
488
489 // make a subdir to make sure relative paths with directories work, also
[email protected]d3216442009-03-05 21:07:27490 // test spaces:
491 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt"
[email protected]650b2d52013-02-10 03:41:45492 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
493 base::FilePath sub_file(
494 FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
[email protected]97803e1b2014-07-25 01:52:14495 base::FilePath new_dir = temp_dir_.path().Append(sub_dir);
[email protected]426d1c92013-12-03 20:08:54496 base::CreateDirectory(new_dir);
initial.commit09911bf2008-07-26 23:55:29497 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
[email protected]15476932013-04-12 05:17:15498 full_path = base::MakeAbsoluteFilePath(full_path);
499 ASSERT_FALSE(full_path.empty());
initial.commit09911bf2008-07-26 23:55:29500
501 // test file in the subdir
[email protected]650b2d52013-02-10 03:41:45502 base::FilePath relative_file = sub_dir.Append(sub_file);
[email protected]9b5b1d602014-06-12 14:29:02503 EXPECT_TRUE(IsMatchingFileURL(
[email protected]97803e1b2014-07-25 01:52:14504 url_fixer::FixupRelativeFile(temp_dir_.path(),
505 relative_file).possibly_invalid_spec(), full_path));
initial.commit09911bf2008-07-26 23:55:29506
[email protected]b1c33f82009-01-23 01:51:23507 // test file in the subdir with different slashes and escaping.
[email protected]650b2d52013-02-10 03:41:45508 base::FilePath::StringType relative_file_str = sub_dir.value() +
[email protected]b1c33f82009-01-23 01:51:23509 FILE_PATH_LITERAL("/") + sub_file.value();
510 ReplaceSubstringsAfterOffset(&relative_file_str, 0,
511 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
[email protected]9b5b1d602014-06-12 14:29:02512 EXPECT_TRUE(IsMatchingFileURL(
[email protected]97803e1b2014-07-25 01:52:14513 url_fixer::FixupRelativeFile(temp_dir_.path(),
514 base::FilePath(relative_file_str)).possibly_invalid_spec(),
515 full_path));
initial.commit09911bf2008-07-26 23:55:29516
517 // test relative directories and duplicate slashes
518 // (should resolve to the same file as above)
[email protected]b1c33f82009-01-23 01:51:23519 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
520 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
[email protected]9b5b1d602014-06-12 14:29:02521 EXPECT_TRUE(IsMatchingFileURL(
[email protected]97803e1b2014-07-25 01:52:14522 url_fixer::FixupRelativeFile(temp_dir_.path(),
523 base::FilePath(relative_file_str)).possibly_invalid_spec(),
524 full_path));
initial.commit09911bf2008-07-26 23:55:29525
526 // done with the subdir
[email protected]dd3aa792013-07-16 19:10:23527 EXPECT_TRUE(base::DeleteFile(full_path, false));
528 EXPECT_TRUE(base::DeleteFile(new_dir, true));
[email protected]a64c3cf2011-08-06 05:25:55529
530 // Test that an obvious HTTP URL isn't accidentally treated as an absolute
531 // file path (on account of system-specific craziness).
[email protected]650b2d52013-02-10 03:41:45532 base::FilePath empty_path;
533 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../"));
[email protected]9b5b1d602014-06-12 14:29:02534 EXPECT_TRUE(
535 url_fixer::FixupRelativeFile(empty_path, http_url_path).SchemeIs("http"));
initial.commit09911bf2008-07-26 23:55:29536}