blob: 29d51330dcdcd7378e1a8487c4c2dcafc7a57a7d [file] [log] [blame]
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <shellapi.h>
10#include <shlobj.h>
tkent@chromium.org8da14162009-10-09 16:33:39 +090011#include <tchar.h>
thakis@chromium.org927d7282011-05-20 08:34:17 +090012#include <winioctl.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090013#endif
initial.commit3f4a7322008-07-27 06:49:38 +090014
jln@chromium.orgd7493932014-03-01 00:35:36 +090015#if defined(OS_POSIX)
16#include <errno.h>
17#include <fcntl.h>
18#include <unistd.h>
19#endif
20
michaelbai@chromium.orgbf8418f2011-10-25 09:08:28 +090021#include <algorithm>
initial.commit3f4a7322008-07-27 06:49:38 +090022#include <fstream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090023#include <set>
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +090024#include <vector>
initial.commit3f4a7322008-07-27 06:49:38 +090025
26#include "base/base_paths.h"
brettw@chromium.org56946722013-06-08 13:53:36 +090027#include "base/files/file_enumerator.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090028#include "base/files/file_path.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +090029#include "base/files/file_util.h"
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +090030#include "base/files/scoped_file.h"
brettw@chromium.org091db522012-11-17 05:34:23 +090031#include "base/files/scoped_temp_dir.h"
initial.commit3f4a7322008-07-27 06:49:38 +090032#include "base/path_service.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090033#include "base/strings/utf_string_conversions.h"
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +090034#include "base/test/test_file_util.h"
brettw@chromium.org61391822011-01-01 05:02:16 +090035#include "base/threading/platform_thread.h"
initial.commit3f4a7322008-07-27 06:49:38 +090036#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090037#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090038
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090039#if defined(OS_WIN)
40#include "base/win/scoped_handle.h"
rvargas@chromium.org56472942013-08-15 05:46:05 +090041#include "base/win/windows_version.h"
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090042#endif
43
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090044#if defined(OS_ANDROID)
45#include "base/android/content_uri_utils.h"
46#endif
47
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090048// This macro helps avoid wrapped lines in the test structs.
49#define FPL(x) FILE_PATH_LITERAL(x)
50
brettw@chromium.org2873d9b2013-11-28 08:22:08 +090051namespace base {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090052
initial.commit3f4a7322008-07-27 06:49:38 +090053namespace {
54
brettw@chromium.org01f3da42014-08-14 05:22:14 +090055// To test that NormalizeFilePath() deals with NTFS reparse points correctly,
56// we need functions to create and delete reparse points.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090057#if defined(OS_WIN)
58typedef struct _REPARSE_DATA_BUFFER {
59 ULONG ReparseTag;
60 USHORT ReparseDataLength;
61 USHORT Reserved;
62 union {
63 struct {
64 USHORT SubstituteNameOffset;
65 USHORT SubstituteNameLength;
66 USHORT PrintNameOffset;
67 USHORT PrintNameLength;
68 ULONG Flags;
69 WCHAR PathBuffer[1];
70 } SymbolicLinkReparseBuffer;
71 struct {
72 USHORT SubstituteNameOffset;
73 USHORT SubstituteNameLength;
74 USHORT PrintNameOffset;
75 USHORT PrintNameLength;
76 WCHAR PathBuffer[1];
77 } MountPointReparseBuffer;
78 struct {
79 UCHAR DataBuffer[1];
80 } GenericReparseBuffer;
81 };
82} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
83
84// Sets a reparse point. |source| will now point to |target|. Returns true if
85// the call succeeds, false otherwise.
86bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
87 std::wstring kPathPrefix = L"\\??\\";
88 std::wstring target_str;
89 // The juction will not work if the target path does not start with \??\ .
90 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
91 target_str += kPathPrefix;
92 target_str += target_path.value();
93 const wchar_t* target = target_str.c_str();
94 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
95 char buffer[2000] = {0};
96 DWORD returned;
97
98 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
99
100 data->ReparseTag = 0xa0000003;
101 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
102
103 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
104 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
105 data->ReparseDataLength = size_target + 4 + 8;
106
107 int data_size = data->ReparseDataLength + 8;
108
109 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
110 NULL, 0, &returned, NULL)) {
111 return false;
112 }
113 return true;
114}
115
116// Delete the reparse point referenced by |source|. Returns true if the call
117// succeeds, false otherwise.
118bool DeleteReparsePoint(HANDLE source) {
119 DWORD returned;
120 REPARSE_DATA_BUFFER data = {0};
121 data.ReparseTag = 0xa0000003;
122 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
123 &returned, NULL)) {
124 return false;
125 }
126 return true;
127}
rvargas@chromium.org56472942013-08-15 05:46:05 +0900128
129// Manages a reparse point for a test.
130class ReparsePoint {
131 public:
132 // Creates a reparse point from |source| (an empty directory) to |target|.
133 ReparsePoint(const FilePath& source, const FilePath& target) {
134 dir_.Set(
135 ::CreateFile(source.value().c_str(),
136 FILE_ALL_ACCESS,
137 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
138 NULL,
139 OPEN_EXISTING,
140 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
141 NULL));
rvargas14fb0e02014-09-24 07:03:11 +0900142 created_ = dir_.IsValid() && SetReparsePoint(dir_.Get(), target);
rvargas@chromium.org56472942013-08-15 05:46:05 +0900143 }
144
145 ~ReparsePoint() {
146 if (created_)
rvargas14fb0e02014-09-24 07:03:11 +0900147 DeleteReparsePoint(dir_.Get());
rvargas@chromium.org56472942013-08-15 05:46:05 +0900148 }
149
150 bool IsValid() { return created_; }
151
152 private:
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900153 win::ScopedHandle dir_;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900154 bool created_;
155 DISALLOW_COPY_AND_ASSIGN(ReparsePoint);
156};
157
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900158#endif
159
skerner@google.com93449ef2011-09-22 23:47:18 +0900160#if defined(OS_POSIX)
161// Provide a simple way to change the permissions bits on |path| in tests.
162// ASSERT failures will return, but not stop the test. Caller should wrap
163// calls to this function in ASSERT_NO_FATAL_FAILURE().
164void ChangePosixFilePermissions(const FilePath& path,
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900165 int mode_bits_to_set,
166 int mode_bits_to_clear) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900167 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
168 << "Can't set and clear the same bits.";
169
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900170 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900171 ASSERT_TRUE(GetPosixFilePermissions(path, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900172 mode |= mode_bits_to_set;
173 mode &= ~mode_bits_to_clear;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900174 ASSERT_TRUE(SetPosixFilePermissions(path, mode));
skerner@google.com93449ef2011-09-22 23:47:18 +0900175}
176#endif // defined(OS_POSIX)
177
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900178const wchar_t bogus_content[] = L"I'm cannon fodder.";
179
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900180const int FILES_AND_DIRECTORIES =
brettw@chromium.org56946722013-06-08 13:53:36 +0900181 FileEnumerator::FILES | FileEnumerator::DIRECTORIES;
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900182
erikkay@google.comf2406842008-08-21 00:59:49 +0900183// file_util winds up using autoreleased objects on the Mac, so this needs
184// to be a PlatformTest
185class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900186 protected:
mostynb1fdbe1e2014-10-08 02:59:11 +0900187 virtual void SetUp() override {
erikkay@google.comf2406842008-08-21 00:59:49 +0900188 PlatformTest::SetUp();
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900189 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commit3f4a7322008-07-27 06:49:38 +0900190 }
191
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900192 ScopedTempDir temp_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900193};
194
195// Collects all the results from the given file enumerator, and provides an
196// interface to query whether a given file is present.
197class FindResultCollector {
198 public:
brettw@chromium.org56946722013-06-08 13:53:36 +0900199 explicit FindResultCollector(FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900200 FilePath cur_file;
201 while (!(cur_file = enumerator.Next()).value().empty()) {
202 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900203 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900204 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900205 << "Same file returned twice";
206
207 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900208 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900209 }
210 }
211
212 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900213 bool HasFile(const FilePath& file) const {
214 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900215 }
evanm@google.com874d1672008-10-31 08:54:04 +0900216
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900217 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900218 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900219 }
initial.commit3f4a7322008-07-27 06:49:38 +0900220
221 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900222 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900223};
224
225// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900226void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900227 const std::wstring& contents) {
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900228 std::wofstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900229 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900230 ASSERT_TRUE(file.is_open());
231 file << contents;
232 file.close();
233}
234
235// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900236std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900237 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900238 std::wifstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900239 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900240 EXPECT_TRUE(file.is_open());
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900241 file.getline(contents, arraysize(contents));
initial.commit3f4a7322008-07-27 06:49:38 +0900242 file.close();
243 return std::wstring(contents);
244}
245
erikkay@google.com014161d2008-08-16 02:45:13 +0900246#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900247uint64 FileTimeAsUint64(const FILETIME& ft) {
248 ULARGE_INTEGER u;
249 u.LowPart = ft.dwLowDateTime;
250 u.HighPart = ft.dwHighDateTime;
251 return u.QuadPart;
252}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900253#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900254
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900255TEST_F(FileUtilTest, FileAndDirectorySize) {
256 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
257 // should return 53 bytes.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900258 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900259 CreateTextFile(file_01, L"12345678901234567890");
260 int64 size_f1 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900261 ASSERT_TRUE(GetFileSize(file_01, &size_f1));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900262 EXPECT_EQ(20ll, size_f1);
263
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900264 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900265 CreateDirectory(subdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900266
267 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
268 CreateTextFile(file_02, L"123456789012345678901234567890");
269 int64 size_f2 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900270 ASSERT_TRUE(GetFileSize(file_02, &size_f2));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900271 EXPECT_EQ(30ll, size_f2);
272
273 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900274 CreateDirectory(subsubdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900275
276 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
277 CreateTextFile(file_03, L"123");
278
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900279 int64 computed_size = ComputeDirectorySize(temp_dir_.path());
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900280 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
281}
282
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900283TEST_F(FileUtilTest, NormalizeFilePathBasic) {
284 // Create a directory under the test dir. Because we create it,
285 // we know it is not a link.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900286 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
287 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900288 FilePath file_b_path = dir_path.Append(FPL("file_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900289 CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900290
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900291 FilePath normalized_file_a_path, normalized_file_b_path;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900292 ASSERT_FALSE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900293 ASSERT_FALSE(NormalizeFilePath(file_a_path, &normalized_file_a_path))
viettrungluu@chromium.orgea703f12010-08-23 01:19:13 +0900294 << "NormalizeFilePath() should fail on nonexistent paths.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900295
296 CreateTextFile(file_a_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900297 ASSERT_TRUE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900298 ASSERT_TRUE(NormalizeFilePath(file_a_path, &normalized_file_a_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900299
300 CreateTextFile(file_b_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900301 ASSERT_TRUE(PathExists(file_b_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900302 ASSERT_TRUE(NormalizeFilePath(file_b_path, &normalized_file_b_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900303
304 // Beacuse this test created |dir_path|, we know it is not a link
305 // or junction. So, the real path of the directory holding file a
306 // must be the parent of the path holding file b.
307 ASSERT_TRUE(normalized_file_a_path.DirName()
308 .IsParent(normalized_file_b_path.DirName()));
309}
310
311#if defined(OS_WIN)
312
313TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
314 // Build the following directory structure:
315 //
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900316 // temp_dir
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900317 // |-> base_a
318 // | |-> sub_a
319 // | |-> file.txt
320 // | |-> long_name___... (Very long name.)
321 // | |-> sub_long
322 // | |-> deep.txt
323 // |-> base_b
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900324 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
325 // |-> to_base_b (reparse point to temp_dir\base_b)
326 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900327
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900328 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900329 ASSERT_TRUE(CreateDirectory(base_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900330
331 FilePath sub_a = base_a.Append(FPL("sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900332 ASSERT_TRUE(CreateDirectory(sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900333
334 FilePath file_txt = sub_a.Append(FPL("file.txt"));
335 CreateTextFile(file_txt, bogus_content);
336
337 // Want a directory whose name is long enough to make the path to the file
338 // inside just under MAX_PATH chars. This will be used to test that when
339 // a junction expands to a path over MAX_PATH chars in length,
340 // NormalizeFilePath() fails without crashing.
341 FilePath sub_long_rel(FPL("sub_long"));
342 FilePath deep_txt(FPL("deep.txt"));
343
344 int target_length = MAX_PATH;
345 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
346 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900347 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900348 // the resulting path is still long enough to hit the failing case in
349 // NormalizePath().
350 const int kCreateDirLimit = 4;
351 target_length -= kCreateDirLimit;
352 FilePath::StringType long_name_str = FPL("long_name_");
353 long_name_str.resize(target_length, '_');
354
355 FilePath long_name = sub_a.Append(FilePath(long_name_str));
356 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
357 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
358
359 FilePath sub_long = deep_file.DirName();
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900360 ASSERT_TRUE(CreateDirectory(sub_long));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900361 CreateTextFile(deep_file, bogus_content);
362
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900363 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900364 ASSERT_TRUE(CreateDirectory(base_b));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900365
366 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900367 ASSERT_TRUE(CreateDirectory(to_sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900368 FilePath normalized_path;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900369 {
370 ReparsePoint reparse_to_sub_a(to_sub_a, sub_a);
371 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900372
rvargas@chromium.org56472942013-08-15 05:46:05 +0900373 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900374 ASSERT_TRUE(CreateDirectory(to_base_b));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900375 ReparsePoint reparse_to_base_b(to_base_b, base_b);
376 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900377
rvargas@chromium.org56472942013-08-15 05:46:05 +0900378 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900379 ASSERT_TRUE(CreateDirectory(to_sub_long));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900380 ReparsePoint reparse_to_sub_long(to_sub_long, sub_long);
381 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900382
rvargas@chromium.org56472942013-08-15 05:46:05 +0900383 // Normalize a junction free path: base_a\sub_a\file.txt .
brettw@chromium.org70684242013-12-05 03:22:49 +0900384 ASSERT_TRUE(NormalizeFilePath(file_txt, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900385 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
386
387 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
388 // the junction to_sub_a.
brettw@chromium.org70684242013-12-05 03:22:49 +0900389 ASSERT_TRUE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900390 &normalized_path));
391 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
392
393 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
394 // normalized to exclude junctions to_base_b and to_sub_a .
brettw@chromium.org70684242013-12-05 03:22:49 +0900395 ASSERT_TRUE(NormalizeFilePath(base_b.Append(FPL("to_base_b"))
rvargas@chromium.org56472942013-08-15 05:46:05 +0900396 .Append(FPL("to_base_b"))
397 .Append(FPL("to_sub_a"))
398 .Append(FPL("file.txt")),
399 &normalized_path));
400 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
401
402 // A long enough path will cause NormalizeFilePath() to fail. Make a long
403 // path using to_base_b many times, and check that paths long enough to fail
404 // do not cause a crash.
405 FilePath long_path = base_b;
406 const int kLengthLimit = MAX_PATH + 200;
407 while (long_path.value().length() <= kLengthLimit) {
408 long_path = long_path.Append(FPL("to_base_b"));
409 }
410 long_path = long_path.Append(FPL("to_sub_a"))
411 .Append(FPL("file.txt"));
412
brettw@chromium.org70684242013-12-05 03:22:49 +0900413 ASSERT_FALSE(NormalizeFilePath(long_path, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900414
415 // Normalizing the junction to deep.txt should fail, because the expanded
416 // path to deep.txt is longer than MAX_PATH.
brettw@chromium.org70684242013-12-05 03:22:49 +0900417 ASSERT_FALSE(NormalizeFilePath(to_sub_long.Append(deep_txt),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900418 &normalized_path));
419
420 // Delete the reparse points, and see that NormalizeFilePath() fails
421 // to traverse them.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900422 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900423
brettw@chromium.org70684242013-12-05 03:22:49 +0900424 ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900425 &normalized_path));
426}
427
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900428TEST_F(FileUtilTest, DevicePathToDriveLetter) {
429 // Get a drive letter.
430 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
431 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
432 LOG(ERROR) << "Can't get a drive letter to test with.";
433 return;
434 }
435
436 // Get the NT style path to that drive.
437 wchar_t device_path[MAX_PATH] = {'\0'};
438 ASSERT_TRUE(
439 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
440 FilePath actual_device_path(device_path);
441 FilePath win32_path;
442
443 // Run DevicePathToDriveLetterPath() on the NT style path we got from
444 // QueryDosDevice(). Expect the drive letter we started with.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900445 ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path, &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900446 ASSERT_EQ(real_drive_letter, win32_path.value());
447
448 // Add some directories to the path. Expect those extra path componenets
449 // to be preserved.
450 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900451 ASSERT_TRUE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900452 actual_device_path.Append(kRelativePath),
453 &win32_path));
454 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
455 win32_path.value());
456
457 // Deform the real path so that it is invalid by removing the last four
458 // characters. The way windows names devices that are hard disks
459 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
460 // than three characters. The only way the truncated string could be a
461 // real drive is if more than 10^3 disks are mounted:
462 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
463 // Check that DevicePathToDriveLetterPath fails.
464 int path_length = actual_device_path.value().length();
465 int new_length = path_length - 4;
466 ASSERT_LT(0, new_length);
467 FilePath prefix_of_real_device_path(
468 actual_device_path.value().substr(0, new_length));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900469 ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path,
470 &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900471
brettw@chromium.orga9154032013-12-05 05:56:49 +0900472 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900473 prefix_of_real_device_path.Append(kRelativePath),
474 &win32_path));
475
476 // Deform the real path so that it is invalid by adding some characters. For
477 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
478 // request for the drive letter whose native path is
479 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
480 // because drives are numbered in order and mounting 112345 hard disks will
481 // never happen.
482 const FilePath::StringType kExtraChars = FPL("12345");
483
484 FilePath real_device_path_plus_numbers(
485 actual_device_path.value() + kExtraChars);
486
brettw@chromium.orga9154032013-12-05 05:56:49 +0900487 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900488 real_device_path_plus_numbers,
489 &win32_path));
490
brettw@chromium.orga9154032013-12-05 05:56:49 +0900491 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900492 real_device_path_plus_numbers.Append(kRelativePath),
493 &win32_path));
494}
495
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900496TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
497 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
498 // if it is available. This test requires that:
499 // - the filesystem at |temp_dir_| supports long filenames.
500 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
501 // directories of |temp_dir_|.
502 const FilePath::CharType kLongDirName[] = FPL("A long path");
503 const FilePath::CharType kTestSubDirName[] = FPL("test");
504 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900505 ASSERT_TRUE(CreateDirectory(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900506
507 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
508 // different short name.
509 WCHAR path_buffer[MAX_PATH];
510 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
511 path_buffer, MAX_PATH);
512 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
513 ASSERT_NE(DWORD(0), path_buffer_length);
514 FilePath short_test_dir(path_buffer);
515 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
516
517 FilePath temp_file;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900518 ASSERT_TRUE(CreateTemporaryFileInDir(short_test_dir, &temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900519 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900520 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900521
522 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
523 // unreadable. We should still be able to create a temp file in the
524 // subdirectory, but we won't be able to determine the long path for it. This
525 // mimics the environment that some users run where their user profiles reside
526 // in a location where the don't have full access to the higher level
527 // directories. (Note that this assumption is true for NTFS, but not for some
528 // network file systems. E.g. AFS).
529 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900530 ASSERT_TRUE(CreateDirectory(access_test_dir));
brettw@chromium.orgbdedd6c2014-08-08 07:57:11 +0900531 base::FilePermissionRestorer long_test_dir_restorer(long_test_dir);
532 ASSERT_TRUE(base::MakeFileUnreadable(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900533
534 // Use the short form of the directory to create a temporary filename.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900535 ASSERT_TRUE(CreateTemporaryFileInDir(
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900536 short_test_dir.Append(kTestSubDirName), &temp_file));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900537 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900538 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
539
540 // Check that the long path can't be determined for |temp_file|.
541 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
542 path_buffer, MAX_PATH);
543 EXPECT_EQ(DWORD(0), path_buffer_length);
544}
545
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900546#endif // defined(OS_WIN)
547
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900548#if defined(OS_POSIX)
549
550TEST_F(FileUtilTest, CreateAndReadSymlinks) {
551 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
552 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
553 CreateTextFile(link_to, bogus_content);
554
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900555 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900556 << "Failed to create file symlink.";
557
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900558 // If we created the link properly, we should be able to read the contents
559 // through it.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900560 std::wstring contents = ReadTextFile(link_from);
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900561 EXPECT_EQ(bogus_content, contents);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900562
563 FilePath result;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900564 ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900565 EXPECT_EQ(link_to.value(), result.value());
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900566
567 // Link to a directory.
568 link_from = temp_dir_.path().Append(FPL("from_dir"));
569 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900570 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900571 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900572 << "Failed to create directory symlink.";
573
574 // Test failures.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900575 EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
576 EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900577 FilePath missing = temp_dir_.path().Append(FPL("missing"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900578 EXPECT_FALSE(ReadSymbolicLink(missing, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900579}
580
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900581// The following test of NormalizeFilePath() require that we create a symlink.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900582// This can not be done on Windows before Vista. On Vista, creating a symlink
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900583// requires privilege "SeCreateSymbolicLinkPrivilege".
584// TODO(skerner): Investigate the possibility of giving base_unittests the
585// privileges required to create a symlink.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900586TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
skerner@chromium.org559baa92010-05-13 00:13:57 +0900587 // Link one file to another.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900588 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
589 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900590 CreateTextFile(link_to, bogus_content);
591
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900592 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900593 << "Failed to create file symlink.";
594
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900595 // Check that NormalizeFilePath sees the link.
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900596 FilePath normalized_path;
brettw@chromium.org70684242013-12-05 03:22:49 +0900597 ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900598 EXPECT_NE(link_from, link_to);
599 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
600 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900601
602 // Link to a directory.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900603 link_from = temp_dir_.path().Append(FPL("from_dir"));
604 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900605 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900606 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900607 << "Failed to create directory symlink.";
608
brettw@chromium.org70684242013-12-05 03:22:49 +0900609 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900610 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900611
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900612 // Test that a loop in the links causes NormalizeFilePath() to return false.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900613 link_from = temp_dir_.path().Append(FPL("link_a"));
614 link_to = temp_dir_.path().Append(FPL("link_b"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900615 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900616 << "Failed to create loop symlink a.";
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900617 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900618 << "Failed to create loop symlink b.";
619
620 // Infinite loop!
brettw@chromium.org70684242013-12-05 03:22:49 +0900621 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900622}
623#endif // defined(OS_POSIX)
624
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900625TEST_F(FileUtilTest, DeleteNonExistent) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900626 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900627 ASSERT_FALSE(PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900628
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900629 EXPECT_TRUE(DeleteFile(non_existent, false));
630 ASSERT_FALSE(PathExists(non_existent));
631 EXPECT_TRUE(DeleteFile(non_existent, true));
632 ASSERT_FALSE(PathExists(non_existent));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900633}
634
ericu@chromium.org472c56f2014-05-22 12:22:48 +0900635TEST_F(FileUtilTest, DeleteNonExistentWithNonExistentParent) {
636 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_topdir");
637 non_existent = non_existent.AppendASCII("bogus_subdir");
638 ASSERT_FALSE(PathExists(non_existent));
639
640 EXPECT_TRUE(DeleteFile(non_existent, false));
641 ASSERT_FALSE(PathExists(non_existent));
642 EXPECT_TRUE(DeleteFile(non_existent, true));
643 ASSERT_FALSE(PathExists(non_existent));
644}
645
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900646TEST_F(FileUtilTest, DeleteFile) {
647 // Create a file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900648 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900649 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900650 ASSERT_TRUE(PathExists(file_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900651
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900652 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900653 EXPECT_TRUE(DeleteFile(file_name, false));
654 EXPECT_FALSE(PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900655
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900656 // Test recursive case, create a new file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900657 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900658 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900659 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900660
661 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900662 EXPECT_TRUE(DeleteFile(file_name, true));
663 EXPECT_FALSE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900664}
665
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900666#if defined(OS_POSIX)
667TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900668 // Create a file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900669 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
670 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900671 ASSERT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900672
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900673 // Create a symlink to the file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900674 FilePath file_link = temp_dir_.path().Append("file_link_2");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900675 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900676 << "Failed to create symlink.";
677
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900678 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900679 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900680
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900681 // Make sure original file is not deleted.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900682 EXPECT_FALSE(PathExists(file_link));
683 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900684}
685
686TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900687 // Create a non-existent file path.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900688 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900689 EXPECT_FALSE(PathExists(non_existent));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900690
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900691 // Create a symlink to the non-existent file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900692 FilePath file_link = temp_dir_.path().Append("file_link_3");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900693 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900694 << "Failed to create symlink.";
695
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900696 // Make sure the symbolic link is exist.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900697 EXPECT_TRUE(IsLink(file_link));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900698 EXPECT_FALSE(PathExists(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900699
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900700 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900701 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900702
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900703 // Make sure the symbolic link is deleted.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900704 EXPECT_FALSE(IsLink(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900705}
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900706
707TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
708 // Create a file path.
709 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900710 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900711
712 const std::string kData("hello");
713
714 int buffer_size = kData.length();
715 char* buffer = new char[buffer_size];
716
717 // Write file.
718 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900719 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900720 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900721
722 // Make sure the file is readable.
723 int32 mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900724 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
725 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900726
727 // Get rid of the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900728 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
729 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
730 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900731 // Make sure the file can't be read.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900732 EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900733
734 // Give the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900735 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
736 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
737 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900738 // Make sure the file can be read.
739 EXPECT_EQ(static_cast<int>(kData.length()),
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900740 ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900741
742 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900743 EXPECT_TRUE(DeleteFile(file_name, false));
744 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900745
746 delete[] buffer;
747}
748
749TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
750 // Create a file path.
751 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900752 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900753
754 const std::string kData("hello");
755
756 // Write file.
757 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900758 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900759 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900760
761 // Make sure the file is writable.
762 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900763 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
764 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900765 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900766
767 // Get rid of the write permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900768 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
769 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
770 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900771 // Make sure the file can't be write.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900772 EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900773 EXPECT_FALSE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900774
775 // Give read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900776 EXPECT_TRUE(SetPosixFilePermissions(file_name,
777 FILE_PERMISSION_WRITE_BY_USER));
778 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
779 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900780 // Make sure the file can be write.
781 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900782 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900783 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900784
785 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900786 EXPECT_TRUE(DeleteFile(file_name, false));
787 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900788}
789
790TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
791 // Create a directory path.
792 FilePath subdir_path =
793 temp_dir_.path().Append(FPL("PermissionTest1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900794 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900795 ASSERT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900796
797 // Create a dummy file to enumerate.
798 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900799 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900800 const std::string kData("hello");
801 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900802 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900803 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900804
805 // Make sure the directory has the all permissions.
806 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900807 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
808 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900809
810 // Get rid of the permissions from the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900811 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
812 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
813 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900814
815 // Make sure the file in the directory can't be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900816 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900817 EXPECT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900818 FindResultCollector c1(f1);
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +0900819 EXPECT_EQ(0, c1.size());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900820 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900821
822 // Give the permissions to the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900823 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
824 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
825 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900826
827 // Make sure the file in the directory can be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900828 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900829 FindResultCollector c2(f2);
830 EXPECT_TRUE(c2.HasFile(file_name));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +0900831 EXPECT_EQ(1, c2.size());
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900832
833 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900834 EXPECT_TRUE(DeleteFile(subdir_path, true));
835 EXPECT_FALSE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900836}
837
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900838#endif // defined(OS_POSIX)
839
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900840#if defined(OS_WIN)
841// Tests that the Delete function works for wild cards, especially
842// with the recursion flag. Also coincidentally tests PathExists.
843// TODO(erikkay): see if anyone's actually using this feature of the API
844TEST_F(FileUtilTest, DeleteWildCard) {
845 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900846 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900847 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900848 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900849
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900850 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900851 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900852 ASSERT_TRUE(PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900853
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900854 // Create the wildcard path
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900855 FilePath directory_contents = temp_dir_.path();
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900856 directory_contents = directory_contents.Append(FPL("*"));
857
initial.commit3f4a7322008-07-27 06:49:38 +0900858 // Delete non-recursively and check that only the file is deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900859 EXPECT_TRUE(DeleteFile(directory_contents, false));
860 EXPECT_FALSE(PathExists(file_name));
861 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900862
zork@chromium.org61be4f42010-05-07 09:05:36 +0900863 // Delete recursively and make sure all contents are deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900864 EXPECT_TRUE(DeleteFile(directory_contents, true));
865 EXPECT_FALSE(PathExists(file_name));
866 EXPECT_FALSE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900867}
868
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900869// TODO(erikkay): see if anyone's actually using this feature of the API
870TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
871 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900872 FilePath subdir_path =
873 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900874 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900875 ASSERT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900876
877 // Create the wildcard path
878 FilePath directory_contents = subdir_path;
879 directory_contents = directory_contents.Append(FPL("*"));
880
881 // Delete non-recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900882 EXPECT_TRUE(DeleteFile(directory_contents, false));
883 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900884
885 // Delete recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900886 EXPECT_TRUE(DeleteFile(directory_contents, true));
887 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900888}
889#endif
890
891// Tests non-recursive Delete() for a directory.
892TEST_F(FileUtilTest, DeleteDirNonRecursive) {
893 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900894 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900895 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900896 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900897
898 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
899 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900900 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900901
902 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900903 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900904 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900905
906 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900907 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900908 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900909
910 // Delete non-recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900911 EXPECT_TRUE(DeleteFile(subdir_path2, false));
912 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900913
914 // Delete non-recursively and check that nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900915 EXPECT_FALSE(DeleteFile(test_subdir, false));
916 EXPECT_TRUE(PathExists(test_subdir));
917 EXPECT_TRUE(PathExists(file_name));
918 EXPECT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900919}
920
921// Tests recursive Delete() for a directory.
922TEST_F(FileUtilTest, DeleteDirRecursive) {
923 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900924 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900925 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900926 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900927
928 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
929 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900930 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900931
932 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900933 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900934 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900935
936 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900937 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900938 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900939
940 // Delete recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900941 EXPECT_TRUE(DeleteFile(subdir_path2, true));
942 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900943
944 // Delete recursively and check that everything got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900945 EXPECT_TRUE(DeleteFile(test_subdir, true));
946 EXPECT_FALSE(PathExists(file_name));
947 EXPECT_FALSE(PathExists(subdir_path1));
948 EXPECT_FALSE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900949}
950
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900951TEST_F(FileUtilTest, MoveFileNew) {
952 // Create a file
953 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900954 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900955 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900956 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900957
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900958 // The destination.
959 FilePath file_name_to = temp_dir_.path().Append(
960 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900961 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900962
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900963 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900964
965 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900966 EXPECT_FALSE(PathExists(file_name_from));
967 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900968}
969
970TEST_F(FileUtilTest, MoveFileExists) {
971 // Create a file
972 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900973 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900974 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900975 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900976
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900977 // The destination name.
978 FilePath file_name_to = temp_dir_.path().Append(
979 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900980 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900981 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900982
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900983 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900984
985 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900986 EXPECT_FALSE(PathExists(file_name_from));
987 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900988 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
989}
990
991TEST_F(FileUtilTest, MoveFileDirExists) {
992 // Create a file
993 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900994 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900995 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900996 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900997
998 // The destination directory
999 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001000 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001001 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001002 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001003
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001004 EXPECT_FALSE(Move(file_name_from, dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001005}
1006
1007
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001008TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001009 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001010 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001011 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001012 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001013 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001014
1015 // Create a file under the directory
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001016 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1017 FilePath file_name_from = dir_name_from.Append(txt_file_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001018 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001019 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001020
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001021 // Move the directory.
1022 FilePath dir_name_to =
1023 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001024 FilePath file_name_to =
1025 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001026
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001027 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001028
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001029 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001030
1031 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001032 EXPECT_FALSE(PathExists(dir_name_from));
1033 EXPECT_FALSE(PathExists(file_name_from));
1034 EXPECT_TRUE(PathExists(dir_name_to));
1035 EXPECT_TRUE(PathExists(file_name_to));
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001036
1037 // Test path traversal.
1038 file_name_from = dir_name_to.Append(txt_file_name);
1039 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1040 file_name_to = file_name_to.Append(txt_file_name);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001041 EXPECT_FALSE(Move(file_name_from, file_name_to));
1042 EXPECT_TRUE(PathExists(file_name_from));
1043 EXPECT_FALSE(PathExists(file_name_to));
1044 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1045 EXPECT_FALSE(PathExists(file_name_from));
1046 EXPECT_TRUE(PathExists(file_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001047}
1048
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001049TEST_F(FileUtilTest, MoveExist) {
1050 // Create a directory
1051 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001052 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001053 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001054 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001055
1056 // Create a file under the directory
1057 FilePath file_name_from =
1058 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1059 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001060 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001061
1062 // Move the directory
1063 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001064 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001065
1066 FilePath dir_name_to =
1067 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1068 FilePath file_name_to =
1069 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1070
1071 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001072 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001073 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001074
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001075 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001076
1077 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001078 EXPECT_FALSE(PathExists(dir_name_from));
1079 EXPECT_FALSE(PathExists(file_name_from));
1080 EXPECT_TRUE(PathExists(dir_name_to));
1081 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001082}
1083
1084TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001085 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001086 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001087 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001088 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001089 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001090
1091 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001092 FilePath file_name_from =
1093 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001094 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001095 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001096
1097 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001098 FilePath subdir_name_from =
1099 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001100 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001101 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001102
1103 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001104 FilePath file_name2_from =
1105 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001106 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001107 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001108
1109 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001110 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001111 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001112 FilePath file_name_to =
1113 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1114 FilePath subdir_name_to =
1115 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1116 FilePath file_name2_to =
1117 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001118
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001119 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001120
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001121 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001122
1123 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001124 EXPECT_TRUE(PathExists(dir_name_from));
1125 EXPECT_TRUE(PathExists(file_name_from));
1126 EXPECT_TRUE(PathExists(subdir_name_from));
1127 EXPECT_TRUE(PathExists(file_name2_from));
1128 EXPECT_TRUE(PathExists(dir_name_to));
1129 EXPECT_TRUE(PathExists(file_name_to));
1130 EXPECT_TRUE(PathExists(subdir_name_to));
1131 EXPECT_TRUE(PathExists(file_name2_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001132}
1133
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001134TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1135 // Create a directory.
1136 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001137 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001138 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001139 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001140
1141 // Create a file under the directory.
1142 FilePath file_name_from =
1143 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1144 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001145 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001146
1147 // Create a subdirectory.
1148 FilePath subdir_name_from =
1149 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001150 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001151 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001152
1153 // Create a file under the subdirectory.
1154 FilePath file_name2_from =
1155 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1156 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001157 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001158
1159 // Copy the directory recursively.
1160 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001161 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001162
1163 FilePath dir_name_to =
1164 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1165 FilePath file_name_to =
1166 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1167 FilePath subdir_name_to =
1168 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1169 FilePath file_name2_to =
1170 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1171
1172 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001173 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001174 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001175
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001176 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001177
1178 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001179 EXPECT_TRUE(PathExists(dir_name_from));
1180 EXPECT_TRUE(PathExists(file_name_from));
1181 EXPECT_TRUE(PathExists(subdir_name_from));
1182 EXPECT_TRUE(PathExists(file_name2_from));
1183 EXPECT_TRUE(PathExists(dir_name_to));
1184 EXPECT_TRUE(PathExists(file_name_to));
1185 EXPECT_TRUE(PathExists(subdir_name_to));
1186 EXPECT_TRUE(PathExists(file_name2_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001187}
1188
1189TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001190 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001191 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001192 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001193 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001194 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001195
1196 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001197 FilePath file_name_from =
1198 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001199 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001200 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001201
1202 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001203 FilePath subdir_name_from =
1204 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001205 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001206 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001207
1208 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001209 FilePath file_name2_from =
1210 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001211 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001212 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001213
1214 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001215 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001216 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001217 FilePath file_name_to =
1218 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1219 FilePath subdir_name_to =
1220 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001221
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001222 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001223
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001224 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001225
1226 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001227 EXPECT_TRUE(PathExists(dir_name_from));
1228 EXPECT_TRUE(PathExists(file_name_from));
1229 EXPECT_TRUE(PathExists(subdir_name_from));
1230 EXPECT_TRUE(PathExists(file_name2_from));
1231 EXPECT_TRUE(PathExists(dir_name_to));
1232 EXPECT_TRUE(PathExists(file_name_to));
1233 EXPECT_FALSE(PathExists(subdir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001234}
1235
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001236TEST_F(FileUtilTest, CopyDirectoryExists) {
1237 // Create a directory.
1238 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001239 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001240 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001241 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001242
1243 // Create a file under the directory.
1244 FilePath file_name_from =
1245 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1246 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001247 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001248
1249 // Create a subdirectory.
1250 FilePath subdir_name_from =
1251 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001252 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001253 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001254
1255 // Create a file under the subdirectory.
1256 FilePath file_name2_from =
1257 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1258 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001259 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001260
1261 // Copy the directory not recursively.
1262 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001263 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001264 FilePath file_name_to =
1265 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1266 FilePath subdir_name_to =
1267 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1268
1269 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001270 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001271 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001272
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001273 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001274
1275 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001276 EXPECT_TRUE(PathExists(dir_name_from));
1277 EXPECT_TRUE(PathExists(file_name_from));
1278 EXPECT_TRUE(PathExists(subdir_name_from));
1279 EXPECT_TRUE(PathExists(file_name2_from));
1280 EXPECT_TRUE(PathExists(dir_name_to));
1281 EXPECT_TRUE(PathExists(file_name_to));
1282 EXPECT_FALSE(PathExists(subdir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001283}
1284
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001285TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1286 // Create a file
1287 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001288 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001289 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001290 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001291
1292 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001293 FilePath file_name_to = temp_dir_.path().Append(
1294 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001295 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001296
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001297 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001298
1299 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001300 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001301}
1302
1303TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1304 // Create a file
1305 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001306 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001307 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001308 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001309
1310 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001311 FilePath file_name_to = temp_dir_.path().Append(
1312 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001313 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001314 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001315
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001316 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001317
1318 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001319 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001320 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1321}
1322
1323TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1324 // Create a file
1325 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001326 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001327 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001328 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001329
1330 // The destination
1331 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001332 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001333 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001334 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001335 FilePath file_name_to =
1336 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1337
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001338 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001339
1340 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001341 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001342}
1343
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001344TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1345 // Create a directory.
1346 FilePath dir_name_from =
1347 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001348 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001349 ASSERT_TRUE(PathExists(dir_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001350
1351 // Create a file under the directory.
1352 FilePath file_name_from =
1353 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1354 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001355 ASSERT_TRUE(PathExists(file_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001356
1357 // Copy the directory recursively.
1358 FilePath dir_name_to =
1359 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1360 FilePath file_name_to =
1361 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1362
1363 // Create from path with trailing separators.
1364#if defined(OS_WIN)
1365 FilePath from_path =
1366 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1367#elif defined (OS_POSIX)
1368 FilePath from_path =
1369 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1370#endif
1371
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001372 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001373
1374 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001375 EXPECT_TRUE(PathExists(dir_name_from));
1376 EXPECT_TRUE(PathExists(file_name_from));
1377 EXPECT_TRUE(PathExists(dir_name_to));
1378 EXPECT_TRUE(PathExists(file_name_to));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001379}
1380
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001381// Sets the source file to read-only.
rippf8459182014-10-27 18:59:53 +09001382void SetReadOnly(const FilePath& path, bool read_only) {
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001383#if defined(OS_WIN)
rippf8459182014-10-27 18:59:53 +09001384 // On Windows, it involves setting/removing the 'readonly' bit.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001385 DWORD attrs = GetFileAttributes(path.value().c_str());
1386 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1387 ASSERT_TRUE(SetFileAttributes(
rippf8459182014-10-27 18:59:53 +09001388 path.value().c_str(),
1389 read_only ? (attrs | FILE_ATTRIBUTE_READONLY) :
1390 (attrs & ~FILE_ATTRIBUTE_READONLY)));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001391 // Files in the temporary directory should not be indexed ever. If this
1392 // assumption change, fix this unit test accordingly.
1393 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
rippf8459182014-10-27 18:59:53 +09001394 DWORD expected = read_only ?
1395 ((attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)) |
1396 FILE_ATTRIBUTE_READONLY) :
1397 (attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY));
1398 // TODO(ripp@yandex-team.ru): this seems out of place here. If we really think
1399 // it is important to verify that temp files are not indexed there should be
1400 // a dedicated test for that (create a file, inspect the attributes)
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001401 if (win::GetVersion() >= win::VERSION_VISTA)
1402 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
rippf8459182014-10-27 18:59:53 +09001403 attrs = GetFileAttributes(path.value().c_str());
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001404 ASSERT_EQ(expected, attrs);
1405#else
rippf8459182014-10-27 18:59:53 +09001406 // On all other platforms, it involves removing/setting the write bit.
1407 mode_t mode = read_only ? S_IRUSR : (S_IRUSR | S_IWUSR);
1408 EXPECT_TRUE(SetPosixFilePermissions(
1409 path, DirectoryExists(path) ? (mode | S_IXUSR) : mode));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001410#endif
1411}
1412
1413bool IsReadOnly(const FilePath& path) {
1414#if defined(OS_WIN)
1415 DWORD attrs = GetFileAttributes(path.value().c_str());
1416 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1417 return attrs & FILE_ATTRIBUTE_READONLY;
1418#else
1419 int mode = 0;
1420 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1421 return !(mode & S_IWUSR);
1422#endif
1423}
1424
1425TEST_F(FileUtilTest, CopyDirectoryACL) {
rippf8459182014-10-27 18:59:53 +09001426 // Create source directories.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001427 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
rippf8459182014-10-27 18:59:53 +09001428 FilePath src_subdir = src.Append(FILE_PATH_LITERAL("subdir"));
1429 CreateDirectory(src_subdir);
1430 ASSERT_TRUE(PathExists(src_subdir));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001431
1432 // Create a file under the directory.
1433 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1434 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
rippf8459182014-10-27 18:59:53 +09001435 SetReadOnly(src_file, true);
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001436 ASSERT_TRUE(IsReadOnly(src_file));
1437
rippf8459182014-10-27 18:59:53 +09001438 // Make directory read-only.
1439 SetReadOnly(src_subdir, true);
1440 ASSERT_TRUE(IsReadOnly(src_subdir));
1441
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001442 // Copy the directory recursively.
1443 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1444 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1445 EXPECT_TRUE(CopyDirectory(src, dst, true));
1446
rippf8459182014-10-27 18:59:53 +09001447 FilePath dst_subdir = dst.Append(FILE_PATH_LITERAL("subdir"));
1448 ASSERT_FALSE(IsReadOnly(dst_subdir));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001449 ASSERT_FALSE(IsReadOnly(dst_file));
rippf8459182014-10-27 18:59:53 +09001450
1451 // Give write permissions to allow deletion.
1452 SetReadOnly(src_subdir, false);
1453 ASSERT_FALSE(IsReadOnly(src_subdir));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001454}
1455
initial.commit3f4a7322008-07-27 06:49:38 +09001456TEST_F(FileUtilTest, CopyFile) {
1457 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001458 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001459 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001460 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001461 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001462
1463 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001464 FilePath file_name_from =
1465 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001466 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1467 CreateTextFile(file_name_from, file_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001468 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001469
1470 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001471 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001472 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001473
cmumford1daaa6b2014-11-18 03:58:20 +09001474 // Try to copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001475 FilePath dest_file2(dir_name_from);
1476 dest_file2 = dest_file2.AppendASCII("..");
1477 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001478 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
evan@chromium.org1543ad32009-08-27 05:00:14 +09001479
1480 FilePath dest_file2_test(dir_name_from);
1481 dest_file2_test = dest_file2_test.DirName();
1482 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001483
cmumford1daaa6b2014-11-18 03:58:20 +09001484 // Check expected copy results.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001485 EXPECT_TRUE(PathExists(file_name_from));
1486 EXPECT_TRUE(PathExists(dest_file));
initial.commit3f4a7322008-07-27 06:49:38 +09001487 const std::wstring read_contents = ReadTextFile(dest_file);
1488 EXPECT_EQ(file_contents, read_contents);
cmumford1daaa6b2014-11-18 03:58:20 +09001489 EXPECT_FALSE(PathExists(dest_file2_test));
1490 EXPECT_FALSE(PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001491}
1492
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001493TEST_F(FileUtilTest, CopyFileACL) {
1494 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1495 // this test case asserts the access control bits are meeting expectations in
cmumford1daaa6b2014-11-18 03:58:20 +09001496 // CopyFile().
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001497 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt"));
1498 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1499 CreateTextFile(src, file_contents);
1500
1501 // Set the source file to read-only.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001502 ASSERT_FALSE(IsReadOnly(src));
rippf8459182014-10-27 18:59:53 +09001503 SetReadOnly(src, true);
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001504 ASSERT_TRUE(IsReadOnly(src));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001505
1506 // Copy the file.
1507 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt"));
1508 ASSERT_TRUE(CopyFile(src, dst));
1509 EXPECT_EQ(file_contents, ReadTextFile(dst));
1510
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001511 ASSERT_FALSE(IsReadOnly(dst));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001512}
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001513
erikkay@google.comf2406842008-08-21 00:59:49 +09001514// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001515// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001516typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001517
erikkay@google.comf2406842008-08-21 00:59:49 +09001518TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001519 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001520 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001521 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001522 ASSERT_TRUE(PathExists(data_dir));
initial.commit3f4a7322008-07-27 06:49:38 +09001523
evanm@google.com874d1672008-10-31 08:54:04 +09001524 FilePath original_file =
1525 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1526 FilePath same_file =
1527 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1528 FilePath same_length_file =
1529 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1530 FilePath different_file =
1531 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1532 FilePath different_first_file =
1533 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1534 FilePath different_last_file =
1535 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1536 FilePath empty1_file =
1537 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1538 FilePath empty2_file =
1539 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1540 FilePath shortened_file =
1541 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1542 FilePath binary_file =
1543 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1544 FilePath binary_file_same =
1545 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1546 FilePath binary_file_diff =
1547 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001548
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001549 EXPECT_TRUE(ContentsEqual(original_file, original_file));
1550 EXPECT_TRUE(ContentsEqual(original_file, same_file));
1551 EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
1552 EXPECT_FALSE(ContentsEqual(original_file, different_file));
1553 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1554 FilePath(FILE_PATH_LITERAL("bogusname"))));
1555 EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
1556 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1557 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1558 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1559 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1560 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1561 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
initial.commit3f4a7322008-07-27 06:49:38 +09001562}
1563
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001564TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1565 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001566 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001567 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001568 ASSERT_TRUE(PathExists(data_dir));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001569
1570 FilePath original_file =
1571 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1572 FilePath same_file =
1573 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1574 FilePath crlf_file =
1575 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1576 FilePath shortened_file =
1577 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1578 FilePath different_file =
1579 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1580 FilePath different_first_file =
1581 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1582 FilePath different_last_file =
1583 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1584 FilePath first1_file =
1585 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1586 FilePath first2_file =
1587 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1588 FilePath empty1_file =
1589 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1590 FilePath empty2_file =
1591 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1592 FilePath blank_line_file =
1593 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1594 FilePath blank_line_crlf_file =
1595 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1596
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001597 EXPECT_TRUE(TextContentsEqual(original_file, same_file));
1598 EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
1599 EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
1600 EXPECT_FALSE(TextContentsEqual(original_file, different_file));
1601 EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
1602 EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
1603 EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
1604 EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
1605 EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
1606 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001607}
1608
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001609// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001610#if defined(OS_WIN)
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001611TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1612 // Create a directory
1613 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001614 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001615 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001616 ASSERT_TRUE(PathExists(dir_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001617
1618 // Create a file under the directory
1619 FilePath file_name_from =
1620 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1621 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001622 ASSERT_TRUE(PathExists(file_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001623
1624 // Move the directory by using CopyAndDeleteDirectory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001625 FilePath dir_name_to = temp_dir_.path().Append(
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001626 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1627 FilePath file_name_to =
1628 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1629
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001630 ASSERT_FALSE(PathExists(dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001631
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001632 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +09001633 dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001634
1635 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001636 EXPECT_FALSE(PathExists(dir_name_from));
1637 EXPECT_FALSE(PathExists(file_name_from));
1638 EXPECT_TRUE(PathExists(dir_name_to));
1639 EXPECT_TRUE(PathExists(file_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001640}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001641
1642TEST_F(FileUtilTest, GetTempDirTest) {
1643 static const TCHAR* kTmpKey = _T("TMP");
1644 static const TCHAR* kTmpValues[] = {
1645 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1646 };
1647 // Save the original $TMP.
1648 size_t original_tmp_size;
1649 TCHAR* original_tmp;
1650 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1651 // original_tmp may be NULL.
1652
1653 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1654 FilePath path;
1655 ::_tputenv_s(kTmpKey, kTmpValues[i]);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001656 GetTempDir(&path);
tkent@chromium.org8da14162009-10-09 16:33:39 +09001657 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1658 " result=" << path.value();
1659 }
1660
1661 // Restore the original $TMP.
1662 if (original_tmp) {
1663 ::_tputenv_s(kTmpKey, original_tmp);
1664 free(original_tmp);
1665 } else {
1666 ::_tputenv_s(kTmpKey, _T(""));
1667 }
1668}
1669#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001670
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001671TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1672 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001673 for (int i = 0; i < 3; i++) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001674 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001675 EXPECT_TRUE(PathExists(temp_files[i]));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001676 EXPECT_FALSE(DirectoryExists(temp_files[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001677 }
1678 for (int i = 0; i < 3; i++)
1679 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1680 for (int i = 0; i < 3; i++)
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001681 EXPECT_TRUE(DeleteFile(temp_files[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001682}
1683
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001684TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001685 FilePath names[3];
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001686 FILE* fps[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001687 int i;
1688
1689 // Create; make sure they are open and exist.
1690 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001691 fps[i] = CreateAndOpenTemporaryFile(&(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001692 ASSERT_TRUE(fps[i]);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001693 EXPECT_TRUE(PathExists(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001694 }
1695
1696 // Make sure all names are unique.
1697 for (i = 0; i < 3; ++i) {
1698 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1699 }
1700
1701 // Close and delete.
1702 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001703 EXPECT_TRUE(CloseFile(fps[i]));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001704 EXPECT_TRUE(DeleteFile(names[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001705 }
initial.commit3f4a7322008-07-27 06:49:38 +09001706}
1707
rvargas@chromium.org43d4a8e2014-06-10 20:19:50 +09001708TEST_F(FileUtilTest, FileToFILE) {
1709 File file;
1710 FILE* stream = FileToFILE(file.Pass(), "w");
1711 EXPECT_FALSE(stream);
1712
1713 FilePath file_name = temp_dir_.path().Append(FPL("The file.txt"));
1714 file = File(file_name, File::FLAG_CREATE | File::FLAG_WRITE);
1715 EXPECT_TRUE(file.IsValid());
1716
1717 stream = FileToFILE(file.Pass(), "w");
1718 EXPECT_TRUE(stream);
1719 EXPECT_FALSE(file.IsValid());
1720 EXPECT_TRUE(CloseFile(stream));
1721}
1722
initial.commit3f4a7322008-07-27 06:49:38 +09001723TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001724 FilePath temp_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001725 ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001726 EXPECT_TRUE(PathExists(temp_dir));
1727 EXPECT_TRUE(DeleteFile(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001728}
1729
skerner@chromium.orge4432392010-05-01 02:00:09 +09001730TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1731 FilePath new_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001732 ASSERT_TRUE(CreateTemporaryDirInDir(
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001733 temp_dir_.path(),
skerner@chromium.orge4432392010-05-01 02:00:09 +09001734 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001735 &new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001736 EXPECT_TRUE(PathExists(new_dir));
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001737 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001738 EXPECT_TRUE(DeleteFile(new_dir, false));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001739}
1740
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001741#if defined(OS_POSIX)
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001742TEST_F(FileUtilTest, GetShmemTempDirTest) {
1743 FilePath dir;
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001744 EXPECT_TRUE(GetShmemTempDir(false, &dir));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001745 EXPECT_TRUE(DirectoryExists(dir));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001746}
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001747#endif
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001748
brettw@chromium.org49de1af2014-02-20 05:34:23 +09001749TEST_F(FileUtilTest, GetHomeDirTest) {
1750#if !defined(OS_ANDROID) // Not implemented on Android.
1751 // We don't actually know what the home directory is supposed to be without
1752 // calling some OS functions which would just duplicate the implementation.
1753 // So here we just test that it returns something "reasonable".
1754 FilePath home = GetHomeDir();
1755 ASSERT_FALSE(home.empty());
1756 ASSERT_TRUE(home.IsAbsolute());
1757#endif
1758}
1759
initial.commit3f4a7322008-07-27 06:49:38 +09001760TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001761 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001762 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001763#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001764 FilePath test_path =
1765 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001766#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001767 FilePath test_path =
1768 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001769#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001770
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001771 EXPECT_FALSE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001772 EXPECT_TRUE(CreateDirectory(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001773 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001774 // CreateDirectory returns true if the DirectoryExists returns true.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001775 EXPECT_TRUE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001776
1777 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001778 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001779 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001780 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001781 EXPECT_TRUE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001782 EXPECT_FALSE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001783
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001784 EXPECT_TRUE(DeleteFile(test_root, true));
1785 EXPECT_FALSE(PathExists(test_root));
1786 EXPECT_FALSE(PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001787
1788 // Verify assumptions made by the Windows implementation:
1789 // 1. The current directory always exists.
1790 // 2. The root directory always exists.
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001791 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001792 FilePath top_level = test_root;
1793 while (top_level != top_level.DirName()) {
1794 top_level = top_level.DirName();
1795 }
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001796 ASSERT_TRUE(DirectoryExists(top_level));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001797
1798 // Given these assumptions hold, it should be safe to
1799 // test that "creating" these directories succeeds.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001800 EXPECT_TRUE(CreateDirectory(
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001801 FilePath(FilePath::kCurrentDirectory)));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001802 EXPECT_TRUE(CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001803
1804#if defined(OS_WIN)
1805 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1806 FilePath invalid_path =
1807 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001808 if (!PathExists(invalid_drive)) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001809 EXPECT_FALSE(CreateDirectory(invalid_path));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001810 }
1811#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001812}
1813
1814TEST_F(FileUtilTest, DetectDirectoryTest) {
1815 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001816 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001817 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001818 EXPECT_FALSE(PathExists(test_root));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001819 EXPECT_TRUE(CreateDirectory(test_root));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001820 EXPECT_TRUE(PathExists(test_root));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001821 EXPECT_TRUE(DirectoryExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001822 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001823 FilePath test_path =
1824 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001825 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001826 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001827 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001828 EXPECT_FALSE(DirectoryExists(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001829 EXPECT_TRUE(DeleteFile(test_path, false));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001830
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001831 EXPECT_TRUE(DeleteFile(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001832}
1833
initial.commit3f4a7322008-07-27 06:49:38 +09001834TEST_F(FileUtilTest, FileEnumeratorTest) {
1835 // Test an empty directory.
brettw@chromium.org56946722013-06-08 13:53:36 +09001836 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001837 EXPECT_EQ(FPL(""), f0.Next().value());
1838 EXPECT_EQ(FPL(""), f0.Next().value());
initial.commit3f4a7322008-07-27 06:49:38 +09001839
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001840 // Test an empty directory, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001841 FileEnumerator f0_dotdot(temp_dir_.path(), false,
1842 FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001843 EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001844 f0_dotdot.Next().value());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001845 EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001846
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001847 // create the directories
rvargas@chromium.org56472942013-08-15 05:46:05 +09001848 FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001849 EXPECT_TRUE(CreateDirectory(dir1));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001850 FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001851 EXPECT_TRUE(CreateDirectory(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001852 FilePath dir2inner = dir2.Append(FPL("inner"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001853 EXPECT_TRUE(CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001854
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001855 // create the files
rvargas@chromium.org56472942013-08-15 05:46:05 +09001856 FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001857 CreateTextFile(dir2file, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001858 FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001859 CreateTextFile(dir2innerfile, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001860 FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001861 CreateTextFile(file1, std::wstring());
1862 FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
rvargas@chromium.org56472942013-08-15 05:46:05 +09001863 .Append(FPL("file2.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001864 CreateTextFile(file2_rel, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001865 FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001866
1867 // Only enumerate files.
brettw@chromium.org56946722013-06-08 13:53:36 +09001868 FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
initial.commit3f4a7322008-07-27 06:49:38 +09001869 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001870 EXPECT_TRUE(c1.HasFile(file1));
1871 EXPECT_TRUE(c1.HasFile(file2_abs));
1872 EXPECT_TRUE(c1.HasFile(dir2file));
1873 EXPECT_TRUE(c1.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001874 EXPECT_EQ(4, c1.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001875
1876 // Only enumerate directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001877 FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001878 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001879 EXPECT_TRUE(c2.HasFile(dir1));
1880 EXPECT_TRUE(c2.HasFile(dir2));
1881 EXPECT_TRUE(c2.HasFile(dir2inner));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001882 EXPECT_EQ(3, c2.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001883
tim@chromium.org989d0972008-10-16 11:42:45 +09001884 // Only enumerate directories non-recursively.
brettw@chromium.org56946722013-06-08 13:53:36 +09001885 FileEnumerator f2_non_recursive(
1886 temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001887 FindResultCollector c2_non_recursive(f2_non_recursive);
1888 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1889 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001890 EXPECT_EQ(2, c2_non_recursive.size());
tim@chromium.org989d0972008-10-16 11:42:45 +09001891
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001892 // Only enumerate directories, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001893 FileEnumerator f2_dotdot(temp_dir_.path(), false,
1894 FileEnumerator::DIRECTORIES |
1895 FileEnumerator::INCLUDE_DOT_DOT);
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001896 FindResultCollector c2_dotdot(f2_dotdot);
1897 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1898 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001899 EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001900 EXPECT_EQ(3, c2_dotdot.size());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001901
initial.commit3f4a7322008-07-27 06:49:38 +09001902 // Enumerate files and directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001903 FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001904 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001905 EXPECT_TRUE(c3.HasFile(dir1));
1906 EXPECT_TRUE(c3.HasFile(dir2));
1907 EXPECT_TRUE(c3.HasFile(file1));
1908 EXPECT_TRUE(c3.HasFile(file2_abs));
1909 EXPECT_TRUE(c3.HasFile(dir2file));
1910 EXPECT_TRUE(c3.HasFile(dir2inner));
1911 EXPECT_TRUE(c3.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001912 EXPECT_EQ(7, c3.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001913
1914 // Non-recursive operation.
brettw@chromium.org56946722013-06-08 13:53:36 +09001915 FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001916 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001917 EXPECT_TRUE(c4.HasFile(dir2));
1918 EXPECT_TRUE(c4.HasFile(dir2));
1919 EXPECT_TRUE(c4.HasFile(file1));
1920 EXPECT_TRUE(c4.HasFile(file2_abs));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001921 EXPECT_EQ(4, c4.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001922
1923 // Enumerate with a pattern.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001924 FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001925 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001926 EXPECT_TRUE(c5.HasFile(dir1));
1927 EXPECT_TRUE(c5.HasFile(dir2));
1928 EXPECT_TRUE(c5.HasFile(dir2file));
1929 EXPECT_TRUE(c5.HasFile(dir2inner));
1930 EXPECT_TRUE(c5.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001931 EXPECT_EQ(5, c5.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001932
rvargas@chromium.org56472942013-08-15 05:46:05 +09001933#if defined(OS_WIN)
1934 {
1935 // Make dir1 point to dir2.
1936 ReparsePoint reparse_point(dir1, dir2);
1937 EXPECT_TRUE(reparse_point.IsValid());
1938
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001939 if ((win::GetVersion() >= win::VERSION_VISTA)) {
rvargas@chromium.org56472942013-08-15 05:46:05 +09001940 // There can be a delay for the enumeration code to see the change on
1941 // the file system so skip this test for XP.
1942 // Enumerate the reparse point.
1943 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
1944 FindResultCollector c6(f6);
1945 FilePath inner2 = dir1.Append(FPL("inner"));
1946 EXPECT_TRUE(c6.HasFile(inner2));
1947 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1948 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001949 EXPECT_EQ(3, c6.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001950 }
1951
1952 // No changes for non recursive operation.
1953 FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
1954 FindResultCollector c7(f7);
1955 EXPECT_TRUE(c7.HasFile(dir2));
1956 EXPECT_TRUE(c7.HasFile(dir2));
1957 EXPECT_TRUE(c7.HasFile(file1));
1958 EXPECT_TRUE(c7.HasFile(file2_abs));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001959 EXPECT_EQ(4, c7.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001960
1961 // Should not enumerate inside dir1 when using recursion.
1962 FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1963 FindResultCollector c8(f8);
1964 EXPECT_TRUE(c8.HasFile(dir1));
1965 EXPECT_TRUE(c8.HasFile(dir2));
1966 EXPECT_TRUE(c8.HasFile(file1));
1967 EXPECT_TRUE(c8.HasFile(file2_abs));
1968 EXPECT_TRUE(c8.HasFile(dir2file));
1969 EXPECT_TRUE(c8.HasFile(dir2inner));
1970 EXPECT_TRUE(c8.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001971 EXPECT_EQ(7, c8.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001972 }
1973#endif
1974
initial.commit3f4a7322008-07-27 06:49:38 +09001975 // Make sure the destructor closes the find handle while in the middle of a
1976 // query to allow TearDown to delete the directory.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001977 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1978 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
avi@google.com5cb79352008-12-11 23:55:12 +09001979 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001980}
license.botf003cfe2008-08-24 09:55:55 +09001981
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001982TEST_F(FileUtilTest, AppendToFile) {
1983 FilePath data_dir =
1984 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1985
1986 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001987 if (PathExists(data_dir)) {
1988 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001989 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001990 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001991
1992 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001993 if (PathExists(data_dir)) {
1994 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001995 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001996 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001997 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1998
1999 std::string data("hello");
chirantan05106cc2014-10-08 08:15:30 +09002000 EXPECT_FALSE(AppendToFile(foobar, data.c_str(), data.size()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09002001 EXPECT_EQ(static_cast<int>(data.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002002 WriteFile(foobar, data.c_str(), data.length()));
chirantan05106cc2014-10-08 08:15:30 +09002003 EXPECT_TRUE(AppendToFile(foobar, data.c_str(), data.size()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09002004
2005 const std::wstring read_content = ReadTextFile(foobar);
2006 EXPECT_EQ(L"hellohello", read_content);
2007}
2008
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002009TEST_F(FileUtilTest, ReadFile) {
2010 // Create a test file to be read.
2011 const std::string kTestData("The quick brown fox jumps over the lazy dog.");
2012 FilePath file_path =
2013 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileTest"));
2014
2015 ASSERT_EQ(static_cast<int>(kTestData.size()),
2016 WriteFile(file_path, kTestData.data(), kTestData.size()));
2017
2018 // Make buffers with various size.
2019 std::vector<char> small_buffer(kTestData.size() / 2);
2020 std::vector<char> exact_buffer(kTestData.size());
2021 std::vector<char> large_buffer(kTestData.size() * 2);
2022
2023 // Read the file with smaller buffer.
2024 int bytes_read_small = ReadFile(
2025 file_path, &small_buffer[0], static_cast<int>(small_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002026 EXPECT_EQ(static_cast<int>(small_buffer.size()), bytes_read_small);
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002027 EXPECT_EQ(
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002028 std::string(kTestData.begin(), kTestData.begin() + small_buffer.size()),
2029 std::string(small_buffer.begin(), small_buffer.end()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002030
2031 // Read the file with buffer which have exactly same size.
2032 int bytes_read_exact = ReadFile(
2033 file_path, &exact_buffer[0], static_cast<int>(exact_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002034 EXPECT_EQ(static_cast<int>(kTestData.size()), bytes_read_exact);
2035 EXPECT_EQ(kTestData, std::string(exact_buffer.begin(), exact_buffer.end()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002036
2037 // Read the file with larger buffer.
2038 int bytes_read_large = ReadFile(
2039 file_path, &large_buffer[0], static_cast<int>(large_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002040 EXPECT_EQ(static_cast<int>(kTestData.size()), bytes_read_large);
2041 EXPECT_EQ(kTestData, std::string(large_buffer.begin(),
2042 large_buffer.begin() + kTestData.size()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002043
2044 // Make sure the return value is -1 if the file doesn't exist.
2045 FilePath file_path_not_exist =
2046 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileNotExistTest"));
2047 EXPECT_EQ(-1,
2048 ReadFile(file_path_not_exist,
2049 &exact_buffer[0],
2050 static_cast<int>(exact_buffer.size())));
2051}
2052
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002053TEST_F(FileUtilTest, ReadFileToString) {
2054 const char kTestData[] = "0123";
2055 std::string data;
2056
2057 FilePath file_path =
2058 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002059 FilePath file_path_dangerous =
2060 temp_dir_.path().Append(FILE_PATH_LITERAL("..")).
2061 Append(temp_dir_.path().BaseName()).
2062 Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002063
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002064 // Create test file.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002065 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002066
2067 EXPECT_TRUE(ReadFileToString(file_path, &data));
2068 EXPECT_EQ(kTestData, data);
2069
2070 data = "temp";
2071 EXPECT_FALSE(ReadFileToString(file_path, &data, 0));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002072 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002073
2074 data = "temp";
2075 EXPECT_FALSE(ReadFileToString(file_path, &data, 2));
2076 EXPECT_EQ("01", data);
2077
2078 data.clear();
2079 EXPECT_FALSE(ReadFileToString(file_path, &data, 3));
2080 EXPECT_EQ("012", data);
2081
2082 data.clear();
2083 EXPECT_TRUE(ReadFileToString(file_path, &data, 4));
2084 EXPECT_EQ("0123", data);
2085
2086 data.clear();
2087 EXPECT_TRUE(ReadFileToString(file_path, &data, 6));
2088 EXPECT_EQ("0123", data);
2089
2090 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6));
2091
2092 EXPECT_TRUE(ReadFileToString(file_path, NULL));
2093
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002094 data = "temp";
2095 EXPECT_FALSE(ReadFileToString(file_path_dangerous, &data));
2096 EXPECT_EQ(0u, data.length());
2097
2098 // Delete test file.
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002099 EXPECT_TRUE(base::DeleteFile(file_path, false));
2100
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002101 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002102 EXPECT_FALSE(ReadFileToString(file_path, &data));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002103 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002104
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002105 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002106 EXPECT_FALSE(ReadFileToString(file_path, &data, 6));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002107 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002108}
2109
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002110TEST_F(FileUtilTest, TouchFile) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002111 FilePath data_dir =
2112 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002113
2114 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002115 if (PathExists(data_dir)) {
2116 ASSERT_TRUE(DeleteFile(data_dir, true));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002117 }
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002118 ASSERT_TRUE(CreateDirectory(data_dir));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002119
2120 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
2121 std::string data("hello");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002122 ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length()));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002123
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002124 Time access_time;
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002125 // This timestamp is divisible by one day (in local timezone),
2126 // to make it work on FAT too.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002127 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002128 &access_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002129
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002130 Time modification_time;
jochen@chromium.orga6879772010-02-18 19:02:26 +09002131 // Note that this timestamp is divisible by two (seconds) - FAT stores
2132 // modification times with 2s resolution.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002133 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
jochen@chromium.orga6879772010-02-18 19:02:26 +09002134 &modification_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002135
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002136 ASSERT_TRUE(TouchFile(foobar, access_time, modification_time));
rvargas@chromium.orgb005b382014-01-07 19:06:58 +09002137 File::Info file_info;
brettw@chromium.orga9154032013-12-05 05:56:49 +09002138 ASSERT_TRUE(GetFileInfo(foobar, &file_info));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002139 EXPECT_EQ(access_time.ToInternalValue(),
2140 file_info.last_accessed.ToInternalValue());
2141 EXPECT_EQ(modification_time.ToInternalValue(),
2142 file_info.last_modified.ToInternalValue());
jochen@chromium.orga6879772010-02-18 19:02:26 +09002143}
2144
tfarina@chromium.org34828222010-05-26 10:40:12 +09002145TEST_F(FileUtilTest, IsDirectoryEmpty) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002146 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002147
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002148 ASSERT_FALSE(PathExists(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002149
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002150 ASSERT_TRUE(CreateDirectory(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002151
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002152 EXPECT_TRUE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002153
2154 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
2155 std::string bar("baz");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002156 ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length()));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002157
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002158 EXPECT_FALSE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002159}
2160
skerner@google.com93449ef2011-09-22 23:47:18 +09002161#if defined(OS_POSIX)
2162
2163// Testing VerifyPathControlledByAdmin() is hard, because there is no
2164// way a test can make a file owned by root, or change file paths
2165// at the root of the file system. VerifyPathControlledByAdmin()
2166// is implemented as a call to VerifyPathControlledByUser, which gives
2167// us the ability to test with paths under the test's temp directory,
2168// using a user id we control.
2169// Pull tests of VerifyPathControlledByUserTest() into a separate test class
2170// with a common SetUp() method.
2171class VerifyPathControlledByUserTest : public FileUtilTest {
2172 protected:
mostynb1fdbe1e2014-10-08 02:59:11 +09002173 virtual void SetUp() override {
skerner@google.com93449ef2011-09-22 23:47:18 +09002174 FileUtilTest::SetUp();
2175
2176 // Create a basic structure used by each test.
2177 // base_dir_
2178 // |-> sub_dir_
2179 // |-> text_file_
2180
2181 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002182 ASSERT_TRUE(CreateDirectory(base_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002183
2184 sub_dir_ = base_dir_.AppendASCII("sub_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002185 ASSERT_TRUE(CreateDirectory(sub_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002186
2187 text_file_ = sub_dir_.AppendASCII("file.txt");
2188 CreateTextFile(text_file_, L"This text file has some text in it.");
2189
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002190 // Get the user and group files are created with from |base_dir_|.
2191 struct stat stat_buf;
2192 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
2193 uid_ = stat_buf.st_uid;
skerner@chromium.org80784142011-10-18 06:30:29 +09002194 ok_gids_.insert(stat_buf.st_gid);
2195 bad_gids_.insert(stat_buf.st_gid + 1);
2196
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002197 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
skerner@google.com93449ef2011-09-22 23:47:18 +09002198
2199 // To ensure that umask settings do not cause the initial state
2200 // of permissions to be different from what we expect, explicitly
2201 // set permissions on the directories we create.
2202 // Make all files and directories non-world-writable.
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002203
2204 // Users and group can read, write, traverse
2205 int enabled_permissions =
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002206 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002207 // Other users can't read, write, traverse
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002208 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
skerner@google.com93449ef2011-09-22 23:47:18 +09002209
2210 ASSERT_NO_FATAL_FAILURE(
2211 ChangePosixFilePermissions(
2212 base_dir_, enabled_permissions, disabled_permissions));
2213 ASSERT_NO_FATAL_FAILURE(
2214 ChangePosixFilePermissions(
2215 sub_dir_, enabled_permissions, disabled_permissions));
2216 }
2217
2218 FilePath base_dir_;
2219 FilePath sub_dir_;
2220 FilePath text_file_;
2221 uid_t uid_;
skerner@chromium.org80784142011-10-18 06:30:29 +09002222
2223 std::set<gid_t> ok_gids_;
2224 std::set<gid_t> bad_gids_;
skerner@google.com93449ef2011-09-22 23:47:18 +09002225};
2226
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002227TEST_F(VerifyPathControlledByUserTest, BadPaths) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002228 // File does not exist.
2229 FilePath does_not_exist = base_dir_.AppendASCII("does")
2230 .AppendASCII("not")
2231 .AppendASCII("exist");
skerner@google.com93449ef2011-09-22 23:47:18 +09002232 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002233 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002234 base_dir_, does_not_exist, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002235
2236 // |base| not a subpath of |path|.
2237 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002238 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002239 sub_dir_, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002240
2241 // An empty base path will fail to be a prefix for any path.
2242 FilePath empty;
2243 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002244 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002245 empty, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002246
2247 // Finding that a bad call fails proves nothing unless a good call succeeds.
2248 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002249 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002250 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002251}
2252
2253TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2254 // Symlinks in the path should cause failure.
2255
2256 // Symlink to the file at the end of the path.
2257 FilePath file_link = base_dir_.AppendASCII("file_link");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002258 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
skerner@google.com93449ef2011-09-22 23:47:18 +09002259 << "Failed to create symlink.";
2260
2261 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002262 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002263 base_dir_, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002264 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002265 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002266 file_link, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002267
2268 // Symlink from one directory to another within the path.
2269 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002270 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
skerner@google.com93449ef2011-09-22 23:47:18 +09002271 << "Failed to create symlink.";
2272
2273 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002274 ASSERT_TRUE(PathExists(file_path_with_link));
skerner@google.com93449ef2011-09-22 23:47:18 +09002275
2276 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002277 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002278 base_dir_, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002279
2280 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002281 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002282 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002283
2284 // Symlinks in parents of base path are allowed.
2285 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002286 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002287 file_path_with_link, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002288}
2289
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002290TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002291 // Get a uid that is not the uid of files we create.
2292 uid_t bad_uid = uid_ + 1;
2293
skerner@google.com93449ef2011-09-22 23:47:18 +09002294 // Make all files and directories non-world-writable.
2295 ASSERT_NO_FATAL_FAILURE(
2296 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2297 ASSERT_NO_FATAL_FAILURE(
2298 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2299 ASSERT_NO_FATAL_FAILURE(
2300 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2301
2302 // We control these paths.
2303 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002304 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002305 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002306 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002307 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002308 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002309 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002310 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002311 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002312
2313 // Another user does not control these paths.
2314 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002315 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002316 base_dir_, sub_dir_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002317 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002318 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002319 base_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002320 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002321 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002322 sub_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002323
2324 // Another group does not control the paths.
2325 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002326 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002327 base_dir_, sub_dir_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002328 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002329 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002330 base_dir_, text_file_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002331 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002332 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002333 sub_dir_, text_file_, uid_, bad_gids_));
2334}
2335
2336TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2337 // Make all files and directories writable only by their owner.
2338 ASSERT_NO_FATAL_FAILURE(
2339 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2340 ASSERT_NO_FATAL_FAILURE(
2341 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2342 ASSERT_NO_FATAL_FAILURE(
2343 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2344
2345 // Any group is okay because the path is not group-writable.
2346 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002347 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002348 base_dir_, sub_dir_, uid_, ok_gids_));
2349 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002350 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002351 base_dir_, text_file_, uid_, ok_gids_));
2352 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002353 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002354 sub_dir_, text_file_, uid_, ok_gids_));
2355
2356 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002357 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002358 base_dir_, sub_dir_, uid_, bad_gids_));
2359 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002360 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002361 base_dir_, text_file_, uid_, bad_gids_));
2362 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002363 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002364 sub_dir_, text_file_, uid_, bad_gids_));
2365
2366 // No group is okay, because we don't check the group
2367 // if no group can write.
2368 std::set<gid_t> no_gids; // Empty set of gids.
2369 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002370 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002371 base_dir_, sub_dir_, uid_, no_gids));
2372 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002373 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002374 base_dir_, text_file_, uid_, no_gids));
2375 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002376 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002377 sub_dir_, text_file_, uid_, no_gids));
2378
2379
2380 // Make all files and directories writable by their group.
2381 ASSERT_NO_FATAL_FAILURE(
2382 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2383 ASSERT_NO_FATAL_FAILURE(
2384 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2385 ASSERT_NO_FATAL_FAILURE(
2386 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2387
2388 // Now |ok_gids_| works, but |bad_gids_| fails.
2389 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002390 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002391 base_dir_, sub_dir_, uid_, ok_gids_));
2392 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002393 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002394 base_dir_, text_file_, uid_, ok_gids_));
2395 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002396 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002397 sub_dir_, text_file_, uid_, ok_gids_));
2398
2399 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002400 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002401 base_dir_, sub_dir_, uid_, bad_gids_));
2402 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002403 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002404 base_dir_, text_file_, uid_, bad_gids_));
2405 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002406 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002407 sub_dir_, text_file_, uid_, bad_gids_));
2408
2409 // Because any group in the group set is allowed,
2410 // the union of good and bad gids passes.
2411
2412 std::set<gid_t> multiple_gids;
2413 std::set_union(
2414 ok_gids_.begin(), ok_gids_.end(),
2415 bad_gids_.begin(), bad_gids_.end(),
2416 std::inserter(multiple_gids, multiple_gids.begin()));
2417
2418 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002419 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002420 base_dir_, sub_dir_, uid_, multiple_gids));
2421 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002422 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002423 base_dir_, text_file_, uid_, multiple_gids));
2424 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002425 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002426 sub_dir_, text_file_, uid_, multiple_gids));
skerner@google.com93449ef2011-09-22 23:47:18 +09002427}
2428
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002429TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002430 // Make all files and directories non-world-writable.
2431 ASSERT_NO_FATAL_FAILURE(
2432 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2433 ASSERT_NO_FATAL_FAILURE(
2434 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2435 ASSERT_NO_FATAL_FAILURE(
2436 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2437
2438 // Initialy, we control all parts of the path.
2439 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002440 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002441 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002442 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002443 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002444 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002445 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002446 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002447 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002448
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09002449 // Make base_dir_ world-writable.
skerner@google.com93449ef2011-09-22 23:47:18 +09002450 ASSERT_NO_FATAL_FAILURE(
2451 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2452 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002453 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002454 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002455 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002456 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002457 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002458 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002459 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002460 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002461
2462 // Make sub_dir_ world writable.
2463 ASSERT_NO_FATAL_FAILURE(
2464 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2465 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002466 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002467 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002468 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002469 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002470 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002471 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002472 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002473 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002474
2475 // Make text_file_ world writable.
2476 ASSERT_NO_FATAL_FAILURE(
2477 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2478 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002479 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002480 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002481 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002482 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002483 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002484 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002485 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002486 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002487
2488 // Make sub_dir_ non-world writable.
2489 ASSERT_NO_FATAL_FAILURE(
2490 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2491 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002492 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002493 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002494 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002495 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002496 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002497 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002498 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002499 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002500
2501 // Make base_dir_ non-world-writable.
2502 ASSERT_NO_FATAL_FAILURE(
2503 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2504 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002505 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002506 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002507 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002508 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002509 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002510 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002511 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002512 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002513
2514 // Back to the initial state: Nothing is writable, so every path
2515 // should pass.
2516 ASSERT_NO_FATAL_FAILURE(
2517 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2518 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002519 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002520 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002521 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002522 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002523 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002524 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002525 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002526 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002527}
2528
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002529#if defined(OS_ANDROID)
2530TEST_F(FileUtilTest, ValidContentUriTest) {
2531 // Get the test image path.
2532 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002533 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002534 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002535 ASSERT_TRUE(PathExists(data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002536 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2537 int64 image_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002538 GetFileSize(image_file, &image_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002539 EXPECT_LT(0, image_size);
2540
2541 // Insert the image into MediaStore. MediaStore will do some conversions, and
2542 // return the content URI.
brettw@chromium.orgbdedd6c2014-08-08 07:57:11 +09002543 FilePath path = base::InsertImageIntoMediaStore(image_file);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002544 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002545 EXPECT_TRUE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002546 // The file size may not equal to the input image as MediaStore may convert
2547 // the image.
2548 int64 content_uri_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002549 GetFileSize(path, &content_uri_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002550 EXPECT_EQ(image_size, content_uri_size);
2551
2552 // We should be able to read the file.
2553 char* buffer = new char[image_size];
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002554 File file = OpenContentUriForRead(path);
2555 EXPECT_TRUE(file.IsValid());
2556 EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002557 delete[] buffer;
2558}
2559
2560TEST_F(FileUtilTest, NonExistentContentUriTest) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002561 FilePath path("content://foo.bar");
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002562 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002563 EXPECT_FALSE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002564 // Size should be smaller than 0.
2565 int64 size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002566 EXPECT_FALSE(GetFileSize(path, &size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002567
2568 // We should not be able to read the file.
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002569 File file = OpenContentUriForRead(path);
2570 EXPECT_FALSE(file.IsValid());
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002571}
2572#endif
2573
jln@chromium.orgd7493932014-03-01 00:35:36 +09002574TEST(ScopedFD, ScopedFDDoesClose) {
2575 int fds[2];
2576 char c = 0;
2577 ASSERT_EQ(0, pipe(fds));
2578 const int write_end = fds[1];
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002579 base::ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002580 {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002581 base::ScopedFD write_end_closer(fds[1]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002582 }
2583 // This is the only thread. This file descriptor should no longer be valid.
2584 int ret = close(write_end);
2585 EXPECT_EQ(-1, ret);
2586 EXPECT_EQ(EBADF, errno);
2587 // Make sure read(2) won't block.
2588 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK));
2589 // Reading the pipe should EOF.
2590 EXPECT_EQ(0, read(fds[0], &c, 1));
2591}
2592
2593#if defined(GTEST_HAS_DEATH_TEST)
2594void CloseWithScopedFD(int fd) {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002595 base::ScopedFD fd_closer(fd);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002596}
2597#endif
2598
2599TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) {
2600 int fds[2];
2601 ASSERT_EQ(0, pipe(fds));
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002602 base::ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002603 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1])));
2604#if defined(GTEST_HAS_DEATH_TEST)
2605 // This is the only thread. This file descriptor should no longer be valid.
2606 // Trying to close it should crash. This is important for security.
2607 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2608#endif
2609}
2610
skerner@google.com93449ef2011-09-22 23:47:18 +09002611#endif // defined(OS_POSIX)
2612
mark@chromium.org17684802008-09-10 09:16:28 +09002613} // namespace
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002614
2615} // namespace base