blob: be647adcc052e11854de6fbb3c8ee4f2b1a85bc5 [file] [log] [blame]
[email protected]13044cd2010-02-10 18:56:181// Copyright (c) 2010 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
7#include "base/basictypes.h"
[email protected]b1c33f82009-01-23 01:51:238#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/path_service.h"
10#include "base/string_util.h"
[email protected]d55194ca2010-03-11 18:25:4511#include "base/utf_string_conversions.h"
[email protected]f870a322009-01-16 21:47:2712#include "chrome/browser/net/url_fixer_upper.h"
initial.commit09911bf2008-07-26 23:55:2913#include "chrome/common/chrome_paths.h"
14#include "googleurl/src/url_parse.h"
15#include "googleurl/src/gurl.h"
16#include "net/base/net_util.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace {
20 class URLFixerUpperTest : public testing::Test {
21 };
22};
23
24std::ostream& operator<<(std::ostream& os, const url_parse::Component& part) {
25 return os << "(begin=" << part.begin << ", len=" << part.len << ")";
26}
27
28struct segment_case {
[email protected]b1c33f82009-01-23 01:51:2329 const std::string input;
30 const std::string result;
initial.commit09911bf2008-07-26 23:55:2931 const url_parse::Component scheme;
32 const url_parse::Component username;
33 const url_parse::Component password;
34 const url_parse::Component host;
35 const url_parse::Component port;
36 const url_parse::Component path;
37 const url_parse::Component query;
38 const url_parse::Component ref;
39};
40
41static const segment_case segment_cases[] = {
[email protected]b1c33f82009-01-23 01:51:2342 { "http://www.google.com/", "http",
initial.commit09911bf2008-07-26 23:55:2943 url_parse::Component(0, 4), // scheme
44 url_parse::Component(), // username
45 url_parse::Component(), // password
46 url_parse::Component(7, 14), // host
47 url_parse::Component(), // port
48 url_parse::Component(21, 1), // path
49 url_parse::Component(), // query
50 url_parse::Component(), // ref
51 },
[email protected]b1c33f82009-01-23 01:51:2352 { "aBoUt:vErSiOn", "about",
initial.commit09911bf2008-07-26 23:55:2953 url_parse::Component(0, 5), // scheme
54 url_parse::Component(), // username
55 url_parse::Component(), // password
56 url_parse::Component(), // host
57 url_parse::Component(), // port
58 url_parse::Component(), // path
59 url_parse::Component(), // query
60 url_parse::Component(), // ref
61 },
[email protected]b1c33f82009-01-23 01:51:2362 { " www.google.com:124?foo#", "http",
initial.commit09911bf2008-07-26 23:55:2963 url_parse::Component(), // scheme
64 url_parse::Component(), // username
65 url_parse::Component(), // password
66 url_parse::Component(4, 14), // host
67 url_parse::Component(19, 3), // port
68 url_parse::Component(), // path
69 url_parse::Component(23, 3), // query
70 url_parse::Component(27, 0), // ref
71 },
[email protected]b1c33f82009-01-23 01:51:2372 { "[email protected]", "http",
initial.commit09911bf2008-07-26 23:55:2973 url_parse::Component(), // scheme
74 url_parse::Component(0, 4), // username
75 url_parse::Component(), // password
76 url_parse::Component(5, 14), // host
77 url_parse::Component(), // port
78 url_parse::Component(), // path
79 url_parse::Component(), // query
80 url_parse::Component(), // ref
81 },
[email protected]b1c33f82009-01-23 01:51:2382 { "ftp:/user:P:[email protected]...::23///pub?foo#bar", "ftp",
initial.commit09911bf2008-07-26 23:55:2983 url_parse::Component(0, 3), // scheme
84 url_parse::Component(5, 4), // username
85 url_parse::Component(10, 7), // password
86 url_parse::Component(18, 20), // host
87 url_parse::Component(39, 2), // port
88 url_parse::Component(41, 6), // path
89 url_parse::Component(48, 3), // query
90 url_parse::Component(52, 3), // ref
91 },
[email protected]818071ce2009-05-18 01:25:2592 { "[2001:db8::1]/path", "http",
93 url_parse::Component(), // scheme
94 url_parse::Component(), // username
95 url_parse::Component(), // password
96 url_parse::Component(0, 13), // host
97 url_parse::Component(), // port
98 url_parse::Component(13, 5), // path
99 url_parse::Component(), // query
100 url_parse::Component(), // ref
101 },
102 { "[::1]", "http",
103 url_parse::Component(), // scheme
104 url_parse::Component(), // username
105 url_parse::Component(), // password
106 url_parse::Component(0, 5), // host
107 url_parse::Component(), // port
108 url_parse::Component(), // path
109 url_parse::Component(), // query
110 url_parse::Component(), // ref
111 },
[email protected]c1991302009-06-04 03:57:39112 // Incomplete IPv6 addresses (will not canonicalize).
113 { "[2001:4860:", "http",
114 url_parse::Component(), // scheme
115 url_parse::Component(), // username
116 url_parse::Component(), // password
117 url_parse::Component(0, 11), // host
118 url_parse::Component(), // port
119 url_parse::Component(), // path
120 url_parse::Component(), // query
121 url_parse::Component(), // ref
122 },
123 { "[2001:4860:/foo", "http",
124 url_parse::Component(), // scheme
125 url_parse::Component(), // username
126 url_parse::Component(), // password
127 url_parse::Component(0, 11), // host
128 url_parse::Component(), // port
129 url_parse::Component(11, 4), // path
130 url_parse::Component(), // query
131 url_parse::Component(), // ref
132 },
133 { "http://:b005::68]", "http",
134 url_parse::Component(0, 4), // scheme
135 url_parse::Component(), // username
136 url_parse::Component(), // password
137 url_parse::Component(7, 10), // host
138 url_parse::Component(), // port
139 url_parse::Component(), // path
140 url_parse::Component(), // query
141 url_parse::Component(), // ref
142 },
143 // Can't do anything useful with this.
144 { ":b005::68]", "",
145 url_parse::Component(0, 0), // scheme
146 url_parse::Component(), // username
147 url_parse::Component(), // password
148 url_parse::Component(), // host
149 url_parse::Component(), // port
150 url_parse::Component(), // path
151 url_parse::Component(), // query
152 url_parse::Component(), // ref
153 },
initial.commit09911bf2008-07-26 23:55:29154};
155
156TEST(URLFixerUpperTest, SegmentURL) {
[email protected]b1c33f82009-01-23 01:51:23157 std::string result;
initial.commit09911bf2008-07-26 23:55:29158 url_parse::Parsed parts;
159
[email protected]b1c33f82009-01-23 01:51:23160 for (size_t i = 0; i < arraysize(segment_cases); ++i) {
initial.commit09911bf2008-07-26 23:55:29161 segment_case value = segment_cases[i];
162 result = URLFixerUpper::SegmentURL(value.input, &parts);
163 EXPECT_EQ(value.result, result);
164 EXPECT_EQ(value.scheme, parts.scheme);
165 EXPECT_EQ(value.username, parts.username);
166 EXPECT_EQ(value.password, parts.password);
167 EXPECT_EQ(value.host, parts.host);
168 EXPECT_EQ(value.port, parts.port);
169 EXPECT_EQ(value.path, parts.path);
170 EXPECT_EQ(value.query, parts.query);
171 EXPECT_EQ(value.ref, parts.ref);
172 }
173}
174
175// Creates a file and returns its full name as well as the decomposed
176// version. Example:
177// full_path = "c:\foo\bar.txt"
178// dir = "c:\foo"
179// file_name = "bar.txt"
[email protected]b1c33f82009-01-23 01:51:23180static bool MakeTempFile(const FilePath& dir,
181 const FilePath& file_name,
182 FilePath* full_path) {
183 *full_path = dir.Append(file_name);
[email protected]7ff3f632009-10-13 18:43:35184 return file_util::WriteFile(*full_path, "", 0) == 0;
initial.commit09911bf2008-07-26 23:55:29185}
186
187// Returns true if the given URL is a file: URL that matches the given file
[email protected]b1c33f82009-01-23 01:51:23188static bool IsMatchingFileURL(const std::string& url,
189 const FilePath& full_file_path) {
initial.commit09911bf2008-07-26 23:55:29190 if (url.length() <= 8)
191 return false;
[email protected]b1c33f82009-01-23 01:51:23192 if (std::string("file:///") != url.substr(0, 8))
initial.commit09911bf2008-07-26 23:55:29193 return false; // no file:/// prefix
[email protected]b1c33f82009-01-23 01:51:23194 if (url.find('\\') != std::string::npos)
initial.commit09911bf2008-07-26 23:55:29195 return false; // contains backslashes
196
[email protected]b1c33f82009-01-23 01:51:23197 FilePath derived_path;
[email protected]56741852008-12-17 19:04:50198 net::FileURLToFilePath(GURL(url), &derived_path);
[email protected]b1c33f82009-01-23 01:51:23199
[email protected]eccb9d12009-10-28 05:40:09200 return FilePath::CompareEqualIgnoreCase(derived_path.value(),
201 full_file_path.value());
initial.commit09911bf2008-07-26 23:55:29202}
203
204struct fixup_case {
[email protected]b1c33f82009-01-23 01:51:23205 const std::string input;
206 const std::string desired_tld;
207 const std::string output;
initial.commit09911bf2008-07-26 23:55:29208} fixup_cases[] = {
[email protected]b1c33f82009-01-23 01:51:23209 {"www.google.com", "", "http://www.google.com/"},
210 {" www.google.com ", "", "http://www.google.com/"},
211 {" foo.com/asdf bar", "", "http://foo.com/asdf bar"},
212 {"..www.google.com..", "", "http://www.google.com./"},
213 {"http://......", "", "http://....../"},
[email protected]ce85f602009-11-07 01:34:53214 {"http://host.com:ninety-two/", "", "http://host.com:ninety-two/"},
215 {"http://host.com:ninety-two?foo", "", "http://host.com:ninety-two/?foo"},
[email protected]b1c33f82009-01-23 01:51:23216 {"google.com:123", "", "http://google.com:123/"},
217 {"about:", "", "about:"},
218 {"about:version", "", "about:version"},
219 {"www:123", "", "http://www:123/"},
220 {" www:123", "", "http://www:123/"},
221 {"www.google.com?foo", "", "http://www.google.com/?foo"},
222 {"www.google.com#foo", "", "http://www.google.com/#foo"},
223 {"www.google.com?", "", "http://www.google.com/?"},
224 {"www.google.com#", "", "http://www.google.com/#"},
225 {"www.google.com:123?foo#bar", "", "http://www.google.com:123/?foo#bar"},
226 {"[email protected]", "", "http://[email protected]/"},
227 {"\xE6\xB0\xB4.com" , "", "http://\xE6\xB0\xB4.com/"},
initial.commit09911bf2008-07-26 23:55:29228 // It would be better if this next case got treated as http, but I don't see
229 // a clean way to guess this isn't the new-and-exciting "user" scheme.
[email protected]b1c33f82009-01-23 01:51:23230 {"user:[email protected]:8080/", "", "user:[email protected]:8080/"},
[email protected]038d52e12009-10-14 16:53:41231 // {"file:///c:/foo/bar%20baz.txt", "", "file:///C:/foo/bar%20baz.txt"},
[email protected]b1c33f82009-01-23 01:51:23232 {"ftp.google.com", "", "ftp://ftp.google.com/"},
233 {" ftp.google.com", "", "ftp://ftp.google.com/"},
234 {"FTP.GooGle.com", "", "ftp://FTP.GooGle.com/"},
235 {"ftpblah.google.com", "", "http://ftpblah.google.com/"},
236 {"ftp", "", "http://ftp/"},
237 {"google.ftp.com", "", "http://google.ftp.com/"},
[email protected]90f933a2009-03-05 03:41:51238 // URLs which end with 0x85 (NEL in ISO-8859).
239 { "http://google.com/search?q=\xd0\x85", "",
240 "http://google.com/search?q=\xd0\x85"
241 },
242 { "http://google.com/search?q=\xec\x97\x85", "",
243 "http://google.com/search?q=\xec\x97\x85"
244 },
245 { "http://google.com/search?q=\xf0\x90\x80\x85", "",
246 "http://google.com/search?q=\xf0\x90\x80\x85"
247 },
248 // URLs which end with 0xA0 (non-break space in ISO-8859).
249 { "http://google.com/search?q=\xd0\xa0", "",
250 "http://google.com/search?q=\xd0\xa0"
251 },
252 { "http://google.com/search?q=\xec\x97\xa0", "",
253 "http://google.com/search?q=\xec\x97\xa0"
254 },
255 { "http://google.com/search?q=\xf0\x90\x80\xa0", "",
256 "http://google.com/search?q=\xf0\x90\x80\xa0"
257 },
[email protected]818071ce2009-05-18 01:25:25258 // URLs containing IPv6 literals.
259 {"[2001:db8::2]", "", "http://[2001:db8::2]/"},
260 {"[::]:80", "", "http://[::]:80/"},
261 {"[::]:80/path", "", "http://[::]:80/path"},
262 // TODO(pmarks): Maybe we should parse bare IPv6 literals someday.
263 {"::1", "", "::1"},
initial.commit09911bf2008-07-26 23:55:29264};
265
266TEST(URLFixerUpperTest, FixupURL) {
[email protected]b1c33f82009-01-23 01:51:23267 std::string output;
initial.commit09911bf2008-07-26 23:55:29268
[email protected]b1c33f82009-01-23 01:51:23269 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
initial.commit09911bf2008-07-26 23:55:29270 fixup_case value = fixup_cases[i];
271 output = URLFixerUpper::FixupURL(value.input, value.desired_tld);
272 EXPECT_EQ(value.output, output);
273 }
274
275 // Check the TLD-appending functionality
276 fixup_case tld_cases[] = {
[email protected]b1c33f82009-01-23 01:51:23277 {"google", "com", "http://www.google.com/"},
278 {"google.", "com", "http://www.google.com/"},
279 {"google..", "com", "http://www.google.com/"},
280 {".google", "com", "http://www.google.com/"},
281 {"www.google", "com", "http://www.google.com/"},
282 {"google.com", "com", "http://google.com/"},
283 {"http://google", "com", "http://www.google.com/"},
284 {"..google..", "com", "http://www.google.com/"},
285 {"http://www.google", "com", "http://www.google.com/"},
[email protected]7e563812010-03-22 20:05:59286 {"9999999999999999", "com", "http://www.9999999999999999.com/"},
[email protected]b1c33f82009-01-23 01:51:23287 {"google/foo", "com", "http://www.google.com/foo"},
288 {"google.com/foo", "com", "http://google.com/foo"},
289 {"google/?foo=.com", "com", "http://www.google.com/?foo=.com"},
290 {"www.google/?foo=www.", "com", "http://www.google.com/?foo=www."},
291 {"google.com/?foo=.com", "com", "http://google.com/?foo=.com"},
292 {"http://www.google.com", "com", "http://www.google.com/"},
293 {"google:123", "com", "http://www.google.com:123/"},
294 {"http://google:123", "com", "http://www.google.com:123/"},
initial.commit09911bf2008-07-26 23:55:29295 };
[email protected]b1c33f82009-01-23 01:51:23296 for (size_t i = 0; i < arraysize(tld_cases); ++i) {
initial.commit09911bf2008-07-26 23:55:29297 fixup_case value = tld_cases[i];
298 output = URLFixerUpper::FixupURL(value.input, value.desired_tld);
299 EXPECT_EQ(value.output, output);
300 }
301}
302
303// Test different types of file inputs to URIFixerUpper::FixupURL. This
304// doesn't go into the nice array of fixups above since the file input
305// has to exist.
306TEST(URLFixerUpperTest, FixupFile) {
307 // this "original" filename is the one we tweak to get all the variations
[email protected]b1c33f82009-01-23 01:51:23308 FilePath dir;
309 FilePath original;
initial.commit09911bf2008-07-26 23:55:29310 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir));
[email protected]d3216442009-03-05 21:07:27311 ASSERT_TRUE(MakeTempFile(
312 dir,
313 FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
314 &original));
initial.commit09911bf2008-07-26 23:55:29315
316 // reference path
[email protected]b1c33f82009-01-23 01:51:23317 std::string golden = net::FilePathToFileURL(original).spec();
initial.commit09911bf2008-07-26 23:55:29318
319 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic)
[email protected]b1c33f82009-01-23 01:51:23320#if defined(OS_WIN)
[email protected]d3216442009-03-05 21:07:27321 std::string fixedup = URLFixerUpper::FixupURL(WideToUTF8(original.value()),
322 "");
[email protected]b1c33f82009-01-23 01:51:23323#elif defined(OS_POSIX)
324 std::string fixedup = URLFixerUpper::FixupURL(original.value(), "");
325#endif
initial.commit09911bf2008-07-26 23:55:29326 EXPECT_EQ(golden, fixedup);
327
[email protected]b1c33f82009-01-23 01:51:23328 // TODO(port): Make some equivalent tests for posix.
329#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29330 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon)
[email protected]b1c33f82009-01-23 01:51:23331 std::string cur(WideToUTF8(original.value()));
initial.commit09911bf2008-07-26 23:55:29332 EXPECT_EQ(':', cur[1]);
333 cur[1] = '|';
[email protected]b1c33f82009-01-23 01:51:23334 fixedup = URLFixerUpper::FixupURL(cur, "");
initial.commit09911bf2008-07-26 23:55:29335 EXPECT_EQ(golden, fixedup);
336
337 fixup_case file_cases[] = {
338 // File URLs go through GURL, which tries to escape intelligently.
[email protected]d3216442009-03-05 21:07:27339 {"c:\\This%20is a non-existent file.txt", "",
340 "file:///C:/This%2520is%20a%20non-existent%20file.txt"},
initial.commit09911bf2008-07-26 23:55:29341
342 // \\foo\bar.txt -> file://foo/bar.txt
343 // UNC paths, this file won't exist, but since there are no escapes, it
344 // should be returned just converted to a file: URL.
[email protected]d3216442009-03-05 21:07:27345 {"\\\\SomeNonexistentHost\\foo\\bar.txt", "",
346 "file://somenonexistenthost/foo/bar.txt"},
[email protected]7fc13ed2010-03-06 05:06:20347 // We do this strictly, like IE8, which only accepts this form using
348 // backslashes and not forward ones. Its a bit weird that the host/path is
349 // "more canonicalized" in the UNC case above, and in the http case it
350 // isn't lowercased, etc. That level of canonicalization will happen when
351 // it's actually turned into a GURL, so we don't care about it here. Turning
352 // "//foo" into "http" matches Firefox and IE, silly though it may seem
353 // (it falls out of adding "http" as the default protocol if you haven't
354 // entered one).
[email protected]d3216442009-03-05 21:07:27355 {"//SomeNonexistentHost\\foo/bar.txt", "",
[email protected]7fc13ed2010-03-06 05:06:20356 "http://SomeNonexistentHost\\foo/bar.txt"},
[email protected]b1c33f82009-01-23 01:51:23357 {"file:///C:/foo/bar", "", "file:///C:/foo/bar"},
initial.commit09911bf2008-07-26 23:55:29358
359 // These are fixups we don't do, but could consider:
360 //
[email protected]b1c33f82009-01-23 01:51:23361 // {"file://C:/foo/bar", "", "file:///C:/foo/bar"},
362 // {"file:c:", "", "file:///c:/"},
363 // {"file:c:WINDOWS", "", "file:///c:/WINDOWS"},
364 // {"file:c|Program Files", "", "file:///c:/Program Files"},
365 // {"file:///foo:/bar", "", "file://foo/bar"},
366 // {"file:/file", "", "file://file/"},
367 // {"file:////////c:\\foo", "", "file:///c:/foo"},
368 // {"file://server/folder/file", "", "file://server/folder/file"},
369 // {"file:/\\/server\\folder/file", "", "file://server/folder/file"},
initial.commit09911bf2008-07-26 23:55:29370 };
[email protected]ba1321d12009-04-21 22:42:29371#elif defined(OS_POSIX)
[email protected]762c5542009-10-21 16:45:38372
373#if defined(OS_MACOSX)
374#define HOME "/Users/"
375#else
376#define HOME "/home/"
377#endif
378 URLFixerUpper::home_directory_override = "/foo";
[email protected]ba1321d12009-04-21 22:42:29379 fixup_case file_cases[] = {
380 // File URLs go through GURL, which tries to escape intelligently.
381 {"/This%20is a non-existent file.txt", "",
382 "file:///This%2520is%20a%20non-existent%20file.txt"},
383 // A plain "/" refers to the root.
384 {"/", "",
385 "file:///"},
[email protected]762c5542009-10-21 16:45:38386
387 // These rely on the above home_directory_override.
388 {"~", "",
389 "file:///foo"},
390 {"~/bar", "",
391 "file:///foo/bar"},
392
393 // References to other users' homedirs.
394 {"~foo", "",
395 "file://" HOME "foo"},
396 {"~x/blah", "",
397 "file://" HOME "x/blah"},
[email protected]ba1321d12009-04-21 22:42:29398 };
399#endif
[email protected]b1c33f82009-01-23 01:51:23400 for (size_t i = 0; i < arraysize(file_cases); i++) {
initial.commit09911bf2008-07-26 23:55:29401 fixedup = URLFixerUpper::FixupURL(file_cases[i].input,
402 file_cases[i].desired_tld);
403 EXPECT_EQ(file_cases[i].output, fixedup);
404 }
405
[email protected]b1c33f82009-01-23 01:51:23406 EXPECT_TRUE(file_util::Delete(original, false));
initial.commit09911bf2008-07-26 23:55:29407}
408
409TEST(URLFixerUpperTest, FixupRelativeFile) {
[email protected]b1c33f82009-01-23 01:51:23410 FilePath full_path, dir;
411 FilePath file_part(FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
initial.commit09911bf2008-07-26 23:55:29412 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir));
413 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path));
[email protected]13044cd2010-02-10 18:56:18414 ASSERT_TRUE(file_util::AbsolutePath(&full_path));
initial.commit09911bf2008-07-26 23:55:29415
416 // make sure we pass through good URLs
[email protected]b1c33f82009-01-23 01:51:23417 std::string fixedup;
418 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
initial.commit09911bf2008-07-26 23:55:29419 fixup_case value = fixup_cases[i];
[email protected]b1c33f82009-01-23 01:51:23420#if defined(OS_WIN)
421 FilePath input(UTF8ToWide(value.input));
422#elif defined(OS_POSIX)
423 FilePath input(value.input);
424#endif
425 fixedup = URLFixerUpper::FixupRelativeFile(dir, input);
initial.commit09911bf2008-07-26 23:55:29426 EXPECT_EQ(value.output, fixedup);
427 }
428
429 // make sure the existing file got fixed-up to a file URL, and that there
430 // are no backslashes
431 fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part);
[email protected]b1c33f82009-01-23 01:51:23432 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path));
433 EXPECT_TRUE(file_util::Delete(full_path, false));
initial.commit09911bf2008-07-26 23:55:29434
435 // create a filename we know doesn't exist and make sure it doesn't get
436 // fixed up to a file URL
[email protected]d3216442009-03-05 21:07:27437 FilePath nonexistent_file(
438 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
initial.commit09911bf2008-07-26 23:55:29439 fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file);
[email protected]b1c33f82009-01-23 01:51:23440 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
initial.commit09911bf2008-07-26 23:55:29441 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
442
443 // make a subdir to make sure relative paths with directories work, also
[email protected]d3216442009-03-05 21:07:27444 // test spaces:
445 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt"
[email protected]b1c33f82009-01-23 01:51:23446 FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
447 FilePath sub_file(FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
448 FilePath new_dir = dir.Append(sub_dir);
449 file_util::CreateDirectory(new_dir);
initial.commit09911bf2008-07-26 23:55:29450 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
[email protected]13044cd2010-02-10 18:56:18451 ASSERT_TRUE(file_util::AbsolutePath(&full_path));
initial.commit09911bf2008-07-26 23:55:29452
453 // test file in the subdir
[email protected]b1c33f82009-01-23 01:51:23454 FilePath relative_file = sub_dir.Append(sub_file);
initial.commit09911bf2008-07-26 23:55:29455 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file);
[email protected]b1c33f82009-01-23 01:51:23456 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path));
initial.commit09911bf2008-07-26 23:55:29457
[email protected]b1c33f82009-01-23 01:51:23458 // test file in the subdir with different slashes and escaping.
459 FilePath::StringType relative_file_str = sub_dir.value() +
460 FILE_PATH_LITERAL("/") + sub_file.value();
461 ReplaceSubstringsAfterOffset(&relative_file_str, 0,
462 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
463 fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str));
464 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path));
initial.commit09911bf2008-07-26 23:55:29465
466 // test relative directories and duplicate slashes
467 // (should resolve to the same file as above)
[email protected]b1c33f82009-01-23 01:51:23468 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
469 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
470 fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str));
471 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path));
initial.commit09911bf2008-07-26 23:55:29472
473 // done with the subdir
[email protected]b1c33f82009-01-23 01:51:23474 EXPECT_TRUE(file_util::Delete(full_path, false));
475 EXPECT_TRUE(file_util::Delete(new_dir, true));
initial.commit09911bf2008-07-26 23:55:29476}