blob: ae6560dc7b45203236baadeda032a7ec5e6a6331 [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>
initial.commit3f4a7322008-07-27 06:49:38 +09009#include <shellapi.h>
10#include <shlobj.h>
tkent@chromium.org8da14162009-10-09 16:33:39 +090011#include <tchar.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090012#endif
initial.commit3f4a7322008-07-27 06:49:38 +090013
14#include <fstream>
15#include <iostream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090016#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090017
18#include "base/base_paths.h"
evanm@google.com874d1672008-10-31 08:54:04 +090019#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090020#include "base/file_util.h"
21#include "base/logging.h"
22#include "base/path_service.h"
erikkay@google.com8d133f62009-04-24 00:05:19 +090023#include "base/platform_thread.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090024#include "base/time.h"
brettw@chromium.org50c94652009-10-07 11:10:20 +090025#include "base/utf_string_conversions.h"
initial.commit3f4a7322008-07-27 06:49:38 +090026#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090027#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090028
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090029// This macro helps avoid wrapped lines in the test structs.
30#define FPL(x) FILE_PATH_LITERAL(x)
31
initial.commit3f4a7322008-07-27 06:49:38 +090032namespace {
33
yuzo@chromium.org2da0f822009-06-09 14:57:38 +090034const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
35 static_cast<file_util::FileEnumerator::FILE_TYPE>(
36 file_util::FileEnumerator::FILES |
37 file_util::FileEnumerator::DIRECTORIES);
38
erikkay@google.comf2406842008-08-21 00:59:49 +090039// file_util winds up using autoreleased objects on the Mac, so this needs
40// to be a PlatformTest
41class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +090042 protected:
43 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +090044 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +090045 // Name a subdirectory of the temp directory.
46 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +090047 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +090048
49 // Create a fresh, empty copy of this directory.
50 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +090051 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +090052 }
53 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +090054 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +090055 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +090056 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +090057 ASSERT_FALSE(file_util::PathExists(test_dir_));
58 }
59
60 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +090061 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +090062};
63
64// Collects all the results from the given file enumerator, and provides an
65// interface to query whether a given file is present.
66class FindResultCollector {
67 public:
evan@chromium.org1543ad32009-08-27 05:00:14 +090068 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +090069 FilePath cur_file;
70 while (!(cur_file = enumerator.Next()).value().empty()) {
71 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +090072 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +090073 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +090074 << "Same file returned twice";
75
76 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +090077 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +090078 }
79 }
80
81 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +090082 bool HasFile(const FilePath& file) const {
83 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +090084 }
evanm@google.com874d1672008-10-31 08:54:04 +090085
erikkay@google.comdfb51b22008-08-16 02:32:10 +090086 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +090087 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +090088 }
initial.commit3f4a7322008-07-27 06:49:38 +090089
90 private:
evanm@google.com874d1672008-10-31 08:54:04 +090091 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +090092};
93
94// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +090095void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +090096 const std::wstring& contents) {
97 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +090098 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +090099 ASSERT_TRUE(file.is_open());
100 file << contents;
101 file.close();
102}
103
104// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900105std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900106 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900107 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900108 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900109 EXPECT_TRUE(file.is_open());
110 file.getline(contents, 64);
111 file.close();
112 return std::wstring(contents);
113}
114
erikkay@google.com014161d2008-08-16 02:45:13 +0900115#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900116uint64 FileTimeAsUint64(const FILETIME& ft) {
117 ULARGE_INTEGER u;
118 u.LowPart = ft.dwLowDateTime;
119 u.HighPart = ft.dwHighDateTime;
120 return u.QuadPart;
121}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900122#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900123
124const struct append_case {
125 const wchar_t* path;
126 const wchar_t* ending;
127 const wchar_t* result;
128} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900129#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900130 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
131 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
132 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
133 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
134 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
135 {L"", L"path", L"\\path"},
136 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900137#elif defined(OS_POSIX)
138 {L"/foo/bar", L"path", L"/foo/bar/path"},
139 {L"/foo/bar/", L"path", L"/foo/bar/path"},
140 {L"/foo/bar//", L"path", L"/foo/bar//path"},
141 {L"/foo/bar/", L"", L"/foo/bar/"},
142 {L"/foo/bar", L"", L"/foo/bar/"},
143 {L"", L"path", L"/path"},
144 {L"", L"", L"/"},
145#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900146};
147
initial.commit3f4a7322008-07-27 06:49:38 +0900148TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900149 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900150 const append_case& value = append_cases[i];
151 std::wstring result = value.path;
152 file_util::AppendToPath(&result, value.ending);
153 EXPECT_EQ(value.result, result);
154 }
155
156#ifdef NDEBUG
157 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
158#endif
159}
160
161static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900162 const FilePath::CharType* path;
163 const FilePath::CharType* suffix;
164 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900165} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900166 {FPL(""), FPL(""), FPL("")},
167 {FPL(""), FPL("txt"), FPL("txt")},
168 {FPL("."), FPL("txt"), FPL("txt.")},
169 {FPL("."), FPL(""), FPL(".")},
170 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
171 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
172 {FPL("foo"), FPL("txt"), FPL("footxt")},
173 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
174 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
175 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
176 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
177 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
178 {FPL("foo"), FPL(""), FPL("foo")},
179 {FPL("foo"), FPL("."), FPL("foo.")},
180 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
181 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900182#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900183 {FPL("\\"), FPL(""), FPL("\\")},
184 {FPL("\\"), FPL("txt"), FPL("\\txt")},
185 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
186 {FPL("\\."), FPL(""), FPL("\\.")},
187 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
188 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
189 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
190 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
191 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
192 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
193 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
194 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
195 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900196#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900197 {FPL("/"), FPL(""), FPL("/")},
198 {FPL("/"), FPL("txt"), FPL("/txt")},
199 {FPL("/."), FPL("txt"), FPL("/txt.")},
200 {FPL("/."), FPL(""), FPL("/.")},
201 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
202 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
203 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
204 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
205 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
206 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
207 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
208 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900209#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900210};
211
212TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900213 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900214 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900215 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900216 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900217 }
218}
219
220static const struct filename_case {
221 const wchar_t* path;
222 const wchar_t* filename;
223} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900224#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900225 {L"c:\\colon\\backslash", L"backslash"},
226 {L"c:\\colon\\backslash\\", L""},
227 {L"\\\\filename.exe", L"filename.exe"},
228 {L"filename.exe", L"filename.exe"},
229 {L"", L""},
230 {L"\\\\\\", L""},
231 {L"c:/colon/backslash", L"backslash"},
232 {L"c:/colon/backslash/", L""},
233 {L"//////", L""},
234 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900235#elif defined(OS_POSIX)
236 {L"/foo/bar", L"bar"},
237 {L"/foo/bar/", L""},
238 {L"/filename.exe", L"filename.exe"},
239 {L"filename.exe", L"filename.exe"},
240 {L"", L""},
241 {L"/", L""},
242#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900243};
244
245TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900246 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900247 const filename_case& value = filename_cases[i];
248 std::wstring result = file_util::GetFilenameFromPath(value.path);
249 EXPECT_EQ(value.filename, result);
250 }
251}
252
253// Test finding the file type from a path name
254static const struct extension_case {
255 const wchar_t* path;
256 const wchar_t* extension;
257} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900258#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900259 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
260 {L"C:\\colon\\backslash\\filename.", L""},
261 {L"C:\\colon\\backslash\\filename", L""},
262 {L"C:\\colon\\backslash\\", L""},
263 {L"C:\\colon\\backslash.\\", L""},
264 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900265#elif defined(OS_POSIX)
266 {L"/foo/bar/filename.extension", L"extension"},
267 {L"/foo/bar/filename.", L""},
268 {L"/foo/bar/filename", L""},
269 {L"/foo/bar/", L""},
270 {L"/foo/bar./", L""},
271 {L"/foo/bar/filename.extension.extension2", L"extension2"},
272 {L".", L""},
273 {L"..", L""},
274 {L"./foo", L""},
275 {L"./foo.extension", L"extension"},
276 {L"/foo.extension1/bar.extension2", L"extension2"},
277#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900278};
279
280TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900281 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900282 const extension_case& ext = extension_cases[i];
283 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
284 EXPECT_EQ(ext.extension, fext);
285 }
286}
287
288// Test finding the directory component of a path
289static const struct dir_case {
290 const wchar_t* full_path;
291 const wchar_t* directory;
292} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900293#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900294 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
295 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
296 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
297 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
298 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
299 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900300 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900301#elif defined(OS_POSIX)
302 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
303 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
304 {L"/foo/bar/", L"/foo/bar"},
305 {L"/foo/bar//", L"/foo/bar"},
306 {L"/foo/bar", L"/foo"},
307 {L"/foo/bar./", L"/foo/bar."},
308 {L"/", L"/"},
309 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900310 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900311#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900312};
313
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900314#if defined(OS_WIN)
315// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900316TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900317 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900318 const dir_case& dir = dir_cases[i];
319 const std::wstring parent =
320 file_util::GetDirectoryFromPath(dir.full_path);
321 EXPECT_EQ(dir.directory, parent);
322 }
323}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900324#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900325
326TEST_F(FileUtilTest, CountFilesCreatedAfter) {
327 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900328 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900329 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
330
331 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900332#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900333 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900334#elif defined(OS_POSIX)
335 // We need to wait at least one second here because the precision of
336 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900337 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900338#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900339
340 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900341 base::Time now(base::Time::NowFromSystemTime());
342 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900343
344 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900345 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900346 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
347
348 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900349 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900350
351 // Delete new file, we should see no files after cutoff now
352 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900353 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900354}
355
356// Tests that the Delete function works as expected, especially
357// the recursion flag. Also coincidentally tests PathExists.
358TEST_F(FileUtilTest, Delete) {
359 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900360 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900361 CreateTextFile(file_name, L"I'm cannon fodder.");
362
363 ASSERT_TRUE(file_util::PathExists(file_name));
364
evanm@google.com874d1672008-10-31 08:54:04 +0900365 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
366 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900367
368 ASSERT_TRUE(file_util::PathExists(subdir_path));
369
evanm@google.com874d1672008-10-31 08:54:04 +0900370 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900371#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900372 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900373 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900374 // Delete non-recursively and check that only the file is deleted
375 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900376 EXPECT_FALSE(file_util::PathExists(file_name));
377 EXPECT_TRUE(file_util::PathExists(subdir_path));
378#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900379
380 // Delete recursively and make sure all contents are deleted
381 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900382 EXPECT_FALSE(file_util::PathExists(file_name));
383 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900384}
385
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900386TEST_F(FileUtilTest, MoveFileNew) {
387 // Create a file
388 FilePath file_name_from =
389 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
390 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
391 ASSERT_TRUE(file_util::PathExists(file_name_from));
392
393 // The destination
394 FilePath file_name_to =
395 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
396 ASSERT_FALSE(file_util::PathExists(file_name_to));
397
398 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
399
400 // Check everything has been moved.
401 EXPECT_FALSE(file_util::PathExists(file_name_from));
402 EXPECT_TRUE(file_util::PathExists(file_name_to));
403}
404
405TEST_F(FileUtilTest, MoveFileExists) {
406 // Create a file
407 FilePath file_name_from =
408 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
409 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
410 ASSERT_TRUE(file_util::PathExists(file_name_from));
411
412 // The destination name
413 FilePath file_name_to =
414 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
415 CreateTextFile(file_name_to, L"Old file content");
416 ASSERT_TRUE(file_util::PathExists(file_name_to));
417
418 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
419
420 // Check everything has been moved.
421 EXPECT_FALSE(file_util::PathExists(file_name_from));
422 EXPECT_TRUE(file_util::PathExists(file_name_to));
423 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
424}
425
426TEST_F(FileUtilTest, MoveFileDirExists) {
427 // Create a file
428 FilePath file_name_from =
429 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
430 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
431 ASSERT_TRUE(file_util::PathExists(file_name_from));
432
433 // The destination directory
434 FilePath dir_name_to =
435 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
436 file_util::CreateDirectory(dir_name_to);
437 ASSERT_TRUE(file_util::PathExists(dir_name_to));
438
439 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
440}
441
442
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900443TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900444 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900445 FilePath dir_name_from =
446 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
447 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900448 ASSERT_TRUE(file_util::PathExists(dir_name_from));
449
450 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900451 FilePath file_name_from =
452 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900453 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
454 ASSERT_TRUE(file_util::PathExists(file_name_from));
455
456 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900457 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
458 FilePath file_name_to =
459 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900460
461 ASSERT_FALSE(file_util::PathExists(dir_name_to));
462
463 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
464
465 // Check everything has been moved.
466 EXPECT_FALSE(file_util::PathExists(dir_name_from));
467 EXPECT_FALSE(file_util::PathExists(file_name_from));
468 EXPECT_TRUE(file_util::PathExists(dir_name_to));
469 EXPECT_TRUE(file_util::PathExists(file_name_to));
470}
471
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900472TEST_F(FileUtilTest, MoveExist) {
473 // Create a directory
474 FilePath dir_name_from =
475 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
476 file_util::CreateDirectory(dir_name_from);
477 ASSERT_TRUE(file_util::PathExists(dir_name_from));
478
479 // Create a file under the directory
480 FilePath file_name_from =
481 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
482 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
483 ASSERT_TRUE(file_util::PathExists(file_name_from));
484
485 // Move the directory
486 FilePath dir_name_exists =
487 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
488
489 FilePath dir_name_to =
490 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
491 FilePath file_name_to =
492 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
493
494 // Create the destination directory.
495 file_util::CreateDirectory(dir_name_exists);
496 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
497
498 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
499
500 // Check everything has been moved.
501 EXPECT_FALSE(file_util::PathExists(dir_name_from));
502 EXPECT_FALSE(file_util::PathExists(file_name_from));
503 EXPECT_TRUE(file_util::PathExists(dir_name_to));
504 EXPECT_TRUE(file_util::PathExists(file_name_to));
505}
506
507TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900508 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900509 FilePath dir_name_from =
510 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
511 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900512 ASSERT_TRUE(file_util::PathExists(dir_name_from));
513
514 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900515 FilePath file_name_from =
516 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900517 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
518 ASSERT_TRUE(file_util::PathExists(file_name_from));
519
520 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900521 FilePath subdir_name_from =
522 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
523 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900524 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
525
526 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900527 FilePath file_name2_from =
528 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900529 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
530 ASSERT_TRUE(file_util::PathExists(file_name2_from));
531
532 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900533 FilePath dir_name_to =
534 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
535 FilePath file_name_to =
536 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
537 FilePath subdir_name_to =
538 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
539 FilePath file_name2_to =
540 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900541
542 ASSERT_FALSE(file_util::PathExists(dir_name_to));
543
544 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
545
546 // Check everything has been copied.
547 EXPECT_TRUE(file_util::PathExists(dir_name_from));
548 EXPECT_TRUE(file_util::PathExists(file_name_from));
549 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
550 EXPECT_TRUE(file_util::PathExists(file_name2_from));
551 EXPECT_TRUE(file_util::PathExists(dir_name_to));
552 EXPECT_TRUE(file_util::PathExists(file_name_to));
553 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
554 EXPECT_TRUE(file_util::PathExists(file_name2_to));
555}
556
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900557TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
558 // Create a directory.
559 FilePath dir_name_from =
560 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
561 file_util::CreateDirectory(dir_name_from);
562 ASSERT_TRUE(file_util::PathExists(dir_name_from));
563
564 // Create a file under the directory.
565 FilePath file_name_from =
566 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
567 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
568 ASSERT_TRUE(file_util::PathExists(file_name_from));
569
570 // Create a subdirectory.
571 FilePath subdir_name_from =
572 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
573 file_util::CreateDirectory(subdir_name_from);
574 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
575
576 // Create a file under the subdirectory.
577 FilePath file_name2_from =
578 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
579 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
580 ASSERT_TRUE(file_util::PathExists(file_name2_from));
581
582 // Copy the directory recursively.
583 FilePath dir_name_exists =
584 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
585
586 FilePath dir_name_to =
587 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
588 FilePath file_name_to =
589 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
590 FilePath subdir_name_to =
591 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
592 FilePath file_name2_to =
593 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
594
595 // Create the destination directory.
596 file_util::CreateDirectory(dir_name_exists);
597 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
598
599 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
600
601 // Check everything has been copied.
602 EXPECT_TRUE(file_util::PathExists(dir_name_from));
603 EXPECT_TRUE(file_util::PathExists(file_name_from));
604 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
605 EXPECT_TRUE(file_util::PathExists(file_name2_from));
606 EXPECT_TRUE(file_util::PathExists(dir_name_to));
607 EXPECT_TRUE(file_util::PathExists(file_name_to));
608 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
609 EXPECT_TRUE(file_util::PathExists(file_name2_to));
610}
611
612TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900613 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900614 FilePath dir_name_from =
615 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
616 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900617 ASSERT_TRUE(file_util::PathExists(dir_name_from));
618
619 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900620 FilePath file_name_from =
621 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900622 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
623 ASSERT_TRUE(file_util::PathExists(file_name_from));
624
625 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900626 FilePath subdir_name_from =
627 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
628 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900629 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
630
631 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900632 FilePath file_name2_from =
633 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900634 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
635 ASSERT_TRUE(file_util::PathExists(file_name2_from));
636
637 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900638 FilePath dir_name_to =
639 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
640 FilePath file_name_to =
641 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
642 FilePath subdir_name_to =
643 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900644
645 ASSERT_FALSE(file_util::PathExists(dir_name_to));
646
647 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
648
649 // Check everything has been copied.
650 EXPECT_TRUE(file_util::PathExists(dir_name_from));
651 EXPECT_TRUE(file_util::PathExists(file_name_from));
652 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
653 EXPECT_TRUE(file_util::PathExists(file_name2_from));
654 EXPECT_TRUE(file_util::PathExists(dir_name_to));
655 EXPECT_TRUE(file_util::PathExists(file_name_to));
656 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
657}
658
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900659TEST_F(FileUtilTest, CopyDirectoryExists) {
660 // Create a directory.
661 FilePath dir_name_from =
662 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
663 file_util::CreateDirectory(dir_name_from);
664 ASSERT_TRUE(file_util::PathExists(dir_name_from));
665
666 // Create a file under the directory.
667 FilePath file_name_from =
668 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
669 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
670 ASSERT_TRUE(file_util::PathExists(file_name_from));
671
672 // Create a subdirectory.
673 FilePath subdir_name_from =
674 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
675 file_util::CreateDirectory(subdir_name_from);
676 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
677
678 // Create a file under the subdirectory.
679 FilePath file_name2_from =
680 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
681 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
682 ASSERT_TRUE(file_util::PathExists(file_name2_from));
683
684 // Copy the directory not recursively.
685 FilePath dir_name_to =
686 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
687 FilePath file_name_to =
688 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
689 FilePath subdir_name_to =
690 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
691
692 // Create the destination directory.
693 file_util::CreateDirectory(dir_name_to);
694 ASSERT_TRUE(file_util::PathExists(dir_name_to));
695
696 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
697
698 // Check everything has been copied.
699 EXPECT_TRUE(file_util::PathExists(dir_name_from));
700 EXPECT_TRUE(file_util::PathExists(file_name_from));
701 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
702 EXPECT_TRUE(file_util::PathExists(file_name2_from));
703 EXPECT_TRUE(file_util::PathExists(dir_name_to));
704 EXPECT_TRUE(file_util::PathExists(file_name_to));
705 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
706}
707
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900708TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
709 // Create a file
710 FilePath file_name_from =
711 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
712 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
713 ASSERT_TRUE(file_util::PathExists(file_name_from));
714
715 // The destination name
716 FilePath file_name_to =
717 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
718 ASSERT_FALSE(file_util::PathExists(file_name_to));
719
720 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
721
722 // Check the has been copied
723 EXPECT_TRUE(file_util::PathExists(file_name_to));
724}
725
726TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
727 // Create a file
728 FilePath file_name_from =
729 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
730 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
731 ASSERT_TRUE(file_util::PathExists(file_name_from));
732
733 // The destination name
734 FilePath file_name_to =
735 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
736 CreateTextFile(file_name_to, L"Old file content");
737 ASSERT_TRUE(file_util::PathExists(file_name_to));
738
739 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
740
741 // Check the has been copied
742 EXPECT_TRUE(file_util::PathExists(file_name_to));
743 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
744}
745
746TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
747 // Create a file
748 FilePath file_name_from =
749 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
750 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
751 ASSERT_TRUE(file_util::PathExists(file_name_from));
752
753 // The destination
754 FilePath dir_name_to =
755 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
756 file_util::CreateDirectory(dir_name_to);
757 ASSERT_TRUE(file_util::PathExists(dir_name_to));
758 FilePath file_name_to =
759 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
760
761 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
762
763 // Check the has been copied
764 EXPECT_TRUE(file_util::PathExists(file_name_to));
765}
766
initial.commit3f4a7322008-07-27 06:49:38 +0900767TEST_F(FileUtilTest, CopyFile) {
768 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900769 FilePath dir_name_from =
770 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
771 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900772 ASSERT_TRUE(file_util::PathExists(dir_name_from));
773
774 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900775 FilePath file_name_from =
776 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900777 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
778 CreateTextFile(file_name_from, file_contents);
779 ASSERT_TRUE(file_util::PathExists(file_name_from));
780
781 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900782 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900783 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900784
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900785 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +0900786 FilePath dest_file2(dir_name_from);
787 dest_file2 = dest_file2.AppendASCII("..");
788 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
789 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
790
791 FilePath dest_file2_test(dir_name_from);
792 dest_file2_test = dest_file2_test.DirName();
793 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900794
795 // Check everything has been copied.
796 EXPECT_TRUE(file_util::PathExists(file_name_from));
797 EXPECT_TRUE(file_util::PathExists(dest_file));
798 const std::wstring read_contents = ReadTextFile(dest_file);
799 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +0900800 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
801 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +0900802}
803
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900804// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900805#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900806TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900807 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900808
809 SYSTEMTIME start_time;
810 GetLocalTime(&start_time);
811 Sleep(100);
812 CreateTextFile(file_name, L"New file!");
813 Sleep(100);
814 SYSTEMTIME end_time;
815 GetLocalTime(&end_time);
816
817 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900818 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900819
820 FILETIME start_filetime;
821 SystemTimeToFileTime(&start_time, &start_filetime);
822 FILETIME end_filetime;
823 SystemTimeToFileTime(&end_time, &end_filetime);
824 FILETIME file_creation_filetime;
825 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
826
827 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
828 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
829 "creation time: " << FileTimeAsUint64(file_creation_filetime);
830
831 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
832 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
833 "end time: " << FileTimeAsUint64(end_filetime);
834
evanm@google.com874d1672008-10-31 08:54:04 +0900835 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900836}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900837#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900838
erikkay@google.comf2406842008-08-21 00:59:49 +0900839// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900840// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900841typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900842
erikkay@google.comf2406842008-08-21 00:59:49 +0900843TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900844 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900845 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900846 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
847 .Append(FILE_PATH_LITERAL("data"))
848 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900849 ASSERT_TRUE(file_util::PathExists(data_dir));
850
evanm@google.com874d1672008-10-31 08:54:04 +0900851 FilePath original_file =
852 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
853 FilePath same_file =
854 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
855 FilePath same_length_file =
856 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
857 FilePath different_file =
858 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
859 FilePath different_first_file =
860 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
861 FilePath different_last_file =
862 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
863 FilePath empty1_file =
864 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
865 FilePath empty2_file =
866 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
867 FilePath shortened_file =
868 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
869 FilePath binary_file =
870 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
871 FilePath binary_file_same =
872 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
873 FilePath binary_file_diff =
874 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900875
876 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
877 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
878 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
879 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +0900880 EXPECT_FALSE(file_util::ContentsEqual(
881 FilePath(FILE_PATH_LITERAL("bogusname")),
882 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +0900883 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
884 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
885 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
886 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
887 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
888 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
889 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
890}
891
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900892TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
893 FilePath data_dir;
894 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
895 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
896 .Append(FILE_PATH_LITERAL("data"))
897 .Append(FILE_PATH_LITERAL("file_util_unittest"));
898 ASSERT_TRUE(file_util::PathExists(data_dir));
899
900 FilePath original_file =
901 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
902 FilePath same_file =
903 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
904 FilePath crlf_file =
905 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
906 FilePath shortened_file =
907 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
908 FilePath different_file =
909 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
910 FilePath different_first_file =
911 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
912 FilePath different_last_file =
913 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
914 FilePath first1_file =
915 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
916 FilePath first2_file =
917 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
918 FilePath empty1_file =
919 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
920 FilePath empty2_file =
921 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
922 FilePath blank_line_file =
923 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
924 FilePath blank_line_crlf_file =
925 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
926
927 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
928 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
929 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
930 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
931 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
932 different_first_file));
933 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
934 different_last_file));
935 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
936 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
937 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
938 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
939 blank_line_crlf_file));
940}
941
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900942// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900943#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900944TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900945 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900946 CreateTextFile(target_file, L"This is the target.");
947
evanm@google.com874d1672008-10-31 08:54:04 +0900948 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900949
950 HRESULT result;
951 IShellLink *shell = NULL;
952 IPersistFile *persist = NULL;
953
954 CoInitialize(NULL);
955 // Temporarily create a shortcut for test
956 result = CoCreateInstance(CLSID_ShellLink, NULL,
957 CLSCTX_INPROC_SERVER, IID_IShellLink,
958 reinterpret_cast<LPVOID*>(&shell));
959 EXPECT_TRUE(SUCCEEDED(result));
960 result = shell->QueryInterface(IID_IPersistFile,
961 reinterpret_cast<LPVOID*>(&persist));
962 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900963 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900964 EXPECT_TRUE(SUCCEEDED(result));
965 result = shell->SetDescription(L"ResolveShortcutTest");
966 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900967 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900968 EXPECT_TRUE(SUCCEEDED(result));
969 if (persist)
970 persist->Release();
971 if (shell)
972 shell->Release();
973
974 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +0900975 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +0900976 EXPECT_TRUE(is_solved);
977 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +0900978 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +0900979 EXPECT_EQ(L"This is the target.", contents);
980
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900981 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900982 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +0900983 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900984 CoUninitialize();
985}
986
987TEST_F(FileUtilTest, CreateShortcutTest) {
988 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900989 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900990 CreateTextFile(target_file, file_contents);
991
evanm@google.com874d1672008-10-31 08:54:04 +0900992 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900993
994 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900995 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
996 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +0900997 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +0900998 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +0900999 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001000 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001001 EXPECT_EQ(file_contents, read_contents);
1002
evanm@google.com874d1672008-10-31 08:54:04 +09001003 DeleteFile(target_file.value().c_str());
1004 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001005 CoUninitialize();
1006}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001007
1008TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1009 // Create a directory
1010 FilePath dir_name_from =
1011 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1012 file_util::CreateDirectory(dir_name_from);
1013 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1014
1015 // Create a file under the directory
1016 FilePath file_name_from =
1017 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1018 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1019 ASSERT_TRUE(file_util::PathExists(file_name_from));
1020
1021 // Move the directory by using CopyAndDeleteDirectory
1022 FilePath dir_name_to = test_dir_.Append(
1023 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1024 FilePath file_name_to =
1025 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1026
1027 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1028
1029 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1030
1031 // Check everything has been moved.
1032 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1033 EXPECT_FALSE(file_util::PathExists(file_name_from));
1034 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1035 EXPECT_TRUE(file_util::PathExists(file_name_to));
1036}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001037
1038TEST_F(FileUtilTest, GetTempDirTest) {
1039 static const TCHAR* kTmpKey = _T("TMP");
1040 static const TCHAR* kTmpValues[] = {
1041 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1042 };
1043 // Save the original $TMP.
1044 size_t original_tmp_size;
1045 TCHAR* original_tmp;
1046 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1047 // original_tmp may be NULL.
1048
1049 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1050 FilePath path;
1051 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1052 file_util::GetTempDir(&path);
1053 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1054 " result=" << path.value();
1055 }
1056
1057 // Restore the original $TMP.
1058 if (original_tmp) {
1059 ::_tputenv_s(kTmpKey, original_tmp);
1060 free(original_tmp);
1061 } else {
1062 ::_tputenv_s(kTmpKey, _T(""));
1063 }
1064}
1065#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001066
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001067TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1068 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001069 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001070 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001071 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1072 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1073 }
1074 for (int i = 0; i < 3; i++)
1075 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1076 for (int i = 0; i < 3; i++)
1077 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1078}
1079
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001080TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001081 FilePath names[3];
1082 FILE *fps[3];
1083 int i;
1084
1085 // Create; make sure they are open and exist.
1086 for (i = 0; i < 3; ++i) {
1087 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1088 ASSERT_TRUE(fps[i]);
1089 EXPECT_TRUE(file_util::PathExists(names[i]));
1090 }
1091
1092 // Make sure all names are unique.
1093 for (i = 0; i < 3; ++i) {
1094 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1095 }
1096
1097 // Close and delete.
1098 for (i = 0; i < 3; ++i) {
1099 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1100 EXPECT_TRUE(file_util::Delete(names[i], false));
1101 }
initial.commit3f4a7322008-07-27 06:49:38 +09001102}
1103
1104TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001105 FilePath temp_dir;
1106 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1107 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001108 EXPECT_TRUE(file_util::PathExists(temp_dir));
1109 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001110}
1111
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001112TEST_F(FileUtilTest, GetShmemTempDirTest) {
1113 FilePath dir;
1114 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1115 EXPECT_TRUE(file_util::DirectoryExists(dir));
1116}
1117
initial.commit3f4a7322008-07-27 06:49:38 +09001118TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001119 FilePath test_root =
1120 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001121#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001122 FilePath test_path =
1123 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001124#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001125 FilePath test_path =
1126 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001127#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001128
1129 EXPECT_FALSE(file_util::PathExists(test_path));
1130 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1131 EXPECT_TRUE(file_util::PathExists(test_path));
1132 // CreateDirectory returns true if the DirectoryExists returns true.
1133 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1134
1135 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001136 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001137 EXPECT_FALSE(file_util::PathExists(test_path));
1138 CreateTextFile(test_path, L"test file");
1139 EXPECT_TRUE(file_util::PathExists(test_path));
1140 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1141
1142 EXPECT_TRUE(file_util::Delete(test_root, true));
1143 EXPECT_FALSE(file_util::PathExists(test_root));
1144 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001145
1146 // Verify assumptions made by the Windows implementation:
1147 // 1. The current directory always exists.
1148 // 2. The root directory always exists.
1149 ASSERT_TRUE(file_util::DirectoryExists(
1150 FilePath(FilePath::kCurrentDirectory)));
1151 FilePath top_level = test_root;
1152 while (top_level != top_level.DirName()) {
1153 top_level = top_level.DirName();
1154 }
1155 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1156
1157 // Given these assumptions hold, it should be safe to
1158 // test that "creating" these directories succeeds.
1159 EXPECT_TRUE(file_util::CreateDirectory(
1160 FilePath(FilePath::kCurrentDirectory)));
1161 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001162
1163#if defined(OS_WIN)
1164 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1165 FilePath invalid_path =
1166 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1167 if (!file_util::PathExists(invalid_drive)) {
1168 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1169 }
1170#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001171}
1172
1173TEST_F(FileUtilTest, DetectDirectoryTest) {
1174 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001175 FilePath test_root =
1176 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001177 EXPECT_FALSE(file_util::PathExists(test_root));
1178 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1179 EXPECT_TRUE(file_util::PathExists(test_root));
1180 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1181
1182 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001183 FilePath test_path =
1184 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001185 EXPECT_FALSE(file_util::PathExists(test_path));
1186 CreateTextFile(test_path, L"test file");
1187 EXPECT_TRUE(file_util::PathExists(test_path));
1188 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1189 EXPECT_TRUE(file_util::Delete(test_path, false));
1190
1191 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001192}
1193
initial.commit3f4a7322008-07-27 06:49:38 +09001194static const struct ReplaceExtensionCase {
1195 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +09001196 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +09001197 std::wstring result;
1198} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +09001199 {L"", FILE_PATH_LITERAL(""), L""},
1200 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
1201 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
1202 {L".", FILE_PATH_LITERAL(""), L""},
1203 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
1204 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
1205 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
1206 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
1207 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
1208 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
1209 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
1210 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
1211 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
1212 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
1213 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
1214 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +09001215};
1216
1217TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001218 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +09001219 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
1220 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
1221 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +09001222 }
1223}
1224
sky@google.com71e7c6f2008-09-20 02:32:18 +09001225// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
1226// the directory names of the path.
1227TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +09001228 FilePath path;
1229 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
1230 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +09001231 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +09001232 FilePath result_path = path;
1233 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001234 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
1235 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +09001236}
1237
initial.commit3f4a7322008-07-27 06:49:38 +09001238TEST_F(FileUtilTest, FileEnumeratorTest) {
1239 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001240 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001241 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1242 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001243
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001244 // Test an empty directory, non-recursively, including "..".
1245 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1246 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1247 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1248 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1249 f0_dotdot.Next().value());
1250 EXPECT_EQ(FILE_PATH_LITERAL(""),
1251 f0_dotdot.Next().value());
1252
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001253 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001254 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001255 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001256 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001257 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001258 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001259 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001260
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001261 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001262 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001263 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001264 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001265 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001266 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001267 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001268 FilePath file2_rel =
1269 dir2.Append(FilePath::kParentDirectory)
1270 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001271 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001272 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001273
1274 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001275 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001276 file_util::FileEnumerator::FILES);
1277 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001278 EXPECT_TRUE(c1.HasFile(file1));
1279 EXPECT_TRUE(c1.HasFile(file2_abs));
1280 EXPECT_TRUE(c1.HasFile(dir2file));
1281 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1282 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001283
1284 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001285 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001286 file_util::FileEnumerator::DIRECTORIES);
1287 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001288 EXPECT_TRUE(c2.HasFile(dir1));
1289 EXPECT_TRUE(c2.HasFile(dir2));
1290 EXPECT_TRUE(c2.HasFile(dir2inner));
1291 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001292
tim@chromium.org989d0972008-10-16 11:42:45 +09001293 // Only enumerate directories non-recursively.
1294 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001295 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001296 FindResultCollector c2_non_recursive(f2_non_recursive);
1297 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1298 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1299 EXPECT_EQ(c2_non_recursive.size(), 2);
1300
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001301 // Only enumerate directories, non-recursively, including "..".
1302 file_util::FileEnumerator f2_dotdot(
1303 test_dir_, false,
1304 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1305 file_util::FileEnumerator::DIRECTORIES |
1306 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1307 FindResultCollector c2_dotdot(f2_dotdot);
1308 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1309 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1310 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1311 EXPECT_EQ(c2_dotdot.size(), 3);
1312
initial.commit3f4a7322008-07-27 06:49:38 +09001313 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001314 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001315 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001316 EXPECT_TRUE(c3.HasFile(dir1));
1317 EXPECT_TRUE(c3.HasFile(dir2));
1318 EXPECT_TRUE(c3.HasFile(file1));
1319 EXPECT_TRUE(c3.HasFile(file2_abs));
1320 EXPECT_TRUE(c3.HasFile(dir2file));
1321 EXPECT_TRUE(c3.HasFile(dir2inner));
1322 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1323 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001324
1325 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001326 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001327 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001328 EXPECT_TRUE(c4.HasFile(dir2));
1329 EXPECT_TRUE(c4.HasFile(dir2));
1330 EXPECT_TRUE(c4.HasFile(file1));
1331 EXPECT_TRUE(c4.HasFile(file2_abs));
1332 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001333
1334 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001335 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001336 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001337 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001338 EXPECT_TRUE(c5.HasFile(dir1));
1339 EXPECT_TRUE(c5.HasFile(dir2));
1340 EXPECT_TRUE(c5.HasFile(dir2file));
1341 EXPECT_TRUE(c5.HasFile(dir2inner));
1342 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1343 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001344
1345 // Make sure the destructor closes the find handle while in the middle of a
1346 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001347 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001348 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1349 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001350}
license.botf003cfe2008-08-24 09:55:55 +09001351
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001352TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001353 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001354
1355 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001356 if (file_util::PathExists(data_dir)) {
1357 ASSERT_TRUE(file_util::Delete(data_dir, true));
1358 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001359 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1360
1361 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1362 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1363 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1364 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1365
1366 // Annoyingly, the directories must actually exist in order for realpath(),
1367 // which Contains() relies on in posix, to work.
1368 ASSERT_TRUE(file_util::CreateDirectory(foo));
1369 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001370 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1371 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1372 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001373
1374 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1375 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1376 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1377 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1378
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001379 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001380 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1381#if defined(OS_WIN)
1382 EXPECT_TRUE(file_util::ContainsPath(foo,
1383 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001384 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001385 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001386#elif defined(OS_MACOSX)
1387 // We can't really do this test on OS X since the case-sensitivity of the
1388 // filesystem is configurable.
1389#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001390 EXPECT_FALSE(file_util::ContainsPath(foo,
1391 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001392#endif
1393}
1394
jochen@chromium.orga6879772010-02-18 19:02:26 +09001395TEST_F(FileUtilTest, LastModified) {
1396 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1397
1398 // Create a fresh, empty copy of this directory.
1399 if (file_util::PathExists(data_dir)) {
1400 ASSERT_TRUE(file_util::Delete(data_dir, true));
1401 }
1402 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1403
1404 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1405 std::string data("hello");
1406 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1407
1408 base::Time modification_time;
1409 // Note that this timestamp is divisible by two (seconds) - FAT stores
1410 // modification times with 2s resolution.
1411 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1412 &modification_time));
1413 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1414 file_util::FileInfo file_info;
1415 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1416 ASSERT_TRUE(file_info.last_modified == modification_time);
1417}
1418
mark@chromium.org17684802008-09-10 09:16:28 +09001419} // namespace