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