blob: fa927372facb97a9a945f8f0ae69dcdb07d84aba [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
evan@chromium.org1db7f942010-02-27 00:11:55 +0900148#if defined(OS_WIN)
149// This function is deprecated, but still used on Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900150TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900151 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900152 const append_case& value = append_cases[i];
153 std::wstring result = value.path;
154 file_util::AppendToPath(&result, value.ending);
155 EXPECT_EQ(value.result, result);
156 }
157
158#ifdef NDEBUG
159 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
160#endif
161}
evan@chromium.org1db7f942010-02-27 00:11:55 +0900162#endif // defined(OS_WIN)
163
initial.commit3f4a7322008-07-27 06:49:38 +0900164
165static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900166 const FilePath::CharType* path;
167 const FilePath::CharType* suffix;
168 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900169} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900170 {FPL(""), FPL(""), FPL("")},
171 {FPL(""), FPL("txt"), FPL("txt")},
172 {FPL("."), FPL("txt"), FPL("txt.")},
173 {FPL("."), FPL(""), FPL(".")},
174 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
175 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
176 {FPL("foo"), FPL("txt"), FPL("footxt")},
177 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
178 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
179 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
180 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
181 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
182 {FPL("foo"), FPL(""), FPL("foo")},
183 {FPL("foo"), FPL("."), FPL("foo.")},
184 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
185 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900186#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900187 {FPL("\\"), FPL(""), FPL("\\")},
188 {FPL("\\"), FPL("txt"), FPL("\\txt")},
189 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
190 {FPL("\\."), FPL(""), FPL("\\.")},
191 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
192 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
193 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
194 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
195 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
196 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
197 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
198 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
199 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900200#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900201 {FPL("/"), FPL(""), FPL("/")},
202 {FPL("/"), FPL("txt"), FPL("/txt")},
203 {FPL("/."), FPL("txt"), FPL("/txt.")},
204 {FPL("/."), FPL(""), FPL("/.")},
205 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
206 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
207 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
208 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
209 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
210 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
211 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
212 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900213#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900214};
215
216TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900217 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900218 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900219 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900220 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900221 }
222}
223
224static const struct filename_case {
225 const wchar_t* path;
226 const wchar_t* filename;
227} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900228#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900229 {L"c:\\colon\\backslash", L"backslash"},
230 {L"c:\\colon\\backslash\\", L""},
231 {L"\\\\filename.exe", L"filename.exe"},
232 {L"filename.exe", L"filename.exe"},
233 {L"", L""},
234 {L"\\\\\\", L""},
235 {L"c:/colon/backslash", L"backslash"},
236 {L"c:/colon/backslash/", L""},
237 {L"//////", L""},
238 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900239#elif defined(OS_POSIX)
240 {L"/foo/bar", L"bar"},
241 {L"/foo/bar/", L""},
242 {L"/filename.exe", L"filename.exe"},
243 {L"filename.exe", L"filename.exe"},
244 {L"", L""},
245 {L"/", L""},
246#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900247};
248
249TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900250 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900251 const filename_case& value = filename_cases[i];
252 std::wstring result = file_util::GetFilenameFromPath(value.path);
253 EXPECT_EQ(value.filename, result);
254 }
255}
256
257// Test finding the file type from a path name
258static const struct extension_case {
259 const wchar_t* path;
260 const wchar_t* extension;
261} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900262#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900263 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
264 {L"C:\\colon\\backslash\\filename.", L""},
265 {L"C:\\colon\\backslash\\filename", L""},
266 {L"C:\\colon\\backslash\\", L""},
267 {L"C:\\colon\\backslash.\\", L""},
268 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900269#elif defined(OS_POSIX)
270 {L"/foo/bar/filename.extension", L"extension"},
271 {L"/foo/bar/filename.", L""},
272 {L"/foo/bar/filename", L""},
273 {L"/foo/bar/", L""},
274 {L"/foo/bar./", L""},
275 {L"/foo/bar/filename.extension.extension2", L"extension2"},
276 {L".", L""},
277 {L"..", L""},
278 {L"./foo", L""},
279 {L"./foo.extension", L"extension"},
280 {L"/foo.extension1/bar.extension2", L"extension2"},
281#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900282};
283
284TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900285 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900286 const extension_case& ext = extension_cases[i];
287 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
288 EXPECT_EQ(ext.extension, fext);
289 }
290}
291
292// Test finding the directory component of a path
293static const struct dir_case {
294 const wchar_t* full_path;
295 const wchar_t* directory;
296} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900297#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900298 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
299 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
300 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
301 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
302 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
303 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900304 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900305#elif defined(OS_POSIX)
306 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
307 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
308 {L"/foo/bar/", L"/foo/bar"},
309 {L"/foo/bar//", L"/foo/bar"},
310 {L"/foo/bar", L"/foo"},
311 {L"/foo/bar./", L"/foo/bar."},
312 {L"/", L"/"},
313 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900314 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900315#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900316};
317
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900318#if defined(OS_WIN)
319// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900320TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900321 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900322 const dir_case& dir = dir_cases[i];
323 const std::wstring parent =
324 file_util::GetDirectoryFromPath(dir.full_path);
325 EXPECT_EQ(dir.directory, parent);
326 }
327}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900328#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900329
330TEST_F(FileUtilTest, CountFilesCreatedAfter) {
331 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900332 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900333 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
334
335 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900336#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900337 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900338#elif defined(OS_POSIX)
339 // We need to wait at least one second here because the precision of
340 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900341 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900342#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900343
344 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900345 base::Time now(base::Time::NowFromSystemTime());
346 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900347
348 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900349 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900350 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
351
352 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900353 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900354
355 // Delete new file, we should see no files after cutoff now
356 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900357 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900358}
359
360// Tests that the Delete function works as expected, especially
361// the recursion flag. Also coincidentally tests PathExists.
362TEST_F(FileUtilTest, Delete) {
363 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900364 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900365 CreateTextFile(file_name, L"I'm cannon fodder.");
366
367 ASSERT_TRUE(file_util::PathExists(file_name));
368
evanm@google.com874d1672008-10-31 08:54:04 +0900369 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
370 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900371
372 ASSERT_TRUE(file_util::PathExists(subdir_path));
373
evanm@google.com874d1672008-10-31 08:54:04 +0900374 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900375#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900376 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900377 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900378 // Delete non-recursively and check that only the file is deleted
379 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900380 EXPECT_FALSE(file_util::PathExists(file_name));
381 EXPECT_TRUE(file_util::PathExists(subdir_path));
382#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900383
384 // Delete recursively and make sure all contents are deleted
385 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900386 EXPECT_FALSE(file_util::PathExists(file_name));
387 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900388}
389
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900390TEST_F(FileUtilTest, MoveFileNew) {
391 // Create a file
392 FilePath file_name_from =
393 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
394 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
395 ASSERT_TRUE(file_util::PathExists(file_name_from));
396
397 // The destination
398 FilePath file_name_to =
399 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
400 ASSERT_FALSE(file_util::PathExists(file_name_to));
401
402 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
403
404 // Check everything has been moved.
405 EXPECT_FALSE(file_util::PathExists(file_name_from));
406 EXPECT_TRUE(file_util::PathExists(file_name_to));
407}
408
409TEST_F(FileUtilTest, MoveFileExists) {
410 // Create a file
411 FilePath file_name_from =
412 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
413 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
414 ASSERT_TRUE(file_util::PathExists(file_name_from));
415
416 // The destination name
417 FilePath file_name_to =
418 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
419 CreateTextFile(file_name_to, L"Old file content");
420 ASSERT_TRUE(file_util::PathExists(file_name_to));
421
422 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
423
424 // Check everything has been moved.
425 EXPECT_FALSE(file_util::PathExists(file_name_from));
426 EXPECT_TRUE(file_util::PathExists(file_name_to));
427 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
428}
429
430TEST_F(FileUtilTest, MoveFileDirExists) {
431 // Create a file
432 FilePath file_name_from =
433 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
434 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
435 ASSERT_TRUE(file_util::PathExists(file_name_from));
436
437 // The destination directory
438 FilePath dir_name_to =
439 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
440 file_util::CreateDirectory(dir_name_to);
441 ASSERT_TRUE(file_util::PathExists(dir_name_to));
442
443 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
444}
445
446
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900447TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900448 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900449 FilePath dir_name_from =
450 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
451 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900452 ASSERT_TRUE(file_util::PathExists(dir_name_from));
453
454 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900455 FilePath file_name_from =
456 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900457 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
458 ASSERT_TRUE(file_util::PathExists(file_name_from));
459
460 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900461 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
462 FilePath file_name_to =
463 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900464
465 ASSERT_FALSE(file_util::PathExists(dir_name_to));
466
467 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
468
469 // Check everything has been moved.
470 EXPECT_FALSE(file_util::PathExists(dir_name_from));
471 EXPECT_FALSE(file_util::PathExists(file_name_from));
472 EXPECT_TRUE(file_util::PathExists(dir_name_to));
473 EXPECT_TRUE(file_util::PathExists(file_name_to));
474}
475
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900476TEST_F(FileUtilTest, MoveExist) {
477 // Create a directory
478 FilePath dir_name_from =
479 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
480 file_util::CreateDirectory(dir_name_from);
481 ASSERT_TRUE(file_util::PathExists(dir_name_from));
482
483 // Create a file under the directory
484 FilePath file_name_from =
485 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
486 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
487 ASSERT_TRUE(file_util::PathExists(file_name_from));
488
489 // Move the directory
490 FilePath dir_name_exists =
491 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
492
493 FilePath dir_name_to =
494 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
495 FilePath file_name_to =
496 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
497
498 // Create the destination directory.
499 file_util::CreateDirectory(dir_name_exists);
500 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
501
502 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
503
504 // Check everything has been moved.
505 EXPECT_FALSE(file_util::PathExists(dir_name_from));
506 EXPECT_FALSE(file_util::PathExists(file_name_from));
507 EXPECT_TRUE(file_util::PathExists(dir_name_to));
508 EXPECT_TRUE(file_util::PathExists(file_name_to));
509}
510
511TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900512 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900513 FilePath dir_name_from =
514 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
515 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900516 ASSERT_TRUE(file_util::PathExists(dir_name_from));
517
518 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900519 FilePath file_name_from =
520 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900521 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
522 ASSERT_TRUE(file_util::PathExists(file_name_from));
523
524 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900525 FilePath subdir_name_from =
526 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
527 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900528 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
529
530 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900531 FilePath file_name2_from =
532 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900533 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
534 ASSERT_TRUE(file_util::PathExists(file_name2_from));
535
536 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900537 FilePath dir_name_to =
538 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
539 FilePath file_name_to =
540 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
541 FilePath subdir_name_to =
542 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
543 FilePath file_name2_to =
544 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900545
546 ASSERT_FALSE(file_util::PathExists(dir_name_to));
547
548 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
549
550 // Check everything has been copied.
551 EXPECT_TRUE(file_util::PathExists(dir_name_from));
552 EXPECT_TRUE(file_util::PathExists(file_name_from));
553 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
554 EXPECT_TRUE(file_util::PathExists(file_name2_from));
555 EXPECT_TRUE(file_util::PathExists(dir_name_to));
556 EXPECT_TRUE(file_util::PathExists(file_name_to));
557 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
558 EXPECT_TRUE(file_util::PathExists(file_name2_to));
559}
560
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900561TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
562 // Create a directory.
563 FilePath dir_name_from =
564 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
565 file_util::CreateDirectory(dir_name_from);
566 ASSERT_TRUE(file_util::PathExists(dir_name_from));
567
568 // Create a file under the directory.
569 FilePath file_name_from =
570 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
571 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
572 ASSERT_TRUE(file_util::PathExists(file_name_from));
573
574 // Create a subdirectory.
575 FilePath subdir_name_from =
576 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
577 file_util::CreateDirectory(subdir_name_from);
578 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
579
580 // Create a file under the subdirectory.
581 FilePath file_name2_from =
582 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
583 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
584 ASSERT_TRUE(file_util::PathExists(file_name2_from));
585
586 // Copy the directory recursively.
587 FilePath dir_name_exists =
588 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
589
590 FilePath dir_name_to =
591 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
592 FilePath file_name_to =
593 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
594 FilePath subdir_name_to =
595 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
596 FilePath file_name2_to =
597 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
598
599 // Create the destination directory.
600 file_util::CreateDirectory(dir_name_exists);
601 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
602
603 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
604
605 // Check everything has been copied.
606 EXPECT_TRUE(file_util::PathExists(dir_name_from));
607 EXPECT_TRUE(file_util::PathExists(file_name_from));
608 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
609 EXPECT_TRUE(file_util::PathExists(file_name2_from));
610 EXPECT_TRUE(file_util::PathExists(dir_name_to));
611 EXPECT_TRUE(file_util::PathExists(file_name_to));
612 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
613 EXPECT_TRUE(file_util::PathExists(file_name2_to));
614}
615
616TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900617 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900618 FilePath dir_name_from =
619 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
620 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900621 ASSERT_TRUE(file_util::PathExists(dir_name_from));
622
623 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900624 FilePath file_name_from =
625 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900626 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
627 ASSERT_TRUE(file_util::PathExists(file_name_from));
628
629 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900630 FilePath subdir_name_from =
631 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
632 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900633 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
634
635 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900636 FilePath file_name2_from =
637 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900638 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
639 ASSERT_TRUE(file_util::PathExists(file_name2_from));
640
641 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900642 FilePath dir_name_to =
643 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
644 FilePath file_name_to =
645 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
646 FilePath subdir_name_to =
647 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900648
649 ASSERT_FALSE(file_util::PathExists(dir_name_to));
650
651 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
652
653 // Check everything has been copied.
654 EXPECT_TRUE(file_util::PathExists(dir_name_from));
655 EXPECT_TRUE(file_util::PathExists(file_name_from));
656 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
657 EXPECT_TRUE(file_util::PathExists(file_name2_from));
658 EXPECT_TRUE(file_util::PathExists(dir_name_to));
659 EXPECT_TRUE(file_util::PathExists(file_name_to));
660 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
661}
662
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900663TEST_F(FileUtilTest, CopyDirectoryExists) {
664 // Create a directory.
665 FilePath dir_name_from =
666 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
667 file_util::CreateDirectory(dir_name_from);
668 ASSERT_TRUE(file_util::PathExists(dir_name_from));
669
670 // Create a file under the directory.
671 FilePath file_name_from =
672 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
673 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
674 ASSERT_TRUE(file_util::PathExists(file_name_from));
675
676 // Create a subdirectory.
677 FilePath subdir_name_from =
678 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
679 file_util::CreateDirectory(subdir_name_from);
680 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
681
682 // Create a file under the subdirectory.
683 FilePath file_name2_from =
684 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
685 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
686 ASSERT_TRUE(file_util::PathExists(file_name2_from));
687
688 // Copy the directory not recursively.
689 FilePath dir_name_to =
690 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
691 FilePath file_name_to =
692 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
693 FilePath subdir_name_to =
694 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
695
696 // Create the destination directory.
697 file_util::CreateDirectory(dir_name_to);
698 ASSERT_TRUE(file_util::PathExists(dir_name_to));
699
700 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
701
702 // Check everything has been copied.
703 EXPECT_TRUE(file_util::PathExists(dir_name_from));
704 EXPECT_TRUE(file_util::PathExists(file_name_from));
705 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
706 EXPECT_TRUE(file_util::PathExists(file_name2_from));
707 EXPECT_TRUE(file_util::PathExists(dir_name_to));
708 EXPECT_TRUE(file_util::PathExists(file_name_to));
709 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
710}
711
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900712TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
713 // Create a file
714 FilePath file_name_from =
715 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
716 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
717 ASSERT_TRUE(file_util::PathExists(file_name_from));
718
719 // The destination name
720 FilePath file_name_to =
721 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
722 ASSERT_FALSE(file_util::PathExists(file_name_to));
723
724 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
725
726 // Check the has been copied
727 EXPECT_TRUE(file_util::PathExists(file_name_to));
728}
729
730TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
731 // Create a file
732 FilePath file_name_from =
733 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
734 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
735 ASSERT_TRUE(file_util::PathExists(file_name_from));
736
737 // The destination name
738 FilePath file_name_to =
739 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
740 CreateTextFile(file_name_to, L"Old file content");
741 ASSERT_TRUE(file_util::PathExists(file_name_to));
742
743 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
744
745 // Check the has been copied
746 EXPECT_TRUE(file_util::PathExists(file_name_to));
747 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
748}
749
750TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
751 // Create a file
752 FilePath file_name_from =
753 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
754 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
755 ASSERT_TRUE(file_util::PathExists(file_name_from));
756
757 // The destination
758 FilePath dir_name_to =
759 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
760 file_util::CreateDirectory(dir_name_to);
761 ASSERT_TRUE(file_util::PathExists(dir_name_to));
762 FilePath file_name_to =
763 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
764
765 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
766
767 // Check the has been copied
768 EXPECT_TRUE(file_util::PathExists(file_name_to));
769}
770
initial.commit3f4a7322008-07-27 06:49:38 +0900771TEST_F(FileUtilTest, CopyFile) {
772 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900773 FilePath dir_name_from =
774 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
775 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900776 ASSERT_TRUE(file_util::PathExists(dir_name_from));
777
778 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900779 FilePath file_name_from =
780 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900781 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
782 CreateTextFile(file_name_from, file_contents);
783 ASSERT_TRUE(file_util::PathExists(file_name_from));
784
785 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900786 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900787 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900788
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900789 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +0900790 FilePath dest_file2(dir_name_from);
791 dest_file2 = dest_file2.AppendASCII("..");
792 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
793 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
794
795 FilePath dest_file2_test(dir_name_from);
796 dest_file2_test = dest_file2_test.DirName();
797 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900798
799 // Check everything has been copied.
800 EXPECT_TRUE(file_util::PathExists(file_name_from));
801 EXPECT_TRUE(file_util::PathExists(dest_file));
802 const std::wstring read_contents = ReadTextFile(dest_file);
803 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +0900804 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
805 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +0900806}
807
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900808// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900809#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900810TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900811 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900812
813 SYSTEMTIME start_time;
814 GetLocalTime(&start_time);
815 Sleep(100);
816 CreateTextFile(file_name, L"New file!");
817 Sleep(100);
818 SYSTEMTIME end_time;
819 GetLocalTime(&end_time);
820
821 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900822 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900823
824 FILETIME start_filetime;
825 SystemTimeToFileTime(&start_time, &start_filetime);
826 FILETIME end_filetime;
827 SystemTimeToFileTime(&end_time, &end_filetime);
828 FILETIME file_creation_filetime;
829 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
830
831 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
832 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
833 "creation time: " << FileTimeAsUint64(file_creation_filetime);
834
835 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
836 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
837 "end time: " << FileTimeAsUint64(end_filetime);
838
evanm@google.com874d1672008-10-31 08:54:04 +0900839 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900840}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900841#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900842
erikkay@google.comf2406842008-08-21 00:59:49 +0900843// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900844// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900845typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900846
erikkay@google.comf2406842008-08-21 00:59:49 +0900847TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900848 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900849 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900850 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
851 .Append(FILE_PATH_LITERAL("data"))
852 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900853 ASSERT_TRUE(file_util::PathExists(data_dir));
854
evanm@google.com874d1672008-10-31 08:54:04 +0900855 FilePath original_file =
856 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
857 FilePath same_file =
858 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
859 FilePath same_length_file =
860 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
861 FilePath different_file =
862 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
863 FilePath different_first_file =
864 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
865 FilePath different_last_file =
866 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
867 FilePath empty1_file =
868 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
869 FilePath empty2_file =
870 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
871 FilePath shortened_file =
872 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
873 FilePath binary_file =
874 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
875 FilePath binary_file_same =
876 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
877 FilePath binary_file_diff =
878 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900879
880 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
881 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
882 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
883 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +0900884 EXPECT_FALSE(file_util::ContentsEqual(
885 FilePath(FILE_PATH_LITERAL("bogusname")),
886 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +0900887 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
888 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
889 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
890 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
891 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
892 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
893 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
894}
895
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900896TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
897 FilePath data_dir;
898 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
899 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
900 .Append(FILE_PATH_LITERAL("data"))
901 .Append(FILE_PATH_LITERAL("file_util_unittest"));
902 ASSERT_TRUE(file_util::PathExists(data_dir));
903
904 FilePath original_file =
905 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
906 FilePath same_file =
907 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
908 FilePath crlf_file =
909 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
910 FilePath shortened_file =
911 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
912 FilePath different_file =
913 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
914 FilePath different_first_file =
915 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
916 FilePath different_last_file =
917 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
918 FilePath first1_file =
919 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
920 FilePath first2_file =
921 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
922 FilePath empty1_file =
923 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
924 FilePath empty2_file =
925 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
926 FilePath blank_line_file =
927 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
928 FilePath blank_line_crlf_file =
929 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
930
931 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
932 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
933 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
934 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
935 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
936 different_first_file));
937 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
938 different_last_file));
939 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
940 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
941 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
942 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
943 blank_line_crlf_file));
944}
945
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900946// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900947#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900948TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900949 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900950 CreateTextFile(target_file, L"This is the target.");
951
evanm@google.com874d1672008-10-31 08:54:04 +0900952 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900953
954 HRESULT result;
955 IShellLink *shell = NULL;
956 IPersistFile *persist = NULL;
957
958 CoInitialize(NULL);
959 // Temporarily create a shortcut for test
960 result = CoCreateInstance(CLSID_ShellLink, NULL,
961 CLSCTX_INPROC_SERVER, IID_IShellLink,
962 reinterpret_cast<LPVOID*>(&shell));
963 EXPECT_TRUE(SUCCEEDED(result));
964 result = shell->QueryInterface(IID_IPersistFile,
965 reinterpret_cast<LPVOID*>(&persist));
966 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900967 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900968 EXPECT_TRUE(SUCCEEDED(result));
969 result = shell->SetDescription(L"ResolveShortcutTest");
970 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900971 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +0900972 EXPECT_TRUE(SUCCEEDED(result));
973 if (persist)
974 persist->Release();
975 if (shell)
976 shell->Release();
977
978 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +0900979 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +0900980 EXPECT_TRUE(is_solved);
981 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +0900982 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +0900983 EXPECT_EQ(L"This is the target.", contents);
984
ericroman@google.comdbff4f52008-08-19 01:00:38 +0900985 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +0900986 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +0900987 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900988 CoUninitialize();
989}
990
991TEST_F(FileUtilTest, CreateShortcutTest) {
992 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +0900993 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900994 CreateTextFile(target_file, file_contents);
995
evanm@google.com874d1672008-10-31 08:54:04 +0900996 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900997
998 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +0900999 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1000 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001001 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001002 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001003 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001004 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001005 EXPECT_EQ(file_contents, read_contents);
1006
evanm@google.com874d1672008-10-31 08:54:04 +09001007 DeleteFile(target_file.value().c_str());
1008 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001009 CoUninitialize();
1010}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001011
1012TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1013 // Create a directory
1014 FilePath dir_name_from =
1015 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1016 file_util::CreateDirectory(dir_name_from);
1017 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1018
1019 // Create a file under the directory
1020 FilePath file_name_from =
1021 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1022 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1023 ASSERT_TRUE(file_util::PathExists(file_name_from));
1024
1025 // Move the directory by using CopyAndDeleteDirectory
1026 FilePath dir_name_to = test_dir_.Append(
1027 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1028 FilePath file_name_to =
1029 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1030
1031 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1032
1033 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1034
1035 // Check everything has been moved.
1036 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1037 EXPECT_FALSE(file_util::PathExists(file_name_from));
1038 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1039 EXPECT_TRUE(file_util::PathExists(file_name_to));
1040}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001041
1042TEST_F(FileUtilTest, GetTempDirTest) {
1043 static const TCHAR* kTmpKey = _T("TMP");
1044 static const TCHAR* kTmpValues[] = {
1045 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1046 };
1047 // Save the original $TMP.
1048 size_t original_tmp_size;
1049 TCHAR* original_tmp;
1050 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1051 // original_tmp may be NULL.
1052
1053 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1054 FilePath path;
1055 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1056 file_util::GetTempDir(&path);
1057 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1058 " result=" << path.value();
1059 }
1060
1061 // Restore the original $TMP.
1062 if (original_tmp) {
1063 ::_tputenv_s(kTmpKey, original_tmp);
1064 free(original_tmp);
1065 } else {
1066 ::_tputenv_s(kTmpKey, _T(""));
1067 }
1068}
1069#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001070
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001071TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1072 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001073 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001074 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001075 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1076 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1077 }
1078 for (int i = 0; i < 3; i++)
1079 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1080 for (int i = 0; i < 3; i++)
1081 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1082}
1083
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001084TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001085 FilePath names[3];
1086 FILE *fps[3];
1087 int i;
1088
1089 // Create; make sure they are open and exist.
1090 for (i = 0; i < 3; ++i) {
1091 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1092 ASSERT_TRUE(fps[i]);
1093 EXPECT_TRUE(file_util::PathExists(names[i]));
1094 }
1095
1096 // Make sure all names are unique.
1097 for (i = 0; i < 3; ++i) {
1098 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1099 }
1100
1101 // Close and delete.
1102 for (i = 0; i < 3; ++i) {
1103 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1104 EXPECT_TRUE(file_util::Delete(names[i], false));
1105 }
initial.commit3f4a7322008-07-27 06:49:38 +09001106}
1107
1108TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001109 FilePath temp_dir;
1110 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1111 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001112 EXPECT_TRUE(file_util::PathExists(temp_dir));
1113 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001114}
1115
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001116TEST_F(FileUtilTest, GetShmemTempDirTest) {
1117 FilePath dir;
1118 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1119 EXPECT_TRUE(file_util::DirectoryExists(dir));
1120}
1121
initial.commit3f4a7322008-07-27 06:49:38 +09001122TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001123 FilePath test_root =
1124 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001125#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001126 FilePath test_path =
1127 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001128#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001129 FilePath test_path =
1130 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001131#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001132
1133 EXPECT_FALSE(file_util::PathExists(test_path));
1134 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1135 EXPECT_TRUE(file_util::PathExists(test_path));
1136 // CreateDirectory returns true if the DirectoryExists returns true.
1137 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1138
1139 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001140 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001141 EXPECT_FALSE(file_util::PathExists(test_path));
1142 CreateTextFile(test_path, L"test file");
1143 EXPECT_TRUE(file_util::PathExists(test_path));
1144 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1145
1146 EXPECT_TRUE(file_util::Delete(test_root, true));
1147 EXPECT_FALSE(file_util::PathExists(test_root));
1148 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001149
1150 // Verify assumptions made by the Windows implementation:
1151 // 1. The current directory always exists.
1152 // 2. The root directory always exists.
1153 ASSERT_TRUE(file_util::DirectoryExists(
1154 FilePath(FilePath::kCurrentDirectory)));
1155 FilePath top_level = test_root;
1156 while (top_level != top_level.DirName()) {
1157 top_level = top_level.DirName();
1158 }
1159 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1160
1161 // Given these assumptions hold, it should be safe to
1162 // test that "creating" these directories succeeds.
1163 EXPECT_TRUE(file_util::CreateDirectory(
1164 FilePath(FilePath::kCurrentDirectory)));
1165 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001166
1167#if defined(OS_WIN)
1168 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1169 FilePath invalid_path =
1170 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1171 if (!file_util::PathExists(invalid_drive)) {
1172 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1173 }
1174#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001175}
1176
1177TEST_F(FileUtilTest, DetectDirectoryTest) {
1178 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001179 FilePath test_root =
1180 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001181 EXPECT_FALSE(file_util::PathExists(test_root));
1182 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1183 EXPECT_TRUE(file_util::PathExists(test_root));
1184 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1185
1186 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001187 FilePath test_path =
1188 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001189 EXPECT_FALSE(file_util::PathExists(test_path));
1190 CreateTextFile(test_path, L"test file");
1191 EXPECT_TRUE(file_util::PathExists(test_path));
1192 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1193 EXPECT_TRUE(file_util::Delete(test_path, false));
1194
1195 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001196}
1197
initial.commit3f4a7322008-07-27 06:49:38 +09001198static const struct ReplaceExtensionCase {
1199 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +09001200 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +09001201 std::wstring result;
1202} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +09001203 {L"", FILE_PATH_LITERAL(""), L""},
1204 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
1205 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
1206 {L".", FILE_PATH_LITERAL(""), L""},
1207 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
1208 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
1209 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
1210 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
1211 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
1212 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
1213 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
1214 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
1215 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
1216 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
1217 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
1218 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +09001219};
1220
1221TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001222 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +09001223 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
1224 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
1225 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +09001226 }
1227}
1228
sky@google.com71e7c6f2008-09-20 02:32:18 +09001229// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
1230// the directory names of the path.
1231TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +09001232 FilePath path;
1233 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
1234 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +09001235 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +09001236 FilePath result_path = path;
1237 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001238 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
1239 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +09001240}
1241
initial.commit3f4a7322008-07-27 06:49:38 +09001242TEST_F(FileUtilTest, FileEnumeratorTest) {
1243 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001244 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001245 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1246 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001247
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001248 // Test an empty directory, non-recursively, including "..".
1249 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1250 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1251 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1252 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1253 f0_dotdot.Next().value());
1254 EXPECT_EQ(FILE_PATH_LITERAL(""),
1255 f0_dotdot.Next().value());
1256
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001257 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001258 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001259 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001260 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001261 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001262 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001263 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001264
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001265 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001266 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001267 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001268 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001269 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001270 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001271 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001272 FilePath file2_rel =
1273 dir2.Append(FilePath::kParentDirectory)
1274 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001275 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001276 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001277
1278 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001279 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001280 file_util::FileEnumerator::FILES);
1281 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001282 EXPECT_TRUE(c1.HasFile(file1));
1283 EXPECT_TRUE(c1.HasFile(file2_abs));
1284 EXPECT_TRUE(c1.HasFile(dir2file));
1285 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1286 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001287
1288 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001289 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001290 file_util::FileEnumerator::DIRECTORIES);
1291 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001292 EXPECT_TRUE(c2.HasFile(dir1));
1293 EXPECT_TRUE(c2.HasFile(dir2));
1294 EXPECT_TRUE(c2.HasFile(dir2inner));
1295 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001296
tim@chromium.org989d0972008-10-16 11:42:45 +09001297 // Only enumerate directories non-recursively.
1298 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001299 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001300 FindResultCollector c2_non_recursive(f2_non_recursive);
1301 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1302 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1303 EXPECT_EQ(c2_non_recursive.size(), 2);
1304
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001305 // Only enumerate directories, non-recursively, including "..".
1306 file_util::FileEnumerator f2_dotdot(
1307 test_dir_, false,
1308 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1309 file_util::FileEnumerator::DIRECTORIES |
1310 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1311 FindResultCollector c2_dotdot(f2_dotdot);
1312 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1313 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1314 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1315 EXPECT_EQ(c2_dotdot.size(), 3);
1316
initial.commit3f4a7322008-07-27 06:49:38 +09001317 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001318 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001319 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001320 EXPECT_TRUE(c3.HasFile(dir1));
1321 EXPECT_TRUE(c3.HasFile(dir2));
1322 EXPECT_TRUE(c3.HasFile(file1));
1323 EXPECT_TRUE(c3.HasFile(file2_abs));
1324 EXPECT_TRUE(c3.HasFile(dir2file));
1325 EXPECT_TRUE(c3.HasFile(dir2inner));
1326 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1327 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001328
1329 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001330 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001331 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001332 EXPECT_TRUE(c4.HasFile(dir2));
1333 EXPECT_TRUE(c4.HasFile(dir2));
1334 EXPECT_TRUE(c4.HasFile(file1));
1335 EXPECT_TRUE(c4.HasFile(file2_abs));
1336 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001337
1338 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001339 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001340 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001341 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001342 EXPECT_TRUE(c5.HasFile(dir1));
1343 EXPECT_TRUE(c5.HasFile(dir2));
1344 EXPECT_TRUE(c5.HasFile(dir2file));
1345 EXPECT_TRUE(c5.HasFile(dir2inner));
1346 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1347 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001348
1349 // Make sure the destructor closes the find handle while in the middle of a
1350 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001351 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001352 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1353 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001354}
license.botf003cfe2008-08-24 09:55:55 +09001355
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001356TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001357 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001358
1359 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001360 if (file_util::PathExists(data_dir)) {
1361 ASSERT_TRUE(file_util::Delete(data_dir, true));
1362 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001363 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1364
1365 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1366 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1367 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1368 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1369
1370 // Annoyingly, the directories must actually exist in order for realpath(),
1371 // which Contains() relies on in posix, to work.
1372 ASSERT_TRUE(file_util::CreateDirectory(foo));
1373 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001374 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1375 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1376 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001377
1378 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1379 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1380 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1381 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1382
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001383 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001384 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1385#if defined(OS_WIN)
1386 EXPECT_TRUE(file_util::ContainsPath(foo,
1387 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001388 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001389 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001390#elif defined(OS_MACOSX)
1391 // We can't really do this test on OS X since the case-sensitivity of the
1392 // filesystem is configurable.
1393#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001394 EXPECT_FALSE(file_util::ContainsPath(foo,
1395 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001396#endif
1397}
1398
jochen@chromium.orga6879772010-02-18 19:02:26 +09001399TEST_F(FileUtilTest, LastModified) {
1400 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1401
1402 // Create a fresh, empty copy of this directory.
1403 if (file_util::PathExists(data_dir)) {
1404 ASSERT_TRUE(file_util::Delete(data_dir, true));
1405 }
1406 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1407
1408 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1409 std::string data("hello");
1410 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1411
1412 base::Time modification_time;
1413 // Note that this timestamp is divisible by two (seconds) - FAT stores
1414 // modification times with 2s resolution.
1415 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1416 &modification_time));
1417 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1418 file_util::FileInfo file_info;
1419 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1420 ASSERT_TRUE(file_info.last_modified == modification_time);
1421}
1422
mark@chromium.org17684802008-09-10 09:16:28 +09001423} // namespace