blob: 219b63da785c7214e9df931955ee52b51d76e05c [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
glider@chromium.org5fd12332010-06-10 22:05:26 +0900409// Flaky, http://crbug.com/46246
glider@chromium.orge1879a22010-06-10 21:40:52 +0900410TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commit3f4a7322008-07-27 06:49:38 +0900411 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900412 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900413 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
414
415 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900416#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900417 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900418#elif defined(OS_POSIX)
419 // We need to wait at least one second here because the precision of
420 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900421 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900422#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900423
424 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900425 base::Time now(base::Time::NowFromSystemTime());
426 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900427
428 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900429 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900430 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
431
432 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900433 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900434
435 // Delete new file, we should see no files after cutoff now
436 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900437 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900438}
439
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900440TEST_F(FileUtilTest, FileAndDirectorySize) {
441 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
442 // should return 53 bytes.
443 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
444 CreateTextFile(file_01, L"12345678901234567890");
445 int64 size_f1 = 0;
446 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
447 EXPECT_EQ(20ll, size_f1);
448
449 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
450 file_util::CreateDirectory(subdir_path);
451
452 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
453 CreateTextFile(file_02, L"123456789012345678901234567890");
454 int64 size_f2 = 0;
455 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
456 EXPECT_EQ(30ll, size_f2);
457
458 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
459 file_util::CreateDirectory(subsubdir_path);
460
461 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
462 CreateTextFile(file_03, L"123");
463
464 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
465 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
466}
467
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900468TEST_F(FileUtilTest, NormalizeFilePathBasic) {
469 // Create a directory under the test dir. Because we create it,
470 // we know it is not a link.
471 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
472 FilePath dir_path = test_dir_.Append(FPL("dir"));
473 FilePath file_b_path = dir_path.Append(FPL("file_b"));
474 file_util::CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900475
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900476 FilePath normalized_file_a_path, normalized_file_b_path;
477 ASSERT_FALSE(file_util::PathExists(file_a_path));
478 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
479 &normalized_file_a_path))
480 << "NormalizeFilePath() should fail on nonexistant paths.";
481
482 CreateTextFile(file_a_path, bogus_content);
483 ASSERT_TRUE(file_util::PathExists(file_a_path));
484 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
485 &normalized_file_a_path));
486
487 CreateTextFile(file_b_path, bogus_content);
488 ASSERT_TRUE(file_util::PathExists(file_b_path));
489 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
490 &normalized_file_b_path));
491
492 // Beacuse this test created |dir_path|, we know it is not a link
493 // or junction. So, the real path of the directory holding file a
494 // must be the parent of the path holding file b.
495 ASSERT_TRUE(normalized_file_a_path.DirName()
496 .IsParent(normalized_file_b_path.DirName()));
497}
498
499#if defined(OS_WIN)
500
501TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
502 // Build the following directory structure:
503 //
504 // test_dir_
505 // |-> base_a
506 // | |-> sub_a
507 // | |-> file.txt
508 // | |-> long_name___... (Very long name.)
509 // | |-> sub_long
510 // | |-> deep.txt
511 // |-> base_b
512 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
513 // |-> to_base_b (reparse point to test_dir_\base_b)
514 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
515
516 FilePath base_a = test_dir_.Append(FPL("base_a"));
517 ASSERT_TRUE(file_util::CreateDirectory(base_a));
518
519 FilePath sub_a = base_a.Append(FPL("sub_a"));
520 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
521
522 FilePath file_txt = sub_a.Append(FPL("file.txt"));
523 CreateTextFile(file_txt, bogus_content);
524
525 // Want a directory whose name is long enough to make the path to the file
526 // inside just under MAX_PATH chars. This will be used to test that when
527 // a junction expands to a path over MAX_PATH chars in length,
528 // NormalizeFilePath() fails without crashing.
529 FilePath sub_long_rel(FPL("sub_long"));
530 FilePath deep_txt(FPL("deep.txt"));
531
532 int target_length = MAX_PATH;
533 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
534 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900535 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900536 // the resulting path is still long enough to hit the failing case in
537 // NormalizePath().
538 const int kCreateDirLimit = 4;
539 target_length -= kCreateDirLimit;
540 FilePath::StringType long_name_str = FPL("long_name_");
541 long_name_str.resize(target_length, '_');
542
543 FilePath long_name = sub_a.Append(FilePath(long_name_str));
544 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
545 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
546
547 FilePath sub_long = deep_file.DirName();
548 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
549 CreateTextFile(deep_file, bogus_content);
550
551 FilePath base_b = test_dir_.Append(FPL("base_b"));
552 ASSERT_TRUE(file_util::CreateDirectory(base_b));
553
554 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
555 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
556 ScopedHandle reparse_to_sub_a(
557 ::CreateFile(to_sub_a.value().c_str(),
558 FILE_ALL_ACCESS,
559 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
560 NULL,
561 OPEN_EXISTING,
562 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
563 NULL));
564 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_a.Get());
565 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
566
567 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
568 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
569 ScopedHandle reparse_to_base_b(
570 ::CreateFile(to_base_b.value().c_str(),
571 FILE_ALL_ACCESS,
572 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
573 NULL,
574 OPEN_EXISTING,
575 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
576 NULL));
577 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_base_b.Get());
578 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
579
580 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
581 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
582 ScopedHandle reparse_to_sub_long(
583 ::CreateFile(to_sub_long.value().c_str(),
584 FILE_ALL_ACCESS,
585 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
586 NULL,
587 OPEN_EXISTING,
588 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
589 NULL));
590 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_long.Get());
591 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
592
593 // Normalize a junction free path: base_a\sub_a\file.txt .
594 FilePath normalized_path;
595 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
596 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
597
598 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
599 // the junction to_sub_a.
600 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
601 &normalized_path));
602 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
603
604 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
605 // normalized to exclude junctions to_base_b and to_sub_a .
606 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
607 .Append(FPL("to_base_b"))
608 .Append(FPL("to_sub_a"))
609 .Append(FPL("file.txt")),
610 &normalized_path));
611 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
612
613 // A long enough path will cause NormalizeFilePath() to fail. Make a long
614 // path using to_base_b many times, and check that paths long enough to fail
615 // do not cause a crash.
616 FilePath long_path = base_b;
617 const int kLengthLimit = MAX_PATH + 200;
618 while (long_path.value().length() <= kLengthLimit) {
619 long_path = long_path.Append(FPL("to_base_b"));
620 }
621 long_path = long_path.Append(FPL("to_sub_a"))
622 .Append(FPL("file.txt"));
623
624 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
625
626 // Normalizing the junction to deep.txt should fail, because the expanded
627 // path to deep.txt is longer than MAX_PATH.
628 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
629 &normalized_path));
630
631 // Delete the reparse points, and see that NormalizeFilePath() fails
632 // to traverse them.
633 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
634 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
635 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
636
637 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
638 &normalized_path));
639}
640
641#endif // defined(OS_WIN)
642
643// The following test of NormalizeFilePath() require that we create a symlink.
644// This can not be done on windows before vista. On vista, creating a symlink
645// requires privilege "SeCreateSymbolicLinkPrivilege".
646// TODO(skerner): Investigate the possibility of giving base_unittests the
647// privileges required to create a symlink.
648#if defined(OS_POSIX)
649
650bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
651 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
652}
653
654TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
655 FilePath normalized_path;
skerner@chromium.org559baa92010-05-13 00:13:57 +0900656
657 // Link one file to another.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900658 FilePath link_from = test_dir_.Append(FPL("from_file"));
659 FilePath link_to = test_dir_.Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900660 CreateTextFile(link_to, bogus_content);
661
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900662 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900663 << "Failed to create file symlink.";
664
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900665 // Check that NormalizeFilePath sees the link.
666 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900667 ASSERT_TRUE(link_to != link_from);
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900668 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
669 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900670
671 // Link to a directory.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900672 link_from = test_dir_.Append(FPL("from_dir"));
673 link_to = test_dir_.Append(FPL("to_dir"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900674 file_util::CreateDirectory(link_to);
675
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900676 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900677 << "Failed to create directory symlink.";
678
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900679 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
680 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900681
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900682 // Test that a loop in the links causes NormalizeFilePath() to return false.
683 link_from = test_dir_.Append(FPL("link_a"));
684 link_to = test_dir_.Append(FPL("link_b"));
685 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900686 << "Failed to create loop symlink a.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900687 ASSERT_TRUE(MakeSymlink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900688 << "Failed to create loop symlink b.";
689
690 // Infinite loop!
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900691 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900692}
693#endif // defined(OS_POSIX)
694
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900695TEST_F(FileUtilTest, DeleteNonExistent) {
696 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
697 ASSERT_FALSE(file_util::PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900698
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900699 EXPECT_TRUE(file_util::Delete(non_existent, false));
700 ASSERT_FALSE(file_util::PathExists(non_existent));
701 EXPECT_TRUE(file_util::Delete(non_existent, true));
702 ASSERT_FALSE(file_util::PathExists(non_existent));
703}
704
705TEST_F(FileUtilTest, DeleteFile) {
706 // Create a file
707 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
708 CreateTextFile(file_name, bogus_content);
initial.commit3f4a7322008-07-27 06:49:38 +0900709 ASSERT_TRUE(file_util::PathExists(file_name));
710
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900711 // Make sure it's deleted
712 EXPECT_TRUE(file_util::Delete(file_name, false));
713 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900714
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900715 // Test recursive case, create a new file
716 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
717 CreateTextFile(file_name, bogus_content);
718 ASSERT_TRUE(file_util::PathExists(file_name));
719
720 // Make sure it's deleted
721 EXPECT_TRUE(file_util::Delete(file_name, true));
722 EXPECT_FALSE(file_util::PathExists(file_name));
723}
724
725#if defined(OS_WIN)
726// Tests that the Delete function works for wild cards, especially
727// with the recursion flag. Also coincidentally tests PathExists.
728// TODO(erikkay): see if anyone's actually using this feature of the API
729TEST_F(FileUtilTest, DeleteWildCard) {
730 // Create a file and a directory
731 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
732 CreateTextFile(file_name, bogus_content);
733 ASSERT_TRUE(file_util::PathExists(file_name));
734
735 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
736 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900737 ASSERT_TRUE(file_util::PathExists(subdir_path));
738
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900739 // Create the wildcard path
evanm@google.com874d1672008-10-31 08:54:04 +0900740 FilePath directory_contents = test_dir_;
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900741 directory_contents = directory_contents.Append(FPL("*"));
742
initial.commit3f4a7322008-07-27 06:49:38 +0900743 // Delete non-recursively and check that only the file is deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900744 EXPECT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900745 EXPECT_FALSE(file_util::PathExists(file_name));
746 EXPECT_TRUE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900747
zork@chromium.org61be4f42010-05-07 09:05:36 +0900748 // Delete recursively and make sure all contents are deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900749 EXPECT_TRUE(file_util::Delete(directory_contents, true));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900750 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900751 EXPECT_FALSE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900752}
753
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900754// TODO(erikkay): see if anyone's actually using this feature of the API
755TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
756 // Create a file and a directory
757 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
758 file_util::CreateDirectory(subdir_path);
759 ASSERT_TRUE(file_util::PathExists(subdir_path));
760
761 // Create the wildcard path
762 FilePath directory_contents = subdir_path;
763 directory_contents = directory_contents.Append(FPL("*"));
764
765 // Delete non-recursively and check nothing got deleted
766 EXPECT_TRUE(file_util::Delete(directory_contents, false));
767 EXPECT_TRUE(file_util::PathExists(subdir_path));
768
769 // Delete recursively and check nothing got deleted
770 EXPECT_TRUE(file_util::Delete(directory_contents, true));
771 EXPECT_TRUE(file_util::PathExists(subdir_path));
772}
773#endif
774
775// Tests non-recursive Delete() for a directory.
776TEST_F(FileUtilTest, DeleteDirNonRecursive) {
777 // Create a subdirectory and put a file and two directories inside.
778 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
779 file_util::CreateDirectory(test_subdir);
780 ASSERT_TRUE(file_util::PathExists(test_subdir));
781
782 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
783 CreateTextFile(file_name, bogus_content);
784 ASSERT_TRUE(file_util::PathExists(file_name));
785
786 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
787 file_util::CreateDirectory(subdir_path1);
788 ASSERT_TRUE(file_util::PathExists(subdir_path1));
789
790 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
791 file_util::CreateDirectory(subdir_path2);
792 ASSERT_TRUE(file_util::PathExists(subdir_path2));
793
794 // Delete non-recursively and check that the empty dir got deleted
795 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
796 EXPECT_FALSE(file_util::PathExists(subdir_path2));
797
798 // Delete non-recursively and check that nothing got deleted
799 EXPECT_FALSE(file_util::Delete(test_subdir, false));
800 EXPECT_TRUE(file_util::PathExists(test_subdir));
801 EXPECT_TRUE(file_util::PathExists(file_name));
802 EXPECT_TRUE(file_util::PathExists(subdir_path1));
803}
804
805// Tests recursive Delete() for a directory.
806TEST_F(FileUtilTest, DeleteDirRecursive) {
807 // Create a subdirectory and put a file and two directories inside.
808 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
809 file_util::CreateDirectory(test_subdir);
810 ASSERT_TRUE(file_util::PathExists(test_subdir));
811
812 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
813 CreateTextFile(file_name, bogus_content);
814 ASSERT_TRUE(file_util::PathExists(file_name));
815
816 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
817 file_util::CreateDirectory(subdir_path1);
818 ASSERT_TRUE(file_util::PathExists(subdir_path1));
819
820 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
821 file_util::CreateDirectory(subdir_path2);
822 ASSERT_TRUE(file_util::PathExists(subdir_path2));
823
824 // Delete recursively and check that the empty dir got deleted
825 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
826 EXPECT_FALSE(file_util::PathExists(subdir_path2));
827
828 // Delete recursively and check that everything got deleted
829 EXPECT_TRUE(file_util::Delete(test_subdir, true));
830 EXPECT_FALSE(file_util::PathExists(file_name));
831 EXPECT_FALSE(file_util::PathExists(subdir_path1));
832 EXPECT_FALSE(file_util::PathExists(test_subdir));
833}
834
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900835TEST_F(FileUtilTest, MoveFileNew) {
836 // Create a file
837 FilePath file_name_from =
838 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
839 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
840 ASSERT_TRUE(file_util::PathExists(file_name_from));
841
842 // The destination
843 FilePath file_name_to =
844 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
845 ASSERT_FALSE(file_util::PathExists(file_name_to));
846
847 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
848
849 // Check everything has been moved.
850 EXPECT_FALSE(file_util::PathExists(file_name_from));
851 EXPECT_TRUE(file_util::PathExists(file_name_to));
852}
853
854TEST_F(FileUtilTest, MoveFileExists) {
855 // Create a file
856 FilePath file_name_from =
857 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
858 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
859 ASSERT_TRUE(file_util::PathExists(file_name_from));
860
861 // The destination name
862 FilePath file_name_to =
863 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
864 CreateTextFile(file_name_to, L"Old file content");
865 ASSERT_TRUE(file_util::PathExists(file_name_to));
866
867 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
868
869 // Check everything has been moved.
870 EXPECT_FALSE(file_util::PathExists(file_name_from));
871 EXPECT_TRUE(file_util::PathExists(file_name_to));
872 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
873}
874
875TEST_F(FileUtilTest, MoveFileDirExists) {
876 // Create a file
877 FilePath file_name_from =
878 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
879 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
880 ASSERT_TRUE(file_util::PathExists(file_name_from));
881
882 // The destination directory
883 FilePath dir_name_to =
884 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
885 file_util::CreateDirectory(dir_name_to);
886 ASSERT_TRUE(file_util::PathExists(dir_name_to));
887
888 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
889}
890
891
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900892TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900893 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900894 FilePath dir_name_from =
895 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
896 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900897 ASSERT_TRUE(file_util::PathExists(dir_name_from));
898
899 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900900 FilePath file_name_from =
901 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900902 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
903 ASSERT_TRUE(file_util::PathExists(file_name_from));
904
905 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900906 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
907 FilePath file_name_to =
908 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900909
910 ASSERT_FALSE(file_util::PathExists(dir_name_to));
911
912 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
913
914 // Check everything has been moved.
915 EXPECT_FALSE(file_util::PathExists(dir_name_from));
916 EXPECT_FALSE(file_util::PathExists(file_name_from));
917 EXPECT_TRUE(file_util::PathExists(dir_name_to));
918 EXPECT_TRUE(file_util::PathExists(file_name_to));
919}
920
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900921TEST_F(FileUtilTest, MoveExist) {
922 // Create a directory
923 FilePath dir_name_from =
924 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
925 file_util::CreateDirectory(dir_name_from);
926 ASSERT_TRUE(file_util::PathExists(dir_name_from));
927
928 // Create a file under the directory
929 FilePath file_name_from =
930 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
931 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
932 ASSERT_TRUE(file_util::PathExists(file_name_from));
933
934 // Move the directory
935 FilePath dir_name_exists =
936 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
937
938 FilePath dir_name_to =
939 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
940 FilePath file_name_to =
941 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
942
943 // Create the destination directory.
944 file_util::CreateDirectory(dir_name_exists);
945 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
946
947 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
948
949 // Check everything has been moved.
950 EXPECT_FALSE(file_util::PathExists(dir_name_from));
951 EXPECT_FALSE(file_util::PathExists(file_name_from));
952 EXPECT_TRUE(file_util::PathExists(dir_name_to));
953 EXPECT_TRUE(file_util::PathExists(file_name_to));
954}
955
956TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900957 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900958 FilePath dir_name_from =
959 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
960 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900961 ASSERT_TRUE(file_util::PathExists(dir_name_from));
962
963 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900964 FilePath file_name_from =
965 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900966 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
967 ASSERT_TRUE(file_util::PathExists(file_name_from));
968
969 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900970 FilePath subdir_name_from =
971 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
972 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900973 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
974
975 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900976 FilePath file_name2_from =
977 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900978 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
979 ASSERT_TRUE(file_util::PathExists(file_name2_from));
980
981 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900982 FilePath dir_name_to =
983 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
984 FilePath file_name_to =
985 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
986 FilePath subdir_name_to =
987 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
988 FilePath file_name2_to =
989 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900990
991 ASSERT_FALSE(file_util::PathExists(dir_name_to));
992
993 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
994
995 // Check everything has been copied.
996 EXPECT_TRUE(file_util::PathExists(dir_name_from));
997 EXPECT_TRUE(file_util::PathExists(file_name_from));
998 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
999 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1000 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1001 EXPECT_TRUE(file_util::PathExists(file_name_to));
1002 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1003 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1004}
1005
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001006TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1007 // Create a directory.
1008 FilePath dir_name_from =
1009 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1010 file_util::CreateDirectory(dir_name_from);
1011 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1012
1013 // Create a file under the directory.
1014 FilePath file_name_from =
1015 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1016 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1017 ASSERT_TRUE(file_util::PathExists(file_name_from));
1018
1019 // Create a subdirectory.
1020 FilePath subdir_name_from =
1021 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1022 file_util::CreateDirectory(subdir_name_from);
1023 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1024
1025 // Create a file under the subdirectory.
1026 FilePath file_name2_from =
1027 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1028 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1029 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1030
1031 // Copy the directory recursively.
1032 FilePath dir_name_exists =
1033 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1034
1035 FilePath dir_name_to =
1036 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1037 FilePath file_name_to =
1038 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1039 FilePath subdir_name_to =
1040 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1041 FilePath file_name2_to =
1042 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1043
1044 // Create the destination directory.
1045 file_util::CreateDirectory(dir_name_exists);
1046 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1047
1048 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1049
1050 // Check everything has been copied.
1051 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1052 EXPECT_TRUE(file_util::PathExists(file_name_from));
1053 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1054 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1055 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1056 EXPECT_TRUE(file_util::PathExists(file_name_to));
1057 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1058 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1059}
1060
1061TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001062 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001063 FilePath dir_name_from =
1064 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1065 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001066 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1067
1068 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001069 FilePath file_name_from =
1070 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001071 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1072 ASSERT_TRUE(file_util::PathExists(file_name_from));
1073
1074 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001075 FilePath subdir_name_from =
1076 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1077 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001078 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1079
1080 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001081 FilePath file_name2_from =
1082 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001083 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1084 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1085
1086 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001087 FilePath dir_name_to =
1088 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1089 FilePath file_name_to =
1090 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1091 FilePath subdir_name_to =
1092 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001093
1094 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1095
1096 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1097
1098 // Check everything has been copied.
1099 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1100 EXPECT_TRUE(file_util::PathExists(file_name_from));
1101 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1102 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1103 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1104 EXPECT_TRUE(file_util::PathExists(file_name_to));
1105 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1106}
1107
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001108TEST_F(FileUtilTest, CopyDirectoryExists) {
1109 // Create a directory.
1110 FilePath dir_name_from =
1111 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1112 file_util::CreateDirectory(dir_name_from);
1113 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1114
1115 // Create a file under the directory.
1116 FilePath file_name_from =
1117 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1118 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1119 ASSERT_TRUE(file_util::PathExists(file_name_from));
1120
1121 // Create a subdirectory.
1122 FilePath subdir_name_from =
1123 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1124 file_util::CreateDirectory(subdir_name_from);
1125 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1126
1127 // Create a file under the subdirectory.
1128 FilePath file_name2_from =
1129 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1130 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1131 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1132
1133 // Copy the directory not recursively.
1134 FilePath dir_name_to =
1135 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1136 FilePath file_name_to =
1137 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1138 FilePath subdir_name_to =
1139 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1140
1141 // Create the destination directory.
1142 file_util::CreateDirectory(dir_name_to);
1143 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1144
1145 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1146
1147 // Check everything has been copied.
1148 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1149 EXPECT_TRUE(file_util::PathExists(file_name_from));
1150 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1151 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1152 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1153 EXPECT_TRUE(file_util::PathExists(file_name_to));
1154 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1155}
1156
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001157TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1158 // Create a file
1159 FilePath file_name_from =
1160 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1161 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1162 ASSERT_TRUE(file_util::PathExists(file_name_from));
1163
1164 // The destination name
1165 FilePath file_name_to =
1166 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1167 ASSERT_FALSE(file_util::PathExists(file_name_to));
1168
1169 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1170
1171 // Check the has been copied
1172 EXPECT_TRUE(file_util::PathExists(file_name_to));
1173}
1174
1175TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1176 // Create a file
1177 FilePath file_name_from =
1178 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1179 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1180 ASSERT_TRUE(file_util::PathExists(file_name_from));
1181
1182 // The destination name
1183 FilePath file_name_to =
1184 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1185 CreateTextFile(file_name_to, L"Old file content");
1186 ASSERT_TRUE(file_util::PathExists(file_name_to));
1187
1188 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1189
1190 // Check the has been copied
1191 EXPECT_TRUE(file_util::PathExists(file_name_to));
1192 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1193}
1194
1195TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1196 // Create a file
1197 FilePath file_name_from =
1198 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1199 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1200 ASSERT_TRUE(file_util::PathExists(file_name_from));
1201
1202 // The destination
1203 FilePath dir_name_to =
1204 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1205 file_util::CreateDirectory(dir_name_to);
1206 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1207 FilePath file_name_to =
1208 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1209
1210 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1211
1212 // Check the has been copied
1213 EXPECT_TRUE(file_util::PathExists(file_name_to));
1214}
1215
initial.commit3f4a7322008-07-27 06:49:38 +09001216TEST_F(FileUtilTest, CopyFile) {
1217 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001218 FilePath dir_name_from =
1219 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1220 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001221 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1222
1223 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001224 FilePath file_name_from =
1225 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001226 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1227 CreateTextFile(file_name_from, file_contents);
1228 ASSERT_TRUE(file_util::PathExists(file_name_from));
1229
1230 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001231 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001232 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001233
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001234 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001235 FilePath dest_file2(dir_name_from);
1236 dest_file2 = dest_file2.AppendASCII("..");
1237 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1238 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1239
1240 FilePath dest_file2_test(dir_name_from);
1241 dest_file2_test = dest_file2_test.DirName();
1242 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001243
1244 // Check everything has been copied.
1245 EXPECT_TRUE(file_util::PathExists(file_name_from));
1246 EXPECT_TRUE(file_util::PathExists(dest_file));
1247 const std::wstring read_contents = ReadTextFile(dest_file);
1248 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +09001249 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1250 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001251}
1252
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001253// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +09001254#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001255TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +09001256 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001257
1258 SYSTEMTIME start_time;
1259 GetLocalTime(&start_time);
1260 Sleep(100);
1261 CreateTextFile(file_name, L"New file!");
1262 Sleep(100);
1263 SYSTEMTIME end_time;
1264 GetLocalTime(&end_time);
1265
1266 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +09001267 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +09001268
1269 FILETIME start_filetime;
1270 SystemTimeToFileTime(&start_time, &start_filetime);
1271 FILETIME end_filetime;
1272 SystemTimeToFileTime(&end_time, &end_filetime);
1273 FILETIME file_creation_filetime;
1274 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1275
1276 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1277 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1278 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1279
1280 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1281 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1282 "end time: " << FileTimeAsUint64(end_filetime);
1283
evanm@google.com874d1672008-10-31 08:54:04 +09001284 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +09001285}
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001286#endif
initial.commit3f4a7322008-07-27 06:49:38 +09001287
erikkay@google.comf2406842008-08-21 00:59:49 +09001288// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001289// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001290typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001291
erikkay@google.comf2406842008-08-21 00:59:49 +09001292TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001293 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +09001294 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +09001295 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1296 .Append(FILE_PATH_LITERAL("data"))
1297 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +09001298 ASSERT_TRUE(file_util::PathExists(data_dir));
1299
evanm@google.com874d1672008-10-31 08:54:04 +09001300 FilePath original_file =
1301 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1302 FilePath same_file =
1303 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1304 FilePath same_length_file =
1305 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1306 FilePath different_file =
1307 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1308 FilePath different_first_file =
1309 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1310 FilePath different_last_file =
1311 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1312 FilePath empty1_file =
1313 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1314 FilePath empty2_file =
1315 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1316 FilePath shortened_file =
1317 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1318 FilePath binary_file =
1319 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1320 FilePath binary_file_same =
1321 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1322 FilePath binary_file_diff =
1323 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001324
1325 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1326 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1327 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1328 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +09001329 EXPECT_FALSE(file_util::ContentsEqual(
1330 FilePath(FILE_PATH_LITERAL("bogusname")),
1331 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +09001332 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1333 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1334 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1335 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1336 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1337 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1338 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1339}
1340
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001341TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1342 FilePath data_dir;
1343 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1344 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1345 .Append(FILE_PATH_LITERAL("data"))
1346 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1347 ASSERT_TRUE(file_util::PathExists(data_dir));
1348
1349 FilePath original_file =
1350 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1351 FilePath same_file =
1352 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1353 FilePath crlf_file =
1354 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1355 FilePath shortened_file =
1356 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1357 FilePath different_file =
1358 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1359 FilePath different_first_file =
1360 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1361 FilePath different_last_file =
1362 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1363 FilePath first1_file =
1364 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1365 FilePath first2_file =
1366 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1367 FilePath empty1_file =
1368 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1369 FilePath empty2_file =
1370 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1371 FilePath blank_line_file =
1372 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1373 FilePath blank_line_crlf_file =
1374 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1375
1376 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1377 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1378 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1379 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1380 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1381 different_first_file));
1382 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1383 different_last_file));
1384 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1385 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1386 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1387 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1388 blank_line_crlf_file));
1389}
1390
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001391// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001392#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001393TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001394 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001395 CreateTextFile(target_file, L"This is the target.");
1396
evanm@google.com874d1672008-10-31 08:54:04 +09001397 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001398
1399 HRESULT result;
1400 IShellLink *shell = NULL;
1401 IPersistFile *persist = NULL;
1402
1403 CoInitialize(NULL);
1404 // Temporarily create a shortcut for test
1405 result = CoCreateInstance(CLSID_ShellLink, NULL,
1406 CLSCTX_INPROC_SERVER, IID_IShellLink,
1407 reinterpret_cast<LPVOID*>(&shell));
1408 EXPECT_TRUE(SUCCEEDED(result));
1409 result = shell->QueryInterface(IID_IPersistFile,
1410 reinterpret_cast<LPVOID*>(&persist));
1411 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001412 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001413 EXPECT_TRUE(SUCCEEDED(result));
1414 result = shell->SetDescription(L"ResolveShortcutTest");
1415 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001416 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001417 EXPECT_TRUE(SUCCEEDED(result));
1418 if (persist)
1419 persist->Release();
1420 if (shell)
1421 shell->Release();
1422
1423 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001424 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001425 EXPECT_TRUE(is_solved);
1426 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001427 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001428 EXPECT_EQ(L"This is the target.", contents);
1429
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001430 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001431 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001432 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001433 CoUninitialize();
1434}
1435
1436TEST_F(FileUtilTest, CreateShortcutTest) {
1437 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001438 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001439 CreateTextFile(target_file, file_contents);
1440
evanm@google.com874d1672008-10-31 08:54:04 +09001441 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001442
1443 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001444 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1445 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001446 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001447 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001448 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001449 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001450 EXPECT_EQ(file_contents, read_contents);
1451
evanm@google.com874d1672008-10-31 08:54:04 +09001452 DeleteFile(target_file.value().c_str());
1453 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001454 CoUninitialize();
1455}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001456
1457TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1458 // Create a directory
1459 FilePath dir_name_from =
1460 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1461 file_util::CreateDirectory(dir_name_from);
1462 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1463
1464 // Create a file under the directory
1465 FilePath file_name_from =
1466 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1467 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1468 ASSERT_TRUE(file_util::PathExists(file_name_from));
1469
1470 // Move the directory by using CopyAndDeleteDirectory
1471 FilePath dir_name_to = test_dir_.Append(
1472 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1473 FilePath file_name_to =
1474 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1475
1476 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1477
1478 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1479
1480 // Check everything has been moved.
1481 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1482 EXPECT_FALSE(file_util::PathExists(file_name_from));
1483 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1484 EXPECT_TRUE(file_util::PathExists(file_name_to));
1485}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001486
1487TEST_F(FileUtilTest, GetTempDirTest) {
1488 static const TCHAR* kTmpKey = _T("TMP");
1489 static const TCHAR* kTmpValues[] = {
1490 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1491 };
1492 // Save the original $TMP.
1493 size_t original_tmp_size;
1494 TCHAR* original_tmp;
1495 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1496 // original_tmp may be NULL.
1497
1498 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1499 FilePath path;
1500 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1501 file_util::GetTempDir(&path);
1502 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1503 " result=" << path.value();
1504 }
1505
1506 // Restore the original $TMP.
1507 if (original_tmp) {
1508 ::_tputenv_s(kTmpKey, original_tmp);
1509 free(original_tmp);
1510 } else {
1511 ::_tputenv_s(kTmpKey, _T(""));
1512 }
1513}
1514#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001515
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001516TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1517 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001518 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001519 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001520 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1521 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1522 }
1523 for (int i = 0; i < 3; i++)
1524 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1525 for (int i = 0; i < 3; i++)
1526 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1527}
1528
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001529TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001530 FilePath names[3];
1531 FILE *fps[3];
1532 int i;
1533
1534 // Create; make sure they are open and exist.
1535 for (i = 0; i < 3; ++i) {
1536 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1537 ASSERT_TRUE(fps[i]);
1538 EXPECT_TRUE(file_util::PathExists(names[i]));
1539 }
1540
1541 // Make sure all names are unique.
1542 for (i = 0; i < 3; ++i) {
1543 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1544 }
1545
1546 // Close and delete.
1547 for (i = 0; i < 3; ++i) {
1548 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1549 EXPECT_TRUE(file_util::Delete(names[i], false));
1550 }
initial.commit3f4a7322008-07-27 06:49:38 +09001551}
1552
1553TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001554 FilePath temp_dir;
1555 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1556 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001557 EXPECT_TRUE(file_util::PathExists(temp_dir));
1558 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001559}
1560
skerner@chromium.orge4432392010-05-01 02:00:09 +09001561TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1562 FilePath new_dir;
1563 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1564 test_dir_,
1565 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1566 &new_dir));
1567 EXPECT_TRUE(file_util::PathExists(new_dir));
1568 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1569 EXPECT_TRUE(file_util::Delete(new_dir, false));
1570}
1571
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001572TEST_F(FileUtilTest, GetShmemTempDirTest) {
1573 FilePath dir;
1574 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1575 EXPECT_TRUE(file_util::DirectoryExists(dir));
1576}
1577
initial.commit3f4a7322008-07-27 06:49:38 +09001578TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001579 FilePath test_root =
1580 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001581#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001582 FilePath test_path =
1583 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001584#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001585 FilePath test_path =
1586 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001587#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001588
1589 EXPECT_FALSE(file_util::PathExists(test_path));
1590 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1591 EXPECT_TRUE(file_util::PathExists(test_path));
1592 // CreateDirectory returns true if the DirectoryExists returns true.
1593 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1594
1595 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001596 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001597 EXPECT_FALSE(file_util::PathExists(test_path));
1598 CreateTextFile(test_path, L"test file");
1599 EXPECT_TRUE(file_util::PathExists(test_path));
1600 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1601
1602 EXPECT_TRUE(file_util::Delete(test_root, true));
1603 EXPECT_FALSE(file_util::PathExists(test_root));
1604 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001605
1606 // Verify assumptions made by the Windows implementation:
1607 // 1. The current directory always exists.
1608 // 2. The root directory always exists.
1609 ASSERT_TRUE(file_util::DirectoryExists(
1610 FilePath(FilePath::kCurrentDirectory)));
1611 FilePath top_level = test_root;
1612 while (top_level != top_level.DirName()) {
1613 top_level = top_level.DirName();
1614 }
1615 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1616
1617 // Given these assumptions hold, it should be safe to
1618 // test that "creating" these directories succeeds.
1619 EXPECT_TRUE(file_util::CreateDirectory(
1620 FilePath(FilePath::kCurrentDirectory)));
1621 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001622
1623#if defined(OS_WIN)
1624 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1625 FilePath invalid_path =
1626 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1627 if (!file_util::PathExists(invalid_drive)) {
1628 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1629 }
1630#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001631}
1632
1633TEST_F(FileUtilTest, DetectDirectoryTest) {
1634 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001635 FilePath test_root =
1636 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001637 EXPECT_FALSE(file_util::PathExists(test_root));
1638 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1639 EXPECT_TRUE(file_util::PathExists(test_root));
1640 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1641
1642 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001643 FilePath test_path =
1644 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001645 EXPECT_FALSE(file_util::PathExists(test_path));
1646 CreateTextFile(test_path, L"test file");
1647 EXPECT_TRUE(file_util::PathExists(test_path));
1648 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1649 EXPECT_TRUE(file_util::Delete(test_path, false));
1650
1651 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001652}
1653
initial.commit3f4a7322008-07-27 06:49:38 +09001654TEST_F(FileUtilTest, FileEnumeratorTest) {
1655 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001656 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001657 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1658 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001659
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001660 // Test an empty directory, non-recursively, including "..".
1661 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1662 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1663 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1664 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1665 f0_dotdot.Next().value());
1666 EXPECT_EQ(FILE_PATH_LITERAL(""),
1667 f0_dotdot.Next().value());
1668
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001669 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001670 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001671 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001672 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001673 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001674 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001675 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001676
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001677 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001678 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001679 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001680 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001681 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001682 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001683 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001684 FilePath file2_rel =
1685 dir2.Append(FilePath::kParentDirectory)
1686 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001687 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001688 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001689
1690 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001691 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001692 file_util::FileEnumerator::FILES);
1693 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001694 EXPECT_TRUE(c1.HasFile(file1));
1695 EXPECT_TRUE(c1.HasFile(file2_abs));
1696 EXPECT_TRUE(c1.HasFile(dir2file));
1697 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1698 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001699
1700 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001701 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001702 file_util::FileEnumerator::DIRECTORIES);
1703 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001704 EXPECT_TRUE(c2.HasFile(dir1));
1705 EXPECT_TRUE(c2.HasFile(dir2));
1706 EXPECT_TRUE(c2.HasFile(dir2inner));
1707 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001708
tim@chromium.org989d0972008-10-16 11:42:45 +09001709 // Only enumerate directories non-recursively.
1710 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001711 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001712 FindResultCollector c2_non_recursive(f2_non_recursive);
1713 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1714 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1715 EXPECT_EQ(c2_non_recursive.size(), 2);
1716
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001717 // Only enumerate directories, non-recursively, including "..".
1718 file_util::FileEnumerator f2_dotdot(
1719 test_dir_, false,
1720 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1721 file_util::FileEnumerator::DIRECTORIES |
1722 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1723 FindResultCollector c2_dotdot(f2_dotdot);
1724 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1725 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1726 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1727 EXPECT_EQ(c2_dotdot.size(), 3);
1728
initial.commit3f4a7322008-07-27 06:49:38 +09001729 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001730 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001731 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001732 EXPECT_TRUE(c3.HasFile(dir1));
1733 EXPECT_TRUE(c3.HasFile(dir2));
1734 EXPECT_TRUE(c3.HasFile(file1));
1735 EXPECT_TRUE(c3.HasFile(file2_abs));
1736 EXPECT_TRUE(c3.HasFile(dir2file));
1737 EXPECT_TRUE(c3.HasFile(dir2inner));
1738 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1739 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001740
1741 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001742 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001743 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001744 EXPECT_TRUE(c4.HasFile(dir2));
1745 EXPECT_TRUE(c4.HasFile(dir2));
1746 EXPECT_TRUE(c4.HasFile(file1));
1747 EXPECT_TRUE(c4.HasFile(file2_abs));
1748 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001749
1750 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001751 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001752 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001753 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001754 EXPECT_TRUE(c5.HasFile(dir1));
1755 EXPECT_TRUE(c5.HasFile(dir2));
1756 EXPECT_TRUE(c5.HasFile(dir2file));
1757 EXPECT_TRUE(c5.HasFile(dir2inner));
1758 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1759 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001760
1761 // Make sure the destructor closes the find handle while in the middle of a
1762 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001763 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001764 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1765 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001766}
license.botf003cfe2008-08-24 09:55:55 +09001767
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001768TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001769 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001770
1771 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001772 if (file_util::PathExists(data_dir)) {
1773 ASSERT_TRUE(file_util::Delete(data_dir, true));
1774 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001775 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1776
1777 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1778 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1779 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1780 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1781
1782 // Annoyingly, the directories must actually exist in order for realpath(),
1783 // which Contains() relies on in posix, to work.
1784 ASSERT_TRUE(file_util::CreateDirectory(foo));
1785 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001786 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1787 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1788 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001789
1790 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1791 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1792 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1793 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1794
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001795 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001796 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1797#if defined(OS_WIN)
1798 EXPECT_TRUE(file_util::ContainsPath(foo,
1799 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001800 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001801 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001802#elif defined(OS_MACOSX)
1803 // We can't really do this test on OS X since the case-sensitivity of the
1804 // filesystem is configurable.
1805#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001806 EXPECT_FALSE(file_util::ContainsPath(foo,
1807 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001808#endif
1809}
1810
jochen@chromium.orga6879772010-02-18 19:02:26 +09001811TEST_F(FileUtilTest, LastModified) {
1812 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1813
1814 // Create a fresh, empty copy of this directory.
1815 if (file_util::PathExists(data_dir)) {
1816 ASSERT_TRUE(file_util::Delete(data_dir, true));
1817 }
1818 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1819
1820 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1821 std::string data("hello");
1822 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1823
1824 base::Time modification_time;
1825 // Note that this timestamp is divisible by two (seconds) - FAT stores
1826 // modification times with 2s resolution.
1827 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1828 &modification_time));
1829 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1830 file_util::FileInfo file_info;
1831 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1832 ASSERT_TRUE(file_info.last_modified == modification_time);
1833}
1834
tfarina@chromium.org34828222010-05-26 10:40:12 +09001835TEST_F(FileUtilTest, IsDirectoryEmpty) {
1836 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1837
1838 ASSERT_FALSE(file_util::PathExists(empty_dir));
1839
1840 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1841
1842 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1843
1844 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1845 std::string bar("baz");
1846 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1847
1848 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1849}
1850
mark@chromium.org17684802008-09-10 09:16:28 +09001851} // namespace