• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 <string>
13 
14 #include "base/compiler_specific.h"
15 #include "base/files/file_path.h"
16 #include "base/macros.h"
17 #include "build/build_config.h"
18 
19 #if defined(OS_ANDROID)
20 #include <jni.h>
21 #endif
22 
23 namespace base {
24 
25 class FilePath;
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 // Clear a specific file from the system cache. After this call, trying
39 // to access this file will result in a cold load from the hard drive.
40 bool EvictFileFromSystemCache(const FilePath& file);
41 
42 #if defined(OS_WIN)
43 // Returns true if the volume supports Alternate Data Streams.
44 bool VolumeSupportsADS(const FilePath& path);
45 
46 // Returns true if the ZoneIdentifier is correctly set to "Internet" (3).
47 // Note that this function must be called from the same process as
48 // the one that set the zone identifier.  I.e. don't use it in UI/automation
49 // based tests.
50 bool HasInternetZoneIdentifier(const FilePath& full_path);
51 #endif  // defined(OS_WIN)
52 
53 // For testing, make the file unreadable or unwritable.
54 // In POSIX, this does not apply to the root user.
55 bool MakeFileUnreadable(const FilePath& path) WARN_UNUSED_RESULT;
56 bool MakeFileUnwritable(const FilePath& path) WARN_UNUSED_RESULT;
57 
58 // Saves the current permissions for a path, and restores it on destruction.
59 class FilePermissionRestorer {
60  public:
61   explicit FilePermissionRestorer(const FilePath& path);
62   ~FilePermissionRestorer();
63 
64  private:
65   const FilePath path_;
66   void* info_;  // The opaque stored permission information.
67   size_t length_;  // The length of the stored permission information.
68 
69   DISALLOW_COPY_AND_ASSIGN(FilePermissionRestorer);
70 };
71 
72 #if defined(OS_ANDROID)
73 // Register the ContentUriTestUrils JNI bindings.
74 bool RegisterContentUriTestUtils(JNIEnv* env);
75 
76 // Insert an image file into the MediaStore, and retrieve the content URI for
77 // testing purpose.
78 FilePath InsertImageIntoMediaStore(const FilePath& path);
79 #endif  // defined(OS_ANDROID)
80 
81 }  // namespace base
82 
83 #endif  // BASE_TEST_TEST_FILE_UTIL_H_
84