blob: bd4839aad50771e4784594ea526900d857b2ab48 [file] [log] [blame]
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <shellapi.h>
10#include <shlobj.h>
tkent@chromium.org8da14162009-10-09 16:33:39 +090011#include <tchar.h>
thakis@chromium.org927d7282011-05-20 08:34:17 +090012#include <winioctl.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090013#endif
initial.commit3f4a7322008-07-27 06:49:38 +090014
jln@chromium.orgd7493932014-03-01 00:35:36 +090015#if defined(OS_POSIX)
16#include <errno.h>
17#include <fcntl.h>
18#include <unistd.h>
19#endif
20
michaelbai@chromium.orgbf8418f2011-10-25 09:08:28 +090021#include <algorithm>
initial.commit3f4a7322008-07-27 06:49:38 +090022#include <fstream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090023#include <set>
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +090024#include <vector>
initial.commit3f4a7322008-07-27 06:49:38 +090025
26#include "base/base_paths.h"
brettw@chromium.org56946722013-06-08 13:53:36 +090027#include "base/files/file_enumerator.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090028#include "base/files/file_path.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +090029#include "base/files/file_util.h"
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +090030#include "base/files/scoped_file.h"
brettw@chromium.org091db522012-11-17 05:34:23 +090031#include "base/files/scoped_temp_dir.h"
initial.commit3f4a7322008-07-27 06:49:38 +090032#include "base/path_service.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090033#include "base/strings/utf_string_conversions.h"
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +090034#include "base/test/test_file_util.h"
brettw@chromium.org61391822011-01-01 05:02:16 +090035#include "base/threading/platform_thread.h"
initial.commit3f4a7322008-07-27 06:49:38 +090036#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090037#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090038
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090039#if defined(OS_WIN)
40#include "base/win/scoped_handle.h"
rvargas@chromium.org56472942013-08-15 05:46:05 +090041#include "base/win/windows_version.h"
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090042#endif
43
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090044#if defined(OS_ANDROID)
45#include "base/android/content_uri_utils.h"
46#endif
47
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090048// This macro helps avoid wrapped lines in the test structs.
49#define FPL(x) FILE_PATH_LITERAL(x)
50
brettw@chromium.org2873d9b2013-11-28 08:22:08 +090051namespace base {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090052
initial.commit3f4a7322008-07-27 06:49:38 +090053namespace {
54
brettw@chromium.org01f3da42014-08-14 05:22:14 +090055// To test that NormalizeFilePath() deals with NTFS reparse points correctly,
56// we need functions to create and delete reparse points.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090057#if defined(OS_WIN)
58typedef struct _REPARSE_DATA_BUFFER {
59 ULONG ReparseTag;
60 USHORT ReparseDataLength;
61 USHORT Reserved;
62 union {
63 struct {
64 USHORT SubstituteNameOffset;
65 USHORT SubstituteNameLength;
66 USHORT PrintNameOffset;
67 USHORT PrintNameLength;
68 ULONG Flags;
69 WCHAR PathBuffer[1];
70 } SymbolicLinkReparseBuffer;
71 struct {
72 USHORT SubstituteNameOffset;
73 USHORT SubstituteNameLength;
74 USHORT PrintNameOffset;
75 USHORT PrintNameLength;
76 WCHAR PathBuffer[1];
77 } MountPointReparseBuffer;
78 struct {
79 UCHAR DataBuffer[1];
80 } GenericReparseBuffer;
81 };
82} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
83
84// Sets a reparse point. |source| will now point to |target|. Returns true if
85// the call succeeds, false otherwise.
86bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
87 std::wstring kPathPrefix = L"\\??\\";
88 std::wstring target_str;
89 // The juction will not work if the target path does not start with \??\ .
90 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
91 target_str += kPathPrefix;
92 target_str += target_path.value();
93 const wchar_t* target = target_str.c_str();
94 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
95 char buffer[2000] = {0};
96 DWORD returned;
97
98 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
99
100 data->ReparseTag = 0xa0000003;
101 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
102
103 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
104 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
105 data->ReparseDataLength = size_target + 4 + 8;
106
107 int data_size = data->ReparseDataLength + 8;
108
109 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
110 NULL, 0, &returned, NULL)) {
111 return false;
112 }
113 return true;
114}
115
116// Delete the reparse point referenced by |source|. Returns true if the call
117// succeeds, false otherwise.
118bool DeleteReparsePoint(HANDLE source) {
119 DWORD returned;
120 REPARSE_DATA_BUFFER data = {0};
121 data.ReparseTag = 0xa0000003;
122 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
123 &returned, NULL)) {
124 return false;
125 }
126 return true;
127}
rvargas@chromium.org56472942013-08-15 05:46:05 +0900128
129// Manages a reparse point for a test.
130class ReparsePoint {
131 public:
132 // Creates a reparse point from |source| (an empty directory) to |target|.
133 ReparsePoint(const FilePath& source, const FilePath& target) {
134 dir_.Set(
135 ::CreateFile(source.value().c_str(),
136 FILE_ALL_ACCESS,
137 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
138 NULL,
139 OPEN_EXISTING,
140 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
141 NULL));
rvargas14fb0e02014-09-24 07:03:11 +0900142 created_ = dir_.IsValid() && SetReparsePoint(dir_.Get(), target);
rvargas@chromium.org56472942013-08-15 05:46:05 +0900143 }
144
145 ~ReparsePoint() {
146 if (created_)
rvargas14fb0e02014-09-24 07:03:11 +0900147 DeleteReparsePoint(dir_.Get());
rvargas@chromium.org56472942013-08-15 05:46:05 +0900148 }
149
150 bool IsValid() { return created_; }
151
152 private:
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900153 win::ScopedHandle dir_;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900154 bool created_;
155 DISALLOW_COPY_AND_ASSIGN(ReparsePoint);
156};
157
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900158#endif
159
skerner@google.com93449ef2011-09-22 23:47:18 +0900160#if defined(OS_POSIX)
161// Provide a simple way to change the permissions bits on |path| in tests.
162// ASSERT failures will return, but not stop the test. Caller should wrap
163// calls to this function in ASSERT_NO_FATAL_FAILURE().
164void ChangePosixFilePermissions(const FilePath& path,
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900165 int mode_bits_to_set,
166 int mode_bits_to_clear) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900167 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
168 << "Can't set and clear the same bits.";
169
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900170 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900171 ASSERT_TRUE(GetPosixFilePermissions(path, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900172 mode |= mode_bits_to_set;
173 mode &= ~mode_bits_to_clear;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900174 ASSERT_TRUE(SetPosixFilePermissions(path, mode));
skerner@google.com93449ef2011-09-22 23:47:18 +0900175}
176#endif // defined(OS_POSIX)
177
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900178const wchar_t bogus_content[] = L"I'm cannon fodder.";
179
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900180const int FILES_AND_DIRECTORIES =
brettw@chromium.org56946722013-06-08 13:53:36 +0900181 FileEnumerator::FILES | FileEnumerator::DIRECTORIES;
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900182
erikkay@google.comf2406842008-08-21 00:59:49 +0900183// file_util winds up using autoreleased objects on the Mac, so this needs
184// to be a PlatformTest
185class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900186 protected:
mostynb1fdbe1e2014-10-08 02:59:11 +0900187 virtual void SetUp() override {
erikkay@google.comf2406842008-08-21 00:59:49 +0900188 PlatformTest::SetUp();
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900189 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commit3f4a7322008-07-27 06:49:38 +0900190 }
191
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900192 ScopedTempDir temp_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900193};
194
195// Collects all the results from the given file enumerator, and provides an
196// interface to query whether a given file is present.
197class FindResultCollector {
198 public:
brettw@chromium.org56946722013-06-08 13:53:36 +0900199 explicit FindResultCollector(FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900200 FilePath cur_file;
201 while (!(cur_file = enumerator.Next()).value().empty()) {
202 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900203 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900204 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900205 << "Same file returned twice";
206
207 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900208 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900209 }
210 }
211
212 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900213 bool HasFile(const FilePath& file) const {
214 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900215 }
evanm@google.com874d1672008-10-31 08:54:04 +0900216
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900217 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900218 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900219 }
initial.commit3f4a7322008-07-27 06:49:38 +0900220
221 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900222 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900223};
224
225// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900226void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900227 const std::wstring& contents) {
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900228 std::wofstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900229 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900230 ASSERT_TRUE(file.is_open());
231 file << contents;
232 file.close();
233}
234
235// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900236std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900237 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900238 std::wifstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900239 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900240 EXPECT_TRUE(file.is_open());
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900241 file.getline(contents, arraysize(contents));
initial.commit3f4a7322008-07-27 06:49:38 +0900242 file.close();
243 return std::wstring(contents);
244}
245
erikkay@google.com014161d2008-08-16 02:45:13 +0900246#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900247uint64 FileTimeAsUint64(const FILETIME& ft) {
248 ULARGE_INTEGER u;
249 u.LowPart = ft.dwLowDateTime;
250 u.HighPart = ft.dwHighDateTime;
251 return u.QuadPart;
252}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900253#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900254
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900255TEST_F(FileUtilTest, FileAndDirectorySize) {
256 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
257 // should return 53 bytes.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900258 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900259 CreateTextFile(file_01, L"12345678901234567890");
260 int64 size_f1 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900261 ASSERT_TRUE(GetFileSize(file_01, &size_f1));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900262 EXPECT_EQ(20ll, size_f1);
263
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900264 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900265 CreateDirectory(subdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900266
267 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
268 CreateTextFile(file_02, L"123456789012345678901234567890");
269 int64 size_f2 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900270 ASSERT_TRUE(GetFileSize(file_02, &size_f2));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900271 EXPECT_EQ(30ll, size_f2);
272
273 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900274 CreateDirectory(subsubdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900275
276 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
277 CreateTextFile(file_03, L"123");
278
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900279 int64 computed_size = ComputeDirectorySize(temp_dir_.path());
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900280 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
281}
282
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900283TEST_F(FileUtilTest, NormalizeFilePathBasic) {
284 // Create a directory under the test dir. Because we create it,
285 // we know it is not a link.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900286 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
287 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900288 FilePath file_b_path = dir_path.Append(FPL("file_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900289 CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900290
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900291 FilePath normalized_file_a_path, normalized_file_b_path;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900292 ASSERT_FALSE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900293 ASSERT_FALSE(NormalizeFilePath(file_a_path, &normalized_file_a_path))
viettrungluu@chromium.orgea703f12010-08-23 01:19:13 +0900294 << "NormalizeFilePath() should fail on nonexistent paths.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900295
296 CreateTextFile(file_a_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900297 ASSERT_TRUE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900298 ASSERT_TRUE(NormalizeFilePath(file_a_path, &normalized_file_a_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900299
300 CreateTextFile(file_b_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900301 ASSERT_TRUE(PathExists(file_b_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900302 ASSERT_TRUE(NormalizeFilePath(file_b_path, &normalized_file_b_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900303
304 // Beacuse this test created |dir_path|, we know it is not a link
305 // or junction. So, the real path of the directory holding file a
306 // must be the parent of the path holding file b.
307 ASSERT_TRUE(normalized_file_a_path.DirName()
308 .IsParent(normalized_file_b_path.DirName()));
309}
310
311#if defined(OS_WIN)
312
313TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
314 // Build the following directory structure:
315 //
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900316 // temp_dir
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900317 // |-> base_a
318 // | |-> sub_a
319 // | |-> file.txt
320 // | |-> long_name___... (Very long name.)
321 // | |-> sub_long
322 // | |-> deep.txt
323 // |-> base_b
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900324 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
325 // |-> to_base_b (reparse point to temp_dir\base_b)
326 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900327
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900328 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900329 ASSERT_TRUE(CreateDirectory(base_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900330
331 FilePath sub_a = base_a.Append(FPL("sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900332 ASSERT_TRUE(CreateDirectory(sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900333
334 FilePath file_txt = sub_a.Append(FPL("file.txt"));
335 CreateTextFile(file_txt, bogus_content);
336
337 // Want a directory whose name is long enough to make the path to the file
338 // inside just under MAX_PATH chars. This will be used to test that when
339 // a junction expands to a path over MAX_PATH chars in length,
340 // NormalizeFilePath() fails without crashing.
341 FilePath sub_long_rel(FPL("sub_long"));
342 FilePath deep_txt(FPL("deep.txt"));
343
344 int target_length = MAX_PATH;
345 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
346 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900347 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900348 // the resulting path is still long enough to hit the failing case in
349 // NormalizePath().
350 const int kCreateDirLimit = 4;
351 target_length -= kCreateDirLimit;
352 FilePath::StringType long_name_str = FPL("long_name_");
353 long_name_str.resize(target_length, '_');
354
355 FilePath long_name = sub_a.Append(FilePath(long_name_str));
356 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
357 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
358
359 FilePath sub_long = deep_file.DirName();
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900360 ASSERT_TRUE(CreateDirectory(sub_long));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900361 CreateTextFile(deep_file, bogus_content);
362
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900363 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900364 ASSERT_TRUE(CreateDirectory(base_b));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900365
366 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900367 ASSERT_TRUE(CreateDirectory(to_sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900368 FilePath normalized_path;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900369 {
370 ReparsePoint reparse_to_sub_a(to_sub_a, sub_a);
371 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900372
rvargas@chromium.org56472942013-08-15 05:46:05 +0900373 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900374 ASSERT_TRUE(CreateDirectory(to_base_b));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900375 ReparsePoint reparse_to_base_b(to_base_b, base_b);
376 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900377
rvargas@chromium.org56472942013-08-15 05:46:05 +0900378 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900379 ASSERT_TRUE(CreateDirectory(to_sub_long));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900380 ReparsePoint reparse_to_sub_long(to_sub_long, sub_long);
381 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900382
rvargas@chromium.org56472942013-08-15 05:46:05 +0900383 // Normalize a junction free path: base_a\sub_a\file.txt .
brettw@chromium.org70684242013-12-05 03:22:49 +0900384 ASSERT_TRUE(NormalizeFilePath(file_txt, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900385 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
386
387 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
388 // the junction to_sub_a.
brettw@chromium.org70684242013-12-05 03:22:49 +0900389 ASSERT_TRUE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900390 &normalized_path));
391 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
392
393 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
394 // normalized to exclude junctions to_base_b and to_sub_a .
brettw@chromium.org70684242013-12-05 03:22:49 +0900395 ASSERT_TRUE(NormalizeFilePath(base_b.Append(FPL("to_base_b"))
rvargas@chromium.org56472942013-08-15 05:46:05 +0900396 .Append(FPL("to_base_b"))
397 .Append(FPL("to_sub_a"))
398 .Append(FPL("file.txt")),
399 &normalized_path));
400 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
401
402 // A long enough path will cause NormalizeFilePath() to fail. Make a long
403 // path using to_base_b many times, and check that paths long enough to fail
404 // do not cause a crash.
405 FilePath long_path = base_b;
406 const int kLengthLimit = MAX_PATH + 200;
407 while (long_path.value().length() <= kLengthLimit) {
408 long_path = long_path.Append(FPL("to_base_b"));
409 }
410 long_path = long_path.Append(FPL("to_sub_a"))
411 .Append(FPL("file.txt"));
412
brettw@chromium.org70684242013-12-05 03:22:49 +0900413 ASSERT_FALSE(NormalizeFilePath(long_path, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900414
415 // Normalizing the junction to deep.txt should fail, because the expanded
416 // path to deep.txt is longer than MAX_PATH.
brettw@chromium.org70684242013-12-05 03:22:49 +0900417 ASSERT_FALSE(NormalizeFilePath(to_sub_long.Append(deep_txt),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900418 &normalized_path));
419
420 // Delete the reparse points, and see that NormalizeFilePath() fails
421 // to traverse them.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900422 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900423
brettw@chromium.org70684242013-12-05 03:22:49 +0900424 ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900425 &normalized_path));
426}
427
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900428TEST_F(FileUtilTest, DevicePathToDriveLetter) {
429 // Get a drive letter.
430 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
431 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
432 LOG(ERROR) << "Can't get a drive letter to test with.";
433 return;
434 }
435
436 // Get the NT style path to that drive.
437 wchar_t device_path[MAX_PATH] = {'\0'};
438 ASSERT_TRUE(
439 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
440 FilePath actual_device_path(device_path);
441 FilePath win32_path;
442
443 // Run DevicePathToDriveLetterPath() on the NT style path we got from
444 // QueryDosDevice(). Expect the drive letter we started with.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900445 ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path, &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900446 ASSERT_EQ(real_drive_letter, win32_path.value());
447
448 // Add some directories to the path. Expect those extra path componenets
449 // to be preserved.
450 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900451 ASSERT_TRUE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900452 actual_device_path.Append(kRelativePath),
453 &win32_path));
454 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
455 win32_path.value());
456
457 // Deform the real path so that it is invalid by removing the last four
458 // characters. The way windows names devices that are hard disks
459 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
460 // than three characters. The only way the truncated string could be a
461 // real drive is if more than 10^3 disks are mounted:
462 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
463 // Check that DevicePathToDriveLetterPath fails.
464 int path_length = actual_device_path.value().length();
465 int new_length = path_length - 4;
466 ASSERT_LT(0, new_length);
467 FilePath prefix_of_real_device_path(
468 actual_device_path.value().substr(0, new_length));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900469 ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path,
470 &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900471
brettw@chromium.orga9154032013-12-05 05:56:49 +0900472 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900473 prefix_of_real_device_path.Append(kRelativePath),
474 &win32_path));
475
476 // Deform the real path so that it is invalid by adding some characters. For
477 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
478 // request for the drive letter whose native path is
479 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
480 // because drives are numbered in order and mounting 112345 hard disks will
481 // never happen.
482 const FilePath::StringType kExtraChars = FPL("12345");
483
484 FilePath real_device_path_plus_numbers(
485 actual_device_path.value() + kExtraChars);
486
brettw@chromium.orga9154032013-12-05 05:56:49 +0900487 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900488 real_device_path_plus_numbers,
489 &win32_path));
490
brettw@chromium.orga9154032013-12-05 05:56:49 +0900491 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900492 real_device_path_plus_numbers.Append(kRelativePath),
493 &win32_path));
494}
495
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900496TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
497 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
498 // if it is available. This test requires that:
499 // - the filesystem at |temp_dir_| supports long filenames.
500 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
501 // directories of |temp_dir_|.
502 const FilePath::CharType kLongDirName[] = FPL("A long path");
503 const FilePath::CharType kTestSubDirName[] = FPL("test");
504 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900505 ASSERT_TRUE(CreateDirectory(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900506
507 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
508 // different short name.
509 WCHAR path_buffer[MAX_PATH];
510 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
511 path_buffer, MAX_PATH);
512 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
513 ASSERT_NE(DWORD(0), path_buffer_length);
514 FilePath short_test_dir(path_buffer);
515 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
516
517 FilePath temp_file;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900518 ASSERT_TRUE(CreateTemporaryFileInDir(short_test_dir, &temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900519 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900520 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900521
522 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
523 // unreadable. We should still be able to create a temp file in the
524 // subdirectory, but we won't be able to determine the long path for it. This
525 // mimics the environment that some users run where their user profiles reside
526 // in a location where the don't have full access to the higher level
527 // directories. (Note that this assumption is true for NTFS, but not for some
528 // network file systems. E.g. AFS).
529 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900530 ASSERT_TRUE(CreateDirectory(access_test_dir));
brettw@chromium.orgbdedd6c2014-08-08 07:57:11 +0900531 base::FilePermissionRestorer long_test_dir_restorer(long_test_dir);
532 ASSERT_TRUE(base::MakeFileUnreadable(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900533
534 // Use the short form of the directory to create a temporary filename.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900535 ASSERT_TRUE(CreateTemporaryFileInDir(
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900536 short_test_dir.Append(kTestSubDirName), &temp_file));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900537 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900538 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
539
540 // Check that the long path can't be determined for |temp_file|.
541 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
542 path_buffer, MAX_PATH);
543 EXPECT_EQ(DWORD(0), path_buffer_length);
544}
545
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900546#endif // defined(OS_WIN)
547
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900548#if defined(OS_POSIX)
549
550TEST_F(FileUtilTest, CreateAndReadSymlinks) {
551 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
552 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
553 CreateTextFile(link_to, bogus_content);
554
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900555 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900556 << "Failed to create file symlink.";
557
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900558 // If we created the link properly, we should be able to read the contents
559 // through it.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900560 std::wstring contents = ReadTextFile(link_from);
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900561 EXPECT_EQ(bogus_content, contents);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900562
563 FilePath result;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900564 ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900565 EXPECT_EQ(link_to.value(), result.value());
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900566
567 // Link to a directory.
568 link_from = temp_dir_.path().Append(FPL("from_dir"));
569 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900570 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900571 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900572 << "Failed to create directory symlink.";
573
574 // Test failures.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900575 EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
576 EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900577 FilePath missing = temp_dir_.path().Append(FPL("missing"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900578 EXPECT_FALSE(ReadSymbolicLink(missing, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900579}
580
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900581// The following test of NormalizeFilePath() require that we create a symlink.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900582// This can not be done on Windows before Vista. On Vista, creating a symlink
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900583// requires privilege "SeCreateSymbolicLinkPrivilege".
584// TODO(skerner): Investigate the possibility of giving base_unittests the
585// privileges required to create a symlink.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900586TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
skerner@chromium.org559baa92010-05-13 00:13:57 +0900587 // Link one file to another.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900588 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
589 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900590 CreateTextFile(link_to, bogus_content);
591
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900592 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900593 << "Failed to create file symlink.";
594
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900595 // Check that NormalizeFilePath sees the link.
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900596 FilePath normalized_path;
brettw@chromium.org70684242013-12-05 03:22:49 +0900597 ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900598 EXPECT_NE(link_from, link_to);
599 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
600 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900601
602 // Link to a directory.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900603 link_from = temp_dir_.path().Append(FPL("from_dir"));
604 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900605 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900606 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900607 << "Failed to create directory symlink.";
608
brettw@chromium.org70684242013-12-05 03:22:49 +0900609 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900610 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900611
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900612 // Test that a loop in the links causes NormalizeFilePath() to return false.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900613 link_from = temp_dir_.path().Append(FPL("link_a"));
614 link_to = temp_dir_.path().Append(FPL("link_b"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900615 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900616 << "Failed to create loop symlink a.";
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900617 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900618 << "Failed to create loop symlink b.";
619
620 // Infinite loop!
brettw@chromium.org70684242013-12-05 03:22:49 +0900621 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900622}
623#endif // defined(OS_POSIX)
624
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900625TEST_F(FileUtilTest, DeleteNonExistent) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900626 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900627 ASSERT_FALSE(PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900628
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900629 EXPECT_TRUE(DeleteFile(non_existent, false));
630 ASSERT_FALSE(PathExists(non_existent));
631 EXPECT_TRUE(DeleteFile(non_existent, true));
632 ASSERT_FALSE(PathExists(non_existent));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900633}
634
ericu@chromium.org472c56f2014-05-22 12:22:48 +0900635TEST_F(FileUtilTest, DeleteNonExistentWithNonExistentParent) {
636 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_topdir");
637 non_existent = non_existent.AppendASCII("bogus_subdir");
638 ASSERT_FALSE(PathExists(non_existent));
639
640 EXPECT_TRUE(DeleteFile(non_existent, false));
641 ASSERT_FALSE(PathExists(non_existent));
642 EXPECT_TRUE(DeleteFile(non_existent, true));
643 ASSERT_FALSE(PathExists(non_existent));
644}
645
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900646TEST_F(FileUtilTest, DeleteFile) {
647 // Create a file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900648 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900649 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900650 ASSERT_TRUE(PathExists(file_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900651
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900652 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900653 EXPECT_TRUE(DeleteFile(file_name, false));
654 EXPECT_FALSE(PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900655
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900656 // Test recursive case, create a new file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900657 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900658 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900659 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900660
661 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900662 EXPECT_TRUE(DeleteFile(file_name, true));
663 EXPECT_FALSE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900664}
665
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900666#if defined(OS_POSIX)
667TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900668 // Create a file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900669 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
670 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900671 ASSERT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900672
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900673 // Create a symlink to the file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900674 FilePath file_link = temp_dir_.path().Append("file_link_2");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900675 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900676 << "Failed to create symlink.";
677
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900678 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900679 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900680
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900681 // Make sure original file is not deleted.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900682 EXPECT_FALSE(PathExists(file_link));
683 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900684}
685
686TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900687 // Create a non-existent file path.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900688 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900689 EXPECT_FALSE(PathExists(non_existent));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900690
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900691 // Create a symlink to the non-existent file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900692 FilePath file_link = temp_dir_.path().Append("file_link_3");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900693 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900694 << "Failed to create symlink.";
695
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900696 // Make sure the symbolic link is exist.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900697 EXPECT_TRUE(IsLink(file_link));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900698 EXPECT_FALSE(PathExists(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900699
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900700 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900701 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900702
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900703 // Make sure the symbolic link is deleted.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900704 EXPECT_FALSE(IsLink(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900705}
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900706
707TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
708 // Create a file path.
709 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900710 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900711
712 const std::string kData("hello");
713
714 int buffer_size = kData.length();
715 char* buffer = new char[buffer_size];
716
717 // Write file.
718 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900719 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900720 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900721
722 // Make sure the file is readable.
723 int32 mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900724 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
725 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900726
727 // Get rid of the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900728 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
729 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
730 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900731 // Make sure the file can't be read.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900732 EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900733
734 // Give the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900735 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
736 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
737 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900738 // Make sure the file can be read.
739 EXPECT_EQ(static_cast<int>(kData.length()),
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900740 ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900741
742 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900743 EXPECT_TRUE(DeleteFile(file_name, false));
744 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900745
746 delete[] buffer;
747}
748
749TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
750 // Create a file path.
751 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900752 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900753
754 const std::string kData("hello");
755
756 // Write file.
757 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900758 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900759 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900760
761 // Make sure the file is writable.
762 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900763 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
764 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900765 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900766
767 // Get rid of the write permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900768 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
769 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
770 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900771 // Make sure the file can't be write.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900772 EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900773 EXPECT_FALSE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900774
775 // Give read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900776 EXPECT_TRUE(SetPosixFilePermissions(file_name,
777 FILE_PERMISSION_WRITE_BY_USER));
778 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
779 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900780 // Make sure the file can be write.
781 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900782 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900783 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900784
785 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900786 EXPECT_TRUE(DeleteFile(file_name, false));
787 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900788}
789
790TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
791 // Create a directory path.
792 FilePath subdir_path =
793 temp_dir_.path().Append(FPL("PermissionTest1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900794 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900795 ASSERT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900796
797 // Create a dummy file to enumerate.
798 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900799 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900800 const std::string kData("hello");
801 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900802 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900803 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900804
805 // Make sure the directory has the all permissions.
806 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900807 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
808 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900809
810 // Get rid of the permissions from the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900811 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
812 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
813 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900814
815 // Make sure the file in the directory can't be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900816 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900817 EXPECT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900818 FindResultCollector c1(f1);
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +0900819 EXPECT_EQ(0, c1.size());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900820 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900821
822 // Give the permissions to the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900823 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
824 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
825 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900826
827 // Make sure the file in the directory can be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900828 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900829 FindResultCollector c2(f2);
830 EXPECT_TRUE(c2.HasFile(file_name));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +0900831 EXPECT_EQ(1, c2.size());
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900832
833 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900834 EXPECT_TRUE(DeleteFile(subdir_path, true));
835 EXPECT_FALSE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900836}
837
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900838#endif // defined(OS_POSIX)
839
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900840#if defined(OS_WIN)
841// Tests that the Delete function works for wild cards, especially
842// with the recursion flag. Also coincidentally tests PathExists.
843// TODO(erikkay): see if anyone's actually using this feature of the API
844TEST_F(FileUtilTest, DeleteWildCard) {
845 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900846 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900847 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900848 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900849
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900850 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900851 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900852 ASSERT_TRUE(PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900853
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900854 // Create the wildcard path
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900855 FilePath directory_contents = temp_dir_.path();
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900856 directory_contents = directory_contents.Append(FPL("*"));
857
initial.commit3f4a7322008-07-27 06:49:38 +0900858 // Delete non-recursively and check that only the file is deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900859 EXPECT_TRUE(DeleteFile(directory_contents, false));
860 EXPECT_FALSE(PathExists(file_name));
861 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900862
zork@chromium.org61be4f42010-05-07 09:05:36 +0900863 // Delete recursively and make sure all contents are deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900864 EXPECT_TRUE(DeleteFile(directory_contents, true));
865 EXPECT_FALSE(PathExists(file_name));
866 EXPECT_FALSE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900867}
868
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900869// TODO(erikkay): see if anyone's actually using this feature of the API
870TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
871 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900872 FilePath subdir_path =
873 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900874 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900875 ASSERT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900876
877 // Create the wildcard path
878 FilePath directory_contents = subdir_path;
879 directory_contents = directory_contents.Append(FPL("*"));
880
881 // Delete non-recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900882 EXPECT_TRUE(DeleteFile(directory_contents, false));
883 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900884
885 // Delete recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900886 EXPECT_TRUE(DeleteFile(directory_contents, true));
887 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900888}
889#endif
890
891// Tests non-recursive Delete() for a directory.
892TEST_F(FileUtilTest, DeleteDirNonRecursive) {
893 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900894 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900895 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900896 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900897
898 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
899 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900900 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900901
902 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900903 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900904 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900905
906 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900907 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900908 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900909
910 // Delete non-recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900911 EXPECT_TRUE(DeleteFile(subdir_path2, false));
912 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900913
914 // Delete non-recursively and check that nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900915 EXPECT_FALSE(DeleteFile(test_subdir, false));
916 EXPECT_TRUE(PathExists(test_subdir));
917 EXPECT_TRUE(PathExists(file_name));
918 EXPECT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900919}
920
921// Tests recursive Delete() for a directory.
922TEST_F(FileUtilTest, DeleteDirRecursive) {
923 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900924 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900925 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900926 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900927
928 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
929 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900930 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900931
932 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900933 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900934 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900935
936 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900937 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900938 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900939
940 // Delete recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900941 EXPECT_TRUE(DeleteFile(subdir_path2, true));
942 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900943
944 // Delete recursively and check that everything got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900945 EXPECT_TRUE(DeleteFile(test_subdir, true));
946 EXPECT_FALSE(PathExists(file_name));
947 EXPECT_FALSE(PathExists(subdir_path1));
948 EXPECT_FALSE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900949}
950
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900951TEST_F(FileUtilTest, MoveFileNew) {
952 // Create a file
953 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900954 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900955 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900956 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900957
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900958 // The destination.
959 FilePath file_name_to = temp_dir_.path().Append(
960 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900961 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900962
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900963 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900964
965 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900966 EXPECT_FALSE(PathExists(file_name_from));
967 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900968}
969
970TEST_F(FileUtilTest, MoveFileExists) {
971 // Create a file
972 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900973 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900974 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900975 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900976
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900977 // The destination name.
978 FilePath file_name_to = temp_dir_.path().Append(
979 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900980 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900981 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900982
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900983 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900984
985 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900986 EXPECT_FALSE(PathExists(file_name_from));
987 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900988 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
989}
990
991TEST_F(FileUtilTest, MoveFileDirExists) {
992 // Create a file
993 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900994 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900995 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900996 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900997
998 // The destination directory
999 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001000 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001001 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001002 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001003
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001004 EXPECT_FALSE(Move(file_name_from, dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001005}
1006
1007
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001008TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001009 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001010 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001011 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001012 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001013 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001014
1015 // Create a file under the directory
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001016 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1017 FilePath file_name_from = dir_name_from.Append(txt_file_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001018 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001019 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001020
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001021 // Move the directory.
1022 FilePath dir_name_to =
1023 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001024 FilePath file_name_to =
1025 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001026
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001027 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001028
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001029 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001030
1031 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001032 EXPECT_FALSE(PathExists(dir_name_from));
1033 EXPECT_FALSE(PathExists(file_name_from));
1034 EXPECT_TRUE(PathExists(dir_name_to));
1035 EXPECT_TRUE(PathExists(file_name_to));
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001036
1037 // Test path traversal.
1038 file_name_from = dir_name_to.Append(txt_file_name);
1039 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1040 file_name_to = file_name_to.Append(txt_file_name);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001041 EXPECT_FALSE(Move(file_name_from, file_name_to));
1042 EXPECT_TRUE(PathExists(file_name_from));
1043 EXPECT_FALSE(PathExists(file_name_to));
1044 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1045 EXPECT_FALSE(PathExists(file_name_from));
1046 EXPECT_TRUE(PathExists(file_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001047}
1048
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001049TEST_F(FileUtilTest, MoveExist) {
1050 // Create a directory
1051 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001052 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001053 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001054 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001055
1056 // Create a file under the directory
1057 FilePath file_name_from =
1058 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1059 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001060 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001061
1062 // Move the directory
1063 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001064 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001065
1066 FilePath dir_name_to =
1067 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1068 FilePath file_name_to =
1069 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1070
1071 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001072 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001073 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001074
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001075 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001076
1077 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001078 EXPECT_FALSE(PathExists(dir_name_from));
1079 EXPECT_FALSE(PathExists(file_name_from));
1080 EXPECT_TRUE(PathExists(dir_name_to));
1081 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001082}
1083
1084TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001085 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001086 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001087 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001088 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001089 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001090
1091 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001092 FilePath file_name_from =
1093 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001094 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001095 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001096
1097 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001098 FilePath subdir_name_from =
1099 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001100 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001101 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001102
1103 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001104 FilePath file_name2_from =
1105 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001106 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001107 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001108
1109 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001110 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001111 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001112 FilePath file_name_to =
1113 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1114 FilePath subdir_name_to =
1115 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1116 FilePath file_name2_to =
1117 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001118
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001119 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001120
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001121 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001122
1123 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001124 EXPECT_TRUE(PathExists(dir_name_from));
1125 EXPECT_TRUE(PathExists(file_name_from));
1126 EXPECT_TRUE(PathExists(subdir_name_from));
1127 EXPECT_TRUE(PathExists(file_name2_from));
1128 EXPECT_TRUE(PathExists(dir_name_to));
1129 EXPECT_TRUE(PathExists(file_name_to));
1130 EXPECT_TRUE(PathExists(subdir_name_to));
1131 EXPECT_TRUE(PathExists(file_name2_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001132}
1133
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001134TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1135 // Create a directory.
1136 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001137 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001138 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001139 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001140
1141 // Create a file under the directory.
1142 FilePath file_name_from =
1143 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1144 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001145 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001146
1147 // Create a subdirectory.
1148 FilePath subdir_name_from =
1149 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001150 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001151 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001152
1153 // Create a file under the subdirectory.
1154 FilePath file_name2_from =
1155 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1156 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001157 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001158
1159 // Copy the directory recursively.
1160 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001161 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001162
1163 FilePath dir_name_to =
1164 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1165 FilePath file_name_to =
1166 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1167 FilePath subdir_name_to =
1168 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1169 FilePath file_name2_to =
1170 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1171
1172 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001173 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001174 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001175
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001176 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001177
1178 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001179 EXPECT_TRUE(PathExists(dir_name_from));
1180 EXPECT_TRUE(PathExists(file_name_from));
1181 EXPECT_TRUE(PathExists(subdir_name_from));
1182 EXPECT_TRUE(PathExists(file_name2_from));
1183 EXPECT_TRUE(PathExists(dir_name_to));
1184 EXPECT_TRUE(PathExists(file_name_to));
1185 EXPECT_TRUE(PathExists(subdir_name_to));
1186 EXPECT_TRUE(PathExists(file_name2_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001187}
1188
1189TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001190 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001191 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001192 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001193 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001194 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001195
1196 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001197 FilePath file_name_from =
1198 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001199 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001200 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001201
1202 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001203 FilePath subdir_name_from =
1204 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001205 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001206 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001207
1208 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001209 FilePath file_name2_from =
1210 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001211 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001212 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001213
1214 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001215 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001216 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001217 FilePath file_name_to =
1218 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1219 FilePath subdir_name_to =
1220 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001221
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001222 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001223
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001224 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001225
1226 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001227 EXPECT_TRUE(PathExists(dir_name_from));
1228 EXPECT_TRUE(PathExists(file_name_from));
1229 EXPECT_TRUE(PathExists(subdir_name_from));
1230 EXPECT_TRUE(PathExists(file_name2_from));
1231 EXPECT_TRUE(PathExists(dir_name_to));
1232 EXPECT_TRUE(PathExists(file_name_to));
1233 EXPECT_FALSE(PathExists(subdir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001234}
1235
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001236TEST_F(FileUtilTest, CopyDirectoryExists) {
1237 // Create a directory.
1238 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001239 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001240 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001241 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001242
1243 // Create a file under the directory.
1244 FilePath file_name_from =
1245 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1246 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001247 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001248
1249 // Create a subdirectory.
1250 FilePath subdir_name_from =
1251 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001252 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001253 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001254
1255 // Create a file under the subdirectory.
1256 FilePath file_name2_from =
1257 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1258 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001259 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001260
1261 // Copy the directory not recursively.
1262 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001263 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001264 FilePath file_name_to =
1265 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1266 FilePath subdir_name_to =
1267 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1268
1269 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001270 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001271 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001272
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001273 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001274
1275 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001276 EXPECT_TRUE(PathExists(dir_name_from));
1277 EXPECT_TRUE(PathExists(file_name_from));
1278 EXPECT_TRUE(PathExists(subdir_name_from));
1279 EXPECT_TRUE(PathExists(file_name2_from));
1280 EXPECT_TRUE(PathExists(dir_name_to));
1281 EXPECT_TRUE(PathExists(file_name_to));
1282 EXPECT_FALSE(PathExists(subdir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001283}
1284
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001285TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1286 // Create a file
1287 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001288 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001289 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001290 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001291
1292 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001293 FilePath file_name_to = temp_dir_.path().Append(
1294 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001295 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001296
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001297 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001298
1299 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001300 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001301}
1302
1303TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1304 // Create a file
1305 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001306 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001307 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001308 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001309
1310 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001311 FilePath file_name_to = temp_dir_.path().Append(
1312 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001313 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001314 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001315
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001316 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001317
1318 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001319 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001320 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1321}
1322
1323TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1324 // Create a file
1325 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001326 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001327 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001328 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001329
1330 // The destination
1331 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001332 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001333 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001334 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001335 FilePath file_name_to =
1336 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1337
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001338 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001339
1340 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001341 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001342}
1343
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001344TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1345 // Create a directory.
1346 FilePath dir_name_from =
1347 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001348 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001349 ASSERT_TRUE(PathExists(dir_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001350
1351 // Create a file under the directory.
1352 FilePath file_name_from =
1353 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1354 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001355 ASSERT_TRUE(PathExists(file_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001356
1357 // Copy the directory recursively.
1358 FilePath dir_name_to =
1359 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1360 FilePath file_name_to =
1361 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1362
1363 // Create from path with trailing separators.
1364#if defined(OS_WIN)
1365 FilePath from_path =
1366 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1367#elif defined (OS_POSIX)
1368 FilePath from_path =
1369 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1370#endif
1371
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001372 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001373
1374 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001375 EXPECT_TRUE(PathExists(dir_name_from));
1376 EXPECT_TRUE(PathExists(file_name_from));
1377 EXPECT_TRUE(PathExists(dir_name_to));
1378 EXPECT_TRUE(PathExists(file_name_to));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001379}
1380
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001381// Sets the source file to read-only.
1382void SetReadOnly(const FilePath& path) {
1383#if defined(OS_WIN)
1384 // On Windows, it involves setting a bit.
1385 DWORD attrs = GetFileAttributes(path.value().c_str());
1386 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1387 ASSERT_TRUE(SetFileAttributes(
1388 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY));
1389 attrs = GetFileAttributes(path.value().c_str());
1390 // Files in the temporary directory should not be indexed ever. If this
1391 // assumption change, fix this unit test accordingly.
1392 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1393 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY;
1394 if (win::GetVersion() >= win::VERSION_VISTA)
1395 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1396 ASSERT_EQ(expected, attrs);
1397#else
1398 // On all other platforms, it involves removing the write bit.
1399 EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR));
1400#endif
1401}
1402
1403bool IsReadOnly(const FilePath& path) {
1404#if defined(OS_WIN)
1405 DWORD attrs = GetFileAttributes(path.value().c_str());
1406 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1407 return attrs & FILE_ATTRIBUTE_READONLY;
1408#else
1409 int mode = 0;
1410 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1411 return !(mode & S_IWUSR);
1412#endif
1413}
1414
1415TEST_F(FileUtilTest, CopyDirectoryACL) {
1416 // Create a directory.
1417 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
1418 CreateDirectory(src);
1419 ASSERT_TRUE(PathExists(src));
1420
1421 // Create a file under the directory.
1422 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1423 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
1424 SetReadOnly(src_file);
1425 ASSERT_TRUE(IsReadOnly(src_file));
1426
1427 // Copy the directory recursively.
1428 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1429 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1430 EXPECT_TRUE(CopyDirectory(src, dst, true));
1431
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001432 ASSERT_FALSE(IsReadOnly(dst_file));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001433}
1434
initial.commit3f4a7322008-07-27 06:49:38 +09001435TEST_F(FileUtilTest, CopyFile) {
1436 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001437 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001438 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001439 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001440 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001441
1442 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001443 FilePath file_name_from =
1444 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001445 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1446 CreateTextFile(file_name_from, file_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001447 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001448
1449 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001450 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001451 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001452
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001453 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001454 FilePath dest_file2(dir_name_from);
1455 dest_file2 = dest_file2.AppendASCII("..");
1456 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001457 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
1458 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from, dest_file2));
evan@chromium.org1543ad32009-08-27 05:00:14 +09001459
1460 FilePath dest_file2_test(dir_name_from);
1461 dest_file2_test = dest_file2_test.DirName();
1462 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001463
1464 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001465 EXPECT_TRUE(PathExists(file_name_from));
1466 EXPECT_TRUE(PathExists(dest_file));
initial.commit3f4a7322008-07-27 06:49:38 +09001467 const std::wstring read_contents = ReadTextFile(dest_file);
1468 EXPECT_EQ(file_contents, read_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001469 EXPECT_TRUE(PathExists(dest_file2_test));
1470 EXPECT_TRUE(PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001471}
1472
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001473TEST_F(FileUtilTest, CopyFileACL) {
1474 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1475 // this test case asserts the access control bits are meeting expectations in
1476 // CopyFileUnsafe().
1477 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt"));
1478 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1479 CreateTextFile(src, file_contents);
1480
1481 // Set the source file to read-only.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001482 ASSERT_FALSE(IsReadOnly(src));
1483 SetReadOnly(src);
1484 ASSERT_TRUE(IsReadOnly(src));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001485
1486 // Copy the file.
1487 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt"));
1488 ASSERT_TRUE(CopyFile(src, dst));
1489 EXPECT_EQ(file_contents, ReadTextFile(dst));
1490
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001491 ASSERT_FALSE(IsReadOnly(dst));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001492}
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001493
erikkay@google.comf2406842008-08-21 00:59:49 +09001494// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001495// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001496typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001497
erikkay@google.comf2406842008-08-21 00:59:49 +09001498TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001499 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001500 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001501 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001502 ASSERT_TRUE(PathExists(data_dir));
initial.commit3f4a7322008-07-27 06:49:38 +09001503
evanm@google.com874d1672008-10-31 08:54:04 +09001504 FilePath original_file =
1505 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1506 FilePath same_file =
1507 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1508 FilePath same_length_file =
1509 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1510 FilePath different_file =
1511 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1512 FilePath different_first_file =
1513 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1514 FilePath different_last_file =
1515 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1516 FilePath empty1_file =
1517 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1518 FilePath empty2_file =
1519 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1520 FilePath shortened_file =
1521 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1522 FilePath binary_file =
1523 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1524 FilePath binary_file_same =
1525 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1526 FilePath binary_file_diff =
1527 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001528
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001529 EXPECT_TRUE(ContentsEqual(original_file, original_file));
1530 EXPECT_TRUE(ContentsEqual(original_file, same_file));
1531 EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
1532 EXPECT_FALSE(ContentsEqual(original_file, different_file));
1533 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1534 FilePath(FILE_PATH_LITERAL("bogusname"))));
1535 EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
1536 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1537 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1538 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1539 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1540 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1541 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
initial.commit3f4a7322008-07-27 06:49:38 +09001542}
1543
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001544TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1545 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001546 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001547 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001548 ASSERT_TRUE(PathExists(data_dir));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001549
1550 FilePath original_file =
1551 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1552 FilePath same_file =
1553 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1554 FilePath crlf_file =
1555 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1556 FilePath shortened_file =
1557 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1558 FilePath different_file =
1559 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1560 FilePath different_first_file =
1561 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1562 FilePath different_last_file =
1563 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1564 FilePath first1_file =
1565 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1566 FilePath first2_file =
1567 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1568 FilePath empty1_file =
1569 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1570 FilePath empty2_file =
1571 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1572 FilePath blank_line_file =
1573 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1574 FilePath blank_line_crlf_file =
1575 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1576
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001577 EXPECT_TRUE(TextContentsEqual(original_file, same_file));
1578 EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
1579 EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
1580 EXPECT_FALSE(TextContentsEqual(original_file, different_file));
1581 EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
1582 EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
1583 EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
1584 EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
1585 EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
1586 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001587}
1588
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001589// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001590#if defined(OS_WIN)
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001591TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1592 // Create a directory
1593 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001594 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001595 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001596 ASSERT_TRUE(PathExists(dir_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001597
1598 // Create a file under the directory
1599 FilePath file_name_from =
1600 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1601 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001602 ASSERT_TRUE(PathExists(file_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001603
1604 // Move the directory by using CopyAndDeleteDirectory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001605 FilePath dir_name_to = temp_dir_.path().Append(
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001606 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1607 FilePath file_name_to =
1608 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1609
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001610 ASSERT_FALSE(PathExists(dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001611
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001612 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +09001613 dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001614
1615 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001616 EXPECT_FALSE(PathExists(dir_name_from));
1617 EXPECT_FALSE(PathExists(file_name_from));
1618 EXPECT_TRUE(PathExists(dir_name_to));
1619 EXPECT_TRUE(PathExists(file_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001620}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001621
1622TEST_F(FileUtilTest, GetTempDirTest) {
1623 static const TCHAR* kTmpKey = _T("TMP");
1624 static const TCHAR* kTmpValues[] = {
1625 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1626 };
1627 // Save the original $TMP.
1628 size_t original_tmp_size;
1629 TCHAR* original_tmp;
1630 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1631 // original_tmp may be NULL.
1632
1633 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1634 FilePath path;
1635 ::_tputenv_s(kTmpKey, kTmpValues[i]);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001636 GetTempDir(&path);
tkent@chromium.org8da14162009-10-09 16:33:39 +09001637 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1638 " result=" << path.value();
1639 }
1640
1641 // Restore the original $TMP.
1642 if (original_tmp) {
1643 ::_tputenv_s(kTmpKey, original_tmp);
1644 free(original_tmp);
1645 } else {
1646 ::_tputenv_s(kTmpKey, _T(""));
1647 }
1648}
1649#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001650
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001651TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1652 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001653 for (int i = 0; i < 3; i++) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001654 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001655 EXPECT_TRUE(PathExists(temp_files[i]));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001656 EXPECT_FALSE(DirectoryExists(temp_files[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001657 }
1658 for (int i = 0; i < 3; i++)
1659 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1660 for (int i = 0; i < 3; i++)
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001661 EXPECT_TRUE(DeleteFile(temp_files[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001662}
1663
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001664TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001665 FilePath names[3];
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001666 FILE* fps[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001667 int i;
1668
1669 // Create; make sure they are open and exist.
1670 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001671 fps[i] = CreateAndOpenTemporaryFile(&(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001672 ASSERT_TRUE(fps[i]);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001673 EXPECT_TRUE(PathExists(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001674 }
1675
1676 // Make sure all names are unique.
1677 for (i = 0; i < 3; ++i) {
1678 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1679 }
1680
1681 // Close and delete.
1682 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001683 EXPECT_TRUE(CloseFile(fps[i]));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001684 EXPECT_TRUE(DeleteFile(names[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001685 }
initial.commit3f4a7322008-07-27 06:49:38 +09001686}
1687
rvargas@chromium.org43d4a8e2014-06-10 20:19:50 +09001688TEST_F(FileUtilTest, FileToFILE) {
1689 File file;
1690 FILE* stream = FileToFILE(file.Pass(), "w");
1691 EXPECT_FALSE(stream);
1692
1693 FilePath file_name = temp_dir_.path().Append(FPL("The file.txt"));
1694 file = File(file_name, File::FLAG_CREATE | File::FLAG_WRITE);
1695 EXPECT_TRUE(file.IsValid());
1696
1697 stream = FileToFILE(file.Pass(), "w");
1698 EXPECT_TRUE(stream);
1699 EXPECT_FALSE(file.IsValid());
1700 EXPECT_TRUE(CloseFile(stream));
1701}
1702
initial.commit3f4a7322008-07-27 06:49:38 +09001703TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001704 FilePath temp_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001705 ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001706 EXPECT_TRUE(PathExists(temp_dir));
1707 EXPECT_TRUE(DeleteFile(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001708}
1709
skerner@chromium.orge4432392010-05-01 02:00:09 +09001710TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1711 FilePath new_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001712 ASSERT_TRUE(CreateTemporaryDirInDir(
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001713 temp_dir_.path(),
skerner@chromium.orge4432392010-05-01 02:00:09 +09001714 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001715 &new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001716 EXPECT_TRUE(PathExists(new_dir));
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001717 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001718 EXPECT_TRUE(DeleteFile(new_dir, false));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001719}
1720
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001721#if defined(OS_POSIX)
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001722TEST_F(FileUtilTest, GetShmemTempDirTest) {
1723 FilePath dir;
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001724 EXPECT_TRUE(GetShmemTempDir(false, &dir));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001725 EXPECT_TRUE(DirectoryExists(dir));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001726}
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001727#endif
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001728
brettw@chromium.org49de1af2014-02-20 05:34:23 +09001729TEST_F(FileUtilTest, GetHomeDirTest) {
1730#if !defined(OS_ANDROID) // Not implemented on Android.
1731 // We don't actually know what the home directory is supposed to be without
1732 // calling some OS functions which would just duplicate the implementation.
1733 // So here we just test that it returns something "reasonable".
1734 FilePath home = GetHomeDir();
1735 ASSERT_FALSE(home.empty());
1736 ASSERT_TRUE(home.IsAbsolute());
1737#endif
1738}
1739
initial.commit3f4a7322008-07-27 06:49:38 +09001740TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001741 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001742 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001743#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001744 FilePath test_path =
1745 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001746#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001747 FilePath test_path =
1748 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001749#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001750
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001751 EXPECT_FALSE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001752 EXPECT_TRUE(CreateDirectory(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001753 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001754 // CreateDirectory returns true if the DirectoryExists returns true.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001755 EXPECT_TRUE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001756
1757 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001758 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001759 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001760 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001761 EXPECT_TRUE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001762 EXPECT_FALSE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001763
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001764 EXPECT_TRUE(DeleteFile(test_root, true));
1765 EXPECT_FALSE(PathExists(test_root));
1766 EXPECT_FALSE(PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001767
1768 // Verify assumptions made by the Windows implementation:
1769 // 1. The current directory always exists.
1770 // 2. The root directory always exists.
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001771 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001772 FilePath top_level = test_root;
1773 while (top_level != top_level.DirName()) {
1774 top_level = top_level.DirName();
1775 }
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001776 ASSERT_TRUE(DirectoryExists(top_level));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001777
1778 // Given these assumptions hold, it should be safe to
1779 // test that "creating" these directories succeeds.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001780 EXPECT_TRUE(CreateDirectory(
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001781 FilePath(FilePath::kCurrentDirectory)));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001782 EXPECT_TRUE(CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001783
1784#if defined(OS_WIN)
1785 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1786 FilePath invalid_path =
1787 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001788 if (!PathExists(invalid_drive)) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001789 EXPECT_FALSE(CreateDirectory(invalid_path));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001790 }
1791#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001792}
1793
1794TEST_F(FileUtilTest, DetectDirectoryTest) {
1795 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001796 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001797 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001798 EXPECT_FALSE(PathExists(test_root));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001799 EXPECT_TRUE(CreateDirectory(test_root));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001800 EXPECT_TRUE(PathExists(test_root));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001801 EXPECT_TRUE(DirectoryExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001802 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001803 FilePath test_path =
1804 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001805 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001806 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001807 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001808 EXPECT_FALSE(DirectoryExists(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001809 EXPECT_TRUE(DeleteFile(test_path, false));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001810
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001811 EXPECT_TRUE(DeleteFile(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001812}
1813
initial.commit3f4a7322008-07-27 06:49:38 +09001814TEST_F(FileUtilTest, FileEnumeratorTest) {
1815 // Test an empty directory.
brettw@chromium.org56946722013-06-08 13:53:36 +09001816 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001817 EXPECT_EQ(FPL(""), f0.Next().value());
1818 EXPECT_EQ(FPL(""), f0.Next().value());
initial.commit3f4a7322008-07-27 06:49:38 +09001819
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001820 // Test an empty directory, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001821 FileEnumerator f0_dotdot(temp_dir_.path(), false,
1822 FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001823 EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001824 f0_dotdot.Next().value());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001825 EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001826
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001827 // create the directories
rvargas@chromium.org56472942013-08-15 05:46:05 +09001828 FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001829 EXPECT_TRUE(CreateDirectory(dir1));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001830 FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001831 EXPECT_TRUE(CreateDirectory(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001832 FilePath dir2inner = dir2.Append(FPL("inner"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001833 EXPECT_TRUE(CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001834
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001835 // create the files
rvargas@chromium.org56472942013-08-15 05:46:05 +09001836 FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001837 CreateTextFile(dir2file, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001838 FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001839 CreateTextFile(dir2innerfile, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001840 FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001841 CreateTextFile(file1, std::wstring());
1842 FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
rvargas@chromium.org56472942013-08-15 05:46:05 +09001843 .Append(FPL("file2.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001844 CreateTextFile(file2_rel, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001845 FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001846
1847 // Only enumerate files.
brettw@chromium.org56946722013-06-08 13:53:36 +09001848 FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
initial.commit3f4a7322008-07-27 06:49:38 +09001849 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001850 EXPECT_TRUE(c1.HasFile(file1));
1851 EXPECT_TRUE(c1.HasFile(file2_abs));
1852 EXPECT_TRUE(c1.HasFile(dir2file));
1853 EXPECT_TRUE(c1.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001854 EXPECT_EQ(4, c1.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001855
1856 // Only enumerate directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001857 FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001858 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001859 EXPECT_TRUE(c2.HasFile(dir1));
1860 EXPECT_TRUE(c2.HasFile(dir2));
1861 EXPECT_TRUE(c2.HasFile(dir2inner));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001862 EXPECT_EQ(3, c2.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001863
tim@chromium.org989d0972008-10-16 11:42:45 +09001864 // Only enumerate directories non-recursively.
brettw@chromium.org56946722013-06-08 13:53:36 +09001865 FileEnumerator f2_non_recursive(
1866 temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001867 FindResultCollector c2_non_recursive(f2_non_recursive);
1868 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1869 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001870 EXPECT_EQ(2, c2_non_recursive.size());
tim@chromium.org989d0972008-10-16 11:42:45 +09001871
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001872 // Only enumerate directories, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001873 FileEnumerator f2_dotdot(temp_dir_.path(), false,
1874 FileEnumerator::DIRECTORIES |
1875 FileEnumerator::INCLUDE_DOT_DOT);
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001876 FindResultCollector c2_dotdot(f2_dotdot);
1877 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1878 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001879 EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001880 EXPECT_EQ(3, c2_dotdot.size());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001881
initial.commit3f4a7322008-07-27 06:49:38 +09001882 // Enumerate files and directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001883 FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001884 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001885 EXPECT_TRUE(c3.HasFile(dir1));
1886 EXPECT_TRUE(c3.HasFile(dir2));
1887 EXPECT_TRUE(c3.HasFile(file1));
1888 EXPECT_TRUE(c3.HasFile(file2_abs));
1889 EXPECT_TRUE(c3.HasFile(dir2file));
1890 EXPECT_TRUE(c3.HasFile(dir2inner));
1891 EXPECT_TRUE(c3.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001892 EXPECT_EQ(7, c3.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001893
1894 // Non-recursive operation.
brettw@chromium.org56946722013-06-08 13:53:36 +09001895 FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001896 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001897 EXPECT_TRUE(c4.HasFile(dir2));
1898 EXPECT_TRUE(c4.HasFile(dir2));
1899 EXPECT_TRUE(c4.HasFile(file1));
1900 EXPECT_TRUE(c4.HasFile(file2_abs));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001901 EXPECT_EQ(4, c4.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001902
1903 // Enumerate with a pattern.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001904 FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001905 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001906 EXPECT_TRUE(c5.HasFile(dir1));
1907 EXPECT_TRUE(c5.HasFile(dir2));
1908 EXPECT_TRUE(c5.HasFile(dir2file));
1909 EXPECT_TRUE(c5.HasFile(dir2inner));
1910 EXPECT_TRUE(c5.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001911 EXPECT_EQ(5, c5.size());
initial.commit3f4a7322008-07-27 06:49:38 +09001912
rvargas@chromium.org56472942013-08-15 05:46:05 +09001913#if defined(OS_WIN)
1914 {
1915 // Make dir1 point to dir2.
1916 ReparsePoint reparse_point(dir1, dir2);
1917 EXPECT_TRUE(reparse_point.IsValid());
1918
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001919 if ((win::GetVersion() >= win::VERSION_VISTA)) {
rvargas@chromium.org56472942013-08-15 05:46:05 +09001920 // There can be a delay for the enumeration code to see the change on
1921 // the file system so skip this test for XP.
1922 // Enumerate the reparse point.
1923 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
1924 FindResultCollector c6(f6);
1925 FilePath inner2 = dir1.Append(FPL("inner"));
1926 EXPECT_TRUE(c6.HasFile(inner2));
1927 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1928 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001929 EXPECT_EQ(3, c6.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001930 }
1931
1932 // No changes for non recursive operation.
1933 FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
1934 FindResultCollector c7(f7);
1935 EXPECT_TRUE(c7.HasFile(dir2));
1936 EXPECT_TRUE(c7.HasFile(dir2));
1937 EXPECT_TRUE(c7.HasFile(file1));
1938 EXPECT_TRUE(c7.HasFile(file2_abs));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001939 EXPECT_EQ(4, c7.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001940
1941 // Should not enumerate inside dir1 when using recursion.
1942 FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1943 FindResultCollector c8(f8);
1944 EXPECT_TRUE(c8.HasFile(dir1));
1945 EXPECT_TRUE(c8.HasFile(dir2));
1946 EXPECT_TRUE(c8.HasFile(file1));
1947 EXPECT_TRUE(c8.HasFile(file2_abs));
1948 EXPECT_TRUE(c8.HasFile(dir2file));
1949 EXPECT_TRUE(c8.HasFile(dir2inner));
1950 EXPECT_TRUE(c8.HasFile(dir2innerfile));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09001951 EXPECT_EQ(7, c8.size());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001952 }
1953#endif
1954
initial.commit3f4a7322008-07-27 06:49:38 +09001955 // Make sure the destructor closes the find handle while in the middle of a
1956 // query to allow TearDown to delete the directory.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001957 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1958 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
avi@google.com5cb79352008-12-11 23:55:12 +09001959 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001960}
license.botf003cfe2008-08-24 09:55:55 +09001961
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001962TEST_F(FileUtilTest, AppendToFile) {
1963 FilePath data_dir =
1964 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1965
1966 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001967 if (PathExists(data_dir)) {
1968 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001969 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001970 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001971
1972 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001973 if (PathExists(data_dir)) {
1974 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001975 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001976 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001977 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1978
1979 std::string data("hello");
chirantan05106cc2014-10-08 08:15:30 +09001980 EXPECT_FALSE(AppendToFile(foobar, data.c_str(), data.size()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001981 EXPECT_EQ(static_cast<int>(data.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09001982 WriteFile(foobar, data.c_str(), data.length()));
chirantan05106cc2014-10-08 08:15:30 +09001983 EXPECT_TRUE(AppendToFile(foobar, data.c_str(), data.size()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001984
1985 const std::wstring read_content = ReadTextFile(foobar);
1986 EXPECT_EQ(L"hellohello", read_content);
1987}
1988
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09001989TEST_F(FileUtilTest, ReadFile) {
1990 // Create a test file to be read.
1991 const std::string kTestData("The quick brown fox jumps over the lazy dog.");
1992 FilePath file_path =
1993 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileTest"));
1994
1995 ASSERT_EQ(static_cast<int>(kTestData.size()),
1996 WriteFile(file_path, kTestData.data(), kTestData.size()));
1997
1998 // Make buffers with various size.
1999 std::vector<char> small_buffer(kTestData.size() / 2);
2000 std::vector<char> exact_buffer(kTestData.size());
2001 std::vector<char> large_buffer(kTestData.size() * 2);
2002
2003 // Read the file with smaller buffer.
2004 int bytes_read_small = ReadFile(
2005 file_path, &small_buffer[0], static_cast<int>(small_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002006 EXPECT_EQ(static_cast<int>(small_buffer.size()), bytes_read_small);
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002007 EXPECT_EQ(
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002008 std::string(kTestData.begin(), kTestData.begin() + small_buffer.size()),
2009 std::string(small_buffer.begin(), small_buffer.end()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002010
2011 // Read the file with buffer which have exactly same size.
2012 int bytes_read_exact = ReadFile(
2013 file_path, &exact_buffer[0], static_cast<int>(exact_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002014 EXPECT_EQ(static_cast<int>(kTestData.size()), bytes_read_exact);
2015 EXPECT_EQ(kTestData, std::string(exact_buffer.begin(), exact_buffer.end()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002016
2017 // Read the file with larger buffer.
2018 int bytes_read_large = ReadFile(
2019 file_path, &large_buffer[0], static_cast<int>(large_buffer.size()));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002020 EXPECT_EQ(static_cast<int>(kTestData.size()), bytes_read_large);
2021 EXPECT_EQ(kTestData, std::string(large_buffer.begin(),
2022 large_buffer.begin() + kTestData.size()));
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09002023
2024 // Make sure the return value is -1 if the file doesn't exist.
2025 FilePath file_path_not_exist =
2026 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileNotExistTest"));
2027 EXPECT_EQ(-1,
2028 ReadFile(file_path_not_exist,
2029 &exact_buffer[0],
2030 static_cast<int>(exact_buffer.size())));
2031}
2032
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002033TEST_F(FileUtilTest, ReadFileToString) {
2034 const char kTestData[] = "0123";
2035 std::string data;
2036
2037 FilePath file_path =
2038 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002039 FilePath file_path_dangerous =
2040 temp_dir_.path().Append(FILE_PATH_LITERAL("..")).
2041 Append(temp_dir_.path().BaseName()).
2042 Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002043
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002044 // Create test file.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002045 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002046
2047 EXPECT_TRUE(ReadFileToString(file_path, &data));
2048 EXPECT_EQ(kTestData, data);
2049
2050 data = "temp";
2051 EXPECT_FALSE(ReadFileToString(file_path, &data, 0));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002052 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002053
2054 data = "temp";
2055 EXPECT_FALSE(ReadFileToString(file_path, &data, 2));
2056 EXPECT_EQ("01", data);
2057
2058 data.clear();
2059 EXPECT_FALSE(ReadFileToString(file_path, &data, 3));
2060 EXPECT_EQ("012", data);
2061
2062 data.clear();
2063 EXPECT_TRUE(ReadFileToString(file_path, &data, 4));
2064 EXPECT_EQ("0123", data);
2065
2066 data.clear();
2067 EXPECT_TRUE(ReadFileToString(file_path, &data, 6));
2068 EXPECT_EQ("0123", data);
2069
2070 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6));
2071
2072 EXPECT_TRUE(ReadFileToString(file_path, NULL));
2073
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002074 data = "temp";
2075 EXPECT_FALSE(ReadFileToString(file_path_dangerous, &data));
2076 EXPECT_EQ(0u, data.length());
2077
2078 // Delete test file.
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002079 EXPECT_TRUE(base::DeleteFile(file_path, false));
2080
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002081 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002082 EXPECT_FALSE(ReadFileToString(file_path, &data));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002083 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002084
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002085 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002086 EXPECT_FALSE(ReadFileToString(file_path, &data, 6));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002087 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002088}
2089
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002090TEST_F(FileUtilTest, TouchFile) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002091 FilePath data_dir =
2092 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002093
2094 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002095 if (PathExists(data_dir)) {
2096 ASSERT_TRUE(DeleteFile(data_dir, true));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002097 }
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002098 ASSERT_TRUE(CreateDirectory(data_dir));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002099
2100 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
2101 std::string data("hello");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002102 ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length()));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002103
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002104 Time access_time;
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002105 // This timestamp is divisible by one day (in local timezone),
2106 // to make it work on FAT too.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002107 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002108 &access_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002109
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002110 Time modification_time;
jochen@chromium.orga6879772010-02-18 19:02:26 +09002111 // Note that this timestamp is divisible by two (seconds) - FAT stores
2112 // modification times with 2s resolution.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002113 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
jochen@chromium.orga6879772010-02-18 19:02:26 +09002114 &modification_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002115
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002116 ASSERT_TRUE(TouchFile(foobar, access_time, modification_time));
rvargas@chromium.orgb005b382014-01-07 19:06:58 +09002117 File::Info file_info;
brettw@chromium.orga9154032013-12-05 05:56:49 +09002118 ASSERT_TRUE(GetFileInfo(foobar, &file_info));
tnagel@chromium.org14b1c0a2014-05-05 22:50:43 +09002119 EXPECT_EQ(access_time.ToInternalValue(),
2120 file_info.last_accessed.ToInternalValue());
2121 EXPECT_EQ(modification_time.ToInternalValue(),
2122 file_info.last_modified.ToInternalValue());
jochen@chromium.orga6879772010-02-18 19:02:26 +09002123}
2124
tfarina@chromium.org34828222010-05-26 10:40:12 +09002125TEST_F(FileUtilTest, IsDirectoryEmpty) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002126 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002127
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002128 ASSERT_FALSE(PathExists(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002129
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002130 ASSERT_TRUE(CreateDirectory(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002131
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002132 EXPECT_TRUE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002133
2134 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
2135 std::string bar("baz");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002136 ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length()));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002137
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002138 EXPECT_FALSE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002139}
2140
skerner@google.com93449ef2011-09-22 23:47:18 +09002141#if defined(OS_POSIX)
2142
2143// Testing VerifyPathControlledByAdmin() is hard, because there is no
2144// way a test can make a file owned by root, or change file paths
2145// at the root of the file system. VerifyPathControlledByAdmin()
2146// is implemented as a call to VerifyPathControlledByUser, which gives
2147// us the ability to test with paths under the test's temp directory,
2148// using a user id we control.
2149// Pull tests of VerifyPathControlledByUserTest() into a separate test class
2150// with a common SetUp() method.
2151class VerifyPathControlledByUserTest : public FileUtilTest {
2152 protected:
mostynb1fdbe1e2014-10-08 02:59:11 +09002153 virtual void SetUp() override {
skerner@google.com93449ef2011-09-22 23:47:18 +09002154 FileUtilTest::SetUp();
2155
2156 // Create a basic structure used by each test.
2157 // base_dir_
2158 // |-> sub_dir_
2159 // |-> text_file_
2160
2161 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002162 ASSERT_TRUE(CreateDirectory(base_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002163
2164 sub_dir_ = base_dir_.AppendASCII("sub_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002165 ASSERT_TRUE(CreateDirectory(sub_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002166
2167 text_file_ = sub_dir_.AppendASCII("file.txt");
2168 CreateTextFile(text_file_, L"This text file has some text in it.");
2169
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002170 // Get the user and group files are created with from |base_dir_|.
2171 struct stat stat_buf;
2172 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
2173 uid_ = stat_buf.st_uid;
skerner@chromium.org80784142011-10-18 06:30:29 +09002174 ok_gids_.insert(stat_buf.st_gid);
2175 bad_gids_.insert(stat_buf.st_gid + 1);
2176
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002177 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
skerner@google.com93449ef2011-09-22 23:47:18 +09002178
2179 // To ensure that umask settings do not cause the initial state
2180 // of permissions to be different from what we expect, explicitly
2181 // set permissions on the directories we create.
2182 // Make all files and directories non-world-writable.
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002183
2184 // Users and group can read, write, traverse
2185 int enabled_permissions =
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002186 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002187 // Other users can't read, write, traverse
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002188 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
skerner@google.com93449ef2011-09-22 23:47:18 +09002189
2190 ASSERT_NO_FATAL_FAILURE(
2191 ChangePosixFilePermissions(
2192 base_dir_, enabled_permissions, disabled_permissions));
2193 ASSERT_NO_FATAL_FAILURE(
2194 ChangePosixFilePermissions(
2195 sub_dir_, enabled_permissions, disabled_permissions));
2196 }
2197
2198 FilePath base_dir_;
2199 FilePath sub_dir_;
2200 FilePath text_file_;
2201 uid_t uid_;
skerner@chromium.org80784142011-10-18 06:30:29 +09002202
2203 std::set<gid_t> ok_gids_;
2204 std::set<gid_t> bad_gids_;
skerner@google.com93449ef2011-09-22 23:47:18 +09002205};
2206
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002207TEST_F(VerifyPathControlledByUserTest, BadPaths) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002208 // File does not exist.
2209 FilePath does_not_exist = base_dir_.AppendASCII("does")
2210 .AppendASCII("not")
2211 .AppendASCII("exist");
skerner@google.com93449ef2011-09-22 23:47:18 +09002212 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002213 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002214 base_dir_, does_not_exist, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002215
2216 // |base| not a subpath of |path|.
2217 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002218 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002219 sub_dir_, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002220
2221 // An empty base path will fail to be a prefix for any path.
2222 FilePath empty;
2223 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002224 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002225 empty, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002226
2227 // Finding that a bad call fails proves nothing unless a good call succeeds.
2228 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002229 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002230 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002231}
2232
2233TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2234 // Symlinks in the path should cause failure.
2235
2236 // Symlink to the file at the end of the path.
2237 FilePath file_link = base_dir_.AppendASCII("file_link");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002238 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
skerner@google.com93449ef2011-09-22 23:47:18 +09002239 << "Failed to create symlink.";
2240
2241 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002242 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002243 base_dir_, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002244 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002245 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002246 file_link, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002247
2248 // Symlink from one directory to another within the path.
2249 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002250 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
skerner@google.com93449ef2011-09-22 23:47:18 +09002251 << "Failed to create symlink.";
2252
2253 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002254 ASSERT_TRUE(PathExists(file_path_with_link));
skerner@google.com93449ef2011-09-22 23:47:18 +09002255
2256 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002257 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002258 base_dir_, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002259
2260 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002261 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002262 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002263
2264 // Symlinks in parents of base path are allowed.
2265 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002266 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002267 file_path_with_link, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002268}
2269
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002270TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002271 // Get a uid that is not the uid of files we create.
2272 uid_t bad_uid = uid_ + 1;
2273
skerner@google.com93449ef2011-09-22 23:47:18 +09002274 // Make all files and directories non-world-writable.
2275 ASSERT_NO_FATAL_FAILURE(
2276 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2277 ASSERT_NO_FATAL_FAILURE(
2278 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2279 ASSERT_NO_FATAL_FAILURE(
2280 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2281
2282 // We control these paths.
2283 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002284 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002285 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002286 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002287 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002288 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002289 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002290 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002291 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002292
2293 // Another user does not control these paths.
2294 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002295 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002296 base_dir_, sub_dir_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002297 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002298 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002299 base_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002300 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002301 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002302 sub_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002303
2304 // Another group does not control the paths.
2305 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002306 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002307 base_dir_, sub_dir_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002308 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002309 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002310 base_dir_, text_file_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002311 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002312 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002313 sub_dir_, text_file_, uid_, bad_gids_));
2314}
2315
2316TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2317 // Make all files and directories writable only by their owner.
2318 ASSERT_NO_FATAL_FAILURE(
2319 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2320 ASSERT_NO_FATAL_FAILURE(
2321 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2322 ASSERT_NO_FATAL_FAILURE(
2323 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2324
2325 // Any group is okay because the path is not group-writable.
2326 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002327 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002328 base_dir_, sub_dir_, uid_, ok_gids_));
2329 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002330 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002331 base_dir_, text_file_, uid_, ok_gids_));
2332 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002333 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002334 sub_dir_, text_file_, uid_, ok_gids_));
2335
2336 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002337 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002338 base_dir_, sub_dir_, uid_, bad_gids_));
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_, text_file_, uid_, bad_gids_));
2342 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002343 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002344 sub_dir_, text_file_, uid_, bad_gids_));
2345
2346 // No group is okay, because we don't check the group
2347 // if no group can write.
2348 std::set<gid_t> no_gids; // Empty set of gids.
2349 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002350 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002351 base_dir_, sub_dir_, uid_, no_gids));
2352 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002353 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002354 base_dir_, text_file_, uid_, no_gids));
2355 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002356 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002357 sub_dir_, text_file_, uid_, no_gids));
2358
2359
2360 // Make all files and directories writable by their group.
2361 ASSERT_NO_FATAL_FAILURE(
2362 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2363 ASSERT_NO_FATAL_FAILURE(
2364 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2365 ASSERT_NO_FATAL_FAILURE(
2366 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2367
2368 // Now |ok_gids_| works, but |bad_gids_| fails.
2369 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002370 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002371 base_dir_, sub_dir_, uid_, ok_gids_));
2372 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002373 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002374 base_dir_, text_file_, uid_, ok_gids_));
2375 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002376 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002377 sub_dir_, text_file_, uid_, ok_gids_));
2378
2379 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002380 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002381 base_dir_, sub_dir_, uid_, bad_gids_));
2382 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002383 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002384 base_dir_, text_file_, uid_, bad_gids_));
2385 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002386 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002387 sub_dir_, text_file_, uid_, bad_gids_));
2388
2389 // Because any group in the group set is allowed,
2390 // the union of good and bad gids passes.
2391
2392 std::set<gid_t> multiple_gids;
2393 std::set_union(
2394 ok_gids_.begin(), ok_gids_.end(),
2395 bad_gids_.begin(), bad_gids_.end(),
2396 std::inserter(multiple_gids, multiple_gids.begin()));
2397
2398 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002399 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002400 base_dir_, sub_dir_, uid_, multiple_gids));
2401 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002402 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002403 base_dir_, text_file_, uid_, multiple_gids));
2404 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002405 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002406 sub_dir_, text_file_, uid_, multiple_gids));
skerner@google.com93449ef2011-09-22 23:47:18 +09002407}
2408
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002409TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002410 // Make all files and directories non-world-writable.
2411 ASSERT_NO_FATAL_FAILURE(
2412 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2413 ASSERT_NO_FATAL_FAILURE(
2414 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2415 ASSERT_NO_FATAL_FAILURE(
2416 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2417
2418 // Initialy, we control all parts of the path.
2419 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002420 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002421 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002422 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002423 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002424 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002425 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002426 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002427 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002428
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09002429 // Make base_dir_ world-writable.
skerner@google.com93449ef2011-09-22 23:47:18 +09002430 ASSERT_NO_FATAL_FAILURE(
2431 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2432 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002433 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002434 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002435 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002436 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002437 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002438 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002439 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002440 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002441
2442 // Make sub_dir_ world writable.
2443 ASSERT_NO_FATAL_FAILURE(
2444 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2445 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002446 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002447 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002448 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002449 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002450 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002451 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002452 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002453 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002454
2455 // Make text_file_ world writable.
2456 ASSERT_NO_FATAL_FAILURE(
2457 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2458 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002459 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002460 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002461 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002462 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002463 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002464 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002465 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002466 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002467
2468 // Make sub_dir_ non-world writable.
2469 ASSERT_NO_FATAL_FAILURE(
2470 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2471 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002472 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002473 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002474 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002475 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002476 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002477 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002478 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002479 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002480
2481 // Make base_dir_ non-world-writable.
2482 ASSERT_NO_FATAL_FAILURE(
2483 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2484 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002485 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002486 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002487 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002488 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002489 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002490 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002491 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002492 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002493
2494 // Back to the initial state: Nothing is writable, so every path
2495 // should pass.
2496 ASSERT_NO_FATAL_FAILURE(
2497 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2498 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002499 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002500 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002501 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002502 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002503 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002504 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002505 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002506 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002507}
2508
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002509#if defined(OS_ANDROID)
2510TEST_F(FileUtilTest, ValidContentUriTest) {
2511 // Get the test image path.
2512 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002513 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002514 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002515 ASSERT_TRUE(PathExists(data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002516 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2517 int64 image_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002518 GetFileSize(image_file, &image_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002519 EXPECT_LT(0, image_size);
2520
2521 // Insert the image into MediaStore. MediaStore will do some conversions, and
2522 // return the content URI.
brettw@chromium.orgbdedd6c2014-08-08 07:57:11 +09002523 FilePath path = base::InsertImageIntoMediaStore(image_file);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002524 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002525 EXPECT_TRUE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002526 // The file size may not equal to the input image as MediaStore may convert
2527 // the image.
2528 int64 content_uri_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002529 GetFileSize(path, &content_uri_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002530 EXPECT_EQ(image_size, content_uri_size);
2531
2532 // We should be able to read the file.
2533 char* buffer = new char[image_size];
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002534 File file = OpenContentUriForRead(path);
2535 EXPECT_TRUE(file.IsValid());
2536 EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002537 delete[] buffer;
2538}
2539
2540TEST_F(FileUtilTest, NonExistentContentUriTest) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002541 FilePath path("content://foo.bar");
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002542 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002543 EXPECT_FALSE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002544 // Size should be smaller than 0.
2545 int64 size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002546 EXPECT_FALSE(GetFileSize(path, &size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002547
2548 // We should not be able to read the file.
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002549 File file = OpenContentUriForRead(path);
2550 EXPECT_FALSE(file.IsValid());
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002551}
2552#endif
2553
jln@chromium.orgd7493932014-03-01 00:35:36 +09002554TEST(ScopedFD, ScopedFDDoesClose) {
2555 int fds[2];
2556 char c = 0;
2557 ASSERT_EQ(0, pipe(fds));
2558 const int write_end = fds[1];
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002559 base::ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002560 {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002561 base::ScopedFD write_end_closer(fds[1]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002562 }
2563 // This is the only thread. This file descriptor should no longer be valid.
2564 int ret = close(write_end);
2565 EXPECT_EQ(-1, ret);
2566 EXPECT_EQ(EBADF, errno);
2567 // Make sure read(2) won't block.
2568 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK));
2569 // Reading the pipe should EOF.
2570 EXPECT_EQ(0, read(fds[0], &c, 1));
2571}
2572
2573#if defined(GTEST_HAS_DEATH_TEST)
2574void CloseWithScopedFD(int fd) {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002575 base::ScopedFD fd_closer(fd);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002576}
2577#endif
2578
2579TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) {
2580 int fds[2];
2581 ASSERT_EQ(0, pipe(fds));
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002582 base::ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002583 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1])));
2584#if defined(GTEST_HAS_DEATH_TEST)
2585 // This is the only thread. This file descriptor should no longer be valid.
2586 // Trying to close it should crash. This is important for security.
2587 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2588#endif
2589}
2590
skerner@google.com93449ef2011-09-22 23:47:18 +09002591#endif // defined(OS_POSIX)
2592
mark@chromium.org17684802008-09-10 09:16:28 +09002593} // namespace
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002594
2595} // namespace base