blob: 25b93e1c460ddadf2d51cba6a8726c5517ed437b [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
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900360TEST_F(FileUtilTest, FileAndDirectorySize) {
361 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
362 // should return 53 bytes.
363 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
364 CreateTextFile(file_01, L"12345678901234567890");
365 int64 size_f1 = 0;
366 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
367 EXPECT_EQ(20ll, size_f1);
368
369 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
370 file_util::CreateDirectory(subdir_path);
371
372 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
373 CreateTextFile(file_02, L"123456789012345678901234567890");
374 int64 size_f2 = 0;
375 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
376 EXPECT_EQ(30ll, size_f2);
377
378 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
379 file_util::CreateDirectory(subsubdir_path);
380
381 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
382 CreateTextFile(file_03, L"123");
383
384 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
385 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
386}
387
initial.commit3f4a7322008-07-27 06:49:38 +0900388// Tests that the Delete function works as expected, especially
389// the recursion flag. Also coincidentally tests PathExists.
390TEST_F(FileUtilTest, Delete) {
391 // Create a file
evanm@google.com874d1672008-10-31 08:54:04 +0900392 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900393 CreateTextFile(file_name, L"I'm cannon fodder.");
394
395 ASSERT_TRUE(file_util::PathExists(file_name));
396
evanm@google.com874d1672008-10-31 08:54:04 +0900397 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
398 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900399
400 ASSERT_TRUE(file_util::PathExists(subdir_path));
401
evanm@google.com874d1672008-10-31 08:54:04 +0900402 FilePath directory_contents = test_dir_;
erikkay@google.com014161d2008-08-16 02:45:13 +0900403#if defined(OS_WIN)
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900404 // TODO(erikkay): see if anyone's actually using this feature of the API
evanm@google.com874d1672008-10-31 08:54:04 +0900405 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
initial.commit3f4a7322008-07-27 06:49:38 +0900406 // Delete non-recursively and check that only the file is deleted
407 ASSERT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900408 EXPECT_FALSE(file_util::PathExists(file_name));
409 EXPECT_TRUE(file_util::PathExists(subdir_path));
410#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900411
412 // Delete recursively and make sure all contents are deleted
413 ASSERT_TRUE(file_util::Delete(directory_contents, true));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900414 EXPECT_FALSE(file_util::PathExists(file_name));
415 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commit3f4a7322008-07-27 06:49:38 +0900416}
417
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900418TEST_F(FileUtilTest, MoveFileNew) {
419 // Create a file
420 FilePath file_name_from =
421 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
422 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
423 ASSERT_TRUE(file_util::PathExists(file_name_from));
424
425 // The destination
426 FilePath file_name_to =
427 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
428 ASSERT_FALSE(file_util::PathExists(file_name_to));
429
430 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
431
432 // Check everything has been moved.
433 EXPECT_FALSE(file_util::PathExists(file_name_from));
434 EXPECT_TRUE(file_util::PathExists(file_name_to));
435}
436
437TEST_F(FileUtilTest, MoveFileExists) {
438 // Create a file
439 FilePath file_name_from =
440 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
441 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
442 ASSERT_TRUE(file_util::PathExists(file_name_from));
443
444 // The destination name
445 FilePath file_name_to =
446 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
447 CreateTextFile(file_name_to, L"Old file content");
448 ASSERT_TRUE(file_util::PathExists(file_name_to));
449
450 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
451
452 // Check everything has been moved.
453 EXPECT_FALSE(file_util::PathExists(file_name_from));
454 EXPECT_TRUE(file_util::PathExists(file_name_to));
455 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
456}
457
458TEST_F(FileUtilTest, MoveFileDirExists) {
459 // Create a file
460 FilePath file_name_from =
461 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
462 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
463 ASSERT_TRUE(file_util::PathExists(file_name_from));
464
465 // The destination directory
466 FilePath dir_name_to =
467 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
468 file_util::CreateDirectory(dir_name_to);
469 ASSERT_TRUE(file_util::PathExists(dir_name_to));
470
471 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
472}
473
474
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900475TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900476 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900477 FilePath dir_name_from =
478 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
479 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900480 ASSERT_TRUE(file_util::PathExists(dir_name_from));
481
482 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900483 FilePath file_name_from =
484 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900485 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
486 ASSERT_TRUE(file_util::PathExists(file_name_from));
487
488 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900489 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
490 FilePath file_name_to =
491 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900492
493 ASSERT_FALSE(file_util::PathExists(dir_name_to));
494
495 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
496
497 // Check everything has been moved.
498 EXPECT_FALSE(file_util::PathExists(dir_name_from));
499 EXPECT_FALSE(file_util::PathExists(file_name_from));
500 EXPECT_TRUE(file_util::PathExists(dir_name_to));
501 EXPECT_TRUE(file_util::PathExists(file_name_to));
502}
503
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900504TEST_F(FileUtilTest, MoveExist) {
505 // Create a directory
506 FilePath dir_name_from =
507 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
508 file_util::CreateDirectory(dir_name_from);
509 ASSERT_TRUE(file_util::PathExists(dir_name_from));
510
511 // Create a file under the directory
512 FilePath file_name_from =
513 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
514 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
515 ASSERT_TRUE(file_util::PathExists(file_name_from));
516
517 // Move the directory
518 FilePath dir_name_exists =
519 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
520
521 FilePath dir_name_to =
522 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
523 FilePath file_name_to =
524 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
525
526 // Create the destination directory.
527 file_util::CreateDirectory(dir_name_exists);
528 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
529
530 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
531
532 // Check everything has been moved.
533 EXPECT_FALSE(file_util::PathExists(dir_name_from));
534 EXPECT_FALSE(file_util::PathExists(file_name_from));
535 EXPECT_TRUE(file_util::PathExists(dir_name_to));
536 EXPECT_TRUE(file_util::PathExists(file_name_to));
537}
538
539TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900540 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900541 FilePath dir_name_from =
542 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
543 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900544 ASSERT_TRUE(file_util::PathExists(dir_name_from));
545
546 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900547 FilePath file_name_from =
548 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900549 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
550 ASSERT_TRUE(file_util::PathExists(file_name_from));
551
552 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900553 FilePath subdir_name_from =
554 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
555 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900556 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
557
558 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900559 FilePath file_name2_from =
560 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900561 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
562 ASSERT_TRUE(file_util::PathExists(file_name2_from));
563
564 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900565 FilePath dir_name_to =
566 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
567 FilePath file_name_to =
568 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
569 FilePath subdir_name_to =
570 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
571 FilePath file_name2_to =
572 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900573
574 ASSERT_FALSE(file_util::PathExists(dir_name_to));
575
576 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
577
578 // Check everything has been copied.
579 EXPECT_TRUE(file_util::PathExists(dir_name_from));
580 EXPECT_TRUE(file_util::PathExists(file_name_from));
581 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
582 EXPECT_TRUE(file_util::PathExists(file_name2_from));
583 EXPECT_TRUE(file_util::PathExists(dir_name_to));
584 EXPECT_TRUE(file_util::PathExists(file_name_to));
585 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
586 EXPECT_TRUE(file_util::PathExists(file_name2_to));
587}
588
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900589TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
590 // Create a directory.
591 FilePath dir_name_from =
592 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
593 file_util::CreateDirectory(dir_name_from);
594 ASSERT_TRUE(file_util::PathExists(dir_name_from));
595
596 // Create a file under the directory.
597 FilePath file_name_from =
598 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
599 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
600 ASSERT_TRUE(file_util::PathExists(file_name_from));
601
602 // Create a subdirectory.
603 FilePath subdir_name_from =
604 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
605 file_util::CreateDirectory(subdir_name_from);
606 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
607
608 // Create a file under the subdirectory.
609 FilePath file_name2_from =
610 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
611 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
612 ASSERT_TRUE(file_util::PathExists(file_name2_from));
613
614 // Copy the directory recursively.
615 FilePath dir_name_exists =
616 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
617
618 FilePath dir_name_to =
619 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
620 FilePath file_name_to =
621 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
622 FilePath subdir_name_to =
623 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
624 FilePath file_name2_to =
625 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
626
627 // Create the destination directory.
628 file_util::CreateDirectory(dir_name_exists);
629 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
630
631 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
632
633 // Check everything has been copied.
634 EXPECT_TRUE(file_util::PathExists(dir_name_from));
635 EXPECT_TRUE(file_util::PathExists(file_name_from));
636 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
637 EXPECT_TRUE(file_util::PathExists(file_name2_from));
638 EXPECT_TRUE(file_util::PathExists(dir_name_to));
639 EXPECT_TRUE(file_util::PathExists(file_name_to));
640 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
641 EXPECT_TRUE(file_util::PathExists(file_name2_to));
642}
643
644TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900645 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900646 FilePath dir_name_from =
647 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
648 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900649 ASSERT_TRUE(file_util::PathExists(dir_name_from));
650
651 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900652 FilePath file_name_from =
653 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900654 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
655 ASSERT_TRUE(file_util::PathExists(file_name_from));
656
657 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900658 FilePath subdir_name_from =
659 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
660 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900661 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
662
663 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900664 FilePath file_name2_from =
665 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900666 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
667 ASSERT_TRUE(file_util::PathExists(file_name2_from));
668
669 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900670 FilePath dir_name_to =
671 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
672 FilePath file_name_to =
673 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
674 FilePath subdir_name_to =
675 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900676
677 ASSERT_FALSE(file_util::PathExists(dir_name_to));
678
679 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
680
681 // Check everything has been copied.
682 EXPECT_TRUE(file_util::PathExists(dir_name_from));
683 EXPECT_TRUE(file_util::PathExists(file_name_from));
684 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
685 EXPECT_TRUE(file_util::PathExists(file_name2_from));
686 EXPECT_TRUE(file_util::PathExists(dir_name_to));
687 EXPECT_TRUE(file_util::PathExists(file_name_to));
688 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
689}
690
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900691TEST_F(FileUtilTest, CopyDirectoryExists) {
692 // Create a directory.
693 FilePath dir_name_from =
694 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
695 file_util::CreateDirectory(dir_name_from);
696 ASSERT_TRUE(file_util::PathExists(dir_name_from));
697
698 // Create a file under the directory.
699 FilePath file_name_from =
700 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
701 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
702 ASSERT_TRUE(file_util::PathExists(file_name_from));
703
704 // Create a subdirectory.
705 FilePath subdir_name_from =
706 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
707 file_util::CreateDirectory(subdir_name_from);
708 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
709
710 // Create a file under the subdirectory.
711 FilePath file_name2_from =
712 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
713 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
714 ASSERT_TRUE(file_util::PathExists(file_name2_from));
715
716 // Copy the directory not recursively.
717 FilePath dir_name_to =
718 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
719 FilePath file_name_to =
720 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
721 FilePath subdir_name_to =
722 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
723
724 // Create the destination directory.
725 file_util::CreateDirectory(dir_name_to);
726 ASSERT_TRUE(file_util::PathExists(dir_name_to));
727
728 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
729
730 // Check everything has been copied.
731 EXPECT_TRUE(file_util::PathExists(dir_name_from));
732 EXPECT_TRUE(file_util::PathExists(file_name_from));
733 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
734 EXPECT_TRUE(file_util::PathExists(file_name2_from));
735 EXPECT_TRUE(file_util::PathExists(dir_name_to));
736 EXPECT_TRUE(file_util::PathExists(file_name_to));
737 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
738}
739
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900740TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
741 // Create a file
742 FilePath file_name_from =
743 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
744 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
745 ASSERT_TRUE(file_util::PathExists(file_name_from));
746
747 // The destination name
748 FilePath file_name_to =
749 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
750 ASSERT_FALSE(file_util::PathExists(file_name_to));
751
752 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
753
754 // Check the has been copied
755 EXPECT_TRUE(file_util::PathExists(file_name_to));
756}
757
758TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
759 // Create a file
760 FilePath file_name_from =
761 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
762 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
763 ASSERT_TRUE(file_util::PathExists(file_name_from));
764
765 // The destination name
766 FilePath file_name_to =
767 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
768 CreateTextFile(file_name_to, L"Old file content");
769 ASSERT_TRUE(file_util::PathExists(file_name_to));
770
771 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
772
773 // Check the has been copied
774 EXPECT_TRUE(file_util::PathExists(file_name_to));
775 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
776}
777
778TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
779 // Create a file
780 FilePath file_name_from =
781 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
782 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
783 ASSERT_TRUE(file_util::PathExists(file_name_from));
784
785 // The destination
786 FilePath dir_name_to =
787 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
788 file_util::CreateDirectory(dir_name_to);
789 ASSERT_TRUE(file_util::PathExists(dir_name_to));
790 FilePath file_name_to =
791 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
792
793 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
794
795 // Check the has been copied
796 EXPECT_TRUE(file_util::PathExists(file_name_to));
797}
798
initial.commit3f4a7322008-07-27 06:49:38 +0900799TEST_F(FileUtilTest, CopyFile) {
800 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900801 FilePath dir_name_from =
802 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
803 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900804 ASSERT_TRUE(file_util::PathExists(dir_name_from));
805
806 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900807 FilePath file_name_from =
808 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900809 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
810 CreateTextFile(file_name_from, file_contents);
811 ASSERT_TRUE(file_util::PathExists(file_name_from));
812
813 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900814 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900815 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900816
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900817 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +0900818 FilePath dest_file2(dir_name_from);
819 dest_file2 = dest_file2.AppendASCII("..");
820 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
821 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
822
823 FilePath dest_file2_test(dir_name_from);
824 dest_file2_test = dest_file2_test.DirName();
825 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900826
827 // Check everything has been copied.
828 EXPECT_TRUE(file_util::PathExists(file_name_from));
829 EXPECT_TRUE(file_util::PathExists(dest_file));
830 const std::wstring read_contents = ReadTextFile(dest_file);
831 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +0900832 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
833 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +0900834}
835
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900836// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +0900837#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900838TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +0900839 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900840
841 SYSTEMTIME start_time;
842 GetLocalTime(&start_time);
843 Sleep(100);
844 CreateTextFile(file_name, L"New file!");
845 Sleep(100);
846 SYSTEMTIME end_time;
847 GetLocalTime(&end_time);
848
849 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +0900850 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900851
852 FILETIME start_filetime;
853 SystemTimeToFileTime(&start_time, &start_filetime);
854 FILETIME end_filetime;
855 SystemTimeToFileTime(&end_time, &end_filetime);
856 FILETIME file_creation_filetime;
857 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
858
859 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
860 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
861 "creation time: " << FileTimeAsUint64(file_creation_filetime);
862
863 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
864 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
865 "end time: " << FileTimeAsUint64(end_filetime);
866
evanm@google.com874d1672008-10-31 08:54:04 +0900867 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +0900868}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900869#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900870
erikkay@google.comf2406842008-08-21 00:59:49 +0900871// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +0900872// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +0900873typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +0900874
erikkay@google.comf2406842008-08-21 00:59:49 +0900875TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +0900876 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +0900877 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +0900878 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
879 .Append(FILE_PATH_LITERAL("data"))
880 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900881 ASSERT_TRUE(file_util::PathExists(data_dir));
882
evanm@google.com874d1672008-10-31 08:54:04 +0900883 FilePath original_file =
884 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
885 FilePath same_file =
886 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
887 FilePath same_length_file =
888 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
889 FilePath different_file =
890 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
891 FilePath different_first_file =
892 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
893 FilePath different_last_file =
894 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
895 FilePath empty1_file =
896 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
897 FilePath empty2_file =
898 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
899 FilePath shortened_file =
900 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
901 FilePath binary_file =
902 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
903 FilePath binary_file_same =
904 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
905 FilePath binary_file_diff =
906 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +0900907
908 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
909 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
910 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
911 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +0900912 EXPECT_FALSE(file_util::ContentsEqual(
913 FilePath(FILE_PATH_LITERAL("bogusname")),
914 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +0900915 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
916 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
917 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
918 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
919 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
920 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
921 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
922}
923
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900924TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
925 FilePath data_dir;
926 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
927 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
928 .Append(FILE_PATH_LITERAL("data"))
929 .Append(FILE_PATH_LITERAL("file_util_unittest"));
930 ASSERT_TRUE(file_util::PathExists(data_dir));
931
932 FilePath original_file =
933 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
934 FilePath same_file =
935 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
936 FilePath crlf_file =
937 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
938 FilePath shortened_file =
939 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
940 FilePath different_file =
941 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
942 FilePath different_first_file =
943 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
944 FilePath different_last_file =
945 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
946 FilePath first1_file =
947 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
948 FilePath first2_file =
949 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
950 FilePath empty1_file =
951 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
952 FilePath empty2_file =
953 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
954 FilePath blank_line_file =
955 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
956 FilePath blank_line_crlf_file =
957 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
958
959 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
960 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
961 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
962 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
963 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
964 different_first_file));
965 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
966 different_last_file));
967 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
968 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
969 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
970 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
971 blank_line_crlf_file));
972}
973
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900974// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +0900975#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900976TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +0900977 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900978 CreateTextFile(target_file, L"This is the target.");
979
evanm@google.com874d1672008-10-31 08:54:04 +0900980 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +0900981
982 HRESULT result;
983 IShellLink *shell = NULL;
984 IPersistFile *persist = NULL;
985
986 CoInitialize(NULL);
987 // Temporarily create a shortcut for test
988 result = CoCreateInstance(CLSID_ShellLink, NULL,
989 CLSCTX_INPROC_SERVER, IID_IShellLink,
990 reinterpret_cast<LPVOID*>(&shell));
991 EXPECT_TRUE(SUCCEEDED(result));
992 result = shell->QueryInterface(IID_IPersistFile,
993 reinterpret_cast<LPVOID*>(&persist));
994 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900995 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900996 EXPECT_TRUE(SUCCEEDED(result));
997 result = shell->SetDescription(L"ResolveShortcutTest");
998 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +0900999 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001000 EXPECT_TRUE(SUCCEEDED(result));
1001 if (persist)
1002 persist->Release();
1003 if (shell)
1004 shell->Release();
1005
1006 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001007 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001008 EXPECT_TRUE(is_solved);
1009 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001010 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001011 EXPECT_EQ(L"This is the target.", contents);
1012
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001013 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001014 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001015 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001016 CoUninitialize();
1017}
1018
1019TEST_F(FileUtilTest, CreateShortcutTest) {
1020 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001021 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001022 CreateTextFile(target_file, file_contents);
1023
evanm@google.com874d1672008-10-31 08:54:04 +09001024 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001025
1026 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001027 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1028 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001029 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001030 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001031 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001032 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001033 EXPECT_EQ(file_contents, read_contents);
1034
evanm@google.com874d1672008-10-31 08:54:04 +09001035 DeleteFile(target_file.value().c_str());
1036 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001037 CoUninitialize();
1038}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001039
1040TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1041 // Create a directory
1042 FilePath dir_name_from =
1043 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1044 file_util::CreateDirectory(dir_name_from);
1045 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1046
1047 // Create a file under the directory
1048 FilePath file_name_from =
1049 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1050 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1051 ASSERT_TRUE(file_util::PathExists(file_name_from));
1052
1053 // Move the directory by using CopyAndDeleteDirectory
1054 FilePath dir_name_to = test_dir_.Append(
1055 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1056 FilePath file_name_to =
1057 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1058
1059 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1060
1061 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1062
1063 // Check everything has been moved.
1064 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1065 EXPECT_FALSE(file_util::PathExists(file_name_from));
1066 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1067 EXPECT_TRUE(file_util::PathExists(file_name_to));
1068}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001069
1070TEST_F(FileUtilTest, GetTempDirTest) {
1071 static const TCHAR* kTmpKey = _T("TMP");
1072 static const TCHAR* kTmpValues[] = {
1073 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1074 };
1075 // Save the original $TMP.
1076 size_t original_tmp_size;
1077 TCHAR* original_tmp;
1078 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1079 // original_tmp may be NULL.
1080
1081 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1082 FilePath path;
1083 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1084 file_util::GetTempDir(&path);
1085 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1086 " result=" << path.value();
1087 }
1088
1089 // Restore the original $TMP.
1090 if (original_tmp) {
1091 ::_tputenv_s(kTmpKey, original_tmp);
1092 free(original_tmp);
1093 } else {
1094 ::_tputenv_s(kTmpKey, _T(""));
1095 }
1096}
1097#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001098
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001099TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1100 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001101 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001102 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001103 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1104 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1105 }
1106 for (int i = 0; i < 3; i++)
1107 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1108 for (int i = 0; i < 3; i++)
1109 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1110}
1111
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001112TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001113 FilePath names[3];
1114 FILE *fps[3];
1115 int i;
1116
1117 // Create; make sure they are open and exist.
1118 for (i = 0; i < 3; ++i) {
1119 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1120 ASSERT_TRUE(fps[i]);
1121 EXPECT_TRUE(file_util::PathExists(names[i]));
1122 }
1123
1124 // Make sure all names are unique.
1125 for (i = 0; i < 3; ++i) {
1126 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1127 }
1128
1129 // Close and delete.
1130 for (i = 0; i < 3; ++i) {
1131 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1132 EXPECT_TRUE(file_util::Delete(names[i], false));
1133 }
initial.commit3f4a7322008-07-27 06:49:38 +09001134}
1135
1136TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001137 FilePath temp_dir;
1138 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1139 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001140 EXPECT_TRUE(file_util::PathExists(temp_dir));
1141 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001142}
1143
skerner@chromium.orge4432392010-05-01 02:00:09 +09001144TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1145 FilePath new_dir;
1146 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1147 test_dir_,
1148 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1149 &new_dir));
1150 EXPECT_TRUE(file_util::PathExists(new_dir));
1151 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1152 EXPECT_TRUE(file_util::Delete(new_dir, false));
1153}
1154
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001155TEST_F(FileUtilTest, GetShmemTempDirTest) {
1156 FilePath dir;
1157 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1158 EXPECT_TRUE(file_util::DirectoryExists(dir));
1159}
1160
initial.commit3f4a7322008-07-27 06:49:38 +09001161TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001162 FilePath test_root =
1163 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001164#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001165 FilePath test_path =
1166 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001167#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001168 FilePath test_path =
1169 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001170#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001171
1172 EXPECT_FALSE(file_util::PathExists(test_path));
1173 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1174 EXPECT_TRUE(file_util::PathExists(test_path));
1175 // CreateDirectory returns true if the DirectoryExists returns true.
1176 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1177
1178 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001179 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001180 EXPECT_FALSE(file_util::PathExists(test_path));
1181 CreateTextFile(test_path, L"test file");
1182 EXPECT_TRUE(file_util::PathExists(test_path));
1183 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1184
1185 EXPECT_TRUE(file_util::Delete(test_root, true));
1186 EXPECT_FALSE(file_util::PathExists(test_root));
1187 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001188
1189 // Verify assumptions made by the Windows implementation:
1190 // 1. The current directory always exists.
1191 // 2. The root directory always exists.
1192 ASSERT_TRUE(file_util::DirectoryExists(
1193 FilePath(FilePath::kCurrentDirectory)));
1194 FilePath top_level = test_root;
1195 while (top_level != top_level.DirName()) {
1196 top_level = top_level.DirName();
1197 }
1198 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1199
1200 // Given these assumptions hold, it should be safe to
1201 // test that "creating" these directories succeeds.
1202 EXPECT_TRUE(file_util::CreateDirectory(
1203 FilePath(FilePath::kCurrentDirectory)));
1204 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001205
1206#if defined(OS_WIN)
1207 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1208 FilePath invalid_path =
1209 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1210 if (!file_util::PathExists(invalid_drive)) {
1211 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1212 }
1213#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001214}
1215
1216TEST_F(FileUtilTest, DetectDirectoryTest) {
1217 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001218 FilePath test_root =
1219 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001220 EXPECT_FALSE(file_util::PathExists(test_root));
1221 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1222 EXPECT_TRUE(file_util::PathExists(test_root));
1223 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1224
1225 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001226 FilePath test_path =
1227 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001228 EXPECT_FALSE(file_util::PathExists(test_path));
1229 CreateTextFile(test_path, L"test file");
1230 EXPECT_TRUE(file_util::PathExists(test_path));
1231 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1232 EXPECT_TRUE(file_util::Delete(test_path, false));
1233
1234 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001235}
1236
initial.commit3f4a7322008-07-27 06:49:38 +09001237static const struct ReplaceExtensionCase {
1238 std::wstring file_name;
estade@chromium.org63343202008-12-05 05:46:06 +09001239 FilePath::StringType extension;
initial.commit3f4a7322008-07-27 06:49:38 +09001240 std::wstring result;
1241} kReplaceExtension[] = {
estade@chromium.org63343202008-12-05 05:46:06 +09001242 {L"", FILE_PATH_LITERAL(""), L""},
1243 {L"", FILE_PATH_LITERAL("txt"), L".txt"},
1244 {L".", FILE_PATH_LITERAL("txt"), L".txt"},
1245 {L".", FILE_PATH_LITERAL(""), L""},
1246 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"},
1247 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
1248 {L"foo", FILE_PATH_LITERAL("txt"), L"foo.txt"},
1249 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"},
1250 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baz.txt"},
1251 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt"},
1252 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo"},
1253 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo"},
1254 {L"foo", FILE_PATH_LITERAL(""), L"foo"},
1255 {L"foo", FILE_PATH_LITERAL("."), L"foo"},
1256 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz"},
1257 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz"},
initial.commit3f4a7322008-07-27 06:49:38 +09001258};
1259
1260TEST_F(FileUtilTest, ReplaceExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001261 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
estade@chromium.org63343202008-12-05 05:46:06 +09001262 FilePath path = FilePath::FromWStringHack(kReplaceExtension[i].file_name);
1263 file_util::ReplaceExtension(&path, kReplaceExtension[i].extension);
1264 EXPECT_EQ(kReplaceExtension[i].result, path.ToWStringHack());
initial.commit3f4a7322008-07-27 06:49:38 +09001265 }
1266}
1267
sky@google.com71e7c6f2008-09-20 02:32:18 +09001268// Make sure ReplaceExtension doesn't replace an extension that occurs as one of
1269// the directory names of the path.
1270TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
estade@chromium.org63343202008-12-05 05:46:06 +09001271 FilePath path;
1272 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
1273 path = path.Append(FILE_PATH_LITERAL("foo"));
sky@google.com71e7c6f2008-09-20 02:32:18 +09001274 // '/foo.bar/foo' with extension '.baz'
estade@chromium.org63343202008-12-05 05:46:06 +09001275 FilePath result_path = path;
1276 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001277 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
1278 result_path.value());
sky@google.com71e7c6f2008-09-20 02:32:18 +09001279}
1280
initial.commit3f4a7322008-07-27 06:49:38 +09001281TEST_F(FileUtilTest, FileEnumeratorTest) {
1282 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001283 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001284 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1285 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001286
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001287 // Test an empty directory, non-recursively, including "..".
1288 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1289 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1290 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1291 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1292 f0_dotdot.Next().value());
1293 EXPECT_EQ(FILE_PATH_LITERAL(""),
1294 f0_dotdot.Next().value());
1295
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001296 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001297 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001298 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001299 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001300 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001301 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001302 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001303
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001304 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001305 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001306 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001307 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001308 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001309 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001310 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001311 FilePath file2_rel =
1312 dir2.Append(FilePath::kParentDirectory)
1313 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001314 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001315 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001316
1317 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001318 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001319 file_util::FileEnumerator::FILES);
1320 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001321 EXPECT_TRUE(c1.HasFile(file1));
1322 EXPECT_TRUE(c1.HasFile(file2_abs));
1323 EXPECT_TRUE(c1.HasFile(dir2file));
1324 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1325 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001326
1327 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001328 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001329 file_util::FileEnumerator::DIRECTORIES);
1330 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001331 EXPECT_TRUE(c2.HasFile(dir1));
1332 EXPECT_TRUE(c2.HasFile(dir2));
1333 EXPECT_TRUE(c2.HasFile(dir2inner));
1334 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001335
tim@chromium.org989d0972008-10-16 11:42:45 +09001336 // Only enumerate directories non-recursively.
1337 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001338 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001339 FindResultCollector c2_non_recursive(f2_non_recursive);
1340 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1341 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1342 EXPECT_EQ(c2_non_recursive.size(), 2);
1343
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001344 // Only enumerate directories, non-recursively, including "..".
1345 file_util::FileEnumerator f2_dotdot(
1346 test_dir_, false,
1347 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1348 file_util::FileEnumerator::DIRECTORIES |
1349 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1350 FindResultCollector c2_dotdot(f2_dotdot);
1351 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1352 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1353 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1354 EXPECT_EQ(c2_dotdot.size(), 3);
1355
initial.commit3f4a7322008-07-27 06:49:38 +09001356 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001357 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001358 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001359 EXPECT_TRUE(c3.HasFile(dir1));
1360 EXPECT_TRUE(c3.HasFile(dir2));
1361 EXPECT_TRUE(c3.HasFile(file1));
1362 EXPECT_TRUE(c3.HasFile(file2_abs));
1363 EXPECT_TRUE(c3.HasFile(dir2file));
1364 EXPECT_TRUE(c3.HasFile(dir2inner));
1365 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1366 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001367
1368 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001369 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001370 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001371 EXPECT_TRUE(c4.HasFile(dir2));
1372 EXPECT_TRUE(c4.HasFile(dir2));
1373 EXPECT_TRUE(c4.HasFile(file1));
1374 EXPECT_TRUE(c4.HasFile(file2_abs));
1375 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001376
1377 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001378 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001379 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001380 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001381 EXPECT_TRUE(c5.HasFile(dir1));
1382 EXPECT_TRUE(c5.HasFile(dir2));
1383 EXPECT_TRUE(c5.HasFile(dir2file));
1384 EXPECT_TRUE(c5.HasFile(dir2inner));
1385 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1386 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001387
1388 // Make sure the destructor closes the find handle while in the middle of a
1389 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001390 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001391 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1392 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001393}
license.botf003cfe2008-08-24 09:55:55 +09001394
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001395TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001396 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001397
1398 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001399 if (file_util::PathExists(data_dir)) {
1400 ASSERT_TRUE(file_util::Delete(data_dir, true));
1401 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001402 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1403
1404 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1405 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1406 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1407 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1408
1409 // Annoyingly, the directories must actually exist in order for realpath(),
1410 // which Contains() relies on in posix, to work.
1411 ASSERT_TRUE(file_util::CreateDirectory(foo));
1412 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001413 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1414 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1415 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001416
1417 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1418 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1419 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1420 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1421
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001422 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001423 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1424#if defined(OS_WIN)
1425 EXPECT_TRUE(file_util::ContainsPath(foo,
1426 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001427 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001428 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001429#elif defined(OS_MACOSX)
1430 // We can't really do this test on OS X since the case-sensitivity of the
1431 // filesystem is configurable.
1432#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001433 EXPECT_FALSE(file_util::ContainsPath(foo,
1434 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001435#endif
1436}
1437
jochen@chromium.orga6879772010-02-18 19:02:26 +09001438TEST_F(FileUtilTest, LastModified) {
1439 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1440
1441 // Create a fresh, empty copy of this directory.
1442 if (file_util::PathExists(data_dir)) {
1443 ASSERT_TRUE(file_util::Delete(data_dir, true));
1444 }
1445 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1446
1447 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1448 std::string data("hello");
1449 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1450
1451 base::Time modification_time;
1452 // Note that this timestamp is divisible by two (seconds) - FAT stores
1453 // modification times with 2s resolution.
1454 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1455 &modification_time));
1456 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1457 file_util::FileInfo file_info;
1458 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1459 ASSERT_TRUE(file_info.last_modified == modification_time);
1460}
1461
mark@chromium.org17684802008-09-10 09:16:28 +09001462} // namespace