Chromium Code Reviews| 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); |
|
satorux1
2012/07/10 00:06:54
the two lines look unnecessary, as we have a DCHEC
yoshiki
2012/07/10 01:31:17
Done.
|
| + 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; |
|
satorux1
2012/07/10 00:06:54
mode = 0; just in case.
yoshiki
2012/07/10 01:31:17
Done.
|
| + 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)) |
|
satorux1
2012/07/10 00:06:54
Is this correct? Shouldn't we have ! here?
if (!f
yoshiki
2012/07/10 01:31:17
Oops... Done.
On 2012/07/10 00:06:54, satorux1 wr
|
| + 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 |
|
satorux1
2012/07/10 00:06:54
period is missing.
yoshiki
2012/07/10 01:31:17
Done.
|
| + FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); |
| + EXPECT_FALSE(file_util::PathExists(file_name)); |
| + |
| + char buffer[32] = "hello"; |
|
satorux1
2012/07/10 00:06:54
remove 32. even better, remove this variable.
yoshiki
2012/07/10 01:31:17
Removed 32. But not remove the variable because it
|
| + std::string data(buffer); |
|
satorux1
2012/07/10 00:06:54
const std::string kData = "hello";
yoshiki
2012/07/10 01:31:17
Made it const and changed name, keeping the initia
satorux1
2012/07/10 06:57:17
Reusing 'buffer' at a later time for writing sound
yoshiki
2012/07/10 18:51:35
I think direct manipulation to buffer of std::stri
|
| + |
| + // 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; |
|
satorux1
2012/07/10 00:06:54
int mode = 0;
yoshiki
2012/07/10 01:31:17
Done.
|
| + 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); |
|
satorux1
2012/07/10 00:06:54
EXPECT_TRUE(mode & file_util::FILE_PERMISSION_READ
yoshiki
2012/07/10 01:31:17
Done.
|
| + |
| + // 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); |
|
satorux1
2012/07/10 00:06:54
EXPECT_FALSE(mode & file_util::FILE_PERMISSION_REA
yoshiki
2012/07/10 01:31:17
Done.
|
| + // Make sure the file can't be read |
| + EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, sizeof(buffer))); |
|
satorux1
2012/07/10 00:06:54
kData.data(), kData.size()
yoshiki
2012/07/10 01:31:17
string::data() returns a pointer of const char*, s
satorux1
2012/07/10 06:57:17
oops, you are right.
On 2012/07/10 01:31:17, yosh
|
| + |
| + // 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( |