1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_TEST_TEST_FILE_UTIL_H_ 6*6777b538SAndroid Build Coastguard Worker #define BASE_TEST_TEST_FILE_UTIL_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker // File utility functions used only by tests. 9*6777b538SAndroid Build Coastguard Worker 10*6777b538SAndroid Build Coastguard Worker #include <stddef.h> 11*6777b538SAndroid Build Coastguard Worker 12*6777b538SAndroid Build Coastguard Worker #include "base/files/file_path.h" 13*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h" 14*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h" 15*6777b538SAndroid Build Coastguard Worker 16*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_ANDROID) 17*6777b538SAndroid Build Coastguard Worker #include <jni.h> 18*6777b538SAndroid Build Coastguard Worker #endif 19*6777b538SAndroid Build Coastguard Worker 20*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 21*6777b538SAndroid Build Coastguard Worker #include <windows.h> 22*6777b538SAndroid Build Coastguard Worker #endif 23*6777b538SAndroid Build Coastguard Worker 24*6777b538SAndroid Build Coastguard Worker namespace base { 25*6777b538SAndroid Build Coastguard Worker 26*6777b538SAndroid Build Coastguard Worker // Clear a specific file from the system cache like EvictFileFromSystemCache, 27*6777b538SAndroid Build Coastguard Worker // but on failure it will sleep and retry. On the Windows buildbots, eviction 28*6777b538SAndroid Build Coastguard Worker // can fail if the file is marked in use, and this will throw off timings that 29*6777b538SAndroid Build Coastguard Worker // rely on uncached files. 30*6777b538SAndroid Build Coastguard Worker bool EvictFileFromSystemCacheWithRetry(const FilePath& file); 31*6777b538SAndroid Build Coastguard Worker 32*6777b538SAndroid Build Coastguard Worker // Wrapper over base::Delete. On Windows repeatedly invokes Delete in case 33*6777b538SAndroid Build Coastguard Worker // of failure to workaround Windows file locking semantics. Returns true on 34*6777b538SAndroid Build Coastguard Worker // success. 35*6777b538SAndroid Build Coastguard Worker bool DieFileDie(const FilePath& file, bool recurse); 36*6777b538SAndroid Build Coastguard Worker 37*6777b538SAndroid Build Coastguard Worker // Convenience wrapper for `base::GetTempDir()` that returns the temp dir as a 38*6777b538SAndroid Build Coastguard Worker // `base::FilePath`. 39*6777b538SAndroid Build Coastguard Worker FilePath GetTempDirForTesting(); 40*6777b538SAndroid Build Coastguard Worker 41*6777b538SAndroid Build Coastguard Worker // Creates a a new unique directory and returns the generated path. The 42*6777b538SAndroid Build Coastguard Worker // directory will be automatically deleted when the test completes. Failure 43*6777b538SAndroid Build Coastguard Worker // upon creation or deletion will cause a test failure. 44*6777b538SAndroid Build Coastguard Worker FilePath CreateUniqueTempDirectoryScopedToTest(); 45*6777b538SAndroid Build Coastguard Worker 46*6777b538SAndroid Build Coastguard Worker // Synchronize all the dirty pages from the page cache to disk (on POSIX 47*6777b538SAndroid Build Coastguard Worker // systems). The Windows analogy for this operation is to 'Flush file buffers'. 48*6777b538SAndroid Build Coastguard Worker // Note: This is currently implemented as a no-op on Windows. 49*6777b538SAndroid Build Coastguard Worker void SyncPageCacheToDisk(); 50*6777b538SAndroid Build Coastguard Worker 51*6777b538SAndroid Build Coastguard Worker // Clear a specific file from the system cache. After this call, trying 52*6777b538SAndroid Build Coastguard Worker // to access this file will result in a cold load from the hard drive. 53*6777b538SAndroid Build Coastguard Worker bool EvictFileFromSystemCache(const FilePath& file); 54*6777b538SAndroid Build Coastguard Worker 55*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 56*6777b538SAndroid Build Coastguard Worker // Deny |permission| on the file |path| for the current user. |permission| is an 57*6777b538SAndroid Build Coastguard Worker // ACCESS_MASK structure which is defined in 58*6777b538SAndroid Build Coastguard Worker // https://msdn.microsoft.com/en-us/library/windows/desktop/aa374892.aspx 59*6777b538SAndroid Build Coastguard Worker // Refer to https://msdn.microsoft.com/en-us/library/aa822867.aspx for a list of 60*6777b538SAndroid Build Coastguard Worker // possible values. 61*6777b538SAndroid Build Coastguard Worker bool DenyFilePermission(const FilePath& path, DWORD permission); 62*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_WIN) 63*6777b538SAndroid Build Coastguard Worker 64*6777b538SAndroid Build Coastguard Worker // For testing, make the file unreadable or unwritable. 65*6777b538SAndroid Build Coastguard Worker // In POSIX, this does not apply to the root user. 66*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool MakeFileUnreadable(const FilePath& path); 67*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool MakeFileUnwritable(const FilePath& path); 68*6777b538SAndroid Build Coastguard Worker 69*6777b538SAndroid Build Coastguard Worker // Saves the current permissions for a path, and restores it on destruction. 70*6777b538SAndroid Build Coastguard Worker class FilePermissionRestorer { 71*6777b538SAndroid Build Coastguard Worker public: 72*6777b538SAndroid Build Coastguard Worker explicit FilePermissionRestorer(const FilePath& path); 73*6777b538SAndroid Build Coastguard Worker 74*6777b538SAndroid Build Coastguard Worker FilePermissionRestorer(const FilePermissionRestorer&) = delete; 75*6777b538SAndroid Build Coastguard Worker FilePermissionRestorer& operator=(const FilePermissionRestorer&) = delete; 76*6777b538SAndroid Build Coastguard Worker 77*6777b538SAndroid Build Coastguard Worker ~FilePermissionRestorer(); 78*6777b538SAndroid Build Coastguard Worker 79*6777b538SAndroid Build Coastguard Worker private: 80*6777b538SAndroid Build Coastguard Worker const FilePath path_; 81*6777b538SAndroid Build Coastguard Worker raw_ptr<void, DanglingUntriaged> 82*6777b538SAndroid Build Coastguard Worker info_; // The opaque stored permission information. 83*6777b538SAndroid Build Coastguard Worker size_t length_; // The length of the stored permission information. 84*6777b538SAndroid Build Coastguard Worker }; 85*6777b538SAndroid Build Coastguard Worker 86*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_ANDROID) 87*6777b538SAndroid Build Coastguard Worker // Insert an image file into the MediaStore, and retrieve the content URI for 88*6777b538SAndroid Build Coastguard Worker // testing purpose. 89*6777b538SAndroid Build Coastguard Worker FilePath InsertImageIntoMediaStore(const FilePath& path); 90*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_ANDROID) 91*6777b538SAndroid Build Coastguard Worker 92*6777b538SAndroid Build Coastguard Worker } // namespace base 93*6777b538SAndroid Build Coastguard Worker 94*6777b538SAndroid Build Coastguard Worker #endif // BASE_TEST_TEST_FILE_UTIL_H_ 95