blob: 9acdf3e3b969a4f73700104e5cbb758af7b345f1 [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"
27#include "base/file_util.h"
brettw@chromium.org56946722013-06-08 13:53:36 +090028#include "base/files/file_enumerator.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090029#include "base/files/file_path.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
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090055// To test that file_util::Normalize FilePath() deals with NTFS reparse points
56// correctly, we need functions to create and delete reparse points.
57#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));
142 created_ = dir_.IsValid() && SetReparsePoint(dir_, target);
143 }
144
145 ~ReparsePoint() {
146 if (created_)
147 DeleteReparsePoint(dir_);
148 }
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:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +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));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900531 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
532 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
533
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
635TEST_F(FileUtilTest, DeleteFile) {
636 // Create a file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900637 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900638 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900639 ASSERT_TRUE(PathExists(file_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900640
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900641 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900642 EXPECT_TRUE(DeleteFile(file_name, false));
643 EXPECT_FALSE(PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900644
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900645 // Test recursive case, create a new file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900646 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900647 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900648 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900649
650 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900651 EXPECT_TRUE(DeleteFile(file_name, true));
652 EXPECT_FALSE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900653}
654
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900655#if defined(OS_POSIX)
656TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900657 // Create a file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900658 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
659 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900660 ASSERT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900661
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900662 // Create a symlink to the file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900663 FilePath file_link = temp_dir_.path().Append("file_link_2");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900664 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900665 << "Failed to create symlink.";
666
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900667 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900668 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900669
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900670 // Make sure original file is not deleted.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900671 EXPECT_FALSE(PathExists(file_link));
672 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900673}
674
675TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900676 // Create a non-existent file path.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900677 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900678 EXPECT_FALSE(PathExists(non_existent));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900679
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900680 // Create a symlink to the non-existent file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900681 FilePath file_link = temp_dir_.path().Append("file_link_3");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900682 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900683 << "Failed to create symlink.";
684
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900685 // Make sure the symbolic link is exist.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900686 EXPECT_TRUE(IsLink(file_link));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900687 EXPECT_FALSE(PathExists(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900688
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900689 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900690 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900691
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900692 // Make sure the symbolic link is deleted.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900693 EXPECT_FALSE(IsLink(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900694}
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900695
696TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
697 // Create a file path.
698 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900699 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900700
701 const std::string kData("hello");
702
703 int buffer_size = kData.length();
704 char* buffer = new char[buffer_size];
705
706 // Write file.
707 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900708 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900709 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900710
711 // Make sure the file is readable.
712 int32 mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900713 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
714 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900715
716 // Get rid of the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900717 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
718 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
719 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900720 // Make sure the file can't be read.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900721 EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900722
723 // Give the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900724 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
725 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
726 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900727 // Make sure the file can be read.
728 EXPECT_EQ(static_cast<int>(kData.length()),
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900729 ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900730
731 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900732 EXPECT_TRUE(DeleteFile(file_name, false));
733 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900734
735 delete[] buffer;
736}
737
738TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
739 // Create a file path.
740 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900741 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900742
743 const std::string kData("hello");
744
745 // Write file.
746 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900747 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900748 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900749
750 // Make sure the file is writable.
751 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900752 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
753 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900754 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900755
756 // Get rid of the write permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900757 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
758 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
759 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900760 // Make sure the file can't be write.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900761 EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900762 EXPECT_FALSE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900763
764 // Give read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900765 EXPECT_TRUE(SetPosixFilePermissions(file_name,
766 FILE_PERMISSION_WRITE_BY_USER));
767 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
768 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900769 // Make sure the file can be write.
770 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900771 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900772 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900773
774 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900775 EXPECT_TRUE(DeleteFile(file_name, false));
776 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900777}
778
779TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
780 // Create a directory path.
781 FilePath subdir_path =
782 temp_dir_.path().Append(FPL("PermissionTest1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900783 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900784 ASSERT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900785
786 // Create a dummy file to enumerate.
787 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900788 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900789 const std::string kData("hello");
790 EXPECT_EQ(static_cast<int>(kData.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +0900791 WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900792 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900793
794 // Make sure the directory has the all permissions.
795 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900796 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
797 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900798
799 // Get rid of the permissions from the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900800 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
801 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
802 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900803
804 // Make sure the file in the directory can't be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900805 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900806 EXPECT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900807 FindResultCollector c1(f1);
808 EXPECT_EQ(c1.size(), 0);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900809 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900810
811 // Give the permissions to the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900812 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
813 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
814 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900815
816 // Make sure the file in the directory can be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900817 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900818 FindResultCollector c2(f2);
819 EXPECT_TRUE(c2.HasFile(file_name));
820 EXPECT_EQ(c2.size(), 1);
821
822 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900823 EXPECT_TRUE(DeleteFile(subdir_path, true));
824 EXPECT_FALSE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900825}
826
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900827#endif // defined(OS_POSIX)
828
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900829#if defined(OS_WIN)
830// Tests that the Delete function works for wild cards, especially
831// with the recursion flag. Also coincidentally tests PathExists.
832// TODO(erikkay): see if anyone's actually using this feature of the API
833TEST_F(FileUtilTest, DeleteWildCard) {
834 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900835 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900836 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900837 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900838
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900839 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900840 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900841 ASSERT_TRUE(PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900842
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900843 // Create the wildcard path
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900844 FilePath directory_contents = temp_dir_.path();
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900845 directory_contents = directory_contents.Append(FPL("*"));
846
initial.commit3f4a7322008-07-27 06:49:38 +0900847 // Delete non-recursively and check that only the file is deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900848 EXPECT_TRUE(DeleteFile(directory_contents, false));
849 EXPECT_FALSE(PathExists(file_name));
850 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900851
zork@chromium.org61be4f42010-05-07 09:05:36 +0900852 // Delete recursively and make sure all contents are deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900853 EXPECT_TRUE(DeleteFile(directory_contents, true));
854 EXPECT_FALSE(PathExists(file_name));
855 EXPECT_FALSE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900856}
857
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900858// TODO(erikkay): see if anyone's actually using this feature of the API
859TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
860 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900861 FilePath subdir_path =
862 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900863 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900864 ASSERT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900865
866 // Create the wildcard path
867 FilePath directory_contents = subdir_path;
868 directory_contents = directory_contents.Append(FPL("*"));
869
870 // Delete non-recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900871 EXPECT_TRUE(DeleteFile(directory_contents, false));
872 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900873
874 // Delete recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900875 EXPECT_TRUE(DeleteFile(directory_contents, true));
876 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900877}
878#endif
879
880// Tests non-recursive Delete() for a directory.
881TEST_F(FileUtilTest, DeleteDirNonRecursive) {
882 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900883 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900884 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900885 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900886
887 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
888 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900889 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900890
891 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900892 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900893 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900894
895 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900896 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900897 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900898
899 // Delete non-recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900900 EXPECT_TRUE(DeleteFile(subdir_path2, false));
901 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900902
903 // Delete non-recursively and check that nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900904 EXPECT_FALSE(DeleteFile(test_subdir, false));
905 EXPECT_TRUE(PathExists(test_subdir));
906 EXPECT_TRUE(PathExists(file_name));
907 EXPECT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900908}
909
910// Tests recursive Delete() for a directory.
911TEST_F(FileUtilTest, DeleteDirRecursive) {
912 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900913 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900914 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900915 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900916
917 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
918 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900919 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900920
921 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900922 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900923 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900924
925 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900926 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900927 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900928
929 // Delete recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900930 EXPECT_TRUE(DeleteFile(subdir_path2, true));
931 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900932
933 // Delete recursively and check that everything got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900934 EXPECT_TRUE(DeleteFile(test_subdir, true));
935 EXPECT_FALSE(PathExists(file_name));
936 EXPECT_FALSE(PathExists(subdir_path1));
937 EXPECT_FALSE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900938}
939
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900940TEST_F(FileUtilTest, MoveFileNew) {
941 // Create a file
942 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900943 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900944 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900945 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900946
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900947 // The destination.
948 FilePath file_name_to = temp_dir_.path().Append(
949 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900950 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900951
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900952 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900953
954 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900955 EXPECT_FALSE(PathExists(file_name_from));
956 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900957}
958
959TEST_F(FileUtilTest, MoveFileExists) {
960 // Create a file
961 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900962 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900963 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900964 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900965
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900966 // The destination name.
967 FilePath file_name_to = temp_dir_.path().Append(
968 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900969 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900970 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900971
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900972 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900973
974 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900975 EXPECT_FALSE(PathExists(file_name_from));
976 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900977 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
978}
979
980TEST_F(FileUtilTest, MoveFileDirExists) {
981 // Create a file
982 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900983 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900984 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900985 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900986
987 // The destination directory
988 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900989 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900990 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900991 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900992
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900993 EXPECT_FALSE(Move(file_name_from, dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900994}
995
996
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900997TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900998 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900999 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001000 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001001 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001002 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001003
1004 // Create a file under the directory
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001005 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1006 FilePath file_name_from = dir_name_from.Append(txt_file_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001007 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001008 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001009
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001010 // Move the directory.
1011 FilePath dir_name_to =
1012 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001013 FilePath file_name_to =
1014 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001015
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001016 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001017
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001018 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001019
1020 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001021 EXPECT_FALSE(PathExists(dir_name_from));
1022 EXPECT_FALSE(PathExists(file_name_from));
1023 EXPECT_TRUE(PathExists(dir_name_to));
1024 EXPECT_TRUE(PathExists(file_name_to));
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001025
1026 // Test path traversal.
1027 file_name_from = dir_name_to.Append(txt_file_name);
1028 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1029 file_name_to = file_name_to.Append(txt_file_name);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001030 EXPECT_FALSE(Move(file_name_from, file_name_to));
1031 EXPECT_TRUE(PathExists(file_name_from));
1032 EXPECT_FALSE(PathExists(file_name_to));
1033 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1034 EXPECT_FALSE(PathExists(file_name_from));
1035 EXPECT_TRUE(PathExists(file_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001036}
1037
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001038TEST_F(FileUtilTest, MoveExist) {
1039 // Create a directory
1040 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001041 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001042 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001043 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001044
1045 // Create a file under the directory
1046 FilePath file_name_from =
1047 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1048 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001049 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001050
1051 // Move the directory
1052 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001053 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001054
1055 FilePath dir_name_to =
1056 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1057 FilePath file_name_to =
1058 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1059
1060 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001061 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001062 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001063
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001064 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001065
1066 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001067 EXPECT_FALSE(PathExists(dir_name_from));
1068 EXPECT_FALSE(PathExists(file_name_from));
1069 EXPECT_TRUE(PathExists(dir_name_to));
1070 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001071}
1072
1073TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001074 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001075 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001076 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001077 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001078 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001079
1080 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001081 FilePath file_name_from =
1082 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001083 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001084 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001085
1086 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001087 FilePath subdir_name_from =
1088 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001089 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001090 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001091
1092 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001093 FilePath file_name2_from =
1094 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001095 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001096 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001097
1098 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001099 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001100 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001101 FilePath file_name_to =
1102 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1103 FilePath subdir_name_to =
1104 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1105 FilePath file_name2_to =
1106 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001107
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001108 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001109
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001110 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001111
1112 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001113 EXPECT_TRUE(PathExists(dir_name_from));
1114 EXPECT_TRUE(PathExists(file_name_from));
1115 EXPECT_TRUE(PathExists(subdir_name_from));
1116 EXPECT_TRUE(PathExists(file_name2_from));
1117 EXPECT_TRUE(PathExists(dir_name_to));
1118 EXPECT_TRUE(PathExists(file_name_to));
1119 EXPECT_TRUE(PathExists(subdir_name_to));
1120 EXPECT_TRUE(PathExists(file_name2_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001121}
1122
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001123TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1124 // Create a directory.
1125 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001126 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001127 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001128 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001129
1130 // Create a file under the directory.
1131 FilePath file_name_from =
1132 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1133 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001134 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001135
1136 // Create a subdirectory.
1137 FilePath subdir_name_from =
1138 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001139 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001140 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001141
1142 // Create a file under the subdirectory.
1143 FilePath file_name2_from =
1144 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1145 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001146 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001147
1148 // Copy the directory recursively.
1149 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001150 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001151
1152 FilePath dir_name_to =
1153 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1154 FilePath file_name_to =
1155 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1156 FilePath subdir_name_to =
1157 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1158 FilePath file_name2_to =
1159 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1160
1161 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001162 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001163 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001164
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001165 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001166
1167 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001168 EXPECT_TRUE(PathExists(dir_name_from));
1169 EXPECT_TRUE(PathExists(file_name_from));
1170 EXPECT_TRUE(PathExists(subdir_name_from));
1171 EXPECT_TRUE(PathExists(file_name2_from));
1172 EXPECT_TRUE(PathExists(dir_name_to));
1173 EXPECT_TRUE(PathExists(file_name_to));
1174 EXPECT_TRUE(PathExists(subdir_name_to));
1175 EXPECT_TRUE(PathExists(file_name2_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001176}
1177
1178TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001179 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001180 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001181 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001182 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001183 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001184
1185 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001186 FilePath file_name_from =
1187 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001188 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001189 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001190
1191 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001192 FilePath subdir_name_from =
1193 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001194 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001195 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001196
1197 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001198 FilePath file_name2_from =
1199 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001200 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001201 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001202
1203 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001204 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001205 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001206 FilePath file_name_to =
1207 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1208 FilePath subdir_name_to =
1209 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001210
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001211 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001212
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001213 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001214
1215 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001216 EXPECT_TRUE(PathExists(dir_name_from));
1217 EXPECT_TRUE(PathExists(file_name_from));
1218 EXPECT_TRUE(PathExists(subdir_name_from));
1219 EXPECT_TRUE(PathExists(file_name2_from));
1220 EXPECT_TRUE(PathExists(dir_name_to));
1221 EXPECT_TRUE(PathExists(file_name_to));
1222 EXPECT_FALSE(PathExists(subdir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001223}
1224
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001225TEST_F(FileUtilTest, CopyDirectoryExists) {
1226 // Create a directory.
1227 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001228 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001229 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001230 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001231
1232 // Create a file under the directory.
1233 FilePath file_name_from =
1234 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1235 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001236 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001237
1238 // Create a subdirectory.
1239 FilePath subdir_name_from =
1240 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001241 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001242 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001243
1244 // Create a file under the subdirectory.
1245 FilePath file_name2_from =
1246 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1247 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001248 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001249
1250 // Copy the directory not recursively.
1251 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001252 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001253 FilePath file_name_to =
1254 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1255 FilePath subdir_name_to =
1256 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1257
1258 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001259 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001260 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001261
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001262 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001263
1264 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001265 EXPECT_TRUE(PathExists(dir_name_from));
1266 EXPECT_TRUE(PathExists(file_name_from));
1267 EXPECT_TRUE(PathExists(subdir_name_from));
1268 EXPECT_TRUE(PathExists(file_name2_from));
1269 EXPECT_TRUE(PathExists(dir_name_to));
1270 EXPECT_TRUE(PathExists(file_name_to));
1271 EXPECT_FALSE(PathExists(subdir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001272}
1273
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001274TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1275 // Create a file
1276 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001277 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001278 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001279 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001280
1281 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001282 FilePath file_name_to = temp_dir_.path().Append(
1283 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001284 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001285
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001286 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001287
1288 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001289 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001290}
1291
1292TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1293 // Create a file
1294 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001295 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001296 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001297 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001298
1299 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001300 FilePath file_name_to = temp_dir_.path().Append(
1301 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001302 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001303 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001304
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001305 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001306
1307 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001308 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001309 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1310}
1311
1312TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1313 // Create a file
1314 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001315 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001316 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001317 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001318
1319 // The destination
1320 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001321 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001322 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001323 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001324 FilePath file_name_to =
1325 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1326
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001327 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001328
1329 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001330 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001331}
1332
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001333TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1334 // Create a directory.
1335 FilePath dir_name_from =
1336 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001337 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001338 ASSERT_TRUE(PathExists(dir_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001339
1340 // Create a file under the directory.
1341 FilePath file_name_from =
1342 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1343 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001344 ASSERT_TRUE(PathExists(file_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001345
1346 // Copy the directory recursively.
1347 FilePath dir_name_to =
1348 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1349 FilePath file_name_to =
1350 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1351
1352 // Create from path with trailing separators.
1353#if defined(OS_WIN)
1354 FilePath from_path =
1355 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1356#elif defined (OS_POSIX)
1357 FilePath from_path =
1358 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1359#endif
1360
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001361 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001362
1363 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001364 EXPECT_TRUE(PathExists(dir_name_from));
1365 EXPECT_TRUE(PathExists(file_name_from));
1366 EXPECT_TRUE(PathExists(dir_name_to));
1367 EXPECT_TRUE(PathExists(file_name_to));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001368}
1369
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001370// Sets the source file to read-only.
1371void SetReadOnly(const FilePath& path) {
1372#if defined(OS_WIN)
1373 // On Windows, it involves setting a bit.
1374 DWORD attrs = GetFileAttributes(path.value().c_str());
1375 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1376 ASSERT_TRUE(SetFileAttributes(
1377 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY));
1378 attrs = GetFileAttributes(path.value().c_str());
1379 // Files in the temporary directory should not be indexed ever. If this
1380 // assumption change, fix this unit test accordingly.
1381 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1382 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY;
1383 if (win::GetVersion() >= win::VERSION_VISTA)
1384 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1385 ASSERT_EQ(expected, attrs);
1386#else
1387 // On all other platforms, it involves removing the write bit.
1388 EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR));
1389#endif
1390}
1391
1392bool IsReadOnly(const FilePath& path) {
1393#if defined(OS_WIN)
1394 DWORD attrs = GetFileAttributes(path.value().c_str());
1395 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1396 return attrs & FILE_ATTRIBUTE_READONLY;
1397#else
1398 int mode = 0;
1399 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1400 return !(mode & S_IWUSR);
1401#endif
1402}
1403
1404TEST_F(FileUtilTest, CopyDirectoryACL) {
1405 // Create a directory.
1406 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
1407 CreateDirectory(src);
1408 ASSERT_TRUE(PathExists(src));
1409
1410 // Create a file under the directory.
1411 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1412 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
1413 SetReadOnly(src_file);
1414 ASSERT_TRUE(IsReadOnly(src_file));
1415
1416 // Copy the directory recursively.
1417 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1418 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1419 EXPECT_TRUE(CopyDirectory(src, dst, true));
1420
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001421 ASSERT_FALSE(IsReadOnly(dst_file));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001422}
1423
initial.commit3f4a7322008-07-27 06:49:38 +09001424TEST_F(FileUtilTest, CopyFile) {
1425 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001426 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001427 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001428 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001429 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001430
1431 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001432 FilePath file_name_from =
1433 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001434 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1435 CreateTextFile(file_name_from, file_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001436 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001437
1438 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001439 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001440 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001441
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001442 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001443 FilePath dest_file2(dir_name_from);
1444 dest_file2 = dest_file2.AppendASCII("..");
1445 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001446 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
1447 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from, dest_file2));
evan@chromium.org1543ad32009-08-27 05:00:14 +09001448
1449 FilePath dest_file2_test(dir_name_from);
1450 dest_file2_test = dest_file2_test.DirName();
1451 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001452
1453 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001454 EXPECT_TRUE(PathExists(file_name_from));
1455 EXPECT_TRUE(PathExists(dest_file));
initial.commit3f4a7322008-07-27 06:49:38 +09001456 const std::wstring read_contents = ReadTextFile(dest_file);
1457 EXPECT_EQ(file_contents, read_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001458 EXPECT_TRUE(PathExists(dest_file2_test));
1459 EXPECT_TRUE(PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001460}
1461
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001462TEST_F(FileUtilTest, CopyFileACL) {
1463 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1464 // this test case asserts the access control bits are meeting expectations in
1465 // CopyFileUnsafe().
1466 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt"));
1467 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1468 CreateTextFile(src, file_contents);
1469
1470 // Set the source file to read-only.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001471 ASSERT_FALSE(IsReadOnly(src));
1472 SetReadOnly(src);
1473 ASSERT_TRUE(IsReadOnly(src));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001474
1475 // Copy the file.
1476 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt"));
1477 ASSERT_TRUE(CopyFile(src, dst));
1478 EXPECT_EQ(file_contents, ReadTextFile(dst));
1479
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001480 ASSERT_FALSE(IsReadOnly(dst));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001481}
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001482
erikkay@google.comf2406842008-08-21 00:59:49 +09001483// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001484// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001485typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001486
erikkay@google.comf2406842008-08-21 00:59:49 +09001487TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001488 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001489 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001490 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001491 ASSERT_TRUE(PathExists(data_dir));
initial.commit3f4a7322008-07-27 06:49:38 +09001492
evanm@google.com874d1672008-10-31 08:54:04 +09001493 FilePath original_file =
1494 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1495 FilePath same_file =
1496 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1497 FilePath same_length_file =
1498 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1499 FilePath different_file =
1500 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1501 FilePath different_first_file =
1502 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1503 FilePath different_last_file =
1504 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1505 FilePath empty1_file =
1506 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1507 FilePath empty2_file =
1508 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1509 FilePath shortened_file =
1510 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1511 FilePath binary_file =
1512 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1513 FilePath binary_file_same =
1514 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1515 FilePath binary_file_diff =
1516 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001517
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001518 EXPECT_TRUE(ContentsEqual(original_file, original_file));
1519 EXPECT_TRUE(ContentsEqual(original_file, same_file));
1520 EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
1521 EXPECT_FALSE(ContentsEqual(original_file, different_file));
1522 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1523 FilePath(FILE_PATH_LITERAL("bogusname"))));
1524 EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
1525 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1526 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1527 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1528 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1529 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1530 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
initial.commit3f4a7322008-07-27 06:49:38 +09001531}
1532
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001533TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1534 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001535 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001536 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001537 ASSERT_TRUE(PathExists(data_dir));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001538
1539 FilePath original_file =
1540 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1541 FilePath same_file =
1542 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1543 FilePath crlf_file =
1544 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1545 FilePath shortened_file =
1546 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1547 FilePath different_file =
1548 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1549 FilePath different_first_file =
1550 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1551 FilePath different_last_file =
1552 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1553 FilePath first1_file =
1554 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1555 FilePath first2_file =
1556 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1557 FilePath empty1_file =
1558 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1559 FilePath empty2_file =
1560 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1561 FilePath blank_line_file =
1562 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1563 FilePath blank_line_crlf_file =
1564 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1565
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001566 EXPECT_TRUE(TextContentsEqual(original_file, same_file));
1567 EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
1568 EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
1569 EXPECT_FALSE(TextContentsEqual(original_file, different_file));
1570 EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
1571 EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
1572 EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
1573 EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
1574 EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
1575 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001576}
1577
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001578// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001579#if defined(OS_WIN)
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001580TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1581 // Create a directory
1582 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001583 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001584 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001585 ASSERT_TRUE(PathExists(dir_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001586
1587 // Create a file under the directory
1588 FilePath file_name_from =
1589 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1590 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001591 ASSERT_TRUE(PathExists(file_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001592
1593 // Move the directory by using CopyAndDeleteDirectory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001594 FilePath dir_name_to = temp_dir_.path().Append(
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001595 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1596 FilePath file_name_to =
1597 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1598
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001599 ASSERT_FALSE(PathExists(dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001600
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001601 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +09001602 dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001603
1604 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001605 EXPECT_FALSE(PathExists(dir_name_from));
1606 EXPECT_FALSE(PathExists(file_name_from));
1607 EXPECT_TRUE(PathExists(dir_name_to));
1608 EXPECT_TRUE(PathExists(file_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001609}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001610
1611TEST_F(FileUtilTest, GetTempDirTest) {
1612 static const TCHAR* kTmpKey = _T("TMP");
1613 static const TCHAR* kTmpValues[] = {
1614 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1615 };
1616 // Save the original $TMP.
1617 size_t original_tmp_size;
1618 TCHAR* original_tmp;
1619 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1620 // original_tmp may be NULL.
1621
1622 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1623 FilePath path;
1624 ::_tputenv_s(kTmpKey, kTmpValues[i]);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001625 GetTempDir(&path);
tkent@chromium.org8da14162009-10-09 16:33:39 +09001626 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1627 " result=" << path.value();
1628 }
1629
1630 // Restore the original $TMP.
1631 if (original_tmp) {
1632 ::_tputenv_s(kTmpKey, original_tmp);
1633 free(original_tmp);
1634 } else {
1635 ::_tputenv_s(kTmpKey, _T(""));
1636 }
1637}
1638#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001639
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001640TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1641 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001642 for (int i = 0; i < 3; i++) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001643 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001644 EXPECT_TRUE(PathExists(temp_files[i]));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001645 EXPECT_FALSE(DirectoryExists(temp_files[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001646 }
1647 for (int i = 0; i < 3; i++)
1648 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1649 for (int i = 0; i < 3; i++)
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001650 EXPECT_TRUE(DeleteFile(temp_files[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001651}
1652
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001653TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001654 FilePath names[3];
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001655 FILE* fps[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001656 int i;
1657
1658 // Create; make sure they are open and exist.
1659 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001660 fps[i] = CreateAndOpenTemporaryFile(&(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001661 ASSERT_TRUE(fps[i]);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001662 EXPECT_TRUE(PathExists(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001663 }
1664
1665 // Make sure all names are unique.
1666 for (i = 0; i < 3; ++i) {
1667 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1668 }
1669
1670 // Close and delete.
1671 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001672 EXPECT_TRUE(CloseFile(fps[i]));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001673 EXPECT_TRUE(DeleteFile(names[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001674 }
initial.commit3f4a7322008-07-27 06:49:38 +09001675}
1676
1677TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001678 FilePath temp_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001679 ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001680 EXPECT_TRUE(PathExists(temp_dir));
1681 EXPECT_TRUE(DeleteFile(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001682}
1683
skerner@chromium.orge4432392010-05-01 02:00:09 +09001684TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1685 FilePath new_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001686 ASSERT_TRUE(CreateTemporaryDirInDir(
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001687 temp_dir_.path(),
skerner@chromium.orge4432392010-05-01 02:00:09 +09001688 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001689 &new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001690 EXPECT_TRUE(PathExists(new_dir));
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001691 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001692 EXPECT_TRUE(DeleteFile(new_dir, false));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001693}
1694
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001695#if defined(OS_POSIX)
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001696TEST_F(FileUtilTest, GetShmemTempDirTest) {
1697 FilePath dir;
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001698 EXPECT_TRUE(GetShmemTempDir(false, &dir));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001699 EXPECT_TRUE(DirectoryExists(dir));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001700}
viettrungluu@chromium.orgc9a4b212014-03-20 16:06:40 +09001701#endif
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001702
brettw@chromium.org49de1af2014-02-20 05:34:23 +09001703TEST_F(FileUtilTest, GetHomeDirTest) {
1704#if !defined(OS_ANDROID) // Not implemented on Android.
1705 // We don't actually know what the home directory is supposed to be without
1706 // calling some OS functions which would just duplicate the implementation.
1707 // So here we just test that it returns something "reasonable".
1708 FilePath home = GetHomeDir();
1709 ASSERT_FALSE(home.empty());
1710 ASSERT_TRUE(home.IsAbsolute());
1711#endif
1712}
1713
initial.commit3f4a7322008-07-27 06:49:38 +09001714TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001715 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001716 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001717#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001718 FilePath test_path =
1719 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001720#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001721 FilePath test_path =
1722 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001723#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001724
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001725 EXPECT_FALSE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001726 EXPECT_TRUE(CreateDirectory(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001727 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001728 // CreateDirectory returns true if the DirectoryExists returns true.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001729 EXPECT_TRUE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001730
1731 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001732 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001733 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001734 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001735 EXPECT_TRUE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001736 EXPECT_FALSE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001737
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001738 EXPECT_TRUE(DeleteFile(test_root, true));
1739 EXPECT_FALSE(PathExists(test_root));
1740 EXPECT_FALSE(PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001741
1742 // Verify assumptions made by the Windows implementation:
1743 // 1. The current directory always exists.
1744 // 2. The root directory always exists.
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001745 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001746 FilePath top_level = test_root;
1747 while (top_level != top_level.DirName()) {
1748 top_level = top_level.DirName();
1749 }
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001750 ASSERT_TRUE(DirectoryExists(top_level));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001751
1752 // Given these assumptions hold, it should be safe to
1753 // test that "creating" these directories succeeds.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001754 EXPECT_TRUE(CreateDirectory(
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001755 FilePath(FilePath::kCurrentDirectory)));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001756 EXPECT_TRUE(CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001757
1758#if defined(OS_WIN)
1759 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1760 FilePath invalid_path =
1761 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001762 if (!PathExists(invalid_drive)) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001763 EXPECT_FALSE(CreateDirectory(invalid_path));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001764 }
1765#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001766}
1767
1768TEST_F(FileUtilTest, DetectDirectoryTest) {
1769 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001770 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001771 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001772 EXPECT_FALSE(PathExists(test_root));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001773 EXPECT_TRUE(CreateDirectory(test_root));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001774 EXPECT_TRUE(PathExists(test_root));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001775 EXPECT_TRUE(DirectoryExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001776 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001777 FilePath test_path =
1778 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001779 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001780 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001781 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001782 EXPECT_FALSE(DirectoryExists(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001783 EXPECT_TRUE(DeleteFile(test_path, false));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001784
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001785 EXPECT_TRUE(DeleteFile(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001786}
1787
initial.commit3f4a7322008-07-27 06:49:38 +09001788TEST_F(FileUtilTest, FileEnumeratorTest) {
1789 // Test an empty directory.
brettw@chromium.org56946722013-06-08 13:53:36 +09001790 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001791 EXPECT_EQ(f0.Next().value(), FPL(""));
1792 EXPECT_EQ(f0.Next().value(), FPL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001793
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001794 // Test an empty directory, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001795 FileEnumerator f0_dotdot(temp_dir_.path(), false,
1796 FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001797 EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001798 f0_dotdot.Next().value());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001799 EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001800
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001801 // create the directories
rvargas@chromium.org56472942013-08-15 05:46:05 +09001802 FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001803 EXPECT_TRUE(CreateDirectory(dir1));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001804 FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001805 EXPECT_TRUE(CreateDirectory(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001806 FilePath dir2inner = dir2.Append(FPL("inner"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001807 EXPECT_TRUE(CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001808
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001809 // create the files
rvargas@chromium.org56472942013-08-15 05:46:05 +09001810 FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001811 CreateTextFile(dir2file, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001812 FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001813 CreateTextFile(dir2innerfile, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001814 FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001815 CreateTextFile(file1, std::wstring());
1816 FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
rvargas@chromium.org56472942013-08-15 05:46:05 +09001817 .Append(FPL("file2.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001818 CreateTextFile(file2_rel, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001819 FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001820
1821 // Only enumerate files.
brettw@chromium.org56946722013-06-08 13:53:36 +09001822 FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
initial.commit3f4a7322008-07-27 06:49:38 +09001823 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001824 EXPECT_TRUE(c1.HasFile(file1));
1825 EXPECT_TRUE(c1.HasFile(file2_abs));
1826 EXPECT_TRUE(c1.HasFile(dir2file));
1827 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1828 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001829
1830 // Only enumerate directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001831 FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001832 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001833 EXPECT_TRUE(c2.HasFile(dir1));
1834 EXPECT_TRUE(c2.HasFile(dir2));
1835 EXPECT_TRUE(c2.HasFile(dir2inner));
1836 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001837
tim@chromium.org989d0972008-10-16 11:42:45 +09001838 // Only enumerate directories non-recursively.
brettw@chromium.org56946722013-06-08 13:53:36 +09001839 FileEnumerator f2_non_recursive(
1840 temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001841 FindResultCollector c2_non_recursive(f2_non_recursive);
1842 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1843 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1844 EXPECT_EQ(c2_non_recursive.size(), 2);
1845
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001846 // Only enumerate directories, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001847 FileEnumerator f2_dotdot(temp_dir_.path(), false,
1848 FileEnumerator::DIRECTORIES |
1849 FileEnumerator::INCLUDE_DOT_DOT);
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001850 FindResultCollector c2_dotdot(f2_dotdot);
1851 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1852 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001853 EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001854 EXPECT_EQ(c2_dotdot.size(), 3);
1855
initial.commit3f4a7322008-07-27 06:49:38 +09001856 // Enumerate files and directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001857 FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001858 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001859 EXPECT_TRUE(c3.HasFile(dir1));
1860 EXPECT_TRUE(c3.HasFile(dir2));
1861 EXPECT_TRUE(c3.HasFile(file1));
1862 EXPECT_TRUE(c3.HasFile(file2_abs));
1863 EXPECT_TRUE(c3.HasFile(dir2file));
1864 EXPECT_TRUE(c3.HasFile(dir2inner));
1865 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1866 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001867
1868 // Non-recursive operation.
brettw@chromium.org56946722013-06-08 13:53:36 +09001869 FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001870 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001871 EXPECT_TRUE(c4.HasFile(dir2));
1872 EXPECT_TRUE(c4.HasFile(dir2));
1873 EXPECT_TRUE(c4.HasFile(file1));
1874 EXPECT_TRUE(c4.HasFile(file2_abs));
1875 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001876
1877 // Enumerate with a pattern.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001878 FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001879 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001880 EXPECT_TRUE(c5.HasFile(dir1));
1881 EXPECT_TRUE(c5.HasFile(dir2));
1882 EXPECT_TRUE(c5.HasFile(dir2file));
1883 EXPECT_TRUE(c5.HasFile(dir2inner));
1884 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1885 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001886
rvargas@chromium.org56472942013-08-15 05:46:05 +09001887#if defined(OS_WIN)
1888 {
1889 // Make dir1 point to dir2.
1890 ReparsePoint reparse_point(dir1, dir2);
1891 EXPECT_TRUE(reparse_point.IsValid());
1892
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001893 if ((win::GetVersion() >= win::VERSION_VISTA)) {
rvargas@chromium.org56472942013-08-15 05:46:05 +09001894 // There can be a delay for the enumeration code to see the change on
1895 // the file system so skip this test for XP.
1896 // Enumerate the reparse point.
1897 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
1898 FindResultCollector c6(f6);
1899 FilePath inner2 = dir1.Append(FPL("inner"));
1900 EXPECT_TRUE(c6.HasFile(inner2));
1901 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1902 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
1903 EXPECT_EQ(c6.size(), 3);
1904 }
1905
1906 // No changes for non recursive operation.
1907 FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
1908 FindResultCollector c7(f7);
1909 EXPECT_TRUE(c7.HasFile(dir2));
1910 EXPECT_TRUE(c7.HasFile(dir2));
1911 EXPECT_TRUE(c7.HasFile(file1));
1912 EXPECT_TRUE(c7.HasFile(file2_abs));
1913 EXPECT_EQ(c7.size(), 4);
1914
1915 // Should not enumerate inside dir1 when using recursion.
1916 FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1917 FindResultCollector c8(f8);
1918 EXPECT_TRUE(c8.HasFile(dir1));
1919 EXPECT_TRUE(c8.HasFile(dir2));
1920 EXPECT_TRUE(c8.HasFile(file1));
1921 EXPECT_TRUE(c8.HasFile(file2_abs));
1922 EXPECT_TRUE(c8.HasFile(dir2file));
1923 EXPECT_TRUE(c8.HasFile(dir2inner));
1924 EXPECT_TRUE(c8.HasFile(dir2innerfile));
1925 EXPECT_EQ(c8.size(), 7);
1926 }
1927#endif
1928
initial.commit3f4a7322008-07-27 06:49:38 +09001929 // Make sure the destructor closes the find handle while in the middle of a
1930 // query to allow TearDown to delete the directory.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001931 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1932 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
avi@google.com5cb79352008-12-11 23:55:12 +09001933 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001934}
license.botf003cfe2008-08-24 09:55:55 +09001935
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001936TEST_F(FileUtilTest, AppendToFile) {
1937 FilePath data_dir =
1938 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1939
1940 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001941 if (PathExists(data_dir)) {
1942 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001943 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001944 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001945
1946 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001947 if (PathExists(data_dir)) {
1948 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001949 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001950 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001951 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1952
1953 std::string data("hello");
brettw@chromium.org14b3aa22014-03-12 05:59:02 +09001954 EXPECT_EQ(-1, AppendToFile(foobar, data.c_str(), data.length()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001955 EXPECT_EQ(static_cast<int>(data.length()),
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09001956 WriteFile(foobar, data.c_str(), data.length()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001957 EXPECT_EQ(static_cast<int>(data.length()),
brettw@chromium.org14b3aa22014-03-12 05:59:02 +09001958 AppendToFile(foobar, data.c_str(), data.length()));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001959
1960 const std::wstring read_content = ReadTextFile(foobar);
1961 EXPECT_EQ(L"hellohello", read_content);
1962}
1963
fukino@chromium.orgaf9cba02014-04-18 19:34:15 +09001964TEST_F(FileUtilTest, ReadFile) {
1965 // Create a test file to be read.
1966 const std::string kTestData("The quick brown fox jumps over the lazy dog.");
1967 FilePath file_path =
1968 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileTest"));
1969
1970 ASSERT_EQ(static_cast<int>(kTestData.size()),
1971 WriteFile(file_path, kTestData.data(), kTestData.size()));
1972
1973 // Make buffers with various size.
1974 std::vector<char> small_buffer(kTestData.size() / 2);
1975 std::vector<char> exact_buffer(kTestData.size());
1976 std::vector<char> large_buffer(kTestData.size() * 2);
1977
1978 // Read the file with smaller buffer.
1979 int bytes_read_small = ReadFile(
1980 file_path, &small_buffer[0], static_cast<int>(small_buffer.size()));
1981 EXPECT_EQ(bytes_read_small, static_cast<int>(small_buffer.size()));
1982 EXPECT_EQ(
1983 std::string(small_buffer.begin(), small_buffer.end()),
1984 std::string(kTestData.begin(), kTestData.begin() + small_buffer.size()));
1985
1986 // Read the file with buffer which have exactly same size.
1987 int bytes_read_exact = ReadFile(
1988 file_path, &exact_buffer[0], static_cast<int>(exact_buffer.size()));
1989 EXPECT_EQ(bytes_read_exact, static_cast<int>(kTestData.size()));
1990 EXPECT_EQ(std::string(exact_buffer.begin(), exact_buffer.end()), kTestData);
1991
1992 // Read the file with larger buffer.
1993 int bytes_read_large = ReadFile(
1994 file_path, &large_buffer[0], static_cast<int>(large_buffer.size()));
1995 EXPECT_EQ(bytes_read_large, static_cast<int>(kTestData.size()));
1996 EXPECT_EQ(std::string(large_buffer.begin(),
1997 large_buffer.begin() + kTestData.size()),
1998 kTestData);
1999
2000 // Make sure the return value is -1 if the file doesn't exist.
2001 FilePath file_path_not_exist =
2002 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileNotExistTest"));
2003 EXPECT_EQ(-1,
2004 ReadFile(file_path_not_exist,
2005 &exact_buffer[0],
2006 static_cast<int>(exact_buffer.size())));
2007}
2008
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002009TEST_F(FileUtilTest, ReadFileToString) {
2010 const char kTestData[] = "0123";
2011 std::string data;
2012
2013 FilePath file_path =
2014 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002015 FilePath file_path_dangerous =
2016 temp_dir_.path().Append(FILE_PATH_LITERAL("..")).
2017 Append(temp_dir_.path().BaseName()).
2018 Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002019
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002020 // Create test file.
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002021 ASSERT_EQ(4, WriteFile(file_path, kTestData, 4));
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002022
2023 EXPECT_TRUE(ReadFileToString(file_path, &data));
2024 EXPECT_EQ(kTestData, data);
2025
2026 data = "temp";
2027 EXPECT_FALSE(ReadFileToString(file_path, &data, 0));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002028 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002029
2030 data = "temp";
2031 EXPECT_FALSE(ReadFileToString(file_path, &data, 2));
2032 EXPECT_EQ("01", data);
2033
2034 data.clear();
2035 EXPECT_FALSE(ReadFileToString(file_path, &data, 3));
2036 EXPECT_EQ("012", data);
2037
2038 data.clear();
2039 EXPECT_TRUE(ReadFileToString(file_path, &data, 4));
2040 EXPECT_EQ("0123", data);
2041
2042 data.clear();
2043 EXPECT_TRUE(ReadFileToString(file_path, &data, 6));
2044 EXPECT_EQ("0123", data);
2045
2046 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6));
2047
2048 EXPECT_TRUE(ReadFileToString(file_path, NULL));
2049
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002050 data = "temp";
2051 EXPECT_FALSE(ReadFileToString(file_path_dangerous, &data));
2052 EXPECT_EQ(0u, data.length());
2053
2054 // Delete test file.
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002055 EXPECT_TRUE(base::DeleteFile(file_path, false));
2056
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002057 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002058 EXPECT_FALSE(ReadFileToString(file_path, &data));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002059 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002060
kaliamoorthi@chromium.orgaed76662014-02-27 21:54:32 +09002061 data = "temp";
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002062 EXPECT_FALSE(ReadFileToString(file_path, &data, 6));
tnagel@chromium.orge50a22b2014-05-01 19:40:51 +09002063 EXPECT_EQ(0u, data.length());
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09002064}
2065
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002066TEST_F(FileUtilTest, TouchFile) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002067 FilePath data_dir =
2068 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002069
2070 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002071 if (PathExists(data_dir)) {
2072 ASSERT_TRUE(DeleteFile(data_dir, true));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002073 }
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002074 ASSERT_TRUE(CreateDirectory(data_dir));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002075
2076 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
2077 std::string data("hello");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002078 ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length()));
jochen@chromium.orga6879772010-02-18 19:02:26 +09002079
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002080 Time access_time;
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002081 // This timestamp is divisible by one day (in local timezone),
2082 // to make it work on FAT too.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002083 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002084 &access_time));
2085
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002086 Time modification_time;
jochen@chromium.orga6879772010-02-18 19:02:26 +09002087 // Note that this timestamp is divisible by two (seconds) - FAT stores
2088 // modification times with 2s resolution.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002089 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
jochen@chromium.orga6879772010-02-18 19:02:26 +09002090 &modification_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002091
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002092 ASSERT_TRUE(TouchFile(foobar, access_time, modification_time));
rvargas@chromium.orgb005b382014-01-07 19:06:58 +09002093 File::Info file_info;
brettw@chromium.orga9154032013-12-05 05:56:49 +09002094 ASSERT_TRUE(GetFileInfo(foobar, &file_info));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002095 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
2096 access_time.ToInternalValue());
2097 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
2098 modification_time.ToInternalValue());
jochen@chromium.orga6879772010-02-18 19:02:26 +09002099}
2100
tfarina@chromium.org34828222010-05-26 10:40:12 +09002101TEST_F(FileUtilTest, IsDirectoryEmpty) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002102 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002103
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002104 ASSERT_FALSE(PathExists(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002105
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002106 ASSERT_TRUE(CreateDirectory(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002107
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002108 EXPECT_TRUE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002109
2110 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
2111 std::string bar("baz");
brettw@chromium.org8c7b6b82014-03-07 05:42:30 +09002112 ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length()));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002113
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002114 EXPECT_FALSE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002115}
2116
skerner@google.com93449ef2011-09-22 23:47:18 +09002117#if defined(OS_POSIX)
2118
2119// Testing VerifyPathControlledByAdmin() is hard, because there is no
2120// way a test can make a file owned by root, or change file paths
2121// at the root of the file system. VerifyPathControlledByAdmin()
2122// is implemented as a call to VerifyPathControlledByUser, which gives
2123// us the ability to test with paths under the test's temp directory,
2124// using a user id we control.
2125// Pull tests of VerifyPathControlledByUserTest() into a separate test class
2126// with a common SetUp() method.
2127class VerifyPathControlledByUserTest : public FileUtilTest {
2128 protected:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +09002129 virtual void SetUp() OVERRIDE {
skerner@google.com93449ef2011-09-22 23:47:18 +09002130 FileUtilTest::SetUp();
2131
2132 // Create a basic structure used by each test.
2133 // base_dir_
2134 // |-> sub_dir_
2135 // |-> text_file_
2136
2137 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002138 ASSERT_TRUE(CreateDirectory(base_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002139
2140 sub_dir_ = base_dir_.AppendASCII("sub_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002141 ASSERT_TRUE(CreateDirectory(sub_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002142
2143 text_file_ = sub_dir_.AppendASCII("file.txt");
2144 CreateTextFile(text_file_, L"This text file has some text in it.");
2145
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002146 // Get the user and group files are created with from |base_dir_|.
2147 struct stat stat_buf;
2148 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
2149 uid_ = stat_buf.st_uid;
skerner@chromium.org80784142011-10-18 06:30:29 +09002150 ok_gids_.insert(stat_buf.st_gid);
2151 bad_gids_.insert(stat_buf.st_gid + 1);
2152
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002153 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
skerner@google.com93449ef2011-09-22 23:47:18 +09002154
2155 // To ensure that umask settings do not cause the initial state
2156 // of permissions to be different from what we expect, explicitly
2157 // set permissions on the directories we create.
2158 // Make all files and directories non-world-writable.
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002159
2160 // Users and group can read, write, traverse
2161 int enabled_permissions =
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002162 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002163 // Other users can't read, write, traverse
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002164 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
skerner@google.com93449ef2011-09-22 23:47:18 +09002165
2166 ASSERT_NO_FATAL_FAILURE(
2167 ChangePosixFilePermissions(
2168 base_dir_, enabled_permissions, disabled_permissions));
2169 ASSERT_NO_FATAL_FAILURE(
2170 ChangePosixFilePermissions(
2171 sub_dir_, enabled_permissions, disabled_permissions));
2172 }
2173
2174 FilePath base_dir_;
2175 FilePath sub_dir_;
2176 FilePath text_file_;
2177 uid_t uid_;
skerner@chromium.org80784142011-10-18 06:30:29 +09002178
2179 std::set<gid_t> ok_gids_;
2180 std::set<gid_t> bad_gids_;
skerner@google.com93449ef2011-09-22 23:47:18 +09002181};
2182
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002183TEST_F(VerifyPathControlledByUserTest, BadPaths) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002184 // File does not exist.
2185 FilePath does_not_exist = base_dir_.AppendASCII("does")
2186 .AppendASCII("not")
2187 .AppendASCII("exist");
skerner@google.com93449ef2011-09-22 23:47:18 +09002188 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002189 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002190 base_dir_, does_not_exist, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002191
2192 // |base| not a subpath of |path|.
2193 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002194 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002195 sub_dir_, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002196
2197 // An empty base path will fail to be a prefix for any path.
2198 FilePath empty;
2199 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002200 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002201 empty, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002202
2203 // Finding that a bad call fails proves nothing unless a good call succeeds.
2204 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002205 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002206 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002207}
2208
2209TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2210 // Symlinks in the path should cause failure.
2211
2212 // Symlink to the file at the end of the path.
2213 FilePath file_link = base_dir_.AppendASCII("file_link");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002214 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
skerner@google.com93449ef2011-09-22 23:47:18 +09002215 << "Failed to create symlink.";
2216
2217 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002218 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002219 base_dir_, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002220 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002221 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002222 file_link, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002223
2224 // Symlink from one directory to another within the path.
2225 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002226 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
skerner@google.com93449ef2011-09-22 23:47:18 +09002227 << "Failed to create symlink.";
2228
2229 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002230 ASSERT_TRUE(PathExists(file_path_with_link));
skerner@google.com93449ef2011-09-22 23:47:18 +09002231
2232 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002233 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002234 base_dir_, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002235
2236 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002237 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002238 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002239
2240 // Symlinks in parents of base path are allowed.
2241 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002242 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002243 file_path_with_link, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002244}
2245
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002246TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002247 // Get a uid that is not the uid of files we create.
2248 uid_t bad_uid = uid_ + 1;
2249
skerner@google.com93449ef2011-09-22 23:47:18 +09002250 // Make all files and directories non-world-writable.
2251 ASSERT_NO_FATAL_FAILURE(
2252 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2253 ASSERT_NO_FATAL_FAILURE(
2254 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2255 ASSERT_NO_FATAL_FAILURE(
2256 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2257
2258 // We control these paths.
2259 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002260 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002261 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002262 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002263 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002264 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002265 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002266 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002267 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002268
2269 // Another user does not control these paths.
2270 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002271 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002272 base_dir_, sub_dir_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002273 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002274 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002275 base_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002276 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002277 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002278 sub_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002279
2280 // Another group does not control the paths.
2281 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002282 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002283 base_dir_, sub_dir_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002284 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002285 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002286 base_dir_, text_file_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002287 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002288 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002289 sub_dir_, text_file_, uid_, bad_gids_));
2290}
2291
2292TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2293 // Make all files and directories writable only by their owner.
2294 ASSERT_NO_FATAL_FAILURE(
2295 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2296 ASSERT_NO_FATAL_FAILURE(
2297 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2298 ASSERT_NO_FATAL_FAILURE(
2299 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2300
2301 // Any group is okay because the path is not group-writable.
2302 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002303 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002304 base_dir_, sub_dir_, uid_, ok_gids_));
2305 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002306 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002307 base_dir_, text_file_, uid_, ok_gids_));
2308 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002309 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002310 sub_dir_, text_file_, uid_, ok_gids_));
2311
2312 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002313 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002314 base_dir_, sub_dir_, uid_, bad_gids_));
2315 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002316 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002317 base_dir_, text_file_, uid_, bad_gids_));
2318 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002319 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002320 sub_dir_, text_file_, uid_, bad_gids_));
2321
2322 // No group is okay, because we don't check the group
2323 // if no group can write.
2324 std::set<gid_t> no_gids; // Empty set of gids.
2325 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002326 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002327 base_dir_, sub_dir_, uid_, no_gids));
2328 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002329 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002330 base_dir_, text_file_, uid_, no_gids));
2331 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002332 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002333 sub_dir_, text_file_, uid_, no_gids));
2334
2335
2336 // Make all files and directories writable by their group.
2337 ASSERT_NO_FATAL_FAILURE(
2338 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2339 ASSERT_NO_FATAL_FAILURE(
2340 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2341 ASSERT_NO_FATAL_FAILURE(
2342 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2343
2344 // Now |ok_gids_| works, but |bad_gids_| fails.
2345 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002346 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002347 base_dir_, sub_dir_, uid_, ok_gids_));
2348 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002349 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002350 base_dir_, text_file_, uid_, ok_gids_));
2351 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002352 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002353 sub_dir_, text_file_, uid_, ok_gids_));
2354
2355 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002356 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002357 base_dir_, sub_dir_, uid_, bad_gids_));
2358 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002359 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002360 base_dir_, text_file_, uid_, bad_gids_));
2361 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002362 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002363 sub_dir_, text_file_, uid_, bad_gids_));
2364
2365 // Because any group in the group set is allowed,
2366 // the union of good and bad gids passes.
2367
2368 std::set<gid_t> multiple_gids;
2369 std::set_union(
2370 ok_gids_.begin(), ok_gids_.end(),
2371 bad_gids_.begin(), bad_gids_.end(),
2372 std::inserter(multiple_gids, multiple_gids.begin()));
2373
2374 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002375 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002376 base_dir_, sub_dir_, uid_, multiple_gids));
2377 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002378 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002379 base_dir_, text_file_, uid_, multiple_gids));
2380 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002381 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002382 sub_dir_, text_file_, uid_, multiple_gids));
skerner@google.com93449ef2011-09-22 23:47:18 +09002383}
2384
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002385TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002386 // Make all files and directories non-world-writable.
2387 ASSERT_NO_FATAL_FAILURE(
2388 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2389 ASSERT_NO_FATAL_FAILURE(
2390 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2391 ASSERT_NO_FATAL_FAILURE(
2392 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2393
2394 // Initialy, we control all parts of the path.
2395 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002396 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002397 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002398 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002399 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002400 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002401 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002402 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002403 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002404
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09002405 // Make base_dir_ world-writable.
skerner@google.com93449ef2011-09-22 23:47:18 +09002406 ASSERT_NO_FATAL_FAILURE(
2407 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2408 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002409 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002410 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002411 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002412 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002413 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002414 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002415 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002416 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002417
2418 // Make sub_dir_ world writable.
2419 ASSERT_NO_FATAL_FAILURE(
2420 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2421 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002422 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002423 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002424 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002425 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002426 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002427 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002428 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002429 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002430
2431 // Make text_file_ world writable.
2432 ASSERT_NO_FATAL_FAILURE(
2433 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2434 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002435 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002436 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002437 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002438 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002439 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002440 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002441 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002442 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002443
2444 // Make sub_dir_ non-world writable.
2445 ASSERT_NO_FATAL_FAILURE(
2446 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2447 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002448 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002449 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002450 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002451 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002452 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002453 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002454 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002455 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002456
2457 // Make base_dir_ non-world-writable.
2458 ASSERT_NO_FATAL_FAILURE(
2459 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2460 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002461 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002462 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002463 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002464 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002465 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002466 EXPECT_FALSE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002467 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002468 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002469
2470 // Back to the initial state: Nothing is writable, so every path
2471 // should pass.
2472 ASSERT_NO_FATAL_FAILURE(
2473 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2474 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002475 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002476 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002477 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002478 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002479 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002480 EXPECT_TRUE(
brettw@chromium.orgaa82a772014-03-14 02:26:21 +09002481 base::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002482 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002483}
2484
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002485#if defined(OS_ANDROID)
2486TEST_F(FileUtilTest, ValidContentUriTest) {
2487 // Get the test image path.
2488 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002489 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002490 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002491 ASSERT_TRUE(PathExists(data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002492 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2493 int64 image_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002494 GetFileSize(image_file, &image_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002495 EXPECT_LT(0, image_size);
2496
2497 // Insert the image into MediaStore. MediaStore will do some conversions, and
2498 // return the content URI.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002499 FilePath path = file_util::InsertImageIntoMediaStore(image_file);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002500 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002501 EXPECT_TRUE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002502 // The file size may not equal to the input image as MediaStore may convert
2503 // the image.
2504 int64 content_uri_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002505 GetFileSize(path, &content_uri_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002506 EXPECT_EQ(image_size, content_uri_size);
2507
2508 // We should be able to read the file.
2509 char* buffer = new char[image_size];
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002510 File file = OpenContentUriForRead(path);
2511 EXPECT_TRUE(file.IsValid());
2512 EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002513 delete[] buffer;
2514}
2515
2516TEST_F(FileUtilTest, NonExistentContentUriTest) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002517 FilePath path("content://foo.bar");
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002518 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002519 EXPECT_FALSE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002520 // Size should be smaller than 0.
2521 int64 size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002522 EXPECT_FALSE(GetFileSize(path, &size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002523
2524 // We should not be able to read the file.
rvargas@chromium.org799ba6c2014-03-21 09:41:15 +09002525 File file = OpenContentUriForRead(path);
2526 EXPECT_FALSE(file.IsValid());
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002527}
2528#endif
2529
jln@chromium.orgd7493932014-03-01 00:35:36 +09002530TEST(ScopedFD, ScopedFDDoesClose) {
2531 int fds[2];
2532 char c = 0;
2533 ASSERT_EQ(0, pipe(fds));
2534 const int write_end = fds[1];
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002535 base::ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002536 {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002537 base::ScopedFD write_end_closer(fds[1]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002538 }
2539 // This is the only thread. This file descriptor should no longer be valid.
2540 int ret = close(write_end);
2541 EXPECT_EQ(-1, ret);
2542 EXPECT_EQ(EBADF, errno);
2543 // Make sure read(2) won't block.
2544 ASSERT_EQ(0, fcntl(fds[0], F_SETFL, O_NONBLOCK));
2545 // Reading the pipe should EOF.
2546 EXPECT_EQ(0, read(fds[0], &c, 1));
2547}
2548
2549#if defined(GTEST_HAS_DEATH_TEST)
2550void CloseWithScopedFD(int fd) {
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002551 base::ScopedFD fd_closer(fd);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002552}
2553#endif
2554
2555TEST(ScopedFD, ScopedFDCrashesOnCloseFailure) {
2556 int fds[2];
2557 ASSERT_EQ(0, pipe(fds));
brettw@chromium.orgb52f29b2014-03-18 04:02:35 +09002558 base::ScopedFD read_end_closer(fds[0]);
jln@chromium.orgd7493932014-03-01 00:35:36 +09002559 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1])));
2560#if defined(GTEST_HAS_DEATH_TEST)
2561 // This is the only thread. This file descriptor should no longer be valid.
2562 // Trying to close it should crash. This is important for security.
2563 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2564#endif
2565}
2566
skerner@google.com93449ef2011-09-22 23:47:18 +09002567#endif // defined(OS_POSIX)
2568
mark@chromium.org17684802008-09-10 09:16:28 +09002569} // namespace
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002570
2571} // namespace base