Home | History | Annotate | Download | only in test
      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 #if defined(OS_WIN)
     24 #include <windows.h>
     25 #endif
     26 
     27 namespace base {
     28 
     29 class FilePath;
     30 
     31 // Clear a specific file from the system cache like EvictFileFromSystemCache,
     32 // but on failure it will sleep and retry. On the Windows buildbots, eviction
     33 // can fail if the file is marked in use, and this will throw off timings that
     34 // rely on uncached files.
     35 bool EvictFileFromSystemCacheWithRetry(const FilePath& file);
     36 
     37 // Wrapper over base::Delete. On Windows repeatedly invokes Delete in case
     38 // of failure to workaround Windows file locking semantics. Returns true on
     39 // success.
     40 bool DieFileDie(const FilePath& file, bool recurse);
     41 
     42 // Clear a specific file from the system cache. After this call, trying
     43 // to access this file will result in a cold load from the hard drive.
     44 bool EvictFileFromSystemCache(const FilePath& file);
     45 
     46 #if defined(OS_WIN)
     47 // Deny |permission| on the file |path| for the current user. |permission| is an
     48 // ACCESS_MASK structure which is defined in
     49 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa374892.aspx
     50 // Refer to https://msdn.microsoft.com/en-us/library/aa822867.aspx for a list of
     51 // possible values.
     52 bool DenyFilePermission(const FilePath& path, DWORD permission);
     53 #endif  // defined(OS_WIN)
     54 
     55 // For testing, make the file unreadable or unwritable.
     56 // In POSIX, this does not apply to the root user.
     57 bool MakeFileUnreadable(const FilePath& path) WARN_UNUSED_RESULT;
     58 bool MakeFileUnwritable(const FilePath& path) WARN_UNUSED_RESULT;
     59 
     60 // Saves the current permissions for a path, and restores it on destruction.
     61 class FilePermissionRestorer {
     62  public:
     63   explicit FilePermissionRestorer(const FilePath& path);
     64   ~FilePermissionRestorer();
     65 
     66  private:
     67   const FilePath path_;
     68   void* info_;  // The opaque stored permission information.
     69   size_t length_;  // The length of the stored permission information.
     70 
     71   DISALLOW_COPY_AND_ASSIGN(FilePermissionRestorer);
     72 };
     73 
     74 #if defined(OS_ANDROID)
     75 // Insert an image file into the MediaStore, and retrieve the content URI for
     76 // testing purpose.
     77 FilePath InsertImageIntoMediaStore(const FilePath& path);
     78 #endif  // defined(OS_ANDROID)
     79 
     80 }  // namespace base
     81 
     82 #endif  // BASE_TEST_TEST_FILE_UTIL_H_
     83