blob: 642c9d5a21b7cf4e09b82322512b4440406f1a94 [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>
initial.commit3f4a7322008-07-27 06:49:38 +090024
25#include "base/base_paths.h"
26#include "base/file_util.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.orgb52f29b2014-03-18 04:02:35 +090029#include "base/files/scoped_file.h"
brettw@chromium.org091db522012-11-17 05:34:23 +090030#include "base/files/scoped_temp_dir.h"
initial.commit3f4a7322008-07-27 06:49:38 +090031#include "base/path_service.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090032#include "base/strings/utf_string_conversions.h"
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +090033#include "base/test/test_file_util.h"
brettw@chromium.org61391822011-01-01 05:02:16 +090034#include "base/threading/platform_thread.h"
initial.commit3f4a7322008-07-27 06:49:38 +090035#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090036#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090037
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090038#if defined(OS_WIN)
39#include "base/win/scoped_handle.h"
rvargas@chromium.org56472942013-08-15 05:46:05 +090040#include "base/win/windows_version.h"
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090041#endif
42
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090043#if defined(OS_ANDROID)
44#include "base/android/content_uri_utils.h"
45#endif
46
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090047// This macro helps avoid wrapped lines in the test structs.
48#define FPL(x) FILE_PATH_LITERAL(x)
49
brettw@chromium.org2873d9b2013-11-28 08:22:08 +090050namespace base {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090051
initial.commit3f4a7322008-07-27 06:49:38 +090052namespace {
53
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090054// To test that file_util::Normalize FilePath() deals with NTFS reparse points
55// correctly, we need functions to create and delete reparse points.
56#if defined(OS_WIN)
57typedef struct _REPARSE_DATA_BUFFER {
58 ULONG ReparseTag;
59 USHORT ReparseDataLength;
60 USHORT Reserved;
61 union {
62 struct {
63 USHORT SubstituteNameOffset;
64 USHORT SubstituteNameLength;
65 USHORT PrintNameOffset;
66 USHORT PrintNameLength;
67 ULONG Flags;
68 WCHAR PathBuffer[1];
69 } SymbolicLinkReparseBuffer;
70 struct {
71 USHORT SubstituteNameOffset;
72 USHORT SubstituteNameLength;
73 USHORT PrintNameOffset;
74 USHORT PrintNameLength;
75 WCHAR PathBuffer[1];
76 } MountPointReparseBuffer;
77 struct {
78 UCHAR DataBuffer[1];
79 } GenericReparseBuffer;
80 };
81} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
82
83// Sets a reparse point. |source| will now point to |target|. Returns true if
84// the call succeeds, false otherwise.
85bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
86 std::wstring kPathPrefix = L"\\??\\";
87 std::wstring target_str;
88 // The juction will not work if the target path does not start with \??\ .
89 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
90 target_str += kPathPrefix;
91 target_str += target_path.value();
92 const wchar_t* target = target_str.c_str();
93 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
94 char buffer[2000] = {0};
95 DWORD returned;
96
97 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
98
99 data->ReparseTag = 0xa0000003;
100 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
101
102 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
103 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
104 data->ReparseDataLength = size_target + 4 + 8;
105
106 int data_size = data->ReparseDataLength + 8;
107
108 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
109 NULL, 0, &returned, NULL)) {
110 return false;
111 }
112 return true;
113}
114
115// Delete the reparse point referenced by |source|. Returns true if the call
116// succeeds, false otherwise.
117bool DeleteReparsePoint(HANDLE source) {
118 DWORD returned;
119 REPARSE_DATA_BUFFER data = {0};
120 data.ReparseTag = 0xa0000003;
121 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
122 &returned, NULL)) {
123 return false;
124 }
125 return true;
126}
rvargas@chromium.org56472942013-08-15 05:46:05 +0900127
128// Manages a reparse point for a test.
129class ReparsePoint {
130 public:
131 // Creates a reparse point from |source| (an empty directory) to |target|.
132 ReparsePoint(const FilePath& source, const FilePath& target) {
133 dir_.Set(
134 ::CreateFile(source.value().c_str(),
135 FILE_ALL_ACCESS,
136 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
137 NULL,
138 OPEN_EXISTING,
139 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
140 NULL));
141 created_ = dir_.IsValid() && SetReparsePoint(dir_, target);
142 }
143
144 ~ReparsePoint() {
145 if (created_)
146 DeleteReparsePoint(dir_);
147 }
148
149 bool IsValid() { return created_; }
150
151 private:
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900152 win::ScopedHandle dir_;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900153 bool created_;
154 DISALLOW_COPY_AND_ASSIGN(ReparsePoint);
155};
156
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900157#endif
158
skerner@google.com93449ef2011-09-22 23:47:18 +0900159#if defined(OS_POSIX)
160// Provide a simple way to change the permissions bits on |path| in tests.
161// ASSERT failures will return, but not stop the test. Caller should wrap
162// calls to this function in ASSERT_NO_FATAL_FAILURE().
163void ChangePosixFilePermissions(const FilePath& path,
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900164 int mode_bits_to_set,
165 int mode_bits_to_clear) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900166 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
167 << "Can't set and clear the same bits.";
168
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900169 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900170 ASSERT_TRUE(GetPosixFilePermissions(path, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900171 mode |= mode_bits_to_set;
172 mode &= ~mode_bits_to_clear;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900173 ASSERT_TRUE(SetPosixFilePermissions(path, mode));
skerner@google.com93449ef2011-09-22 23:47:18 +0900174}
175#endif // defined(OS_POSIX)
176
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900177const wchar_t bogus_content[] = L"I'm cannon fodder.";
178
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900179const int FILES_AND_DIRECTORIES =
brettw@chromium.org56946722013-06-08 13:53:36 +0900180 FileEnumerator::FILES | FileEnumerator::DIRECTORIES;
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900181
erikkay@google.comf2406842008-08-21 00:59:49 +0900182// file_util winds up using autoreleased objects on the Mac, so this needs
183// to be a PlatformTest
184class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900185 protected:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +0900186 virtual void SetUp() OVERRIDE {
erikkay@google.comf2406842008-08-21 00:59:49 +0900187 PlatformTest::SetUp();
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900188 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commit3f4a7322008-07-27 06:49:38 +0900189 }
190
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900191 ScopedTempDir temp_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900192};
193
194// Collects all the results from the given file enumerator, and provides an
195// interface to query whether a given file is present.
196class FindResultCollector {
197 public:
brettw@chromium.org56946722013-06-08 13:53:36 +0900198 explicit FindResultCollector(FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900199 FilePath cur_file;
200 while (!(cur_file = enumerator.Next()).value().empty()) {
201 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900202 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900203 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900204 << "Same file returned twice";
205
206 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900207 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900208 }
209 }
210
211 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900212 bool HasFile(const FilePath& file) const {
213 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900214 }
evanm@google.com874d1672008-10-31 08:54:04 +0900215
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900216 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900217 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900218 }
initial.commit3f4a7322008-07-27 06:49:38 +0900219
220 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900221 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900222};
223
224// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900225void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900226 const std::wstring& contents) {
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900227 std::wofstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900228 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900229 ASSERT_TRUE(file.is_open());
230 file << contents;
231 file.close();
232}
233
234// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900235std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900236 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900237 std::wifstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900238 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900239 EXPECT_TRUE(file.is_open());
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900240 file.getline(contents, arraysize(contents));
initial.commit3f4a7322008-07-27 06:49:38 +0900241 file.close();
242 return std::wstring(contents);
243}
244
erikkay@google.com014161d2008-08-16 02:45:13 +0900245#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900246uint64 FileTimeAsUint64(const FILETIME& ft) {
247 ULARGE_INTEGER u;
248 u.LowPart = ft.dwLowDateTime;
249 u.HighPart = ft.dwHighDateTime;
250 return u.QuadPart;
251}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900252#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900253
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900254TEST_F(FileUtilTest, FileAndDirectorySize) {
255 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
256 // should return 53 bytes.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900257 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900258 CreateTextFile(file_01, L"12345678901234567890");
259 int64 size_f1 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900260 ASSERT_TRUE(GetFileSize(file_01, &size_f1));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900261 EXPECT_EQ(20ll, size_f1);
262
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900263 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900264 CreateDirectory(subdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900265
266 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
267 CreateTextFile(file_02, L"123456789012345678901234567890");
268 int64 size_f2 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900269 ASSERT_TRUE(GetFileSize(file_02, &size_f2));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900270 EXPECT_EQ(30ll, size_f2);
271
272 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900273 CreateDirectory(subsubdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900274
275 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
276 CreateTextFile(file_03, L"123");
277
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900278 int64 computed_size = ComputeDirectorySize(temp_dir_.path());
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900279 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
280}
281
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900282TEST_F(FileUtilTest, NormalizeFilePathBasic) {
283 // Create a directory under the test dir. Because we create it,
284 // we know it is not a link.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900285 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
286 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900287 FilePath file_b_path = dir_path.Append(FPL("file_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900288 CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900289
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900290 FilePath normalized_file_a_path, normalized_file_b_path;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900291 ASSERT_FALSE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900292 ASSERT_FALSE(NormalizeFilePath(file_a_path, &normalized_file_a_path))
viettrungluu@chromium.orgea703f12010-08-23 01:19:13 +0900293 << "NormalizeFilePath() should fail on nonexistent paths.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900294
295 CreateTextFile(file_a_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900296 ASSERT_TRUE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900297 ASSERT_TRUE(NormalizeFilePath(file_a_path, &normalized_file_a_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900298
299 CreateTextFile(file_b_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900300 ASSERT_TRUE(PathExists(file_b_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900301 ASSERT_TRUE(NormalizeFilePath(file_b_path, &normalized_file_b_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900302
303 // Beacuse this test created |dir_path|, we know it is not a link
304 // or junction. So, the real path of the directory holding file a
305 // must be the parent of the path holding file b.
306 ASSERT_TRUE(normalized_file_a_path.DirName()
307 .IsParent(normalized_file_b_path.DirName()));
308}
309
310#if defined(OS_WIN)
311
312TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
313 // Build the following directory structure:
314 //
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900315 // temp_dir
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900316 // |-> base_a
317 // | |-> sub_a
318 // | |-> file.txt
319 // | |-> long_name___... (Very long name.)
320 // | |-> sub_long
321 // | |-> deep.txt
322 // |-> base_b
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900323 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
324 // |-> to_base_b (reparse point to temp_dir\base_b)
325 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900326
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900327 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900328 ASSERT_TRUE(CreateDirectory(base_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900329
330 FilePath sub_a = base_a.Append(FPL("sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900331 ASSERT_TRUE(CreateDirectory(sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900332
333 FilePath file_txt = sub_a.Append(FPL("file.txt"));
334 CreateTextFile(file_txt, bogus_content);
335
336 // Want a directory whose name is long enough to make the path to the file
337 // inside just under MAX_PATH chars. This will be used to test that when
338 // a junction expands to a path over MAX_PATH chars in length,
339 // NormalizeFilePath() fails without crashing.
340 FilePath sub_long_rel(FPL("sub_long"));
341 FilePath deep_txt(FPL("deep.txt"));
342
343 int target_length = MAX_PATH;
344 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
345 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900346 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900347 // the resulting path is still long enough to hit the failing case in
348 // NormalizePath().
349 const int kCreateDirLimit = 4;
350 target_length -= kCreateDirLimit;
351 FilePath::StringType long_name_str = FPL("long_name_");
352 long_name_str.resize(target_length, '_');
353
354 FilePath long_name = sub_a.Append(FilePath(long_name_str));
355 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
356 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
357
358 FilePath sub_long = deep_file.DirName();
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900359 ASSERT_TRUE(CreateDirectory(sub_long));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900360 CreateTextFile(deep_file, bogus_content);
361
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900362 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900363 ASSERT_TRUE(CreateDirectory(base_b));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900364
365 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900366 ASSERT_TRUE(CreateDirectory(to_sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900367 FilePath normalized_path;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900368 {
369 ReparsePoint reparse_to_sub_a(to_sub_a, sub_a);
370 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900371
rvargas@chromium.org56472942013-08-15 05:46:05 +0900372 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900373 ASSERT_TRUE(CreateDirectory(to_base_b));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900374 ReparsePoint reparse_to_base_b(to_base_b, base_b);
375 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900376
rvargas@chromium.org56472942013-08-15 05:46:05 +0900377 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900378 ASSERT_TRUE(CreateDirectory(to_sub_long));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900379 ReparsePoint reparse_to_sub_long(to_sub_long, sub_long);
380 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900381
rvargas@chromium.org56472942013-08-15 05:46:05 +0900382 // Normalize a junction free path: base_a\sub_a\file.txt .
brettw@chromium.org70684242013-12-05 03:22:49 +0900383 ASSERT_TRUE(NormalizeFilePath(file_txt, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900384 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
385
386 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
387 // the junction to_sub_a.
brettw@chromium.org70684242013-12-05 03:22:49 +0900388 ASSERT_TRUE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900389 &normalized_path));
390 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
391
392 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
393 // normalized to exclude junctions to_base_b and to_sub_a .
brettw@chromium.org70684242013-12-05 03:22:49 +0900394 ASSERT_TRUE(NormalizeFilePath(base_b.Append(FPL("to_base_b"))
rvargas@chromium.org56472942013-08-15 05:46:05 +0900395 .Append(FPL("to_base_b"))
396 .Append(FPL("to_sub_a"))
397 .Append(FPL("file.txt")),
398 &normalized_path));
399 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
400
401 // A long enough path will cause NormalizeFilePath() to fail. Make a long
402 // path using to_base_b many times, and check that paths long enough to fail
403 // do not cause a crash.
404 FilePath long_path = base_b;
405 const int kLengthLimit = MAX_PATH + 200;
406 while (long_path.value().length() <= kLengthLimit) {
407 long_path = long_path.Append(FPL("to_base_b"));
408 }
409 long_path = long_path.Append(FPL("to_sub_a"))
410 .Append(FPL("file.txt"));
411
brettw@chromium.org70684242013-12-05 03:22:49 +0900412 ASSERT_FALSE(NormalizeFilePath(long_path, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900413
414 // Normalizing the junction to deep.txt should fail, because the expanded
415 // path to deep.txt is longer than MAX_PATH.
brettw@chromium.org70684242013-12-05 03:22:49 +0900416 ASSERT_FALSE(NormalizeFilePath(to_sub_long.Append(deep_txt),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900417 &normalized_path));
418
419 // Delete the reparse points, and see that NormalizeFilePath() fails
420 // to traverse them.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900421 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900422
brettw@chromium.org70684242013-12-05 03:22:49 +0900423 ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900424 &normalized_path));
425}
426
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900427TEST_F(FileUtilTest, DevicePathToDriveLetter) {
428 // Get a drive letter.
429 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
430 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
431 LOG(ERROR) << "Can't get a drive letter to test with.";
432 return;
433 }
434
435 // Get the NT style path to that drive.
436 wchar_t device_path[MAX_PATH] = {'\0'};
437 ASSERT_TRUE(
438 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
439 FilePath actual_device_path(device_path);
440 FilePath win32_path;
441
442 // Run DevicePathToDriveLetterPath() on the NT style path we got from
443 // QueryDosDevice(). Expect the drive letter we started with.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900444 ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path, &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900445 ASSERT_EQ(real_drive_letter, win32_path.value());
446
447 // Add some directories to the path. Expect those extra path componenets
448 // to be preserved.
449 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900450 ASSERT_TRUE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900451 actual_device_path.Append(kRelativePath),
452 &win32_path));
453 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
454 win32_path.value());
455
456 // Deform the real path so that it is invalid by removing the last four
457 // characters. The way windows names devices that are hard disks
458 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
459 // than three characters. The only way the truncated string could be a
460 // real drive is if more than 10^3 disks are mounted:
461 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
462 // Check that DevicePathToDriveLetterPath fails.
463 int path_length = actual_device_path.value().length();
464 int new_length = path_length - 4;
465 ASSERT_LT(0, new_length);
466 FilePath prefix_of_real_device_path(
467 actual_device_path.value().substr(0, new_length));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900468 ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path,
469 &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900470
brettw@chromium.orga9154032013-12-05 05:56:49 +0900471 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900472 prefix_of_real_device_path.Append(kRelativePath),
473 &win32_path));
474
475 // Deform the real path so that it is invalid by adding some characters. For
476 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
477 // request for the drive letter whose native path is
478 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
479 // because drives are numbered in order and mounting 112345 hard disks will
480 // never happen.
481 const FilePath::StringType kExtraChars = FPL("12345");
482
483 FilePath real_device_path_plus_numbers(
484 actual_device_path.value() + kExtraChars);
485
brettw@chromium.orga9154032013-12-05 05:56:49 +0900486 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900487 real_device_path_plus_numbers,
488 &win32_path));
489
brettw@chromium.orga9154032013-12-05 05:56:49 +0900490 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900491 real_device_path_plus_numbers.Append(kRelativePath),
492 &win32_path));
493}
494
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900495TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
496 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
497 // if it is available. This test requires that:
498 // - the filesystem at |temp_dir_| supports long filenames.
499 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
500 // directories of |temp_dir_|.
501 const FilePath::CharType kLongDirName[] = FPL("A long path");
502 const FilePath::CharType kTestSubDirName[] = FPL("test");
503 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900504 ASSERT_TRUE(CreateDirectory(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900505
506 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
507 // different short name.
508 WCHAR path_buffer[MAX_PATH];
509 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
510 path_buffer, MAX_PATH);
511 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
512 ASSERT_NE(DWORD(0), path_buffer_length);
513 FilePath short_test_dir(path_buffer);
514 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
515
516 FilePath temp_file;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900517 ASSERT_TRUE(CreateTemporaryFileInDir(short_test_dir, &temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900518 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900519 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900520
521 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
522 // unreadable. We should still be able to create a temp file in the
523 // subdirectory, but we won't be able to determine the long path for it. This
524 // mimics the environment that some users run where their user profiles reside
525 // in a location where the don't have full access to the higher level
526 // directories. (Note that this assumption is true for NTFS, but not for some
527 // network file systems. E.g. AFS).
528 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900529 ASSERT_TRUE(CreateDirectory(access_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900530 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
531 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
532
533 // Use the short form of the directory to create a temporary filename.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900534 ASSERT_TRUE(CreateTemporaryFileInDir(
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900535 short_test_dir.Append(kTestSubDirName), &temp_file));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900536 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900537 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
538
539 // Check that the long path can't be determined for |temp_file|.
540 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
541 path_buffer, MAX_PATH);
542 EXPECT_EQ(DWORD(0), path_buffer_length);
543}
544
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900545#endif // defined(OS_WIN)
546
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900547#if defined(OS_POSIX)
548
549TEST_F(FileUtilTest, CreateAndReadSymlinks) {
550 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
551 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
552 CreateTextFile(link_to, bogus_content);
553
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900554 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900555 << "Failed to create file symlink.";
556
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900557 // If we created the link properly, we should be able to read the contents
558 // through it.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900559 std::wstring contents = ReadTextFile(link_from);
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900560 EXPECT_EQ(bogus_content, contents);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900561
562 FilePath result;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900563 ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900564 EXPECT_EQ(link_to.value(), result.value());
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900565
566 // Link to a directory.
567 link_from = temp_dir_.path().Append(FPL("from_dir"));
568 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900569 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900570 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900571 << "Failed to create directory symlink.";
572
573 // Test failures.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900574 EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
575 EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900576 FilePath missing = temp_dir_.path().Append(FPL("missing"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900577 EXPECT_FALSE(ReadSymbolicLink(missing, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900578}
579
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900580// The following test of NormalizeFilePath() require that we create a symlink.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900581// This can not be done on Windows before Vista. On Vista, creating a symlink
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900582// requires privilege "SeCreateSymbolicLinkPrivilege".
583// TODO(skerner): Investigate the possibility of giving base_unittests the
584// privileges required to create a symlink.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900585TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
skerner@chromium.org559baa92010-05-13 00:13:57 +0900586 // Link one file to another.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900587 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
588 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900589 CreateTextFile(link_to, bogus_content);
590
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900591 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900592 << "Failed to create file symlink.";
593
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900594 // Check that NormalizeFilePath sees the link.
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900595 FilePath normalized_path;
brettw@chromium.org70684242013-12-05 03:22:49 +0900596 ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900597 EXPECT_NE(link_from, link_to);
598 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
599 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900600
601 // Link to a directory.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900602 link_from = temp_dir_.path().Append(FPL("from_dir"));
603 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900604 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900605 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900606 << "Failed to create directory symlink.";
607
brettw@chromium.org70684242013-12-05 03:22:49 +0900608 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900609 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900610
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900611 // Test that a loop in the links causes NormalizeFilePath() to return false.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900612 link_from = temp_dir_.path().Append(FPL("link_a"));
613 link_to = temp_dir_.path().Append(FPL("link_b"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900614 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900615 << "Failed to create loop symlink a.";
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900616 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900617 << "Failed to create loop symlink b.";
618
619 // Infinite loop!
brettw@chromium.org70684242013-12-05 03:22:49 +0900620 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900621}
622#endif // defined(OS_POSIX)
623
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900624TEST_F(FileUtilTest, DeleteNonExistent) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900625 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900626 ASSERT_FALSE(PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900627
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900628 EXPECT_TRUE(DeleteFile(non_existent, false));
629 ASSERT_FALSE(PathExists(non_existent));
630 EXPECT_TRUE(DeleteFile(non_existent, true));
631 ASSERT_FALSE(PathExists(non_existent));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900632}
633
634TEST_F(FileUtilTest, DeleteFile) {
635 // Create a file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900636 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900637 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900638 ASSERT_TRUE(PathExists(file_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900639
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900640 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900641 EXPECT_TRUE(DeleteFile(file_name, false));
642 EXPECT_FALSE(PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900643
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900644 // Test recursive case, create a new file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900645 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900646 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900647 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900648
649 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900650 EXPECT_TRUE(DeleteFile(file_name, true));
651 EXPECT_FALSE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900652}
653
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900654#if defined(OS_POSIX)
655TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900656 // Create a file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900657 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
658 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900659 ASSERT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900660
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900661 // Create a symlink to the file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900662 FilePath file_link = temp_dir_.path().Append("file_link_2");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900663 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900664 << "Failed to create symlink.";
665
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900666 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900667 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900668
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900669 // Make sure original file is not deleted.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900670 EXPECT_FALSE(PathExists(file_link));
671 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900672}
673
674TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900675 // Create a non-existent file path.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900676 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900677 EXPECT_FALSE(PathExists(non_existent));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900678
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900679 // Create a symlink to the non-existent file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900680 FilePath file_link = temp_dir_.path().Append("file_link_3");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900681 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900682 << "Failed to create symlink.";
683
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900684 // Make sure the symbolic link is exist.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900685 EXPECT_TRUE(IsLink(file_link));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900686 EXPECT_FALSE(PathExists(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900687
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900688 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900689 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900690
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900691 // Make sure the symbolic link is deleted.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900692 EXPECT_FALSE(IsLink(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900693}
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900694
695TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
696 // Create a file path.
697 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900698 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900699
700 const std::string kData("hello");
701
702 int buffer_size = kData.length();
703 char* buffer = new char[buffer_size];
704
705 // Write file.
706 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900707 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900708 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900709
710 // Make sure the file is readable.
711 int32 mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900712 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
713 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900714
715 // Get rid of the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900716 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
717 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
718 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900719 // Make sure the file can't be read.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900720 EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900721
722 // Give the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900723 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
724 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
725 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900726 // Make sure the file can be read.
727 EXPECT_EQ(static_cast<int>(kData.length()),
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900728 ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900729
730 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900731 EXPECT_TRUE(DeleteFile(file_name, false));
732 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900733
734 delete[] buffer;
735}
736
737TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
738 // Create a file path.
739 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900740 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900741
742 const std::string kData("hello");
743
744 // Write file.
745 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900746 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900747 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900748
749 // Make sure the file is writable.
750 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900751 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
752 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900753 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900754
755 // Get rid of the write permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900756 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
757 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
758 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900759 // Make sure the file can't be write.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900760 EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900761 EXPECT_FALSE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900762
763 // Give read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900764 EXPECT_TRUE(SetPosixFilePermissions(file_name,
765 FILE_PERMISSION_WRITE_BY_USER));
766 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
767 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900768 // Make sure the file can be write.
769 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900770 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900771 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900772
773 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900774 EXPECT_TRUE(DeleteFile(file_name, false));
775 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900776}
777
778TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
779 // Create a directory path.
780 FilePath subdir_path =
781 temp_dir_.path().Append(FPL("PermissionTest1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900782 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900783 ASSERT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900784
785 // Create a dummy file to enumerate.
786 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900787 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900788 const std::string kData("hello");
789 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900790 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900791 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900792
793 // Make sure the directory has the all permissions.
794 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900795 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
796 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900797
798 // Get rid of the permissions from the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900799 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
800 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
801 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900802
803 // Make sure the file in the directory can't be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900804 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900805 EXPECT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900806 FindResultCollector c1(f1);
807 EXPECT_EQ(c1.size(), 0);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900808 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900809
810 // Give the permissions to the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900811 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
812 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
813 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900814
815 // Make sure the file in the directory can be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900816 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900817 FindResultCollector c2(f2);
818 EXPECT_TRUE(c2.HasFile(file_name));
819 EXPECT_EQ(c2.size(), 1);
820
821 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900822 EXPECT_TRUE(DeleteFile(subdir_path, true));
823 EXPECT_FALSE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900824}
825
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900826#endif // defined(OS_POSIX)
827
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900828#if defined(OS_WIN)
829// Tests that the Delete function works for wild cards, especially
830// with the recursion flag. Also coincidentally tests PathExists.
831// TODO(erikkay): see if anyone's actually using this feature of the API
832TEST_F(FileUtilTest, DeleteWildCard) {
833 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900834 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900835 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900836 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900837
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900838 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900839 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900840 ASSERT_TRUE(PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900841
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900842 // Create the wildcard path
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900843 FilePath directory_contents = temp_dir_.path();
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900844 directory_contents = directory_contents.Append(FPL("*"));
845
initial.commit3f4a7322008-07-27 06:49:38 +0900846 // Delete non-recursively and check that only the file is deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900847 EXPECT_TRUE(DeleteFile(directory_contents, false));
848 EXPECT_FALSE(PathExists(file_name));
849 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900850
zork@chromium.org61be4f42010-05-07 09:05:36 +0900851 // Delete recursively and make sure all contents are deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900852 EXPECT_TRUE(DeleteFile(directory_contents, true));
853 EXPECT_FALSE(PathExists(file_name));
854 EXPECT_FALSE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900855}
856
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900857// TODO(erikkay): see if anyone's actually using this feature of the API
858TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
859 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900860 FilePath subdir_path =
861 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900862 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900863 ASSERT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900864
865 // Create the wildcard path
866 FilePath directory_contents = subdir_path;
867 directory_contents = directory_contents.Append(FPL("*"));
868
869 // Delete non-recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900870 EXPECT_TRUE(DeleteFile(directory_contents, false));
871 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900872
873 // Delete recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900874 EXPECT_TRUE(DeleteFile(directory_contents, true));
875 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900876}
877#endif
878
879// Tests non-recursive Delete() for a directory.
880TEST_F(FileUtilTest, DeleteDirNonRecursive) {
881 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900882 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900883 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900884 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900885
886 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
887 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900888 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900889
890 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900891 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900892 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900893
894 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900895 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900896 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900897
898 // Delete non-recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900899 EXPECT_TRUE(DeleteFile(subdir_path2, false));
900 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900901
902 // Delete non-recursively and check that nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900903 EXPECT_FALSE(DeleteFile(test_subdir, false));
904 EXPECT_TRUE(PathExists(test_subdir));
905 EXPECT_TRUE(PathExists(file_name));
906 EXPECT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900907}
908
909// Tests recursive Delete() for a directory.
910TEST_F(FileUtilTest, DeleteDirRecursive) {
911 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900912 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900913 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900914 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900915
916 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
917 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900918 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900919
920 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900921 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900922 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900923
924 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900925 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900926 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900927
928 // Delete recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900929 EXPECT_TRUE(DeleteFile(subdir_path2, true));
930 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900931
932 // Delete recursively and check that everything got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900933 EXPECT_TRUE(DeleteFile(test_subdir, true));
934 EXPECT_FALSE(PathExists(file_name));
935 EXPECT_FALSE(PathExists(subdir_path1));
936 EXPECT_FALSE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900937}
938
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900939TEST_F(FileUtilTest, MoveFileNew) {
940 // Create a file
941 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900942 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900943 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900944 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900945
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900946 // The destination.
947 FilePath file_name_to = temp_dir_.path().Append(
948 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900949 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900950
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900951 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900952
953 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900954 EXPECT_FALSE(PathExists(file_name_from));
955 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900956}
957
958TEST_F(FileUtilTest, MoveFileExists) {
959 // Create a file
960 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900961 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900962 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900963 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900964
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900965 // The destination name.
966 FilePath file_name_to = temp_dir_.path().Append(
967 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900968 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900969 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900970
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900971 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900972
973 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900974 EXPECT_FALSE(PathExists(file_name_from));
975 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900976 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
977}
978
979TEST_F(FileUtilTest, MoveFileDirExists) {
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
986 // The destination directory
987 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900988 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900989 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900990 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900991
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900992 EXPECT_FALSE(Move(file_name_from, dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900993}
994
995
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900996TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900997 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900998 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900999 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001000 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001001 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001002
1003 // Create a file under the directory
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001004 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1005 FilePath file_name_from = dir_name_from.Append(txt_file_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001006 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001007 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001008
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001009 // Move the directory.
1010 FilePath dir_name_to =
1011 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001012 FilePath file_name_to =
1013 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001014
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001015 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001016
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001017 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001018
1019 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001020 EXPECT_FALSE(PathExists(dir_name_from));
1021 EXPECT_FALSE(PathExists(file_name_from));
1022 EXPECT_TRUE(PathExists(dir_name_to));
1023 EXPECT_TRUE(PathExists(file_name_to));
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001024
1025 // Test path traversal.
1026 file_name_from = dir_name_to.Append(txt_file_name);
1027 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1028 file_name_to = file_name_to.Append(txt_file_name);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001029 EXPECT_FALSE(Move(file_name_from, file_name_to));
1030 EXPECT_TRUE(PathExists(file_name_from));
1031 EXPECT_FALSE(PathExists(file_name_to));
1032 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1033 EXPECT_FALSE(PathExists(file_name_from));
1034 EXPECT_TRUE(PathExists(file_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001035}
1036
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001037TEST_F(FileUtilTest, MoveExist) {
1038 // Create a directory
1039 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001040 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001041 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001042 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001043
1044 // Create a file under the directory
1045 FilePath file_name_from =
1046 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1047 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001048 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001049
1050 // Move the directory
1051 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001052 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001053
1054 FilePath dir_name_to =
1055 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1056 FilePath file_name_to =
1057 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1058
1059 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001060 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001061 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001062
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001063 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001064
1065 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001066 EXPECT_FALSE(PathExists(dir_name_from));
1067 EXPECT_FALSE(PathExists(file_name_from));
1068 EXPECT_TRUE(PathExists(dir_name_to));
1069 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001070}
1071
1072TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001073 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001074 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001075 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001076 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001077 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001078
1079 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001080 FilePath file_name_from =
1081 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001082 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001083 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001084
1085 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001086 FilePath subdir_name_from =
1087 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001088 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001089 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001090
1091 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001092 FilePath file_name2_from =
1093 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001094 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001095 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001096
1097 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001098 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001099 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001100 FilePath file_name_to =
1101 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1102 FilePath subdir_name_to =
1103 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1104 FilePath file_name2_to =
1105 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001106
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001107 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001108
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001109 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001110
1111 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001112 EXPECT_TRUE(PathExists(dir_name_from));
1113 EXPECT_TRUE(PathExists(file_name_from));
1114 EXPECT_TRUE(PathExists(subdir_name_from));
1115 EXPECT_TRUE(PathExists(file_name2_from));
1116 EXPECT_TRUE(PathExists(dir_name_to));
1117 EXPECT_TRUE(PathExists(file_name_to));
1118 EXPECT_TRUE(PathExists(subdir_name_to));
1119 EXPECT_TRUE(PathExists(file_name2_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001120}
1121
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001122TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1123 // Create a directory.
1124 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001125 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001126 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001127 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001128
1129 // Create a file under the directory.
1130 FilePath file_name_from =
1131 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1132 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001133 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001134
1135 // Create a subdirectory.
1136 FilePath subdir_name_from =
1137 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001138 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001139 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001140
1141 // Create a file under the subdirectory.
1142 FilePath file_name2_from =
1143 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1144 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001145 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001146
1147 // Copy the directory recursively.
1148 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001149 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001150
1151 FilePath dir_name_to =
1152 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1153 FilePath file_name_to =
1154 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1155 FilePath subdir_name_to =
1156 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1157 FilePath file_name2_to =
1158 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1159
1160 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001161 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001162 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001163
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001164 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001165
1166 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001167 EXPECT_TRUE(PathExists(dir_name_from));
1168 EXPECT_TRUE(PathExists(file_name_from));
1169 EXPECT_TRUE(PathExists(subdir_name_from));
1170 EXPECT_TRUE(PathExists(file_name2_from));
1171 EXPECT_TRUE(PathExists(dir_name_to));
1172 EXPECT_TRUE(PathExists(file_name_to));
1173 EXPECT_TRUE(PathExists(subdir_name_to));
1174 EXPECT_TRUE(PathExists(file_name2_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001175}
1176
1177TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001178 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001179 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001180 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001181 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001182 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001183
1184 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001185 FilePath file_name_from =
1186 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001187 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001188 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001189
1190 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001191 FilePath subdir_name_from =
1192 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001193 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001194 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001195
1196 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001197 FilePath file_name2_from =
1198 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001199 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001200 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001201
1202 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001203 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001204 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001205 FilePath file_name_to =
1206 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1207 FilePath subdir_name_to =
1208 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001209
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001210 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001211
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001212 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001213
1214 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001215 EXPECT_TRUE(PathExists(dir_name_from));
1216 EXPECT_TRUE(PathExists(file_name_from));
1217 EXPECT_TRUE(PathExists(subdir_name_from));
1218 EXPECT_TRUE(PathExists(file_name2_from));
1219 EXPECT_TRUE(PathExists(dir_name_to));
1220 EXPECT_TRUE(PathExists(file_name_to));
1221 EXPECT_FALSE(PathExists(subdir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001222}
1223
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001224TEST_F(FileUtilTest, CopyDirectoryExists) {
1225 // Create a directory.
1226 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001227 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001228 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001229 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001230
1231 // Create a file under the directory.
1232 FilePath file_name_from =
1233 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1234 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001235 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001236
1237 // Create a subdirectory.
1238 FilePath subdir_name_from =
1239 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001240 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001241 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001242
1243 // Create a file under the subdirectory.
1244 FilePath file_name2_from =
1245 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1246 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001247 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001248
1249 // Copy the directory not recursively.
1250 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001251 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001252 FilePath file_name_to =
1253 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1254 FilePath subdir_name_to =
1255 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1256
1257 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001258 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001259 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001260
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001261 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001262
1263 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001264 EXPECT_TRUE(PathExists(dir_name_from));
1265 EXPECT_TRUE(PathExists(file_name_from));
1266 EXPECT_TRUE(PathExists(subdir_name_from));
1267 EXPECT_TRUE(PathExists(file_name2_from));
1268 EXPECT_TRUE(PathExists(dir_name_to));
1269 EXPECT_TRUE(PathExists(file_name_to));
1270 EXPECT_FALSE(PathExists(subdir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001271}
1272
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001273TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1274 // Create a file
1275 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001276 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001277 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001278 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001279
1280 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001281 FilePath file_name_to = temp_dir_.path().Append(
1282 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001283 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001284
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001285 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001286
1287 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001288 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001289}
1290
1291TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1292 // Create a file
1293 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001294 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001295 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001296 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001297
1298 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001299 FilePath file_name_to = temp_dir_.path().Append(
1300 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001301 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001302 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001303
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001304 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001305
1306 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001307 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001308 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1309}
1310
1311TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1312 // Create a file
1313 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001314 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001315 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001316 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001317
1318 // The destination
1319 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001320 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001321 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001322 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001323 FilePath file_name_to =
1324 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1325
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001326 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001327
1328 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001329 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001330}
1331
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001332TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1333 // Create a directory.
1334 FilePath dir_name_from =
1335 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001336 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001337 ASSERT_TRUE(PathExists(dir_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001338
1339 // Create a file under the directory.
1340 FilePath file_name_from =
1341 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1342 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001343 ASSERT_TRUE(PathExists(file_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001344
1345 // Copy the directory recursively.
1346 FilePath dir_name_to =
1347 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1348 FilePath file_name_to =
1349 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1350
1351 // Create from path with trailing separators.
1352#if defined(OS_WIN)
1353 FilePath from_path =
1354 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1355#elif defined (OS_POSIX)
1356 FilePath from_path =
1357 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1358#endif
1359
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001360 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001361
1362 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001363 EXPECT_TRUE(PathExists(dir_name_from));
1364 EXPECT_TRUE(PathExists(file_name_from));
1365 EXPECT_TRUE(PathExists(dir_name_to));
1366 EXPECT_TRUE(PathExists(file_name_to));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001367}
1368
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001369// Sets the source file to read-only.
1370void SetReadOnly(const FilePath& path) {
1371#if defined(OS_WIN)
1372 // On Windows, it involves setting a bit.
1373 DWORD attrs = GetFileAttributes(path.value().c_str());
1374 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1375 ASSERT_TRUE(SetFileAttributes(
1376 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY));
1377 attrs = GetFileAttributes(path.value().c_str());
1378 // Files in the temporary directory should not be indexed ever. If this
1379 // assumption change, fix this unit test accordingly.
1380 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1381 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY;
1382 if (win::GetVersion() >= win::VERSION_VISTA)
1383 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1384 ASSERT_EQ(expected, attrs);
1385#else
1386 // On all other platforms, it involves removing the write bit.
1387 EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR));
1388#endif
1389}
1390
1391bool IsReadOnly(const FilePath& path) {
1392#if defined(OS_WIN)
1393 DWORD attrs = GetFileAttributes(path.value().c_str());
1394 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1395 return attrs & FILE_ATTRIBUTE_READONLY;
1396#else
1397 int mode = 0;
1398 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1399 return !(mode & S_IWUSR);
1400#endif
1401}
1402
1403TEST_F(FileUtilTest, CopyDirectoryACL) {
1404 // Create a directory.
1405 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
1406 CreateDirectory(src);
1407 ASSERT_TRUE(PathExists(src));
1408
1409 // Create a file under the directory.
1410 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1411 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
1412 SetReadOnly(src_file);
1413 ASSERT_TRUE(IsReadOnly(src_file));
1414
1415 // Copy the directory recursively.
1416 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1417 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1418 EXPECT_TRUE(CopyDirectory(src, dst, true));
1419
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001420 ASSERT_FALSE(IsReadOnly(dst_file));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001421}
1422
initial.commit3f4a7322008-07-27 06:49:38 +09001423TEST_F(FileUtilTest, CopyFile) {
1424 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001425 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001426 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001427 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001428 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001429
1430 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001431 FilePath file_name_from =
1432 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001433 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1434 CreateTextFile(file_name_from, file_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001435 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001436
1437 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001438 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001439 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001440
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001441 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001442 FilePath dest_file2(dir_name_from);
1443 dest_file2 = dest_file2.AppendASCII("..");
1444 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001445 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
1446 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from, dest_file2));
evan@chromium.org1543ad32009-08-27 05:00:14 +09001447
1448 FilePath dest_file2_test(dir_name_from);
1449 dest_file2_test = dest_file2_test.DirName();
1450 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001451
1452 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001453 EXPECT_TRUE(PathExists(file_name_from));
1454 EXPECT_TRUE(PathExists(dest_file));
initial.commit3f4a7322008-07-27 06:49:38 +09001455 const std::wstring read_contents = ReadTextFile(dest_file);
1456 EXPECT_EQ(file_contents, read_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001457 EXPECT_TRUE(PathExists(dest_file2_test));
1458 EXPECT_TRUE(PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001459}
1460
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001461TEST_F(FileUtilTest, CopyFileACL) {
1462 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1463 // this test case asserts the access control bits are meeting expectations in
1464 // CopyFileUnsafe().
1465 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt"));
1466 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1467 CreateTextFile(src, file_contents);
1468
1469 // Set the source file to read-only.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001470 ASSERT_FALSE(IsReadOnly(src));
1471 SetReadOnly(src);
1472 ASSERT_TRUE(IsReadOnly(src));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001473
1474 // Copy the file.
1475 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt"));
1476 ASSERT_TRUE(CopyFile(src, dst));
1477 EXPECT_EQ(file_contents, ReadTextFile(dst));
1478
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001479 ASSERT_FALSE(IsReadOnly(dst));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001480}
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001481
erikkay@google.comf2406842008-08-21 00:59:49 +09001482// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001483// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001484typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001485
erikkay@google.comf2406842008-08-21 00:59:49 +09001486TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001487 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001488 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001489 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001490 ASSERT_TRUE(PathExists(data_dir));
initial.commit3f4a7322008-07-27 06:49:38 +09001491
evanm@google.com874d1672008-10-31 08:54:04 +09001492 FilePath original_file =
1493 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1494 FilePath same_file =
1495 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1496 FilePath same_length_file =
1497 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1498 FilePath different_file =
1499 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1500 FilePath different_first_file =
1501 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1502 FilePath different_last_file =
1503 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1504 FilePath empty1_file =
1505 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1506 FilePath empty2_file =
1507 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1508 FilePath shortened_file =
1509 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1510 FilePath binary_file =
1511 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1512 FilePath binary_file_same =
1513 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1514 FilePath binary_file_diff =
1515 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001516
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001517 EXPECT_TRUE(ContentsEqual(original_file, original_file));
1518 EXPECT_TRUE(ContentsEqual(original_file, same_file));
1519 EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
1520 EXPECT_FALSE(ContentsEqual(original_file, different_file));
1521 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1522 FilePath(FILE_PATH_LITERAL("bogusname"))));
1523 EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
1524 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1525 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1526 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1527 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1528 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1529 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
initial.commit3f4a7322008-07-27 06:49:38 +09001530}
1531
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001532TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1533 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001534 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001535 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001536 ASSERT_TRUE(PathExists(data_dir));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001537
1538 FilePath original_file =
1539 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1540 FilePath same_file =
1541 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1542 FilePath crlf_file =
1543 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1544 FilePath shortened_file =
1545 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1546 FilePath different_file =
1547 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1548 FilePath different_first_file =
1549 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1550 FilePath different_last_file =
1551 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1552 FilePath first1_file =
1553 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1554 FilePath first2_file =
1555 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1556 FilePath empty1_file =
1557 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1558 FilePath empty2_file =
1559 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1560 FilePath blank_line_file =
1561 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1562 FilePath blank_line_crlf_file =
1563 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1564
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001565 EXPECT_TRUE(TextContentsEqual(original_file, same_file));
1566 EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
1567 EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
1568 EXPECT_FALSE(TextContentsEqual(original_file, different_file));
1569 EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
1570 EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
1571 EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
1572 EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
1573 EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
1574 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001575}
1576
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001577// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001578#if defined(OS_WIN)
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001579TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1580 // Create a directory
1581 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001582 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001583 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001584 ASSERT_TRUE(PathExists(dir_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001585
1586 // Create a file under the directory
1587 FilePath file_name_from =
1588 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1589 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001590 ASSERT_TRUE(PathExists(file_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001591
1592 // Move the directory by using CopyAndDeleteDirectory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001593 FilePath dir_name_to = temp_dir_.path().Append(
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001594 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1595 FilePath file_name_to =
1596 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1597
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001598 ASSERT_FALSE(PathExists(dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001599
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001600 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +09001601 dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001602
1603 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001604 EXPECT_FALSE(PathExists(dir_name_from));
1605 EXPECT_FALSE(PathExists(file_name_from));
1606 EXPECT_TRUE(PathExists(dir_name_to));
1607 EXPECT_TRUE(PathExists(file_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001608}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001609
1610TEST_F(FileUtilTest, GetTempDirTest) {
1611 static const TCHAR* kTmpKey = _T("TMP");
1612 static const TCHAR* kTmpValues[] = {
1613 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1614 };
1615 // Save the original $TMP.
1616 size_t original_tmp_size;
1617 TCHAR* original_tmp;
1618 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1619 // original_tmp may be NULL.
1620
1621 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1622 FilePath path;
1623 ::_tputenv_s(kTmpKey, kTmpValues[i]);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001624 GetTempDir(&path);
tkent@chromium.org8da14162009-10-09 16:33:39 +09001625 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1626 " result=" << path.value();
1627 }
1628
1629 // Restore the original $TMP.
1630 if (original_tmp) {
1631 ::_tputenv_s(kTmpKey, original_tmp);
1632 free(original_tmp);
1633 } else {
1634 ::_tputenv_s(kTmpKey, _T(""));
1635 }
1636}
1637#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001638
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001639TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1640 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001641 for (int i = 0; i < 3; i++) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001642 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001643 EXPECT_TRUE(PathExists(temp_files[i]));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001644 EXPECT_FALSE(DirectoryExists(temp_files[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001645 }
1646 for (int i = 0; i < 3; i++)
1647 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1648 for (int i = 0; i < 3; i++)
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001649 EXPECT_TRUE(DeleteFile(temp_files[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001650}
1651
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001652TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001653 FilePath names[3];
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001654 FILE* fps[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001655 int i;
1656
1657 // Create; make sure they are open and exist.
1658 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001659 fps[i] = CreateAndOpenTemporaryFile(&(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001660 ASSERT_TRUE(fps[i]);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001661 EXPECT_TRUE(PathExists(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001662 }
1663
1664 // Make sure all names are unique.
1665 for (i = 0; i < 3; ++i) {
1666 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1667 }
1668
1669 // Close and delete.
1670 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001671 EXPECT_TRUE(CloseFile(fps[i]));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001672 EXPECT_TRUE(DeleteFile(names[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001673 }
initial.commit3f4a7322008-07-27 06:49:38 +09001674}
1675
1676TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001677 FilePath temp_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001678 ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001679 EXPECT_TRUE(PathExists(temp_dir));
1680 EXPECT_TRUE(DeleteFile(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001681}
1682
skerner@chromium.orge4432392010-05-01 02:00:09 +09001683TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1684 FilePath new_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001685 ASSERT_TRUE(CreateTemporaryDirInDir(
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001686 temp_dir_.path(),
skerner@chromium.orge4432392010-05-01 02:00:09 +09001687 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001688 &new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001689 EXPECT_TRUE(PathExists(new_dir));
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001690 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001691 EXPECT_TRUE(DeleteFile(new_dir, false));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001692}
1693
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001694#if defined(OS_POSIX)
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001695TEST_F(FileUtilTest, GetShmemTempDirTest) {
1696 FilePath dir;
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001697 EXPECT_TRUE(GetShmemTempDir(false, &dir));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001698 EXPECT_TRUE(DirectoryExists(dir));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001699}
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001700#endif
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001701
brettw@chromium.org49de1af2014-02-20 05:34:23 +09001702TEST_F(FileUtilTest, GetHomeDirTest) {
1703#if !defined(OS_ANDROID) // Not implemented on Android.
1704 // We don't actually know what the home directory is supposed to be without
1705 // calling some OS functions which would just duplicate the implementation.
1706 // So here we just test that it returns something "reasonable".
1707 FilePath home = GetHomeDir();
1708 ASSERT_FALSE(home.empty());
1709 ASSERT_TRUE(home.IsAbsolute());
1710#endif
1711}
1712
initial.commit3f4a7322008-07-27 06:49:38 +09001713TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001714 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001715 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001716#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001717 FilePath test_path =
1718 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001719#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001720 FilePath test_path =
1721 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001722#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001723
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001724 EXPECT_FALSE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001725 EXPECT_TRUE(CreateDirectory(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001726 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001727 // CreateDirectory returns true if the DirectoryExists returns true.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001728 EXPECT_TRUE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001729
1730 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001731 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001732 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001733 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001734 EXPECT_TRUE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001735 EXPECT_FALSE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001736
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001737 EXPECT_TRUE(DeleteFile(test_root, true));
1738 EXPECT_FALSE(PathExists(test_root));
1739 EXPECT_FALSE(PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001740
1741 // Verify assumptions made by the Windows implementation:
1742 // 1. The current directory always exists.
1743 // 2. The root directory always exists.
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001744 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001745 FilePath top_level = test_root;
1746 while (top_level != top_level.DirName()) {
1747 top_level = top_level.DirName();
1748 }
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001749 ASSERT_TRUE(DirectoryExists(top_level));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001750
1751 // Given these assumptions hold, it should be safe to
1752 // test that "creating" these directories succeeds.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001753 EXPECT_TRUE(CreateDirectory(
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001754 FilePath(FilePath::kCurrentDirectory)));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001755 EXPECT_TRUE(CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001756
1757#if defined(OS_WIN)
1758 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1759 FilePath invalid_path =
1760 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001761 if (!PathExists(invalid_drive)) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001762 EXPECT_FALSE(CreateDirectory(invalid_path));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001763 }
1764#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001765}
1766
1767TEST_F(FileUtilTest, DetectDirectoryTest) {
1768 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001769 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001770 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001771 EXPECT_FALSE(PathExists(test_root));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001772 EXPECT_TRUE(CreateDirectory(test_root));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001773 EXPECT_TRUE(PathExists(test_root));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001774 EXPECT_TRUE(DirectoryExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001775 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001776 FilePath test_path =
1777 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001778 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001779 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001780 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001781 EXPECT_FALSE(DirectoryExists(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001782 EXPECT_TRUE(DeleteFile(test_path, false));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001783
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001784 EXPECT_TRUE(DeleteFile(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001785}
1786
initial.commit3f4a7322008-07-27 06:49:38 +09001787TEST_F(FileUtilTest, FileEnumeratorTest) {
1788 // Test an empty directory.
brettw@chromium.org56946722013-06-08 13:53:36 +09001789 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001790 EXPECT_EQ(f0.Next().value(), FPL(""));
1791 EXPECT_EQ(f0.Next().value(), FPL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001792
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001793 // Test an empty directory, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001794 FileEnumerator f0_dotdot(temp_dir_.path(), false,
1795 FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001796 EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001797 f0_dotdot.Next().value());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001798 EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001799
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001800 // create the directories
rvargas@chromium.org56472942013-08-15 05:46:05 +09001801 FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001802 EXPECT_TRUE(CreateDirectory(dir1));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001803 FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001804 EXPECT_TRUE(CreateDirectory(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001805 FilePath dir2inner = dir2.Append(FPL("inner"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001806 EXPECT_TRUE(CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001807
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001808 // create the files
rvargas@chromium.org56472942013-08-15 05:46:05 +09001809 FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001810 CreateTextFile(dir2file, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001811 FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001812 CreateTextFile(dir2innerfile, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001813 FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001814 CreateTextFile(file1, std::wstring());
1815 FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
rvargas@chromium.org56472942013-08-15 05:46:05 +09001816 .Append(FPL("file2.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001817 CreateTextFile(file2_rel, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001818 FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001819
1820 // Only enumerate files.
brettw@chromium.org56946722013-06-08 13:53:36 +09001821 FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
initial.commit3f4a7322008-07-27 06:49:38 +09001822 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001823 EXPECT_TRUE(c1.HasFile(file1));
1824 EXPECT_TRUE(c1.HasFile(file2_abs));
1825 EXPECT_TRUE(c1.HasFile(dir2file));
1826 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1827 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001828
1829 // Only enumerate directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001830 FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001831 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001832 EXPECT_TRUE(c2.HasFile(dir1));
1833 EXPECT_TRUE(c2.HasFile(dir2));
1834 EXPECT_TRUE(c2.HasFile(dir2inner));
1835 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001836
tim@chromium.org989d0972008-10-16 11:42:45 +09001837 // Only enumerate directories non-recursively.
brettw@chromium.org56946722013-06-08 13:53:36 +09001838 FileEnumerator f2_non_recursive(
1839 temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001840 FindResultCollector c2_non_recursive(f2_non_recursive);
1841 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1842 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1843 EXPECT_EQ(c2_non_recursive.size(), 2);
1844
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001845 // Only enumerate directories, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001846 FileEnumerator f2_dotdot(temp_dir_.path(), false,
1847 FileEnumerator::DIRECTORIES |
1848 FileEnumerator::INCLUDE_DOT_DOT);
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001849 FindResultCollector c2_dotdot(f2_dotdot);
1850 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1851 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001852 EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001853 EXPECT_EQ(c2_dotdot.size(), 3);
1854
initial.commit3f4a7322008-07-27 06:49:38 +09001855 // Enumerate files and directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001856 FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001857 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001858 EXPECT_TRUE(c3.HasFile(dir1));
1859 EXPECT_TRUE(c3.HasFile(dir2));
1860 EXPECT_TRUE(c3.HasFile(file1));
1861 EXPECT_TRUE(c3.HasFile(file2_abs));
1862 EXPECT_TRUE(c3.HasFile(dir2file));
1863 EXPECT_TRUE(c3.HasFile(dir2inner));
1864 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1865 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001866
1867 // Non-recursive operation.
brettw@chromium.org56946722013-06-08 13:53:36 +09001868 FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001869 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001870 EXPECT_TRUE(c4.HasFile(dir2));
1871 EXPECT_TRUE(c4.HasFile(dir2));
1872 EXPECT_TRUE(c4.HasFile(file1));
1873 EXPECT_TRUE(c4.HasFile(file2_abs));
1874 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001875
1876 // Enumerate with a pattern.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001877 FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001878 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001879 EXPECT_TRUE(c5.HasFile(dir1));
1880 EXPECT_TRUE(c5.HasFile(dir2));
1881 EXPECT_TRUE(c5.HasFile(dir2file));
1882 EXPECT_TRUE(c5.HasFile(dir2inner));
1883 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1884 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001885
rvargas@chromium.org56472942013-08-15 05:46:05 +09001886#if defined(OS_WIN)
1887 {
1888 // Make dir1 point to dir2.
1889 ReparsePoint reparse_point(dir1, dir2);
1890 EXPECT_TRUE(reparse_point.IsValid());
1891
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001892 if ((win::GetVersion() >= win::VERSION_VISTA)) {
rvargas@chromium.org56472942013-08-15 05:46:05 +09001893 // There can be a delay for the enumeration code to see the change on
1894 // the file system so skip this test for XP.
1895 // Enumerate the reparse point.
1896 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
1897 FindResultCollector c6(f6);
1898 FilePath inner2 = dir1.Append(FPL("inner"));
1899 EXPECT_TRUE(c6.HasFile(inner2));
1900 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1901 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
1902 EXPECT_EQ(c6.size(), 3);
1903 }
1904
1905 // No changes for non recursive operation.
1906 FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
1907 FindResultCollector c7(f7);
1908 EXPECT_TRUE(c7.HasFile(dir2));
1909 EXPECT_TRUE(c7.HasFile(dir2));
1910 EXPECT_TRUE(c7.HasFile(file1));
1911 EXPECT_TRUE(c7.HasFile(file2_abs));
1912 EXPECT_EQ(c7.size(), 4);
1913
1914 // Should not enumerate inside dir1 when using recursion.
1915 FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1916 FindResultCollector c8(f8);
1917 EXPECT_TRUE(c8.HasFile(dir1));
1918 EXPECT_TRUE(c8.HasFile(dir2));
1919 EXPECT_TRUE(c8.HasFile(file1));
1920 EXPECT_TRUE(c8.HasFile(file2_abs));
1921 EXPECT_TRUE(c8.HasFile(dir2file));
1922 EXPECT_TRUE(c8.HasFile(dir2inner));
1923 EXPECT_TRUE(c8.HasFile(dir2innerfile));
1924 EXPECT_EQ(c8.size(), 7);
1925 }
1926#endif
1927
initial.commit3f4a7322008-07-27 06:49:38 +09001928 // Make sure the destructor closes the find handle while in the middle of a
1929 // query to allow TearDown to delete the directory.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001930 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1931 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
avi@google.com5cb79352008-12-11 23:55:12 +09001932 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001933}
license.botf003cfe2008-08-24 09:55:55 +09001934
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001935TEST_F(FileUtilTest, AppendToFile) {
1936 FilePath data_dir =
1937 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1938
1939 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001940 if (PathExists(data_dir)) {
1941 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001942 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001943 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001944
1945 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001946 if (PathExists(data_dir)) {
1947 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001948 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001949 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001950 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1951
1952 std::string data("hello");
brettw@chromium.org14b3aa22014-03-12 05:59:02 +09001953 EXPECT_EQ(-1, AppendToFile(foobar, data.c_str(), data.length()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001954 EXPECT_EQ(static_cast<int>(data.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09001955 WriteFile(foobar, data.c_str(), data.length()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001956 EXPECT_EQ(static_cast<int>(data.length()),
brettw@chromium.org14b3aa22014-03-12 05:59:02 +09001957 AppendToFile(foobar, data.c_str(), data.length()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001958
1959 const std::wstring read_content = ReadTextFile(foobar);
1960 EXPECT_EQ(L"hellohello", read_content);
1961}
1962
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09001963TEST_F(FileUtilTest, ReadFileToString) {
1964 const char kTestData[] = "0123";
1965 std::string data;
1966
1967 FilePath file_path =
1968 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
1969
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09001970 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09001971
1972 EXPECT_TRUE(ReadFileToString(file_path, &data));
1973 EXPECT_EQ(kTestData, data);
1974
1975 data = "temp";
1976 EXPECT_FALSE(ReadFileToString(file_path, &data, 0));
1977 EXPECT_EQ(data.length(), 0u);
1978
1979 data = "temp";
1980 EXPECT_FALSE(ReadFileToString(file_path, &data, 2));
1981 EXPECT_EQ("01", data);
1982
1983 data.clear();
1984 EXPECT_FALSE(ReadFileToString(file_path, &data, 3));
1985 EXPECT_EQ("012", data);
1986
1987 data.clear();
1988 EXPECT_TRUE(ReadFileToString(file_path, &data, 4));
1989 EXPECT_EQ("0123", data);
1990
1991 data.clear();
1992 EXPECT_TRUE(ReadFileToString(file_path, &data, 6));
1993 EXPECT_EQ("0123", data);
1994
1995 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6));
1996
1997 EXPECT_TRUE(ReadFileToString(file_path, NULL));
1998
1999 EXPECT_TRUE(base::DeleteFile(file_path, false));
2000
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002001 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002002 EXPECT_FALSE(ReadFileToString(file_path, &data));
2003 EXPECT_EQ(data.length(), 0u);
2004
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002005 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002006 EXPECT_FALSE(ReadFileToString(file_path, &data, 6));
2007 EXPECT_EQ(data.length(), 0u);
2008}
2009
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002010TEST_F(FileUtilTest, TouchFile) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002011 FilePath data_dir =
2012 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002013
2014 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002015 if (PathExists(data_dir)) {
2016 ASSERT_TRUE(DeleteFile(data_dir, true));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002017 }
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002018 ASSERT_TRUE(CreateDirectory(data_dir));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002019
2020 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
2021 std::string data("hello");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002022 ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length()));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002023
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002024 Time access_time;
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002025 // This timestamp is divisible by one day (in local timezone),
2026 // to make it work on FAT too.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002027 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002028 &access_time));
2029
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002030 Time modification_time;
jochen@chromium.orga6879772010-02-18 19:02:26 +09002031 // Note that this timestamp is divisible by two (seconds) - FAT stores
2032 // modification times with 2s resolution.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002033 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
jochen@chromium.orga6879772010-02-18 19:02:26 +09002034 &modification_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002035
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002036 ASSERT_TRUE(TouchFile(foobar, access_time, modification_time));
rvargas@chromium.orgb005b382014-01-07 19:06:58 +09002037 File::Info file_info;
brettw@chromium.orga9154032013-12-05 05:56:49 +09002038 ASSERT_TRUE(GetFileInfo(foobar, &file_info));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002039 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
2040 access_time.ToInternalValue());
2041 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
2042 modification_time.ToInternalValue());
jochen@chromium.orga6879772010-02-18 19:02:26 +09002043}
2044
tfarina@chromium.org34828222010-05-26 10:40:12 +09002045TEST_F(FileUtilTest, IsDirectoryEmpty) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002046 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002047
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002048 ASSERT_FALSE(PathExists(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002049
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002050 ASSERT_TRUE(CreateDirectory(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002051
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002052 EXPECT_TRUE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002053
2054 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
2055 std::string bar("baz");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002056 ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length()));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002057
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002058 EXPECT_FALSE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002059}
2060
skerner@google.com93449ef2011-09-22 23:47:18 +09002061#if defined(OS_POSIX)
2062
2063// Testing VerifyPathControlledByAdmin() is hard, because there is no
2064// way a test can make a file owned by root, or change file paths
2065// at the root of the file system. VerifyPathControlledByAdmin()
2066// is implemented as a call to VerifyPathControlledByUser, which gives
2067// us the ability to test with paths under the test's temp directory,
2068// using a user id we control.
2069// Pull tests of VerifyPathControlledByUserTest() into a separate test class
2070// with a common SetUp() method.
2071class VerifyPathControlledByUserTest : public FileUtilTest {
2072 protected:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +09002073 virtual void SetUp() OVERRIDE {
skerner@google.com93449ef2011-09-22 23:47:18 +09002074 FileUtilTest::SetUp();
2075
2076 // Create a basic structure used by each test.
2077 // base_dir_
2078 // |-> sub_dir_
2079 // |-> text_file_
2080
2081 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002082 ASSERT_TRUE(CreateDirectory(base_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002083
2084 sub_dir_ = base_dir_.AppendASCII("sub_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002085 ASSERT_TRUE(CreateDirectory(sub_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002086
2087 text_file_ = sub_dir_.AppendASCII("file.txt");
2088 CreateTextFile(text_file_, L"This text file has some text in it.");
2089
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002090 // Get the user and group files are created with from |base_dir_|.
2091 struct stat stat_buf;
2092 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
2093 uid_ = stat_buf.st_uid;
skerner@chromium.org80784142011-10-18 06:30:29 +09002094 ok_gids_.insert(stat_buf.st_gid);
2095 bad_gids_.insert(stat_buf.st_gid + 1);
2096
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002097 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
skerner@google.com93449ef2011-09-22 23:47:18 +09002098
2099 // To ensure that umask settings do not cause the initial state
2100 // of permissions to be different from what we expect, explicitly
2101 // set permissions on the directories we create.
2102 // Make all files and directories non-world-writable.
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002103
2104 // Users and group can read, write, traverse
2105 int enabled_permissions =
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002106 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002107 // Other users can't read, write, traverse
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002108 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
skerner@google.com93449ef2011-09-22 23:47:18 +09002109
2110 ASSERT_NO_FATAL_FAILURE(
2111 ChangePosixFilePermissions(
2112 base_dir_, enabled_permissions, disabled_permissions));
2113 ASSERT_NO_FATAL_FAILURE(
2114 ChangePosixFilePermissions(
2115 sub_dir_, enabled_permissions, disabled_permissions));
2116 }
2117
2118 FilePath base_dir_;
2119 FilePath sub_dir_;
2120 FilePath text_file_;
2121 uid_t uid_;
skerner@chromium.org80784142011-10-18 06:30:29 +09002122
2123 std::set<gid_t> ok_gids_;
2124 std::set<gid_t> bad_gids_;
skerner@google.com93449ef2011-09-22 23:47:18 +09002125};
2126
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002127TEST_F(VerifyPathControlledByUserTest, BadPaths) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002128 // File does not exist.
2129 FilePath does_not_exist = base_dir_.AppendASCII("does")
2130 .AppendASCII("not")
2131 .AppendASCII("exist");
skerner@google.com93449ef2011-09-22 23:47:18 +09002132 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002133 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002134 base_dir_, does_not_exist, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002135
2136 // |base| not a subpath of |path|.
2137 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002138 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002139 sub_dir_, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002140
2141 // An empty base path will fail to be a prefix for any path.
2142 FilePath empty;
2143 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002144 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002145 empty, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002146
2147 // Finding that a bad call fails proves nothing unless a good call succeeds.
2148 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002149 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002150 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002151}
2152
2153TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2154 // Symlinks in the path should cause failure.
2155
2156 // Symlink to the file at the end of the path.
2157 FilePath file_link = base_dir_.AppendASCII("file_link");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002158 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
skerner@google.com93449ef2011-09-22 23:47:18 +09002159 << "Failed to create symlink.";
2160
2161 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002162 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002163 base_dir_, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002164 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002165 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002166 file_link, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002167
2168 // Symlink from one directory to another within the path.
2169 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002170 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
skerner@google.com93449ef2011-09-22 23:47:18 +09002171 << "Failed to create symlink.";
2172
2173 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002174 ASSERT_TRUE(PathExists(file_path_with_link));
skerner@google.com93449ef2011-09-22 23:47:18 +09002175
2176 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002177 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002178 base_dir_, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002179
2180 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002181 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002182 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002183
2184 // Symlinks in parents of base path are allowed.
2185 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002186 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002187 file_path_with_link, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002188}
2189
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002190TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002191 // Get a uid that is not the uid of files we create.
2192 uid_t bad_uid = uid_ + 1;
2193
skerner@google.com93449ef2011-09-22 23:47:18 +09002194 // Make all files and directories non-world-writable.
2195 ASSERT_NO_FATAL_FAILURE(
2196 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2197 ASSERT_NO_FATAL_FAILURE(
2198 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2199 ASSERT_NO_FATAL_FAILURE(
2200 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2201
2202 // We control these paths.
2203 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002204 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002205 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002206 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002207 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002208 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002209 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002210 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002211 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002212
2213 // Another user does not control these paths.
2214 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002215 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002216 base_dir_, sub_dir_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002217 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002218 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002219 base_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002220 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002221 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002222 sub_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002223
2224 // Another group does not control the paths.
2225 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002226 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002227 base_dir_, sub_dir_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002228 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002229 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002230 base_dir_, text_file_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002231 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002232 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002233 sub_dir_, text_file_, uid_, bad_gids_));
2234}
2235
2236TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2237 // Make all files and directories writable only by their owner.
2238 ASSERT_NO_FATAL_FAILURE(
2239 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2240 ASSERT_NO_FATAL_FAILURE(
2241 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2242 ASSERT_NO_FATAL_FAILURE(
2243 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2244
2245 // Any group is okay because the path is not group-writable.
2246 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002247 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002248 base_dir_, sub_dir_, uid_, ok_gids_));
2249 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002250 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002251 base_dir_, text_file_, uid_, ok_gids_));
2252 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002253 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002254 sub_dir_, text_file_, uid_, ok_gids_));
2255
2256 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002257 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002258 base_dir_, sub_dir_, uid_, bad_gids_));
2259 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002260 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002261 base_dir_, text_file_, uid_, bad_gids_));
2262 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002263 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002264 sub_dir_, text_file_, uid_, bad_gids_));
2265
2266 // No group is okay, because we don't check the group
2267 // if no group can write.
2268 std::set<gid_t> no_gids; // Empty set of gids.
2269 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002270 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002271 base_dir_, sub_dir_, uid_, no_gids));
2272 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002273 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002274 base_dir_, text_file_, uid_, no_gids));
2275 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002276 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002277 sub_dir_, text_file_, uid_, no_gids));
2278
2279
2280 // Make all files and directories writable by their group.
2281 ASSERT_NO_FATAL_FAILURE(
2282 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2283 ASSERT_NO_FATAL_FAILURE(
2284 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2285 ASSERT_NO_FATAL_FAILURE(
2286 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2287
2288 // Now |ok_gids_| works, but |bad_gids_| fails.
2289 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002290 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002291 base_dir_, sub_dir_, uid_, ok_gids_));
2292 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002293 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002294 base_dir_, text_file_, uid_, ok_gids_));
2295 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002296 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002297 sub_dir_, text_file_, uid_, ok_gids_));
2298
2299 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002300 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002301 base_dir_, sub_dir_, uid_, bad_gids_));
2302 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002303 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002304 base_dir_, text_file_, uid_, bad_gids_));
2305 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002306 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002307 sub_dir_, text_file_, uid_, bad_gids_));
2308
2309 // Because any group in the group set is allowed,
2310 // the union of good and bad gids passes.
2311
2312 std::set<gid_t> multiple_gids;
2313 std::set_union(
2314 ok_gids_.begin(), ok_gids_.end(),
2315 bad_gids_.begin(), bad_gids_.end(),
2316 std::inserter(multiple_gids, multiple_gids.begin()));
2317
2318 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002319 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002320 base_dir_, sub_dir_, uid_, multiple_gids));
2321 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002322 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002323 base_dir_, text_file_, uid_, multiple_gids));
2324 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002325 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002326 sub_dir_, text_file_, uid_, multiple_gids));
skerner@google.com93449ef2011-09-22 23:47:18 +09002327}
2328
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002329TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002330 // Make all files and directories non-world-writable.
2331 ASSERT_NO_FATAL_FAILURE(
2332 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2333 ASSERT_NO_FATAL_FAILURE(
2334 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2335 ASSERT_NO_FATAL_FAILURE(
2336 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2337
2338 // Initialy, we control all parts of the path.
2339 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002340 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002341 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002342 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002343 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002344 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002345 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002346 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002347 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002348
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09002349 // Make base_dir_ world-writable.
skerner@google.com93449ef2011-09-22 23:47:18 +09002350 ASSERT_NO_FATAL_FAILURE(
2351 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2352 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002353 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002354 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002355 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002356 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002357 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002358 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002359 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002360 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002361
2362 // Make sub_dir_ world writable.
2363 ASSERT_NO_FATAL_FAILURE(
2364 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2365 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002366 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002367 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002368 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002369 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002370 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002371 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002372 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002373 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002374
2375 // Make text_file_ world writable.
2376 ASSERT_NO_FATAL_FAILURE(
2377 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2378 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002379 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002380 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002381 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002382 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002383 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002384 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002385 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002386 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002387
2388 // Make sub_dir_ non-world writable.
2389 ASSERT_NO_FATAL_FAILURE(
2390 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2391 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002392 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002393 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002394 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002395 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002396 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002397 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002398 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002399 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002400
2401 // Make base_dir_ non-world-writable.
2402 ASSERT_NO_FATAL_FAILURE(
2403 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2404 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002405 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002406 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002407 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002408 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002409 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002410 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002411 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002412 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002413
2414 // Back to the initial state: Nothing is writable, so every path
2415 // should pass.
2416 ASSERT_NO_FATAL_FAILURE(
2417 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2418 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002419 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002420 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002421 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002422 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002423 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002424 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002425 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002426 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002427}
2428
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002429#if defined(OS_ANDROID)
2430TEST_F(FileUtilTest, ValidContentUriTest) {
2431 // Get the test image path.
2432 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002433 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002434 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002435 ASSERT_TRUE(PathExists(data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002436 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2437 int64 image_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002438 GetFileSize(image_file, &image_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002439 EXPECT_LT(0, image_size);
2440
2441 // Insert the image into MediaStore. MediaStore will do some conversions, and
2442 // return the content URI.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002443 FilePath path = file_util::InsertImageIntoMediaStore(image_file);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002444 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002445 EXPECT_TRUE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002446 // The file size may not equal to the input image as MediaStore may convert
2447 // the image.
2448 int64 content_uri_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002449 GetFileSize(path, &content_uri_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002450 EXPECT_EQ(image_size, content_uri_size);
2451
2452 // We should be able to read the file.
2453 char* buffer = new char[image_size];
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002454 File file = OpenContentUriForRead(path);
2455 EXPECT_TRUE(file.IsValid());
2456 EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002457 delete[] buffer;
2458}
2459
2460TEST_F(FileUtilTest, NonExistentContentUriTest) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002461 FilePath path("content://foo.bar");
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002462 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002463 EXPECT_FALSE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002464 // Size should be smaller than 0.
2465 int64 size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002466 EXPECT_FALSE(GetFileSize(path, &size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002467
2468 // We should not be able to read the file.
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002469 File file = OpenContentUriForRead(path);
2470 EXPECT_FALSE(file.IsValid());
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002471}
2472#endif
2473
jln@chromium.orgd7493932014-03-01 00:35:36 +09002474TEST(ScopedFD, ScopedFDDoesClose) {
2475 int fds[2];
2476 char c = 0;
2477 ASSERT_EQ(0, pipe(fds));
2478 const int write_end = fds[1];
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002479 base::ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002480 {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002481 base::ScopedFD write_end_closer(fds[1]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002482 }
2483 // This is the only thread. This file descriptor should no longer be valid.
2484 int ret = close(write_end);
2485 EXPECT_EQ(-1, ret);
2486 EXPECT_EQ(EBADF, errno);
2487 // Make sure read(2) won't block.
2488 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK));
2489 // Reading the pipe should EOF.
2490 EXPECT_EQ(0, read(fds[0], &c, 1));
2491}
2492
2493#if defined(GTEST_HAS_DEATH_TEST)
2494void CloseWithScopedFD(int fd) {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002495 base::ScopedFD fd_closer(fd);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002496}
2497#endif
2498
2499TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) {
2500 int fds[2];
2501 ASSERT_EQ(0, pipe(fds));
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002502 base::ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002503 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1])));
2504#if defined(GTEST_HAS_DEATH_TEST)
2505 // This is the only thread. This file descriptor should no longer be valid.
2506 // Trying to close it should crash. This is important for security.
2507 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2508#endif
2509}
2510
skerner@google.com93449ef2011-09-22 23:47:18 +09002511#endif // defined(OS_POSIX)
2512
mark@chromium.org17684802008-09-10 09:16:28 +09002513} // namespace
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002514
2515} // namespace base