xref: /aosp_15_r20/external/libchrome/base/test/test_file_util.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TEST_TEST_FILE_UTIL_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_TEST_TEST_FILE_UTIL_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker // File utility functions used only by tests.
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include <string>
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/files/file_path.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
17*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
20*635a8641SAndroid Build Coastguard Worker #include <jni.h>
21*635a8641SAndroid Build Coastguard Worker #endif
22*635a8641SAndroid Build Coastguard Worker 
23*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
24*635a8641SAndroid Build Coastguard Worker #include <windows.h>
25*635a8641SAndroid Build Coastguard Worker #endif
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker namespace base {
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker class FilePath;
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker // Clear a specific file from the system cache like EvictFileFromSystemCache,
32*635a8641SAndroid Build Coastguard Worker // but on failure it will sleep and retry. On the Windows buildbots, eviction
33*635a8641SAndroid Build Coastguard Worker // can fail if the file is marked in use, and this will throw off timings that
34*635a8641SAndroid Build Coastguard Worker // rely on uncached files.
35*635a8641SAndroid Build Coastguard Worker bool EvictFileFromSystemCacheWithRetry(const FilePath& file);
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker // Wrapper over base::Delete. On Windows repeatedly invokes Delete in case
38*635a8641SAndroid Build Coastguard Worker // of failure to workaround Windows file locking semantics. Returns true on
39*635a8641SAndroid Build Coastguard Worker // success.
40*635a8641SAndroid Build Coastguard Worker bool DieFileDie(const FilePath& file, bool recurse);
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker // Clear a specific file from the system cache. After this call, trying
43*635a8641SAndroid Build Coastguard Worker // to access this file will result in a cold load from the hard drive.
44*635a8641SAndroid Build Coastguard Worker bool EvictFileFromSystemCache(const FilePath& file);
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
47*635a8641SAndroid Build Coastguard Worker // Deny |permission| on the file |path| for the current user. |permission| is an
48*635a8641SAndroid Build Coastguard Worker // ACCESS_MASK structure which is defined in
49*635a8641SAndroid Build Coastguard Worker // https://msdn.microsoft.com/en-us/library/windows/desktop/aa374892.aspx
50*635a8641SAndroid Build Coastguard Worker // Refer to https://msdn.microsoft.com/en-us/library/aa822867.aspx for a list of
51*635a8641SAndroid Build Coastguard Worker // possible values.
52*635a8641SAndroid Build Coastguard Worker bool DenyFilePermission(const FilePath& path, DWORD permission);
53*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker // For testing, make the file unreadable or unwritable.
56*635a8641SAndroid Build Coastguard Worker // In POSIX, this does not apply to the root user.
57*635a8641SAndroid Build Coastguard Worker bool MakeFileUnreadable(const FilePath& path) WARN_UNUSED_RESULT;
58*635a8641SAndroid Build Coastguard Worker bool MakeFileUnwritable(const FilePath& path) WARN_UNUSED_RESULT;
59*635a8641SAndroid Build Coastguard Worker 
60*635a8641SAndroid Build Coastguard Worker // Saves the current permissions for a path, and restores it on destruction.
61*635a8641SAndroid Build Coastguard Worker class FilePermissionRestorer {
62*635a8641SAndroid Build Coastguard Worker  public:
63*635a8641SAndroid Build Coastguard Worker   explicit FilePermissionRestorer(const FilePath& path);
64*635a8641SAndroid Build Coastguard Worker   ~FilePermissionRestorer();
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker  private:
67*635a8641SAndroid Build Coastguard Worker   const FilePath path_;
68*635a8641SAndroid Build Coastguard Worker   void* info_;  // The opaque stored permission information.
69*635a8641SAndroid Build Coastguard Worker   size_t length_;  // The length of the stored permission information.
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(FilePermissionRestorer);
72*635a8641SAndroid Build Coastguard Worker };
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
75*635a8641SAndroid Build Coastguard Worker // Insert an image file into the MediaStore, and retrieve the content URI for
76*635a8641SAndroid Build Coastguard Worker // testing purpose.
77*635a8641SAndroid Build Coastguard Worker FilePath InsertImageIntoMediaStore(const FilePath& path);
78*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_ANDROID)
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker }  // namespace base
81*635a8641SAndroid Build Coastguard Worker 
82*635a8641SAndroid Build Coastguard Worker #endif  // BASE_TEST_TEST_FILE_UTIL_H_
83