test_file_util.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // Creates a a new unique directory and returns the generated path. The
  30. // directory will be automatically deleted when the test completes. Failure
  31. // upon creation or deletion will cause a test failure.
  32. FilePath CreateUniqueTempDirectoryScopedToTest();
  33. // Synchronize all the dirty pages from the page cache to disk (on POSIX
  34. // systems). The Windows analogy for this operation is to 'Flush file buffers'.
  35. // Note: This is currently implemented as a no-op on Windows.
  36. void SyncPageCacheToDisk();
  37. // Clear a specific file from the system cache. After this call, trying
  38. // to access this file will result in a cold load from the hard drive.
  39. bool EvictFileFromSystemCache(const FilePath& file);
  40. #if defined(OS_WIN)
  41. // Deny |permission| on the file |path| for the current user. |permission| is an
  42. // ACCESS_MASK structure which is defined in
  43. // https://msdn.microsoft.com/en-us/library/windows/desktop/aa374892.aspx
  44. // Refer to https://msdn.microsoft.com/en-us/library/aa822867.aspx for a list of
  45. // possible values.
  46. bool DenyFilePermission(const FilePath& path, DWORD permission);
  47. #endif // defined(OS_WIN)
  48. // For testing, make the file unreadable or unwritable.
  49. // In POSIX, this does not apply to the root user.
  50. bool MakeFileUnreadable(const FilePath& path) WARN_UNUSED_RESULT;
  51. bool MakeFileUnwritable(const FilePath& path) WARN_UNUSED_RESULT;
  52. // Saves the current permissions for a path, and restores it on destruction.
  53. class FilePermissionRestorer {
  54. public:
  55. explicit FilePermissionRestorer(const FilePath& path);
  56. ~FilePermissionRestorer();
  57. private:
  58. const FilePath path_;
  59. void* info_; // The opaque stored permission information.
  60. size_t length_; // The length of the stored permission information.
  61. DISALLOW_COPY_AND_ASSIGN(FilePermissionRestorer);
  62. };
  63. #if defined(OS_ANDROID)
  64. // Insert an image file into the MediaStore, and retrieve the content URI for
  65. // testing purpose.
  66. FilePath InsertImageIntoMediaStore(const FilePath& path);
  67. #endif // defined(OS_ANDROID)
  68. } // namespace base
  69. #endif // BASE_TEST_TEST_FILE_UTIL_H_