blob: d66a4261e50fb5be620713fa67c5e1a7bfbafb59 [file] [log] [blame]
jochen@chromium.orga6879772010-02-18 19:02:26 +09001// Copyright (c) 2010 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>
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +09009#include <winioctl.h>
initial.commit3f4a7322008-07-27 06:49:38 +090010#include <shellapi.h>
11#include <shlobj.h>
tkent@chromium.org8da14162009-10-09 16:33:39 +090012#include <tchar.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090013#endif
initial.commit3f4a7322008-07-27 06:49:38 +090014
15#include <fstream>
16#include <iostream>
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"
evanm@google.com874d1672008-10-31 08:54:04 +090020#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090021#include "base/file_util.h"
22#include "base/logging.h"
23#include "base/path_service.h"
erikkay@google.com8d133f62009-04-24 00:05:19 +090024#include "base/platform_thread.h"
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090025#include "base/scoped_handle.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090026#include "base/time.h"
brettw@chromium.org50c94652009-10-07 11:10:20 +090027#include "base/utf_string_conversions.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
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090031// This macro helps avoid wrapped lines in the test structs.
32#define FPL(x) FILE_PATH_LITERAL(x)
33
initial.commit3f4a7322008-07-27 06:49:38 +090034namespace {
35
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090036// To test that file_util::Normalize FilePath() deals with NTFS reparse points
37// correctly, we need functions to create and delete reparse points.
38#if defined(OS_WIN)
39typedef struct _REPARSE_DATA_BUFFER {
40 ULONG ReparseTag;
41 USHORT ReparseDataLength;
42 USHORT Reserved;
43 union {
44 struct {
45 USHORT SubstituteNameOffset;
46 USHORT SubstituteNameLength;
47 USHORT PrintNameOffset;
48 USHORT PrintNameLength;
49 ULONG Flags;
50 WCHAR PathBuffer[1];
51 } SymbolicLinkReparseBuffer;
52 struct {
53 USHORT SubstituteNameOffset;
54 USHORT SubstituteNameLength;
55 USHORT PrintNameOffset;
56 USHORT PrintNameLength;
57 WCHAR PathBuffer[1];
58 } MountPointReparseBuffer;
59 struct {
60 UCHAR DataBuffer[1];
61 } GenericReparseBuffer;
62 };
63} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
64
65// Sets a reparse point. |source| will now point to |target|. Returns true if
66// the call succeeds, false otherwise.
67bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
68 std::wstring kPathPrefix = L"\\??\\";
69 std::wstring target_str;
70 // The juction will not work if the target path does not start with \??\ .
71 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
72 target_str += kPathPrefix;
73 target_str += target_path.value();
74 const wchar_t* target = target_str.c_str();
75 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
76 char buffer[2000] = {0};
77 DWORD returned;
78
79 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
80
81 data->ReparseTag = 0xa0000003;
82 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
83
84 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
85 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
86 data->ReparseDataLength = size_target + 4 + 8;
87
88 int data_size = data->ReparseDataLength + 8;
89
90 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
91 NULL, 0, &returned, NULL)) {
92 return false;
93 }
94 return true;
95}
96
97// Delete the reparse point referenced by |source|. Returns true if the call
98// succeeds, false otherwise.
99bool DeleteReparsePoint(HANDLE source) {
100 DWORD returned;
101 REPARSE_DATA_BUFFER data = {0};
102 data.ReparseTag = 0xa0000003;
103 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
104 &returned, NULL)) {
105 return false;
106 }
107 return true;
108}
109#endif
110
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900111const wchar_t bogus_content[] = L"I'm cannon fodder.";
112
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900113const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
114 static_cast<file_util::FileEnumerator::FILE_TYPE>(
115 file_util::FileEnumerator::FILES |
116 file_util::FileEnumerator::DIRECTORIES);
117
erikkay@google.comf2406842008-08-21 00:59:49 +0900118// file_util winds up using autoreleased objects on the Mac, so this needs
119// to be a PlatformTest
120class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900121 protected:
122 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +0900123 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +0900124 // Name a subdirectory of the temp directory.
125 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +0900126 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900127
128 // Create a fresh, empty copy of this directory.
129 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +0900130 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +0900131 }
132 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +0900133 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +0900134 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900135 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900136 ASSERT_FALSE(file_util::PathExists(test_dir_));
137 }
138
139 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +0900140 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900141};
142
143// Collects all the results from the given file enumerator, and provides an
144// interface to query whether a given file is present.
145class FindResultCollector {
146 public:
evan@chromium.org1543ad32009-08-27 05:00:14 +0900147 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900148 FilePath cur_file;
149 while (!(cur_file = enumerator.Next()).value().empty()) {
150 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900151 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900152 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900153 << "Same file returned twice";
154
155 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900156 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900157 }
158 }
159
160 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900161 bool HasFile(const FilePath& file) const {
162 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900163 }
evanm@google.com874d1672008-10-31 08:54:04 +0900164
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900165 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900166 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900167 }
initial.commit3f4a7322008-07-27 06:49:38 +0900168
169 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900170 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900171};
172
173// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900174void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900175 const std::wstring& contents) {
176 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900177 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900178 ASSERT_TRUE(file.is_open());
179 file << contents;
180 file.close();
181}
182
183// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900184std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900185 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900186 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900187 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900188 EXPECT_TRUE(file.is_open());
189 file.getline(contents, 64);
190 file.close();
191 return std::wstring(contents);
192}
193
erikkay@google.com014161d2008-08-16 02:45:13 +0900194#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900195uint64 FileTimeAsUint64(const FILETIME& ft) {
196 ULARGE_INTEGER u;
197 u.LowPart = ft.dwLowDateTime;
198 u.HighPart = ft.dwHighDateTime;
199 return u.QuadPart;
200}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900201#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900202
203const struct append_case {
204 const wchar_t* path;
205 const wchar_t* ending;
206 const wchar_t* result;
207} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900208#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900209 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
210 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
211 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
212 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
213 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
214 {L"", L"path", L"\\path"},
215 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900216#elif defined(OS_POSIX)
217 {L"/foo/bar", L"path", L"/foo/bar/path"},
218 {L"/foo/bar/", L"path", L"/foo/bar/path"},
219 {L"/foo/bar//", L"path", L"/foo/bar//path"},
220 {L"/foo/bar/", L"", L"/foo/bar/"},
221 {L"/foo/bar", L"", L"/foo/bar/"},
222 {L"", L"path", L"/path"},
223 {L"", L"", L"/"},
224#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900225};
226
evan@chromium.org1db7f942010-02-27 00:11:55 +0900227#if defined(OS_WIN)
228// This function is deprecated, but still used on Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900229TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900230 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900231 const append_case& value = append_cases[i];
232 std::wstring result = value.path;
233 file_util::AppendToPath(&result, value.ending);
234 EXPECT_EQ(value.result, result);
235 }
236
237#ifdef NDEBUG
238 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
239#endif
240}
evan@chromium.org1db7f942010-02-27 00:11:55 +0900241#endif // defined(OS_WIN)
242
initial.commit3f4a7322008-07-27 06:49:38 +0900243
244static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900245 const FilePath::CharType* path;
246 const FilePath::CharType* suffix;
247 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900248} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900249 {FPL(""), FPL(""), FPL("")},
250 {FPL(""), FPL("txt"), FPL("txt")},
251 {FPL("."), FPL("txt"), FPL("txt.")},
252 {FPL("."), FPL(""), FPL(".")},
253 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
254 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
255 {FPL("foo"), FPL("txt"), FPL("footxt")},
256 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
257 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
258 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
259 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
260 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
261 {FPL("foo"), FPL(""), FPL("foo")},
262 {FPL("foo"), FPL("."), FPL("foo.")},
263 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
264 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900265#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900266 {FPL("\\"), FPL(""), FPL("\\")},
267 {FPL("\\"), FPL("txt"), FPL("\\txt")},
268 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
269 {FPL("\\."), FPL(""), FPL("\\.")},
270 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
271 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
272 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
273 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
274 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
275 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
276 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
277 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
278 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900279#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900280 {FPL("/"), FPL(""), FPL("/")},
281 {FPL("/"), FPL("txt"), FPL("/txt")},
282 {FPL("/."), FPL("txt"), FPL("/txt.")},
283 {FPL("/."), FPL(""), FPL("/.")},
284 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
285 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
286 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
287 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
288 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
289 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
290 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
291 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900292#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900293};
294
295TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900296 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900297 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900298 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900299 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900300 }
301}
302
303static const struct filename_case {
304 const wchar_t* path;
305 const wchar_t* filename;
306} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900307#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900308 {L"c:\\colon\\backslash", L"backslash"},
309 {L"c:\\colon\\backslash\\", L""},
310 {L"\\\\filename.exe", L"filename.exe"},
311 {L"filename.exe", L"filename.exe"},
312 {L"", L""},
313 {L"\\\\\\", L""},
314 {L"c:/colon/backslash", L"backslash"},
315 {L"c:/colon/backslash/", L""},
316 {L"//////", L""},
317 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900318#elif defined(OS_POSIX)
319 {L"/foo/bar", L"bar"},
320 {L"/foo/bar/", L""},
321 {L"/filename.exe", L"filename.exe"},
322 {L"filename.exe", L"filename.exe"},
323 {L"", L""},
324 {L"/", L""},
325#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900326};
327
328TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900329 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900330 const filename_case& value = filename_cases[i];
331 std::wstring result = file_util::GetFilenameFromPath(value.path);
332 EXPECT_EQ(value.filename, result);
333 }
334}
335
336// Test finding the file type from a path name
337static const struct extension_case {
338 const wchar_t* path;
339 const wchar_t* extension;
340} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900341#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900342 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
343 {L"C:\\colon\\backslash\\filename.", L""},
344 {L"C:\\colon\\backslash\\filename", L""},
345 {L"C:\\colon\\backslash\\", L""},
346 {L"C:\\colon\\backslash.\\", L""},
347 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900348#elif defined(OS_POSIX)
349 {L"/foo/bar/filename.extension", L"extension"},
350 {L"/foo/bar/filename.", L""},
351 {L"/foo/bar/filename", L""},
352 {L"/foo/bar/", L""},
353 {L"/foo/bar./", L""},
354 {L"/foo/bar/filename.extension.extension2", L"extension2"},
355 {L".", L""},
356 {L"..", L""},
357 {L"./foo", L""},
358 {L"./foo.extension", L"extension"},
359 {L"/foo.extension1/bar.extension2", L"extension2"},
360#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900361};
362
363TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900364 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900365 const extension_case& ext = extension_cases[i];
366 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
367 EXPECT_EQ(ext.extension, fext);
368 }
369}
370
371// Test finding the directory component of a path
372static const struct dir_case {
373 const wchar_t* full_path;
374 const wchar_t* directory;
375} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900376#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900377 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
378 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
379 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
380 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
381 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
382 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900383 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900384#elif defined(OS_POSIX)
385 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
386 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
387 {L"/foo/bar/", L"/foo/bar"},
388 {L"/foo/bar//", L"/foo/bar"},
389 {L"/foo/bar", L"/foo"},
390 {L"/foo/bar./", L"/foo/bar."},
391 {L"/", L"/"},
392 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900393 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900394#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900395};
396
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900397#if defined(OS_WIN)
398// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900399TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900400 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900401 const dir_case& dir = dir_cases[i];
402 const std::wstring parent =
403 file_util::GetDirectoryFromPath(dir.full_path);
404 EXPECT_EQ(dir.directory, parent);
405 }
406}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900407#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900408
409TEST_F(FileUtilTest, CountFilesCreatedAfter) {
410 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900411 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900412 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
413
414 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900415#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900416 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900417#elif defined(OS_POSIX)
418 // We need to wait at least one second here because the precision of
419 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900420 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900421#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900422
423 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900424 base::Time now(base::Time::NowFromSystemTime());
425 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900426
427 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900428 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900429 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
430
431 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900432 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900433
434 // Delete new file, we should see no files after cutoff now
435 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900436 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900437}
438
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900439TEST_F(FileUtilTest, FileAndDirectorySize) {
440 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
441 // should return 53 bytes.
442 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
443 CreateTextFile(file_01, L"12345678901234567890");
444 int64 size_f1 = 0;
445 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
446 EXPECT_EQ(20ll, size_f1);
447
448 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
449 file_util::CreateDirectory(subdir_path);
450
451 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
452 CreateTextFile(file_02, L"123456789012345678901234567890");
453 int64 size_f2 = 0;
454 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
455 EXPECT_EQ(30ll, size_f2);
456
457 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
458 file_util::CreateDirectory(subsubdir_path);
459
460 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
461 CreateTextFile(file_03, L"123");
462
463 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
464 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
465}
466
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900467TEST_F(FileUtilTest, NormalizeFilePathBasic) {
468 // Create a directory under the test dir. Because we create it,
469 // we know it is not a link.
470 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
471 FilePath dir_path = test_dir_.Append(FPL("dir"));
472 FilePath file_b_path = dir_path.Append(FPL("file_b"));
473 file_util::CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900474
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900475 FilePath normalized_file_a_path, normalized_file_b_path;
476 ASSERT_FALSE(file_util::PathExists(file_a_path));
477 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
478 &normalized_file_a_path))
479 << "NormalizeFilePath() should fail on nonexistant paths.";
480
481 CreateTextFile(file_a_path, bogus_content);
482 ASSERT_TRUE(file_util::PathExists(file_a_path));
483 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
484 &normalized_file_a_path));
485
486 CreateTextFile(file_b_path, bogus_content);
487 ASSERT_TRUE(file_util::PathExists(file_b_path));
488 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
489 &normalized_file_b_path));
490
491 // Beacuse this test created |dir_path|, we know it is not a link
492 // or junction. So, the real path of the directory holding file a
493 // must be the parent of the path holding file b.
494 ASSERT_TRUE(normalized_file_a_path.DirName()
495 .IsParent(normalized_file_b_path.DirName()));
496}
497
498#if defined(OS_WIN)
499
500TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
501 // Build the following directory structure:
502 //
503 // test_dir_
504 // |-> base_a
505 // | |-> sub_a
506 // | |-> file.txt
507 // | |-> long_name___... (Very long name.)
508 // | |-> sub_long
509 // | |-> deep.txt
510 // |-> base_b
511 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
512 // |-> to_base_b (reparse point to test_dir_\base_b)
513 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
514
515 FilePath base_a = test_dir_.Append(FPL("base_a"));
516 ASSERT_TRUE(file_util::CreateDirectory(base_a));
517
518 FilePath sub_a = base_a.Append(FPL("sub_a"));
519 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
520
521 FilePath file_txt = sub_a.Append(FPL("file.txt"));
522 CreateTextFile(file_txt, bogus_content);
523
524 // Want a directory whose name is long enough to make the path to the file
525 // inside just under MAX_PATH chars. This will be used to test that when
526 // a junction expands to a path over MAX_PATH chars in length,
527 // NormalizeFilePath() fails without crashing.
528 FilePath sub_long_rel(FPL("sub_long"));
529 FilePath deep_txt(FPL("deep.txt"));
530
531 int target_length = MAX_PATH;
532 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
533 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
534 // Without making the path a bit shorter, CreateDirectory() fails.
535 // the resulting path is still long enough to hit the failing case in
536 // NormalizePath().
537 const int kCreateDirLimit = 4;
538 target_length -= kCreateDirLimit;
539 FilePath::StringType long_name_str = FPL("long_name_");
540 long_name_str.resize(target_length, '_');
541
542 FilePath long_name = sub_a.Append(FilePath(long_name_str));
543 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
544 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
545
546 FilePath sub_long = deep_file.DirName();
547 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
548 CreateTextFile(deep_file, bogus_content);
549
550 FilePath base_b = test_dir_.Append(FPL("base_b"));
551 ASSERT_TRUE(file_util::CreateDirectory(base_b));
552
553 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
554 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
555 ScopedHandle reparse_to_sub_a(
556 ::CreateFile(to_sub_a.value().c_str(),
557 FILE_ALL_ACCESS,
558 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
559 NULL,
560 OPEN_EXISTING,
561 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
562 NULL));
563 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_a.Get());
564 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
565
566 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
567 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
568 ScopedHandle reparse_to_base_b(
569 ::CreateFile(to_base_b.value().c_str(),
570 FILE_ALL_ACCESS,
571 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
572 NULL,
573 OPEN_EXISTING,
574 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
575 NULL));
576 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_base_b.Get());
577 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
578
579 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
580 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
581 ScopedHandle reparse_to_sub_long(
582 ::CreateFile(to_sub_long.value().c_str(),
583 FILE_ALL_ACCESS,
584 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
585 NULL,
586 OPEN_EXISTING,
587 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
588 NULL));
589 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_long.Get());
590 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
591
592 // Normalize a junction free path: base_a\sub_a\file.txt .
593 FilePath normalized_path;
594 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
595 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
596
597 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
598 // the junction to_sub_a.
599 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
600 &normalized_path));
601 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
602
603 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
604 // normalized to exclude junctions to_base_b and to_sub_a .
605 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
606 .Append(FPL("to_base_b"))
607 .Append(FPL("to_sub_a"))
608 .Append(FPL("file.txt")),
609 &normalized_path));
610 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
611
612 // A long enough path will cause NormalizeFilePath() to fail. Make a long
613 // path using to_base_b many times, and check that paths long enough to fail
614 // do not cause a crash.
615 FilePath long_path = base_b;
616 const int kLengthLimit = MAX_PATH + 200;
617 while (long_path.value().length() <= kLengthLimit) {
618 long_path = long_path.Append(FPL("to_base_b"));
619 }
620 long_path = long_path.Append(FPL("to_sub_a"))
621 .Append(FPL("file.txt"));
622
623 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
624
625 // Normalizing the junction to deep.txt should fail, because the expanded
626 // path to deep.txt is longer than MAX_PATH.
627 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
628 &normalized_path));
629
630 // Delete the reparse points, and see that NormalizeFilePath() fails
631 // to traverse them.
632 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
633 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
634 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
635
636 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
637 &normalized_path));
638}
639
640#endif // defined(OS_WIN)
641
642// The following test of NormalizeFilePath() require that we create a symlink.
643// This can not be done on windows before vista. On vista, creating a symlink
644// requires privilege "SeCreateSymbolicLinkPrivilege".
645// TODO(skerner): Investigate the possibility of giving base_unittests the
646// privileges required to create a symlink.
647#if defined(OS_POSIX)
648
649bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
650 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
651}
652
653TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
654 FilePath normalized_path;
skerner@chromium.org559baa92010-05-13 00:13:57 +0900655
656 // Link one file to another.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900657 FilePath link_from = test_dir_.Append(FPL("from_file"));
658 FilePath link_to = test_dir_.Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900659 CreateTextFile(link_to, bogus_content);
660
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900661 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900662 << "Failed to create file symlink.";
663
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900664 // Check that NormalizeFilePath sees the link.
665 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900666 ASSERT_TRUE(link_to != link_from);
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900667 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
668 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900669
670 // Link to a directory.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900671 link_from = test_dir_.Append(FPL("from_dir"));
672 link_to = test_dir_.Append(FPL("to_dir"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900673 file_util::CreateDirectory(link_to);
674
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900675 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900676 << "Failed to create directory symlink.";
677
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900678 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
679 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900680
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900681 // Test that a loop in the links causes NormalizeFilePath() to return false.
682 link_from = test_dir_.Append(FPL("link_a"));
683 link_to = test_dir_.Append(FPL("link_b"));
684 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900685 << "Failed to create loop symlink a.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900686 ASSERT_TRUE(MakeSymlink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900687 << "Failed to create loop symlink b.";
688
689 // Infinite loop!
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900690 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900691}
692#endif // defined(OS_POSIX)
693
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900694TEST_F(FileUtilTest, DeleteNonExistent) {
695 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
696 ASSERT_FALSE(file_util::PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900697
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900698 EXPECT_TRUE(file_util::Delete(non_existent, false));
699 ASSERT_FALSE(file_util::PathExists(non_existent));
700 EXPECT_TRUE(file_util::Delete(non_existent, true));
701 ASSERT_FALSE(file_util::PathExists(non_existent));
702}
703
704TEST_F(FileUtilTest, DeleteFile) {
705 // Create a file
706 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
707 CreateTextFile(file_name, bogus_content);
initial.commit3f4a7322008-07-27 06:49:38 +0900708 ASSERT_TRUE(file_util::PathExists(file_name));
709
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900710 // Make sure it's deleted
711 EXPECT_TRUE(file_util::Delete(file_name, false));
712 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900713
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900714 // Test recursive case, create a new file
715 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
716 CreateTextFile(file_name, bogus_content);
717 ASSERT_TRUE(file_util::PathExists(file_name));
718
719 // Make sure it's deleted
720 EXPECT_TRUE(file_util::Delete(file_name, true));
721 EXPECT_FALSE(file_util::PathExists(file_name));
722}
723
724#if defined(OS_WIN)
725// Tests that the Delete function works for wild cards, especially
726// with the recursion flag. Also coincidentally tests PathExists.
727// TODO(erikkay): see if anyone's actually using this feature of the API
728TEST_F(FileUtilTest, DeleteWildCard) {
729 // Create a file and a directory
730 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
731 CreateTextFile(file_name, bogus_content);
732 ASSERT_TRUE(file_util::PathExists(file_name));
733
734 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
735 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900736 ASSERT_TRUE(file_util::PathExists(subdir_path));
737
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900738 // Create the wildcard path
evanm@google.com874d1672008-10-31 08:54:04 +0900739 FilePath directory_contents = test_dir_;
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900740 directory_contents = directory_contents.Append(FPL("*"));
741
initial.commit3f4a7322008-07-27 06:49:38 +0900742 // Delete non-recursively and check that only the file is deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900743 EXPECT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900744 EXPECT_FALSE(file_util::PathExists(file_name));
745 EXPECT_TRUE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900746
zork@chromium.org61be4f42010-05-07 09:05:36 +0900747 // Delete recursively and make sure all contents are deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900748 EXPECT_TRUE(file_util::Delete(directory_contents, true));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900749 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900750 EXPECT_FALSE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900751}
752
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900753// TODO(erikkay): see if anyone's actually using this feature of the API
754TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
755 // Create a file and a directory
756 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
757 file_util::CreateDirectory(subdir_path);
758 ASSERT_TRUE(file_util::PathExists(subdir_path));
759
760 // Create the wildcard path
761 FilePath directory_contents = subdir_path;
762 directory_contents = directory_contents.Append(FPL("*"));
763
764 // Delete non-recursively and check nothing got deleted
765 EXPECT_TRUE(file_util::Delete(directory_contents, false));
766 EXPECT_TRUE(file_util::PathExists(subdir_path));
767
768 // Delete recursively and check nothing got deleted
769 EXPECT_TRUE(file_util::Delete(directory_contents, true));
770 EXPECT_TRUE(file_util::PathExists(subdir_path));
771}
772#endif
773
774// Tests non-recursive Delete() for a directory.
775TEST_F(FileUtilTest, DeleteDirNonRecursive) {
776 // Create a subdirectory and put a file and two directories inside.
777 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
778 file_util::CreateDirectory(test_subdir);
779 ASSERT_TRUE(file_util::PathExists(test_subdir));
780
781 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
782 CreateTextFile(file_name, bogus_content);
783 ASSERT_TRUE(file_util::PathExists(file_name));
784
785 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
786 file_util::CreateDirectory(subdir_path1);
787 ASSERT_TRUE(file_util::PathExists(subdir_path1));
788
789 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
790 file_util::CreateDirectory(subdir_path2);
791 ASSERT_TRUE(file_util::PathExists(subdir_path2));
792
793 // Delete non-recursively and check that the empty dir got deleted
794 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
795 EXPECT_FALSE(file_util::PathExists(subdir_path2));
796
797 // Delete non-recursively and check that nothing got deleted
798 EXPECT_FALSE(file_util::Delete(test_subdir, false));
799 EXPECT_TRUE(file_util::PathExists(test_subdir));
800 EXPECT_TRUE(file_util::PathExists(file_name));
801 EXPECT_TRUE(file_util::PathExists(subdir_path1));
802}
803
804// Tests recursive Delete() for a directory.
805TEST_F(FileUtilTest, DeleteDirRecursive) {
806 // Create a subdirectory and put a file and two directories inside.
807 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
808 file_util::CreateDirectory(test_subdir);
809 ASSERT_TRUE(file_util::PathExists(test_subdir));
810
811 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
812 CreateTextFile(file_name, bogus_content);
813 ASSERT_TRUE(file_util::PathExists(file_name));
814
815 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
816 file_util::CreateDirectory(subdir_path1);
817 ASSERT_TRUE(file_util::PathExists(subdir_path1));
818
819 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
820 file_util::CreateDirectory(subdir_path2);
821 ASSERT_TRUE(file_util::PathExists(subdir_path2));
822
823 // Delete recursively and check that the empty dir got deleted
824 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
825 EXPECT_FALSE(file_util::PathExists(subdir_path2));
826
827 // Delete recursively and check that everything got deleted
828 EXPECT_TRUE(file_util::Delete(test_subdir, true));
829 EXPECT_FALSE(file_util::PathExists(file_name));
830 EXPECT_FALSE(file_util::PathExists(subdir_path1));
831 EXPECT_FALSE(file_util::PathExists(test_subdir));
832}
833
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900834TEST_F(FileUtilTest, MoveFileNew) {
835 // Create a file
836 FilePath file_name_from =
837 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
838 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
839 ASSERT_TRUE(file_util::PathExists(file_name_from));
840
841 // The destination
842 FilePath file_name_to =
843 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
844 ASSERT_FALSE(file_util::PathExists(file_name_to));
845
846 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
847
848 // Check everything has been moved.
849 EXPECT_FALSE(file_util::PathExists(file_name_from));
850 EXPECT_TRUE(file_util::PathExists(file_name_to));
851}
852
853TEST_F(FileUtilTest, MoveFileExists) {
854 // Create a file
855 FilePath file_name_from =
856 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
857 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
858 ASSERT_TRUE(file_util::PathExists(file_name_from));
859
860 // The destination name
861 FilePath file_name_to =
862 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
863 CreateTextFile(file_name_to, L"Old file content");
864 ASSERT_TRUE(file_util::PathExists(file_name_to));
865
866 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
867
868 // Check everything has been moved.
869 EXPECT_FALSE(file_util::PathExists(file_name_from));
870 EXPECT_TRUE(file_util::PathExists(file_name_to));
871 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
872}
873
874TEST_F(FileUtilTest, MoveFileDirExists) {
875 // Create a file
876 FilePath file_name_from =
877 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
878 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
879 ASSERT_TRUE(file_util::PathExists(file_name_from));
880
881 // The destination directory
882 FilePath dir_name_to =
883 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
884 file_util::CreateDirectory(dir_name_to);
885 ASSERT_TRUE(file_util::PathExists(dir_name_to));
886
887 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
888}
889
890
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900891TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900892 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900893 FilePath dir_name_from =
894 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
895 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900896 ASSERT_TRUE(file_util::PathExists(dir_name_from));
897
898 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900899 FilePath file_name_from =
900 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900901 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
902 ASSERT_TRUE(file_util::PathExists(file_name_from));
903
904 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900905 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
906 FilePath file_name_to =
907 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900908
909 ASSERT_FALSE(file_util::PathExists(dir_name_to));
910
911 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
912
913 // Check everything has been moved.
914 EXPECT_FALSE(file_util::PathExists(dir_name_from));
915 EXPECT_FALSE(file_util::PathExists(file_name_from));
916 EXPECT_TRUE(file_util::PathExists(dir_name_to));
917 EXPECT_TRUE(file_util::PathExists(file_name_to));
918}
919
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900920TEST_F(FileUtilTest, MoveExist) {
921 // Create a directory
922 FilePath dir_name_from =
923 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
924 file_util::CreateDirectory(dir_name_from);
925 ASSERT_TRUE(file_util::PathExists(dir_name_from));
926
927 // Create a file under the directory
928 FilePath file_name_from =
929 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
930 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
931 ASSERT_TRUE(file_util::PathExists(file_name_from));
932
933 // Move the directory
934 FilePath dir_name_exists =
935 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
936
937 FilePath dir_name_to =
938 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
939 FilePath file_name_to =
940 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
941
942 // Create the destination directory.
943 file_util::CreateDirectory(dir_name_exists);
944 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
945
946 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
947
948 // Check everything has been moved.
949 EXPECT_FALSE(file_util::PathExists(dir_name_from));
950 EXPECT_FALSE(file_util::PathExists(file_name_from));
951 EXPECT_TRUE(file_util::PathExists(dir_name_to));
952 EXPECT_TRUE(file_util::PathExists(file_name_to));
953}
954
955TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900956 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900957 FilePath dir_name_from =
958 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
959 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900960 ASSERT_TRUE(file_util::PathExists(dir_name_from));
961
962 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900963 FilePath file_name_from =
964 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900965 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
966 ASSERT_TRUE(file_util::PathExists(file_name_from));
967
968 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900969 FilePath subdir_name_from =
970 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
971 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900972 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
973
974 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900975 FilePath file_name2_from =
976 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900977 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
978 ASSERT_TRUE(file_util::PathExists(file_name2_from));
979
980 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900981 FilePath dir_name_to =
982 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
983 FilePath file_name_to =
984 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
985 FilePath subdir_name_to =
986 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
987 FilePath file_name2_to =
988 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900989
990 ASSERT_FALSE(file_util::PathExists(dir_name_to));
991
992 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
993
994 // Check everything has been copied.
995 EXPECT_TRUE(file_util::PathExists(dir_name_from));
996 EXPECT_TRUE(file_util::PathExists(file_name_from));
997 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
998 EXPECT_TRUE(file_util::PathExists(file_name2_from));
999 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1000 EXPECT_TRUE(file_util::PathExists(file_name_to));
1001 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1002 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1003}
1004
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001005TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1006 // Create a directory.
1007 FilePath dir_name_from =
1008 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1009 file_util::CreateDirectory(dir_name_from);
1010 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1011
1012 // Create a file under the directory.
1013 FilePath file_name_from =
1014 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1015 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1016 ASSERT_TRUE(file_util::PathExists(file_name_from));
1017
1018 // Create a subdirectory.
1019 FilePath subdir_name_from =
1020 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1021 file_util::CreateDirectory(subdir_name_from);
1022 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1023
1024 // Create a file under the subdirectory.
1025 FilePath file_name2_from =
1026 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1027 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1028 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1029
1030 // Copy the directory recursively.
1031 FilePath dir_name_exists =
1032 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1033
1034 FilePath dir_name_to =
1035 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1036 FilePath file_name_to =
1037 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1038 FilePath subdir_name_to =
1039 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1040 FilePath file_name2_to =
1041 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1042
1043 // Create the destination directory.
1044 file_util::CreateDirectory(dir_name_exists);
1045 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1046
1047 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1048
1049 // Check everything has been copied.
1050 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1051 EXPECT_TRUE(file_util::PathExists(file_name_from));
1052 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1053 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1054 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1055 EXPECT_TRUE(file_util::PathExists(file_name_to));
1056 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1057 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1058}
1059
1060TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001061 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001062 FilePath dir_name_from =
1063 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1064 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001065 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1066
1067 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001068 FilePath file_name_from =
1069 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001070 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1071 ASSERT_TRUE(file_util::PathExists(file_name_from));
1072
1073 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001074 FilePath subdir_name_from =
1075 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1076 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001077 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1078
1079 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001080 FilePath file_name2_from =
1081 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001082 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1083 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1084
1085 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001086 FilePath dir_name_to =
1087 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1088 FilePath file_name_to =
1089 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1090 FilePath subdir_name_to =
1091 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001092
1093 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1094
1095 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1096
1097 // Check everything has been copied.
1098 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1099 EXPECT_TRUE(file_util::PathExists(file_name_from));
1100 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1101 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1102 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1103 EXPECT_TRUE(file_util::PathExists(file_name_to));
1104 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1105}
1106
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001107TEST_F(FileUtilTest, CopyDirectoryExists) {
1108 // Create a directory.
1109 FilePath dir_name_from =
1110 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1111 file_util::CreateDirectory(dir_name_from);
1112 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1113
1114 // Create a file under the directory.
1115 FilePath file_name_from =
1116 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1117 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1118 ASSERT_TRUE(file_util::PathExists(file_name_from));
1119
1120 // Create a subdirectory.
1121 FilePath subdir_name_from =
1122 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1123 file_util::CreateDirectory(subdir_name_from);
1124 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1125
1126 // Create a file under the subdirectory.
1127 FilePath file_name2_from =
1128 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1129 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1130 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1131
1132 // Copy the directory not recursively.
1133 FilePath dir_name_to =
1134 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1135 FilePath file_name_to =
1136 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1137 FilePath subdir_name_to =
1138 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1139
1140 // Create the destination directory.
1141 file_util::CreateDirectory(dir_name_to);
1142 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1143
1144 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1145
1146 // Check everything has been copied.
1147 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1148 EXPECT_TRUE(file_util::PathExists(file_name_from));
1149 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1150 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1151 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1152 EXPECT_TRUE(file_util::PathExists(file_name_to));
1153 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1154}
1155
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001156TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1157 // Create a file
1158 FilePath file_name_from =
1159 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1160 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1161 ASSERT_TRUE(file_util::PathExists(file_name_from));
1162
1163 // The destination name
1164 FilePath file_name_to =
1165 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1166 ASSERT_FALSE(file_util::PathExists(file_name_to));
1167
1168 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1169
1170 // Check the has been copied
1171 EXPECT_TRUE(file_util::PathExists(file_name_to));
1172}
1173
1174TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1175 // Create a file
1176 FilePath file_name_from =
1177 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1178 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1179 ASSERT_TRUE(file_util::PathExists(file_name_from));
1180
1181 // The destination name
1182 FilePath file_name_to =
1183 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1184 CreateTextFile(file_name_to, L"Old file content");
1185 ASSERT_TRUE(file_util::PathExists(file_name_to));
1186
1187 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1188
1189 // Check the has been copied
1190 EXPECT_TRUE(file_util::PathExists(file_name_to));
1191 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1192}
1193
1194TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1195 // Create a file
1196 FilePath file_name_from =
1197 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1198 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1199 ASSERT_TRUE(file_util::PathExists(file_name_from));
1200
1201 // The destination
1202 FilePath dir_name_to =
1203 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1204 file_util::CreateDirectory(dir_name_to);
1205 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1206 FilePath file_name_to =
1207 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1208
1209 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1210
1211 // Check the has been copied
1212 EXPECT_TRUE(file_util::PathExists(file_name_to));
1213}
1214
initial.commit3f4a7322008-07-27 06:49:38 +09001215TEST_F(FileUtilTest, CopyFile) {
1216 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001217 FilePath dir_name_from =
1218 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1219 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001220 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1221
1222 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001223 FilePath file_name_from =
1224 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001225 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1226 CreateTextFile(file_name_from, file_contents);
1227 ASSERT_TRUE(file_util::PathExists(file_name_from));
1228
1229 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001230 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001231 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001232
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001233 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001234 FilePath dest_file2(dir_name_from);
1235 dest_file2 = dest_file2.AppendASCII("..");
1236 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1237 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1238
1239 FilePath dest_file2_test(dir_name_from);
1240 dest_file2_test = dest_file2_test.DirName();
1241 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001242
1243 // Check everything has been copied.
1244 EXPECT_TRUE(file_util::PathExists(file_name_from));
1245 EXPECT_TRUE(file_util::PathExists(dest_file));
1246 const std::wstring read_contents = ReadTextFile(dest_file);
1247 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +09001248 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1249 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001250}
1251
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001252// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +09001253#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001254TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +09001255 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001256
1257 SYSTEMTIME start_time;
1258 GetLocalTime(&start_time);
1259 Sleep(100);
1260 CreateTextFile(file_name, L"New file!");
1261 Sleep(100);
1262 SYSTEMTIME end_time;
1263 GetLocalTime(&end_time);
1264
1265 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +09001266 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +09001267
1268 FILETIME start_filetime;
1269 SystemTimeToFileTime(&start_time, &start_filetime);
1270 FILETIME end_filetime;
1271 SystemTimeToFileTime(&end_time, &end_filetime);
1272 FILETIME file_creation_filetime;
1273 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1274
1275 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1276 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1277 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1278
1279 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1280 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1281 "end time: " << FileTimeAsUint64(end_filetime);
1282
evanm@google.com874d1672008-10-31 08:54:04 +09001283 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +09001284}
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001285#endif
initial.commit3f4a7322008-07-27 06:49:38 +09001286
erikkay@google.comf2406842008-08-21 00:59:49 +09001287// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001288// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001289typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001290
erikkay@google.comf2406842008-08-21 00:59:49 +09001291TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001292 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +09001293 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +09001294 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1295 .Append(FILE_PATH_LITERAL("data"))
1296 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +09001297 ASSERT_TRUE(file_util::PathExists(data_dir));
1298
evanm@google.com874d1672008-10-31 08:54:04 +09001299 FilePath original_file =
1300 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1301 FilePath same_file =
1302 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1303 FilePath same_length_file =
1304 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1305 FilePath different_file =
1306 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1307 FilePath different_first_file =
1308 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1309 FilePath different_last_file =
1310 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1311 FilePath empty1_file =
1312 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1313 FilePath empty2_file =
1314 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1315 FilePath shortened_file =
1316 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1317 FilePath binary_file =
1318 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1319 FilePath binary_file_same =
1320 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1321 FilePath binary_file_diff =
1322 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001323
1324 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1325 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1326 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1327 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +09001328 EXPECT_FALSE(file_util::ContentsEqual(
1329 FilePath(FILE_PATH_LITERAL("bogusname")),
1330 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +09001331 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1332 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1333 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1334 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1335 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1336 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1337 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1338}
1339
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001340TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1341 FilePath data_dir;
1342 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1343 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1344 .Append(FILE_PATH_LITERAL("data"))
1345 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1346 ASSERT_TRUE(file_util::PathExists(data_dir));
1347
1348 FilePath original_file =
1349 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1350 FilePath same_file =
1351 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1352 FilePath crlf_file =
1353 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1354 FilePath shortened_file =
1355 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1356 FilePath different_file =
1357 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1358 FilePath different_first_file =
1359 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1360 FilePath different_last_file =
1361 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1362 FilePath first1_file =
1363 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1364 FilePath first2_file =
1365 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1366 FilePath empty1_file =
1367 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1368 FilePath empty2_file =
1369 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1370 FilePath blank_line_file =
1371 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1372 FilePath blank_line_crlf_file =
1373 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1374
1375 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1376 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1377 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1378 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1379 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1380 different_first_file));
1381 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1382 different_last_file));
1383 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1384 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1385 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1386 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1387 blank_line_crlf_file));
1388}
1389
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001390// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001391#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001392TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001393 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001394 CreateTextFile(target_file, L"This is the target.");
1395
evanm@google.com874d1672008-10-31 08:54:04 +09001396 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001397
1398 HRESULT result;
1399 IShellLink *shell = NULL;
1400 IPersistFile *persist = NULL;
1401
1402 CoInitialize(NULL);
1403 // Temporarily create a shortcut for test
1404 result = CoCreateInstance(CLSID_ShellLink, NULL,
1405 CLSCTX_INPROC_SERVER, IID_IShellLink,
1406 reinterpret_cast<LPVOID*>(&shell));
1407 EXPECT_TRUE(SUCCEEDED(result));
1408 result = shell->QueryInterface(IID_IPersistFile,
1409 reinterpret_cast<LPVOID*>(&persist));
1410 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001411 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001412 EXPECT_TRUE(SUCCEEDED(result));
1413 result = shell->SetDescription(L"ResolveShortcutTest");
1414 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001415 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001416 EXPECT_TRUE(SUCCEEDED(result));
1417 if (persist)
1418 persist->Release();
1419 if (shell)
1420 shell->Release();
1421
1422 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001423 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001424 EXPECT_TRUE(is_solved);
1425 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001426 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001427 EXPECT_EQ(L"This is the target.", contents);
1428
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001429 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001430 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001431 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001432 CoUninitialize();
1433}
1434
1435TEST_F(FileUtilTest, CreateShortcutTest) {
1436 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001437 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001438 CreateTextFile(target_file, file_contents);
1439
evanm@google.com874d1672008-10-31 08:54:04 +09001440 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001441
1442 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001443 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1444 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001445 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001446 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001447 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001448 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001449 EXPECT_EQ(file_contents, read_contents);
1450
evanm@google.com874d1672008-10-31 08:54:04 +09001451 DeleteFile(target_file.value().c_str());
1452 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001453 CoUninitialize();
1454}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001455
1456TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1457 // Create a directory
1458 FilePath dir_name_from =
1459 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1460 file_util::CreateDirectory(dir_name_from);
1461 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1462
1463 // Create a file under the directory
1464 FilePath file_name_from =
1465 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1466 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1467 ASSERT_TRUE(file_util::PathExists(file_name_from));
1468
1469 // Move the directory by using CopyAndDeleteDirectory
1470 FilePath dir_name_to = test_dir_.Append(
1471 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1472 FilePath file_name_to =
1473 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1474
1475 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1476
1477 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1478
1479 // Check everything has been moved.
1480 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1481 EXPECT_FALSE(file_util::PathExists(file_name_from));
1482 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1483 EXPECT_TRUE(file_util::PathExists(file_name_to));
1484}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001485
1486TEST_F(FileUtilTest, GetTempDirTest) {
1487 static const TCHAR* kTmpKey = _T("TMP");
1488 static const TCHAR* kTmpValues[] = {
1489 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1490 };
1491 // Save the original $TMP.
1492 size_t original_tmp_size;
1493 TCHAR* original_tmp;
1494 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1495 // original_tmp may be NULL.
1496
1497 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1498 FilePath path;
1499 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1500 file_util::GetTempDir(&path);
1501 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1502 " result=" << path.value();
1503 }
1504
1505 // Restore the original $TMP.
1506 if (original_tmp) {
1507 ::_tputenv_s(kTmpKey, original_tmp);
1508 free(original_tmp);
1509 } else {
1510 ::_tputenv_s(kTmpKey, _T(""));
1511 }
1512}
1513#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001514
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001515TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1516 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001517 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001518 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001519 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1520 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1521 }
1522 for (int i = 0; i < 3; i++)
1523 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1524 for (int i = 0; i < 3; i++)
1525 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1526}
1527
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001528TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001529 FilePath names[3];
1530 FILE *fps[3];
1531 int i;
1532
1533 // Create; make sure they are open and exist.
1534 for (i = 0; i < 3; ++i) {
1535 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1536 ASSERT_TRUE(fps[i]);
1537 EXPECT_TRUE(file_util::PathExists(names[i]));
1538 }
1539
1540 // Make sure all names are unique.
1541 for (i = 0; i < 3; ++i) {
1542 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1543 }
1544
1545 // Close and delete.
1546 for (i = 0; i < 3; ++i) {
1547 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1548 EXPECT_TRUE(file_util::Delete(names[i], false));
1549 }
initial.commit3f4a7322008-07-27 06:49:38 +09001550}
1551
1552TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001553 FilePath temp_dir;
1554 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1555 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001556 EXPECT_TRUE(file_util::PathExists(temp_dir));
1557 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001558}
1559
skerner@chromium.orge4432392010-05-01 02:00:09 +09001560TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1561 FilePath new_dir;
1562 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1563 test_dir_,
1564 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1565 &new_dir));
1566 EXPECT_TRUE(file_util::PathExists(new_dir));
1567 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1568 EXPECT_TRUE(file_util::Delete(new_dir, false));
1569}
1570
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001571TEST_F(FileUtilTest, GetShmemTempDirTest) {
1572 FilePath dir;
1573 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1574 EXPECT_TRUE(file_util::DirectoryExists(dir));
1575}
1576
initial.commit3f4a7322008-07-27 06:49:38 +09001577TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001578 FilePath test_root =
1579 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001580#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001581 FilePath test_path =
1582 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001583#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001584 FilePath test_path =
1585 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001586#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001587
1588 EXPECT_FALSE(file_util::PathExists(test_path));
1589 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1590 EXPECT_TRUE(file_util::PathExists(test_path));
1591 // CreateDirectory returns true if the DirectoryExists returns true.
1592 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1593
1594 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001595 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001596 EXPECT_FALSE(file_util::PathExists(test_path));
1597 CreateTextFile(test_path, L"test file");
1598 EXPECT_TRUE(file_util::PathExists(test_path));
1599 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1600
1601 EXPECT_TRUE(file_util::Delete(test_root, true));
1602 EXPECT_FALSE(file_util::PathExists(test_root));
1603 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001604
1605 // Verify assumptions made by the Windows implementation:
1606 // 1. The current directory always exists.
1607 // 2. The root directory always exists.
1608 ASSERT_TRUE(file_util::DirectoryExists(
1609 FilePath(FilePath::kCurrentDirectory)));
1610 FilePath top_level = test_root;
1611 while (top_level != top_level.DirName()) {
1612 top_level = top_level.DirName();
1613 }
1614 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1615
1616 // Given these assumptions hold, it should be safe to
1617 // test that "creating" these directories succeeds.
1618 EXPECT_TRUE(file_util::CreateDirectory(
1619 FilePath(FilePath::kCurrentDirectory)));
1620 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001621
1622#if defined(OS_WIN)
1623 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1624 FilePath invalid_path =
1625 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1626 if (!file_util::PathExists(invalid_drive)) {
1627 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1628 }
1629#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001630}
1631
1632TEST_F(FileUtilTest, DetectDirectoryTest) {
1633 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001634 FilePath test_root =
1635 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001636 EXPECT_FALSE(file_util::PathExists(test_root));
1637 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1638 EXPECT_TRUE(file_util::PathExists(test_root));
1639 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1640
1641 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001642 FilePath test_path =
1643 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001644 EXPECT_FALSE(file_util::PathExists(test_path));
1645 CreateTextFile(test_path, L"test file");
1646 EXPECT_TRUE(file_util::PathExists(test_path));
1647 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1648 EXPECT_TRUE(file_util::Delete(test_path, false));
1649
1650 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001651}
1652
initial.commit3f4a7322008-07-27 06:49:38 +09001653TEST_F(FileUtilTest, FileEnumeratorTest) {
1654 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001655 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001656 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1657 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001658
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001659 // Test an empty directory, non-recursively, including "..".
1660 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1661 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1662 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1663 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1664 f0_dotdot.Next().value());
1665 EXPECT_EQ(FILE_PATH_LITERAL(""),
1666 f0_dotdot.Next().value());
1667
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001668 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001669 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001670 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001671 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001672 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001673 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001674 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001675
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001676 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001677 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001678 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001679 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001680 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001681 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001682 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001683 FilePath file2_rel =
1684 dir2.Append(FilePath::kParentDirectory)
1685 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001686 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001687 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001688
1689 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001690 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001691 file_util::FileEnumerator::FILES);
1692 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001693 EXPECT_TRUE(c1.HasFile(file1));
1694 EXPECT_TRUE(c1.HasFile(file2_abs));
1695 EXPECT_TRUE(c1.HasFile(dir2file));
1696 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1697 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001698
1699 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001700 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001701 file_util::FileEnumerator::DIRECTORIES);
1702 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001703 EXPECT_TRUE(c2.HasFile(dir1));
1704 EXPECT_TRUE(c2.HasFile(dir2));
1705 EXPECT_TRUE(c2.HasFile(dir2inner));
1706 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001707
tim@chromium.org989d0972008-10-16 11:42:45 +09001708 // Only enumerate directories non-recursively.
1709 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001710 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001711 FindResultCollector c2_non_recursive(f2_non_recursive);
1712 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1713 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1714 EXPECT_EQ(c2_non_recursive.size(), 2);
1715
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001716 // Only enumerate directories, non-recursively, including "..".
1717 file_util::FileEnumerator f2_dotdot(
1718 test_dir_, false,
1719 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1720 file_util::FileEnumerator::DIRECTORIES |
1721 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1722 FindResultCollector c2_dotdot(f2_dotdot);
1723 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1724 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1725 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1726 EXPECT_EQ(c2_dotdot.size(), 3);
1727
initial.commit3f4a7322008-07-27 06:49:38 +09001728 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001729 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001730 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001731 EXPECT_TRUE(c3.HasFile(dir1));
1732 EXPECT_TRUE(c3.HasFile(dir2));
1733 EXPECT_TRUE(c3.HasFile(file1));
1734 EXPECT_TRUE(c3.HasFile(file2_abs));
1735 EXPECT_TRUE(c3.HasFile(dir2file));
1736 EXPECT_TRUE(c3.HasFile(dir2inner));
1737 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1738 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001739
1740 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001741 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001742 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001743 EXPECT_TRUE(c4.HasFile(dir2));
1744 EXPECT_TRUE(c4.HasFile(dir2));
1745 EXPECT_TRUE(c4.HasFile(file1));
1746 EXPECT_TRUE(c4.HasFile(file2_abs));
1747 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001748
1749 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001750 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001751 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001752 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001753 EXPECT_TRUE(c5.HasFile(dir1));
1754 EXPECT_TRUE(c5.HasFile(dir2));
1755 EXPECT_TRUE(c5.HasFile(dir2file));
1756 EXPECT_TRUE(c5.HasFile(dir2inner));
1757 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1758 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001759
1760 // Make sure the destructor closes the find handle while in the middle of a
1761 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001762 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001763 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1764 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001765}
license.botf003cfe2008-08-24 09:55:55 +09001766
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001767TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001768 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001769
1770 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001771 if (file_util::PathExists(data_dir)) {
1772 ASSERT_TRUE(file_util::Delete(data_dir, true));
1773 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001774 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1775
1776 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1777 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1778 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1779 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1780
1781 // Annoyingly, the directories must actually exist in order for realpath(),
1782 // which Contains() relies on in posix, to work.
1783 ASSERT_TRUE(file_util::CreateDirectory(foo));
1784 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001785 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1786 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1787 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001788
1789 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1790 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1791 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1792 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1793
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001794 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001795 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1796#if defined(OS_WIN)
1797 EXPECT_TRUE(file_util::ContainsPath(foo,
1798 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001799 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001800 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001801#elif defined(OS_MACOSX)
1802 // We can't really do this test on OS X since the case-sensitivity of the
1803 // filesystem is configurable.
1804#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001805 EXPECT_FALSE(file_util::ContainsPath(foo,
1806 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001807#endif
1808}
1809
jochen@chromium.orga6879772010-02-18 19:02:26 +09001810TEST_F(FileUtilTest, LastModified) {
1811 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1812
1813 // Create a fresh, empty copy of this directory.
1814 if (file_util::PathExists(data_dir)) {
1815 ASSERT_TRUE(file_util::Delete(data_dir, true));
1816 }
1817 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1818
1819 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1820 std::string data("hello");
1821 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1822
1823 base::Time modification_time;
1824 // Note that this timestamp is divisible by two (seconds) - FAT stores
1825 // modification times with 2s resolution.
1826 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1827 &modification_time));
1828 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1829 file_util::FileInfo file_info;
1830 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1831 ASSERT_TRUE(file_info.last_modified == modification_time);
1832}
1833
tfarina@chromium.org34828222010-05-26 10:40:12 +09001834TEST_F(FileUtilTest, IsDirectoryEmpty) {
1835 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1836
1837 ASSERT_FALSE(file_util::PathExists(empty_dir));
1838
1839 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1840
1841 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1842
1843 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1844 std::string bar("baz");
1845 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1846
1847 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1848}
1849
mark@chromium.org17684802008-09-10 09:16:28 +09001850} // namespace