diff options
author | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-02 18:36:33 +0000 |
---|---|---|
committer | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-02 18:36:33 +0000 |
commit | f36a813bcc5c1d7e86375e761e592cd1959117f8 (patch) | |
tree | ddb121d1b4fc035474abaab07c981cda822a8594 /base | |
parent | bfe3b9e2ec11565e3a1d90133d1a49d1fef1b817 (diff) | |
download | chromium_src-f36a813bcc5c1d7e86375e761e592cd1959117f8.zip chromium_src-f36a813bcc5c1d7e86375e761e592cd1959117f8.tar.gz chromium_src-f36a813bcc5c1d7e86375e761e592cd1959117f8.tar.bz2 |
Move test utilities into test_file_util.*.
Split from CL 7134019.
brettw: base.
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/7740081
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99416 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_util.h | 55 | ||||
-rw-r--r-- | base/files/file_path_watcher_browsertest.cc | 1 | ||||
-rw-r--r-- | base/test/test_file_util.h | 7 | ||||
-rw-r--r-- | base/test/test_file_util_posix.cc | 23 | ||||
-rw-r--r-- | base/test/test_file_util_win.cc | 49 |
5 files changed, 80 insertions, 55 deletions
diff --git a/base/file_util.h b/base/file_util.h index cd7fa9f..1010b63 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -13,9 +13,6 @@ #if defined(OS_WIN) #include <windows.h> -#if defined(UNIT_TEST) -#include <aclapi.h> -#endif #elif defined(OS_POSIX) #include <sys/stat.h> #include <unistd.h> @@ -577,58 +574,6 @@ BASE_EXPORT bool HasFileBeenModifiedSince( const FileEnumerator::FindInfo& find_info, const base::Time& cutoff_time); -#ifdef UNIT_TEST - -inline bool MakeFileUnreadable(const FilePath& path) { -#if defined(OS_POSIX) - struct stat stat_buf; - if (stat(path.value().c_str(), &stat_buf) != 0) - return false; - stat_buf.st_mode &= ~(S_IRUSR | S_IRGRP | S_IROTH); - - return chmod(path.value().c_str(), stat_buf.st_mode) == 0; - -#elif defined(OS_WIN) - PACL old_dacl; - PSECURITY_DESCRIPTOR security_descriptor; - if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), - SE_FILE_OBJECT, - DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl, - NULL, &security_descriptor) != ERROR_SUCCESS) - return false; - - // Deny Read access for the current user. - EXPLICIT_ACCESS change; - change.grfAccessPermissions = GENERIC_READ; - change.grfAccessMode = DENY_ACCESS; - change.grfInheritance = 0; - change.Trustee.pMultipleTrustee = NULL; - change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; - change.Trustee.TrusteeForm = TRUSTEE_IS_NAME; - change.Trustee.TrusteeType = TRUSTEE_IS_USER; - change.Trustee.ptstrName = L"CURRENT_USER"; - - PACL new_dacl; - if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) { - LocalFree(security_descriptor); - return false; - } - - DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), - SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, - NULL, NULL, new_dacl, NULL); - LocalFree(security_descriptor); - LocalFree(new_dacl); - - return rc == ERROR_SUCCESS; -#else - NOTIMPLEMENTED(); - return false; -#endif -} - -#endif // UNIT_TEST - #if defined(OS_WIN) // Loads the file passed in as an image section and touches pages to avoid // subsequent hard page faults during LoadLibrary. The size to be pre read diff --git a/base/files/file_path_watcher_browsertest.cc b/base/files/file_path_watcher_browsertest.cc index a422f84..8e5f361 100644 --- a/base/files/file_path_watcher_browsertest.cc +++ b/base/files/file_path_watcher_browsertest.cc @@ -24,6 +24,7 @@ #include "base/stl_util.h" #include "base/stringprintf.h" #include "base/synchronization/waitable_event.h" +#include "base/test/test_file_util.h" #include "base/test/test_timeouts.h" #include "base/threading/thread.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/base/test/test_file_util.h b/base/test/test_file_util.h index 9b9389d..ce0b302 100644 --- a/base/test/test_file_util.h +++ b/base/test/test_file_util.h @@ -10,6 +10,8 @@ #include <string> +#include "base/compiler_specific.h" + class FilePath; namespace file_util { @@ -49,6 +51,11 @@ bool HasInternetZoneIdentifier(const FilePath& full_path); std::wstring FilePathAsWString(const FilePath& path); FilePath WStringAsFilePath(const std::wstring& path); +// For testing, make the file unreadable or unwritable. +// In POSIX, this does not apply to the root user. +bool MakeFileUnreadable(const FilePath& path) WARN_UNUSED_RESULT; +bool MakeFileUnwritable(const FilePath& path) WARN_UNUSED_RESULT; + } // namespace file_util #endif // BASE_TEST_TEST_FILE_UTIL_H_ diff --git a/base/test/test_file_util_posix.cc b/base/test/test_file_util_posix.cc index b9485d0..cca17b59 100644 --- a/base/test/test_file_util_posix.cc +++ b/base/test/test_file_util_posix.cc @@ -19,6 +19,21 @@ namespace file_util { +namespace { + +// Deny |permission| on the file |path|. +bool DenyFilePermission(const FilePath& path, mode_t permission) { + struct stat stat_buf; + if (stat(path.value().c_str(), &stat_buf) != 0) + return false; + stat_buf.st_mode &= ~permission; + + int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode)); + return rv == 0; +} + +} // namespace + bool DieFileDie(const FilePath& file, bool recurse) { // There is no need to workaround Windows problems on POSIX. // Just pass-through. @@ -117,4 +132,12 @@ FilePath WStringAsFilePath(const std::wstring& path) { return FilePath(WideToUTF8(path)); } +bool MakeFileUnreadable(const FilePath& path) { + return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH); +} + +bool MakeFileUnwritable(const FilePath& path) { + return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH); +} + } // namespace file_util diff --git a/base/test/test_file_util_win.cc b/base/test/test_file_util_win.cc index cec2f51..90df710 100644 --- a/base/test/test_file_util_win.cc +++ b/base/test/test_file_util_win.cc @@ -4,6 +4,7 @@ #include "base/test/test_file_util.h" +#include <aclapi.h> #include <shlwapi.h> #include <windows.h> @@ -20,6 +21,46 @@ namespace file_util { static const ptrdiff_t kOneMB = 1024 * 1024; +namespace { + +// Deny |permission| on the file |path|, for the current user. +bool DenyFilePermission(const FilePath& path, DWORD permission) { + PACL old_dacl; + PSECURITY_DESCRIPTOR security_descriptor; + if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), + SE_FILE_OBJECT, + DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl, + NULL, &security_descriptor) != ERROR_SUCCESS) { + return false; + } + + EXPLICIT_ACCESS change; + change.grfAccessPermissions = permission; + change.grfAccessMode = DENY_ACCESS; + change.grfInheritance = 0; + change.Trustee.pMultipleTrustee = NULL; + change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; + change.Trustee.TrusteeForm = TRUSTEE_IS_NAME; + change.Trustee.TrusteeType = TRUSTEE_IS_USER; + change.Trustee.ptstrName = L"CURRENT_USER"; + + PACL new_dacl; + if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) { + LocalFree(security_descriptor); + return false; + } + + DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), + SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, + NULL, NULL, new_dacl, NULL); + LocalFree(security_descriptor); + LocalFree(new_dacl); + + return rc == ERROR_SUCCESS; +} + +} // namespace + bool DieFileDie(const FilePath& file, bool recurse) { // It turns out that to not induce flakiness a long timeout is needed. const int kTimeoutMs = 10000; @@ -227,4 +268,12 @@ FilePath WStringAsFilePath(const std::wstring& path) { return FilePath(path); } +bool MakeFileUnreadable(const FilePath& path) { + return DenyFilePermission(path, GENERIC_READ); +} + +bool MakeFileUnwritable(const FilePath& path) { + return DenyFilePermission(path, GENERIC_WRITE); +} + } // namespace file_util |