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, | |
118 mode_t mode_bits_to_set, | |
119 mode_t mode_bits_to_clear) { | |
120 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear) | |
121 << "Can't set and clear the same bits."; | |
122 | |
123 struct stat stat_buf; | |
124 ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf)); | |
125 | |
126 mode_t updated_mode_bits = stat_buf.st_mode; | |
127 updated_mode_bits |= mode_bits_to_set; | |
128 updated_mode_bits &= ~mode_bits_to_clear; | |
129 | |
130 ASSERT_EQ(0, chmod(path.value().c_str(), updated_mode_bits)); | |
131 } | |
132 #endif // defined(OS_POSIX) | 114 #endif // defined(OS_POSIX) |
133 | 115 |
134 const wchar_t bogus_content[] = L"I'm cannon fodder."; | 116 const wchar_t bogus_content[] = L"I'm cannon fodder."; |
135 | 117 |
136 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = | 118 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = |
137 static_cast<file_util::FileEnumerator::FileType>( | 119 static_cast<file_util::FileEnumerator::FileType>( |
138 file_util::FileEnumerator::FILES | | 120 file_util::FileEnumerator::FILES | |
139 file_util::FileEnumerator::DIRECTORIES); | 121 file_util::FileEnumerator::DIRECTORIES); |
140 | 122 |
141 // file_util winds up using autoreleased objects on the Mac, so this needs | 123 // file_util winds up using autoreleased objects on the Mac, so this needs |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 642 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
661 << "Failed to create directory symlink."; | 643 << "Failed to create directory symlink."; |
662 | 644 |
663 // Test failures. | 645 // Test failures. |
664 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); | 646 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); |
665 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); | 647 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); |
666 FilePath missing = temp_dir_.path().Append(FPL("missing")); | 648 FilePath missing = temp_dir_.path().Append(FPL("missing")); |
667 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result)); | 649 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result)); |
668 } | 650 } |
669 | 651 |
670 | |
671 // The following test of NormalizeFilePath() require that we create a symlink. | 652 // 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 | 653 // This can not be done on Windows before Vista. On Vista, creating a symlink |
673 // requires privilege "SeCreateSymbolicLinkPrivilege". | 654 // requires privilege "SeCreateSymbolicLinkPrivilege". |
674 // TODO(skerner): Investigate the possibility of giving base_unittests the | 655 // TODO(skerner): Investigate the possibility of giving base_unittests the |
675 // privileges required to create a symlink. | 656 // privileges required to create a symlink. |
676 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { | 657 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { |
677 FilePath normalized_path; | 658 FilePath normalized_path; |
678 | 659 |
679 // Link one file to another. | 660 // Link one file to another. |
680 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); | 661 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 // Make sure the symbolic link is exist | 758 // Make sure the symbolic link is exist |
778 EXPECT_TRUE(file_util::IsLink(file_link)); | 759 EXPECT_TRUE(file_util::IsLink(file_link)); |
779 EXPECT_FALSE(file_util::PathExists(file_link)); | 760 EXPECT_FALSE(file_util::PathExists(file_link)); |
780 | 761 |
781 // Delete the symbolic link | 762 // Delete the symbolic link |
782 EXPECT_TRUE(file_util::Delete(file_link, false)); | 763 EXPECT_TRUE(file_util::Delete(file_link, false)); |
783 | 764 |
784 // Make sure the symbolic link is deleted | 765 // Make sure the symbolic link is deleted |
785 EXPECT_FALSE(file_util::IsLink(file_link)); | 766 EXPECT_FALSE(file_util::IsLink(file_link)); |
786 } | 767 } |
| 768 |
| 769 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) { |
| 770 // Create a file path |
| 771 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
| 772 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 773 |
| 774 char buffer[32] = "hello"; |
| 775 std::string data(buffer); |
| 776 |
| 777 // Write file |
| 778 EXPECT_EQ(static_cast<int>(data.length()), |
| 779 file_util::WriteFile(file_name, data.c_str(), data.length())); |
| 780 EXPECT_TRUE(file_util::PathExists(file_name)); |
| 781 |
| 782 // Meke sure the file is readable |
| 783 int32 mode; |
| 784 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 785 EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER, |
| 786 mode & file_util::FILE_PERMISSION_READ_BY_USER); |
| 787 |
| 788 // Get rid of the read permission |
| 789 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); |
| 790 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 791 EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_READ_BY_USER); |
| 792 // Make sure the file can't be read |
| 793 EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, sizeof(buffer))); |
| 794 |
| 795 // Give the read permission |
| 796 EXPECT_TRUE(file_util::SetPosixFilePermissions( |
| 797 file_name, |
| 798 file_util::FILE_PERMISSION_READ_BY_USER)); |
| 799 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 800 EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER, |
| 801 mode & file_util::FILE_PERMISSION_READ_BY_USER); |
| 802 // Make sure the file can be read |
| 803 EXPECT_EQ(static_cast<int>(data.length()), |
| 804 file_util::ReadFile(file_name, buffer, sizeof(buffer))); |
| 805 |
| 806 // Delete the file |
| 807 EXPECT_TRUE(file_util::Delete(file_name, false)); |
| 808 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 809 } |
| 810 |
| 811 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { |
| 812 // Create a file path |
| 813 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
| 814 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 815 |
| 816 char buffer[32] = "hello"; |
| 817 std::string data(buffer); |
| 818 |
| 819 // Write file |
| 820 EXPECT_EQ(static_cast<int>(data.length()), |
| 821 file_util::WriteFile(file_name, data.c_str(), data.length())); |
| 822 EXPECT_TRUE(file_util::PathExists(file_name)); |
| 823 |
| 824 // Meke sure the file is writable |
| 825 int mode; |
| 826 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 827 EXPECT_EQ(file_util::FILE_PERMISSION_WRITE_BY_USER, |
| 828 mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
| 829 EXPECT_TRUE(file_util::PathIsWritable(file_name)); |
| 830 |
| 831 // Get rid of the write permission |
| 832 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); |
| 833 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 834 EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
| 835 // Make sure the file can't be write |
| 836 EXPECT_EQ(-1, |
| 837 file_util::WriteFile(file_name, data.c_str(), data.length())); |
| 838 EXPECT_FALSE(file_util::PathIsWritable(file_name)); |
| 839 |
| 840 // Give read permission |
| 841 EXPECT_TRUE(file_util::SetPosixFilePermissions( |
| 842 file_name, |
| 843 file_util::FILE_PERMISSION_WRITE_BY_USER)); |
| 844 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 845 EXPECT_EQ(file_util::FILE_PERMISSION_WRITE_BY_USER, |
| 846 mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
| 847 // Make sure the file can be write |
| 848 EXPECT_EQ(static_cast<int>(data.length()), |
| 849 file_util::WriteFile(file_name, data.c_str(), data.length())); |
| 850 EXPECT_TRUE(file_util::PathIsWritable(file_name)); |
| 851 |
| 852 // Delete the file |
| 853 EXPECT_TRUE(file_util::Delete(file_name, false)); |
| 854 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 855 } |
| 856 |
| 857 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { |
| 858 // Create a directory path |
| 859 FilePath subdir_path = |
| 860 temp_dir_.path().Append(FPL("PermissionTest1")); |
| 861 file_util::CreateDirectory(subdir_path); |
| 862 ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 863 |
| 864 // Create a dummy file to enumerate |
| 865 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); |
| 866 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 867 char buffer[32] = "hello"; |
| 868 std::string data(buffer); |
| 869 EXPECT_EQ(static_cast<int>(data.length()), |
| 870 file_util::WriteFile(file_name, data.c_str(), data.length())); |
| 871 EXPECT_TRUE(file_util::PathExists(file_name)); |
| 872 |
| 873 // Meke sure the file is the all permissions |
| 874 int mode; |
| 875 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
| 876 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, |
| 877 mode & file_util::FILE_PERMISSION_USER_MASK); |
| 878 |
| 879 // Get rid of the permissions from the directory |
| 880 EXPECT_TRUE(file_util::SetPosixFilePermissions(subdir_path, 0u)); |
| 881 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
| 882 EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_USER_MASK); |
| 883 |
| 884 // Make sure the file in the directory can't be enumerated. |
| 885 file_util::FileEnumerator f1(subdir_path, true, |
| 886 file_util::FileEnumerator::FILES); |
| 887 EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 888 FindResultCollector c1(f1); |
| 889 EXPECT_EQ(c1.size(), 0); |
| 890 EXPECT_FALSE(file_util::GetPosixFilePermissions(file_name, &mode)); |
| 891 |
| 892 // Give the permissions to the directory |
| 893 EXPECT_TRUE(file_util::SetPosixFilePermissions( |
| 894 subdir_path, |
| 895 file_util::FILE_PERMISSION_USER_MASK)); |
| 896 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
| 897 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, |
| 898 mode & file_util::FILE_PERMISSION_USER_MASK); |
| 899 |
| 900 // Make sure the file in the directory can be enumerated. |
| 901 file_util::FileEnumerator f2(subdir_path, true, |
| 902 file_util::FileEnumerator::FILES); |
| 903 FindResultCollector c2(f2); |
| 904 EXPECT_TRUE(c2.HasFile(file_name)); |
| 905 EXPECT_EQ(c2.size(), 1); |
| 906 |
| 907 // Delete the file |
| 908 EXPECT_TRUE(file_util::Delete(subdir_path, true)); |
| 909 EXPECT_FALSE(file_util::PathExists(subdir_path)); |
| 910 } |
| 911 |
787 #endif // defined(OS_POSIX) | 912 #endif // defined(OS_POSIX) |
788 | 913 |
789 #if defined(OS_WIN) | 914 #if defined(OS_WIN) |
790 // Tests that the Delete function works for wild cards, especially | 915 // Tests that the Delete function works for wild cards, especially |
791 // with the recursion flag. Also coincidentally tests PathExists. | 916 // with the recursion flag. Also coincidentally tests PathExists. |
792 // TODO(erikkay): see if anyone's actually using this feature of the API | 917 // TODO(erikkay): see if anyone's actually using this feature of the API |
793 TEST_F(FileUtilTest, DeleteWildCard) { | 918 TEST_F(FileUtilTest, DeleteWildCard) { |
794 // Create a file and a directory | 919 // Create a file and a directory |
795 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); | 920 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); |
796 CreateTextFile(file_name, bogus_content); | 921 CreateTextFile(file_name, bogus_content); |
(...skipping 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1990 uid_ = stat_buf.st_uid; | 2115 uid_ = stat_buf.st_uid; |
1991 ok_gids_.insert(stat_buf.st_gid); | 2116 ok_gids_.insert(stat_buf.st_gid); |
1992 bad_gids_.insert(stat_buf.st_gid + 1); | 2117 bad_gids_.insert(stat_buf.st_gid + 1); |
1993 | 2118 |
1994 ASSERT_EQ(uid_, getuid()); // This process should be the owner. | 2119 ASSERT_EQ(uid_, getuid()); // This process should be the owner. |
1995 | 2120 |
1996 // To ensure that umask settings do not cause the initial state | 2121 // To ensure that umask settings do not cause the initial state |
1997 // of permissions to be different from what we expect, explicitly | 2122 // of permissions to be different from what we expect, explicitly |
1998 // set permissions on the directories we create. | 2123 // set permissions on the directories we create. |
1999 // Make all files and directories non-world-writable. | 2124 // Make all files and directories non-world-writable. |
2000 mode_t enabled_permissions = | |
2001 S_IRWXU | // User can read, write, traverse | |
2002 S_IRWXG; // Group can read, write, traverse | |
2003 mode_t disabled_permissions = | |
2004 S_IRWXO; // Other users can't read, write, traverse. | |
2005 | 2125 |
2006 ASSERT_NO_FATAL_FAILURE( | 2126 // Users and group can read, write, traverse |
2007 ChangePosixFilePermissions( | 2127 int enabled_permissions = |
| 2128 file_util::FILE_PERMISSION_USER_MASK | |
| 2129 file_util::FILE_PERMISSION_GROUP_MASK; |
| 2130 // Other users can't read, write, traverse |
| 2131 int disabled_permissions = |
| 2132 file_util::FILE_PERMISSION_OTHERS_MASK; |
| 2133 |
| 2134 ASSERT_TRUE( |
| 2135 file_util::ChangePosixFilePermissions( |
2008 base_dir_, enabled_permissions, disabled_permissions)); | 2136 base_dir_, enabled_permissions, disabled_permissions)); |
2009 ASSERT_NO_FATAL_FAILURE( | 2137 ASSERT_TRUE( |
2010 ChangePosixFilePermissions( | 2138 file_util::ChangePosixFilePermissions( |
2011 sub_dir_, enabled_permissions, disabled_permissions)); | 2139 sub_dir_, enabled_permissions, disabled_permissions)); |
2012 } | 2140 } |
2013 | 2141 |
2014 FilePath base_dir_; | 2142 FilePath base_dir_; |
2015 FilePath sub_dir_; | 2143 FilePath sub_dir_; |
2016 FilePath text_file_; | 2144 FilePath text_file_; |
2017 uid_t uid_; | 2145 uid_t uid_; |
2018 | 2146 |
2019 std::set<gid_t> ok_gids_; | 2147 std::set<gid_t> ok_gids_; |
2020 std::set<gid_t> bad_gids_; | 2148 std::set<gid_t> bad_gids_; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2081 EXPECT_TRUE( | 2209 EXPECT_TRUE( |
2082 file_util::VerifyPathControlledByUser( | 2210 file_util::VerifyPathControlledByUser( |
2083 file_path_with_link, file_path_with_link, uid_, ok_gids_)); | 2211 file_path_with_link, file_path_with_link, uid_, ok_gids_)); |
2084 } | 2212 } |
2085 | 2213 |
2086 TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) { | 2214 TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) { |
2087 // Get a uid that is not the uid of files we create. | 2215 // Get a uid that is not the uid of files we create. |
2088 uid_t bad_uid = uid_ + 1; | 2216 uid_t bad_uid = uid_ + 1; |
2089 | 2217 |
2090 // Make all files and directories non-world-writable. | 2218 // Make all files and directories non-world-writable. |
2091 ASSERT_NO_FATAL_FAILURE( | 2219 ASSERT_TRUE( |
2092 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH)); | 2220 file_util:: ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH)); |
2093 ASSERT_NO_FATAL_FAILURE( | 2221 ASSERT_TRUE( |
2094 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH)); | 2222 file_util::ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH)); |
2095 ASSERT_NO_FATAL_FAILURE( | 2223 ASSERT_TRUE( |
2096 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH)); | 2224 file_util::ChangePosixFilePermissions(text_file_, 0u, S_IWOTH)); |
2097 | 2225 |
2098 // We control these paths. | 2226 // We control these paths. |
2099 EXPECT_TRUE( | 2227 EXPECT_TRUE( |
2100 file_util::VerifyPathControlledByUser( | 2228 file_util::VerifyPathControlledByUser( |
2101 base_dir_, sub_dir_, uid_, ok_gids_)); | 2229 base_dir_, sub_dir_, uid_, ok_gids_)); |
2102 EXPECT_TRUE( | 2230 EXPECT_TRUE( |
2103 file_util::VerifyPathControlledByUser( | 2231 file_util::VerifyPathControlledByUser( |
2104 base_dir_, text_file_, uid_, ok_gids_)); | 2232 base_dir_, text_file_, uid_, ok_gids_)); |
2105 EXPECT_TRUE( | 2233 EXPECT_TRUE( |
2106 file_util::VerifyPathControlledByUser( | 2234 file_util::VerifyPathControlledByUser( |
(...skipping 17 matching lines...) Expand all Loading... |
2124 EXPECT_FALSE( | 2252 EXPECT_FALSE( |
2125 file_util::VerifyPathControlledByUser( | 2253 file_util::VerifyPathControlledByUser( |
2126 base_dir_, text_file_, uid_, bad_gids_)); | 2254 base_dir_, text_file_, uid_, bad_gids_)); |
2127 EXPECT_FALSE( | 2255 EXPECT_FALSE( |
2128 file_util::VerifyPathControlledByUser( | 2256 file_util::VerifyPathControlledByUser( |
2129 sub_dir_, text_file_, uid_, bad_gids_)); | 2257 sub_dir_, text_file_, uid_, bad_gids_)); |
2130 } | 2258 } |
2131 | 2259 |
2132 TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) { | 2260 TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) { |
2133 // Make all files and directories writable only by their owner. | 2261 // Make all files and directories writable only by their owner. |
2134 ASSERT_NO_FATAL_FAILURE( | 2262 ASSERT_TRUE( |
2135 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP)); | 2263 file_util::ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP)); |
2136 ASSERT_NO_FATAL_FAILURE( | 2264 ASSERT_TRUE( |
2137 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP)); | 2265 file_util::ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP)); |
2138 ASSERT_NO_FATAL_FAILURE( | 2266 ASSERT_TRUE( |
2139 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP)); | 2267 file_util::ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP)); |
2140 | 2268 |
2141 // Any group is okay because the path is not group-writable. | 2269 // Any group is okay because the path is not group-writable. |
2142 EXPECT_TRUE( | 2270 EXPECT_TRUE( |
2143 file_util::VerifyPathControlledByUser( | 2271 file_util::VerifyPathControlledByUser( |
2144 base_dir_, sub_dir_, uid_, ok_gids_)); | 2272 base_dir_, sub_dir_, uid_, ok_gids_)); |
2145 EXPECT_TRUE( | 2273 EXPECT_TRUE( |
2146 file_util::VerifyPathControlledByUser( | 2274 file_util::VerifyPathControlledByUser( |
2147 base_dir_, text_file_, uid_, ok_gids_)); | 2275 base_dir_, text_file_, uid_, ok_gids_)); |
2148 EXPECT_TRUE( | 2276 EXPECT_TRUE( |
2149 file_util::VerifyPathControlledByUser( | 2277 file_util::VerifyPathControlledByUser( |
(...skipping 17 matching lines...) Expand all Loading... |
2167 base_dir_, sub_dir_, uid_, no_gids)); | 2295 base_dir_, sub_dir_, uid_, no_gids)); |
2168 EXPECT_TRUE( | 2296 EXPECT_TRUE( |
2169 file_util::VerifyPathControlledByUser( | 2297 file_util::VerifyPathControlledByUser( |
2170 base_dir_, text_file_, uid_, no_gids)); | 2298 base_dir_, text_file_, uid_, no_gids)); |
2171 EXPECT_TRUE( | 2299 EXPECT_TRUE( |
2172 file_util::VerifyPathControlledByUser( | 2300 file_util::VerifyPathControlledByUser( |
2173 sub_dir_, text_file_, uid_, no_gids)); | 2301 sub_dir_, text_file_, uid_, no_gids)); |
2174 | 2302 |
2175 | 2303 |
2176 // Make all files and directories writable by their group. | 2304 // Make all files and directories writable by their group. |
2177 ASSERT_NO_FATAL_FAILURE( | 2305 ASSERT_TRUE( |
2178 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u)); | 2306 file_util::ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u)); |
2179 ASSERT_NO_FATAL_FAILURE( | 2307 ASSERT_TRUE( |
2180 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u)); | 2308 file_util::ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u)); |
2181 ASSERT_NO_FATAL_FAILURE( | 2309 ASSERT_TRUE( |
2182 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u)); | 2310 file_util::ChangePosixFilePermissions(text_file_, S_IWGRP, 0u)); |
2183 | 2311 |
2184 // Now |ok_gids_| works, but |bad_gids_| fails. | 2312 // Now |ok_gids_| works, but |bad_gids_| fails. |
2185 EXPECT_TRUE( | 2313 EXPECT_TRUE( |
2186 file_util::VerifyPathControlledByUser( | 2314 file_util::VerifyPathControlledByUser( |
2187 base_dir_, sub_dir_, uid_, ok_gids_)); | 2315 base_dir_, sub_dir_, uid_, ok_gids_)); |
2188 EXPECT_TRUE( | 2316 EXPECT_TRUE( |
2189 file_util::VerifyPathControlledByUser( | 2317 file_util::VerifyPathControlledByUser( |
2190 base_dir_, text_file_, uid_, ok_gids_)); | 2318 base_dir_, text_file_, uid_, ok_gids_)); |
2191 EXPECT_TRUE( | 2319 EXPECT_TRUE( |
2192 file_util::VerifyPathControlledByUser( | 2320 file_util::VerifyPathControlledByUser( |
(...skipping 24 matching lines...) Expand all Loading... |
2217 EXPECT_TRUE( | 2345 EXPECT_TRUE( |
2218 file_util::VerifyPathControlledByUser( | 2346 file_util::VerifyPathControlledByUser( |
2219 base_dir_, text_file_, uid_, multiple_gids)); | 2347 base_dir_, text_file_, uid_, multiple_gids)); |
2220 EXPECT_TRUE( | 2348 EXPECT_TRUE( |
2221 file_util::VerifyPathControlledByUser( | 2349 file_util::VerifyPathControlledByUser( |
2222 sub_dir_, text_file_, uid_, multiple_gids)); | 2350 sub_dir_, text_file_, uid_, multiple_gids)); |
2223 } | 2351 } |
2224 | 2352 |
2225 TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) { | 2353 TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) { |
2226 // Make all files and directories non-world-writable. | 2354 // Make all files and directories non-world-writable. |
2227 ASSERT_NO_FATAL_FAILURE( | 2355 ASSERT_TRUE( |
2228 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH)); | 2356 file_util::ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH)); |
2229 ASSERT_NO_FATAL_FAILURE( | 2357 ASSERT_TRUE( |
2230 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH)); | 2358 file_util::ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH)); |
2231 ASSERT_NO_FATAL_FAILURE( | 2359 ASSERT_TRUE( |
2232 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH)); | 2360 file_util::ChangePosixFilePermissions(text_file_, 0u, S_IWOTH)); |
2233 | 2361 |
2234 // Initialy, we control all parts of the path. | 2362 // Initialy, we control all parts of the path. |
2235 EXPECT_TRUE( | 2363 EXPECT_TRUE( |
2236 file_util::VerifyPathControlledByUser( | 2364 file_util::VerifyPathControlledByUser( |
2237 base_dir_, sub_dir_, uid_, ok_gids_)); | 2365 base_dir_, sub_dir_, uid_, ok_gids_)); |
2238 EXPECT_TRUE( | 2366 EXPECT_TRUE( |
2239 file_util::VerifyPathControlledByUser( | 2367 file_util::VerifyPathControlledByUser( |
2240 base_dir_, text_file_, uid_, ok_gids_)); | 2368 base_dir_, text_file_, uid_, ok_gids_)); |
2241 EXPECT_TRUE( | 2369 EXPECT_TRUE( |
2242 file_util::VerifyPathControlledByUser( | 2370 file_util::VerifyPathControlledByUser( |
2243 sub_dir_, text_file_, uid_, ok_gids_)); | 2371 sub_dir_, text_file_, uid_, ok_gids_)); |
2244 | 2372 |
2245 // Make base_dir_ world-writable. | 2373 // Make base_dir_ world-writable. |
2246 ASSERT_NO_FATAL_FAILURE( | 2374 ASSERT_TRUE( |
2247 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u)); | 2375 file_util::ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u)); |
2248 EXPECT_FALSE( | 2376 EXPECT_FALSE( |
2249 file_util::VerifyPathControlledByUser( | 2377 file_util::VerifyPathControlledByUser( |
2250 base_dir_, sub_dir_, uid_, ok_gids_)); | 2378 base_dir_, sub_dir_, uid_, ok_gids_)); |
2251 EXPECT_FALSE( | 2379 EXPECT_FALSE( |
2252 file_util::VerifyPathControlledByUser( | 2380 file_util::VerifyPathControlledByUser( |
2253 base_dir_, text_file_, uid_, ok_gids_)); | 2381 base_dir_, text_file_, uid_, ok_gids_)); |
2254 EXPECT_TRUE( | 2382 EXPECT_TRUE( |
2255 file_util::VerifyPathControlledByUser( | 2383 file_util::VerifyPathControlledByUser( |
2256 sub_dir_, text_file_, uid_, ok_gids_)); | 2384 sub_dir_, text_file_, uid_, ok_gids_)); |
2257 | 2385 |
2258 // Make sub_dir_ world writable. | 2386 // Make sub_dir_ world writable. |
2259 ASSERT_NO_FATAL_FAILURE( | 2387 ASSERT_TRUE( |
2260 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u)); | 2388 file_util::ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u)); |
2261 EXPECT_FALSE( | 2389 EXPECT_FALSE( |
2262 file_util::VerifyPathControlledByUser( | 2390 file_util::VerifyPathControlledByUser( |
2263 base_dir_, sub_dir_, uid_, ok_gids_)); | 2391 base_dir_, sub_dir_, uid_, ok_gids_)); |
2264 EXPECT_FALSE( | 2392 EXPECT_FALSE( |
2265 file_util::VerifyPathControlledByUser( | 2393 file_util::VerifyPathControlledByUser( |
2266 base_dir_, text_file_, uid_, ok_gids_)); | 2394 base_dir_, text_file_, uid_, ok_gids_)); |
2267 EXPECT_FALSE( | 2395 EXPECT_FALSE( |
2268 file_util::VerifyPathControlledByUser( | 2396 file_util::VerifyPathControlledByUser( |
2269 sub_dir_, text_file_, uid_, ok_gids_)); | 2397 sub_dir_, text_file_, uid_, ok_gids_)); |
2270 | 2398 |
2271 // Make text_file_ world writable. | 2399 // Make text_file_ world writable. |
2272 ASSERT_NO_FATAL_FAILURE( | 2400 ASSERT_TRUE( |
2273 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u)); | 2401 file_util::ChangePosixFilePermissions(text_file_, S_IWOTH, 0u)); |
2274 EXPECT_FALSE( | 2402 EXPECT_FALSE( |
2275 file_util::VerifyPathControlledByUser( | 2403 file_util::VerifyPathControlledByUser( |
2276 base_dir_, sub_dir_, uid_, ok_gids_)); | 2404 base_dir_, sub_dir_, uid_, ok_gids_)); |
2277 EXPECT_FALSE( | 2405 EXPECT_FALSE( |
2278 file_util::VerifyPathControlledByUser( | 2406 file_util::VerifyPathControlledByUser( |
2279 base_dir_, text_file_, uid_, ok_gids_)); | 2407 base_dir_, text_file_, uid_, ok_gids_)); |
2280 EXPECT_FALSE( | 2408 EXPECT_FALSE( |
2281 file_util::VerifyPathControlledByUser( | 2409 file_util::VerifyPathControlledByUser( |
2282 sub_dir_, text_file_, uid_, ok_gids_)); | 2410 sub_dir_, text_file_, uid_, ok_gids_)); |
2283 | 2411 |
2284 // Make sub_dir_ non-world writable. | 2412 // Make sub_dir_ non-world writable. |
2285 ASSERT_NO_FATAL_FAILURE( | 2413 ASSERT_TRUE( |
2286 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH)); | 2414 file_util::ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH)); |
2287 EXPECT_FALSE( | 2415 EXPECT_FALSE( |
2288 file_util::VerifyPathControlledByUser( | 2416 file_util::VerifyPathControlledByUser( |
2289 base_dir_, sub_dir_, uid_, ok_gids_)); | 2417 base_dir_, sub_dir_, uid_, ok_gids_)); |
2290 EXPECT_FALSE( | 2418 EXPECT_FALSE( |
2291 file_util::VerifyPathControlledByUser( | 2419 file_util::VerifyPathControlledByUser( |
2292 base_dir_, text_file_, uid_, ok_gids_)); | 2420 base_dir_, text_file_, uid_, ok_gids_)); |
2293 EXPECT_FALSE( | 2421 EXPECT_FALSE( |
2294 file_util::VerifyPathControlledByUser( | 2422 file_util::VerifyPathControlledByUser( |
2295 sub_dir_, text_file_, uid_, ok_gids_)); | 2423 sub_dir_, text_file_, uid_, ok_gids_)); |
2296 | 2424 |
2297 // Make base_dir_ non-world-writable. | 2425 // Make base_dir_ non-world-writable. |
2298 ASSERT_NO_FATAL_FAILURE( | 2426 ASSERT_TRUE( |
2299 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH)); | 2427 file_util::ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH)); |
2300 EXPECT_TRUE( | 2428 EXPECT_TRUE( |
2301 file_util::VerifyPathControlledByUser( | 2429 file_util::VerifyPathControlledByUser( |
2302 base_dir_, sub_dir_, uid_, ok_gids_)); | 2430 base_dir_, sub_dir_, uid_, ok_gids_)); |
2303 EXPECT_FALSE( | 2431 EXPECT_FALSE( |
2304 file_util::VerifyPathControlledByUser( | 2432 file_util::VerifyPathControlledByUser( |
2305 base_dir_, text_file_, uid_, ok_gids_)); | 2433 base_dir_, text_file_, uid_, ok_gids_)); |
2306 EXPECT_FALSE( | 2434 EXPECT_FALSE( |
2307 file_util::VerifyPathControlledByUser( | 2435 file_util::VerifyPathControlledByUser( |
2308 sub_dir_, text_file_, uid_, ok_gids_)); | 2436 sub_dir_, text_file_, uid_, ok_gids_)); |
2309 | 2437 |
2310 // Back to the initial state: Nothing is writable, so every path | 2438 // Back to the initial state: Nothing is writable, so every path |
2311 // should pass. | 2439 // should pass. |
2312 ASSERT_NO_FATAL_FAILURE( | 2440 ASSERT_TRUE( |
2313 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH)); | 2441 file_util::ChangePosixFilePermissions(text_file_, 0u, S_IWOTH)); |
2314 EXPECT_TRUE( | 2442 EXPECT_TRUE( |
2315 file_util::VerifyPathControlledByUser( | 2443 file_util::VerifyPathControlledByUser( |
2316 base_dir_, sub_dir_, uid_, ok_gids_)); | 2444 base_dir_, sub_dir_, uid_, ok_gids_)); |
2317 EXPECT_TRUE( | 2445 EXPECT_TRUE( |
2318 file_util::VerifyPathControlledByUser( | 2446 file_util::VerifyPathControlledByUser( |
2319 base_dir_, text_file_, uid_, ok_gids_)); | 2447 base_dir_, text_file_, uid_, ok_gids_)); |
2320 EXPECT_TRUE( | 2448 EXPECT_TRUE( |
2321 file_util::VerifyPathControlledByUser( | 2449 file_util::VerifyPathControlledByUser( |
2322 sub_dir_, text_file_, uid_, ok_gids_)); | 2450 sub_dir_, text_file_, uid_, ok_gids_)); |
2323 } | 2451 } |
2324 | 2452 |
2325 #endif // defined(OS_POSIX) | 2453 #endif // defined(OS_POSIX) |
2326 | 2454 |
2327 } // namespace | 2455 } // namespace |
OLD | NEW |