test_file_util.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <string>
  9. #include "base/compiler_specific.h"
  10. #include "base/files/file_path.h"
  11. #include "base/macros.h"
  12. #include "build/build_config.h"
  13. #if defined(OS_ANDROID)
  14. #include <jni.h>
  15. #endif
  16. #if defined(OS_WIN)
  17. #include <windows.h>
  18. #endif
  19. namespace base {
  20. // Clear a specific file from the system cache like EvictFileFromSystemCache,
  21. // but on failure it will sleep and retry. On the Windows buildbots, eviction
  22. // can fail if the file is marked in use, and this will throw off timings that
  23. // rely on uncached files.
  24. bool EvictFileFromSystemCacheWithRetry(const FilePath& file);
  25. // Wrapper over base::Delete. On Windows repeatedly invokes Delete in case
  26. // of failure to workaround Windows file locking semantics. Returns true on
  27. // success.
  28. bool DieFileDie(const FilePath& file, bool recurse);
  29. // Synchronize all the dirty pages from the page cache to disk (on POSIX
  30. // systems). The Windows analogy for this operation is to 'Flush file buffers'.
  31. // Note: This is currently implemented as a no-op on Windows.
  32. void SyncPageCacheToDisk();
  33. // Clear a specific file from the system cache. After this call, trying
  34. // to access this file will result in a cold load from the hard drive.
  35. bool EvictFileFromSystemCache(const FilePath& file);
  36. #if defined(OS_WIN)
  37. // Deny |permission| on the file |path| for the current user. |permission| is an
  38. // ACCESS_MASK structure which is defined in
  39. // https://msdn.microsoft.com/en-us/library/windows/desktop/aa374892.aspx
  40. // Refer to https://msdn.microsoft.com/en-us/library/aa822867.aspx for a list of
  41. // possible values.
  42. bool DenyFilePermission(const FilePath& path, DWORD permission);
  43. #endif // defined(OS_WIN)
  44. // For testing, make the file unreadable or unwritable.
  45. // In POSIX, this does not apply to the root user.
  46. bool MakeFileUnreadable(const FilePath& path) WARN_UNUSED_RESULT;
  47. bool MakeFileUnwritable(const FilePath& path) WARN_UNUSED_RESULT;
  48. // Saves the current permissions for a path, and restores it on destruction.
  49. class FilePermissionRestorer {
  50. public:
  51. explicit FilePermissionRestorer(const FilePath& path);
  52. ~FilePermissionRestorer();
  53. private:
  54. const FilePath path_;
  55. void* info_; // The opaque stored permission information.
  56. size_t length_; // The length of the stored permission information.
  57. DISALLOW_COPY_AND_ASSIGN(FilePermissionRestorer);
  58. };
  59. #if defined(OS_ANDROID)
  60. // Insert an image file into the MediaStore, and retrieve the content URI for
  61. // testing purpose.
  62. FilePath InsertImageIntoMediaStore(const FilePath& path);
  63. #endif // defined(OS_ANDROID)
  64. } // namespace base
  65. #endif // BASE_TEST_TEST_FILE_UTIL_H_