blob: fd54963eba93ab8667d05efee73fb1e6a9843d3f [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
evan@chromium.org5a196702010-07-02 08:19:02 +0900328#if defined(OS_WIN)
329// This function is deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900330TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900331 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900332 const filename_case& value = filename_cases[i];
333 std::wstring result = file_util::GetFilenameFromPath(value.path);
334 EXPECT_EQ(value.filename, result);
335 }
336}
evan@chromium.org5a196702010-07-02 08:19:02 +0900337#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900338
339// Test finding the file type from a path name
340static const struct extension_case {
341 const wchar_t* path;
342 const wchar_t* extension;
343} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900344#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900345 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
346 {L"C:\\colon\\backslash\\filename.", L""},
347 {L"C:\\colon\\backslash\\filename", L""},
348 {L"C:\\colon\\backslash\\", L""},
349 {L"C:\\colon\\backslash.\\", L""},
350 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900351#elif defined(OS_POSIX)
352 {L"/foo/bar/filename.extension", L"extension"},
353 {L"/foo/bar/filename.", L""},
354 {L"/foo/bar/filename", L""},
355 {L"/foo/bar/", L""},
356 {L"/foo/bar./", L""},
357 {L"/foo/bar/filename.extension.extension2", L"extension2"},
358 {L".", L""},
359 {L"..", L""},
360 {L"./foo", L""},
361 {L"./foo.extension", L"extension"},
362 {L"/foo.extension1/bar.extension2", L"extension2"},
363#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900364};
365
366TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900367 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900368 const extension_case& ext = extension_cases[i];
369 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
370 EXPECT_EQ(ext.extension, fext);
371 }
372}
373
374// Test finding the directory component of a path
375static const struct dir_case {
376 const wchar_t* full_path;
377 const wchar_t* directory;
378} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900379#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900380 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
381 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
382 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
383 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
384 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
385 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900386 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900387#elif defined(OS_POSIX)
388 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
389 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
390 {L"/foo/bar/", L"/foo/bar"},
391 {L"/foo/bar//", L"/foo/bar"},
392 {L"/foo/bar", L"/foo"},
393 {L"/foo/bar./", L"/foo/bar."},
394 {L"/", L"/"},
395 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900396 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900397#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900398};
399
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900400#if defined(OS_WIN)
401// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900402TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900403 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900404 const dir_case& dir = dir_cases[i];
405 const std::wstring parent =
406 file_util::GetDirectoryFromPath(dir.full_path);
407 EXPECT_EQ(dir.directory, parent);
408 }
409}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900410#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900411
glider@chromium.org5fd12332010-06-10 22:05:26 +0900412// Flaky, http://crbug.com/46246
glider@chromium.orge1879a22010-06-10 21:40:52 +0900413TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commit3f4a7322008-07-27 06:49:38 +0900414 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900415 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900416 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
417
418 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900419#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900420 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900421#elif defined(OS_POSIX)
422 // We need to wait at least one second here because the precision of
423 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900424 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900425#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900426
427 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900428 base::Time now(base::Time::NowFromSystemTime());
429 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900430
431 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900432 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900433 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
434
435 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900436 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900437
438 // Delete new file, we should see no files after cutoff now
439 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900440 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900441}
442
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900443TEST_F(FileUtilTest, FileAndDirectorySize) {
444 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
445 // should return 53 bytes.
446 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
447 CreateTextFile(file_01, L"12345678901234567890");
448 int64 size_f1 = 0;
449 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
450 EXPECT_EQ(20ll, size_f1);
451
452 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
453 file_util::CreateDirectory(subdir_path);
454
455 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
456 CreateTextFile(file_02, L"123456789012345678901234567890");
457 int64 size_f2 = 0;
458 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
459 EXPECT_EQ(30ll, size_f2);
460
461 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
462 file_util::CreateDirectory(subsubdir_path);
463
464 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
465 CreateTextFile(file_03, L"123");
466
467 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
468 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
rvargas@google.comaa24e112010-06-12 07:53:43 +0900469
470 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("The file*"));
471 EXPECT_EQ(size_f1, computed_size);
472
473 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("bla*"));
474 EXPECT_EQ(0, computed_size);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900475}
476
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900477TEST_F(FileUtilTest, NormalizeFilePathBasic) {
478 // Create a directory under the test dir. Because we create it,
479 // we know it is not a link.
480 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
481 FilePath dir_path = test_dir_.Append(FPL("dir"));
482 FilePath file_b_path = dir_path.Append(FPL("file_b"));
483 file_util::CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900484
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900485 FilePath normalized_file_a_path, normalized_file_b_path;
486 ASSERT_FALSE(file_util::PathExists(file_a_path));
487 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
488 &normalized_file_a_path))
489 << "NormalizeFilePath() should fail on nonexistant paths.";
490
491 CreateTextFile(file_a_path, bogus_content);
492 ASSERT_TRUE(file_util::PathExists(file_a_path));
493 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
494 &normalized_file_a_path));
495
496 CreateTextFile(file_b_path, bogus_content);
497 ASSERT_TRUE(file_util::PathExists(file_b_path));
498 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
499 &normalized_file_b_path));
500
501 // Beacuse this test created |dir_path|, we know it is not a link
502 // or junction. So, the real path of the directory holding file a
503 // must be the parent of the path holding file b.
504 ASSERT_TRUE(normalized_file_a_path.DirName()
505 .IsParent(normalized_file_b_path.DirName()));
506}
507
508#if defined(OS_WIN)
509
510TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
511 // Build the following directory structure:
512 //
513 // test_dir_
514 // |-> base_a
515 // | |-> sub_a
516 // | |-> file.txt
517 // | |-> long_name___... (Very long name.)
518 // | |-> sub_long
519 // | |-> deep.txt
520 // |-> base_b
521 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
522 // |-> to_base_b (reparse point to test_dir_\base_b)
523 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
524
525 FilePath base_a = test_dir_.Append(FPL("base_a"));
526 ASSERT_TRUE(file_util::CreateDirectory(base_a));
527
528 FilePath sub_a = base_a.Append(FPL("sub_a"));
529 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
530
531 FilePath file_txt = sub_a.Append(FPL("file.txt"));
532 CreateTextFile(file_txt, bogus_content);
533
534 // Want a directory whose name is long enough to make the path to the file
535 // inside just under MAX_PATH chars. This will be used to test that when
536 // a junction expands to a path over MAX_PATH chars in length,
537 // NormalizeFilePath() fails without crashing.
538 FilePath sub_long_rel(FPL("sub_long"));
539 FilePath deep_txt(FPL("deep.txt"));
540
541 int target_length = MAX_PATH;
542 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
543 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900544 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900545 // the resulting path is still long enough to hit the failing case in
546 // NormalizePath().
547 const int kCreateDirLimit = 4;
548 target_length -= kCreateDirLimit;
549 FilePath::StringType long_name_str = FPL("long_name_");
550 long_name_str.resize(target_length, '_');
551
552 FilePath long_name = sub_a.Append(FilePath(long_name_str));
553 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
554 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
555
556 FilePath sub_long = deep_file.DirName();
557 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
558 CreateTextFile(deep_file, bogus_content);
559
560 FilePath base_b = test_dir_.Append(FPL("base_b"));
561 ASSERT_TRUE(file_util::CreateDirectory(base_b));
562
563 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
564 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
565 ScopedHandle reparse_to_sub_a(
566 ::CreateFile(to_sub_a.value().c_str(),
567 FILE_ALL_ACCESS,
568 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
569 NULL,
570 OPEN_EXISTING,
571 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
572 NULL));
573 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_a.Get());
574 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
575
576 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
577 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
578 ScopedHandle reparse_to_base_b(
579 ::CreateFile(to_base_b.value().c_str(),
580 FILE_ALL_ACCESS,
581 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
582 NULL,
583 OPEN_EXISTING,
584 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
585 NULL));
586 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_base_b.Get());
587 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
588
589 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
590 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
591 ScopedHandle reparse_to_sub_long(
592 ::CreateFile(to_sub_long.value().c_str(),
593 FILE_ALL_ACCESS,
594 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
595 NULL,
596 OPEN_EXISTING,
597 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
598 NULL));
599 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_long.Get());
600 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
601
602 // Normalize a junction free path: base_a\sub_a\file.txt .
603 FilePath normalized_path;
604 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
605 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
606
607 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
608 // the junction to_sub_a.
609 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
610 &normalized_path));
611 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
612
613 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
614 // normalized to exclude junctions to_base_b and to_sub_a .
615 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
616 .Append(FPL("to_base_b"))
617 .Append(FPL("to_sub_a"))
618 .Append(FPL("file.txt")),
619 &normalized_path));
620 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
621
622 // A long enough path will cause NormalizeFilePath() to fail. Make a long
623 // path using to_base_b many times, and check that paths long enough to fail
624 // do not cause a crash.
625 FilePath long_path = base_b;
626 const int kLengthLimit = MAX_PATH + 200;
627 while (long_path.value().length() <= kLengthLimit) {
628 long_path = long_path.Append(FPL("to_base_b"));
629 }
630 long_path = long_path.Append(FPL("to_sub_a"))
631 .Append(FPL("file.txt"));
632
633 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
634
635 // Normalizing the junction to deep.txt should fail, because the expanded
636 // path to deep.txt is longer than MAX_PATH.
637 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
638 &normalized_path));
639
640 // Delete the reparse points, and see that NormalizeFilePath() fails
641 // to traverse them.
642 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
643 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
644 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
645
646 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
647 &normalized_path));
648}
649
650#endif // defined(OS_WIN)
651
652// The following test of NormalizeFilePath() require that we create a symlink.
653// This can not be done on windows before vista. On vista, creating a symlink
654// requires privilege "SeCreateSymbolicLinkPrivilege".
655// TODO(skerner): Investigate the possibility of giving base_unittests the
656// privileges required to create a symlink.
657#if defined(OS_POSIX)
658
659bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
660 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
661}
662
663TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
664 FilePath normalized_path;
skerner@chromium.org559baa92010-05-13 00:13:57 +0900665
666 // Link one file to another.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900667 FilePath link_from = test_dir_.Append(FPL("from_file"));
668 FilePath link_to = test_dir_.Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900669 CreateTextFile(link_to, bogus_content);
670
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900671 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900672 << "Failed to create file symlink.";
673
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900674 // Check that NormalizeFilePath sees the link.
675 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900676 ASSERT_TRUE(link_to != link_from);
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900677 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
678 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900679
680 // Link to a directory.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900681 link_from = test_dir_.Append(FPL("from_dir"));
682 link_to = test_dir_.Append(FPL("to_dir"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900683 file_util::CreateDirectory(link_to);
684
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900685 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900686 << "Failed to create directory symlink.";
687
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900688 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
689 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900690
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900691 // Test that a loop in the links causes NormalizeFilePath() to return false.
692 link_from = test_dir_.Append(FPL("link_a"));
693 link_to = test_dir_.Append(FPL("link_b"));
694 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900695 << "Failed to create loop symlink a.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900696 ASSERT_TRUE(MakeSymlink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900697 << "Failed to create loop symlink b.";
698
699 // Infinite loop!
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900700 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900701}
702#endif // defined(OS_POSIX)
703
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900704TEST_F(FileUtilTest, DeleteNonExistent) {
705 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
706 ASSERT_FALSE(file_util::PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900707
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900708 EXPECT_TRUE(file_util::Delete(non_existent, false));
709 ASSERT_FALSE(file_util::PathExists(non_existent));
710 EXPECT_TRUE(file_util::Delete(non_existent, true));
711 ASSERT_FALSE(file_util::PathExists(non_existent));
712}
713
714TEST_F(FileUtilTest, DeleteFile) {
715 // Create a file
716 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
717 CreateTextFile(file_name, bogus_content);
initial.commit3f4a7322008-07-27 06:49:38 +0900718 ASSERT_TRUE(file_util::PathExists(file_name));
719
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900720 // Make sure it's deleted
721 EXPECT_TRUE(file_util::Delete(file_name, false));
722 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900723
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900724 // Test recursive case, create a new file
725 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
726 CreateTextFile(file_name, bogus_content);
727 ASSERT_TRUE(file_util::PathExists(file_name));
728
729 // Make sure it's deleted
730 EXPECT_TRUE(file_util::Delete(file_name, true));
731 EXPECT_FALSE(file_util::PathExists(file_name));
732}
733
734#if defined(OS_WIN)
735// Tests that the Delete function works for wild cards, especially
736// with the recursion flag. Also coincidentally tests PathExists.
737// TODO(erikkay): see if anyone's actually using this feature of the API
738TEST_F(FileUtilTest, DeleteWildCard) {
739 // Create a file and a directory
740 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
741 CreateTextFile(file_name, bogus_content);
742 ASSERT_TRUE(file_util::PathExists(file_name));
743
744 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
745 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900746 ASSERT_TRUE(file_util::PathExists(subdir_path));
747
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900748 // Create the wildcard path
evanm@google.com874d1672008-10-31 08:54:04 +0900749 FilePath directory_contents = test_dir_;
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900750 directory_contents = directory_contents.Append(FPL("*"));
751
initial.commit3f4a7322008-07-27 06:49:38 +0900752 // Delete non-recursively and check that only the file is deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900753 EXPECT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900754 EXPECT_FALSE(file_util::PathExists(file_name));
755 EXPECT_TRUE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900756
zork@chromium.org61be4f42010-05-07 09:05:36 +0900757 // Delete recursively and make sure all contents are deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900758 EXPECT_TRUE(file_util::Delete(directory_contents, true));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900759 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900760 EXPECT_FALSE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900761}
762
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900763// TODO(erikkay): see if anyone's actually using this feature of the API
764TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
765 // Create a file and a directory
766 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
767 file_util::CreateDirectory(subdir_path);
768 ASSERT_TRUE(file_util::PathExists(subdir_path));
769
770 // Create the wildcard path
771 FilePath directory_contents = subdir_path;
772 directory_contents = directory_contents.Append(FPL("*"));
773
774 // Delete non-recursively and check nothing got deleted
775 EXPECT_TRUE(file_util::Delete(directory_contents, false));
776 EXPECT_TRUE(file_util::PathExists(subdir_path));
777
778 // Delete recursively and check nothing got deleted
779 EXPECT_TRUE(file_util::Delete(directory_contents, true));
780 EXPECT_TRUE(file_util::PathExists(subdir_path));
781}
782#endif
783
784// Tests non-recursive Delete() for a directory.
785TEST_F(FileUtilTest, DeleteDirNonRecursive) {
786 // Create a subdirectory and put a file and two directories inside.
787 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
788 file_util::CreateDirectory(test_subdir);
789 ASSERT_TRUE(file_util::PathExists(test_subdir));
790
791 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
792 CreateTextFile(file_name, bogus_content);
793 ASSERT_TRUE(file_util::PathExists(file_name));
794
795 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
796 file_util::CreateDirectory(subdir_path1);
797 ASSERT_TRUE(file_util::PathExists(subdir_path1));
798
799 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
800 file_util::CreateDirectory(subdir_path2);
801 ASSERT_TRUE(file_util::PathExists(subdir_path2));
802
803 // Delete non-recursively and check that the empty dir got deleted
804 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
805 EXPECT_FALSE(file_util::PathExists(subdir_path2));
806
807 // Delete non-recursively and check that nothing got deleted
808 EXPECT_FALSE(file_util::Delete(test_subdir, false));
809 EXPECT_TRUE(file_util::PathExists(test_subdir));
810 EXPECT_TRUE(file_util::PathExists(file_name));
811 EXPECT_TRUE(file_util::PathExists(subdir_path1));
812}
813
814// Tests recursive Delete() for a directory.
815TEST_F(FileUtilTest, DeleteDirRecursive) {
816 // Create a subdirectory and put a file and two directories inside.
817 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
818 file_util::CreateDirectory(test_subdir);
819 ASSERT_TRUE(file_util::PathExists(test_subdir));
820
821 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
822 CreateTextFile(file_name, bogus_content);
823 ASSERT_TRUE(file_util::PathExists(file_name));
824
825 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
826 file_util::CreateDirectory(subdir_path1);
827 ASSERT_TRUE(file_util::PathExists(subdir_path1));
828
829 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
830 file_util::CreateDirectory(subdir_path2);
831 ASSERT_TRUE(file_util::PathExists(subdir_path2));
832
833 // Delete recursively and check that the empty dir got deleted
834 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
835 EXPECT_FALSE(file_util::PathExists(subdir_path2));
836
837 // Delete recursively and check that everything got deleted
838 EXPECT_TRUE(file_util::Delete(test_subdir, true));
839 EXPECT_FALSE(file_util::PathExists(file_name));
840 EXPECT_FALSE(file_util::PathExists(subdir_path1));
841 EXPECT_FALSE(file_util::PathExists(test_subdir));
842}
843
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900844TEST_F(FileUtilTest, MoveFileNew) {
845 // Create a file
846 FilePath file_name_from =
847 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
848 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
849 ASSERT_TRUE(file_util::PathExists(file_name_from));
850
851 // The destination
852 FilePath file_name_to =
853 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
854 ASSERT_FALSE(file_util::PathExists(file_name_to));
855
856 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
857
858 // Check everything has been moved.
859 EXPECT_FALSE(file_util::PathExists(file_name_from));
860 EXPECT_TRUE(file_util::PathExists(file_name_to));
861}
862
863TEST_F(FileUtilTest, MoveFileExists) {
864 // Create a file
865 FilePath file_name_from =
866 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
867 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
868 ASSERT_TRUE(file_util::PathExists(file_name_from));
869
870 // The destination name
871 FilePath file_name_to =
872 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
873 CreateTextFile(file_name_to, L"Old file content");
874 ASSERT_TRUE(file_util::PathExists(file_name_to));
875
876 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
877
878 // Check everything has been moved.
879 EXPECT_FALSE(file_util::PathExists(file_name_from));
880 EXPECT_TRUE(file_util::PathExists(file_name_to));
881 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
882}
883
884TEST_F(FileUtilTest, MoveFileDirExists) {
885 // Create a file
886 FilePath file_name_from =
887 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
888 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
889 ASSERT_TRUE(file_util::PathExists(file_name_from));
890
891 // The destination directory
892 FilePath dir_name_to =
893 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
894 file_util::CreateDirectory(dir_name_to);
895 ASSERT_TRUE(file_util::PathExists(dir_name_to));
896
897 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
898}
899
900
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900901TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900902 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900903 FilePath dir_name_from =
904 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
905 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900906 ASSERT_TRUE(file_util::PathExists(dir_name_from));
907
908 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900909 FilePath file_name_from =
910 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900911 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
912 ASSERT_TRUE(file_util::PathExists(file_name_from));
913
914 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900915 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
916 FilePath file_name_to =
917 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900918
919 ASSERT_FALSE(file_util::PathExists(dir_name_to));
920
921 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
922
923 // Check everything has been moved.
924 EXPECT_FALSE(file_util::PathExists(dir_name_from));
925 EXPECT_FALSE(file_util::PathExists(file_name_from));
926 EXPECT_TRUE(file_util::PathExists(dir_name_to));
927 EXPECT_TRUE(file_util::PathExists(file_name_to));
928}
929
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900930TEST_F(FileUtilTest, MoveExist) {
931 // Create a directory
932 FilePath dir_name_from =
933 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
934 file_util::CreateDirectory(dir_name_from);
935 ASSERT_TRUE(file_util::PathExists(dir_name_from));
936
937 // Create a file under the directory
938 FilePath file_name_from =
939 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
940 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
941 ASSERT_TRUE(file_util::PathExists(file_name_from));
942
943 // Move the directory
944 FilePath dir_name_exists =
945 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
946
947 FilePath dir_name_to =
948 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
949 FilePath file_name_to =
950 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
951
952 // Create the destination directory.
953 file_util::CreateDirectory(dir_name_exists);
954 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
955
956 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
957
958 // Check everything has been moved.
959 EXPECT_FALSE(file_util::PathExists(dir_name_from));
960 EXPECT_FALSE(file_util::PathExists(file_name_from));
961 EXPECT_TRUE(file_util::PathExists(dir_name_to));
962 EXPECT_TRUE(file_util::PathExists(file_name_to));
963}
964
965TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900966 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900967 FilePath dir_name_from =
968 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
969 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900970 ASSERT_TRUE(file_util::PathExists(dir_name_from));
971
972 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900973 FilePath file_name_from =
974 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900975 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
976 ASSERT_TRUE(file_util::PathExists(file_name_from));
977
978 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900979 FilePath subdir_name_from =
980 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
981 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900982 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
983
984 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900985 FilePath file_name2_from =
986 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900987 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
988 ASSERT_TRUE(file_util::PathExists(file_name2_from));
989
990 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900991 FilePath dir_name_to =
992 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
993 FilePath file_name_to =
994 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
995 FilePath subdir_name_to =
996 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
997 FilePath file_name2_to =
998 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900999
1000 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1001
1002 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
1003
1004 // Check everything has been copied.
1005 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1006 EXPECT_TRUE(file_util::PathExists(file_name_from));
1007 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1008 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1009 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1010 EXPECT_TRUE(file_util::PathExists(file_name_to));
1011 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1012 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1013}
1014
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001015TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1016 // Create a directory.
1017 FilePath dir_name_from =
1018 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1019 file_util::CreateDirectory(dir_name_from);
1020 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1021
1022 // Create a file under the directory.
1023 FilePath file_name_from =
1024 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1025 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1026 ASSERT_TRUE(file_util::PathExists(file_name_from));
1027
1028 // Create a subdirectory.
1029 FilePath subdir_name_from =
1030 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1031 file_util::CreateDirectory(subdir_name_from);
1032 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1033
1034 // Create a file under the subdirectory.
1035 FilePath file_name2_from =
1036 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1037 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1038 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1039
1040 // Copy the directory recursively.
1041 FilePath dir_name_exists =
1042 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1043
1044 FilePath dir_name_to =
1045 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1046 FilePath file_name_to =
1047 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1048 FilePath subdir_name_to =
1049 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1050 FilePath file_name2_to =
1051 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1052
1053 // Create the destination directory.
1054 file_util::CreateDirectory(dir_name_exists);
1055 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1056
1057 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1058
1059 // Check everything has been copied.
1060 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1061 EXPECT_TRUE(file_util::PathExists(file_name_from));
1062 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1063 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1064 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1065 EXPECT_TRUE(file_util::PathExists(file_name_to));
1066 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1067 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1068}
1069
1070TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001071 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001072 FilePath dir_name_from =
1073 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1074 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001075 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1076
1077 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001078 FilePath file_name_from =
1079 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001080 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1081 ASSERT_TRUE(file_util::PathExists(file_name_from));
1082
1083 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001084 FilePath subdir_name_from =
1085 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1086 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001087 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1088
1089 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001090 FilePath file_name2_from =
1091 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001092 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1093 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1094
1095 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001096 FilePath dir_name_to =
1097 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1098 FilePath file_name_to =
1099 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1100 FilePath subdir_name_to =
1101 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001102
1103 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1104
1105 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1106
1107 // Check everything has been copied.
1108 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1109 EXPECT_TRUE(file_util::PathExists(file_name_from));
1110 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1111 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1112 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1113 EXPECT_TRUE(file_util::PathExists(file_name_to));
1114 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1115}
1116
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001117TEST_F(FileUtilTest, CopyDirectoryExists) {
1118 // Create a directory.
1119 FilePath dir_name_from =
1120 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1121 file_util::CreateDirectory(dir_name_from);
1122 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1123
1124 // Create a file under the directory.
1125 FilePath file_name_from =
1126 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1127 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1128 ASSERT_TRUE(file_util::PathExists(file_name_from));
1129
1130 // Create a subdirectory.
1131 FilePath subdir_name_from =
1132 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1133 file_util::CreateDirectory(subdir_name_from);
1134 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1135
1136 // Create a file under the subdirectory.
1137 FilePath file_name2_from =
1138 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1139 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1140 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1141
1142 // Copy the directory not recursively.
1143 FilePath dir_name_to =
1144 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1145 FilePath file_name_to =
1146 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1147 FilePath subdir_name_to =
1148 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1149
1150 // Create the destination directory.
1151 file_util::CreateDirectory(dir_name_to);
1152 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1153
1154 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1155
1156 // Check everything has been copied.
1157 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1158 EXPECT_TRUE(file_util::PathExists(file_name_from));
1159 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1160 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1161 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1162 EXPECT_TRUE(file_util::PathExists(file_name_to));
1163 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1164}
1165
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001166TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1167 // Create a file
1168 FilePath file_name_from =
1169 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1170 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1171 ASSERT_TRUE(file_util::PathExists(file_name_from));
1172
1173 // The destination name
1174 FilePath file_name_to =
1175 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1176 ASSERT_FALSE(file_util::PathExists(file_name_to));
1177
1178 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1179
1180 // Check the has been copied
1181 EXPECT_TRUE(file_util::PathExists(file_name_to));
1182}
1183
1184TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1185 // Create a file
1186 FilePath file_name_from =
1187 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1188 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1189 ASSERT_TRUE(file_util::PathExists(file_name_from));
1190
1191 // The destination name
1192 FilePath file_name_to =
1193 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1194 CreateTextFile(file_name_to, L"Old file content");
1195 ASSERT_TRUE(file_util::PathExists(file_name_to));
1196
1197 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1198
1199 // Check the has been copied
1200 EXPECT_TRUE(file_util::PathExists(file_name_to));
1201 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1202}
1203
1204TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1205 // Create a file
1206 FilePath file_name_from =
1207 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1208 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1209 ASSERT_TRUE(file_util::PathExists(file_name_from));
1210
1211 // The destination
1212 FilePath dir_name_to =
1213 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1214 file_util::CreateDirectory(dir_name_to);
1215 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1216 FilePath file_name_to =
1217 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1218
1219 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1220
1221 // Check the has been copied
1222 EXPECT_TRUE(file_util::PathExists(file_name_to));
1223}
1224
initial.commit3f4a7322008-07-27 06:49:38 +09001225TEST_F(FileUtilTest, CopyFile) {
1226 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001227 FilePath dir_name_from =
1228 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1229 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001230 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1231
1232 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001233 FilePath file_name_from =
1234 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001235 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1236 CreateTextFile(file_name_from, file_contents);
1237 ASSERT_TRUE(file_util::PathExists(file_name_from));
1238
1239 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001240 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001241 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001242
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001243 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001244 FilePath dest_file2(dir_name_from);
1245 dest_file2 = dest_file2.AppendASCII("..");
1246 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1247 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1248
1249 FilePath dest_file2_test(dir_name_from);
1250 dest_file2_test = dest_file2_test.DirName();
1251 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001252
1253 // Check everything has been copied.
1254 EXPECT_TRUE(file_util::PathExists(file_name_from));
1255 EXPECT_TRUE(file_util::PathExists(dest_file));
1256 const std::wstring read_contents = ReadTextFile(dest_file);
1257 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +09001258 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1259 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001260}
1261
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001262// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +09001263#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001264TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +09001265 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001266
1267 SYSTEMTIME start_time;
1268 GetLocalTime(&start_time);
1269 Sleep(100);
1270 CreateTextFile(file_name, L"New file!");
1271 Sleep(100);
1272 SYSTEMTIME end_time;
1273 GetLocalTime(&end_time);
1274
1275 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +09001276 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +09001277
1278 FILETIME start_filetime;
1279 SystemTimeToFileTime(&start_time, &start_filetime);
1280 FILETIME end_filetime;
1281 SystemTimeToFileTime(&end_time, &end_filetime);
1282 FILETIME file_creation_filetime;
1283 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1284
1285 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1286 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1287 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1288
1289 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1290 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1291 "end time: " << FileTimeAsUint64(end_filetime);
1292
evanm@google.com874d1672008-10-31 08:54:04 +09001293 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +09001294}
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001295#endif
initial.commit3f4a7322008-07-27 06:49:38 +09001296
erikkay@google.comf2406842008-08-21 00:59:49 +09001297// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001298// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001299typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001300
erikkay@google.comf2406842008-08-21 00:59:49 +09001301TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001302 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +09001303 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +09001304 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1305 .Append(FILE_PATH_LITERAL("data"))
1306 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +09001307 ASSERT_TRUE(file_util::PathExists(data_dir));
1308
evanm@google.com874d1672008-10-31 08:54:04 +09001309 FilePath original_file =
1310 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1311 FilePath same_file =
1312 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1313 FilePath same_length_file =
1314 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1315 FilePath different_file =
1316 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1317 FilePath different_first_file =
1318 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1319 FilePath different_last_file =
1320 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1321 FilePath empty1_file =
1322 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1323 FilePath empty2_file =
1324 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1325 FilePath shortened_file =
1326 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1327 FilePath binary_file =
1328 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1329 FilePath binary_file_same =
1330 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1331 FilePath binary_file_diff =
1332 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001333
1334 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1335 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1336 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1337 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +09001338 EXPECT_FALSE(file_util::ContentsEqual(
1339 FilePath(FILE_PATH_LITERAL("bogusname")),
1340 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +09001341 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1342 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1343 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1344 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1345 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1346 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1347 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1348}
1349
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001350TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1351 FilePath data_dir;
1352 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1353 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1354 .Append(FILE_PATH_LITERAL("data"))
1355 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1356 ASSERT_TRUE(file_util::PathExists(data_dir));
1357
1358 FilePath original_file =
1359 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1360 FilePath same_file =
1361 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1362 FilePath crlf_file =
1363 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1364 FilePath shortened_file =
1365 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1366 FilePath different_file =
1367 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1368 FilePath different_first_file =
1369 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1370 FilePath different_last_file =
1371 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1372 FilePath first1_file =
1373 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1374 FilePath first2_file =
1375 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1376 FilePath empty1_file =
1377 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1378 FilePath empty2_file =
1379 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1380 FilePath blank_line_file =
1381 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1382 FilePath blank_line_crlf_file =
1383 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1384
1385 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1386 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1387 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1388 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1389 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1390 different_first_file));
1391 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1392 different_last_file));
1393 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1394 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1395 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1396 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1397 blank_line_crlf_file));
1398}
1399
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001400// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001401#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001402TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001403 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001404 CreateTextFile(target_file, L"This is the target.");
1405
evanm@google.com874d1672008-10-31 08:54:04 +09001406 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001407
1408 HRESULT result;
1409 IShellLink *shell = NULL;
1410 IPersistFile *persist = NULL;
1411
1412 CoInitialize(NULL);
1413 // Temporarily create a shortcut for test
1414 result = CoCreateInstance(CLSID_ShellLink, NULL,
1415 CLSCTX_INPROC_SERVER, IID_IShellLink,
1416 reinterpret_cast<LPVOID*>(&shell));
1417 EXPECT_TRUE(SUCCEEDED(result));
1418 result = shell->QueryInterface(IID_IPersistFile,
1419 reinterpret_cast<LPVOID*>(&persist));
1420 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001421 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001422 EXPECT_TRUE(SUCCEEDED(result));
1423 result = shell->SetDescription(L"ResolveShortcutTest");
1424 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001425 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001426 EXPECT_TRUE(SUCCEEDED(result));
1427 if (persist)
1428 persist->Release();
1429 if (shell)
1430 shell->Release();
1431
1432 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001433 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001434 EXPECT_TRUE(is_solved);
1435 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001436 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001437 EXPECT_EQ(L"This is the target.", contents);
1438
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001439 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001440 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001441 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001442 CoUninitialize();
1443}
1444
1445TEST_F(FileUtilTest, CreateShortcutTest) {
1446 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001447 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001448 CreateTextFile(target_file, file_contents);
1449
evanm@google.com874d1672008-10-31 08:54:04 +09001450 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001451
1452 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001453 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1454 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001455 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001456 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001457 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001458 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001459 EXPECT_EQ(file_contents, read_contents);
1460
evanm@google.com874d1672008-10-31 08:54:04 +09001461 DeleteFile(target_file.value().c_str());
1462 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001463 CoUninitialize();
1464}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001465
1466TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1467 // Create a directory
1468 FilePath dir_name_from =
1469 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1470 file_util::CreateDirectory(dir_name_from);
1471 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1472
1473 // Create a file under the directory
1474 FilePath file_name_from =
1475 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1476 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1477 ASSERT_TRUE(file_util::PathExists(file_name_from));
1478
1479 // Move the directory by using CopyAndDeleteDirectory
1480 FilePath dir_name_to = test_dir_.Append(
1481 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1482 FilePath file_name_to =
1483 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1484
1485 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1486
1487 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1488
1489 // Check everything has been moved.
1490 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1491 EXPECT_FALSE(file_util::PathExists(file_name_from));
1492 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1493 EXPECT_TRUE(file_util::PathExists(file_name_to));
1494}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001495
1496TEST_F(FileUtilTest, GetTempDirTest) {
1497 static const TCHAR* kTmpKey = _T("TMP");
1498 static const TCHAR* kTmpValues[] = {
1499 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1500 };
1501 // Save the original $TMP.
1502 size_t original_tmp_size;
1503 TCHAR* original_tmp;
1504 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1505 // original_tmp may be NULL.
1506
1507 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1508 FilePath path;
1509 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1510 file_util::GetTempDir(&path);
1511 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1512 " result=" << path.value();
1513 }
1514
1515 // Restore the original $TMP.
1516 if (original_tmp) {
1517 ::_tputenv_s(kTmpKey, original_tmp);
1518 free(original_tmp);
1519 } else {
1520 ::_tputenv_s(kTmpKey, _T(""));
1521 }
1522}
1523#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001524
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001525TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1526 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001527 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001528 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001529 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1530 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1531 }
1532 for (int i = 0; i < 3; i++)
1533 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1534 for (int i = 0; i < 3; i++)
1535 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1536}
1537
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001538TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001539 FilePath names[3];
1540 FILE *fps[3];
1541 int i;
1542
1543 // Create; make sure they are open and exist.
1544 for (i = 0; i < 3; ++i) {
1545 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1546 ASSERT_TRUE(fps[i]);
1547 EXPECT_TRUE(file_util::PathExists(names[i]));
1548 }
1549
1550 // Make sure all names are unique.
1551 for (i = 0; i < 3; ++i) {
1552 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1553 }
1554
1555 // Close and delete.
1556 for (i = 0; i < 3; ++i) {
1557 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1558 EXPECT_TRUE(file_util::Delete(names[i], false));
1559 }
initial.commit3f4a7322008-07-27 06:49:38 +09001560}
1561
1562TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001563 FilePath temp_dir;
1564 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1565 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001566 EXPECT_TRUE(file_util::PathExists(temp_dir));
1567 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001568}
1569
skerner@chromium.orge4432392010-05-01 02:00:09 +09001570TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1571 FilePath new_dir;
1572 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1573 test_dir_,
1574 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.org2c12d672010-07-02 05:24:09 +09001575 false,
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001576 &new_dir));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001577 EXPECT_TRUE(file_util::PathExists(new_dir));
1578 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1579 EXPECT_TRUE(file_util::Delete(new_dir, false));
1580}
1581
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001582TEST_F(FileUtilTest, GetShmemTempDirTest) {
1583 FilePath dir;
1584 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1585 EXPECT_TRUE(file_util::DirectoryExists(dir));
1586}
1587
initial.commit3f4a7322008-07-27 06:49:38 +09001588TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001589 FilePath test_root =
1590 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001591#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001592 FilePath test_path =
1593 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001594#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001595 FilePath test_path =
1596 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001597#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001598
1599 EXPECT_FALSE(file_util::PathExists(test_path));
1600 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1601 EXPECT_TRUE(file_util::PathExists(test_path));
1602 // CreateDirectory returns true if the DirectoryExists returns true.
1603 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1604
1605 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001606 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001607 EXPECT_FALSE(file_util::PathExists(test_path));
1608 CreateTextFile(test_path, L"test file");
1609 EXPECT_TRUE(file_util::PathExists(test_path));
1610 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1611
1612 EXPECT_TRUE(file_util::Delete(test_root, true));
1613 EXPECT_FALSE(file_util::PathExists(test_root));
1614 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001615
1616 // Verify assumptions made by the Windows implementation:
1617 // 1. The current directory always exists.
1618 // 2. The root directory always exists.
1619 ASSERT_TRUE(file_util::DirectoryExists(
1620 FilePath(FilePath::kCurrentDirectory)));
1621 FilePath top_level = test_root;
1622 while (top_level != top_level.DirName()) {
1623 top_level = top_level.DirName();
1624 }
1625 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1626
1627 // Given these assumptions hold, it should be safe to
1628 // test that "creating" these directories succeeds.
1629 EXPECT_TRUE(file_util::CreateDirectory(
1630 FilePath(FilePath::kCurrentDirectory)));
1631 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001632
1633#if defined(OS_WIN)
1634 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1635 FilePath invalid_path =
1636 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1637 if (!file_util::PathExists(invalid_drive)) {
1638 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1639 }
1640#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001641}
1642
1643TEST_F(FileUtilTest, DetectDirectoryTest) {
1644 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001645 FilePath test_root =
1646 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001647 EXPECT_FALSE(file_util::PathExists(test_root));
1648 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1649 EXPECT_TRUE(file_util::PathExists(test_root));
1650 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1651
1652 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001653 FilePath test_path =
1654 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001655 EXPECT_FALSE(file_util::PathExists(test_path));
1656 CreateTextFile(test_path, L"test file");
1657 EXPECT_TRUE(file_util::PathExists(test_path));
1658 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1659 EXPECT_TRUE(file_util::Delete(test_path, false));
1660
1661 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001662}
1663
initial.commit3f4a7322008-07-27 06:49:38 +09001664TEST_F(FileUtilTest, FileEnumeratorTest) {
1665 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001666 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001667 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1668 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001669
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001670 // Test an empty directory, non-recursively, including "..".
1671 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1672 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1673 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1674 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1675 f0_dotdot.Next().value());
1676 EXPECT_EQ(FILE_PATH_LITERAL(""),
1677 f0_dotdot.Next().value());
1678
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001679 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001680 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001681 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001682 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001683 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001684 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001685 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001686
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001687 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001688 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001689 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001690 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001691 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001692 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001693 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001694 FilePath file2_rel =
1695 dir2.Append(FilePath::kParentDirectory)
1696 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001697 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001698 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001699
1700 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001701 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001702 file_util::FileEnumerator::FILES);
1703 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001704 EXPECT_TRUE(c1.HasFile(file1));
1705 EXPECT_TRUE(c1.HasFile(file2_abs));
1706 EXPECT_TRUE(c1.HasFile(dir2file));
1707 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1708 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001709
1710 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001711 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001712 file_util::FileEnumerator::DIRECTORIES);
1713 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001714 EXPECT_TRUE(c2.HasFile(dir1));
1715 EXPECT_TRUE(c2.HasFile(dir2));
1716 EXPECT_TRUE(c2.HasFile(dir2inner));
1717 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001718
tim@chromium.org989d0972008-10-16 11:42:45 +09001719 // Only enumerate directories non-recursively.
1720 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001721 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001722 FindResultCollector c2_non_recursive(f2_non_recursive);
1723 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1724 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1725 EXPECT_EQ(c2_non_recursive.size(), 2);
1726
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001727 // Only enumerate directories, non-recursively, including "..".
1728 file_util::FileEnumerator f2_dotdot(
1729 test_dir_, false,
1730 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1731 file_util::FileEnumerator::DIRECTORIES |
1732 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1733 FindResultCollector c2_dotdot(f2_dotdot);
1734 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1735 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1736 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1737 EXPECT_EQ(c2_dotdot.size(), 3);
1738
initial.commit3f4a7322008-07-27 06:49:38 +09001739 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001740 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001741 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001742 EXPECT_TRUE(c3.HasFile(dir1));
1743 EXPECT_TRUE(c3.HasFile(dir2));
1744 EXPECT_TRUE(c3.HasFile(file1));
1745 EXPECT_TRUE(c3.HasFile(file2_abs));
1746 EXPECT_TRUE(c3.HasFile(dir2file));
1747 EXPECT_TRUE(c3.HasFile(dir2inner));
1748 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1749 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001750
1751 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001752 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001753 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001754 EXPECT_TRUE(c4.HasFile(dir2));
1755 EXPECT_TRUE(c4.HasFile(dir2));
1756 EXPECT_TRUE(c4.HasFile(file1));
1757 EXPECT_TRUE(c4.HasFile(file2_abs));
1758 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001759
1760 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001761 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001762 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001763 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001764 EXPECT_TRUE(c5.HasFile(dir1));
1765 EXPECT_TRUE(c5.HasFile(dir2));
1766 EXPECT_TRUE(c5.HasFile(dir2file));
1767 EXPECT_TRUE(c5.HasFile(dir2inner));
1768 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1769 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001770
1771 // Make sure the destructor closes the find handle while in the middle of a
1772 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001773 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001774 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1775 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001776}
license.botf003cfe2008-08-24 09:55:55 +09001777
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001778TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001779 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001780
1781 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001782 if (file_util::PathExists(data_dir)) {
1783 ASSERT_TRUE(file_util::Delete(data_dir, true));
1784 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001785 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1786
1787 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1788 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1789 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1790 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1791
1792 // Annoyingly, the directories must actually exist in order for realpath(),
1793 // which Contains() relies on in posix, to work.
1794 ASSERT_TRUE(file_util::CreateDirectory(foo));
1795 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001796 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1797 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1798 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001799
1800 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1801 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1802 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1803 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1804
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001805 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001806 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1807#if defined(OS_WIN)
1808 EXPECT_TRUE(file_util::ContainsPath(foo,
1809 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001810 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001811 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001812#elif defined(OS_MACOSX)
1813 // We can't really do this test on OS X since the case-sensitivity of the
1814 // filesystem is configurable.
1815#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001816 EXPECT_FALSE(file_util::ContainsPath(foo,
1817 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001818#endif
1819}
1820
jochen@chromium.orga6879772010-02-18 19:02:26 +09001821TEST_F(FileUtilTest, LastModified) {
1822 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1823
1824 // Create a fresh, empty copy of this directory.
1825 if (file_util::PathExists(data_dir)) {
1826 ASSERT_TRUE(file_util::Delete(data_dir, true));
1827 }
1828 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1829
1830 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1831 std::string data("hello");
1832 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1833
1834 base::Time modification_time;
1835 // Note that this timestamp is divisible by two (seconds) - FAT stores
1836 // modification times with 2s resolution.
1837 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1838 &modification_time));
1839 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1840 file_util::FileInfo file_info;
1841 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1842 ASSERT_TRUE(file_info.last_modified == modification_time);
1843}
1844
tfarina@chromium.org34828222010-05-26 10:40:12 +09001845TEST_F(FileUtilTest, IsDirectoryEmpty) {
1846 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1847
1848 ASSERT_FALSE(file_util::PathExists(empty_dir));
1849
1850 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1851
1852 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1853
1854 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1855 std::string bar("baz");
1856 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1857
1858 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1859}
1860
mark@chromium.org17684802008-09-10 09:16:28 +09001861} // namespace