test_file_util.h 3.1 KB

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