blob: 7ee762852e209f1c7c2b8497e37670ab6aca8bff [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 +0900242
243static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900244 const FilePath::CharType* path;
245 const FilePath::CharType* suffix;
246 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900247} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900248 {FPL(""), FPL(""), FPL("")},
249 {FPL(""), FPL("txt"), FPL("txt")},
250 {FPL("."), FPL("txt"), FPL("txt.")},
251 {FPL("."), FPL(""), FPL(".")},
252 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
253 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
254 {FPL("foo"), FPL("txt"), FPL("footxt")},
255 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
256 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
257 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
258 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
259 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
260 {FPL("foo"), FPL(""), FPL("foo")},
261 {FPL("foo"), FPL("."), FPL("foo.")},
262 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
263 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900264#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900265 {FPL("\\"), FPL(""), FPL("\\")},
266 {FPL("\\"), FPL("txt"), FPL("\\txt")},
267 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
268 {FPL("\\."), FPL(""), FPL("\\.")},
269 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
270 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
271 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
272 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
273 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
274 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
275 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
276 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
277 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900278#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900279 {FPL("/"), FPL(""), FPL("/")},
280 {FPL("/"), FPL("txt"), FPL("/txt")},
281 {FPL("/."), FPL("txt"), FPL("/txt.")},
282 {FPL("/."), FPL(""), FPL("/.")},
283 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
284 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
285 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
286 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
287 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
288 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
289 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
290 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900291#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900292};
293
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900294#if defined(OS_WIN)
295// This function has been deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900296TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900297 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900298 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900299 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900300 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900301 }
302}
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900303#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900304
305static const struct filename_case {
306 const wchar_t* path;
307 const wchar_t* filename;
308} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900309#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900310 {L"c:\\colon\\backslash", L"backslash"},
311 {L"c:\\colon\\backslash\\", L""},
312 {L"\\\\filename.exe", L"filename.exe"},
313 {L"filename.exe", L"filename.exe"},
314 {L"", L""},
315 {L"\\\\\\", L""},
316 {L"c:/colon/backslash", L"backslash"},
317 {L"c:/colon/backslash/", L""},
318 {L"//////", L""},
319 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900320#elif defined(OS_POSIX)
321 {L"/foo/bar", L"bar"},
322 {L"/foo/bar/", L""},
323 {L"/filename.exe", L"filename.exe"},
324 {L"filename.exe", L"filename.exe"},
325 {L"", L""},
326 {L"/", L""},
327#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900328};
329
evan@chromium.org5a196702010-07-02 08:19:02 +0900330#if defined(OS_WIN)
331// This function is deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900332TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900333 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900334 const filename_case& value = filename_cases[i];
335 std::wstring result = file_util::GetFilenameFromPath(value.path);
336 EXPECT_EQ(value.filename, result);
337 }
338}
evan@chromium.org5a196702010-07-02 08:19:02 +0900339#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900340
341// Test finding the file type from a path name
342static const struct extension_case {
343 const wchar_t* path;
344 const wchar_t* extension;
345} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900346#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900347 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
348 {L"C:\\colon\\backslash\\filename.", L""},
349 {L"C:\\colon\\backslash\\filename", L""},
350 {L"C:\\colon\\backslash\\", L""},
351 {L"C:\\colon\\backslash.\\", L""},
352 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900353#elif defined(OS_POSIX)
354 {L"/foo/bar/filename.extension", L"extension"},
355 {L"/foo/bar/filename.", L""},
356 {L"/foo/bar/filename", L""},
357 {L"/foo/bar/", L""},
358 {L"/foo/bar./", L""},
359 {L"/foo/bar/filename.extension.extension2", L"extension2"},
360 {L".", L""},
361 {L"..", L""},
362 {L"./foo", L""},
363 {L"./foo.extension", L"extension"},
364 {L"/foo.extension1/bar.extension2", L"extension2"},
365#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900366};
367
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900368#if defined(OS_WIN)
369// This function has been deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900370TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900371 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900372 const extension_case& ext = extension_cases[i];
373 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
374 EXPECT_EQ(ext.extension, fext);
375 }
376}
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900377#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900378
379// Test finding the directory component of a path
380static const struct dir_case {
381 const wchar_t* full_path;
382 const wchar_t* directory;
383} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900384#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900385 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
386 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
387 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
388 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
389 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
390 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900391 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900392#elif defined(OS_POSIX)
393 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
394 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
395 {L"/foo/bar/", L"/foo/bar"},
396 {L"/foo/bar//", L"/foo/bar"},
397 {L"/foo/bar", L"/foo"},
398 {L"/foo/bar./", L"/foo/bar."},
399 {L"/", L"/"},
400 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900401 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900402#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900403};
404
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900405#if defined(OS_WIN)
406// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900407TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900408 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900409 const dir_case& dir = dir_cases[i];
410 const std::wstring parent =
411 file_util::GetDirectoryFromPath(dir.full_path);
412 EXPECT_EQ(dir.directory, parent);
413 }
414}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900415#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900416
glider@chromium.org5fd12332010-06-10 22:05:26 +0900417// Flaky, http://crbug.com/46246
glider@chromium.orge1879a22010-06-10 21:40:52 +0900418TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commit3f4a7322008-07-27 06:49:38 +0900419 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900420 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900421 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
422
423 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900424#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900425 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900426#elif defined(OS_POSIX)
427 // We need to wait at least one second here because the precision of
428 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900429 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900430#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900431
432 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900433 base::Time now(base::Time::NowFromSystemTime());
434 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900435
436 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900437 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900438 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
439
440 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900441 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900442
443 // Delete new file, we should see no files after cutoff now
444 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900445 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900446}
447
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900448TEST_F(FileUtilTest, FileAndDirectorySize) {
449 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
450 // should return 53 bytes.
451 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
452 CreateTextFile(file_01, L"12345678901234567890");
453 int64 size_f1 = 0;
454 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
455 EXPECT_EQ(20ll, size_f1);
456
457 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
458 file_util::CreateDirectory(subdir_path);
459
460 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
461 CreateTextFile(file_02, L"123456789012345678901234567890");
462 int64 size_f2 = 0;
463 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
464 EXPECT_EQ(30ll, size_f2);
465
466 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
467 file_util::CreateDirectory(subsubdir_path);
468
469 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
470 CreateTextFile(file_03, L"123");
471
472 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
473 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
rvargas@google.comaa24e112010-06-12 07:53:43 +0900474
475 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("The file*"));
476 EXPECT_EQ(size_f1, computed_size);
477
478 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("bla*"));
479 EXPECT_EQ(0, computed_size);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900480}
481
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900482TEST_F(FileUtilTest, NormalizeFilePathBasic) {
483 // Create a directory under the test dir. Because we create it,
484 // we know it is not a link.
485 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
486 FilePath dir_path = test_dir_.Append(FPL("dir"));
487 FilePath file_b_path = dir_path.Append(FPL("file_b"));
488 file_util::CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900489
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900490 FilePath normalized_file_a_path, normalized_file_b_path;
491 ASSERT_FALSE(file_util::PathExists(file_a_path));
492 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
493 &normalized_file_a_path))
494 << "NormalizeFilePath() should fail on nonexistant paths.";
495
496 CreateTextFile(file_a_path, bogus_content);
497 ASSERT_TRUE(file_util::PathExists(file_a_path));
498 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
499 &normalized_file_a_path));
500
501 CreateTextFile(file_b_path, bogus_content);
502 ASSERT_TRUE(file_util::PathExists(file_b_path));
503 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
504 &normalized_file_b_path));
505
506 // Beacuse this test created |dir_path|, we know it is not a link
507 // or junction. So, the real path of the directory holding file a
508 // must be the parent of the path holding file b.
509 ASSERT_TRUE(normalized_file_a_path.DirName()
510 .IsParent(normalized_file_b_path.DirName()));
511}
512
513#if defined(OS_WIN)
514
515TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
516 // Build the following directory structure:
517 //
518 // test_dir_
519 // |-> base_a
520 // | |-> sub_a
521 // | |-> file.txt
522 // | |-> long_name___... (Very long name.)
523 // | |-> sub_long
524 // | |-> deep.txt
525 // |-> base_b
526 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
527 // |-> to_base_b (reparse point to test_dir_\base_b)
528 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
529
530 FilePath base_a = test_dir_.Append(FPL("base_a"));
531 ASSERT_TRUE(file_util::CreateDirectory(base_a));
532
533 FilePath sub_a = base_a.Append(FPL("sub_a"));
534 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
535
536 FilePath file_txt = sub_a.Append(FPL("file.txt"));
537 CreateTextFile(file_txt, bogus_content);
538
539 // Want a directory whose name is long enough to make the path to the file
540 // inside just under MAX_PATH chars. This will be used to test that when
541 // a junction expands to a path over MAX_PATH chars in length,
542 // NormalizeFilePath() fails without crashing.
543 FilePath sub_long_rel(FPL("sub_long"));
544 FilePath deep_txt(FPL("deep.txt"));
545
546 int target_length = MAX_PATH;
547 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
548 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900549 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900550 // the resulting path is still long enough to hit the failing case in
551 // NormalizePath().
552 const int kCreateDirLimit = 4;
553 target_length -= kCreateDirLimit;
554 FilePath::StringType long_name_str = FPL("long_name_");
555 long_name_str.resize(target_length, '_');
556
557 FilePath long_name = sub_a.Append(FilePath(long_name_str));
558 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
559 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
560
561 FilePath sub_long = deep_file.DirName();
562 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
563 CreateTextFile(deep_file, bogus_content);
564
565 FilePath base_b = test_dir_.Append(FPL("base_b"));
566 ASSERT_TRUE(file_util::CreateDirectory(base_b));
567
568 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
569 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
570 ScopedHandle reparse_to_sub_a(
571 ::CreateFile(to_sub_a.value().c_str(),
572 FILE_ALL_ACCESS,
573 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
574 NULL,
575 OPEN_EXISTING,
576 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
577 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900578 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900579 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
580
581 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
582 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
583 ScopedHandle reparse_to_base_b(
584 ::CreateFile(to_base_b.value().c_str(),
585 FILE_ALL_ACCESS,
586 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
587 NULL,
588 OPEN_EXISTING,
589 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
590 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900591 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900592 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
593
594 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
595 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
596 ScopedHandle reparse_to_sub_long(
597 ::CreateFile(to_sub_long.value().c_str(),
598 FILE_ALL_ACCESS,
599 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
600 NULL,
601 OPEN_EXISTING,
602 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
603 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900604 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900605 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
606
607 // Normalize a junction free path: base_a\sub_a\file.txt .
608 FilePath normalized_path;
609 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
610 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
611
612 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
613 // the junction to_sub_a.
614 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
615 &normalized_path));
616 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
617
618 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
619 // normalized to exclude junctions to_base_b and to_sub_a .
620 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
621 .Append(FPL("to_base_b"))
622 .Append(FPL("to_sub_a"))
623 .Append(FPL("file.txt")),
624 &normalized_path));
625 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
626
627 // A long enough path will cause NormalizeFilePath() to fail. Make a long
628 // path using to_base_b many times, and check that paths long enough to fail
629 // do not cause a crash.
630 FilePath long_path = base_b;
631 const int kLengthLimit = MAX_PATH + 200;
632 while (long_path.value().length() <= kLengthLimit) {
633 long_path = long_path.Append(FPL("to_base_b"));
634 }
635 long_path = long_path.Append(FPL("to_sub_a"))
636 .Append(FPL("file.txt"));
637
638 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
639
640 // Normalizing the junction to deep.txt should fail, because the expanded
641 // path to deep.txt is longer than MAX_PATH.
642 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
643 &normalized_path));
644
645 // Delete the reparse points, and see that NormalizeFilePath() fails
646 // to traverse them.
647 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
648 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
649 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
650
651 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
652 &normalized_path));
653}
654
655#endif // defined(OS_WIN)
656
657// The following test of NormalizeFilePath() require that we create a symlink.
658// This can not be done on windows before vista. On vista, creating a symlink
659// requires privilege "SeCreateSymbolicLinkPrivilege".
660// TODO(skerner): Investigate the possibility of giving base_unittests the
661// privileges required to create a symlink.
662#if defined(OS_POSIX)
663
664bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
665 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
666}
667
668TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
669 FilePath normalized_path;
skerner@chromium.org559baa92010-05-13 00:13:57 +0900670
671 // Link one file to another.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900672 FilePath link_from = test_dir_.Append(FPL("from_file"));
673 FilePath link_to = test_dir_.Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900674 CreateTextFile(link_to, bogus_content);
675
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900676 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900677 << "Failed to create file symlink.";
678
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900679 // Check that NormalizeFilePath sees the link.
680 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900681 ASSERT_TRUE(link_to != link_from);
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900682 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
683 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900684
685 // Link to a directory.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900686 link_from = test_dir_.Append(FPL("from_dir"));
687 link_to = test_dir_.Append(FPL("to_dir"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900688 file_util::CreateDirectory(link_to);
689
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900690 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900691 << "Failed to create directory symlink.";
692
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900693 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
694 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900695
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900696 // Test that a loop in the links causes NormalizeFilePath() to return false.
697 link_from = test_dir_.Append(FPL("link_a"));
698 link_to = test_dir_.Append(FPL("link_b"));
699 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900700 << "Failed to create loop symlink a.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900701 ASSERT_TRUE(MakeSymlink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900702 << "Failed to create loop symlink b.";
703
704 // Infinite loop!
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900705 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900706}
707#endif // defined(OS_POSIX)
708
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900709TEST_F(FileUtilTest, DeleteNonExistent) {
710 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
711 ASSERT_FALSE(file_util::PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900712
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900713 EXPECT_TRUE(file_util::Delete(non_existent, false));
714 ASSERT_FALSE(file_util::PathExists(non_existent));
715 EXPECT_TRUE(file_util::Delete(non_existent, true));
716 ASSERT_FALSE(file_util::PathExists(non_existent));
717}
718
719TEST_F(FileUtilTest, DeleteFile) {
720 // Create a file
721 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
722 CreateTextFile(file_name, bogus_content);
initial.commit3f4a7322008-07-27 06:49:38 +0900723 ASSERT_TRUE(file_util::PathExists(file_name));
724
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900725 // Make sure it's deleted
726 EXPECT_TRUE(file_util::Delete(file_name, false));
727 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900728
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900729 // Test recursive case, create a new file
730 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
731 CreateTextFile(file_name, bogus_content);
732 ASSERT_TRUE(file_util::PathExists(file_name));
733
734 // Make sure it's deleted
735 EXPECT_TRUE(file_util::Delete(file_name, true));
736 EXPECT_FALSE(file_util::PathExists(file_name));
737}
738
739#if defined(OS_WIN)
740// Tests that the Delete function works for wild cards, especially
741// with the recursion flag. Also coincidentally tests PathExists.
742// TODO(erikkay): see if anyone's actually using this feature of the API
743TEST_F(FileUtilTest, DeleteWildCard) {
744 // Create a file and a directory
745 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
746 CreateTextFile(file_name, bogus_content);
747 ASSERT_TRUE(file_util::PathExists(file_name));
748
749 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
750 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900751 ASSERT_TRUE(file_util::PathExists(subdir_path));
752
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900753 // Create the wildcard path
evanm@google.com874d1672008-10-31 08:54:04 +0900754 FilePath directory_contents = test_dir_;
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900755 directory_contents = directory_contents.Append(FPL("*"));
756
initial.commit3f4a7322008-07-27 06:49:38 +0900757 // Delete non-recursively and check that only the file is deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900758 EXPECT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900759 EXPECT_FALSE(file_util::PathExists(file_name));
760 EXPECT_TRUE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900761
zork@chromium.org61be4f42010-05-07 09:05:36 +0900762 // Delete recursively and make sure all contents are deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900763 EXPECT_TRUE(file_util::Delete(directory_contents, true));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900764 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900765 EXPECT_FALSE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900766}
767
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900768// TODO(erikkay): see if anyone's actually using this feature of the API
769TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
770 // Create a file and a directory
771 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
772 file_util::CreateDirectory(subdir_path);
773 ASSERT_TRUE(file_util::PathExists(subdir_path));
774
775 // Create the wildcard path
776 FilePath directory_contents = subdir_path;
777 directory_contents = directory_contents.Append(FPL("*"));
778
779 // Delete non-recursively and check nothing got deleted
780 EXPECT_TRUE(file_util::Delete(directory_contents, false));
781 EXPECT_TRUE(file_util::PathExists(subdir_path));
782
783 // Delete recursively and check nothing got deleted
784 EXPECT_TRUE(file_util::Delete(directory_contents, true));
785 EXPECT_TRUE(file_util::PathExists(subdir_path));
786}
787#endif
788
789// Tests non-recursive Delete() for a directory.
790TEST_F(FileUtilTest, DeleteDirNonRecursive) {
791 // Create a subdirectory and put a file and two directories inside.
792 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
793 file_util::CreateDirectory(test_subdir);
794 ASSERT_TRUE(file_util::PathExists(test_subdir));
795
796 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
797 CreateTextFile(file_name, bogus_content);
798 ASSERT_TRUE(file_util::PathExists(file_name));
799
800 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
801 file_util::CreateDirectory(subdir_path1);
802 ASSERT_TRUE(file_util::PathExists(subdir_path1));
803
804 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
805 file_util::CreateDirectory(subdir_path2);
806 ASSERT_TRUE(file_util::PathExists(subdir_path2));
807
808 // Delete non-recursively and check that the empty dir got deleted
809 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
810 EXPECT_FALSE(file_util::PathExists(subdir_path2));
811
812 // Delete non-recursively and check that nothing got deleted
813 EXPECT_FALSE(file_util::Delete(test_subdir, false));
814 EXPECT_TRUE(file_util::PathExists(test_subdir));
815 EXPECT_TRUE(file_util::PathExists(file_name));
816 EXPECT_TRUE(file_util::PathExists(subdir_path1));
817}
818
819// Tests recursive Delete() for a directory.
820TEST_F(FileUtilTest, DeleteDirRecursive) {
821 // Create a subdirectory and put a file and two directories inside.
822 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
823 file_util::CreateDirectory(test_subdir);
824 ASSERT_TRUE(file_util::PathExists(test_subdir));
825
826 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
827 CreateTextFile(file_name, bogus_content);
828 ASSERT_TRUE(file_util::PathExists(file_name));
829
830 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
831 file_util::CreateDirectory(subdir_path1);
832 ASSERT_TRUE(file_util::PathExists(subdir_path1));
833
834 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
835 file_util::CreateDirectory(subdir_path2);
836 ASSERT_TRUE(file_util::PathExists(subdir_path2));
837
838 // Delete recursively and check that the empty dir got deleted
839 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
840 EXPECT_FALSE(file_util::PathExists(subdir_path2));
841
842 // Delete recursively and check that everything got deleted
843 EXPECT_TRUE(file_util::Delete(test_subdir, true));
844 EXPECT_FALSE(file_util::PathExists(file_name));
845 EXPECT_FALSE(file_util::PathExists(subdir_path1));
846 EXPECT_FALSE(file_util::PathExists(test_subdir));
847}
848
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900849TEST_F(FileUtilTest, MoveFileNew) {
850 // Create a file
851 FilePath file_name_from =
852 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
853 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
854 ASSERT_TRUE(file_util::PathExists(file_name_from));
855
856 // The destination
857 FilePath file_name_to =
858 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
859 ASSERT_FALSE(file_util::PathExists(file_name_to));
860
861 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
862
863 // Check everything has been moved.
864 EXPECT_FALSE(file_util::PathExists(file_name_from));
865 EXPECT_TRUE(file_util::PathExists(file_name_to));
866}
867
868TEST_F(FileUtilTest, MoveFileExists) {
869 // Create a file
870 FilePath file_name_from =
871 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
872 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
873 ASSERT_TRUE(file_util::PathExists(file_name_from));
874
875 // The destination name
876 FilePath file_name_to =
877 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
878 CreateTextFile(file_name_to, L"Old file content");
879 ASSERT_TRUE(file_util::PathExists(file_name_to));
880
881 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
882
883 // Check everything has been moved.
884 EXPECT_FALSE(file_util::PathExists(file_name_from));
885 EXPECT_TRUE(file_util::PathExists(file_name_to));
886 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
887}
888
889TEST_F(FileUtilTest, MoveFileDirExists) {
890 // Create a file
891 FilePath file_name_from =
892 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
893 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
894 ASSERT_TRUE(file_util::PathExists(file_name_from));
895
896 // The destination directory
897 FilePath dir_name_to =
898 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
899 file_util::CreateDirectory(dir_name_to);
900 ASSERT_TRUE(file_util::PathExists(dir_name_to));
901
902 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
903}
904
905
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900906TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900907 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900908 FilePath dir_name_from =
909 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
910 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900911 ASSERT_TRUE(file_util::PathExists(dir_name_from));
912
913 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900914 FilePath file_name_from =
915 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900916 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
917 ASSERT_TRUE(file_util::PathExists(file_name_from));
918
919 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900920 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
921 FilePath file_name_to =
922 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900923
924 ASSERT_FALSE(file_util::PathExists(dir_name_to));
925
926 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
927
928 // Check everything has been moved.
929 EXPECT_FALSE(file_util::PathExists(dir_name_from));
930 EXPECT_FALSE(file_util::PathExists(file_name_from));
931 EXPECT_TRUE(file_util::PathExists(dir_name_to));
932 EXPECT_TRUE(file_util::PathExists(file_name_to));
933}
934
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900935TEST_F(FileUtilTest, MoveExist) {
936 // Create a directory
937 FilePath dir_name_from =
938 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
939 file_util::CreateDirectory(dir_name_from);
940 ASSERT_TRUE(file_util::PathExists(dir_name_from));
941
942 // Create a file under the directory
943 FilePath file_name_from =
944 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
945 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
946 ASSERT_TRUE(file_util::PathExists(file_name_from));
947
948 // Move the directory
949 FilePath dir_name_exists =
950 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
951
952 FilePath dir_name_to =
953 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
954 FilePath file_name_to =
955 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
956
957 // Create the destination directory.
958 file_util::CreateDirectory(dir_name_exists);
959 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
960
961 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
962
963 // Check everything has been moved.
964 EXPECT_FALSE(file_util::PathExists(dir_name_from));
965 EXPECT_FALSE(file_util::PathExists(file_name_from));
966 EXPECT_TRUE(file_util::PathExists(dir_name_to));
967 EXPECT_TRUE(file_util::PathExists(file_name_to));
968}
969
970TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900971 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900972 FilePath dir_name_from =
973 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
974 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900975 ASSERT_TRUE(file_util::PathExists(dir_name_from));
976
977 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900978 FilePath file_name_from =
979 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900980 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
981 ASSERT_TRUE(file_util::PathExists(file_name_from));
982
983 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900984 FilePath subdir_name_from =
985 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
986 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900987 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
988
989 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900990 FilePath file_name2_from =
991 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900992 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
993 ASSERT_TRUE(file_util::PathExists(file_name2_from));
994
995 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900996 FilePath dir_name_to =
997 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
998 FilePath file_name_to =
999 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1000 FilePath subdir_name_to =
1001 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1002 FilePath file_name2_to =
1003 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001004
1005 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1006
1007 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
1008
1009 // Check everything has been copied.
1010 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1011 EXPECT_TRUE(file_util::PathExists(file_name_from));
1012 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1013 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1014 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1015 EXPECT_TRUE(file_util::PathExists(file_name_to));
1016 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1017 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1018}
1019
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001020TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1021 // Create a directory.
1022 FilePath dir_name_from =
1023 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1024 file_util::CreateDirectory(dir_name_from);
1025 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1026
1027 // Create a file under the directory.
1028 FilePath file_name_from =
1029 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1030 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1031 ASSERT_TRUE(file_util::PathExists(file_name_from));
1032
1033 // Create a subdirectory.
1034 FilePath subdir_name_from =
1035 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1036 file_util::CreateDirectory(subdir_name_from);
1037 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1038
1039 // Create a file under the subdirectory.
1040 FilePath file_name2_from =
1041 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1042 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1043 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1044
1045 // Copy the directory recursively.
1046 FilePath dir_name_exists =
1047 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1048
1049 FilePath dir_name_to =
1050 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1051 FilePath file_name_to =
1052 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1053 FilePath subdir_name_to =
1054 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1055 FilePath file_name2_to =
1056 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1057
1058 // Create the destination directory.
1059 file_util::CreateDirectory(dir_name_exists);
1060 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1061
1062 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1063
1064 // Check everything has been copied.
1065 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1066 EXPECT_TRUE(file_util::PathExists(file_name_from));
1067 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1068 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1069 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1070 EXPECT_TRUE(file_util::PathExists(file_name_to));
1071 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1072 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1073}
1074
1075TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001076 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001077 FilePath dir_name_from =
1078 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1079 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001080 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1081
1082 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001083 FilePath file_name_from =
1084 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001085 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1086 ASSERT_TRUE(file_util::PathExists(file_name_from));
1087
1088 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001089 FilePath subdir_name_from =
1090 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1091 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001092 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1093
1094 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001095 FilePath file_name2_from =
1096 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001097 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1098 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1099
1100 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001101 FilePath dir_name_to =
1102 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1103 FilePath file_name_to =
1104 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1105 FilePath subdir_name_to =
1106 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001107
1108 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1109
1110 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1111
1112 // Check everything has been copied.
1113 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1114 EXPECT_TRUE(file_util::PathExists(file_name_from));
1115 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1116 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1117 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1118 EXPECT_TRUE(file_util::PathExists(file_name_to));
1119 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1120}
1121
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001122TEST_F(FileUtilTest, CopyDirectoryExists) {
1123 // Create a directory.
1124 FilePath dir_name_from =
1125 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1126 file_util::CreateDirectory(dir_name_from);
1127 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1128
1129 // Create a file under the directory.
1130 FilePath file_name_from =
1131 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1132 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1133 ASSERT_TRUE(file_util::PathExists(file_name_from));
1134
1135 // Create a subdirectory.
1136 FilePath subdir_name_from =
1137 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1138 file_util::CreateDirectory(subdir_name_from);
1139 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1140
1141 // Create a file under the subdirectory.
1142 FilePath file_name2_from =
1143 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1144 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1145 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1146
1147 // Copy the directory not recursively.
1148 FilePath dir_name_to =
1149 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1150 FilePath file_name_to =
1151 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1152 FilePath subdir_name_to =
1153 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1154
1155 // Create the destination directory.
1156 file_util::CreateDirectory(dir_name_to);
1157 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1158
1159 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1160
1161 // Check everything has been copied.
1162 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1163 EXPECT_TRUE(file_util::PathExists(file_name_from));
1164 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1165 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1166 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1167 EXPECT_TRUE(file_util::PathExists(file_name_to));
1168 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1169}
1170
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001171TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1172 // Create a file
1173 FilePath file_name_from =
1174 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1175 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1176 ASSERT_TRUE(file_util::PathExists(file_name_from));
1177
1178 // The destination name
1179 FilePath file_name_to =
1180 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1181 ASSERT_FALSE(file_util::PathExists(file_name_to));
1182
1183 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1184
1185 // Check the has been copied
1186 EXPECT_TRUE(file_util::PathExists(file_name_to));
1187}
1188
1189TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1190 // Create a file
1191 FilePath file_name_from =
1192 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1193 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1194 ASSERT_TRUE(file_util::PathExists(file_name_from));
1195
1196 // The destination name
1197 FilePath file_name_to =
1198 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1199 CreateTextFile(file_name_to, L"Old file content");
1200 ASSERT_TRUE(file_util::PathExists(file_name_to));
1201
1202 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1203
1204 // Check the has been copied
1205 EXPECT_TRUE(file_util::PathExists(file_name_to));
1206 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1207}
1208
1209TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1210 // Create a file
1211 FilePath file_name_from =
1212 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1213 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1214 ASSERT_TRUE(file_util::PathExists(file_name_from));
1215
1216 // The destination
1217 FilePath dir_name_to =
1218 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1219 file_util::CreateDirectory(dir_name_to);
1220 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1221 FilePath file_name_to =
1222 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1223
1224 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1225
1226 // Check the has been copied
1227 EXPECT_TRUE(file_util::PathExists(file_name_to));
1228}
1229
initial.commit3f4a7322008-07-27 06:49:38 +09001230TEST_F(FileUtilTest, CopyFile) {
1231 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001232 FilePath dir_name_from =
1233 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1234 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001235 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1236
1237 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001238 FilePath file_name_from =
1239 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001240 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1241 CreateTextFile(file_name_from, file_contents);
1242 ASSERT_TRUE(file_util::PathExists(file_name_from));
1243
1244 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001245 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001246 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001247
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001248 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001249 FilePath dest_file2(dir_name_from);
1250 dest_file2 = dest_file2.AppendASCII("..");
1251 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1252 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1253
1254 FilePath dest_file2_test(dir_name_from);
1255 dest_file2_test = dest_file2_test.DirName();
1256 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001257
1258 // Check everything has been copied.
1259 EXPECT_TRUE(file_util::PathExists(file_name_from));
1260 EXPECT_TRUE(file_util::PathExists(dest_file));
1261 const std::wstring read_contents = ReadTextFile(dest_file);
1262 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +09001263 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1264 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001265}
1266
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001267// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +09001268#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001269TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +09001270 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001271
1272 SYSTEMTIME start_time;
1273 GetLocalTime(&start_time);
1274 Sleep(100);
1275 CreateTextFile(file_name, L"New file!");
1276 Sleep(100);
1277 SYSTEMTIME end_time;
1278 GetLocalTime(&end_time);
1279
1280 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +09001281 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +09001282
1283 FILETIME start_filetime;
1284 SystemTimeToFileTime(&start_time, &start_filetime);
1285 FILETIME end_filetime;
1286 SystemTimeToFileTime(&end_time, &end_filetime);
1287 FILETIME file_creation_filetime;
1288 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1289
1290 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1291 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1292 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1293
1294 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1295 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1296 "end time: " << FileTimeAsUint64(end_filetime);
1297
evanm@google.com874d1672008-10-31 08:54:04 +09001298 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +09001299}
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001300#endif
initial.commit3f4a7322008-07-27 06:49:38 +09001301
erikkay@google.comf2406842008-08-21 00:59:49 +09001302// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001303// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001304typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001305
erikkay@google.comf2406842008-08-21 00:59:49 +09001306TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001307 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +09001308 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +09001309 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1310 .Append(FILE_PATH_LITERAL("data"))
1311 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +09001312 ASSERT_TRUE(file_util::PathExists(data_dir));
1313
evanm@google.com874d1672008-10-31 08:54:04 +09001314 FilePath original_file =
1315 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1316 FilePath same_file =
1317 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1318 FilePath same_length_file =
1319 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1320 FilePath different_file =
1321 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1322 FilePath different_first_file =
1323 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1324 FilePath different_last_file =
1325 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1326 FilePath empty1_file =
1327 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1328 FilePath empty2_file =
1329 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1330 FilePath shortened_file =
1331 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1332 FilePath binary_file =
1333 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1334 FilePath binary_file_same =
1335 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1336 FilePath binary_file_diff =
1337 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001338
1339 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1340 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1341 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1342 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +09001343 EXPECT_FALSE(file_util::ContentsEqual(
1344 FilePath(FILE_PATH_LITERAL("bogusname")),
1345 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +09001346 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1347 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1348 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1349 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1350 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1351 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1352 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1353}
1354
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001355TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1356 FilePath data_dir;
1357 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1358 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1359 .Append(FILE_PATH_LITERAL("data"))
1360 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1361 ASSERT_TRUE(file_util::PathExists(data_dir));
1362
1363 FilePath original_file =
1364 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1365 FilePath same_file =
1366 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1367 FilePath crlf_file =
1368 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1369 FilePath shortened_file =
1370 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1371 FilePath different_file =
1372 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1373 FilePath different_first_file =
1374 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1375 FilePath different_last_file =
1376 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1377 FilePath first1_file =
1378 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1379 FilePath first2_file =
1380 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1381 FilePath empty1_file =
1382 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1383 FilePath empty2_file =
1384 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1385 FilePath blank_line_file =
1386 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1387 FilePath blank_line_crlf_file =
1388 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1389
1390 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1391 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1392 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1393 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1394 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1395 different_first_file));
1396 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1397 different_last_file));
1398 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1399 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1400 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1401 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1402 blank_line_crlf_file));
1403}
1404
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001405// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001406#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001407TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001408 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001409 CreateTextFile(target_file, L"This is the target.");
1410
evanm@google.com874d1672008-10-31 08:54:04 +09001411 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001412
1413 HRESULT result;
1414 IShellLink *shell = NULL;
1415 IPersistFile *persist = NULL;
1416
1417 CoInitialize(NULL);
1418 // Temporarily create a shortcut for test
1419 result = CoCreateInstance(CLSID_ShellLink, NULL,
1420 CLSCTX_INPROC_SERVER, IID_IShellLink,
1421 reinterpret_cast<LPVOID*>(&shell));
1422 EXPECT_TRUE(SUCCEEDED(result));
1423 result = shell->QueryInterface(IID_IPersistFile,
1424 reinterpret_cast<LPVOID*>(&persist));
1425 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001426 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001427 EXPECT_TRUE(SUCCEEDED(result));
1428 result = shell->SetDescription(L"ResolveShortcutTest");
1429 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001430 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001431 EXPECT_TRUE(SUCCEEDED(result));
1432 if (persist)
1433 persist->Release();
1434 if (shell)
1435 shell->Release();
1436
1437 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001438 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001439 EXPECT_TRUE(is_solved);
1440 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001441 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001442 EXPECT_EQ(L"This is the target.", contents);
1443
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001444 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001445 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001446 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001447 CoUninitialize();
1448}
1449
1450TEST_F(FileUtilTest, CreateShortcutTest) {
1451 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001452 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001453 CreateTextFile(target_file, file_contents);
1454
evanm@google.com874d1672008-10-31 08:54:04 +09001455 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001456
1457 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001458 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1459 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001460 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001461 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001462 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001463 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001464 EXPECT_EQ(file_contents, read_contents);
1465
evanm@google.com874d1672008-10-31 08:54:04 +09001466 DeleteFile(target_file.value().c_str());
1467 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001468 CoUninitialize();
1469}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001470
1471TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1472 // Create a directory
1473 FilePath dir_name_from =
1474 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1475 file_util::CreateDirectory(dir_name_from);
1476 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1477
1478 // Create a file under the directory
1479 FilePath file_name_from =
1480 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1481 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1482 ASSERT_TRUE(file_util::PathExists(file_name_from));
1483
1484 // Move the directory by using CopyAndDeleteDirectory
1485 FilePath dir_name_to = test_dir_.Append(
1486 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1487 FilePath file_name_to =
1488 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1489
1490 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1491
1492 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1493
1494 // Check everything has been moved.
1495 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1496 EXPECT_FALSE(file_util::PathExists(file_name_from));
1497 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1498 EXPECT_TRUE(file_util::PathExists(file_name_to));
1499}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001500
1501TEST_F(FileUtilTest, GetTempDirTest) {
1502 static const TCHAR* kTmpKey = _T("TMP");
1503 static const TCHAR* kTmpValues[] = {
1504 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1505 };
1506 // Save the original $TMP.
1507 size_t original_tmp_size;
1508 TCHAR* original_tmp;
1509 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1510 // original_tmp may be NULL.
1511
1512 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1513 FilePath path;
1514 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1515 file_util::GetTempDir(&path);
1516 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1517 " result=" << path.value();
1518 }
1519
1520 // Restore the original $TMP.
1521 if (original_tmp) {
1522 ::_tputenv_s(kTmpKey, original_tmp);
1523 free(original_tmp);
1524 } else {
1525 ::_tputenv_s(kTmpKey, _T(""));
1526 }
1527}
1528#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001529
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001530TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1531 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001532 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001533 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001534 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1535 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1536 }
1537 for (int i = 0; i < 3; i++)
1538 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1539 for (int i = 0; i < 3; i++)
1540 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1541}
1542
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001543TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001544 FilePath names[3];
1545 FILE *fps[3];
1546 int i;
1547
1548 // Create; make sure they are open and exist.
1549 for (i = 0; i < 3; ++i) {
1550 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1551 ASSERT_TRUE(fps[i]);
1552 EXPECT_TRUE(file_util::PathExists(names[i]));
1553 }
1554
1555 // Make sure all names are unique.
1556 for (i = 0; i < 3; ++i) {
1557 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1558 }
1559
1560 // Close and delete.
1561 for (i = 0; i < 3; ++i) {
1562 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1563 EXPECT_TRUE(file_util::Delete(names[i], false));
1564 }
initial.commit3f4a7322008-07-27 06:49:38 +09001565}
1566
1567TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001568 FilePath temp_dir;
1569 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1570 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001571 EXPECT_TRUE(file_util::PathExists(temp_dir));
1572 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001573}
1574
skerner@chromium.orge4432392010-05-01 02:00:09 +09001575TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1576 FilePath new_dir;
1577 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1578 test_dir_,
1579 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001580 &new_dir));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001581 EXPECT_TRUE(file_util::PathExists(new_dir));
1582 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1583 EXPECT_TRUE(file_util::Delete(new_dir, false));
1584}
1585
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001586TEST_F(FileUtilTest, GetShmemTempDirTest) {
1587 FilePath dir;
1588 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1589 EXPECT_TRUE(file_util::DirectoryExists(dir));
1590}
1591
initial.commit3f4a7322008-07-27 06:49:38 +09001592TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001593 FilePath test_root =
1594 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001595#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001596 FilePath test_path =
1597 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001598#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001599 FilePath test_path =
1600 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001601#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001602
1603 EXPECT_FALSE(file_util::PathExists(test_path));
1604 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1605 EXPECT_TRUE(file_util::PathExists(test_path));
1606 // CreateDirectory returns true if the DirectoryExists returns true.
1607 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1608
1609 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001610 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001611 EXPECT_FALSE(file_util::PathExists(test_path));
1612 CreateTextFile(test_path, L"test file");
1613 EXPECT_TRUE(file_util::PathExists(test_path));
1614 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1615
1616 EXPECT_TRUE(file_util::Delete(test_root, true));
1617 EXPECT_FALSE(file_util::PathExists(test_root));
1618 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001619
1620 // Verify assumptions made by the Windows implementation:
1621 // 1. The current directory always exists.
1622 // 2. The root directory always exists.
1623 ASSERT_TRUE(file_util::DirectoryExists(
1624 FilePath(FilePath::kCurrentDirectory)));
1625 FilePath top_level = test_root;
1626 while (top_level != top_level.DirName()) {
1627 top_level = top_level.DirName();
1628 }
1629 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1630
1631 // Given these assumptions hold, it should be safe to
1632 // test that "creating" these directories succeeds.
1633 EXPECT_TRUE(file_util::CreateDirectory(
1634 FilePath(FilePath::kCurrentDirectory)));
1635 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001636
1637#if defined(OS_WIN)
1638 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1639 FilePath invalid_path =
1640 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1641 if (!file_util::PathExists(invalid_drive)) {
1642 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1643 }
1644#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001645}
1646
1647TEST_F(FileUtilTest, DetectDirectoryTest) {
1648 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001649 FilePath test_root =
1650 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001651 EXPECT_FALSE(file_util::PathExists(test_root));
1652 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1653 EXPECT_TRUE(file_util::PathExists(test_root));
1654 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1655
1656 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001657 FilePath test_path =
1658 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001659 EXPECT_FALSE(file_util::PathExists(test_path));
1660 CreateTextFile(test_path, L"test file");
1661 EXPECT_TRUE(file_util::PathExists(test_path));
1662 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1663 EXPECT_TRUE(file_util::Delete(test_path, false));
1664
1665 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001666}
1667
initial.commit3f4a7322008-07-27 06:49:38 +09001668TEST_F(FileUtilTest, FileEnumeratorTest) {
1669 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001670 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001671 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1672 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001673
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001674 // Test an empty directory, non-recursively, including "..".
1675 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1676 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1677 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1678 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1679 f0_dotdot.Next().value());
1680 EXPECT_EQ(FILE_PATH_LITERAL(""),
1681 f0_dotdot.Next().value());
1682
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001683 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001684 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001685 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001686 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001687 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001688 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001689 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001690
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001691 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001692 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001693 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001694 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001695 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001696 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001697 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001698 FilePath file2_rel =
1699 dir2.Append(FilePath::kParentDirectory)
1700 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001701 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001702 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001703
1704 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001705 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001706 file_util::FileEnumerator::FILES);
1707 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001708 EXPECT_TRUE(c1.HasFile(file1));
1709 EXPECT_TRUE(c1.HasFile(file2_abs));
1710 EXPECT_TRUE(c1.HasFile(dir2file));
1711 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1712 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001713
1714 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001715 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001716 file_util::FileEnumerator::DIRECTORIES);
1717 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001718 EXPECT_TRUE(c2.HasFile(dir1));
1719 EXPECT_TRUE(c2.HasFile(dir2));
1720 EXPECT_TRUE(c2.HasFile(dir2inner));
1721 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001722
tim@chromium.org989d0972008-10-16 11:42:45 +09001723 // Only enumerate directories non-recursively.
1724 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001725 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001726 FindResultCollector c2_non_recursive(f2_non_recursive);
1727 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1728 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1729 EXPECT_EQ(c2_non_recursive.size(), 2);
1730
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001731 // Only enumerate directories, non-recursively, including "..".
1732 file_util::FileEnumerator f2_dotdot(
1733 test_dir_, false,
1734 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1735 file_util::FileEnumerator::DIRECTORIES |
1736 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1737 FindResultCollector c2_dotdot(f2_dotdot);
1738 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1739 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1740 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1741 EXPECT_EQ(c2_dotdot.size(), 3);
1742
initial.commit3f4a7322008-07-27 06:49:38 +09001743 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001744 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001745 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001746 EXPECT_TRUE(c3.HasFile(dir1));
1747 EXPECT_TRUE(c3.HasFile(dir2));
1748 EXPECT_TRUE(c3.HasFile(file1));
1749 EXPECT_TRUE(c3.HasFile(file2_abs));
1750 EXPECT_TRUE(c3.HasFile(dir2file));
1751 EXPECT_TRUE(c3.HasFile(dir2inner));
1752 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1753 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001754
1755 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001756 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001757 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001758 EXPECT_TRUE(c4.HasFile(dir2));
1759 EXPECT_TRUE(c4.HasFile(dir2));
1760 EXPECT_TRUE(c4.HasFile(file1));
1761 EXPECT_TRUE(c4.HasFile(file2_abs));
1762 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001763
1764 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001765 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001766 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001767 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001768 EXPECT_TRUE(c5.HasFile(dir1));
1769 EXPECT_TRUE(c5.HasFile(dir2));
1770 EXPECT_TRUE(c5.HasFile(dir2file));
1771 EXPECT_TRUE(c5.HasFile(dir2inner));
1772 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1773 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001774
1775 // Make sure the destructor closes the find handle while in the middle of a
1776 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001777 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001778 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1779 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001780}
license.botf003cfe2008-08-24 09:55:55 +09001781
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001782TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001783 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001784
1785 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001786 if (file_util::PathExists(data_dir)) {
1787 ASSERT_TRUE(file_util::Delete(data_dir, true));
1788 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001789 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1790
1791 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1792 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1793 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1794 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1795
1796 // Annoyingly, the directories must actually exist in order for realpath(),
1797 // which Contains() relies on in posix, to work.
1798 ASSERT_TRUE(file_util::CreateDirectory(foo));
1799 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001800 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1801 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1802 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001803
1804 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1805 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1806 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1807 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1808
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001809 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001810 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1811#if defined(OS_WIN)
1812 EXPECT_TRUE(file_util::ContainsPath(foo,
1813 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001814 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001815 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001816#elif defined(OS_MACOSX)
1817 // We can't really do this test on OS X since the case-sensitivity of the
1818 // filesystem is configurable.
1819#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001820 EXPECT_FALSE(file_util::ContainsPath(foo,
1821 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001822#endif
1823}
1824
jochen@chromium.orga6879772010-02-18 19:02:26 +09001825TEST_F(FileUtilTest, LastModified) {
1826 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1827
1828 // Create a fresh, empty copy of this directory.
1829 if (file_util::PathExists(data_dir)) {
1830 ASSERT_TRUE(file_util::Delete(data_dir, true));
1831 }
1832 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1833
1834 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1835 std::string data("hello");
1836 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1837
1838 base::Time modification_time;
1839 // Note that this timestamp is divisible by two (seconds) - FAT stores
1840 // modification times with 2s resolution.
1841 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1842 &modification_time));
1843 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1844 file_util::FileInfo file_info;
1845 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1846 ASSERT_TRUE(file_info.last_modified == modification_time);
1847}
1848
tfarina@chromium.org34828222010-05-26 10:40:12 +09001849TEST_F(FileUtilTest, IsDirectoryEmpty) {
1850 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1851
1852 ASSERT_FALSE(file_util::PathExists(empty_dir));
1853
1854 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1855
1856 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1857
1858 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1859 std::string bar("baz");
1860 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1861
1862 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1863}
1864
mark@chromium.org17684802008-09-10 09:16:28 +09001865} // namespace