blob: ec62b3c421dcefcb8c821f2480340e1fe6615b39 [file] [log] [blame]
jochen@chromium.orga6879772010-02-18 19:02:26 +09001// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +09009#include <winioctl.h>
initial.commit3f4a7322008-07-27 06:49:38 +090010#include <shellapi.h>
11#include <shlobj.h>
tkent@chromium.org8da14162009-10-09 16:33:39 +090012#include <tchar.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090013#endif
initial.commit3f4a7322008-07-27 06:49:38 +090014
15#include <fstream>
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"
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090024#include "base/scoped_handle.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090025#include "base/time.h"
brettw@chromium.org50c94652009-10-07 11:10:20 +090026#include "base/utf_string_conversions.h"
initial.commit3f4a7322008-07-27 06:49:38 +090027#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090028#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090029
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090030// This macro helps avoid wrapped lines in the test structs.
31#define FPL(x) FILE_PATH_LITERAL(x)
32
initial.commit3f4a7322008-07-27 06:49:38 +090033namespace {
34
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090035// To test that file_util::Normalize FilePath() deals with NTFS reparse points
36// correctly, we need functions to create and delete reparse points.
37#if defined(OS_WIN)
38typedef struct _REPARSE_DATA_BUFFER {
39 ULONG ReparseTag;
40 USHORT ReparseDataLength;
41 USHORT Reserved;
42 union {
43 struct {
44 USHORT SubstituteNameOffset;
45 USHORT SubstituteNameLength;
46 USHORT PrintNameOffset;
47 USHORT PrintNameLength;
48 ULONG Flags;
49 WCHAR PathBuffer[1];
50 } SymbolicLinkReparseBuffer;
51 struct {
52 USHORT SubstituteNameOffset;
53 USHORT SubstituteNameLength;
54 USHORT PrintNameOffset;
55 USHORT PrintNameLength;
56 WCHAR PathBuffer[1];
57 } MountPointReparseBuffer;
58 struct {
59 UCHAR DataBuffer[1];
60 } GenericReparseBuffer;
61 };
62} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
63
64// Sets a reparse point. |source| will now point to |target|. Returns true if
65// the call succeeds, false otherwise.
66bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
67 std::wstring kPathPrefix = L"\\??\\";
68 std::wstring target_str;
69 // The juction will not work if the target path does not start with \??\ .
70 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
71 target_str += kPathPrefix;
72 target_str += target_path.value();
73 const wchar_t* target = target_str.c_str();
74 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
75 char buffer[2000] = {0};
76 DWORD returned;
77
78 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
79
80 data->ReparseTag = 0xa0000003;
81 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
82
83 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
84 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
85 data->ReparseDataLength = size_target + 4 + 8;
86
87 int data_size = data->ReparseDataLength + 8;
88
89 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
90 NULL, 0, &returned, NULL)) {
91 return false;
92 }
93 return true;
94}
95
96// Delete the reparse point referenced by |source|. Returns true if the call
97// succeeds, false otherwise.
98bool DeleteReparsePoint(HANDLE source) {
99 DWORD returned;
100 REPARSE_DATA_BUFFER data = {0};
101 data.ReparseTag = 0xa0000003;
102 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
103 &returned, NULL)) {
104 return false;
105 }
106 return true;
107}
108#endif
109
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900110const wchar_t bogus_content[] = L"I'm cannon fodder.";
111
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900112const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
113 static_cast<file_util::FileEnumerator::FILE_TYPE>(
114 file_util::FileEnumerator::FILES |
115 file_util::FileEnumerator::DIRECTORIES);
116
erikkay@google.comf2406842008-08-21 00:59:49 +0900117// file_util winds up using autoreleased objects on the Mac, so this needs
118// to be a PlatformTest
119class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900120 protected:
121 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +0900122 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +0900123 // Name a subdirectory of the temp directory.
124 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +0900125 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900126
127 // Create a fresh, empty copy of this directory.
128 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +0900129 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +0900130 }
131 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +0900132 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +0900133 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900134 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900135 ASSERT_FALSE(file_util::PathExists(test_dir_));
136 }
137
138 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +0900139 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900140};
141
142// Collects all the results from the given file enumerator, and provides an
143// interface to query whether a given file is present.
144class FindResultCollector {
145 public:
evan@chromium.org1543ad32009-08-27 05:00:14 +0900146 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900147 FilePath cur_file;
148 while (!(cur_file = enumerator.Next()).value().empty()) {
149 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900150 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900151 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900152 << "Same file returned twice";
153
154 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900155 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900156 }
157 }
158
159 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900160 bool HasFile(const FilePath& file) const {
161 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900162 }
evanm@google.com874d1672008-10-31 08:54:04 +0900163
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900164 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900165 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900166 }
initial.commit3f4a7322008-07-27 06:49:38 +0900167
168 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900169 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900170};
171
172// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900173void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900174 const std::wstring& contents) {
175 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900176 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900177 ASSERT_TRUE(file.is_open());
178 file << contents;
179 file.close();
180}
181
182// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900183std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900184 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900185 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900186 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900187 EXPECT_TRUE(file.is_open());
188 file.getline(contents, 64);
189 file.close();
190 return std::wstring(contents);
191}
192
erikkay@google.com014161d2008-08-16 02:45:13 +0900193#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900194uint64 FileTimeAsUint64(const FILETIME& ft) {
195 ULARGE_INTEGER u;
196 u.LowPart = ft.dwLowDateTime;
197 u.HighPart = ft.dwHighDateTime;
198 return u.QuadPart;
199}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900200#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900201
202const struct append_case {
203 const wchar_t* path;
204 const wchar_t* ending;
205 const wchar_t* result;
206} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900207#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900208 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
209 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
210 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
211 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
212 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
213 {L"", L"path", L"\\path"},
214 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900215#elif defined(OS_POSIX)
216 {L"/foo/bar", L"path", L"/foo/bar/path"},
217 {L"/foo/bar/", L"path", L"/foo/bar/path"},
218 {L"/foo/bar//", L"path", L"/foo/bar//path"},
219 {L"/foo/bar/", L"", L"/foo/bar/"},
220 {L"/foo/bar", L"", L"/foo/bar/"},
221 {L"", L"path", L"/path"},
222 {L"", L"", L"/"},
223#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900224};
225
evan@chromium.org1db7f942010-02-27 00:11:55 +0900226#if defined(OS_WIN)
227// This function is deprecated, but still used on Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900228TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900229 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900230 const append_case& value = append_cases[i];
231 std::wstring result = value.path;
232 file_util::AppendToPath(&result, value.ending);
233 EXPECT_EQ(value.result, result);
234 }
235
236#ifdef NDEBUG
237 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
238#endif
239}
evan@chromium.org1db7f942010-02-27 00:11:55 +0900240#endif // defined(OS_WIN)
241
initial.commit3f4a7322008-07-27 06:49:38 +0900242static const struct filename_case {
243 const wchar_t* path;
244 const wchar_t* filename;
245} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900246#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900247 {L"c:\\colon\\backslash", L"backslash"},
248 {L"c:\\colon\\backslash\\", L""},
249 {L"\\\\filename.exe", L"filename.exe"},
250 {L"filename.exe", L"filename.exe"},
251 {L"", L""},
252 {L"\\\\\\", L""},
253 {L"c:/colon/backslash", L"backslash"},
254 {L"c:/colon/backslash/", L""},
255 {L"//////", L""},
256 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900257#elif defined(OS_POSIX)
258 {L"/foo/bar", L"bar"},
259 {L"/foo/bar/", L""},
260 {L"/filename.exe", L"filename.exe"},
261 {L"filename.exe", L"filename.exe"},
262 {L"", L""},
263 {L"/", L""},
264#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900265};
266
evan@chromium.org5a196702010-07-02 08:19:02 +0900267#if defined(OS_WIN)
268// This function is deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900269TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900270 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900271 const filename_case& value = filename_cases[i];
272 std::wstring result = file_util::GetFilenameFromPath(value.path);
273 EXPECT_EQ(value.filename, result);
274 }
275}
evan@chromium.org5a196702010-07-02 08:19:02 +0900276#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900277
278// Test finding the file type from a path name
279static const struct extension_case {
280 const wchar_t* path;
281 const wchar_t* extension;
282} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900283#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900284 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
285 {L"C:\\colon\\backslash\\filename.", L""},
286 {L"C:\\colon\\backslash\\filename", L""},
287 {L"C:\\colon\\backslash\\", L""},
288 {L"C:\\colon\\backslash.\\", L""},
289 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900290#elif defined(OS_POSIX)
291 {L"/foo/bar/filename.extension", L"extension"},
292 {L"/foo/bar/filename.", L""},
293 {L"/foo/bar/filename", L""},
294 {L"/foo/bar/", L""},
295 {L"/foo/bar./", L""},
296 {L"/foo/bar/filename.extension.extension2", L"extension2"},
297 {L".", L""},
298 {L"..", L""},
299 {L"./foo", L""},
300 {L"./foo.extension", L"extension"},
301 {L"/foo.extension1/bar.extension2", L"extension2"},
302#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900303};
304
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900305#if defined(OS_WIN)
306// This function has been deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900307TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900308 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900309 const extension_case& ext = extension_cases[i];
310 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
311 EXPECT_EQ(ext.extension, fext);
312 }
313}
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900314#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900315
316// Test finding the directory component of a path
317static const struct dir_case {
318 const wchar_t* full_path;
319 const wchar_t* directory;
320} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900321#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900322 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
323 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
324 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
325 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
326 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
327 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900328 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900329#elif defined(OS_POSIX)
330 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
331 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
332 {L"/foo/bar/", L"/foo/bar"},
333 {L"/foo/bar//", L"/foo/bar"},
334 {L"/foo/bar", L"/foo"},
335 {L"/foo/bar./", L"/foo/bar."},
336 {L"/", L"/"},
337 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900338 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900339#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900340};
341
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900342#if defined(OS_WIN)
343// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900344TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900345 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900346 const dir_case& dir = dir_cases[i];
347 const std::wstring parent =
348 file_util::GetDirectoryFromPath(dir.full_path);
349 EXPECT_EQ(dir.directory, parent);
350 }
351}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900352#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900353
glider@chromium.org5fd12332010-06-10 22:05:26 +0900354// Flaky, http://crbug.com/46246
glider@chromium.orge1879a22010-06-10 21:40:52 +0900355TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commit3f4a7322008-07-27 06:49:38 +0900356 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900357 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900358 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
359
360 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900361#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900362 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900363#elif defined(OS_POSIX)
364 // We need to wait at least one second here because the precision of
365 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900366 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900367#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900368
369 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900370 base::Time now(base::Time::NowFromSystemTime());
371 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900372
373 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900374 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900375 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
376
377 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900378 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900379
380 // Delete new file, we should see no files after cutoff now
381 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900382 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900383}
384
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900385TEST_F(FileUtilTest, FileAndDirectorySize) {
386 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
387 // should return 53 bytes.
388 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
389 CreateTextFile(file_01, L"12345678901234567890");
390 int64 size_f1 = 0;
391 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
392 EXPECT_EQ(20ll, size_f1);
393
394 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
395 file_util::CreateDirectory(subdir_path);
396
397 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
398 CreateTextFile(file_02, L"123456789012345678901234567890");
399 int64 size_f2 = 0;
400 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
401 EXPECT_EQ(30ll, size_f2);
402
403 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
404 file_util::CreateDirectory(subsubdir_path);
405
406 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
407 CreateTextFile(file_03, L"123");
408
409 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
410 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
rvargas@google.comaa24e112010-06-12 07:53:43 +0900411
412 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("The file*"));
413 EXPECT_EQ(size_f1, computed_size);
414
415 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("bla*"));
416 EXPECT_EQ(0, computed_size);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900417}
418
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900419TEST_F(FileUtilTest, NormalizeFilePathBasic) {
420 // Create a directory under the test dir. Because we create it,
421 // we know it is not a link.
422 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
423 FilePath dir_path = test_dir_.Append(FPL("dir"));
424 FilePath file_b_path = dir_path.Append(FPL("file_b"));
425 file_util::CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900426
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900427 FilePath normalized_file_a_path, normalized_file_b_path;
428 ASSERT_FALSE(file_util::PathExists(file_a_path));
429 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
430 &normalized_file_a_path))
431 << "NormalizeFilePath() should fail on nonexistant paths.";
432
433 CreateTextFile(file_a_path, bogus_content);
434 ASSERT_TRUE(file_util::PathExists(file_a_path));
435 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
436 &normalized_file_a_path));
437
438 CreateTextFile(file_b_path, bogus_content);
439 ASSERT_TRUE(file_util::PathExists(file_b_path));
440 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
441 &normalized_file_b_path));
442
443 // Beacuse this test created |dir_path|, we know it is not a link
444 // or junction. So, the real path of the directory holding file a
445 // must be the parent of the path holding file b.
446 ASSERT_TRUE(normalized_file_a_path.DirName()
447 .IsParent(normalized_file_b_path.DirName()));
448}
449
450#if defined(OS_WIN)
451
452TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
453 // Build the following directory structure:
454 //
455 // test_dir_
456 // |-> base_a
457 // | |-> sub_a
458 // | |-> file.txt
459 // | |-> long_name___... (Very long name.)
460 // | |-> sub_long
461 // | |-> deep.txt
462 // |-> base_b
463 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
464 // |-> to_base_b (reparse point to test_dir_\base_b)
465 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
466
467 FilePath base_a = test_dir_.Append(FPL("base_a"));
468 ASSERT_TRUE(file_util::CreateDirectory(base_a));
469
470 FilePath sub_a = base_a.Append(FPL("sub_a"));
471 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
472
473 FilePath file_txt = sub_a.Append(FPL("file.txt"));
474 CreateTextFile(file_txt, bogus_content);
475
476 // Want a directory whose name is long enough to make the path to the file
477 // inside just under MAX_PATH chars. This will be used to test that when
478 // a junction expands to a path over MAX_PATH chars in length,
479 // NormalizeFilePath() fails without crashing.
480 FilePath sub_long_rel(FPL("sub_long"));
481 FilePath deep_txt(FPL("deep.txt"));
482
483 int target_length = MAX_PATH;
484 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
485 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900486 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900487 // the resulting path is still long enough to hit the failing case in
488 // NormalizePath().
489 const int kCreateDirLimit = 4;
490 target_length -= kCreateDirLimit;
491 FilePath::StringType long_name_str = FPL("long_name_");
492 long_name_str.resize(target_length, '_');
493
494 FilePath long_name = sub_a.Append(FilePath(long_name_str));
495 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
496 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
497
498 FilePath sub_long = deep_file.DirName();
499 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
500 CreateTextFile(deep_file, bogus_content);
501
502 FilePath base_b = test_dir_.Append(FPL("base_b"));
503 ASSERT_TRUE(file_util::CreateDirectory(base_b));
504
505 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
506 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
507 ScopedHandle reparse_to_sub_a(
508 ::CreateFile(to_sub_a.value().c_str(),
509 FILE_ALL_ACCESS,
510 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
511 NULL,
512 OPEN_EXISTING,
513 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
514 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900515 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900516 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
517
518 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
519 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
520 ScopedHandle reparse_to_base_b(
521 ::CreateFile(to_base_b.value().c_str(),
522 FILE_ALL_ACCESS,
523 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
524 NULL,
525 OPEN_EXISTING,
526 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
527 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900528 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900529 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
530
531 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
532 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
533 ScopedHandle reparse_to_sub_long(
534 ::CreateFile(to_sub_long.value().c_str(),
535 FILE_ALL_ACCESS,
536 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
537 NULL,
538 OPEN_EXISTING,
539 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
540 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900541 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900542 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
543
544 // Normalize a junction free path: base_a\sub_a\file.txt .
545 FilePath normalized_path;
546 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
547 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
548
549 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
550 // the junction to_sub_a.
551 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
552 &normalized_path));
553 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
554
555 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
556 // normalized to exclude junctions to_base_b and to_sub_a .
557 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
558 .Append(FPL("to_base_b"))
559 .Append(FPL("to_sub_a"))
560 .Append(FPL("file.txt")),
561 &normalized_path));
562 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
563
564 // A long enough path will cause NormalizeFilePath() to fail. Make a long
565 // path using to_base_b many times, and check that paths long enough to fail
566 // do not cause a crash.
567 FilePath long_path = base_b;
568 const int kLengthLimit = MAX_PATH + 200;
569 while (long_path.value().length() <= kLengthLimit) {
570 long_path = long_path.Append(FPL("to_base_b"));
571 }
572 long_path = long_path.Append(FPL("to_sub_a"))
573 .Append(FPL("file.txt"));
574
575 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
576
577 // Normalizing the junction to deep.txt should fail, because the expanded
578 // path to deep.txt is longer than MAX_PATH.
579 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
580 &normalized_path));
581
582 // Delete the reparse points, and see that NormalizeFilePath() fails
583 // to traverse them.
584 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
585 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
586 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
587
588 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
589 &normalized_path));
590}
591
592#endif // defined(OS_WIN)
593
594// The following test of NormalizeFilePath() require that we create a symlink.
595// This can not be done on windows before vista. On vista, creating a symlink
596// requires privilege "SeCreateSymbolicLinkPrivilege".
597// TODO(skerner): Investigate the possibility of giving base_unittests the
598// privileges required to create a symlink.
599#if defined(OS_POSIX)
600
601bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
602 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
603}
604
605TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
606 FilePath normalized_path;
skerner@chromium.org559baa92010-05-13 00:13:57 +0900607
608 // Link one file to another.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900609 FilePath link_from = test_dir_.Append(FPL("from_file"));
610 FilePath link_to = test_dir_.Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900611 CreateTextFile(link_to, bogus_content);
612
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900613 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900614 << "Failed to create file symlink.";
615
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900616 // Check that NormalizeFilePath sees the link.
617 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900618 ASSERT_TRUE(link_to != link_from);
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900619 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
620 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900621
622 // Link to a directory.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900623 link_from = test_dir_.Append(FPL("from_dir"));
624 link_to = test_dir_.Append(FPL("to_dir"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900625 file_util::CreateDirectory(link_to);
626
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900627 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900628 << "Failed to create directory symlink.";
629
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900630 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
631 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900632
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900633 // Test that a loop in the links causes NormalizeFilePath() to return false.
634 link_from = test_dir_.Append(FPL("link_a"));
635 link_to = test_dir_.Append(FPL("link_b"));
636 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900637 << "Failed to create loop symlink a.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900638 ASSERT_TRUE(MakeSymlink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900639 << "Failed to create loop symlink b.";
640
641 // Infinite loop!
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900642 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900643}
644#endif // defined(OS_POSIX)
645
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900646TEST_F(FileUtilTest, DeleteNonExistent) {
647 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
648 ASSERT_FALSE(file_util::PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900649
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900650 EXPECT_TRUE(file_util::Delete(non_existent, false));
651 ASSERT_FALSE(file_util::PathExists(non_existent));
652 EXPECT_TRUE(file_util::Delete(non_existent, true));
653 ASSERT_FALSE(file_util::PathExists(non_existent));
654}
655
656TEST_F(FileUtilTest, DeleteFile) {
657 // Create a file
658 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
659 CreateTextFile(file_name, bogus_content);
initial.commit3f4a7322008-07-27 06:49:38 +0900660 ASSERT_TRUE(file_util::PathExists(file_name));
661
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900662 // Make sure it's deleted
663 EXPECT_TRUE(file_util::Delete(file_name, false));
664 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900665
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900666 // Test recursive case, create a new file
667 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
668 CreateTextFile(file_name, bogus_content);
669 ASSERT_TRUE(file_util::PathExists(file_name));
670
671 // Make sure it's deleted
672 EXPECT_TRUE(file_util::Delete(file_name, true));
673 EXPECT_FALSE(file_util::PathExists(file_name));
674}
675
676#if defined(OS_WIN)
677// Tests that the Delete function works for wild cards, especially
678// with the recursion flag. Also coincidentally tests PathExists.
679// TODO(erikkay): see if anyone's actually using this feature of the API
680TEST_F(FileUtilTest, DeleteWildCard) {
681 // Create a file and a directory
682 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
683 CreateTextFile(file_name, bogus_content);
684 ASSERT_TRUE(file_util::PathExists(file_name));
685
686 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
687 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900688 ASSERT_TRUE(file_util::PathExists(subdir_path));
689
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900690 // Create the wildcard path
evanm@google.com874d1672008-10-31 08:54:04 +0900691 FilePath directory_contents = test_dir_;
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900692 directory_contents = directory_contents.Append(FPL("*"));
693
initial.commit3f4a7322008-07-27 06:49:38 +0900694 // Delete non-recursively and check that only the file is deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900695 EXPECT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900696 EXPECT_FALSE(file_util::PathExists(file_name));
697 EXPECT_TRUE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900698
zork@chromium.org61be4f42010-05-07 09:05:36 +0900699 // Delete recursively and make sure all contents are deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900700 EXPECT_TRUE(file_util::Delete(directory_contents, true));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900701 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900702 EXPECT_FALSE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900703}
704
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900705// TODO(erikkay): see if anyone's actually using this feature of the API
706TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
707 // Create a file and a directory
708 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
709 file_util::CreateDirectory(subdir_path);
710 ASSERT_TRUE(file_util::PathExists(subdir_path));
711
712 // Create the wildcard path
713 FilePath directory_contents = subdir_path;
714 directory_contents = directory_contents.Append(FPL("*"));
715
716 // Delete non-recursively and check nothing got deleted
717 EXPECT_TRUE(file_util::Delete(directory_contents, false));
718 EXPECT_TRUE(file_util::PathExists(subdir_path));
719
720 // Delete recursively and check nothing got deleted
721 EXPECT_TRUE(file_util::Delete(directory_contents, true));
722 EXPECT_TRUE(file_util::PathExists(subdir_path));
723}
724#endif
725
726// Tests non-recursive Delete() for a directory.
727TEST_F(FileUtilTest, DeleteDirNonRecursive) {
728 // Create a subdirectory and put a file and two directories inside.
729 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
730 file_util::CreateDirectory(test_subdir);
731 ASSERT_TRUE(file_util::PathExists(test_subdir));
732
733 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
734 CreateTextFile(file_name, bogus_content);
735 ASSERT_TRUE(file_util::PathExists(file_name));
736
737 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
738 file_util::CreateDirectory(subdir_path1);
739 ASSERT_TRUE(file_util::PathExists(subdir_path1));
740
741 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
742 file_util::CreateDirectory(subdir_path2);
743 ASSERT_TRUE(file_util::PathExists(subdir_path2));
744
745 // Delete non-recursively and check that the empty dir got deleted
746 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
747 EXPECT_FALSE(file_util::PathExists(subdir_path2));
748
749 // Delete non-recursively and check that nothing got deleted
750 EXPECT_FALSE(file_util::Delete(test_subdir, false));
751 EXPECT_TRUE(file_util::PathExists(test_subdir));
752 EXPECT_TRUE(file_util::PathExists(file_name));
753 EXPECT_TRUE(file_util::PathExists(subdir_path1));
754}
755
756// Tests recursive Delete() for a directory.
757TEST_F(FileUtilTest, DeleteDirRecursive) {
758 // Create a subdirectory and put a file and two directories inside.
759 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
760 file_util::CreateDirectory(test_subdir);
761 ASSERT_TRUE(file_util::PathExists(test_subdir));
762
763 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
764 CreateTextFile(file_name, bogus_content);
765 ASSERT_TRUE(file_util::PathExists(file_name));
766
767 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
768 file_util::CreateDirectory(subdir_path1);
769 ASSERT_TRUE(file_util::PathExists(subdir_path1));
770
771 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
772 file_util::CreateDirectory(subdir_path2);
773 ASSERT_TRUE(file_util::PathExists(subdir_path2));
774
775 // Delete recursively and check that the empty dir got deleted
776 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
777 EXPECT_FALSE(file_util::PathExists(subdir_path2));
778
779 // Delete recursively and check that everything got deleted
780 EXPECT_TRUE(file_util::Delete(test_subdir, true));
781 EXPECT_FALSE(file_util::PathExists(file_name));
782 EXPECT_FALSE(file_util::PathExists(subdir_path1));
783 EXPECT_FALSE(file_util::PathExists(test_subdir));
784}
785
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900786TEST_F(FileUtilTest, MoveFileNew) {
787 // Create a file
788 FilePath file_name_from =
789 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
790 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
791 ASSERT_TRUE(file_util::PathExists(file_name_from));
792
793 // The destination
794 FilePath file_name_to =
795 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
796 ASSERT_FALSE(file_util::PathExists(file_name_to));
797
798 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
799
800 // Check everything has been moved.
801 EXPECT_FALSE(file_util::PathExists(file_name_from));
802 EXPECT_TRUE(file_util::PathExists(file_name_to));
803}
804
805TEST_F(FileUtilTest, MoveFileExists) {
806 // Create a file
807 FilePath file_name_from =
808 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
809 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
810 ASSERT_TRUE(file_util::PathExists(file_name_from));
811
812 // The destination name
813 FilePath file_name_to =
814 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
815 CreateTextFile(file_name_to, L"Old file content");
816 ASSERT_TRUE(file_util::PathExists(file_name_to));
817
818 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
819
820 // Check everything has been moved.
821 EXPECT_FALSE(file_util::PathExists(file_name_from));
822 EXPECT_TRUE(file_util::PathExists(file_name_to));
823 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
824}
825
826TEST_F(FileUtilTest, MoveFileDirExists) {
827 // Create a file
828 FilePath file_name_from =
829 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
830 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
831 ASSERT_TRUE(file_util::PathExists(file_name_from));
832
833 // The destination directory
834 FilePath dir_name_to =
835 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
836 file_util::CreateDirectory(dir_name_to);
837 ASSERT_TRUE(file_util::PathExists(dir_name_to));
838
839 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
840}
841
842
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900843TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900844 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900845 FilePath dir_name_from =
846 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
847 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900848 ASSERT_TRUE(file_util::PathExists(dir_name_from));
849
850 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900851 FilePath file_name_from =
852 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900853 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
854 ASSERT_TRUE(file_util::PathExists(file_name_from));
855
856 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900857 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
858 FilePath file_name_to =
859 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900860
861 ASSERT_FALSE(file_util::PathExists(dir_name_to));
862
863 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
864
865 // Check everything has been moved.
866 EXPECT_FALSE(file_util::PathExists(dir_name_from));
867 EXPECT_FALSE(file_util::PathExists(file_name_from));
868 EXPECT_TRUE(file_util::PathExists(dir_name_to));
869 EXPECT_TRUE(file_util::PathExists(file_name_to));
870}
871
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900872TEST_F(FileUtilTest, MoveExist) {
873 // Create a directory
874 FilePath dir_name_from =
875 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
876 file_util::CreateDirectory(dir_name_from);
877 ASSERT_TRUE(file_util::PathExists(dir_name_from));
878
879 // Create a file under the directory
880 FilePath file_name_from =
881 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
882 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
883 ASSERT_TRUE(file_util::PathExists(file_name_from));
884
885 // Move the directory
886 FilePath dir_name_exists =
887 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
888
889 FilePath dir_name_to =
890 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
891 FilePath file_name_to =
892 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
893
894 // Create the destination directory.
895 file_util::CreateDirectory(dir_name_exists);
896 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
897
898 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
899
900 // Check everything has been moved.
901 EXPECT_FALSE(file_util::PathExists(dir_name_from));
902 EXPECT_FALSE(file_util::PathExists(file_name_from));
903 EXPECT_TRUE(file_util::PathExists(dir_name_to));
904 EXPECT_TRUE(file_util::PathExists(file_name_to));
905}
906
907TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900908 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900909 FilePath dir_name_from =
910 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
911 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900912 ASSERT_TRUE(file_util::PathExists(dir_name_from));
913
914 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900915 FilePath file_name_from =
916 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900917 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
918 ASSERT_TRUE(file_util::PathExists(file_name_from));
919
920 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900921 FilePath subdir_name_from =
922 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
923 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900924 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
925
926 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900927 FilePath file_name2_from =
928 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900929 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
930 ASSERT_TRUE(file_util::PathExists(file_name2_from));
931
932 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900933 FilePath dir_name_to =
934 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
935 FilePath file_name_to =
936 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
937 FilePath subdir_name_to =
938 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
939 FilePath file_name2_to =
940 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900941
942 ASSERT_FALSE(file_util::PathExists(dir_name_to));
943
944 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
945
946 // Check everything has been copied.
947 EXPECT_TRUE(file_util::PathExists(dir_name_from));
948 EXPECT_TRUE(file_util::PathExists(file_name_from));
949 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
950 EXPECT_TRUE(file_util::PathExists(file_name2_from));
951 EXPECT_TRUE(file_util::PathExists(dir_name_to));
952 EXPECT_TRUE(file_util::PathExists(file_name_to));
953 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
954 EXPECT_TRUE(file_util::PathExists(file_name2_to));
955}
956
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900957TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
958 // Create a directory.
959 FilePath dir_name_from =
960 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
961 file_util::CreateDirectory(dir_name_from);
962 ASSERT_TRUE(file_util::PathExists(dir_name_from));
963
964 // Create a file under the directory.
965 FilePath file_name_from =
966 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
967 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
968 ASSERT_TRUE(file_util::PathExists(file_name_from));
969
970 // Create a subdirectory.
971 FilePath subdir_name_from =
972 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
973 file_util::CreateDirectory(subdir_name_from);
974 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
975
976 // Create a file under the subdirectory.
977 FilePath file_name2_from =
978 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
979 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
980 ASSERT_TRUE(file_util::PathExists(file_name2_from));
981
982 // Copy the directory recursively.
983 FilePath dir_name_exists =
984 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
985
986 FilePath dir_name_to =
987 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
988 FilePath file_name_to =
989 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
990 FilePath subdir_name_to =
991 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
992 FilePath file_name2_to =
993 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
994
995 // Create the destination directory.
996 file_util::CreateDirectory(dir_name_exists);
997 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
998
999 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1000
1001 // Check everything has been copied.
1002 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1003 EXPECT_TRUE(file_util::PathExists(file_name_from));
1004 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1005 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1006 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1007 EXPECT_TRUE(file_util::PathExists(file_name_to));
1008 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1009 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1010}
1011
1012TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001013 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001014 FilePath dir_name_from =
1015 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1016 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001017 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1018
1019 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001020 FilePath file_name_from =
1021 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001022 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1023 ASSERT_TRUE(file_util::PathExists(file_name_from));
1024
1025 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001026 FilePath subdir_name_from =
1027 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1028 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001029 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1030
1031 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001032 FilePath file_name2_from =
1033 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001034 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1035 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1036
1037 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001038 FilePath dir_name_to =
1039 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1040 FilePath file_name_to =
1041 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1042 FilePath subdir_name_to =
1043 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001044
1045 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1046
1047 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1048
1049 // Check everything has been copied.
1050 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1051 EXPECT_TRUE(file_util::PathExists(file_name_from));
1052 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1053 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1054 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1055 EXPECT_TRUE(file_util::PathExists(file_name_to));
1056 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1057}
1058
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001059TEST_F(FileUtilTest, CopyDirectoryExists) {
1060 // Create a directory.
1061 FilePath dir_name_from =
1062 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1063 file_util::CreateDirectory(dir_name_from);
1064 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1065
1066 // Create a file under the directory.
1067 FilePath file_name_from =
1068 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1069 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1070 ASSERT_TRUE(file_util::PathExists(file_name_from));
1071
1072 // Create a subdirectory.
1073 FilePath subdir_name_from =
1074 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1075 file_util::CreateDirectory(subdir_name_from);
1076 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1077
1078 // Create a file under the subdirectory.
1079 FilePath file_name2_from =
1080 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1081 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1082 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1083
1084 // Copy the directory not recursively.
1085 FilePath dir_name_to =
1086 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1087 FilePath file_name_to =
1088 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1089 FilePath subdir_name_to =
1090 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1091
1092 // Create the destination directory.
1093 file_util::CreateDirectory(dir_name_to);
1094 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1095
1096 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1097
1098 // Check everything has been copied.
1099 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1100 EXPECT_TRUE(file_util::PathExists(file_name_from));
1101 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1102 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1103 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1104 EXPECT_TRUE(file_util::PathExists(file_name_to));
1105 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1106}
1107
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001108TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1109 // Create a file
1110 FilePath file_name_from =
1111 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1112 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1113 ASSERT_TRUE(file_util::PathExists(file_name_from));
1114
1115 // The destination name
1116 FilePath file_name_to =
1117 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1118 ASSERT_FALSE(file_util::PathExists(file_name_to));
1119
1120 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1121
1122 // Check the has been copied
1123 EXPECT_TRUE(file_util::PathExists(file_name_to));
1124}
1125
1126TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1127 // Create a file
1128 FilePath file_name_from =
1129 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1130 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1131 ASSERT_TRUE(file_util::PathExists(file_name_from));
1132
1133 // The destination name
1134 FilePath file_name_to =
1135 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1136 CreateTextFile(file_name_to, L"Old file content");
1137 ASSERT_TRUE(file_util::PathExists(file_name_to));
1138
1139 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1140
1141 // Check the has been copied
1142 EXPECT_TRUE(file_util::PathExists(file_name_to));
1143 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1144}
1145
1146TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1147 // Create a file
1148 FilePath file_name_from =
1149 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1150 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1151 ASSERT_TRUE(file_util::PathExists(file_name_from));
1152
1153 // The destination
1154 FilePath dir_name_to =
1155 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1156 file_util::CreateDirectory(dir_name_to);
1157 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1158 FilePath file_name_to =
1159 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1160
1161 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1162
1163 // Check the has been copied
1164 EXPECT_TRUE(file_util::PathExists(file_name_to));
1165}
1166
initial.commit3f4a7322008-07-27 06:49:38 +09001167TEST_F(FileUtilTest, CopyFile) {
1168 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001169 FilePath dir_name_from =
1170 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1171 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001172 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1173
1174 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001175 FilePath file_name_from =
1176 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001177 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1178 CreateTextFile(file_name_from, file_contents);
1179 ASSERT_TRUE(file_util::PathExists(file_name_from));
1180
1181 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001182 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001183 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001184
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001185 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001186 FilePath dest_file2(dir_name_from);
1187 dest_file2 = dest_file2.AppendASCII("..");
1188 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1189 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1190
1191 FilePath dest_file2_test(dir_name_from);
1192 dest_file2_test = dest_file2_test.DirName();
1193 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001194
1195 // Check everything has been copied.
1196 EXPECT_TRUE(file_util::PathExists(file_name_from));
1197 EXPECT_TRUE(file_util::PathExists(dest_file));
1198 const std::wstring read_contents = ReadTextFile(dest_file);
1199 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +09001200 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1201 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001202}
1203
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001204// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +09001205#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001206TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +09001207 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001208
1209 SYSTEMTIME start_time;
1210 GetLocalTime(&start_time);
1211 Sleep(100);
1212 CreateTextFile(file_name, L"New file!");
1213 Sleep(100);
1214 SYSTEMTIME end_time;
1215 GetLocalTime(&end_time);
1216
1217 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +09001218 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +09001219
1220 FILETIME start_filetime;
1221 SystemTimeToFileTime(&start_time, &start_filetime);
1222 FILETIME end_filetime;
1223 SystemTimeToFileTime(&end_time, &end_filetime);
1224 FILETIME file_creation_filetime;
1225 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1226
1227 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1228 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1229 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1230
1231 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1232 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1233 "end time: " << FileTimeAsUint64(end_filetime);
1234
evanm@google.com874d1672008-10-31 08:54:04 +09001235 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +09001236}
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001237#endif
initial.commit3f4a7322008-07-27 06:49:38 +09001238
erikkay@google.comf2406842008-08-21 00:59:49 +09001239// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001240// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001241typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001242
erikkay@google.comf2406842008-08-21 00:59:49 +09001243TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001244 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +09001245 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +09001246 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1247 .Append(FILE_PATH_LITERAL("data"))
1248 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +09001249 ASSERT_TRUE(file_util::PathExists(data_dir));
1250
evanm@google.com874d1672008-10-31 08:54:04 +09001251 FilePath original_file =
1252 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1253 FilePath same_file =
1254 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1255 FilePath same_length_file =
1256 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1257 FilePath different_file =
1258 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1259 FilePath different_first_file =
1260 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1261 FilePath different_last_file =
1262 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1263 FilePath empty1_file =
1264 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1265 FilePath empty2_file =
1266 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1267 FilePath shortened_file =
1268 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1269 FilePath binary_file =
1270 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1271 FilePath binary_file_same =
1272 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1273 FilePath binary_file_diff =
1274 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001275
1276 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1277 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1278 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1279 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +09001280 EXPECT_FALSE(file_util::ContentsEqual(
1281 FilePath(FILE_PATH_LITERAL("bogusname")),
1282 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +09001283 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1284 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1285 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1286 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1287 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1288 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1289 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1290}
1291
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001292TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1293 FilePath data_dir;
1294 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1295 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1296 .Append(FILE_PATH_LITERAL("data"))
1297 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1298 ASSERT_TRUE(file_util::PathExists(data_dir));
1299
1300 FilePath original_file =
1301 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1302 FilePath same_file =
1303 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1304 FilePath crlf_file =
1305 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1306 FilePath shortened_file =
1307 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1308 FilePath different_file =
1309 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1310 FilePath different_first_file =
1311 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1312 FilePath different_last_file =
1313 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1314 FilePath first1_file =
1315 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1316 FilePath first2_file =
1317 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1318 FilePath empty1_file =
1319 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1320 FilePath empty2_file =
1321 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1322 FilePath blank_line_file =
1323 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1324 FilePath blank_line_crlf_file =
1325 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1326
1327 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1328 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1329 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1330 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1331 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1332 different_first_file));
1333 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1334 different_last_file));
1335 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1336 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1337 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1338 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1339 blank_line_crlf_file));
1340}
1341
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001342// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001343#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001344TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001345 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001346 CreateTextFile(target_file, L"This is the target.");
1347
evanm@google.com874d1672008-10-31 08:54:04 +09001348 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001349
1350 HRESULT result;
1351 IShellLink *shell = NULL;
1352 IPersistFile *persist = NULL;
1353
1354 CoInitialize(NULL);
1355 // Temporarily create a shortcut for test
1356 result = CoCreateInstance(CLSID_ShellLink, NULL,
1357 CLSCTX_INPROC_SERVER, IID_IShellLink,
1358 reinterpret_cast<LPVOID*>(&shell));
1359 EXPECT_TRUE(SUCCEEDED(result));
1360 result = shell->QueryInterface(IID_IPersistFile,
1361 reinterpret_cast<LPVOID*>(&persist));
1362 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001363 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001364 EXPECT_TRUE(SUCCEEDED(result));
1365 result = shell->SetDescription(L"ResolveShortcutTest");
1366 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001367 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001368 EXPECT_TRUE(SUCCEEDED(result));
1369 if (persist)
1370 persist->Release();
1371 if (shell)
1372 shell->Release();
1373
1374 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001375 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001376 EXPECT_TRUE(is_solved);
1377 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001378 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001379 EXPECT_EQ(L"This is the target.", contents);
1380
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001381 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001382 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001383 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001384 CoUninitialize();
1385}
1386
1387TEST_F(FileUtilTest, CreateShortcutTest) {
1388 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001389 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001390 CreateTextFile(target_file, file_contents);
1391
evanm@google.com874d1672008-10-31 08:54:04 +09001392 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001393
1394 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001395 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1396 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001397 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001398 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001399 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001400 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001401 EXPECT_EQ(file_contents, read_contents);
1402
evanm@google.com874d1672008-10-31 08:54:04 +09001403 DeleteFile(target_file.value().c_str());
1404 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001405 CoUninitialize();
1406}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001407
1408TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1409 // Create a directory
1410 FilePath dir_name_from =
1411 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1412 file_util::CreateDirectory(dir_name_from);
1413 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1414
1415 // Create a file under the directory
1416 FilePath file_name_from =
1417 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1418 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1419 ASSERT_TRUE(file_util::PathExists(file_name_from));
1420
1421 // Move the directory by using CopyAndDeleteDirectory
1422 FilePath dir_name_to = test_dir_.Append(
1423 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1424 FilePath file_name_to =
1425 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1426
1427 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1428
1429 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1430
1431 // Check everything has been moved.
1432 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1433 EXPECT_FALSE(file_util::PathExists(file_name_from));
1434 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1435 EXPECT_TRUE(file_util::PathExists(file_name_to));
1436}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001437
1438TEST_F(FileUtilTest, GetTempDirTest) {
1439 static const TCHAR* kTmpKey = _T("TMP");
1440 static const TCHAR* kTmpValues[] = {
1441 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1442 };
1443 // Save the original $TMP.
1444 size_t original_tmp_size;
1445 TCHAR* original_tmp;
1446 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1447 // original_tmp may be NULL.
1448
1449 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1450 FilePath path;
1451 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1452 file_util::GetTempDir(&path);
1453 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1454 " result=" << path.value();
1455 }
1456
1457 // Restore the original $TMP.
1458 if (original_tmp) {
1459 ::_tputenv_s(kTmpKey, original_tmp);
1460 free(original_tmp);
1461 } else {
1462 ::_tputenv_s(kTmpKey, _T(""));
1463 }
1464}
1465#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001466
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001467TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1468 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001469 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001470 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001471 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1472 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1473 }
1474 for (int i = 0; i < 3; i++)
1475 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1476 for (int i = 0; i < 3; i++)
1477 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1478}
1479
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001480TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001481 FilePath names[3];
1482 FILE *fps[3];
1483 int i;
1484
1485 // Create; make sure they are open and exist.
1486 for (i = 0; i < 3; ++i) {
1487 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1488 ASSERT_TRUE(fps[i]);
1489 EXPECT_TRUE(file_util::PathExists(names[i]));
1490 }
1491
1492 // Make sure all names are unique.
1493 for (i = 0; i < 3; ++i) {
1494 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1495 }
1496
1497 // Close and delete.
1498 for (i = 0; i < 3; ++i) {
1499 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1500 EXPECT_TRUE(file_util::Delete(names[i], false));
1501 }
initial.commit3f4a7322008-07-27 06:49:38 +09001502}
1503
1504TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001505 FilePath temp_dir;
1506 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1507 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001508 EXPECT_TRUE(file_util::PathExists(temp_dir));
1509 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001510}
1511
skerner@chromium.orge4432392010-05-01 02:00:09 +09001512TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1513 FilePath new_dir;
1514 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1515 test_dir_,
1516 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001517 &new_dir));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001518 EXPECT_TRUE(file_util::PathExists(new_dir));
1519 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1520 EXPECT_TRUE(file_util::Delete(new_dir, false));
1521}
1522
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001523TEST_F(FileUtilTest, GetShmemTempDirTest) {
1524 FilePath dir;
1525 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1526 EXPECT_TRUE(file_util::DirectoryExists(dir));
1527}
1528
initial.commit3f4a7322008-07-27 06:49:38 +09001529TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001530 FilePath test_root =
1531 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001532#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001533 FilePath test_path =
1534 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001535#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001536 FilePath test_path =
1537 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001538#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001539
1540 EXPECT_FALSE(file_util::PathExists(test_path));
1541 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1542 EXPECT_TRUE(file_util::PathExists(test_path));
1543 // CreateDirectory returns true if the DirectoryExists returns true.
1544 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1545
1546 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001547 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001548 EXPECT_FALSE(file_util::PathExists(test_path));
1549 CreateTextFile(test_path, L"test file");
1550 EXPECT_TRUE(file_util::PathExists(test_path));
1551 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1552
1553 EXPECT_TRUE(file_util::Delete(test_root, true));
1554 EXPECT_FALSE(file_util::PathExists(test_root));
1555 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001556
1557 // Verify assumptions made by the Windows implementation:
1558 // 1. The current directory always exists.
1559 // 2. The root directory always exists.
1560 ASSERT_TRUE(file_util::DirectoryExists(
1561 FilePath(FilePath::kCurrentDirectory)));
1562 FilePath top_level = test_root;
1563 while (top_level != top_level.DirName()) {
1564 top_level = top_level.DirName();
1565 }
1566 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1567
1568 // Given these assumptions hold, it should be safe to
1569 // test that "creating" these directories succeeds.
1570 EXPECT_TRUE(file_util::CreateDirectory(
1571 FilePath(FilePath::kCurrentDirectory)));
1572 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001573
1574#if defined(OS_WIN)
1575 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1576 FilePath invalid_path =
1577 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1578 if (!file_util::PathExists(invalid_drive)) {
1579 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1580 }
1581#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001582}
1583
1584TEST_F(FileUtilTest, DetectDirectoryTest) {
1585 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001586 FilePath test_root =
1587 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001588 EXPECT_FALSE(file_util::PathExists(test_root));
1589 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1590 EXPECT_TRUE(file_util::PathExists(test_root));
1591 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1592
1593 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001594 FilePath test_path =
1595 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001596 EXPECT_FALSE(file_util::PathExists(test_path));
1597 CreateTextFile(test_path, L"test file");
1598 EXPECT_TRUE(file_util::PathExists(test_path));
1599 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1600 EXPECT_TRUE(file_util::Delete(test_path, false));
1601
1602 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001603}
1604
initial.commit3f4a7322008-07-27 06:49:38 +09001605TEST_F(FileUtilTest, FileEnumeratorTest) {
1606 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001607 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001608 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1609 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001610
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001611 // Test an empty directory, non-recursively, including "..".
1612 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1613 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1614 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1615 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1616 f0_dotdot.Next().value());
1617 EXPECT_EQ(FILE_PATH_LITERAL(""),
1618 f0_dotdot.Next().value());
1619
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001620 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001621 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001622 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001623 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001624 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001625 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001626 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001627
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001628 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001629 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001630 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001631 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001632 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001633 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001634 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001635 FilePath file2_rel =
1636 dir2.Append(FilePath::kParentDirectory)
1637 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001638 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001639 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001640
1641 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001642 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001643 file_util::FileEnumerator::FILES);
1644 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001645 EXPECT_TRUE(c1.HasFile(file1));
1646 EXPECT_TRUE(c1.HasFile(file2_abs));
1647 EXPECT_TRUE(c1.HasFile(dir2file));
1648 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1649 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001650
1651 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001652 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001653 file_util::FileEnumerator::DIRECTORIES);
1654 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001655 EXPECT_TRUE(c2.HasFile(dir1));
1656 EXPECT_TRUE(c2.HasFile(dir2));
1657 EXPECT_TRUE(c2.HasFile(dir2inner));
1658 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001659
tim@chromium.org989d0972008-10-16 11:42:45 +09001660 // Only enumerate directories non-recursively.
1661 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001662 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001663 FindResultCollector c2_non_recursive(f2_non_recursive);
1664 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1665 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1666 EXPECT_EQ(c2_non_recursive.size(), 2);
1667
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001668 // Only enumerate directories, non-recursively, including "..".
1669 file_util::FileEnumerator f2_dotdot(
1670 test_dir_, false,
1671 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1672 file_util::FileEnumerator::DIRECTORIES |
1673 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1674 FindResultCollector c2_dotdot(f2_dotdot);
1675 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1676 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1677 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1678 EXPECT_EQ(c2_dotdot.size(), 3);
1679
initial.commit3f4a7322008-07-27 06:49:38 +09001680 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001681 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001682 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001683 EXPECT_TRUE(c3.HasFile(dir1));
1684 EXPECT_TRUE(c3.HasFile(dir2));
1685 EXPECT_TRUE(c3.HasFile(file1));
1686 EXPECT_TRUE(c3.HasFile(file2_abs));
1687 EXPECT_TRUE(c3.HasFile(dir2file));
1688 EXPECT_TRUE(c3.HasFile(dir2inner));
1689 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1690 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001691
1692 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001693 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001694 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001695 EXPECT_TRUE(c4.HasFile(dir2));
1696 EXPECT_TRUE(c4.HasFile(dir2));
1697 EXPECT_TRUE(c4.HasFile(file1));
1698 EXPECT_TRUE(c4.HasFile(file2_abs));
1699 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001700
1701 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001702 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001703 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001704 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001705 EXPECT_TRUE(c5.HasFile(dir1));
1706 EXPECT_TRUE(c5.HasFile(dir2));
1707 EXPECT_TRUE(c5.HasFile(dir2file));
1708 EXPECT_TRUE(c5.HasFile(dir2inner));
1709 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1710 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001711
1712 // Make sure the destructor closes the find handle while in the middle of a
1713 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001714 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001715 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1716 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001717}
license.botf003cfe2008-08-24 09:55:55 +09001718
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001719TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001720 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001721
1722 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001723 if (file_util::PathExists(data_dir)) {
1724 ASSERT_TRUE(file_util::Delete(data_dir, true));
1725 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001726 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1727
1728 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1729 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1730 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1731 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1732
1733 // Annoyingly, the directories must actually exist in order for realpath(),
1734 // which Contains() relies on in posix, to work.
1735 ASSERT_TRUE(file_util::CreateDirectory(foo));
1736 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001737 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1738 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1739 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001740
1741 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1742 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1743 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1744 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1745
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001746 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001747 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1748#if defined(OS_WIN)
1749 EXPECT_TRUE(file_util::ContainsPath(foo,
1750 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001751 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001752 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001753#elif defined(OS_MACOSX)
1754 // We can't really do this test on OS X since the case-sensitivity of the
1755 // filesystem is configurable.
1756#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001757 EXPECT_FALSE(file_util::ContainsPath(foo,
1758 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001759#endif
1760}
1761
jochen@chromium.orga6879772010-02-18 19:02:26 +09001762TEST_F(FileUtilTest, LastModified) {
1763 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1764
1765 // Create a fresh, empty copy of this directory.
1766 if (file_util::PathExists(data_dir)) {
1767 ASSERT_TRUE(file_util::Delete(data_dir, true));
1768 }
1769 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1770
1771 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1772 std::string data("hello");
1773 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1774
1775 base::Time modification_time;
1776 // Note that this timestamp is divisible by two (seconds) - FAT stores
1777 // modification times with 2s resolution.
1778 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1779 &modification_time));
1780 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1781 file_util::FileInfo file_info;
1782 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1783 ASSERT_TRUE(file_info.last_modified == modification_time);
1784}
1785
tfarina@chromium.org34828222010-05-26 10:40:12 +09001786TEST_F(FileUtilTest, IsDirectoryEmpty) {
1787 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1788
1789 ASSERT_FALSE(file_util::PathExists(empty_dir));
1790
1791 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1792
1793 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1794
1795 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1796 std::string bar("baz");
1797 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1798
1799 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1800}
1801
mark@chromium.org17684802008-09-10 09:16:28 +09001802} // namespace