blob: 1d8eeef653519a2b1492d2eeaecd7b6724a95298 [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"
rvargas4e1aefc2015-02-10 07:43:44 +090033#include "base/strings/string_util.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090034#include "base/strings/utf_string_conversions.h"
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +090035#include "base/test/test_file_util.h"
brettw@chromium.org61391822011-01-01 05:02:16 +090036#include "base/threading/platform_thread.h"
initial.commit3f4a7322008-07-27 06:49:38 +090037#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090038#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090039
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090040#if defined(OS_WIN)
41#include "base/win/scoped_handle.h"
rvargas@chromium.org56472942013-08-15 05:46:05 +090042#include "base/win/windows_version.h"
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090043#endif
44
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090045#if defined(OS_ANDROID)
46#include "base/android/content_uri_utils.h"
47#endif
48
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090049// This macro helps avoid wrapped lines in the test structs.
50#define FPL(x) FILE_PATH_LITERAL(x)
51
brettw@chromium.org2873d9b2013-11-28 08:22:08 +090052namespace base {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090053
initial.commit3f4a7322008-07-27 06:49:38 +090054namespace {
55
brettw@chromium.org01f3da42014-08-14 05:22:14 +090056// To test that NormalizeFilePath() deals with NTFS reparse points correctly,
57// we need functions to create and delete reparse points.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090058#if defined(OS_WIN)
59typedef struct _REPARSE_DATA_BUFFER {
60 ULONG ReparseTag;
61 USHORT ReparseDataLength;
62 USHORT Reserved;
63 union {
64 struct {
65 USHORT SubstituteNameOffset;
66 USHORT SubstituteNameLength;
67 USHORT PrintNameOffset;
68 USHORT PrintNameLength;
69 ULONG Flags;
70 WCHAR PathBuffer[1];
71 } SymbolicLinkReparseBuffer;
72 struct {
73 USHORT SubstituteNameOffset;
74 USHORT SubstituteNameLength;
75 USHORT PrintNameOffset;
76 USHORT PrintNameLength;
77 WCHAR PathBuffer[1];
78 } MountPointReparseBuffer;
79 struct {
80 UCHAR DataBuffer[1];
81 } GenericReparseBuffer;
82 };
83} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
84
85// Sets a reparse point. |source| will now point to |target|. Returns true if
86// the call succeeds, false otherwise.
87bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
88 std::wstring kPathPrefix = L"\\??\\";
89 std::wstring target_str;
90 // The juction will not work if the target path does not start with \??\ .
91 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
92 target_str += kPathPrefix;
93 target_str += target_path.value();
94 const wchar_t* target = target_str.c_str();
95 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
96 char buffer[2000] = {0};
97 DWORD returned;
98
99 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
100
101 data->ReparseTag = 0xa0000003;
102 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
103
104 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
105 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
106 data->ReparseDataLength = size_target + 4 + 8;
107
108 int data_size = data->ReparseDataLength + 8;
109
110 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
111 NULL, 0, &returned, NULL)) {
112 return false;
113 }
114 return true;
115}
116
117// Delete the reparse point referenced by |source|. Returns true if the call
118// succeeds, false otherwise.
119bool DeleteReparsePoint(HANDLE source) {
120 DWORD returned;
121 REPARSE_DATA_BUFFER data = {0};
122 data.ReparseTag = 0xa0000003;
123 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
124 &returned, NULL)) {
125 return false;
126 }
127 return true;
128}
rvargas@chromium.org56472942013-08-15 05:46:05 +0900129
130// Manages a reparse point for a test.
131class ReparsePoint {
132 public:
133 // Creates a reparse point from |source| (an empty directory) to |target|.
134 ReparsePoint(const FilePath& source, const FilePath& target) {
135 dir_.Set(
136 ::CreateFile(source.value().c_str(),
137 FILE_ALL_ACCESS,
138 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
139 NULL,
140 OPEN_EXISTING,
141 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
142 NULL));
rvargas14fb0e02014-09-24 07:03:11 +0900143 created_ = dir_.IsValid() && SetReparsePoint(dir_.Get(), target);
rvargas@chromium.org56472942013-08-15 05:46:05 +0900144 }
145
146 ~ReparsePoint() {
147 if (created_)
rvargas14fb0e02014-09-24 07:03:11 +0900148 DeleteReparsePoint(dir_.Get());
rvargas@chromium.org56472942013-08-15 05:46:05 +0900149 }
150
151 bool IsValid() { return created_; }
152
153 private:
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900154 win::ScopedHandle dir_;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900155 bool created_;
156 DISALLOW_COPY_AND_ASSIGN(ReparsePoint);
157};
158
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900159#endif
160
skerner@google.com93449ef2011-09-22 23:47:18 +0900161#if defined(OS_POSIX)
162// Provide a simple way to change the permissions bits on |path| in tests.
163// ASSERT failures will return, but not stop the test. Caller should wrap
164// calls to this function in ASSERT_NO_FATAL_FAILURE().
165void ChangePosixFilePermissions(const FilePath& path,
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900166 int mode_bits_to_set,
167 int mode_bits_to_clear) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900168 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
169 << "Can't set and clear the same bits.";
170
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900171 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900172 ASSERT_TRUE(GetPosixFilePermissions(path, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900173 mode |= mode_bits_to_set;
174 mode &= ~mode_bits_to_clear;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900175 ASSERT_TRUE(SetPosixFilePermissions(path, mode));
skerner@google.com93449ef2011-09-22 23:47:18 +0900176}
177#endif // defined(OS_POSIX)
178
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900179const wchar_t bogus_content[] = L"I'm cannon fodder.";
180
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900181const int FILES_AND_DIRECTORIES =
brettw@chromium.org56946722013-06-08 13:53:36 +0900182 FileEnumerator::FILES | FileEnumerator::DIRECTORIES;
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900183
erikkay@google.comf2406842008-08-21 00:59:49 +0900184// file_util winds up using autoreleased objects on the Mac, so this needs
185// to be a PlatformTest
186class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900187 protected:
dchengca87abb2014-12-23 11:56:47 +0900188 void SetUp() override {
erikkay@google.comf2406842008-08-21 00:59:49 +0900189 PlatformTest::SetUp();
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900190 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commit3f4a7322008-07-27 06:49:38 +0900191 }
192
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900193 ScopedTempDir temp_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900194};
195
196// Collects all the results from the given file enumerator, and provides an
197// interface to query whether a given file is present.
198class FindResultCollector {
199 public:
danakjd40295f2015-03-10 10:48:05 +0900200 explicit FindResultCollector(FileEnumerator* enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900201 FilePath cur_file;
danakjd40295f2015-03-10 10:48:05 +0900202 while (!(cur_file = enumerator->Next()).value().empty()) {
avi@google.com5cb79352008-12-11 23:55:12 +0900203 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900204 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900205 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900206 << "Same file returned twice";
207
208 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900209 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900210 }
211 }
212
213 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900214 bool HasFile(const FilePath& file) const {
215 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900216 }
evanm@google.com874d1672008-10-31 08:54:04 +0900217
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900218 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900219 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900220 }
initial.commit3f4a7322008-07-27 06:49:38 +0900221
222 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900223 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900224};
225
226// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900227void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900228 const std::wstring& contents) {
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900229 std::wofstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900230 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900231 ASSERT_TRUE(file.is_open());
232 file << contents;
233 file.close();
234}
235
236// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900237std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900238 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900239 std::wifstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900240 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900241 EXPECT_TRUE(file.is_open());
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900242 file.getline(contents, arraysize(contents));
initial.commit3f4a7322008-07-27 06:49:38 +0900243 file.close();
244 return std::wstring(contents);
245}
246
erikkay@google.com014161d2008-08-16 02:45:13 +0900247#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900248uint64 FileTimeAsUint64(const FILETIME& ft) {
249 ULARGE_INTEGER u;
250 u.LowPart = ft.dwLowDateTime;
251 u.HighPart = ft.dwHighDateTime;
252 return u.QuadPart;
253}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900254#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900255
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900256TEST_F(FileUtilTest, FileAndDirectorySize) {
257 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
258 // should return 53 bytes.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900259 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900260 CreateTextFile(file_01, L"12345678901234567890");
261 int64 size_f1 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900262 ASSERT_TRUE(GetFileSize(file_01, &size_f1));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900263 EXPECT_EQ(20ll, size_f1);
264
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900265 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900266 CreateDirectory(subdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900267
268 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
269 CreateTextFile(file_02, L"123456789012345678901234567890");
270 int64 size_f2 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900271 ASSERT_TRUE(GetFileSize(file_02, &size_f2));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900272 EXPECT_EQ(30ll, size_f2);
273
274 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900275 CreateDirectory(subsubdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900276
277 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
278 CreateTextFile(file_03, L"123");
279
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900280 int64 computed_size = ComputeDirectorySize(temp_dir_.path());
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900281 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
282}
283
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900284TEST_F(FileUtilTest, NormalizeFilePathBasic) {
285 // Create a directory under the test dir. Because we create it,
286 // we know it is not a link.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900287 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
288 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900289 FilePath file_b_path = dir_path.Append(FPL("file_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900290 CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900291
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900292 FilePath normalized_file_a_path, normalized_file_b_path;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900293 ASSERT_FALSE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900294 ASSERT_FALSE(NormalizeFilePath(file_a_path, &normalized_file_a_path))
viettrungluu@chromium.orgea703f12010-08-23 01:19:13 +0900295 << "NormalizeFilePath() should fail on nonexistent paths.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900296
297 CreateTextFile(file_a_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900298 ASSERT_TRUE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900299 ASSERT_TRUE(NormalizeFilePath(file_a_path, &normalized_file_a_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900300
301 CreateTextFile(file_b_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900302 ASSERT_TRUE(PathExists(file_b_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900303 ASSERT_TRUE(NormalizeFilePath(file_b_path, &normalized_file_b_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900304
305 // Beacuse this test created |dir_path|, we know it is not a link
306 // or junction. So, the real path of the directory holding file a
307 // must be the parent of the path holding file b.
308 ASSERT_TRUE(normalized_file_a_path.DirName()
309 .IsParent(normalized_file_b_path.DirName()));
310}
311
312#if defined(OS_WIN)
313
314TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
315 // Build the following directory structure:
316 //
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900317 // temp_dir
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900318 // |-> base_a
319 // | |-> sub_a
320 // | |-> file.txt
321 // | |-> long_name___... (Very long name.)
322 // | |-> sub_long
323 // | |-> deep.txt
324 // |-> base_b
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900325 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
326 // |-> to_base_b (reparse point to temp_dir\base_b)
327 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900328
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900329 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
rvargas4e1aefc2015-02-10 07:43:44 +0900330#if defined(OS_WIN)
331 // TEMP can have a lower case drive letter.
332 string16 temp_base_a = base_a.value();
333 ASSERT_FALSE(temp_base_a.empty());
brettwbb5442f2015-06-10 05:20:14 +0900334 *temp_base_a.begin() = ToUpperASCII(*temp_base_a.begin());
rvargas4e1aefc2015-02-10 07:43:44 +0900335 base_a = FilePath(temp_base_a);
336#endif
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900337 ASSERT_TRUE(CreateDirectory(base_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900338
339 FilePath sub_a = base_a.Append(FPL("sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900340 ASSERT_TRUE(CreateDirectory(sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900341
342 FilePath file_txt = sub_a.Append(FPL("file.txt"));
343 CreateTextFile(file_txt, bogus_content);
344
345 // Want a directory whose name is long enough to make the path to the file
346 // inside just under MAX_PATH chars. This will be used to test that when
347 // a junction expands to a path over MAX_PATH chars in length,
348 // NormalizeFilePath() fails without crashing.
349 FilePath sub_long_rel(FPL("sub_long"));
350 FilePath deep_txt(FPL("deep.txt"));
351
352 int target_length = MAX_PATH;
353 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
354 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900355 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900356 // the resulting path is still long enough to hit the failing case in
357 // NormalizePath().
358 const int kCreateDirLimit = 4;
359 target_length -= kCreateDirLimit;
360 FilePath::StringType long_name_str = FPL("long_name_");
361 long_name_str.resize(target_length, '_');
362
363 FilePath long_name = sub_a.Append(FilePath(long_name_str));
364 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
365 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
366
367 FilePath sub_long = deep_file.DirName();
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900368 ASSERT_TRUE(CreateDirectory(sub_long));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900369 CreateTextFile(deep_file, bogus_content);
370
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900371 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900372 ASSERT_TRUE(CreateDirectory(base_b));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900373
374 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900375 ASSERT_TRUE(CreateDirectory(to_sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900376 FilePath normalized_path;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900377 {
378 ReparsePoint reparse_to_sub_a(to_sub_a, sub_a);
379 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900380
rvargas@chromium.org56472942013-08-15 05:46:05 +0900381 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900382 ASSERT_TRUE(CreateDirectory(to_base_b));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900383 ReparsePoint reparse_to_base_b(to_base_b, base_b);
384 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900385
rvargas@chromium.org56472942013-08-15 05:46:05 +0900386 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900387 ASSERT_TRUE(CreateDirectory(to_sub_long));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900388 ReparsePoint reparse_to_sub_long(to_sub_long, sub_long);
389 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900390
rvargas@chromium.org56472942013-08-15 05:46:05 +0900391 // Normalize a junction free path: base_a\sub_a\file.txt .
brettw@chromium.org70684242013-12-05 03:22:49 +0900392 ASSERT_TRUE(NormalizeFilePath(file_txt, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900393 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
394
395 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
396 // the junction to_sub_a.
brettw@chromium.org70684242013-12-05 03:22:49 +0900397 ASSERT_TRUE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900398 &normalized_path));
399 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
400
401 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
402 // normalized to exclude junctions to_base_b and to_sub_a .
brettw@chromium.org70684242013-12-05 03:22:49 +0900403 ASSERT_TRUE(NormalizeFilePath(base_b.Append(FPL("to_base_b"))
rvargas@chromium.org56472942013-08-15 05:46:05 +0900404 .Append(FPL("to_base_b"))
405 .Append(FPL("to_sub_a"))
406 .Append(FPL("file.txt")),
407 &normalized_path));
408 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
409
410 // A long enough path will cause NormalizeFilePath() to fail. Make a long
411 // path using to_base_b many times, and check that paths long enough to fail
412 // do not cause a crash.
413 FilePath long_path = base_b;
414 const int kLengthLimit = MAX_PATH + 200;
415 while (long_path.value().length() <= kLengthLimit) {
416 long_path = long_path.Append(FPL("to_base_b"));
417 }
418 long_path = long_path.Append(FPL("to_sub_a"))
419 .Append(FPL("file.txt"));
420
brettw@chromium.org70684242013-12-05 03:22:49 +0900421 ASSERT_FALSE(NormalizeFilePath(long_path, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900422
423 // Normalizing the junction to deep.txt should fail, because the expanded
424 // path to deep.txt is longer than MAX_PATH.
brettw@chromium.org70684242013-12-05 03:22:49 +0900425 ASSERT_FALSE(NormalizeFilePath(to_sub_long.Append(deep_txt),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900426 &normalized_path));
427
428 // Delete the reparse points, and see that NormalizeFilePath() fails
429 // to traverse them.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900430 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900431
brettw@chromium.org70684242013-12-05 03:22:49 +0900432 ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900433 &normalized_path));
434}
435
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900436TEST_F(FileUtilTest, DevicePathToDriveLetter) {
437 // Get a drive letter.
438 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
rvargas4e1aefc2015-02-10 07:43:44 +0900439 StringToUpperASCII(&real_drive_letter);
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900440 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
441 LOG(ERROR) << "Can't get a drive letter to test with.";
442 return;
443 }
444
445 // Get the NT style path to that drive.
446 wchar_t device_path[MAX_PATH] = {'\0'};
447 ASSERT_TRUE(
448 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
449 FilePath actual_device_path(device_path);
450 FilePath win32_path;
451
452 // Run DevicePathToDriveLetterPath() on the NT style path we got from
453 // QueryDosDevice(). Expect the drive letter we started with.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900454 ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path, &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900455 ASSERT_EQ(real_drive_letter, win32_path.value());
456
457 // Add some directories to the path. Expect those extra path componenets
458 // to be preserved.
459 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900460 ASSERT_TRUE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900461 actual_device_path.Append(kRelativePath),
462 &win32_path));
463 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
464 win32_path.value());
465
466 // Deform the real path so that it is invalid by removing the last four
467 // characters. The way windows names devices that are hard disks
468 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
469 // than three characters. The only way the truncated string could be a
470 // real drive is if more than 10^3 disks are mounted:
471 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
472 // Check that DevicePathToDriveLetterPath fails.
473 int path_length = actual_device_path.value().length();
474 int new_length = path_length - 4;
475 ASSERT_LT(0, new_length);
476 FilePath prefix_of_real_device_path(
477 actual_device_path.value().substr(0, new_length));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900478 ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path,
479 &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900480
brettw@chromium.orga9154032013-12-05 05:56:49 +0900481 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900482 prefix_of_real_device_path.Append(kRelativePath),
483 &win32_path));
484
485 // Deform the real path so that it is invalid by adding some characters. For
486 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
487 // request for the drive letter whose native path is
488 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
489 // because drives are numbered in order and mounting 112345 hard disks will
490 // never happen.
491 const FilePath::StringType kExtraChars = FPL("12345");
492
493 FilePath real_device_path_plus_numbers(
494 actual_device_path.value() + kExtraChars);
495
brettw@chromium.orga9154032013-12-05 05:56:49 +0900496 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900497 real_device_path_plus_numbers,
498 &win32_path));
499
brettw@chromium.orga9154032013-12-05 05:56:49 +0900500 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900501 real_device_path_plus_numbers.Append(kRelativePath),
502 &win32_path));
503}
504
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900505TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
506 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
507 // if it is available. This test requires that:
508 // - the filesystem at |temp_dir_| supports long filenames.
509 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
510 // directories of |temp_dir_|.
511 const FilePath::CharType kLongDirName[] = FPL("A long path");
512 const FilePath::CharType kTestSubDirName[] = FPL("test");
513 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900514 ASSERT_TRUE(CreateDirectory(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900515
516 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
517 // different short name.
518 WCHAR path_buffer[MAX_PATH];
519 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
520 path_buffer, MAX_PATH);
521 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
522 ASSERT_NE(DWORD(0), path_buffer_length);
523 FilePath short_test_dir(path_buffer);
524 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
525
526 FilePath temp_file;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900527 ASSERT_TRUE(CreateTemporaryFileInDir(short_test_dir, &temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900528 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900529 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900530
531 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
532 // unreadable. We should still be able to create a temp file in the
533 // subdirectory, but we won't be able to determine the long path for it. This
534 // mimics the environment that some users run where their user profiles reside
535 // in a location where the don't have full access to the higher level
536 // directories. (Note that this assumption is true for NTFS, but not for some
537 // network file systems. E.g. AFS).
538 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900539 ASSERT_TRUE(CreateDirectory(access_test_dir));
brettwbb5442f2015-06-10 05:20:14 +0900540 FilePermissionRestorer long_test_dir_restorer(long_test_dir);
541 ASSERT_TRUE(MakeFileUnreadable(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900542
543 // Use the short form of the directory to create a temporary filename.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900544 ASSERT_TRUE(CreateTemporaryFileInDir(
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900545 short_test_dir.Append(kTestSubDirName), &temp_file));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900546 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900547 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
548
549 // Check that the long path can't be determined for |temp_file|.
550 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
551 path_buffer, MAX_PATH);
552 EXPECT_EQ(DWORD(0), path_buffer_length);
553}
554
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900555#endif // defined(OS_WIN)
556
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900557#if defined(OS_POSIX)
558
559TEST_F(FileUtilTest, CreateAndReadSymlinks) {
560 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
561 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
562 CreateTextFile(link_to, bogus_content);
563
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900564 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900565 << "Failed to create file symlink.";
566
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900567 // If we created the link properly, we should be able to read the contents
568 // through it.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900569 std::wstring contents = ReadTextFile(link_from);
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900570 EXPECT_EQ(bogus_content, contents);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900571
572 FilePath result;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900573 ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900574 EXPECT_EQ(link_to.value(), result.value());
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900575
576 // Link to a directory.
577 link_from = temp_dir_.path().Append(FPL("from_dir"));
578 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900579 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900580 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900581 << "Failed to create directory symlink.";
582
583 // Test failures.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900584 EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
585 EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900586 FilePath missing = temp_dir_.path().Append(FPL("missing"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900587 EXPECT_FALSE(ReadSymbolicLink(missing, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900588}
589
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900590// The following test of NormalizeFilePath() require that we create a symlink.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900591// This can not be done on Windows before Vista. On Vista, creating a symlink
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900592// requires privilege "SeCreateSymbolicLinkPrivilege".
593// TODO(skerner): Investigate the possibility of giving base_unittests the
594// privileges required to create a symlink.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900595TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
skerner@chromium.org559baa92010-05-13 00:13:57 +0900596 // Link one file to another.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900597 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
598 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900599 CreateTextFile(link_to, bogus_content);
600
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900601 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900602 << "Failed to create file symlink.";
603
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900604 // Check that NormalizeFilePath sees the link.
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900605 FilePath normalized_path;
brettw@chromium.org70684242013-12-05 03:22:49 +0900606 ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900607 EXPECT_NE(link_from, link_to);
608 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
609 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900610
611 // Link to a directory.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900612 link_from = temp_dir_.path().Append(FPL("from_dir"));
613 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900614 ASSERT_TRUE(CreateDirectory(link_to));
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 directory symlink.";
617
brettw@chromium.org70684242013-12-05 03:22:49 +0900618 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900619 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900620
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900621 // Test that a loop in the links causes NormalizeFilePath() to return false.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900622 link_from = temp_dir_.path().Append(FPL("link_a"));
623 link_to = temp_dir_.path().Append(FPL("link_b"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900624 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900625 << "Failed to create loop symlink a.";
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900626 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900627 << "Failed to create loop symlink b.";
628
629 // Infinite loop!
brettw@chromium.org70684242013-12-05 03:22:49 +0900630 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900631}
632#endif // defined(OS_POSIX)
633
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900634TEST_F(FileUtilTest, DeleteNonExistent) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900635 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900636 ASSERT_FALSE(PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900637
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900638 EXPECT_TRUE(DeleteFile(non_existent, false));
639 ASSERT_FALSE(PathExists(non_existent));
640 EXPECT_TRUE(DeleteFile(non_existent, true));
641 ASSERT_FALSE(PathExists(non_existent));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900642}
643
ericu@chromium.org472c56f2014-05-22 12:22:48 +0900644TEST_F(FileUtilTest, DeleteNonExistentWithNonExistentParent) {
645 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_topdir");
646 non_existent = non_existent.AppendASCII("bogus_subdir");
647 ASSERT_FALSE(PathExists(non_existent));
648
649 EXPECT_TRUE(DeleteFile(non_existent, false));
650 ASSERT_FALSE(PathExists(non_existent));
651 EXPECT_TRUE(DeleteFile(non_existent, true));
652 ASSERT_FALSE(PathExists(non_existent));
653}
654
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900655TEST_F(FileUtilTest, DeleteFile) {
656 // Create a file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900657 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.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));
initial.commit3f4a7322008-07-27 06:49:38 +0900660
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900661 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900662 EXPECT_TRUE(DeleteFile(file_name, false));
663 EXPECT_FALSE(PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900664
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900665 // Test recursive case, create a new file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900666 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900667 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900668 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900669
670 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900671 EXPECT_TRUE(DeleteFile(file_name, true));
672 EXPECT_FALSE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900673}
674
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900675#if defined(OS_POSIX)
676TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900677 // Create a file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900678 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
679 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900680 ASSERT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900681
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900682 // Create a symlink to the file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900683 FilePath file_link = temp_dir_.path().Append("file_link_2");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900684 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900685 << "Failed to create symlink.";
686
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900687 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900688 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900689
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900690 // Make sure original file is not deleted.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900691 EXPECT_FALSE(PathExists(file_link));
692 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900693}
694
695TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900696 // Create a non-existent file path.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900697 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900698 EXPECT_FALSE(PathExists(non_existent));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900699
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900700 // Create a symlink to the non-existent file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900701 FilePath file_link = temp_dir_.path().Append("file_link_3");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900702 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900703 << "Failed to create symlink.";
704
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900705 // Make sure the symbolic link is exist.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900706 EXPECT_TRUE(IsLink(file_link));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900707 EXPECT_FALSE(PathExists(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900708
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900709 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900710 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900711
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900712 // Make sure the symbolic link is deleted.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900713 EXPECT_FALSE(IsLink(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900714}
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900715
716TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
717 // Create a file path.
718 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900719 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900720
721 const std::string kData("hello");
722
723 int buffer_size = kData.length();
724 char* buffer = new char[buffer_size];
725
726 // Write file.
727 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900728 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900729 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900730
731 // Make sure the file is readable.
732 int32 mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900733 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
734 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900735
736 // Get rid of the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900737 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
738 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
739 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900740 // Make sure the file can't be read.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900741 EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900742
743 // Give the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900744 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
745 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
746 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900747 // Make sure the file can be read.
748 EXPECT_EQ(static_cast<int>(kData.length()),
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900749 ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900750
751 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900752 EXPECT_TRUE(DeleteFile(file_name, false));
753 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900754
755 delete[] buffer;
756}
757
758TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
759 // Create a file path.
760 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900761 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900762
763 const std::string kData("hello");
764
765 // Write file.
766 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900767 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900768 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900769
770 // Make sure the file is writable.
771 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900772 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
773 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900774 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900775
776 // Get rid of the write permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900777 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
778 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
779 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900780 // Make sure the file can't be write.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900781 EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900782 EXPECT_FALSE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900783
784 // Give read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900785 EXPECT_TRUE(SetPosixFilePermissions(file_name,
786 FILE_PERMISSION_WRITE_BY_USER));
787 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
788 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900789 // Make sure the file can be write.
790 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900791 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900792 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900793
794 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900795 EXPECT_TRUE(DeleteFile(file_name, false));
796 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900797}
798
799TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
800 // Create a directory path.
801 FilePath subdir_path =
802 temp_dir_.path().Append(FPL("PermissionTest1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900803 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900804 ASSERT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900805
806 // Create a dummy file to enumerate.
807 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900808 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900809 const std::string kData("hello");
810 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900811 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900812 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900813
814 // Make sure the directory has the all permissions.
815 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900816 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
817 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900818
819 // Get rid of the permissions from the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900820 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
821 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
822 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900823
824 // Make sure the file in the directory can't be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900825 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900826 EXPECT_TRUE(PathExists(subdir_path));
danakjd40295f2015-03-10 10:48:05 +0900827 FindResultCollector c1(&f1);
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +0900828 EXPECT_EQ(0, c1.size());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900829 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900830
831 // Give the permissions to the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900832 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
833 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
834 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900835
836 // Make sure the file in the directory can be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900837 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
danakjd40295f2015-03-10 10:48:05 +0900838 FindResultCollector c2(&f2);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900839 EXPECT_TRUE(c2.HasFile(file_name));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +0900840 EXPECT_EQ(1, c2.size());
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900841
842 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900843 EXPECT_TRUE(DeleteFile(subdir_path, true));
844 EXPECT_FALSE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900845}
846
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900847#endif // defined(OS_POSIX)
848
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900849#if defined(OS_WIN)
850// Tests that the Delete function works for wild cards, especially
851// with the recursion flag. Also coincidentally tests PathExists.
852// TODO(erikkay): see if anyone's actually using this feature of the API
853TEST_F(FileUtilTest, DeleteWildCard) {
854 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900855 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900856 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900857 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900858
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900859 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900860 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900861 ASSERT_TRUE(PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900862
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900863 // Create the wildcard path
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900864 FilePath directory_contents = temp_dir_.path();
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900865 directory_contents = directory_contents.Append(FPL("*"));
866
initial.commit3f4a7322008-07-27 06:49:38 +0900867 // Delete non-recursively and check that only the file is deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900868 EXPECT_TRUE(DeleteFile(directory_contents, false));
869 EXPECT_FALSE(PathExists(file_name));
870 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900871
zork@chromium.org61be4f42010-05-07 09:05:36 +0900872 // Delete recursively and make sure all contents are deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900873 EXPECT_TRUE(DeleteFile(directory_contents, true));
874 EXPECT_FALSE(PathExists(file_name));
875 EXPECT_FALSE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900876}
877
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900878// TODO(erikkay): see if anyone's actually using this feature of the API
879TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
880 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900881 FilePath subdir_path =
882 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900883 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900884 ASSERT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900885
886 // Create the wildcard path
887 FilePath directory_contents = subdir_path;
888 directory_contents = directory_contents.Append(FPL("*"));
889
890 // Delete non-recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900891 EXPECT_TRUE(DeleteFile(directory_contents, false));
892 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900893
894 // Delete recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900895 EXPECT_TRUE(DeleteFile(directory_contents, true));
896 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900897}
898#endif
899
900// Tests non-recursive Delete() for a directory.
901TEST_F(FileUtilTest, DeleteDirNonRecursive) {
902 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900903 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900904 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900905 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900906
907 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
908 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900909 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900910
911 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900912 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900913 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900914
915 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900916 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900917 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900918
919 // Delete non-recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900920 EXPECT_TRUE(DeleteFile(subdir_path2, false));
921 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900922
923 // Delete non-recursively and check that nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900924 EXPECT_FALSE(DeleteFile(test_subdir, false));
925 EXPECT_TRUE(PathExists(test_subdir));
926 EXPECT_TRUE(PathExists(file_name));
927 EXPECT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900928}
929
930// Tests recursive Delete() for a directory.
931TEST_F(FileUtilTest, DeleteDirRecursive) {
932 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900933 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900934 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900935 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900936
937 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
938 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900939 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900940
941 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900942 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900943 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900944
945 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900946 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900947 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900948
949 // Delete recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900950 EXPECT_TRUE(DeleteFile(subdir_path2, true));
951 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900952
953 // Delete recursively and check that everything got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900954 EXPECT_TRUE(DeleteFile(test_subdir, true));
955 EXPECT_FALSE(PathExists(file_name));
956 EXPECT_FALSE(PathExists(subdir_path1));
957 EXPECT_FALSE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900958}
959
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900960TEST_F(FileUtilTest, MoveFileNew) {
961 // Create a file
962 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900963 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900964 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900965 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900966
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900967 // The destination.
968 FilePath file_name_to = temp_dir_.path().Append(
969 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900970 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900971
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900972 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900973
974 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900975 EXPECT_FALSE(PathExists(file_name_from));
976 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900977}
978
979TEST_F(FileUtilTest, MoveFileExists) {
980 // Create a file
981 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900982 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900983 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900984 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900985
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900986 // The destination name.
987 FilePath file_name_to = temp_dir_.path().Append(
988 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900989 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900990 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900991
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900992 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900993
994 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900995 EXPECT_FALSE(PathExists(file_name_from));
996 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900997 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
998}
999
1000TEST_F(FileUtilTest, MoveFileDirExists) {
1001 // Create a file
1002 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001003 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001004 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001005 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001006
1007 // The destination directory
1008 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001009 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001010 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001011 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001012
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001013 EXPECT_FALSE(Move(file_name_from, dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001014}
1015
1016
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001017TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001018 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001019 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001020 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001021 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001022 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001023
1024 // Create a file under the directory
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001025 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1026 FilePath file_name_from = dir_name_from.Append(txt_file_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001027 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001028 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001029
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001030 // Move the directory.
1031 FilePath dir_name_to =
1032 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001033 FilePath file_name_to =
1034 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001035
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001036 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001037
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001038 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001039
1040 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001041 EXPECT_FALSE(PathExists(dir_name_from));
1042 EXPECT_FALSE(PathExists(file_name_from));
1043 EXPECT_TRUE(PathExists(dir_name_to));
1044 EXPECT_TRUE(PathExists(file_name_to));
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001045
1046 // Test path traversal.
1047 file_name_from = dir_name_to.Append(txt_file_name);
1048 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1049 file_name_to = file_name_to.Append(txt_file_name);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001050 EXPECT_FALSE(Move(file_name_from, file_name_to));
1051 EXPECT_TRUE(PathExists(file_name_from));
1052 EXPECT_FALSE(PathExists(file_name_to));
1053 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1054 EXPECT_FALSE(PathExists(file_name_from));
1055 EXPECT_TRUE(PathExists(file_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001056}
1057
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001058TEST_F(FileUtilTest, MoveExist) {
1059 // Create a directory
1060 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001061 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001062 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001063 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001064
1065 // Create a file under the directory
1066 FilePath file_name_from =
1067 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1068 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001069 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001070
1071 // Move the directory
1072 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001073 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001074
1075 FilePath dir_name_to =
1076 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1077 FilePath file_name_to =
1078 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1079
1080 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001081 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001082 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001083
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001084 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001085
1086 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001087 EXPECT_FALSE(PathExists(dir_name_from));
1088 EXPECT_FALSE(PathExists(file_name_from));
1089 EXPECT_TRUE(PathExists(dir_name_to));
1090 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001091}
1092
1093TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001094 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001095 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001096 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001097 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001098 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001099
1100 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001101 FilePath file_name_from =
1102 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001103 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001104 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001105
1106 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001107 FilePath subdir_name_from =
1108 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001109 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001110 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001111
1112 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001113 FilePath file_name2_from =
1114 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001115 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001116 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001117
1118 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001119 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001120 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001121 FilePath file_name_to =
1122 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1123 FilePath subdir_name_to =
1124 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1125 FilePath file_name2_to =
1126 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001127
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001128 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001129
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001130 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001131
1132 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001133 EXPECT_TRUE(PathExists(dir_name_from));
1134 EXPECT_TRUE(PathExists(file_name_from));
1135 EXPECT_TRUE(PathExists(subdir_name_from));
1136 EXPECT_TRUE(PathExists(file_name2_from));
1137 EXPECT_TRUE(PathExists(dir_name_to));
1138 EXPECT_TRUE(PathExists(file_name_to));
1139 EXPECT_TRUE(PathExists(subdir_name_to));
1140 EXPECT_TRUE(PathExists(file_name2_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001141}
1142
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001143TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1144 // Create a directory.
1145 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001146 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001147 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001148 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001149
1150 // Create a file under the directory.
1151 FilePath file_name_from =
1152 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1153 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001154 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001155
1156 // Create a subdirectory.
1157 FilePath subdir_name_from =
1158 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001159 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001160 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001161
1162 // Create a file under the subdirectory.
1163 FilePath file_name2_from =
1164 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1165 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001166 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001167
1168 // Copy the directory recursively.
1169 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001170 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001171
1172 FilePath dir_name_to =
1173 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1174 FilePath file_name_to =
1175 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1176 FilePath subdir_name_to =
1177 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1178 FilePath file_name2_to =
1179 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1180
1181 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001182 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001183 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001184
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001185 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001186
1187 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001188 EXPECT_TRUE(PathExists(dir_name_from));
1189 EXPECT_TRUE(PathExists(file_name_from));
1190 EXPECT_TRUE(PathExists(subdir_name_from));
1191 EXPECT_TRUE(PathExists(file_name2_from));
1192 EXPECT_TRUE(PathExists(dir_name_to));
1193 EXPECT_TRUE(PathExists(file_name_to));
1194 EXPECT_TRUE(PathExists(subdir_name_to));
1195 EXPECT_TRUE(PathExists(file_name2_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001196}
1197
1198TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001199 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001200 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001201 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001202 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001203 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001204
1205 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001206 FilePath file_name_from =
1207 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001208 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001209 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001210
1211 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001212 FilePath subdir_name_from =
1213 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001214 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001215 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001216
1217 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001218 FilePath file_name2_from =
1219 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001220 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001221 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001222
1223 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001224 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001225 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001226 FilePath file_name_to =
1227 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1228 FilePath subdir_name_to =
1229 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001230
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001231 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001232
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001233 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001234
1235 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001236 EXPECT_TRUE(PathExists(dir_name_from));
1237 EXPECT_TRUE(PathExists(file_name_from));
1238 EXPECT_TRUE(PathExists(subdir_name_from));
1239 EXPECT_TRUE(PathExists(file_name2_from));
1240 EXPECT_TRUE(PathExists(dir_name_to));
1241 EXPECT_TRUE(PathExists(file_name_to));
1242 EXPECT_FALSE(PathExists(subdir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001243}
1244
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001245TEST_F(FileUtilTest, CopyDirectoryExists) {
1246 // Create a directory.
1247 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001248 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001249 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001250 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001251
1252 // Create a file under the directory.
1253 FilePath file_name_from =
1254 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1255 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001256 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001257
1258 // Create a subdirectory.
1259 FilePath subdir_name_from =
1260 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001261 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001262 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001263
1264 // Create a file under the subdirectory.
1265 FilePath file_name2_from =
1266 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1267 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001268 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001269
1270 // Copy the directory not recursively.
1271 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001272 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001273 FilePath file_name_to =
1274 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1275 FilePath subdir_name_to =
1276 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1277
1278 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001279 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001280 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001281
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001282 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001283
1284 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001285 EXPECT_TRUE(PathExists(dir_name_from));
1286 EXPECT_TRUE(PathExists(file_name_from));
1287 EXPECT_TRUE(PathExists(subdir_name_from));
1288 EXPECT_TRUE(PathExists(file_name2_from));
1289 EXPECT_TRUE(PathExists(dir_name_to));
1290 EXPECT_TRUE(PathExists(file_name_to));
1291 EXPECT_FALSE(PathExists(subdir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001292}
1293
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001294TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1295 // Create a file
1296 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001297 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001298 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001299 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001300
1301 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001302 FilePath file_name_to = temp_dir_.path().Append(
1303 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001304 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001305
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001306 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001307
1308 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001309 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001310}
1311
1312TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1313 // Create a file
1314 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001315 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001316 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001317 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001318
1319 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001320 FilePath file_name_to = temp_dir_.path().Append(
1321 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001322 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001323 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001324
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001325 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001326
1327 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001328 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001329 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1330}
1331
1332TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1333 // Create a file
1334 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001335 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001336 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001337 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001338
1339 // The destination
1340 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001341 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001342 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001343 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001344 FilePath file_name_to =
1345 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1346
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001347 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001348
1349 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001350 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001351}
1352
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001353TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1354 // Create a directory.
1355 FilePath dir_name_from =
1356 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001357 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001358 ASSERT_TRUE(PathExists(dir_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001359
1360 // Create a file under the directory.
1361 FilePath file_name_from =
1362 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1363 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001364 ASSERT_TRUE(PathExists(file_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001365
1366 // Copy the directory recursively.
1367 FilePath dir_name_to =
1368 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1369 FilePath file_name_to =
1370 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1371
1372 // Create from path with trailing separators.
1373#if defined(OS_WIN)
1374 FilePath from_path =
1375 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1376#elif defined (OS_POSIX)
1377 FilePath from_path =
1378 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1379#endif
1380
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001381 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001382
1383 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001384 EXPECT_TRUE(PathExists(dir_name_from));
1385 EXPECT_TRUE(PathExists(file_name_from));
1386 EXPECT_TRUE(PathExists(dir_name_to));
1387 EXPECT_TRUE(PathExists(file_name_to));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001388}
1389
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001390// Sets the source file to read-only.
rippf8459182014-10-27 18:59:53 +09001391void SetReadOnly(const FilePath& path, bool read_only) {
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001392#if defined(OS_WIN)
rippf8459182014-10-27 18:59:53 +09001393 // On Windows, it involves setting/removing the 'readonly' bit.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001394 DWORD attrs = GetFileAttributes(path.value().c_str());
1395 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1396 ASSERT_TRUE(SetFileAttributes(
rippf8459182014-10-27 18:59:53 +09001397 path.value().c_str(),
1398 read_only ? (attrs | FILE_ATTRIBUTE_READONLY) :
1399 (attrs & ~FILE_ATTRIBUTE_READONLY)));
rvargasbf75a6c2015-02-10 06:02:39 +09001400
rippf8459182014-10-27 18:59:53 +09001401 DWORD expected = read_only ?
1402 ((attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY)) |
1403 FILE_ATTRIBUTE_READONLY) :
1404 (attrs & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_DIRECTORY));
rvargasbf75a6c2015-02-10 06:02:39 +09001405
1406 // Ignore FILE_ATTRIBUTE_NOT_CONTENT_INDEXED if present.
1407 attrs = GetFileAttributes(path.value().c_str()) &
1408 ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001409 ASSERT_EQ(expected, attrs);
1410#else
rippf8459182014-10-27 18:59:53 +09001411 // On all other platforms, it involves removing/setting the write bit.
1412 mode_t mode = read_only ? S_IRUSR : (S_IRUSR | S_IWUSR);
1413 EXPECT_TRUE(SetPosixFilePermissions(
1414 path, DirectoryExists(path) ? (mode | S_IXUSR) : mode));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001415#endif
1416}
1417
1418bool IsReadOnly(const FilePath& path) {
1419#if defined(OS_WIN)
1420 DWORD attrs = GetFileAttributes(path.value().c_str());
1421 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1422 return attrs & FILE_ATTRIBUTE_READONLY;
1423#else
1424 int mode = 0;
1425 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1426 return !(mode & S_IWUSR);
1427#endif
1428}
1429
1430TEST_F(FileUtilTest, CopyDirectoryACL) {
rippf8459182014-10-27 18:59:53 +09001431 // Create source directories.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001432 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
rippf8459182014-10-27 18:59:53 +09001433 FilePath src_subdir = src.Append(FILE_PATH_LITERAL("subdir"));
1434 CreateDirectory(src_subdir);
1435 ASSERT_TRUE(PathExists(src_subdir));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001436
1437 // Create a file under the directory.
1438 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1439 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
rippf8459182014-10-27 18:59:53 +09001440 SetReadOnly(src_file, true);
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001441 ASSERT_TRUE(IsReadOnly(src_file));
1442
rippf8459182014-10-27 18:59:53 +09001443 // Make directory read-only.
1444 SetReadOnly(src_subdir, true);
1445 ASSERT_TRUE(IsReadOnly(src_subdir));
1446
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001447 // Copy the directory recursively.
1448 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1449 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1450 EXPECT_TRUE(CopyDirectory(src, dst, true));
1451
rippf8459182014-10-27 18:59:53 +09001452 FilePath dst_subdir = dst.Append(FILE_PATH_LITERAL("subdir"));
1453 ASSERT_FALSE(IsReadOnly(dst_subdir));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001454 ASSERT_FALSE(IsReadOnly(dst_file));
rippf8459182014-10-27 18:59:53 +09001455
1456 // Give write permissions to allow deletion.
1457 SetReadOnly(src_subdir, false);
1458 ASSERT_FALSE(IsReadOnly(src_subdir));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001459}
1460
initial.commit3f4a7322008-07-27 06:49:38 +09001461TEST_F(FileUtilTest, CopyFile) {
1462 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001463 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001464 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001465 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001466 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001467
1468 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001469 FilePath file_name_from =
1470 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001471 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1472 CreateTextFile(file_name_from, file_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001473 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001474
1475 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001476 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001477 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001478
cmumford1daaa6b2014-11-18 03:58:20 +09001479 // Try to copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001480 FilePath dest_file2(dir_name_from);
1481 dest_file2 = dest_file2.AppendASCII("..");
1482 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001483 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
evan@chromium.org1543ad32009-08-27 05:00:14 +09001484
1485 FilePath dest_file2_test(dir_name_from);
1486 dest_file2_test = dest_file2_test.DirName();
1487 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001488
cmumford1daaa6b2014-11-18 03:58:20 +09001489 // Check expected copy results.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001490 EXPECT_TRUE(PathExists(file_name_from));
1491 EXPECT_TRUE(PathExists(dest_file));
initial.commit3f4a7322008-07-27 06:49:38 +09001492 const std::wstring read_contents = ReadTextFile(dest_file);
1493 EXPECT_EQ(file_contents, read_contents);
cmumford1daaa6b2014-11-18 03:58:20 +09001494 EXPECT_FALSE(PathExists(dest_file2_test));
1495 EXPECT_FALSE(PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001496}
1497
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001498TEST_F(FileUtilTest, CopyFileACL) {
1499 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1500 // this test case asserts the access control bits are meeting expectations in
cmumford1daaa6b2014-11-18 03:58:20 +09001501 // CopyFile().
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001502 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt"));
1503 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1504 CreateTextFile(src, file_contents);
1505
1506 // Set the source file to read-only.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001507 ASSERT_FALSE(IsReadOnly(src));
rippf8459182014-10-27 18:59:53 +09001508 SetReadOnly(src, true);
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001509 ASSERT_TRUE(IsReadOnly(src));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001510
1511 // Copy the file.
1512 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt"));
1513 ASSERT_TRUE(CopyFile(src, dst));
1514 EXPECT_EQ(file_contents, ReadTextFile(dst));
1515
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001516 ASSERT_FALSE(IsReadOnly(dst));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001517}
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001518
erikkay@google.comf2406842008-08-21 00:59:49 +09001519// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001520// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001521typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001522
erikkay@google.comf2406842008-08-21 00:59:49 +09001523TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001524 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001525 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001526 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001527 ASSERT_TRUE(PathExists(data_dir));
initial.commit3f4a7322008-07-27 06:49:38 +09001528
evanm@google.com874d1672008-10-31 08:54:04 +09001529 FilePath original_file =
1530 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1531 FilePath same_file =
1532 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1533 FilePath same_length_file =
1534 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1535 FilePath different_file =
1536 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1537 FilePath different_first_file =
1538 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1539 FilePath different_last_file =
1540 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1541 FilePath empty1_file =
1542 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1543 FilePath empty2_file =
1544 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1545 FilePath shortened_file =
1546 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1547 FilePath binary_file =
1548 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1549 FilePath binary_file_same =
1550 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1551 FilePath binary_file_diff =
1552 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001553
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001554 EXPECT_TRUE(ContentsEqual(original_file, original_file));
1555 EXPECT_TRUE(ContentsEqual(original_file, same_file));
1556 EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
1557 EXPECT_FALSE(ContentsEqual(original_file, different_file));
1558 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1559 FilePath(FILE_PATH_LITERAL("bogusname"))));
1560 EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
1561 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1562 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1563 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1564 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1565 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1566 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
initial.commit3f4a7322008-07-27 06:49:38 +09001567}
1568
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001569TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1570 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001571 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001572 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001573 ASSERT_TRUE(PathExists(data_dir));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001574
1575 FilePath original_file =
1576 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1577 FilePath same_file =
1578 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1579 FilePath crlf_file =
1580 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1581 FilePath shortened_file =
1582 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1583 FilePath different_file =
1584 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1585 FilePath different_first_file =
1586 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1587 FilePath different_last_file =
1588 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1589 FilePath first1_file =
1590 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1591 FilePath first2_file =
1592 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1593 FilePath empty1_file =
1594 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1595 FilePath empty2_file =
1596 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1597 FilePath blank_line_file =
1598 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1599 FilePath blank_line_crlf_file =
1600 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1601
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001602 EXPECT_TRUE(TextContentsEqual(original_file, same_file));
1603 EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
1604 EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
1605 EXPECT_FALSE(TextContentsEqual(original_file, different_file));
1606 EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
1607 EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
1608 EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
1609 EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
1610 EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
1611 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001612}
1613
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001614// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001615#if defined(OS_WIN)
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001616TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1617 // Create a directory
1618 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001619 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001620 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001621 ASSERT_TRUE(PathExists(dir_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001622
1623 // Create a file under the directory
1624 FilePath file_name_from =
1625 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1626 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001627 ASSERT_TRUE(PathExists(file_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001628
1629 // Move the directory by using CopyAndDeleteDirectory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001630 FilePath dir_name_to = temp_dir_.path().Append(
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001631 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1632 FilePath file_name_to =
1633 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1634
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001635 ASSERT_FALSE(PathExists(dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001636
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001637 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +09001638 dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001639
1640 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001641 EXPECT_FALSE(PathExists(dir_name_from));
1642 EXPECT_FALSE(PathExists(file_name_from));
1643 EXPECT_TRUE(PathExists(dir_name_to));
1644 EXPECT_TRUE(PathExists(file_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001645}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001646
1647TEST_F(FileUtilTest, GetTempDirTest) {
1648 static const TCHAR* kTmpKey = _T("TMP");
1649 static const TCHAR* kTmpValues[] = {
1650 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1651 };
1652 // Save the original $TMP.
1653 size_t original_tmp_size;
1654 TCHAR* original_tmp;
1655 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1656 // original_tmp may be NULL.
1657
1658 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1659 FilePath path;
1660 ::_tputenv_s(kTmpKey, kTmpValues[i]);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001661 GetTempDir(&path);
tkent@chromium.org8da14162009-10-09 16:33:39 +09001662 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1663 " result=" << path.value();
1664 }
1665
1666 // Restore the original $TMP.
1667 if (original_tmp) {
1668 ::_tputenv_s(kTmpKey, original_tmp);
1669 free(original_tmp);
1670 } else {
1671 ::_tputenv_s(kTmpKey, _T(""));
1672 }
1673}
1674#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001675
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001676TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1677 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001678 for (int i = 0; i < 3; i++) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001679 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001680 EXPECT_TRUE(PathExists(temp_files[i]));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001681 EXPECT_FALSE(DirectoryExists(temp_files[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001682 }
1683 for (int i = 0; i < 3; i++)
1684 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1685 for (int i = 0; i < 3; i++)
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001686 EXPECT_TRUE(DeleteFile(temp_files[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001687}
1688
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001689TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001690 FilePath names[3];
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001691 FILE* fps[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001692 int i;
1693
1694 // Create; make sure they are open and exist.
1695 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001696 fps[i] = CreateAndOpenTemporaryFile(&(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001697 ASSERT_TRUE(fps[i]);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001698 EXPECT_TRUE(PathExists(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001699 }
1700
1701 // Make sure all names are unique.
1702 for (i = 0; i < 3; ++i) {
1703 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1704 }
1705
1706 // Close and delete.
1707 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001708 EXPECT_TRUE(CloseFile(fps[i]));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001709 EXPECT_TRUE(DeleteFile(names[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001710 }
initial.commit3f4a7322008-07-27 06:49:38 +09001711}
1712
rvargas@chromium.org43d4a8e2014-06-10 20:19:50 +09001713TEST_F(FileUtilTest, FileToFILE) {
1714 File file;
1715 FILE* stream = FileToFILE(file.Pass(), "w");
1716 EXPECT_FALSE(stream);
1717
1718 FilePath file_name = temp_dir_.path().Append(FPL("The file.txt"));
1719 file = File(file_name, File::FLAG_CREATE | File::FLAG_WRITE);
1720 EXPECT_TRUE(file.IsValid());
1721
1722 stream = FileToFILE(file.Pass(), "w");
1723 EXPECT_TRUE(stream);
1724 EXPECT_FALSE(file.IsValid());
1725 EXPECT_TRUE(CloseFile(stream));
1726}
1727
initial.commit3f4a7322008-07-27 06:49:38 +09001728TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001729 FilePath temp_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001730 ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001731 EXPECT_TRUE(PathExists(temp_dir));
1732 EXPECT_TRUE(DeleteFile(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001733}
1734
skerner@chromium.orge4432392010-05-01 02:00:09 +09001735TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1736 FilePath new_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001737 ASSERT_TRUE(CreateTemporaryDirInDir(
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001738 temp_dir_.path(),
skerner@chromium.orge4432392010-05-01 02:00:09 +09001739 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001740 &new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001741 EXPECT_TRUE(PathExists(new_dir));
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001742 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001743 EXPECT_TRUE(DeleteFile(new_dir, false));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001744}
1745
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001746#if defined(OS_POSIX)
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001747TEST_F(FileUtilTest, GetShmemTempDirTest) {
1748 FilePath dir;
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001749 EXPECT_TRUE(GetShmemTempDir(false, &dir));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001750 EXPECT_TRUE(DirectoryExists(dir));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001751}
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001752#endif
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001753
brettw@chromium.org49de1af2014-02-20 05:34:23 +09001754TEST_F(FileUtilTest, GetHomeDirTest) {
1755#if !defined(OS_ANDROID) // Not implemented on Android.
1756 // We don't actually know what the home directory is supposed to be without
1757 // calling some OS functions which would just duplicate the implementation.
1758 // So here we just test that it returns something "reasonable".
1759 FilePath home = GetHomeDir();
1760 ASSERT_FALSE(home.empty());
1761 ASSERT_TRUE(home.IsAbsolute());
1762#endif
1763}
1764
initial.commit3f4a7322008-07-27 06:49:38 +09001765TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001766 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001767 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001768#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001769 FilePath test_path =
1770 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001771#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001772 FilePath test_path =
1773 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001774#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001775
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001776 EXPECT_FALSE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001777 EXPECT_TRUE(CreateDirectory(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001778 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001779 // CreateDirectory returns true if the DirectoryExists returns true.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001780 EXPECT_TRUE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001781
1782 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001783 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001784 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001785 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001786 EXPECT_TRUE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001787 EXPECT_FALSE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001788
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001789 EXPECT_TRUE(DeleteFile(test_root, true));
1790 EXPECT_FALSE(PathExists(test_root));
1791 EXPECT_FALSE(PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001792
1793 // Verify assumptions made by the Windows implementation:
1794 // 1. The current directory always exists.
1795 // 2. The root directory always exists.
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001796 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001797 FilePath top_level = test_root;
1798 while (top_level != top_level.DirName()) {
1799 top_level = top_level.DirName();
1800 }
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001801 ASSERT_TRUE(DirectoryExists(top_level));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001802
1803 // Given these assumptions hold, it should be safe to
1804 // test that "creating" these directories succeeds.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001805 EXPECT_TRUE(CreateDirectory(
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001806 FilePath(FilePath::kCurrentDirectory)));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001807 EXPECT_TRUE(CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001808
1809#if defined(OS_WIN)
1810 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1811 FilePath invalid_path =
1812 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001813 if (!PathExists(invalid_drive)) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001814 EXPECT_FALSE(CreateDirectory(invalid_path));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001815 }
1816#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001817}
1818
1819TEST_F(FileUtilTest, DetectDirectoryTest) {
1820 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001821 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001822 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001823 EXPECT_FALSE(PathExists(test_root));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001824 EXPECT_TRUE(CreateDirectory(test_root));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001825 EXPECT_TRUE(PathExists(test_root));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001826 EXPECT_TRUE(DirectoryExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001827 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001828 FilePath test_path =
1829 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001830 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001831 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001832 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001833 EXPECT_FALSE(DirectoryExists(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001834 EXPECT_TRUE(DeleteFile(test_path, false));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001835
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001836 EXPECT_TRUE(DeleteFile(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001837}
1838
initial.commit3f4a7322008-07-27 06:49:38 +09001839TEST_F(FileUtilTest, FileEnumeratorTest) {
1840 // Test an empty directory.
brettw@chromium.org56946722013-06-08 13:53:36 +09001841 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001842 EXPECT_EQ(FPL(""), f0.Next().value());
1843 EXPECT_EQ(FPL(""), f0.Next().value());
initial.commit3f4a7322008-07-27 06:49:38 +09001844
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001845 // Test an empty directory, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001846 FileEnumerator f0_dotdot(temp_dir_.path(), false,
1847 FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001848 EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001849 f0_dotdot.Next().value());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001850 EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001851
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001852 // create the directories
rvargas@chromium.org56472942013-08-15 05:46:05 +09001853 FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001854 EXPECT_TRUE(CreateDirectory(dir1));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001855 FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001856 EXPECT_TRUE(CreateDirectory(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001857 FilePath dir2inner = dir2.Append(FPL("inner"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001858 EXPECT_TRUE(CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001859
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001860 // create the files
rvargas@chromium.org56472942013-08-15 05:46:05 +09001861 FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001862 CreateTextFile(dir2file, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001863 FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001864 CreateTextFile(dir2innerfile, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001865 FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001866 CreateTextFile(file1, std::wstring());
1867 FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
rvargas@chromium.org56472942013-08-15 05:46:05 +09001868 .Append(FPL("file2.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001869 CreateTextFile(file2_rel, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001870 FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001871
1872 // Only enumerate files.
brettw@chromium.org56946722013-06-08 13:53:36 +09001873 FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
danakjd40295f2015-03-10 10:48:05 +09001874 FindResultCollector c1(&f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001875 EXPECT_TRUE(c1.HasFile(file1));
1876 EXPECT_TRUE(c1.HasFile(file2_abs));
1877 EXPECT_TRUE(c1.HasFile(dir2file));
1878 EXPECT_TRUE(c1.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001879 EXPECT_EQ(4, c1.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001880
1881 // Only enumerate directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001882 FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
danakjd40295f2015-03-10 10:48:05 +09001883 FindResultCollector c2(&f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001884 EXPECT_TRUE(c2.HasFile(dir1));
1885 EXPECT_TRUE(c2.HasFile(dir2));
1886 EXPECT_TRUE(c2.HasFile(dir2inner));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001887 EXPECT_EQ(3, c2.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001888
tim@chromium.org989d0972008-10-16 11:42:45 +09001889 // Only enumerate directories non-recursively.
brettw@chromium.org56946722013-06-08 13:53:36 +09001890 FileEnumerator f2_non_recursive(
1891 temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
danakjd40295f2015-03-10 10:48:05 +09001892 FindResultCollector c2_non_recursive(&f2_non_recursive);
tim@chromium.org989d0972008-10-16 11:42:45 +09001893 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1894 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001895 EXPECT_EQ(2, c2_non_recursive.size());
tim@chromium.org989d0972008-10-16 11:42:45 +09001896
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001897 // Only enumerate directories, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001898 FileEnumerator f2_dotdot(temp_dir_.path(), false,
1899 FileEnumerator::DIRECTORIES |
1900 FileEnumerator::INCLUDE_DOT_DOT);
danakjd40295f2015-03-10 10:48:05 +09001901 FindResultCollector c2_dotdot(&f2_dotdot);
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001902 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1903 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001904 EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001905 EXPECT_EQ(3, c2_dotdot.size());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001906
initial.commit3f4a7322008-07-27 06:49:38 +09001907 // Enumerate files and directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001908 FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
danakjd40295f2015-03-10 10:48:05 +09001909 FindResultCollector c3(&f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001910 EXPECT_TRUE(c3.HasFile(dir1));
1911 EXPECT_TRUE(c3.HasFile(dir2));
1912 EXPECT_TRUE(c3.HasFile(file1));
1913 EXPECT_TRUE(c3.HasFile(file2_abs));
1914 EXPECT_TRUE(c3.HasFile(dir2file));
1915 EXPECT_TRUE(c3.HasFile(dir2inner));
1916 EXPECT_TRUE(c3.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001917 EXPECT_EQ(7, c3.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001918
1919 // Non-recursive operation.
brettw@chromium.org56946722013-06-08 13:53:36 +09001920 FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
danakjd40295f2015-03-10 10:48:05 +09001921 FindResultCollector c4(&f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001922 EXPECT_TRUE(c4.HasFile(dir2));
1923 EXPECT_TRUE(c4.HasFile(dir2));
1924 EXPECT_TRUE(c4.HasFile(file1));
1925 EXPECT_TRUE(c4.HasFile(file2_abs));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001926 EXPECT_EQ(4, c4.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001927
1928 // Enumerate with a pattern.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001929 FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
danakjd40295f2015-03-10 10:48:05 +09001930 FindResultCollector c5(&f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001931 EXPECT_TRUE(c5.HasFile(dir1));
1932 EXPECT_TRUE(c5.HasFile(dir2));
1933 EXPECT_TRUE(c5.HasFile(dir2file));
1934 EXPECT_TRUE(c5.HasFile(dir2inner));
1935 EXPECT_TRUE(c5.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001936 EXPECT_EQ(5, c5.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001937
rvargas@chromium.org56472942013-08-15 05:46:05 +09001938#if defined(OS_WIN)
1939 {
1940 // Make dir1 point to dir2.
1941 ReparsePoint reparse_point(dir1, dir2);
1942 EXPECT_TRUE(reparse_point.IsValid());
1943
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001944 if ((win::GetVersion() >= win::VERSION_VISTA)) {
rvargas@chromium.org56472942013-08-15 05:46:05 +09001945 // There can be a delay for the enumeration code to see the change on
1946 // the file system so skip this test for XP.
1947 // Enumerate the reparse point.
1948 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
danakjd40295f2015-03-10 10:48:05 +09001949 FindResultCollector c6(&f6);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001950 FilePath inner2 = dir1.Append(FPL("inner"));
1951 EXPECT_TRUE(c6.HasFile(inner2));
1952 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1953 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001954 EXPECT_EQ(3, c6.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001955 }
1956
1957 // No changes for non recursive operation.
1958 FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
danakjd40295f2015-03-10 10:48:05 +09001959 FindResultCollector c7(&f7);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001960 EXPECT_TRUE(c7.HasFile(dir2));
1961 EXPECT_TRUE(c7.HasFile(dir2));
1962 EXPECT_TRUE(c7.HasFile(file1));
1963 EXPECT_TRUE(c7.HasFile(file2_abs));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001964 EXPECT_EQ(4, c7.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001965
1966 // Should not enumerate inside dir1 when using recursion.
1967 FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
danakjd40295f2015-03-10 10:48:05 +09001968 FindResultCollector c8(&f8);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001969 EXPECT_TRUE(c8.HasFile(dir1));
1970 EXPECT_TRUE(c8.HasFile(dir2));
1971 EXPECT_TRUE(c8.HasFile(file1));
1972 EXPECT_TRUE(c8.HasFile(file2_abs));
1973 EXPECT_TRUE(c8.HasFile(dir2file));
1974 EXPECT_TRUE(c8.HasFile(dir2inner));
1975 EXPECT_TRUE(c8.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001976 EXPECT_EQ(7, c8.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001977 }
1978#endif
1979
initial.commit3f4a7322008-07-27 06:49:38 +09001980 // Make sure the destructor closes the find handle while in the middle of a
1981 // query to allow TearDown to delete the directory.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001982 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1983 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
avi@google.com5cb79352008-12-11 23:55:12 +09001984 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001985}
license.botf003cfe2008-08-24 09:55:55 +09001986
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001987TEST_F(FileUtilTest, AppendToFile) {
1988 FilePath data_dir =
1989 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1990
1991 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001992 if (PathExists(data_dir)) {
1993 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001994 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001995 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001996
1997 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001998 if (PathExists(data_dir)) {
1999 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09002000 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002001 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09002002 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
2003
2004 std::string data("hello");
chirantan05106cc2014-10-08 08:15:30 +09002005 EXPECT_FALSE(AppendToFile(foobar, data.c_str(), data.size()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09002006 EXPECT_EQ(static_cast<int>(data.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002007 WriteFile(foobar, data.c_str(), data.length()));
chirantan05106cc2014-10-08 08:15:30 +09002008 EXPECT_TRUE(AppendToFile(foobar, data.c_str(), data.size()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09002009
2010 const std::wstring read_content = ReadTextFile(foobar);
2011 EXPECT_EQ(L"hellohello", read_content);
2012}
2013
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002014TEST_F(FileUtilTest, ReadFile) {
2015 // Create a test file to be read.
2016 const std::string kTestData("The quick brown fox jumps over the lazy dog.");
2017 FilePath file_path =
2018 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileTest"));
2019
2020 ASSERT_EQ(static_cast<int>(kTestData.size()),
2021 WriteFile(file_path, kTestData.data(), kTestData.size()));
2022
2023 // Make buffers with various size.
2024 std::vector<char> small_buffer(kTestData.size() / 2);
2025 std::vector<char> exact_buffer(kTestData.size());
2026 std::vector<char> large_buffer(kTestData.size() * 2);
2027
2028 // Read the file with smaller buffer.
2029 int bytes_read_small = ReadFile(
2030 file_path, &small_buffer[0], static_cast<int>(small_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002031 EXPECT_EQ(static_cast<int>(small_buffer.size()), bytes_read_small);
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002032 EXPECT_EQ(
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002033 std::string(kTestData.begin(), kTestData.begin() + small_buffer.size()),
2034 std::string(small_buffer.begin(), small_buffer.end()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002035
2036 // Read the file with buffer which have exactly same size.
2037 int bytes_read_exact = ReadFile(
2038 file_path, &exact_buffer[0], static_cast<int>(exact_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002039 EXPECT_EQ(static_cast<int>(kTestData.size()), bytes_read_exact);
2040 EXPECT_EQ(kTestData, std::string(exact_buffer.begin(), exact_buffer.end()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002041
2042 // Read the file with larger buffer.
2043 int bytes_read_large = ReadFile(
2044 file_path, &large_buffer[0], static_cast<int>(large_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002045 EXPECT_EQ(static_cast<int>(kTestData.size()), bytes_read_large);
2046 EXPECT_EQ(kTestData, std::string(large_buffer.begin(),
2047 large_buffer.begin() + kTestData.size()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002048
2049 // Make sure the return value is -1 if the file doesn't exist.
2050 FilePath file_path_not_exist =
2051 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileNotExistTest"));
2052 EXPECT_EQ(-1,
2053 ReadFile(file_path_not_exist,
2054 &exact_buffer[0],
2055 static_cast<int>(exact_buffer.size())));
2056}
2057
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002058TEST_F(FileUtilTest, ReadFileToString) {
2059 const char kTestData[] = "0123";
2060 std::string data;
2061
2062 FilePath file_path =
2063 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002064 FilePath file_path_dangerous =
2065 temp_dir_.path().Append(FILE_PATH_LITERAL("..")).
2066 Append(temp_dir_.path().BaseName()).
2067 Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002068
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002069 // Create test file.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002070 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002071
2072 EXPECT_TRUE(ReadFileToString(file_path, &data));
2073 EXPECT_EQ(kTestData, data);
2074
2075 data = "temp";
2076 EXPECT_FALSE(ReadFileToString(file_path, &data, 0));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002077 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002078
2079 data = "temp";
2080 EXPECT_FALSE(ReadFileToString(file_path, &data, 2));
2081 EXPECT_EQ("01", data);
2082
2083 data.clear();
2084 EXPECT_FALSE(ReadFileToString(file_path, &data, 3));
2085 EXPECT_EQ("012", data);
2086
2087 data.clear();
2088 EXPECT_TRUE(ReadFileToString(file_path, &data, 4));
2089 EXPECT_EQ("0123", data);
2090
2091 data.clear();
2092 EXPECT_TRUE(ReadFileToString(file_path, &data, 6));
2093 EXPECT_EQ("0123", data);
2094
2095 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6));
2096
2097 EXPECT_TRUE(ReadFileToString(file_path, NULL));
2098
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002099 data = "temp";
2100 EXPECT_FALSE(ReadFileToString(file_path_dangerous, &data));
2101 EXPECT_EQ(0u, data.length());
2102
2103 // Delete test file.
brettwbb5442f2015-06-10 05:20:14 +09002104 EXPECT_TRUE(DeleteFile(file_path, false));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002105
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002106 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002107 EXPECT_FALSE(ReadFileToString(file_path, &data));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002108 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002109
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002110 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002111 EXPECT_FALSE(ReadFileToString(file_path, &data, 6));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002112 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002113}
2114
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002115TEST_F(FileUtilTest, TouchFile) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002116 FilePath data_dir =
2117 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002118
2119 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002120 if (PathExists(data_dir)) {
2121 ASSERT_TRUE(DeleteFile(data_dir, true));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002122 }
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002123 ASSERT_TRUE(CreateDirectory(data_dir));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002124
2125 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
2126 std::string data("hello");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002127 ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length()));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002128
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002129 Time access_time;
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002130 // This timestamp is divisible by one day (in local timezone),
2131 // to make it work on FAT too.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002132 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002133 &access_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002134
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002135 Time modification_time;
jochen@chromium.orga6879772010-02-18 19:02:26 +09002136 // Note that this timestamp is divisible by two (seconds) - FAT stores
2137 // modification times with 2s resolution.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002138 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
jochen@chromium.orga6879772010-02-18 19:02:26 +09002139 &modification_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002140
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002141 ASSERT_TRUE(TouchFile(foobar, access_time, modification_time));
rvargas@chromium.orgb005b382014-01-07 19:06:58 +09002142 File::Info file_info;
brettw@chromium.orga9154032013-12-05 05:56:49 +09002143 ASSERT_TRUE(GetFileInfo(foobar, &file_info));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002144 EXPECT_EQ(access_time.ToInternalValue(),
2145 file_info.last_accessed.ToInternalValue());
2146 EXPECT_EQ(modification_time.ToInternalValue(),
2147 file_info.last_modified.ToInternalValue());
jochen@chromium.orga6879772010-02-18 19:02:26 +09002148}
2149
tfarina@chromium.org34828222010-05-26 10:40:12 +09002150TEST_F(FileUtilTest, IsDirectoryEmpty) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002151 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002152
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002153 ASSERT_FALSE(PathExists(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002154
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002155 ASSERT_TRUE(CreateDirectory(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002156
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002157 EXPECT_TRUE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002158
2159 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
2160 std::string bar("baz");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002161 ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length()));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002162
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002163 EXPECT_FALSE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002164}
2165
skerner@google.com93449ef2011-09-22 23:47:18 +09002166#if defined(OS_POSIX)
2167
2168// Testing VerifyPathControlledByAdmin() is hard, because there is no
2169// way a test can make a file owned by root, or change file paths
2170// at the root of the file system. VerifyPathControlledByAdmin()
2171// is implemented as a call to VerifyPathControlledByUser, which gives
2172// us the ability to test with paths under the test's temp directory,
2173// using a user id we control.
2174// Pull tests of VerifyPathControlledByUserTest() into a separate test class
2175// with a common SetUp() method.
2176class VerifyPathControlledByUserTest : public FileUtilTest {
2177 protected:
dchengca87abb2014-12-23 11:56:47 +09002178 void SetUp() override {
skerner@google.com93449ef2011-09-22 23:47:18 +09002179 FileUtilTest::SetUp();
2180
2181 // Create a basic structure used by each test.
2182 // base_dir_
2183 // |-> sub_dir_
2184 // |-> text_file_
2185
2186 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002187 ASSERT_TRUE(CreateDirectory(base_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002188
2189 sub_dir_ = base_dir_.AppendASCII("sub_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002190 ASSERT_TRUE(CreateDirectory(sub_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002191
2192 text_file_ = sub_dir_.AppendASCII("file.txt");
2193 CreateTextFile(text_file_, L"This text file has some text in it.");
2194
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002195 // Get the user and group files are created with from |base_dir_|.
2196 struct stat stat_buf;
2197 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
2198 uid_ = stat_buf.st_uid;
skerner@chromium.org80784142011-10-18 06:30:29 +09002199 ok_gids_.insert(stat_buf.st_gid);
2200 bad_gids_.insert(stat_buf.st_gid + 1);
2201
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002202 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
skerner@google.com93449ef2011-09-22 23:47:18 +09002203
2204 // To ensure that umask settings do not cause the initial state
2205 // of permissions to be different from what we expect, explicitly
2206 // set permissions on the directories we create.
2207 // Make all files and directories non-world-writable.
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002208
2209 // Users and group can read, write, traverse
2210 int enabled_permissions =
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002211 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002212 // Other users can't read, write, traverse
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002213 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
skerner@google.com93449ef2011-09-22 23:47:18 +09002214
2215 ASSERT_NO_FATAL_FAILURE(
2216 ChangePosixFilePermissions(
2217 base_dir_, enabled_permissions, disabled_permissions));
2218 ASSERT_NO_FATAL_FAILURE(
2219 ChangePosixFilePermissions(
2220 sub_dir_, enabled_permissions, disabled_permissions));
2221 }
2222
2223 FilePath base_dir_;
2224 FilePath sub_dir_;
2225 FilePath text_file_;
2226 uid_t uid_;
skerner@chromium.org80784142011-10-18 06:30:29 +09002227
2228 std::set<gid_t> ok_gids_;
2229 std::set<gid_t> bad_gids_;
skerner@google.com93449ef2011-09-22 23:47:18 +09002230};
2231
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002232TEST_F(VerifyPathControlledByUserTest, BadPaths) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002233 // File does not exist.
2234 FilePath does_not_exist = base_dir_.AppendASCII("does")
2235 .AppendASCII("not")
2236 .AppendASCII("exist");
skerner@google.com93449ef2011-09-22 23:47:18 +09002237 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002238 VerifyPathControlledByUser(base_dir_, does_not_exist, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002239
2240 // |base| not a subpath of |path|.
brettwbb5442f2015-06-10 05:20:14 +09002241 EXPECT_FALSE(VerifyPathControlledByUser(sub_dir_, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002242
2243 // An empty base path will fail to be a prefix for any path.
2244 FilePath empty;
brettwbb5442f2015-06-10 05:20:14 +09002245 EXPECT_FALSE(VerifyPathControlledByUser(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.
brettwbb5442f2015-06-10 05:20:14 +09002248 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002249}
2250
2251TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2252 // Symlinks in the path should cause failure.
2253
2254 // Symlink to the file at the end of the path.
2255 FilePath file_link = base_dir_.AppendASCII("file_link");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002256 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
skerner@google.com93449ef2011-09-22 23:47:18 +09002257 << "Failed to create symlink.";
2258
2259 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002260 VerifyPathControlledByUser(base_dir_, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002261 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002262 VerifyPathControlledByUser(file_link, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002263
2264 // Symlink from one directory to another within the path.
2265 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002266 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
skerner@google.com93449ef2011-09-22 23:47:18 +09002267 << "Failed to create symlink.";
2268
2269 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002270 ASSERT_TRUE(PathExists(file_path_with_link));
skerner@google.com93449ef2011-09-22 23:47:18 +09002271
brettwbb5442f2015-06-10 05:20:14 +09002272 EXPECT_FALSE(VerifyPathControlledByUser(base_dir_, file_path_with_link, uid_,
2273 ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002274
brettwbb5442f2015-06-10 05:20:14 +09002275 EXPECT_FALSE(VerifyPathControlledByUser(link_to_sub_dir, file_path_with_link,
2276 uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002277
2278 // Symlinks in parents of base path are allowed.
brettwbb5442f2015-06-10 05:20:14 +09002279 EXPECT_TRUE(VerifyPathControlledByUser(file_path_with_link,
2280 file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002281}
2282
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002283TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002284 // Get a uid that is not the uid of files we create.
2285 uid_t bad_uid = uid_ + 1;
2286
skerner@google.com93449ef2011-09-22 23:47:18 +09002287 // Make all files and directories non-world-writable.
2288 ASSERT_NO_FATAL_FAILURE(
2289 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2290 ASSERT_NO_FATAL_FAILURE(
2291 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2292 ASSERT_NO_FATAL_FAILURE(
2293 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2294
2295 // We control these paths.
brettwbb5442f2015-06-10 05:20:14 +09002296 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002297 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002298 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
2299 EXPECT_TRUE(VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002300
2301 // Another user does not control these paths.
2302 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002303 VerifyPathControlledByUser(base_dir_, sub_dir_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002304 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002305 VerifyPathControlledByUser(base_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002306 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002307 VerifyPathControlledByUser(sub_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002308
2309 // Another group does not control the paths.
2310 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002311 VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002312 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002313 VerifyPathControlledByUser(base_dir_, text_file_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002314 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002315 VerifyPathControlledByUser(sub_dir_, text_file_, uid_, bad_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002316}
2317
2318TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2319 // Make all files and directories writable only by their owner.
2320 ASSERT_NO_FATAL_FAILURE(
2321 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2322 ASSERT_NO_FATAL_FAILURE(
2323 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2324 ASSERT_NO_FATAL_FAILURE(
2325 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2326
2327 // Any group is okay because the path is not group-writable.
brettwbb5442f2015-06-10 05:20:14 +09002328 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002329 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002330 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
2331 EXPECT_TRUE(VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002332
brettwbb5442f2015-06-10 05:20:14 +09002333 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, bad_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002334 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002335 VerifyPathControlledByUser(base_dir_, text_file_, uid_, bad_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002336 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002337 VerifyPathControlledByUser(sub_dir_, text_file_, uid_, bad_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002338
2339 // No group is okay, because we don't check the group
2340 // if no group can write.
2341 std::set<gid_t> no_gids; // Empty set of gids.
brettwbb5442f2015-06-10 05:20:14 +09002342 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, no_gids));
2343 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, text_file_, uid_, no_gids));
2344 EXPECT_TRUE(VerifyPathControlledByUser(sub_dir_, text_file_, uid_, no_gids));
skerner@chromium.org80784142011-10-18 06:30:29 +09002345
2346 // Make all files and directories writable by their group.
brettwbb5442f2015-06-10 05:20:14 +09002347 ASSERT_NO_FATAL_FAILURE(ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2348 ASSERT_NO_FATAL_FAILURE(ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2349 ASSERT_NO_FATAL_FAILURE(ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
skerner@chromium.org80784142011-10-18 06:30:29 +09002350
2351 // Now |ok_gids_| works, but |bad_gids_| fails.
brettwbb5442f2015-06-10 05:20:14 +09002352 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002353 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002354 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
2355 EXPECT_TRUE(VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002356
2357 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002358 VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, bad_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002359 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002360 VerifyPathControlledByUser(base_dir_, text_file_, uid_, bad_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002361 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002362 VerifyPathControlledByUser(sub_dir_, text_file_, uid_, bad_gids_));
skerner@chromium.org80784142011-10-18 06:30:29 +09002363
2364 // Because any group in the group set is allowed,
2365 // the union of good and bad gids passes.
2366
2367 std::set<gid_t> multiple_gids;
2368 std::set_union(
2369 ok_gids_.begin(), ok_gids_.end(),
2370 bad_gids_.begin(), bad_gids_.end(),
2371 std::inserter(multiple_gids, multiple_gids.begin()));
2372
2373 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002374 VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, multiple_gids));
skerner@chromium.org80784142011-10-18 06:30:29 +09002375 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002376 VerifyPathControlledByUser(base_dir_, text_file_, uid_, multiple_gids));
skerner@chromium.org80784142011-10-18 06:30:29 +09002377 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002378 VerifyPathControlledByUser(sub_dir_, text_file_, uid_, multiple_gids));
skerner@google.com93449ef2011-09-22 23:47:18 +09002379}
2380
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002381TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002382 // Make all files and directories non-world-writable.
2383 ASSERT_NO_FATAL_FAILURE(
2384 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2385 ASSERT_NO_FATAL_FAILURE(
2386 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2387 ASSERT_NO_FATAL_FAILURE(
2388 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2389
2390 // Initialy, we control all parts of the path.
brettwbb5442f2015-06-10 05:20:14 +09002391 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002392 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002393 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
2394 EXPECT_TRUE(VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002395
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09002396 // Make base_dir_ world-writable.
skerner@google.com93449ef2011-09-22 23:47:18 +09002397 ASSERT_NO_FATAL_FAILURE(
2398 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
brettwbb5442f2015-06-10 05:20:14 +09002399 EXPECT_FALSE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002400 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002401 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
2402 EXPECT_TRUE(VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002403
2404 // Make sub_dir_ world writable.
2405 ASSERT_NO_FATAL_FAILURE(
2406 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
brettwbb5442f2015-06-10 05:20:14 +09002407 EXPECT_FALSE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002408 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002409 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002410 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002411 VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002412
2413 // Make text_file_ world writable.
2414 ASSERT_NO_FATAL_FAILURE(
2415 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
brettwbb5442f2015-06-10 05:20:14 +09002416 EXPECT_FALSE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002417 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002418 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002419 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002420 VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002421
2422 // Make sub_dir_ non-world writable.
2423 ASSERT_NO_FATAL_FAILURE(
2424 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
brettwbb5442f2015-06-10 05:20:14 +09002425 EXPECT_FALSE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002426 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002427 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002428 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002429 VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002430
2431 // Make base_dir_ non-world-writable.
2432 ASSERT_NO_FATAL_FAILURE(
2433 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
brettwbb5442f2015-06-10 05:20:14 +09002434 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002435 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002436 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002437 EXPECT_FALSE(
brettwbb5442f2015-06-10 05:20:14 +09002438 VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002439
2440 // Back to the initial state: Nothing is writable, so every path
2441 // should pass.
2442 ASSERT_NO_FATAL_FAILURE(
2443 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
brettwbb5442f2015-06-10 05:20:14 +09002444 EXPECT_TRUE(VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002445 EXPECT_TRUE(
brettwbb5442f2015-06-10 05:20:14 +09002446 VerifyPathControlledByUser(base_dir_, text_file_, uid_, ok_gids_));
2447 EXPECT_TRUE(VerifyPathControlledByUser(sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002448}
2449
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002450#if defined(OS_ANDROID)
2451TEST_F(FileUtilTest, ValidContentUriTest) {
2452 // Get the test image path.
2453 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002454 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002455 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002456 ASSERT_TRUE(PathExists(data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002457 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2458 int64 image_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002459 GetFileSize(image_file, &image_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002460 EXPECT_LT(0, image_size);
2461
2462 // Insert the image into MediaStore. MediaStore will do some conversions, and
2463 // return the content URI.
brettwbb5442f2015-06-10 05:20:14 +09002464 FilePath path = InsertImageIntoMediaStore(image_file);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002465 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002466 EXPECT_TRUE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002467 // The file size may not equal to the input image as MediaStore may convert
2468 // the image.
2469 int64 content_uri_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002470 GetFileSize(path, &content_uri_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002471 EXPECT_EQ(image_size, content_uri_size);
2472
2473 // We should be able to read the file.
2474 char* buffer = new char[image_size];
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002475 File file = OpenContentUriForRead(path);
2476 EXPECT_TRUE(file.IsValid());
2477 EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002478 delete[] buffer;
2479}
2480
2481TEST_F(FileUtilTest, NonExistentContentUriTest) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002482 FilePath path("content://foo.bar");
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002483 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002484 EXPECT_FALSE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002485 // Size should be smaller than 0.
2486 int64 size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002487 EXPECT_FALSE(GetFileSize(path, &size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002488
2489 // We should not be able to read the file.
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002490 File file = OpenContentUriForRead(path);
2491 EXPECT_FALSE(file.IsValid());
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002492}
2493#endif
2494
jln@chromium.orgd7493932014-03-01 00:35:36 +09002495TEST(ScopedFD, ScopedFDDoesClose) {
2496 int fds[2];
2497 char c = 0;
2498 ASSERT_EQ(0, pipe(fds));
2499 const int write_end = fds[1];
brettwbb5442f2015-06-10 05:20:14 +09002500 ScopedFD read_end_closer(fds[0]);
2501 { ScopedFD write_end_closer(fds[1]); }
jln@chromium.orgd7493932014-03-01 00:35:36 +09002502 // This is the only thread. This file descriptor should no longer be valid.
2503 int ret = close(write_end);
2504 EXPECT_EQ(-1, ret);
2505 EXPECT_EQ(EBADF, errno);
2506 // Make sure read(2) won't block.
2507 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK));
2508 // Reading the pipe should EOF.
2509 EXPECT_EQ(0, read(fds[0], &c, 1));
2510}
2511
2512#if defined(GTEST_HAS_DEATH_TEST)
2513void CloseWithScopedFD(int fd) {
brettwbb5442f2015-06-10 05:20:14 +09002514 ScopedFD fd_closer(fd);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002515}
2516#endif
2517
2518TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) {
2519 int fds[2];
2520 ASSERT_EQ(0, pipe(fds));
brettwbb5442f2015-06-10 05:20:14 +09002521 ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002522 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1])));
2523#if defined(GTEST_HAS_DEATH_TEST)
2524 // This is the only thread. This file descriptor should no longer be valid.
2525 // Trying to close it should crash. This is important for security.
2526 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2527#endif
2528}
2529
skerner@google.com93449ef2011-09-22 23:47:18 +09002530#endif // defined(OS_POSIX)
2531
mark@chromium.org17684802008-09-10 09:16:28 +09002532} // namespace
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002533
2534} // namespace base