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 <stddef.h>
6 #include <stdlib.h>
7
8 #include <string>
9
10 #include "base/base_paths.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "build/build_config.h"
18 #include "components/url_formatter/url_fixer.h"
19 #include "net/base/filename_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h"
22 #include "url/third_party/mozilla/url_parse.h"
23
24 namespace url {
25
operator <<(std::ostream & os,const Component & part)26 std::ostream& operator<<(std::ostream& os, const Component& part) {
27 return os << "(begin=" << part.begin << ", len=" << part.len << ")";
28 }
29
30 } // namespace url
31
32 struct SegmentCase {
33 const std::string input;
34 const std::string result;
35 const url::Component scheme;
36 const url::Component username;
37 const url::Component password;
38 const url::Component host;
39 const url::Component port;
40 const url::Component path;
41 const url::Component query;
42 const url::Component ref;
43 };
44
45 static const SegmentCase segment_cases[] = {
46 {
47 "http://www.google.com/", "http", url::Component(0, 4), // scheme
48 url::Component(), // username
49 url::Component(), // password
50 url::Component(7, 14), // host
51 url::Component(), // port
52 url::Component(21, 1), // path
53 url::Component(), // query
54 url::Component(), // ref
55 },
56 {
57 "aBoUt:vErSiOn", "about", url::Component(0, 5), // scheme
58 url::Component(), // username
59 url::Component(), // password
60 url::Component(6, 7), // host
61 url::Component(), // port
62 url::Component(), // path
63 url::Component(), // query
64 url::Component(), // ref
65 },
66 {
67 "about:host/path?query#ref", "about", url::Component(0, 5), // scheme
68 url::Component(), // username
69 url::Component(), // password
70 url::Component(6, 4), // host
71 url::Component(), // port
72 url::Component(10, 5), // path
73 url::Component(16, 5), // query
74 url::Component(22, 3), // ref
75 },
76 {
77 "about://host/path?query#ref", "about", url::Component(0, 5), // scheme
78 url::Component(), // username
79 url::Component(), // password
80 url::Component(8, 4), // host
81 url::Component(), // port
82 url::Component(12, 5), // path
83 url::Component(18, 5), // query
84 url::Component(24, 3), // ref
85 },
86 {
87 "chrome:host/path?query#ref", "chrome", url::Component(0, 6), // scheme
88 url::Component(), // username
89 url::Component(), // password
90 url::Component(7, 4), // host
91 url::Component(), // port
92 url::Component(11, 5), // path
93 url::Component(17, 5), // query
94 url::Component(23, 3), // ref
95 },
96 {
97 "chrome://host/path?query#ref", "chrome",
98 url::Component(0, 6), // scheme
99 url::Component(), // username
100 url::Component(), // password
101 url::Component(9, 4), // host
102 url::Component(), // port
103 url::Component(13, 5), // path
104 url::Component(19, 5), // query
105 url::Component(25, 3), // ref
106 },
107 {
108 " www.google.com:124?foo#", "http",
109 url::Component(), // scheme
110 url::Component(), // username
111 url::Component(), // password
112 url::Component(4, 14), // host
113 url::Component(19, 3), // port
114 url::Component(), // path
115 url::Component(23, 3), // query
116 url::Component(27, 0), // ref
117 },
118 {
119 "user@www.google.com", "http",
120 url::Component(), // scheme
121 url::Component(0, 4), // username
122 url::Component(), // password
123 url::Component(5, 14), // host
124 url::Component(), // port
125 url::Component(), // path
126 url::Component(), // query
127 url::Component(), // ref
128 },
129 {
130 "ftp:/user:P:a$$Wd@..ftp.google.com...::23///pub?foo#bar", "ftp",
131 url::Component(0, 3), // scheme
132 url::Component(5, 4), // username
133 url::Component(10, 7), // password
134 url::Component(18, 20), // host
135 url::Component(39, 2), // port
136 url::Component(41, 6), // path
137 url::Component(48, 3), // query
138 url::Component(52, 3), // ref
139 },
140 {
141 "[2001:db8::1]/path", "http",
142 url::Component(), // scheme
143 url::Component(), // username
144 url::Component(), // password
145 url::Component(0, 13), // host
146 url::Component(), // port
147 url::Component(13, 5), // path
148 url::Component(), // query
149 url::Component(), // ref
150 },
151 {
152 "[::1]", "http",
153 url::Component(), // scheme
154 url::Component(), // username
155 url::Component(), // password
156 url::Component(0, 5), // host
157 url::Component(), // port
158 url::Component(), // path
159 url::Component(), // query
160 url::Component(), // ref
161 },
162 // Incomplete IPv6 addresses (will not canonicalize).
163 {
164 "[2001:4860:", "http",
165 url::Component(), // scheme
166 url::Component(), // username
167 url::Component(), // password
168 url::Component(0, 11), // host
169 url::Component(), // port
170 url::Component(), // path
171 url::Component(), // query
172 url::Component(), // ref
173 },
174 {
175 "[2001:4860:/foo", "http",
176 url::Component(), // scheme
177 url::Component(), // username
178 url::Component(), // password
179 url::Component(0, 11), // host
180 url::Component(), // port
181 url::Component(11, 4), // path
182 url::Component(), // query
183 url::Component(), // ref
184 },
185 {
186 "http://:b005::68]", "http", url::Component(0, 4), // scheme
187 url::Component(), // username
188 url::Component(), // password
189 url::Component(7, 10), // host
190 url::Component(), // port
191 url::Component(), // path
192 url::Component(), // query
193 url::Component(), // ref
194 },
195 {
196 ":b005::68]", "http",
197 url::Component(), // scheme
198 url::Component(), // username
199 url::Component(), // password
200 url::Component(1, 9), // host
201 url::Component(), // port
202 url::Component(), // path
203 url::Component(), // query
204 url::Component(), // ref
205 },
206 {
207 "file://host/path/file#ref", "file", url::Component(0, 4), // scheme
208 url::Component(), // username
209 url::Component(), // password
210 url::Component(7, 4), // host
211 url::Component(), // port
212 url::Component(11, 10), // path
213 url::Component(), // query
214 url::Component(22, 3), // ref
215 },
216 {
217 "file:///notahost/path/file#ref", "file",
218 url::Component(0, 4), // scheme
219 url::Component(), // username
220 url::Component(), // password
221 url::Component(), // host
222 url::Component(), // port
223 url::Component(7, 19), // path
224 url::Component(), // query
225 url::Component(27, 3), // ref
226 },
227 #if defined(OS_WIN)
228 {
229 "c:/notahost/path/file#ref", "file",
230 url::Component(), // scheme
231 url::Component(), // username
232 url::Component(), // password
233 url::Component(), // host
234 url::Component(), // port
235 url::Component(0, 21), // path
236 url::Component(), // query
237 url::Component(22, 3), // ref
238 },
239 #elif defined(OS_POSIX)
240 {
241 "~/notahost/path/file#ref", "file",
242 url::Component(), // scheme
243 url::Component(), // username
244 url::Component(), // password
245 url::Component(), // host
246 url::Component(), // port
247 url::Component(0, 20), // path
248 url::Component(), // query
249 url::Component(21, 3), // ref
250 },
251 #endif
252 {
253 "devtools://bundled/devtools/inspector.html?ws=localhost:9221",
254 "devtools", url::Component(0, 8), // scheme
255 url::Component(), // username
256 url::Component(), // password
257 url::Component(11, 7), // host
258 url::Component(), // port
259 url::Component(18, 24), // path
260 url::Component(43, 17), // query
261 url::Component(), // ref
262 },
263 };
264
265 typedef testing::Test URLFixerTest;
266
TEST(URLFixerTest,SegmentURL)267 TEST(URLFixerTest, SegmentURL) {
268 std::string result;
269 url::Parsed parts;
270
271 for (size_t i = 0; i < base::size(segment_cases); ++i) {
272 SegmentCase value = segment_cases[i];
273 SCOPED_TRACE(testing::Message() << "test #" << i << ": " << value.input);
274
275 result = url_formatter::SegmentURL(value.input, &parts);
276 EXPECT_EQ(value.result, result);
277 EXPECT_EQ(value.scheme, parts.scheme);
278 EXPECT_EQ(value.username, parts.username);
279 EXPECT_EQ(value.password, parts.password);
280 EXPECT_EQ(value.host, parts.host);
281 EXPECT_EQ(value.port, parts.port);
282 EXPECT_EQ(value.path, parts.path);
283 EXPECT_EQ(value.query, parts.query);
284 EXPECT_EQ(value.ref, parts.ref);
285 }
286 }
287
288 // Creates a file and returns its full name as well as the decomposed
289 // version. Example:
290 // full_path = "c:\foo\bar.txt"
291 // dir = "c:\foo"
292 // file_name = "bar.txt"
MakeTempFile(const base::FilePath & dir,const base::FilePath & file_name,base::FilePath * full_path)293 static bool MakeTempFile(const base::FilePath& dir,
294 const base::FilePath& file_name,
295 base::FilePath* full_path) {
296 *full_path = dir.Append(file_name);
297 return base::WriteFile(*full_path, "", 0) == 0;
298 }
299
300 // Returns true if the given URL is a file: URL that matches the given file
IsMatchingFileURL(const std::string & url,const base::FilePath & full_file_path)301 static bool IsMatchingFileURL(const std::string& url,
302 const base::FilePath& full_file_path) {
303 if (url.length() <= 8)
304 return false;
305 if (std::string("file:///") != url.substr(0, 8))
306 return false; // no file:/// prefix
307 if (url.find('\\') != std::string::npos)
308 return false; // contains backslashes
309
310 base::FilePath derived_path;
311 net::FileURLToFilePath(GURL(url), &derived_path);
312
313 return base::FilePath::CompareEqualIgnoreCase(derived_path.value(),
314 full_file_path.value());
315 }
316
317 struct FixupCase {
318 const std::string input;
319 const std::string output;
320 } fixup_cases[] = {
321 {"www.google.com", "http://www.google.com/"},
322 {" www.google.com ", "http://www.google.com/"},
323 {" foo.com/asdf bar", "http://foo.com/asdf%20%20bar"},
324 {"..www.google.com..", "http://www.google.com./"},
325 {"http://......", "http://....../"},
326 {"http://host.com:ninety-two/", "http://host.com:ninety-two/"},
327 {"http://host.com:ninety-two?foo", "http://host.com:ninety-two/?foo"},
328 {"google.com:123", "http://google.com:123/"},
329 {"about:", "chrome://version/"},
330 {"about:foo", "chrome://foo/"},
331 {"about:version", "chrome://version/"},
332 {"about:blank", "about:blank"},
333 {"About:blaNk", "about:blank"},
334 {"about:blank#blah", "about:blank#blah"},
335 {"about:blank/#blah", "about:blank/#blah"},
336 {"about:srcdoc", "about:srcdoc"},
337 {"about:srcdoc#blah", "about:srcdoc#blah"},
338 {"about:srcdoc/#blah", "about:srcdoc/#blah"},
339 {"about:usr:pwd@hst:20/pth?qry#ref", "chrome://hst/pth?qry#ref"},
340 {"about://usr:pwd@hst/pth?qry#ref", "chrome://hst/pth?qry#ref"},
341 {"chrome:usr:pwd@hst/pth?qry#ref", "chrome://hst/pth?qry#ref"},
342 {"chrome://usr:pwd@hst/pth?qry#ref", "chrome://hst/pth?qry#ref"},
343 {"www:123", "http://www:123/"},
344 {" www:123", "http://www:123/"},
345 {"www.google.com?foo", "http://www.google.com/?foo"},
346 {"www.google.com#foo", "http://www.google.com/#foo"},
347 {"www.google.com?", "http://www.google.com/?"},
348 {"www.google.com#", "http://www.google.com/#"},
349 {"www.google.com:123?foo#bar", "http://www.google.com:123/?foo#bar"},
350 {"user@www.google.com", "http://user@www.google.com/"},
351 {"\xE6\xB0\xB4.com", "http://xn--1rw.com/"},
352 // It would be better if this next case got treated as http, but I don't see
353 // a clean way to guess this isn't the new-and-exciting "user" scheme.
354 {"user:passwd@www.google.com:8080/", "user:passwd@www.google.com:8080/"},
355 // {"file:///c:/foo/bar%20baz.txt", "file:///C:/foo/bar%20baz.txt"},
356 // URLs which end with 0x85 (NEL in ISO-8859).
357 {"http://foo.com/s?q=\xd0\x85", "http://foo.com/s?q=%D0%85"},
358 {"http://foo.com/s?q=\xec\x97\x85", "http://foo.com/s?q=%EC%97%85"},
359 {"http://foo.com/s?q=\xf0\x90\x80\x85", "http://foo.com/s?q=%F0%90%80%85"},
360 // URLs which end with 0xA0 (non-break space in ISO-8859).
361 {"http://foo.com/s?q=\xd0\xa0", "http://foo.com/s?q=%D0%A0"},
362 {"http://foo.com/s?q=\xec\x97\xa0", "http://foo.com/s?q=%EC%97%A0"},
363 {"http://foo.com/s?q=\xf0\x90\x80\xa0", "http://foo.com/s?q=%F0%90%80%A0"},
364 // URLs containing IPv6 literals.
365 {"[2001:db8::2]", "http://[2001:db8::2]/"},
366 {"[::]:80", "http://[::]/"},
367 {"[::]:80/path", "http://[::]/path"},
368 {"[::]:180/path", "http://[::]:180/path"},
369 // TODO(pmarks): Maybe we should parse bare IPv6 literals someday. Currently
370 // the first colon is treated as a scheme separator, and we default
371 // unspecified schemes to "http".
372 {"::1", "http://:1/"},
373 // Semicolon as scheme separator for standard schemes.
374 {"http;//www.google.com/", "http://www.google.com/"},
375 {"about;help", "chrome://help/"},
376 // Semicolon in non-standard schemes is not replaced by colon.
377 {"whatsup;//fool", "http://whatsup%3B//fool"},
378 // Semicolon left as-is in URL itself.
379 {"http://host/port?query;moar", "http://host/port?query;moar"},
380 // Fewer slashes than expected.
381 {"http;www.google.com/", "http://www.google.com/"},
382 {"http;/www.google.com/", "http://www.google.com/"},
383 // Semicolon at start.
384 {";http://www.google.com/", "http://%3Bhttp//www.google.com/"},
385 // Devtools scheme.
386 {"devtools://bundled/devtools/node.html",
387 "devtools://bundled/devtools/node.html"},
388 // Devtools scheme with websocket query.
389 {"devtools://bundled/devtools/inspector.html?ws=ws://localhost:9222/guid",
390 "devtools://bundled/devtools/inspector.html?ws=ws://localhost:9222/guid"},
391 // host:123 should be rewritten to http://host:123/, but only if the port
392 // number is valid - in particular telephone numbers are not port numbers
393 // (see also SendTabToSelfUtilTest.ShouldNotOfferFeatureForTelephoneLink).
394 {"host:123", "http://host:123/"},
395 {"host:80", "http://host/"}, // default port is removed
396 {"host:9999", "http://host:9999/"},
397 {"host:00009999", "http://host:9999/"}, // leading zeros are removed
398 {"host:0", "http://host:0/"}, // min valid port
399 {"host:65535", "http://host:65535/"}, // max valid port
400 {"host:-1", "host:-1"},
401 {"host:65536", "host:65536"},
402 {"host:18446744073709551619", "host:18446744073709551619"}, // > uint64.max
403 {"host:", "host:"},
404 {"host: 123", "host: 123"},
405 {"host:+123", "host:+123"},
406 {"host:1.23", "host:1.23"},
407 {"host:x", "host:x"},
408 {"host:᠐", "host:%E1%A0%90"}, // non-ASCII digit (U+1810)
409 {"host:", "host:%F0%9D%9F%A8"}, // non-ASCII digit (U+1D7E8)
410 {"tel:12345678901", "tel:12345678901"},
411 {"tel:123-456-78901", "tel:123-456-78901"},
412 // Double colon after host should not convert to an empty port.
413 {"foo.com::/server-redirect?http%3A%2F%2Fbar.com%2Ftitle2.html",
414 "http://foo.com/server-redirect?http%3A%2F%2Fbar.com%2Ftitle2.html"},
415 };
416
TEST(URLFixerTest,FixupURL)417 TEST(URLFixerTest, FixupURL) {
418 for (size_t i = 0; i < base::size(fixup_cases); ++i) {
419 FixupCase value = fixup_cases[i];
420 GURL actual_output = url_formatter::FixupURL(value.input, std::string());
421 EXPECT_EQ(value.output, actual_output.possibly_invalid_spec())
422 << "input: " << value.input;
423
424 // Fixup URL should never translate a valid GURL into an invalid one.
425 if (GURL(value.input).is_valid())
426 EXPECT_TRUE(actual_output.is_valid());
427 }
428
429 // Check the TLD-appending functionality.
430 FixupCase tld_cases[] = {
431 {"somedomainthatwillnotbeagtld",
432 "http://www.somedomainthatwillnotbeagtld.com/"},
433 {"somedomainthatwillnotbeagtld.",
434 "http://www.somedomainthatwillnotbeagtld.com/"},
435 {"somedomainthatwillnotbeagtld..",
436 "http://www.somedomainthatwillnotbeagtld.com/"},
437 {".somedomainthatwillnotbeagtld",
438 "http://www.somedomainthatwillnotbeagtld.com/"},
439 {"www.somedomainthatwillnotbeagtld",
440 "http://www.somedomainthatwillnotbeagtld.com/"},
441 {"somedomainthatwillnotbeagtld.com",
442 "http://somedomainthatwillnotbeagtld.com/"},
443 {"http://somedomainthatwillnotbeagtld",
444 "http://www.somedomainthatwillnotbeagtld.com/"},
445 {"..somedomainthatwillnotbeagtld..",
446 "http://www.somedomainthatwillnotbeagtld.com/"},
447 {"http://www.somedomainthatwillnotbeagtld",
448 "http://www.somedomainthatwillnotbeagtld.com/"},
449 {"9999999999999999", "http://www.9999999999999999.com/"},
450 {"somedomainthatwillnotbeagtld/foo",
451 "http://www.somedomainthatwillnotbeagtld.com/foo"},
452 {"somedomainthatwillnotbeagtld.com/foo",
453 "http://somedomainthatwillnotbeagtld.com/foo"},
454 {"somedomainthatwillnotbeagtld/?foo=.com",
455 "http://www.somedomainthatwillnotbeagtld.com/?foo=.com"},
456 {"www.somedomainthatwillnotbeagtld/?foo=www.",
457 "http://www.somedomainthatwillnotbeagtld.com/?foo=www."},
458 {"somedomainthatwillnotbeagtld.com/?foo=.com",
459 "http://somedomainthatwillnotbeagtld.com/?foo=.com"},
460 {"http://www.somedomainthatwillnotbeagtld.com",
461 "http://www.somedomainthatwillnotbeagtld.com/"},
462 {"somedomainthatwillnotbeagtld:123",
463 "http://www.somedomainthatwillnotbeagtld.com:123/"},
464 {"http://somedomainthatwillnotbeagtld:123",
465 "http://www.somedomainthatwillnotbeagtld.com:123/"},
466 };
467 for (size_t i = 0; i < base::size(tld_cases); ++i) {
468 FixupCase value = tld_cases[i];
469 EXPECT_EQ(value.output, url_formatter::FixupURL(value.input, "com")
470 .possibly_invalid_spec());
471 }
472 }
473
474 // Test different types of file inputs to URIFixerUpper::FixupURL. This
475 // doesn't go into the nice array of fixups above since the file input
476 // has to exist.
TEST(URLFixerTest,FixupFile)477 TEST(URLFixerTest, FixupFile) {
478 // this "original" filename is the one we tweak to get all the variations
479 base::ScopedTempDir temp_dir_;
480 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
481 base::FilePath original;
482 ASSERT_TRUE(MakeTempFile(
483 temp_dir_.GetPath(),
484 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
485 &original));
486
487 // reference path
488 GURL golden(net::FilePathToFileURL(original));
489
490 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic)
491 GURL fixedup(url_formatter::FixupURL(original.AsUTF8Unsafe(), std::string()));
492 EXPECT_EQ(golden, fixedup);
493
494 // TODO(port): Make some equivalent tests for posix.
495 #if defined(OS_WIN)
496 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon)
497 std::string cur(base::WideToUTF8(original.value()));
498 EXPECT_EQ(':', cur[1]);
499 cur[1] = '|';
500 EXPECT_EQ(golden, url_formatter::FixupURL(cur, std::string()));
501
502 FixupCase cases[] = {
503 {"c:\\Non-existent%20file.txt", "file:///C:/Non-existent%2520file.txt"},
504
505 // \\foo\bar.txt -> file://foo/bar.txt
506 // UNC paths, this file won't exist, but since there are no escapes, it
507 // should be returned just converted to a file: URL.
508 {"\\\\NonexistentHost\\foo\\bar.txt", "file://nonexistenthost/foo/bar.txt"},
509 // We do this strictly, like IE8, which only accepts this form using
510 // backslashes and not forward ones. Turning "//foo" into "http" matches
511 // Firefox and IE, silly though it may seem (it falls out of adding "http"
512 // as the default protocol if you haven't entered one).
513 {"//NonexistentHost\\foo/bar.txt", "http://nonexistenthost/foo/bar.txt"},
514 {"file:///C:/foo/bar", "file:///C:/foo/bar"},
515
516 // Much of the work here comes from GURL's canonicalization stage.
517 {"file://C:/foo/bar", "file:///C:/foo/bar"},
518 {"file:c:", "file:///C:/"},
519 {"file:c:WINDOWS", "file:///C:/WINDOWS"},
520 {"file:c|Program Files", "file:///C:/Program%20Files"},
521 {"file:/file", "file://file/"},
522 {"file:////////c:\\foo", "file:///C:/foo"},
523 {"file://server/folder/file", "file://server/folder/file"},
524
525 // These are fixups we don't do, but could consider:
526 // {"file:///foo:/bar", "file://foo/bar"},
527 // {"file:/\\/server\\folder/file", "file://server/folder/file"},
528 };
529 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
530
531 #if defined(OS_MACOSX)
532 #define HOME "/Users/"
533 #else
534 #define HOME "/home/"
535 #endif
536 url_formatter::home_directory_override = "/foo";
537 FixupCase cases[] = {
538 // File URLs go through GURL, which tries to escape intelligently.
539 {"/A%20non-existent file.txt", "file:///A%2520non-existent%20file.txt"},
540 // A plain "/" refers to the root.
541 {"/", "file:///"},
542
543 // These rely on the above home_directory_override.
544 {"~", "file:///foo"},
545 {"~/bar", "file:///foo/bar"},
546
547 // References to other users' homedirs.
548 {"~foo", "file://" HOME "foo"},
549 {"~x/blah", "file://" HOME "x/blah"},
550 };
551 #endif
552
553 for (size_t i = 0; i < base::size(cases); i++) {
554 EXPECT_EQ(cases[i].output,
555 url_formatter::FixupURL(cases[i].input, std::string())
556 .possibly_invalid_spec());
557 }
558
559 EXPECT_TRUE(base::DeleteFile(original, false));
560 }
561
TEST(URLFixerTest,FixupRelativeFile)562 TEST(URLFixerTest, FixupRelativeFile) {
563 base::FilePath full_path;
564 base::FilePath file_part(
565 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
566 base::ScopedTempDir temp_dir_;
567 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
568 ASSERT_TRUE(MakeTempFile(temp_dir_.GetPath(), file_part, &full_path));
569 full_path = base::MakeAbsoluteFilePath(full_path);
570 ASSERT_FALSE(full_path.empty());
571
572 // make sure we pass through good URLs
573 for (size_t i = 0; i < base::size(fixup_cases); ++i) {
574 FixupCase value = fixup_cases[i];
575 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input);
576 EXPECT_EQ(value.output,
577 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), input)
578 .possibly_invalid_spec())
579 << "input: " << value.input;
580 }
581
582 // make sure the existing file got fixed-up to a file URL, and that there
583 // are no backslashes
584 EXPECT_TRUE(IsMatchingFileURL(
585 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), file_part)
586 .possibly_invalid_spec(),
587 full_path));
588 EXPECT_TRUE(base::DeleteFile(full_path, false));
589
590 // create a filename we know doesn't exist and make sure it doesn't get
591 // fixed up to a file URL
592 base::FilePath nonexistent_file(
593 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
594 std::string fixedup(
595 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), nonexistent_file)
596 .possibly_invalid_spec());
597 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
598 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
599
600 // make a subdir to make sure relative paths with directories work, also
601 // test spaces:
602 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt"
603 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
604 base::FilePath sub_file(
605 FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
606 base::FilePath new_dir = temp_dir_.GetPath().Append(sub_dir);
607 base::CreateDirectory(new_dir);
608 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
609 full_path = base::MakeAbsoluteFilePath(full_path);
610 ASSERT_FALSE(full_path.empty());
611
612 // test file in the subdir
613 base::FilePath relative_file = sub_dir.Append(sub_file);
614 EXPECT_TRUE(IsMatchingFileURL(
615 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), relative_file)
616 .possibly_invalid_spec(),
617 full_path));
618
619 // test file in the subdir with different slashes and escaping.
620 base::FilePath::StringType relative_file_str = sub_dir.value() +
621 FILE_PATH_LITERAL("/") + sub_file.value();
622 base::ReplaceSubstringsAfterOffset(&relative_file_str, 0,
623 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
624 EXPECT_TRUE(IsMatchingFileURL(
625 url_formatter::FixupRelativeFile(temp_dir_.GetPath(),
626 base::FilePath(relative_file_str))
627 .possibly_invalid_spec(),
628 full_path));
629
630 // test relative directories and duplicate slashes
631 // (should resolve to the same file as above)
632 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
633 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
634 EXPECT_TRUE(IsMatchingFileURL(
635 url_formatter::FixupRelativeFile(temp_dir_.GetPath(),
636 base::FilePath(relative_file_str))
637 .possibly_invalid_spec(),
638 full_path));
639
640 // done with the subdir
641 EXPECT_TRUE(base::DeleteFile(full_path, false));
642 EXPECT_TRUE(base::DeleteFileRecursively(new_dir));
643
644 // Test that an obvious HTTP URL isn't accidentally treated as an absolute
645 // file path (on account of system-specific craziness).
646 base::FilePath empty_path;
647 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../"));
648 EXPECT_TRUE(url_formatter::FixupRelativeFile(empty_path, http_url_path)
649 .SchemeIs("http"));
650 }
651