blob: 26eb42d7a66b450f34a7c99a92f3dbfd3f2afa8a [file] [log] [blame]
jochen@chromium.orga6879772010-02-18 19:02:26 +09001// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
erikkay@google.comc8ec9e92008-08-16 02:50:10 +09005#include "build/build_config.h"
6
erikkay@google.com014161d2008-08-16 02:45:13 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +09009#include <winioctl.h>
initial.commit3f4a7322008-07-27 06:49:38 +090010#include <shellapi.h>
11#include <shlobj.h>
tkent@chromium.org8da14162009-10-09 16:33:39 +090012#include <tchar.h>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090013#endif
initial.commit3f4a7322008-07-27 06:49:38 +090014
15#include <fstream>
erikkay@google.comdfb51b22008-08-16 02:32:10 +090016#include <set>
initial.commit3f4a7322008-07-27 06:49:38 +090017
18#include "base/base_paths.h"
evanm@google.com874d1672008-10-31 08:54:04 +090019#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090020#include "base/file_util.h"
initial.commit3f4a7322008-07-27 06:49:38 +090021#include "base/path_service.h"
erikkay@google.com8d133f62009-04-24 00:05:19 +090022#include "base/platform_thread.h"
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090023#include "base/scoped_handle.h"
erikkay@google.com9ac26762009-04-18 09:42:48 +090024#include "base/time.h"
brettw@chromium.org50c94652009-10-07 11:10:20 +090025#include "base/utf_string_conversions.h"
initial.commit3f4a7322008-07-27 06:49:38 +090026#include "testing/gtest/include/gtest/gtest.h"
jeremy@chromium.org0d8eba72008-12-03 04:20:15 +090027#include "testing/platform_test.h"
initial.commit3f4a7322008-07-27 06:49:38 +090028
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +090029// This macro helps avoid wrapped lines in the test structs.
30#define FPL(x) FILE_PATH_LITERAL(x)
31
initial.commit3f4a7322008-07-27 06:49:38 +090032namespace {
33
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +090034// To test that file_util::Normalize FilePath() deals with NTFS reparse points
35// correctly, we need functions to create and delete reparse points.
36#if defined(OS_WIN)
37typedef struct _REPARSE_DATA_BUFFER {
38 ULONG ReparseTag;
39 USHORT ReparseDataLength;
40 USHORT Reserved;
41 union {
42 struct {
43 USHORT SubstituteNameOffset;
44 USHORT SubstituteNameLength;
45 USHORT PrintNameOffset;
46 USHORT PrintNameLength;
47 ULONG Flags;
48 WCHAR PathBuffer[1];
49 } SymbolicLinkReparseBuffer;
50 struct {
51 USHORT SubstituteNameOffset;
52 USHORT SubstituteNameLength;
53 USHORT PrintNameOffset;
54 USHORT PrintNameLength;
55 WCHAR PathBuffer[1];
56 } MountPointReparseBuffer;
57 struct {
58 UCHAR DataBuffer[1];
59 } GenericReparseBuffer;
60 };
61} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
62
63// Sets a reparse point. |source| will now point to |target|. Returns true if
64// the call succeeds, false otherwise.
65bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
66 std::wstring kPathPrefix = L"\\??\\";
67 std::wstring target_str;
68 // The juction will not work if the target path does not start with \??\ .
69 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
70 target_str += kPathPrefix;
71 target_str += target_path.value();
72 const wchar_t* target = target_str.c_str();
73 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
74 char buffer[2000] = {0};
75 DWORD returned;
76
77 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
78
79 data->ReparseTag = 0xa0000003;
80 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
81
82 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
83 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
84 data->ReparseDataLength = size_target + 4 + 8;
85
86 int data_size = data->ReparseDataLength + 8;
87
88 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
89 NULL, 0, &returned, NULL)) {
90 return false;
91 }
92 return true;
93}
94
95// Delete the reparse point referenced by |source|. Returns true if the call
96// succeeds, false otherwise.
97bool DeleteReparsePoint(HANDLE source) {
98 DWORD returned;
99 REPARSE_DATA_BUFFER data = {0};
100 data.ReparseTag = 0xa0000003;
101 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
102 &returned, NULL)) {
103 return false;
104 }
105 return true;
106}
107#endif
108
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900109const wchar_t bogus_content[] = L"I'm cannon fodder.";
110
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900111const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
112 static_cast<file_util::FileEnumerator::FILE_TYPE>(
113 file_util::FileEnumerator::FILES |
114 file_util::FileEnumerator::DIRECTORIES);
115
erikkay@google.comf2406842008-08-21 00:59:49 +0900116// file_util winds up using autoreleased objects on the Mac, so this needs
117// to be a PlatformTest
118class FileUtilTest : public PlatformTest {
initial.commit3f4a7322008-07-27 06:49:38 +0900119 protected:
120 virtual void SetUp() {
erikkay@google.comf2406842008-08-21 00:59:49 +0900121 PlatformTest::SetUp();
initial.commit3f4a7322008-07-27 06:49:38 +0900122 // Name a subdirectory of the temp directory.
123 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
evanm@google.com874d1672008-10-31 08:54:04 +0900124 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commit3f4a7322008-07-27 06:49:38 +0900125
126 // Create a fresh, empty copy of this directory.
127 file_util::Delete(test_dir_, true);
evanm@google.com874d1672008-10-31 08:54:04 +0900128 file_util::CreateDirectory(test_dir_);
initial.commit3f4a7322008-07-27 06:49:38 +0900129 }
130 virtual void TearDown() {
erikkay@google.comf2406842008-08-21 00:59:49 +0900131 PlatformTest::TearDown();
initial.commit3f4a7322008-07-27 06:49:38 +0900132 // Clean up test directory
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900133 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commit3f4a7322008-07-27 06:49:38 +0900134 ASSERT_FALSE(file_util::PathExists(test_dir_));
135 }
136
137 // the path to temporary directory used to contain the test operations
evanm@google.com874d1672008-10-31 08:54:04 +0900138 FilePath test_dir_;
initial.commit3f4a7322008-07-27 06:49:38 +0900139};
140
141// Collects all the results from the given file enumerator, and provides an
142// interface to query whether a given file is present.
143class FindResultCollector {
144 public:
evan@chromium.org1543ad32009-08-27 05:00:14 +0900145 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
avi@google.com5cb79352008-12-11 23:55:12 +0900146 FilePath cur_file;
147 while (!(cur_file = enumerator.Next()).value().empty()) {
148 FilePath::StringType path = cur_file.value();
initial.commit3f4a7322008-07-27 06:49:38 +0900149 // The file should not be returned twice.
evanm@google.com874d1672008-10-31 08:54:04 +0900150 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commit3f4a7322008-07-27 06:49:38 +0900151 << "Same file returned twice";
152
153 // Save for later.
evanm@google.com874d1672008-10-31 08:54:04 +0900154 files_.insert(path);
initial.commit3f4a7322008-07-27 06:49:38 +0900155 }
156 }
157
158 // Returns true if the enumerator found the file.
evanm@google.com874d1672008-10-31 08:54:04 +0900159 bool HasFile(const FilePath& file) const {
160 return files_.find(file.value()) != files_.end();
initial.commit3f4a7322008-07-27 06:49:38 +0900161 }
evanm@google.com874d1672008-10-31 08:54:04 +0900162
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900163 int size() {
erikkay@google.comc8ec9e92008-08-16 02:50:10 +0900164 return static_cast<int>(files_.size());
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900165 }
initial.commit3f4a7322008-07-27 06:49:38 +0900166
167 private:
evanm@google.com874d1672008-10-31 08:54:04 +0900168 std::set<FilePath::StringType> files_;
initial.commit3f4a7322008-07-27 06:49:38 +0900169};
170
171// Simple function to dump some text into a new file.
evanm@google.com874d1672008-10-31 08:54:04 +0900172void CreateTextFile(const FilePath& filename,
initial.commit3f4a7322008-07-27 06:49:38 +0900173 const std::wstring& contents) {
174 std::ofstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900175 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900176 ASSERT_TRUE(file.is_open());
177 file << contents;
178 file.close();
179}
180
181// Simple function to take out some text from a file.
evanm@google.com874d1672008-10-31 08:54:04 +0900182std::wstring ReadTextFile(const FilePath& filename) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900183 wchar_t contents[64];
initial.commit3f4a7322008-07-27 06:49:38 +0900184 std::wifstream file;
evanm@google.com874d1672008-10-31 08:54:04 +0900185 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commit3f4a7322008-07-27 06:49:38 +0900186 EXPECT_TRUE(file.is_open());
187 file.getline(contents, 64);
188 file.close();
189 return std::wstring(contents);
190}
191
erikkay@google.com014161d2008-08-16 02:45:13 +0900192#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900193uint64 FileTimeAsUint64(const FILETIME& ft) {
194 ULARGE_INTEGER u;
195 u.LowPart = ft.dwLowDateTime;
196 u.HighPart = ft.dwHighDateTime;
197 return u.QuadPart;
198}
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900199#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900200
201const struct append_case {
202 const wchar_t* path;
203 const wchar_t* ending;
204 const wchar_t* result;
205} append_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900206#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900207 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
208 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
209 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
210 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
211 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
212 {L"", L"path", L"\\path"},
213 {L"", L"", L"\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900214#elif defined(OS_POSIX)
215 {L"/foo/bar", L"path", L"/foo/bar/path"},
216 {L"/foo/bar/", L"path", L"/foo/bar/path"},
217 {L"/foo/bar//", L"path", L"/foo/bar//path"},
218 {L"/foo/bar/", L"", L"/foo/bar/"},
219 {L"/foo/bar", L"", L"/foo/bar/"},
220 {L"", L"path", L"/path"},
221 {L"", L"", L"/"},
222#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900223};
224
evan@chromium.org1db7f942010-02-27 00:11:55 +0900225#if defined(OS_WIN)
226// This function is deprecated, but still used on Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900227TEST_F(FileUtilTest, AppendToPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900228 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900229 const append_case& value = append_cases[i];
230 std::wstring result = value.path;
231 file_util::AppendToPath(&result, value.ending);
232 EXPECT_EQ(value.result, result);
233 }
234
235#ifdef NDEBUG
236 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
237#endif
238}
evan@chromium.org1db7f942010-02-27 00:11:55 +0900239#endif // defined(OS_WIN)
240
initial.commit3f4a7322008-07-27 06:49:38 +0900241static const struct filename_case {
242 const wchar_t* path;
243 const wchar_t* filename;
244} filename_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900245#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900246 {L"c:\\colon\\backslash", L"backslash"},
247 {L"c:\\colon\\backslash\\", L""},
248 {L"\\\\filename.exe", L"filename.exe"},
249 {L"filename.exe", L"filename.exe"},
250 {L"", L""},
251 {L"\\\\\\", L""},
252 {L"c:/colon/backslash", L"backslash"},
253 {L"c:/colon/backslash/", L""},
254 {L"//////", L""},
255 {L"///filename.exe", L"filename.exe"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900256#elif defined(OS_POSIX)
257 {L"/foo/bar", L"bar"},
258 {L"/foo/bar/", L""},
259 {L"/filename.exe", L"filename.exe"},
260 {L"filename.exe", L"filename.exe"},
261 {L"", L""},
262 {L"/", L""},
263#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900264};
265
evan@chromium.org5a196702010-07-02 08:19:02 +0900266#if defined(OS_WIN)
267// This function is deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900268TEST_F(FileUtilTest, GetFilenameFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900269 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900270 const filename_case& value = filename_cases[i];
271 std::wstring result = file_util::GetFilenameFromPath(value.path);
272 EXPECT_EQ(value.filename, result);
273 }
274}
evan@chromium.org5a196702010-07-02 08:19:02 +0900275#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900276
277// Test finding the file type from a path name
278static const struct extension_case {
279 const wchar_t* path;
280 const wchar_t* extension;
281} extension_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900282#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900283 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
284 {L"C:\\colon\\backslash\\filename.", L""},
285 {L"C:\\colon\\backslash\\filename", L""},
286 {L"C:\\colon\\backslash\\", L""},
287 {L"C:\\colon\\backslash.\\", L""},
288 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900289#elif defined(OS_POSIX)
290 {L"/foo/bar/filename.extension", L"extension"},
291 {L"/foo/bar/filename.", L""},
292 {L"/foo/bar/filename", L""},
293 {L"/foo/bar/", L""},
294 {L"/foo/bar./", L""},
295 {L"/foo/bar/filename.extension.extension2", L"extension2"},
296 {L".", L""},
297 {L"..", L""},
298 {L"./foo", L""},
299 {L"./foo.extension", L"extension"},
300 {L"/foo.extension1/bar.extension2", L"extension2"},
301#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900302};
303
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900304#if defined(OS_WIN)
305// This function has been deprecated on non-Windows.
initial.commit3f4a7322008-07-27 06:49:38 +0900306TEST_F(FileUtilTest, GetFileExtensionFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900307 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900308 const extension_case& ext = extension_cases[i];
309 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
310 EXPECT_EQ(ext.extension, fext);
311 }
312}
evan@chromium.org3b660ad2010-07-09 02:49:05 +0900313#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900314
315// Test finding the directory component of a path
316static const struct dir_case {
317 const wchar_t* full_path;
318 const wchar_t* directory;
319} dir_cases[] = {
erikkay@google.com014161d2008-08-16 02:45:13 +0900320#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900321 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
322 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
323 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
324 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
325 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
326 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
tkent@chromium.orgfce07c72009-10-15 14:00:25 +0900327 {L"C:\\", L"C:\\"},
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900328#elif defined(OS_POSIX)
329 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
330 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
331 {L"/foo/bar/", L"/foo/bar"},
332 {L"/foo/bar//", L"/foo/bar"},
333 {L"/foo/bar", L"/foo"},
334 {L"/foo/bar./", L"/foo/bar."},
335 {L"/", L"/"},
336 {L".", L"."},
evan@chromium.org1543ad32009-08-27 05:00:14 +0900337 {L"..", L"."}, // yes, ".." technically lives in "."
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900338#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900339};
340
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900341#if defined(OS_WIN)
342// This function is deprecated, and only exists on Windows anymore.
initial.commit3f4a7322008-07-27 06:49:38 +0900343TEST_F(FileUtilTest, GetDirectoryFromPath) {
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900344 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commit3f4a7322008-07-27 06:49:38 +0900345 const dir_case& dir = dir_cases[i];
346 const std::wstring parent =
347 file_util::GetDirectoryFromPath(dir.full_path);
348 EXPECT_EQ(dir.directory, parent);
349 }
350}
evan@chromium.org9c7cbbe2010-02-23 21:52:11 +0900351#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900352
glider@chromium.org5fd12332010-06-10 22:05:26 +0900353// Flaky, http://crbug.com/46246
glider@chromium.orge1879a22010-06-10 21:40:52 +0900354TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commit3f4a7322008-07-27 06:49:38 +0900355 // Create old file (that we don't want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900356 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900357 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
358
359 // Age to perfection
evan@chromium.org37301322009-04-21 10:50:39 +0900360#if defined(OS_WIN)
erikkay@google.com8d133f62009-04-24 00:05:19 +0900361 PlatformThread::Sleep(100);
evan@chromium.org37301322009-04-21 10:50:39 +0900362#elif defined(OS_POSIX)
363 // We need to wait at least one second here because the precision of
364 // file creation time is one second.
erikkay@google.com8d133f62009-04-24 00:05:19 +0900365 PlatformThread::Sleep(1500);
evan@chromium.org37301322009-04-21 10:50:39 +0900366#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900367
368 // Establish our cutoff time
erikkay@google.com9ac26762009-04-18 09:42:48 +0900369 base::Time now(base::Time::NowFromSystemTime());
370 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900371
372 // Create a new file (that we do want to count)
erikkay@google.com9ac26762009-04-18 09:42:48 +0900373 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900374 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
375
376 // We should see only the new file.
erikkay@google.com9ac26762009-04-18 09:42:48 +0900377 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900378
379 // Delete new file, we should see no files after cutoff now
380 EXPECT_TRUE(file_util::Delete(new_file_name, false));
erikkay@google.com9ac26762009-04-18 09:42:48 +0900381 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commit3f4a7322008-07-27 06:49:38 +0900382}
383
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900384TEST_F(FileUtilTest, FileAndDirectorySize) {
385 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
386 // should return 53 bytes.
387 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
388 CreateTextFile(file_01, L"12345678901234567890");
389 int64 size_f1 = 0;
390 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
391 EXPECT_EQ(20ll, size_f1);
392
393 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
394 file_util::CreateDirectory(subdir_path);
395
396 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
397 CreateTextFile(file_02, L"123456789012345678901234567890");
398 int64 size_f2 = 0;
399 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
400 EXPECT_EQ(30ll, size_f2);
401
402 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
403 file_util::CreateDirectory(subsubdir_path);
404
405 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
406 CreateTextFile(file_03, L"123");
407
408 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
409 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
rvargas@google.comaa24e112010-06-12 07:53:43 +0900410
411 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("The file*"));
412 EXPECT_EQ(size_f1, computed_size);
413
414 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("bla*"));
415 EXPECT_EQ(0, computed_size);
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900416}
417
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900418TEST_F(FileUtilTest, NormalizeFilePathBasic) {
419 // Create a directory under the test dir. Because we create it,
420 // we know it is not a link.
421 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
422 FilePath dir_path = test_dir_.Append(FPL("dir"));
423 FilePath file_b_path = dir_path.Append(FPL("file_b"));
424 file_util::CreateDirectory(dir_path);
skerner@chromium.org559baa92010-05-13 00:13:57 +0900425
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900426 FilePath normalized_file_a_path, normalized_file_b_path;
427 ASSERT_FALSE(file_util::PathExists(file_a_path));
428 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
429 &normalized_file_a_path))
viettrungluu@chromium.orgea703f12010-08-23 01:19:13 +0900430 << "NormalizeFilePath() should fail on nonexistent paths.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900431
432 CreateTextFile(file_a_path, bogus_content);
433 ASSERT_TRUE(file_util::PathExists(file_a_path));
434 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
435 &normalized_file_a_path));
436
437 CreateTextFile(file_b_path, bogus_content);
438 ASSERT_TRUE(file_util::PathExists(file_b_path));
439 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
440 &normalized_file_b_path));
441
442 // Beacuse this test created |dir_path|, we know it is not a link
443 // or junction. So, the real path of the directory holding file a
444 // must be the parent of the path holding file b.
445 ASSERT_TRUE(normalized_file_a_path.DirName()
446 .IsParent(normalized_file_b_path.DirName()));
447}
448
449#if defined(OS_WIN)
450
451TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
452 // Build the following directory structure:
453 //
454 // test_dir_
455 // |-> base_a
456 // | |-> sub_a
457 // | |-> file.txt
458 // | |-> long_name___... (Very long name.)
459 // | |-> sub_long
460 // | |-> deep.txt
461 // |-> base_b
462 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
463 // |-> to_base_b (reparse point to test_dir_\base_b)
464 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
465
466 FilePath base_a = test_dir_.Append(FPL("base_a"));
467 ASSERT_TRUE(file_util::CreateDirectory(base_a));
468
469 FilePath sub_a = base_a.Append(FPL("sub_a"));
470 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
471
472 FilePath file_txt = sub_a.Append(FPL("file.txt"));
473 CreateTextFile(file_txt, bogus_content);
474
475 // Want a directory whose name is long enough to make the path to the file
476 // inside just under MAX_PATH chars. This will be used to test that when
477 // a junction expands to a path over MAX_PATH chars in length,
478 // NormalizeFilePath() fails without crashing.
479 FilePath sub_long_rel(FPL("sub_long"));
480 FilePath deep_txt(FPL("deep.txt"));
481
482 int target_length = MAX_PATH;
483 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
484 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
glider@chromium.orge1879a22010-06-10 21:40:52 +0900485 // Without making the path a bit shorter, CreateDirectory() fails.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900486 // the resulting path is still long enough to hit the failing case in
487 // NormalizePath().
488 const int kCreateDirLimit = 4;
489 target_length -= kCreateDirLimit;
490 FilePath::StringType long_name_str = FPL("long_name_");
491 long_name_str.resize(target_length, '_');
492
493 FilePath long_name = sub_a.Append(FilePath(long_name_str));
494 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
495 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
496
497 FilePath sub_long = deep_file.DirName();
498 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
499 CreateTextFile(deep_file, bogus_content);
500
501 FilePath base_b = test_dir_.Append(FPL("base_b"));
502 ASSERT_TRUE(file_util::CreateDirectory(base_b));
503
504 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
505 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
506 ScopedHandle reparse_to_sub_a(
507 ::CreateFile(to_sub_a.value().c_str(),
508 FILE_ALL_ACCESS,
509 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
510 NULL,
511 OPEN_EXISTING,
512 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
513 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900514 ASSERT_TRUE(reparse_to_sub_a.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900515 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
516
517 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
518 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
519 ScopedHandle reparse_to_base_b(
520 ::CreateFile(to_base_b.value().c_str(),
521 FILE_ALL_ACCESS,
522 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
523 NULL,
524 OPEN_EXISTING,
525 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
526 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900527 ASSERT_TRUE(reparse_to_base_b.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900528 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
529
530 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
531 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
532 ScopedHandle reparse_to_sub_long(
533 ::CreateFile(to_sub_long.value().c_str(),
534 FILE_ALL_ACCESS,
535 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
536 NULL,
537 OPEN_EXISTING,
538 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
539 NULL));
skerner@chromium.orgf1a62a32010-08-03 12:51:56 +0900540 ASSERT_TRUE(reparse_to_sub_long.IsValid());
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900541 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
542
543 // Normalize a junction free path: base_a\sub_a\file.txt .
544 FilePath normalized_path;
545 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
546 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
547
548 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
549 // the junction to_sub_a.
550 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
551 &normalized_path));
552 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
553
554 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
555 // normalized to exclude junctions to_base_b and to_sub_a .
556 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
557 .Append(FPL("to_base_b"))
558 .Append(FPL("to_sub_a"))
559 .Append(FPL("file.txt")),
560 &normalized_path));
561 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
562
563 // A long enough path will cause NormalizeFilePath() to fail. Make a long
564 // path using to_base_b many times, and check that paths long enough to fail
565 // do not cause a crash.
566 FilePath long_path = base_b;
567 const int kLengthLimit = MAX_PATH + 200;
568 while (long_path.value().length() <= kLengthLimit) {
569 long_path = long_path.Append(FPL("to_base_b"));
570 }
571 long_path = long_path.Append(FPL("to_sub_a"))
572 .Append(FPL("file.txt"));
573
574 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
575
576 // Normalizing the junction to deep.txt should fail, because the expanded
577 // path to deep.txt is longer than MAX_PATH.
578 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
579 &normalized_path));
580
581 // Delete the reparse points, and see that NormalizeFilePath() fails
582 // to traverse them.
583 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
584 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
585 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
586
587 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
588 &normalized_path));
589}
590
591#endif // defined(OS_WIN)
592
593// The following test of NormalizeFilePath() require that we create a symlink.
594// This can not be done on windows before vista. On vista, creating a symlink
595// requires privilege "SeCreateSymbolicLinkPrivilege".
596// TODO(skerner): Investigate the possibility of giving base_unittests the
597// privileges required to create a symlink.
598#if defined(OS_POSIX)
599
600bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
601 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
602}
603
604TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
605 FilePath normalized_path;
skerner@chromium.org559baa92010-05-13 00:13:57 +0900606
607 // Link one file to another.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900608 FilePath link_from = test_dir_.Append(FPL("from_file"));
609 FilePath link_to = test_dir_.Append(FPL("to_file"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900610 CreateTextFile(link_to, bogus_content);
611
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900612 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900613 << "Failed to create file symlink.";
614
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900615 // Check that NormalizeFilePath sees the link.
616 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900617 ASSERT_TRUE(link_to != link_from);
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900618 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
619 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
skerner@chromium.org559baa92010-05-13 00:13:57 +0900620
621 // Link to a directory.
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900622 link_from = test_dir_.Append(FPL("from_dir"));
623 link_to = test_dir_.Append(FPL("to_dir"));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900624 file_util::CreateDirectory(link_to);
625
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900626 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900627 << "Failed to create directory symlink.";
628
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900629 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
630 << "Links to directories should return false.";
skerner@chromium.org559baa92010-05-13 00:13:57 +0900631
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900632 // Test that a loop in the links causes NormalizeFilePath() to return false.
633 link_from = test_dir_.Append(FPL("link_a"));
634 link_to = test_dir_.Append(FPL("link_b"));
635 ASSERT_TRUE(MakeSymlink(link_to, link_from))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900636 << "Failed to create loop symlink a.";
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900637 ASSERT_TRUE(MakeSymlink(link_from, link_to))
skerner@chromium.org559baa92010-05-13 00:13:57 +0900638 << "Failed to create loop symlink b.";
639
640 // Infinite loop!
skerner@chromium.org8bbe5be2010-06-10 07:56:48 +0900641 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
skerner@chromium.org559baa92010-05-13 00:13:57 +0900642}
643#endif // defined(OS_POSIX)
644
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900645TEST_F(FileUtilTest, DeleteNonExistent) {
646 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
647 ASSERT_FALSE(file_util::PathExists(non_existent));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900648
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900649 EXPECT_TRUE(file_util::Delete(non_existent, false));
650 ASSERT_FALSE(file_util::PathExists(non_existent));
651 EXPECT_TRUE(file_util::Delete(non_existent, true));
652 ASSERT_FALSE(file_util::PathExists(non_existent));
653}
654
655TEST_F(FileUtilTest, DeleteFile) {
656 // Create a file
657 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
658 CreateTextFile(file_name, bogus_content);
initial.commit3f4a7322008-07-27 06:49:38 +0900659 ASSERT_TRUE(file_util::PathExists(file_name));
660
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900661 // Make sure it's deleted
662 EXPECT_TRUE(file_util::Delete(file_name, false));
663 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900664
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900665 // Test recursive case, create a new file
666 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
667 CreateTextFile(file_name, bogus_content);
668 ASSERT_TRUE(file_util::PathExists(file_name));
669
670 // Make sure it's deleted
671 EXPECT_TRUE(file_util::Delete(file_name, true));
672 EXPECT_FALSE(file_util::PathExists(file_name));
673}
674
675#if defined(OS_WIN)
676// Tests that the Delete function works for wild cards, especially
677// with the recursion flag. Also coincidentally tests PathExists.
678// TODO(erikkay): see if anyone's actually using this feature of the API
679TEST_F(FileUtilTest, DeleteWildCard) {
680 // Create a file and a directory
681 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
682 CreateTextFile(file_name, bogus_content);
683 ASSERT_TRUE(file_util::PathExists(file_name));
684
685 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
686 file_util::CreateDirectory(subdir_path);
initial.commit3f4a7322008-07-27 06:49:38 +0900687 ASSERT_TRUE(file_util::PathExists(subdir_path));
688
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900689 // Create the wildcard path
evanm@google.com874d1672008-10-31 08:54:04 +0900690 FilePath directory_contents = test_dir_;
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900691 directory_contents = directory_contents.Append(FPL("*"));
692
initial.commit3f4a7322008-07-27 06:49:38 +0900693 // Delete non-recursively and check that only the file is deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900694 EXPECT_TRUE(file_util::Delete(directory_contents, false));
erikkay@google.comdfb51b22008-08-16 02:32:10 +0900695 EXPECT_FALSE(file_util::PathExists(file_name));
696 EXPECT_TRUE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900697
zork@chromium.org61be4f42010-05-07 09:05:36 +0900698 // Delete recursively and make sure all contents are deleted
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900699 EXPECT_TRUE(file_util::Delete(directory_contents, true));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900700 EXPECT_FALSE(file_util::PathExists(file_name));
zork@chromium.org61be4f42010-05-07 09:05:36 +0900701 EXPECT_FALSE(file_util::PathExists(subdir_path));
thestig@chromium.orgafd8dd42010-05-07 06:56:40 +0900702}
703
thestig@chromium.org1dad8c62010-05-08 03:58:45 +0900704// TODO(erikkay): see if anyone's actually using this feature of the API
705TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
706 // Create a file and a directory
707 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
708 file_util::CreateDirectory(subdir_path);
709 ASSERT_TRUE(file_util::PathExists(subdir_path));
710
711 // Create the wildcard path
712 FilePath directory_contents = subdir_path;
713 directory_contents = directory_contents.Append(FPL("*"));
714
715 // Delete non-recursively and check nothing got deleted
716 EXPECT_TRUE(file_util::Delete(directory_contents, false));
717 EXPECT_TRUE(file_util::PathExists(subdir_path));
718
719 // Delete recursively and check nothing got deleted
720 EXPECT_TRUE(file_util::Delete(directory_contents, true));
721 EXPECT_TRUE(file_util::PathExists(subdir_path));
722}
723#endif
724
725// Tests non-recursive Delete() for a directory.
726TEST_F(FileUtilTest, DeleteDirNonRecursive) {
727 // Create a subdirectory and put a file and two directories inside.
728 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
729 file_util::CreateDirectory(test_subdir);
730 ASSERT_TRUE(file_util::PathExists(test_subdir));
731
732 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
733 CreateTextFile(file_name, bogus_content);
734 ASSERT_TRUE(file_util::PathExists(file_name));
735
736 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
737 file_util::CreateDirectory(subdir_path1);
738 ASSERT_TRUE(file_util::PathExists(subdir_path1));
739
740 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
741 file_util::CreateDirectory(subdir_path2);
742 ASSERT_TRUE(file_util::PathExists(subdir_path2));
743
744 // Delete non-recursively and check that the empty dir got deleted
745 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
746 EXPECT_FALSE(file_util::PathExists(subdir_path2));
747
748 // Delete non-recursively and check that nothing got deleted
749 EXPECT_FALSE(file_util::Delete(test_subdir, false));
750 EXPECT_TRUE(file_util::PathExists(test_subdir));
751 EXPECT_TRUE(file_util::PathExists(file_name));
752 EXPECT_TRUE(file_util::PathExists(subdir_path1));
753}
754
755// Tests recursive Delete() for a directory.
756TEST_F(FileUtilTest, DeleteDirRecursive) {
757 // Create a subdirectory and put a file and two directories inside.
758 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
759 file_util::CreateDirectory(test_subdir);
760 ASSERT_TRUE(file_util::PathExists(test_subdir));
761
762 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
763 CreateTextFile(file_name, bogus_content);
764 ASSERT_TRUE(file_util::PathExists(file_name));
765
766 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
767 file_util::CreateDirectory(subdir_path1);
768 ASSERT_TRUE(file_util::PathExists(subdir_path1));
769
770 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
771 file_util::CreateDirectory(subdir_path2);
772 ASSERT_TRUE(file_util::PathExists(subdir_path2));
773
774 // Delete recursively and check that the empty dir got deleted
775 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
776 EXPECT_FALSE(file_util::PathExists(subdir_path2));
777
778 // Delete recursively and check that everything got deleted
779 EXPECT_TRUE(file_util::Delete(test_subdir, true));
780 EXPECT_FALSE(file_util::PathExists(file_name));
781 EXPECT_FALSE(file_util::PathExists(subdir_path1));
782 EXPECT_FALSE(file_util::PathExists(test_subdir));
783}
784
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +0900785TEST_F(FileUtilTest, MoveFileNew) {
786 // Create a file
787 FilePath file_name_from =
788 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
789 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
790 ASSERT_TRUE(file_util::PathExists(file_name_from));
791
792 // The destination
793 FilePath file_name_to =
794 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
795 ASSERT_FALSE(file_util::PathExists(file_name_to));
796
797 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
798
799 // Check everything has been moved.
800 EXPECT_FALSE(file_util::PathExists(file_name_from));
801 EXPECT_TRUE(file_util::PathExists(file_name_to));
802}
803
804TEST_F(FileUtilTest, MoveFileExists) {
805 // Create a file
806 FilePath file_name_from =
807 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
808 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
809 ASSERT_TRUE(file_util::PathExists(file_name_from));
810
811 // The destination name
812 FilePath file_name_to =
813 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
814 CreateTextFile(file_name_to, L"Old file content");
815 ASSERT_TRUE(file_util::PathExists(file_name_to));
816
817 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
818
819 // Check everything has been moved.
820 EXPECT_FALSE(file_util::PathExists(file_name_from));
821 EXPECT_TRUE(file_util::PathExists(file_name_to));
822 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
823}
824
825TEST_F(FileUtilTest, MoveFileDirExists) {
826 // Create a file
827 FilePath file_name_from =
828 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
829 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
830 ASSERT_TRUE(file_util::PathExists(file_name_from));
831
832 // The destination directory
833 FilePath dir_name_to =
834 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
835 file_util::CreateDirectory(dir_name_to);
836 ASSERT_TRUE(file_util::PathExists(dir_name_to));
837
838 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
839}
840
841
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900842TEST_F(FileUtilTest, MoveNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900843 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +0900844 FilePath dir_name_from =
845 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
846 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900847 ASSERT_TRUE(file_util::PathExists(dir_name_from));
848
849 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900850 FilePath file_name_from =
851 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900852 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
853 ASSERT_TRUE(file_util::PathExists(file_name_from));
854
855 // Move the directory
evanm@google.com874d1672008-10-31 08:54:04 +0900856 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
857 FilePath file_name_to =
858 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900859
860 ASSERT_FALSE(file_util::PathExists(dir_name_to));
861
862 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
863
864 // Check everything has been moved.
865 EXPECT_FALSE(file_util::PathExists(dir_name_from));
866 EXPECT_FALSE(file_util::PathExists(file_name_from));
867 EXPECT_TRUE(file_util::PathExists(dir_name_to));
868 EXPECT_TRUE(file_util::PathExists(file_name_to));
869}
870
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900871TEST_F(FileUtilTest, MoveExist) {
872 // Create a directory
873 FilePath dir_name_from =
874 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
875 file_util::CreateDirectory(dir_name_from);
876 ASSERT_TRUE(file_util::PathExists(dir_name_from));
877
878 // Create a file under the directory
879 FilePath file_name_from =
880 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
881 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
882 ASSERT_TRUE(file_util::PathExists(file_name_from));
883
884 // Move the directory
885 FilePath dir_name_exists =
886 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
887
888 FilePath dir_name_to =
889 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
890 FilePath file_name_to =
891 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
892
893 // Create the destination directory.
894 file_util::CreateDirectory(dir_name_exists);
895 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
896
897 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
898
899 // Check everything has been moved.
900 EXPECT_FALSE(file_util::PathExists(dir_name_from));
901 EXPECT_FALSE(file_util::PathExists(file_name_from));
902 EXPECT_TRUE(file_util::PathExists(dir_name_to));
903 EXPECT_TRUE(file_util::PathExists(file_name_to));
904}
905
906TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commit3f4a7322008-07-27 06:49:38 +0900907 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900908 FilePath dir_name_from =
909 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
910 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900911 ASSERT_TRUE(file_util::PathExists(dir_name_from));
912
913 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +0900914 FilePath file_name_from =
915 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900916 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
917 ASSERT_TRUE(file_util::PathExists(file_name_from));
918
919 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900920 FilePath subdir_name_from =
921 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
922 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +0900923 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
924
925 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +0900926 FilePath file_name2_from =
927 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900928 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
929 ASSERT_TRUE(file_util::PathExists(file_name2_from));
930
931 // Copy the directory recursively.
evanm@google.com874d1672008-10-31 08:54:04 +0900932 FilePath dir_name_to =
933 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
934 FilePath file_name_to =
935 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
936 FilePath subdir_name_to =
937 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
938 FilePath file_name2_to =
939 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +0900940
941 ASSERT_FALSE(file_util::PathExists(dir_name_to));
942
943 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
944
945 // Check everything has been copied.
946 EXPECT_TRUE(file_util::PathExists(dir_name_from));
947 EXPECT_TRUE(file_util::PathExists(file_name_from));
948 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
949 EXPECT_TRUE(file_util::PathExists(file_name2_from));
950 EXPECT_TRUE(file_util::PathExists(dir_name_to));
951 EXPECT_TRUE(file_util::PathExists(file_name_to));
952 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
953 EXPECT_TRUE(file_util::PathExists(file_name2_to));
954}
955
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +0900956TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
957 // Create a directory.
958 FilePath dir_name_from =
959 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
960 file_util::CreateDirectory(dir_name_from);
961 ASSERT_TRUE(file_util::PathExists(dir_name_from));
962
963 // Create a file under the directory.
964 FilePath file_name_from =
965 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
966 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
967 ASSERT_TRUE(file_util::PathExists(file_name_from));
968
969 // Create a subdirectory.
970 FilePath subdir_name_from =
971 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
972 file_util::CreateDirectory(subdir_name_from);
973 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
974
975 // Create a file under the subdirectory.
976 FilePath file_name2_from =
977 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
978 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
979 ASSERT_TRUE(file_util::PathExists(file_name2_from));
980
981 // Copy the directory recursively.
982 FilePath dir_name_exists =
983 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
984
985 FilePath dir_name_to =
986 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
987 FilePath file_name_to =
988 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
989 FilePath subdir_name_to =
990 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
991 FilePath file_name2_to =
992 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
993
994 // Create the destination directory.
995 file_util::CreateDirectory(dir_name_exists);
996 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
997
998 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
999
1000 // Check everything has been copied.
1001 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1002 EXPECT_TRUE(file_util::PathExists(file_name_from));
1003 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1004 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1005 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1006 EXPECT_TRUE(file_util::PathExists(file_name_to));
1007 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1008 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1009}
1010
1011TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commit3f4a7322008-07-27 06:49:38 +09001012 // Create a directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001013 FilePath dir_name_from =
1014 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1015 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001016 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1017
1018 // Create a file under the directory.
evanm@google.com874d1672008-10-31 08:54:04 +09001019 FilePath file_name_from =
1020 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001021 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1022 ASSERT_TRUE(file_util::PathExists(file_name_from));
1023
1024 // Create a subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001025 FilePath subdir_name_from =
1026 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1027 file_util::CreateDirectory(subdir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001028 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1029
1030 // Create a file under the subdirectory.
evanm@google.com874d1672008-10-31 08:54:04 +09001031 FilePath file_name2_from =
1032 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001033 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1034 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1035
1036 // Copy the directory not recursively.
evanm@google.com874d1672008-10-31 08:54:04 +09001037 FilePath dir_name_to =
1038 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1039 FilePath file_name_to =
1040 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1041 FilePath subdir_name_to =
1042 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commit3f4a7322008-07-27 06:49:38 +09001043
1044 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1045
1046 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1047
1048 // Check everything has been copied.
1049 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1050 EXPECT_TRUE(file_util::PathExists(file_name_from));
1051 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1052 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1053 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1054 EXPECT_TRUE(file_util::PathExists(file_name_to));
1055 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1056}
1057
vandebo@chromium.org70cf3f12009-10-14 02:57:27 +09001058TEST_F(FileUtilTest, CopyDirectoryExists) {
1059 // Create a directory.
1060 FilePath dir_name_from =
1061 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1062 file_util::CreateDirectory(dir_name_from);
1063 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1064
1065 // Create a file under the directory.
1066 FilePath file_name_from =
1067 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1068 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1069 ASSERT_TRUE(file_util::PathExists(file_name_from));
1070
1071 // Create a subdirectory.
1072 FilePath subdir_name_from =
1073 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1074 file_util::CreateDirectory(subdir_name_from);
1075 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1076
1077 // Create a file under the subdirectory.
1078 FilePath file_name2_from =
1079 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1080 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1081 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1082
1083 // Copy the directory not recursively.
1084 FilePath dir_name_to =
1085 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1086 FilePath file_name_to =
1087 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1088 FilePath subdir_name_to =
1089 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1090
1091 // Create the destination directory.
1092 file_util::CreateDirectory(dir_name_to);
1093 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1094
1095 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1096
1097 // Check everything has been copied.
1098 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1099 EXPECT_TRUE(file_util::PathExists(file_name_from));
1100 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1101 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1102 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1103 EXPECT_TRUE(file_util::PathExists(file_name_to));
1104 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1105}
1106
vandebo@chromium.orgc0cf77e2009-10-15 10:11:44 +09001107TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1108 // Create a file
1109 FilePath file_name_from =
1110 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1111 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1112 ASSERT_TRUE(file_util::PathExists(file_name_from));
1113
1114 // The destination name
1115 FilePath file_name_to =
1116 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1117 ASSERT_FALSE(file_util::PathExists(file_name_to));
1118
1119 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1120
1121 // Check the has been copied
1122 EXPECT_TRUE(file_util::PathExists(file_name_to));
1123}
1124
1125TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1126 // Create a file
1127 FilePath file_name_from =
1128 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1129 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1130 ASSERT_TRUE(file_util::PathExists(file_name_from));
1131
1132 // The destination name
1133 FilePath file_name_to =
1134 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1135 CreateTextFile(file_name_to, L"Old file content");
1136 ASSERT_TRUE(file_util::PathExists(file_name_to));
1137
1138 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1139
1140 // Check the has been copied
1141 EXPECT_TRUE(file_util::PathExists(file_name_to));
1142 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1143}
1144
1145TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1146 // Create a file
1147 FilePath file_name_from =
1148 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1149 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1150 ASSERT_TRUE(file_util::PathExists(file_name_from));
1151
1152 // The destination
1153 FilePath dir_name_to =
1154 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1155 file_util::CreateDirectory(dir_name_to);
1156 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1157 FilePath file_name_to =
1158 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1159
1160 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1161
1162 // Check the has been copied
1163 EXPECT_TRUE(file_util::PathExists(file_name_to));
1164}
1165
initial.commit3f4a7322008-07-27 06:49:38 +09001166TEST_F(FileUtilTest, CopyFile) {
1167 // Create a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001168 FilePath dir_name_from =
1169 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1170 file_util::CreateDirectory(dir_name_from);
initial.commit3f4a7322008-07-27 06:49:38 +09001171 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1172
1173 // Create a file under the directory
evanm@google.com874d1672008-10-31 08:54:04 +09001174 FilePath file_name_from =
1175 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001176 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1177 CreateTextFile(file_name_from, file_contents);
1178 ASSERT_TRUE(file_util::PathExists(file_name_from));
1179
1180 // Copy the file.
evanm@google.com874d1672008-10-31 08:54:04 +09001181 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001182 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001183
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001184 // Copy the file to another location using '..' in the path.
evan@chromium.org1543ad32009-08-27 05:00:14 +09001185 FilePath dest_file2(dir_name_from);
1186 dest_file2 = dest_file2.AppendASCII("..");
1187 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1188 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1189
1190 FilePath dest_file2_test(dir_name_from);
1191 dest_file2_test = dest_file2_test.DirName();
1192 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001193
1194 // Check everything has been copied.
1195 EXPECT_TRUE(file_util::PathExists(file_name_from));
1196 EXPECT_TRUE(file_util::PathExists(dest_file));
1197 const std::wstring read_contents = ReadTextFile(dest_file);
1198 EXPECT_EQ(file_contents, read_contents);
evan@chromium.org1543ad32009-08-27 05:00:14 +09001199 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1200 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commit3f4a7322008-07-27 06:49:38 +09001201}
1202
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001203// TODO(erikkay): implement
erikkay@google.com014161d2008-08-16 02:45:13 +09001204#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001205TEST_F(FileUtilTest, GetFileCreationLocalTime) {
evanm@google.com874d1672008-10-31 08:54:04 +09001206 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001207
1208 SYSTEMTIME start_time;
1209 GetLocalTime(&start_time);
1210 Sleep(100);
1211 CreateTextFile(file_name, L"New file!");
1212 Sleep(100);
1213 SYSTEMTIME end_time;
1214 GetLocalTime(&end_time);
1215
1216 SYSTEMTIME file_creation_time;
evanm@google.com874d1672008-10-31 08:54:04 +09001217 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commit3f4a7322008-07-27 06:49:38 +09001218
1219 FILETIME start_filetime;
1220 SystemTimeToFileTime(&start_time, &start_filetime);
1221 FILETIME end_filetime;
1222 SystemTimeToFileTime(&end_time, &end_filetime);
1223 FILETIME file_creation_filetime;
1224 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1225
1226 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1227 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1228 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1229
1230 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1231 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1232 "end time: " << FileTimeAsUint64(end_filetime);
1233
evanm@google.com874d1672008-10-31 08:54:04 +09001234 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commit3f4a7322008-07-27 06:49:38 +09001235}
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001236#endif
initial.commit3f4a7322008-07-27 06:49:38 +09001237
erikkay@google.comf2406842008-08-21 00:59:49 +09001238// file_util winds up using autoreleased objects on the Mac, so this needs
evanm@google.com874d1672008-10-31 08:54:04 +09001239// to be a PlatformTest.
erikkay@google.comf2406842008-08-21 00:59:49 +09001240typedef PlatformTest ReadOnlyFileUtilTest;
initial.commit3f4a7322008-07-27 06:49:38 +09001241
erikkay@google.comf2406842008-08-21 00:59:49 +09001242TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
evanm@google.com874d1672008-10-31 08:54:04 +09001243 FilePath data_dir;
initial.commit3f4a7322008-07-27 06:49:38 +09001244 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
evanm@google.com874d1672008-10-31 08:54:04 +09001245 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1246 .Append(FILE_PATH_LITERAL("data"))
1247 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commit3f4a7322008-07-27 06:49:38 +09001248 ASSERT_TRUE(file_util::PathExists(data_dir));
1249
evanm@google.com874d1672008-10-31 08:54:04 +09001250 FilePath original_file =
1251 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1252 FilePath same_file =
1253 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1254 FilePath same_length_file =
1255 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1256 FilePath different_file =
1257 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1258 FilePath different_first_file =
1259 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1260 FilePath different_last_file =
1261 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1262 FilePath empty1_file =
1263 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1264 FilePath empty2_file =
1265 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1266 FilePath shortened_file =
1267 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1268 FilePath binary_file =
1269 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1270 FilePath binary_file_same =
1271 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1272 FilePath binary_file_diff =
1273 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commit3f4a7322008-07-27 06:49:38 +09001274
1275 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1276 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1277 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1278 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
thakis@chromium.org506f0912009-12-02 07:14:22 +09001279 EXPECT_FALSE(file_util::ContentsEqual(
1280 FilePath(FILE_PATH_LITERAL("bogusname")),
1281 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commit3f4a7322008-07-27 06:49:38 +09001282 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1283 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1284 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1285 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1286 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1287 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1288 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1289}
1290
mark@chromium.org95c9ec92009-06-27 06:17:24 +09001291TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1292 FilePath data_dir;
1293 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1294 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1295 .Append(FILE_PATH_LITERAL("data"))
1296 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1297 ASSERT_TRUE(file_util::PathExists(data_dir));
1298
1299 FilePath original_file =
1300 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1301 FilePath same_file =
1302 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1303 FilePath crlf_file =
1304 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1305 FilePath shortened_file =
1306 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1307 FilePath different_file =
1308 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1309 FilePath different_first_file =
1310 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1311 FilePath different_last_file =
1312 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1313 FilePath first1_file =
1314 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1315 FilePath first2_file =
1316 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1317 FilePath empty1_file =
1318 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1319 FilePath empty2_file =
1320 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1321 FilePath blank_line_file =
1322 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1323 FilePath blank_line_crlf_file =
1324 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1325
1326 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1327 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1328 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1329 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1330 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1331 different_first_file));
1332 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1333 different_last_file));
1334 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1335 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1336 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1337 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1338 blank_line_crlf_file));
1339}
1340
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001341// We don't need equivalent functionality outside of Windows.
erikkay@google.com014161d2008-08-16 02:45:13 +09001342#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09001343TEST_F(FileUtilTest, ResolveShortcutTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001344 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001345 CreateTextFile(target_file, L"This is the target.");
1346
evanm@google.com874d1672008-10-31 08:54:04 +09001347 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001348
1349 HRESULT result;
1350 IShellLink *shell = NULL;
1351 IPersistFile *persist = NULL;
1352
1353 CoInitialize(NULL);
1354 // Temporarily create a shortcut for test
1355 result = CoCreateInstance(CLSID_ShellLink, NULL,
1356 CLSCTX_INPROC_SERVER, IID_IShellLink,
1357 reinterpret_cast<LPVOID*>(&shell));
1358 EXPECT_TRUE(SUCCEEDED(result));
1359 result = shell->QueryInterface(IID_IPersistFile,
1360 reinterpret_cast<LPVOID*>(&persist));
1361 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001362 result = shell->SetPath(target_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001363 EXPECT_TRUE(SUCCEEDED(result));
1364 result = shell->SetDescription(L"ResolveShortcutTest");
1365 EXPECT_TRUE(SUCCEEDED(result));
evanm@google.com874d1672008-10-31 08:54:04 +09001366 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commit3f4a7322008-07-27 06:49:38 +09001367 EXPECT_TRUE(SUCCEEDED(result));
1368 if (persist)
1369 persist->Release();
1370 if (shell)
1371 shell->Release();
1372
1373 bool is_solved;
evan@chromium.orga4899042009-08-25 10:51:44 +09001374 is_solved = file_util::ResolveShortcut(&link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001375 EXPECT_TRUE(is_solved);
1376 std::wstring contents;
evan@chromium.orga4899042009-08-25 10:51:44 +09001377 contents = ReadTextFile(link_file);
initial.commit3f4a7322008-07-27 06:49:38 +09001378 EXPECT_EQ(L"This is the target.", contents);
1379
ericroman@google.comdbff4f52008-08-19 01:00:38 +09001380 // Cleaning
evanm@google.com874d1672008-10-31 08:54:04 +09001381 DeleteFile(target_file.value().c_str());
evan@chromium.orga4899042009-08-25 10:51:44 +09001382 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001383 CoUninitialize();
1384}
1385
1386TEST_F(FileUtilTest, CreateShortcutTest) {
1387 const wchar_t file_contents[] = L"This is another target.";
evanm@google.com874d1672008-10-31 08:54:04 +09001388 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commit3f4a7322008-07-27 06:49:38 +09001389 CreateTextFile(target_file, file_contents);
1390
evanm@google.com874d1672008-10-31 08:54:04 +09001391 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commit3f4a7322008-07-27 06:49:38 +09001392
1393 CoInitialize(NULL);
evanm@google.com874d1672008-10-31 08:54:04 +09001394 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1395 link_file.value().c_str(),
xiyuan@chromium.orgd9e9bb42009-11-19 18:18:50 +09001396 NULL, NULL, NULL, NULL, 0, NULL));
evan@chromium.orga4899042009-08-25 10:51:44 +09001397 FilePath resolved_name = link_file;
initial.commit3f4a7322008-07-27 06:49:38 +09001398 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
evan@chromium.orga4899042009-08-25 10:51:44 +09001399 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commit3f4a7322008-07-27 06:49:38 +09001400 EXPECT_EQ(file_contents, read_contents);
1401
evanm@google.com874d1672008-10-31 08:54:04 +09001402 DeleteFile(target_file.value().c_str());
1403 DeleteFile(link_file.value().c_str());
initial.commit3f4a7322008-07-27 06:49:38 +09001404 CoUninitialize();
1405}
huanr@chromium.org7f2c6af2009-03-12 03:37:48 +09001406
1407TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1408 // Create a directory
1409 FilePath dir_name_from =
1410 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1411 file_util::CreateDirectory(dir_name_from);
1412 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1413
1414 // Create a file under the directory
1415 FilePath file_name_from =
1416 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1417 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1418 ASSERT_TRUE(file_util::PathExists(file_name_from));
1419
1420 // Move the directory by using CopyAndDeleteDirectory
1421 FilePath dir_name_to = test_dir_.Append(
1422 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1423 FilePath file_name_to =
1424 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1425
1426 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1427
1428 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1429
1430 // Check everything has been moved.
1431 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1432 EXPECT_FALSE(file_util::PathExists(file_name_from));
1433 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1434 EXPECT_TRUE(file_util::PathExists(file_name_to));
1435}
tkent@chromium.org8da14162009-10-09 16:33:39 +09001436
1437TEST_F(FileUtilTest, GetTempDirTest) {
1438 static const TCHAR* kTmpKey = _T("TMP");
1439 static const TCHAR* kTmpValues[] = {
1440 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1441 };
1442 // Save the original $TMP.
1443 size_t original_tmp_size;
1444 TCHAR* original_tmp;
1445 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1446 // original_tmp may be NULL.
1447
1448 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1449 FilePath path;
1450 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1451 file_util::GetTempDir(&path);
1452 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1453 " result=" << path.value();
1454 }
1455
1456 // Restore the original $TMP.
1457 if (original_tmp) {
1458 ::_tputenv_s(kTmpKey, original_tmp);
1459 free(original_tmp);
1460 } else {
1461 ::_tputenv_s(kTmpKey, _T(""));
1462 }
1463}
1464#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +09001465
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001466TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1467 FilePath temp_files[3];
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001468 for (int i = 0; i < 3; i++) {
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001469 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001470 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1471 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1472 }
1473 for (int i = 0; i < 3; i++)
1474 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1475 for (int i = 0; i < 3; i++)
1476 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1477}
1478
erikkay@chromium.org18f0dde2009-08-19 01:07:55 +09001479TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001480 FilePath names[3];
1481 FILE *fps[3];
1482 int i;
1483
1484 // Create; make sure they are open and exist.
1485 for (i = 0; i < 3; ++i) {
1486 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1487 ASSERT_TRUE(fps[i]);
1488 EXPECT_TRUE(file_util::PathExists(names[i]));
1489 }
1490
1491 // Make sure all names are unique.
1492 for (i = 0; i < 3; ++i) {
1493 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1494 }
1495
1496 // Close and delete.
1497 for (i = 0; i < 3; ++i) {
1498 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1499 EXPECT_TRUE(file_util::Delete(names[i], false));
1500 }
initial.commit3f4a7322008-07-27 06:49:38 +09001501}
1502
1503TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
evan@chromium.org1543ad32009-08-27 05:00:14 +09001504 FilePath temp_dir;
1505 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1506 &temp_dir));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001507 EXPECT_TRUE(file_util::PathExists(temp_dir));
1508 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commit3f4a7322008-07-27 06:49:38 +09001509}
1510
skerner@chromium.orge4432392010-05-01 02:00:09 +09001511TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1512 FilePath new_dir;
1513 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1514 test_dir_,
1515 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
skerner@chromium.orgbd112ab2010-06-30 16:19:11 +09001516 &new_dir));
skerner@chromium.orge4432392010-05-01 02:00:09 +09001517 EXPECT_TRUE(file_util::PathExists(new_dir));
1518 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1519 EXPECT_TRUE(file_util::Delete(new_dir, false));
1520}
1521
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001522TEST_F(FileUtilTest, GetShmemTempDirTest) {
1523 FilePath dir;
1524 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1525 EXPECT_TRUE(file_util::DirectoryExists(dir));
1526}
1527
initial.commit3f4a7322008-07-27 06:49:38 +09001528TEST_F(FileUtilTest, CreateDirectoryTest) {
evanm@google.com874d1672008-10-31 08:54:04 +09001529 FilePath test_root =
1530 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
erikkay@google.com014161d2008-08-16 02:45:13 +09001531#if defined(OS_WIN)
evanm@google.com874d1672008-10-31 08:54:04 +09001532 FilePath test_path =
1533 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001534#elif defined(OS_POSIX)
evanm@google.com874d1672008-10-31 08:54:04 +09001535 FilePath test_path =
1536 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001537#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001538
1539 EXPECT_FALSE(file_util::PathExists(test_path));
1540 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1541 EXPECT_TRUE(file_util::PathExists(test_path));
1542 // CreateDirectory returns true if the DirectoryExists returns true.
1543 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1544
1545 // Doesn't work to create it on top of a non-dir
evanm@google.com874d1672008-10-31 08:54:04 +09001546 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001547 EXPECT_FALSE(file_util::PathExists(test_path));
1548 CreateTextFile(test_path, L"test file");
1549 EXPECT_TRUE(file_util::PathExists(test_path));
1550 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1551
1552 EXPECT_TRUE(file_util::Delete(test_root, true));
1553 EXPECT_FALSE(file_util::PathExists(test_root));
1554 EXPECT_FALSE(file_util::PathExists(test_path));
joi@chromium.org9cd6dd22009-11-27 23:54:41 +09001555
1556 // Verify assumptions made by the Windows implementation:
1557 // 1. The current directory always exists.
1558 // 2. The root directory always exists.
1559 ASSERT_TRUE(file_util::DirectoryExists(
1560 FilePath(FilePath::kCurrentDirectory)));
1561 FilePath top_level = test_root;
1562 while (top_level != top_level.DirName()) {
1563 top_level = top_level.DirName();
1564 }
1565 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1566
1567 // Given these assumptions hold, it should be safe to
1568 // test that "creating" these directories succeeds.
1569 EXPECT_TRUE(file_util::CreateDirectory(
1570 FilePath(FilePath::kCurrentDirectory)));
1571 EXPECT_TRUE(file_util::CreateDirectory(top_level));
huanr@chromium.org57c9dc32009-12-18 05:42:40 +09001572
1573#if defined(OS_WIN)
1574 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1575 FilePath invalid_path =
1576 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1577 if (!file_util::PathExists(invalid_drive)) {
1578 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1579 }
1580#endif
mmoss@google.com733df6b2008-09-12 01:09:11 +09001581}
1582
1583TEST_F(FileUtilTest, DetectDirectoryTest) {
1584 // Check a directory
evanm@google.com874d1672008-10-31 08:54:04 +09001585 FilePath test_root =
1586 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001587 EXPECT_FALSE(file_util::PathExists(test_root));
1588 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1589 EXPECT_TRUE(file_util::PathExists(test_root));
1590 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1591
1592 // Check a file
evanm@google.com874d1672008-10-31 08:54:04 +09001593 FilePath test_path =
1594 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
mmoss@google.com733df6b2008-09-12 01:09:11 +09001595 EXPECT_FALSE(file_util::PathExists(test_path));
1596 CreateTextFile(test_path, L"test file");
1597 EXPECT_TRUE(file_util::PathExists(test_path));
1598 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1599 EXPECT_TRUE(file_util::Delete(test_path, false));
1600
1601 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commit3f4a7322008-07-27 06:49:38 +09001602}
1603
initial.commit3f4a7322008-07-27 06:49:38 +09001604TEST_F(FileUtilTest, FileEnumeratorTest) {
1605 // Test an empty directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001606 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001607 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1608 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commit3f4a7322008-07-27 06:49:38 +09001609
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001610 // Test an empty directory, non-recursively, including "..".
1611 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1612 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1613 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1614 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1615 f0_dotdot.Next().value());
1616 EXPECT_EQ(FILE_PATH_LITERAL(""),
1617 f0_dotdot.Next().value());
1618
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001619 // create the directories
evanm@google.com874d1672008-10-31 08:54:04 +09001620 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001621 EXPECT_TRUE(file_util::CreateDirectory(dir1));
evanm@google.com874d1672008-10-31 08:54:04 +09001622 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001623 EXPECT_TRUE(file_util::CreateDirectory(dir2));
evanm@google.com874d1672008-10-31 08:54:04 +09001624 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001625 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
evanm@google.com874d1672008-10-31 08:54:04 +09001626
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001627 // create the files
evanm@google.com874d1672008-10-31 08:54:04 +09001628 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001629 CreateTextFile(dir2file, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001630 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001631 CreateTextFile(dir2innerfile, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001632 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001633 CreateTextFile(file1, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001634 FilePath file2_rel =
1635 dir2.Append(FilePath::kParentDirectory)
1636 .Append(FILE_PATH_LITERAL("file2.txt"));
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001637 CreateTextFile(file2_rel, L"");
evanm@google.com874d1672008-10-31 08:54:04 +09001638 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commit3f4a7322008-07-27 06:49:38 +09001639
1640 // Only enumerate files.
avi@google.com5cb79352008-12-11 23:55:12 +09001641 file_util::FileEnumerator f1(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001642 file_util::FileEnumerator::FILES);
1643 FindResultCollector c1(f1);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001644 EXPECT_TRUE(c1.HasFile(file1));
1645 EXPECT_TRUE(c1.HasFile(file2_abs));
1646 EXPECT_TRUE(c1.HasFile(dir2file));
1647 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1648 EXPECT_EQ(c1.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001649
1650 // Only enumerate directories.
avi@google.com5cb79352008-12-11 23:55:12 +09001651 file_util::FileEnumerator f2(test_dir_, true,
initial.commit3f4a7322008-07-27 06:49:38 +09001652 file_util::FileEnumerator::DIRECTORIES);
1653 FindResultCollector c2(f2);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001654 EXPECT_TRUE(c2.HasFile(dir1));
1655 EXPECT_TRUE(c2.HasFile(dir2));
1656 EXPECT_TRUE(c2.HasFile(dir2inner));
1657 EXPECT_EQ(c2.size(), 3);
initial.commit3f4a7322008-07-27 06:49:38 +09001658
tim@chromium.org989d0972008-10-16 11:42:45 +09001659 // Only enumerate directories non-recursively.
1660 file_util::FileEnumerator f2_non_recursive(
avi@google.com5cb79352008-12-11 23:55:12 +09001661 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
tim@chromium.org989d0972008-10-16 11:42:45 +09001662 FindResultCollector c2_non_recursive(f2_non_recursive);
1663 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1664 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1665 EXPECT_EQ(c2_non_recursive.size(), 2);
1666
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001667 // Only enumerate directories, non-recursively, including "..".
1668 file_util::FileEnumerator f2_dotdot(
1669 test_dir_, false,
1670 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1671 file_util::FileEnumerator::DIRECTORIES |
1672 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1673 FindResultCollector c2_dotdot(f2_dotdot);
1674 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1675 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1676 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1677 EXPECT_EQ(c2_dotdot.size(), 3);
1678
initial.commit3f4a7322008-07-27 06:49:38 +09001679 // Enumerate files and directories.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001680 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001681 FindResultCollector c3(f3);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001682 EXPECT_TRUE(c3.HasFile(dir1));
1683 EXPECT_TRUE(c3.HasFile(dir2));
1684 EXPECT_TRUE(c3.HasFile(file1));
1685 EXPECT_TRUE(c3.HasFile(file2_abs));
1686 EXPECT_TRUE(c3.HasFile(dir2file));
1687 EXPECT_TRUE(c3.HasFile(dir2inner));
1688 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1689 EXPECT_EQ(c3.size(), 7);
initial.commit3f4a7322008-07-27 06:49:38 +09001690
1691 // Non-recursive operation.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001692 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commit3f4a7322008-07-27 06:49:38 +09001693 FindResultCollector c4(f4);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001694 EXPECT_TRUE(c4.HasFile(dir2));
1695 EXPECT_TRUE(c4.HasFile(dir2));
1696 EXPECT_TRUE(c4.HasFile(file1));
1697 EXPECT_TRUE(c4.HasFile(file2_abs));
1698 EXPECT_EQ(c4.size(), 4);
initial.commit3f4a7322008-07-27 06:49:38 +09001699
1700 // Enumerate with a pattern.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001701 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
avi@google.com5cb79352008-12-11 23:55:12 +09001702 FILE_PATH_LITERAL("dir*"));
initial.commit3f4a7322008-07-27 06:49:38 +09001703 FindResultCollector c5(f5);
erikkay@google.comdfb51b22008-08-16 02:32:10 +09001704 EXPECT_TRUE(c5.HasFile(dir1));
1705 EXPECT_TRUE(c5.HasFile(dir2));
1706 EXPECT_TRUE(c5.HasFile(dir2file));
1707 EXPECT_TRUE(c5.HasFile(dir2inner));
1708 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1709 EXPECT_EQ(c5.size(), 5);
initial.commit3f4a7322008-07-27 06:49:38 +09001710
1711 // Make sure the destructor closes the find handle while in the middle of a
1712 // query to allow TearDown to delete the directory.
yuzo@chromium.org2da0f822009-06-09 14:57:38 +09001713 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
avi@google.com5cb79352008-12-11 23:55:12 +09001714 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1715 // (we don't care what).
initial.commit3f4a7322008-07-27 06:49:38 +09001716}
license.botf003cfe2008-08-24 09:55:55 +09001717
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001718TEST_F(FileUtilTest, Contains) {
thestig@chromium.org4cfbf7a2009-03-11 03:20:44 +09001719 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001720
1721 // Create a fresh, empty copy of this directory.
rvargas@google.com5a0ae3b2009-01-31 10:19:57 +09001722 if (file_util::PathExists(data_dir)) {
1723 ASSERT_TRUE(file_util::Delete(data_dir, true));
1724 }
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001725 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1726
1727 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1728 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1729 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1730 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1731
1732 // Annoyingly, the directories must actually exist in order for realpath(),
1733 // which Contains() relies on in posix, to work.
1734 ASSERT_TRUE(file_util::CreateDirectory(foo));
1735 std::string data("hello");
phajdan.jr@chromium.orgf9908a72009-04-04 02:17:58 +09001736 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1737 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1738 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001739
1740 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1741 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1742 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1743 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1744
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001745 // Platform-specific concerns.
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001746 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1747#if defined(OS_WIN)
1748 EXPECT_TRUE(file_util::ContainsPath(foo,
1749 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
jrg@chromium.orgd505c3a2009-02-04 09:58:39 +09001750 EXPECT_TRUE(file_util::ContainsPath(foo,
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001751 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
evan@chromium.org875bb6e2009-12-29 09:32:52 +09001752#elif defined(OS_MACOSX)
1753 // We can't really do this test on OS X since the case-sensitivity of the
1754 // filesystem is configurable.
1755#elif defined(OS_POSIX)
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001756 EXPECT_FALSE(file_util::ContainsPath(foo,
1757 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
aa@chromium.orga4dbdf22009-01-10 07:14:27 +09001758#endif
1759}
1760
jochen@chromium.orga6879772010-02-18 19:02:26 +09001761TEST_F(FileUtilTest, LastModified) {
1762 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1763
1764 // Create a fresh, empty copy of this directory.
1765 if (file_util::PathExists(data_dir)) {
1766 ASSERT_TRUE(file_util::Delete(data_dir, true));
1767 }
1768 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1769
1770 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1771 std::string data("hello");
1772 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1773
1774 base::Time modification_time;
1775 // Note that this timestamp is divisible by two (seconds) - FAT stores
1776 // modification times with 2s resolution.
1777 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1778 &modification_time));
1779 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1780 file_util::FileInfo file_info;
1781 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1782 ASSERT_TRUE(file_info.last_modified == modification_time);
1783}
1784
tfarina@chromium.org34828222010-05-26 10:40:12 +09001785TEST_F(FileUtilTest, IsDirectoryEmpty) {
1786 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1787
1788 ASSERT_FALSE(file_util::PathExists(empty_dir));
1789
1790 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1791
1792 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1793
1794 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1795 std::string bar("baz");
1796 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1797
1798 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1799}
1800
mark@chromium.org17684802008-09-10 09:16:28 +09001801} // namespace