Index: base/file_util_unittest.cc |
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc |
index c44b804f00949c85b4fd877c261e85f45a9155a6..9c11e792760b43ca1da1a84cfc84bbdd40a1bb15 100644 |
--- a/base/file_util_unittest.cc |
+++ b/base/file_util_unittest.cc |
@@ -110,6 +110,24 @@ bool DeleteReparsePoint(HANDLE source) { |
} |
#endif |
+#if defined (OS_POSIX) |
Evan Martin
2011/08/24 22:38:53
no space after defined
Sam Kerner (Chrome)
2011/08/25 14:04:37
Done.
|
+void ChangePosixFilePermissions(const FilePath& path, |
Evan Martin
2011/08/24 22:38:53
can you add doc comments?
Sam Kerner (Chrome)
2011/08/25 14:04:37
Done.
|
+ mode_t mode_bits_to_set, |
+ mode_t mode_bits_to_clear) { |
+ ASSERT_EQ(0, mode_bits_to_set & mode_bits_to_clear) |
+ << "Can't set and clear the same bit."; |
+ |
+ struct stat stat_buf; |
+ ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf)); |
+ |
+ mode_t new_mode_bits = stat_buf.st_mode; |
+ new_mode_bits |= mode_bits_to_set; |
+ new_mode_bits &= ~mode_bits_to_clear; |
+ |
+ ASSERT_EQ(0, chmod(path.value().c_str(), new_mode_bits)); |
Evan Martin
2011/08/24 22:38:53
ASSERT doesn't owrk in helper functions unless you
Sam Kerner (Chrome)
2011/08/25 14:04:37
Done.
|
+} |
+#endif // defined (OS_POSIX) |
Evan Martin
2011/08/24 22:38:53
no space after defined
Sam Kerner (Chrome)
2011/08/25 14:04:37
Done.
|
+ |
const wchar_t bogus_content[] = L"I'm cannon fodder."; |
const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = |
@@ -1813,4 +1831,96 @@ TEST_F(FileUtilTest, IsDirectoryEmpty) { |
EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir)); |
} |
+#if defined(OS_POSIX) |
+TEST_F(FileUtilTest, IsPathControledByAdmin) { |
+ // Testing IsPathControlledByAdmin() is hard, because there is no |
+ // way a test can make a file owned by root, or change file paths |
+ // at the root of the file system. IsPathControlledByAdmin() |
+ // is implemented as a call to IsPathControlledByUser, which gives |
+ // us the ability to test with paths under the test's temp directory, |
+ // using a user id we control. |
Evan Martin
2011/08/24 22:38:53
This comment is good. I'd rename the test to say
Sam Kerner (Chrome)
2011/08/25 14:04:37
Done.
|
+ |
+ FilePath base_dir = temp_dir_.path().AppendASCII("base_dir"); |
+ ASSERT_TRUE(file_util::CreateDirectory(base_dir)); |
+ |
+ FilePath sub_dir = base_dir.AppendASCII("sub_dir"); |
+ ASSERT_TRUE(file_util::CreateDirectory(sub_dir)); |
+ |
+ FilePath text_file = sub_dir.AppendASCII("file.txt"); |
+ CreateTextFile(text_file, L"This text file has some text in it."); |
+ |
+ // Get our uid, and another uid, so that we can test both a |
+ // matching and non-matching uid. |
+ uid_t our_uid = getuid(); |
+ uid_t not_our_uid = our_uid + 1; |
+ |
+ // Make all files and directories non-world-writable. |
+ ChangePosixFilePermissions(base_dir, 0u, S_IWOTH); |
+ ChangePosixFilePermissions(sub_dir, 0u, S_IWOTH); |
+ ChangePosixFilePermissions(text_file, 0u, S_IWOTH); |
+ |
+ // We control these paths. |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
+ |
+ // Another user does not control these paths. |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(base_dir, sub_dir, not_our_uid )); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(base_dir, text_file, not_our_uid)); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(sub_dir, text_file, not_our_uid)); |
+ |
+ // Make base_dir world-writable. No change, because the base dir should |
+ // not be tested. |
+ ChangePosixFilePermissions(base_dir, S_IWOTH, 0u); |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
+ |
+ // Make sub_dir world writable. |
+ ChangePosixFilePermissions(sub_dir, S_IWOTH, 0u); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
+ |
+ // Make text_file world writable. |
+ ChangePosixFilePermissions(text_file, S_IWOTH, 0u); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
+ |
+ // Make sub_dir non-world writable. |
+ ChangePosixFilePermissions(sub_dir, 0u, S_IWOTH); |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
+ |
+ // Make base_dir non-world-writable. |
+ ChangePosixFilePermissions(base_dir, 0u, S_IWOTH); |
+ ASSERT_TRUE( |
+ file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
+ ASSERT_FALSE( |
+ file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
+} |
+#endif // defined(OS_POSIX) |
TVL
2011/08/25 14:07:28
i tend to include tests of bad inputs to make sure
Sam Kerner (Chrome)
2011/08/26 19:59:15
Lots of tests added, including all these cases.
|
+ |
} // namespace |