15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_TEST_TEST_FILE_UTIL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_TEST_TEST_FILE_UTIL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// File utility functions used only by tests.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if defined(OS_ANDROID)
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <jni.h>
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/basictypes.h"
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FilePath;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Clear a specific file from the system cache like EvictFileFromSystemCache,
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// but on failure it will sleep and retry. On the Windows buildbots, eviction
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// can fail if the file is marked in use, and this will throw off timings that
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// rely on uncached files.
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool EvictFileFromSystemCacheWithRetry(const FilePath& file);
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Wrapper over base::Delete. On Windows repeatedly invokes Delete in case
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of failure to workaround Windows file locking semantics. Returns true on
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// success.
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool DieFileDie(const FilePath& file, bool recurse);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Clear a specific file from the system cache. After this call, trying
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to access this file will result in a cold load from the hard drive.
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool EvictFileFromSystemCache(const FilePath& file);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the volume supports Alternate Data Streams.
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool VolumeSupportsADS(const FilePath& path);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the ZoneIdentifier is correctly set to "Internet" (3).
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that this function must be called from the same process as
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the one that set the zone identifier.  I.e. don't use it in UI/automation
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// based tests.
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool HasInternetZoneIdentifier(const FilePath& full_path);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_WIN)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For testing, make the file unreadable or unwritable.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In POSIX, this does not apply to the root user.
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)bool MakeFileUnreadable(const FilePath& path) WARN_UNUSED_RESULT;
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)bool MakeFileUnwritable(const FilePath& path) WARN_UNUSED_RESULT;
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Saves the current permissions for a path, and restores it on destruction.
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class FilePermissionRestorer {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  explicit FilePermissionRestorer(const FilePath& path);
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ~FilePermissionRestorer();
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  const FilePath path_;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void* info_;  // The opaque stored permission information.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t length_;  // The length of the stored permission information.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FilePermissionRestorer);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(OS_ANDROID)
706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Register the ContentUriTestUrils JNI bindings.
716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)bool RegisterContentUriTestUtils(JNIEnv* env);
726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Insert an image file into the MediaStore, and retrieve the content URI for
746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// testing purpose.
756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)FilePath InsertImageIntoMediaStore(const FilePath& path);
766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif  // defined(OS_ANDROID)
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}  // namespace base
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_TEST_TEST_FILE_UTIL_H_
81