blob: d36d4b2d78cf033756cf2fa5b56a342aa436ba66 [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
michaelbai@chromium.orgbf8418f2011-10-25 09:08:28 +090015#include <algorithm>
initial.commit3f4a7322008-07-27 06:49:38 +090016#include <fstream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090017#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090018
19#include "base/base_paths.h"
20#include "base/file_util.h"
brettw@chromium.org56946722013-06-08 13:53:36 +090021#include "base/files/file_enumerator.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090022#include "base/files/file_path.h"
brettw@chromium.org091db522012-11-17 05:34:23 +090023#include "base/files/scoped_temp_dir.h"
initial.commit3f4a7322008-07-27 06:49:38 +090024#include "base/path_service.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090025#include "base/strings/utf_string_conversions.h"
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +090026#include "base/test/test_file_util.h"
brettw@chromium.org61391822011-01-01 05:02:16 +090027#include "base/threading/platform_thread.h"
initial.commit3f4a7322008-07-27 06:49:38 +090028#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090029#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090030
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090031#if defined(OS_WIN)
32#include "base/win/scoped_handle.h"
rvargas@chromium.org56472942013-08-15 05:46:05 +090033#include "base/win/windows_version.h"
tfarina@chromium.orgc89216a2011-01-10 01:32:20 +090034#endif
35
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +090036#if defined(OS_ANDROID)
37#include "base/android/content_uri_utils.h"
38#endif
39
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090040// This macro helps avoid wrapped lines in the test structs.
41#define FPL(x) FILE_PATH_LITERAL(x)
42
brettw@chromium.org2873d9b2013-11-28 08:22:08 +090043namespace base {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090044
initial.commit3f4a7322008-07-27 06:49:38 +090045namespace {
46
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090047// To test that file_util::Normalize FilePath() deals with NTFS reparse points
48// correctly, we need functions to create and delete reparse points.
49#if defined(OS_WIN)
50typedef struct _REPARSE_DATA_BUFFER {
51 ULONG ReparseTag;
52 USHORT ReparseDataLength;
53 USHORT Reserved;
54 union {
55 struct {
56 USHORT SubstituteNameOffset;
57 USHORT SubstituteNameLength;
58 USHORT PrintNameOffset;
59 USHORT PrintNameLength;
60 ULONG Flags;
61 WCHAR PathBuffer[1];
62 } SymbolicLinkReparseBuffer;
63 struct {
64 USHORT SubstituteNameOffset;
65 USHORT SubstituteNameLength;
66 USHORT PrintNameOffset;
67 USHORT PrintNameLength;
68 WCHAR PathBuffer[1];
69 } MountPointReparseBuffer;
70 struct {
71 UCHAR DataBuffer[1];
72 } GenericReparseBuffer;
73 };
74} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
75
76// Sets a reparse point. |source| will now point to |target|. Returns true if
77// the call succeeds, false otherwise.
78bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
79 std::wstring kPathPrefix = L"\\??\\";
80 std::wstring target_str;
81 // The juction will not work if the target path does not start with \??\ .
82 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
83 target_str += kPathPrefix;
84 target_str += target_path.value();
85 const wchar_t* target = target_str.c_str();
86 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
87 char buffer[2000] = {0};
88 DWORD returned;
89
90 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
91
92 data->ReparseTag = 0xa0000003;
93 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
94
95 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
96 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
97 data->ReparseDataLength = size_target + 4 + 8;
98
99 int data_size = data->ReparseDataLength + 8;
100
101 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
102 NULL, 0, &returned, NULL)) {
103 return false;
104 }
105 return true;
106}
107
108// Delete the reparse point referenced by |source|. Returns true if the call
109// succeeds, false otherwise.
110bool DeleteReparsePoint(HANDLE source) {
111 DWORD returned;
112 REPARSE_DATA_BUFFER data = {0};
113 data.ReparseTag = 0xa0000003;
114 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
115 &returned, NULL)) {
116 return false;
117 }
118 return true;
119}
rvargas@chromium.org56472942013-08-15 05:46:05 +0900120
121// Manages a reparse point for a test.
122class ReparsePoint {
123 public:
124 // Creates a reparse point from |source| (an empty directory) to |target|.
125 ReparsePoint(const FilePath& source, const FilePath& target) {
126 dir_.Set(
127 ::CreateFile(source.value().c_str(),
128 FILE_ALL_ACCESS,
129 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
130 NULL,
131 OPEN_EXISTING,
132 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
133 NULL));
134 created_ = dir_.IsValid() && SetReparsePoint(dir_, target);
135 }
136
137 ~ReparsePoint() {
138 if (created_)
139 DeleteReparsePoint(dir_);
140 }
141
142 bool IsValid() { return created_; }
143
144 private:
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900145 win::ScopedHandle dir_;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900146 bool created_;
147 DISALLOW_COPY_AND_ASSIGN(ReparsePoint);
148};
149
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900150#endif
151
skerner@google.com93449ef2011-09-22 23:47:18 +0900152#if defined(OS_POSIX)
153// Provide a simple way to change the permissions bits on |path| in tests.
154// ASSERT failures will return, but not stop the test. Caller should wrap
155// calls to this function in ASSERT_NO_FATAL_FAILURE().
156void ChangePosixFilePermissions(const FilePath& path,
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900157 int mode_bits_to_set,
158 int mode_bits_to_clear) {
skerner@google.com93449ef2011-09-22 23:47:18 +0900159 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
160 << "Can't set and clear the same bits.";
161
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900162 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900163 ASSERT_TRUE(GetPosixFilePermissions(path, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900164 mode |= mode_bits_to_set;
165 mode &= ~mode_bits_to_clear;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900166 ASSERT_TRUE(SetPosixFilePermissions(path, mode));
skerner@google.com93449ef2011-09-22 23:47:18 +0900167}
168#endif // defined(OS_POSIX)
169
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900170const wchar_t bogus_content[] = L"I'm cannon fodder.";
171
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900172const int FILES_AND_DIRECTORIES =
brettw@chromium.org56946722013-06-08 13:53:36 +0900173 FileEnumerator::FILES | FileEnumerator::DIRECTORIES;
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900174
erikkay@google.comf2406842008-08-21 00:59:49 +0900175// file_util winds up using autoreleased objects on the Mac, so this needs
176// to be a PlatformTest
177class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900178 protected:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +0900179 virtual void SetUp() OVERRIDE {
erikkay@google.comf2406842008-08-21 00:59:49 +0900180 PlatformTest::SetUp();
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900181 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commit3f4a7322008-07-27 06:49:38 +0900182 }
183
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900184 ScopedTempDir temp_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900185};
186
187// Collects all the results from the given file enumerator, and provides an
188// interface to query whether a given file is present.
189class FindResultCollector {
190 public:
brettw@chromium.org56946722013-06-08 13:53:36 +0900191 explicit FindResultCollector(FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900192 FilePath cur_file;
193 while (!(cur_file = enumerator.Next()).value().empty()) {
194 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900195 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900196 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900197 << "Same file returned twice";
198
199 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900200 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900201 }
202 }
203
204 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900205 bool HasFile(const FilePath& file) const {
206 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900207 }
evanm@google.com874d1672008-10-31 08:54:04 +0900208
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900209 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900210 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900211 }
initial.commit3f4a7322008-07-27 06:49:38 +0900212
213 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900214 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900215};
216
217// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900218void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900219 const std::wstring& contents) {
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900220 std::wofstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900221 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900222 ASSERT_TRUE(file.is_open());
223 file << contents;
224 file.close();
225}
226
227// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900228std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900229 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900230 std::wifstream file;
evan@chromium.org1ae6e0d2011-02-05 05:41:33 +0900231 file.open(filename.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900232 EXPECT_TRUE(file.is_open());
rvargas@google.com6c6d2642011-03-26 05:49:54 +0900233 file.getline(contents, arraysize(contents));
initial.commit3f4a7322008-07-27 06:49:38 +0900234 file.close();
235 return std::wstring(contents);
236}
237
erikkay@google.com014161d2008-08-16 02:45:13 +0900238#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900239uint64 FileTimeAsUint64(const FILETIME& ft) {
240 ULARGE_INTEGER u;
241 u.LowPart = ft.dwLowDateTime;
242 u.HighPart = ft.dwHighDateTime;
243 return u.QuadPart;
244}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900245#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900246
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900247TEST_F(FileUtilTest, FileAndDirectorySize) {
248 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
249 // should return 53 bytes.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900250 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900251 CreateTextFile(file_01, L"12345678901234567890");
252 int64 size_f1 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900253 ASSERT_TRUE(GetFileSize(file_01, &size_f1));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900254 EXPECT_EQ(20ll, size_f1);
255
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900256 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900257 CreateDirectory(subdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900258
259 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
260 CreateTextFile(file_02, L"123456789012345678901234567890");
261 int64 size_f2 = 0;
brettw@chromium.org70684242013-12-05 03:22:49 +0900262 ASSERT_TRUE(GetFileSize(file_02, &size_f2));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900263 EXPECT_EQ(30ll, size_f2);
264
265 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900266 CreateDirectory(subsubdir_path);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900267
268 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
269 CreateTextFile(file_03, L"123");
270
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900271 int64 computed_size = ComputeDirectorySize(temp_dir_.path());
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900272 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
273}
274
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900275TEST_F(FileUtilTest, NormalizeFilePathBasic) {
276 // Create a directory under the test dir. Because we create it,
277 // we know it is not a link.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900278 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
279 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900280 FilePath file_b_path = dir_path.Append(FPL("file_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900281 CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900282
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900283 FilePath normalized_file_a_path, normalized_file_b_path;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900284 ASSERT_FALSE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900285 ASSERT_FALSE(NormalizeFilePath(file_a_path, &normalized_file_a_path))
viettrungluu@chromium.orgea703f12010-08-23 01:19:13 +0900286 << "NormalizeFilePath() should fail on nonexistent paths.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900287
288 CreateTextFile(file_a_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900289 ASSERT_TRUE(PathExists(file_a_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900290 ASSERT_TRUE(NormalizeFilePath(file_a_path, &normalized_file_a_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900291
292 CreateTextFile(file_b_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900293 ASSERT_TRUE(PathExists(file_b_path));
brettw@chromium.org70684242013-12-05 03:22:49 +0900294 ASSERT_TRUE(NormalizeFilePath(file_b_path, &normalized_file_b_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900295
296 // Beacuse this test created |dir_path|, we know it is not a link
297 // or junction. So, the real path of the directory holding file a
298 // must be the parent of the path holding file b.
299 ASSERT_TRUE(normalized_file_a_path.DirName()
300 .IsParent(normalized_file_b_path.DirName()));
301}
302
303#if defined(OS_WIN)
304
305TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
306 // Build the following directory structure:
307 //
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900308 // temp_dir
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900309 // |-> base_a
310 // | |-> sub_a
311 // | |-> file.txt
312 // | |-> long_name___... (Very long name.)
313 // | |-> sub_long
314 // | |-> deep.txt
315 // |-> base_b
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900316 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
317 // |-> to_base_b (reparse point to temp_dir\base_b)
318 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900319
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900320 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900321 ASSERT_TRUE(CreateDirectory(base_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900322
323 FilePath sub_a = base_a.Append(FPL("sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900324 ASSERT_TRUE(CreateDirectory(sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900325
326 FilePath file_txt = sub_a.Append(FPL("file.txt"));
327 CreateTextFile(file_txt, bogus_content);
328
329 // Want a directory whose name is long enough to make the path to the file
330 // inside just under MAX_PATH chars. This will be used to test that when
331 // a junction expands to a path over MAX_PATH chars in length,
332 // NormalizeFilePath() fails without crashing.
333 FilePath sub_long_rel(FPL("sub_long"));
334 FilePath deep_txt(FPL("deep.txt"));
335
336 int target_length = MAX_PATH;
337 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
338 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900339 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900340 // the resulting path is still long enough to hit the failing case in
341 // NormalizePath().
342 const int kCreateDirLimit = 4;
343 target_length -= kCreateDirLimit;
344 FilePath::StringType long_name_str = FPL("long_name_");
345 long_name_str.resize(target_length, '_');
346
347 FilePath long_name = sub_a.Append(FilePath(long_name_str));
348 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
349 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
350
351 FilePath sub_long = deep_file.DirName();
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900352 ASSERT_TRUE(CreateDirectory(sub_long));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900353 CreateTextFile(deep_file, bogus_content);
354
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900355 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900356 ASSERT_TRUE(CreateDirectory(base_b));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900357
358 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900359 ASSERT_TRUE(CreateDirectory(to_sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900360 FilePath normalized_path;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900361 {
362 ReparsePoint reparse_to_sub_a(to_sub_a, sub_a);
363 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900364
rvargas@chromium.org56472942013-08-15 05:46:05 +0900365 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900366 ASSERT_TRUE(CreateDirectory(to_base_b));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900367 ReparsePoint reparse_to_base_b(to_base_b, base_b);
368 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900369
rvargas@chromium.org56472942013-08-15 05:46:05 +0900370 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900371 ASSERT_TRUE(CreateDirectory(to_sub_long));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900372 ReparsePoint reparse_to_sub_long(to_sub_long, sub_long);
373 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900374
rvargas@chromium.org56472942013-08-15 05:46:05 +0900375 // Normalize a junction free path: base_a\sub_a\file.txt .
brettw@chromium.org70684242013-12-05 03:22:49 +0900376 ASSERT_TRUE(NormalizeFilePath(file_txt, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900377 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
378
379 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
380 // the junction to_sub_a.
brettw@chromium.org70684242013-12-05 03:22:49 +0900381 ASSERT_TRUE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900382 &normalized_path));
383 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
384
385 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
386 // normalized to exclude junctions to_base_b and to_sub_a .
brettw@chromium.org70684242013-12-05 03:22:49 +0900387 ASSERT_TRUE(NormalizeFilePath(base_b.Append(FPL("to_base_b"))
rvargas@chromium.org56472942013-08-15 05:46:05 +0900388 .Append(FPL("to_base_b"))
389 .Append(FPL("to_sub_a"))
390 .Append(FPL("file.txt")),
391 &normalized_path));
392 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
393
394 // A long enough path will cause NormalizeFilePath() to fail. Make a long
395 // path using to_base_b many times, and check that paths long enough to fail
396 // do not cause a crash.
397 FilePath long_path = base_b;
398 const int kLengthLimit = MAX_PATH + 200;
399 while (long_path.value().length() <= kLengthLimit) {
400 long_path = long_path.Append(FPL("to_base_b"));
401 }
402 long_path = long_path.Append(FPL("to_sub_a"))
403 .Append(FPL("file.txt"));
404
brettw@chromium.org70684242013-12-05 03:22:49 +0900405 ASSERT_FALSE(NormalizeFilePath(long_path, &normalized_path));
rvargas@chromium.org56472942013-08-15 05:46:05 +0900406
407 // Normalizing the junction to deep.txt should fail, because the expanded
408 // path to deep.txt is longer than MAX_PATH.
brettw@chromium.org70684242013-12-05 03:22:49 +0900409 ASSERT_FALSE(NormalizeFilePath(to_sub_long.Append(deep_txt),
rvargas@chromium.org56472942013-08-15 05:46:05 +0900410 &normalized_path));
411
412 // Delete the reparse points, and see that NormalizeFilePath() fails
413 // to traverse them.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900414 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900415
brettw@chromium.org70684242013-12-05 03:22:49 +0900416 ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900417 &normalized_path));
418}
419
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900420TEST_F(FileUtilTest, DevicePathToDriveLetter) {
421 // Get a drive letter.
422 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
423 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
424 LOG(ERROR) << "Can't get a drive letter to test with.";
425 return;
426 }
427
428 // Get the NT style path to that drive.
429 wchar_t device_path[MAX_PATH] = {'\0'};
430 ASSERT_TRUE(
431 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
432 FilePath actual_device_path(device_path);
433 FilePath win32_path;
434
435 // Run DevicePathToDriveLetterPath() on the NT style path we got from
436 // QueryDosDevice(). Expect the drive letter we started with.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900437 ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path, &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900438 ASSERT_EQ(real_drive_letter, win32_path.value());
439
440 // Add some directories to the path. Expect those extra path componenets
441 // to be preserved.
442 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900443 ASSERT_TRUE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900444 actual_device_path.Append(kRelativePath),
445 &win32_path));
446 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
447 win32_path.value());
448
449 // Deform the real path so that it is invalid by removing the last four
450 // characters. The way windows names devices that are hard disks
451 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
452 // than three characters. The only way the truncated string could be a
453 // real drive is if more than 10^3 disks are mounted:
454 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
455 // Check that DevicePathToDriveLetterPath fails.
456 int path_length = actual_device_path.value().length();
457 int new_length = path_length - 4;
458 ASSERT_LT(0, new_length);
459 FilePath prefix_of_real_device_path(
460 actual_device_path.value().substr(0, new_length));
brettw@chromium.orga9154032013-12-05 05:56:49 +0900461 ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path,
462 &win32_path));
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900463
brettw@chromium.orga9154032013-12-05 05:56:49 +0900464 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900465 prefix_of_real_device_path.Append(kRelativePath),
466 &win32_path));
467
468 // Deform the real path so that it is invalid by adding some characters. For
469 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
470 // request for the drive letter whose native path is
471 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
472 // because drives are numbered in order and mounting 112345 hard disks will
473 // never happen.
474 const FilePath::StringType kExtraChars = FPL("12345");
475
476 FilePath real_device_path_plus_numbers(
477 actual_device_path.value() + kExtraChars);
478
brettw@chromium.orga9154032013-12-05 05:56:49 +0900479 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900480 real_device_path_plus_numbers,
481 &win32_path));
482
brettw@chromium.orga9154032013-12-05 05:56:49 +0900483 ASSERT_FALSE(DevicePathToDriveLetterPath(
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900484 real_device_path_plus_numbers.Append(kRelativePath),
485 &win32_path));
486}
487
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900488TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
489 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
490 // if it is available. This test requires that:
491 // - the filesystem at |temp_dir_| supports long filenames.
492 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
493 // directories of |temp_dir_|.
494 const FilePath::CharType kLongDirName[] = FPL("A long path");
495 const FilePath::CharType kTestSubDirName[] = FPL("test");
496 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900497 ASSERT_TRUE(CreateDirectory(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900498
499 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
500 // different short name.
501 WCHAR path_buffer[MAX_PATH];
502 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
503 path_buffer, MAX_PATH);
504 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
505 ASSERT_NE(DWORD(0), path_buffer_length);
506 FilePath short_test_dir(path_buffer);
507 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
508
509 FilePath temp_file;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900510 ASSERT_TRUE(CreateTemporaryFileInDir(short_test_dir, &temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900511 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900512 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900513
514 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
515 // unreadable. We should still be able to create a temp file in the
516 // subdirectory, but we won't be able to determine the long path for it. This
517 // mimics the environment that some users run where their user profiles reside
518 // in a location where the don't have full access to the higher level
519 // directories. (Note that this assumption is true for NTFS, but not for some
520 // network file systems. E.g. AFS).
521 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900522 ASSERT_TRUE(CreateDirectory(access_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900523 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
524 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
525
526 // Use the short form of the directory to create a temporary filename.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900527 ASSERT_TRUE(CreateTemporaryFileInDir(
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900528 short_test_dir.Append(kTestSubDirName), &temp_file));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900529 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900530 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
531
532 // Check that the long path can't be determined for |temp_file|.
533 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
534 path_buffer, MAX_PATH);
535 EXPECT_EQ(DWORD(0), path_buffer_length);
536}
537
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900538#endif // defined(OS_WIN)
539
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900540#if defined(OS_POSIX)
541
542TEST_F(FileUtilTest, CreateAndReadSymlinks) {
543 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
544 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
545 CreateTextFile(link_to, bogus_content);
546
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900547 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900548 << "Failed to create file symlink.";
549
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900550 // If we created the link properly, we should be able to read the contents
551 // through it.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900552 std::wstring contents = ReadTextFile(link_from);
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900553 EXPECT_EQ(bogus_content, contents);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900554
555 FilePath result;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900556 ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900557 EXPECT_EQ(link_to.value(), result.value());
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900558
559 // Link to a directory.
560 link_from = temp_dir_.path().Append(FPL("from_dir"));
561 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900562 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900563 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900564 << "Failed to create directory symlink.";
565
566 // Test failures.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900567 EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
568 EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900569 FilePath missing = temp_dir_.path().Append(FPL("missing"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900570 EXPECT_FALSE(ReadSymbolicLink(missing, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900571}
572
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900573// The following test of NormalizeFilePath() require that we create a symlink.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900574// This can not be done on Windows before Vista. On Vista, creating a symlink
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900575// requires privilege "SeCreateSymbolicLinkPrivilege".
576// TODO(skerner): Investigate the possibility of giving base_unittests the
577// privileges required to create a symlink.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900578TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
skerner@chromium.org559baa92010-05-13 00:13:57 +0900579 // Link one file to another.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900580 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
581 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900582 CreateTextFile(link_to, bogus_content);
583
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900584 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900585 << "Failed to create file symlink.";
586
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900587 // Check that NormalizeFilePath sees the link.
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900588 FilePath normalized_path;
brettw@chromium.org70684242013-12-05 03:22:49 +0900589 ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900590 EXPECT_NE(link_from, link_to);
591 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
592 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900593
594 // Link to a directory.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900595 link_from = temp_dir_.path().Append(FPL("from_dir"));
596 link_to = temp_dir_.path().Append(FPL("to_dir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900597 ASSERT_TRUE(CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900598 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900599 << "Failed to create directory symlink.";
600
brettw@chromium.org70684242013-12-05 03:22:49 +0900601 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900602 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900603
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900604 // Test that a loop in the links causes NormalizeFilePath() to return false.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900605 link_from = temp_dir_.path().Append(FPL("link_a"));
606 link_to = temp_dir_.path().Append(FPL("link_b"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900607 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900608 << "Failed to create loop symlink a.";
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900609 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900610 << "Failed to create loop symlink b.";
611
612 // Infinite loop!
brettw@chromium.org70684242013-12-05 03:22:49 +0900613 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900614}
615#endif // defined(OS_POSIX)
616
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900617TEST_F(FileUtilTest, DeleteNonExistent) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900618 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900619 ASSERT_FALSE(PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900620
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900621 EXPECT_TRUE(DeleteFile(non_existent, false));
622 ASSERT_FALSE(PathExists(non_existent));
623 EXPECT_TRUE(DeleteFile(non_existent, true));
624 ASSERT_FALSE(PathExists(non_existent));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900625}
626
627TEST_F(FileUtilTest, DeleteFile) {
628 // Create a file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900629 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900630 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900631 ASSERT_TRUE(PathExists(file_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900632
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900633 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900634 EXPECT_TRUE(DeleteFile(file_name, false));
635 EXPECT_FALSE(PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900636
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900637 // Test recursive case, create a new file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900638 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900639 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900640 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900641
642 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900643 EXPECT_TRUE(DeleteFile(file_name, true));
644 EXPECT_FALSE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900645}
646
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900647#if defined(OS_POSIX)
648TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900649 // Create a file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900650 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
651 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900652 ASSERT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900653
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900654 // Create a symlink to the file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900655 FilePath file_link = temp_dir_.path().Append("file_link_2");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900656 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900657 << "Failed to create symlink.";
658
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900659 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900660 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900661
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900662 // Make sure original file is not deleted.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900663 EXPECT_FALSE(PathExists(file_link));
664 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900665}
666
667TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900668 // Create a non-existent file path.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900669 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900670 EXPECT_FALSE(PathExists(non_existent));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900671
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900672 // Create a symlink to the non-existent file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900673 FilePath file_link = temp_dir_.path().Append("file_link_3");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900674 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900675 << "Failed to create symlink.";
676
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900677 // Make sure the symbolic link is exist.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900678 EXPECT_TRUE(IsLink(file_link));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900679 EXPECT_FALSE(PathExists(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900680
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900681 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900682 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900683
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900684 // Make sure the symbolic link is deleted.
brettw@chromium.orga9154032013-12-05 05:56:49 +0900685 EXPECT_FALSE(IsLink(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900686}
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900687
688TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
689 // Create a file path.
690 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900691 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900692
693 const std::string kData("hello");
694
695 int buffer_size = kData.length();
696 char* buffer = new char[buffer_size];
697
698 // Write file.
699 EXPECT_EQ(static_cast<int>(kData.length()),
700 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900701 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900702
703 // Make sure the file is readable.
704 int32 mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900705 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
706 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900707
708 // Get rid of the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900709 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
710 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
711 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900712 // Make sure the file can't be read.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900713 EXPECT_EQ(-1, ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900714
715 // Give the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900716 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
717 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
718 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900719 // Make sure the file can be read.
720 EXPECT_EQ(static_cast<int>(kData.length()),
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900721 ReadFile(file_name, buffer, buffer_size));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900722
723 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900724 EXPECT_TRUE(DeleteFile(file_name, false));
725 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900726
727 delete[] buffer;
728}
729
730TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
731 // Create a file path.
732 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900733 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900734
735 const std::string kData("hello");
736
737 // Write file.
738 EXPECT_EQ(static_cast<int>(kData.length()),
739 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900740 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900741
742 // Make sure the file is writable.
743 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900744 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
745 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900746 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900747
748 // Get rid of the write permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900749 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
750 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
751 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900752 // Make sure the file can't be write.
753 EXPECT_EQ(-1,
754 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900755 EXPECT_FALSE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900756
757 // Give read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900758 EXPECT_TRUE(SetPosixFilePermissions(file_name,
759 FILE_PERMISSION_WRITE_BY_USER));
760 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
761 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900762 // Make sure the file can be write.
763 EXPECT_EQ(static_cast<int>(kData.length()),
764 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900765 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900766
767 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900768 EXPECT_TRUE(DeleteFile(file_name, false));
769 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900770}
771
772TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
773 // Create a directory path.
774 FilePath subdir_path =
775 temp_dir_.path().Append(FPL("PermissionTest1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900776 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900777 ASSERT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900778
779 // Create a dummy file to enumerate.
780 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900781 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900782 const std::string kData("hello");
783 EXPECT_EQ(static_cast<int>(kData.length()),
784 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900785 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900786
787 // Make sure the directory has the all permissions.
788 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900789 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
790 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900791
792 // Get rid of the permissions from the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900793 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
794 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
795 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900796
797 // Make sure the file in the directory can't be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900798 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900799 EXPECT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900800 FindResultCollector c1(f1);
801 EXPECT_EQ(c1.size(), 0);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900802 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900803
804 // Give the permissions to the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900805 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
806 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
807 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900808
809 // Make sure the file in the directory can be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900810 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900811 FindResultCollector c2(f2);
812 EXPECT_TRUE(c2.HasFile(file_name));
813 EXPECT_EQ(c2.size(), 1);
814
815 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900816 EXPECT_TRUE(DeleteFile(subdir_path, true));
817 EXPECT_FALSE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900818}
819
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900820#endif // defined(OS_POSIX)
821
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900822#if defined(OS_WIN)
823// Tests that the Delete function works for wild cards, especially
824// with the recursion flag. Also coincidentally tests PathExists.
825// TODO(erikkay): see if anyone's actually using this feature of the API
826TEST_F(FileUtilTest, DeleteWildCard) {
827 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900828 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900829 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900830 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900831
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900832 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900833 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900834 ASSERT_TRUE(PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900835
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900836 // Create the wildcard path
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900837 FilePath directory_contents = temp_dir_.path();
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900838 directory_contents = directory_contents.Append(FPL("*"));
839
initial.commit3f4a7322008-07-27 06:49:38 +0900840 // Delete non-recursively and check that only the file is deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900841 EXPECT_TRUE(DeleteFile(directory_contents, false));
842 EXPECT_FALSE(PathExists(file_name));
843 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900844
zork@chromium.org61be4f42010-05-07 09:05:36 +0900845 // Delete recursively and make sure all contents are deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900846 EXPECT_TRUE(DeleteFile(directory_contents, true));
847 EXPECT_FALSE(PathExists(file_name));
848 EXPECT_FALSE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900849}
850
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900851// TODO(erikkay): see if anyone's actually using this feature of the API
852TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
853 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900854 FilePath subdir_path =
855 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900856 CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900857 ASSERT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900858
859 // Create the wildcard path
860 FilePath directory_contents = subdir_path;
861 directory_contents = directory_contents.Append(FPL("*"));
862
863 // Delete non-recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900864 EXPECT_TRUE(DeleteFile(directory_contents, false));
865 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900866
867 // Delete recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900868 EXPECT_TRUE(DeleteFile(directory_contents, true));
869 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900870}
871#endif
872
873// Tests non-recursive Delete() for a directory.
874TEST_F(FileUtilTest, DeleteDirNonRecursive) {
875 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900876 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900877 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900878 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900879
880 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
881 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900882 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900883
884 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900885 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900886 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900887
888 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900889 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900890 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900891
892 // Delete non-recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900893 EXPECT_TRUE(DeleteFile(subdir_path2, false));
894 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900895
896 // Delete non-recursively and check that nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900897 EXPECT_FALSE(DeleteFile(test_subdir, false));
898 EXPECT_TRUE(PathExists(test_subdir));
899 EXPECT_TRUE(PathExists(file_name));
900 EXPECT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900901}
902
903// Tests recursive Delete() for a directory.
904TEST_F(FileUtilTest, DeleteDirRecursive) {
905 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900906 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900907 CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900908 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900909
910 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
911 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900912 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900913
914 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900915 CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900916 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900917
918 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900919 CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900920 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900921
922 // Delete recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900923 EXPECT_TRUE(DeleteFile(subdir_path2, true));
924 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900925
926 // Delete recursively and check that everything got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900927 EXPECT_TRUE(DeleteFile(test_subdir, true));
928 EXPECT_FALSE(PathExists(file_name));
929 EXPECT_FALSE(PathExists(subdir_path1));
930 EXPECT_FALSE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900931}
932
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900933TEST_F(FileUtilTest, MoveFileNew) {
934 // Create a file
935 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900936 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900937 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900938 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900939
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900940 // The destination.
941 FilePath file_name_to = temp_dir_.path().Append(
942 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900943 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900944
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900945 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900946
947 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900948 EXPECT_FALSE(PathExists(file_name_from));
949 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900950}
951
952TEST_F(FileUtilTest, MoveFileExists) {
953 // Create a file
954 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900955 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900956 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900957 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900958
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900959 // The destination name.
960 FilePath file_name_to = temp_dir_.path().Append(
961 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900962 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900963 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900964
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900965 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900966
967 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900968 EXPECT_FALSE(PathExists(file_name_from));
969 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900970 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
971}
972
973TEST_F(FileUtilTest, MoveFileDirExists) {
974 // Create a file
975 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900976 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900977 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900978 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900979
980 // The destination directory
981 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900982 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900983 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900984 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900985
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900986 EXPECT_FALSE(Move(file_name_from, dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900987}
988
989
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900990TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900991 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900992 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900993 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +0900994 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900995 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +0900996
997 // Create a file under the directory
cevans@chromium.org007dbe22013-02-07 05:38:07 +0900998 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
999 FilePath file_name_from = dir_name_from.Append(txt_file_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001000 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001001 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001002
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001003 // Move the directory.
1004 FilePath dir_name_to =
1005 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001006 FilePath file_name_to =
1007 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001008
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001009 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001010
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001011 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001012
1013 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001014 EXPECT_FALSE(PathExists(dir_name_from));
1015 EXPECT_FALSE(PathExists(file_name_from));
1016 EXPECT_TRUE(PathExists(dir_name_to));
1017 EXPECT_TRUE(PathExists(file_name_to));
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001018
1019 // Test path traversal.
1020 file_name_from = dir_name_to.Append(txt_file_name);
1021 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1022 file_name_to = file_name_to.Append(txt_file_name);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001023 EXPECT_FALSE(Move(file_name_from, file_name_to));
1024 EXPECT_TRUE(PathExists(file_name_from));
1025 EXPECT_FALSE(PathExists(file_name_to));
1026 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1027 EXPECT_FALSE(PathExists(file_name_from));
1028 EXPECT_TRUE(PathExists(file_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001029}
1030
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001031TEST_F(FileUtilTest, MoveExist) {
1032 // Create a directory
1033 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001034 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001035 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001036 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001037
1038 // Create a file under the directory
1039 FilePath file_name_from =
1040 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1041 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001042 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001043
1044 // Move the directory
1045 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001046 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001047
1048 FilePath dir_name_to =
1049 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1050 FilePath file_name_to =
1051 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1052
1053 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001054 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001055 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001056
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001057 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001058
1059 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001060 EXPECT_FALSE(PathExists(dir_name_from));
1061 EXPECT_FALSE(PathExists(file_name_from));
1062 EXPECT_TRUE(PathExists(dir_name_to));
1063 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001064}
1065
1066TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001067 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001068 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001069 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001070 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001071 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001072
1073 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001074 FilePath file_name_from =
1075 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001076 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001077 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001078
1079 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001080 FilePath subdir_name_from =
1081 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001082 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001083 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001084
1085 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001086 FilePath file_name2_from =
1087 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001088 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001089 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001090
1091 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001092 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001093 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001094 FilePath file_name_to =
1095 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1096 FilePath subdir_name_to =
1097 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1098 FilePath file_name2_to =
1099 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001100
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001101 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001102
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001103 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001104
1105 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001106 EXPECT_TRUE(PathExists(dir_name_from));
1107 EXPECT_TRUE(PathExists(file_name_from));
1108 EXPECT_TRUE(PathExists(subdir_name_from));
1109 EXPECT_TRUE(PathExists(file_name2_from));
1110 EXPECT_TRUE(PathExists(dir_name_to));
1111 EXPECT_TRUE(PathExists(file_name_to));
1112 EXPECT_TRUE(PathExists(subdir_name_to));
1113 EXPECT_TRUE(PathExists(file_name2_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001114}
1115
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001116TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1117 // Create a directory.
1118 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001119 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001120 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001121 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001122
1123 // Create a file under the directory.
1124 FilePath file_name_from =
1125 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1126 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001127 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001128
1129 // Create a subdirectory.
1130 FilePath subdir_name_from =
1131 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001132 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001133 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001134
1135 // Create a file under the subdirectory.
1136 FilePath file_name2_from =
1137 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1138 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001139 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001140
1141 // Copy the directory recursively.
1142 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001143 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001144
1145 FilePath dir_name_to =
1146 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1147 FilePath file_name_to =
1148 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1149 FilePath subdir_name_to =
1150 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1151 FilePath file_name2_to =
1152 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1153
1154 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001155 CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001156 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001157
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001158 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001159
1160 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001161 EXPECT_TRUE(PathExists(dir_name_from));
1162 EXPECT_TRUE(PathExists(file_name_from));
1163 EXPECT_TRUE(PathExists(subdir_name_from));
1164 EXPECT_TRUE(PathExists(file_name2_from));
1165 EXPECT_TRUE(PathExists(dir_name_to));
1166 EXPECT_TRUE(PathExists(file_name_to));
1167 EXPECT_TRUE(PathExists(subdir_name_to));
1168 EXPECT_TRUE(PathExists(file_name2_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001169}
1170
1171TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001172 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001173 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001174 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001175 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001176 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001177
1178 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001179 FilePath file_name_from =
1180 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001181 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001182 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001183
1184 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001185 FilePath subdir_name_from =
1186 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001187 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001188 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001189
1190 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001191 FilePath file_name2_from =
1192 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001193 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001194 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001195
1196 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001197 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001198 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001199 FilePath file_name_to =
1200 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1201 FilePath subdir_name_to =
1202 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001203
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001204 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001205
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001206 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001207
1208 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001209 EXPECT_TRUE(PathExists(dir_name_from));
1210 EXPECT_TRUE(PathExists(file_name_from));
1211 EXPECT_TRUE(PathExists(subdir_name_from));
1212 EXPECT_TRUE(PathExists(file_name2_from));
1213 EXPECT_TRUE(PathExists(dir_name_to));
1214 EXPECT_TRUE(PathExists(file_name_to));
1215 EXPECT_FALSE(PathExists(subdir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001216}
1217
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001218TEST_F(FileUtilTest, CopyDirectoryExists) {
1219 // Create a directory.
1220 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001221 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001222 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001223 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001224
1225 // Create a file under the directory.
1226 FilePath file_name_from =
1227 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1228 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001229 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001230
1231 // Create a subdirectory.
1232 FilePath subdir_name_from =
1233 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001234 CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001235 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001236
1237 // Create a file under the subdirectory.
1238 FilePath file_name2_from =
1239 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1240 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001241 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001242
1243 // Copy the directory not recursively.
1244 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001245 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001246 FilePath file_name_to =
1247 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1248 FilePath subdir_name_to =
1249 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1250
1251 // Create the destination directory.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001252 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001253 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001254
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001255 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001256
1257 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001258 EXPECT_TRUE(PathExists(dir_name_from));
1259 EXPECT_TRUE(PathExists(file_name_from));
1260 EXPECT_TRUE(PathExists(subdir_name_from));
1261 EXPECT_TRUE(PathExists(file_name2_from));
1262 EXPECT_TRUE(PathExists(dir_name_to));
1263 EXPECT_TRUE(PathExists(file_name_to));
1264 EXPECT_FALSE(PathExists(subdir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001265}
1266
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001267TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1268 // Create a file
1269 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001270 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001271 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001272 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001273
1274 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001275 FilePath file_name_to = temp_dir_.path().Append(
1276 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001277 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001278
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001279 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001280
1281 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001282 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001283}
1284
1285TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1286 // Create a file
1287 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001288 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001289 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001290 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001291
1292 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001293 FilePath file_name_to = temp_dir_.path().Append(
1294 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001295 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001296 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001297
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001298 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001299
1300 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001301 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001302 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1303}
1304
1305TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1306 // Create a file
1307 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001308 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001309 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001310 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001311
1312 // The destination
1313 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001314 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001315 CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001316 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001317 FilePath file_name_to =
1318 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1319
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001320 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001321
1322 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001323 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001324}
1325
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001326TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1327 // Create a directory.
1328 FilePath dir_name_from =
1329 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001330 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001331 ASSERT_TRUE(PathExists(dir_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001332
1333 // Create a file under the directory.
1334 FilePath file_name_from =
1335 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1336 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001337 ASSERT_TRUE(PathExists(file_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001338
1339 // Copy the directory recursively.
1340 FilePath dir_name_to =
1341 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1342 FilePath file_name_to =
1343 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1344
1345 // Create from path with trailing separators.
1346#if defined(OS_WIN)
1347 FilePath from_path =
1348 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1349#elif defined (OS_POSIX)
1350 FilePath from_path =
1351 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1352#endif
1353
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001354 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001355
1356 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001357 EXPECT_TRUE(PathExists(dir_name_from));
1358 EXPECT_TRUE(PathExists(file_name_from));
1359 EXPECT_TRUE(PathExists(dir_name_to));
1360 EXPECT_TRUE(PathExists(file_name_to));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001361}
1362
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001363// Sets the source file to read-only.
1364void SetReadOnly(const FilePath& path) {
1365#if defined(OS_WIN)
1366 // On Windows, it involves setting a bit.
1367 DWORD attrs = GetFileAttributes(path.value().c_str());
1368 ASSERT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1369 ASSERT_TRUE(SetFileAttributes(
1370 path.value().c_str(), attrs | FILE_ATTRIBUTE_READONLY));
1371 attrs = GetFileAttributes(path.value().c_str());
1372 // Files in the temporary directory should not be indexed ever. If this
1373 // assumption change, fix this unit test accordingly.
1374 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1375 DWORD expected = FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY;
1376 if (win::GetVersion() >= win::VERSION_VISTA)
1377 expected |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
1378 ASSERT_EQ(expected, attrs);
1379#else
1380 // On all other platforms, it involves removing the write bit.
1381 EXPECT_TRUE(SetPosixFilePermissions(path, S_IRUSR));
1382#endif
1383}
1384
1385bool IsReadOnly(const FilePath& path) {
1386#if defined(OS_WIN)
1387 DWORD attrs = GetFileAttributes(path.value().c_str());
1388 EXPECT_NE(INVALID_FILE_ATTRIBUTES, attrs);
1389 return attrs & FILE_ATTRIBUTE_READONLY;
1390#else
1391 int mode = 0;
1392 EXPECT_TRUE(GetPosixFilePermissions(path, &mode));
1393 return !(mode & S_IWUSR);
1394#endif
1395}
1396
1397TEST_F(FileUtilTest, CopyDirectoryACL) {
1398 // Create a directory.
1399 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src"));
1400 CreateDirectory(src);
1401 ASSERT_TRUE(PathExists(src));
1402
1403 // Create a file under the directory.
1404 FilePath src_file = src.Append(FILE_PATH_LITERAL("src.txt"));
1405 CreateTextFile(src_file, L"Gooooooooooooooooooooogle");
1406 SetReadOnly(src_file);
1407 ASSERT_TRUE(IsReadOnly(src_file));
1408
1409 // Copy the directory recursively.
1410 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst"));
1411 FilePath dst_file = dst.Append(FILE_PATH_LITERAL("src.txt"));
1412 EXPECT_TRUE(CopyDirectory(src, dst, true));
1413
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001414 ASSERT_FALSE(IsReadOnly(dst_file));
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001415}
1416
initial.commit3f4a7322008-07-27 06:49:38 +09001417TEST_F(FileUtilTest, CopyFile) {
1418 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001419 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001420 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001421 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001422 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001423
1424 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001425 FilePath file_name_from =
1426 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001427 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1428 CreateTextFile(file_name_from, file_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001429 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001430
1431 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001432 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001433 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001434
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001435 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001436 FilePath dest_file2(dir_name_from);
1437 dest_file2 = dest_file2.AppendASCII("..");
1438 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001439 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
1440 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from, dest_file2));
evan@chromium.org1543ad32009-08-27 05:00:14 +09001441
1442 FilePath dest_file2_test(dir_name_from);
1443 dest_file2_test = dest_file2_test.DirName();
1444 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001445
1446 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001447 EXPECT_TRUE(PathExists(file_name_from));
1448 EXPECT_TRUE(PathExists(dest_file));
initial.commit3f4a7322008-07-27 06:49:38 +09001449 const std::wstring read_contents = ReadTextFile(dest_file);
1450 EXPECT_EQ(file_contents, read_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001451 EXPECT_TRUE(PathExists(dest_file2_test));
1452 EXPECT_TRUE(PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001453}
1454
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001455TEST_F(FileUtilTest, CopyFileACL) {
1456 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1457 // this test case asserts the access control bits are meeting expectations in
1458 // CopyFileUnsafe().
1459 FilePath src = temp_dir_.path().Append(FILE_PATH_LITERAL("src.txt"));
1460 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1461 CreateTextFile(src, file_contents);
1462
1463 // Set the source file to read-only.
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001464 ASSERT_FALSE(IsReadOnly(src));
1465 SetReadOnly(src);
1466 ASSERT_TRUE(IsReadOnly(src));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001467
1468 // Copy the file.
1469 FilePath dst = temp_dir_.path().Append(FILE_PATH_LITERAL("dst.txt"));
1470 ASSERT_TRUE(CopyFile(src, dst));
1471 EXPECT_EQ(file_contents, ReadTextFile(dst));
1472
maruel@chromium.org0a177fa2014-02-06 04:55:52 +09001473 ASSERT_FALSE(IsReadOnly(dst));
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001474}
maruel@chromium.org43b615a2014-01-16 03:48:42 +09001475
erikkay@google.comf2406842008-08-21 00:59:49 +09001476// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001477// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001478typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001479
erikkay@google.comf2406842008-08-21 00:59:49 +09001480TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001481 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001482 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001483 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001484 ASSERT_TRUE(PathExists(data_dir));
initial.commit3f4a7322008-07-27 06:49:38 +09001485
evanm@google.com874d1672008-10-31 08:54:04 +09001486 FilePath original_file =
1487 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1488 FilePath same_file =
1489 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1490 FilePath same_length_file =
1491 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1492 FilePath different_file =
1493 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1494 FilePath different_first_file =
1495 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1496 FilePath different_last_file =
1497 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1498 FilePath empty1_file =
1499 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1500 FilePath empty2_file =
1501 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1502 FilePath shortened_file =
1503 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1504 FilePath binary_file =
1505 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1506 FilePath binary_file_same =
1507 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1508 FilePath binary_file_diff =
1509 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001510
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001511 EXPECT_TRUE(ContentsEqual(original_file, original_file));
1512 EXPECT_TRUE(ContentsEqual(original_file, same_file));
1513 EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
1514 EXPECT_FALSE(ContentsEqual(original_file, different_file));
1515 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1516 FilePath(FILE_PATH_LITERAL("bogusname"))));
1517 EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
1518 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1519 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1520 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1521 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1522 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1523 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
initial.commit3f4a7322008-07-27 06:49:38 +09001524}
1525
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001526TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1527 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001528 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001529 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001530 ASSERT_TRUE(PathExists(data_dir));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001531
1532 FilePath original_file =
1533 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1534 FilePath same_file =
1535 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1536 FilePath crlf_file =
1537 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1538 FilePath shortened_file =
1539 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1540 FilePath different_file =
1541 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1542 FilePath different_first_file =
1543 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1544 FilePath different_last_file =
1545 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1546 FilePath first1_file =
1547 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1548 FilePath first2_file =
1549 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1550 FilePath empty1_file =
1551 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1552 FilePath empty2_file =
1553 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1554 FilePath blank_line_file =
1555 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1556 FilePath blank_line_crlf_file =
1557 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1558
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001559 EXPECT_TRUE(TextContentsEqual(original_file, same_file));
1560 EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
1561 EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
1562 EXPECT_FALSE(TextContentsEqual(original_file, different_file));
1563 EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
1564 EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
1565 EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
1566 EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
1567 EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
1568 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001569}
1570
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001571// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001572#if defined(OS_WIN)
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001573TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1574 // Create a directory
1575 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001576 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001577 CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001578 ASSERT_TRUE(PathExists(dir_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001579
1580 // Create a file under the directory
1581 FilePath file_name_from =
1582 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1583 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001584 ASSERT_TRUE(PathExists(file_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001585
1586 // Move the directory by using CopyAndDeleteDirectory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001587 FilePath dir_name_to = temp_dir_.path().Append(
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001588 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1589 FilePath file_name_to =
1590 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1591
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001592 ASSERT_FALSE(PathExists(dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001593
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001594 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +09001595 dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001596
1597 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001598 EXPECT_FALSE(PathExists(dir_name_from));
1599 EXPECT_FALSE(PathExists(file_name_from));
1600 EXPECT_TRUE(PathExists(dir_name_to));
1601 EXPECT_TRUE(PathExists(file_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001602}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001603
1604TEST_F(FileUtilTest, GetTempDirTest) {
1605 static const TCHAR* kTmpKey = _T("TMP");
1606 static const TCHAR* kTmpValues[] = {
1607 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1608 };
1609 // Save the original $TMP.
1610 size_t original_tmp_size;
1611 TCHAR* original_tmp;
1612 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1613 // original_tmp may be NULL.
1614
1615 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1616 FilePath path;
1617 ::_tputenv_s(kTmpKey, kTmpValues[i]);
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001618 GetTempDir(&path);
tkent@chromium.org8da14162009-10-09 16:33:39 +09001619 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1620 " result=" << path.value();
1621 }
1622
1623 // Restore the original $TMP.
1624 if (original_tmp) {
1625 ::_tputenv_s(kTmpKey, original_tmp);
1626 free(original_tmp);
1627 } else {
1628 ::_tputenv_s(kTmpKey, _T(""));
1629 }
1630}
1631#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001632
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001633TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1634 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001635 for (int i = 0; i < 3; i++) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001636 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001637 EXPECT_TRUE(PathExists(temp_files[i]));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001638 EXPECT_FALSE(DirectoryExists(temp_files[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001639 }
1640 for (int i = 0; i < 3; i++)
1641 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1642 for (int i = 0; i < 3; i++)
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001643 EXPECT_TRUE(DeleteFile(temp_files[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001644}
1645
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001646TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001647 FilePath names[3];
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001648 FILE* fps[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001649 int i;
1650
1651 // Create; make sure they are open and exist.
1652 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001653 fps[i] = CreateAndOpenTemporaryFile(&(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001654 ASSERT_TRUE(fps[i]);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001655 EXPECT_TRUE(PathExists(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001656 }
1657
1658 // Make sure all names are unique.
1659 for (i = 0; i < 3; ++i) {
1660 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1661 }
1662
1663 // Close and delete.
1664 for (i = 0; i < 3; ++i) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001665 EXPECT_TRUE(CloseFile(fps[i]));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001666 EXPECT_TRUE(DeleteFile(names[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001667 }
initial.commit3f4a7322008-07-27 06:49:38 +09001668}
1669
1670TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001671 FilePath temp_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001672 ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001673 EXPECT_TRUE(PathExists(temp_dir));
1674 EXPECT_TRUE(DeleteFile(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001675}
1676
skerner@chromium.orge4432392010-05-01 02:00:09 +09001677TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1678 FilePath new_dir;
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001679 ASSERT_TRUE(CreateTemporaryDirInDir(
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001680 temp_dir_.path(),
skerner@chromium.orge4432392010-05-01 02:00:09 +09001681 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001682 &new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001683 EXPECT_TRUE(PathExists(new_dir));
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001684 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001685 EXPECT_TRUE(DeleteFile(new_dir, false));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001686}
1687
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001688TEST_F(FileUtilTest, GetShmemTempDirTest) {
1689 FilePath dir;
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001690 EXPECT_TRUE(GetShmemTempDir(false, &dir));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001691 EXPECT_TRUE(DirectoryExists(dir));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001692}
1693
initial.commit3f4a7322008-07-27 06:49:38 +09001694TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001695 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001696 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001697#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001698 FilePath test_path =
1699 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001700#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001701 FilePath test_path =
1702 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001703#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001704
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001705 EXPECT_FALSE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001706 EXPECT_TRUE(CreateDirectory(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001707 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001708 // CreateDirectory returns true if the DirectoryExists returns true.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001709 EXPECT_TRUE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001710
1711 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001712 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001713 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001714 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001715 EXPECT_TRUE(PathExists(test_path));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001716 EXPECT_FALSE(CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001717
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001718 EXPECT_TRUE(DeleteFile(test_root, true));
1719 EXPECT_FALSE(PathExists(test_root));
1720 EXPECT_FALSE(PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001721
1722 // Verify assumptions made by the Windows implementation:
1723 // 1. The current directory always exists.
1724 // 2. The root directory always exists.
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001725 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001726 FilePath top_level = test_root;
1727 while (top_level != top_level.DirName()) {
1728 top_level = top_level.DirName();
1729 }
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001730 ASSERT_TRUE(DirectoryExists(top_level));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001731
1732 // Given these assumptions hold, it should be safe to
1733 // test that "creating" these directories succeeds.
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001734 EXPECT_TRUE(CreateDirectory(
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001735 FilePath(FilePath::kCurrentDirectory)));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001736 EXPECT_TRUE(CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001737
1738#if defined(OS_WIN)
1739 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1740 FilePath invalid_path =
1741 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001742 if (!PathExists(invalid_drive)) {
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001743 EXPECT_FALSE(CreateDirectory(invalid_path));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001744 }
1745#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001746}
1747
1748TEST_F(FileUtilTest, DetectDirectoryTest) {
1749 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001750 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001751 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001752 EXPECT_FALSE(PathExists(test_root));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001753 EXPECT_TRUE(CreateDirectory(test_root));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001754 EXPECT_TRUE(PathExists(test_root));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001755 EXPECT_TRUE(DirectoryExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001756 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001757 FilePath test_path =
1758 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001759 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001760 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001761 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001762 EXPECT_FALSE(DirectoryExists(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001763 EXPECT_TRUE(DeleteFile(test_path, false));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001764
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001765 EXPECT_TRUE(DeleteFile(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001766}
1767
initial.commit3f4a7322008-07-27 06:49:38 +09001768TEST_F(FileUtilTest, FileEnumeratorTest) {
1769 // Test an empty directory.
brettw@chromium.org56946722013-06-08 13:53:36 +09001770 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001771 EXPECT_EQ(f0.Next().value(), FPL(""));
1772 EXPECT_EQ(f0.Next().value(), FPL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001773
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001774 // Test an empty directory, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001775 FileEnumerator f0_dotdot(temp_dir_.path(), false,
1776 FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001777 EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001778 f0_dotdot.Next().value());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001779 EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001780
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001781 // create the directories
rvargas@chromium.org56472942013-08-15 05:46:05 +09001782 FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001783 EXPECT_TRUE(CreateDirectory(dir1));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001784 FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001785 EXPECT_TRUE(CreateDirectory(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001786 FilePath dir2inner = dir2.Append(FPL("inner"));
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001787 EXPECT_TRUE(CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001788
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001789 // create the files
rvargas@chromium.org56472942013-08-15 05:46:05 +09001790 FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001791 CreateTextFile(dir2file, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001792 FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001793 CreateTextFile(dir2innerfile, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001794 FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001795 CreateTextFile(file1, std::wstring());
1796 FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
rvargas@chromium.org56472942013-08-15 05:46:05 +09001797 .Append(FPL("file2.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001798 CreateTextFile(file2_rel, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001799 FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001800
1801 // Only enumerate files.
brettw@chromium.org56946722013-06-08 13:53:36 +09001802 FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
initial.commit3f4a7322008-07-27 06:49:38 +09001803 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001804 EXPECT_TRUE(c1.HasFile(file1));
1805 EXPECT_TRUE(c1.HasFile(file2_abs));
1806 EXPECT_TRUE(c1.HasFile(dir2file));
1807 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1808 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001809
1810 // Only enumerate directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001811 FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001812 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001813 EXPECT_TRUE(c2.HasFile(dir1));
1814 EXPECT_TRUE(c2.HasFile(dir2));
1815 EXPECT_TRUE(c2.HasFile(dir2inner));
1816 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001817
tim@chromium.org989d0972008-10-16 11:42:45 +09001818 // Only enumerate directories non-recursively.
brettw@chromium.org56946722013-06-08 13:53:36 +09001819 FileEnumerator f2_non_recursive(
1820 temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001821 FindResultCollector c2_non_recursive(f2_non_recursive);
1822 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1823 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1824 EXPECT_EQ(c2_non_recursive.size(), 2);
1825
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001826 // Only enumerate directories, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001827 FileEnumerator f2_dotdot(temp_dir_.path(), false,
1828 FileEnumerator::DIRECTORIES |
1829 FileEnumerator::INCLUDE_DOT_DOT);
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001830 FindResultCollector c2_dotdot(f2_dotdot);
1831 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1832 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001833 EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001834 EXPECT_EQ(c2_dotdot.size(), 3);
1835
initial.commit3f4a7322008-07-27 06:49:38 +09001836 // Enumerate files and directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001837 FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001838 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001839 EXPECT_TRUE(c3.HasFile(dir1));
1840 EXPECT_TRUE(c3.HasFile(dir2));
1841 EXPECT_TRUE(c3.HasFile(file1));
1842 EXPECT_TRUE(c3.HasFile(file2_abs));
1843 EXPECT_TRUE(c3.HasFile(dir2file));
1844 EXPECT_TRUE(c3.HasFile(dir2inner));
1845 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1846 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001847
1848 // Non-recursive operation.
brettw@chromium.org56946722013-06-08 13:53:36 +09001849 FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001850 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001851 EXPECT_TRUE(c4.HasFile(dir2));
1852 EXPECT_TRUE(c4.HasFile(dir2));
1853 EXPECT_TRUE(c4.HasFile(file1));
1854 EXPECT_TRUE(c4.HasFile(file2_abs));
1855 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001856
1857 // Enumerate with a pattern.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001858 FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001859 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001860 EXPECT_TRUE(c5.HasFile(dir1));
1861 EXPECT_TRUE(c5.HasFile(dir2));
1862 EXPECT_TRUE(c5.HasFile(dir2file));
1863 EXPECT_TRUE(c5.HasFile(dir2inner));
1864 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1865 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001866
rvargas@chromium.org56472942013-08-15 05:46:05 +09001867#if defined(OS_WIN)
1868 {
1869 // Make dir1 point to dir2.
1870 ReparsePoint reparse_point(dir1, dir2);
1871 EXPECT_TRUE(reparse_point.IsValid());
1872
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001873 if ((win::GetVersion() >= win::VERSION_VISTA)) {
rvargas@chromium.org56472942013-08-15 05:46:05 +09001874 // There can be a delay for the enumeration code to see the change on
1875 // the file system so skip this test for XP.
1876 // Enumerate the reparse point.
1877 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
1878 FindResultCollector c6(f6);
1879 FilePath inner2 = dir1.Append(FPL("inner"));
1880 EXPECT_TRUE(c6.HasFile(inner2));
1881 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1882 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
1883 EXPECT_EQ(c6.size(), 3);
1884 }
1885
1886 // No changes for non recursive operation.
1887 FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
1888 FindResultCollector c7(f7);
1889 EXPECT_TRUE(c7.HasFile(dir2));
1890 EXPECT_TRUE(c7.HasFile(dir2));
1891 EXPECT_TRUE(c7.HasFile(file1));
1892 EXPECT_TRUE(c7.HasFile(file2_abs));
1893 EXPECT_EQ(c7.size(), 4);
1894
1895 // Should not enumerate inside dir1 when using recursion.
1896 FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1897 FindResultCollector c8(f8);
1898 EXPECT_TRUE(c8.HasFile(dir1));
1899 EXPECT_TRUE(c8.HasFile(dir2));
1900 EXPECT_TRUE(c8.HasFile(file1));
1901 EXPECT_TRUE(c8.HasFile(file2_abs));
1902 EXPECT_TRUE(c8.HasFile(dir2file));
1903 EXPECT_TRUE(c8.HasFile(dir2inner));
1904 EXPECT_TRUE(c8.HasFile(dir2innerfile));
1905 EXPECT_EQ(c8.size(), 7);
1906 }
1907#endif
1908
initial.commit3f4a7322008-07-27 06:49:38 +09001909 // Make sure the destructor closes the find handle while in the middle of a
1910 // query to allow TearDown to delete the directory.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001911 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1912 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
avi@google.com5cb79352008-12-11 23:55:12 +09001913 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001914}
license.botf003cfe2008-08-24 09:55:55 +09001915
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001916TEST_F(FileUtilTest, AppendToFile) {
1917 FilePath data_dir =
1918 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1919
1920 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001921 if (PathExists(data_dir)) {
1922 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001923 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001924 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001925
1926 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001927 if (PathExists(data_dir)) {
1928 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001929 }
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09001930 ASSERT_TRUE(CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001931 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1932
1933 std::string data("hello");
1934 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length()));
1935 EXPECT_EQ(static_cast<int>(data.length()),
1936 file_util::WriteFile(foobar, data.c_str(), data.length()));
1937 EXPECT_EQ(static_cast<int>(data.length()),
1938 file_util::AppendToFile(foobar, data.c_str(), data.length()));
1939
1940 const std::wstring read_content = ReadTextFile(foobar);
1941 EXPECT_EQ(L"hellohello", read_content);
1942}
1943
kaliamoorthi@chromium.org8d3c40d2014-02-19 01:31:51 +09001944TEST_F(FileUtilTest, ReadFileToString) {
1945 const char kTestData[] = "0123";
1946 std::string data;
1947
1948 FilePath file_path =
1949 temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
1950
1951 ASSERT_EQ(4, file_util::WriteFile(file_path, kTestData, 4));
1952
1953 EXPECT_TRUE(ReadFileToString(file_path, &data));
1954 EXPECT_EQ(kTestData, data);
1955
1956 data = "temp";
1957 EXPECT_FALSE(ReadFileToString(file_path, &data, 0));
1958 EXPECT_EQ(data.length(), 0u);
1959
1960 data = "temp";
1961 EXPECT_FALSE(ReadFileToString(file_path, &data, 2));
1962 EXPECT_EQ("01", data);
1963
1964 data.clear();
1965 EXPECT_FALSE(ReadFileToString(file_path, &data, 3));
1966 EXPECT_EQ("012", data);
1967
1968 data.clear();
1969 EXPECT_TRUE(ReadFileToString(file_path, &data, 4));
1970 EXPECT_EQ("0123", data);
1971
1972 data.clear();
1973 EXPECT_TRUE(ReadFileToString(file_path, &data, 6));
1974 EXPECT_EQ("0123", data);
1975
1976 EXPECT_TRUE(ReadFileToString(file_path, NULL, 6));
1977
1978 EXPECT_TRUE(ReadFileToString(file_path, NULL));
1979
1980 EXPECT_TRUE(base::DeleteFile(file_path, false));
1981
1982 EXPECT_FALSE(ReadFileToString(file_path, &data));
1983 EXPECT_EQ(data.length(), 0u);
1984
1985 EXPECT_FALSE(ReadFileToString(file_path, &data, 6));
1986 EXPECT_EQ(data.length(), 0u);
1987}
1988
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001989TEST_F(FileUtilTest, TouchFile) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001990 FilePath data_dir =
1991 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
jochen@chromium.orga6879772010-02-18 19:02:26 +09001992
1993 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001994 if (PathExists(data_dir)) {
1995 ASSERT_TRUE(DeleteFile(data_dir, true));
jochen@chromium.orga6879772010-02-18 19:02:26 +09001996 }
brettw@chromium.org458d1e32013-12-05 07:49:00 +09001997 ASSERT_TRUE(CreateDirectory(data_dir));
jochen@chromium.orga6879772010-02-18 19:02:26 +09001998
1999 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
2000 std::string data("hello");
2001 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
2002
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002003 Time access_time;
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002004 // This timestamp is divisible by one day (in local timezone),
2005 // to make it work on FAT too.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002006 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002007 &access_time));
2008
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002009 Time modification_time;
jochen@chromium.orga6879772010-02-18 19:02:26 +09002010 // Note that this timestamp is divisible by two (seconds) - FAT stores
2011 // modification times with 2s resolution.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002012 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
jochen@chromium.orga6879772010-02-18 19:02:26 +09002013 &modification_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002014
brettw@chromium.org458d1e32013-12-05 07:49:00 +09002015 ASSERT_TRUE(TouchFile(foobar, access_time, modification_time));
rvargas@chromium.orgb005b382014-01-07 19:06:58 +09002016 File::Info file_info;
brettw@chromium.orga9154032013-12-05 05:56:49 +09002017 ASSERT_TRUE(GetFileInfo(foobar, &file_info));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09002018 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
2019 access_time.ToInternalValue());
2020 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
2021 modification_time.ToInternalValue());
jochen@chromium.orga6879772010-02-18 19:02:26 +09002022}
2023
tfarina@chromium.org34828222010-05-26 10:40:12 +09002024TEST_F(FileUtilTest, IsDirectoryEmpty) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09002025 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002026
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002027 ASSERT_FALSE(PathExists(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002028
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002029 ASSERT_TRUE(CreateDirectory(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002030
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002031 EXPECT_TRUE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002032
2033 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
2034 std::string bar("baz");
2035 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
2036
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002037 EXPECT_FALSE(IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09002038}
2039
skerner@google.com93449ef2011-09-22 23:47:18 +09002040#if defined(OS_POSIX)
2041
2042// Testing VerifyPathControlledByAdmin() is hard, because there is no
2043// way a test can make a file owned by root, or change file paths
2044// at the root of the file system. VerifyPathControlledByAdmin()
2045// is implemented as a call to VerifyPathControlledByUser, which gives
2046// us the ability to test with paths under the test's temp directory,
2047// using a user id we control.
2048// Pull tests of VerifyPathControlledByUserTest() into a separate test class
2049// with a common SetUp() method.
2050class VerifyPathControlledByUserTest : public FileUtilTest {
2051 protected:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +09002052 virtual void SetUp() OVERRIDE {
skerner@google.com93449ef2011-09-22 23:47:18 +09002053 FileUtilTest::SetUp();
2054
2055 // Create a basic structure used by each test.
2056 // base_dir_
2057 // |-> sub_dir_
2058 // |-> text_file_
2059
2060 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002061 ASSERT_TRUE(CreateDirectory(base_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002062
2063 sub_dir_ = base_dir_.AppendASCII("sub_dir");
thakis@chromium.orgf01de7e2013-12-09 06:43:30 +09002064 ASSERT_TRUE(CreateDirectory(sub_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002065
2066 text_file_ = sub_dir_.AppendASCII("file.txt");
2067 CreateTextFile(text_file_, L"This text file has some text in it.");
2068
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002069 // Get the user and group files are created with from |base_dir_|.
2070 struct stat stat_buf;
2071 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
2072 uid_ = stat_buf.st_uid;
skerner@chromium.org80784142011-10-18 06:30:29 +09002073 ok_gids_.insert(stat_buf.st_gid);
2074 bad_gids_.insert(stat_buf.st_gid + 1);
2075
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002076 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
skerner@google.com93449ef2011-09-22 23:47:18 +09002077
2078 // To ensure that umask settings do not cause the initial state
2079 // of permissions to be different from what we expect, explicitly
2080 // set permissions on the directories we create.
2081 // Make all files and directories non-world-writable.
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002082
2083 // Users and group can read, write, traverse
2084 int enabled_permissions =
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002085 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09002086 // Other users can't read, write, traverse
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002087 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
skerner@google.com93449ef2011-09-22 23:47:18 +09002088
2089 ASSERT_NO_FATAL_FAILURE(
2090 ChangePosixFilePermissions(
2091 base_dir_, enabled_permissions, disabled_permissions));
2092 ASSERT_NO_FATAL_FAILURE(
2093 ChangePosixFilePermissions(
2094 sub_dir_, enabled_permissions, disabled_permissions));
2095 }
2096
2097 FilePath base_dir_;
2098 FilePath sub_dir_;
2099 FilePath text_file_;
2100 uid_t uid_;
skerner@chromium.org80784142011-10-18 06:30:29 +09002101
2102 std::set<gid_t> ok_gids_;
2103 std::set<gid_t> bad_gids_;
skerner@google.com93449ef2011-09-22 23:47:18 +09002104};
2105
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002106TEST_F(VerifyPathControlledByUserTest, BadPaths) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002107 // File does not exist.
2108 FilePath does_not_exist = base_dir_.AppendASCII("does")
2109 .AppendASCII("not")
2110 .AppendASCII("exist");
skerner@google.com93449ef2011-09-22 23:47:18 +09002111 EXPECT_FALSE(
2112 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002113 base_dir_, does_not_exist, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002114
2115 // |base| not a subpath of |path|.
2116 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002117 file_util::VerifyPathControlledByUser(
2118 sub_dir_, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002119
2120 // An empty base path will fail to be a prefix for any path.
2121 FilePath empty;
2122 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002123 file_util::VerifyPathControlledByUser(
2124 empty, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002125
2126 // Finding that a bad call fails proves nothing unless a good call succeeds.
2127 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002128 file_util::VerifyPathControlledByUser(
2129 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002130}
2131
2132TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2133 // Symlinks in the path should cause failure.
2134
2135 // Symlink to the file at the end of the path.
2136 FilePath file_link = base_dir_.AppendASCII("file_link");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002137 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
skerner@google.com93449ef2011-09-22 23:47:18 +09002138 << "Failed to create symlink.";
2139
2140 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002141 file_util::VerifyPathControlledByUser(
2142 base_dir_, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002143 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002144 file_util::VerifyPathControlledByUser(
2145 file_link, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002146
2147 // Symlink from one directory to another within the path.
2148 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002149 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
skerner@google.com93449ef2011-09-22 23:47:18 +09002150 << "Failed to create symlink.";
2151
2152 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002153 ASSERT_TRUE(PathExists(file_path_with_link));
skerner@google.com93449ef2011-09-22 23:47:18 +09002154
2155 EXPECT_FALSE(
2156 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002157 base_dir_, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002158
2159 EXPECT_FALSE(
2160 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002161 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002162
2163 // Symlinks in parents of base path are allowed.
2164 EXPECT_TRUE(
2165 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002166 file_path_with_link, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002167}
2168
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002169TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002170 // Get a uid that is not the uid of files we create.
2171 uid_t bad_uid = uid_ + 1;
2172
skerner@google.com93449ef2011-09-22 23:47:18 +09002173 // Make all files and directories non-world-writable.
2174 ASSERT_NO_FATAL_FAILURE(
2175 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2176 ASSERT_NO_FATAL_FAILURE(
2177 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2178 ASSERT_NO_FATAL_FAILURE(
2179 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2180
2181 // We control these paths.
2182 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002183 file_util::VerifyPathControlledByUser(
2184 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002185 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002186 file_util::VerifyPathControlledByUser(
2187 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002188 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002189 file_util::VerifyPathControlledByUser(
2190 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002191
2192 // Another user does not control these paths.
2193 EXPECT_FALSE(
2194 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002195 base_dir_, sub_dir_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002196 EXPECT_FALSE(
2197 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002198 base_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002199 EXPECT_FALSE(
2200 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002201 sub_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002202
2203 // Another group does not control the paths.
2204 EXPECT_FALSE(
2205 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002206 base_dir_, sub_dir_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002207 EXPECT_FALSE(
2208 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002209 base_dir_, text_file_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002210 EXPECT_FALSE(
2211 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002212 sub_dir_, text_file_, uid_, bad_gids_));
2213}
2214
2215TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2216 // Make all files and directories writable only by their owner.
2217 ASSERT_NO_FATAL_FAILURE(
2218 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2219 ASSERT_NO_FATAL_FAILURE(
2220 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2221 ASSERT_NO_FATAL_FAILURE(
2222 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2223
2224 // Any group is okay because the path is not group-writable.
2225 EXPECT_TRUE(
2226 file_util::VerifyPathControlledByUser(
2227 base_dir_, sub_dir_, uid_, ok_gids_));
2228 EXPECT_TRUE(
2229 file_util::VerifyPathControlledByUser(
2230 base_dir_, text_file_, uid_, ok_gids_));
2231 EXPECT_TRUE(
2232 file_util::VerifyPathControlledByUser(
2233 sub_dir_, text_file_, uid_, ok_gids_));
2234
2235 EXPECT_TRUE(
2236 file_util::VerifyPathControlledByUser(
2237 base_dir_, sub_dir_, uid_, bad_gids_));
2238 EXPECT_TRUE(
2239 file_util::VerifyPathControlledByUser(
2240 base_dir_, text_file_, uid_, bad_gids_));
2241 EXPECT_TRUE(
2242 file_util::VerifyPathControlledByUser(
2243 sub_dir_, text_file_, uid_, bad_gids_));
2244
2245 // No group is okay, because we don't check the group
2246 // if no group can write.
2247 std::set<gid_t> no_gids; // Empty set of gids.
2248 EXPECT_TRUE(
2249 file_util::VerifyPathControlledByUser(
2250 base_dir_, sub_dir_, uid_, no_gids));
2251 EXPECT_TRUE(
2252 file_util::VerifyPathControlledByUser(
2253 base_dir_, text_file_, uid_, no_gids));
2254 EXPECT_TRUE(
2255 file_util::VerifyPathControlledByUser(
2256 sub_dir_, text_file_, uid_, no_gids));
2257
2258
2259 // Make all files and directories writable by their group.
2260 ASSERT_NO_FATAL_FAILURE(
2261 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2262 ASSERT_NO_FATAL_FAILURE(
2263 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2264 ASSERT_NO_FATAL_FAILURE(
2265 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2266
2267 // Now |ok_gids_| works, but |bad_gids_| fails.
2268 EXPECT_TRUE(
2269 file_util::VerifyPathControlledByUser(
2270 base_dir_, sub_dir_, uid_, ok_gids_));
2271 EXPECT_TRUE(
2272 file_util::VerifyPathControlledByUser(
2273 base_dir_, text_file_, uid_, ok_gids_));
2274 EXPECT_TRUE(
2275 file_util::VerifyPathControlledByUser(
2276 sub_dir_, text_file_, uid_, ok_gids_));
2277
2278 EXPECT_FALSE(
2279 file_util::VerifyPathControlledByUser(
2280 base_dir_, sub_dir_, uid_, bad_gids_));
2281 EXPECT_FALSE(
2282 file_util::VerifyPathControlledByUser(
2283 base_dir_, text_file_, uid_, bad_gids_));
2284 EXPECT_FALSE(
2285 file_util::VerifyPathControlledByUser(
2286 sub_dir_, text_file_, uid_, bad_gids_));
2287
2288 // Because any group in the group set is allowed,
2289 // the union of good and bad gids passes.
2290
2291 std::set<gid_t> multiple_gids;
2292 std::set_union(
2293 ok_gids_.begin(), ok_gids_.end(),
2294 bad_gids_.begin(), bad_gids_.end(),
2295 std::inserter(multiple_gids, multiple_gids.begin()));
2296
2297 EXPECT_TRUE(
2298 file_util::VerifyPathControlledByUser(
2299 base_dir_, sub_dir_, uid_, multiple_gids));
2300 EXPECT_TRUE(
2301 file_util::VerifyPathControlledByUser(
2302 base_dir_, text_file_, uid_, multiple_gids));
2303 EXPECT_TRUE(
2304 file_util::VerifyPathControlledByUser(
2305 sub_dir_, text_file_, uid_, multiple_gids));
skerner@google.com93449ef2011-09-22 23:47:18 +09002306}
2307
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002308TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002309 // Make all files and directories non-world-writable.
2310 ASSERT_NO_FATAL_FAILURE(
2311 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2312 ASSERT_NO_FATAL_FAILURE(
2313 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2314 ASSERT_NO_FATAL_FAILURE(
2315 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2316
2317 // Initialy, we control all parts of the path.
2318 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002319 file_util::VerifyPathControlledByUser(
2320 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002321 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002322 file_util::VerifyPathControlledByUser(
2323 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002324 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002325 file_util::VerifyPathControlledByUser(
2326 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002327
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09002328 // Make base_dir_ world-writable.
skerner@google.com93449ef2011-09-22 23:47:18 +09002329 ASSERT_NO_FATAL_FAILURE(
2330 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2331 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002332 file_util::VerifyPathControlledByUser(
2333 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002334 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002335 file_util::VerifyPathControlledByUser(
2336 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002337 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002338 file_util::VerifyPathControlledByUser(
2339 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002340
2341 // Make sub_dir_ world writable.
2342 ASSERT_NO_FATAL_FAILURE(
2343 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2344 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002345 file_util::VerifyPathControlledByUser(
2346 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002347 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002348 file_util::VerifyPathControlledByUser(
2349 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002350 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002351 file_util::VerifyPathControlledByUser(
2352 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002353
2354 // Make text_file_ world writable.
2355 ASSERT_NO_FATAL_FAILURE(
2356 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2357 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002358 file_util::VerifyPathControlledByUser(
2359 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002360 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002361 file_util::VerifyPathControlledByUser(
2362 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002363 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002364 file_util::VerifyPathControlledByUser(
2365 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002366
2367 // Make sub_dir_ non-world writable.
2368 ASSERT_NO_FATAL_FAILURE(
2369 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2370 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002371 file_util::VerifyPathControlledByUser(
2372 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002373 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002374 file_util::VerifyPathControlledByUser(
2375 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002376 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002377 file_util::VerifyPathControlledByUser(
2378 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002379
2380 // Make base_dir_ non-world-writable.
2381 ASSERT_NO_FATAL_FAILURE(
2382 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2383 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002384 file_util::VerifyPathControlledByUser(
2385 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002386 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002387 file_util::VerifyPathControlledByUser(
2388 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002389 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002390 file_util::VerifyPathControlledByUser(
2391 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002392
2393 // Back to the initial state: Nothing is writable, so every path
2394 // should pass.
2395 ASSERT_NO_FATAL_FAILURE(
2396 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2397 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002398 file_util::VerifyPathControlledByUser(
2399 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002400 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002401 file_util::VerifyPathControlledByUser(
2402 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002403 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002404 file_util::VerifyPathControlledByUser(
2405 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002406}
2407
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002408#if defined(OS_ANDROID)
2409TEST_F(FileUtilTest, ValidContentUriTest) {
2410 // Get the test image path.
2411 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002412 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002413 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002414 ASSERT_TRUE(PathExists(data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002415 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2416 int64 image_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002417 GetFileSize(image_file, &image_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002418 EXPECT_LT(0, image_size);
2419
2420 // Insert the image into MediaStore. MediaStore will do some conversions, and
2421 // return the content URI.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002422 FilePath path = file_util::InsertImageIntoMediaStore(image_file);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002423 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002424 EXPECT_TRUE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002425 // The file size may not equal to the input image as MediaStore may convert
2426 // the image.
2427 int64 content_uri_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002428 GetFileSize(path, &content_uri_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002429 EXPECT_EQ(image_size, content_uri_size);
2430
2431 // We should be able to read the file.
2432 char* buffer = new char[image_size];
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002433 int fd = OpenContentUriForRead(path);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002434 EXPECT_LT(0, fd);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002435 EXPECT_TRUE(ReadFromFD(fd, buffer, image_size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002436 delete[] buffer;
2437}
2438
2439TEST_F(FileUtilTest, NonExistentContentUriTest) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002440 FilePath path("content://foo.bar");
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002441 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002442 EXPECT_FALSE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002443 // Size should be smaller than 0.
2444 int64 size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002445 EXPECT_FALSE(GetFileSize(path, &size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002446
2447 // We should not be able to read the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002448 int fd = OpenContentUriForRead(path);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002449 EXPECT_EQ(-1, fd);
2450}
2451#endif
2452
skerner@google.com93449ef2011-09-22 23:47:18 +09002453#endif // defined(OS_POSIX)
2454
mark@chromium.org17684802008-09-10 09:16:28 +09002455} // namespace
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002456
2457} // namespace base