| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #include <shlobj.h> | 10 #include <shlobj.h> |
| (...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 #if defined(OS_POSIX) | 657 #if defined(OS_POSIX) |
| 658 | 658 |
| 659 TEST_F(FileUtilTest, CreateAndReadSymlinks) { | 659 TEST_F(FileUtilTest, CreateAndReadSymlinks) { |
| 660 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); | 660 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); |
| 661 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); | 661 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); |
| 662 CreateTextFile(link_to, bogus_content); | 662 CreateTextFile(link_to, bogus_content); |
| 663 | 663 |
| 664 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 664 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
| 665 << "Failed to create file symlink."; | 665 << "Failed to create file symlink."; |
| 666 | 666 |
| 667 // If we created the link properly, we should be able to read the | 667 // If we created the link properly, we should be able to read the contents |
| 668 // contents through it. | 668 // through it. |
| 669 std::wstring contents = ReadTextFile(link_from); | 669 std::wstring contents = ReadTextFile(link_from); |
| 670 ASSERT_EQ(contents, bogus_content); | 670 EXPECT_EQ(bogus_content, contents); |
| 671 | 671 |
| 672 FilePath result; | 672 FilePath result; |
| 673 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from, &result)); | 673 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from, &result)); |
| 674 ASSERT_EQ(link_to.value(), result.value()); | 674 EXPECT_EQ(link_to.value(), result.value()); |
| 675 | 675 |
| 676 // Link to a directory. | 676 // Link to a directory. |
| 677 link_from = temp_dir_.path().Append(FPL("from_dir")); | 677 link_from = temp_dir_.path().Append(FPL("from_dir")); |
| 678 link_to = temp_dir_.path().Append(FPL("to_dir")); | 678 link_to = temp_dir_.path().Append(FPL("to_dir")); |
| 679 file_util::CreateDirectory(link_to); | 679 ASSERT_TRUE(file_util::CreateDirectory(link_to)); |
| 680 | |
| 681 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 680 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
| 682 << "Failed to create directory symlink."; | 681 << "Failed to create directory symlink."; |
| 683 | 682 |
| 684 // Test failures. | 683 // Test failures. |
| 685 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); | 684 EXPECT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); |
| 686 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); | 685 EXPECT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); |
| 687 FilePath missing = temp_dir_.path().Append(FPL("missing")); | 686 FilePath missing = temp_dir_.path().Append(FPL("missing")); |
| 688 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result)); | 687 EXPECT_FALSE(file_util::ReadSymbolicLink(missing, &result)); |
| 689 } | 688 } |
| 690 | 689 |
| 691 // The following test of NormalizeFilePath() require that we create a symlink. | 690 // The following test of NormalizeFilePath() require that we create a symlink. |
| 692 // This can not be done on Windows before Vista. On Vista, creating a symlink | 691 // This can not be done on Windows before Vista. On Vista, creating a symlink |
| 693 // requires privilege "SeCreateSymbolicLinkPrivilege". | 692 // requires privilege "SeCreateSymbolicLinkPrivilege". |
| 694 // TODO(skerner): Investigate the possibility of giving base_unittests the | 693 // TODO(skerner): Investigate the possibility of giving base_unittests the |
| 695 // privileges required to create a symlink. | 694 // privileges required to create a symlink. |
| 696 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { | 695 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { |
| 697 FilePath normalized_path; | |
| 698 | |
| 699 // Link one file to another. | 696 // Link one file to another. |
| 700 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); | 697 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); |
| 701 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); | 698 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); |
| 702 CreateTextFile(link_to, bogus_content); | 699 CreateTextFile(link_to, bogus_content); |
| 703 | 700 |
| 704 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 701 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
| 705 << "Failed to create file symlink."; | 702 << "Failed to create file symlink."; |
| 706 | 703 |
| 707 // Check that NormalizeFilePath sees the link. | 704 // Check that NormalizeFilePath sees the link. |
| 705 FilePath normalized_path; |
| 708 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path)); | 706 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path)); |
| 709 ASSERT_TRUE(link_to != link_from); | 707 EXPECT_NE(link_from, link_to); |
| 710 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); | 708 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); |
| 711 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); | 709 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); |
| 712 | 710 |
| 713 // Link to a directory. | 711 // Link to a directory. |
| 714 link_from = temp_dir_.path().Append(FPL("from_dir")); | 712 link_from = temp_dir_.path().Append(FPL("from_dir")); |
| 715 link_to = temp_dir_.path().Append(FPL("to_dir")); | 713 link_to = temp_dir_.path().Append(FPL("to_dir")); |
| 716 file_util::CreateDirectory(link_to); | 714 ASSERT_TRUE(file_util::CreateDirectory(link_to)); |
| 717 | |
| 718 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 715 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
| 719 << "Failed to create directory symlink."; | 716 << "Failed to create directory symlink."; |
| 720 | 717 |
| 721 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)) | 718 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)) |
| 722 << "Links to directories should return false."; | 719 << "Links to directories should return false."; |
| 723 | 720 |
| 724 // Test that a loop in the links causes NormalizeFilePath() to return false. | 721 // Test that a loop in the links causes NormalizeFilePath() to return false. |
| 725 link_from = temp_dir_.path().Append(FPL("link_a")); | 722 link_from = temp_dir_.path().Append(FPL("link_a")); |
| 726 link_to = temp_dir_.path().Append(FPL("link_b")); | 723 link_to = temp_dir_.path().Append(FPL("link_b")); |
| 727 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 724 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
| 728 << "Failed to create loop symlink a."; | 725 << "Failed to create loop symlink a."; |
| 729 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from, link_to)) | 726 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from, link_to)) |
| 730 << "Failed to create loop symlink b."; | 727 << "Failed to create loop symlink b."; |
| 731 | 728 |
| 732 // Infinite loop! | 729 // Infinite loop! |
| 733 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)); | 730 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)); |
| 734 } | 731 } |
| 735 #endif // defined(OS_POSIX) | 732 #endif // defined(OS_POSIX) |
| 736 | 733 |
| 737 TEST_F(FileUtilTest, DeleteNonExistent) { | 734 TEST_F(FileUtilTest, DeleteNonExistent) { |
| 738 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar"); | 735 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar"); |
| 739 ASSERT_FALSE(file_util::PathExists(non_existent)); | 736 ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 740 | 737 |
| 741 EXPECT_TRUE(file_util::Delete(non_existent, false)); | 738 EXPECT_TRUE(file_util::Delete(non_existent, false)); |
| 742 ASSERT_FALSE(file_util::PathExists(non_existent)); | 739 ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 743 EXPECT_TRUE(file_util::Delete(non_existent, true)); | 740 EXPECT_TRUE(file_util::Delete(non_existent, true)); |
| (...skipping 1632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2376 file_util::VerifyPathControlledByUser( | 2373 file_util::VerifyPathControlledByUser( |
| 2377 base_dir_, text_file_, uid_, ok_gids_)); | 2374 base_dir_, text_file_, uid_, ok_gids_)); |
| 2378 EXPECT_TRUE( | 2375 EXPECT_TRUE( |
| 2379 file_util::VerifyPathControlledByUser( | 2376 file_util::VerifyPathControlledByUser( |
| 2380 sub_dir_, text_file_, uid_, ok_gids_)); | 2377 sub_dir_, text_file_, uid_, ok_gids_)); |
| 2381 } | 2378 } |
| 2382 | 2379 |
| 2383 #endif // defined(OS_POSIX) | 2380 #endif // defined(OS_POSIX) |
| 2384 | 2381 |
| 2385 } // namespace | 2382 } // namespace |
| OLD | NEW |