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