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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 } | 108 } |
109 return true; | 109 return true; |
110 } | 110 } |
111 #endif | 111 #endif |
112 | 112 |
113 #if defined(OS_POSIX) | 113 #if defined(OS_POSIX) |
114 // Provide a simple way to change the permissions bits on |path| in tests. | 114 // Provide a simple way to change the permissions bits on |path| in tests. |
115 // ASSERT failures will return, but not stop the test. Caller should wrap | 115 // ASSERT failures will return, but not stop the test. Caller should wrap |
116 // calls to this function in ASSERT_NO_FATAL_FAILURE(). | 116 // calls to this function in ASSERT_NO_FATAL_FAILURE(). |
117 void ChangePosixFilePermissions(const FilePath& path, | 117 void ChangePosixFilePermissions(const FilePath& path, |
118 mode_t mode_bits_to_set, | 118 int mode_bits_to_set, |
119 mode_t mode_bits_to_clear) { | 119 int mode_bits_to_clear) { |
120 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear) | 120 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear) |
121 << "Can't set and clear the same bits."; | 121 << "Can't set and clear the same bits."; |
122 | 122 |
123 struct stat stat_buf; | 123 int mode = 0; |
124 ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf)); | 124 ASSERT_TRUE(file_util::GetPosixFilePermissions(path, &mode)); |
125 | 125 mode |= mode_bits_to_set; |
126 mode_t updated_mode_bits = stat_buf.st_mode; | 126 mode &= ~mode_bits_to_clear; |
127 updated_mode_bits |= mode_bits_to_set; | 127 ASSERT_TRUE(file_util::SetPosixFilePermissions(path, mode)); |
128 updated_mode_bits &= ~mode_bits_to_clear; | |
129 | |
130 ASSERT_EQ(0, chmod(path.value().c_str(), updated_mode_bits)); | |
131 } | 128 } |
132 #endif // defined(OS_POSIX) | 129 #endif // defined(OS_POSIX) |
133 | 130 |
134 const wchar_t bogus_content[] = L"I'm cannon fodder."; | 131 const wchar_t bogus_content[] = L"I'm cannon fodder."; |
135 | 132 |
136 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = | 133 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = |
137 static_cast<file_util::FileEnumerator::FileType>( | 134 static_cast<file_util::FileEnumerator::FileType>( |
138 file_util::FileEnumerator::FILES | | 135 file_util::FileEnumerator::FILES | |
139 file_util::FileEnumerator::DIRECTORIES); | 136 file_util::FileEnumerator::DIRECTORIES); |
140 | 137 |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 657 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
661 << "Failed to create directory symlink."; | 658 << "Failed to create directory symlink."; |
662 | 659 |
663 // Test failures. | 660 // Test failures. |
664 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); | 661 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); |
665 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); | 662 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); |
666 FilePath missing = temp_dir_.path().Append(FPL("missing")); | 663 FilePath missing = temp_dir_.path().Append(FPL("missing")); |
667 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result)); | 664 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result)); |
668 } | 665 } |
669 | 666 |
670 | |
671 // The following test of NormalizeFilePath() require that we create a symlink. | 667 // The following test of NormalizeFilePath() require that we create a symlink. |
672 // This can not be done on Windows before Vista. On Vista, creating a symlink | 668 // This can not be done on Windows before Vista. On Vista, creating a symlink |
673 // requires privilege "SeCreateSymbolicLinkPrivilege". | 669 // requires privilege "SeCreateSymbolicLinkPrivilege". |
674 // TODO(skerner): Investigate the possibility of giving base_unittests the | 670 // TODO(skerner): Investigate the possibility of giving base_unittests the |
675 // privileges required to create a symlink. | 671 // privileges required to create a symlink. |
676 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { | 672 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { |
677 FilePath normalized_path; | 673 FilePath normalized_path; |
678 | 674 |
679 // Link one file to another. | 675 // Link one file to another. |
680 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); | 676 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 CreateTextFile(file_name, bogus_content); | 735 CreateTextFile(file_name, bogus_content); |
740 ASSERT_TRUE(file_util::PathExists(file_name)); | 736 ASSERT_TRUE(file_util::PathExists(file_name)); |
741 | 737 |
742 // Make sure it's deleted | 738 // Make sure it's deleted |
743 EXPECT_TRUE(file_util::Delete(file_name, true)); | 739 EXPECT_TRUE(file_util::Delete(file_name, true)); |
744 EXPECT_FALSE(file_util::PathExists(file_name)); | 740 EXPECT_FALSE(file_util::PathExists(file_name)); |
745 } | 741 } |
746 | 742 |
747 #if defined(OS_POSIX) | 743 #if defined(OS_POSIX) |
748 TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) { | 744 TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) { |
749 // Create a file | 745 // Create a file. |
750 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt")); | 746 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt")); |
751 CreateTextFile(file_name, bogus_content); | 747 CreateTextFile(file_name, bogus_content); |
752 ASSERT_TRUE(file_util::PathExists(file_name)); | 748 ASSERT_TRUE(file_util::PathExists(file_name)); |
753 | 749 |
754 // Create a symlink to the file | 750 // Create a symlink to the file. |
755 FilePath file_link = temp_dir_.path().Append("file_link_2"); | 751 FilePath file_link = temp_dir_.path().Append("file_link_2"); |
756 ASSERT_TRUE(file_util::CreateSymbolicLink(file_name, file_link)) | 752 ASSERT_TRUE(file_util::CreateSymbolicLink(file_name, file_link)) |
757 << "Failed to create symlink."; | 753 << "Failed to create symlink."; |
758 | 754 |
759 // Delete the symbolic link | 755 // Delete the symbolic link. |
760 EXPECT_TRUE(file_util::Delete(file_link, false)); | 756 EXPECT_TRUE(file_util::Delete(file_link, false)); |
761 | 757 |
762 // Make sure original file is not deleted | 758 // Make sure original file is not deleted. |
763 EXPECT_FALSE(file_util::PathExists(file_link)); | 759 EXPECT_FALSE(file_util::PathExists(file_link)); |
764 EXPECT_TRUE(file_util::PathExists(file_name)); | 760 EXPECT_TRUE(file_util::PathExists(file_name)); |
765 } | 761 } |
766 | 762 |
767 TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) { | 763 TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) { |
768 // Create a non-existent file path | 764 // Create a non-existent file path. |
769 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt")); | 765 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt")); |
770 EXPECT_FALSE(file_util::PathExists(non_existent)); | 766 EXPECT_FALSE(file_util::PathExists(non_existent)); |
771 | 767 |
772 // Create a symlink to the non-existent file | 768 // Create a symlink to the non-existent file. |
773 FilePath file_link = temp_dir_.path().Append("file_link_3"); | 769 FilePath file_link = temp_dir_.path().Append("file_link_3"); |
774 ASSERT_TRUE(file_util::CreateSymbolicLink(non_existent, file_link)) | 770 ASSERT_TRUE(file_util::CreateSymbolicLink(non_existent, file_link)) |
775 << "Failed to create symlink."; | 771 << "Failed to create symlink."; |
776 | 772 |
777 // Make sure the symbolic link is exist | 773 // Make sure the symbolic link is exist. |
778 EXPECT_TRUE(file_util::IsLink(file_link)); | 774 EXPECT_TRUE(file_util::IsLink(file_link)); |
779 EXPECT_FALSE(file_util::PathExists(file_link)); | 775 EXPECT_FALSE(file_util::PathExists(file_link)); |
780 | 776 |
781 // Delete the symbolic link | 777 // Delete the symbolic link. |
782 EXPECT_TRUE(file_util::Delete(file_link, false)); | 778 EXPECT_TRUE(file_util::Delete(file_link, false)); |
783 | 779 |
784 // Make sure the symbolic link is deleted | 780 // Make sure the symbolic link is deleted. |
785 EXPECT_FALSE(file_util::IsLink(file_link)); | 781 EXPECT_FALSE(file_util::IsLink(file_link)); |
786 } | 782 } |
| 783 |
| 784 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) { |
| 785 // Create a file path. |
| 786 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
| 787 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 788 |
| 789 const std::string kData("hello"); |
| 790 |
| 791 int buffer_size = kData.length(); |
| 792 char* buffer = new char[buffer_size]; |
| 793 |
| 794 // Write file. |
| 795 EXPECT_EQ(static_cast<int>(kData.length()), |
| 796 file_util::WriteFile(file_name, kData.data(), kData.length())); |
| 797 EXPECT_TRUE(file_util::PathExists(file_name)); |
| 798 |
| 799 // Make sure the file is readable. |
| 800 int32 mode = 0; |
| 801 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 802 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_READ_BY_USER); |
| 803 |
| 804 // Get rid of the read permission. |
| 805 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); |
| 806 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 807 EXPECT_FALSE(mode & file_util::FILE_PERMISSION_READ_BY_USER); |
| 808 // Make sure the file can't be read. |
| 809 EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, buffer_size)); |
| 810 |
| 811 // Give the read permission. |
| 812 EXPECT_TRUE(file_util::SetPosixFilePermissions( |
| 813 file_name, |
| 814 file_util::FILE_PERMISSION_READ_BY_USER)); |
| 815 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 816 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_READ_BY_USER); |
| 817 // Make sure the file can be read. |
| 818 EXPECT_EQ(static_cast<int>(kData.length()), |
| 819 file_util::ReadFile(file_name, buffer, buffer_size)); |
| 820 |
| 821 // Delete the file. |
| 822 EXPECT_TRUE(file_util::Delete(file_name, false)); |
| 823 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 824 |
| 825 delete[] buffer; |
| 826 } |
| 827 |
| 828 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { |
| 829 // Create a file path. |
| 830 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
| 831 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 832 |
| 833 const std::string kData("hello"); |
| 834 |
| 835 // Write file. |
| 836 EXPECT_EQ(static_cast<int>(kData.length()), |
| 837 file_util::WriteFile(file_name, kData.data(), kData.length())); |
| 838 EXPECT_TRUE(file_util::PathExists(file_name)); |
| 839 |
| 840 // Make sure the file is writable. |
| 841 int mode = 0; |
| 842 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 843 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
| 844 EXPECT_TRUE(file_util::PathIsWritable(file_name)); |
| 845 |
| 846 // Get rid of the write permission. |
| 847 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); |
| 848 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 849 EXPECT_FALSE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
| 850 // Make sure the file can't be write. |
| 851 EXPECT_EQ(-1, |
| 852 file_util::WriteFile(file_name, kData.data(), kData.length())); |
| 853 EXPECT_FALSE(file_util::PathIsWritable(file_name)); |
| 854 |
| 855 // Give read permission. |
| 856 EXPECT_TRUE(file_util::SetPosixFilePermissions( |
| 857 file_name, |
| 858 file_util::FILE_PERMISSION_WRITE_BY_USER)); |
| 859 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 860 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
| 861 // Make sure the file can be write. |
| 862 EXPECT_EQ(static_cast<int>(kData.length()), |
| 863 file_util::WriteFile(file_name, kData.data(), kData.length())); |
| 864 EXPECT_TRUE(file_util::PathIsWritable(file_name)); |
| 865 |
| 866 // Delete the file. |
| 867 EXPECT_TRUE(file_util::Delete(file_name, false)); |
| 868 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 869 } |
| 870 |
| 871 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { |
| 872 // Create a directory path. |
| 873 FilePath subdir_path = |
| 874 temp_dir_.path().Append(FPL("PermissionTest1")); |
| 875 file_util::CreateDirectory(subdir_path); |
| 876 ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 877 |
| 878 // Create a dummy file to enumerate. |
| 879 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); |
| 880 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 881 const std::string kData("hello"); |
| 882 EXPECT_EQ(static_cast<int>(kData.length()), |
| 883 file_util::WriteFile(file_name, kData.data(), kData.length())); |
| 884 EXPECT_TRUE(file_util::PathExists(file_name)); |
| 885 |
| 886 // Make sure the directory has the all permissions. |
| 887 int mode = 0; |
| 888 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
| 889 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, |
| 890 mode & file_util::FILE_PERMISSION_USER_MASK); |
| 891 |
| 892 // Get rid of the permissions from the directory. |
| 893 EXPECT_TRUE(file_util::SetPosixFilePermissions(subdir_path, 0u)); |
| 894 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
| 895 EXPECT_FALSE(mode & file_util::FILE_PERMISSION_USER_MASK); |
| 896 |
| 897 // Make sure the file in the directory can't be enumerated. |
| 898 file_util::FileEnumerator f1(subdir_path, true, |
| 899 file_util::FileEnumerator::FILES); |
| 900 EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 901 FindResultCollector c1(f1); |
| 902 EXPECT_EQ(c1.size(), 0); |
| 903 EXPECT_FALSE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 904 |
| 905 // Give the permissions to the directory. |
| 906 EXPECT_TRUE(file_util::SetPosixFilePermissions( |
| 907 subdir_path, |
| 908 file_util::FILE_PERMISSION_USER_MASK)); |
| 909 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
| 910 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, |
| 911 mode & file_util::FILE_PERMISSION_USER_MASK); |
| 912 |
| 913 // Make sure the file in the directory can be enumerated. |
| 914 file_util::FileEnumerator f2(subdir_path, true, |
| 915 file_util::FileEnumerator::FILES); |
| 916 FindResultCollector c2(f2); |
| 917 EXPECT_TRUE(c2.HasFile(file_name)); |
| 918 EXPECT_EQ(c2.size(), 1); |
| 919 |
| 920 // Delete the file. |
| 921 EXPECT_TRUE(file_util::Delete(subdir_path, true)); |
| 922 EXPECT_FALSE(file_util::PathExists(subdir_path)); |
| 923 } |
| 924 |
787 #endif // defined(OS_POSIX) | 925 #endif // defined(OS_POSIX) |
788 | 926 |
789 #if defined(OS_WIN) | 927 #if defined(OS_WIN) |
790 // Tests that the Delete function works for wild cards, especially | 928 // Tests that the Delete function works for wild cards, especially |
791 // with the recursion flag. Also coincidentally tests PathExists. | 929 // with the recursion flag. Also coincidentally tests PathExists. |
792 // TODO(erikkay): see if anyone's actually using this feature of the API | 930 // TODO(erikkay): see if anyone's actually using this feature of the API |
793 TEST_F(FileUtilTest, DeleteWildCard) { | 931 TEST_F(FileUtilTest, DeleteWildCard) { |
794 // Create a file and a directory | 932 // Create a file and a directory |
795 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); | 933 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); |
796 CreateTextFile(file_name, bogus_content); | 934 CreateTextFile(file_name, bogus_content); |
(...skipping 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1990 uid_ = stat_buf.st_uid; | 2128 uid_ = stat_buf.st_uid; |
1991 ok_gids_.insert(stat_buf.st_gid); | 2129 ok_gids_.insert(stat_buf.st_gid); |
1992 bad_gids_.insert(stat_buf.st_gid + 1); | 2130 bad_gids_.insert(stat_buf.st_gid + 1); |
1993 | 2131 |
1994 ASSERT_EQ(uid_, getuid()); // This process should be the owner. | 2132 ASSERT_EQ(uid_, getuid()); // This process should be the owner. |
1995 | 2133 |
1996 // To ensure that umask settings do not cause the initial state | 2134 // To ensure that umask settings do not cause the initial state |
1997 // of permissions to be different from what we expect, explicitly | 2135 // of permissions to be different from what we expect, explicitly |
1998 // set permissions on the directories we create. | 2136 // set permissions on the directories we create. |
1999 // Make all files and directories non-world-writable. | 2137 // Make all files and directories non-world-writable. |
2000 mode_t enabled_permissions = | 2138 |
2001 S_IRWXU | // User can read, write, traverse | 2139 // Users and group can read, write, traverse |
2002 S_IRWXG; // Group can read, write, traverse | 2140 int enabled_permissions = |
2003 mode_t disabled_permissions = | 2141 file_util::FILE_PERMISSION_USER_MASK | |
2004 S_IRWXO; // Other users can't read, write, traverse. | 2142 file_util::FILE_PERMISSION_GROUP_MASK; |
| 2143 // Other users can't read, write, traverse |
| 2144 int disabled_permissions = |
| 2145 file_util::FILE_PERMISSION_OTHERS_MASK; |
2005 | 2146 |
2006 ASSERT_NO_FATAL_FAILURE( | 2147 ASSERT_NO_FATAL_FAILURE( |
2007 ChangePosixFilePermissions( | 2148 ChangePosixFilePermissions( |
2008 base_dir_, enabled_permissions, disabled_permissions)); | 2149 base_dir_, enabled_permissions, disabled_permissions)); |
2009 ASSERT_NO_FATAL_FAILURE( | 2150 ASSERT_NO_FATAL_FAILURE( |
2010 ChangePosixFilePermissions( | 2151 ChangePosixFilePermissions( |
2011 sub_dir_, enabled_permissions, disabled_permissions)); | 2152 sub_dir_, enabled_permissions, disabled_permissions)); |
2012 } | 2153 } |
2013 | 2154 |
2014 FilePath base_dir_; | 2155 FilePath base_dir_; |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2318 file_util::VerifyPathControlledByUser( | 2459 file_util::VerifyPathControlledByUser( |
2319 base_dir_, text_file_, uid_, ok_gids_)); | 2460 base_dir_, text_file_, uid_, ok_gids_)); |
2320 EXPECT_TRUE( | 2461 EXPECT_TRUE( |
2321 file_util::VerifyPathControlledByUser( | 2462 file_util::VerifyPathControlledByUser( |
2322 sub_dir_, text_file_, uid_, ok_gids_)); | 2463 sub_dir_, text_file_, uid_, ok_gids_)); |
2323 } | 2464 } |
2324 | 2465 |
2325 #endif // defined(OS_POSIX) | 2466 #endif // defined(OS_POSIX) |
2326 | 2467 |
2327 } // namespace | 2468 } // namespace |
OLD | NEW |