blob: 7acf00c4640bb2b1ad58f981ff1d32aea194423e [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>
16#include <iostream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090017#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090018
19#include "base/base_paths.h"
evanm@google.com874d1672008-10-31 08:54:04 +090020#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090021#include "base/file_util.h"
22#include "base/logging.h"
23#include "base/path_service.h"
erikkay@google.com8d133f62009-04-24 00:05:19 +090024#include "base/platform_thread.h"
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090025#include "base/scoped_handle.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090026#include "base/time.h"
brettw@chromium.org50c94652009-10-07 11:10:20 +090027#include "base/utf_string_conversions.h"
initial.commit3f4a7322008-07-27 06:49:38 +090028#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090029#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090030
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090031// This macro helps avoid wrapped lines in the test structs.
32#define FPL(x) FILE_PATH_LITERAL(x)
33
initial.commit3f4a7322008-07-27 06:49:38 +090034namespace {
35
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090036// To test that file_util::Normalize FilePath() deals with NTFS reparse points
37// correctly, we need functions to create and delete reparse points.
38#if defined(OS_WIN)
39typedef struct _REPARSE_DATA_BUFFER {
40 ULONG ReparseTag;
41 USHORT ReparseDataLength;
42 USHORT Reserved;
43 union {
44 struct {
45 USHORT SubstituteNameOffset;
46 USHORT SubstituteNameLength;
47 USHORT PrintNameOffset;
48 USHORT PrintNameLength;
49 ULONG Flags;
50 WCHAR PathBuffer[1];
51 } SymbolicLinkReparseBuffer;
52 struct {
53 USHORT SubstituteNameOffset;
54 USHORT SubstituteNameLength;
55 USHORT PrintNameOffset;
56 USHORT PrintNameLength;
57 WCHAR PathBuffer[1];
58 } MountPointReparseBuffer;
59 struct {
60 UCHAR DataBuffer[1];
61 } GenericReparseBuffer;
62 };
63} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
64
65// Sets a reparse point. |source| will now point to |target|. Returns true if
66// the call succeeds, false otherwise.
67bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
68 std::wstring kPathPrefix = L"\\??\\";
69 std::wstring target_str;
70 // The juction will not work if the target path does not start with \??\ .
71 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
72 target_str += kPathPrefix;
73 target_str += target_path.value();
74 const wchar_t* target = target_str.c_str();
75 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
76 char buffer[2000] = {0};
77 DWORD returned;
78
79 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
80
81 data->ReparseTag = 0xa0000003;
82 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
83
84 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
85 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
86 data->ReparseDataLength = size_target + 4 + 8;
87
88 int data_size = data->ReparseDataLength + 8;
89
90 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
91 NULL, 0, &returned, NULL)) {
92 return false;
93 }
94 return true;
95}
96
97// Delete the reparse point referenced by |source|. Returns true if the call
98// succeeds, false otherwise.
99bool DeleteReparsePoint(HANDLE source) {
100 DWORD returned;
101 REPARSE_DATA_BUFFER data = {0};
102 data.ReparseTag = 0xa0000003;
103 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
104 &returned, NULL)) {
105 return false;
106 }
107 return true;
108}
109#endif
110
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900111const wchar_t bogus_content[] = L"I'm cannon fodder.";
112
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900113const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
114 static_cast<file_util::FileEnumerator::FILE_TYPE>(
115 file_util::FileEnumerator::FILES |
116 file_util::FileEnumerator::DIRECTORIES);
117
erikkay@google.comf2406842008-08-21 00:59:49 +0900118// file_util winds up using autoreleased objects on the Mac, so this needs
119// to be a PlatformTest
120class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900121 protected:
122 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +0900123 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +0900124 // Name a subdirectory of the temp directory.
125 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +0900126 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900127
128 // Create a fresh, empty copy of this directory.
129 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +0900130 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +0900131 }
132 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +0900133 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +0900134 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900135 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900136 ASSERT_FALSE(file_util::PathExists(test_dir_));
137 }
138
139 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +0900140 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900141};
142
143// Collects all the results from the given file enumerator, and provides an
144// interface to query whether a given file is present.
145class FindResultCollector {
146 public:
evan@chromium.org1543ad32009-08-27 05:00:14 +0900147 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900148 FilePath cur_file;
149 while (!(cur_file = enumerator.Next()).value().empty()) {
150 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900151 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900152 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900153 << "Same file returned twice";
154
155 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900156 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900157 }
158 }
159
160 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900161 bool HasFile(const FilePath& file) const {
162 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900163 }
evanm@google.com874d1672008-10-31 08:54:04 +0900164
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900165 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900166 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900167 }
initial.commit3f4a7322008-07-27 06:49:38 +0900168
169 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900170 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900171};
172
173// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900174void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900175 const std::wstring& contents) {
176 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900177 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900178 ASSERT_TRUE(file.is_open());
179 file << contents;
180 file.close();
181}
182
183// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900184std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900185 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900186 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900187 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900188 EXPECT_TRUE(file.is_open());
189 file.getline(contents, 64);
190 file.close();
191 return std::wstring(contents);
192}
193
erikkay@google.com014161d2008-08-16 02:45:13 +0900194#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900195uint64 FileTimeAsUint64(const FILETIME& ft) {
196 ULARGE_INTEGER u;
197 u.LowPart = ft.dwLowDateTime;
198 u.HighPart = ft.dwHighDateTime;
199 return u.QuadPart;
200}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900201#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900202
203const struct append_case {
204 const wchar_t* path;
205 const wchar_t* ending;
206 const wchar_t* result;
207} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900208#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900209 {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"path", L"c:\\colon\\backslash\\\\path"},
212 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
213 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
214 {L"", L"path", L"\\path"},
215 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900216#elif defined(OS_POSIX)
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"path", L"/foo/bar//path"},
220 {L"/foo/bar/", L"", L"/foo/bar/"},
221 {L"/foo/bar", L"", L"/foo/bar/"},
222 {L"", L"path", L"/path"},
223 {L"", L"", L"/"},
224#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900225};
226
evan@chromium.org1db7f942010-02-27 00:11:55 +0900227#if defined(OS_WIN)
228// This function is deprecated, but still used on Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900229TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900230 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900231 const append_case& value = append_cases[i];
232 std::wstring result = value.path;
233 file_util::AppendToPath(&result, value.ending);
234 EXPECT_EQ(value.result, result);
235 }
236
237#ifdef NDEBUG
238 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
239#endif
240}
evan@chromium.org1db7f942010-02-27 00:11:55 +0900241#endif // defined(OS_WIN)
242
initial.commit3f4a7322008-07-27 06:49:38 +0900243
244static const struct InsertBeforeExtensionCase {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900245 const FilePath::CharType* path;
246 const FilePath::CharType* suffix;
247 const FilePath::CharType* result;
initial.commit3f4a7322008-07-27 06:49:38 +0900248} kInsertBeforeExtension[] = {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900249 {FPL(""), FPL(""), FPL("")},
250 {FPL(""), FPL("txt"), FPL("txt")},
251 {FPL("."), FPL("txt"), FPL("txt.")},
252 {FPL("."), FPL(""), FPL(".")},
253 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
254 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
255 {FPL("foo"), FPL("txt"), FPL("footxt")},
256 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
257 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
258 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
259 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
260 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
261 {FPL("foo"), FPL(""), FPL("foo")},
262 {FPL("foo"), FPL("."), FPL("foo.")},
263 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
264 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
erikkay@google.com014161d2008-08-16 02:45:13 +0900265#if defined(OS_WIN)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900266 {FPL("\\"), FPL(""), FPL("\\")},
267 {FPL("\\"), FPL("txt"), FPL("\\txt")},
268 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
269 {FPL("\\."), FPL(""), FPL("\\.")},
270 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
271 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
272 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
273 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
274 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
275 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
276 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
277 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
278 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900279#elif defined(OS_POSIX)
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900280 {FPL("/"), FPL(""), FPL("/")},
281 {FPL("/"), FPL("txt"), FPL("/txt")},
282 {FPL("/."), FPL("txt"), FPL("/txt.")},
283 {FPL("/."), FPL(""), FPL("/.")},
284 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
285 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
286 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
287 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
288 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
289 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
290 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
291 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900292#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900293};
294
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900295#if defined(OS_WIN)
296// This function has been deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900297TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900298 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900299 FilePath path(kInsertBeforeExtension[i].path);
initial.commit3f4a7322008-07-27 06:49:38 +0900300 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +0900301 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commit3f4a7322008-07-27 06:49:38 +0900302 }
303}
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900304#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900305
306static const struct filename_case {
307 const wchar_t* path;
308 const wchar_t* filename;
309} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900310#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900311 {L"c:\\colon\\backslash", L"backslash"},
312 {L"c:\\colon\\backslash\\", L""},
313 {L"\\\\filename.exe", L"filename.exe"},
314 {L"filename.exe", L"filename.exe"},
315 {L"", L""},
316 {L"\\\\\\", L""},
317 {L"c:/colon/backslash", L"backslash"},
318 {L"c:/colon/backslash/", L""},
319 {L"//////", L""},
320 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900321#elif defined(OS_POSIX)
322 {L"/foo/bar", L"bar"},
323 {L"/foo/bar/", L""},
324 {L"/filename.exe", L"filename.exe"},
325 {L"filename.exe", L"filename.exe"},
326 {L"", L""},
327 {L"/", L""},
328#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900329};
330
evan@chromium.org5a196702010-07-02 08:19:02 +0900331#if defined(OS_WIN)
332// This function is deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900333TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900334 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900335 const filename_case& value = filename_cases[i];
336 std::wstring result = file_util::GetFilenameFromPath(value.path);
337 EXPECT_EQ(value.filename, result);
338 }
339}
evan@chromium.org5a196702010-07-02 08:19:02 +0900340#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900341
342// Test finding the file type from a path name
343static const struct extension_case {
344 const wchar_t* path;
345 const wchar_t* extension;
346} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900347#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900348 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
349 {L"C:\\colon\\backslash\\filename.", L""},
350 {L"C:\\colon\\backslash\\filename", L""},
351 {L"C:\\colon\\backslash\\", L""},
352 {L"C:\\colon\\backslash.\\", L""},
353 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900354#elif defined(OS_POSIX)
355 {L"/foo/bar/filename.extension", L"extension"},
356 {L"/foo/bar/filename.", L""},
357 {L"/foo/bar/filename", L""},
358 {L"/foo/bar/", L""},
359 {L"/foo/bar./", L""},
360 {L"/foo/bar/filename.extension.extension2", L"extension2"},
361 {L".", L""},
362 {L"..", L""},
363 {L"./foo", L""},
364 {L"./foo.extension", L"extension"},
365 {L"/foo.extension1/bar.extension2", L"extension2"},
366#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900367};
368
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900369#if defined(OS_WIN)
370// This function has been deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900371TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900372 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900373 const extension_case& ext = extension_cases[i];
374 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
375 EXPECT_EQ(ext.extension, fext);
376 }
377}
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900378#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900379
380// Test finding the directory component of a path
381static const struct dir_case {
382 const wchar_t* full_path;
383 const wchar_t* directory;
384} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900385#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900386 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
387 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
388 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
389 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
390 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
391 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900392 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900393#elif defined(OS_POSIX)
394 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
395 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
396 {L"/foo/bar/", L"/foo/bar"},
397 {L"/foo/bar//", L"/foo/bar"},
398 {L"/foo/bar", L"/foo"},
399 {L"/foo/bar./", L"/foo/bar."},
400 {L"/", L"/"},
401 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900402 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900403#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900404};
405
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900406#if defined(OS_WIN)
407// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900408TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900409 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900410 const dir_case& dir = dir_cases[i];
411 const std::wstring parent =
412 file_util::GetDirectoryFromPath(dir.full_path);
413 EXPECT_EQ(dir.directory, parent);
414 }
415}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900416#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900417
glider@chromium.org5fd12332010-06-10 22:05:26 +0900418// Flaky, http://crbug.com/46246
glider@chromium.orge1879a22010-06-10 21:40:52 +0900419TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commit3f4a7322008-07-27 06:49:38 +0900420 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900421 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900422 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
423
424 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900425#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900426 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900427#elif defined(OS_POSIX)
428 // We need to wait at least one second here because the precision of
429 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900430 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900431#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900432
433 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900434 base::Time now(base::Time::NowFromSystemTime());
435 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900436
437 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900438 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900439 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
440
441 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900442 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900443
444 // Delete new file, we should see no files after cutoff now
445 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900446 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900447}
448
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900449TEST_F(FileUtilTest, FileAndDirectorySize) {
450 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
451 // should return 53 bytes.
452 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
453 CreateTextFile(file_01, L"12345678901234567890");
454 int64 size_f1 = 0;
455 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
456 EXPECT_EQ(20ll, size_f1);
457
458 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
459 file_util::CreateDirectory(subdir_path);
460
461 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
462 CreateTextFile(file_02, L"123456789012345678901234567890");
463 int64 size_f2 = 0;
464 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
465 EXPECT_EQ(30ll, size_f2);
466
467 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
468 file_util::CreateDirectory(subsubdir_path);
469
470 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
471 CreateTextFile(file_03, L"123");
472
473 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
474 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
rvargas@google.comaa24e112010-06-12 07:53:43 +0900475
476 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("The file*"));
477 EXPECT_EQ(size_f1, computed_size);
478
479 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("bla*"));
480 EXPECT_EQ(0, computed_size);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900481}
482
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900483TEST_F(FileUtilTest, NormalizeFilePathBasic) {
484 // Create a directory under the test dir. Because we create it,
485 // we know it is not a link.
486 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
487 FilePath dir_path = test_dir_.Append(FPL("dir"));
488 FilePath file_b_path = dir_path.Append(FPL("file_b"));
489 file_util::CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900490
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900491 FilePath normalized_file_a_path, normalized_file_b_path;
492 ASSERT_FALSE(file_util::PathExists(file_a_path));
493 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
494 &normalized_file_a_path))
495 << "NormalizeFilePath() should fail on nonexistant paths.";
496
497 CreateTextFile(file_a_path, bogus_content);
498 ASSERT_TRUE(file_util::PathExists(file_a_path));
499 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
500 &normalized_file_a_path));
501
502 CreateTextFile(file_b_path, bogus_content);
503 ASSERT_TRUE(file_util::PathExists(file_b_path));
504 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
505 &normalized_file_b_path));
506
507 // Beacuse this test created |dir_path|, we know it is not a link
508 // or junction. So, the real path of the directory holding file a
509 // must be the parent of the path holding file b.
510 ASSERT_TRUE(normalized_file_a_path.DirName()
511 .IsParent(normalized_file_b_path.DirName()));
512}
513
514#if defined(OS_WIN)
515
516TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
517 // Build the following directory structure:
518 //
519 // test_dir_
520 // |-> base_a
521 // | |-> sub_a
522 // | |-> file.txt
523 // | |-> long_name___... (Very long name.)
524 // | |-> sub_long
525 // | |-> deep.txt
526 // |-> base_b
527 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
528 // |-> to_base_b (reparse point to test_dir_\base_b)
529 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
530
531 FilePath base_a = test_dir_.Append(FPL("base_a"));
532 ASSERT_TRUE(file_util::CreateDirectory(base_a));
533
534 FilePath sub_a = base_a.Append(FPL("sub_a"));
535 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
536
537 FilePath file_txt = sub_a.Append(FPL("file.txt"));
538 CreateTextFile(file_txt, bogus_content);
539
540 // Want a directory whose name is long enough to make the path to the file
541 // inside just under MAX_PATH chars. This will be used to test that when
542 // a junction expands to a path over MAX_PATH chars in length,
543 // NormalizeFilePath() fails without crashing.
544 FilePath sub_long_rel(FPL("sub_long"));
545 FilePath deep_txt(FPL("deep.txt"));
546
547 int target_length = MAX_PATH;
548 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
549 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900550 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900551 // the resulting path is still long enough to hit the failing case in
552 // NormalizePath().
553 const int kCreateDirLimit = 4;
554 target_length -= kCreateDirLimit;
555 FilePath::StringType long_name_str = FPL("long_name_");
556 long_name_str.resize(target_length, '_');
557
558 FilePath long_name = sub_a.Append(FilePath(long_name_str));
559 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
560 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
561
562 FilePath sub_long = deep_file.DirName();
563 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
564 CreateTextFile(deep_file, bogus_content);
565
566 FilePath base_b = test_dir_.Append(FPL("base_b"));
567 ASSERT_TRUE(file_util::CreateDirectory(base_b));
568
569 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
570 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
571 ScopedHandle reparse_to_sub_a(
572 ::CreateFile(to_sub_a.value().c_str(),
573 FILE_ALL_ACCESS,
574 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
575 NULL,
576 OPEN_EXISTING,
577 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
578 NULL));
579 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_a.Get());
580 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
581
582 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
583 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
584 ScopedHandle reparse_to_base_b(
585 ::CreateFile(to_base_b.value().c_str(),
586 FILE_ALL_ACCESS,
587 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
588 NULL,
589 OPEN_EXISTING,
590 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
591 NULL));
592 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_base_b.Get());
593 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
594
595 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
596 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
597 ScopedHandle reparse_to_sub_long(
598 ::CreateFile(to_sub_long.value().c_str(),
599 FILE_ALL_ACCESS,
600 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
601 NULL,
602 OPEN_EXISTING,
603 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
604 NULL));
605 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_long.Get());
606 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
607
608 // Normalize a junction free path: base_a\sub_a\file.txt .
609 FilePath normalized_path;
610 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
611 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
612
613 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
614 // the junction to_sub_a.
615 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
616 &normalized_path));
617 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
618
619 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
620 // normalized to exclude junctions to_base_b and to_sub_a .
621 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
622 .Append(FPL("to_base_b"))
623 .Append(FPL("to_sub_a"))
624 .Append(FPL("file.txt")),
625 &normalized_path));
626 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
627
628 // A long enough path will cause NormalizeFilePath() to fail. Make a long
629 // path using to_base_b many times, and check that paths long enough to fail
630 // do not cause a crash.
631 FilePath long_path = base_b;
632 const int kLengthLimit = MAX_PATH + 200;
633 while (long_path.value().length() <= kLengthLimit) {
634 long_path = long_path.Append(FPL("to_base_b"));
635 }
636 long_path = long_path.Append(FPL("to_sub_a"))
637 .Append(FPL("file.txt"));
638
639 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
640
641 // Normalizing the junction to deep.txt should fail, because the expanded
642 // path to deep.txt is longer than MAX_PATH.
643 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
644 &normalized_path));
645
646 // Delete the reparse points, and see that NormalizeFilePath() fails
647 // to traverse them.
648 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
649 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
650 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
651
652 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
653 &normalized_path));
654}
655
656#endif // defined(OS_WIN)
657
658// The following test of NormalizeFilePath() require that we create a symlink.
659// This can not be done on windows before vista. On vista, creating a symlink
660// requires privilege "SeCreateSymbolicLinkPrivilege".
661// TODO(skerner): Investigate the possibility of giving base_unittests the
662// privileges required to create a symlink.
663#if defined(OS_POSIX)
664
665bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
666 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
667}
668
669TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
670 FilePath normalized_path;
skerner@chromium.org559baa92010-05-13 00:13:57 +0900671
672 // Link one file to another.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900673 FilePath link_from = test_dir_.Append(FPL("from_file"));
674 FilePath link_to = test_dir_.Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900675 CreateTextFile(link_to, bogus_content);
676
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900677 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900678 << "Failed to create file symlink.";
679
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900680 // Check that NormalizeFilePath sees the link.
681 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900682 ASSERT_TRUE(link_to != link_from);
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900683 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
684 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900685
686 // Link to a directory.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900687 link_from = test_dir_.Append(FPL("from_dir"));
688 link_to = test_dir_.Append(FPL("to_dir"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900689 file_util::CreateDirectory(link_to);
690
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900691 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900692 << "Failed to create directory symlink.";
693
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900694 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
695 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900696
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900697 // Test that a loop in the links causes NormalizeFilePath() to return false.
698 link_from = test_dir_.Append(FPL("link_a"));
699 link_to = test_dir_.Append(FPL("link_b"));
700 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900701 << "Failed to create loop symlink a.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900702 ASSERT_TRUE(MakeSymlink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900703 << "Failed to create loop symlink b.";
704
705 // Infinite loop!
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900706 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900707}
708#endif // defined(OS_POSIX)
709
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900710TEST_F(FileUtilTest, DeleteNonExistent) {
711 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
712 ASSERT_FALSE(file_util::PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900713
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900714 EXPECT_TRUE(file_util::Delete(non_existent, false));
715 ASSERT_FALSE(file_util::PathExists(non_existent));
716 EXPECT_TRUE(file_util::Delete(non_existent, true));
717 ASSERT_FALSE(file_util::PathExists(non_existent));
718}
719
720TEST_F(FileUtilTest, DeleteFile) {
721 // Create a file
722 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
723 CreateTextFile(file_name, bogus_content);
initial.commit3f4a7322008-07-27 06:49:38 +0900724 ASSERT_TRUE(file_util::PathExists(file_name));
725
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900726 // Make sure it's deleted
727 EXPECT_TRUE(file_util::Delete(file_name, false));
728 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900729
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900730 // Test recursive case, create a new file
731 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
732 CreateTextFile(file_name, bogus_content);
733 ASSERT_TRUE(file_util::PathExists(file_name));
734
735 // Make sure it's deleted
736 EXPECT_TRUE(file_util::Delete(file_name, true));
737 EXPECT_FALSE(file_util::PathExists(file_name));
738}
739
740#if defined(OS_WIN)
741// Tests that the Delete function works for wild cards, especially
742// with the recursion flag. Also coincidentally tests PathExists.
743// TODO(erikkay): see if anyone's actually using this feature of the API
744TEST_F(FileUtilTest, DeleteWildCard) {
745 // Create a file and a directory
746 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
747 CreateTextFile(file_name, bogus_content);
748 ASSERT_TRUE(file_util::PathExists(file_name));
749
750 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
751 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900752 ASSERT_TRUE(file_util::PathExists(subdir_path));
753
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900754 // Create the wildcard path
evanm@google.com874d1672008-10-31 08:54:04 +0900755 FilePath directory_contents = test_dir_;
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900756 directory_contents = directory_contents.Append(FPL("*"));
757
initial.commit3f4a7322008-07-27 06:49:38 +0900758 // Delete non-recursively and check that only the file is deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900759 EXPECT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900760 EXPECT_FALSE(file_util::PathExists(file_name));
761 EXPECT_TRUE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900762
zork@chromium.org61be4f42010-05-07 09:05:36 +0900763 // Delete recursively and make sure all contents are deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900764 EXPECT_TRUE(file_util::Delete(directory_contents, true));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900765 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900766 EXPECT_FALSE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900767}
768
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900769// TODO(erikkay): see if anyone's actually using this feature of the API
770TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
771 // Create a file and a directory
772 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
773 file_util::CreateDirectory(subdir_path);
774 ASSERT_TRUE(file_util::PathExists(subdir_path));
775
776 // Create the wildcard path
777 FilePath directory_contents = subdir_path;
778 directory_contents = directory_contents.Append(FPL("*"));
779
780 // Delete non-recursively and check nothing got deleted
781 EXPECT_TRUE(file_util::Delete(directory_contents, false));
782 EXPECT_TRUE(file_util::PathExists(subdir_path));
783
784 // Delete recursively and check nothing got deleted
785 EXPECT_TRUE(file_util::Delete(directory_contents, true));
786 EXPECT_TRUE(file_util::PathExists(subdir_path));
787}
788#endif
789
790// Tests non-recursive Delete() for a directory.
791TEST_F(FileUtilTest, DeleteDirNonRecursive) {
792 // Create a subdirectory and put a file and two directories inside.
793 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
794 file_util::CreateDirectory(test_subdir);
795 ASSERT_TRUE(file_util::PathExists(test_subdir));
796
797 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
798 CreateTextFile(file_name, bogus_content);
799 ASSERT_TRUE(file_util::PathExists(file_name));
800
801 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
802 file_util::CreateDirectory(subdir_path1);
803 ASSERT_TRUE(file_util::PathExists(subdir_path1));
804
805 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
806 file_util::CreateDirectory(subdir_path2);
807 ASSERT_TRUE(file_util::PathExists(subdir_path2));
808
809 // Delete non-recursively and check that the empty dir got deleted
810 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
811 EXPECT_FALSE(file_util::PathExists(subdir_path2));
812
813 // Delete non-recursively and check that nothing got deleted
814 EXPECT_FALSE(file_util::Delete(test_subdir, false));
815 EXPECT_TRUE(file_util::PathExists(test_subdir));
816 EXPECT_TRUE(file_util::PathExists(file_name));
817 EXPECT_TRUE(file_util::PathExists(subdir_path1));
818}
819
820// Tests recursive Delete() for a directory.
821TEST_F(FileUtilTest, DeleteDirRecursive) {
822 // Create a subdirectory and put a file and two directories inside.
823 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
824 file_util::CreateDirectory(test_subdir);
825 ASSERT_TRUE(file_util::PathExists(test_subdir));
826
827 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
828 CreateTextFile(file_name, bogus_content);
829 ASSERT_TRUE(file_util::PathExists(file_name));
830
831 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
832 file_util::CreateDirectory(subdir_path1);
833 ASSERT_TRUE(file_util::PathExists(subdir_path1));
834
835 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
836 file_util::CreateDirectory(subdir_path2);
837 ASSERT_TRUE(file_util::PathExists(subdir_path2));
838
839 // Delete recursively and check that the empty dir got deleted
840 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
841 EXPECT_FALSE(file_util::PathExists(subdir_path2));
842
843 // Delete recursively and check that everything got deleted
844 EXPECT_TRUE(file_util::Delete(test_subdir, true));
845 EXPECT_FALSE(file_util::PathExists(file_name));
846 EXPECT_FALSE(file_util::PathExists(subdir_path1));
847 EXPECT_FALSE(file_util::PathExists(test_subdir));
848}
849
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900850TEST_F(FileUtilTest, MoveFileNew) {
851 // Create a file
852 FilePath file_name_from =
853 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
854 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
855 ASSERT_TRUE(file_util::PathExists(file_name_from));
856
857 // The destination
858 FilePath file_name_to =
859 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
860 ASSERT_FALSE(file_util::PathExists(file_name_to));
861
862 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
863
864 // Check everything has been moved.
865 EXPECT_FALSE(file_util::PathExists(file_name_from));
866 EXPECT_TRUE(file_util::PathExists(file_name_to));
867}
868
869TEST_F(FileUtilTest, MoveFileExists) {
870 // Create a file
871 FilePath file_name_from =
872 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
873 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
874 ASSERT_TRUE(file_util::PathExists(file_name_from));
875
876 // The destination name
877 FilePath file_name_to =
878 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
879 CreateTextFile(file_name_to, L"Old file content");
880 ASSERT_TRUE(file_util::PathExists(file_name_to));
881
882 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
883
884 // Check everything has been moved.
885 EXPECT_FALSE(file_util::PathExists(file_name_from));
886 EXPECT_TRUE(file_util::PathExists(file_name_to));
887 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
888}
889
890TEST_F(FileUtilTest, MoveFileDirExists) {
891 // Create a file
892 FilePath file_name_from =
893 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
894 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
895 ASSERT_TRUE(file_util::PathExists(file_name_from));
896
897 // The destination directory
898 FilePath dir_name_to =
899 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
900 file_util::CreateDirectory(dir_name_to);
901 ASSERT_TRUE(file_util::PathExists(dir_name_to));
902
903 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
904}
905
906
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900907TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900908 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900909 FilePath dir_name_from =
910 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
911 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900912 ASSERT_TRUE(file_util::PathExists(dir_name_from));
913
914 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900915 FilePath file_name_from =
916 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900917 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
918 ASSERT_TRUE(file_util::PathExists(file_name_from));
919
920 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900921 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
922 FilePath file_name_to =
923 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900924
925 ASSERT_FALSE(file_util::PathExists(dir_name_to));
926
927 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
928
929 // Check everything has been moved.
930 EXPECT_FALSE(file_util::PathExists(dir_name_from));
931 EXPECT_FALSE(file_util::PathExists(file_name_from));
932 EXPECT_TRUE(file_util::PathExists(dir_name_to));
933 EXPECT_TRUE(file_util::PathExists(file_name_to));
934}
935
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900936TEST_F(FileUtilTest, MoveExist) {
937 // Create a directory
938 FilePath dir_name_from =
939 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
940 file_util::CreateDirectory(dir_name_from);
941 ASSERT_TRUE(file_util::PathExists(dir_name_from));
942
943 // Create a file under the directory
944 FilePath file_name_from =
945 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
946 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
947 ASSERT_TRUE(file_util::PathExists(file_name_from));
948
949 // Move the directory
950 FilePath dir_name_exists =
951 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
952
953 FilePath dir_name_to =
954 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
955 FilePath file_name_to =
956 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
957
958 // Create the destination directory.
959 file_util::CreateDirectory(dir_name_exists);
960 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
961
962 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
963
964 // Check everything has been moved.
965 EXPECT_FALSE(file_util::PathExists(dir_name_from));
966 EXPECT_FALSE(file_util::PathExists(file_name_from));
967 EXPECT_TRUE(file_util::PathExists(dir_name_to));
968 EXPECT_TRUE(file_util::PathExists(file_name_to));
969}
970
971TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900972 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900973 FilePath dir_name_from =
974 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
975 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900976 ASSERT_TRUE(file_util::PathExists(dir_name_from));
977
978 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900979 FilePath file_name_from =
980 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900981 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
982 ASSERT_TRUE(file_util::PathExists(file_name_from));
983
984 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900985 FilePath subdir_name_from =
986 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
987 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900988 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
989
990 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900991 FilePath file_name2_from =
992 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900993 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
994 ASSERT_TRUE(file_util::PathExists(file_name2_from));
995
996 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900997 FilePath dir_name_to =
998 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
999 FilePath file_name_to =
1000 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1001 FilePath subdir_name_to =
1002 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1003 FilePath file_name2_to =
1004 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001005
1006 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1007
1008 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
1009
1010 // Check everything has been copied.
1011 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1012 EXPECT_TRUE(file_util::PathExists(file_name_from));
1013 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1014 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1015 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1016 EXPECT_TRUE(file_util::PathExists(file_name_to));
1017 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1018 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1019}
1020
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001021TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1022 // Create a directory.
1023 FilePath dir_name_from =
1024 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1025 file_util::CreateDirectory(dir_name_from);
1026 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1027
1028 // Create a file under the directory.
1029 FilePath file_name_from =
1030 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1031 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1032 ASSERT_TRUE(file_util::PathExists(file_name_from));
1033
1034 // Create a subdirectory.
1035 FilePath subdir_name_from =
1036 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1037 file_util::CreateDirectory(subdir_name_from);
1038 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1039
1040 // Create a file under the subdirectory.
1041 FilePath file_name2_from =
1042 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1043 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1044 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1045
1046 // Copy the directory recursively.
1047 FilePath dir_name_exists =
1048 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1049
1050 FilePath dir_name_to =
1051 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1052 FilePath file_name_to =
1053 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1054 FilePath subdir_name_to =
1055 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1056 FilePath file_name2_to =
1057 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1058
1059 // Create the destination directory.
1060 file_util::CreateDirectory(dir_name_exists);
1061 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1062
1063 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1064
1065 // Check everything has been copied.
1066 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1067 EXPECT_TRUE(file_util::PathExists(file_name_from));
1068 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1069 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1070 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1071 EXPECT_TRUE(file_util::PathExists(file_name_to));
1072 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1073 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1074}
1075
1076TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001077 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001078 FilePath dir_name_from =
1079 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1080 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001081 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1082
1083 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001084 FilePath file_name_from =
1085 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001086 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1087 ASSERT_TRUE(file_util::PathExists(file_name_from));
1088
1089 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001090 FilePath subdir_name_from =
1091 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1092 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001093 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1094
1095 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001096 FilePath file_name2_from =
1097 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001098 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1099 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1100
1101 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001102 FilePath dir_name_to =
1103 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1104 FilePath file_name_to =
1105 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1106 FilePath subdir_name_to =
1107 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001108
1109 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1110
1111 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1112
1113 // Check everything has been copied.
1114 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1115 EXPECT_TRUE(file_util::PathExists(file_name_from));
1116 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1117 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1118 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1119 EXPECT_TRUE(file_util::PathExists(file_name_to));
1120 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1121}
1122
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001123TEST_F(FileUtilTest, CopyDirectoryExists) {
1124 // Create a directory.
1125 FilePath dir_name_from =
1126 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1127 file_util::CreateDirectory(dir_name_from);
1128 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1129
1130 // Create a file under the directory.
1131 FilePath file_name_from =
1132 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1133 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1134 ASSERT_TRUE(file_util::PathExists(file_name_from));
1135
1136 // Create a subdirectory.
1137 FilePath subdir_name_from =
1138 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1139 file_util::CreateDirectory(subdir_name_from);
1140 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1141
1142 // Create a file under the subdirectory.
1143 FilePath file_name2_from =
1144 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1145 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1146 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1147
1148 // Copy the directory not recursively.
1149 FilePath dir_name_to =
1150 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1151 FilePath file_name_to =
1152 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1153 FilePath subdir_name_to =
1154 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1155
1156 // Create the destination directory.
1157 file_util::CreateDirectory(dir_name_to);
1158 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1159
1160 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1161
1162 // Check everything has been copied.
1163 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1164 EXPECT_TRUE(file_util::PathExists(file_name_from));
1165 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1166 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1167 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1168 EXPECT_TRUE(file_util::PathExists(file_name_to));
1169 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1170}
1171
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001172TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1173 // Create a file
1174 FilePath file_name_from =
1175 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1176 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1177 ASSERT_TRUE(file_util::PathExists(file_name_from));
1178
1179 // The destination name
1180 FilePath file_name_to =
1181 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1182 ASSERT_FALSE(file_util::PathExists(file_name_to));
1183
1184 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1185
1186 // Check the has been copied
1187 EXPECT_TRUE(file_util::PathExists(file_name_to));
1188}
1189
1190TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1191 // Create a file
1192 FilePath file_name_from =
1193 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1194 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1195 ASSERT_TRUE(file_util::PathExists(file_name_from));
1196
1197 // The destination name
1198 FilePath file_name_to =
1199 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1200 CreateTextFile(file_name_to, L"Old file content");
1201 ASSERT_TRUE(file_util::PathExists(file_name_to));
1202
1203 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1204
1205 // Check the has been copied
1206 EXPECT_TRUE(file_util::PathExists(file_name_to));
1207 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1208}
1209
1210TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1211 // Create a file
1212 FilePath file_name_from =
1213 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1214 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1215 ASSERT_TRUE(file_util::PathExists(file_name_from));
1216
1217 // The destination
1218 FilePath dir_name_to =
1219 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1220 file_util::CreateDirectory(dir_name_to);
1221 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1222 FilePath file_name_to =
1223 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1224
1225 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1226
1227 // Check the has been copied
1228 EXPECT_TRUE(file_util::PathExists(file_name_to));
1229}
1230
initial.commit3f4a7322008-07-27 06:49:38 +09001231TEST_F(FileUtilTest, CopyFile) {
1232 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001233 FilePath dir_name_from =
1234 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1235 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001236 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1237
1238 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001239 FilePath file_name_from =
1240 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001241 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1242 CreateTextFile(file_name_from, file_contents);
1243 ASSERT_TRUE(file_util::PathExists(file_name_from));
1244
1245 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001246 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001247 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001248
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001249 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001250 FilePath dest_file2(dir_name_from);
1251 dest_file2 = dest_file2.AppendASCII("..");
1252 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1253 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1254
1255 FilePath dest_file2_test(dir_name_from);
1256 dest_file2_test = dest_file2_test.DirName();
1257 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001258
1259 // Check everything has been copied.
1260 EXPECT_TRUE(file_util::PathExists(file_name_from));
1261 EXPECT_TRUE(file_util::PathExists(dest_file));
1262 const std::wstring read_contents = ReadTextFile(dest_file);
1263 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +09001264 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1265 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001266}
1267
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001268// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +09001269#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001270TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +09001271 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001272
1273 SYSTEMTIME start_time;
1274 GetLocalTime(&start_time);
1275 Sleep(100);
1276 CreateTextFile(file_name, L"New file!");
1277 Sleep(100);
1278 SYSTEMTIME end_time;
1279 GetLocalTime(&end_time);
1280
1281 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +09001282 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +09001283
1284 FILETIME start_filetime;
1285 SystemTimeToFileTime(&start_time, &start_filetime);
1286 FILETIME end_filetime;
1287 SystemTimeToFileTime(&end_time, &end_filetime);
1288 FILETIME file_creation_filetime;
1289 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1290
1291 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1292 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1293 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1294
1295 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1296 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1297 "end time: " << FileTimeAsUint64(end_filetime);
1298
evanm@google.com874d1672008-10-31 08:54:04 +09001299 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +09001300}
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001301#endif
initial.commit3f4a7322008-07-27 06:49:38 +09001302
erikkay@google.comf2406842008-08-21 00:59:49 +09001303// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001304// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001305typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001306
erikkay@google.comf2406842008-08-21 00:59:49 +09001307TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001308 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +09001309 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +09001310 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1311 .Append(FILE_PATH_LITERAL("data"))
1312 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +09001313 ASSERT_TRUE(file_util::PathExists(data_dir));
1314
evanm@google.com874d1672008-10-31 08:54:04 +09001315 FilePath original_file =
1316 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1317 FilePath same_file =
1318 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1319 FilePath same_length_file =
1320 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1321 FilePath different_file =
1322 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1323 FilePath different_first_file =
1324 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1325 FilePath different_last_file =
1326 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1327 FilePath empty1_file =
1328 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1329 FilePath empty2_file =
1330 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1331 FilePath shortened_file =
1332 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1333 FilePath binary_file =
1334 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1335 FilePath binary_file_same =
1336 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1337 FilePath binary_file_diff =
1338 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001339
1340 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1341 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1342 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1343 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +09001344 EXPECT_FALSE(file_util::ContentsEqual(
1345 FilePath(FILE_PATH_LITERAL("bogusname")),
1346 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +09001347 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1348 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1349 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1350 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1351 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1352 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1353 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1354}
1355
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001356TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1357 FilePath data_dir;
1358 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1359 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1360 .Append(FILE_PATH_LITERAL("data"))
1361 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1362 ASSERT_TRUE(file_util::PathExists(data_dir));
1363
1364 FilePath original_file =
1365 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1366 FilePath same_file =
1367 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1368 FilePath crlf_file =
1369 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1370 FilePath shortened_file =
1371 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1372 FilePath different_file =
1373 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1374 FilePath different_first_file =
1375 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1376 FilePath different_last_file =
1377 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1378 FilePath first1_file =
1379 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1380 FilePath first2_file =
1381 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1382 FilePath empty1_file =
1383 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1384 FilePath empty2_file =
1385 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1386 FilePath blank_line_file =
1387 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1388 FilePath blank_line_crlf_file =
1389 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1390
1391 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1392 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1393 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1394 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1395 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1396 different_first_file));
1397 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1398 different_last_file));
1399 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1400 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1401 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1402 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1403 blank_line_crlf_file));
1404}
1405
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001406// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001407#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001408TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001409 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001410 CreateTextFile(target_file, L"This is the target.");
1411
evanm@google.com874d1672008-10-31 08:54:04 +09001412 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001413
1414 HRESULT result;
1415 IShellLink *shell = NULL;
1416 IPersistFile *persist = NULL;
1417
1418 CoInitialize(NULL);
1419 // Temporarily create a shortcut for test
1420 result = CoCreateInstance(CLSID_ShellLink, NULL,
1421 CLSCTX_INPROC_SERVER, IID_IShellLink,
1422 reinterpret_cast<LPVOID*>(&shell));
1423 EXPECT_TRUE(SUCCEEDED(result));
1424 result = shell->QueryInterface(IID_IPersistFile,
1425 reinterpret_cast<LPVOID*>(&persist));
1426 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001427 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001428 EXPECT_TRUE(SUCCEEDED(result));
1429 result = shell->SetDescription(L"ResolveShortcutTest");
1430 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001431 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001432 EXPECT_TRUE(SUCCEEDED(result));
1433 if (persist)
1434 persist->Release();
1435 if (shell)
1436 shell->Release();
1437
1438 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001439 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001440 EXPECT_TRUE(is_solved);
1441 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001442 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001443 EXPECT_EQ(L"This is the target.", contents);
1444
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001445 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001446 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001447 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001448 CoUninitialize();
1449}
1450
1451TEST_F(FileUtilTest, CreateShortcutTest) {
1452 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001453 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001454 CreateTextFile(target_file, file_contents);
1455
evanm@google.com874d1672008-10-31 08:54:04 +09001456 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001457
1458 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001459 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1460 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001461 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001462 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001463 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001464 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001465 EXPECT_EQ(file_contents, read_contents);
1466
evanm@google.com874d1672008-10-31 08:54:04 +09001467 DeleteFile(target_file.value().c_str());
1468 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001469 CoUninitialize();
1470}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001471
1472TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1473 // Create a directory
1474 FilePath dir_name_from =
1475 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1476 file_util::CreateDirectory(dir_name_from);
1477 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1478
1479 // Create a file under the directory
1480 FilePath file_name_from =
1481 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1482 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1483 ASSERT_TRUE(file_util::PathExists(file_name_from));
1484
1485 // Move the directory by using CopyAndDeleteDirectory
1486 FilePath dir_name_to = test_dir_.Append(
1487 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1488 FilePath file_name_to =
1489 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1490
1491 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1492
1493 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1494
1495 // Check everything has been moved.
1496 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1497 EXPECT_FALSE(file_util::PathExists(file_name_from));
1498 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1499 EXPECT_TRUE(file_util::PathExists(file_name_to));
1500}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001501
1502TEST_F(FileUtilTest, GetTempDirTest) {
1503 static const TCHAR* kTmpKey = _T("TMP");
1504 static const TCHAR* kTmpValues[] = {
1505 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1506 };
1507 // Save the original $TMP.
1508 size_t original_tmp_size;
1509 TCHAR* original_tmp;
1510 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1511 // original_tmp may be NULL.
1512
1513 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1514 FilePath path;
1515 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1516 file_util::GetTempDir(&path);
1517 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1518 " result=" << path.value();
1519 }
1520
1521 // Restore the original $TMP.
1522 if (original_tmp) {
1523 ::_tputenv_s(kTmpKey, original_tmp);
1524 free(original_tmp);
1525 } else {
1526 ::_tputenv_s(kTmpKey, _T(""));
1527 }
1528}
1529#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001530
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001531TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1532 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001533 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001534 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001535 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1536 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1537 }
1538 for (int i = 0; i < 3; i++)
1539 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1540 for (int i = 0; i < 3; i++)
1541 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1542}
1543
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001544TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001545 FilePath names[3];
1546 FILE *fps[3];
1547 int i;
1548
1549 // Create; make sure they are open and exist.
1550 for (i = 0; i < 3; ++i) {
1551 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1552 ASSERT_TRUE(fps[i]);
1553 EXPECT_TRUE(file_util::PathExists(names[i]));
1554 }
1555
1556 // Make sure all names are unique.
1557 for (i = 0; i < 3; ++i) {
1558 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1559 }
1560
1561 // Close and delete.
1562 for (i = 0; i < 3; ++i) {
1563 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1564 EXPECT_TRUE(file_util::Delete(names[i], false));
1565 }
initial.commit3f4a7322008-07-27 06:49:38 +09001566}
1567
1568TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001569 FilePath temp_dir;
1570 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1571 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001572 EXPECT_TRUE(file_util::PathExists(temp_dir));
1573 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001574}
1575
skerner@chromium.orge4432392010-05-01 02:00:09 +09001576TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1577 FilePath new_dir;
1578 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1579 test_dir_,
1580 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.org2c12d672010-07-02 05:24:09 +09001581 false,
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001582 &new_dir));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001583 EXPECT_TRUE(file_util::PathExists(new_dir));
1584 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1585 EXPECT_TRUE(file_util::Delete(new_dir, false));
1586}
1587
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001588TEST_F(FileUtilTest, GetShmemTempDirTest) {
1589 FilePath dir;
1590 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1591 EXPECT_TRUE(file_util::DirectoryExists(dir));
1592}
1593
initial.commit3f4a7322008-07-27 06:49:38 +09001594TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001595 FilePath test_root =
1596 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001597#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001598 FilePath test_path =
1599 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001600#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001601 FilePath test_path =
1602 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001603#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001604
1605 EXPECT_FALSE(file_util::PathExists(test_path));
1606 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1607 EXPECT_TRUE(file_util::PathExists(test_path));
1608 // CreateDirectory returns true if the DirectoryExists returns true.
1609 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1610
1611 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001612 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001613 EXPECT_FALSE(file_util::PathExists(test_path));
1614 CreateTextFile(test_path, L"test file");
1615 EXPECT_TRUE(file_util::PathExists(test_path));
1616 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1617
1618 EXPECT_TRUE(file_util::Delete(test_root, true));
1619 EXPECT_FALSE(file_util::PathExists(test_root));
1620 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001621
1622 // Verify assumptions made by the Windows implementation:
1623 // 1. The current directory always exists.
1624 // 2. The root directory always exists.
1625 ASSERT_TRUE(file_util::DirectoryExists(
1626 FilePath(FilePath::kCurrentDirectory)));
1627 FilePath top_level = test_root;
1628 while (top_level != top_level.DirName()) {
1629 top_level = top_level.DirName();
1630 }
1631 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1632
1633 // Given these assumptions hold, it should be safe to
1634 // test that "creating" these directories succeeds.
1635 EXPECT_TRUE(file_util::CreateDirectory(
1636 FilePath(FilePath::kCurrentDirectory)));
1637 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001638
1639#if defined(OS_WIN)
1640 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1641 FilePath invalid_path =
1642 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1643 if (!file_util::PathExists(invalid_drive)) {
1644 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1645 }
1646#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001647}
1648
1649TEST_F(FileUtilTest, DetectDirectoryTest) {
1650 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001651 FilePath test_root =
1652 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001653 EXPECT_FALSE(file_util::PathExists(test_root));
1654 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1655 EXPECT_TRUE(file_util::PathExists(test_root));
1656 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1657
1658 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001659 FilePath test_path =
1660 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001661 EXPECT_FALSE(file_util::PathExists(test_path));
1662 CreateTextFile(test_path, L"test file");
1663 EXPECT_TRUE(file_util::PathExists(test_path));
1664 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1665 EXPECT_TRUE(file_util::Delete(test_path, false));
1666
1667 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001668}
1669
initial.commit3f4a7322008-07-27 06:49:38 +09001670TEST_F(FileUtilTest, FileEnumeratorTest) {
1671 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001672 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001673 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1674 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001675
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001676 // Test an empty directory, non-recursively, including "..".
1677 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1678 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1679 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1680 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1681 f0_dotdot.Next().value());
1682 EXPECT_EQ(FILE_PATH_LITERAL(""),
1683 f0_dotdot.Next().value());
1684
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001685 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001686 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001687 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001688 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001689 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001690 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001691 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001692
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001693 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001694 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001695 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001696 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001697 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001698 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001699 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001700 FilePath file2_rel =
1701 dir2.Append(FilePath::kParentDirectory)
1702 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001703 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001704 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001705
1706 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001707 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001708 file_util::FileEnumerator::FILES);
1709 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001710 EXPECT_TRUE(c1.HasFile(file1));
1711 EXPECT_TRUE(c1.HasFile(file2_abs));
1712 EXPECT_TRUE(c1.HasFile(dir2file));
1713 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1714 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001715
1716 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001717 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001718 file_util::FileEnumerator::DIRECTORIES);
1719 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001720 EXPECT_TRUE(c2.HasFile(dir1));
1721 EXPECT_TRUE(c2.HasFile(dir2));
1722 EXPECT_TRUE(c2.HasFile(dir2inner));
1723 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001724
tim@chromium.org989d0972008-10-16 11:42:45 +09001725 // Only enumerate directories non-recursively.
1726 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001727 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001728 FindResultCollector c2_non_recursive(f2_non_recursive);
1729 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1730 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1731 EXPECT_EQ(c2_non_recursive.size(), 2);
1732
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001733 // Only enumerate directories, non-recursively, including "..".
1734 file_util::FileEnumerator f2_dotdot(
1735 test_dir_, false,
1736 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1737 file_util::FileEnumerator::DIRECTORIES |
1738 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1739 FindResultCollector c2_dotdot(f2_dotdot);
1740 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1741 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1742 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1743 EXPECT_EQ(c2_dotdot.size(), 3);
1744
initial.commit3f4a7322008-07-27 06:49:38 +09001745 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001746 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001747 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001748 EXPECT_TRUE(c3.HasFile(dir1));
1749 EXPECT_TRUE(c3.HasFile(dir2));
1750 EXPECT_TRUE(c3.HasFile(file1));
1751 EXPECT_TRUE(c3.HasFile(file2_abs));
1752 EXPECT_TRUE(c3.HasFile(dir2file));
1753 EXPECT_TRUE(c3.HasFile(dir2inner));
1754 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1755 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001756
1757 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001758 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001759 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001760 EXPECT_TRUE(c4.HasFile(dir2));
1761 EXPECT_TRUE(c4.HasFile(dir2));
1762 EXPECT_TRUE(c4.HasFile(file1));
1763 EXPECT_TRUE(c4.HasFile(file2_abs));
1764 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001765
1766 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001767 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001768 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001769 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001770 EXPECT_TRUE(c5.HasFile(dir1));
1771 EXPECT_TRUE(c5.HasFile(dir2));
1772 EXPECT_TRUE(c5.HasFile(dir2file));
1773 EXPECT_TRUE(c5.HasFile(dir2inner));
1774 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1775 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001776
1777 // Make sure the destructor closes the find handle while in the middle of a
1778 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001779 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001780 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1781 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001782}
license.botf003cfe2008-08-24 09:55:55 +09001783
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001784TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001785 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001786
1787 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001788 if (file_util::PathExists(data_dir)) {
1789 ASSERT_TRUE(file_util::Delete(data_dir, true));
1790 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001791 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1792
1793 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1794 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1795 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1796 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1797
1798 // Annoyingly, the directories must actually exist in order for realpath(),
1799 // which Contains() relies on in posix, to work.
1800 ASSERT_TRUE(file_util::CreateDirectory(foo));
1801 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001802 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1803 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1804 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001805
1806 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1807 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1808 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1809 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1810
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001811 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001812 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1813#if defined(OS_WIN)
1814 EXPECT_TRUE(file_util::ContainsPath(foo,
1815 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001816 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001817 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001818#elif defined(OS_MACOSX)
1819 // We can't really do this test on OS X since the case-sensitivity of the
1820 // filesystem is configurable.
1821#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001822 EXPECT_FALSE(file_util::ContainsPath(foo,
1823 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001824#endif
1825}
1826
jochen@chromium.orga6879772010-02-18 19:02:26 +09001827TEST_F(FileUtilTest, LastModified) {
1828 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1829
1830 // Create a fresh, empty copy of this directory.
1831 if (file_util::PathExists(data_dir)) {
1832 ASSERT_TRUE(file_util::Delete(data_dir, true));
1833 }
1834 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1835
1836 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1837 std::string data("hello");
1838 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1839
1840 base::Time modification_time;
1841 // Note that this timestamp is divisible by two (seconds) - FAT stores
1842 // modification times with 2s resolution.
1843 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1844 &modification_time));
1845 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1846 file_util::FileInfo file_info;
1847 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1848 ASSERT_TRUE(file_info.last_modified == modification_time);
1849}
1850
tfarina@chromium.org34828222010-05-26 10:40:12 +09001851TEST_F(FileUtilTest, IsDirectoryEmpty) {
1852 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1853
1854 ASSERT_FALSE(file_util::PathExists(empty_dir));
1855
1856 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1857
1858 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1859
1860 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1861 std::string bar("baz");
1862 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1863
1864 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1865}
1866
mark@chromium.org17684802008-09-10 09:16:28 +09001867} // namespace