blob: 6341beb4d552a2b30f545bc7ac531570947118cc [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]552b3152010-06-10 12:40:52409TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commitd7cae122008-07-26 21:49:38410 // Create old file (that we don't want to count)
[email protected]85786f92009-04-18 00:42:48411 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commitd7cae122008-07-26 21:49:38412 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
413
414 // Age to perfection
[email protected]4b7743de2009-04-21 01:50:39415#if defined(OS_WIN)
[email protected]2126577b2009-04-23 15:05:19416 PlatformThread::Sleep(100);
[email protected]4b7743de2009-04-21 01:50:39417#elif defined(OS_POSIX)
418 // We need to wait at least one second here because the precision of
419 // file creation time is one second.
[email protected]2126577b2009-04-23 15:05:19420 PlatformThread::Sleep(1500);
[email protected]4b7743de2009-04-21 01:50:39421#endif
initial.commitd7cae122008-07-26 21:49:38422
423 // Establish our cutoff time
[email protected]85786f92009-04-18 00:42:48424 base::Time now(base::Time::NowFromSystemTime());
425 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38426
427 // Create a new file (that we do want to count)
[email protected]85786f92009-04-18 00:42:48428 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commitd7cae122008-07-26 21:49:38429 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
430
431 // We should see only the new file.
[email protected]85786f92009-04-18 00:42:48432 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38433
434 // Delete new file, we should see no files after cutoff now
435 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]85786f92009-04-18 00:42:48436 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38437}
438
[email protected]c2c132c62010-03-24 21:56:26439TEST_F(FileUtilTest, FileAndDirectorySize) {
440 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
441 // should return 53 bytes.
442 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
443 CreateTextFile(file_01, L"12345678901234567890");
444 int64 size_f1 = 0;
445 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
446 EXPECT_EQ(20ll, size_f1);
447
448 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
449 file_util::CreateDirectory(subdir_path);
450
451 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
452 CreateTextFile(file_02, L"123456789012345678901234567890");
453 int64 size_f2 = 0;
454 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
455 EXPECT_EQ(30ll, size_f2);
456
457 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
458 file_util::CreateDirectory(subsubdir_path);
459
460 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
461 CreateTextFile(file_03, L"123");
462
463 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
464 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
465}
466
[email protected]6f5f4322010-06-09 22:56:48467TEST_F(FileUtilTest, NormalizeFilePathBasic) {
468 // Create a directory under the test dir. Because we create it,
469 // we know it is not a link.
470 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
471 FilePath dir_path = test_dir_.Append(FPL("dir"));
472 FilePath file_b_path = dir_path.Append(FPL("file_b"));
473 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57474
[email protected]6f5f4322010-06-09 22:56:48475 FilePath normalized_file_a_path, normalized_file_b_path;
476 ASSERT_FALSE(file_util::PathExists(file_a_path));
477 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
478 &normalized_file_a_path))
479 << "NormalizeFilePath() should fail on nonexistant paths.";
480
481 CreateTextFile(file_a_path, bogus_content);
482 ASSERT_TRUE(file_util::PathExists(file_a_path));
483 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
484 &normalized_file_a_path));
485
486 CreateTextFile(file_b_path, bogus_content);
487 ASSERT_TRUE(file_util::PathExists(file_b_path));
488 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
489 &normalized_file_b_path));
490
491 // Beacuse this test created |dir_path|, we know it is not a link
492 // or junction. So, the real path of the directory holding file a
493 // must be the parent of the path holding file b.
494 ASSERT_TRUE(normalized_file_a_path.DirName()
495 .IsParent(normalized_file_b_path.DirName()));
496}
497
498#if defined(OS_WIN)
499
500TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
501 // Build the following directory structure:
502 //
503 // test_dir_
504 // |-> base_a
505 // | |-> sub_a
506 // | |-> file.txt
507 // | |-> long_name___... (Very long name.)
508 // | |-> sub_long
509 // | |-> deep.txt
510 // |-> base_b
511 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
512 // |-> to_base_b (reparse point to test_dir_\base_b)
513 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
514
515 FilePath base_a = test_dir_.Append(FPL("base_a"));
516 ASSERT_TRUE(file_util::CreateDirectory(base_a));
517
518 FilePath sub_a = base_a.Append(FPL("sub_a"));
519 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
520
521 FilePath file_txt = sub_a.Append(FPL("file.txt"));
522 CreateTextFile(file_txt, bogus_content);
523
524 // Want a directory whose name is long enough to make the path to the file
525 // inside just under MAX_PATH chars. This will be used to test that when
526 // a junction expands to a path over MAX_PATH chars in length,
527 // NormalizeFilePath() fails without crashing.
528 FilePath sub_long_rel(FPL("sub_long"));
529 FilePath deep_txt(FPL("deep.txt"));
530
531 int target_length = MAX_PATH;
532 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
533 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52534 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48535 // the resulting path is still long enough to hit the failing case in
536 // NormalizePath().
537 const int kCreateDirLimit = 4;
538 target_length -= kCreateDirLimit;
539 FilePath::StringType long_name_str = FPL("long_name_");
540 long_name_str.resize(target_length, '_');
541
542 FilePath long_name = sub_a.Append(FilePath(long_name_str));
543 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
544 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
545
546 FilePath sub_long = deep_file.DirName();
547 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
548 CreateTextFile(deep_file, bogus_content);
549
550 FilePath base_b = test_dir_.Append(FPL("base_b"));
551 ASSERT_TRUE(file_util::CreateDirectory(base_b));
552
553 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
554 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
555 ScopedHandle reparse_to_sub_a(
556 ::CreateFile(to_sub_a.value().c_str(),
557 FILE_ALL_ACCESS,
558 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
559 NULL,
560 OPEN_EXISTING,
561 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
562 NULL));
563 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_a.Get());
564 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
565
566 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
567 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
568 ScopedHandle reparse_to_base_b(
569 ::CreateFile(to_base_b.value().c_str(),
570 FILE_ALL_ACCESS,
571 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
572 NULL,
573 OPEN_EXISTING,
574 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
575 NULL));
576 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_base_b.Get());
577 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
578
579 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
580 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
581 ScopedHandle reparse_to_sub_long(
582 ::CreateFile(to_sub_long.value().c_str(),
583 FILE_ALL_ACCESS,
584 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
585 NULL,
586 OPEN_EXISTING,
587 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
588 NULL));
589 ASSERT_NE(INVALID_HANDLE_VALUE, reparse_to_sub_long.Get());
590 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
591
592 // Normalize a junction free path: base_a\sub_a\file.txt .
593 FilePath normalized_path;
594 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
595 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
596
597 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
598 // the junction to_sub_a.
599 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
600 &normalized_path));
601 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
602
603 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
604 // normalized to exclude junctions to_base_b and to_sub_a .
605 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
606 .Append(FPL("to_base_b"))
607 .Append(FPL("to_sub_a"))
608 .Append(FPL("file.txt")),
609 &normalized_path));
610 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
611
612 // A long enough path will cause NormalizeFilePath() to fail. Make a long
613 // path using to_base_b many times, and check that paths long enough to fail
614 // do not cause a crash.
615 FilePath long_path = base_b;
616 const int kLengthLimit = MAX_PATH + 200;
617 while (long_path.value().length() <= kLengthLimit) {
618 long_path = long_path.Append(FPL("to_base_b"));
619 }
620 long_path = long_path.Append(FPL("to_sub_a"))
621 .Append(FPL("file.txt"));
622
623 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
624
625 // Normalizing the junction to deep.txt should fail, because the expanded
626 // path to deep.txt is longer than MAX_PATH.
627 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
628 &normalized_path));
629
630 // Delete the reparse points, and see that NormalizeFilePath() fails
631 // to traverse them.
632 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
633 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
634 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
635
636 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
637 &normalized_path));
638}
639
640#endif // defined(OS_WIN)
641
642// The following test of NormalizeFilePath() require that we create a symlink.
643// This can not be done on windows before vista. On vista, creating a symlink
644// requires privilege "SeCreateSymbolicLinkPrivilege".
645// TODO(skerner): Investigate the possibility of giving base_unittests the
646// privileges required to create a symlink.
647#if defined(OS_POSIX)
648
649bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
650 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
651}
652
653TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
654 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57655
656 // Link one file to another.
[email protected]6f5f4322010-06-09 22:56:48657 FilePath link_from = test_dir_.Append(FPL("from_file"));
658 FilePath link_to = test_dir_.Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57659 CreateTextFile(link_to, bogus_content);
660
[email protected]6f5f4322010-06-09 22:56:48661 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57662 << "Failed to create file symlink.";
663
[email protected]6f5f4322010-06-09 22:56:48664 // Check that NormalizeFilePath sees the link.
665 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57666 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48667 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
668 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57669
670 // Link to a directory.
[email protected]6f5f4322010-06-09 22:56:48671 link_from = test_dir_.Append(FPL("from_dir"));
672 link_to = test_dir_.Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57673 file_util::CreateDirectory(link_to);
674
[email protected]6f5f4322010-06-09 22:56:48675 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57676 << "Failed to create directory symlink.";
677
[email protected]6f5f4322010-06-09 22:56:48678 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
679 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57680
[email protected]6f5f4322010-06-09 22:56:48681 // Test that a loop in the links causes NormalizeFilePath() to return false.
682 link_from = test_dir_.Append(FPL("link_a"));
683 link_to = test_dir_.Append(FPL("link_b"));
684 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57685 << "Failed to create loop symlink a.";
[email protected]6f5f4322010-06-09 22:56:48686 ASSERT_TRUE(MakeSymlink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57687 << "Failed to create loop symlink b.";
688
689 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48690 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57691}
692#endif // defined(OS_POSIX)
693
[email protected]83d86252010-05-07 18:58:45694TEST_F(FileUtilTest, DeleteNonExistent) {
695 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
696 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36697
[email protected]83d86252010-05-07 18:58:45698 EXPECT_TRUE(file_util::Delete(non_existent, false));
699 ASSERT_FALSE(file_util::PathExists(non_existent));
700 EXPECT_TRUE(file_util::Delete(non_existent, true));
701 ASSERT_FALSE(file_util::PathExists(non_existent));
702}
703
704TEST_F(FileUtilTest, DeleteFile) {
705 // Create a file
706 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
707 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38708 ASSERT_TRUE(file_util::PathExists(file_name));
709
[email protected]83d86252010-05-07 18:58:45710 // Make sure it's deleted
711 EXPECT_TRUE(file_util::Delete(file_name, false));
712 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36713
[email protected]83d86252010-05-07 18:58:45714 // Test recursive case, create a new file
715 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
716 CreateTextFile(file_name, bogus_content);
717 ASSERT_TRUE(file_util::PathExists(file_name));
718
719 // Make sure it's deleted
720 EXPECT_TRUE(file_util::Delete(file_name, true));
721 EXPECT_FALSE(file_util::PathExists(file_name));
722}
723
724#if defined(OS_WIN)
725// Tests that the Delete function works for wild cards, especially
726// with the recursion flag. Also coincidentally tests PathExists.
727// TODO(erikkay): see if anyone's actually using this feature of the API
728TEST_F(FileUtilTest, DeleteWildCard) {
729 // Create a file and a directory
730 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
731 CreateTextFile(file_name, bogus_content);
732 ASSERT_TRUE(file_util::PathExists(file_name));
733
734 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
735 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38736 ASSERT_TRUE(file_util::PathExists(subdir_path));
737
[email protected]83d86252010-05-07 18:58:45738 // Create the wildcard path
[email protected]640517f2008-10-30 23:54:04739 FilePath directory_contents = test_dir_;
[email protected]83d86252010-05-07 18:58:45740 directory_contents = directory_contents.Append(FPL("*"));
741
initial.commitd7cae122008-07-26 21:49:38742 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45743 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10744 EXPECT_FALSE(file_util::PathExists(file_name));
745 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40746
[email protected]3a51cfdd2010-05-07 00:05:36747 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45748 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40749 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36750 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40751}
752
[email protected]83d86252010-05-07 18:58:45753// TODO(erikkay): see if anyone's actually using this feature of the API
754TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
755 // Create a file and a directory
756 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
757 file_util::CreateDirectory(subdir_path);
758 ASSERT_TRUE(file_util::PathExists(subdir_path));
759
760 // Create the wildcard path
761 FilePath directory_contents = subdir_path;
762 directory_contents = directory_contents.Append(FPL("*"));
763
764 // Delete non-recursively and check nothing got deleted
765 EXPECT_TRUE(file_util::Delete(directory_contents, false));
766 EXPECT_TRUE(file_util::PathExists(subdir_path));
767
768 // Delete recursively and check nothing got deleted
769 EXPECT_TRUE(file_util::Delete(directory_contents, true));
770 EXPECT_TRUE(file_util::PathExists(subdir_path));
771}
772#endif
773
774// Tests non-recursive Delete() for a directory.
775TEST_F(FileUtilTest, DeleteDirNonRecursive) {
776 // Create a subdirectory and put a file and two directories inside.
777 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
778 file_util::CreateDirectory(test_subdir);
779 ASSERT_TRUE(file_util::PathExists(test_subdir));
780
781 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
782 CreateTextFile(file_name, bogus_content);
783 ASSERT_TRUE(file_util::PathExists(file_name));
784
785 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
786 file_util::CreateDirectory(subdir_path1);
787 ASSERT_TRUE(file_util::PathExists(subdir_path1));
788
789 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
790 file_util::CreateDirectory(subdir_path2);
791 ASSERT_TRUE(file_util::PathExists(subdir_path2));
792
793 // Delete non-recursively and check that the empty dir got deleted
794 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
795 EXPECT_FALSE(file_util::PathExists(subdir_path2));
796
797 // Delete non-recursively and check that nothing got deleted
798 EXPECT_FALSE(file_util::Delete(test_subdir, false));
799 EXPECT_TRUE(file_util::PathExists(test_subdir));
800 EXPECT_TRUE(file_util::PathExists(file_name));
801 EXPECT_TRUE(file_util::PathExists(subdir_path1));
802}
803
804// Tests recursive Delete() for a directory.
805TEST_F(FileUtilTest, DeleteDirRecursive) {
806 // Create a subdirectory and put a file and two directories inside.
807 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
808 file_util::CreateDirectory(test_subdir);
809 ASSERT_TRUE(file_util::PathExists(test_subdir));
810
811 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
812 CreateTextFile(file_name, bogus_content);
813 ASSERT_TRUE(file_util::PathExists(file_name));
814
815 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
816 file_util::CreateDirectory(subdir_path1);
817 ASSERT_TRUE(file_util::PathExists(subdir_path1));
818
819 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
820 file_util::CreateDirectory(subdir_path2);
821 ASSERT_TRUE(file_util::PathExists(subdir_path2));
822
823 // Delete recursively and check that the empty dir got deleted
824 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
825 EXPECT_FALSE(file_util::PathExists(subdir_path2));
826
827 // Delete recursively and check that everything got deleted
828 EXPECT_TRUE(file_util::Delete(test_subdir, true));
829 EXPECT_FALSE(file_util::PathExists(file_name));
830 EXPECT_FALSE(file_util::PathExists(subdir_path1));
831 EXPECT_FALSE(file_util::PathExists(test_subdir));
832}
833
[email protected]bc6a9012009-10-15 01:11:44834TEST_F(FileUtilTest, MoveFileNew) {
835 // Create a file
836 FilePath file_name_from =
837 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
838 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
839 ASSERT_TRUE(file_util::PathExists(file_name_from));
840
841 // The destination
842 FilePath file_name_to =
843 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
844 ASSERT_FALSE(file_util::PathExists(file_name_to));
845
846 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
847
848 // Check everything has been moved.
849 EXPECT_FALSE(file_util::PathExists(file_name_from));
850 EXPECT_TRUE(file_util::PathExists(file_name_to));
851}
852
853TEST_F(FileUtilTest, MoveFileExists) {
854 // Create a file
855 FilePath file_name_from =
856 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
857 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
858 ASSERT_TRUE(file_util::PathExists(file_name_from));
859
860 // The destination name
861 FilePath file_name_to =
862 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
863 CreateTextFile(file_name_to, L"Old file content");
864 ASSERT_TRUE(file_util::PathExists(file_name_to));
865
866 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
867
868 // Check everything has been moved.
869 EXPECT_FALSE(file_util::PathExists(file_name_from));
870 EXPECT_TRUE(file_util::PathExists(file_name_to));
871 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
872}
873
874TEST_F(FileUtilTest, MoveFileDirExists) {
875 // Create a file
876 FilePath file_name_from =
877 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
878 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
879 ASSERT_TRUE(file_util::PathExists(file_name_from));
880
881 // The destination directory
882 FilePath dir_name_to =
883 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
884 file_util::CreateDirectory(dir_name_to);
885 ASSERT_TRUE(file_util::PathExists(dir_name_to));
886
887 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
888}
889
890
[email protected]abbc5732009-10-13 17:57:27891TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38892 // Create a directory
[email protected]640517f2008-10-30 23:54:04893 FilePath dir_name_from =
894 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
895 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38896 ASSERT_TRUE(file_util::PathExists(dir_name_from));
897
898 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04899 FilePath file_name_from =
900 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38901 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
902 ASSERT_TRUE(file_util::PathExists(file_name_from));
903
904 // Move the directory
[email protected]640517f2008-10-30 23:54:04905 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
906 FilePath file_name_to =
907 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38908
909 ASSERT_FALSE(file_util::PathExists(dir_name_to));
910
911 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
912
913 // Check everything has been moved.
914 EXPECT_FALSE(file_util::PathExists(dir_name_from));
915 EXPECT_FALSE(file_util::PathExists(file_name_from));
916 EXPECT_TRUE(file_util::PathExists(dir_name_to));
917 EXPECT_TRUE(file_util::PathExists(file_name_to));
918}
919
[email protected]abbc5732009-10-13 17:57:27920TEST_F(FileUtilTest, MoveExist) {
921 // Create a directory
922 FilePath dir_name_from =
923 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
924 file_util::CreateDirectory(dir_name_from);
925 ASSERT_TRUE(file_util::PathExists(dir_name_from));
926
927 // Create a file under the directory
928 FilePath file_name_from =
929 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
930 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
931 ASSERT_TRUE(file_util::PathExists(file_name_from));
932
933 // Move the directory
934 FilePath dir_name_exists =
935 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
936
937 FilePath dir_name_to =
938 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
939 FilePath file_name_to =
940 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
941
942 // Create the destination directory.
943 file_util::CreateDirectory(dir_name_exists);
944 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
945
946 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
947
948 // Check everything has been moved.
949 EXPECT_FALSE(file_util::PathExists(dir_name_from));
950 EXPECT_FALSE(file_util::PathExists(file_name_from));
951 EXPECT_TRUE(file_util::PathExists(dir_name_to));
952 EXPECT_TRUE(file_util::PathExists(file_name_to));
953}
954
955TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38956 // Create a directory.
[email protected]640517f2008-10-30 23:54:04957 FilePath dir_name_from =
958 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
959 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38960 ASSERT_TRUE(file_util::PathExists(dir_name_from));
961
962 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04963 FilePath file_name_from =
964 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38965 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
966 ASSERT_TRUE(file_util::PathExists(file_name_from));
967
968 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04969 FilePath subdir_name_from =
970 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
971 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38972 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
973
974 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04975 FilePath file_name2_from =
976 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38977 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
978 ASSERT_TRUE(file_util::PathExists(file_name2_from));
979
980 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04981 FilePath dir_name_to =
982 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
983 FilePath file_name_to =
984 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
985 FilePath subdir_name_to =
986 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
987 FilePath file_name2_to =
988 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38989
990 ASSERT_FALSE(file_util::PathExists(dir_name_to));
991
992 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
993
994 // Check everything has been copied.
995 EXPECT_TRUE(file_util::PathExists(dir_name_from));
996 EXPECT_TRUE(file_util::PathExists(file_name_from));
997 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
998 EXPECT_TRUE(file_util::PathExists(file_name2_from));
999 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1000 EXPECT_TRUE(file_util::PathExists(file_name_to));
1001 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1002 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1003}
1004
[email protected]abbc5732009-10-13 17:57:271005TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1006 // Create a directory.
1007 FilePath dir_name_from =
1008 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1009 file_util::CreateDirectory(dir_name_from);
1010 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1011
1012 // Create a file under the directory.
1013 FilePath file_name_from =
1014 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1015 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1016 ASSERT_TRUE(file_util::PathExists(file_name_from));
1017
1018 // Create a subdirectory.
1019 FilePath subdir_name_from =
1020 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1021 file_util::CreateDirectory(subdir_name_from);
1022 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1023
1024 // Create a file under the subdirectory.
1025 FilePath file_name2_from =
1026 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1027 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1028 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1029
1030 // Copy the directory recursively.
1031 FilePath dir_name_exists =
1032 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1033
1034 FilePath dir_name_to =
1035 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1036 FilePath file_name_to =
1037 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1038 FilePath subdir_name_to =
1039 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1040 FilePath file_name2_to =
1041 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1042
1043 // Create the destination directory.
1044 file_util::CreateDirectory(dir_name_exists);
1045 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1046
1047 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1048
1049 // Check everything has been copied.
1050 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1051 EXPECT_TRUE(file_util::PathExists(file_name_from));
1052 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1053 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1054 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1055 EXPECT_TRUE(file_util::PathExists(file_name_to));
1056 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1057 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1058}
1059
1060TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381061 // Create a directory.
[email protected]640517f2008-10-30 23:54:041062 FilePath dir_name_from =
1063 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1064 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381065 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1066
1067 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041068 FilePath file_name_from =
1069 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381070 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1071 ASSERT_TRUE(file_util::PathExists(file_name_from));
1072
1073 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041074 FilePath subdir_name_from =
1075 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1076 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381077 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1078
1079 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041080 FilePath file_name2_from =
1081 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381082 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1083 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1084
1085 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041086 FilePath dir_name_to =
1087 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1088 FilePath file_name_to =
1089 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1090 FilePath subdir_name_to =
1091 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381092
1093 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1094
1095 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1096
1097 // Check everything has been copied.
1098 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1099 EXPECT_TRUE(file_util::PathExists(file_name_from));
1100 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1101 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1102 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1103 EXPECT_TRUE(file_util::PathExists(file_name_to));
1104 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1105}
1106
[email protected]abbc5732009-10-13 17:57:271107TEST_F(FileUtilTest, CopyDirectoryExists) {
1108 // Create a directory.
1109 FilePath dir_name_from =
1110 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1111 file_util::CreateDirectory(dir_name_from);
1112 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1113
1114 // Create a file under the directory.
1115 FilePath file_name_from =
1116 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1117 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1118 ASSERT_TRUE(file_util::PathExists(file_name_from));
1119
1120 // Create a subdirectory.
1121 FilePath subdir_name_from =
1122 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1123 file_util::CreateDirectory(subdir_name_from);
1124 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1125
1126 // Create a file under the subdirectory.
1127 FilePath file_name2_from =
1128 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1129 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1130 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1131
1132 // Copy the directory not recursively.
1133 FilePath dir_name_to =
1134 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1135 FilePath file_name_to =
1136 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1137 FilePath subdir_name_to =
1138 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1139
1140 // Create the destination directory.
1141 file_util::CreateDirectory(dir_name_to);
1142 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1143
1144 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1145
1146 // Check everything has been copied.
1147 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1148 EXPECT_TRUE(file_util::PathExists(file_name_from));
1149 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1150 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1151 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1152 EXPECT_TRUE(file_util::PathExists(file_name_to));
1153 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1154}
1155
[email protected]bc6a9012009-10-15 01:11:441156TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1157 // Create a file
1158 FilePath file_name_from =
1159 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1160 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1161 ASSERT_TRUE(file_util::PathExists(file_name_from));
1162
1163 // The destination name
1164 FilePath file_name_to =
1165 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1166 ASSERT_FALSE(file_util::PathExists(file_name_to));
1167
1168 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1169
1170 // Check the has been copied
1171 EXPECT_TRUE(file_util::PathExists(file_name_to));
1172}
1173
1174TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1175 // Create a file
1176 FilePath file_name_from =
1177 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1178 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1179 ASSERT_TRUE(file_util::PathExists(file_name_from));
1180
1181 // The destination name
1182 FilePath file_name_to =
1183 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1184 CreateTextFile(file_name_to, L"Old file content");
1185 ASSERT_TRUE(file_util::PathExists(file_name_to));
1186
1187 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1188
1189 // Check the has been copied
1190 EXPECT_TRUE(file_util::PathExists(file_name_to));
1191 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1192}
1193
1194TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1195 // Create a file
1196 FilePath file_name_from =
1197 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1198 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1199 ASSERT_TRUE(file_util::PathExists(file_name_from));
1200
1201 // The destination
1202 FilePath dir_name_to =
1203 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1204 file_util::CreateDirectory(dir_name_to);
1205 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1206 FilePath file_name_to =
1207 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1208
1209 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1210
1211 // Check the has been copied
1212 EXPECT_TRUE(file_util::PathExists(file_name_to));
1213}
1214
initial.commitd7cae122008-07-26 21:49:381215TEST_F(FileUtilTest, CopyFile) {
1216 // Create a directory
[email protected]640517f2008-10-30 23:54:041217 FilePath dir_name_from =
1218 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1219 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381220 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1221
1222 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041223 FilePath file_name_from =
1224 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381225 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1226 CreateTextFile(file_name_from, file_contents);
1227 ASSERT_TRUE(file_util::PathExists(file_name_from));
1228
1229 // Copy the file.
[email protected]640517f2008-10-30 23:54:041230 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381231 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111232
[email protected]37088fef2008-08-15 17:32:101233 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141234 FilePath dest_file2(dir_name_from);
1235 dest_file2 = dest_file2.AppendASCII("..");
1236 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1237 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1238
1239 FilePath dest_file2_test(dir_name_from);
1240 dest_file2_test = dest_file2_test.DirName();
1241 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381242
1243 // Check everything has been copied.
1244 EXPECT_TRUE(file_util::PathExists(file_name_from));
1245 EXPECT_TRUE(file_util::PathExists(dest_file));
1246 const std::wstring read_contents = ReadTextFile(dest_file);
1247 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141248 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1249 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381250}
1251
[email protected]37088fef2008-08-15 17:32:101252// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131253#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381254TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]640517f2008-10-30 23:54:041255 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381256
1257 SYSTEMTIME start_time;
1258 GetLocalTime(&start_time);
1259 Sleep(100);
1260 CreateTextFile(file_name, L"New file!");
1261 Sleep(100);
1262 SYSTEMTIME end_time;
1263 GetLocalTime(&end_time);
1264
1265 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041266 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381267
1268 FILETIME start_filetime;
1269 SystemTimeToFileTime(&start_time, &start_filetime);
1270 FILETIME end_filetime;
1271 SystemTimeToFileTime(&end_time, &end_filetime);
1272 FILETIME file_creation_filetime;
1273 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1274
1275 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1276 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1277 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1278
1279 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1280 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1281 "end time: " << FileTimeAsUint64(end_filetime);
1282
[email protected]640517f2008-10-30 23:54:041283 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381284}
[email protected]37088fef2008-08-15 17:32:101285#endif
initial.commitd7cae122008-07-26 21:49:381286
[email protected]ed2f2332008-08-20 15:59:491287// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041288// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491289typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381290
[email protected]ed2f2332008-08-20 15:59:491291TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041292 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381293 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041294 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1295 .Append(FILE_PATH_LITERAL("data"))
1296 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381297 ASSERT_TRUE(file_util::PathExists(data_dir));
1298
[email protected]640517f2008-10-30 23:54:041299 FilePath original_file =
1300 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1301 FilePath same_file =
1302 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1303 FilePath same_length_file =
1304 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1305 FilePath different_file =
1306 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1307 FilePath different_first_file =
1308 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1309 FilePath different_last_file =
1310 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1311 FilePath empty1_file =
1312 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1313 FilePath empty2_file =
1314 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1315 FilePath shortened_file =
1316 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1317 FilePath binary_file =
1318 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1319 FilePath binary_file_same =
1320 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1321 FilePath binary_file_diff =
1322 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381323
1324 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1325 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1326 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1327 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221328 EXPECT_FALSE(file_util::ContentsEqual(
1329 FilePath(FILE_PATH_LITERAL("bogusname")),
1330 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381331 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1332 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1333 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1334 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1335 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1336 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1337 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1338}
1339
[email protected]b81637c32009-06-26 21:17:241340TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1341 FilePath data_dir;
1342 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1343 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1344 .Append(FILE_PATH_LITERAL("data"))
1345 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1346 ASSERT_TRUE(file_util::PathExists(data_dir));
1347
1348 FilePath original_file =
1349 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1350 FilePath same_file =
1351 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1352 FilePath crlf_file =
1353 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1354 FilePath shortened_file =
1355 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1356 FilePath different_file =
1357 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1358 FilePath different_first_file =
1359 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1360 FilePath different_last_file =
1361 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1362 FilePath first1_file =
1363 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1364 FilePath first2_file =
1365 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1366 FilePath empty1_file =
1367 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1368 FilePath empty2_file =
1369 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1370 FilePath blank_line_file =
1371 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1372 FilePath blank_line_crlf_file =
1373 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1374
1375 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1376 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1377 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1378 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1379 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1380 different_first_file));
1381 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1382 different_last_file));
1383 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1384 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1385 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1386 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1387 blank_line_crlf_file));
1388}
1389
[email protected]37088fef2008-08-15 17:32:101390// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131391#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381392TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]640517f2008-10-30 23:54:041393 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381394 CreateTextFile(target_file, L"This is the target.");
1395
[email protected]640517f2008-10-30 23:54:041396 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381397
1398 HRESULT result;
1399 IShellLink *shell = NULL;
1400 IPersistFile *persist = NULL;
1401
1402 CoInitialize(NULL);
1403 // Temporarily create a shortcut for test
1404 result = CoCreateInstance(CLSID_ShellLink, NULL,
1405 CLSCTX_INPROC_SERVER, IID_IShellLink,
1406 reinterpret_cast<LPVOID*>(&shell));
1407 EXPECT_TRUE(SUCCEEDED(result));
1408 result = shell->QueryInterface(IID_IPersistFile,
1409 reinterpret_cast<LPVOID*>(&persist));
1410 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041411 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381412 EXPECT_TRUE(SUCCEEDED(result));
1413 result = shell->SetDescription(L"ResolveShortcutTest");
1414 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041415 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381416 EXPECT_TRUE(SUCCEEDED(result));
1417 if (persist)
1418 persist->Release();
1419 if (shell)
1420 shell->Release();
1421
1422 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441423 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381424 EXPECT_TRUE(is_solved);
1425 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441426 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381427 EXPECT_EQ(L"This is the target.", contents);
1428
[email protected]d324ab332008-08-18 16:00:381429 // Cleaning
[email protected]640517f2008-10-30 23:54:041430 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441431 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381432 CoUninitialize();
1433}
1434
1435TEST_F(FileUtilTest, CreateShortcutTest) {
1436 const wchar_t file_contents[] = L"This is another target.";
[email protected]640517f2008-10-30 23:54:041437 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381438 CreateTextFile(target_file, file_contents);
1439
[email protected]640517f2008-10-30 23:54:041440 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381441
1442 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041443 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1444 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501445 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441446 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381447 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441448 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381449 EXPECT_EQ(file_contents, read_contents);
1450
[email protected]640517f2008-10-30 23:54:041451 DeleteFile(target_file.value().c_str());
1452 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381453 CoUninitialize();
1454}
[email protected]2c59af7dca2009-03-11 18:37:481455
1456TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1457 // Create a directory
1458 FilePath dir_name_from =
1459 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1460 file_util::CreateDirectory(dir_name_from);
1461 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1462
1463 // Create a file under the directory
1464 FilePath file_name_from =
1465 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1466 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1467 ASSERT_TRUE(file_util::PathExists(file_name_from));
1468
1469 // Move the directory by using CopyAndDeleteDirectory
1470 FilePath dir_name_to = test_dir_.Append(
1471 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1472 FilePath file_name_to =
1473 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1474
1475 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1476
1477 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1478
1479 // Check everything has been moved.
1480 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1481 EXPECT_FALSE(file_util::PathExists(file_name_from));
1482 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1483 EXPECT_TRUE(file_util::PathExists(file_name_to));
1484}
[email protected]7d95aae2009-10-09 07:33:391485
1486TEST_F(FileUtilTest, GetTempDirTest) {
1487 static const TCHAR* kTmpKey = _T("TMP");
1488 static const TCHAR* kTmpValues[] = {
1489 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1490 };
1491 // Save the original $TMP.
1492 size_t original_tmp_size;
1493 TCHAR* original_tmp;
1494 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1495 // original_tmp may be NULL.
1496
1497 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1498 FilePath path;
1499 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1500 file_util::GetTempDir(&path);
1501 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1502 " result=" << path.value();
1503 }
1504
1505 // Restore the original $TMP.
1506 if (original_tmp) {
1507 ::_tputenv_s(kTmpKey, original_tmp);
1508 free(original_tmp);
1509 } else {
1510 ::_tputenv_s(kTmpKey, _T(""));
1511 }
1512}
1513#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381514
[email protected]33edeab2009-08-18 16:07:551515TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1516 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391517 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551518 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391519 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1520 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1521 }
1522 for (int i = 0; i < 3; i++)
1523 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1524 for (int i = 0; i < 3; i++)
1525 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1526}
1527
[email protected]33edeab2009-08-18 16:07:551528TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391529 FilePath names[3];
1530 FILE *fps[3];
1531 int i;
1532
1533 // Create; make sure they are open and exist.
1534 for (i = 0; i < 3; ++i) {
1535 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1536 ASSERT_TRUE(fps[i]);
1537 EXPECT_TRUE(file_util::PathExists(names[i]));
1538 }
1539
1540 // Make sure all names are unique.
1541 for (i = 0; i < 3; ++i) {
1542 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1543 }
1544
1545 // Close and delete.
1546 for (i = 0; i < 3; ++i) {
1547 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1548 EXPECT_TRUE(file_util::Delete(names[i], false));
1549 }
initial.commitd7cae122008-07-26 21:49:381550}
1551
1552TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141553 FilePath temp_dir;
1554 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1555 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111556 EXPECT_TRUE(file_util::PathExists(temp_dir));
1557 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381558}
1559
[email protected]b0b3abd92010-04-30 17:00:091560TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1561 FilePath new_dir;
1562 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1563 test_dir_,
1564 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1565 &new_dir));
1566 EXPECT_TRUE(file_util::PathExists(new_dir));
1567 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1568 EXPECT_TRUE(file_util::Delete(new_dir, false));
1569}
1570
[email protected]9e51af92009-02-04 00:58:391571TEST_F(FileUtilTest, GetShmemTempDirTest) {
1572 FilePath dir;
1573 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1574 EXPECT_TRUE(file_util::DirectoryExists(dir));
1575}
1576
initial.commitd7cae122008-07-26 21:49:381577TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041578 FilePath test_root =
1579 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131580#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041581 FilePath test_path =
1582 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101583#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041584 FilePath test_path =
1585 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101586#endif
[email protected]806b9c62008-09-11 16:09:111587
1588 EXPECT_FALSE(file_util::PathExists(test_path));
1589 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1590 EXPECT_TRUE(file_util::PathExists(test_path));
1591 // CreateDirectory returns true if the DirectoryExists returns true.
1592 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1593
1594 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041595 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111596 EXPECT_FALSE(file_util::PathExists(test_path));
1597 CreateTextFile(test_path, L"test file");
1598 EXPECT_TRUE(file_util::PathExists(test_path));
1599 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1600
1601 EXPECT_TRUE(file_util::Delete(test_root, true));
1602 EXPECT_FALSE(file_util::PathExists(test_root));
1603 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411604
1605 // Verify assumptions made by the Windows implementation:
1606 // 1. The current directory always exists.
1607 // 2. The root directory always exists.
1608 ASSERT_TRUE(file_util::DirectoryExists(
1609 FilePath(FilePath::kCurrentDirectory)));
1610 FilePath top_level = test_root;
1611 while (top_level != top_level.DirName()) {
1612 top_level = top_level.DirName();
1613 }
1614 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1615
1616 // Given these assumptions hold, it should be safe to
1617 // test that "creating" these directories succeeds.
1618 EXPECT_TRUE(file_util::CreateDirectory(
1619 FilePath(FilePath::kCurrentDirectory)));
1620 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401621
1622#if defined(OS_WIN)
1623 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1624 FilePath invalid_path =
1625 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1626 if (!file_util::PathExists(invalid_drive)) {
1627 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1628 }
1629#endif
[email protected]806b9c62008-09-11 16:09:111630}
1631
1632TEST_F(FileUtilTest, DetectDirectoryTest) {
1633 // Check a directory
[email protected]640517f2008-10-30 23:54:041634 FilePath test_root =
1635 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111636 EXPECT_FALSE(file_util::PathExists(test_root));
1637 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1638 EXPECT_TRUE(file_util::PathExists(test_root));
1639 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1640
1641 // Check a file
[email protected]640517f2008-10-30 23:54:041642 FilePath test_path =
1643 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111644 EXPECT_FALSE(file_util::PathExists(test_path));
1645 CreateTextFile(test_path, L"test file");
1646 EXPECT_TRUE(file_util::PathExists(test_path));
1647 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1648 EXPECT_TRUE(file_util::Delete(test_path, false));
1649
1650 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381651}
1652
initial.commitd7cae122008-07-26 21:49:381653TEST_F(FileUtilTest, FileEnumeratorTest) {
1654 // Test an empty directory.
[email protected]8199b3a2009-06-09 05:57:381655 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121656 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1657 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381658
[email protected]8199b3a2009-06-09 05:57:381659 // Test an empty directory, non-recursively, including "..".
1660 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1661 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1662 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1663 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1664 f0_dotdot.Next().value());
1665 EXPECT_EQ(FILE_PATH_LITERAL(""),
1666 f0_dotdot.Next().value());
1667
[email protected]37088fef2008-08-15 17:32:101668 // create the directories
[email protected]640517f2008-10-30 23:54:041669 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101670 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]640517f2008-10-30 23:54:041671 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101672 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041673 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101674 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041675
[email protected]37088fef2008-08-15 17:32:101676 // create the files
[email protected]640517f2008-10-30 23:54:041677 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101678 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041679 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101680 CreateTextFile(dir2innerfile, L"");
[email protected]640517f2008-10-30 23:54:041681 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101682 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041683 FilePath file2_rel =
1684 dir2.Append(FilePath::kParentDirectory)
1685 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101686 CreateTextFile(file2_rel, L"");
[email protected]640517f2008-10-30 23:54:041687 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381688
1689 // Only enumerate files.
[email protected]0b733222008-12-11 14:55:121690 file_util::FileEnumerator f1(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381691 file_util::FileEnumerator::FILES);
1692 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101693 EXPECT_TRUE(c1.HasFile(file1));
1694 EXPECT_TRUE(c1.HasFile(file2_abs));
1695 EXPECT_TRUE(c1.HasFile(dir2file));
1696 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1697 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381698
1699 // Only enumerate directories.
[email protected]0b733222008-12-11 14:55:121700 file_util::FileEnumerator f2(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381701 file_util::FileEnumerator::DIRECTORIES);
1702 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101703 EXPECT_TRUE(c2.HasFile(dir1));
1704 EXPECT_TRUE(c2.HasFile(dir2));
1705 EXPECT_TRUE(c2.HasFile(dir2inner));
1706 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381707
[email protected]3bc9ffaf2008-10-16 02:42:451708 // Only enumerate directories non-recursively.
1709 file_util::FileEnumerator f2_non_recursive(
[email protected]0b733222008-12-11 14:55:121710 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451711 FindResultCollector c2_non_recursive(f2_non_recursive);
1712 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1713 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1714 EXPECT_EQ(c2_non_recursive.size(), 2);
1715
[email protected]8199b3a2009-06-09 05:57:381716 // Only enumerate directories, non-recursively, including "..".
1717 file_util::FileEnumerator f2_dotdot(
1718 test_dir_, false,
1719 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1720 file_util::FileEnumerator::DIRECTORIES |
1721 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1722 FindResultCollector c2_dotdot(f2_dotdot);
1723 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1724 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1725 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1726 EXPECT_EQ(c2_dotdot.size(), 3);
1727
initial.commitd7cae122008-07-26 21:49:381728 // Enumerate files and directories.
[email protected]8199b3a2009-06-09 05:57:381729 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381730 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101731 EXPECT_TRUE(c3.HasFile(dir1));
1732 EXPECT_TRUE(c3.HasFile(dir2));
1733 EXPECT_TRUE(c3.HasFile(file1));
1734 EXPECT_TRUE(c3.HasFile(file2_abs));
1735 EXPECT_TRUE(c3.HasFile(dir2file));
1736 EXPECT_TRUE(c3.HasFile(dir2inner));
1737 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1738 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381739
1740 // Non-recursive operation.
[email protected]8199b3a2009-06-09 05:57:381741 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381742 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101743 EXPECT_TRUE(c4.HasFile(dir2));
1744 EXPECT_TRUE(c4.HasFile(dir2));
1745 EXPECT_TRUE(c4.HasFile(file1));
1746 EXPECT_TRUE(c4.HasFile(file2_abs));
1747 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381748
1749 // Enumerate with a pattern.
[email protected]8199b3a2009-06-09 05:57:381750 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121751 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381752 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101753 EXPECT_TRUE(c5.HasFile(dir1));
1754 EXPECT_TRUE(c5.HasFile(dir2));
1755 EXPECT_TRUE(c5.HasFile(dir2file));
1756 EXPECT_TRUE(c5.HasFile(dir2inner));
1757 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1758 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381759
1760 // Make sure the destructor closes the find handle while in the middle of a
1761 // query to allow TearDown to delete the directory.
[email protected]8199b3a2009-06-09 05:57:381762 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121763 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1764 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381765}
license.botbf09a502008-08-24 00:55:551766
[email protected]ee5c29da2009-01-09 22:14:271767TEST_F(FileUtilTest, Contains) {
[email protected]11e53f62009-03-10 18:20:441768 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271769
1770 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571771 if (file_util::PathExists(data_dir)) {
1772 ASSERT_TRUE(file_util::Delete(data_dir, true));
1773 }
[email protected]ee5c29da2009-01-09 22:14:271774 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1775
1776 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1777 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1778 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1779 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1780
1781 // Annoyingly, the directories must actually exist in order for realpath(),
1782 // which Contains() relies on in posix, to work.
1783 ASSERT_TRUE(file_util::CreateDirectory(foo));
1784 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581785 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1786 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1787 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271788
1789 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1790 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1791 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1792 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1793
[email protected]e43eddf12009-12-29 00:32:521794 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271795 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1796#if defined(OS_WIN)
1797 EXPECT_TRUE(file_util::ContainsPath(foo,
1798 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391799 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271800 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521801#elif defined(OS_MACOSX)
1802 // We can't really do this test on OS X since the case-sensitivity of the
1803 // filesystem is configurable.
1804#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271805 EXPECT_FALSE(file_util::ContainsPath(foo,
1806 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271807#endif
1808}
1809
[email protected]ec3d1452010-02-18 10:02:261810TEST_F(FileUtilTest, LastModified) {
1811 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1812
1813 // Create a fresh, empty copy of this directory.
1814 if (file_util::PathExists(data_dir)) {
1815 ASSERT_TRUE(file_util::Delete(data_dir, true));
1816 }
1817 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1818
1819 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1820 std::string data("hello");
1821 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1822
1823 base::Time modification_time;
1824 // Note that this timestamp is divisible by two (seconds) - FAT stores
1825 // modification times with 2s resolution.
1826 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1827 &modification_time));
1828 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
1829 file_util::FileInfo file_info;
1830 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1831 ASSERT_TRUE(file_info.last_modified == modification_time);
1832}
1833
[email protected]b33f1d92010-05-26 01:40:121834TEST_F(FileUtilTest, IsDirectoryEmpty) {
1835 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1836
1837 ASSERT_FALSE(file_util::PathExists(empty_dir));
1838
1839 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1840
1841 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1842
1843 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1844 std::string bar("baz");
1845 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1846
1847 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1848}
1849
[email protected]11b901ee2008-09-10 00:16:281850} // namespace