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