This source file includes following definitions.
- SetReparsePoint
- DeleteReparsePoint
- IsValid
- ChangePosixFilePermissions
- SetUp
- HasFile
- size
- CreateTextFile
- ReadTextFile
- FileTimeAsUint64
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SetReadOnly
- IsReadOnly
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST
- CloseWithScopedFD
- TEST
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <tchar.h>
#include <winioctl.h>
#endif
#if defined(OS_POSIX)
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <algorithm>
#include <fstream>
#include <set>
#include "base/base_paths.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_file_util.h"
#include "base/threading/platform_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#if defined(OS_WIN)
#include "base/win/scoped_handle.h"
#include "base/win/windows_version.h"
#endif
#if defined(OS_ANDROID)
#include "base/android/content_uri_utils.h"
#endif
#define FPL(x) FILE_PATH_LITERAL(x)
namespace base {
namespace {
#if defined(OS_WIN)
typedef struct _REPARSE_DATA_BUFFER {
ULONG ReparseTag;
USHORT ReparseDataLength;
USHORT Reserved;
union {
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
ULONG Flags;
WCHAR PathBuffer[1];
} SymbolicLinkReparseBuffer;
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
WCHAR PathBuffer[1];
} MountPointReparseBuffer;
struct {
UCHAR DataBuffer[1];
} GenericReparseBuffer;
};
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
std::wstring kPathPrefix = L"\\??\\";
std::wstring target_str;
if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
target_str += kPathPrefix;
target_str += target_path.value();
const wchar_t* target = target_str.c_str();
USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
char buffer[2000] = {0};
DWORD returned;
REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
data->ReparseTag = 0xa0000003;
memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
data->MountPointReparseBuffer.SubstituteNameLength = size_target;
data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
data->ReparseDataLength = size_target + 4 + 8;
int data_size = data->ReparseDataLength + 8;
if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
NULL, 0, &returned, NULL)) {
return false;
}
return true;
}
bool DeleteReparsePoint(HANDLE source) {
DWORD returned;
REPARSE_DATA_BUFFER data = {0};
data.ReparseTag = 0xa0000003;
if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
&returned, NULL)) {
return false;
}
return true;
}
class ReparsePoint {
public:
ReparsePoint(const FilePath& source, const FilePath& target) {
dir_.Set(
::CreateFile(source.value().c_str(),
FILE_ALL_ACCESS,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL));
created_ = dir_.IsValid() && SetReparsePoint(dir_, target);
}
~ReparsePoint() {
if (created_)
DeleteReparsePoint(dir_);
}
bool IsValid() { return created_; }
private:
win::ScopedHandle dir_;
bool created_;
DISALLOW_COPY_AND_ASSIGN(ReparsePoint);
};
#endif
#if defined(OS_POSIX)
void ChangePosixFilePermissions(const FilePath& path,
int mode_bits_to_set,
int mode_bits_to_clear) {
ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
<< "Can't set and clear the same bits.";
int mode = 0;
ASSERT_TRUE(GetPosixFilePermissions(path, &mode));
mode |= mode_bits_to_set;
mode &= ~mode_bits_to_clear;
ASSERT_TRUE(SetPosixFilePermissions(path, mode));
}
#endif
const wchar_t bogus_content[] = L"I'm cannon fodder.";
const int FILES_AND_DIRECTORIES =
FileEnumerator::FILES | FileEnumerator::DIRECTORIES;
class FileUtilTest : public PlatformTest {
protected:
virtual void SetUp() OVERRIDE {
PlatformTest::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
}
ScopedTempDir temp_dir_;
};
class FindResultCollector {
public:
explicit FindResultCollector(FileEnumerator& enumerator) {
FilePath cur_file;
while (!(cur_file = enumerator.Next()).value().empty()) {
FilePath::StringType path = cur_file.value();
EXPECT_TRUE(files_.end() == files_.find(path))
<< "Same file returned twice";
files_.insert(path);
}
}
bool HasFile(const FilePath& file) const {
return files_.find(file.value()) != files_.end();
}
int size() {
return static_cast<int>(files_.size());
}
private:
std::set<FilePath::StringType> files_;
};
void CreateTextFile(const FilePath& filename,
const std::wstring& contents) {
std::wofstream file;
file.open(filename.value().c_str());
ASSERT_TRUE(file.is_open());
file << contents;
file.close();
}
std::wstring ReadTextFile(const FilePath& filename) {
wchar_t contents[64];
std::wifstream file;
file.open(filename.value().c_str());
EXPECT_TRUE(file.is_open());
file.getline(contents, arraysize(contents));
file.close();
return std::wstring(contents);
}
#if defined(OS_WIN)
uint64 FileTimeAsUint64(const FILETIME& ft) {
ULARGE_INTEGER u;
u.LowPart = ft.dwLowDateTime;
u.HighPart = ft.dwHighDateTime;
return u.QuadPart;
}
#endif
TEST_F(FileUtilTest, FileAndDirectorySize) {
FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
CreateTextFile(file_01, L"12345678901234567890");
int64 size_f1 = 0;
ASSERT_TRUE(GetFileSize(file_01, &size_f1));
EXPECT_EQ(20ll, size_f1);
FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
CreateDirectory(subdir_path);
FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
CreateTextFile(file_02, L"123456789012345678901234567890");
int64 size_f2 = 0;
ASSERT_TRUE(GetFileSize(file_02, &size_f2));
EXPECT_EQ(30ll, size_f2);
FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
CreateDirectory(subsubdir_path);
FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
CreateTextFile(file_03, L"123");
int64 computed_size = ComputeDirectorySize(temp_dir_.path());
EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
}
TEST_F(FileUtilTest, NormalizeFilePathBasic) {
FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
FilePath file_b_path = dir_path.Append(FPL("file_b"));
CreateDirectory(dir_path);
FilePath normalized_file_a_path, normalized_file_b_path;
ASSERT_FALSE(PathExists(file_a_path));
ASSERT_FALSE(NormalizeFilePath(file_a_path, &normalized_file_a_path))
<< "NormalizeFilePath() should fail on nonexistent paths.";
CreateTextFile(file_a_path, bogus_content);
ASSERT_TRUE(PathExists(file_a_path));
ASSERT_TRUE(NormalizeFilePath(file_a_path, &normalized_file_a_path));
CreateTextFile(file_b_path, bogus_content);
ASSERT_TRUE(PathExists(file_b_path));
ASSERT_TRUE(NormalizeFilePath(file_b_path, &normalized_file_b_path));
ASSERT_TRUE(normalized_file_a_path.DirName()
.IsParent(normalized_file_b_path.DirName()));
}
#if defined(OS_WIN)
TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
ASSERT_TRUE(CreateDirectory(base_a));
FilePath sub_a = base_a.Append(FPL("sub_a"));
ASSERT_TRUE(CreateDirectory(sub_a));
FilePath file_txt = sub_a.Append(FPL("file.txt"));
CreateTextFile(file_txt, bogus_content);
FilePath sub_long_rel(FPL("sub_long"));
FilePath deep_txt(FPL("deep.txt"));
int target_length = MAX_PATH;
target_length -= (sub_a.value().length() + 1);
target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
const int kCreateDirLimit = 4;
target_length -= kCreateDirLimit;
FilePath::StringType long_name_str = FPL("long_name_");
long_name_str.resize(target_length, '_');
FilePath long_name = sub_a.Append(FilePath(long_name_str));
FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
FilePath sub_long = deep_file.DirName();
ASSERT_TRUE(CreateDirectory(sub_long));
CreateTextFile(deep_file, bogus_content);
FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
ASSERT_TRUE(CreateDirectory(base_b));
FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
ASSERT_TRUE(CreateDirectory(to_sub_a));
FilePath normalized_path;
{
ReparsePoint reparse_to_sub_a(to_sub_a, sub_a);
ASSERT_TRUE(reparse_to_sub_a.IsValid());
FilePath to_base_b = base_b.Append(FPL("to_base_b"));
ASSERT_TRUE(CreateDirectory(to_base_b));
ReparsePoint reparse_to_base_b(to_base_b, base_b);
ASSERT_TRUE(reparse_to_base_b.IsValid());
FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
ASSERT_TRUE(CreateDirectory(to_sub_long));
ReparsePoint reparse_to_sub_long(to_sub_long, sub_long);
ASSERT_TRUE(reparse_to_sub_long.IsValid());
ASSERT_TRUE(NormalizeFilePath(file_txt, &normalized_path));
ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
ASSERT_TRUE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
&normalized_path));
ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
ASSERT_TRUE(NormalizeFilePath(base_b.Append(FPL("to_base_b"))
.Append(FPL("to_base_b"))
.Append(FPL("to_sub_a"))
.Append(FPL("file.txt")),
&normalized_path));
ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
FilePath long_path = base_b;
const int kLengthLimit = MAX_PATH + 200;
while (long_path.value().length() <= kLengthLimit) {
long_path = long_path.Append(FPL("to_base_b"));
}
long_path = long_path.Append(FPL("to_sub_a"))
.Append(FPL("file.txt"));
ASSERT_FALSE(NormalizeFilePath(long_path, &normalized_path));
ASSERT_FALSE(NormalizeFilePath(to_sub_long.Append(deep_txt),
&normalized_path));
}
ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
&normalized_path));
}
TEST_F(FileUtilTest, DevicePathToDriveLetter) {
std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
LOG(ERROR) << "Can't get a drive letter to test with.";
return;
}
wchar_t device_path[MAX_PATH] = {'\0'};
ASSERT_TRUE(
::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
FilePath actual_device_path(device_path);
FilePath win32_path;
ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path, &win32_path));
ASSERT_EQ(real_drive_letter, win32_path.value());
FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
ASSERT_TRUE(DevicePathToDriveLetterPath(
actual_device_path.Append(kRelativePath),
&win32_path));
EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
win32_path.value());
int path_length = actual_device_path.value().length();
int new_length = path_length - 4;
ASSERT_LT(0, new_length);
FilePath prefix_of_real_device_path(
actual_device_path.value().substr(0, new_length));
ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path,
&win32_path));
ASSERT_FALSE(DevicePathToDriveLetterPath(
prefix_of_real_device_path.Append(kRelativePath),
&win32_path));
const FilePath::StringType kExtraChars = FPL("12345");
FilePath real_device_path_plus_numbers(
actual_device_path.value() + kExtraChars);
ASSERT_FALSE(DevicePathToDriveLetterPath(
real_device_path_plus_numbers,
&win32_path));
ASSERT_FALSE(DevicePathToDriveLetterPath(
real_device_path_plus_numbers.Append(kRelativePath),
&win32_path));
}
TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
const FilePath::CharType kLongDirName[] = FPL("A long path");
const FilePath::CharType kTestSubDirName[] = FPL("test");
FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
ASSERT_TRUE(CreateDirectory(long_test_dir));
WCHAR path_buffer[MAX_PATH];
DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
path_buffer, MAX_PATH);
ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
ASSERT_NE(DWORD(0), path_buffer_length);
FilePath short_test_dir(path_buffer);
ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
FilePath temp_file;
ASSERT_TRUE(CreateTemporaryFileInDir(short_test_dir, &temp_file));
EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
EXPECT_TRUE(PathExists(temp_file));
FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
ASSERT_TRUE(CreateDirectory(access_test_dir));
file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
ASSERT_TRUE(CreateTemporaryFileInDir(
short_test_dir.Append(kTestSubDirName), &temp_file));
EXPECT_TRUE(PathExists(temp_file));
EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
path_buffer_length = GetLongPathName(temp_file.value().c_str(),
path_buffer, MAX_PATH);
EXPECT_EQ(DWORD(0), path_buffer_length);
}
#endif
#if defined(OS_POSIX)
TEST_F(FileUtilTest, CreateAndReadSymlinks) {
FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
CreateTextFile(link_to, bogus_content);
ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
<< "Failed to create file symlink.";
std::wstring contents = ReadTextFile(link_from);
EXPECT_EQ(bogus_content, contents);
FilePath result;
ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
EXPECT_EQ(link_to.value(), result.value());
link_from = temp_dir_.path().Append(FPL("from_dir"));
link_to = temp_dir_.path().Append(FPL("to_dir"));
ASSERT_TRUE(CreateDirectory(link_to));
ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
<< "Failed to create directory symlink.";
EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
FilePath missing = temp_dir_.path().Append(FPL("missing"));
EXPECT_FALSE(ReadSymbolicLink(missing, &result));
}
TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
CreateTextFile(link_to, bogus_content);
ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
<< "Failed to create file symlink.";
FilePath normalized_path;
ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
EXPECT_NE(link_from, link_to);
EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
link_from = temp_dir_.path().Append(FPL("from_dir"));
link_to = temp_dir_.path().Append(FPL("to_dir"));
ASSERT_TRUE(CreateDirectory(link_to));
ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
<< "Failed to create directory symlink.";
EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
<< "Links to directories should return false.";
link_from = temp_dir_.path().Append(FPL("link_a"));
link_to = temp_dir_.path().Append(FPL("link_b"));
ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
<< "Failed to create loop symlink a.";
ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
<< "Failed to create loop symlink b.";
EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
}
#endif
TEST_F(FileUtilTest, DeleteNonExistent) {
FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
ASSERT_FALSE(PathExists(non_existent));
EXPECT_TRUE(DeleteFile(non_existent, false));
ASSERT_FALSE(PathExists(non_existent));
EXPECT_TRUE(DeleteFile(non_existent, true));
ASSERT_FALSE(PathExists(non_existent));
}
TEST_F(FileUtilTest, DeleteFile) {
FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
CreateTextFile(file_name, bogus_content);
ASSERT_TRUE(PathExists(file_name));
EXPECT_TRUE(DeleteFile(file_name, false));
EXPECT_FALSE(PathExists(file_name));
file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
CreateTextFile(file_name, bogus_content);
ASSERT_TRUE(PathExists(file_name));
EXPECT_TRUE(DeleteFile(file_name, true));
EXPECT_FALSE(PathExists(file_name));
}
#if defined(OS_POSIX)
TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
CreateTextFile(file_name, bogus_content);
ASSERT_TRUE(PathExists(file_name));
FilePath file_link = temp_dir_.path().Append("file_link_2");
ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
<< "Failed to create symlink.";
EXPECT_TRUE(DeleteFile(file_link, false));
EXPECT_FALSE(PathExists(file_link));
EXPECT_TRUE(PathExists(file_name));
}
TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
EXPECT_FALSE(PathExists(non_existent));
FilePath file_link = temp_dir_.path().Append("file_link_3");
ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
<< "Failed to create symlink.";
EXPECT_TRUE(IsLink(file_link));
EXPECT_FALSE(PathExists(file_link));
EXPECT_TRUE(DeleteFile(file_link, false));
EXPECT_FALSE(IsLink(file_link));
}
TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
EXPECT_FALSE(PathExists(file_name));
const std::string kData("hello");
int buffer_size = kData.length();
char* buffer = new char[buffer_size];
EXPECT_EQ(static_cast<int>(kData.length()),
WriteFile(file_name, kData.data(), kData.length()));
EXPECT_TRUE(PathExists(file_name));
int32 mode = 0;
EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size));
EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
EXPECT_EQ(static_cast<int>(kData.length()),
ReadFile(file_name, buffer, buffer_size));
EXPECT_TRUE(DeleteFile(file_name, false));
EXPECT_FALSE(PathExists(file_name));
delete[] buffer;
}
TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
EXPECT_FALSE(PathExists(file_name));
const std::string kData("hello");
EXPECT_EQ(static_cast<int>(kData.length()),
WriteFile(file_name, kData.data(), kData.length()));
EXPECT_TRUE(PathExists(file_name));
int mode = 0;
EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
EXPECT_TRUE(PathIsWritable(file_name));
EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length()));
EXPECT_FALSE(PathIsWritable(file_name));
EXPECT_TRUE(SetPosixFilePermissions(file_name,
FILE_PERMISSION_WRITE_BY_USER));
EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
EXPECT_EQ(static_cast<int>(kData.length()),
WriteFile(file_name, kData.data(), kData.length()));
EXPECT_TRUE(PathIsWritable(file_name));
EXPECT_TRUE(DeleteFile(file_name, false));
EXPECT_FALSE(PathExists(file_name));
}
TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
FilePath subdir_path =
temp_dir_.path().Append(FPL("PermissionTest1"));
CreateDirectory(subdir_path);
ASSERT_TRUE(PathExists(subdir_path));
FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
EXPECT_FALSE(PathExists(file_name));
const std::string kData("hello");
EXPECT_EQ(static_cast<int>(kData.length()),
WriteFile(file_name, kData.data(), kData.length()));
EXPECT_TRUE(PathExists(file_name));
int mode = 0;
EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
EXPECT_TRUE(PathExists(subdir_path));
FindResultCollector c1(f1);
EXPECT_EQ(c1.size(), 0);
EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
FindResultCollector c2(f2);
EXPECT_TRUE(c2.HasFile(file_name));
EXPECT_EQ(c2.size(), 1);
EXPECT_TRUE(DeleteFile(subdir_path, true));
EXPECT_FALSE(PathExists(subdir_path));
}
#endif
#if defined(OS_WIN)
TEST_F(FileUtilTest, DeleteWildCard) {
FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
CreateTextFile(file_name, bogus_content);
ASSERT_TRUE(PathExists(file_name));
FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
CreateDirectory(subdir_path);
ASSERT_TRUE(PathExists(subdir_path));
FilePath directory_contents = temp_dir_.path();
directory_contents = directory_contents.Append(FPL("*"));
EXPECT_TRUE(DeleteFile(directory_contents, false));
EXPECT_FALSE(PathExists(file_name));
EXPECT_TRUE(PathExists(subdir_path));
EXPECT_TRUE(DeleteFile(directory_contents, true));
EXPECT_FALSE(PathExists(file_name));
EXPECT_FALSE(PathExists(subdir_path));
}
TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
FilePath subdir_path =
temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
CreateDirectory(subdir_path);
ASSERT_TRUE(PathExists(subdir_path));
FilePath directory_contents = subdir_path;
directory_contents = directory_contents.Append(FPL("*"));
EXPECT_TRUE(DeleteFile(directory_contents, false));
EXPECT_TRUE(PathExists(subdir_path));
EXPECT_TRUE(DeleteFile(directory_contents, true));
EXPECT_TRUE(PathExists(subdir_path));
}
#endif
TEST_F(FileUtilTest, DeleteDirNonRecursive) {
FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
CreateDirectory(test_subdir);
ASSERT_TRUE(PathExists(test_subdir));
FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
CreateTextFile(file_name, bogus_content);
ASSERT_TRUE(PathExists(file_name));
FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
CreateDirectory(subdir_path1);
ASSERT_TRUE(PathExists(subdir_path1));
FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
CreateDirectory(subdir_path2);
ASSERT_TRUE(PathExists(subdir_path2));
EXPECT_TRUE(DeleteFile(subdir_path2, false));
EXPECT_FALSE(PathExists(subdir_path2));
EXPECT_FALSE(DeleteFile(test_subdir, false));
EXPECT_TRUE(PathExists(test_subdir));
EXPECT_TRUE(PathExists(file_name));
EXPECT_TRUE(PathExists(subdir_path1));
}
TEST_F(FileUtilTest, DeleteDirRecursive) {
FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
CreateDirectory(test_subdir);
ASSERT_TRUE(PathExists(test_subdir));
FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
CreateTextFile(file_name, bogus_content);
ASSERT_TRUE(PathExists(file_name));
FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
CreateDirectory(subdir_path1);
ASSERT_TRUE(PathExists(subdir_path1));
FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
CreateDirectory(subdir_path2);
ASSERT_TRUE(PathExists(subdir_path2));
EXPECT_TRUE(DeleteFile(subdir_path2, true));
EXPECT_FALSE(PathExists(subdir_path2));
EXPECT_TRUE(DeleteFile(test_subdir, true));
EXPECT_FALSE(PathExists(file_name));
EXPECT_FALSE(PathExists(subdir_path1));
EXPECT_FALSE(PathExists(test_subdir));
}
TEST_F(FileUtilTest, MoveFileNew) {
FilePath file_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath file_name_to = temp_dir_.path().Append(
FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
ASSERT_FALSE(PathExists(file_name_to));
EXPECT_TRUE(Move(file_name_from, file_name_to));
EXPECT_FALSE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(file_name_to));
}
TEST_F(FileUtilTest, MoveFileExists) {
FilePath file_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath file_name_to = temp_dir_.path().Append(
FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
CreateTextFile(file_name_to, L"Old file content");
ASSERT_TRUE(PathExists(file_name_to));
EXPECT_TRUE(Move(file_name_from, file_name_to));
EXPECT_FALSE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(file_name_to));
EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
}
TEST_F(FileUtilTest, MoveFileDirExists) {
FilePath file_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath dir_name_to =
temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
CreateDirectory(dir_name_to);
ASSERT_TRUE(PathExists(dir_name_to));
EXPECT_FALSE(Move(file_name_from, dir_name_to));
}
TEST_F(FileUtilTest, MoveNew) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
FilePath file_name_from = dir_name_from.Append(txt_file_name);
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath dir_name_to =
temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
ASSERT_FALSE(PathExists(dir_name_to));
EXPECT_TRUE(Move(dir_name_from, dir_name_to));
EXPECT_FALSE(PathExists(dir_name_from));
EXPECT_FALSE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(PathExists(file_name_to));
file_name_from = dir_name_to.Append(txt_file_name);
file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
file_name_to = file_name_to.Append(txt_file_name);
EXPECT_FALSE(Move(file_name_from, file_name_to));
EXPECT_TRUE(PathExists(file_name_from));
EXPECT_FALSE(PathExists(file_name_to));
EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
EXPECT_FALSE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(file_name_to));
}
TEST_F(FileUtilTest, MoveExist) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath file_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath dir_name_exists =
temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
FilePath dir_name_to =
dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
CreateDirectory(dir_name_exists);
ASSERT_TRUE(PathExists(dir_name_exists));
EXPECT_TRUE(Move(dir_name_from, dir_name_to));
EXPECT_FALSE(PathExists(dir_name_from));
EXPECT_FALSE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(PathExists(file_name_to));
}
TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath file_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath subdir_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
CreateDirectory(subdir_name_from);
ASSERT_TRUE(PathExists(subdir_name_from));
FilePath file_name2_from =
subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name2_from));
FilePath dir_name_to =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
FilePath subdir_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
FilePath file_name2_to =
subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
ASSERT_FALSE(PathExists(dir_name_to));
EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
EXPECT_TRUE(PathExists(dir_name_from));
EXPECT_TRUE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(subdir_name_from));
EXPECT_TRUE(PathExists(file_name2_from));
EXPECT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(PathExists(file_name_to));
EXPECT_TRUE(PathExists(subdir_name_to));
EXPECT_TRUE(PathExists(file_name2_to));
}
TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath file_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath subdir_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
CreateDirectory(subdir_name_from);
ASSERT_TRUE(PathExists(subdir_name_from));
FilePath file_name2_from =
subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name2_from));
FilePath dir_name_exists =
temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
FilePath dir_name_to =
dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
FilePath subdir_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
FilePath file_name2_to =
subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateDirectory(dir_name_exists);
ASSERT_TRUE(PathExists(dir_name_exists));
EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
EXPECT_TRUE(PathExists(dir_name_from));
EXPECT_TRUE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(subdir_name_from));
EXPECT_TRUE(PathExists(file_name2_from));
EXPECT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(PathExists(file_name_to));
EXPECT_TRUE(PathExists(subdir_name_to));
EXPECT_TRUE(PathExists(file_name2_to));
}
TEST_F(FileUtilTest, CopyDirectoryNew) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath file_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath subdir_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
CreateDirectory(subdir_name_from);
ASSERT_TRUE(PathExists(subdir_name_from));
FilePath file_name2_from =
subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name2_from));
FilePath dir_name_to =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
FilePath subdir_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
ASSERT_FALSE(PathExists(dir_name_to));
EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
EXPECT_TRUE(PathExists(dir_name_from));
EXPECT_TRUE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(subdir_name_from));
EXPECT_TRUE(PathExists(file_name2_from));
EXPECT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(PathExists(file_name_to));
EXPECT_FALSE(PathExists(subdir_name_to));
}
TEST_F(FileUtilTest, CopyDirectoryExists) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath file_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath subdir_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
CreateDirectory(subdir_name_from);
ASSERT_TRUE(PathExists(subdir_name_from));
FilePath file_name2_from =
subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name2_from));
FilePath dir_name_to =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
FilePath subdir_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
CreateDirectory(dir_name_to);
ASSERT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
EXPECT_TRUE(PathExists(dir_name_from));
EXPECT_TRUE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(subdir_name_from));
EXPECT_TRUE(PathExists(file_name2_from));
EXPECT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(PathExists(file_name_to));
EXPECT_FALSE(PathExists(subdir_name_to));
}
TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
FilePath file_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath file_name_to = temp_dir_.path().Append(
FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
ASSERT_FALSE(PathExists(file_name_to));
EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
EXPECT_TRUE(PathExists(file_name_to));
}
TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
FilePath file_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath file_name_to = temp_dir_.path().Append(
FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
CreateTextFile(file_name_to, L"Old file content");
ASSERT_TRUE(PathExists(file_name_to));
EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
EXPECT_TRUE(PathExists(file_name_to));
EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
}
TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
FilePath file_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath dir_name_to =
temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
CreateDirectory(dir_name_to);
ASSERT_TRUE(PathExists(dir_name_to));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
EXPECT_TRUE(PathExists(file_name_to));
}
TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath file_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath dir_name_to =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
#if defined(OS_WIN)
FilePath from_path =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
#elif defined (OS_POSIX)
FilePath from_path =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
#endif
EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
EXPECT_TRUE(PathExists(dir_name_from));
EXPECT_TRUE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(PathExists(file_name_to));
}
void SetReadOnly(const FilePath& path) {
#if defined(OS_WIN)
DWORD attrs = GetFileAttributes(path.value().c_str());
ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
ASSERT_TRUE(SetFileAttributes(
path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY));
attrs = GetFileAttributes(path.value().c_str());
DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY;
if (win::GetVersion() >= win::VERSION_VISTA)
expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
ASSERT_EQ(expected, attrs);
#else
EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR));
#endif
}
bool IsReadOnly(const FilePath& path) {
#if defined(OS_WIN)
DWORD attrs = GetFileAttributes(path.value().c_str());
EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
return attrs & FILE_ATTRIBUTE_READONLY;
#else
int mode = 0;
EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
return !(mode & S_IWUSR);
#endif
}
TEST_F(FileUtilTest, CopyDirectoryACL) {
FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
CreateDirectory(src);
ASSERT_TRUE(PathExists(src));
FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
SetReadOnly(src_file);
ASSERT_TRUE(IsReadOnly(src_file));
FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
EXPECT_TRUE(CopyDirectory(src, dst, true));
ASSERT_FALSE(IsReadOnly(dst_file));
}
TEST_F(FileUtilTest, CopyFile) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath file_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
const std::wstring file_contents(L"Gooooooooooooooooooooogle");
CreateTextFile(file_name_from, file_contents);
ASSERT_TRUE(PathExists(file_name_from));
FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
ASSERT_TRUE(CopyFile(file_name_from, dest_file));
FilePath dest_file2(dir_name_from);
dest_file2 = dest_file2.AppendASCII("..");
dest_file2 = dest_file2.AppendASCII("DestFile.txt");
ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from, dest_file2));
FilePath dest_file2_test(dir_name_from);
dest_file2_test = dest_file2_test.DirName();
dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
EXPECT_TRUE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(dest_file));
const std::wstring read_contents = ReadTextFile(dest_file);
EXPECT_EQ(file_contents, read_contents);
EXPECT_TRUE(PathExists(dest_file2_test));
EXPECT_TRUE(PathExists(dest_file2));
}
TEST_F(FileUtilTest, CopyFileACL) {
FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt"));
const std::wstring file_contents(L"Gooooooooooooooooooooogle");
CreateTextFile(src, file_contents);
ASSERT_FALSE(IsReadOnly(src));
SetReadOnly(src);
ASSERT_TRUE(IsReadOnly(src));
FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt"));
ASSERT_TRUE(CopyFile(src, dst));
EXPECT_EQ(file_contents, ReadTextFile(dst));
ASSERT_FALSE(IsReadOnly(dst));
}
typedef PlatformTest ReadOnlyFileUtilTest;
TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
FilePath data_dir;
ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
data_dir = data_dir.AppendASCII("file_util");
ASSERT_TRUE(PathExists(data_dir));
FilePath original_file =
data_dir.Append(FILE_PATH_LITERAL("original.txt"));
FilePath same_file =
data_dir.Append(FILE_PATH_LITERAL("same.txt"));
FilePath same_length_file =
data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
FilePath different_file =
data_dir.Append(FILE_PATH_LITERAL("different.txt"));
FilePath different_first_file =
data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
FilePath different_last_file =
data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
FilePath empty1_file =
data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
FilePath empty2_file =
data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
FilePath shortened_file =
data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
FilePath binary_file =
data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
FilePath binary_file_same =
data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
FilePath binary_file_diff =
data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
EXPECT_TRUE(ContentsEqual(original_file, original_file));
EXPECT_TRUE(ContentsEqual(original_file, same_file));
EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
EXPECT_FALSE(ContentsEqual(original_file, different_file));
EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
FilePath(FILE_PATH_LITERAL("bogusname"))));
EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
}
TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
FilePath data_dir;
ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
data_dir = data_dir.AppendASCII("file_util");
ASSERT_TRUE(PathExists(data_dir));
FilePath original_file =
data_dir.Append(FILE_PATH_LITERAL("original.txt"));
FilePath same_file =
data_dir.Append(FILE_PATH_LITERAL("same.txt"));
FilePath crlf_file =
data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
FilePath shortened_file =
data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
FilePath different_file =
data_dir.Append(FILE_PATH_LITERAL("different.txt"));
FilePath different_first_file =
data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
FilePath different_last_file =
data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
FilePath first1_file =
data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
FilePath first2_file =
data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
FilePath empty1_file =
data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
FilePath empty2_file =
data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
FilePath blank_line_file =
data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
FilePath blank_line_crlf_file =
data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
EXPECT_TRUE(TextContentsEqual(original_file, same_file));
EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
EXPECT_FALSE(TextContentsEqual(original_file, different_file));
EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
}
#if defined(OS_WIN)
TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
FilePath dir_name_from =
temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
CreateDirectory(dir_name_from);
ASSERT_TRUE(PathExists(dir_name_from));
FilePath file_name_from =
dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
ASSERT_TRUE(PathExists(file_name_from));
FilePath dir_name_to = temp_dir_.path().Append(
FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
FilePath file_name_to =
dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
ASSERT_FALSE(PathExists(dir_name_to));
EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
dir_name_to));
EXPECT_FALSE(PathExists(dir_name_from));
EXPECT_FALSE(PathExists(file_name_from));
EXPECT_TRUE(PathExists(dir_name_to));
EXPECT_TRUE(PathExists(file_name_to));
}
TEST_F(FileUtilTest, GetTempDirTest) {
static const TCHAR* kTmpKey = _T("TMP");
static const TCHAR* kTmpValues[] = {
_T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
};
size_t original_tmp_size;
TCHAR* original_tmp;
ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
FilePath path;
::_tputenv_s(kTmpKey, kTmpValues[i]);
GetTempDir(&path);
EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
" result=" << path.value();
}
if (original_tmp) {
::_tputenv_s(kTmpKey, original_tmp);
free(original_tmp);
} else {
::_tputenv_s(kTmpKey, _T(""));
}
}
#endif
TEST_F(FileUtilTest, CreateTemporaryFileTest) {
FilePath temp_files[3];
for (int i = 0; i < 3; i++) {
ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
EXPECT_TRUE(PathExists(temp_files[i]));
EXPECT_FALSE(DirectoryExists(temp_files[i]));
}
for (int i = 0; i < 3; i++)
EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
for (int i = 0; i < 3; i++)
EXPECT_TRUE(DeleteFile(temp_files[i], false));
}
TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
FilePath names[3];
FILE* fps[3];
int i;
for (i = 0; i < 3; ++i) {
fps[i] = CreateAndOpenTemporaryFile(&(names[i]));
ASSERT_TRUE(fps[i]);
EXPECT_TRUE(PathExists(names[i]));
}
for (i = 0; i < 3; ++i) {
EXPECT_FALSE(names[i] == names[(i+1)%3]);
}
for (i = 0; i < 3; ++i) {
EXPECT_TRUE(CloseFile(fps[i]));
EXPECT_TRUE(DeleteFile(names[i], false));
}
}
TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
FilePath temp_dir;
ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
EXPECT_TRUE(PathExists(temp_dir));
EXPECT_TRUE(DeleteFile(temp_dir, false));
}
TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
FilePath new_dir;
ASSERT_TRUE(CreateTemporaryDirInDir(
temp_dir_.path(),
FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
&new_dir));
EXPECT_TRUE(PathExists(new_dir));
EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
EXPECT_TRUE(DeleteFile(new_dir, false));
}
#if defined(OS_POSIX)
TEST_F(FileUtilTest, GetShmemTempDirTest) {
FilePath dir;
EXPECT_TRUE(GetShmemTempDir(false, &dir));
EXPECT_TRUE(DirectoryExists(dir));
}
#endif
TEST_F(FileUtilTest, GetHomeDirTest) {
#if !defined(OS_ANDROID)
FilePath home = GetHomeDir();
ASSERT_FALSE(home.empty());
ASSERT_TRUE(home.IsAbsolute());
#endif
}
TEST_F(FileUtilTest, CreateDirectoryTest) {
FilePath test_root =
temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
#if defined(OS_WIN)
FilePath test_path =
test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
#elif defined(OS_POSIX)
FilePath test_path =
test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
#endif
EXPECT_FALSE(PathExists(test_path));
EXPECT_TRUE(CreateDirectory(test_path));
EXPECT_TRUE(PathExists(test_path));
EXPECT_TRUE(CreateDirectory(test_path));
test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
EXPECT_FALSE(PathExists(test_path));
CreateTextFile(test_path, L"test file");
EXPECT_TRUE(PathExists(test_path));
EXPECT_FALSE(CreateDirectory(test_path));
EXPECT_TRUE(DeleteFile(test_root, true));
EXPECT_FALSE(PathExists(test_root));
EXPECT_FALSE(PathExists(test_path));
ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
FilePath top_level = test_root;
while (top_level != top_level.DirName()) {
top_level = top_level.DirName();
}
ASSERT_TRUE(DirectoryExists(top_level));
EXPECT_TRUE(CreateDirectory(
FilePath(FilePath::kCurrentDirectory)));
EXPECT_TRUE(CreateDirectory(top_level));
#if defined(OS_WIN)
FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
FilePath invalid_path =
invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
if (!PathExists(invalid_drive)) {
EXPECT_FALSE(CreateDirectory(invalid_path));
}
#endif
}
TEST_F(FileUtilTest, DetectDirectoryTest) {
FilePath test_root =
temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
EXPECT_FALSE(PathExists(test_root));
EXPECT_TRUE(CreateDirectory(test_root));
EXPECT_TRUE(PathExists(test_root));
EXPECT_TRUE(DirectoryExists(test_root));
FilePath test_path =
test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
EXPECT_FALSE(PathExists(test_path));
CreateTextFile(test_path, L"test file");
EXPECT_TRUE(PathExists(test_path));
EXPECT_FALSE(DirectoryExists(test_path));
EXPECT_TRUE(DeleteFile(test_path, false));
EXPECT_TRUE(DeleteFile(test_root, true));
}
TEST_F(FileUtilTest, FileEnumeratorTest) {
FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
EXPECT_EQ(f0.Next().value(), FPL(""));
EXPECT_EQ(f0.Next().value(), FPL(""));
FileEnumerator f0_dotdot(temp_dir_.path(), false,
FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
f0_dotdot.Next().value());
EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
EXPECT_TRUE(CreateDirectory(dir1));
FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
EXPECT_TRUE(CreateDirectory(dir2));
FilePath dir2inner = dir2.Append(FPL("inner"));
EXPECT_TRUE(CreateDirectory(dir2inner));
FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
CreateTextFile(dir2file, std::wstring());
FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
CreateTextFile(dir2innerfile, std::wstring());
FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
CreateTextFile(file1, std::wstring());
FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
.Append(FPL("file2.txt"));
CreateTextFile(file2_rel, std::wstring());
FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
FindResultCollector c1(f1);
EXPECT_TRUE(c1.HasFile(file1));
EXPECT_TRUE(c1.HasFile(file2_abs));
EXPECT_TRUE(c1.HasFile(dir2file));
EXPECT_TRUE(c1.HasFile(dir2innerfile));
EXPECT_EQ(c1.size(), 4);
FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
FindResultCollector c2(f2);
EXPECT_TRUE(c2.HasFile(dir1));
EXPECT_TRUE(c2.HasFile(dir2));
EXPECT_TRUE(c2.HasFile(dir2inner));
EXPECT_EQ(c2.size(), 3);
FileEnumerator f2_non_recursive(
temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
FindResultCollector c2_non_recursive(f2_non_recursive);
EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
EXPECT_EQ(c2_non_recursive.size(), 2);
FileEnumerator f2_dotdot(temp_dir_.path(), false,
FileEnumerator::DIRECTORIES |
FileEnumerator::INCLUDE_DOT_DOT);
FindResultCollector c2_dotdot(f2_dotdot);
EXPECT_TRUE(c2_dotdot.HasFile(dir1));
EXPECT_TRUE(c2_dotdot.HasFile(dir2));
EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
EXPECT_EQ(c2_dotdot.size(), 3);
FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
FindResultCollector c3(f3);
EXPECT_TRUE(c3.HasFile(dir1));
EXPECT_TRUE(c3.HasFile(dir2));
EXPECT_TRUE(c3.HasFile(file1));
EXPECT_TRUE(c3.HasFile(file2_abs));
EXPECT_TRUE(c3.HasFile(dir2file));
EXPECT_TRUE(c3.HasFile(dir2inner));
EXPECT_TRUE(c3.HasFile(dir2innerfile));
EXPECT_EQ(c3.size(), 7);
FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
FindResultCollector c4(f4);
EXPECT_TRUE(c4.HasFile(dir2));
EXPECT_TRUE(c4.HasFile(dir2));
EXPECT_TRUE(c4.HasFile(file1));
EXPECT_TRUE(c4.HasFile(file2_abs));
EXPECT_EQ(c4.size(), 4);
FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
FindResultCollector c5(f5);
EXPECT_TRUE(c5.HasFile(dir1));
EXPECT_TRUE(c5.HasFile(dir2));
EXPECT_TRUE(c5.HasFile(dir2file));
EXPECT_TRUE(c5.HasFile(dir2inner));
EXPECT_TRUE(c5.HasFile(dir2innerfile));
EXPECT_EQ(c5.size(), 5);
#if defined(OS_WIN)
{
ReparsePoint reparse_point(dir1, dir2);
EXPECT_TRUE(reparse_point.IsValid());
if ((win::GetVersion() >= win::VERSION_VISTA)) {
FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
FindResultCollector c6(f6);
FilePath inner2 = dir1.Append(FPL("inner"));
EXPECT_TRUE(c6.HasFile(inner2));
EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
EXPECT_EQ(c6.size(), 3);
}
FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
FindResultCollector c7(f7);
EXPECT_TRUE(c7.HasFile(dir2));
EXPECT_TRUE(c7.HasFile(dir2));
EXPECT_TRUE(c7.HasFile(file1));
EXPECT_TRUE(c7.HasFile(file2_abs));
EXPECT_EQ(c7.size(), 4);
FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
FindResultCollector c8(f8);
EXPECT_TRUE(c8.HasFile(dir1));
EXPECT_TRUE(c8.HasFile(dir2));
EXPECT_TRUE(c8.HasFile(file1));
EXPECT_TRUE(c8.HasFile(file2_abs));
EXPECT_TRUE(c8.HasFile(dir2file));
EXPECT_TRUE(c8.HasFile(dir2inner));
EXPECT_TRUE(c8.HasFile(dir2innerfile));
EXPECT_EQ(c8.size(), 7);
}
#endif
FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
EXPECT_FALSE(f9.Next().value().empty());
}
TEST_F(FileUtilTest, AppendToFile) {
FilePath data_dir =
temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
if (PathExists(data_dir)) {
ASSERT_TRUE(DeleteFile(data_dir, true));
}
ASSERT_TRUE(CreateDirectory(data_dir));
if (PathExists(data_dir)) {
ASSERT_TRUE(DeleteFile(data_dir, true));
}
ASSERT_TRUE(CreateDirectory(data_dir));
FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
std::string data("hello");
EXPECT_EQ(-1, AppendToFile(foobar, data.c_str(), data.length()));
EXPECT_EQ(static_cast<int>(data.length()),
WriteFile(foobar, data.c_str(), data.length()));
EXPECT_EQ(static_cast<int>(data.length()),
AppendToFile(foobar, data.c_str(), data.length()));
const std::wstring read_content = ReadTextFile(foobar);
EXPECT_EQ(L"hellohello", read_content);
}
TEST_F(FileUtilTest, ReadFileToString) {
const char kTestData[] = "0123";
std::string data;
FilePath file_path =
temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
ASSERT_EQ(4, WriteFile(file_path, kTestData, 4));
EXPECT_TRUE(ReadFileToString(file_path, &data));
EXPECT_EQ(kTestData, data);
data = "temp";
EXPECT_FALSE(ReadFileToString(file_path, &data, 0));
EXPECT_EQ(data.length(), 0u);
data = "temp";
EXPECT_FALSE(ReadFileToString(file_path, &data, 2));
EXPECT_EQ("01", data);
data.clear();
EXPECT_FALSE(ReadFileToString(file_path, &data, 3));
EXPECT_EQ("012", data);
data.clear();
EXPECT_TRUE(ReadFileToString(file_path, &data, 4));
EXPECT_EQ("0123", data);
data.clear();
EXPECT_TRUE(ReadFileToString(file_path, &data, 6));
EXPECT_EQ("0123", data);
EXPECT_TRUE(ReadFileToString(file_path, NULL, 6));
EXPECT_TRUE(ReadFileToString(file_path, NULL));
EXPECT_TRUE(base::DeleteFile(file_path, false));
data = "temp";
EXPECT_FALSE(ReadFileToString(file_path, &data));
EXPECT_EQ(data.length(), 0u);
data = "temp";
EXPECT_FALSE(ReadFileToString(file_path, &data, 6));
EXPECT_EQ(data.length(), 0u);
}
TEST_F(FileUtilTest, TouchFile) {
FilePath data_dir =
temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
if (PathExists(data_dir)) {
ASSERT_TRUE(DeleteFile(data_dir, true));
}
ASSERT_TRUE(CreateDirectory(data_dir));
FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
std::string data("hello");
ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length()));
Time access_time;
ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
&access_time));
Time modification_time;
ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
&modification_time));
ASSERT_TRUE(TouchFile(foobar, access_time, modification_time));
File::Info file_info;
ASSERT_TRUE(GetFileInfo(foobar, &file_info));
EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
access_time.ToInternalValue());
EXPECT_EQ(file_info.last_modified.ToInternalValue(),
modification_time.ToInternalValue());
}
TEST_F(FileUtilTest, IsDirectoryEmpty) {
FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
ASSERT_FALSE(PathExists(empty_dir));
ASSERT_TRUE(CreateDirectory(empty_dir));
EXPECT_TRUE(IsDirectoryEmpty(empty_dir));
FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
std::string bar("baz");
ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length()));
EXPECT_FALSE(IsDirectoryEmpty(empty_dir));
}
#if defined(OS_POSIX)
class VerifyPathControlledByUserTest : public FileUtilTest {
protected:
virtual void SetUp() OVERRIDE {
FileUtilTest::SetUp();
base_dir_ = temp_dir_.path().AppendASCII("base_dir");
ASSERT_TRUE(CreateDirectory(base_dir_));
sub_dir_ = base_dir_.AppendASCII("sub_dir");
ASSERT_TRUE(CreateDirectory(sub_dir_));
text_file_ = sub_dir_.AppendASCII("file.txt");
CreateTextFile(text_file_, L"This text file has some text in it.");
struct stat stat_buf;
ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
uid_ = stat_buf.st_uid;
ok_gids_.insert(stat_buf.st_gid);
bad_gids_.insert(stat_buf.st_gid + 1);
ASSERT_EQ(uid_, getuid());
int enabled_permissions =
FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(
base_dir_, enabled_permissions, disabled_permissions));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(
sub_dir_, enabled_permissions, disabled_permissions));
}
FilePath base_dir_;
FilePath sub_dir_;
FilePath text_file_;
uid_t uid_;
std::set<gid_t> ok_gids_;
std::set<gid_t> bad_gids_;
};
TEST_F(VerifyPathControlledByUserTest, BadPaths) {
FilePath does_not_exist = base_dir_.AppendASCII("does")
.AppendASCII("not")
.AppendASCII("exist");
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, does_not_exist, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
sub_dir_, base_dir_, uid_, ok_gids_));
FilePath empty;
EXPECT_FALSE(
base::VerifyPathControlledByUser(
empty, base_dir_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
}
TEST_F(VerifyPathControlledByUserTest, Symlinks) {
FilePath file_link = base_dir_.AppendASCII("file_link");
ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
<< "Failed to create symlink.";
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, file_link, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
file_link, file_link, uid_, ok_gids_));
FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
<< "Failed to create symlink.";
FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
ASSERT_TRUE(PathExists(file_path_with_link));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, file_path_with_link, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
file_path_with_link, file_path_with_link, uid_, ok_gids_));
}
TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
uid_t bad_uid = uid_ + 1;
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, bad_uid, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, bad_uid, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, bad_uid, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, bad_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, bad_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, bad_gids_));
}
TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, bad_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, bad_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, bad_gids_));
std::set<gid_t> no_gids;
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, no_gids));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, no_gids));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, no_gids));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, bad_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, bad_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, bad_gids_));
std::set<gid_t> multiple_gids;
std::set_union(
ok_gids_.begin(), ok_gids_.end(),
bad_gids_.begin(), bad_gids_.end(),
std::inserter(multiple_gids, multiple_gids.begin()));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, multiple_gids));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, multiple_gids));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, multiple_gids));
}
TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_FALSE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
ASSERT_NO_FATAL_FAILURE(
ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, sub_dir_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
base_dir_, text_file_, uid_, ok_gids_));
EXPECT_TRUE(
base::VerifyPathControlledByUser(
sub_dir_, text_file_, uid_, ok_gids_));
}
#if defined(OS_ANDROID)
TEST_F(FileUtilTest, ValidContentUriTest) {
FilePath data_dir;
ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
data_dir = data_dir.AppendASCII("file_util");
ASSERT_TRUE(PathExists(data_dir));
FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
int64 image_size;
GetFileSize(image_file, &image_size);
EXPECT_LT(0, image_size);
FilePath path = file_util::InsertImageIntoMediaStore(image_file);
EXPECT_TRUE(path.IsContentUri());
EXPECT_TRUE(PathExists(path));
int64 content_uri_size;
GetFileSize(path, &content_uri_size);
EXPECT_EQ(image_size, content_uri_size);
char* buffer = new char[image_size];
File file = OpenContentUriForRead(path);
EXPECT_TRUE(file.IsValid());
EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size));
delete[] buffer;
}
TEST_F(FileUtilTest, NonExistentContentUriTest) {
FilePath path("content://foo.bar");
EXPECT_TRUE(path.IsContentUri());
EXPECT_FALSE(PathExists(path));
int64 size;
EXPECT_FALSE(GetFileSize(path, &size));
File file = OpenContentUriForRead(path);
EXPECT_FALSE(file.IsValid());
}
#endif
TEST(ScopedFD, ScopedFDDoesClose) {
int fds[2];
char c = 0;
ASSERT_EQ(0, pipe(fds));
const int write_end = fds[1];
base::ScopedFD read_end_closer(fds[0]);
{
base::ScopedFD write_end_closer(fds[1]);
}
int ret = close(write_end);
EXPECT_EQ(-1, ret);
EXPECT_EQ(EBADF, errno);
ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK));
EXPECT_EQ(0, read(fds[0], &c, 1));
}
#if defined(GTEST_HAS_DEATH_TEST)
void CloseWithScopedFD(int fd) {
base::ScopedFD fd_closer(fd);
}
#endif
TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) {
int fds[2];
ASSERT_EQ(0, pipe(fds));
base::ScopedFD read_end_closer(fds[0]);
EXPECT_EQ(0, IGNORE_EINTR(close(fds[1])));
#if defined(GTEST_HAS_DEATH_TEST)
EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
#endif
}
#endif
}
}