blob: ebccfd4ec7d370f75224baa074a83570e6cff108 [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;
253 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
254 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"));
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900257 file_util::CreateDirectory(subdir_path);
258
259 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
260 CreateTextFile(file_02, L"123456789012345678901234567890");
261 int64 size_f2 = 0;
262 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
263 EXPECT_EQ(30ll, size_f2);
264
265 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
266 file_util::CreateDirectory(subsubdir_path);
267
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"));
281 file_util::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));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900285 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
286 &normalized_file_a_path))
viettrungluu@chromium.orgea703f12010-08-23 01:19:13 +0900287 << "NormalizeFilePath() should fail on nonexistent paths.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900288
289 CreateTextFile(file_a_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900290 ASSERT_TRUE(PathExists(file_a_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900291 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
292 &normalized_file_a_path));
293
294 CreateTextFile(file_b_path, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900295 ASSERT_TRUE(PathExists(file_b_path));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900296 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
297 &normalized_file_b_path));
298
299 // Beacuse this test created |dir_path|, we know it is not a link
300 // or junction. So, the real path of the directory holding file a
301 // must be the parent of the path holding file b.
302 ASSERT_TRUE(normalized_file_a_path.DirName()
303 .IsParent(normalized_file_b_path.DirName()));
304}
305
306#if defined(OS_WIN)
307
308TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
309 // Build the following directory structure:
310 //
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900311 // temp_dir
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900312 // |-> base_a
313 // | |-> sub_a
314 // | |-> file.txt
315 // | |-> long_name___... (Very long name.)
316 // | |-> sub_long
317 // | |-> deep.txt
318 // |-> base_b
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900319 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
320 // |-> to_base_b (reparse point to temp_dir\base_b)
321 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900322
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900323 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900324 ASSERT_TRUE(file_util::CreateDirectory(base_a));
325
326 FilePath sub_a = base_a.Append(FPL("sub_a"));
327 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
328
329 FilePath file_txt = sub_a.Append(FPL("file.txt"));
330 CreateTextFile(file_txt, bogus_content);
331
332 // Want a directory whose name is long enough to make the path to the file
333 // inside just under MAX_PATH chars. This will be used to test that when
334 // a junction expands to a path over MAX_PATH chars in length,
335 // NormalizeFilePath() fails without crashing.
336 FilePath sub_long_rel(FPL("sub_long"));
337 FilePath deep_txt(FPL("deep.txt"));
338
339 int target_length = MAX_PATH;
340 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
341 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900342 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900343 // the resulting path is still long enough to hit the failing case in
344 // NormalizePath().
345 const int kCreateDirLimit = 4;
346 target_length -= kCreateDirLimit;
347 FilePath::StringType long_name_str = FPL("long_name_");
348 long_name_str.resize(target_length, '_');
349
350 FilePath long_name = sub_a.Append(FilePath(long_name_str));
351 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
352 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
353
354 FilePath sub_long = deep_file.DirName();
355 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
356 CreateTextFile(deep_file, bogus_content);
357
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900358 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900359 ASSERT_TRUE(file_util::CreateDirectory(base_b));
360
361 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
362 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900363 FilePath normalized_path;
rvargas@chromium.org56472942013-08-15 05:46:05 +0900364 {
365 ReparsePoint reparse_to_sub_a(to_sub_a, sub_a);
366 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900367
rvargas@chromium.org56472942013-08-15 05:46:05 +0900368 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
369 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
370 ReparsePoint reparse_to_base_b(to_base_b, base_b);
371 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900372
rvargas@chromium.org56472942013-08-15 05:46:05 +0900373 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
374 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
375 ReparsePoint reparse_to_sub_long(to_sub_long, sub_long);
376 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900377
rvargas@chromium.org56472942013-08-15 05:46:05 +0900378 // Normalize a junction free path: base_a\sub_a\file.txt .
379 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
380 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
381
382 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
383 // the junction to_sub_a.
384 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
385 &normalized_path));
386 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
387
388 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
389 // normalized to exclude junctions to_base_b and to_sub_a .
390 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
391 .Append(FPL("to_base_b"))
392 .Append(FPL("to_sub_a"))
393 .Append(FPL("file.txt")),
394 &normalized_path));
395 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
396
397 // A long enough path will cause NormalizeFilePath() to fail. Make a long
398 // path using to_base_b many times, and check that paths long enough to fail
399 // do not cause a crash.
400 FilePath long_path = base_b;
401 const int kLengthLimit = MAX_PATH + 200;
402 while (long_path.value().length() <= kLengthLimit) {
403 long_path = long_path.Append(FPL("to_base_b"));
404 }
405 long_path = long_path.Append(FPL("to_sub_a"))
406 .Append(FPL("file.txt"));
407
408 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
409
410 // Normalizing the junction to deep.txt should fail, because the expanded
411 // path to deep.txt is longer than MAX_PATH.
412 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
413 &normalized_path));
414
415 // Delete the reparse points, and see that NormalizeFilePath() fails
416 // to traverse them.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900417 }
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900418
419 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
420 &normalized_path));
421}
422
skerner@chromium.orgd05e16c2012-01-18 07:44:31 +0900423TEST_F(FileUtilTest, DevicePathToDriveLetter) {
424 // Get a drive letter.
425 std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
426 if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
427 LOG(ERROR) << "Can't get a drive letter to test with.";
428 return;
429 }
430
431 // Get the NT style path to that drive.
432 wchar_t device_path[MAX_PATH] = {'\0'};
433 ASSERT_TRUE(
434 ::QueryDosDevice(real_drive_letter.c_str(), device_path, MAX_PATH));
435 FilePath actual_device_path(device_path);
436 FilePath win32_path;
437
438 // Run DevicePathToDriveLetterPath() on the NT style path we got from
439 // QueryDosDevice(). Expect the drive letter we started with.
440 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path,
441 &win32_path));
442 ASSERT_EQ(real_drive_letter, win32_path.value());
443
444 // Add some directories to the path. Expect those extra path componenets
445 // to be preserved.
446 FilePath kRelativePath(FPL("dir1\\dir2\\file.txt"));
447 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
448 actual_device_path.Append(kRelativePath),
449 &win32_path));
450 EXPECT_EQ(FilePath(real_drive_letter + L"\\").Append(kRelativePath).value(),
451 win32_path.value());
452
453 // Deform the real path so that it is invalid by removing the last four
454 // characters. The way windows names devices that are hard disks
455 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
456 // than three characters. The only way the truncated string could be a
457 // real drive is if more than 10^3 disks are mounted:
458 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
459 // Check that DevicePathToDriveLetterPath fails.
460 int path_length = actual_device_path.value().length();
461 int new_length = path_length - 4;
462 ASSERT_LT(0, new_length);
463 FilePath prefix_of_real_device_path(
464 actual_device_path.value().substr(0, new_length));
465 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
466 prefix_of_real_device_path,
467 &win32_path));
468
469 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
470 prefix_of_real_device_path.Append(kRelativePath),
471 &win32_path));
472
473 // Deform the real path so that it is invalid by adding some characters. For
474 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
475 // request for the drive letter whose native path is
476 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
477 // because drives are numbered in order and mounting 112345 hard disks will
478 // never happen.
479 const FilePath::StringType kExtraChars = FPL("12345");
480
481 FilePath real_device_path_plus_numbers(
482 actual_device_path.value() + kExtraChars);
483
484 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
485 real_device_path_plus_numbers,
486 &win32_path));
487
488 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
489 real_device_path_plus_numbers.Append(kRelativePath),
490 &win32_path));
491}
492
cpu@chromium.orge6490ed2011-12-30 07:59:22 +0900493TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
494 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test"));
495 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900496 win::ScopedHandle dir(
cpu@chromium.orge6490ed2011-12-30 07:59:22 +0900497 ::CreateFile(empty_dir.value().c_str(),
498 FILE_ALL_ACCESS,
499 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
500 NULL,
501 OPEN_EXISTING,
502 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
503 NULL));
504 ASSERT_TRUE(dir.IsValid());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900505 PlatformFileInfo info;
506 EXPECT_TRUE(GetPlatformFileInfo(dir.Get(), &info));
cpu@chromium.orge6490ed2011-12-30 07:59:22 +0900507 EXPECT_TRUE(info.is_directory);
508 EXPECT_FALSE(info.is_symbolic_link);
509 EXPECT_EQ(0, info.size);
510}
511
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900512TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
513 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
514 // if it is available. This test requires that:
515 // - the filesystem at |temp_dir_| supports long filenames.
516 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
517 // directories of |temp_dir_|.
518 const FilePath::CharType kLongDirName[] = FPL("A long path");
519 const FilePath::CharType kTestSubDirName[] = FPL("test");
520 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
521 ASSERT_TRUE(file_util::CreateDirectory(long_test_dir));
522
523 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
524 // different short name.
525 WCHAR path_buffer[MAX_PATH];
526 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
527 path_buffer, MAX_PATH);
528 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
529 ASSERT_NE(DWORD(0), path_buffer_length);
530 FilePath short_test_dir(path_buffer);
531 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
532
533 FilePath temp_file;
534 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir, &temp_file));
535 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900536 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900537
538 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
539 // unreadable. We should still be able to create a temp file in the
540 // subdirectory, but we won't be able to determine the long path for it. This
541 // mimics the environment that some users run where their user profiles reside
542 // in a location where the don't have full access to the higher level
543 // directories. (Note that this assumption is true for NTFS, but not for some
544 // network file systems. E.g. AFS).
545 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
546 ASSERT_TRUE(file_util::CreateDirectory(access_test_dir));
547 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
548 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
549
550 // Use the short form of the directory to create a temporary filename.
551 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
552 short_test_dir.Append(kTestSubDirName), &temp_file));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900553 EXPECT_TRUE(PathExists(temp_file));
asanka@chromium.org0f9d10c2012-11-13 01:28:07 +0900554 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
555
556 // Check that the long path can't be determined for |temp_file|.
557 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
558 path_buffer, MAX_PATH);
559 EXPECT_EQ(DWORD(0), path_buffer_length);
560}
561
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900562#endif // defined(OS_WIN)
563
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900564#if defined(OS_POSIX)
565
566TEST_F(FileUtilTest, CreateAndReadSymlinks) {
567 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
568 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
569 CreateTextFile(link_to, bogus_content);
570
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900571 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900572 << "Failed to create file symlink.";
573
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900574 // If we created the link properly, we should be able to read the contents
575 // through it.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900576 std::wstring contents = ReadTextFile(link_from);
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900577 EXPECT_EQ(bogus_content, contents);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900578
579 FilePath result;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900580 ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900581 EXPECT_EQ(link_to.value(), result.value());
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900582
583 // Link to a directory.
584 link_from = temp_dir_.path().Append(FPL("from_dir"));
585 link_to = temp_dir_.path().Append(FPL("to_dir"));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900586 ASSERT_TRUE(file_util::CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900587 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900588 << "Failed to create directory symlink.";
589
590 // Test failures.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900591 EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
592 EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900593 FilePath missing = temp_dir_.path().Append(FPL("missing"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900594 EXPECT_FALSE(ReadSymbolicLink(missing, &result));
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900595}
596
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900597// The following test of NormalizeFilePath() require that we create a symlink.
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900598// This can not be done on Windows before Vista. On Vista, creating a symlink
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900599// requires privilege "SeCreateSymbolicLinkPrivilege".
600// TODO(skerner): Investigate the possibility of giving base_unittests the
601// privileges required to create a symlink.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900602TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
skerner@chromium.org559baa92010-05-13 00:13:57 +0900603 // Link one file to another.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900604 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
605 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900606 CreateTextFile(link_to, bogus_content);
607
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900608 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900609 << "Failed to create file symlink.";
610
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900611 // Check that NormalizeFilePath sees the link.
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900612 FilePath normalized_path;
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900613 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900614 EXPECT_NE(link_from, link_to);
615 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
616 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900617
618 // Link to a directory.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900619 link_from = temp_dir_.path().Append(FPL("from_dir"));
620 link_to = temp_dir_.path().Append(FPL("to_dir"));
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900621 ASSERT_TRUE(file_util::CreateDirectory(link_to));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900622 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900623 << "Failed to create directory symlink.";
624
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900625 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900626 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900627
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900628 // Test that a loop in the links causes NormalizeFilePath() to return false.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900629 link_from = temp_dir_.path().Append(FPL("link_a"));
630 link_to = temp_dir_.path().Append(FPL("link_b"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900631 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900632 << "Failed to create loop symlink a.";
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900633 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900634 << "Failed to create loop symlink b.";
635
636 // Infinite loop!
gavinp@chromium.orgd83141b2013-07-04 02:11:43 +0900637 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900638}
639#endif // defined(OS_POSIX)
640
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900641TEST_F(FileUtilTest, DeleteNonExistent) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900642 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900643 ASSERT_FALSE(PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900644
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900645 EXPECT_TRUE(DeleteFile(non_existent, false));
646 ASSERT_FALSE(PathExists(non_existent));
647 EXPECT_TRUE(DeleteFile(non_existent, true));
648 ASSERT_FALSE(PathExists(non_existent));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900649}
650
651TEST_F(FileUtilTest, DeleteFile) {
652 // Create a file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900653 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900654 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900655 ASSERT_TRUE(PathExists(file_name));
initial.commit3f4a7322008-07-27 06:49:38 +0900656
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900657 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900658 EXPECT_TRUE(DeleteFile(file_name, false));
659 EXPECT_FALSE(PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900660
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900661 // Test recursive case, create a new file
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900662 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900663 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900664 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900665
666 // Make sure it's deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900667 EXPECT_TRUE(DeleteFile(file_name, true));
668 EXPECT_FALSE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900669}
670
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900671#if defined(OS_POSIX)
672TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900673 // Create a file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900674 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
675 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900676 ASSERT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900677
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900678 // Create a symlink to the file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900679 FilePath file_link = temp_dir_.path().Append("file_link_2");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900680 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900681 << "Failed to create symlink.";
682
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900683 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900684 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900685
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900686 // Make sure original file is not deleted.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900687 EXPECT_FALSE(PathExists(file_link));
688 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900689}
690
691TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900692 // Create a non-existent file path.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900693 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900694 EXPECT_FALSE(PathExists(non_existent));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900695
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900696 // Create a symlink to the non-existent file.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900697 FilePath file_link = temp_dir_.path().Append("file_link_3");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900698 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900699 << "Failed to create symlink.";
700
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900701 // Make sure the symbolic link is exist.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900702 EXPECT_TRUE(file_util::IsLink(file_link));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900703 EXPECT_FALSE(PathExists(file_link));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900704
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900705 // Delete the symbolic link.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900706 EXPECT_TRUE(DeleteFile(file_link, false));
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900707
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900708 // Make sure the symbolic link is deleted.
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900709 EXPECT_FALSE(file_util::IsLink(file_link));
710}
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900711
712TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
713 // Create a file path.
714 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900715 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900716
717 const std::string kData("hello");
718
719 int buffer_size = kData.length();
720 char* buffer = new char[buffer_size];
721
722 // Write file.
723 EXPECT_EQ(static_cast<int>(kData.length()),
724 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900725 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900726
727 // Make sure the file is readable.
728 int32 mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900729 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
730 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900731
732 // Get rid of the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900733 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
734 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
735 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900736 // Make sure the file can't be read.
737 EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, buffer_size));
738
739 // Give the read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900740 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
741 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
742 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900743 // Make sure the file can be read.
744 EXPECT_EQ(static_cast<int>(kData.length()),
745 file_util::ReadFile(file_name, buffer, buffer_size));
746
747 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900748 EXPECT_TRUE(DeleteFile(file_name, false));
749 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900750
751 delete[] buffer;
752}
753
754TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
755 // Create a file path.
756 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900757 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900758
759 const std::string kData("hello");
760
761 // Write file.
762 EXPECT_EQ(static_cast<int>(kData.length()),
763 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900764 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900765
766 // Make sure the file is writable.
767 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900768 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
769 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900770 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900771
772 // Get rid of the write permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900773 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
774 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
775 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900776 // Make sure the file can't be write.
777 EXPECT_EQ(-1,
778 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900779 EXPECT_FALSE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900780
781 // Give read permission.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900782 EXPECT_TRUE(SetPosixFilePermissions(file_name,
783 FILE_PERMISSION_WRITE_BY_USER));
784 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
785 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900786 // Make sure the file can be write.
787 EXPECT_EQ(static_cast<int>(kData.length()),
788 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org5a112e72013-07-16 05:18:09 +0900789 EXPECT_TRUE(PathIsWritable(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900790
791 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900792 EXPECT_TRUE(DeleteFile(file_name, false));
793 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900794}
795
796TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
797 // Create a directory path.
798 FilePath subdir_path =
799 temp_dir_.path().Append(FPL("PermissionTest1"));
800 file_util::CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900801 ASSERT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900802
803 // Create a dummy file to enumerate.
804 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900805 EXPECT_FALSE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900806 const std::string kData("hello");
807 EXPECT_EQ(static_cast<int>(kData.length()),
808 file_util::WriteFile(file_name, kData.data(), kData.length()));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900809 EXPECT_TRUE(PathExists(file_name));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900810
811 // Make sure the directory has the all permissions.
812 int mode = 0;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900813 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
814 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900815
816 // Get rid of the permissions from the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900817 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
818 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
819 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900820
821 // Make sure the file in the directory can't be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900822 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900823 EXPECT_TRUE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900824 FindResultCollector c1(f1);
825 EXPECT_EQ(c1.size(), 0);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900826 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900827
828 // Give the permissions to the directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900829 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
830 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
831 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900832
833 // Make sure the file in the directory can be enumerated.
brettw@chromium.org56946722013-06-08 13:53:36 +0900834 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900835 FindResultCollector c2(f2);
836 EXPECT_TRUE(c2.HasFile(file_name));
837 EXPECT_EQ(c2.size(), 1);
838
839 // Delete the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900840 EXPECT_TRUE(DeleteFile(subdir_path, true));
841 EXPECT_FALSE(PathExists(subdir_path));
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +0900842}
843
yoshiki@chromium.org45cbd632012-06-30 14:26:59 +0900844#endif // defined(OS_POSIX)
845
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900846#if defined(OS_WIN)
847// Tests that the Delete function works for wild cards, especially
848// with the recursion flag. Also coincidentally tests PathExists.
849// TODO(erikkay): see if anyone's actually using this feature of the API
850TEST_F(FileUtilTest, DeleteWildCard) {
851 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900852 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900853 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900854 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900855
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900856 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900857 file_util::CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900858 ASSERT_TRUE(PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900859
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900860 // Create the wildcard path
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900861 FilePath directory_contents = temp_dir_.path();
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900862 directory_contents = directory_contents.Append(FPL("*"));
863
initial.commit3f4a7322008-07-27 06:49:38 +0900864 // Delete non-recursively and check that only the file is deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900865 EXPECT_TRUE(DeleteFile(directory_contents, false));
866 EXPECT_FALSE(PathExists(file_name));
867 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900868
zork@chromium.org61be4f42010-05-07 09:05:36 +0900869 // Delete recursively and make sure all contents are deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900870 EXPECT_TRUE(DeleteFile(directory_contents, true));
871 EXPECT_FALSE(PathExists(file_name));
872 EXPECT_FALSE(PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900873}
874
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900875// TODO(erikkay): see if anyone's actually using this feature of the API
876TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
877 // Create a file and a directory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900878 FilePath subdir_path =
879 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900880 file_util::CreateDirectory(subdir_path);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900881 ASSERT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900882
883 // Create the wildcard path
884 FilePath directory_contents = subdir_path;
885 directory_contents = directory_contents.Append(FPL("*"));
886
887 // Delete non-recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900888 EXPECT_TRUE(DeleteFile(directory_contents, false));
889 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900890
891 // Delete recursively and check nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900892 EXPECT_TRUE(DeleteFile(directory_contents, true));
893 EXPECT_TRUE(PathExists(subdir_path));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900894}
895#endif
896
897// Tests non-recursive Delete() for a directory.
898TEST_F(FileUtilTest, DeleteDirNonRecursive) {
899 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900900 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900901 file_util::CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900902 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900903
904 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
905 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900906 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900907
908 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
909 file_util::CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900910 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900911
912 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
913 file_util::CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900914 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900915
916 // Delete non-recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900917 EXPECT_TRUE(DeleteFile(subdir_path2, false));
918 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900919
920 // Delete non-recursively and check that nothing got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900921 EXPECT_FALSE(DeleteFile(test_subdir, false));
922 EXPECT_TRUE(PathExists(test_subdir));
923 EXPECT_TRUE(PathExists(file_name));
924 EXPECT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900925}
926
927// Tests recursive Delete() for a directory.
928TEST_F(FileUtilTest, DeleteDirRecursive) {
929 // Create a subdirectory and put a file and two directories inside.
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900930 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900931 file_util::CreateDirectory(test_subdir);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900932 ASSERT_TRUE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900933
934 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
935 CreateTextFile(file_name, bogus_content);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900936 ASSERT_TRUE(PathExists(file_name));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900937
938 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
939 file_util::CreateDirectory(subdir_path1);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900940 ASSERT_TRUE(PathExists(subdir_path1));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900941
942 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
943 file_util::CreateDirectory(subdir_path2);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900944 ASSERT_TRUE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900945
946 // Delete recursively and check that the empty dir got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900947 EXPECT_TRUE(DeleteFile(subdir_path2, true));
948 EXPECT_FALSE(PathExists(subdir_path2));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900949
950 // Delete recursively and check that everything got deleted
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900951 EXPECT_TRUE(DeleteFile(test_subdir, true));
952 EXPECT_FALSE(PathExists(file_name));
953 EXPECT_FALSE(PathExists(subdir_path1));
954 EXPECT_FALSE(PathExists(test_subdir));
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900955}
956
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900957TEST_F(FileUtilTest, MoveFileNew) {
958 // Create a file
959 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900960 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900961 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900962 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900963
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900964 // The destination.
965 FilePath file_name_to = temp_dir_.path().Append(
966 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900967 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900968
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900969 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900970
971 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900972 EXPECT_FALSE(PathExists(file_name_from));
973 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900974}
975
976TEST_F(FileUtilTest, MoveFileExists) {
977 // Create a file
978 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900979 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900980 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900981 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900982
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +0900983 // The destination name.
984 FilePath file_name_to = temp_dir_.path().Append(
985 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900986 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900987 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900988
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900989 EXPECT_TRUE(Move(file_name_from, file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900990
991 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900992 EXPECT_FALSE(PathExists(file_name_from));
993 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900994 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
995}
996
997TEST_F(FileUtilTest, MoveFileDirExists) {
998 // Create a file
999 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001000 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001001 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001002 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001003
1004 // The destination directory
1005 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001006 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001007 file_util::CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001008 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001009
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001010 EXPECT_FALSE(Move(file_name_from, dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001011}
1012
1013
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001014TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001015 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001016 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001017 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001018 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001019 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001020
1021 // Create a file under the directory
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001022 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1023 FilePath file_name_from = dir_name_from.Append(txt_file_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001024 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001025 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001026
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001027 // Move the directory.
1028 FilePath dir_name_to =
1029 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001030 FilePath file_name_to =
1031 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001032
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001033 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001034
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001035 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001036
1037 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001038 EXPECT_FALSE(PathExists(dir_name_from));
1039 EXPECT_FALSE(PathExists(file_name_from));
1040 EXPECT_TRUE(PathExists(dir_name_to));
1041 EXPECT_TRUE(PathExists(file_name_to));
cevans@chromium.org007dbe22013-02-07 05:38:07 +09001042
1043 // Test path traversal.
1044 file_name_from = dir_name_to.Append(txt_file_name);
1045 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1046 file_name_to = file_name_to.Append(txt_file_name);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001047 EXPECT_FALSE(Move(file_name_from, file_name_to));
1048 EXPECT_TRUE(PathExists(file_name_from));
1049 EXPECT_FALSE(PathExists(file_name_to));
1050 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1051 EXPECT_FALSE(PathExists(file_name_from));
1052 EXPECT_TRUE(PathExists(file_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001053}
1054
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001055TEST_F(FileUtilTest, MoveExist) {
1056 // Create a directory
1057 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001058 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001059 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001060 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001061
1062 // Create a file under the directory
1063 FilePath file_name_from =
1064 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1065 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001066 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001067
1068 // Move the directory
1069 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001070 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001071
1072 FilePath dir_name_to =
1073 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1074 FilePath file_name_to =
1075 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1076
1077 // Create the destination directory.
1078 file_util::CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001079 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001080
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001081 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001082
1083 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001084 EXPECT_FALSE(PathExists(dir_name_from));
1085 EXPECT_FALSE(PathExists(file_name_from));
1086 EXPECT_TRUE(PathExists(dir_name_to));
1087 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001088}
1089
1090TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001091 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001092 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001093 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001094 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001095 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001096
1097 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001098 FilePath file_name_from =
1099 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001100 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001101 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001102
1103 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001104 FilePath subdir_name_from =
1105 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1106 file_util::CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001107 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001108
1109 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001110 FilePath file_name2_from =
1111 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001112 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001113 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001114
1115 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001116 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001117 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001118 FilePath file_name_to =
1119 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1120 FilePath subdir_name_to =
1121 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1122 FilePath file_name2_to =
1123 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001124
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001125 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001126
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001127 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001128
1129 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001130 EXPECT_TRUE(PathExists(dir_name_from));
1131 EXPECT_TRUE(PathExists(file_name_from));
1132 EXPECT_TRUE(PathExists(subdir_name_from));
1133 EXPECT_TRUE(PathExists(file_name2_from));
1134 EXPECT_TRUE(PathExists(dir_name_to));
1135 EXPECT_TRUE(PathExists(file_name_to));
1136 EXPECT_TRUE(PathExists(subdir_name_to));
1137 EXPECT_TRUE(PathExists(file_name2_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001138}
1139
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001140TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1141 // Create a directory.
1142 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001143 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001144 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001145 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001146
1147 // Create a file under the directory.
1148 FilePath file_name_from =
1149 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1150 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001151 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001152
1153 // Create a subdirectory.
1154 FilePath subdir_name_from =
1155 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1156 file_util::CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001157 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001158
1159 // Create a file under the subdirectory.
1160 FilePath file_name2_from =
1161 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1162 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001163 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001164
1165 // Copy the directory recursively.
1166 FilePath dir_name_exists =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001167 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001168
1169 FilePath dir_name_to =
1170 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1171 FilePath file_name_to =
1172 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1173 FilePath subdir_name_to =
1174 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1175 FilePath file_name2_to =
1176 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1177
1178 // Create the destination directory.
1179 file_util::CreateDirectory(dir_name_exists);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001180 ASSERT_TRUE(PathExists(dir_name_exists));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001181
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001182 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001183
1184 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001185 EXPECT_TRUE(PathExists(dir_name_from));
1186 EXPECT_TRUE(PathExists(file_name_from));
1187 EXPECT_TRUE(PathExists(subdir_name_from));
1188 EXPECT_TRUE(PathExists(file_name2_from));
1189 EXPECT_TRUE(PathExists(dir_name_to));
1190 EXPECT_TRUE(PathExists(file_name_to));
1191 EXPECT_TRUE(PathExists(subdir_name_to));
1192 EXPECT_TRUE(PathExists(file_name2_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001193}
1194
1195TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001196 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001197 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001198 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001199 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001200 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001201
1202 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001203 FilePath file_name_from =
1204 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001205 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001206 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001207
1208 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001209 FilePath subdir_name_from =
1210 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1211 file_util::CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001212 ASSERT_TRUE(PathExists(subdir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001213
1214 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001215 FilePath file_name2_from =
1216 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001217 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001218 ASSERT_TRUE(PathExists(file_name2_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001219
1220 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001221 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001222 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001223 FilePath file_name_to =
1224 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1225 FilePath subdir_name_to =
1226 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001227
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001228 ASSERT_FALSE(PathExists(dir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001229
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001230 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001231
1232 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001233 EXPECT_TRUE(PathExists(dir_name_from));
1234 EXPECT_TRUE(PathExists(file_name_from));
1235 EXPECT_TRUE(PathExists(subdir_name_from));
1236 EXPECT_TRUE(PathExists(file_name2_from));
1237 EXPECT_TRUE(PathExists(dir_name_to));
1238 EXPECT_TRUE(PathExists(file_name_to));
1239 EXPECT_FALSE(PathExists(subdir_name_to));
initial.commit3f4a7322008-07-27 06:49:38 +09001240}
1241
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001242TEST_F(FileUtilTest, CopyDirectoryExists) {
1243 // Create a directory.
1244 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001245 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001246 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001247 ASSERT_TRUE(PathExists(dir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001248
1249 // Create a file under the directory.
1250 FilePath file_name_from =
1251 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1252 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001253 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001254
1255 // Create a subdirectory.
1256 FilePath subdir_name_from =
1257 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1258 file_util::CreateDirectory(subdir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001259 ASSERT_TRUE(PathExists(subdir_name_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001260
1261 // Create a file under the subdirectory.
1262 FilePath file_name2_from =
1263 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1264 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001265 ASSERT_TRUE(PathExists(file_name2_from));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001266
1267 // Copy the directory not recursively.
1268 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001269 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001270 FilePath file_name_to =
1271 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1272 FilePath subdir_name_to =
1273 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1274
1275 // Create the destination directory.
1276 file_util::CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001277 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001278
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001279 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001280
1281 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001282 EXPECT_TRUE(PathExists(dir_name_from));
1283 EXPECT_TRUE(PathExists(file_name_from));
1284 EXPECT_TRUE(PathExists(subdir_name_from));
1285 EXPECT_TRUE(PathExists(file_name2_from));
1286 EXPECT_TRUE(PathExists(dir_name_to));
1287 EXPECT_TRUE(PathExists(file_name_to));
1288 EXPECT_FALSE(PathExists(subdir_name_to));
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001289}
1290
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001291TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1292 // Create a file
1293 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001294 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001295 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001296 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001297
1298 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001299 FilePath file_name_to = temp_dir_.path().Append(
1300 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001301 ASSERT_FALSE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001302
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001303 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001304
1305 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001306 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001307}
1308
1309TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1310 // Create a file
1311 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001312 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001313 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001314 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001315
1316 // The destination name
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001317 FilePath file_name_to = temp_dir_.path().Append(
1318 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001319 CreateTextFile(file_name_to, L"Old file content");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001320 ASSERT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001321
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001322 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001323
1324 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001325 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001326 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1327}
1328
1329TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1330 // Create a file
1331 FilePath file_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001332 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001333 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001334 ASSERT_TRUE(PathExists(file_name_from));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001335
1336 // The destination
1337 FilePath dir_name_to =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001338 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001339 file_util::CreateDirectory(dir_name_to);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001340 ASSERT_TRUE(PathExists(dir_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001341 FilePath file_name_to =
1342 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1343
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001344 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001345
1346 // Check the has been copied
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001347 EXPECT_TRUE(PathExists(file_name_to));
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001348}
1349
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001350TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1351 // Create a directory.
1352 FilePath dir_name_from =
1353 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1354 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001355 ASSERT_TRUE(PathExists(dir_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001356
1357 // Create a file under the directory.
1358 FilePath file_name_from =
1359 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1360 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001361 ASSERT_TRUE(PathExists(file_name_from));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001362
1363 // Copy the directory recursively.
1364 FilePath dir_name_to =
1365 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1366 FilePath file_name_to =
1367 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1368
1369 // Create from path with trailing separators.
1370#if defined(OS_WIN)
1371 FilePath from_path =
1372 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1373#elif defined (OS_POSIX)
1374 FilePath from_path =
1375 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1376#endif
1377
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001378 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001379
1380 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001381 EXPECT_TRUE(PathExists(dir_name_from));
1382 EXPECT_TRUE(PathExists(file_name_from));
1383 EXPECT_TRUE(PathExists(dir_name_to));
1384 EXPECT_TRUE(PathExists(file_name_to));
aedla@chromium.orgfef1a202013-01-30 20:38:02 +09001385}
1386
initial.commit3f4a7322008-07-27 06:49:38 +09001387TEST_F(FileUtilTest, CopyFile) {
1388 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001389 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001390 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
evanm@google.com874d1672008-10-31 08:54:04 +09001391 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001392 ASSERT_TRUE(PathExists(dir_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001393
1394 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001395 FilePath file_name_from =
1396 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001397 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1398 CreateTextFile(file_name_from, file_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001399 ASSERT_TRUE(PathExists(file_name_from));
initial.commit3f4a7322008-07-27 06:49:38 +09001400
1401 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001402 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001403 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001404
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001405 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001406 FilePath dest_file2(dir_name_from);
1407 dest_file2 = dest_file2.AppendASCII("..");
1408 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001409 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
1410 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from, dest_file2));
evan@chromium.org1543ad32009-08-27 05:00:14 +09001411
1412 FilePath dest_file2_test(dir_name_from);
1413 dest_file2_test = dest_file2_test.DirName();
1414 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001415
1416 // Check everything has been copied.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001417 EXPECT_TRUE(PathExists(file_name_from));
1418 EXPECT_TRUE(PathExists(dest_file));
initial.commit3f4a7322008-07-27 06:49:38 +09001419 const std::wstring read_contents = ReadTextFile(dest_file);
1420 EXPECT_EQ(file_contents, read_contents);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001421 EXPECT_TRUE(PathExists(dest_file2_test));
1422 EXPECT_TRUE(PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001423}
1424
erikkay@google.comf2406842008-08-21 00:59:49 +09001425// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001426// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001427typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001428
erikkay@google.comf2406842008-08-21 00:59:49 +09001429TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001430 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001431 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001432 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001433 ASSERT_TRUE(PathExists(data_dir));
initial.commit3f4a7322008-07-27 06:49:38 +09001434
evanm@google.com874d1672008-10-31 08:54:04 +09001435 FilePath original_file =
1436 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1437 FilePath same_file =
1438 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1439 FilePath same_length_file =
1440 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1441 FilePath different_file =
1442 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1443 FilePath different_first_file =
1444 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1445 FilePath different_last_file =
1446 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1447 FilePath empty1_file =
1448 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1449 FilePath empty2_file =
1450 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1451 FilePath shortened_file =
1452 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1453 FilePath binary_file =
1454 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1455 FilePath binary_file_same =
1456 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1457 FilePath binary_file_diff =
1458 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001459
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001460 EXPECT_TRUE(ContentsEqual(original_file, original_file));
1461 EXPECT_TRUE(ContentsEqual(original_file, same_file));
1462 EXPECT_FALSE(ContentsEqual(original_file, same_length_file));
1463 EXPECT_FALSE(ContentsEqual(original_file, different_file));
1464 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1465 FilePath(FILE_PATH_LITERAL("bogusname"))));
1466 EXPECT_FALSE(ContentsEqual(original_file, different_first_file));
1467 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1468 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1469 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1470 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1471 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1472 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
initial.commit3f4a7322008-07-27 06:49:38 +09001473}
1474
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001475TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1476 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001477 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
tfarina@chromium.orgd05540d2013-04-08 01:27:46 +09001478 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001479 ASSERT_TRUE(PathExists(data_dir));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001480
1481 FilePath original_file =
1482 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1483 FilePath same_file =
1484 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1485 FilePath crlf_file =
1486 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1487 FilePath shortened_file =
1488 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1489 FilePath different_file =
1490 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1491 FilePath different_first_file =
1492 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1493 FilePath different_last_file =
1494 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1495 FilePath first1_file =
1496 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1497 FilePath first2_file =
1498 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1499 FilePath empty1_file =
1500 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1501 FilePath empty2_file =
1502 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1503 FilePath blank_line_file =
1504 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1505 FilePath blank_line_crlf_file =
1506 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1507
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001508 EXPECT_TRUE(TextContentsEqual(original_file, same_file));
1509 EXPECT_TRUE(TextContentsEqual(original_file, crlf_file));
1510 EXPECT_FALSE(TextContentsEqual(original_file, shortened_file));
1511 EXPECT_FALSE(TextContentsEqual(original_file, different_file));
1512 EXPECT_FALSE(TextContentsEqual(original_file, different_first_file));
1513 EXPECT_FALSE(TextContentsEqual(original_file, different_last_file));
1514 EXPECT_FALSE(TextContentsEqual(first1_file, first2_file));
1515 EXPECT_TRUE(TextContentsEqual(empty1_file, empty2_file));
1516 EXPECT_FALSE(TextContentsEqual(original_file, empty1_file));
1517 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001518}
1519
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001520// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001521#if defined(OS_WIN)
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001522TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1523 // Create a directory
1524 FilePath dir_name_from =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001525 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001526 file_util::CreateDirectory(dir_name_from);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001527 ASSERT_TRUE(PathExists(dir_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001528
1529 // Create a file under the directory
1530 FilePath file_name_from =
1531 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1532 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001533 ASSERT_TRUE(PathExists(file_name_from));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001534
1535 // Move the directory by using CopyAndDeleteDirectory
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001536 FilePath dir_name_to = temp_dir_.path().Append(
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001537 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1538 FilePath file_name_to =
1539 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1540
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001541 ASSERT_FALSE(PathExists(dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001542
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001543 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
brettw@chromium.orgaecf7a32013-07-10 02:42:26 +09001544 dir_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001545
1546 // Check everything has been moved.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001547 EXPECT_FALSE(PathExists(dir_name_from));
1548 EXPECT_FALSE(PathExists(file_name_from));
1549 EXPECT_TRUE(PathExists(dir_name_to));
1550 EXPECT_TRUE(PathExists(file_name_to));
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001551}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001552
1553TEST_F(FileUtilTest, GetTempDirTest) {
1554 static const TCHAR* kTmpKey = _T("TMP");
1555 static const TCHAR* kTmpValues[] = {
1556 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1557 };
1558 // Save the original $TMP.
1559 size_t original_tmp_size;
1560 TCHAR* original_tmp;
1561 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1562 // original_tmp may be NULL.
1563
1564 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1565 FilePath path;
1566 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1567 file_util::GetTempDir(&path);
1568 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1569 " result=" << path.value();
1570 }
1571
1572 // Restore the original $TMP.
1573 if (original_tmp) {
1574 ::_tputenv_s(kTmpKey, original_tmp);
1575 free(original_tmp);
1576 } else {
1577 ::_tputenv_s(kTmpKey, _T(""));
1578 }
1579}
1580#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001581
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001582TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1583 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001584 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001585 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001586 EXPECT_TRUE(PathExists(temp_files[i]));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001587 EXPECT_FALSE(DirectoryExists(temp_files[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001588 }
1589 for (int i = 0; i < 3; i++)
1590 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1591 for (int i = 0; i < 3; i++)
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001592 EXPECT_TRUE(DeleteFile(temp_files[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001593}
1594
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001595TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001596 FilePath names[3];
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001597 FILE* fps[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001598 int i;
1599
1600 // Create; make sure they are open and exist.
1601 for (i = 0; i < 3; ++i) {
1602 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1603 ASSERT_TRUE(fps[i]);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001604 EXPECT_TRUE(PathExists(names[i]));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001605 }
1606
1607 // Make sure all names are unique.
1608 for (i = 0; i < 3; ++i) {
1609 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1610 }
1611
1612 // Close and delete.
1613 for (i = 0; i < 3; ++i) {
1614 EXPECT_TRUE(file_util::CloseFile(fps[i]));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001615 EXPECT_TRUE(DeleteFile(names[i], false));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001616 }
initial.commit3f4a7322008-07-27 06:49:38 +09001617}
1618
1619TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001620 FilePath temp_dir;
1621 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1622 &temp_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001623 EXPECT_TRUE(PathExists(temp_dir));
1624 EXPECT_TRUE(DeleteFile(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001625}
1626
skerner@chromium.orge4432392010-05-01 02:00:09 +09001627TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1628 FilePath new_dir;
1629 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001630 temp_dir_.path(),
skerner@chromium.orge4432392010-05-01 02:00:09 +09001631 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001632 &new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001633 EXPECT_TRUE(PathExists(new_dir));
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001634 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001635 EXPECT_TRUE(DeleteFile(new_dir, false));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001636}
1637
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001638TEST_F(FileUtilTest, GetShmemTempDirTest) {
1639 FilePath dir;
mcgrathr@chromium.org569a4232011-12-07 03:07:05 +09001640 EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001641 EXPECT_TRUE(DirectoryExists(dir));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001642}
1643
initial.commit3f4a7322008-07-27 06:49:38 +09001644TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001645 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001646 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001647#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001648 FilePath test_path =
1649 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001650#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001651 FilePath test_path =
1652 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001653#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001654
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001655 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001656 EXPECT_TRUE(file_util::CreateDirectory(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001657 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001658 // CreateDirectory returns true if the DirectoryExists returns true.
1659 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1660
1661 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001662 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001663 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001664 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001665 EXPECT_TRUE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001666 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1667
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001668 EXPECT_TRUE(DeleteFile(test_root, true));
1669 EXPECT_FALSE(PathExists(test_root));
1670 EXPECT_FALSE(PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001671
1672 // Verify assumptions made by the Windows implementation:
1673 // 1. The current directory always exists.
1674 // 2. The root directory always exists.
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001675 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001676 FilePath top_level = test_root;
1677 while (top_level != top_level.DirName()) {
1678 top_level = top_level.DirName();
1679 }
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001680 ASSERT_TRUE(DirectoryExists(top_level));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001681
1682 // Given these assumptions hold, it should be safe to
1683 // test that "creating" these directories succeeds.
1684 EXPECT_TRUE(file_util::CreateDirectory(
1685 FilePath(FilePath::kCurrentDirectory)));
1686 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001687
1688#if defined(OS_WIN)
1689 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1690 FilePath invalid_path =
1691 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001692 if (!PathExists(invalid_drive)) {
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001693 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1694 }
1695#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001696}
1697
1698TEST_F(FileUtilTest, DetectDirectoryTest) {
1699 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001700 FilePath test_root =
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001701 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001702 EXPECT_FALSE(PathExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001703 EXPECT_TRUE(file_util::CreateDirectory(test_root));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001704 EXPECT_TRUE(PathExists(test_root));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001705 EXPECT_TRUE(DirectoryExists(test_root));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001706 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001707 FilePath test_path =
1708 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001709 EXPECT_FALSE(PathExists(test_path));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001710 CreateTextFile(test_path, L"test file");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001711 EXPECT_TRUE(PathExists(test_path));
brettw@chromium.org5a112e72013-07-16 05:18:09 +09001712 EXPECT_FALSE(DirectoryExists(test_path));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001713 EXPECT_TRUE(DeleteFile(test_path, false));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001714
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001715 EXPECT_TRUE(DeleteFile(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001716}
1717
initial.commit3f4a7322008-07-27 06:49:38 +09001718TEST_F(FileUtilTest, FileEnumeratorTest) {
1719 // Test an empty directory.
brettw@chromium.org56946722013-06-08 13:53:36 +09001720 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001721 EXPECT_EQ(f0.Next().value(), FPL(""));
1722 EXPECT_EQ(f0.Next().value(), FPL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001723
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001724 // Test an empty directory, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001725 FileEnumerator f0_dotdot(temp_dir_.path(), false,
1726 FILES_AND_DIRECTORIES | FileEnumerator::INCLUDE_DOT_DOT);
rvargas@chromium.org56472942013-08-15 05:46:05 +09001727 EXPECT_EQ(temp_dir_.path().Append(FPL("..")).value(),
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001728 f0_dotdot.Next().value());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001729 EXPECT_EQ(FPL(""), f0_dotdot.Next().value());
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001730
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001731 // create the directories
rvargas@chromium.org56472942013-08-15 05:46:05 +09001732 FilePath dir1 = temp_dir_.path().Append(FPL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001733 EXPECT_TRUE(file_util::CreateDirectory(dir1));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001734 FilePath dir2 = temp_dir_.path().Append(FPL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001735 EXPECT_TRUE(file_util::CreateDirectory(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001736 FilePath dir2inner = dir2.Append(FPL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001737 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001738
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001739 // create the files
rvargas@chromium.org56472942013-08-15 05:46:05 +09001740 FilePath dir2file = dir2.Append(FPL("dir2file.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001741 CreateTextFile(dir2file, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001742 FilePath dir2innerfile = dir2inner.Append(FPL("innerfile.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001743 CreateTextFile(dir2innerfile, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001744 FilePath file1 = temp_dir_.path().Append(FPL("file1.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001745 CreateTextFile(file1, std::wstring());
1746 FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
rvargas@chromium.org56472942013-08-15 05:46:05 +09001747 .Append(FPL("file2.txt"));
dcheng@chromium.org8164c2c2013-04-09 17:46:45 +09001748 CreateTextFile(file2_rel, std::wstring());
rvargas@chromium.org56472942013-08-15 05:46:05 +09001749 FilePath file2_abs = temp_dir_.path().Append(FPL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001750
1751 // Only enumerate files.
brettw@chromium.org56946722013-06-08 13:53:36 +09001752 FileEnumerator f1(temp_dir_.path(), true, FileEnumerator::FILES);
initial.commit3f4a7322008-07-27 06:49:38 +09001753 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001754 EXPECT_TRUE(c1.HasFile(file1));
1755 EXPECT_TRUE(c1.HasFile(file2_abs));
1756 EXPECT_TRUE(c1.HasFile(dir2file));
1757 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1758 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001759
1760 // Only enumerate directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001761 FileEnumerator f2(temp_dir_.path(), true, FileEnumerator::DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001762 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001763 EXPECT_TRUE(c2.HasFile(dir1));
1764 EXPECT_TRUE(c2.HasFile(dir2));
1765 EXPECT_TRUE(c2.HasFile(dir2inner));
1766 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001767
tim@chromium.org989d0972008-10-16 11:42:45 +09001768 // Only enumerate directories non-recursively.
brettw@chromium.org56946722013-06-08 13:53:36 +09001769 FileEnumerator f2_non_recursive(
1770 temp_dir_.path(), false, FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001771 FindResultCollector c2_non_recursive(f2_non_recursive);
1772 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1773 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1774 EXPECT_EQ(c2_non_recursive.size(), 2);
1775
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001776 // Only enumerate directories, non-recursively, including "..".
brettw@chromium.org56946722013-06-08 13:53:36 +09001777 FileEnumerator f2_dotdot(temp_dir_.path(), false,
1778 FileEnumerator::DIRECTORIES |
1779 FileEnumerator::INCLUDE_DOT_DOT);
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001780 FindResultCollector c2_dotdot(f2_dotdot);
1781 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1782 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
rvargas@chromium.org56472942013-08-15 05:46:05 +09001783 EXPECT_TRUE(c2_dotdot.HasFile(temp_dir_.path().Append(FPL(".."))));
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001784 EXPECT_EQ(c2_dotdot.size(), 3);
1785
initial.commit3f4a7322008-07-27 06:49:38 +09001786 // Enumerate files and directories.
brettw@chromium.org56946722013-06-08 13:53:36 +09001787 FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001788 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001789 EXPECT_TRUE(c3.HasFile(dir1));
1790 EXPECT_TRUE(c3.HasFile(dir2));
1791 EXPECT_TRUE(c3.HasFile(file1));
1792 EXPECT_TRUE(c3.HasFile(file2_abs));
1793 EXPECT_TRUE(c3.HasFile(dir2file));
1794 EXPECT_TRUE(c3.HasFile(dir2inner));
1795 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1796 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001797
1798 // Non-recursive operation.
brettw@chromium.org56946722013-06-08 13:53:36 +09001799 FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001800 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001801 EXPECT_TRUE(c4.HasFile(dir2));
1802 EXPECT_TRUE(c4.HasFile(dir2));
1803 EXPECT_TRUE(c4.HasFile(file1));
1804 EXPECT_TRUE(c4.HasFile(file2_abs));
1805 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001806
1807 // Enumerate with a pattern.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001808 FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES, FPL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001809 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001810 EXPECT_TRUE(c5.HasFile(dir1));
1811 EXPECT_TRUE(c5.HasFile(dir2));
1812 EXPECT_TRUE(c5.HasFile(dir2file));
1813 EXPECT_TRUE(c5.HasFile(dir2inner));
1814 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1815 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001816
rvargas@chromium.org56472942013-08-15 05:46:05 +09001817#if defined(OS_WIN)
1818 {
1819 // Make dir1 point to dir2.
1820 ReparsePoint reparse_point(dir1, dir2);
1821 EXPECT_TRUE(reparse_point.IsValid());
1822
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001823 if ((win::GetVersion() >= base::win::VERSION_VISTA)) {
rvargas@chromium.org56472942013-08-15 05:46:05 +09001824 // There can be a delay for the enumeration code to see the change on
1825 // the file system so skip this test for XP.
1826 // Enumerate the reparse point.
1827 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
1828 FindResultCollector c6(f6);
1829 FilePath inner2 = dir1.Append(FPL("inner"));
1830 EXPECT_TRUE(c6.HasFile(inner2));
1831 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1832 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
1833 EXPECT_EQ(c6.size(), 3);
1834 }
1835
1836 // No changes for non recursive operation.
1837 FileEnumerator f7(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
1838 FindResultCollector c7(f7);
1839 EXPECT_TRUE(c7.HasFile(dir2));
1840 EXPECT_TRUE(c7.HasFile(dir2));
1841 EXPECT_TRUE(c7.HasFile(file1));
1842 EXPECT_TRUE(c7.HasFile(file2_abs));
1843 EXPECT_EQ(c7.size(), 4);
1844
1845 // Should not enumerate inside dir1 when using recursion.
1846 FileEnumerator f8(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1847 FindResultCollector c8(f8);
1848 EXPECT_TRUE(c8.HasFile(dir1));
1849 EXPECT_TRUE(c8.HasFile(dir2));
1850 EXPECT_TRUE(c8.HasFile(file1));
1851 EXPECT_TRUE(c8.HasFile(file2_abs));
1852 EXPECT_TRUE(c8.HasFile(dir2file));
1853 EXPECT_TRUE(c8.HasFile(dir2inner));
1854 EXPECT_TRUE(c8.HasFile(dir2innerfile));
1855 EXPECT_EQ(c8.size(), 7);
1856 }
1857#endif
1858
initial.commit3f4a7322008-07-27 06:49:38 +09001859 // Make sure the destructor closes the find handle while in the middle of a
1860 // query to allow TearDown to delete the directory.
rvargas@chromium.org56472942013-08-15 05:46:05 +09001861 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1862 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
avi@google.com5cb79352008-12-11 23:55:12 +09001863 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001864}
license.botf003cfe2008-08-24 09:55:55 +09001865
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001866TEST_F(FileUtilTest, AppendToFile) {
1867 FilePath data_dir =
1868 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1869
1870 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001871 if (PathExists(data_dir)) {
1872 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001873 }
1874 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1875
1876 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001877 if (PathExists(data_dir)) {
1878 ASSERT_TRUE(DeleteFile(data_dir, true));
loislo@chromium.orgeae0dcb2012-04-29 21:57:10 +09001879 }
1880 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1881 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1882
1883 std::string data("hello");
1884 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length()));
1885 EXPECT_EQ(static_cast<int>(data.length()),
1886 file_util::WriteFile(foobar, data.c_str(), data.length()));
1887 EXPECT_EQ(static_cast<int>(data.length()),
1888 file_util::AppendToFile(foobar, data.c_str(), data.length()));
1889
1890 const std::wstring read_content = ReadTextFile(foobar);
1891 EXPECT_EQ(L"hellohello", read_content);
1892}
1893
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001894TEST_F(FileUtilTest, TouchFile) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001895 FilePath data_dir =
1896 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
jochen@chromium.orga6879772010-02-18 19:02:26 +09001897
1898 // Create a fresh, empty copy of this directory.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001899 if (PathExists(data_dir)) {
1900 ASSERT_TRUE(DeleteFile(data_dir, true));
jochen@chromium.orga6879772010-02-18 19:02:26 +09001901 }
1902 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1903
1904 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1905 std::string data("hello");
1906 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1907
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001908 Time access_time;
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001909 // This timestamp is divisible by one day (in local timezone),
1910 // to make it work on FAT too.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001911 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001912 &access_time));
1913
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001914 Time modification_time;
jochen@chromium.orga6879772010-02-18 19:02:26 +09001915 // Note that this timestamp is divisible by two (seconds) - FAT stores
1916 // modification times with 2s resolution.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001917 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
jochen@chromium.orga6879772010-02-18 19:02:26 +09001918 &modification_time));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001919
1920 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001921 PlatformFileInfo file_info;
jochen@chromium.orga6879772010-02-18 19:02:26 +09001922 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
dumi@chromium.orgc941a182010-09-24 08:28:22 +09001923 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
1924 access_time.ToInternalValue());
1925 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
1926 modification_time.ToInternalValue());
jochen@chromium.orga6879772010-02-18 19:02:26 +09001927}
1928
tfarina@chromium.org34828222010-05-26 10:40:12 +09001929TEST_F(FileUtilTest, IsDirectoryEmpty) {
phajdan.jr@chromium.org8fe305d2010-09-16 05:40:47 +09001930 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
tfarina@chromium.org34828222010-05-26 10:40:12 +09001931
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001932 ASSERT_FALSE(PathExists(empty_dir));
tfarina@chromium.org34828222010-05-26 10:40:12 +09001933
1934 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1935
1936 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1937
1938 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1939 std::string bar("baz");
1940 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1941
1942 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1943}
1944
skerner@google.com93449ef2011-09-22 23:47:18 +09001945#if defined(OS_POSIX)
1946
1947// Testing VerifyPathControlledByAdmin() is hard, because there is no
1948// way a test can make a file owned by root, or change file paths
1949// at the root of the file system. VerifyPathControlledByAdmin()
1950// is implemented as a call to VerifyPathControlledByUser, which gives
1951// us the ability to test with paths under the test's temp directory,
1952// using a user id we control.
1953// Pull tests of VerifyPathControlledByUserTest() into a separate test class
1954// with a common SetUp() method.
1955class VerifyPathControlledByUserTest : public FileUtilTest {
1956 protected:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +09001957 virtual void SetUp() OVERRIDE {
skerner@google.com93449ef2011-09-22 23:47:18 +09001958 FileUtilTest::SetUp();
1959
1960 // Create a basic structure used by each test.
1961 // base_dir_
1962 // |-> sub_dir_
1963 // |-> text_file_
1964
1965 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
1966 ASSERT_TRUE(file_util::CreateDirectory(base_dir_));
1967
1968 sub_dir_ = base_dir_.AppendASCII("sub_dir");
1969 ASSERT_TRUE(file_util::CreateDirectory(sub_dir_));
1970
1971 text_file_ = sub_dir_.AppendASCII("file.txt");
1972 CreateTextFile(text_file_, L"This text file has some text in it.");
1973
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09001974 // Get the user and group files are created with from |base_dir_|.
1975 struct stat stat_buf;
1976 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
1977 uid_ = stat_buf.st_uid;
skerner@chromium.org80784142011-10-18 06:30:29 +09001978 ok_gids_.insert(stat_buf.st_gid);
1979 bad_gids_.insert(stat_buf.st_gid + 1);
1980
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09001981 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
skerner@google.com93449ef2011-09-22 23:47:18 +09001982
1983 // To ensure that umask settings do not cause the initial state
1984 // of permissions to be different from what we expect, explicitly
1985 // set permissions on the directories we create.
1986 // Make all files and directories non-world-writable.
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09001987
1988 // Users and group can read, write, traverse
1989 int enabled_permissions =
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001990 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
yoshiki@chromium.org670a38f2012-07-11 10:24:02 +09001991 // Other users can't read, write, traverse
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09001992 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
skerner@google.com93449ef2011-09-22 23:47:18 +09001993
1994 ASSERT_NO_FATAL_FAILURE(
1995 ChangePosixFilePermissions(
1996 base_dir_, enabled_permissions, disabled_permissions));
1997 ASSERT_NO_FATAL_FAILURE(
1998 ChangePosixFilePermissions(
1999 sub_dir_, enabled_permissions, disabled_permissions));
2000 }
2001
2002 FilePath base_dir_;
2003 FilePath sub_dir_;
2004 FilePath text_file_;
2005 uid_t uid_;
skerner@chromium.org80784142011-10-18 06:30:29 +09002006
2007 std::set<gid_t> ok_gids_;
2008 std::set<gid_t> bad_gids_;
skerner@google.com93449ef2011-09-22 23:47:18 +09002009};
2010
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002011TEST_F(VerifyPathControlledByUserTest, BadPaths) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002012 // File does not exist.
2013 FilePath does_not_exist = base_dir_.AppendASCII("does")
2014 .AppendASCII("not")
2015 .AppendASCII("exist");
skerner@google.com93449ef2011-09-22 23:47:18 +09002016 EXPECT_FALSE(
2017 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002018 base_dir_, does_not_exist, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002019
2020 // |base| not a subpath of |path|.
2021 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002022 file_util::VerifyPathControlledByUser(
2023 sub_dir_, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002024
2025 // An empty base path will fail to be a prefix for any path.
2026 FilePath empty;
2027 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002028 file_util::VerifyPathControlledByUser(
2029 empty, base_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002030
2031 // Finding that a bad call fails proves nothing unless a good call succeeds.
2032 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002033 file_util::VerifyPathControlledByUser(
2034 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002035}
2036
2037TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2038 // Symlinks in the path should cause failure.
2039
2040 // Symlink to the file at the end of the path.
2041 FilePath file_link = base_dir_.AppendASCII("file_link");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002042 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
skerner@google.com93449ef2011-09-22 23:47:18 +09002043 << "Failed to create symlink.";
2044
2045 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002046 file_util::VerifyPathControlledByUser(
2047 base_dir_, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002048 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002049 file_util::VerifyPathControlledByUser(
2050 file_link, file_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002051
2052 // Symlink from one directory to another within the path.
2053 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002054 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
skerner@google.com93449ef2011-09-22 23:47:18 +09002055 << "Failed to create symlink.";
2056
2057 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002058 ASSERT_TRUE(PathExists(file_path_with_link));
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 base_dir_, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002063
2064 EXPECT_FALSE(
2065 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002066 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002067
2068 // Symlinks in parents of base path are allowed.
2069 EXPECT_TRUE(
2070 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002071 file_path_with_link, file_path_with_link, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002072}
2073
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002074TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002075 // Get a uid that is not the uid of files we create.
2076 uid_t bad_uid = uid_ + 1;
2077
skerner@google.com93449ef2011-09-22 23:47:18 +09002078 // Make all files and directories non-world-writable.
2079 ASSERT_NO_FATAL_FAILURE(
2080 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2081 ASSERT_NO_FATAL_FAILURE(
2082 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2083 ASSERT_NO_FATAL_FAILURE(
2084 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2085
2086 // We control these paths.
2087 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002088 file_util::VerifyPathControlledByUser(
2089 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002090 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002091 file_util::VerifyPathControlledByUser(
2092 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002093 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002094 file_util::VerifyPathControlledByUser(
2095 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002096
2097 // Another user does not control these paths.
2098 EXPECT_FALSE(
2099 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002100 base_dir_, sub_dir_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002101 EXPECT_FALSE(
2102 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002103 base_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002104 EXPECT_FALSE(
2105 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002106 sub_dir_, text_file_, bad_uid, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002107
2108 // Another group does not control the paths.
2109 EXPECT_FALSE(
2110 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002111 base_dir_, sub_dir_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002112 EXPECT_FALSE(
2113 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002114 base_dir_, text_file_, uid_, bad_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002115 EXPECT_FALSE(
2116 file_util::VerifyPathControlledByUser(
skerner@chromium.org80784142011-10-18 06:30:29 +09002117 sub_dir_, text_file_, uid_, bad_gids_));
2118}
2119
2120TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2121 // Make all files and directories writable only by their owner.
2122 ASSERT_NO_FATAL_FAILURE(
2123 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2124 ASSERT_NO_FATAL_FAILURE(
2125 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2126 ASSERT_NO_FATAL_FAILURE(
2127 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2128
2129 // Any group is okay because the path is not group-writable.
2130 EXPECT_TRUE(
2131 file_util::VerifyPathControlledByUser(
2132 base_dir_, sub_dir_, uid_, ok_gids_));
2133 EXPECT_TRUE(
2134 file_util::VerifyPathControlledByUser(
2135 base_dir_, text_file_, uid_, ok_gids_));
2136 EXPECT_TRUE(
2137 file_util::VerifyPathControlledByUser(
2138 sub_dir_, text_file_, uid_, ok_gids_));
2139
2140 EXPECT_TRUE(
2141 file_util::VerifyPathControlledByUser(
2142 base_dir_, sub_dir_, uid_, bad_gids_));
2143 EXPECT_TRUE(
2144 file_util::VerifyPathControlledByUser(
2145 base_dir_, text_file_, uid_, bad_gids_));
2146 EXPECT_TRUE(
2147 file_util::VerifyPathControlledByUser(
2148 sub_dir_, text_file_, uid_, bad_gids_));
2149
2150 // No group is okay, because we don't check the group
2151 // if no group can write.
2152 std::set<gid_t> no_gids; // Empty set of gids.
2153 EXPECT_TRUE(
2154 file_util::VerifyPathControlledByUser(
2155 base_dir_, sub_dir_, uid_, no_gids));
2156 EXPECT_TRUE(
2157 file_util::VerifyPathControlledByUser(
2158 base_dir_, text_file_, uid_, no_gids));
2159 EXPECT_TRUE(
2160 file_util::VerifyPathControlledByUser(
2161 sub_dir_, text_file_, uid_, no_gids));
2162
2163
2164 // Make all files and directories writable by their group.
2165 ASSERT_NO_FATAL_FAILURE(
2166 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2167 ASSERT_NO_FATAL_FAILURE(
2168 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2169 ASSERT_NO_FATAL_FAILURE(
2170 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2171
2172 // Now |ok_gids_| works, but |bad_gids_| fails.
2173 EXPECT_TRUE(
2174 file_util::VerifyPathControlledByUser(
2175 base_dir_, sub_dir_, uid_, ok_gids_));
2176 EXPECT_TRUE(
2177 file_util::VerifyPathControlledByUser(
2178 base_dir_, text_file_, uid_, ok_gids_));
2179 EXPECT_TRUE(
2180 file_util::VerifyPathControlledByUser(
2181 sub_dir_, text_file_, uid_, ok_gids_));
2182
2183 EXPECT_FALSE(
2184 file_util::VerifyPathControlledByUser(
2185 base_dir_, sub_dir_, uid_, bad_gids_));
2186 EXPECT_FALSE(
2187 file_util::VerifyPathControlledByUser(
2188 base_dir_, text_file_, uid_, bad_gids_));
2189 EXPECT_FALSE(
2190 file_util::VerifyPathControlledByUser(
2191 sub_dir_, text_file_, uid_, bad_gids_));
2192
2193 // Because any group in the group set is allowed,
2194 // the union of good and bad gids passes.
2195
2196 std::set<gid_t> multiple_gids;
2197 std::set_union(
2198 ok_gids_.begin(), ok_gids_.end(),
2199 bad_gids_.begin(), bad_gids_.end(),
2200 std::inserter(multiple_gids, multiple_gids.begin()));
2201
2202 EXPECT_TRUE(
2203 file_util::VerifyPathControlledByUser(
2204 base_dir_, sub_dir_, uid_, multiple_gids));
2205 EXPECT_TRUE(
2206 file_util::VerifyPathControlledByUser(
2207 base_dir_, text_file_, uid_, multiple_gids));
2208 EXPECT_TRUE(
2209 file_util::VerifyPathControlledByUser(
2210 sub_dir_, text_file_, uid_, multiple_gids));
skerner@google.com93449ef2011-09-22 23:47:18 +09002211}
2212
skerner@chromium.org19ff3c72011-09-27 02:18:43 +09002213TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
skerner@google.com93449ef2011-09-22 23:47:18 +09002214 // Make all files and directories non-world-writable.
2215 ASSERT_NO_FATAL_FAILURE(
2216 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2217 ASSERT_NO_FATAL_FAILURE(
2218 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2219 ASSERT_NO_FATAL_FAILURE(
2220 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2221
2222 // Initialy, we control all parts of the path.
2223 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002224 file_util::VerifyPathControlledByUser(
2225 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002226 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002227 file_util::VerifyPathControlledByUser(
2228 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002229 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002230 file_util::VerifyPathControlledByUser(
2231 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002232
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09002233 // Make base_dir_ world-writable.
skerner@google.com93449ef2011-09-22 23:47:18 +09002234 ASSERT_NO_FATAL_FAILURE(
2235 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2236 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002237 file_util::VerifyPathControlledByUser(
2238 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002239 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002240 file_util::VerifyPathControlledByUser(
2241 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002242 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002243 file_util::VerifyPathControlledByUser(
2244 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002245
2246 // Make sub_dir_ world writable.
2247 ASSERT_NO_FATAL_FAILURE(
2248 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2249 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002250 file_util::VerifyPathControlledByUser(
2251 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002252 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002253 file_util::VerifyPathControlledByUser(
2254 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002255 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002256 file_util::VerifyPathControlledByUser(
2257 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002258
2259 // Make text_file_ world writable.
2260 ASSERT_NO_FATAL_FAILURE(
2261 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2262 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002263 file_util::VerifyPathControlledByUser(
2264 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002265 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002266 file_util::VerifyPathControlledByUser(
2267 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002268 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002269 file_util::VerifyPathControlledByUser(
2270 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002271
2272 // Make sub_dir_ non-world writable.
2273 ASSERT_NO_FATAL_FAILURE(
2274 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2275 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002276 file_util::VerifyPathControlledByUser(
2277 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002278 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002279 file_util::VerifyPathControlledByUser(
2280 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002281 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002282 file_util::VerifyPathControlledByUser(
2283 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002284
2285 // Make base_dir_ non-world-writable.
2286 ASSERT_NO_FATAL_FAILURE(
2287 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2288 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002289 file_util::VerifyPathControlledByUser(
2290 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002291 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002292 file_util::VerifyPathControlledByUser(
2293 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002294 EXPECT_FALSE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002295 file_util::VerifyPathControlledByUser(
2296 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002297
2298 // Back to the initial state: Nothing is writable, so every path
2299 // should pass.
2300 ASSERT_NO_FATAL_FAILURE(
2301 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2302 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002303 file_util::VerifyPathControlledByUser(
2304 base_dir_, sub_dir_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002305 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002306 file_util::VerifyPathControlledByUser(
2307 base_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002308 EXPECT_TRUE(
skerner@chromium.org80784142011-10-18 06:30:29 +09002309 file_util::VerifyPathControlledByUser(
2310 sub_dir_, text_file_, uid_, ok_gids_));
skerner@google.com93449ef2011-09-22 23:47:18 +09002311}
2312
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002313#if defined(OS_ANDROID)
2314TEST_F(FileUtilTest, ValidContentUriTest) {
2315 // Get the test image path.
2316 FilePath data_dir;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002317 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002318 data_dir = data_dir.AppendASCII("file_util");
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002319 ASSERT_TRUE(PathExists(data_dir));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002320 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2321 int64 image_size;
2322 file_util::GetFileSize(image_file, &image_size);
2323 EXPECT_LT(0, image_size);
2324
2325 // Insert the image into MediaStore. MediaStore will do some conversions, and
2326 // return the content URI.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002327 FilePath path = file_util::InsertImageIntoMediaStore(image_file);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002328 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002329 EXPECT_TRUE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002330 // The file size may not equal to the input image as MediaStore may convert
2331 // the image.
2332 int64 content_uri_size;
2333 file_util::GetFileSize(path, &content_uri_size);
2334 EXPECT_EQ(image_size, content_uri_size);
2335
2336 // We should be able to read the file.
2337 char* buffer = new char[image_size];
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002338 int fd = OpenContentUriForRead(path);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002339 EXPECT_LT(0, fd);
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002340 EXPECT_TRUE(ReadFromFD(fd, buffer, image_size));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002341 delete[] buffer;
2342}
2343
2344TEST_F(FileUtilTest, NonExistentContentUriTest) {
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002345 FilePath path("content://foo.bar");
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002346 EXPECT_TRUE(path.IsContentUri());
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002347 EXPECT_FALSE(PathExists(path));
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002348 // Size should be smaller than 0.
2349 int64 size;
2350 file_util::GetFileSize(path, &size);
2351 EXPECT_GT(0, size);
2352
2353 // We should not be able to read the file.
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002354 int fd = OpenContentUriForRead(path);
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09002355 EXPECT_EQ(-1, fd);
2356}
2357#endif
2358
skerner@google.com93449ef2011-09-22 23:47:18 +09002359#endif // defined(OS_POSIX)
2360
mark@chromium.org17684802008-09-10 09:16:28 +09002361} // namespace
brettw@chromium.org2873d9b2013-11-28 08:22:08 +09002362
2363} // namespace base