OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 } | 236 } |
237 | 237 |
238 // Test different types of file inputs to URIFixerUpper::FixupURL. This | 238 // Test different types of file inputs to URIFixerUpper::FixupURL. This |
239 // doesn't go into the nice array of fixups above since the file input | 239 // doesn't go into the nice array of fixups above since the file input |
240 // has to exist. | 240 // has to exist. |
241 TEST(URLFixerUpperTest, FixupFile) { | 241 TEST(URLFixerUpperTest, FixupFile) { |
242 // this "original" filename is the one we tweak to get all the variations | 242 // this "original" filename is the one we tweak to get all the variations |
243 FilePath dir; | 243 FilePath dir; |
244 FilePath original; | 244 FilePath original; |
245 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); | 245 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); |
246 ASSERT_TRUE(MakeTempFile(dir, | 246 ASSERT_TRUE(MakeTempFile( |
247 FilePath(FILE_PATH_LITERAL("url fixer upper existing
file.txt")), | 247 dir, |
248 &original)); | 248 FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), |
| 249 &original)); |
249 | 250 |
250 // reference path | 251 // reference path |
251 std::string golden = net::FilePathToFileURL(original).spec(); | 252 std::string golden = net::FilePathToFileURL(original).spec(); |
252 | 253 |
253 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) | 254 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) |
254 #if defined(OS_WIN) | 255 #if defined(OS_WIN) |
255 std::string fixedup = URLFixerUpper::FixupURL(WideToUTF8(original.value()), ""
); | 256 std::string fixedup = URLFixerUpper::FixupURL(WideToUTF8(original.value()), |
| 257 ""); |
256 #elif defined(OS_POSIX) | 258 #elif defined(OS_POSIX) |
257 std::string fixedup = URLFixerUpper::FixupURL(original.value(), ""); | 259 std::string fixedup = URLFixerUpper::FixupURL(original.value(), ""); |
258 #endif | 260 #endif |
259 EXPECT_EQ(golden, fixedup); | 261 EXPECT_EQ(golden, fixedup); |
260 | 262 |
261 // TODO(port): Make some equivalent tests for posix. | 263 // TODO(port): Make some equivalent tests for posix. |
262 #if defined(OS_WIN) | 264 #if defined(OS_WIN) |
263 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon) | 265 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon) |
264 std::string cur(WideToUTF8(original.value())); | 266 std::string cur(WideToUTF8(original.value())); |
265 EXPECT_EQ(':', cur[1]); | 267 EXPECT_EQ(':', cur[1]); |
266 cur[1] = '|'; | 268 cur[1] = '|'; |
267 fixedup = URLFixerUpper::FixupURL(cur, ""); | 269 fixedup = URLFixerUpper::FixupURL(cur, ""); |
268 EXPECT_EQ(golden, fixedup); | 270 EXPECT_EQ(golden, fixedup); |
269 | 271 |
270 fixup_case file_cases[] = { | 272 fixup_case file_cases[] = { |
271 // File URLs go through GURL, which tries to escape intelligently. | 273 // File URLs go through GURL, which tries to escape intelligently. |
272 {"c:\\This%20is a non-existent file.txt", "", "file:///C:/This%2520is%20a%20
non-existent%20file.txt"}, | 274 {"c:\\This%20is a non-existent file.txt", "", |
| 275 "file:///C:/This%2520is%20a%20non-existent%20file.txt"}, |
273 | 276 |
274 // \\foo\bar.txt -> file://foo/bar.txt | 277 // \\foo\bar.txt -> file://foo/bar.txt |
275 // UNC paths, this file won't exist, but since there are no escapes, it | 278 // UNC paths, this file won't exist, but since there are no escapes, it |
276 // should be returned just converted to a file: URL. | 279 // should be returned just converted to a file: URL. |
277 {"\\\\SomeNonexistentHost\\foo\\bar.txt", "", "file://somenonexistenthost/fo
o/bar.txt"}, | 280 {"\\\\SomeNonexistentHost\\foo\\bar.txt", "", |
278 {"//SomeNonexistentHost\\foo/bar.txt", "", "file://somenonexistenthost/foo/b
ar.txt"}, | 281 "file://somenonexistenthost/foo/bar.txt"}, |
| 282 {"//SomeNonexistentHost\\foo/bar.txt", "", |
| 283 "file://somenonexistenthost/foo/bar.txt"}, |
279 {"file:///C:/foo/bar", "", "file:///C:/foo/bar"}, | 284 {"file:///C:/foo/bar", "", "file:///C:/foo/bar"}, |
280 | 285 |
281 // These are fixups we don't do, but could consider: | 286 // These are fixups we don't do, but could consider: |
282 // | 287 // |
283 // {"file://C:/foo/bar", "", "file:///C:/foo/bar"}, | 288 // {"file://C:/foo/bar", "", "file:///C:/foo/bar"}, |
284 // {"file:c:", "", "file:///c:/"}, | 289 // {"file:c:", "", "file:///c:/"}, |
285 // {"file:c:WINDOWS", "", "file:///c:/WINDOWS"}, | 290 // {"file:c:WINDOWS", "", "file:///c:/WINDOWS"}, |
286 // {"file:c|Program Files", "", "file:///c:/Program Files"}, | 291 // {"file:c|Program Files", "", "file:///c:/Program Files"}, |
287 // {"file:///foo:/bar", "", "file://foo/bar"}, | 292 // {"file:///foo:/bar", "", "file://foo/bar"}, |
288 // {"file:/file", "", "file://file/"}, | 293 // {"file:/file", "", "file://file/"}, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 } | 325 } |
321 | 326 |
322 // make sure the existing file got fixed-up to a file URL, and that there | 327 // make sure the existing file got fixed-up to a file URL, and that there |
323 // are no backslashes | 328 // are no backslashes |
324 fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part); | 329 fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part); |
325 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); | 330 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
326 EXPECT_TRUE(file_util::Delete(full_path, false)); | 331 EXPECT_TRUE(file_util::Delete(full_path, false)); |
327 | 332 |
328 // create a filename we know doesn't exist and make sure it doesn't get | 333 // create a filename we know doesn't exist and make sure it doesn't get |
329 // fixed up to a file URL | 334 // fixed up to a file URL |
330 FilePath nonexistent_file(FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.
txt")); | 335 FilePath nonexistent_file( |
| 336 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); |
331 fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file); | 337 fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file); |
332 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); | 338 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); |
333 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); | 339 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); |
334 | 340 |
335 // make a subdir to make sure relative paths with directories work, also | 341 // make a subdir to make sure relative paths with directories work, also |
336 // test spaces: "app_dir\url fixer-upper dir\url fixer-upper existing file.txt
" | 342 // test spaces: |
| 343 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" |
337 FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); | 344 FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); |
338 FilePath sub_file(FILE_PATH_LITERAL("url fixer-upper existing file.txt")); | 345 FilePath sub_file(FILE_PATH_LITERAL("url fixer-upper existing file.txt")); |
339 FilePath new_dir = dir.Append(sub_dir); | 346 FilePath new_dir = dir.Append(sub_dir); |
340 file_util::CreateDirectory(new_dir); | 347 file_util::CreateDirectory(new_dir); |
341 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); | 348 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); |
342 | 349 |
343 // test file in the subdir | 350 // test file in the subdir |
344 FilePath relative_file = sub_dir.Append(sub_file); | 351 FilePath relative_file = sub_dir.Append(sub_file); |
345 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); | 352 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); |
346 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); | 353 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
(...skipping 10 matching lines...) Expand all Loading... |
357 // (should resolve to the same file as above) | 364 // (should resolve to the same file as above) |
358 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + | 365 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + |
359 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); | 366 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); |
360 fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str)); | 367 fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str)); |
361 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); | 368 EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
362 | 369 |
363 // done with the subdir | 370 // done with the subdir |
364 EXPECT_TRUE(file_util::Delete(full_path, false)); | 371 EXPECT_TRUE(file_util::Delete(full_path, false)); |
365 EXPECT_TRUE(file_util::Delete(new_dir, true)); | 372 EXPECT_TRUE(file_util::Delete(new_dir, true)); |
366 } | 373 } |
OLD | NEW |