OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/scoped_temp_dir.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "components/url_fixer/url_fixer.h" | 15 #include "components/url_fixer/url_fixer.h" |
15 #include "net/base/filename_util.h" | 16 #include "net/base/filename_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 #include "url/url_parse.h" | 19 #include "url/url_parse.h" |
19 | 20 |
20 namespace url { | 21 namespace url { |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 EXPECT_EQ(value.output, | 346 EXPECT_EQ(value.output, |
346 url_fixer::FixupURL(value.input, "com").possibly_invalid_spec()); | 347 url_fixer::FixupURL(value.input, "com").possibly_invalid_spec()); |
347 } | 348 } |
348 } | 349 } |
349 | 350 |
350 // Test different types of file inputs to URIFixerUpper::FixupURL. This | 351 // Test different types of file inputs to URIFixerUpper::FixupURL. This |
351 // doesn't go into the nice array of fixups above since the file input | 352 // doesn't go into the nice array of fixups above since the file input |
352 // has to exist. | 353 // has to exist. |
353 TEST(URLFixerTest, FixupFile) { | 354 TEST(URLFixerTest, FixupFile) { |
354 // this "original" filename is the one we tweak to get all the variations | 355 // this "original" filename is the one we tweak to get all the variations |
355 base::FilePath dir; | 356 base::ScopedTempDir temp_dir_; |
| 357 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
356 base::FilePath original; | 358 base::FilePath original; |
357 ASSERT_TRUE(PathService::Get(base::DIR_MODULE, &dir)); | |
358 ASSERT_TRUE(MakeTempFile( | 359 ASSERT_TRUE(MakeTempFile( |
359 dir, | 360 temp_dir_.path(), |
360 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), | 361 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), |
361 &original)); | 362 &original)); |
362 | 363 |
363 // reference path | 364 // reference path |
364 GURL golden(net::FilePathToFileURL(original)); | 365 GURL golden(net::FilePathToFileURL(original)); |
365 | 366 |
366 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) | 367 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) |
367 GURL fixedup(url_fixer::FixupURL(original.AsUTF8Unsafe(), std::string())); | 368 GURL fixedup(url_fixer::FixupURL(original.AsUTF8Unsafe(), std::string())); |
368 EXPECT_EQ(golden, fixedup); | 369 EXPECT_EQ(golden, fixedup); |
369 | 370 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 | 429 |
429 for (size_t i = 0; i < arraysize(cases); i++) { | 430 for (size_t i = 0; i < arraysize(cases); i++) { |
430 EXPECT_EQ(cases[i].output, | 431 EXPECT_EQ(cases[i].output, |
431 url_fixer::FixupURL(cases[i].input, "").possibly_invalid_spec()); | 432 url_fixer::FixupURL(cases[i].input, "").possibly_invalid_spec()); |
432 } | 433 } |
433 | 434 |
434 EXPECT_TRUE(base::DeleteFile(original, false)); | 435 EXPECT_TRUE(base::DeleteFile(original, false)); |
435 } | 436 } |
436 | 437 |
437 TEST(URLFixerTest, FixupRelativeFile) { | 438 TEST(URLFixerTest, FixupRelativeFile) { |
438 base::FilePath full_path, dir; | 439 base::FilePath full_path; |
439 base::FilePath file_part( | 440 base::FilePath file_part( |
440 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); | 441 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); |
441 ASSERT_TRUE(PathService::Get(base::DIR_MODULE, &dir)); | 442 base::ScopedTempDir temp_dir_; |
442 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path)); | 443 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 444 ASSERT_TRUE(MakeTempFile(temp_dir_.path(), file_part, &full_path)); |
443 full_path = base::MakeAbsoluteFilePath(full_path); | 445 full_path = base::MakeAbsoluteFilePath(full_path); |
444 ASSERT_FALSE(full_path.empty()); | 446 ASSERT_FALSE(full_path.empty()); |
445 | 447 |
446 // make sure we pass through good URLs | 448 // make sure we pass through good URLs |
447 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { | 449 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { |
448 FixupCase value = fixup_cases[i]; | 450 FixupCase value = fixup_cases[i]; |
449 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input); | 451 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input); |
450 EXPECT_EQ(value.output, | 452 EXPECT_EQ(value.output, |
451 url_fixer::FixupRelativeFile(dir, input).possibly_invalid_spec()); | 453 url_fixer::FixupRelativeFile(temp_dir_.path(), |
| 454 input).possibly_invalid_spec()); |
452 } | 455 } |
453 | 456 |
454 // make sure the existing file got fixed-up to a file URL, and that there | 457 // make sure the existing file got fixed-up to a file URL, and that there |
455 // are no backslashes | 458 // are no backslashes |
456 EXPECT_TRUE(IsMatchingFileURL( | 459 EXPECT_TRUE(IsMatchingFileURL( |
457 url_fixer::FixupRelativeFile(dir, file_part).possibly_invalid_spec(), | 460 url_fixer::FixupRelativeFile(temp_dir_.path(), |
458 full_path)); | 461 file_part).possibly_invalid_spec(), full_path)); |
459 EXPECT_TRUE(base::DeleteFile(full_path, false)); | 462 EXPECT_TRUE(base::DeleteFile(full_path, false)); |
460 | 463 |
461 // create a filename we know doesn't exist and make sure it doesn't get | 464 // create a filename we know doesn't exist and make sure it doesn't get |
462 // fixed up to a file URL | 465 // fixed up to a file URL |
463 base::FilePath nonexistent_file( | 466 base::FilePath nonexistent_file( |
464 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); | 467 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); |
465 std::string fixedup(url_fixer::FixupRelativeFile(dir, nonexistent_file) | 468 std::string fixedup(url_fixer::FixupRelativeFile( |
466 .possibly_invalid_spec()); | 469 temp_dir_.path(), nonexistent_file).possibly_invalid_spec()); |
467 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); | 470 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); |
468 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); | 471 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); |
469 | 472 |
470 // make a subdir to make sure relative paths with directories work, also | 473 // make a subdir to make sure relative paths with directories work, also |
471 // test spaces: | 474 // test spaces: |
472 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" | 475 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" |
473 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); | 476 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); |
474 base::FilePath sub_file( | 477 base::FilePath sub_file( |
475 FILE_PATH_LITERAL("url fixer-upper existing file.txt")); | 478 FILE_PATH_LITERAL("url fixer-upper existing file.txt")); |
476 base::FilePath new_dir = dir.Append(sub_dir); | 479 base::FilePath new_dir = temp_dir_.path().Append(sub_dir); |
477 base::CreateDirectory(new_dir); | 480 base::CreateDirectory(new_dir); |
478 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); | 481 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); |
479 full_path = base::MakeAbsoluteFilePath(full_path); | 482 full_path = base::MakeAbsoluteFilePath(full_path); |
480 ASSERT_FALSE(full_path.empty()); | 483 ASSERT_FALSE(full_path.empty()); |
481 | 484 |
482 // test file in the subdir | 485 // test file in the subdir |
483 base::FilePath relative_file = sub_dir.Append(sub_file); | 486 base::FilePath relative_file = sub_dir.Append(sub_file); |
484 EXPECT_TRUE(IsMatchingFileURL( | 487 EXPECT_TRUE(IsMatchingFileURL( |
485 url_fixer::FixupRelativeFile(dir, relative_file).possibly_invalid_spec(), | 488 url_fixer::FixupRelativeFile(temp_dir_.path(), |
486 full_path)); | 489 relative_file).possibly_invalid_spec(), full_path)); |
487 | 490 |
488 // test file in the subdir with different slashes and escaping. | 491 // test file in the subdir with different slashes and escaping. |
489 base::FilePath::StringType relative_file_str = sub_dir.value() + | 492 base::FilePath::StringType relative_file_str = sub_dir.value() + |
490 FILE_PATH_LITERAL("/") + sub_file.value(); | 493 FILE_PATH_LITERAL("/") + sub_file.value(); |
491 ReplaceSubstringsAfterOffset(&relative_file_str, 0, | 494 ReplaceSubstringsAfterOffset(&relative_file_str, 0, |
492 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); | 495 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); |
493 EXPECT_TRUE(IsMatchingFileURL( | 496 EXPECT_TRUE(IsMatchingFileURL( |
494 url_fixer::FixupRelativeFile(dir, base::FilePath(relative_file_str)) | 497 url_fixer::FixupRelativeFile(temp_dir_.path(), |
495 .possibly_invalid_spec(), | 498 base::FilePath(relative_file_str)).possibly_invalid_spec(), |
496 full_path)); | 499 full_path)); |
497 | 500 |
498 // test relative directories and duplicate slashes | 501 // test relative directories and duplicate slashes |
499 // (should resolve to the same file as above) | 502 // (should resolve to the same file as above) |
500 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + | 503 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + |
501 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); | 504 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); |
502 EXPECT_TRUE(IsMatchingFileURL( | 505 EXPECT_TRUE(IsMatchingFileURL( |
503 url_fixer::FixupRelativeFile(dir, base::FilePath(relative_file_str)) | 506 url_fixer::FixupRelativeFile(temp_dir_.path(), |
504 .possibly_invalid_spec(), | 507 base::FilePath(relative_file_str)).possibly_invalid_spec(), |
505 full_path)); | 508 full_path)); |
506 | 509 |
507 // done with the subdir | 510 // done with the subdir |
508 EXPECT_TRUE(base::DeleteFile(full_path, false)); | 511 EXPECT_TRUE(base::DeleteFile(full_path, false)); |
509 EXPECT_TRUE(base::DeleteFile(new_dir, true)); | 512 EXPECT_TRUE(base::DeleteFile(new_dir, true)); |
510 | 513 |
511 // Test that an obvious HTTP URL isn't accidentally treated as an absolute | 514 // Test that an obvious HTTP URL isn't accidentally treated as an absolute |
512 // file path (on account of system-specific craziness). | 515 // file path (on account of system-specific craziness). |
513 base::FilePath empty_path; | 516 base::FilePath empty_path; |
514 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../")); | 517 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../")); |
515 EXPECT_TRUE( | 518 EXPECT_TRUE( |
516 url_fixer::FixupRelativeFile(empty_path, http_url_path).SchemeIs("http")); | 519 url_fixer::FixupRelativeFile(empty_path, http_url_path).SchemeIs("http")); |
517 } | 520 } |
OLD | NEW |