blob: 219b63da785c7214e9df931955ee52b51d76e05c [file] [log] [blame]
[email protected]ec3d1452010-02-18 10:02:261// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]f35d39b2008-08-15 17:50:105#include "build/build_config.h"
6
[email protected]8541bb82008-08-15 17:45:137#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:388#include <windows.h>
[email protected]6f5f4322010-06-09 22:56:489#include <winioctl.h>
initial.commitd7cae122008-07-26 21:49:3810#include <shellapi.h>
11#include <shlobj.h>
[email protected]7d95aae2009-10-09 07:33:3912#include <tchar.h>
[email protected]37088fef2008-08-15 17:32:1013#endif
initial.commitd7cae122008-07-26 21:49:3814
15#include <fstream>
16#include <iostream>
[email protected]37088fef2008-08-15 17:32:1017#include <set>
initial.commitd7cae122008-07-26 21:49:3818
19#include "base/base_paths.h"
[email protected]640517f2008-10-30 23:54:0420#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3821#include "base/file_util.h"
22#include "base/logging.h"
23#include "base/path_service.h"
[email protected]2126577b2009-04-23 15:05:1924#include "base/platform_thread.h"
[email protected]6f5f4322010-06-09 22:56:4825#include "base/scoped_handle.h"
[email protected]85786f92009-04-18 00:42:4826#include "base/time.h"
[email protected]047a03f2009-10-07 02:10:2027#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3828#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1529#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3830
[email protected]4e9f6be2009-04-03 17:17:5831// This macro helps avoid wrapped lines in the test structs.
32#define FPL(x) FILE_PATH_LITERAL(x)
33
initial.commitd7cae122008-07-26 21:49:3834namespace {
35
[email protected]6f5f4322010-06-09 22:56:4836// 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
[email protected]83d86252010-05-07 18:58:45111const wchar_t bogus_content[] = L"I'm cannon fodder.";
112
[email protected]8199b3a2009-06-09 05:57:38113const 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
[email protected]ed2f2332008-08-20 15:59:49118// file_util winds up using autoreleased objects on the Mac, so this needs
119// to be a PlatformTest
120class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:38121 protected:
122 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:49123 PlatformTest::SetUp();
initial.commitd7cae122008-07-26 21:49:38124 // Name a subdirectory of the temp directory.
125 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
[email protected]640517f2008-10-30 23:54:04126 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commitd7cae122008-07-26 21:49:38127
128 // Create a fresh, empty copy of this directory.
129 file_util::Delete(test_dir_, true);
[email protected]640517f2008-10-30 23:54:04130 file_util::CreateDirectory(test_dir_);
initial.commitd7cae122008-07-26 21:49:38131 }
132 virtual void TearDown() {
[email protected]ed2f2332008-08-20 15:59:49133 PlatformTest::TearDown();
initial.commitd7cae122008-07-26 21:49:38134 // Clean up test directory
[email protected]37088fef2008-08-15 17:32:10135 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commitd7cae122008-07-26 21:49:38136 ASSERT_FALSE(file_util::PathExists(test_dir_));
137 }
138
139 // the path to temporary directory used to contain the test operations
[email protected]640517f2008-10-30 23:54:04140 FilePath test_dir_;
initial.commitd7cae122008-07-26 21:49:38141};
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:
[email protected]53c58042009-08-26 20:00:14147 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
[email protected]0b733222008-12-11 14:55:12148 FilePath cur_file;
149 while (!(cur_file = enumerator.Next()).value().empty()) {
150 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:38151 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:04152 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:38153 << "Same file returned twice";
154
155 // Save for later.
[email protected]640517f2008-10-30 23:54:04156 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:38157 }
158 }
159
160 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:04161 bool HasFile(const FilePath& file) const {
162 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:38163 }
[email protected]640517f2008-10-30 23:54:04164
[email protected]37088fef2008-08-15 17:32:10165 int size() {
[email protected]f35d39b2008-08-15 17:50:10166 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:10167 }
initial.commitd7cae122008-07-26 21:49:38168
169 private:
[email protected]640517f2008-10-30 23:54:04170 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:38171};
172
173// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:04174void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:38175 const std::wstring& contents) {
176 std::ofstream file;
[email protected]640517f2008-10-30 23:54:04177 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:38178 ASSERT_TRUE(file.is_open());
179 file << contents;
180 file.close();
181}
182
183// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:04184std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:10185 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38186 std::wifstream file;
[email protected]640517f2008-10-30 23:54:04187 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:38188 EXPECT_TRUE(file.is_open());
189 file.getline(contents, 64);
190 file.close();
191 return std::wstring(contents);
192}
193
[email protected]8541bb82008-08-15 17:45:13194#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38195uint64 FileTimeAsUint64(const FILETIME& ft) {
196 ULARGE_INTEGER u;
197 u.LowPart = ft.dwLowDateTime;
198 u.HighPart = ft.dwHighDateTime;
199 return u.QuadPart;
200}
[email protected]37088fef2008-08-15 17:32:10201#endif
initial.commitd7cae122008-07-26 21:49:38202
203const struct append_case {
204 const wchar_t* path;
205 const wchar_t* ending;
206 const wchar_t* result;
207} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13208#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38209 {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"\\"},
[email protected]37088fef2008-08-15 17:32:10216#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.commitd7cae122008-07-26 21:49:38225};
226
[email protected]1840cfc2010-02-26 15:11:55227#if defined(OS_WIN)
228// This function is deprecated, but still used on Windows.
initial.commitd7cae122008-07-26 21:49:38229TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10230 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38231 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}
[email protected]1840cfc2010-02-26 15:11:55241#endif // defined(OS_WIN)
242
initial.commitd7cae122008-07-26 21:49:38243
244static const struct InsertBeforeExtensionCase {
[email protected]4e9f6be2009-04-03 17:17:58245 const FilePath::CharType* path;
246 const FilePath::CharType* suffix;
247 const FilePath::CharType* result;
initial.commitd7cae122008-07-26 21:49:38248} kInsertBeforeExtension[] = {
[email protected]4e9f6be2009-04-03 17:17:58249 {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")},
[email protected]8541bb82008-08-15 17:45:13265#if defined(OS_WIN)
[email protected]4e9f6be2009-04-03 17:17:58266 {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")},
[email protected]37088fef2008-08-15 17:32:10279#elif defined(OS_POSIX)
[email protected]4e9f6be2009-04-03 17:17:58280 {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")},
[email protected]37088fef2008-08-15 17:32:10292#endif
initial.commitd7cae122008-07-26 21:49:38293};
294
295TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
[email protected]37088fef2008-08-15 17:32:10296 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
[email protected]4e9f6be2009-04-03 17:17:58297 FilePath path(kInsertBeforeExtension[i].path);
initial.commitd7cae122008-07-26 21:49:38298 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
[email protected]4e9f6be2009-04-03 17:17:58299 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
initial.commitd7cae122008-07-26 21:49:38300 }
301}
302
303static const struct filename_case {
304 const wchar_t* path;
305 const wchar_t* filename;
306} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13307#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38308 {L"c:\\colon\\backslash", L"backslash"},
309 {L"c:\\colon\\backslash\\", L""},
310 {L"\\\\filename.exe", L"filename.exe"},
311 {L"filename.exe", L"filename.exe"},
312 {L"", L""},
313 {L"\\\\\\", L""},
314 {L"c:/colon/backslash", L"backslash"},
315 {L"c:/colon/backslash/", L""},
316 {L"//////", L""},
317 {L"///filename.exe", L"filename.exe"},
[email protected]37088fef2008-08-15 17:32:10318#elif defined(OS_POSIX)
319 {L"/foo/bar", L"bar"},
320 {L"/foo/bar/", L""},
321 {L"/filename.exe", L"filename.exe"},
322 {L"filename.exe", L"filename.exe"},
323 {L"", L""},
324 {L"/", L""},
325#endif
initial.commitd7cae122008-07-26 21:49:38326};
327
328TEST_F(FileUtilTest, GetFilenameFromPath) {
[email protected]37088fef2008-08-15 17:32:10329 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38330 const filename_case& value = filename_cases[i];
331 std::wstring result = file_util::GetFilenameFromPath(value.path);
332 EXPECT_EQ(value.filename, result);
333 }
334}
335
336// Test finding the file type from a path name
337static const struct extension_case {
338 const wchar_t* path;
339 const wchar_t* extension;
340} extension_cases[] = {
[email protected]8541bb82008-08-15 17:45:13341#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38342 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
343 {L"C:\\colon\\backslash\\filename.", L""},
344 {L"C:\\colon\\backslash\\filename", L""},
345 {L"C:\\colon\\backslash\\", L""},
346 {L"C:\\colon\\backslash.\\", L""},
347 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
[email protected]37088fef2008-08-15 17:32:10348#elif defined(OS_POSIX)
349 {L"/foo/bar/filename.extension", L"extension"},
350 {L"/foo/bar/filename.", L""},
351 {L"/foo/bar/filename", L""},
352 {L"/foo/bar/", L""},
353 {L"/foo/bar./", L""},
354 {L"/foo/bar/filename.extension.extension2", L"extension2"},
355 {L".", L""},
356 {L"..", L""},
357 {L"./foo", L""},
358 {L"./foo.extension", L"extension"},
359 {L"/foo.extension1/bar.extension2", L"extension2"},
360#endif
initial.commitd7cae122008-07-26 21:49:38361};
362
363TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10364 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38365 const extension_case& ext = extension_cases[i];
366 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
367 EXPECT_EQ(ext.extension, fext);
368 }
369}
370
371// Test finding the directory component of a path
372static const struct dir_case {
373 const wchar_t* full_path;
374 const wchar_t* directory;
375} dir_cases[] = {
[email protected]8541bb82008-08-15 17:45:13376#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38377 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
378 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
379 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
380 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
381 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
382 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
[email protected]2c8088a42009-10-15 05:00:25383 {L"C:\\", L"C:\\"},
[email protected]37088fef2008-08-15 17:32:10384#elif defined(OS_POSIX)
385 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
386 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
387 {L"/foo/bar/", L"/foo/bar"},
388 {L"/foo/bar//", L"/foo/bar"},
389 {L"/foo/bar", L"/foo"},
390 {L"/foo/bar./", L"/foo/bar."},
391 {L"/", L"/"},
392 {L".", L"."},
[email protected]53c58042009-08-26 20:00:14393 {L"..", L"."}, // yes, ".." technically lives in "."
[email protected]37088fef2008-08-15 17:32:10394#endif
initial.commitd7cae122008-07-26 21:49:38395};
396
[email protected]a18f79a2010-02-23 12:52:11397#if defined(OS_WIN)
398// This function is deprecated, and only exists on Windows anymore.
initial.commitd7cae122008-07-26 21:49:38399TEST_F(FileUtilTest, GetDirectoryFromPath) {
[email protected]37088fef2008-08-15 17:32:10400 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38401 const dir_case& dir = dir_cases[i];
402 const std::wstring parent =
403 file_util::GetDirectoryFromPath(dir.full_path);
404 EXPECT_EQ(dir.directory, parent);
405 }
406}
[email protected]a18f79a2010-02-23 12:52:11407#endif
initial.commitd7cae122008-07-26 21:49:38408
[email protected]2ec79f72010-06-10 13:05:26409// Flaky, http://crbug.com/46246
[email protected]552b3152010-06-10 12:40:52410TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commitd7cae122008-07-26 21:49:38411 // Create old file (that we don't want to count)
[email protected]85786f92009-04-18 00:42:48412 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commitd7cae122008-07-26 21:49:38413 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
414
415 // Age to perfection
[email protected]4b7743de2009-04-21 01:50:39416#if defined(OS_WIN)
[email protected]2126577b2009-04-23 15:05:19417 PlatformThread::Sleep(100);
[email protected]4b7743de2009-04-21 01:50:39418#elif defined(OS_POSIX)
419 // We need to wait at least one second here because the precision of
420 // file creation time is one second.
[email protected]2126577b2009-04-23 15:05:19421 PlatformThread::Sleep(1500);
[email protected]4b7743de2009-04-21 01:50:39422#endif
initial.commitd7cae122008-07-26 21:49:38423
424 // Establish our cutoff time
[email protected]85786f92009-04-18 00:42:48425 base::Time now(base::Time::NowFromSystemTime());
426 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38427
428 // Create a new file (that we do want to count)
[email protected]85786f92009-04-18 00:42:48429 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commitd7cae122008-07-26 21:49:38430 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
431
432 // We should see only the new file.
[email protected]85786f92009-04-18 00:42:48433 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38434
435 // Delete new file, we should see no files after cutoff now
436 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]85786f92009-04-18 00:42:48437 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38438}
439
[email protected]c2c132c62010-03-24 21:56:26440TEST_F(FileUtilTest, FileAndDirectorySize) {
441 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
442 // should return 53 bytes.
443 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
444 CreateTextFile(file_01, L"12345678901234567890");
445 int64 size_f1 = 0;
446 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
447 EXPECT_EQ(20ll, size_f1);
448
449 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
450 file_util::CreateDirectory(subdir_path);
451
452 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
453 CreateTextFile(file_02, L"123456789012345678901234567890");
454 int64 size_f2 = 0;
455 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
456 EXPECT_EQ(30ll, size_f2);
457
458 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
459 file_util::CreateDirectory(subsubdir_path);
460
461 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
462 CreateTextFile(file_03, L"123");
463
464 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
465 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
466}
467
[email protected]6f5f4322010-06-09 22:56:48468TEST_F(FileUtilTest, NormalizeFilePathBasic) {
469 // Create a directory under the test dir. Because we create it,
470 // we know it is not a link.
471 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
472 FilePath dir_path = test_dir_.Append(FPL("dir"));
473 FilePath file_b_path = dir_path.Append(FPL("file_b"));
474 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57475
[email protected]6f5f4322010-06-09 22:56:48476 FilePath normalized_file_a_path, normalized_file_b_path;
477 ASSERT_FALSE(file_util::PathExists(file_a_path));
478 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
479 &normalized_file_a_path))
480 << "NormalizeFilePath() should fail on nonexistant paths.";
481
482 CreateTextFile(file_a_path, bogus_content);
483 ASSERT_TRUE(file_util::PathExists(file_a_path));
484 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
485 &normalized_file_a_path));
486
487 CreateTextFile(file_b_path, bogus_content);
488 ASSERT_TRUE(file_util::PathExists(file_b_path));
489 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
490 &normalized_file_b_path));
491
492 // Beacuse this test created |dir_path|, we know it is not a link
493 // or junction. So, the real path of the directory holding file a
494 // must be the parent of the path holding file b.
495 ASSERT_TRUE(normalized_file_a_path.DirName()
496 .IsParent(normalized_file_b_path.DirName()));
497}
498
499#if defined(OS_WIN)
500
501TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
502 // Build the following directory structure:
503 //
504 // test_dir_
505 // |-> base_a
506 // | |-> sub_a
507 // | |-> file.txt
508 // | |-> long_name___... (Very long name.)
509 // | |-> sub_long
510 // | |-> deep.txt
511 // |-> base_b
512 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
513 // |-> to_base_b (reparse point to test_dir_\base_b)
514 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
515
516 FilePath base_a = test_dir_.Append(FPL("base_a"));
517 ASSERT_TRUE(file_util::CreateDirectory(base_a));
518
519 FilePath sub_a = base_a.Append(FPL("sub_a"));
520 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
521
522 FilePath file_txt = sub_a.Append(FPL("file.txt"));
523 CreateTextFile(file_txt, bogus_content);
524
525 // Want a directory whose name is long enough to make the path to the file
526 // inside just under MAX_PATH chars. This will be used to test that when
527 // a junction expands to a path over MAX_PATH chars in length,
528 // NormalizeFilePath() fails without crashing.
529 FilePath sub_long_rel(FPL("sub_long"));
530 FilePath deep_txt(FPL("deep.txt"));
531
532 int target_length = MAX_PATH;
533 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
534 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52535 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48536 // the resulting path is still long enough to hit the failing case in
537 // NormalizePath().
538 const int kCreateDirLimit = 4;
539 target_length -= kCreateDirLimit;
540 FilePath::StringType long_name_str = FPL("long_name_");
541 long_name_str.resize(target_length, '_');
542
543 FilePath long_name = sub_a.Append(FilePath(long_name_str));
544 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
545 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
546
547 FilePath sub_long = deep_file.DirName();
548 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
549 CreateTextFile(deep_file, bogus_content);
550
551 FilePath base_b = test_dir_.Append(FPL("base_b"));
552 ASSERT_TRUE(file_util::CreateDirectory(base_b));
553
554 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
555 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
556 ScopedHandle reparse_to_sub_a(
557 ::CreateFile(to_sub_a.value().c_str(),
558 FILE_ALL_ACCESS,
559 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
560 NULL,
561 OPEN_EXISTING,
562 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
563 NULL));
564 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_a.Get());
565 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
566
567 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
568 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
569 ScopedHandle reparse_to_base_b(
570 ::CreateFile(to_base_b.value().c_str(),
571 FILE_ALL_ACCESS,
572 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
573 NULL,
574 OPEN_EXISTING,
575 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
576 NULL));
577 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_base_b.Get());
578 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
579
580 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
581 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
582 ScopedHandle reparse_to_sub_long(
583 ::CreateFile(to_sub_long.value().c_str(),
584 FILE_ALL_ACCESS,
585 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
586 NULL,
587 OPEN_EXISTING,
588 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
589 NULL));
590 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_long.Get());
591 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
592
593 // Normalize a junction free path: base_a\sub_a\file.txt .
594 FilePath normalized_path;
595 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
596 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
597
598 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
599 // the junction to_sub_a.
600 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
601 &normalized_path));
602 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
603
604 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
605 // normalized to exclude junctions to_base_b and to_sub_a .
606 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
607 .Append(FPL("to_base_b"))
608 .Append(FPL("to_sub_a"))
609 .Append(FPL("file.txt")),
610 &normalized_path));
611 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
612
613 // A long enough path will cause NormalizeFilePath() to fail. Make a long
614 // path using to_base_b many times, and check that paths long enough to fail
615 // do not cause a crash.
616 FilePath long_path = base_b;
617 const int kLengthLimit = MAX_PATH + 200;
618 while (long_path.value().length() <= kLengthLimit) {
619 long_path = long_path.Append(FPL("to_base_b"));
620 }
621 long_path = long_path.Append(FPL("to_sub_a"))
622 .Append(FPL("file.txt"));
623
624 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
625
626 // Normalizing the junction to deep.txt should fail, because the expanded
627 // path to deep.txt is longer than MAX_PATH.
628 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
629 &normalized_path));
630
631 // Delete the reparse points, and see that NormalizeFilePath() fails
632 // to traverse them.
633 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
634 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
635 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
636
637 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
638 &normalized_path));
639}
640
641#endif // defined(OS_WIN)
642
643// The following test of NormalizeFilePath() require that we create a symlink.
644// This can not be done on windows before vista. On vista, creating a symlink
645// requires privilege "SeCreateSymbolicLinkPrivilege".
646// TODO(skerner): Investigate the possibility of giving base_unittests the
647// privileges required to create a symlink.
648#if defined(OS_POSIX)
649
650bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
651 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
652}
653
654TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
655 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57656
657 // Link one file to another.
[email protected]6f5f4322010-06-09 22:56:48658 FilePath link_from = test_dir_.Append(FPL("from_file"));
659 FilePath link_to = test_dir_.Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57660 CreateTextFile(link_to, bogus_content);
661
[email protected]6f5f4322010-06-09 22:56:48662 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57663 << "Failed to create file symlink.";
664
[email protected]6f5f4322010-06-09 22:56:48665 // Check that NormalizeFilePath sees the link.
666 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57667 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48668 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
669 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57670
671 // Link to a directory.
[email protected]6f5f4322010-06-09 22:56:48672 link_from = test_dir_.Append(FPL("from_dir"));
673 link_to = test_dir_.Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57674 file_util::CreateDirectory(link_to);
675
[email protected]6f5f4322010-06-09 22:56:48676 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57677 << "Failed to create directory symlink.";
678
[email protected]6f5f4322010-06-09 22:56:48679 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
680 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57681
[email protected]6f5f4322010-06-09 22:56:48682 // Test that a loop in the links causes NormalizeFilePath() to return false.
683 link_from = test_dir_.Append(FPL("link_a"));
684 link_to = test_dir_.Append(FPL("link_b"));
685 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57686 << "Failed to create loop symlink a.";
[email protected]6f5f4322010-06-09 22:56:48687 ASSERT_TRUE(MakeSymlink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57688 << "Failed to create loop symlink b.";
689
690 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48691 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57692}
693#endif // defined(OS_POSIX)
694
[email protected]83d86252010-05-07 18:58:45695TEST_F(FileUtilTest, DeleteNonExistent) {
696 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
697 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36698
[email protected]83d86252010-05-07 18:58:45699 EXPECT_TRUE(file_util::Delete(non_existent, false));
700 ASSERT_FALSE(file_util::PathExists(non_existent));
701 EXPECT_TRUE(file_util::Delete(non_existent, true));
702 ASSERT_FALSE(file_util::PathExists(non_existent));
703}
704
705TEST_F(FileUtilTest, DeleteFile) {
706 // Create a file
707 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
708 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38709 ASSERT_TRUE(file_util::PathExists(file_name));
710
[email protected]83d86252010-05-07 18:58:45711 // Make sure it's deleted
712 EXPECT_TRUE(file_util::Delete(file_name, false));
713 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36714
[email protected]83d86252010-05-07 18:58:45715 // Test recursive case, create a new file
716 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
717 CreateTextFile(file_name, bogus_content);
718 ASSERT_TRUE(file_util::PathExists(file_name));
719
720 // Make sure it's deleted
721 EXPECT_TRUE(file_util::Delete(file_name, true));
722 EXPECT_FALSE(file_util::PathExists(file_name));
723}
724
725#if defined(OS_WIN)
726// Tests that the Delete function works for wild cards, especially
727// with the recursion flag. Also coincidentally tests PathExists.
728// TODO(erikkay): see if anyone's actually using this feature of the API
729TEST_F(FileUtilTest, DeleteWildCard) {
730 // Create a file and a directory
731 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
732 CreateTextFile(file_name, bogus_content);
733 ASSERT_TRUE(file_util::PathExists(file_name));
734
735 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
736 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38737 ASSERT_TRUE(file_util::PathExists(subdir_path));
738
[email protected]83d86252010-05-07 18:58:45739 // Create the wildcard path
[email protected]640517f2008-10-30 23:54:04740 FilePath directory_contents = test_dir_;
[email protected]83d86252010-05-07 18:58:45741 directory_contents = directory_contents.Append(FPL("*"));
742
initial.commitd7cae122008-07-26 21:49:38743 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45744 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10745 EXPECT_FALSE(file_util::PathExists(file_name));
746 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40747
[email protected]3a51cfdd2010-05-07 00:05:36748 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45749 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40750 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36751 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40752}
753
[email protected]83d86252010-05-07 18:58:45754// TODO(erikkay): see if anyone's actually using this feature of the API
755TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
756 // Create a file and a directory
757 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
758 file_util::CreateDirectory(subdir_path);
759 ASSERT_TRUE(file_util::PathExists(subdir_path));
760
761 // Create the wildcard path
762 FilePath directory_contents = subdir_path;
763 directory_contents = directory_contents.Append(FPL("*"));
764
765 // Delete non-recursively and check nothing got deleted
766 EXPECT_TRUE(file_util::Delete(directory_contents, false));
767 EXPECT_TRUE(file_util::PathExists(subdir_path));
768
769 // Delete recursively and check nothing got deleted
770 EXPECT_TRUE(file_util::Delete(directory_contents, true));
771 EXPECT_TRUE(file_util::PathExists(subdir_path));
772}
773#endif
774
775// Tests non-recursive Delete() for a directory.
776TEST_F(FileUtilTest, DeleteDirNonRecursive) {
777 // Create a subdirectory and put a file and two directories inside.
778 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
779 file_util::CreateDirectory(test_subdir);
780 ASSERT_TRUE(file_util::PathExists(test_subdir));
781
782 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
783 CreateTextFile(file_name, bogus_content);
784 ASSERT_TRUE(file_util::PathExists(file_name));
785
786 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
787 file_util::CreateDirectory(subdir_path1);
788 ASSERT_TRUE(file_util::PathExists(subdir_path1));
789
790 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
791 file_util::CreateDirectory(subdir_path2);
792 ASSERT_TRUE(file_util::PathExists(subdir_path2));
793
794 // Delete non-recursively and check that the empty dir got deleted
795 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
796 EXPECT_FALSE(file_util::PathExists(subdir_path2));
797
798 // Delete non-recursively and check that nothing got deleted
799 EXPECT_FALSE(file_util::Delete(test_subdir, false));
800 EXPECT_TRUE(file_util::PathExists(test_subdir));
801 EXPECT_TRUE(file_util::PathExists(file_name));
802 EXPECT_TRUE(file_util::PathExists(subdir_path1));
803}
804
805// Tests recursive Delete() for a directory.
806TEST_F(FileUtilTest, DeleteDirRecursive) {
807 // Create a subdirectory and put a file and two directories inside.
808 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
809 file_util::CreateDirectory(test_subdir);
810 ASSERT_TRUE(file_util::PathExists(test_subdir));
811
812 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
813 CreateTextFile(file_name, bogus_content);
814 ASSERT_TRUE(file_util::PathExists(file_name));
815
816 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
817 file_util::CreateDirectory(subdir_path1);
818 ASSERT_TRUE(file_util::PathExists(subdir_path1));
819
820 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
821 file_util::CreateDirectory(subdir_path2);
822 ASSERT_TRUE(file_util::PathExists(subdir_path2));
823
824 // Delete recursively and check that the empty dir got deleted
825 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
826 EXPECT_FALSE(file_util::PathExists(subdir_path2));
827
828 // Delete recursively and check that everything got deleted
829 EXPECT_TRUE(file_util::Delete(test_subdir, true));
830 EXPECT_FALSE(file_util::PathExists(file_name));
831 EXPECT_FALSE(file_util::PathExists(subdir_path1));
832 EXPECT_FALSE(file_util::PathExists(test_subdir));
833}
834
[email protected]bc6a9012009-10-15 01:11:44835TEST_F(FileUtilTest, MoveFileNew) {
836 // Create a file
837 FilePath file_name_from =
838 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
839 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
840 ASSERT_TRUE(file_util::PathExists(file_name_from));
841
842 // The destination
843 FilePath file_name_to =
844 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
845 ASSERT_FALSE(file_util::PathExists(file_name_to));
846
847 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
848
849 // Check everything has been moved.
850 EXPECT_FALSE(file_util::PathExists(file_name_from));
851 EXPECT_TRUE(file_util::PathExists(file_name_to));
852}
853
854TEST_F(FileUtilTest, MoveFileExists) {
855 // Create a file
856 FilePath file_name_from =
857 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
858 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
859 ASSERT_TRUE(file_util::PathExists(file_name_from));
860
861 // The destination name
862 FilePath file_name_to =
863 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
864 CreateTextFile(file_name_to, L"Old file content");
865 ASSERT_TRUE(file_util::PathExists(file_name_to));
866
867 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
868
869 // Check everything has been moved.
870 EXPECT_FALSE(file_util::PathExists(file_name_from));
871 EXPECT_TRUE(file_util::PathExists(file_name_to));
872 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
873}
874
875TEST_F(FileUtilTest, MoveFileDirExists) {
876 // Create a file
877 FilePath file_name_from =
878 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
879 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
880 ASSERT_TRUE(file_util::PathExists(file_name_from));
881
882 // The destination directory
883 FilePath dir_name_to =
884 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
885 file_util::CreateDirectory(dir_name_to);
886 ASSERT_TRUE(file_util::PathExists(dir_name_to));
887
888 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
889}
890
891
[email protected]abbc5732009-10-13 17:57:27892TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38893 // Create a directory
[email protected]640517f2008-10-30 23:54:04894 FilePath dir_name_from =
895 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
896 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38897 ASSERT_TRUE(file_util::PathExists(dir_name_from));
898
899 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04900 FilePath file_name_from =
901 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38902 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
903 ASSERT_TRUE(file_util::PathExists(file_name_from));
904
905 // Move the directory
[email protected]640517f2008-10-30 23:54:04906 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
907 FilePath file_name_to =
908 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38909
910 ASSERT_FALSE(file_util::PathExists(dir_name_to));
911
912 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
913
914 // Check everything has been moved.
915 EXPECT_FALSE(file_util::PathExists(dir_name_from));
916 EXPECT_FALSE(file_util::PathExists(file_name_from));
917 EXPECT_TRUE(file_util::PathExists(dir_name_to));
918 EXPECT_TRUE(file_util::PathExists(file_name_to));
919}
920
[email protected]abbc5732009-10-13 17:57:27921TEST_F(FileUtilTest, MoveExist) {
922 // Create a directory
923 FilePath dir_name_from =
924 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
925 file_util::CreateDirectory(dir_name_from);
926 ASSERT_TRUE(file_util::PathExists(dir_name_from));
927
928 // Create a file under the directory
929 FilePath file_name_from =
930 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
931 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
932 ASSERT_TRUE(file_util::PathExists(file_name_from));
933
934 // Move the directory
935 FilePath dir_name_exists =
936 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
937
938 FilePath dir_name_to =
939 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
940 FilePath file_name_to =
941 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
942
943 // Create the destination directory.
944 file_util::CreateDirectory(dir_name_exists);
945 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
946
947 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
948
949 // Check everything has been moved.
950 EXPECT_FALSE(file_util::PathExists(dir_name_from));
951 EXPECT_FALSE(file_util::PathExists(file_name_from));
952 EXPECT_TRUE(file_util::PathExists(dir_name_to));
953 EXPECT_TRUE(file_util::PathExists(file_name_to));
954}
955
956TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38957 // Create a directory.
[email protected]640517f2008-10-30 23:54:04958 FilePath dir_name_from =
959 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
960 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38961 ASSERT_TRUE(file_util::PathExists(dir_name_from));
962
963 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04964 FilePath file_name_from =
965 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38966 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
967 ASSERT_TRUE(file_util::PathExists(file_name_from));
968
969 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04970 FilePath subdir_name_from =
971 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
972 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38973 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
974
975 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04976 FilePath file_name2_from =
977 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38978 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
979 ASSERT_TRUE(file_util::PathExists(file_name2_from));
980
981 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04982 FilePath dir_name_to =
983 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
984 FilePath file_name_to =
985 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
986 FilePath subdir_name_to =
987 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
988 FilePath file_name2_to =
989 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38990
991 ASSERT_FALSE(file_util::PathExists(dir_name_to));
992
993 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
994
995 // Check everything has been copied.
996 EXPECT_TRUE(file_util::PathExists(dir_name_from));
997 EXPECT_TRUE(file_util::PathExists(file_name_from));
998 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
999 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1000 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1001 EXPECT_TRUE(file_util::PathExists(file_name_to));
1002 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1003 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1004}
1005
[email protected]abbc5732009-10-13 17:57:271006TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1007 // Create a directory.
1008 FilePath dir_name_from =
1009 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1010 file_util::CreateDirectory(dir_name_from);
1011 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1012
1013 // Create a file under the directory.
1014 FilePath file_name_from =
1015 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1016 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1017 ASSERT_TRUE(file_util::PathExists(file_name_from));
1018
1019 // Create a subdirectory.
1020 FilePath subdir_name_from =
1021 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1022 file_util::CreateDirectory(subdir_name_from);
1023 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1024
1025 // Create a file under the subdirectory.
1026 FilePath file_name2_from =
1027 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1028 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1029 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1030
1031 // Copy the directory recursively.
1032 FilePath dir_name_exists =
1033 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1034
1035 FilePath dir_name_to =
1036 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1037 FilePath file_name_to =
1038 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1039 FilePath subdir_name_to =
1040 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1041 FilePath file_name2_to =
1042 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1043
1044 // Create the destination directory.
1045 file_util::CreateDirectory(dir_name_exists);
1046 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1047
1048 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1049
1050 // Check everything has been copied.
1051 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1052 EXPECT_TRUE(file_util::PathExists(file_name_from));
1053 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1054 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1055 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1056 EXPECT_TRUE(file_util::PathExists(file_name_to));
1057 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1058 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1059}
1060
1061TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381062 // Create a directory.
[email protected]640517f2008-10-30 23:54:041063 FilePath dir_name_from =
1064 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1065 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381066 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1067
1068 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041069 FilePath file_name_from =
1070 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381071 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1072 ASSERT_TRUE(file_util::PathExists(file_name_from));
1073
1074 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041075 FilePath subdir_name_from =
1076 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1077 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381078 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1079
1080 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041081 FilePath file_name2_from =
1082 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381083 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1084 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1085
1086 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041087 FilePath dir_name_to =
1088 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1089 FilePath file_name_to =
1090 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1091 FilePath subdir_name_to =
1092 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381093
1094 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1095
1096 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1097
1098 // Check everything has been copied.
1099 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1100 EXPECT_TRUE(file_util::PathExists(file_name_from));
1101 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1102 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1103 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1104 EXPECT_TRUE(file_util::PathExists(file_name_to));
1105 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1106}
1107
[email protected]abbc5732009-10-13 17:57:271108TEST_F(FileUtilTest, CopyDirectoryExists) {
1109 // Create a directory.
1110 FilePath dir_name_from =
1111 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1112 file_util::CreateDirectory(dir_name_from);
1113 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1114
1115 // Create a file under the directory.
1116 FilePath file_name_from =
1117 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1118 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1119 ASSERT_TRUE(file_util::PathExists(file_name_from));
1120
1121 // Create a subdirectory.
1122 FilePath subdir_name_from =
1123 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1124 file_util::CreateDirectory(subdir_name_from);
1125 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1126
1127 // Create a file under the subdirectory.
1128 FilePath file_name2_from =
1129 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1130 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1131 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1132
1133 // Copy the directory not recursively.
1134 FilePath dir_name_to =
1135 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1136 FilePath file_name_to =
1137 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1138 FilePath subdir_name_to =
1139 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1140
1141 // Create the destination directory.
1142 file_util::CreateDirectory(dir_name_to);
1143 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1144
1145 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1146
1147 // Check everything has been copied.
1148 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1149 EXPECT_TRUE(file_util::PathExists(file_name_from));
1150 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1151 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1152 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1153 EXPECT_TRUE(file_util::PathExists(file_name_to));
1154 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1155}
1156
[email protected]bc6a9012009-10-15 01:11:441157TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1158 // Create a file
1159 FilePath file_name_from =
1160 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1161 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1162 ASSERT_TRUE(file_util::PathExists(file_name_from));
1163
1164 // The destination name
1165 FilePath file_name_to =
1166 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1167 ASSERT_FALSE(file_util::PathExists(file_name_to));
1168
1169 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1170
1171 // Check the has been copied
1172 EXPECT_TRUE(file_util::PathExists(file_name_to));
1173}
1174
1175TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1176 // Create a file
1177 FilePath file_name_from =
1178 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1179 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1180 ASSERT_TRUE(file_util::PathExists(file_name_from));
1181
1182 // The destination name
1183 FilePath file_name_to =
1184 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1185 CreateTextFile(file_name_to, L"Old file content");
1186 ASSERT_TRUE(file_util::PathExists(file_name_to));
1187
1188 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1189
1190 // Check the has been copied
1191 EXPECT_TRUE(file_util::PathExists(file_name_to));
1192 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1193}
1194
1195TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1196 // Create a file
1197 FilePath file_name_from =
1198 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1199 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1200 ASSERT_TRUE(file_util::PathExists(file_name_from));
1201
1202 // The destination
1203 FilePath dir_name_to =
1204 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1205 file_util::CreateDirectory(dir_name_to);
1206 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1207 FilePath file_name_to =
1208 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1209
1210 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1211
1212 // Check the has been copied
1213 EXPECT_TRUE(file_util::PathExists(file_name_to));
1214}
1215
initial.commitd7cae122008-07-26 21:49:381216TEST_F(FileUtilTest, CopyFile) {
1217 // Create a directory
[email protected]640517f2008-10-30 23:54:041218 FilePath dir_name_from =
1219 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1220 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381221 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1222
1223 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041224 FilePath file_name_from =
1225 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381226 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1227 CreateTextFile(file_name_from, file_contents);
1228 ASSERT_TRUE(file_util::PathExists(file_name_from));
1229
1230 // Copy the file.
[email protected]640517f2008-10-30 23:54:041231 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381232 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111233
[email protected]37088fef2008-08-15 17:32:101234 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141235 FilePath dest_file2(dir_name_from);
1236 dest_file2 = dest_file2.AppendASCII("..");
1237 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1238 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1239
1240 FilePath dest_file2_test(dir_name_from);
1241 dest_file2_test = dest_file2_test.DirName();
1242 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381243
1244 // Check everything has been copied.
1245 EXPECT_TRUE(file_util::PathExists(file_name_from));
1246 EXPECT_TRUE(file_util::PathExists(dest_file));
1247 const std::wstring read_contents = ReadTextFile(dest_file);
1248 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141249 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1250 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381251}
1252
[email protected]37088fef2008-08-15 17:32:101253// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131254#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381255TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]640517f2008-10-30 23:54:041256 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381257
1258 SYSTEMTIME start_time;
1259 GetLocalTime(&start_time);
1260 Sleep(100);
1261 CreateTextFile(file_name, L"New file!");
1262 Sleep(100);
1263 SYSTEMTIME end_time;
1264 GetLocalTime(&end_time);
1265
1266 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041267 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381268
1269 FILETIME start_filetime;
1270 SystemTimeToFileTime(&start_time, &start_filetime);
1271 FILETIME end_filetime;
1272 SystemTimeToFileTime(&end_time, &end_filetime);
1273 FILETIME file_creation_filetime;
1274 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1275
1276 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1277 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1278 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1279
1280 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1281 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1282 "end time: " << FileTimeAsUint64(end_filetime);
1283
[email protected]640517f2008-10-30 23:54:041284 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381285}
[email protected]37088fef2008-08-15 17:32:101286#endif
initial.commitd7cae122008-07-26 21:49:381287
[email protected]ed2f2332008-08-20 15:59:491288// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041289// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491290typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381291
[email protected]ed2f2332008-08-20 15:59:491292TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041293 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381294 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041295 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1296 .Append(FILE_PATH_LITERAL("data"))
1297 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381298 ASSERT_TRUE(file_util::PathExists(data_dir));
1299
[email protected]640517f2008-10-30 23:54:041300 FilePath original_file =
1301 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1302 FilePath same_file =
1303 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1304 FilePath same_length_file =
1305 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1306 FilePath different_file =
1307 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1308 FilePath different_first_file =
1309 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1310 FilePath different_last_file =
1311 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1312 FilePath empty1_file =
1313 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1314 FilePath empty2_file =
1315 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1316 FilePath shortened_file =
1317 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1318 FilePath binary_file =
1319 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1320 FilePath binary_file_same =
1321 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1322 FilePath binary_file_diff =
1323 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381324
1325 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1326 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1327 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1328 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221329 EXPECT_FALSE(file_util::ContentsEqual(
1330 FilePath(FILE_PATH_LITERAL("bogusname")),
1331 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381332 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1333 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1334 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1335 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1336 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1337 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1338 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1339}
1340
[email protected]b81637c32009-06-26 21:17:241341TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1342 FilePath data_dir;
1343 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1344 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1345 .Append(FILE_PATH_LITERAL("data"))
1346 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1347 ASSERT_TRUE(file_util::PathExists(data_dir));
1348
1349 FilePath original_file =
1350 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1351 FilePath same_file =
1352 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1353 FilePath crlf_file =
1354 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1355 FilePath shortened_file =
1356 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1357 FilePath different_file =
1358 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1359 FilePath different_first_file =
1360 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1361 FilePath different_last_file =
1362 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1363 FilePath first1_file =
1364 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1365 FilePath first2_file =
1366 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1367 FilePath empty1_file =
1368 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1369 FilePath empty2_file =
1370 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1371 FilePath blank_line_file =
1372 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1373 FilePath blank_line_crlf_file =
1374 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1375
1376 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1377 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1378 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1379 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1380 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1381 different_first_file));
1382 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1383 different_last_file));
1384 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1385 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1386 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1387 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1388 blank_line_crlf_file));
1389}
1390
[email protected]37088fef2008-08-15 17:32:101391// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131392#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381393TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]640517f2008-10-30 23:54:041394 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381395 CreateTextFile(target_file, L"This is the target.");
1396
[email protected]640517f2008-10-30 23:54:041397 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381398
1399 HRESULT result;
1400 IShellLink *shell = NULL;
1401 IPersistFile *persist = NULL;
1402
1403 CoInitialize(NULL);
1404 // Temporarily create a shortcut for test
1405 result = CoCreateInstance(CLSID_ShellLink, NULL,
1406 CLSCTX_INPROC_SERVER, IID_IShellLink,
1407 reinterpret_cast<LPVOID*>(&shell));
1408 EXPECT_TRUE(SUCCEEDED(result));
1409 result = shell->QueryInterface(IID_IPersistFile,
1410 reinterpret_cast<LPVOID*>(&persist));
1411 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041412 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381413 EXPECT_TRUE(SUCCEEDED(result));
1414 result = shell->SetDescription(L"ResolveShortcutTest");
1415 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041416 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381417 EXPECT_TRUE(SUCCEEDED(result));
1418 if (persist)
1419 persist->Release();
1420 if (shell)
1421 shell->Release();
1422
1423 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441424 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381425 EXPECT_TRUE(is_solved);
1426 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441427 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381428 EXPECT_EQ(L"This is the target.", contents);
1429
[email protected]d324ab332008-08-18 16:00:381430 // Cleaning
[email protected]640517f2008-10-30 23:54:041431 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441432 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381433 CoUninitialize();
1434}
1435
1436TEST_F(FileUtilTest, CreateShortcutTest) {
1437 const wchar_t file_contents[] = L"This is another target.";
[email protected]640517f2008-10-30 23:54:041438 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381439 CreateTextFile(target_file, file_contents);
1440
[email protected]640517f2008-10-30 23:54:041441 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381442
1443 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041444 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1445 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501446 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441447 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381448 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441449 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381450 EXPECT_EQ(file_contents, read_contents);
1451
[email protected]640517f2008-10-30 23:54:041452 DeleteFile(target_file.value().c_str());
1453 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381454 CoUninitialize();
1455}
[email protected]2c59af7dca2009-03-11 18:37:481456
1457TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1458 // Create a directory
1459 FilePath dir_name_from =
1460 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1461 file_util::CreateDirectory(dir_name_from);
1462 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1463
1464 // Create a file under the directory
1465 FilePath file_name_from =
1466 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1467 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1468 ASSERT_TRUE(file_util::PathExists(file_name_from));
1469
1470 // Move the directory by using CopyAndDeleteDirectory
1471 FilePath dir_name_to = test_dir_.Append(
1472 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1473 FilePath file_name_to =
1474 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1475
1476 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1477
1478 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1479
1480 // Check everything has been moved.
1481 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1482 EXPECT_FALSE(file_util::PathExists(file_name_from));
1483 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1484 EXPECT_TRUE(file_util::PathExists(file_name_to));
1485}
[email protected]7d95aae2009-10-09 07:33:391486
1487TEST_F(FileUtilTest, GetTempDirTest) {
1488 static const TCHAR* kTmpKey = _T("TMP");
1489 static const TCHAR* kTmpValues[] = {
1490 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1491 };
1492 // Save the original $TMP.
1493 size_t original_tmp_size;
1494 TCHAR* original_tmp;
1495 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1496 // original_tmp may be NULL.
1497
1498 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1499 FilePath path;
1500 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1501 file_util::GetTempDir(&path);
1502 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1503 " result=" << path.value();
1504 }
1505
1506 // Restore the original $TMP.
1507 if (original_tmp) {
1508 ::_tputenv_s(kTmpKey, original_tmp);
1509 free(original_tmp);
1510 } else {
1511 ::_tputenv_s(kTmpKey, _T(""));
1512 }
1513}
1514#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381515
[email protected]33edeab2009-08-18 16:07:551516TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1517 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391518 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551519 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391520 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1521 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1522 }
1523 for (int i = 0; i < 3; i++)
1524 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1525 for (int i = 0; i < 3; i++)
1526 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1527}
1528
[email protected]33edeab2009-08-18 16:07:551529TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391530 FilePath names[3];
1531 FILE *fps[3];
1532 int i;
1533
1534 // Create; make sure they are open and exist.
1535 for (i = 0; i < 3; ++i) {
1536 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1537 ASSERT_TRUE(fps[i]);
1538 EXPECT_TRUE(file_util::PathExists(names[i]));
1539 }
1540
1541 // Make sure all names are unique.
1542 for (i = 0; i < 3; ++i) {
1543 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1544 }
1545
1546 // Close and delete.
1547 for (i = 0; i < 3; ++i) {
1548 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1549 EXPECT_TRUE(file_util::Delete(names[i], false));
1550 }
initial.commitd7cae122008-07-26 21:49:381551}
1552
1553TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141554 FilePath temp_dir;
1555 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1556 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111557 EXPECT_TRUE(file_util::PathExists(temp_dir));
1558 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381559}
1560
[email protected]b0b3abd92010-04-30 17:00:091561TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1562 FilePath new_dir;
1563 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1564 test_dir_,
1565 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1566 &new_dir));
1567 EXPECT_TRUE(file_util::PathExists(new_dir));
1568 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1569 EXPECT_TRUE(file_util::Delete(new_dir, false));
1570}
1571
[email protected]9e51af92009-02-04 00:58:391572TEST_F(FileUtilTest, GetShmemTempDirTest) {
1573 FilePath dir;
1574 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1575 EXPECT_TRUE(file_util::DirectoryExists(dir));
1576}
1577
initial.commitd7cae122008-07-26 21:49:381578TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041579 FilePath test_root =
1580 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131581#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041582 FilePath test_path =
1583 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101584#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041585 FilePath test_path =
1586 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101587#endif
[email protected]806b9c62008-09-11 16:09:111588
1589 EXPECT_FALSE(file_util::PathExists(test_path));
1590 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1591 EXPECT_TRUE(file_util::PathExists(test_path));
1592 // CreateDirectory returns true if the DirectoryExists returns true.
1593 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1594
1595 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041596 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111597 EXPECT_FALSE(file_util::PathExists(test_path));
1598 CreateTextFile(test_path, L"test file");
1599 EXPECT_TRUE(file_util::PathExists(test_path));
1600 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1601
1602 EXPECT_TRUE(file_util::Delete(test_root, true));
1603 EXPECT_FALSE(file_util::PathExists(test_root));
1604 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411605
1606 // Verify assumptions made by the Windows implementation:
1607 // 1. The current directory always exists.
1608 // 2. The root directory always exists.
1609 ASSERT_TRUE(file_util::DirectoryExists(
1610 FilePath(FilePath::kCurrentDirectory)));
1611 FilePath top_level = test_root;
1612 while (top_level != top_level.DirName()) {
1613 top_level = top_level.DirName();
1614 }
1615 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1616
1617 // Given these assumptions hold, it should be safe to
1618 // test that "creating" these directories succeeds.
1619 EXPECT_TRUE(file_util::CreateDirectory(
1620 FilePath(FilePath::kCurrentDirectory)));
1621 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401622
1623#if defined(OS_WIN)
1624 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1625 FilePath invalid_path =
1626 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1627 if (!file_util::PathExists(invalid_drive)) {
1628 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1629 }
1630#endif
[email protected]806b9c62008-09-11 16:09:111631}
1632
1633TEST_F(FileUtilTest, DetectDirectoryTest) {
1634 // Check a directory
[email protected]640517f2008-10-30 23:54:041635 FilePath test_root =
1636 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111637 EXPECT_FALSE(file_util::PathExists(test_root));
1638 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1639 EXPECT_TRUE(file_util::PathExists(test_root));
1640 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1641
1642 // Check a file
[email protected]640517f2008-10-30 23:54:041643 FilePath test_path =
1644 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111645 EXPECT_FALSE(file_util::PathExists(test_path));
1646 CreateTextFile(test_path, L"test file");
1647 EXPECT_TRUE(file_util::PathExists(test_path));
1648 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1649 EXPECT_TRUE(file_util::Delete(test_path, false));
1650
1651 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381652}
1653
initial.commitd7cae122008-07-26 21:49:381654TEST_F(FileUtilTest, FileEnumeratorTest) {
1655 // Test an empty directory.
[email protected]8199b3a2009-06-09 05:57:381656 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121657 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1658 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381659
[email protected]8199b3a2009-06-09 05:57:381660 // Test an empty directory, non-recursively, including "..".
1661 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1662 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1663 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1664 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1665 f0_dotdot.Next().value());
1666 EXPECT_EQ(FILE_PATH_LITERAL(""),
1667 f0_dotdot.Next().value());
1668
[email protected]37088fef2008-08-15 17:32:101669 // create the directories
[email protected]640517f2008-10-30 23:54:041670 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101671 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]640517f2008-10-30 23:54:041672 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101673 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041674 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101675 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041676
[email protected]37088fef2008-08-15 17:32:101677 // create the files
[email protected]640517f2008-10-30 23:54:041678 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101679 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041680 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101681 CreateTextFile(dir2innerfile, L"");
[email protected]640517f2008-10-30 23:54:041682 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101683 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041684 FilePath file2_rel =
1685 dir2.Append(FilePath::kParentDirectory)
1686 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101687 CreateTextFile(file2_rel, L"");
[email protected]640517f2008-10-30 23:54:041688 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381689
1690 // Only enumerate files.
[email protected]0b733222008-12-11 14:55:121691 file_util::FileEnumerator f1(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381692 file_util::FileEnumerator::FILES);
1693 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101694 EXPECT_TRUE(c1.HasFile(file1));
1695 EXPECT_TRUE(c1.HasFile(file2_abs));
1696 EXPECT_TRUE(c1.HasFile(dir2file));
1697 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1698 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381699
1700 // Only enumerate directories.
[email protected]0b733222008-12-11 14:55:121701 file_util::FileEnumerator f2(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381702 file_util::FileEnumerator::DIRECTORIES);
1703 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101704 EXPECT_TRUE(c2.HasFile(dir1));
1705 EXPECT_TRUE(c2.HasFile(dir2));
1706 EXPECT_TRUE(c2.HasFile(dir2inner));
1707 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381708
[email protected]3bc9ffaf2008-10-16 02:42:451709 // Only enumerate directories non-recursively.
1710 file_util::FileEnumerator f2_non_recursive(
[email protected]0b733222008-12-11 14:55:121711 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451712 FindResultCollector c2_non_recursive(f2_non_recursive);
1713 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1714 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1715 EXPECT_EQ(c2_non_recursive.size(), 2);
1716
[email protected]8199b3a2009-06-09 05:57:381717 // Only enumerate directories, non-recursively, including "..".
1718 file_util::FileEnumerator f2_dotdot(
1719 test_dir_, false,
1720 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1721 file_util::FileEnumerator::DIRECTORIES |
1722 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1723 FindResultCollector c2_dotdot(f2_dotdot);
1724 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1725 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1726 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1727 EXPECT_EQ(c2_dotdot.size(), 3);
1728
initial.commitd7cae122008-07-26 21:49:381729 // Enumerate files and directories.
[email protected]8199b3a2009-06-09 05:57:381730 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381731 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101732 EXPECT_TRUE(c3.HasFile(dir1));
1733 EXPECT_TRUE(c3.HasFile(dir2));
1734 EXPECT_TRUE(c3.HasFile(file1));
1735 EXPECT_TRUE(c3.HasFile(file2_abs));
1736 EXPECT_TRUE(c3.HasFile(dir2file));
1737 EXPECT_TRUE(c3.HasFile(dir2inner));
1738 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1739 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381740
1741 // Non-recursive operation.
[email protected]8199b3a2009-06-09 05:57:381742 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381743 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101744 EXPECT_TRUE(c4.HasFile(dir2));
1745 EXPECT_TRUE(c4.HasFile(dir2));
1746 EXPECT_TRUE(c4.HasFile(file1));
1747 EXPECT_TRUE(c4.HasFile(file2_abs));
1748 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381749
1750 // Enumerate with a pattern.
[email protected]8199b3a2009-06-09 05:57:381751 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121752 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381753 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101754 EXPECT_TRUE(c5.HasFile(dir1));
1755 EXPECT_TRUE(c5.HasFile(dir2));
1756 EXPECT_TRUE(c5.HasFile(dir2file));
1757 EXPECT_TRUE(c5.HasFile(dir2inner));
1758 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1759 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381760
1761 // Make sure the destructor closes the find handle while in the middle of a
1762 // query to allow TearDown to delete the directory.
[email protected]8199b3a2009-06-09 05:57:381763 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121764 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1765 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381766}
license.botbf09a502008-08-24 00:55:551767
[email protected]ee5c29da2009-01-09 22:14:271768TEST_F(FileUtilTest, Contains) {
[email protected]11e53f62009-03-10 18:20:441769 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271770
1771 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571772 if (file_util::PathExists(data_dir)) {
1773 ASSERT_TRUE(file_util::Delete(data_dir, true));
1774 }
[email protected]ee5c29da2009-01-09 22:14:271775 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1776
1777 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1778 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1779 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1780 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1781
1782 // Annoyingly, the directories must actually exist in order for realpath(),
1783 // which Contains() relies on in posix, to work.
1784 ASSERT_TRUE(file_util::CreateDirectory(foo));
1785 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581786 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1787 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1788 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271789
1790 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1791 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1792 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1793 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1794
[email protected]e43eddf12009-12-29 00:32:521795 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271796 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1797#if defined(OS_WIN)
1798 EXPECT_TRUE(file_util::ContainsPath(foo,
1799 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391800 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271801 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521802#elif defined(OS_MACOSX)
1803 // We can't really do this test on OS X since the case-sensitivity of the
1804 // filesystem is configurable.
1805#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271806 EXPECT_FALSE(file_util::ContainsPath(foo,
1807 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271808#endif
1809}
1810
[email protected]ec3d1452010-02-18 10:02:261811TEST_F(FileUtilTest, LastModified) {
1812 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1813
1814 // Create a fresh, empty copy of this directory.
1815 if (file_util::PathExists(data_dir)) {
1816 ASSERT_TRUE(file_util::Delete(data_dir, true));
1817 }
1818 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1819
1820 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1821 std::string data("hello");
1822 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1823
1824 base::Time modification_time;
1825 // Note that this timestamp is divisible by two (seconds) - FAT stores
1826 // modification times with 2s resolution.
1827 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1828 &modification_time));
1829 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1830 file_util::FileInfo file_info;
1831 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1832 ASSERT_TRUE(file_info.last_modified == modification_time);
1833}
1834
[email protected]b33f1d92010-05-26 01:40:121835TEST_F(FileUtilTest, IsDirectoryEmpty) {
1836 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1837
1838 ASSERT_FALSE(file_util::PathExists(empty_dir));
1839
1840 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1841
1842 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1843
1844 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1845 std::string bar("baz");
1846 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1847
1848 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1849}
1850
[email protected]11b901ee2008-09-10 00:16:281851} // namespace