blob: 0460c6ec9e18b854fe68bd968aa6173361e1f95f [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"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900257 base::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"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900266 base::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"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900281 base::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"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900321 ASSERT_TRUE(base::CreateDirectory(base_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900322
323 FilePath sub_a = base_a.Append(FPL("sub_a"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900324 ASSERT_TRUE(base::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();
brettw@chromium.org738669a2013-12-04 05:08:54 +0900352 ASSERT_TRUE(base::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"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900356 ASSERT_TRUE(base::CreateDirectory(base_b));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900357
358 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900359 ASSERT_TRUE(base::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"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900366 ASSERT_TRUE(base::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"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900371 ASSERT_TRUE(base::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.
437 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path,
438 &win32_path));
439 ASSERT_EQ(real_drive_letter, win32_path.value());
440
441 // Add some directories to the path. Expect those extra path componenets
442 // to be preserved.
443 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
444 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
445 actual_device_path.Append(kRelativePath),
446 &win32_path));
447 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
448 win32_path.value());
449
450 // Deform the real path so that it is invalid by removing the last four
451 // characters. The way windows names devices that are hard disks
452 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
453 // than three characters. The only way the truncated string could be a
454 // real drive is if more than 10^3 disks are mounted:
455 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
456 // Check that DevicePathToDriveLetterPath fails.
457 int path_length = actual_device_path.value().length();
458 int new_length = path_length - 4;
459 ASSERT_LT(0, new_length);
460 FilePath prefix_of_real_device_path(
461 actual_device_path.value().substr(0, new_length));
462 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
463 prefix_of_real_device_path,
464 &win32_path));
465
466 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
467 prefix_of_real_device_path.Append(kRelativePath),
468 &win32_path));
469
470 // Deform the real path so that it is invalid by adding some characters. For
471 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
472 // request for the drive letter whose native path is
473 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
474 // because drives are numbered in order and mounting 112345 hard disks will
475 // never happen.
476 const FilePath::StringType kExtraChars = FPL("12345");
477
478 FilePath real_device_path_plus_numbers(
479 actual_device_path.value() + kExtraChars);
480
481 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
482 real_device_path_plus_numbers,
483 &win32_path));
484
485 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
486 real_device_path_plus_numbers.Append(kRelativePath),
487 &win32_path));
488}
489
cpu@chromium.orge6490ed2011-12-30 07:59:22 +0900490TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
491 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900492 ASSERT_TRUE(base::CreateDirectory(empty_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900493 win::ScopedHandle dir(
cpu@chromium.orge6490ed2011-12-30 07:59:22 +0900494 ::CreateFile(empty_dir.value().c_str(),
495 FILE_ALL_ACCESS,
496 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
497 NULL,
498 OPEN_EXISTING,
499 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
500 NULL));
501 ASSERT_TRUE(dir.IsValid());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900502 PlatformFileInfo info;
503 EXPECT_TRUE(GetPlatformFileInfo(dir.Get(), &info));
cpu@chromium.orge6490ed2011-12-30 07:59:22 +0900504 EXPECT_TRUE(info.is_directory);
505 EXPECT_FALSE(info.is_symbolic_link);
506 EXPECT_EQ(0, info.size);
507}
508
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900509TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
510 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
511 // if it is available. This test requires that:
512 // - the filesystem at |temp_dir_| supports long filenames.
513 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
514 // directories of |temp_dir_|.
515 const FilePath::CharType kLongDirName[] = FPL("A long path");
516 const FilePath::CharType kTestSubDirName[] = FPL("test");
517 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
brettw@chromium.org738669a2013-12-04 05:08:54 +0900518 ASSERT_TRUE(base::CreateDirectory(long_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900519
520 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
521 // different short name.
522 WCHAR path_buffer[MAX_PATH];
523 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
524 path_buffer, MAX_PATH);
525 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
526 ASSERT_NE(DWORD(0), path_buffer_length);
527 FilePath short_test_dir(path_buffer);
528 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
529
530 FilePath temp_file;
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900531 ASSERT_TRUE(base::CreateTemporaryFileInDir(short_test_dir, &temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900532 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900533 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900534
535 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
536 // unreadable. We should still be able to create a temp file in the
537 // subdirectory, but we won't be able to determine the long path for it. This
538 // mimics the environment that some users run where their user profiles reside
539 // in a location where the don't have full access to the higher level
540 // directories. (Note that this assumption is true for NTFS, but not for some
541 // network file systems. E.g. AFS).
542 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
brettw@chromium.org738669a2013-12-04 05:08:54 +0900543 ASSERT_TRUE(base::CreateDirectory(access_test_dir));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900544 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
545 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
546
547 // Use the short form of the directory to create a temporary filename.
brettw@chromium.org735d11d2013-12-04 02:55:52 +0900548 ASSERT_TRUE(base::CreateTemporaryFileInDir(
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900549 short_test_dir.Append(kTestSubDirName), &temp_file));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900550 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900551 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
552
553 // Check that the long path can't be determined for |temp_file|.
554 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
555 path_buffer, MAX_PATH);
556 EXPECT_EQ(DWORD(0), path_buffer_length);
557}
558
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900559#endif // defined(OS_WIN)
560
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900561#if defined(OS_POSIX)
562
563TEST_F(FileUtilTest, CreateAndReadSymlinks) {
564 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
565 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
566 CreateTextFile(link_to, bogus_content);
567
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900568 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900569 << "Failed to create file symlink.";
570
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900571 // If we created the link properly, we should be able to read the contents
572 // through it.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900573 std::wstring contents = ReadTextFile(link_from);
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900574 EXPECT_EQ(bogus_content, contents);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900575
576 FilePath result;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900577 ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900578 EXPECT_EQ(link_to.value(), result.value());
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900579
580 // Link to a directory.
581 link_from = temp_dir_.path().Append(FPL("from_dir"));
582 link_to = temp_dir_.path().Append(FPL("to_dir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900583 ASSERT_TRUE(base::CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900584 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900585 << "Failed to create directory symlink.";
586
587 // Test failures.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900588 EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
589 EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900590 FilePath missing = temp_dir_.path().Append(FPL("missing"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900591 EXPECT_FALSE(ReadSymbolicLink(missing, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900592}
593
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900594// The following test of NormalizeFilePath() require that we create a symlink.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900595// This can not be done on Windows before Vista. On Vista, creating a symlink
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900596// requires privilege "SeCreateSymbolicLinkPrivilege".
597// TODO(skerner): Investigate the possibility of giving base_unittests the
598// privileges required to create a symlink.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900599TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
skerner@chromium.org559baa92010-05-13 00:13:57 +0900600 // Link one file to another.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900601 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
602 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900603 CreateTextFile(link_to, bogus_content);
604
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900605 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900606 << "Failed to create file symlink.";
607
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900608 // Check that NormalizeFilePath sees the link.
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900609 FilePath normalized_path;
brettw@chromium.org70684242013-12-05 03:22:49 +0900610 ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900611 EXPECT_NE(link_from, link_to);
612 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
613 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900614
615 // Link to a directory.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900616 link_from = temp_dir_.path().Append(FPL("from_dir"));
617 link_to = temp_dir_.path().Append(FPL("to_dir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900618 ASSERT_TRUE(base::CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900619 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900620 << "Failed to create directory symlink.";
621
brettw@chromium.org70684242013-12-05 03:22:49 +0900622 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900623 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900624
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900625 // Test that a loop in the links causes NormalizeFilePath() to return false.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900626 link_from = temp_dir_.path().Append(FPL("link_a"));
627 link_to = temp_dir_.path().Append(FPL("link_b"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900628 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900629 << "Failed to create loop symlink a.";
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900630 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900631 << "Failed to create loop symlink b.";
632
633 // Infinite loop!
brettw@chromium.org70684242013-12-05 03:22:49 +0900634 EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900635}
636#endif // defined(OS_POSIX)
637
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900638TEST_F(FileUtilTest, DeleteNonExistent) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900639 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900640 ASSERT_FALSE(PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900641
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900642 EXPECT_TRUE(DeleteFile(non_existent, false));
643 ASSERT_FALSE(PathExists(non_existent));
644 EXPECT_TRUE(DeleteFile(non_existent, true));
645 ASSERT_FALSE(PathExists(non_existent));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900646}
647
648TEST_F(FileUtilTest, DeleteFile) {
649 // Create a file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900650 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900651 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900652 ASSERT_TRUE(PathExists(file_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900653
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900654 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900655 EXPECT_TRUE(DeleteFile(file_name, false));
656 EXPECT_FALSE(PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900657
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900658 // Test recursive case, create a new file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900659 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900660 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900661 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900662
663 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900664 EXPECT_TRUE(DeleteFile(file_name, true));
665 EXPECT_FALSE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900666}
667
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900668#if defined(OS_POSIX)
669TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900670 // Create a file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900671 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
672 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900673 ASSERT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900674
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900675 // Create a symlink to the file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900676 FilePath file_link = temp_dir_.path().Append("file_link_2");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900677 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900678 << "Failed to create symlink.";
679
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900680 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900681 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900682
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900683 // Make sure original file is not deleted.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900684 EXPECT_FALSE(PathExists(file_link));
685 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900686}
687
688TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900689 // Create a non-existent file path.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900690 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900691 EXPECT_FALSE(PathExists(non_existent));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900692
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900693 // Create a symlink to the non-existent file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900694 FilePath file_link = temp_dir_.path().Append("file_link_3");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900695 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900696 << "Failed to create symlink.";
697
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900698 // Make sure the symbolic link is exist.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900699 EXPECT_TRUE(file_util::IsLink(file_link));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900700 EXPECT_FALSE(PathExists(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900701
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900702 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900703 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900704
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900705 // Make sure the symbolic link is deleted.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900706 EXPECT_FALSE(file_util::IsLink(file_link));
707}
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900708
709TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
710 // Create a file path.
711 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900712 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900713
714 const std::string kData("hello");
715
716 int buffer_size = kData.length();
717 char* buffer = new char[buffer_size];
718
719 // Write file.
720 EXPECT_EQ(static_cast<int>(kData.length()),
721 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900722 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900723
724 // Make sure the file is readable.
725 int32 mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900726 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
727 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900728
729 // Get rid of the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900730 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
731 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
732 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900733 // Make sure the file can't be read.
734 EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, buffer_size));
735
736 // Give the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900737 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
738 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
739 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900740 // Make sure the file can be read.
741 EXPECT_EQ(static_cast<int>(kData.length()),
742 file_util::ReadFile(file_name, buffer, buffer_size));
743
744 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900745 EXPECT_TRUE(DeleteFile(file_name, false));
746 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900747
748 delete[] buffer;
749}
750
751TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
752 // Create a file path.
753 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900754 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900755
756 const std::string kData("hello");
757
758 // Write file.
759 EXPECT_EQ(static_cast<int>(kData.length()),
760 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900761 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900762
763 // Make sure the file is writable.
764 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900765 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
766 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900767 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900768
769 // Get rid of the write permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900770 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
771 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
772 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900773 // Make sure the file can't be write.
774 EXPECT_EQ(-1,
775 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900776 EXPECT_FALSE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900777
778 // Give read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900779 EXPECT_TRUE(SetPosixFilePermissions(file_name,
780 FILE_PERMISSION_WRITE_BY_USER));
781 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
782 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900783 // Make sure the file can be write.
784 EXPECT_EQ(static_cast<int>(kData.length()),
785 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900786 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900787
788 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900789 EXPECT_TRUE(DeleteFile(file_name, false));
790 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900791}
792
793TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
794 // Create a directory path.
795 FilePath subdir_path =
796 temp_dir_.path().Append(FPL("PermissionTest1"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900797 base::CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900798 ASSERT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900799
800 // Create a dummy file to enumerate.
801 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900802 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900803 const std::string kData("hello");
804 EXPECT_EQ(static_cast<int>(kData.length()),
805 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900806 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900807
808 // Make sure the directory has the all permissions.
809 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900810 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
811 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900812
813 // Get rid of the permissions from the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900814 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
815 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
816 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900817
818 // Make sure the file in the directory can't be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900819 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900820 EXPECT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900821 FindResultCollector c1(f1);
822 EXPECT_EQ(c1.size(), 0);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900823 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900824
825 // Give the permissions to the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900826 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
827 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
828 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900829
830 // Make sure the file in the directory can be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900831 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900832 FindResultCollector c2(f2);
833 EXPECT_TRUE(c2.HasFile(file_name));
834 EXPECT_EQ(c2.size(), 1);
835
836 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900837 EXPECT_TRUE(DeleteFile(subdir_path, true));
838 EXPECT_FALSE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900839}
840
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900841#endif // defined(OS_POSIX)
842
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900843#if defined(OS_WIN)
844// Tests that the Delete function works for wild cards, especially
845// with the recursion flag. Also coincidentally tests PathExists.
846// TODO(erikkay): see if anyone's actually using this feature of the API
847TEST_F(FileUtilTest, DeleteWildCard) {
848 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900849 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900850 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900851 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900852
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900853 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900854 base::CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900855 ASSERT_TRUE(PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900856
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900857 // Create the wildcard path
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900858 FilePath directory_contents = temp_dir_.path();
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900859 directory_contents = directory_contents.Append(FPL("*"));
860
initial.commit3f4a7322008-07-27 06:49:38 +0900861 // Delete non-recursively and check that only the file is deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900862 EXPECT_TRUE(DeleteFile(directory_contents, false));
863 EXPECT_FALSE(PathExists(file_name));
864 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900865
zork@chromium.org61be4f42010-05-07 09:05:36 +0900866 // Delete recursively and make sure all contents are deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900867 EXPECT_TRUE(DeleteFile(directory_contents, true));
868 EXPECT_FALSE(PathExists(file_name));
869 EXPECT_FALSE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900870}
871
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900872// TODO(erikkay): see if anyone's actually using this feature of the API
873TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
874 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900875 FilePath subdir_path =
876 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900877 base::CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900878 ASSERT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900879
880 // Create the wildcard path
881 FilePath directory_contents = subdir_path;
882 directory_contents = directory_contents.Append(FPL("*"));
883
884 // Delete non-recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900885 EXPECT_TRUE(DeleteFile(directory_contents, false));
886 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900887
888 // Delete recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900889 EXPECT_TRUE(DeleteFile(directory_contents, true));
890 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900891}
892#endif
893
894// Tests non-recursive Delete() for a directory.
895TEST_F(FileUtilTest, DeleteDirNonRecursive) {
896 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900897 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900898 base::CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900899 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900900
901 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
902 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900903 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900904
905 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900906 base::CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900907 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900908
909 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900910 base::CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900911 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900912
913 // Delete non-recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900914 EXPECT_TRUE(DeleteFile(subdir_path2, false));
915 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900916
917 // Delete non-recursively and check that nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900918 EXPECT_FALSE(DeleteFile(test_subdir, false));
919 EXPECT_TRUE(PathExists(test_subdir));
920 EXPECT_TRUE(PathExists(file_name));
921 EXPECT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900922}
923
924// Tests recursive Delete() for a directory.
925TEST_F(FileUtilTest, DeleteDirRecursive) {
926 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900927 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900928 base::CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900929 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900930
931 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
932 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900933 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900934
935 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900936 base::CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900937 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900938
939 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
brettw@chromium.org738669a2013-12-04 05:08:54 +0900940 base::CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900941 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900942
943 // Delete recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900944 EXPECT_TRUE(DeleteFile(subdir_path2, true));
945 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900946
947 // Delete recursively and check that everything got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900948 EXPECT_TRUE(DeleteFile(test_subdir, true));
949 EXPECT_FALSE(PathExists(file_name));
950 EXPECT_FALSE(PathExists(subdir_path1));
951 EXPECT_FALSE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900952}
953
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900954TEST_F(FileUtilTest, MoveFileNew) {
955 // Create a file
956 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900957 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900958 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900959 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900960
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900961 // The destination.
962 FilePath file_name_to = temp_dir_.path().Append(
963 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900964 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900965
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900966 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900967
968 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900969 EXPECT_FALSE(PathExists(file_name_from));
970 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900971}
972
973TEST_F(FileUtilTest, MoveFileExists) {
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
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900980 // The destination name.
981 FilePath file_name_to = temp_dir_.path().Append(
982 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900983 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900984 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900985
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900986 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900987
988 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900989 EXPECT_FALSE(PathExists(file_name_from));
990 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900991 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
992}
993
994TEST_F(FileUtilTest, MoveFileDirExists) {
995 // Create a file
996 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900997 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900998 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900999 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001000
1001 // The destination directory
1002 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001003 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001004 base::CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001005 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001006
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001007 EXPECT_FALSE(Move(file_name_from, dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001008}
1009
1010
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001011TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001012 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001013 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001014 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001015 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001016 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001017
1018 // Create a file under the directory
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001019 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1020 FilePath file_name_from = dir_name_from.Append(txt_file_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001021 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001022 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001023
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001024 // Move the directory.
1025 FilePath dir_name_to =
1026 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001027 FilePath file_name_to =
1028 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001029
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001030 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001031
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001032 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001033
1034 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001035 EXPECT_FALSE(PathExists(dir_name_from));
1036 EXPECT_FALSE(PathExists(file_name_from));
1037 EXPECT_TRUE(PathExists(dir_name_to));
1038 EXPECT_TRUE(PathExists(file_name_to));
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001039
1040 // Test path traversal.
1041 file_name_from = dir_name_to.Append(txt_file_name);
1042 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1043 file_name_to = file_name_to.Append(txt_file_name);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001044 EXPECT_FALSE(Move(file_name_from, file_name_to));
1045 EXPECT_TRUE(PathExists(file_name_from));
1046 EXPECT_FALSE(PathExists(file_name_to));
1047 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1048 EXPECT_FALSE(PathExists(file_name_from));
1049 EXPECT_TRUE(PathExists(file_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001050}
1051
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001052TEST_F(FileUtilTest, MoveExist) {
1053 // Create a directory
1054 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001055 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001056 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001057 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001058
1059 // Create a file under the directory
1060 FilePath file_name_from =
1061 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1062 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001063 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001064
1065 // Move the directory
1066 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001067 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001068
1069 FilePath dir_name_to =
1070 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1071 FilePath file_name_to =
1072 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1073
1074 // Create the destination directory.
brettw@chromium.org738669a2013-12-04 05:08:54 +09001075 base::CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001076 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001077
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001078 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001079
1080 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001081 EXPECT_FALSE(PathExists(dir_name_from));
1082 EXPECT_FALSE(PathExists(file_name_from));
1083 EXPECT_TRUE(PathExists(dir_name_to));
1084 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001085}
1086
1087TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001088 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001089 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001090 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001091 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001092 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001093
1094 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001095 FilePath file_name_from =
1096 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001097 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001098 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001099
1100 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001101 FilePath subdir_name_from =
1102 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001103 base::CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001104 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001105
1106 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001107 FilePath file_name2_from =
1108 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001109 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001110 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001111
1112 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001113 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001114 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001115 FilePath file_name_to =
1116 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1117 FilePath subdir_name_to =
1118 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1119 FilePath file_name2_to =
1120 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001121
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001122 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001123
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001124 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001125
1126 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001127 EXPECT_TRUE(PathExists(dir_name_from));
1128 EXPECT_TRUE(PathExists(file_name_from));
1129 EXPECT_TRUE(PathExists(subdir_name_from));
1130 EXPECT_TRUE(PathExists(file_name2_from));
1131 EXPECT_TRUE(PathExists(dir_name_to));
1132 EXPECT_TRUE(PathExists(file_name_to));
1133 EXPECT_TRUE(PathExists(subdir_name_to));
1134 EXPECT_TRUE(PathExists(file_name2_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001135}
1136
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001137TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1138 // Create a directory.
1139 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001140 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001141 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001142 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001143
1144 // Create a file under the directory.
1145 FilePath file_name_from =
1146 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1147 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001148 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001149
1150 // Create a subdirectory.
1151 FilePath subdir_name_from =
1152 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001153 base::CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001154 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001155
1156 // Create a file under the subdirectory.
1157 FilePath file_name2_from =
1158 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1159 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001160 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001161
1162 // Copy the directory recursively.
1163 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001164 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001165
1166 FilePath dir_name_to =
1167 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1168 FilePath file_name_to =
1169 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1170 FilePath subdir_name_to =
1171 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1172 FilePath file_name2_to =
1173 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1174
1175 // Create the destination directory.
brettw@chromium.org738669a2013-12-04 05:08:54 +09001176 base::CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001177 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001178
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001179 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001180
1181 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001182 EXPECT_TRUE(PathExists(dir_name_from));
1183 EXPECT_TRUE(PathExists(file_name_from));
1184 EXPECT_TRUE(PathExists(subdir_name_from));
1185 EXPECT_TRUE(PathExists(file_name2_from));
1186 EXPECT_TRUE(PathExists(dir_name_to));
1187 EXPECT_TRUE(PathExists(file_name_to));
1188 EXPECT_TRUE(PathExists(subdir_name_to));
1189 EXPECT_TRUE(PathExists(file_name2_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001190}
1191
1192TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001193 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001194 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001195 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001196 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001197 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001198
1199 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001200 FilePath file_name_from =
1201 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001202 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001203 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001204
1205 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001206 FilePath subdir_name_from =
1207 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001208 base::CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001209 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001210
1211 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001212 FilePath file_name2_from =
1213 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001214 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001215 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001216
1217 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001218 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001219 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001220 FilePath file_name_to =
1221 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1222 FilePath subdir_name_to =
1223 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001224
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001225 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001226
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001227 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001228
1229 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001230 EXPECT_TRUE(PathExists(dir_name_from));
1231 EXPECT_TRUE(PathExists(file_name_from));
1232 EXPECT_TRUE(PathExists(subdir_name_from));
1233 EXPECT_TRUE(PathExists(file_name2_from));
1234 EXPECT_TRUE(PathExists(dir_name_to));
1235 EXPECT_TRUE(PathExists(file_name_to));
1236 EXPECT_FALSE(PathExists(subdir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001237}
1238
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001239TEST_F(FileUtilTest, CopyDirectoryExists) {
1240 // Create a directory.
1241 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001242 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001243 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001244 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001245
1246 // Create a file under the directory.
1247 FilePath file_name_from =
1248 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1249 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001250 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001251
1252 // Create a subdirectory.
1253 FilePath subdir_name_from =
1254 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001255 base::CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001256 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001257
1258 // Create a file under the subdirectory.
1259 FilePath file_name2_from =
1260 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1261 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001262 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001263
1264 // Copy the directory not recursively.
1265 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001266 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001267 FilePath file_name_to =
1268 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1269 FilePath subdir_name_to =
1270 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1271
1272 // Create the destination directory.
brettw@chromium.org738669a2013-12-04 05:08:54 +09001273 base::CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001274 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001275
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001276 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001277
1278 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001279 EXPECT_TRUE(PathExists(dir_name_from));
1280 EXPECT_TRUE(PathExists(file_name_from));
1281 EXPECT_TRUE(PathExists(subdir_name_from));
1282 EXPECT_TRUE(PathExists(file_name2_from));
1283 EXPECT_TRUE(PathExists(dir_name_to));
1284 EXPECT_TRUE(PathExists(file_name_to));
1285 EXPECT_FALSE(PathExists(subdir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001286}
1287
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001288TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1289 // Create a file
1290 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001291 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001292 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001293 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001294
1295 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001296 FilePath file_name_to = temp_dir_.path().Append(
1297 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001298 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001299
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001300 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001301
1302 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001303 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001304}
1305
1306TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1307 // Create a file
1308 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001309 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001310 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001311 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001312
1313 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001314 FilePath file_name_to = temp_dir_.path().Append(
1315 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001316 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001317 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001318
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001319 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001320
1321 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001322 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001323 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1324}
1325
1326TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1327 // Create a file
1328 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001329 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001330 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001331 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001332
1333 // The destination
1334 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001335 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001336 base::CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001337 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001338 FilePath file_name_to =
1339 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1340
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001341 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001342
1343 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001344 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001345}
1346
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001347TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1348 // Create a directory.
1349 FilePath dir_name_from =
1350 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001351 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001352 ASSERT_TRUE(PathExists(dir_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001353
1354 // Create a file under the directory.
1355 FilePath file_name_from =
1356 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1357 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001358 ASSERT_TRUE(PathExists(file_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001359
1360 // Copy the directory recursively.
1361 FilePath dir_name_to =
1362 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1363 FilePath file_name_to =
1364 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1365
1366 // Create from path with trailing separators.
1367#if defined(OS_WIN)
1368 FilePath from_path =
1369 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1370#elif defined (OS_POSIX)
1371 FilePath from_path =
1372 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1373#endif
1374
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001375 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001376
1377 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001378 EXPECT_TRUE(PathExists(dir_name_from));
1379 EXPECT_TRUE(PathExists(file_name_from));
1380 EXPECT_TRUE(PathExists(dir_name_to));
1381 EXPECT_TRUE(PathExists(file_name_to));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001382}
1383
initial.commit3f4a7322008-07-27 06:49:38 +09001384TEST_F(FileUtilTest, CopyFile) {
1385 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001386 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001387 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001388 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001389 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001390
1391 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001392 FilePath file_name_from =
1393 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001394 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1395 CreateTextFile(file_name_from, file_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001396 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001397
1398 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001399 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001400 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001401
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001402 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001403 FilePath dest_file2(dir_name_from);
1404 dest_file2 = dest_file2.AppendASCII("..");
1405 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001406 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
1407 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from, dest_file2));
evan@chromium.org1543ad32009-08-27 05:00:14 +09001408
1409 FilePath dest_file2_test(dir_name_from);
1410 dest_file2_test = dest_file2_test.DirName();
1411 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001412
1413 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001414 EXPECT_TRUE(PathExists(file_name_from));
1415 EXPECT_TRUE(PathExists(dest_file));
initial.commit3f4a7322008-07-27 06:49:38 +09001416 const std::wstring read_contents = ReadTextFile(dest_file);
1417 EXPECT_EQ(file_contents, read_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001418 EXPECT_TRUE(PathExists(dest_file2_test));
1419 EXPECT_TRUE(PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001420}
1421
erikkay@google.comf2406842008-08-21 00:59:49 +09001422// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001423// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001424typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001425
erikkay@google.comf2406842008-08-21 00:59:49 +09001426TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001427 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001428 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001429 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001430 ASSERT_TRUE(PathExists(data_dir));
initial.commit3f4a7322008-07-27 06:49:38 +09001431
evanm@google.com874d1672008-10-31 08:54:04 +09001432 FilePath original_file =
1433 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1434 FilePath same_file =
1435 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1436 FilePath same_length_file =
1437 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1438 FilePath different_file =
1439 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1440 FilePath different_first_file =
1441 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1442 FilePath different_last_file =
1443 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1444 FilePath empty1_file =
1445 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1446 FilePath empty2_file =
1447 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1448 FilePath shortened_file =
1449 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1450 FilePath binary_file =
1451 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1452 FilePath binary_file_same =
1453 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1454 FilePath binary_file_diff =
1455 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001456
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001457 EXPECT_TRUE(ContentsEqual(original_file, original_file));
1458 EXPECT_TRUE(ContentsEqual(original_file, same_file));
1459 EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
1460 EXPECT_FALSE(ContentsEqual(original_file, different_file));
1461 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1462 FilePath(FILE_PATH_LITERAL("bogusname"))));
1463 EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
1464 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1465 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1466 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1467 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1468 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1469 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
initial.commit3f4a7322008-07-27 06:49:38 +09001470}
1471
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001472TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1473 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001474 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001475 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001476 ASSERT_TRUE(PathExists(data_dir));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001477
1478 FilePath original_file =
1479 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1480 FilePath same_file =
1481 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1482 FilePath crlf_file =
1483 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1484 FilePath shortened_file =
1485 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1486 FilePath different_file =
1487 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1488 FilePath different_first_file =
1489 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1490 FilePath different_last_file =
1491 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1492 FilePath first1_file =
1493 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1494 FilePath first2_file =
1495 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1496 FilePath empty1_file =
1497 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1498 FilePath empty2_file =
1499 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1500 FilePath blank_line_file =
1501 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1502 FilePath blank_line_crlf_file =
1503 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1504
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001505 EXPECT_TRUE(TextContentsEqual(original_file, same_file));
1506 EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
1507 EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
1508 EXPECT_FALSE(TextContentsEqual(original_file, different_file));
1509 EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
1510 EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
1511 EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
1512 EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
1513 EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
1514 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001515}
1516
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001517// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001518#if defined(OS_WIN)
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001519TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1520 // Create a directory
1521 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001522 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001523 base::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001524 ASSERT_TRUE(PathExists(dir_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001525
1526 // Create a file under the directory
1527 FilePath file_name_from =
1528 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1529 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001530 ASSERT_TRUE(PathExists(file_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001531
1532 // Move the directory by using CopyAndDeleteDirectory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001533 FilePath dir_name_to = temp_dir_.path().Append(
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001534 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1535 FilePath file_name_to =
1536 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1537
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001538 ASSERT_FALSE(PathExists(dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001539
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001540 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +09001541 dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001542
1543 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001544 EXPECT_FALSE(PathExists(dir_name_from));
1545 EXPECT_FALSE(PathExists(file_name_from));
1546 EXPECT_TRUE(PathExists(dir_name_to));
1547 EXPECT_TRUE(PathExists(file_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001548}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001549
1550TEST_F(FileUtilTest, GetTempDirTest) {
1551 static const TCHAR* kTmpKey = _T("TMP");
1552 static const TCHAR* kTmpValues[] = {
1553 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1554 };
1555 // Save the original $TMP.
1556 size_t original_tmp_size;
1557 TCHAR* original_tmp;
1558 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1559 // original_tmp may be NULL.
1560
1561 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1562 FilePath path;
1563 ::_tputenv_s(kTmpKey, kTmpValues[i]);
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001564 base::GetTempDir(&path);
tkent@chromium.org8da14162009-10-09 16:33:39 +09001565 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1566 " result=" << path.value();
1567 }
1568
1569 // Restore the original $TMP.
1570 if (original_tmp) {
1571 ::_tputenv_s(kTmpKey, original_tmp);
1572 free(original_tmp);
1573 } else {
1574 ::_tputenv_s(kTmpKey, _T(""));
1575 }
1576}
1577#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001578
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001579TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1580 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001581 for (int i = 0; i < 3; i++) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +09001582 ASSERT_TRUE(base::CreateTemporaryFile(&(temp_files[i])));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001583 EXPECT_TRUE(PathExists(temp_files[i]));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001584 EXPECT_FALSE(DirectoryExists(temp_files[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001585 }
1586 for (int i = 0; i < 3; i++)
1587 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1588 for (int i = 0; i < 3; i++)
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001589 EXPECT_TRUE(DeleteFile(temp_files[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001590}
1591
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001592TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001593 FilePath names[3];
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001594 FILE* fps[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001595 int i;
1596
1597 // Create; make sure they are open and exist.
1598 for (i = 0; i < 3; ++i) {
brettw@chromium.org735d11d2013-12-04 02:55:52 +09001599 fps[i] = base::CreateAndOpenTemporaryFile(&(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001600 ASSERT_TRUE(fps[i]);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001601 EXPECT_TRUE(PathExists(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001602 }
1603
1604 // Make sure all names are unique.
1605 for (i = 0; i < 3; ++i) {
1606 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1607 }
1608
1609 // Close and delete.
1610 for (i = 0; i < 3; ++i) {
1611 EXPECT_TRUE(file_util::CloseFile(fps[i]));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001612 EXPECT_TRUE(DeleteFile(names[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001613 }
initial.commit3f4a7322008-07-27 06:49:38 +09001614}
1615
1616TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001617 FilePath temp_dir;
brettw@chromium.org735d11d2013-12-04 02:55:52 +09001618 ASSERT_TRUE(base::CreateNewTempDirectory(FilePath::StringType(), &temp_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001619 EXPECT_TRUE(PathExists(temp_dir));
1620 EXPECT_TRUE(DeleteFile(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001621}
1622
skerner@chromium.orge4432392010-05-01 02:00:09 +09001623TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1624 FilePath new_dir;
brettw@chromium.org735d11d2013-12-04 02:55:52 +09001625 ASSERT_TRUE(base::CreateTemporaryDirInDir(
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001626 temp_dir_.path(),
skerner@chromium.orge4432392010-05-01 02:00:09 +09001627 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001628 &new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001629 EXPECT_TRUE(PathExists(new_dir));
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001630 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001631 EXPECT_TRUE(DeleteFile(new_dir, false));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001632}
1633
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001634TEST_F(FileUtilTest, GetShmemTempDirTest) {
1635 FilePath dir;
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001636 EXPECT_TRUE(GetShmemTempDir(false, &dir));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001637 EXPECT_TRUE(DirectoryExists(dir));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001638}
1639
initial.commit3f4a7322008-07-27 06:49:38 +09001640TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001641 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001642 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001643#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001644 FilePath test_path =
1645 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001646#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001647 FilePath test_path =
1648 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001649#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001650
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001651 EXPECT_FALSE(PathExists(test_path));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001652 EXPECT_TRUE(base::CreateDirectory(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001653 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001654 // CreateDirectory returns true if the DirectoryExists returns true.
brettw@chromium.org738669a2013-12-04 05:08:54 +09001655 EXPECT_TRUE(base::CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001656
1657 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001658 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001659 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001660 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001661 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001662 EXPECT_FALSE(base::CreateDirectory(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001663
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001664 EXPECT_TRUE(DeleteFile(test_root, true));
1665 EXPECT_FALSE(PathExists(test_root));
1666 EXPECT_FALSE(PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001667
1668 // Verify assumptions made by the Windows implementation:
1669 // 1. The current directory always exists.
1670 // 2. The root directory always exists.
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001671 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001672 FilePath top_level = test_root;
1673 while (top_level != top_level.DirName()) {
1674 top_level = top_level.DirName();
1675 }
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001676 ASSERT_TRUE(DirectoryExists(top_level));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001677
1678 // Given these assumptions hold, it should be safe to
1679 // test that "creating" these directories succeeds.
brettw@chromium.org738669a2013-12-04 05:08:54 +09001680 EXPECT_TRUE(base::CreateDirectory(
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001681 FilePath(FilePath::kCurrentDirectory)));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001682 EXPECT_TRUE(base::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001683
1684#if defined(OS_WIN)
1685 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1686 FilePath invalid_path =
1687 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001688 if (!PathExists(invalid_drive)) {
brettw@chromium.org738669a2013-12-04 05:08:54 +09001689 EXPECT_FALSE(base::CreateDirectory(invalid_path));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001690 }
1691#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001692}
1693
1694TEST_F(FileUtilTest, DetectDirectoryTest) {
1695 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001696 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001697 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001698 EXPECT_FALSE(PathExists(test_root));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001699 EXPECT_TRUE(base::CreateDirectory(test_root));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001700 EXPECT_TRUE(PathExists(test_root));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001701 EXPECT_TRUE(DirectoryExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001702 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001703 FilePath test_path =
1704 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001705 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001706 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001707 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001708 EXPECT_FALSE(DirectoryExists(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001709 EXPECT_TRUE(DeleteFile(test_path, false));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001710
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001711 EXPECT_TRUE(DeleteFile(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001712}
1713
initial.commit3f4a7322008-07-27 06:49:38 +09001714TEST_F(FileUtilTest, FileEnumeratorTest) {
1715 // Test an empty directory.
brettw@chromium.org56946722013-06-08 13:53:36 +09001716 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001717 EXPECT_EQ(f0.Next().value(), FPL(""));
1718 EXPECT_EQ(f0.Next().value(), FPL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001719
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001720 // Test an empty directory, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001721 FileEnumerator f0_dotdot(temp_dir_.path(), false,
1722 FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001723 EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001724 f0_dotdot.Next().value());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001725 EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001726
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001727 // create the directories
rvargas@chromium.org56472942013-08-15 05:46:05 +09001728 FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001729 EXPECT_TRUE(base::CreateDirectory(dir1));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001730 FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001731 EXPECT_TRUE(base::CreateDirectory(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001732 FilePath dir2inner = dir2.Append(FPL("inner"));
brettw@chromium.org738669a2013-12-04 05:08:54 +09001733 EXPECT_TRUE(base::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001734
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001735 // create the files
rvargas@chromium.org56472942013-08-15 05:46:05 +09001736 FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001737 CreateTextFile(dir2file, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001738 FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001739 CreateTextFile(dir2innerfile, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001740 FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001741 CreateTextFile(file1, std::wstring());
1742 FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
rvargas@chromium.org56472942013-08-15 05:46:05 +09001743 .Append(FPL("file2.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001744 CreateTextFile(file2_rel, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001745 FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001746
1747 // Only enumerate files.
brettw@chromium.org56946722013-06-08 13:53:36 +09001748 FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
initial.commit3f4a7322008-07-27 06:49:38 +09001749 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001750 EXPECT_TRUE(c1.HasFile(file1));
1751 EXPECT_TRUE(c1.HasFile(file2_abs));
1752 EXPECT_TRUE(c1.HasFile(dir2file));
1753 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1754 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001755
1756 // Only enumerate directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001757 FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001758 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001759 EXPECT_TRUE(c2.HasFile(dir1));
1760 EXPECT_TRUE(c2.HasFile(dir2));
1761 EXPECT_TRUE(c2.HasFile(dir2inner));
1762 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001763
tim@chromium.org989d0972008-10-16 11:42:45 +09001764 // Only enumerate directories non-recursively.
brettw@chromium.org56946722013-06-08 13:53:36 +09001765 FileEnumerator f2_non_recursive(
1766 temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001767 FindResultCollector c2_non_recursive(f2_non_recursive);
1768 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1769 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1770 EXPECT_EQ(c2_non_recursive.size(), 2);
1771
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001772 // Only enumerate directories, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001773 FileEnumerator f2_dotdot(temp_dir_.path(), false,
1774 FileEnumerator::DIRECTORIES |
1775 FileEnumerator::INCLUDE_DOT_DOT);
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001776 FindResultCollector c2_dotdot(f2_dotdot);
1777 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1778 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001779 EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001780 EXPECT_EQ(c2_dotdot.size(), 3);
1781
initial.commit3f4a7322008-07-27 06:49:38 +09001782 // Enumerate files and directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001783 FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001784 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001785 EXPECT_TRUE(c3.HasFile(dir1));
1786 EXPECT_TRUE(c3.HasFile(dir2));
1787 EXPECT_TRUE(c3.HasFile(file1));
1788 EXPECT_TRUE(c3.HasFile(file2_abs));
1789 EXPECT_TRUE(c3.HasFile(dir2file));
1790 EXPECT_TRUE(c3.HasFile(dir2inner));
1791 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1792 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001793
1794 // Non-recursive operation.
brettw@chromium.org56946722013-06-08 13:53:36 +09001795 FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001796 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001797 EXPECT_TRUE(c4.HasFile(dir2));
1798 EXPECT_TRUE(c4.HasFile(dir2));
1799 EXPECT_TRUE(c4.HasFile(file1));
1800 EXPECT_TRUE(c4.HasFile(file2_abs));
1801 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001802
1803 // Enumerate with a pattern.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001804 FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001805 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001806 EXPECT_TRUE(c5.HasFile(dir1));
1807 EXPECT_TRUE(c5.HasFile(dir2));
1808 EXPECT_TRUE(c5.HasFile(dir2file));
1809 EXPECT_TRUE(c5.HasFile(dir2inner));
1810 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1811 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001812
rvargas@chromium.org56472942013-08-15 05:46:05 +09001813#if defined(OS_WIN)
1814 {
1815 // Make dir1 point to dir2.
1816 ReparsePoint reparse_point(dir1, dir2);
1817 EXPECT_TRUE(reparse_point.IsValid());
1818
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001819 if ((win::GetVersion() >= base::win::VERSION_VISTA)) {
rvargas@chromium.org56472942013-08-15 05:46:05 +09001820 // There can be a delay for the enumeration code to see the change on
1821 // the file system so skip this test for XP.
1822 // Enumerate the reparse point.
1823 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
1824 FindResultCollector c6(f6);
1825 FilePath inner2 = dir1.Append(FPL("inner"));
1826 EXPECT_TRUE(c6.HasFile(inner2));
1827 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1828 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
1829 EXPECT_EQ(c6.size(), 3);
1830 }
1831
1832 // No changes for non recursive operation.
1833 FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
1834 FindResultCollector c7(f7);
1835 EXPECT_TRUE(c7.HasFile(dir2));
1836 EXPECT_TRUE(c7.HasFile(dir2));
1837 EXPECT_TRUE(c7.HasFile(file1));
1838 EXPECT_TRUE(c7.HasFile(file2_abs));
1839 EXPECT_EQ(c7.size(), 4);
1840
1841 // Should not enumerate inside dir1 when using recursion.
1842 FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1843 FindResultCollector c8(f8);
1844 EXPECT_TRUE(c8.HasFile(dir1));
1845 EXPECT_TRUE(c8.HasFile(dir2));
1846 EXPECT_TRUE(c8.HasFile(file1));
1847 EXPECT_TRUE(c8.HasFile(file2_abs));
1848 EXPECT_TRUE(c8.HasFile(dir2file));
1849 EXPECT_TRUE(c8.HasFile(dir2inner));
1850 EXPECT_TRUE(c8.HasFile(dir2innerfile));
1851 EXPECT_EQ(c8.size(), 7);
1852 }
1853#endif
1854
initial.commit3f4a7322008-07-27 06:49:38 +09001855 // Make sure the destructor closes the find handle while in the middle of a
1856 // query to allow TearDown to delete the directory.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001857 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1858 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
avi@google.com5cb79352008-12-11 23:55:12 +09001859 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001860}
license.botf003cfe2008-08-24 09:55:55 +09001861
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001862TEST_F(FileUtilTest, AppendToFile) {
1863 FilePath data_dir =
1864 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1865
1866 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001867 if (PathExists(data_dir)) {
1868 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001869 }
brettw@chromium.org738669a2013-12-04 05:08:54 +09001870 ASSERT_TRUE(base::CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001871
1872 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001873 if (PathExists(data_dir)) {
1874 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001875 }
brettw@chromium.org738669a2013-12-04 05:08:54 +09001876 ASSERT_TRUE(base::CreateDirectory(data_dir));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001877 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1878
1879 std::string data("hello");
1880 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length()));
1881 EXPECT_EQ(static_cast<int>(data.length()),
1882 file_util::WriteFile(foobar, data.c_str(), data.length()));
1883 EXPECT_EQ(static_cast<int>(data.length()),
1884 file_util::AppendToFile(foobar, data.c_str(), data.length()));
1885
1886 const std::wstring read_content = ReadTextFile(foobar);
1887 EXPECT_EQ(L"hellohello", read_content);
1888}
1889
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001890TEST_F(FileUtilTest, TouchFile) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001891 FilePath data_dir =
1892 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
jochen@chromium.orga6879772010-02-18 19:02:26 +09001893
1894 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001895 if (PathExists(data_dir)) {
1896 ASSERT_TRUE(DeleteFile(data_dir, true));
jochen@chromium.orga6879772010-02-18 19:02:26 +09001897 }
brettw@chromium.org738669a2013-12-04 05:08:54 +09001898 ASSERT_TRUE(base::CreateDirectory(data_dir));
jochen@chromium.orga6879772010-02-18 19:02:26 +09001899
1900 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1901 std::string data("hello");
1902 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1903
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001904 Time access_time;
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001905 // This timestamp is divisible by one day (in local timezone),
1906 // to make it work on FAT too.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001907 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001908 &access_time));
1909
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001910 Time modification_time;
jochen@chromium.orga6879772010-02-18 19:02:26 +09001911 // Note that this timestamp is divisible by two (seconds) - FAT stores
1912 // modification times with 2s resolution.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001913 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
jochen@chromium.orga6879772010-02-18 19:02:26 +09001914 &modification_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001915
1916 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001917 PlatformFileInfo file_info;
jochen@chromium.orga6879772010-02-18 19:02:26 +09001918 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001919 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
1920 access_time.ToInternalValue());
1921 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
1922 modification_time.ToInternalValue());
jochen@chromium.orga6879772010-02-18 19:02:26 +09001923}
1924
tfarina@chromium.org34828222010-05-26 10:40:12 +09001925TEST_F(FileUtilTest, IsDirectoryEmpty) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001926 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
tfarina@chromium.org34828222010-05-26 10:40:12 +09001927
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001928 ASSERT_FALSE(PathExists(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09001929
brettw@chromium.org738669a2013-12-04 05:08:54 +09001930 ASSERT_TRUE(base::CreateDirectory(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09001931
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001932 EXPECT_TRUE(base::IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09001933
1934 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1935 std::string bar("baz");
1936 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1937
brettw@chromium.org83c44c82013-12-03 03:55:49 +09001938 EXPECT_FALSE(base::IsDirectoryEmpty(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09001939}
1940
skerner@google.com93449ef2011-09-22 23:47:18 +09001941#if defined(OS_POSIX)
1942
1943// Testing VerifyPathControlledByAdmin() is hard, because there is no
1944// way a test can make a file owned by root, or change file paths
1945// at the root of the file system. VerifyPathControlledByAdmin()
1946// is implemented as a call to VerifyPathControlledByUser, which gives
1947// us the ability to test with paths under the test's temp directory,
1948// using a user id we control.
1949// Pull tests of VerifyPathControlledByUserTest() into a separate test class
1950// with a common SetUp() method.
1951class VerifyPathControlledByUserTest : public FileUtilTest {
1952 protected:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +09001953 virtual void SetUp() OVERRIDE {
skerner@google.com93449ef2011-09-22 23:47:18 +09001954 FileUtilTest::SetUp();
1955
1956 // Create a basic structure used by each test.
1957 // base_dir_
1958 // |-> sub_dir_
1959 // |-> text_file_
1960
1961 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
brettw@chromium.org738669a2013-12-04 05:08:54 +09001962 ASSERT_TRUE(base::CreateDirectory(base_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09001963
1964 sub_dir_ = base_dir_.AppendASCII("sub_dir");
brettw@chromium.org738669a2013-12-04 05:08:54 +09001965 ASSERT_TRUE(base::CreateDirectory(sub_dir_));
skerner@google.com93449ef2011-09-22 23:47:18 +09001966
1967 text_file_ = sub_dir_.AppendASCII("file.txt");
1968 CreateTextFile(text_file_, L"This text file has some text in it.");
1969
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09001970 // Get the user and group files are created with from |base_dir_|.
1971 struct stat stat_buf;
1972 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
1973 uid_ = stat_buf.st_uid;
skerner@chromium.org80784142011-10-18 06:30:29 +09001974 ok_gids_.insert(stat_buf.st_gid);
1975 bad_gids_.insert(stat_buf.st_gid + 1);
1976
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09001977 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
skerner@google.com93449ef2011-09-22 23:47:18 +09001978
1979 // To ensure that umask settings do not cause the initial state
1980 // of permissions to be different from what we expect, explicitly
1981 // set permissions on the directories we create.
1982 // Make all files and directories non-world-writable.
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09001983
1984 // Users and group can read, write, traverse
1985 int enabled_permissions =
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001986 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09001987 // Other users can't read, write, traverse
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001988 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
skerner@google.com93449ef2011-09-22 23:47:18 +09001989
1990 ASSERT_NO_FATAL_FAILURE(
1991 ChangePosixFilePermissions(
1992 base_dir_, enabled_permissions, disabled_permissions));
1993 ASSERT_NO_FATAL_FAILURE(
1994 ChangePosixFilePermissions(
1995 sub_dir_, enabled_permissions, disabled_permissions));
1996 }
1997
1998 FilePath base_dir_;
1999 FilePath sub_dir_;
2000 FilePath text_file_;
2001 uid_t uid_;
skerner@chromium.org80784142011-10-18 06:30:29 +09002002
2003 std::set<gid_t> ok_gids_;
2004 std::set<gid_t> bad_gids_;
skerner@google.com93449ef2011-09-22 23:47:18 +09002005};
2006
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002007TEST_F(VerifyPathControlledByUserTest, BadPaths) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002008 // File does not exist.
2009 FilePath does_not_exist = base_dir_.AppendASCII("does")
2010 .AppendASCII("not")
2011 .AppendASCII("exist");
skerner@google.com93449ef2011-09-22 23:47:18 +09002012 EXPECT_FALSE(
2013 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002014 base_dir_, does_not_exist, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002015
2016 // |base| not a subpath of |path|.
2017 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002018 file_util::VerifyPathControlledByUser(
2019 sub_dir_, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002020
2021 // An empty base path will fail to be a prefix for any path.
2022 FilePath empty;
2023 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002024 file_util::VerifyPathControlledByUser(
2025 empty, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002026
2027 // Finding that a bad call fails proves nothing unless a good call succeeds.
2028 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002029 file_util::VerifyPathControlledByUser(
2030 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002031}
2032
2033TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2034 // Symlinks in the path should cause failure.
2035
2036 // Symlink to the file at the end of the path.
2037 FilePath file_link = base_dir_.AppendASCII("file_link");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002038 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
skerner@google.com93449ef2011-09-22 23:47:18 +09002039 << "Failed to create symlink.";
2040
2041 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002042 file_util::VerifyPathControlledByUser(
2043 base_dir_, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002044 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002045 file_util::VerifyPathControlledByUser(
2046 file_link, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002047
2048 // Symlink from one directory to another within the path.
2049 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002050 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
skerner@google.com93449ef2011-09-22 23:47:18 +09002051 << "Failed to create symlink.";
2052
2053 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002054 ASSERT_TRUE(PathExists(file_path_with_link));
skerner@google.com93449ef2011-09-22 23:47:18 +09002055
2056 EXPECT_FALSE(
2057 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002058 base_dir_, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002059
2060 EXPECT_FALSE(
2061 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002062 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002063
2064 // Symlinks in parents of base path are allowed.
2065 EXPECT_TRUE(
2066 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002067 file_path_with_link, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002068}
2069
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002070TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002071 // Get a uid that is not the uid of files we create.
2072 uid_t bad_uid = uid_ + 1;
2073
skerner@google.com93449ef2011-09-22 23:47:18 +09002074 // Make all files and directories non-world-writable.
2075 ASSERT_NO_FATAL_FAILURE(
2076 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2077 ASSERT_NO_FATAL_FAILURE(
2078 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2079 ASSERT_NO_FATAL_FAILURE(
2080 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2081
2082 // We control these paths.
2083 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002084 file_util::VerifyPathControlledByUser(
2085 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002086 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002087 file_util::VerifyPathControlledByUser(
2088 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002089 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002090 file_util::VerifyPathControlledByUser(
2091 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002092
2093 // Another user does not control these paths.
2094 EXPECT_FALSE(
2095 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002096 base_dir_, sub_dir_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002097 EXPECT_FALSE(
2098 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002099 base_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002100 EXPECT_FALSE(
2101 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002102 sub_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002103
2104 // Another group does not control the paths.
2105 EXPECT_FALSE(
2106 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002107 base_dir_, sub_dir_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002108 EXPECT_FALSE(
2109 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002110 base_dir_, text_file_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002111 EXPECT_FALSE(
2112 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002113 sub_dir_, text_file_, uid_, bad_gids_));
2114}
2115
2116TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2117 // Make all files and directories writable only by their owner.
2118 ASSERT_NO_FATAL_FAILURE(
2119 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2120 ASSERT_NO_FATAL_FAILURE(
2121 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2122 ASSERT_NO_FATAL_FAILURE(
2123 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2124
2125 // Any group is okay because the path is not group-writable.
2126 EXPECT_TRUE(
2127 file_util::VerifyPathControlledByUser(
2128 base_dir_, sub_dir_, uid_, ok_gids_));
2129 EXPECT_TRUE(
2130 file_util::VerifyPathControlledByUser(
2131 base_dir_, text_file_, uid_, ok_gids_));
2132 EXPECT_TRUE(
2133 file_util::VerifyPathControlledByUser(
2134 sub_dir_, text_file_, uid_, ok_gids_));
2135
2136 EXPECT_TRUE(
2137 file_util::VerifyPathControlledByUser(
2138 base_dir_, sub_dir_, uid_, bad_gids_));
2139 EXPECT_TRUE(
2140 file_util::VerifyPathControlledByUser(
2141 base_dir_, text_file_, uid_, bad_gids_));
2142 EXPECT_TRUE(
2143 file_util::VerifyPathControlledByUser(
2144 sub_dir_, text_file_, uid_, bad_gids_));
2145
2146 // No group is okay, because we don't check the group
2147 // if no group can write.
2148 std::set<gid_t> no_gids; // Empty set of gids.
2149 EXPECT_TRUE(
2150 file_util::VerifyPathControlledByUser(
2151 base_dir_, sub_dir_, uid_, no_gids));
2152 EXPECT_TRUE(
2153 file_util::VerifyPathControlledByUser(
2154 base_dir_, text_file_, uid_, no_gids));
2155 EXPECT_TRUE(
2156 file_util::VerifyPathControlledByUser(
2157 sub_dir_, text_file_, uid_, no_gids));
2158
2159
2160 // Make all files and directories writable by their group.
2161 ASSERT_NO_FATAL_FAILURE(
2162 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2163 ASSERT_NO_FATAL_FAILURE(
2164 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2165 ASSERT_NO_FATAL_FAILURE(
2166 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2167
2168 // Now |ok_gids_| works, but |bad_gids_| fails.
2169 EXPECT_TRUE(
2170 file_util::VerifyPathControlledByUser(
2171 base_dir_, sub_dir_, uid_, ok_gids_));
2172 EXPECT_TRUE(
2173 file_util::VerifyPathControlledByUser(
2174 base_dir_, text_file_, uid_, ok_gids_));
2175 EXPECT_TRUE(
2176 file_util::VerifyPathControlledByUser(
2177 sub_dir_, text_file_, uid_, ok_gids_));
2178
2179 EXPECT_FALSE(
2180 file_util::VerifyPathControlledByUser(
2181 base_dir_, sub_dir_, uid_, bad_gids_));
2182 EXPECT_FALSE(
2183 file_util::VerifyPathControlledByUser(
2184 base_dir_, text_file_, uid_, bad_gids_));
2185 EXPECT_FALSE(
2186 file_util::VerifyPathControlledByUser(
2187 sub_dir_, text_file_, uid_, bad_gids_));
2188
2189 // Because any group in the group set is allowed,
2190 // the union of good and bad gids passes.
2191
2192 std::set<gid_t> multiple_gids;
2193 std::set_union(
2194 ok_gids_.begin(), ok_gids_.end(),
2195 bad_gids_.begin(), bad_gids_.end(),
2196 std::inserter(multiple_gids, multiple_gids.begin()));
2197
2198 EXPECT_TRUE(
2199 file_util::VerifyPathControlledByUser(
2200 base_dir_, sub_dir_, uid_, multiple_gids));
2201 EXPECT_TRUE(
2202 file_util::VerifyPathControlledByUser(
2203 base_dir_, text_file_, uid_, multiple_gids));
2204 EXPECT_TRUE(
2205 file_util::VerifyPathControlledByUser(
2206 sub_dir_, text_file_, uid_, multiple_gids));
skerner@google.com93449ef2011-09-22 23:47:18 +09002207}
2208
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002209TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002210 // Make all files and directories non-world-writable.
2211 ASSERT_NO_FATAL_FAILURE(
2212 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2213 ASSERT_NO_FATAL_FAILURE(
2214 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2215 ASSERT_NO_FATAL_FAILURE(
2216 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2217
2218 // Initialy, we control all parts of the path.
2219 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002220 file_util::VerifyPathControlledByUser(
2221 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002222 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002223 file_util::VerifyPathControlledByUser(
2224 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002225 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002226 file_util::VerifyPathControlledByUser(
2227 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002228
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09002229 // Make base_dir_ world-writable.
skerner@google.com93449ef2011-09-22 23:47:18 +09002230 ASSERT_NO_FATAL_FAILURE(
2231 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2232 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002233 file_util::VerifyPathControlledByUser(
2234 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002235 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002236 file_util::VerifyPathControlledByUser(
2237 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002238 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002239 file_util::VerifyPathControlledByUser(
2240 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002241
2242 // Make sub_dir_ world writable.
2243 ASSERT_NO_FATAL_FAILURE(
2244 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2245 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002246 file_util::VerifyPathControlledByUser(
2247 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002248 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002249 file_util::VerifyPathControlledByUser(
2250 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002251 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002252 file_util::VerifyPathControlledByUser(
2253 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002254
2255 // Make text_file_ world writable.
2256 ASSERT_NO_FATAL_FAILURE(
2257 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2258 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002259 file_util::VerifyPathControlledByUser(
2260 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002261 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002262 file_util::VerifyPathControlledByUser(
2263 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002264 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002265 file_util::VerifyPathControlledByUser(
2266 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002267
2268 // Make sub_dir_ non-world writable.
2269 ASSERT_NO_FATAL_FAILURE(
2270 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2271 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002272 file_util::VerifyPathControlledByUser(
2273 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002274 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002275 file_util::VerifyPathControlledByUser(
2276 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002277 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002278 file_util::VerifyPathControlledByUser(
2279 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002280
2281 // Make base_dir_ non-world-writable.
2282 ASSERT_NO_FATAL_FAILURE(
2283 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2284 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002285 file_util::VerifyPathControlledByUser(
2286 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002287 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002288 file_util::VerifyPathControlledByUser(
2289 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002290 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002291 file_util::VerifyPathControlledByUser(
2292 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002293
2294 // Back to the initial state: Nothing is writable, so every path
2295 // should pass.
2296 ASSERT_NO_FATAL_FAILURE(
2297 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2298 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002299 file_util::VerifyPathControlledByUser(
2300 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002301 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002302 file_util::VerifyPathControlledByUser(
2303 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002304 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002305 file_util::VerifyPathControlledByUser(
2306 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002307}
2308
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002309#if defined(OS_ANDROID)
2310TEST_F(FileUtilTest, ValidContentUriTest) {
2311 // Get the test image path.
2312 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002313 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002314 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002315 ASSERT_TRUE(PathExists(data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002316 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2317 int64 image_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002318 GetFileSize(image_file, &image_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002319 EXPECT_LT(0, image_size);
2320
2321 // Insert the image into MediaStore. MediaStore will do some conversions, and
2322 // return the content URI.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002323 FilePath path = file_util::InsertImageIntoMediaStore(image_file);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002324 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002325 EXPECT_TRUE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002326 // The file size may not equal to the input image as MediaStore may convert
2327 // the image.
2328 int64 content_uri_size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002329 GetFileSize(path, &content_uri_size);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002330 EXPECT_EQ(image_size, content_uri_size);
2331
2332 // We should be able to read the file.
2333 char* buffer = new char[image_size];
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002334 int fd = OpenContentUriForRead(path);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002335 EXPECT_LT(0, fd);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002336 EXPECT_TRUE(ReadFromFD(fd, buffer, image_size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002337 delete[] buffer;
2338}
2339
2340TEST_F(FileUtilTest, NonExistentContentUriTest) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002341 FilePath path("content://foo.bar");
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002342 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002343 EXPECT_FALSE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002344 // Size should be smaller than 0.
2345 int64 size;
brettw@chromium.org70684242013-12-05 03:22:49 +09002346 EXPECT_FALSE(GetFileSize(path, &size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002347
2348 // We should not be able to read the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002349 int fd = OpenContentUriForRead(path);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002350 EXPECT_EQ(-1, fd);
2351}
2352#endif
2353
skerner@google.com93449ef2011-09-22 23:47:18 +09002354#endif // defined(OS_POSIX)
2355
mark@chromium.org17684802008-09-10 09:16:28 +09002356} // namespace
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002357
2358} // namespace base