blob: 05a1aeedc09e47c3a1dc8f304fa4871074bb5802 [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
thestig@chromium.org1dad8c62010-05-08 03:58:45 +090034const wchar_t bogus_content[] = L"I'm cannon fodder.";
35
yuzo@chromium.org2da0f822009-06-09 14:57:38 +090036const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
37 static_cast<file_util::FileEnumerator::FILE_TYPE>(
38 file_util::FileEnumerator::FILES |
39 file_util::FileEnumerator::DIRECTORIES);
40
erikkay@google.comf2406842008-08-21 00:59:49 +090041// file_util winds up using autoreleased objects on the Mac, so this needs
42// to be a PlatformTest
43class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +090044 protected:
45 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +090046 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +090047 // Name a subdirectory of the temp directory.
48 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +090049 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +090050
51 // Create a fresh, empty copy of this directory.
52 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +090053 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +090054 }
55 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +090056 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +090057 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +090058 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +090059 ASSERT_FALSE(file_util::PathExists(test_dir_));
60 }
61
62 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +090063 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +090064};
65
66// Collects all the results from the given file enumerator, and provides an
67// interface to query whether a given file is present.
68class FindResultCollector {
69 public:
evan@chromium.org1543ad32009-08-27 05:00:14 +090070 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +090071 FilePath cur_file;
72 while (!(cur_file = enumerator.Next()).value().empty()) {
73 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +090074 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +090075 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +090076 << "Same file returned twice";
77
78 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +090079 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +090080 }
81 }
82
83 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +090084 bool HasFile(const FilePath& file) const {
85 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +090086 }
evanm@google.com874d1672008-10-31 08:54:04 +090087
erikkay@google.comdfb51b22008-08-16 02:32:10 +090088 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +090089 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +090090 }
initial.commit3f4a7322008-07-27 06:49:38 +090091
92 private:
evanm@google.com874d1672008-10-31 08:54:04 +090093 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +090094};
95
96// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +090097void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +090098 const std::wstring& contents) {
99 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900100 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900101 ASSERT_TRUE(file.is_open());
102 file << contents;
103 file.close();
104}
105
106// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900107std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900108 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900109 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900110 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900111 EXPECT_TRUE(file.is_open());
112 file.getline(contents, 64);
113 file.close();
114 return std::wstring(contents);
115}
116
erikkay@google.com014161d2008-08-16 02:45:13 +0900117#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900118uint64 FileTimeAsUint64(const FILETIME& ft) {
119 ULARGE_INTEGER u;
120 u.LowPart = ft.dwLowDateTime;
121 u.HighPart = ft.dwHighDateTime;
122 return u.QuadPart;
123}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900124#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900125
126const struct append_case {
127 const wchar_t* path;
128 const wchar_t* ending;
129 const wchar_t* result;
130} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900131#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900132 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
133 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
134 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
135 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
136 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
137 {L"", L"path", L"\\path"},
138 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900139#elif defined(OS_POSIX)
140 {L"/foo/bar", L"path", L"/foo/bar/path"},
141 {L"/foo/bar/", L"path", L"/foo/bar/path"},
142 {L"/foo/bar//", L"path", L"/foo/bar//path"},
143 {L"/foo/bar/", L"", L"/foo/bar/"},
144 {L"/foo/bar", L"", L"/foo/bar/"},
145 {L"", L"path", L"/path"},
146 {L"", L"", L"/"},
147#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900148};
149
evan@chromium.org1db7f942010-02-27 00:11:55 +0900150#if defined(OS_WIN)
151// This function is deprecated, but still used on Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900152TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900153 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900154 const append_case& value = append_cases[i];
155 std::wstring result = value.path;
156 file_util::AppendToPath(&result, value.ending);
157 EXPECT_EQ(value.result, result);
158 }
159
160#ifdef NDEBUG
161 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
162#endif
163}
evan@chromium.org1db7f942010-02-27 00:11:55 +0900164#endif // defined(OS_WIN)
165
initial.commit3f4a7322008-07-27 06:49:38 +0900166
167static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900168 const FilePath::CharType* path;
169 const FilePath::CharType* suffix;
170 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900171} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900172 {FPL(""), FPL(""), FPL("")},
173 {FPL(""), FPL("txt"), FPL("txt")},
174 {FPL("."), FPL("txt"), FPL("txt.")},
175 {FPL("."), FPL(""), FPL(".")},
176 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
177 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
178 {FPL("foo"), FPL("txt"), FPL("footxt")},
179 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
180 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
181 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
182 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
183 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
184 {FPL("foo"), FPL(""), FPL("foo")},
185 {FPL("foo"), FPL("."), FPL("foo.")},
186 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
187 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900188#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900189 {FPL("\\"), FPL(""), FPL("\\")},
190 {FPL("\\"), FPL("txt"), FPL("\\txt")},
191 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
192 {FPL("\\."), FPL(""), FPL("\\.")},
193 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
194 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
195 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
196 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
197 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
198 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
199 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
200 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
201 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900202#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900203 {FPL("/"), FPL(""), FPL("/")},
204 {FPL("/"), FPL("txt"), FPL("/txt")},
205 {FPL("/."), FPL("txt"), FPL("/txt.")},
206 {FPL("/."), FPL(""), FPL("/.")},
207 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
208 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
209 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
210 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
211 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
212 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
213 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
214 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900215#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900216};
217
218TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900219 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900220 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900221 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900222 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900223 }
224}
225
226static const struct filename_case {
227 const wchar_t* path;
228 const wchar_t* filename;
229} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900230#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900231 {L"c:\\colon\\backslash", L"backslash"},
232 {L"c:\\colon\\backslash\\", L""},
233 {L"\\\\filename.exe", L"filename.exe"},
234 {L"filename.exe", L"filename.exe"},
235 {L"", L""},
236 {L"\\\\\\", L""},
237 {L"c:/colon/backslash", L"backslash"},
238 {L"c:/colon/backslash/", L""},
239 {L"//////", L""},
240 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900241#elif defined(OS_POSIX)
242 {L"/foo/bar", L"bar"},
243 {L"/foo/bar/", L""},
244 {L"/filename.exe", L"filename.exe"},
245 {L"filename.exe", L"filename.exe"},
246 {L"", L""},
247 {L"/", L""},
248#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900249};
250
251TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900252 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900253 const filename_case& value = filename_cases[i];
254 std::wstring result = file_util::GetFilenameFromPath(value.path);
255 EXPECT_EQ(value.filename, result);
256 }
257}
258
259// Test finding the file type from a path name
260static const struct extension_case {
261 const wchar_t* path;
262 const wchar_t* extension;
263} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900264#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900265 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
266 {L"C:\\colon\\backslash\\filename.", L""},
267 {L"C:\\colon\\backslash\\filename", L""},
268 {L"C:\\colon\\backslash\\", L""},
269 {L"C:\\colon\\backslash.\\", L""},
270 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900271#elif defined(OS_POSIX)
272 {L"/foo/bar/filename.extension", L"extension"},
273 {L"/foo/bar/filename.", L""},
274 {L"/foo/bar/filename", L""},
275 {L"/foo/bar/", L""},
276 {L"/foo/bar./", L""},
277 {L"/foo/bar/filename.extension.extension2", L"extension2"},
278 {L".", L""},
279 {L"..", L""},
280 {L"./foo", L""},
281 {L"./foo.extension", L"extension"},
282 {L"/foo.extension1/bar.extension2", L"extension2"},
283#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900284};
285
286TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900287 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900288 const extension_case& ext = extension_cases[i];
289 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
290 EXPECT_EQ(ext.extension, fext);
291 }
292}
293
294// Test finding the directory component of a path
295static const struct dir_case {
296 const wchar_t* full_path;
297 const wchar_t* directory;
298} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900299#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900300 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
301 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
302 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
303 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
304 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
305 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900306 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900307#elif defined(OS_POSIX)
308 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
309 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
310 {L"/foo/bar/", L"/foo/bar"},
311 {L"/foo/bar//", L"/foo/bar"},
312 {L"/foo/bar", L"/foo"},
313 {L"/foo/bar./", L"/foo/bar."},
314 {L"/", L"/"},
315 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900316 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900317#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900318};
319
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900320#if defined(OS_WIN)
321// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900322TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900323 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900324 const dir_case& dir = dir_cases[i];
325 const std::wstring parent =
326 file_util::GetDirectoryFromPath(dir.full_path);
327 EXPECT_EQ(dir.directory, parent);
328 }
329}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900330#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900331
332TEST_F(FileUtilTest, CountFilesCreatedAfter) {
333 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900334 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900335 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
336
337 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900338#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900339 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900340#elif defined(OS_POSIX)
341 // We need to wait at least one second here because the precision of
342 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900343 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900344#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900345
346 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900347 base::Time now(base::Time::NowFromSystemTime());
348 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900349
350 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900351 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900352 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
353
354 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900355 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900356
357 // Delete new file, we should see no files after cutoff now
358 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900359 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900360}
361
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900362TEST_F(FileUtilTest, FileAndDirectorySize) {
363 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
364 // should return 53 bytes.
365 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
366 CreateTextFile(file_01, L"12345678901234567890");
367 int64 size_f1 = 0;
368 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
369 EXPECT_EQ(20ll, size_f1);
370
371 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
372 file_util::CreateDirectory(subdir_path);
373
374 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
375 CreateTextFile(file_02, L"123456789012345678901234567890");
376 int64 size_f2 = 0;
377 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
378 EXPECT_EQ(30ll, size_f2);
379
380 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
381 file_util::CreateDirectory(subsubdir_path);
382
383 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
384 CreateTextFile(file_03, L"123");
385
386 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
387 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
388}
389
skerner@chromium.org559baa92010-05-13 00:13:57 +0900390#if defined(OS_POSIX)
391TEST_F(FileUtilTest, RealPath) {
392 // Get the real test directory, in case some future change to the
393 // test setup makes the path to test_dir_ include a symlink.
394 FilePath real_test_dir;
395 ASSERT_TRUE(file_util::RealPath(test_dir_, &real_test_dir));
396
397 FilePath real_path;
398 ASSERT_TRUE(file_util::RealPath(real_test_dir, &real_path));
399 ASSERT_TRUE(real_test_dir == real_path);
400
401 // Link one file to another.
402 FilePath link_from = real_test_dir.Append(FPL("from_file"));
403 FilePath link_to = real_test_dir.Append(FPL("to_file"));
404 CreateTextFile(link_to, bogus_content);
405
406 ASSERT_EQ(0, symlink(link_to.value().c_str(), link_from.value().c_str()))
407 << "Failed to create file symlink.";
408
409 // Check that RealPath sees the link.
410 ASSERT_TRUE(file_util::RealPath(link_from, &real_path));
411 ASSERT_TRUE(link_to != link_from);
412 ASSERT_TRUE(link_to == real_path);
413
414
415 // Link to a directory.
416 link_from = real_test_dir.Append(FPL("from_dir"));
417 link_to = real_test_dir.Append(FPL("to_dir"));
418 file_util::CreateDirectory(link_to);
419
420 ASSERT_EQ(0, symlink(link_to.value().c_str(), link_from.value().c_str()))
421 << "Failed to create directory symlink.";
422
423 ASSERT_TRUE(file_util::RealPath(link_from, &real_path));
424 ASSERT_TRUE(link_to != link_from);
425 ASSERT_TRUE(link_to == real_path);
426
427
428 // Test that a loop in the links causes RealPath() to return false.
429 link_from = real_test_dir.Append(FPL("link_a"));
430 link_to = real_test_dir.Append(FPL("link_b"));
431 ASSERT_EQ(0, symlink(link_to.value().c_str(), link_from.value().c_str()))
432 << "Failed to create loop symlink a.";
433 ASSERT_EQ(0, symlink(link_from.value().c_str(), link_to.value().c_str()))
434 << "Failed to create loop symlink b.";
435
436 // Infinite loop!
437 ASSERT_FALSE(file_util::RealPath(link_from, &real_path));
438}
439#endif // defined(OS_POSIX)
440
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900441TEST_F(FileUtilTest, DeleteNonExistent) {
442 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
443 ASSERT_FALSE(file_util::PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900444
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900445 EXPECT_TRUE(file_util::Delete(non_existent, false));
446 ASSERT_FALSE(file_util::PathExists(non_existent));
447 EXPECT_TRUE(file_util::Delete(non_existent, true));
448 ASSERT_FALSE(file_util::PathExists(non_existent));
449}
450
451TEST_F(FileUtilTest, DeleteFile) {
452 // Create a file
453 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
454 CreateTextFile(file_name, bogus_content);
initial.commit3f4a7322008-07-27 06:49:38 +0900455 ASSERT_TRUE(file_util::PathExists(file_name));
456
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900457 // Make sure it's deleted
458 EXPECT_TRUE(file_util::Delete(file_name, false));
459 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900460
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900461 // Test recursive case, create a new file
462 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
463 CreateTextFile(file_name, bogus_content);
464 ASSERT_TRUE(file_util::PathExists(file_name));
465
466 // Make sure it's deleted
467 EXPECT_TRUE(file_util::Delete(file_name, true));
468 EXPECT_FALSE(file_util::PathExists(file_name));
469}
470
471#if defined(OS_WIN)
472// Tests that the Delete function works for wild cards, especially
473// with the recursion flag. Also coincidentally tests PathExists.
474// TODO(erikkay): see if anyone's actually using this feature of the API
475TEST_F(FileUtilTest, DeleteWildCard) {
476 // Create a file and a directory
477 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
478 CreateTextFile(file_name, bogus_content);
479 ASSERT_TRUE(file_util::PathExists(file_name));
480
481 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
482 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900483 ASSERT_TRUE(file_util::PathExists(subdir_path));
484
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900485 // Create the wildcard path
evanm@google.com874d1672008-10-31 08:54:04 +0900486 FilePath directory_contents = test_dir_;
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900487 directory_contents = directory_contents.Append(FPL("*"));
488
initial.commit3f4a7322008-07-27 06:49:38 +0900489 // Delete non-recursively and check that only the file is deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900490 EXPECT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900491 EXPECT_FALSE(file_util::PathExists(file_name));
492 EXPECT_TRUE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900493
zork@chromium.org61be4f42010-05-07 09:05:36 +0900494 // Delete recursively and make sure all contents are deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900495 EXPECT_TRUE(file_util::Delete(directory_contents, true));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900496 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900497 EXPECT_FALSE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900498}
499
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900500// TODO(erikkay): see if anyone's actually using this feature of the API
501TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
502 // Create a file and a directory
503 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
504 file_util::CreateDirectory(subdir_path);
505 ASSERT_TRUE(file_util::PathExists(subdir_path));
506
507 // Create the wildcard path
508 FilePath directory_contents = subdir_path;
509 directory_contents = directory_contents.Append(FPL("*"));
510
511 // Delete non-recursively and check nothing got deleted
512 EXPECT_TRUE(file_util::Delete(directory_contents, false));
513 EXPECT_TRUE(file_util::PathExists(subdir_path));
514
515 // Delete recursively and check nothing got deleted
516 EXPECT_TRUE(file_util::Delete(directory_contents, true));
517 EXPECT_TRUE(file_util::PathExists(subdir_path));
518}
519#endif
520
521// Tests non-recursive Delete() for a directory.
522TEST_F(FileUtilTest, DeleteDirNonRecursive) {
523 // Create a subdirectory and put a file and two directories inside.
524 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
525 file_util::CreateDirectory(test_subdir);
526 ASSERT_TRUE(file_util::PathExists(test_subdir));
527
528 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
529 CreateTextFile(file_name, bogus_content);
530 ASSERT_TRUE(file_util::PathExists(file_name));
531
532 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
533 file_util::CreateDirectory(subdir_path1);
534 ASSERT_TRUE(file_util::PathExists(subdir_path1));
535
536 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
537 file_util::CreateDirectory(subdir_path2);
538 ASSERT_TRUE(file_util::PathExists(subdir_path2));
539
540 // Delete non-recursively and check that the empty dir got deleted
541 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
542 EXPECT_FALSE(file_util::PathExists(subdir_path2));
543
544 // Delete non-recursively and check that nothing got deleted
545 EXPECT_FALSE(file_util::Delete(test_subdir, false));
546 EXPECT_TRUE(file_util::PathExists(test_subdir));
547 EXPECT_TRUE(file_util::PathExists(file_name));
548 EXPECT_TRUE(file_util::PathExists(subdir_path1));
549}
550
551// Tests recursive Delete() for a directory.
552TEST_F(FileUtilTest, DeleteDirRecursive) {
553 // Create a subdirectory and put a file and two directories inside.
554 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
555 file_util::CreateDirectory(test_subdir);
556 ASSERT_TRUE(file_util::PathExists(test_subdir));
557
558 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
559 CreateTextFile(file_name, bogus_content);
560 ASSERT_TRUE(file_util::PathExists(file_name));
561
562 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
563 file_util::CreateDirectory(subdir_path1);
564 ASSERT_TRUE(file_util::PathExists(subdir_path1));
565
566 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
567 file_util::CreateDirectory(subdir_path2);
568 ASSERT_TRUE(file_util::PathExists(subdir_path2));
569
570 // Delete recursively and check that the empty dir got deleted
571 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
572 EXPECT_FALSE(file_util::PathExists(subdir_path2));
573
574 // Delete recursively and check that everything got deleted
575 EXPECT_TRUE(file_util::Delete(test_subdir, true));
576 EXPECT_FALSE(file_util::PathExists(file_name));
577 EXPECT_FALSE(file_util::PathExists(subdir_path1));
578 EXPECT_FALSE(file_util::PathExists(test_subdir));
579}
580
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900581TEST_F(FileUtilTest, MoveFileNew) {
582 // Create a file
583 FilePath file_name_from =
584 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
585 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
586 ASSERT_TRUE(file_util::PathExists(file_name_from));
587
588 // The destination
589 FilePath file_name_to =
590 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
591 ASSERT_FALSE(file_util::PathExists(file_name_to));
592
593 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
594
595 // Check everything has been moved.
596 EXPECT_FALSE(file_util::PathExists(file_name_from));
597 EXPECT_TRUE(file_util::PathExists(file_name_to));
598}
599
600TEST_F(FileUtilTest, MoveFileExists) {
601 // Create a file
602 FilePath file_name_from =
603 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
604 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
605 ASSERT_TRUE(file_util::PathExists(file_name_from));
606
607 // The destination name
608 FilePath file_name_to =
609 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
610 CreateTextFile(file_name_to, L"Old file content");
611 ASSERT_TRUE(file_util::PathExists(file_name_to));
612
613 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
614
615 // Check everything has been moved.
616 EXPECT_FALSE(file_util::PathExists(file_name_from));
617 EXPECT_TRUE(file_util::PathExists(file_name_to));
618 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
619}
620
621TEST_F(FileUtilTest, MoveFileDirExists) {
622 // Create a file
623 FilePath file_name_from =
624 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
625 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
626 ASSERT_TRUE(file_util::PathExists(file_name_from));
627
628 // The destination directory
629 FilePath dir_name_to =
630 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
631 file_util::CreateDirectory(dir_name_to);
632 ASSERT_TRUE(file_util::PathExists(dir_name_to));
633
634 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
635}
636
637
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900638TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900639 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900640 FilePath dir_name_from =
641 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
642 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900643 ASSERT_TRUE(file_util::PathExists(dir_name_from));
644
645 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900646 FilePath file_name_from =
647 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900648 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
649 ASSERT_TRUE(file_util::PathExists(file_name_from));
650
651 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900652 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
653 FilePath file_name_to =
654 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900655
656 ASSERT_FALSE(file_util::PathExists(dir_name_to));
657
658 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
659
660 // Check everything has been moved.
661 EXPECT_FALSE(file_util::PathExists(dir_name_from));
662 EXPECT_FALSE(file_util::PathExists(file_name_from));
663 EXPECT_TRUE(file_util::PathExists(dir_name_to));
664 EXPECT_TRUE(file_util::PathExists(file_name_to));
665}
666
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900667TEST_F(FileUtilTest, MoveExist) {
668 // Create a directory
669 FilePath dir_name_from =
670 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
671 file_util::CreateDirectory(dir_name_from);
672 ASSERT_TRUE(file_util::PathExists(dir_name_from));
673
674 // Create a file under the directory
675 FilePath file_name_from =
676 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
677 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
678 ASSERT_TRUE(file_util::PathExists(file_name_from));
679
680 // Move the directory
681 FilePath dir_name_exists =
682 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
683
684 FilePath dir_name_to =
685 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
686 FilePath file_name_to =
687 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
688
689 // Create the destination directory.
690 file_util::CreateDirectory(dir_name_exists);
691 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
692
693 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
694
695 // Check everything has been moved.
696 EXPECT_FALSE(file_util::PathExists(dir_name_from));
697 EXPECT_FALSE(file_util::PathExists(file_name_from));
698 EXPECT_TRUE(file_util::PathExists(dir_name_to));
699 EXPECT_TRUE(file_util::PathExists(file_name_to));
700}
701
702TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900703 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900704 FilePath dir_name_from =
705 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
706 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900707 ASSERT_TRUE(file_util::PathExists(dir_name_from));
708
709 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900710 FilePath file_name_from =
711 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900712 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
713 ASSERT_TRUE(file_util::PathExists(file_name_from));
714
715 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900716 FilePath subdir_name_from =
717 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
718 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900719 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
720
721 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900722 FilePath file_name2_from =
723 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900724 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
725 ASSERT_TRUE(file_util::PathExists(file_name2_from));
726
727 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900728 FilePath dir_name_to =
729 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
730 FilePath file_name_to =
731 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
732 FilePath subdir_name_to =
733 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
734 FilePath file_name2_to =
735 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900736
737 ASSERT_FALSE(file_util::PathExists(dir_name_to));
738
739 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
740
741 // Check everything has been copied.
742 EXPECT_TRUE(file_util::PathExists(dir_name_from));
743 EXPECT_TRUE(file_util::PathExists(file_name_from));
744 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
745 EXPECT_TRUE(file_util::PathExists(file_name2_from));
746 EXPECT_TRUE(file_util::PathExists(dir_name_to));
747 EXPECT_TRUE(file_util::PathExists(file_name_to));
748 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
749 EXPECT_TRUE(file_util::PathExists(file_name2_to));
750}
751
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900752TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
753 // Create a directory.
754 FilePath dir_name_from =
755 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
756 file_util::CreateDirectory(dir_name_from);
757 ASSERT_TRUE(file_util::PathExists(dir_name_from));
758
759 // Create a file under the directory.
760 FilePath file_name_from =
761 dir_name_from.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 // Create a subdirectory.
766 FilePath subdir_name_from =
767 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
768 file_util::CreateDirectory(subdir_name_from);
769 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
770
771 // Create a file under the subdirectory.
772 FilePath file_name2_from =
773 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
774 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
775 ASSERT_TRUE(file_util::PathExists(file_name2_from));
776
777 // Copy the directory recursively.
778 FilePath dir_name_exists =
779 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
780
781 FilePath dir_name_to =
782 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
783 FilePath file_name_to =
784 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
785 FilePath subdir_name_to =
786 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
787 FilePath file_name2_to =
788 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
789
790 // Create the destination directory.
791 file_util::CreateDirectory(dir_name_exists);
792 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
793
794 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
795
796 // Check everything has been copied.
797 EXPECT_TRUE(file_util::PathExists(dir_name_from));
798 EXPECT_TRUE(file_util::PathExists(file_name_from));
799 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
800 EXPECT_TRUE(file_util::PathExists(file_name2_from));
801 EXPECT_TRUE(file_util::PathExists(dir_name_to));
802 EXPECT_TRUE(file_util::PathExists(file_name_to));
803 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
804 EXPECT_TRUE(file_util::PathExists(file_name2_to));
805}
806
807TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900808 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900809 FilePath dir_name_from =
810 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
811 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900812 ASSERT_TRUE(file_util::PathExists(dir_name_from));
813
814 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900815 FilePath file_name_from =
816 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900817 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
818 ASSERT_TRUE(file_util::PathExists(file_name_from));
819
820 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900821 FilePath subdir_name_from =
822 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
823 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900824 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
825
826 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900827 FilePath file_name2_from =
828 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900829 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
830 ASSERT_TRUE(file_util::PathExists(file_name2_from));
831
832 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900833 FilePath dir_name_to =
834 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
835 FilePath file_name_to =
836 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
837 FilePath subdir_name_to =
838 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +0900839
840 ASSERT_FALSE(file_util::PathExists(dir_name_to));
841
842 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
843
844 // Check everything has been copied.
845 EXPECT_TRUE(file_util::PathExists(dir_name_from));
846 EXPECT_TRUE(file_util::PathExists(file_name_from));
847 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
848 EXPECT_TRUE(file_util::PathExists(file_name2_from));
849 EXPECT_TRUE(file_util::PathExists(dir_name_to));
850 EXPECT_TRUE(file_util::PathExists(file_name_to));
851 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
852}
853
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900854TEST_F(FileUtilTest, CopyDirectoryExists) {
855 // Create a directory.
856 FilePath dir_name_from =
857 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
858 file_util::CreateDirectory(dir_name_from);
859 ASSERT_TRUE(file_util::PathExists(dir_name_from));
860
861 // Create a file under the directory.
862 FilePath file_name_from =
863 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
864 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
865 ASSERT_TRUE(file_util::PathExists(file_name_from));
866
867 // Create a subdirectory.
868 FilePath subdir_name_from =
869 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
870 file_util::CreateDirectory(subdir_name_from);
871 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
872
873 // Create a file under the subdirectory.
874 FilePath file_name2_from =
875 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
876 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
877 ASSERT_TRUE(file_util::PathExists(file_name2_from));
878
879 // Copy the directory not recursively.
880 FilePath dir_name_to =
881 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
882 FilePath file_name_to =
883 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
884 FilePath subdir_name_to =
885 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
886
887 // Create the destination directory.
888 file_util::CreateDirectory(dir_name_to);
889 ASSERT_TRUE(file_util::PathExists(dir_name_to));
890
891 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
892
893 // Check everything has been copied.
894 EXPECT_TRUE(file_util::PathExists(dir_name_from));
895 EXPECT_TRUE(file_util::PathExists(file_name_from));
896 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
897 EXPECT_TRUE(file_util::PathExists(file_name2_from));
898 EXPECT_TRUE(file_util::PathExists(dir_name_to));
899 EXPECT_TRUE(file_util::PathExists(file_name_to));
900 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
901}
902
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900903TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
904 // Create a file
905 FilePath file_name_from =
906 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
907 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
908 ASSERT_TRUE(file_util::PathExists(file_name_from));
909
910 // The destination name
911 FilePath file_name_to =
912 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
913 ASSERT_FALSE(file_util::PathExists(file_name_to));
914
915 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
916
917 // Check the has been copied
918 EXPECT_TRUE(file_util::PathExists(file_name_to));
919}
920
921TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
922 // Create a file
923 FilePath file_name_from =
924 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
925 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
926 ASSERT_TRUE(file_util::PathExists(file_name_from));
927
928 // The destination name
929 FilePath file_name_to =
930 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
931 CreateTextFile(file_name_to, L"Old file content");
932 ASSERT_TRUE(file_util::PathExists(file_name_to));
933
934 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
935
936 // Check the has been copied
937 EXPECT_TRUE(file_util::PathExists(file_name_to));
938 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
939}
940
941TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
942 // Create a file
943 FilePath file_name_from =
944 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
945 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
946 ASSERT_TRUE(file_util::PathExists(file_name_from));
947
948 // The destination
949 FilePath dir_name_to =
950 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
951 file_util::CreateDirectory(dir_name_to);
952 ASSERT_TRUE(file_util::PathExists(dir_name_to));
953 FilePath file_name_to =
954 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
955
956 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
957
958 // Check the has been copied
959 EXPECT_TRUE(file_util::PathExists(file_name_to));
960}
961
initial.commit3f4a7322008-07-27 06:49:38 +0900962TEST_F(FileUtilTest, CopyFile) {
963 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900964 FilePath dir_name_from =
965 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
966 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900967 ASSERT_TRUE(file_util::PathExists(dir_name_from));
968
969 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900970 FilePath file_name_from =
971 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900972 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
973 CreateTextFile(file_name_from, file_contents);
974 ASSERT_TRUE(file_util::PathExists(file_name_from));
975
976 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900977 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900978 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +0900979
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900980 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +0900981 FilePath dest_file2(dir_name_from);
982 dest_file2 = dest_file2.AppendASCII("..");
983 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
984 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
985
986 FilePath dest_file2_test(dir_name_from);
987 dest_file2_test = dest_file2_test.DirName();
988 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +0900989
990 // Check everything has been copied.
991 EXPECT_TRUE(file_util::PathExists(file_name_from));
992 EXPECT_TRUE(file_util::PathExists(dest_file));
993 const std::wstring read_contents = ReadTextFile(dest_file);
994 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +0900995 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
996 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +0900997}
998
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900999// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +09001000#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001001TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +09001002 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001003
1004 SYSTEMTIME start_time;
1005 GetLocalTime(&start_time);
1006 Sleep(100);
1007 CreateTextFile(file_name, L"New file!");
1008 Sleep(100);
1009 SYSTEMTIME end_time;
1010 GetLocalTime(&end_time);
1011
1012 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +09001013 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +09001014
1015 FILETIME start_filetime;
1016 SystemTimeToFileTime(&start_time, &start_filetime);
1017 FILETIME end_filetime;
1018 SystemTimeToFileTime(&end_time, &end_filetime);
1019 FILETIME file_creation_filetime;
1020 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1021
1022 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1023 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1024 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1025
1026 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1027 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1028 "end time: " << FileTimeAsUint64(end_filetime);
1029
evanm@google.com874d1672008-10-31 08:54:04 +09001030 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +09001031}
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001032#endif
initial.commit3f4a7322008-07-27 06:49:38 +09001033
erikkay@google.comf2406842008-08-21 00:59:49 +09001034// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001035// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001036typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001037
erikkay@google.comf2406842008-08-21 00:59:49 +09001038TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001039 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +09001040 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +09001041 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1042 .Append(FILE_PATH_LITERAL("data"))
1043 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +09001044 ASSERT_TRUE(file_util::PathExists(data_dir));
1045
evanm@google.com874d1672008-10-31 08:54:04 +09001046 FilePath original_file =
1047 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1048 FilePath same_file =
1049 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1050 FilePath same_length_file =
1051 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1052 FilePath different_file =
1053 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1054 FilePath different_first_file =
1055 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1056 FilePath different_last_file =
1057 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1058 FilePath empty1_file =
1059 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1060 FilePath empty2_file =
1061 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1062 FilePath shortened_file =
1063 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1064 FilePath binary_file =
1065 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1066 FilePath binary_file_same =
1067 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1068 FilePath binary_file_diff =
1069 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001070
1071 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1072 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1073 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1074 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +09001075 EXPECT_FALSE(file_util::ContentsEqual(
1076 FilePath(FILE_PATH_LITERAL("bogusname")),
1077 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +09001078 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1079 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1080 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1081 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1082 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1083 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1084 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1085}
1086
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001087TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1088 FilePath data_dir;
1089 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1090 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1091 .Append(FILE_PATH_LITERAL("data"))
1092 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1093 ASSERT_TRUE(file_util::PathExists(data_dir));
1094
1095 FilePath original_file =
1096 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1097 FilePath same_file =
1098 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1099 FilePath crlf_file =
1100 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1101 FilePath shortened_file =
1102 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1103 FilePath different_file =
1104 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1105 FilePath different_first_file =
1106 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1107 FilePath different_last_file =
1108 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1109 FilePath first1_file =
1110 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1111 FilePath first2_file =
1112 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1113 FilePath empty1_file =
1114 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1115 FilePath empty2_file =
1116 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1117 FilePath blank_line_file =
1118 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1119 FilePath blank_line_crlf_file =
1120 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1121
1122 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1123 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1124 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1125 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1126 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1127 different_first_file));
1128 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1129 different_last_file));
1130 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1131 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1132 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1133 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1134 blank_line_crlf_file));
1135}
1136
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001137// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001138#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001139TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001140 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001141 CreateTextFile(target_file, L"This is the target.");
1142
evanm@google.com874d1672008-10-31 08:54:04 +09001143 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001144
1145 HRESULT result;
1146 IShellLink *shell = NULL;
1147 IPersistFile *persist = NULL;
1148
1149 CoInitialize(NULL);
1150 // Temporarily create a shortcut for test
1151 result = CoCreateInstance(CLSID_ShellLink, NULL,
1152 CLSCTX_INPROC_SERVER, IID_IShellLink,
1153 reinterpret_cast<LPVOID*>(&shell));
1154 EXPECT_TRUE(SUCCEEDED(result));
1155 result = shell->QueryInterface(IID_IPersistFile,
1156 reinterpret_cast<LPVOID*>(&persist));
1157 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001158 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001159 EXPECT_TRUE(SUCCEEDED(result));
1160 result = shell->SetDescription(L"ResolveShortcutTest");
1161 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001162 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001163 EXPECT_TRUE(SUCCEEDED(result));
1164 if (persist)
1165 persist->Release();
1166 if (shell)
1167 shell->Release();
1168
1169 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001170 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001171 EXPECT_TRUE(is_solved);
1172 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001173 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001174 EXPECT_EQ(L"This is the target.", contents);
1175
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001176 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001177 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001178 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001179 CoUninitialize();
1180}
1181
1182TEST_F(FileUtilTest, CreateShortcutTest) {
1183 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001184 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001185 CreateTextFile(target_file, file_contents);
1186
evanm@google.com874d1672008-10-31 08:54:04 +09001187 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001188
1189 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001190 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1191 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001192 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001193 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001194 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001195 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001196 EXPECT_EQ(file_contents, read_contents);
1197
evanm@google.com874d1672008-10-31 08:54:04 +09001198 DeleteFile(target_file.value().c_str());
1199 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001200 CoUninitialize();
1201}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001202
1203TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1204 // Create a directory
1205 FilePath dir_name_from =
1206 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1207 file_util::CreateDirectory(dir_name_from);
1208 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1209
1210 // Create a file under the directory
1211 FilePath file_name_from =
1212 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1213 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1214 ASSERT_TRUE(file_util::PathExists(file_name_from));
1215
1216 // Move the directory by using CopyAndDeleteDirectory
1217 FilePath dir_name_to = test_dir_.Append(
1218 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1219 FilePath file_name_to =
1220 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1221
1222 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1223
1224 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1225
1226 // Check everything has been moved.
1227 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1228 EXPECT_FALSE(file_util::PathExists(file_name_from));
1229 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1230 EXPECT_TRUE(file_util::PathExists(file_name_to));
1231}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001232
1233TEST_F(FileUtilTest, GetTempDirTest) {
1234 static const TCHAR* kTmpKey = _T("TMP");
1235 static const TCHAR* kTmpValues[] = {
1236 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1237 };
1238 // Save the original $TMP.
1239 size_t original_tmp_size;
1240 TCHAR* original_tmp;
1241 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1242 // original_tmp may be NULL.
1243
1244 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1245 FilePath path;
1246 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1247 file_util::GetTempDir(&path);
1248 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1249 " result=" << path.value();
1250 }
1251
1252 // Restore the original $TMP.
1253 if (original_tmp) {
1254 ::_tputenv_s(kTmpKey, original_tmp);
1255 free(original_tmp);
1256 } else {
1257 ::_tputenv_s(kTmpKey, _T(""));
1258 }
1259}
1260#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001261
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001262TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1263 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001264 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001265 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001266 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1267 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1268 }
1269 for (int i = 0; i < 3; i++)
1270 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1271 for (int i = 0; i < 3; i++)
1272 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1273}
1274
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001275TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001276 FilePath names[3];
1277 FILE *fps[3];
1278 int i;
1279
1280 // Create; make sure they are open and exist.
1281 for (i = 0; i < 3; ++i) {
1282 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1283 ASSERT_TRUE(fps[i]);
1284 EXPECT_TRUE(file_util::PathExists(names[i]));
1285 }
1286
1287 // Make sure all names are unique.
1288 for (i = 0; i < 3; ++i) {
1289 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1290 }
1291
1292 // Close and delete.
1293 for (i = 0; i < 3; ++i) {
1294 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1295 EXPECT_TRUE(file_util::Delete(names[i], false));
1296 }
initial.commit3f4a7322008-07-27 06:49:38 +09001297}
1298
1299TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001300 FilePath temp_dir;
1301 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1302 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001303 EXPECT_TRUE(file_util::PathExists(temp_dir));
1304 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001305}
1306
skerner@chromium.orge4432392010-05-01 02:00:09 +09001307TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1308 FilePath new_dir;
1309 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1310 test_dir_,
1311 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1312 &new_dir));
1313 EXPECT_TRUE(file_util::PathExists(new_dir));
1314 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1315 EXPECT_TRUE(file_util::Delete(new_dir, false));
1316}
1317
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001318TEST_F(FileUtilTest, GetShmemTempDirTest) {
1319 FilePath dir;
1320 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1321 EXPECT_TRUE(file_util::DirectoryExists(dir));
1322}
1323
initial.commit3f4a7322008-07-27 06:49:38 +09001324TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001325 FilePath test_root =
1326 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001327#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001328 FilePath test_path =
1329 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001330#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001331 FilePath test_path =
1332 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001333#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001334
1335 EXPECT_FALSE(file_util::PathExists(test_path));
1336 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1337 EXPECT_TRUE(file_util::PathExists(test_path));
1338 // CreateDirectory returns true if the DirectoryExists returns true.
1339 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1340
1341 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001342 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001343 EXPECT_FALSE(file_util::PathExists(test_path));
1344 CreateTextFile(test_path, L"test file");
1345 EXPECT_TRUE(file_util::PathExists(test_path));
1346 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1347
1348 EXPECT_TRUE(file_util::Delete(test_root, true));
1349 EXPECT_FALSE(file_util::PathExists(test_root));
1350 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001351
1352 // Verify assumptions made by the Windows implementation:
1353 // 1. The current directory always exists.
1354 // 2. The root directory always exists.
1355 ASSERT_TRUE(file_util::DirectoryExists(
1356 FilePath(FilePath::kCurrentDirectory)));
1357 FilePath top_level = test_root;
1358 while (top_level != top_level.DirName()) {
1359 top_level = top_level.DirName();
1360 }
1361 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1362
1363 // Given these assumptions hold, it should be safe to
1364 // test that "creating" these directories succeeds.
1365 EXPECT_TRUE(file_util::CreateDirectory(
1366 FilePath(FilePath::kCurrentDirectory)));
1367 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001368
1369#if defined(OS_WIN)
1370 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1371 FilePath invalid_path =
1372 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1373 if (!file_util::PathExists(invalid_drive)) {
1374 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1375 }
1376#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001377}
1378
1379TEST_F(FileUtilTest, DetectDirectoryTest) {
1380 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001381 FilePath test_root =
1382 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001383 EXPECT_FALSE(file_util::PathExists(test_root));
1384 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1385 EXPECT_TRUE(file_util::PathExists(test_root));
1386 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1387
1388 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001389 FilePath test_path =
1390 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001391 EXPECT_FALSE(file_util::PathExists(test_path));
1392 CreateTextFile(test_path, L"test file");
1393 EXPECT_TRUE(file_util::PathExists(test_path));
1394 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1395 EXPECT_TRUE(file_util::Delete(test_path, false));
1396
1397 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001398}
1399
initial.commit3f4a7322008-07-27 06:49:38 +09001400TEST_F(FileUtilTest, FileEnumeratorTest) {
1401 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001402 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001403 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1404 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001405
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001406 // Test an empty directory, non-recursively, including "..".
1407 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1408 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1409 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1410 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1411 f0_dotdot.Next().value());
1412 EXPECT_EQ(FILE_PATH_LITERAL(""),
1413 f0_dotdot.Next().value());
1414
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001415 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001416 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001417 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001418 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001419 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001420 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001421 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001422
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001423 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001424 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001425 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001426 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001427 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001428 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001429 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001430 FilePath file2_rel =
1431 dir2.Append(FilePath::kParentDirectory)
1432 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001433 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001434 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001435
1436 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001437 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001438 file_util::FileEnumerator::FILES);
1439 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001440 EXPECT_TRUE(c1.HasFile(file1));
1441 EXPECT_TRUE(c1.HasFile(file2_abs));
1442 EXPECT_TRUE(c1.HasFile(dir2file));
1443 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1444 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001445
1446 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001447 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001448 file_util::FileEnumerator::DIRECTORIES);
1449 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001450 EXPECT_TRUE(c2.HasFile(dir1));
1451 EXPECT_TRUE(c2.HasFile(dir2));
1452 EXPECT_TRUE(c2.HasFile(dir2inner));
1453 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001454
tim@chromium.org989d0972008-10-16 11:42:45 +09001455 // Only enumerate directories non-recursively.
1456 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001457 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001458 FindResultCollector c2_non_recursive(f2_non_recursive);
1459 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1460 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1461 EXPECT_EQ(c2_non_recursive.size(), 2);
1462
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001463 // Only enumerate directories, non-recursively, including "..".
1464 file_util::FileEnumerator f2_dotdot(
1465 test_dir_, false,
1466 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1467 file_util::FileEnumerator::DIRECTORIES |
1468 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1469 FindResultCollector c2_dotdot(f2_dotdot);
1470 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1471 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1472 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1473 EXPECT_EQ(c2_dotdot.size(), 3);
1474
initial.commit3f4a7322008-07-27 06:49:38 +09001475 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001476 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001477 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001478 EXPECT_TRUE(c3.HasFile(dir1));
1479 EXPECT_TRUE(c3.HasFile(dir2));
1480 EXPECT_TRUE(c3.HasFile(file1));
1481 EXPECT_TRUE(c3.HasFile(file2_abs));
1482 EXPECT_TRUE(c3.HasFile(dir2file));
1483 EXPECT_TRUE(c3.HasFile(dir2inner));
1484 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1485 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001486
1487 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001488 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001489 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001490 EXPECT_TRUE(c4.HasFile(dir2));
1491 EXPECT_TRUE(c4.HasFile(dir2));
1492 EXPECT_TRUE(c4.HasFile(file1));
1493 EXPECT_TRUE(c4.HasFile(file2_abs));
1494 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001495
1496 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001497 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001498 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001499 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001500 EXPECT_TRUE(c5.HasFile(dir1));
1501 EXPECT_TRUE(c5.HasFile(dir2));
1502 EXPECT_TRUE(c5.HasFile(dir2file));
1503 EXPECT_TRUE(c5.HasFile(dir2inner));
1504 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1505 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001506
1507 // Make sure the destructor closes the find handle while in the middle of a
1508 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001509 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001510 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1511 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001512}
license.botf003cfe2008-08-24 09:55:55 +09001513
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001514TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001515 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001516
1517 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001518 if (file_util::PathExists(data_dir)) {
1519 ASSERT_TRUE(file_util::Delete(data_dir, true));
1520 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001521 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1522
1523 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1524 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1525 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1526 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1527
1528 // Annoyingly, the directories must actually exist in order for realpath(),
1529 // which Contains() relies on in posix, to work.
1530 ASSERT_TRUE(file_util::CreateDirectory(foo));
1531 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001532 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1533 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1534 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001535
1536 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1537 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1538 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1539 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1540
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001541 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001542 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1543#if defined(OS_WIN)
1544 EXPECT_TRUE(file_util::ContainsPath(foo,
1545 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001546 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001547 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001548#elif defined(OS_MACOSX)
1549 // We can't really do this test on OS X since the case-sensitivity of the
1550 // filesystem is configurable.
1551#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001552 EXPECT_FALSE(file_util::ContainsPath(foo,
1553 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001554#endif
1555}
1556
jochen@chromium.orga6879772010-02-18 19:02:26 +09001557TEST_F(FileUtilTest, LastModified) {
1558 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1559
1560 // Create a fresh, empty copy of this directory.
1561 if (file_util::PathExists(data_dir)) {
1562 ASSERT_TRUE(file_util::Delete(data_dir, true));
1563 }
1564 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1565
1566 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1567 std::string data("hello");
1568 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1569
1570 base::Time modification_time;
1571 // Note that this timestamp is divisible by two (seconds) - FAT stores
1572 // modification times with 2s resolution.
1573 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1574 &modification_time));
1575 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1576 file_util::FileInfo file_info;
1577 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1578 ASSERT_TRUE(file_info.last_modified == modification_time);
1579}
1580
tfarina@chromium.org34828222010-05-26 10:40:12 +09001581TEST_F(FileUtilTest, IsDirectoryEmpty) {
1582 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1583
1584 ASSERT_FALSE(file_util::PathExists(empty_dir));
1585
1586 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1587
1588 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1589
1590 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1591 std::string bar("baz");
1592 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1593
1594 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1595}
1596
mark@chromium.org17684802008-09-10 09:16:28 +09001597} // namespace