blob: 9eb330bd87b9db2814a76e777541c65f5f770391 [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]b1c33f82009-01-23 01:51:239#include "base/file_util.h"
[email protected]9b5b1d602014-06-12 14:29:0210#include "base/files/file_path.h"
[email protected]97803e1b2014-07-25 01:52:1411#include "base/files/scoped_temp_dir.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/path_service.h"
[email protected]f9b294362013-06-10 20:22:3113#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1814#include "base/strings/utf_string_conversions.h"
[email protected]9b5b1d602014-06-12 14:29:0215#include "components/url_fixer/url_fixer.h"
[email protected]d96cf752014-04-09 04:05:2816#include "net/base/filename_util.h"
initial.commit09911bf2008-07-26 23:55:2917#include "testing/gtest/include/gtest/gtest.h"
[email protected]761fa4702013-07-02 15:25:1518#include "url/gurl.h"
19#include "url/url_parse.h"
initial.commit09911bf2008-07-26 23:55:2920
[email protected]0318f922014-04-22 00:09:2321namespace url {
[email protected]46cf4be92010-10-01 11:23:2022
23std::ostream& operator<<(std::ostream& os, const Component& part) {
initial.commit09911bf2008-07-26 23:55:2924 return os << "(begin=" << part.begin << ", len=" << part.len << ")";
25}
26
[email protected]0318f922014-04-22 00:09:2327} // namespace url
[email protected]46cf4be92010-10-01 11:23:2028
[email protected]dc7d3052013-05-31 21:23:2829struct SegmentCase {
[email protected]b1c33f82009-01-23 01:51:2330 const std::string input;
31 const std::string result;
[email protected]b45334502014-04-30 19:44:0532 const url::Component scheme;
33 const url::Component username;
34 const url::Component password;
35 const url::Component host;
36 const url::Component port;
37 const url::Component path;
38 const url::Component query;
39 const url::Component ref;
initial.commit09911bf2008-07-26 23:55:2940};
41
[email protected]dc7d3052013-05-31 21:23:2842static const SegmentCase segment_cases[] = {
[email protected]b1c33f82009-01-23 01:51:2343 { "http://www.google.com/", "http",
[email protected]b45334502014-04-30 19:44:0544 url::Component(0, 4), // scheme
45 url::Component(), // username
46 url::Component(), // password
47 url::Component(7, 14), // host
48 url::Component(), // port
49 url::Component(21, 1), // path
50 url::Component(), // query
51 url::Component(), // ref
initial.commit09911bf2008-07-26 23:55:2952 },
[email protected]b1c33f82009-01-23 01:51:2353 { "aBoUt:vErSiOn", "about",
[email protected]b45334502014-04-30 19:44:0554 url::Component(0, 5), // scheme
55 url::Component(), // username
56 url::Component(), // password
57 url::Component(6, 7), // host
58 url::Component(), // port
59 url::Component(), // path
60 url::Component(), // query
61 url::Component(), // ref
initial.commit09911bf2008-07-26 23:55:2962 },
[email protected]89f550b2011-06-08 18:34:0363 { "about:host/path?query#ref", "about",
[email protected]b45334502014-04-30 19:44:0564 url::Component(0, 5), // scheme
65 url::Component(), // username
66 url::Component(), // password
67 url::Component(6, 4), // host
68 url::Component(), // port
69 url::Component(10, 5), // path
70 url::Component(16, 5), // query
71 url::Component(22, 3), // ref
[email protected]89f550b2011-06-08 18:34:0372 },
73 { "about://host/path?query#ref", "about",
[email protected]b45334502014-04-30 19:44:0574 url::Component(0, 5), // scheme
75 url::Component(), // username
76 url::Component(), // password
77 url::Component(8, 4), // host
78 url::Component(), // port
79 url::Component(12, 5), // path
80 url::Component(18, 5), // query
81 url::Component(24, 3), // ref
[email protected]89f550b2011-06-08 18:34:0382 },
83 { "chrome:host/path?query#ref", "chrome",
[email protected]b45334502014-04-30 19:44:0584 url::Component(0, 6), // scheme
85 url::Component(), // username
86 url::Component(), // password
87 url::Component(7, 4), // host
88 url::Component(), // port
89 url::Component(11, 5), // path
90 url::Component(17, 5), // query
91 url::Component(23, 3), // ref
[email protected]89f550b2011-06-08 18:34:0392 },
93 { "chrome://host/path?query#ref", "chrome",
[email protected]b45334502014-04-30 19:44:0594 url::Component(0, 6), // scheme
95 url::Component(), // username
96 url::Component(), // password
97 url::Component(9, 4), // host
98 url::Component(), // port
99 url::Component(13, 5), // path
100 url::Component(19, 5), // query
101 url::Component(25, 3), // ref
[email protected]89f550b2011-06-08 18:34:03102 },
[email protected]b1c33f82009-01-23 01:51:23103 { " www.google.com:124?foo#", "http",
[email protected]b45334502014-04-30 19:44:05104 url::Component(), // scheme
105 url::Component(), // username
106 url::Component(), // password
107 url::Component(4, 14), // host
108 url::Component(19, 3), // port
109 url::Component(), // path
110 url::Component(23, 3), // query
111 url::Component(27, 0), // ref
initial.commit09911bf2008-07-26 23:55:29112 },
[email protected]b1c33f82009-01-23 01:51:23113 { "[email protected]", "http",
[email protected]b45334502014-04-30 19:44:05114 url::Component(), // scheme
115 url::Component(0, 4), // username
116 url::Component(), // password
117 url::Component(5, 14), // host
118 url::Component(), // port
119 url::Component(), // path
120 url::Component(), // query
121 url::Component(), // ref
initial.commit09911bf2008-07-26 23:55:29122 },
[email protected]b1c33f82009-01-23 01:51:23123 { "ftp:/user:P:[email protected]...::23///pub?foo#bar", "ftp",
[email protected]b45334502014-04-30 19:44:05124 url::Component(0, 3), // scheme
125 url::Component(5, 4), // username
126 url::Component(10, 7), // password
127 url::Component(18, 20), // host
128 url::Component(39, 2), // port
129 url::Component(41, 6), // path
130 url::Component(48, 3), // query
131 url::Component(52, 3), // ref
initial.commit09911bf2008-07-26 23:55:29132 },
[email protected]818071ce2009-05-18 01:25:25133 { "[2001:db8::1]/path", "http",
[email protected]b45334502014-04-30 19:44:05134 url::Component(), // scheme
135 url::Component(), // username
136 url::Component(), // password
137 url::Component(0, 13), // host
138 url::Component(), // port
139 url::Component(13, 5), // path
140 url::Component(), // query
141 url::Component(), // ref
[email protected]818071ce2009-05-18 01:25:25142 },
143 { "[::1]", "http",
[email protected]b45334502014-04-30 19:44:05144 url::Component(), // scheme
145 url::Component(), // username
146 url::Component(), // password
147 url::Component(0, 5), // host
148 url::Component(), // port
149 url::Component(), // path
150 url::Component(), // query
151 url::Component(), // ref
[email protected]818071ce2009-05-18 01:25:25152 },
[email protected]c1991302009-06-04 03:57:39153 // Incomplete IPv6 addresses (will not canonicalize).
154 { "[2001:4860:", "http",
[email protected]b45334502014-04-30 19:44:05155 url::Component(), // scheme
156 url::Component(), // username
157 url::Component(), // password
158 url::Component(0, 11), // host
159 url::Component(), // port
160 url::Component(), // path
161 url::Component(), // query
162 url::Component(), // ref
[email protected]c1991302009-06-04 03:57:39163 },
164 { "[2001:4860:/foo", "http",
[email protected]b45334502014-04-30 19:44:05165 url::Component(), // scheme
166 url::Component(), // username
167 url::Component(), // password
168 url::Component(0, 11), // host
169 url::Component(), // port
170 url::Component(11, 4), // path
171 url::Component(), // query
172 url::Component(), // ref
[email protected]c1991302009-06-04 03:57:39173 },
174 { "http://:b005::68]", "http",
[email protected]b45334502014-04-30 19:44:05175 url::Component(0, 4), // scheme
176 url::Component(), // username
177 url::Component(), // password
178 url::Component(7, 10), // host
179 url::Component(), // port
180 url::Component(), // path
181 url::Component(), // query
182 url::Component(), // ref
[email protected]c1991302009-06-04 03:57:39183 },
184 // Can't do anything useful with this.
185 { ":b005::68]", "",
[email protected]b45334502014-04-30 19:44:05186 url::Component(0, 0), // scheme
187 url::Component(), // username
188 url::Component(), // password
189 url::Component(), // host
190 url::Component(), // port
191 url::Component(), // path
192 url::Component(), // query
193 url::Component(), // ref
[email protected]c1991302009-06-04 03:57:39194 },
initial.commit09911bf2008-07-26 23:55:29195};
196
[email protected]9b5b1d602014-06-12 14:29:02197typedef testing::Test URLFixerTest;
[email protected]78977682014-05-10 18:42:25198
[email protected]9b5b1d602014-06-12 14:29:02199TEST(URLFixerTest, SegmentURL) {
[email protected]b1c33f82009-01-23 01:51:23200 std::string result;
[email protected]b45334502014-04-30 19:44:05201 url::Parsed parts;
initial.commit09911bf2008-07-26 23:55:29202
[email protected]b1c33f82009-01-23 01:51:23203 for (size_t i = 0; i < arraysize(segment_cases); ++i) {
[email protected]dc7d3052013-05-31 21:23:28204 SegmentCase value = segment_cases[i];
[email protected]9b5b1d602014-06-12 14:29:02205 result = url_fixer::SegmentURL(value.input, &parts);
initial.commit09911bf2008-07-26 23:55:29206 EXPECT_EQ(value.result, result);
207 EXPECT_EQ(value.scheme, parts.scheme);
208 EXPECT_EQ(value.username, parts.username);
209 EXPECT_EQ(value.password, parts.password);
210 EXPECT_EQ(value.host, parts.host);
211 EXPECT_EQ(value.port, parts.port);
212 EXPECT_EQ(value.path, parts.path);
213 EXPECT_EQ(value.query, parts.query);
214 EXPECT_EQ(value.ref, parts.ref);
215 }
216}
217
218// Creates a file and returns its full name as well as the decomposed
219// version. Example:
220// full_path = "c:\foo\bar.txt"
221// dir = "c:\foo"
222// file_name = "bar.txt"
[email protected]650b2d52013-02-10 03:41:45223static bool MakeTempFile(const base::FilePath& dir,
224 const base::FilePath& file_name,
225 base::FilePath* full_path) {
[email protected]b1c33f82009-01-23 01:51:23226 *full_path = dir.Append(file_name);
[email protected]e5c2a22e2014-03-06 20:42:30227 return base::WriteFile(*full_path, "", 0) == 0;
initial.commit09911bf2008-07-26 23:55:29228}
229
230// Returns true if the given URL is a file: URL that matches the given file
[email protected]b1c33f82009-01-23 01:51:23231static bool IsMatchingFileURL(const std::string& url,
[email protected]650b2d52013-02-10 03:41:45232 const base::FilePath& full_file_path) {
initial.commit09911bf2008-07-26 23:55:29233 if (url.length() <= 8)
234 return false;
[email protected]b1c33f82009-01-23 01:51:23235 if (std::string("file:///") != url.substr(0, 8))
initial.commit09911bf2008-07-26 23:55:29236 return false; // no file:/// prefix
[email protected]b1c33f82009-01-23 01:51:23237 if (url.find('\\') != std::string::npos)
initial.commit09911bf2008-07-26 23:55:29238 return false; // contains backslashes
239
[email protected]650b2d52013-02-10 03:41:45240 base::FilePath derived_path;
[email protected]56741852008-12-17 19:04:50241 net::FileURLToFilePath(GURL(url), &derived_path);
[email protected]b1c33f82009-01-23 01:51:23242
[email protected]650b2d52013-02-10 03:41:45243 return base::FilePath::CompareEqualIgnoreCase(derived_path.value(),
[email protected]eccb9d12009-10-28 05:40:09244 full_file_path.value());
initial.commit09911bf2008-07-26 23:55:29245}
246
[email protected]dc7d3052013-05-31 21:23:28247struct FixupCase {
[email protected]b1c33f82009-01-23 01:51:23248 const std::string input;
[email protected]b1c33f82009-01-23 01:51:23249 const std::string output;
initial.commit09911bf2008-07-26 23:55:29250} fixup_cases[] = {
[email protected]78977682014-05-10 18:42:25251 {"www.google.com", "http://www.google.com/"},
252 {" www.google.com ", "http://www.google.com/"},
253 {" foo.com/asdf bar", "http://foo.com/asdf%20%20bar"},
254 {"..www.google.com..", "http://www.google.com./"},
255 {"http://......", "http://....../"},
256 {"http://host.com:ninety-two/", "http://host.com:ninety-two/"},
257 {"http://host.com:ninety-two?foo", "http://host.com:ninety-two/?foo"},
258 {"google.com:123", "http://google.com:123/"},
259 {"about:", "chrome://version/"},
260 {"about:foo", "chrome://foo/"},
261 {"about:version", "chrome://version/"},
262 {"about:blank", "about:blank"},
263 {"about:usr:pwd@hst/pth?qry#ref", "chrome://usr:pwd@hst/pth?qry#ref"},
264 {"about://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 {"chrome://usr:pwd@hst/pth?qry#ref", "chrome://usr:pwd@hst/pth?qry#ref"},
267 {"www:123", "http://www:123/"},
268 {" www:123", "http://www:123/"},
269 {"www.google.com?foo", "http://www.google.com/?foo"},
270 {"www.google.com#foo", "http://www.google.com/#foo"},
271 {"www.google.com?", "http://www.google.com/?"},
272 {"www.google.com#", "http://www.google.com/#"},
273 {"www.google.com:123?foo#bar", "http://www.google.com:123/?foo#bar"},
274 {"[email protected]", "http://[email protected]/"},
275 {"\xE6\xB0\xB4.com", "http://xn--1rw.com/"},
initial.commit09911bf2008-07-26 23:55:29276 // It would be better if this next case got treated as http, but I don't see
277 // a clean way to guess this isn't the new-and-exciting "user" scheme.
[email protected]78977682014-05-10 18:42:25278 {"user:[email protected]:8080/", "user:[email protected]:8080/"},
279 // {"file:///c:/foo/bar%20baz.txt", "file:///C:/foo/bar%20baz.txt"},
280 {"ftp.google.com", "ftp://ftp.google.com/"},
281 {" ftp.google.com", "ftp://ftp.google.com/"},
282 {"FTP.GooGle.com", "ftp://ftp.google.com/"},
283 {"ftpblah.google.com", "http://ftpblah.google.com/"},
284 {"ftp", "http://ftp/"},
285 {"google.ftp.com", "http://google.ftp.com/"},
[email protected]90f933a2009-03-05 03:41:51286 // URLs which end with 0x85 (NEL in ISO-8859).
[email protected]78977682014-05-10 18:42:25287 {"http://foo.com/s?q=\xd0\x85", "http://foo.com/s?q=%D0%85"},
288 {"http://foo.com/s?q=\xec\x97\x85", "http://foo.com/s?q=%EC%97%85"},
289 {"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:51290 // URLs which end with 0xA0 (non-break space in ISO-8859).
[email protected]78977682014-05-10 18:42:25291 {"http://foo.com/s?q=\xd0\xa0", "http://foo.com/s?q=%D0%A0"},
292 {"http://foo.com/s?q=\xec\x97\xa0", "http://foo.com/s?q=%EC%97%A0"},
293 {"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:25294 // URLs containing IPv6 literals.
[email protected]78977682014-05-10 18:42:25295 {"[2001:db8::2]", "http://[2001:db8::2]/"},
296 {"[::]:80", "http://[::]/"},
297 {"[::]:80/path", "http://[::]/path"},
298 {"[::]:180/path", "http://[::]:180/path"},
[email protected]818071ce2009-05-18 01:25:25299 // TODO(pmarks): Maybe we should parse bare IPv6 literals someday.
[email protected]78977682014-05-10 18:42:25300 {"::1", "::1"},
[email protected]ae8e3672013-03-20 09:00:08301 // Semicolon as scheme separator for standard schemes.
[email protected]78977682014-05-10 18:42:25302 {"http;//www.google.com/", "http://www.google.com/"},
303 {"about;chrome", "chrome://chrome/"},
[email protected]ae8e3672013-03-20 09:00:08304 // Semicolon left as-is for non-standard schemes.
[email protected]78977682014-05-10 18:42:25305 {"whatsup;//fool", "whatsup://fool"},
[email protected]ae8e3672013-03-20 09:00:08306 // Semicolon left as-is in URL itself.
[email protected]78977682014-05-10 18:42:25307 {"http://host/port?query;moar", "http://host/port?query;moar"},
[email protected]ae8e3672013-03-20 09:00:08308 // Fewer slashes than expected.
[email protected]78977682014-05-10 18:42:25309 {"http;www.google.com/", "http://www.google.com/"},
310 {"http;/www.google.com/", "http://www.google.com/"},
[email protected]ae8e3672013-03-20 09:00:08311 // Semicolon at start.
[email protected]78977682014-05-10 18:42:25312 {";http://www.google.com/", "http://%3Bhttp//www.google.com/"},
initial.commit09911bf2008-07-26 23:55:29313};
314
[email protected]9b5b1d602014-06-12 14:29:02315TEST(URLFixerTest, FixupURL) {
[email protected]b1c33f82009-01-23 01:51:23316 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
[email protected]dc7d3052013-05-31 21:23:28317 FixupCase value = fixup_cases[i];
[email protected]78977682014-05-10 18:42:25318 EXPECT_EQ(value.output,
[email protected]9b5b1d602014-06-12 14:29:02319 url_fixer::FixupURL(value.input, "").possibly_invalid_spec())
[email protected]ae8e3672013-03-20 09:00:08320 << "input: " << value.input;
initial.commit09911bf2008-07-26 23:55:29321 }
322
[email protected]78977682014-05-10 18:42:25323 // Check the TLD-appending functionality.
[email protected]dc7d3052013-05-31 21:23:28324 FixupCase tld_cases[] = {
[email protected]91bf0c12014-08-12 14:49:24325 {"somedomainthatwillnotbeagtld",
326 "http://www.somedomainthatwillnotbeagtld.com/"},
327 {"somedomainthatwillnotbeagtld.",
328 "http://www.somedomainthatwillnotbeagtld.com/"},
329 {"somedomainthatwillnotbeagtld..",
330 "http://www.somedomainthatwillnotbeagtld.com/"},
331 {".somedomainthatwillnotbeagtld",
332 "http://www.somedomainthatwillnotbeagtld.com/"},
333 {"www.somedomainthatwillnotbeagtld",
334 "http://www.somedomainthatwillnotbeagtld.com/"},
335 {"somedomainthatwillnotbeagtld.com",
336 "http://somedomainthatwillnotbeagtld.com/"},
337 {"http://somedomainthatwillnotbeagtld",
338 "http://www.somedomainthatwillnotbeagtld.com/"},
339 {"..somedomainthatwillnotbeagtld..",
340 "http://www.somedomainthatwillnotbeagtld.com/"},
341 {"http://www.somedomainthatwillnotbeagtld",
342 "http://www.somedomainthatwillnotbeagtld.com/"},
343 {"9999999999999999", "http://www.9999999999999999.com/"},
344 {"somedomainthatwillnotbeagtld/foo",
345 "http://www.somedomainthatwillnotbeagtld.com/foo"},
346 {"somedomainthatwillnotbeagtld.com/foo",
347 "http://somedomainthatwillnotbeagtld.com/foo"},
348 {"somedomainthatwillnotbeagtld/?foo=.com",
349 "http://www.somedomainthatwillnotbeagtld.com/?foo=.com"},
350 {"www.somedomainthatwillnotbeagtld/?foo=www.",
351 "http://www.somedomainthatwillnotbeagtld.com/?foo=www."},
352 {"somedomainthatwillnotbeagtld.com/?foo=.com",
353 "http://somedomainthatwillnotbeagtld.com/?foo=.com"},
354 {"http://www.somedomainthatwillnotbeagtld.com",
355 "http://www.somedomainthatwillnotbeagtld.com/"},
356 {"somedomainthatwillnotbeagtld:123",
357 "http://www.somedomainthatwillnotbeagtld.com:123/"},
358 {"http://somedomainthatwillnotbeagtld:123",
359 "http://www.somedomainthatwillnotbeagtld.com:123/"},
initial.commit09911bf2008-07-26 23:55:29360 };
[email protected]b1c33f82009-01-23 01:51:23361 for (size_t i = 0; i < arraysize(tld_cases); ++i) {
[email protected]dc7d3052013-05-31 21:23:28362 FixupCase value = tld_cases[i];
[email protected]78977682014-05-10 18:42:25363 EXPECT_EQ(value.output,
[email protected]9b5b1d602014-06-12 14:29:02364 url_fixer::FixupURL(value.input, "com").possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29365 }
366}
367
368// Test different types of file inputs to URIFixerUpper::FixupURL. This
369// doesn't go into the nice array of fixups above since the file input
370// has to exist.
[email protected]9b5b1d602014-06-12 14:29:02371TEST(URLFixerTest, FixupFile) {
initial.commit09911bf2008-07-26 23:55:29372 // this "original" filename is the one we tweak to get all the variations
[email protected]97803e1b2014-07-25 01:52:14373 base::ScopedTempDir temp_dir_;
374 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:45375 base::FilePath original;
[email protected]d3216442009-03-05 21:07:27376 ASSERT_TRUE(MakeTempFile(
[email protected]97803e1b2014-07-25 01:52:14377 temp_dir_.path(),
[email protected]650b2d52013-02-10 03:41:45378 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
[email protected]d3216442009-03-05 21:07:27379 &original));
initial.commit09911bf2008-07-26 23:55:29380
381 // reference path
[email protected]76e7da22010-06-18 22:44:49382 GURL golden(net::FilePathToFileURL(original));
initial.commit09911bf2008-07-26 23:55:29383
384 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic)
[email protected]9b5b1d602014-06-12 14:29:02385 GURL fixedup(url_fixer::FixupURL(original.AsUTF8Unsafe(), std::string()));
initial.commit09911bf2008-07-26 23:55:29386 EXPECT_EQ(golden, fixedup);
387
[email protected]b1c33f82009-01-23 01:51:23388 // TODO(port): Make some equivalent tests for posix.
389#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29390 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon)
[email protected]036a5f32013-12-25 00:26:11391 std::string cur(base::WideToUTF8(original.value()));
initial.commit09911bf2008-07-26 23:55:29392 EXPECT_EQ(':', cur[1]);
393 cur[1] = '|';
[email protected]9b5b1d602014-06-12 14:29:02394 EXPECT_EQ(golden, url_fixer::FixupURL(cur, std::string()));
initial.commit09911bf2008-07-26 23:55:29395
[email protected]78977682014-05-10 18:42:25396 FixupCase cases[] = {
397 {"c:\\Non-existent%20file.txt", "file:///C:/Non-existent%2520file.txt"},
initial.commit09911bf2008-07-26 23:55:29398
399 // \\foo\bar.txt -> file://foo/bar.txt
400 // UNC paths, this file won't exist, but since there are no escapes, it
401 // should be returned just converted to a file: URL.
[email protected]78977682014-05-10 18:42:25402 {"\\\\NonexistentHost\\foo\\bar.txt", "file://nonexistenthost/foo/bar.txt"},
[email protected]7fc13ed2010-03-06 05:06:20403 // We do this strictly, like IE8, which only accepts this form using
[email protected]76e7da22010-06-18 22:44:49404 // backslashes and not forward ones. Turning "//foo" into "http" matches
405 // Firefox and IE, silly though it may seem (it falls out of adding "http"
406 // as the default protocol if you haven't entered one).
[email protected]78977682014-05-10 18:42:25407 {"//NonexistentHost\\foo/bar.txt", "http://nonexistenthost/foo/bar.txt"},
408 {"file:///C:/foo/bar", "file:///C:/foo/bar"},
initial.commit09911bf2008-07-26 23:55:29409
[email protected]76e7da22010-06-18 22:44:49410 // Much of the work here comes from GURL's canonicalization stage.
[email protected]78977682014-05-10 18:42:25411 {"file://C:/foo/bar", "file:///C:/foo/bar"},
412 {"file:c:", "file:///C:/"},
413 {"file:c:WINDOWS", "file:///C:/WINDOWS"},
414 {"file:c|Program Files", "file:///C:/Program%20Files"},
415 {"file:/file", "file://file/"},
416 {"file:////////c:\\foo", "file:///C:/foo"},
417 {"file://server/folder/file", "file://server/folder/file"},
[email protected]76e7da22010-06-18 22:44:49418
initial.commit09911bf2008-07-26 23:55:29419 // These are fixups we don't do, but could consider:
[email protected]78977682014-05-10 18:42:25420 // {"file:///foo:/bar", "file://foo/bar"},
421 // {"file:/\\/server\\folder/file", "file://server/folder/file"},
initial.commit09911bf2008-07-26 23:55:29422 };
[email protected]ba1321d12009-04-21 22:42:29423#elif defined(OS_POSIX)
[email protected]762c5542009-10-21 16:45:38424
425#if defined(OS_MACOSX)
426#define HOME "/Users/"
427#else
428#define HOME "/home/"
429#endif
[email protected]9b5b1d602014-06-12 14:29:02430 url_fixer::home_directory_override = "/foo";
[email protected]78977682014-05-10 18:42:25431 FixupCase cases[] = {
[email protected]ba1321d12009-04-21 22:42:29432 // File URLs go through GURL, which tries to escape intelligently.
[email protected]78977682014-05-10 18:42:25433 {"/A%20non-existent file.txt", "file:///A%2520non-existent%20file.txt"},
[email protected]ba1321d12009-04-21 22:42:29434 // A plain "/" refers to the root.
[email protected]78977682014-05-10 18:42:25435 {"/", "file:///"},
[email protected]762c5542009-10-21 16:45:38436
437 // These rely on the above home_directory_override.
[email protected]78977682014-05-10 18:42:25438 {"~", "file:///foo"},
439 {"~/bar", "file:///foo/bar"},
[email protected]762c5542009-10-21 16:45:38440
441 // References to other users' homedirs.
[email protected]78977682014-05-10 18:42:25442 {"~foo", "file://" HOME "foo"},
443 {"~x/blah", "file://" HOME "x/blah"},
[email protected]ba1321d12009-04-21 22:42:29444 };
445#endif
[email protected]78977682014-05-10 18:42:25446
447 for (size_t i = 0; i < arraysize(cases); i++) {
448 EXPECT_EQ(cases[i].output,
[email protected]9b5b1d602014-06-12 14:29:02449 url_fixer::FixupURL(cases[i].input, "").possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29450 }
451
[email protected]dd3aa792013-07-16 19:10:23452 EXPECT_TRUE(base::DeleteFile(original, false));
initial.commit09911bf2008-07-26 23:55:29453}
454
[email protected]9b5b1d602014-06-12 14:29:02455TEST(URLFixerTest, FixupRelativeFile) {
[email protected]97803e1b2014-07-25 01:52:14456 base::FilePath full_path;
[email protected]650b2d52013-02-10 03:41:45457 base::FilePath file_part(
458 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
[email protected]97803e1b2014-07-25 01:52:14459 base::ScopedTempDir temp_dir_;
460 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
461 ASSERT_TRUE(MakeTempFile(temp_dir_.path(), file_part, &full_path));
[email protected]15476932013-04-12 05:17:15462 full_path = base::MakeAbsoluteFilePath(full_path);
463 ASSERT_FALSE(full_path.empty());
initial.commit09911bf2008-07-26 23:55:29464
465 // make sure we pass through good URLs
[email protected]b1c33f82009-01-23 01:51:23466 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
[email protected]dc7d3052013-05-31 21:23:28467 FixupCase value = fixup_cases[i];
[email protected]0b58a812013-05-31 01:15:17468 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input);
[email protected]76e7da22010-06-18 22:44:49469 EXPECT_EQ(value.output,
[email protected]97803e1b2014-07-25 01:52:14470 url_fixer::FixupRelativeFile(temp_dir_.path(),
471 input).possibly_invalid_spec());
initial.commit09911bf2008-07-26 23:55:29472 }
473
474 // make sure the existing file got fixed-up to a file URL, and that there
475 // are no backslashes
[email protected]9b5b1d602014-06-12 14:29:02476 EXPECT_TRUE(IsMatchingFileURL(
[email protected]97803e1b2014-07-25 01:52:14477 url_fixer::FixupRelativeFile(temp_dir_.path(),
478 file_part).possibly_invalid_spec(), full_path));
[email protected]dd3aa792013-07-16 19:10:23479 EXPECT_TRUE(base::DeleteFile(full_path, false));
initial.commit09911bf2008-07-26 23:55:29480
481 // create a filename we know doesn't exist and make sure it doesn't get
482 // fixed up to a file URL
[email protected]650b2d52013-02-10 03:41:45483 base::FilePath nonexistent_file(
[email protected]d3216442009-03-05 21:07:27484 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
[email protected]97803e1b2014-07-25 01:52:14485 std::string fixedup(url_fixer::FixupRelativeFile(
486 temp_dir_.path(), nonexistent_file).possibly_invalid_spec());
[email protected]b1c33f82009-01-23 01:51:23487 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
initial.commit09911bf2008-07-26 23:55:29488 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
489
490 // make a subdir to make sure relative paths with directories work, also
[email protected]d3216442009-03-05 21:07:27491 // test spaces:
492 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt"
[email protected]650b2d52013-02-10 03:41:45493 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
494 base::FilePath sub_file(
495 FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
[email protected]97803e1b2014-07-25 01:52:14496 base::FilePath new_dir = temp_dir_.path().Append(sub_dir);
[email protected]426d1c92013-12-03 20:08:54497 base::CreateDirectory(new_dir);
initial.commit09911bf2008-07-26 23:55:29498 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
[email protected]15476932013-04-12 05:17:15499 full_path = base::MakeAbsoluteFilePath(full_path);
500 ASSERT_FALSE(full_path.empty());
initial.commit09911bf2008-07-26 23:55:29501
502 // test file in the subdir
[email protected]650b2d52013-02-10 03:41:45503 base::FilePath relative_file = sub_dir.Append(sub_file);
[email protected]9b5b1d602014-06-12 14:29:02504 EXPECT_TRUE(IsMatchingFileURL(
[email protected]97803e1b2014-07-25 01:52:14505 url_fixer::FixupRelativeFile(temp_dir_.path(),
506 relative_file).possibly_invalid_spec(), full_path));
initial.commit09911bf2008-07-26 23:55:29507
[email protected]b1c33f82009-01-23 01:51:23508 // test file in the subdir with different slashes and escaping.
[email protected]650b2d52013-02-10 03:41:45509 base::FilePath::StringType relative_file_str = sub_dir.value() +
[email protected]b1c33f82009-01-23 01:51:23510 FILE_PATH_LITERAL("/") + sub_file.value();
511 ReplaceSubstringsAfterOffset(&relative_file_str, 0,
512 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
[email protected]9b5b1d602014-06-12 14:29:02513 EXPECT_TRUE(IsMatchingFileURL(
[email protected]97803e1b2014-07-25 01:52:14514 url_fixer::FixupRelativeFile(temp_dir_.path(),
515 base::FilePath(relative_file_str)).possibly_invalid_spec(),
516 full_path));
initial.commit09911bf2008-07-26 23:55:29517
518 // test relative directories and duplicate slashes
519 // (should resolve to the same file as above)
[email protected]b1c33f82009-01-23 01:51:23520 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
521 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
[email protected]9b5b1d602014-06-12 14:29:02522 EXPECT_TRUE(IsMatchingFileURL(
[email protected]97803e1b2014-07-25 01:52:14523 url_fixer::FixupRelativeFile(temp_dir_.path(),
524 base::FilePath(relative_file_str)).possibly_invalid_spec(),
525 full_path));
initial.commit09911bf2008-07-26 23:55:29526
527 // done with the subdir
[email protected]dd3aa792013-07-16 19:10:23528 EXPECT_TRUE(base::DeleteFile(full_path, false));
529 EXPECT_TRUE(base::DeleteFile(new_dir, true));
[email protected]a64c3cf2011-08-06 05:25:55530
531 // Test that an obvious HTTP URL isn't accidentally treated as an absolute
532 // file path (on account of system-specific craziness).
[email protected]650b2d52013-02-10 03:41:45533 base::FilePath empty_path;
534 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../"));
[email protected]9b5b1d602014-06-12 14:29:02535 EXPECT_TRUE(
536 url_fixer::FixupRelativeFile(empty_path, http_url_path).SchemeIs("http"));
initial.commit09911bf2008-07-26 23:55:29537}