| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdlib.h> | 6 #include <stdlib.h> |
| 7 | 7 |
| 8 #include "base/base_paths.h" | 8 #include "base/base_paths.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 | 368 |
| 369 // Test different types of file inputs to URIFixerUpper::FixupURL. This | 369 // Test different types of file inputs to URIFixerUpper::FixupURL. This |
| 370 // doesn't go into the nice array of fixups above since the file input | 370 // doesn't go into the nice array of fixups above since the file input |
| 371 // has to exist. | 371 // has to exist. |
| 372 TEST(URLFixerTest, FixupFile) { | 372 TEST(URLFixerTest, FixupFile) { |
| 373 // this "original" filename is the one we tweak to get all the variations | 373 // this "original" filename is the one we tweak to get all the variations |
| 374 base::ScopedTempDir temp_dir_; | 374 base::ScopedTempDir temp_dir_; |
| 375 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 375 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 376 base::FilePath original; | 376 base::FilePath original; |
| 377 ASSERT_TRUE(MakeTempFile( | 377 ASSERT_TRUE(MakeTempFile( |
| 378 temp_dir_.path(), | 378 temp_dir_.GetPath(), |
| 379 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), | 379 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), |
| 380 &original)); | 380 &original)); |
| 381 | 381 |
| 382 // reference path | 382 // reference path |
| 383 GURL golden(net::FilePathToFileURL(original)); | 383 GURL golden(net::FilePathToFileURL(original)); |
| 384 | 384 |
| 385 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) | 385 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) |
| 386 GURL fixedup(url_formatter::FixupURL(original.AsUTF8Unsafe(), std::string())); | 386 GURL fixedup(url_formatter::FixupURL(original.AsUTF8Unsafe(), std::string())); |
| 387 EXPECT_EQ(golden, fixedup); | 387 EXPECT_EQ(golden, fixedup); |
| 388 | 388 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 | 453 |
| 454 EXPECT_TRUE(base::DeleteFile(original, false)); | 454 EXPECT_TRUE(base::DeleteFile(original, false)); |
| 455 } | 455 } |
| 456 | 456 |
| 457 TEST(URLFixerTest, FixupRelativeFile) { | 457 TEST(URLFixerTest, FixupRelativeFile) { |
| 458 base::FilePath full_path; | 458 base::FilePath full_path; |
| 459 base::FilePath file_part( | 459 base::FilePath file_part( |
| 460 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); | 460 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); |
| 461 base::ScopedTempDir temp_dir_; | 461 base::ScopedTempDir temp_dir_; |
| 462 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 462 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 463 ASSERT_TRUE(MakeTempFile(temp_dir_.path(), file_part, &full_path)); | 463 ASSERT_TRUE(MakeTempFile(temp_dir_.GetPath(), file_part, &full_path)); |
| 464 full_path = base::MakeAbsoluteFilePath(full_path); | 464 full_path = base::MakeAbsoluteFilePath(full_path); |
| 465 ASSERT_FALSE(full_path.empty()); | 465 ASSERT_FALSE(full_path.empty()); |
| 466 | 466 |
| 467 // make sure we pass through good URLs | 467 // make sure we pass through good URLs |
| 468 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { | 468 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { |
| 469 FixupCase value = fixup_cases[i]; | 469 FixupCase value = fixup_cases[i]; |
| 470 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input); | 470 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input); |
| 471 EXPECT_EQ(value.output, | 471 EXPECT_EQ(value.output, |
| 472 url_formatter::FixupRelativeFile(temp_dir_.path(), | 472 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), input) |
| 473 input).possibly_invalid_spec()); | 473 .possibly_invalid_spec()); |
| 474 } | 474 } |
| 475 | 475 |
| 476 // make sure the existing file got fixed-up to a file URL, and that there | 476 // make sure the existing file got fixed-up to a file URL, and that there |
| 477 // are no backslashes | 477 // are no backslashes |
| 478 EXPECT_TRUE(IsMatchingFileURL( | 478 EXPECT_TRUE(IsMatchingFileURL( |
| 479 url_formatter::FixupRelativeFile(temp_dir_.path(), | 479 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), file_part) |
| 480 file_part).possibly_invalid_spec(), full_path)); | 480 .possibly_invalid_spec(), |
| 481 full_path)); |
| 481 EXPECT_TRUE(base::DeleteFile(full_path, false)); | 482 EXPECT_TRUE(base::DeleteFile(full_path, false)); |
| 482 | 483 |
| 483 // create a filename we know doesn't exist and make sure it doesn't get | 484 // create a filename we know doesn't exist and make sure it doesn't get |
| 484 // fixed up to a file URL | 485 // fixed up to a file URL |
| 485 base::FilePath nonexistent_file( | 486 base::FilePath nonexistent_file( |
| 486 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); | 487 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); |
| 487 std::string fixedup(url_formatter::FixupRelativeFile( | 488 std::string fixedup( |
| 488 temp_dir_.path(), nonexistent_file).possibly_invalid_spec()); | 489 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), nonexistent_file) |
| 490 .possibly_invalid_spec()); |
| 489 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); | 491 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); |
| 490 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); | 492 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); |
| 491 | 493 |
| 492 // make a subdir to make sure relative paths with directories work, also | 494 // make a subdir to make sure relative paths with directories work, also |
| 493 // test spaces: | 495 // test spaces: |
| 494 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" | 496 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" |
| 495 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); | 497 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); |
| 496 base::FilePath sub_file( | 498 base::FilePath sub_file( |
| 497 FILE_PATH_LITERAL("url fixer-upper existing file.txt")); | 499 FILE_PATH_LITERAL("url fixer-upper existing file.txt")); |
| 498 base::FilePath new_dir = temp_dir_.path().Append(sub_dir); | 500 base::FilePath new_dir = temp_dir_.GetPath().Append(sub_dir); |
| 499 base::CreateDirectory(new_dir); | 501 base::CreateDirectory(new_dir); |
| 500 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); | 502 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); |
| 501 full_path = base::MakeAbsoluteFilePath(full_path); | 503 full_path = base::MakeAbsoluteFilePath(full_path); |
| 502 ASSERT_FALSE(full_path.empty()); | 504 ASSERT_FALSE(full_path.empty()); |
| 503 | 505 |
| 504 // test file in the subdir | 506 // test file in the subdir |
| 505 base::FilePath relative_file = sub_dir.Append(sub_file); | 507 base::FilePath relative_file = sub_dir.Append(sub_file); |
| 506 EXPECT_TRUE(IsMatchingFileURL( | 508 EXPECT_TRUE(IsMatchingFileURL( |
| 507 url_formatter::FixupRelativeFile(temp_dir_.path(), | 509 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), relative_file) |
| 508 relative_file).possibly_invalid_spec(), full_path)); | 510 .possibly_invalid_spec(), |
| 511 full_path)); |
| 509 | 512 |
| 510 // test file in the subdir with different slashes and escaping. | 513 // test file in the subdir with different slashes and escaping. |
| 511 base::FilePath::StringType relative_file_str = sub_dir.value() + | 514 base::FilePath::StringType relative_file_str = sub_dir.value() + |
| 512 FILE_PATH_LITERAL("/") + sub_file.value(); | 515 FILE_PATH_LITERAL("/") + sub_file.value(); |
| 513 base::ReplaceSubstringsAfterOffset(&relative_file_str, 0, | 516 base::ReplaceSubstringsAfterOffset(&relative_file_str, 0, |
| 514 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); | 517 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); |
| 515 EXPECT_TRUE(IsMatchingFileURL( | 518 EXPECT_TRUE(IsMatchingFileURL( |
| 516 url_formatter::FixupRelativeFile(temp_dir_.path(), | 519 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), |
| 517 base::FilePath(relative_file_str)).possibly_invalid_spec(), | 520 base::FilePath(relative_file_str)) |
| 518 full_path)); | 521 .possibly_invalid_spec(), |
| 522 full_path)); |
| 519 | 523 |
| 520 // test relative directories and duplicate slashes | 524 // test relative directories and duplicate slashes |
| 521 // (should resolve to the same file as above) | 525 // (should resolve to the same file as above) |
| 522 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + | 526 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + |
| 523 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); | 527 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); |
| 524 EXPECT_TRUE(IsMatchingFileURL( | 528 EXPECT_TRUE(IsMatchingFileURL( |
| 525 url_formatter::FixupRelativeFile(temp_dir_.path(), | 529 url_formatter::FixupRelativeFile(temp_dir_.GetPath(), |
| 526 base::FilePath(relative_file_str)).possibly_invalid_spec(), | 530 base::FilePath(relative_file_str)) |
| 527 full_path)); | 531 .possibly_invalid_spec(), |
| 532 full_path)); |
| 528 | 533 |
| 529 // done with the subdir | 534 // done with the subdir |
| 530 EXPECT_TRUE(base::DeleteFile(full_path, false)); | 535 EXPECT_TRUE(base::DeleteFile(full_path, false)); |
| 531 EXPECT_TRUE(base::DeleteFile(new_dir, true)); | 536 EXPECT_TRUE(base::DeleteFile(new_dir, true)); |
| 532 | 537 |
| 533 // Test that an obvious HTTP URL isn't accidentally treated as an absolute | 538 // Test that an obvious HTTP URL isn't accidentally treated as an absolute |
| 534 // file path (on account of system-specific craziness). | 539 // file path (on account of system-specific craziness). |
| 535 base::FilePath empty_path; | 540 base::FilePath empty_path; |
| 536 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../")); | 541 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../")); |
| 537 EXPECT_TRUE(url_formatter::FixupRelativeFile(empty_path, http_url_path) | 542 EXPECT_TRUE(url_formatter::FixupRelativeFile(empty_path, http_url_path) |
| 538 .SchemeIs("http")); | 543 .SchemeIs("http")); |
| 539 } | 544 } |
| OLD | NEW |