Index: base/file_util_unittest.cc |
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc |
index 55f2039e4519d23183898a8b7bda990f1860ba6a..acd2e102dbf37472d5e9451d8a537f58158ea893 100644 |
--- a/base/file_util_unittest.cc |
+++ b/base/file_util_unittest.cc |
@@ -111,23 +111,25 @@ bool DeleteReparsePoint(HANDLE source) { |
#endif |
#if defined(OS_POSIX) |
-// Provide a simple way to change the permissions bits on |path| in tests. |
-// ASSERT failures will return, but not stop the test. Caller should wrap |
-// calls to this function in ASSERT_NO_FATAL_FAILURE(). |
-void ChangePosixFilePermissions(const FilePath& path, |
- mode_t mode_bits_to_set, |
- mode_t mode_bits_to_clear) { |
- ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear) |
+bool ChangePosixFilePermissions(const FilePath& path, |
+ int mode_bits_to_set, |
+ int mode_bits_to_clear) { |
+ DCHECK((mode_bits_to_set & ~file_util::FILE_PERMISSION_MASK) == 0); |
+ DCHECK((mode_bits_to_clear & ~file_util::FILE_PERMISSION_MASK) == 0); |
+ DCHECK(!(mode_bits_to_set & mode_bits_to_clear)) |
<< "Can't set and clear the same bits."; |
- struct stat stat_buf; |
- ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf)); |
+ int mode; |
+ if (!file_util::GetPosixFilePermissions(path, &mode)) |
+ return false; |
+ |
+ mode |= mode_bits_to_set; |
+ mode &= ~mode_bits_to_clear; |
- mode_t updated_mode_bits = stat_buf.st_mode; |
- updated_mode_bits |= mode_bits_to_set; |
- updated_mode_bits &= ~mode_bits_to_clear; |
+ if (file_util::SetPosixFilePermissions(path, mode)) |
+ return false; |
- ASSERT_EQ(0, chmod(path.value().c_str(), updated_mode_bits)); |
+ return true; |
} |
#endif // defined(OS_POSIX) |
@@ -667,7 +669,6 @@ TEST_F(FileUtilTest, CreateAndReadSymlinks) { |
ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result)); |
} |
- |
// The following test of NormalizeFilePath() require that we create a symlink. |
// This can not be done on Windows before Vista. On Vista, creating a symlink |
// requires privilege "SeCreateSymbolicLinkPrivilege". |
@@ -784,6 +785,150 @@ TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) { |
// Make sure the symbolic link is deleted |
EXPECT_FALSE(file_util::IsLink(file_link)); |
} |
+ |
+TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) { |
+ // Create a file path |
+ FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
+ EXPECT_FALSE(file_util::PathExists(file_name)); |
+ |
+ char buffer[32] = "hello"; |
+ std::string data(buffer); |
+ |
+ // Write file |
+ EXPECT_EQ(static_cast<int>(data.length()), |
+ file_util::WriteFile(file_name, data.c_str(), data.length())); |
+ EXPECT_TRUE(file_util::PathExists(file_name)); |
+ |
+ // Meke sure the file is readable |
+ int32 mode; |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
+ EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER, |
+ mode & file_util::FILE_PERMISSION_READ_BY_USER); |
+ |
+ // Get rid of the read permission |
+ EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
+ EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_READ_BY_USER); |
+ // Make sure the file can't be read |
+ EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, sizeof(buffer))); |
+ |
+ // Give the read permission |
+ EXPECT_TRUE(file_util::SetPosixFilePermissions( |
+ file_name, |
+ file_util::FILE_PERMISSION_READ_BY_USER)); |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
+ EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER, |
+ mode & file_util::FILE_PERMISSION_READ_BY_USER); |
+ // Make sure the file can be read |
+ EXPECT_EQ(static_cast<int>(data.length()), |
+ file_util::ReadFile(file_name, buffer, sizeof(buffer))); |
+ |
+ // Delete the file |
+ EXPECT_TRUE(file_util::Delete(file_name, false)); |
+ EXPECT_FALSE(file_util::PathExists(file_name)); |
+} |
+ |
+TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { |
+ // Create a file path |
+ FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
+ EXPECT_FALSE(file_util::PathExists(file_name)); |
+ |
+ char buffer[32] = "hello"; |
+ std::string data(buffer); |
+ |
+ // Write file |
+ EXPECT_EQ(static_cast<int>(data.length()), |
+ file_util::WriteFile(file_name, data.c_str(), data.length())); |
+ EXPECT_TRUE(file_util::PathExists(file_name)); |
+ |
+ // Meke sure the file is writable |
+ int mode; |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
+ EXPECT_EQ(file_util::FILE_PERMISSION_WRITE_BY_USER, |
+ mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
+ EXPECT_TRUE(file_util::PathIsWritable(file_name)); |
+ |
+ // Get rid of the write permission |
+ EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
+ EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
+ // Make sure the file can't be write |
+ EXPECT_EQ(-1, |
+ file_util::WriteFile(file_name, data.c_str(), data.length())); |
+ EXPECT_FALSE(file_util::PathIsWritable(file_name)); |
+ |
+ // Give read permission |
+ EXPECT_TRUE(file_util::SetPosixFilePermissions( |
+ file_name, |
+ file_util::FILE_PERMISSION_WRITE_BY_USER)); |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); |
+ EXPECT_EQ(file_util::FILE_PERMISSION_WRITE_BY_USER, |
+ mode & file_util::FILE_PERMISSION_WRITE_BY_USER); |
+ // Make sure the file can be write |
+ EXPECT_EQ(static_cast<int>(data.length()), |
+ file_util::WriteFile(file_name, data.c_str(), data.length())); |
+ EXPECT_TRUE(file_util::PathIsWritable(file_name)); |
+ |
+ // Delete the file |
+ EXPECT_TRUE(file_util::Delete(file_name, false)); |
+ EXPECT_FALSE(file_util::PathExists(file_name)); |
+} |
+ |
+TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { |
+ // Create a directory path |
+ FilePath subdir_path = |
+ temp_dir_.path().Append(FPL("PermissionTest1")); |
+ file_util::CreateDirectory(subdir_path); |
+ ASSERT_TRUE(file_util::PathExists(subdir_path)); |
+ |
+ // Create a dummy file to enumerate |
+ FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); |
+ EXPECT_FALSE(file_util::PathExists(file_name)); |
+ char buffer[32] = "hello"; |
+ std::string data(buffer); |
+ EXPECT_EQ(static_cast<int>(data.length()), |
+ file_util::WriteFile(file_name, data.c_str(), data.length())); |
+ EXPECT_TRUE(file_util::PathExists(file_name)); |
+ |
+ // Meke sure the file is the all permissions |
+ int mode; |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
+ EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, |
+ mode & file_util::FILE_PERMISSION_USER_MASK); |
+ |
+ // Get rid of the permissions from the directory |
+ EXPECT_TRUE(file_util::SetPosixFilePermissions(subdir_path, 0u)); |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
+ EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_USER_MASK); |
+ |
+ // Make sure the file in the directory can't be enumerated. |
+ file_util::FileEnumerator f1(subdir_path, true, |
+ file_util::FileEnumerator::FILES); |
+ EXPECT_TRUE(file_util::PathExists(subdir_path)); |
+ FindResultCollector c1(f1); |
+ EXPECT_EQ(c1.size(), 0); |
+ EXPECT_FALSE(file_util::GetPosixFilePermissions(file_name, &mode)); |
+ |
+ // Give the permissions to the directory |
+ EXPECT_TRUE(file_util::SetPosixFilePermissions( |
+ subdir_path, |
+ file_util::FILE_PERMISSION_USER_MASK)); |
+ EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); |
+ EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, |
+ mode & file_util::FILE_PERMISSION_USER_MASK); |
+ |
+ // Make sure the file in the directory can be enumerated. |
+ file_util::FileEnumerator f2(subdir_path, true, |
+ file_util::FileEnumerator::FILES); |
+ FindResultCollector c2(f2); |
+ EXPECT_TRUE(c2.HasFile(file_name)); |
+ EXPECT_EQ(c2.size(), 1); |
+ |
+ // Delete the file |
+ EXPECT_TRUE(file_util::Delete(subdir_path, true)); |
+ EXPECT_FALSE(file_util::PathExists(subdir_path)); |
+} |
+ |
#endif // defined(OS_POSIX) |
#if defined(OS_WIN) |
@@ -1997,11 +2142,14 @@ class VerifyPathControlledByUserTest : public FileUtilTest { |
// of permissions to be different from what we expect, explicitly |
// set permissions on the directories we create. |
// Make all files and directories non-world-writable. |
- mode_t enabled_permissions = |
- S_IRWXU | // User can read, write, traverse |
- S_IRWXG; // Group can read, write, traverse |
- mode_t disabled_permissions = |
- S_IRWXO; // Other users can't read, write, traverse. |
+ |
+ // Users and group can read, write, traverse |
+ int enabled_permissions = |
+ file_util::FILE_PERMISSION_USER_MASK | |
+ file_util::FILE_PERMISSION_GROUP_MASK; |
+ // Other users can't read, write, traverse |
+ int disabled_permissions = |
+ file_util::FILE_PERMISSION_OTHERS_MASK; |
ASSERT_NO_FATAL_FAILURE( |
ChangePosixFilePermissions( |