blob: 62ca1f9279af438a94ea0de73c7644591ceb82d8 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
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]f870a322009-01-16 21:47:2711#include "chrome/browser/net/url_fixer_upper.h"
initial.commit09911bf2008-07-26 23:55:2912#include "chrome/common/chrome_paths.h"
13#include "googleurl/src/url_parse.h"
14#include "googleurl/src/gurl.h"
15#include "net/base/net_util.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace {
19 class URLFixerUpperTest : public testing::Test {
20 };
21};
22
23std::ostream& operator<<(std::ostream& os, const url_parse::Component& part) {
24 return os << "(begin=" << part.begin << ", len=" << part.len << ")";
25}
26
27struct segment_case {
[email protected]b1c33f82009-01-23 01:51:2328 const std::string input;
29 const std::string result;
initial.commit09911bf2008-07-26 23:55:2930 const url_parse::Component scheme;
31 const url_parse::Component username;
32 const url_parse::Component password;
33 const url_parse::Component host;
34 const url_parse::Component port;
35 const url_parse::Component path;
36 const url_parse::Component query;
37 const url_parse::Component ref;
38};
39
40static const segment_case segment_cases[] = {
[email protected]b1c33f82009-01-23 01:51:2341 { "http://www.google.com/", "http",
initial.commit09911bf2008-07-26 23:55:2942 url_parse::Component(0, 4), // scheme
43 url_parse::Component(), // username
44 url_parse::Component(), // password
45 url_parse::Component(7, 14), // host
46 url_parse::Component(), // port
47 url_parse::Component(21, 1), // path
48 url_parse::Component(), // query
49 url_parse::Component(), // ref
50 },
[email protected]b1c33f82009-01-23 01:51:2351 { "aBoUt:vErSiOn", "about",
initial.commit09911bf2008-07-26 23:55:2952 url_parse::Component(0, 5), // scheme
53 url_parse::Component(), // username
54 url_parse::Component(), // password
55 url_parse::Component(), // host
56 url_parse::Component(), // port
57 url_parse::Component(), // path
58 url_parse::Component(), // query
59 url_parse::Component(), // ref
60 },
[email protected]b1c33f82009-01-23 01:51:2361 { " www.google.com:124?foo#", "http",
initial.commit09911bf2008-07-26 23:55:2962 url_parse::Component(), // scheme
63 url_parse::Component(), // username
64 url_parse::Component(), // password
65 url_parse::Component(4, 14), // host
66 url_parse::Component(19, 3), // port
67 url_parse::Component(), // path
68 url_parse::Component(23, 3), // query
69 url_parse::Component(27, 0), // ref
70 },
[email protected]b1c33f82009-01-23 01:51:2371 { "[email protected]", "http",
initial.commit09911bf2008-07-26 23:55:2972 url_parse::Component(), // scheme
73 url_parse::Component(0, 4), // username
74 url_parse::Component(), // password
75 url_parse::Component(5, 14), // host
76 url_parse::Component(), // port
77 url_parse::Component(), // path
78 url_parse::Component(), // query
79 url_parse::Component(), // ref
80 },
[email protected]b1c33f82009-01-23 01:51:2381 { "ftp:/user:P:[email protected]...::23///pub?foo#bar", "ftp",
initial.commit09911bf2008-07-26 23:55:2982 url_parse::Component(0, 3), // scheme
83 url_parse::Component(5, 4), // username
84 url_parse::Component(10, 7), // password
85 url_parse::Component(18, 20), // host
86 url_parse::Component(39, 2), // port
87 url_parse::Component(41, 6), // path
88 url_parse::Component(48, 3), // query
89 url_parse::Component(52, 3), // ref
90 },
[email protected]818071ce2009-05-18 01:25:2591 { "[2001:db8::1]/path", "http",
92 url_parse::Component(), // scheme
93 url_parse::Component(), // username
94 url_parse::Component(), // password
95 url_parse::Component(0, 13), // host
96 url_parse::Component(), // port
97 url_parse::Component(13, 5), // path
98 url_parse::Component(), // query
99 url_parse::Component(), // ref
100 },
101 { "[::1]", "http",
102 url_parse::Component(), // scheme
103 url_parse::Component(), // username
104 url_parse::Component(), // password
105 url_parse::Component(0, 5), // host
106 url_parse::Component(), // port
107 url_parse::Component(), // path
108 url_parse::Component(), // query
109 url_parse::Component(), // ref
110 },
[email protected]c1991302009-06-04 03:57:39111 // Incomplete IPv6 addresses (will not canonicalize).
112 { "[2001:4860:", "http",
113 url_parse::Component(), // scheme
114 url_parse::Component(), // username
115 url_parse::Component(), // password
116 url_parse::Component(0, 11), // host
117 url_parse::Component(), // port
118 url_parse::Component(), // path
119 url_parse::Component(), // query
120 url_parse::Component(), // ref
121 },
122 { "[2001:4860:/foo", "http",
123 url_parse::Component(), // scheme
124 url_parse::Component(), // username
125 url_parse::Component(), // password
126 url_parse::Component(0, 11), // host
127 url_parse::Component(), // port
128 url_parse::Component(11, 4), // path
129 url_parse::Component(), // query
130 url_parse::Component(), // ref
131 },
132 { "http://:b005::68]", "http",
133 url_parse::Component(0, 4), // scheme
134 url_parse::Component(), // username
135 url_parse::Component(), // password
136 url_parse::Component(7, 10), // host
137 url_parse::Component(), // port
138 url_parse::Component(), // path
139 url_parse::Component(), // query
140 url_parse::Component(), // ref
141 },
142 // Can't do anything useful with this.
143 { ":b005::68]", "",
144 url_parse::Component(0, 0), // scheme
145 url_parse::Component(), // username
146 url_parse::Component(), // password
147 url_parse::Component(), // host
148 url_parse::Component(), // port
149 url_parse::Component(), // path
150 url_parse::Component(), // query
151 url_parse::Component(), // ref
152 },
initial.commit09911bf2008-07-26 23:55:29153};
154
155TEST(URLFixerUpperTest, SegmentURL) {
[email protected]b1c33f82009-01-23 01:51:23156 std::string result;
initial.commit09911bf2008-07-26 23:55:29157 url_parse::Parsed parts;
158
[email protected]b1c33f82009-01-23 01:51:23159 for (size_t i = 0; i < arraysize(segment_cases); ++i) {
initial.commit09911bf2008-07-26 23:55:29160 segment_case value = segment_cases[i];
161 result = URLFixerUpper::SegmentURL(value.input, &parts);
162 EXPECT_EQ(value.result, result);
163 EXPECT_EQ(value.scheme, parts.scheme);
164 EXPECT_EQ(value.username, parts.username);
165 EXPECT_EQ(value.password, parts.password);
166 EXPECT_EQ(value.host, parts.host);
167 EXPECT_EQ(value.port, parts.port);
168 EXPECT_EQ(value.path, parts.path);
169 EXPECT_EQ(value.query, parts.query);
170 EXPECT_EQ(value.ref, parts.ref);
171 }
172}
173
174// Creates a file and returns its full name as well as the decomposed
175// version. Example:
176// full_path = "c:\foo\bar.txt"
177// dir = "c:\foo"
178// file_name = "bar.txt"
[email protected]b1c33f82009-01-23 01:51:23179static bool MakeTempFile(const FilePath& dir,
180 const FilePath& file_name,
181 FilePath* full_path) {
182 *full_path = dir.Append(file_name);
[email protected]7ff3f632009-10-13 18:43:35183 return file_util::WriteFile(*full_path, "", 0) == 0;
initial.commit09911bf2008-07-26 23:55:29184}
185
186// Returns true if the given URL is a file: URL that matches the given file
[email protected]b1c33f82009-01-23 01:51:23187static bool IsMatchingFileURL(const std::string& url,
188 const FilePath& full_file_path) {
initial.commit09911bf2008-07-26 23:55:29189 if (url.length() <= 8)
190 return false;
[email protected]b1c33f82009-01-23 01:51:23191 if (std::string("file:///") != url.substr(0, 8))
initial.commit09911bf2008-07-26 23:55:29192 return false; // no file:/// prefix
[email protected]b1c33f82009-01-23 01:51:23193 if (url.find('\\') != std::string::npos)
initial.commit09911bf2008-07-26 23:55:29194 return false; // contains backslashes
195
[email protected]b1c33f82009-01-23 01:51:23196 FilePath derived_path;
[email protected]56741852008-12-17 19:04:50197 net::FileURLToFilePath(GURL(url), &derived_path);
[email protected]b1c33f82009-01-23 01:51:23198
199 FilePath::StringType derived_path_str = derived_path.value();
200 return (derived_path_str.length() == full_file_path.value().length()) &&
201 std::equal(derived_path_str.begin(),
202 derived_path_str.end(),
203 full_file_path.value().begin(),
204 CaseInsensitiveCompare<FilePath::CharType>());
initial.commit09911bf2008-07-26 23:55:29205}
206
207struct fixup_case {
[email protected]b1c33f82009-01-23 01:51:23208 const std::string input;
209 const std::string desired_tld;
210 const std::string output;
initial.commit09911bf2008-07-26 23:55:29211} fixup_cases[] = {
[email protected]b1c33f82009-01-23 01:51:23212 {"www.google.com", "", "http://www.google.com/"},
213 {" www.google.com ", "", "http://www.google.com/"},
214 {" foo.com/asdf bar", "", "http://foo.com/asdf bar"},
215 {"..www.google.com..", "", "http://www.google.com./"},
216 {"http://......", "", "http://....../"},
217 {"http://host.com:ninety-two/", "", "http://host.com/"},
218 {"http://host.com:ninety-two?foo", "", "http://host.com/?foo"},
219 {"google.com:123", "", "http://google.com:123/"},
220 {"about:", "", "about:"},
221 {"about:version", "", "about:version"},
222 {"www:123", "", "http://www:123/"},
223 {" www:123", "", "http://www:123/"},
224 {"www.google.com?foo", "", "http://www.google.com/?foo"},
225 {"www.google.com#foo", "", "http://www.google.com/#foo"},
226 {"www.google.com?", "", "http://www.google.com/?"},
227 {"www.google.com#", "", "http://www.google.com/#"},
228 {"www.google.com:123?foo#bar", "", "http://www.google.com:123/?foo#bar"},
229 {"[email protected]", "", "http://[email protected]/"},
230 {"\xE6\xB0\xB4.com" , "", "http://\xE6\xB0\xB4.com/"},
initial.commit09911bf2008-07-26 23:55:29231 // It would be better if this next case got treated as http, but I don't see
232 // a clean way to guess this isn't the new-and-exciting "user" scheme.
[email protected]b1c33f82009-01-23 01:51:23233 {"user:[email protected]:8080/", "", "user:[email protected]:8080/"},
[email protected]038d52e12009-10-14 16:53:41234 // {"file:///c:/foo/bar%20baz.txt", "", "file:///C:/foo/bar%20baz.txt"},
[email protected]b1c33f82009-01-23 01:51:23235 {"ftp.google.com", "", "ftp://ftp.google.com/"},
236 {" ftp.google.com", "", "ftp://ftp.google.com/"},
237 {"FTP.GooGle.com", "", "ftp://FTP.GooGle.com/"},
238 {"ftpblah.google.com", "", "http://ftpblah.google.com/"},
239 {"ftp", "", "http://ftp/"},
240 {"google.ftp.com", "", "http://google.ftp.com/"},
[email protected]90f933a2009-03-05 03:41:51241 // URLs which end with 0x85 (NEL in ISO-8859).
242 { "http://google.com/search?q=\xd0\x85", "",
243 "http://google.com/search?q=\xd0\x85"
244 },
245 { "http://google.com/search?q=\xec\x97\x85", "",
246 "http://google.com/search?q=\xec\x97\x85"
247 },
248 { "http://google.com/search?q=\xf0\x90\x80\x85", "",
249 "http://google.com/search?q=\xf0\x90\x80\x85"
250 },
251 // URLs which end with 0xA0 (non-break space in ISO-8859).
252 { "http://google.com/search?q=\xd0\xa0", "",
253 "http://google.com/search?q=\xd0\xa0"
254 },
255 { "http://google.com/search?q=\xec\x97\xa0", "",
256 "http://google.com/search?q=\xec\x97\xa0"
257 },
258 { "http://google.com/search?q=\xf0\x90\x80\xa0", "",
259 "http://google.com/search?q=\xf0\x90\x80\xa0"
260 },
[email protected]818071ce2009-05-18 01:25:25261 // URLs containing IPv6 literals.
262 {"[2001:db8::2]", "", "http://[2001:db8::2]/"},
263 {"[::]:80", "", "http://[::]:80/"},
264 {"[::]:80/path", "", "http://[::]:80/path"},
265 // TODO(pmarks): Maybe we should parse bare IPv6 literals someday.
266 {"::1", "", "::1"},
initial.commit09911bf2008-07-26 23:55:29267};
268
269TEST(URLFixerUpperTest, FixupURL) {
[email protected]b1c33f82009-01-23 01:51:23270 std::string output;
initial.commit09911bf2008-07-26 23:55:29271
[email protected]b1c33f82009-01-23 01:51:23272 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
initial.commit09911bf2008-07-26 23:55:29273 fixup_case value = fixup_cases[i];
274 output = URLFixerUpper::FixupURL(value.input, value.desired_tld);
275 EXPECT_EQ(value.output, output);
276 }
277
278 // Check the TLD-appending functionality
279 fixup_case tld_cases[] = {
[email protected]b1c33f82009-01-23 01:51:23280 {"google", "com", "http://www.google.com/"},
281 {"google.", "com", "http://www.google.com/"},
282 {"google..", "com", "http://www.google.com/"},
283 {".google", "com", "http://www.google.com/"},
284 {"www.google", "com", "http://www.google.com/"},
285 {"google.com", "com", "http://google.com/"},
286 {"http://google", "com", "http://www.google.com/"},
287 {"..google..", "com", "http://www.google.com/"},
288 {"http://www.google", "com", "http://www.google.com/"},
289 {"google/foo", "com", "http://www.google.com/foo"},
290 {"google.com/foo", "com", "http://google.com/foo"},
291 {"google/?foo=.com", "com", "http://www.google.com/?foo=.com"},
292 {"www.google/?foo=www.", "com", "http://www.google.com/?foo=www."},
293 {"google.com/?foo=.com", "com", "http://google.com/?foo=.com"},
294 {"http://www.google.com", "com", "http://www.google.com/"},
295 {"google:123", "com", "http://www.google.com:123/"},
296 {"http://google:123", "com", "http://www.google.com:123/"},
initial.commit09911bf2008-07-26 23:55:29297 };
[email protected]b1c33f82009-01-23 01:51:23298 for (size_t i = 0; i < arraysize(tld_cases); ++i) {
initial.commit09911bf2008-07-26 23:55:29299 fixup_case value = tld_cases[i];
300 output = URLFixerUpper::FixupURL(value.input, value.desired_tld);
301 EXPECT_EQ(value.output, output);
302 }
303}
304
305// Test different types of file inputs to URIFixerUpper::FixupURL. This
306// doesn't go into the nice array of fixups above since the file input
307// has to exist.
308TEST(URLFixerUpperTest, FixupFile) {
309 // this "original" filename is the one we tweak to get all the variations
[email protected]b1c33f82009-01-23 01:51:23310 FilePath dir;
311 FilePath original;
initial.commit09911bf2008-07-26 23:55:29312 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir));
[email protected]d3216442009-03-05 21:07:27313 ASSERT_TRUE(MakeTempFile(
314 dir,
315 FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
316 &original));
initial.commit09911bf2008-07-26 23:55:29317
318 // reference path
[email protected]b1c33f82009-01-23 01:51:23319 std::string golden = net::FilePathToFileURL(original).spec();
initial.commit09911bf2008-07-26 23:55:29320
321 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic)
[email protected]b1c33f82009-01-23 01:51:23322#if defined(OS_WIN)
[email protected]d3216442009-03-05 21:07:27323 std::string fixedup = URLFixerUpper::FixupURL(WideToUTF8(original.value()),
324 "");
[email protected]b1c33f82009-01-23 01:51:23325#elif defined(OS_POSIX)
326 std::string fixedup = URLFixerUpper::FixupURL(original.value(), "");
327#endif
initial.commit09911bf2008-07-26 23:55:29328 EXPECT_EQ(golden, fixedup);
329
[email protected]b1c33f82009-01-23 01:51:23330 // TODO(port): Make some equivalent tests for posix.
331#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29332 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon)
[email protected]b1c33f82009-01-23 01:51:23333 std::string cur(WideToUTF8(original.value()));
initial.commit09911bf2008-07-26 23:55:29334 EXPECT_EQ(':', cur[1]);
335 cur[1] = '|';
[email protected]b1c33f82009-01-23 01:51:23336 fixedup = URLFixerUpper::FixupURL(cur, "");
initial.commit09911bf2008-07-26 23:55:29337 EXPECT_EQ(golden, fixedup);
338
339 fixup_case file_cases[] = {
340 // File URLs go through GURL, which tries to escape intelligently.
[email protected]d3216442009-03-05 21:07:27341 {"c:\\This%20is a non-existent file.txt", "",
342 "file:///C:/This%2520is%20a%20non-existent%20file.txt"},
initial.commit09911bf2008-07-26 23:55:29343
344 // \\foo\bar.txt -> file://foo/bar.txt
345 // UNC paths, this file won't exist, but since there are no escapes, it
346 // should be returned just converted to a file: URL.
[email protected]d3216442009-03-05 21:07:27347 {"\\\\SomeNonexistentHost\\foo\\bar.txt", "",
348 "file://somenonexistenthost/foo/bar.txt"},
349 {"//SomeNonexistentHost\\foo/bar.txt", "",
350 "file://somenonexistenthost/foo/bar.txt"},
[email protected]b1c33f82009-01-23 01:51:23351 {"file:///C:/foo/bar", "", "file:///C:/foo/bar"},
initial.commit09911bf2008-07-26 23:55:29352
353 // These are fixups we don't do, but could consider:
354 //
[email protected]b1c33f82009-01-23 01:51:23355 // {"file://C:/foo/bar", "", "file:///C:/foo/bar"},
356 // {"file:c:", "", "file:///c:/"},
357 // {"file:c:WINDOWS", "", "file:///c:/WINDOWS"},
358 // {"file:c|Program Files", "", "file:///c:/Program Files"},
359 // {"file:///foo:/bar", "", "file://foo/bar"},
360 // {"file:/file", "", "file://file/"},
361 // {"file:////////c:\\foo", "", "file:///c:/foo"},
362 // {"file://server/folder/file", "", "file://server/folder/file"},
363 // {"file:/\\/server\\folder/file", "", "file://server/folder/file"},
initial.commit09911bf2008-07-26 23:55:29364 };
[email protected]ba1321d12009-04-21 22:42:29365#elif defined(OS_POSIX)
366 fixup_case file_cases[] = {
367 // File URLs go through GURL, which tries to escape intelligently.
368 {"/This%20is a non-existent file.txt", "",
369 "file:///This%2520is%20a%20non-existent%20file.txt"},
370 // A plain "/" refers to the root.
371 {"/", "",
372 "file:///"},
373 };
374#endif
[email protected]b1c33f82009-01-23 01:51:23375 for (size_t i = 0; i < arraysize(file_cases); i++) {
initial.commit09911bf2008-07-26 23:55:29376 fixedup = URLFixerUpper::FixupURL(file_cases[i].input,
377 file_cases[i].desired_tld);
378 EXPECT_EQ(file_cases[i].output, fixedup);
379 }
380
[email protected]b1c33f82009-01-23 01:51:23381 EXPECT_TRUE(file_util::Delete(original, false));
initial.commit09911bf2008-07-26 23:55:29382}
383
384TEST(URLFixerUpperTest, FixupRelativeFile) {
[email protected]b1c33f82009-01-23 01:51:23385 FilePath full_path, dir;
386 FilePath file_part(FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
initial.commit09911bf2008-07-26 23:55:29387 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir));
388 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path));
389
390 // make sure we pass through good URLs
[email protected]b1c33f82009-01-23 01:51:23391 std::string fixedup;
392 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
initial.commit09911bf2008-07-26 23:55:29393 fixup_case value = fixup_cases[i];
[email protected]b1c33f82009-01-23 01:51:23394#if defined(OS_WIN)
395 FilePath input(UTF8ToWide(value.input));
396#elif defined(OS_POSIX)
397 FilePath input(value.input);
398#endif
399 fixedup = URLFixerUpper::FixupRelativeFile(dir, input);
initial.commit09911bf2008-07-26 23:55:29400 EXPECT_EQ(value.output, fixedup);
401 }
402
403 // make sure the existing file got fixed-up to a file URL, and that there
404 // are no backslashes
405 fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part);
[email protected]b1c33f82009-01-23 01:51:23406 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path));
407 EXPECT_TRUE(file_util::Delete(full_path, false));
initial.commit09911bf2008-07-26 23:55:29408
409 // create a filename we know doesn't exist and make sure it doesn't get
410 // fixed up to a file URL
[email protected]d3216442009-03-05 21:07:27411 FilePath nonexistent_file(
412 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
initial.commit09911bf2008-07-26 23:55:29413 fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file);
[email protected]b1c33f82009-01-23 01:51:23414 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
initial.commit09911bf2008-07-26 23:55:29415 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
416
417 // make a subdir to make sure relative paths with directories work, also
[email protected]d3216442009-03-05 21:07:27418 // test spaces:
419 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt"
[email protected]b1c33f82009-01-23 01:51:23420 FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
421 FilePath sub_file(FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
422 FilePath new_dir = dir.Append(sub_dir);
423 file_util::CreateDirectory(new_dir);
initial.commit09911bf2008-07-26 23:55:29424 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
425
426 // test file in the subdir
[email protected]b1c33f82009-01-23 01:51:23427 FilePath relative_file = sub_dir.Append(sub_file);
initial.commit09911bf2008-07-26 23:55:29428 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file);
[email protected]b1c33f82009-01-23 01:51:23429 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path));
initial.commit09911bf2008-07-26 23:55:29430
[email protected]b1c33f82009-01-23 01:51:23431 // test file in the subdir with different slashes and escaping.
432 FilePath::StringType relative_file_str = sub_dir.value() +
433 FILE_PATH_LITERAL("/") + sub_file.value();
434 ReplaceSubstringsAfterOffset(&relative_file_str, 0,
435 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
436 fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str));
437 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path));
initial.commit09911bf2008-07-26 23:55:29438
439 // test relative directories and duplicate slashes
440 // (should resolve to the same file as above)
[email protected]b1c33f82009-01-23 01:51:23441 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
442 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
443 fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str));
444 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path));
initial.commit09911bf2008-07-26 23:55:29445
446 // done with the subdir
[email protected]b1c33f82009-01-23 01:51:23447 EXPECT_TRUE(file_util::Delete(full_path, false));
448 EXPECT_TRUE(file_util::Delete(new_dir, true));
initial.commit09911bf2008-07-26 23:55:29449}