blob: 39e5398cb2f0f9cdd0def3dcc918730e46bad9a0 [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);
[email protected]a04876b92010-06-11 22:53:43466
467 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("The file*"));
468 EXPECT_EQ(size_f1, computed_size);
469
470 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("bla*"));
471 EXPECT_EQ(0, computed_size);
[email protected]c2c132c62010-03-24 21:56:26472}
473
[email protected]6f5f4322010-06-09 22:56:48474TEST_F(FileUtilTest, NormalizeFilePathBasic) {
475 // Create a directory under the test dir. Because we create it,
476 // we know it is not a link.
477 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
478 FilePath dir_path = test_dir_.Append(FPL("dir"));
479 FilePath file_b_path = dir_path.Append(FPL("file_b"));
480 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57481
[email protected]6f5f4322010-06-09 22:56:48482 FilePath normalized_file_a_path, normalized_file_b_path;
483 ASSERT_FALSE(file_util::PathExists(file_a_path));
484 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
485 &normalized_file_a_path))
486 << "NormalizeFilePath() should fail on nonexistant paths.";
487
488 CreateTextFile(file_a_path, bogus_content);
489 ASSERT_TRUE(file_util::PathExists(file_a_path));
490 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
491 &normalized_file_a_path));
492
493 CreateTextFile(file_b_path, bogus_content);
494 ASSERT_TRUE(file_util::PathExists(file_b_path));
495 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
496 &normalized_file_b_path));
497
498 // Beacuse this test created |dir_path|, we know it is not a link
499 // or junction. So, the real path of the directory holding file a
500 // must be the parent of the path holding file b.
501 ASSERT_TRUE(normalized_file_a_path.DirName()
502 .IsParent(normalized_file_b_path.DirName()));
503}
504
505#if defined(OS_WIN)
506
507TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
508 // Build the following directory structure:
509 //
510 // test_dir_
511 // |-> base_a
512 // | |-> sub_a
513 // | |-> file.txt
514 // | |-> long_name___... (Very long name.)
515 // | |-> sub_long
516 // | |-> deep.txt
517 // |-> base_b
518 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
519 // |-> to_base_b (reparse point to test_dir_\base_b)
520 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
521
522 FilePath base_a = test_dir_.Append(FPL("base_a"));
523 ASSERT_TRUE(file_util::CreateDirectory(base_a));
524
525 FilePath sub_a = base_a.Append(FPL("sub_a"));
526 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
527
528 FilePath file_txt = sub_a.Append(FPL("file.txt"));
529 CreateTextFile(file_txt, bogus_content);
530
531 // Want a directory whose name is long enough to make the path to the file
532 // inside just under MAX_PATH chars. This will be used to test that when
533 // a junction expands to a path over MAX_PATH chars in length,
534 // NormalizeFilePath() fails without crashing.
535 FilePath sub_long_rel(FPL("sub_long"));
536 FilePath deep_txt(FPL("deep.txt"));
537
538 int target_length = MAX_PATH;
539 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
540 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52541 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48542 // the resulting path is still long enough to hit the failing case in
543 // NormalizePath().
544 const int kCreateDirLimit = 4;
545 target_length -= kCreateDirLimit;
546 FilePath::StringType long_name_str = FPL("long_name_");
547 long_name_str.resize(target_length, '_');
548
549 FilePath long_name = sub_a.Append(FilePath(long_name_str));
550 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
551 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
552
553 FilePath sub_long = deep_file.DirName();
554 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
555 CreateTextFile(deep_file, bogus_content);
556
557 FilePath base_b = test_dir_.Append(FPL("base_b"));
558 ASSERT_TRUE(file_util::CreateDirectory(base_b));
559
560 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
561 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
562 ScopedHandle reparse_to_sub_a(
563 ::CreateFile(to_sub_a.value().c_str(),
564 FILE_ALL_ACCESS,
565 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
566 NULL,
567 OPEN_EXISTING,
568 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
569 NULL));
570 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_a.Get());
571 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
572
573 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
574 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
575 ScopedHandle reparse_to_base_b(
576 ::CreateFile(to_base_b.value().c_str(),
577 FILE_ALL_ACCESS,
578 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
579 NULL,
580 OPEN_EXISTING,
581 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
582 NULL));
583 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_base_b.Get());
584 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
585
586 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
587 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
588 ScopedHandle reparse_to_sub_long(
589 ::CreateFile(to_sub_long.value().c_str(),
590 FILE_ALL_ACCESS,
591 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
592 NULL,
593 OPEN_EXISTING,
594 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
595 NULL));
596 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_long.Get());
597 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
598
599 // Normalize a junction free path: base_a\sub_a\file.txt .
600 FilePath normalized_path;
601 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
602 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
603
604 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
605 // the junction to_sub_a.
606 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
607 &normalized_path));
608 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
609
610 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
611 // normalized to exclude junctions to_base_b and to_sub_a .
612 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
613 .Append(FPL("to_base_b"))
614 .Append(FPL("to_sub_a"))
615 .Append(FPL("file.txt")),
616 &normalized_path));
617 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
618
619 // A long enough path will cause NormalizeFilePath() to fail. Make a long
620 // path using to_base_b many times, and check that paths long enough to fail
621 // do not cause a crash.
622 FilePath long_path = base_b;
623 const int kLengthLimit = MAX_PATH + 200;
624 while (long_path.value().length() <= kLengthLimit) {
625 long_path = long_path.Append(FPL("to_base_b"));
626 }
627 long_path = long_path.Append(FPL("to_sub_a"))
628 .Append(FPL("file.txt"));
629
630 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
631
632 // Normalizing the junction to deep.txt should fail, because the expanded
633 // path to deep.txt is longer than MAX_PATH.
634 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
635 &normalized_path));
636
637 // Delete the reparse points, and see that NormalizeFilePath() fails
638 // to traverse them.
639 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
640 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
641 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
642
643 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
644 &normalized_path));
645}
646
647#endif // defined(OS_WIN)
648
649// The following test of NormalizeFilePath() require that we create a symlink.
650// This can not be done on windows before vista. On vista, creating a symlink
651// requires privilege "SeCreateSymbolicLinkPrivilege".
652// TODO(skerner): Investigate the possibility of giving base_unittests the
653// privileges required to create a symlink.
654#if defined(OS_POSIX)
655
656bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
657 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
658}
659
660TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
661 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57662
663 // Link one file to another.
[email protected]6f5f4322010-06-09 22:56:48664 FilePath link_from = test_dir_.Append(FPL("from_file"));
665 FilePath link_to = test_dir_.Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57666 CreateTextFile(link_to, bogus_content);
667
[email protected]6f5f4322010-06-09 22:56:48668 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57669 << "Failed to create file symlink.";
670
[email protected]6f5f4322010-06-09 22:56:48671 // Check that NormalizeFilePath sees the link.
672 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57673 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48674 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
675 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57676
677 // Link to a directory.
[email protected]6f5f4322010-06-09 22:56:48678 link_from = test_dir_.Append(FPL("from_dir"));
679 link_to = test_dir_.Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57680 file_util::CreateDirectory(link_to);
681
[email protected]6f5f4322010-06-09 22:56:48682 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57683 << "Failed to create directory symlink.";
684
[email protected]6f5f4322010-06-09 22:56:48685 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
686 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57687
[email protected]6f5f4322010-06-09 22:56:48688 // Test that a loop in the links causes NormalizeFilePath() to return false.
689 link_from = test_dir_.Append(FPL("link_a"));
690 link_to = test_dir_.Append(FPL("link_b"));
691 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57692 << "Failed to create loop symlink a.";
[email protected]6f5f4322010-06-09 22:56:48693 ASSERT_TRUE(MakeSymlink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57694 << "Failed to create loop symlink b.";
695
696 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48697 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57698}
699#endif // defined(OS_POSIX)
700
[email protected]83d86252010-05-07 18:58:45701TEST_F(FileUtilTest, DeleteNonExistent) {
702 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
703 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36704
[email protected]83d86252010-05-07 18:58:45705 EXPECT_TRUE(file_util::Delete(non_existent, false));
706 ASSERT_FALSE(file_util::PathExists(non_existent));
707 EXPECT_TRUE(file_util::Delete(non_existent, true));
708 ASSERT_FALSE(file_util::PathExists(non_existent));
709}
710
711TEST_F(FileUtilTest, DeleteFile) {
712 // Create a file
713 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
714 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38715 ASSERT_TRUE(file_util::PathExists(file_name));
716
[email protected]83d86252010-05-07 18:58:45717 // Make sure it's deleted
718 EXPECT_TRUE(file_util::Delete(file_name, false));
719 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36720
[email protected]83d86252010-05-07 18:58:45721 // Test recursive case, create a new file
722 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
723 CreateTextFile(file_name, bogus_content);
724 ASSERT_TRUE(file_util::PathExists(file_name));
725
726 // Make sure it's deleted
727 EXPECT_TRUE(file_util::Delete(file_name, true));
728 EXPECT_FALSE(file_util::PathExists(file_name));
729}
730
731#if defined(OS_WIN)
732// Tests that the Delete function works for wild cards, especially
733// with the recursion flag. Also coincidentally tests PathExists.
734// TODO(erikkay): see if anyone's actually using this feature of the API
735TEST_F(FileUtilTest, DeleteWildCard) {
736 // Create a file and a directory
737 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
738 CreateTextFile(file_name, bogus_content);
739 ASSERT_TRUE(file_util::PathExists(file_name));
740
741 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
742 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38743 ASSERT_TRUE(file_util::PathExists(subdir_path));
744
[email protected]83d86252010-05-07 18:58:45745 // Create the wildcard path
[email protected]640517f2008-10-30 23:54:04746 FilePath directory_contents = test_dir_;
[email protected]83d86252010-05-07 18:58:45747 directory_contents = directory_contents.Append(FPL("*"));
748
initial.commitd7cae122008-07-26 21:49:38749 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45750 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10751 EXPECT_FALSE(file_util::PathExists(file_name));
752 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40753
[email protected]3a51cfdd2010-05-07 00:05:36754 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45755 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40756 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36757 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40758}
759
[email protected]83d86252010-05-07 18:58:45760// TODO(erikkay): see if anyone's actually using this feature of the API
761TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
762 // Create a file and a directory
763 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
764 file_util::CreateDirectory(subdir_path);
765 ASSERT_TRUE(file_util::PathExists(subdir_path));
766
767 // Create the wildcard path
768 FilePath directory_contents = subdir_path;
769 directory_contents = directory_contents.Append(FPL("*"));
770
771 // Delete non-recursively and check nothing got deleted
772 EXPECT_TRUE(file_util::Delete(directory_contents, false));
773 EXPECT_TRUE(file_util::PathExists(subdir_path));
774
775 // Delete recursively and check nothing got deleted
776 EXPECT_TRUE(file_util::Delete(directory_contents, true));
777 EXPECT_TRUE(file_util::PathExists(subdir_path));
778}
779#endif
780
781// Tests non-recursive Delete() for a directory.
782TEST_F(FileUtilTest, DeleteDirNonRecursive) {
783 // Create a subdirectory and put a file and two directories inside.
784 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
785 file_util::CreateDirectory(test_subdir);
786 ASSERT_TRUE(file_util::PathExists(test_subdir));
787
788 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
789 CreateTextFile(file_name, bogus_content);
790 ASSERT_TRUE(file_util::PathExists(file_name));
791
792 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
793 file_util::CreateDirectory(subdir_path1);
794 ASSERT_TRUE(file_util::PathExists(subdir_path1));
795
796 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
797 file_util::CreateDirectory(subdir_path2);
798 ASSERT_TRUE(file_util::PathExists(subdir_path2));
799
800 // Delete non-recursively and check that the empty dir got deleted
801 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
802 EXPECT_FALSE(file_util::PathExists(subdir_path2));
803
804 // Delete non-recursively and check that nothing got deleted
805 EXPECT_FALSE(file_util::Delete(test_subdir, false));
806 EXPECT_TRUE(file_util::PathExists(test_subdir));
807 EXPECT_TRUE(file_util::PathExists(file_name));
808 EXPECT_TRUE(file_util::PathExists(subdir_path1));
809}
810
811// Tests recursive Delete() for a directory.
812TEST_F(FileUtilTest, DeleteDirRecursive) {
813 // Create a subdirectory and put a file and two directories inside.
814 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
815 file_util::CreateDirectory(test_subdir);
816 ASSERT_TRUE(file_util::PathExists(test_subdir));
817
818 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
819 CreateTextFile(file_name, bogus_content);
820 ASSERT_TRUE(file_util::PathExists(file_name));
821
822 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
823 file_util::CreateDirectory(subdir_path1);
824 ASSERT_TRUE(file_util::PathExists(subdir_path1));
825
826 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
827 file_util::CreateDirectory(subdir_path2);
828 ASSERT_TRUE(file_util::PathExists(subdir_path2));
829
830 // Delete recursively and check that the empty dir got deleted
831 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
832 EXPECT_FALSE(file_util::PathExists(subdir_path2));
833
834 // Delete recursively and check that everything got deleted
835 EXPECT_TRUE(file_util::Delete(test_subdir, true));
836 EXPECT_FALSE(file_util::PathExists(file_name));
837 EXPECT_FALSE(file_util::PathExists(subdir_path1));
838 EXPECT_FALSE(file_util::PathExists(test_subdir));
839}
840
[email protected]bc6a9012009-10-15 01:11:44841TEST_F(FileUtilTest, MoveFileNew) {
842 // Create a file
843 FilePath file_name_from =
844 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
845 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
846 ASSERT_TRUE(file_util::PathExists(file_name_from));
847
848 // The destination
849 FilePath file_name_to =
850 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
851 ASSERT_FALSE(file_util::PathExists(file_name_to));
852
853 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
854
855 // Check everything has been moved.
856 EXPECT_FALSE(file_util::PathExists(file_name_from));
857 EXPECT_TRUE(file_util::PathExists(file_name_to));
858}
859
860TEST_F(FileUtilTest, MoveFileExists) {
861 // Create a file
862 FilePath file_name_from =
863 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
864 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
865 ASSERT_TRUE(file_util::PathExists(file_name_from));
866
867 // The destination name
868 FilePath file_name_to =
869 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
870 CreateTextFile(file_name_to, L"Old file content");
871 ASSERT_TRUE(file_util::PathExists(file_name_to));
872
873 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
874
875 // Check everything has been moved.
876 EXPECT_FALSE(file_util::PathExists(file_name_from));
877 EXPECT_TRUE(file_util::PathExists(file_name_to));
878 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
879}
880
881TEST_F(FileUtilTest, MoveFileDirExists) {
882 // Create a file
883 FilePath file_name_from =
884 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
885 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
886 ASSERT_TRUE(file_util::PathExists(file_name_from));
887
888 // The destination directory
889 FilePath dir_name_to =
890 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
891 file_util::CreateDirectory(dir_name_to);
892 ASSERT_TRUE(file_util::PathExists(dir_name_to));
893
894 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
895}
896
897
[email protected]abbc5732009-10-13 17:57:27898TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38899 // Create a directory
[email protected]640517f2008-10-30 23:54:04900 FilePath dir_name_from =
901 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
902 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38903 ASSERT_TRUE(file_util::PathExists(dir_name_from));
904
905 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04906 FilePath file_name_from =
907 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38908 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
909 ASSERT_TRUE(file_util::PathExists(file_name_from));
910
911 // Move the directory
[email protected]640517f2008-10-30 23:54:04912 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
913 FilePath file_name_to =
914 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38915
916 ASSERT_FALSE(file_util::PathExists(dir_name_to));
917
918 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
919
920 // Check everything has been moved.
921 EXPECT_FALSE(file_util::PathExists(dir_name_from));
922 EXPECT_FALSE(file_util::PathExists(file_name_from));
923 EXPECT_TRUE(file_util::PathExists(dir_name_to));
924 EXPECT_TRUE(file_util::PathExists(file_name_to));
925}
926
[email protected]abbc5732009-10-13 17:57:27927TEST_F(FileUtilTest, MoveExist) {
928 // Create a directory
929 FilePath dir_name_from =
930 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
931 file_util::CreateDirectory(dir_name_from);
932 ASSERT_TRUE(file_util::PathExists(dir_name_from));
933
934 // Create a file under the directory
935 FilePath file_name_from =
936 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
937 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
938 ASSERT_TRUE(file_util::PathExists(file_name_from));
939
940 // Move the directory
941 FilePath dir_name_exists =
942 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
943
944 FilePath dir_name_to =
945 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
946 FilePath file_name_to =
947 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
948
949 // Create the destination directory.
950 file_util::CreateDirectory(dir_name_exists);
951 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
952
953 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
954
955 // Check everything has been moved.
956 EXPECT_FALSE(file_util::PathExists(dir_name_from));
957 EXPECT_FALSE(file_util::PathExists(file_name_from));
958 EXPECT_TRUE(file_util::PathExists(dir_name_to));
959 EXPECT_TRUE(file_util::PathExists(file_name_to));
960}
961
962TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38963 // Create a directory.
[email protected]640517f2008-10-30 23:54:04964 FilePath dir_name_from =
965 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
966 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38967 ASSERT_TRUE(file_util::PathExists(dir_name_from));
968
969 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04970 FilePath file_name_from =
971 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38972 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
973 ASSERT_TRUE(file_util::PathExists(file_name_from));
974
975 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04976 FilePath subdir_name_from =
977 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
978 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38979 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
980
981 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04982 FilePath file_name2_from =
983 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38984 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
985 ASSERT_TRUE(file_util::PathExists(file_name2_from));
986
987 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04988 FilePath dir_name_to =
989 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
990 FilePath file_name_to =
991 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
992 FilePath subdir_name_to =
993 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
994 FilePath file_name2_to =
995 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38996
997 ASSERT_FALSE(file_util::PathExists(dir_name_to));
998
999 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
1000
1001 // Check everything has been copied.
1002 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1003 EXPECT_TRUE(file_util::PathExists(file_name_from));
1004 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1005 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1006 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1007 EXPECT_TRUE(file_util::PathExists(file_name_to));
1008 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1009 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1010}
1011
[email protected]abbc5732009-10-13 17:57:271012TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1013 // Create a directory.
1014 FilePath dir_name_from =
1015 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1016 file_util::CreateDirectory(dir_name_from);
1017 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1018
1019 // Create a file under the directory.
1020 FilePath file_name_from =
1021 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1022 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1023 ASSERT_TRUE(file_util::PathExists(file_name_from));
1024
1025 // Create a subdirectory.
1026 FilePath subdir_name_from =
1027 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1028 file_util::CreateDirectory(subdir_name_from);
1029 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1030
1031 // Create a file under the subdirectory.
1032 FilePath file_name2_from =
1033 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1034 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1035 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1036
1037 // Copy the directory recursively.
1038 FilePath dir_name_exists =
1039 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1040
1041 FilePath dir_name_to =
1042 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1043 FilePath file_name_to =
1044 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1045 FilePath subdir_name_to =
1046 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1047 FilePath file_name2_to =
1048 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1049
1050 // Create the destination directory.
1051 file_util::CreateDirectory(dir_name_exists);
1052 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1053
1054 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1055
1056 // Check everything has been copied.
1057 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1058 EXPECT_TRUE(file_util::PathExists(file_name_from));
1059 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1060 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1061 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1062 EXPECT_TRUE(file_util::PathExists(file_name_to));
1063 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1064 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1065}
1066
1067TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381068 // Create a directory.
[email protected]640517f2008-10-30 23:54:041069 FilePath dir_name_from =
1070 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1071 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381072 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1073
1074 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041075 FilePath file_name_from =
1076 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381077 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1078 ASSERT_TRUE(file_util::PathExists(file_name_from));
1079
1080 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041081 FilePath subdir_name_from =
1082 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1083 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381084 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1085
1086 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041087 FilePath file_name2_from =
1088 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381089 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1090 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1091
1092 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041093 FilePath dir_name_to =
1094 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1095 FilePath file_name_to =
1096 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1097 FilePath subdir_name_to =
1098 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381099
1100 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1101
1102 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1103
1104 // Check everything has been copied.
1105 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1106 EXPECT_TRUE(file_util::PathExists(file_name_from));
1107 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1108 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1109 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1110 EXPECT_TRUE(file_util::PathExists(file_name_to));
1111 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1112}
1113
[email protected]abbc5732009-10-13 17:57:271114TEST_F(FileUtilTest, CopyDirectoryExists) {
1115 // Create a directory.
1116 FilePath dir_name_from =
1117 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1118 file_util::CreateDirectory(dir_name_from);
1119 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1120
1121 // Create a file under the directory.
1122 FilePath file_name_from =
1123 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1124 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1125 ASSERT_TRUE(file_util::PathExists(file_name_from));
1126
1127 // Create a subdirectory.
1128 FilePath subdir_name_from =
1129 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1130 file_util::CreateDirectory(subdir_name_from);
1131 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1132
1133 // Create a file under the subdirectory.
1134 FilePath file_name2_from =
1135 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1136 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1137 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1138
1139 // Copy the directory not recursively.
1140 FilePath dir_name_to =
1141 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1142 FilePath file_name_to =
1143 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1144 FilePath subdir_name_to =
1145 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1146
1147 // Create the destination directory.
1148 file_util::CreateDirectory(dir_name_to);
1149 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1150
1151 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1152
1153 // Check everything has been copied.
1154 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1155 EXPECT_TRUE(file_util::PathExists(file_name_from));
1156 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1157 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1158 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1159 EXPECT_TRUE(file_util::PathExists(file_name_to));
1160 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1161}
1162
[email protected]bc6a9012009-10-15 01:11:441163TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1164 // Create a file
1165 FilePath file_name_from =
1166 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1167 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1168 ASSERT_TRUE(file_util::PathExists(file_name_from));
1169
1170 // The destination name
1171 FilePath file_name_to =
1172 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1173 ASSERT_FALSE(file_util::PathExists(file_name_to));
1174
1175 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1176
1177 // Check the has been copied
1178 EXPECT_TRUE(file_util::PathExists(file_name_to));
1179}
1180
1181TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1182 // Create a file
1183 FilePath file_name_from =
1184 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1185 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1186 ASSERT_TRUE(file_util::PathExists(file_name_from));
1187
1188 // The destination name
1189 FilePath file_name_to =
1190 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1191 CreateTextFile(file_name_to, L"Old file content");
1192 ASSERT_TRUE(file_util::PathExists(file_name_to));
1193
1194 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1195
1196 // Check the has been copied
1197 EXPECT_TRUE(file_util::PathExists(file_name_to));
1198 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1199}
1200
1201TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1202 // Create a file
1203 FilePath file_name_from =
1204 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1205 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1206 ASSERT_TRUE(file_util::PathExists(file_name_from));
1207
1208 // The destination
1209 FilePath dir_name_to =
1210 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1211 file_util::CreateDirectory(dir_name_to);
1212 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1213 FilePath file_name_to =
1214 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1215
1216 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1217
1218 // Check the has been copied
1219 EXPECT_TRUE(file_util::PathExists(file_name_to));
1220}
1221
initial.commitd7cae122008-07-26 21:49:381222TEST_F(FileUtilTest, CopyFile) {
1223 // Create a directory
[email protected]640517f2008-10-30 23:54:041224 FilePath dir_name_from =
1225 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1226 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381227 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1228
1229 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041230 FilePath file_name_from =
1231 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381232 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1233 CreateTextFile(file_name_from, file_contents);
1234 ASSERT_TRUE(file_util::PathExists(file_name_from));
1235
1236 // Copy the file.
[email protected]640517f2008-10-30 23:54:041237 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381238 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111239
[email protected]37088fef2008-08-15 17:32:101240 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141241 FilePath dest_file2(dir_name_from);
1242 dest_file2 = dest_file2.AppendASCII("..");
1243 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1244 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1245
1246 FilePath dest_file2_test(dir_name_from);
1247 dest_file2_test = dest_file2_test.DirName();
1248 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381249
1250 // Check everything has been copied.
1251 EXPECT_TRUE(file_util::PathExists(file_name_from));
1252 EXPECT_TRUE(file_util::PathExists(dest_file));
1253 const std::wstring read_contents = ReadTextFile(dest_file);
1254 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141255 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1256 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381257}
1258
[email protected]37088fef2008-08-15 17:32:101259// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131260#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381261TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]640517f2008-10-30 23:54:041262 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381263
1264 SYSTEMTIME start_time;
1265 GetLocalTime(&start_time);
1266 Sleep(100);
1267 CreateTextFile(file_name, L"New file!");
1268 Sleep(100);
1269 SYSTEMTIME end_time;
1270 GetLocalTime(&end_time);
1271
1272 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041273 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381274
1275 FILETIME start_filetime;
1276 SystemTimeToFileTime(&start_time, &start_filetime);
1277 FILETIME end_filetime;
1278 SystemTimeToFileTime(&end_time, &end_filetime);
1279 FILETIME file_creation_filetime;
1280 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1281
1282 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1283 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1284 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1285
1286 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1287 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1288 "end time: " << FileTimeAsUint64(end_filetime);
1289
[email protected]640517f2008-10-30 23:54:041290 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381291}
[email protected]37088fef2008-08-15 17:32:101292#endif
initial.commitd7cae122008-07-26 21:49:381293
[email protected]ed2f2332008-08-20 15:59:491294// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041295// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491296typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381297
[email protected]ed2f2332008-08-20 15:59:491298TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041299 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381300 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041301 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1302 .Append(FILE_PATH_LITERAL("data"))
1303 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381304 ASSERT_TRUE(file_util::PathExists(data_dir));
1305
[email protected]640517f2008-10-30 23:54:041306 FilePath original_file =
1307 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1308 FilePath same_file =
1309 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1310 FilePath same_length_file =
1311 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1312 FilePath different_file =
1313 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1314 FilePath different_first_file =
1315 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1316 FilePath different_last_file =
1317 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1318 FilePath empty1_file =
1319 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1320 FilePath empty2_file =
1321 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1322 FilePath shortened_file =
1323 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1324 FilePath binary_file =
1325 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1326 FilePath binary_file_same =
1327 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1328 FilePath binary_file_diff =
1329 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381330
1331 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1332 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1333 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1334 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221335 EXPECT_FALSE(file_util::ContentsEqual(
1336 FilePath(FILE_PATH_LITERAL("bogusname")),
1337 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381338 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1339 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1340 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1341 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1342 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1343 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1344 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1345}
1346
[email protected]b81637c32009-06-26 21:17:241347TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1348 FilePath data_dir;
1349 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1350 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1351 .Append(FILE_PATH_LITERAL("data"))
1352 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1353 ASSERT_TRUE(file_util::PathExists(data_dir));
1354
1355 FilePath original_file =
1356 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1357 FilePath same_file =
1358 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1359 FilePath crlf_file =
1360 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1361 FilePath shortened_file =
1362 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1363 FilePath different_file =
1364 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1365 FilePath different_first_file =
1366 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1367 FilePath different_last_file =
1368 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1369 FilePath first1_file =
1370 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1371 FilePath first2_file =
1372 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1373 FilePath empty1_file =
1374 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1375 FilePath empty2_file =
1376 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1377 FilePath blank_line_file =
1378 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1379 FilePath blank_line_crlf_file =
1380 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1381
1382 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1383 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1384 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1385 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1386 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1387 different_first_file));
1388 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1389 different_last_file));
1390 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1391 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1392 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1393 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1394 blank_line_crlf_file));
1395}
1396
[email protected]37088fef2008-08-15 17:32:101397// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131398#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381399TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]640517f2008-10-30 23:54:041400 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381401 CreateTextFile(target_file, L"This is the target.");
1402
[email protected]640517f2008-10-30 23:54:041403 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381404
1405 HRESULT result;
1406 IShellLink *shell = NULL;
1407 IPersistFile *persist = NULL;
1408
1409 CoInitialize(NULL);
1410 // Temporarily create a shortcut for test
1411 result = CoCreateInstance(CLSID_ShellLink, NULL,
1412 CLSCTX_INPROC_SERVER, IID_IShellLink,
1413 reinterpret_cast<LPVOID*>(&shell));
1414 EXPECT_TRUE(SUCCEEDED(result));
1415 result = shell->QueryInterface(IID_IPersistFile,
1416 reinterpret_cast<LPVOID*>(&persist));
1417 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041418 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381419 EXPECT_TRUE(SUCCEEDED(result));
1420 result = shell->SetDescription(L"ResolveShortcutTest");
1421 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041422 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381423 EXPECT_TRUE(SUCCEEDED(result));
1424 if (persist)
1425 persist->Release();
1426 if (shell)
1427 shell->Release();
1428
1429 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441430 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381431 EXPECT_TRUE(is_solved);
1432 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441433 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381434 EXPECT_EQ(L"This is the target.", contents);
1435
[email protected]d324ab332008-08-18 16:00:381436 // Cleaning
[email protected]640517f2008-10-30 23:54:041437 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441438 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381439 CoUninitialize();
1440}
1441
1442TEST_F(FileUtilTest, CreateShortcutTest) {
1443 const wchar_t file_contents[] = L"This is another target.";
[email protected]640517f2008-10-30 23:54:041444 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381445 CreateTextFile(target_file, file_contents);
1446
[email protected]640517f2008-10-30 23:54:041447 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381448
1449 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041450 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1451 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501452 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441453 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381454 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441455 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381456 EXPECT_EQ(file_contents, read_contents);
1457
[email protected]640517f2008-10-30 23:54:041458 DeleteFile(target_file.value().c_str());
1459 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381460 CoUninitialize();
1461}
[email protected]2c59af7dca2009-03-11 18:37:481462
1463TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1464 // Create a directory
1465 FilePath dir_name_from =
1466 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1467 file_util::CreateDirectory(dir_name_from);
1468 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1469
1470 // Create a file under the directory
1471 FilePath file_name_from =
1472 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1473 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1474 ASSERT_TRUE(file_util::PathExists(file_name_from));
1475
1476 // Move the directory by using CopyAndDeleteDirectory
1477 FilePath dir_name_to = test_dir_.Append(
1478 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1479 FilePath file_name_to =
1480 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1481
1482 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1483
1484 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1485
1486 // Check everything has been moved.
1487 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1488 EXPECT_FALSE(file_util::PathExists(file_name_from));
1489 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1490 EXPECT_TRUE(file_util::PathExists(file_name_to));
1491}
[email protected]7d95aae2009-10-09 07:33:391492
1493TEST_F(FileUtilTest, GetTempDirTest) {
1494 static const TCHAR* kTmpKey = _T("TMP");
1495 static const TCHAR* kTmpValues[] = {
1496 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1497 };
1498 // Save the original $TMP.
1499 size_t original_tmp_size;
1500 TCHAR* original_tmp;
1501 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1502 // original_tmp may be NULL.
1503
1504 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1505 FilePath path;
1506 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1507 file_util::GetTempDir(&path);
1508 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1509 " result=" << path.value();
1510 }
1511
1512 // Restore the original $TMP.
1513 if (original_tmp) {
1514 ::_tputenv_s(kTmpKey, original_tmp);
1515 free(original_tmp);
1516 } else {
1517 ::_tputenv_s(kTmpKey, _T(""));
1518 }
1519}
1520#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381521
[email protected]33edeab2009-08-18 16:07:551522TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1523 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391524 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551525 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391526 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1527 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1528 }
1529 for (int i = 0; i < 3; i++)
1530 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1531 for (int i = 0; i < 3; i++)
1532 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1533}
1534
[email protected]33edeab2009-08-18 16:07:551535TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391536 FilePath names[3];
1537 FILE *fps[3];
1538 int i;
1539
1540 // Create; make sure they are open and exist.
1541 for (i = 0; i < 3; ++i) {
1542 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1543 ASSERT_TRUE(fps[i]);
1544 EXPECT_TRUE(file_util::PathExists(names[i]));
1545 }
1546
1547 // Make sure all names are unique.
1548 for (i = 0; i < 3; ++i) {
1549 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1550 }
1551
1552 // Close and delete.
1553 for (i = 0; i < 3; ++i) {
1554 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1555 EXPECT_TRUE(file_util::Delete(names[i], false));
1556 }
initial.commitd7cae122008-07-26 21:49:381557}
1558
1559TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141560 FilePath temp_dir;
1561 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1562 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111563 EXPECT_TRUE(file_util::PathExists(temp_dir));
1564 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381565}
1566
[email protected]b0b3abd92010-04-30 17:00:091567TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1568 FilePath new_dir;
1569 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1570 test_dir_,
1571 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1572 &new_dir));
1573 EXPECT_TRUE(file_util::PathExists(new_dir));
1574 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1575 EXPECT_TRUE(file_util::Delete(new_dir, false));
1576}
1577
[email protected]9e51af92009-02-04 00:58:391578TEST_F(FileUtilTest, GetShmemTempDirTest) {
1579 FilePath dir;
1580 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1581 EXPECT_TRUE(file_util::DirectoryExists(dir));
1582}
1583
initial.commitd7cae122008-07-26 21:49:381584TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041585 FilePath test_root =
1586 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131587#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041588 FilePath test_path =
1589 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101590#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041591 FilePath test_path =
1592 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101593#endif
[email protected]806b9c62008-09-11 16:09:111594
1595 EXPECT_FALSE(file_util::PathExists(test_path));
1596 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1597 EXPECT_TRUE(file_util::PathExists(test_path));
1598 // CreateDirectory returns true if the DirectoryExists returns true.
1599 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1600
1601 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041602 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111603 EXPECT_FALSE(file_util::PathExists(test_path));
1604 CreateTextFile(test_path, L"test file");
1605 EXPECT_TRUE(file_util::PathExists(test_path));
1606 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1607
1608 EXPECT_TRUE(file_util::Delete(test_root, true));
1609 EXPECT_FALSE(file_util::PathExists(test_root));
1610 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411611
1612 // Verify assumptions made by the Windows implementation:
1613 // 1. The current directory always exists.
1614 // 2. The root directory always exists.
1615 ASSERT_TRUE(file_util::DirectoryExists(
1616 FilePath(FilePath::kCurrentDirectory)));
1617 FilePath top_level = test_root;
1618 while (top_level != top_level.DirName()) {
1619 top_level = top_level.DirName();
1620 }
1621 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1622
1623 // Given these assumptions hold, it should be safe to
1624 // test that "creating" these directories succeeds.
1625 EXPECT_TRUE(file_util::CreateDirectory(
1626 FilePath(FilePath::kCurrentDirectory)));
1627 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401628
1629#if defined(OS_WIN)
1630 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1631 FilePath invalid_path =
1632 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1633 if (!file_util::PathExists(invalid_drive)) {
1634 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1635 }
1636#endif
[email protected]806b9c62008-09-11 16:09:111637}
1638
1639TEST_F(FileUtilTest, DetectDirectoryTest) {
1640 // Check a directory
[email protected]640517f2008-10-30 23:54:041641 FilePath test_root =
1642 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111643 EXPECT_FALSE(file_util::PathExists(test_root));
1644 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1645 EXPECT_TRUE(file_util::PathExists(test_root));
1646 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1647
1648 // Check a file
[email protected]640517f2008-10-30 23:54:041649 FilePath test_path =
1650 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111651 EXPECT_FALSE(file_util::PathExists(test_path));
1652 CreateTextFile(test_path, L"test file");
1653 EXPECT_TRUE(file_util::PathExists(test_path));
1654 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1655 EXPECT_TRUE(file_util::Delete(test_path, false));
1656
1657 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381658}
1659
initial.commitd7cae122008-07-26 21:49:381660TEST_F(FileUtilTest, FileEnumeratorTest) {
1661 // Test an empty directory.
[email protected]8199b3a2009-06-09 05:57:381662 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121663 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1664 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381665
[email protected]8199b3a2009-06-09 05:57:381666 // Test an empty directory, non-recursively, including "..".
1667 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1668 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1669 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1670 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1671 f0_dotdot.Next().value());
1672 EXPECT_EQ(FILE_PATH_LITERAL(""),
1673 f0_dotdot.Next().value());
1674
[email protected]37088fef2008-08-15 17:32:101675 // create the directories
[email protected]640517f2008-10-30 23:54:041676 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101677 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]640517f2008-10-30 23:54:041678 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101679 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041680 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101681 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041682
[email protected]37088fef2008-08-15 17:32:101683 // create the files
[email protected]640517f2008-10-30 23:54:041684 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101685 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041686 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101687 CreateTextFile(dir2innerfile, L"");
[email protected]640517f2008-10-30 23:54:041688 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101689 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041690 FilePath file2_rel =
1691 dir2.Append(FilePath::kParentDirectory)
1692 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101693 CreateTextFile(file2_rel, L"");
[email protected]640517f2008-10-30 23:54:041694 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381695
1696 // Only enumerate files.
[email protected]0b733222008-12-11 14:55:121697 file_util::FileEnumerator f1(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381698 file_util::FileEnumerator::FILES);
1699 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101700 EXPECT_TRUE(c1.HasFile(file1));
1701 EXPECT_TRUE(c1.HasFile(file2_abs));
1702 EXPECT_TRUE(c1.HasFile(dir2file));
1703 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1704 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381705
1706 // Only enumerate directories.
[email protected]0b733222008-12-11 14:55:121707 file_util::FileEnumerator f2(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381708 file_util::FileEnumerator::DIRECTORIES);
1709 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101710 EXPECT_TRUE(c2.HasFile(dir1));
1711 EXPECT_TRUE(c2.HasFile(dir2));
1712 EXPECT_TRUE(c2.HasFile(dir2inner));
1713 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381714
[email protected]3bc9ffaf2008-10-16 02:42:451715 // Only enumerate directories non-recursively.
1716 file_util::FileEnumerator f2_non_recursive(
[email protected]0b733222008-12-11 14:55:121717 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451718 FindResultCollector c2_non_recursive(f2_non_recursive);
1719 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1720 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1721 EXPECT_EQ(c2_non_recursive.size(), 2);
1722
[email protected]8199b3a2009-06-09 05:57:381723 // Only enumerate directories, non-recursively, including "..".
1724 file_util::FileEnumerator f2_dotdot(
1725 test_dir_, false,
1726 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1727 file_util::FileEnumerator::DIRECTORIES |
1728 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1729 FindResultCollector c2_dotdot(f2_dotdot);
1730 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1731 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1732 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1733 EXPECT_EQ(c2_dotdot.size(), 3);
1734
initial.commitd7cae122008-07-26 21:49:381735 // Enumerate files and directories.
[email protected]8199b3a2009-06-09 05:57:381736 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381737 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101738 EXPECT_TRUE(c3.HasFile(dir1));
1739 EXPECT_TRUE(c3.HasFile(dir2));
1740 EXPECT_TRUE(c3.HasFile(file1));
1741 EXPECT_TRUE(c3.HasFile(file2_abs));
1742 EXPECT_TRUE(c3.HasFile(dir2file));
1743 EXPECT_TRUE(c3.HasFile(dir2inner));
1744 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1745 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381746
1747 // Non-recursive operation.
[email protected]8199b3a2009-06-09 05:57:381748 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381749 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101750 EXPECT_TRUE(c4.HasFile(dir2));
1751 EXPECT_TRUE(c4.HasFile(dir2));
1752 EXPECT_TRUE(c4.HasFile(file1));
1753 EXPECT_TRUE(c4.HasFile(file2_abs));
1754 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381755
1756 // Enumerate with a pattern.
[email protected]8199b3a2009-06-09 05:57:381757 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121758 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381759 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101760 EXPECT_TRUE(c5.HasFile(dir1));
1761 EXPECT_TRUE(c5.HasFile(dir2));
1762 EXPECT_TRUE(c5.HasFile(dir2file));
1763 EXPECT_TRUE(c5.HasFile(dir2inner));
1764 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1765 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381766
1767 // Make sure the destructor closes the find handle while in the middle of a
1768 // query to allow TearDown to delete the directory.
[email protected]8199b3a2009-06-09 05:57:381769 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121770 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1771 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381772}
license.botbf09a502008-08-24 00:55:551773
[email protected]ee5c29da2009-01-09 22:14:271774TEST_F(FileUtilTest, Contains) {
[email protected]11e53f62009-03-10 18:20:441775 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271776
1777 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571778 if (file_util::PathExists(data_dir)) {
1779 ASSERT_TRUE(file_util::Delete(data_dir, true));
1780 }
[email protected]ee5c29da2009-01-09 22:14:271781 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1782
1783 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1784 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1785 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1786 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1787
1788 // Annoyingly, the directories must actually exist in order for realpath(),
1789 // which Contains() relies on in posix, to work.
1790 ASSERT_TRUE(file_util::CreateDirectory(foo));
1791 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581792 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1793 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1794 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271795
1796 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1797 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1798 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1799 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1800
[email protected]e43eddf12009-12-29 00:32:521801 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271802 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1803#if defined(OS_WIN)
1804 EXPECT_TRUE(file_util::ContainsPath(foo,
1805 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391806 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271807 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521808#elif defined(OS_MACOSX)
1809 // We can't really do this test on OS X since the case-sensitivity of the
1810 // filesystem is configurable.
1811#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271812 EXPECT_FALSE(file_util::ContainsPath(foo,
1813 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271814#endif
1815}
1816
[email protected]ec3d1452010-02-18 10:02:261817TEST_F(FileUtilTest, LastModified) {
1818 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1819
1820 // Create a fresh, empty copy of this directory.
1821 if (file_util::PathExists(data_dir)) {
1822 ASSERT_TRUE(file_util::Delete(data_dir, true));
1823 }
1824 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1825
1826 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1827 std::string data("hello");
1828 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1829
1830 base::Time modification_time;
1831 // Note that this timestamp is divisible by two (seconds) - FAT stores
1832 // modification times with 2s resolution.
1833 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1834 &modification_time));
1835 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1836 file_util::FileInfo file_info;
1837 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1838 ASSERT_TRUE(file_info.last_modified == modification_time);
1839}
1840
[email protected]b33f1d92010-05-26 01:40:121841TEST_F(FileUtilTest, IsDirectoryEmpty) {
1842 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1843
1844 ASSERT_FALSE(file_util::PathExists(empty_dir));
1845
1846 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1847
1848 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1849
1850 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1851 std::string bar("baz");
1852 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1853
1854 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1855}
1856
[email protected]11b901ee2008-09-10 00:16:281857} // namespace