blob: fe3927d9915937aebd8bd22e745dc6a7154b92cf [file] [log] [blame]
[email protected]b90d7e802011-01-09 16:32:201// Copyright (c) 2011 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>
initial.commitd7cae122008-07-26 21:49:389#include <shellapi.h>
10#include <shlobj.h>
[email protected]7d95aae2009-10-09 07:33:3911#include <tchar.h>
[email protected]e0785902011-05-19 23:34:1712#include <winioctl.h>
[email protected]37088fef2008-08-15 17:32:1013#endif
initial.commitd7cae122008-07-26 21:49:3814
15#include <fstream>
[email protected]37088fef2008-08-15 17:32:1016#include <set>
initial.commitd7cae122008-07-26 21:49:3817
18#include "base/base_paths.h"
[email protected]640517f2008-10-30 23:54:0419#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3820#include "base/file_util.h"
initial.commitd7cae122008-07-26 21:49:3821#include "base/path_service.h"
[email protected]e0785902011-05-19 23:34:1722#include "base/scoped_temp_dir.h"
[email protected]ce072a72010-12-31 20:02:1623#include "base/threading/platform_thread.h"
[email protected]85786f92009-04-18 00:42:4824#include "base/time.h"
[email protected]047a03f2009-10-07 02:10:2025#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3826#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1527#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3828
[email protected]b90d7e802011-01-09 16:32:2029#if defined(OS_WIN)
30#include "base/win/scoped_handle.h"
31#endif
32
[email protected]4e9f6be2009-04-03 17:17:5833// This macro helps avoid wrapped lines in the test structs.
34#define FPL(x) FILE_PATH_LITERAL(x)
35
initial.commitd7cae122008-07-26 21:49:3836namespace {
37
[email protected]6f5f4322010-06-09 22:56:4838// To test that file_util::Normalize FilePath() deals with NTFS reparse points
39// correctly, we need functions to create and delete reparse points.
40#if defined(OS_WIN)
41typedef struct _REPARSE_DATA_BUFFER {
42 ULONG ReparseTag;
43 USHORT ReparseDataLength;
44 USHORT Reserved;
45 union {
46 struct {
47 USHORT SubstituteNameOffset;
48 USHORT SubstituteNameLength;
49 USHORT PrintNameOffset;
50 USHORT PrintNameLength;
51 ULONG Flags;
52 WCHAR PathBuffer[1];
53 } SymbolicLinkReparseBuffer;
54 struct {
55 USHORT SubstituteNameOffset;
56 USHORT SubstituteNameLength;
57 USHORT PrintNameOffset;
58 USHORT PrintNameLength;
59 WCHAR PathBuffer[1];
60 } MountPointReparseBuffer;
61 struct {
62 UCHAR DataBuffer[1];
63 } GenericReparseBuffer;
64 };
65} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
66
67// Sets a reparse point. |source| will now point to |target|. Returns true if
68// the call succeeds, false otherwise.
69bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
70 std::wstring kPathPrefix = L"\\??\\";
71 std::wstring target_str;
72 // The juction will not work if the target path does not start with \??\ .
73 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
74 target_str += kPathPrefix;
75 target_str += target_path.value();
76 const wchar_t* target = target_str.c_str();
77 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
78 char buffer[2000] = {0};
79 DWORD returned;
80
81 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
82
83 data->ReparseTag = 0xa0000003;
84 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
85
86 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
87 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
88 data->ReparseDataLength = size_target + 4 + 8;
89
90 int data_size = data->ReparseDataLength + 8;
91
92 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
93 NULL, 0, &returned, NULL)) {
94 return false;
95 }
96 return true;
97}
98
99// Delete the reparse point referenced by |source|. Returns true if the call
100// succeeds, false otherwise.
101bool DeleteReparsePoint(HANDLE source) {
102 DWORD returned;
103 REPARSE_DATA_BUFFER data = {0};
104 data.ReparseTag = 0xa0000003;
105 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
106 &returned, NULL)) {
107 return false;
108 }
109 return true;
110}
111#endif
112
[email protected]83d86252010-05-07 18:58:45113const wchar_t bogus_content[] = L"I'm cannon fodder.";
114
[email protected]8199b3a2009-06-09 05:57:38115const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
116 static_cast<file_util::FileEnumerator::FILE_TYPE>(
117 file_util::FileEnumerator::FILES |
118 file_util::FileEnumerator::DIRECTORIES);
119
[email protected]ed2f2332008-08-20 15:59:49120// file_util winds up using autoreleased objects on the Mac, so this needs
121// to be a PlatformTest
122class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:38123 protected:
124 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:49125 PlatformTest::SetUp();
[email protected]90314c0a82010-09-15 20:40:47126 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commitd7cae122008-07-26 21:49:38127 }
128
[email protected]90314c0a82010-09-15 20:40:47129 ScopedTempDir temp_dir_;
initial.commitd7cae122008-07-26 21:49:38130};
131
132// Collects all the results from the given file enumerator, and provides an
133// interface to query whether a given file is present.
134class FindResultCollector {
135 public:
[email protected]53c58042009-08-26 20:00:14136 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
[email protected]0b733222008-12-11 14:55:12137 FilePath cur_file;
138 while (!(cur_file = enumerator.Next()).value().empty()) {
139 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:38140 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:04141 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:38142 << "Same file returned twice";
143
144 // Save for later.
[email protected]640517f2008-10-30 23:54:04145 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:38146 }
147 }
148
149 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:04150 bool HasFile(const FilePath& file) const {
151 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:38152 }
[email protected]640517f2008-10-30 23:54:04153
[email protected]37088fef2008-08-15 17:32:10154 int size() {
[email protected]f35d39b2008-08-15 17:50:10155 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:10156 }
initial.commitd7cae122008-07-26 21:49:38157
158 private:
[email protected]640517f2008-10-30 23:54:04159 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:38160};
161
162// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:04163void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:38164 const std::wstring& contents) {
[email protected]76c551c2011-03-25 20:49:54165 std::wofstream file;
[email protected]8a205c02011-02-04 20:41:33166 file.open(filename.value().c_str());
initial.commitd7cae122008-07-26 21:49:38167 ASSERT_TRUE(file.is_open());
168 file << contents;
169 file.close();
170}
171
172// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:04173std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:10174 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38175 std::wifstream file;
[email protected]8a205c02011-02-04 20:41:33176 file.open(filename.value().c_str());
initial.commitd7cae122008-07-26 21:49:38177 EXPECT_TRUE(file.is_open());
[email protected]76c551c2011-03-25 20:49:54178 file.getline(contents, arraysize(contents));
initial.commitd7cae122008-07-26 21:49:38179 file.close();
180 return std::wstring(contents);
181}
182
[email protected]8541bb82008-08-15 17:45:13183#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38184uint64 FileTimeAsUint64(const FILETIME& ft) {
185 ULARGE_INTEGER u;
186 u.LowPart = ft.dwLowDateTime;
187 u.HighPart = ft.dwHighDateTime;
188 return u.QuadPart;
189}
[email protected]37088fef2008-08-15 17:32:10190#endif
initial.commitd7cae122008-07-26 21:49:38191
192const struct append_case {
193 const wchar_t* path;
194 const wchar_t* ending;
195 const wchar_t* result;
196} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13197#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38198 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
199 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
200 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
201 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
202 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
203 {L"", L"path", L"\\path"},
204 {L"", L"", L"\\"},
[email protected]37088fef2008-08-15 17:32:10205#elif defined(OS_POSIX)
206 {L"/foo/bar", L"path", L"/foo/bar/path"},
207 {L"/foo/bar/", L"path", L"/foo/bar/path"},
208 {L"/foo/bar//", L"path", L"/foo/bar//path"},
209 {L"/foo/bar/", L"", L"/foo/bar/"},
210 {L"/foo/bar", L"", L"/foo/bar/"},
211 {L"", L"path", L"/path"},
212 {L"", L"", L"/"},
213#endif
initial.commitd7cae122008-07-26 21:49:38214};
215
[email protected]1840cfc2010-02-26 15:11:55216#if defined(OS_WIN)
217// This function is deprecated, but still used on Windows.
initial.commitd7cae122008-07-26 21:49:38218TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10219 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38220 const append_case& value = append_cases[i];
221 std::wstring result = value.path;
222 file_util::AppendToPath(&result, value.ending);
223 EXPECT_EQ(value.result, result);
224 }
225
226#ifdef NDEBUG
227 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
228#endif
229}
[email protected]1840cfc2010-02-26 15:11:55230#endif // defined(OS_WIN)
231
initial.commitd7cae122008-07-26 21:49:38232static const struct filename_case {
233 const wchar_t* path;
234 const wchar_t* filename;
235} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13236#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38237 {L"c:\\colon\\backslash", L"backslash"},
238 {L"c:\\colon\\backslash\\", L""},
239 {L"\\\\filename.exe", L"filename.exe"},
240 {L"filename.exe", L"filename.exe"},
241 {L"", L""},
242 {L"\\\\\\", L""},
243 {L"c:/colon/backslash", L"backslash"},
244 {L"c:/colon/backslash/", L""},
245 {L"//////", L""},
246 {L"///filename.exe", L"filename.exe"},
[email protected]37088fef2008-08-15 17:32:10247#elif defined(OS_POSIX)
248 {L"/foo/bar", L"bar"},
249 {L"/foo/bar/", L""},
250 {L"/filename.exe", L"filename.exe"},
251 {L"filename.exe", L"filename.exe"},
252 {L"", L""},
253 {L"/", L""},
254#endif
initial.commitd7cae122008-07-26 21:49:38255};
256
initial.commitd7cae122008-07-26 21:49:38257// Test finding the file type from a path name
258static const struct extension_case {
259 const wchar_t* path;
260 const wchar_t* extension;
261} extension_cases[] = {
[email protected]8541bb82008-08-15 17:45:13262#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38263 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
264 {L"C:\\colon\\backslash\\filename.", L""},
265 {L"C:\\colon\\backslash\\filename", L""},
266 {L"C:\\colon\\backslash\\", L""},
267 {L"C:\\colon\\backslash.\\", L""},
268 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
[email protected]37088fef2008-08-15 17:32:10269#elif defined(OS_POSIX)
270 {L"/foo/bar/filename.extension", L"extension"},
271 {L"/foo/bar/filename.", L""},
272 {L"/foo/bar/filename", L""},
273 {L"/foo/bar/", L""},
274 {L"/foo/bar./", L""},
275 {L"/foo/bar/filename.extension.extension2", L"extension2"},
276 {L".", L""},
277 {L"..", L""},
278 {L"./foo", L""},
279 {L"./foo.extension", L"extension"},
280 {L"/foo.extension1/bar.extension2", L"extension2"},
281#endif
initial.commitd7cae122008-07-26 21:49:38282};
283
[email protected]63597e4e2010-07-08 17:49:05284#if defined(OS_WIN)
285// This function has been deprecated on non-Windows.
initial.commitd7cae122008-07-26 21:49:38286TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10287 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38288 const extension_case& ext = extension_cases[i];
289 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
290 EXPECT_EQ(ext.extension, fext);
291 }
292}
[email protected]63597e4e2010-07-08 17:49:05293#endif
initial.commitd7cae122008-07-26 21:49:38294
295// Test finding the directory component of a path
296static const struct dir_case {
297 const wchar_t* full_path;
298 const wchar_t* directory;
299} dir_cases[] = {
[email protected]8541bb82008-08-15 17:45:13300#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38301 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
302 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
303 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
304 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
305 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
306 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
[email protected]2c8088a42009-10-15 05:00:25307 {L"C:\\", L"C:\\"},
[email protected]37088fef2008-08-15 17:32:10308#elif defined(OS_POSIX)
309 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
310 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
311 {L"/foo/bar/", L"/foo/bar"},
312 {L"/foo/bar//", L"/foo/bar"},
313 {L"/foo/bar", L"/foo"},
314 {L"/foo/bar./", L"/foo/bar."},
315 {L"/", L"/"},
316 {L".", L"."},
[email protected]53c58042009-08-26 20:00:14317 {L"..", L"."}, // yes, ".." technically lives in "."
[email protected]37088fef2008-08-15 17:32:10318#endif
initial.commitd7cae122008-07-26 21:49:38319};
320
[email protected]2ec79f72010-06-10 13:05:26321// Flaky, http://crbug.com/46246
[email protected]552b3152010-06-10 12:40:52322TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commitd7cae122008-07-26 21:49:38323 // Create old file (that we don't want to count)
[email protected]90314c0a82010-09-15 20:40:47324 FilePath old_file_name =
325 temp_dir_.path().Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commitd7cae122008-07-26 21:49:38326 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
327
328 // Age to perfection
[email protected]4b7743de2009-04-21 01:50:39329#if defined(OS_WIN)
[email protected]ce072a72010-12-31 20:02:16330 base::PlatformThread::Sleep(100);
[email protected]4b7743de2009-04-21 01:50:39331#elif defined(OS_POSIX)
332 // We need to wait at least one second here because the precision of
333 // file creation time is one second.
[email protected]ce072a72010-12-31 20:02:16334 base::PlatformThread::Sleep(1500);
[email protected]4b7743de2009-04-21 01:50:39335#endif
initial.commitd7cae122008-07-26 21:49:38336
337 // Establish our cutoff time
[email protected]85786f92009-04-18 00:42:48338 base::Time now(base::Time::NowFromSystemTime());
[email protected]90314c0a82010-09-15 20:40:47339 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38340
341 // Create a new file (that we do want to count)
[email protected]90314c0a82010-09-15 20:40:47342 FilePath new_file_name =
343 temp_dir_.path().Append(FILE_PATH_LITERAL("New File.txt"));
initial.commitd7cae122008-07-26 21:49:38344 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
345
346 // We should see only the new file.
[email protected]90314c0a82010-09-15 20:40:47347 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38348
349 // Delete new file, we should see no files after cutoff now
350 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]90314c0a82010-09-15 20:40:47351 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38352}
353
[email protected]c2c132c62010-03-24 21:56:26354TEST_F(FileUtilTest, FileAndDirectorySize) {
355 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
356 // should return 53 bytes.
[email protected]90314c0a82010-09-15 20:40:47357 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
[email protected]c2c132c62010-03-24 21:56:26358 CreateTextFile(file_01, L"12345678901234567890");
359 int64 size_f1 = 0;
360 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
361 EXPECT_EQ(20ll, size_f1);
362
[email protected]90314c0a82010-09-15 20:40:47363 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
[email protected]c2c132c62010-03-24 21:56:26364 file_util::CreateDirectory(subdir_path);
365
366 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
367 CreateTextFile(file_02, L"123456789012345678901234567890");
368 int64 size_f2 = 0;
369 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
370 EXPECT_EQ(30ll, size_f2);
371
372 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
373 file_util::CreateDirectory(subsubdir_path);
374
375 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
376 CreateTextFile(file_03, L"123");
377
[email protected]90314c0a82010-09-15 20:40:47378 int64 computed_size = file_util::ComputeDirectorySize(temp_dir_.path());
[email protected]c2c132c62010-03-24 21:56:26379 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
[email protected]a04876b92010-06-11 22:53:43380
[email protected]90314c0a82010-09-15 20:40:47381 computed_size =
382 file_util::ComputeFilesSize(temp_dir_.path(), FPL("The file*"));
[email protected]a04876b92010-06-11 22:53:43383 EXPECT_EQ(size_f1, computed_size);
384
[email protected]90314c0a82010-09-15 20:40:47385 computed_size = file_util::ComputeFilesSize(temp_dir_.path(), FPL("bla*"));
[email protected]a04876b92010-06-11 22:53:43386 EXPECT_EQ(0, computed_size);
[email protected]c2c132c62010-03-24 21:56:26387}
388
[email protected]6f5f4322010-06-09 22:56:48389TEST_F(FileUtilTest, NormalizeFilePathBasic) {
390 // Create a directory under the test dir. Because we create it,
391 // we know it is not a link.
[email protected]90314c0a82010-09-15 20:40:47392 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
393 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
[email protected]6f5f4322010-06-09 22:56:48394 FilePath file_b_path = dir_path.Append(FPL("file_b"));
395 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57396
[email protected]6f5f4322010-06-09 22:56:48397 FilePath normalized_file_a_path, normalized_file_b_path;
398 ASSERT_FALSE(file_util::PathExists(file_a_path));
399 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
400 &normalized_file_a_path))
[email protected]e7afe2452010-08-22 16:19:13401 << "NormalizeFilePath() should fail on nonexistent paths.";
[email protected]6f5f4322010-06-09 22:56:48402
403 CreateTextFile(file_a_path, bogus_content);
404 ASSERT_TRUE(file_util::PathExists(file_a_path));
405 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
406 &normalized_file_a_path));
407
408 CreateTextFile(file_b_path, bogus_content);
409 ASSERT_TRUE(file_util::PathExists(file_b_path));
410 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
411 &normalized_file_b_path));
412
413 // Beacuse this test created |dir_path|, we know it is not a link
414 // or junction. So, the real path of the directory holding file a
415 // must be the parent of the path holding file b.
416 ASSERT_TRUE(normalized_file_a_path.DirName()
417 .IsParent(normalized_file_b_path.DirName()));
418}
419
420#if defined(OS_WIN)
421
422TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
423 // Build the following directory structure:
424 //
[email protected]90314c0a82010-09-15 20:40:47425 // temp_dir
[email protected]6f5f4322010-06-09 22:56:48426 // |-> base_a
427 // | |-> sub_a
428 // | |-> file.txt
429 // | |-> long_name___... (Very long name.)
430 // | |-> sub_long
431 // | |-> deep.txt
432 // |-> base_b
[email protected]90314c0a82010-09-15 20:40:47433 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
434 // |-> to_base_b (reparse point to temp_dir\base_b)
435 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
[email protected]6f5f4322010-06-09 22:56:48436
[email protected]90314c0a82010-09-15 20:40:47437 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
[email protected]6f5f4322010-06-09 22:56:48438 ASSERT_TRUE(file_util::CreateDirectory(base_a));
439
440 FilePath sub_a = base_a.Append(FPL("sub_a"));
441 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
442
443 FilePath file_txt = sub_a.Append(FPL("file.txt"));
444 CreateTextFile(file_txt, bogus_content);
445
446 // Want a directory whose name is long enough to make the path to the file
447 // inside just under MAX_PATH chars. This will be used to test that when
448 // a junction expands to a path over MAX_PATH chars in length,
449 // NormalizeFilePath() fails without crashing.
450 FilePath sub_long_rel(FPL("sub_long"));
451 FilePath deep_txt(FPL("deep.txt"));
452
453 int target_length = MAX_PATH;
454 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
455 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52456 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48457 // the resulting path is still long enough to hit the failing case in
458 // NormalizePath().
459 const int kCreateDirLimit = 4;
460 target_length -= kCreateDirLimit;
461 FilePath::StringType long_name_str = FPL("long_name_");
462 long_name_str.resize(target_length, '_');
463
464 FilePath long_name = sub_a.Append(FilePath(long_name_str));
465 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
466 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
467
468 FilePath sub_long = deep_file.DirName();
469 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
470 CreateTextFile(deep_file, bogus_content);
471
[email protected]90314c0a82010-09-15 20:40:47472 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
[email protected]6f5f4322010-06-09 22:56:48473 ASSERT_TRUE(file_util::CreateDirectory(base_b));
474
475 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
476 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
[email protected]b90d7e802011-01-09 16:32:20477 base::win::ScopedHandle reparse_to_sub_a(
[email protected]6f5f4322010-06-09 22:56:48478 ::CreateFile(to_sub_a.value().c_str(),
479 FILE_ALL_ACCESS,
480 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
481 NULL,
482 OPEN_EXISTING,
483 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
484 NULL));
[email protected]094f9762010-08-03 03:51:56485 ASSERT_TRUE(reparse_to_sub_a.IsValid());
[email protected]6f5f4322010-06-09 22:56:48486 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
487
488 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
489 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
[email protected]b90d7e802011-01-09 16:32:20490 base::win::ScopedHandle reparse_to_base_b(
[email protected]6f5f4322010-06-09 22:56:48491 ::CreateFile(to_base_b.value().c_str(),
492 FILE_ALL_ACCESS,
493 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
494 NULL,
495 OPEN_EXISTING,
496 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
497 NULL));
[email protected]094f9762010-08-03 03:51:56498 ASSERT_TRUE(reparse_to_base_b.IsValid());
[email protected]6f5f4322010-06-09 22:56:48499 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
500
501 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
502 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
[email protected]b90d7e802011-01-09 16:32:20503 base::win::ScopedHandle reparse_to_sub_long(
[email protected]6f5f4322010-06-09 22:56:48504 ::CreateFile(to_sub_long.value().c_str(),
505 FILE_ALL_ACCESS,
506 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
507 NULL,
508 OPEN_EXISTING,
509 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
510 NULL));
[email protected]094f9762010-08-03 03:51:56511 ASSERT_TRUE(reparse_to_sub_long.IsValid());
[email protected]6f5f4322010-06-09 22:56:48512 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
513
514 // Normalize a junction free path: base_a\sub_a\file.txt .
515 FilePath normalized_path;
516 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
517 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
518
519 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
520 // the junction to_sub_a.
521 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
522 &normalized_path));
523 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
524
525 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
526 // normalized to exclude junctions to_base_b and to_sub_a .
527 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
528 .Append(FPL("to_base_b"))
529 .Append(FPL("to_sub_a"))
530 .Append(FPL("file.txt")),
531 &normalized_path));
532 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
533
534 // A long enough path will cause NormalizeFilePath() to fail. Make a long
535 // path using to_base_b many times, and check that paths long enough to fail
536 // do not cause a crash.
537 FilePath long_path = base_b;
538 const int kLengthLimit = MAX_PATH + 200;
539 while (long_path.value().length() <= kLengthLimit) {
540 long_path = long_path.Append(FPL("to_base_b"));
541 }
542 long_path = long_path.Append(FPL("to_sub_a"))
543 .Append(FPL("file.txt"));
544
545 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
546
547 // Normalizing the junction to deep.txt should fail, because the expanded
548 // path to deep.txt is longer than MAX_PATH.
549 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
550 &normalized_path));
551
552 // Delete the reparse points, and see that NormalizeFilePath() fails
553 // to traverse them.
554 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
555 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
556 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
557
558 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
559 &normalized_path));
560}
561
562#endif // defined(OS_WIN)
563
[email protected]2e733d102010-11-30 00:43:37564#if defined(OS_POSIX)
565
566TEST_F(FileUtilTest, CreateAndReadSymlinks) {
567 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
568 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
569 CreateTextFile(link_to, bogus_content);
570
571 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
572 << "Failed to create file symlink.";
573
574 // If we created the link properly, we should be able to read the
575 // contents through it.
576 std::wstring contents = ReadTextFile(link_from);
577 ASSERT_EQ(contents, bogus_content);
578
579 FilePath result;
580 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from, &result));
581 ASSERT_EQ(link_to.value(), result.value());
582
583 // Link to a directory.
584 link_from = temp_dir_.path().Append(FPL("from_dir"));
585 link_to = temp_dir_.path().Append(FPL("to_dir"));
586 file_util::CreateDirectory(link_to);
587
588 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
589 << "Failed to create directory symlink.";
590
591 // Test failures.
592 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to));
593 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result));
594 FilePath missing = temp_dir_.path().Append(FPL("missing"));
595 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result));
596}
597
598
[email protected]6f5f4322010-06-09 22:56:48599// The following test of NormalizeFilePath() require that we create a symlink.
[email protected]2e733d102010-11-30 00:43:37600// This can not be done on Windows before Vista. On Vista, creating a symlink
[email protected]6f5f4322010-06-09 22:56:48601// requires privilege "SeCreateSymbolicLinkPrivilege".
602// TODO(skerner): Investigate the possibility of giving base_unittests the
603// privileges required to create a symlink.
[email protected]6f5f4322010-06-09 22:56:48604TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
605 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57606
607 // Link one file to another.
[email protected]90314c0a82010-09-15 20:40:47608 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
609 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57610 CreateTextFile(link_to, bogus_content);
611
[email protected]2e733d102010-11-30 00:43:37612 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57613 << "Failed to create file symlink.";
614
[email protected]6f5f4322010-06-09 22:56:48615 // Check that NormalizeFilePath sees the link.
616 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57617 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48618 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
619 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57620
621 // Link to a directory.
[email protected]90314c0a82010-09-15 20:40:47622 link_from = temp_dir_.path().Append(FPL("from_dir"));
623 link_to = temp_dir_.path().Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57624 file_util::CreateDirectory(link_to);
625
[email protected]2e733d102010-11-30 00:43:37626 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57627 << "Failed to create directory symlink.";
628
[email protected]6f5f4322010-06-09 22:56:48629 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
630 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57631
[email protected]6f5f4322010-06-09 22:56:48632 // Test that a loop in the links causes NormalizeFilePath() to return false.
[email protected]90314c0a82010-09-15 20:40:47633 link_from = temp_dir_.path().Append(FPL("link_a"));
634 link_to = temp_dir_.path().Append(FPL("link_b"));
[email protected]2e733d102010-11-30 00:43:37635 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57636 << "Failed to create loop symlink a.";
[email protected]2e733d102010-11-30 00:43:37637 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57638 << "Failed to create loop symlink b.";
639
640 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48641 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57642}
643#endif // defined(OS_POSIX)
644
[email protected]83d86252010-05-07 18:58:45645TEST_F(FileUtilTest, DeleteNonExistent) {
[email protected]90314c0a82010-09-15 20:40:47646 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
[email protected]83d86252010-05-07 18:58:45647 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36648
[email protected]83d86252010-05-07 18:58:45649 EXPECT_TRUE(file_util::Delete(non_existent, false));
650 ASSERT_FALSE(file_util::PathExists(non_existent));
651 EXPECT_TRUE(file_util::Delete(non_existent, true));
652 ASSERT_FALSE(file_util::PathExists(non_existent));
653}
654
655TEST_F(FileUtilTest, DeleteFile) {
656 // Create a file
[email protected]90314c0a82010-09-15 20:40:47657 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
[email protected]83d86252010-05-07 18:58:45658 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38659 ASSERT_TRUE(file_util::PathExists(file_name));
660
[email protected]83d86252010-05-07 18:58:45661 // Make sure it's deleted
662 EXPECT_TRUE(file_util::Delete(file_name, false));
663 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36664
[email protected]83d86252010-05-07 18:58:45665 // Test recursive case, create a new file
[email protected]90314c0a82010-09-15 20:40:47666 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
[email protected]83d86252010-05-07 18:58:45667 CreateTextFile(file_name, bogus_content);
668 ASSERT_TRUE(file_util::PathExists(file_name));
669
670 // Make sure it's deleted
671 EXPECT_TRUE(file_util::Delete(file_name, true));
672 EXPECT_FALSE(file_util::PathExists(file_name));
673}
674
675#if defined(OS_WIN)
676// Tests that the Delete function works for wild cards, especially
677// with the recursion flag. Also coincidentally tests PathExists.
678// TODO(erikkay): see if anyone's actually using this feature of the API
679TEST_F(FileUtilTest, DeleteWildCard) {
680 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47681 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
[email protected]83d86252010-05-07 18:58:45682 CreateTextFile(file_name, bogus_content);
683 ASSERT_TRUE(file_util::PathExists(file_name));
684
[email protected]90314c0a82010-09-15 20:40:47685 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
[email protected]83d86252010-05-07 18:58:45686 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38687 ASSERT_TRUE(file_util::PathExists(subdir_path));
688
[email protected]83d86252010-05-07 18:58:45689 // Create the wildcard path
[email protected]90314c0a82010-09-15 20:40:47690 FilePath directory_contents = temp_dir_.path();
[email protected]83d86252010-05-07 18:58:45691 directory_contents = directory_contents.Append(FPL("*"));
692
initial.commitd7cae122008-07-26 21:49:38693 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45694 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10695 EXPECT_FALSE(file_util::PathExists(file_name));
696 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40697
[email protected]3a51cfdd2010-05-07 00:05:36698 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45699 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40700 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36701 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40702}
703
[email protected]83d86252010-05-07 18:58:45704// TODO(erikkay): see if anyone's actually using this feature of the API
705TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
706 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47707 FilePath subdir_path =
708 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
[email protected]83d86252010-05-07 18:58:45709 file_util::CreateDirectory(subdir_path);
710 ASSERT_TRUE(file_util::PathExists(subdir_path));
711
712 // Create the wildcard path
713 FilePath directory_contents = subdir_path;
714 directory_contents = directory_contents.Append(FPL("*"));
715
716 // Delete non-recursively and check nothing got deleted
717 EXPECT_TRUE(file_util::Delete(directory_contents, false));
718 EXPECT_TRUE(file_util::PathExists(subdir_path));
719
720 // Delete recursively and check nothing got deleted
721 EXPECT_TRUE(file_util::Delete(directory_contents, true));
722 EXPECT_TRUE(file_util::PathExists(subdir_path));
723}
724#endif
725
726// Tests non-recursive Delete() for a directory.
727TEST_F(FileUtilTest, DeleteDirNonRecursive) {
728 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47729 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
[email protected]83d86252010-05-07 18:58:45730 file_util::CreateDirectory(test_subdir);
731 ASSERT_TRUE(file_util::PathExists(test_subdir));
732
733 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
734 CreateTextFile(file_name, bogus_content);
735 ASSERT_TRUE(file_util::PathExists(file_name));
736
737 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
738 file_util::CreateDirectory(subdir_path1);
739 ASSERT_TRUE(file_util::PathExists(subdir_path1));
740
741 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
742 file_util::CreateDirectory(subdir_path2);
743 ASSERT_TRUE(file_util::PathExists(subdir_path2));
744
745 // Delete non-recursively and check that the empty dir got deleted
746 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
747 EXPECT_FALSE(file_util::PathExists(subdir_path2));
748
749 // Delete non-recursively and check that nothing got deleted
750 EXPECT_FALSE(file_util::Delete(test_subdir, false));
751 EXPECT_TRUE(file_util::PathExists(test_subdir));
752 EXPECT_TRUE(file_util::PathExists(file_name));
753 EXPECT_TRUE(file_util::PathExists(subdir_path1));
754}
755
756// Tests recursive Delete() for a directory.
757TEST_F(FileUtilTest, DeleteDirRecursive) {
758 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47759 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
[email protected]83d86252010-05-07 18:58:45760 file_util::CreateDirectory(test_subdir);
761 ASSERT_TRUE(file_util::PathExists(test_subdir));
762
763 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
764 CreateTextFile(file_name, bogus_content);
765 ASSERT_TRUE(file_util::PathExists(file_name));
766
767 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
768 file_util::CreateDirectory(subdir_path1);
769 ASSERT_TRUE(file_util::PathExists(subdir_path1));
770
771 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
772 file_util::CreateDirectory(subdir_path2);
773 ASSERT_TRUE(file_util::PathExists(subdir_path2));
774
775 // Delete recursively and check that the empty dir got deleted
776 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
777 EXPECT_FALSE(file_util::PathExists(subdir_path2));
778
779 // Delete recursively and check that everything got deleted
780 EXPECT_TRUE(file_util::Delete(test_subdir, true));
781 EXPECT_FALSE(file_util::PathExists(file_name));
782 EXPECT_FALSE(file_util::PathExists(subdir_path1));
783 EXPECT_FALSE(file_util::PathExists(test_subdir));
784}
785
[email protected]bc6a9012009-10-15 01:11:44786TEST_F(FileUtilTest, MoveFileNew) {
787 // Create a file
788 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47789 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44790 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
791 ASSERT_TRUE(file_util::PathExists(file_name_from));
792
[email protected]90314c0a82010-09-15 20:40:47793 // The destination.
794 FilePath file_name_to = temp_dir_.path().Append(
795 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44796 ASSERT_FALSE(file_util::PathExists(file_name_to));
797
798 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
799
800 // Check everything has been moved.
801 EXPECT_FALSE(file_util::PathExists(file_name_from));
802 EXPECT_TRUE(file_util::PathExists(file_name_to));
803}
804
805TEST_F(FileUtilTest, MoveFileExists) {
806 // Create a file
807 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47808 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44809 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
810 ASSERT_TRUE(file_util::PathExists(file_name_from));
811
[email protected]90314c0a82010-09-15 20:40:47812 // The destination name.
813 FilePath file_name_to = temp_dir_.path().Append(
814 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44815 CreateTextFile(file_name_to, L"Old file content");
816 ASSERT_TRUE(file_util::PathExists(file_name_to));
817
818 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
819
820 // Check everything has been moved.
821 EXPECT_FALSE(file_util::PathExists(file_name_from));
822 EXPECT_TRUE(file_util::PathExists(file_name_to));
823 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
824}
825
826TEST_F(FileUtilTest, MoveFileDirExists) {
827 // Create a file
828 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47829 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44830 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
831 ASSERT_TRUE(file_util::PathExists(file_name_from));
832
833 // The destination directory
834 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47835 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:44836 file_util::CreateDirectory(dir_name_to);
837 ASSERT_TRUE(file_util::PathExists(dir_name_to));
838
839 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
840}
841
842
[email protected]abbc5732009-10-13 17:57:27843TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38844 // Create a directory
[email protected]640517f2008-10-30 23:54:04845 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47846 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04847 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38848 ASSERT_TRUE(file_util::PathExists(dir_name_from));
849
850 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04851 FilePath file_name_from =
852 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38853 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
854 ASSERT_TRUE(file_util::PathExists(file_name_from));
855
[email protected]90314c0a82010-09-15 20:40:47856 // Move the directory.
857 FilePath dir_name_to =
858 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04859 FilePath file_name_to =
860 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38861
862 ASSERT_FALSE(file_util::PathExists(dir_name_to));
863
864 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
865
866 // Check everything has been moved.
867 EXPECT_FALSE(file_util::PathExists(dir_name_from));
868 EXPECT_FALSE(file_util::PathExists(file_name_from));
869 EXPECT_TRUE(file_util::PathExists(dir_name_to));
870 EXPECT_TRUE(file_util::PathExists(file_name_to));
871}
872
[email protected]abbc5732009-10-13 17:57:27873TEST_F(FileUtilTest, MoveExist) {
874 // Create a directory
875 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47876 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:27877 file_util::CreateDirectory(dir_name_from);
878 ASSERT_TRUE(file_util::PathExists(dir_name_from));
879
880 // Create a file under the directory
881 FilePath file_name_from =
882 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
883 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
884 ASSERT_TRUE(file_util::PathExists(file_name_from));
885
886 // Move the directory
887 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:47888 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:27889
890 FilePath dir_name_to =
891 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
892 FilePath file_name_to =
893 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
894
895 // Create the destination directory.
896 file_util::CreateDirectory(dir_name_exists);
897 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
898
899 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
900
901 // Check everything has been moved.
902 EXPECT_FALSE(file_util::PathExists(dir_name_from));
903 EXPECT_FALSE(file_util::PathExists(file_name_from));
904 EXPECT_TRUE(file_util::PathExists(dir_name_to));
905 EXPECT_TRUE(file_util::PathExists(file_name_to));
906}
907
908TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38909 // Create a directory.
[email protected]640517f2008-10-30 23:54:04910 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47911 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04912 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38913 ASSERT_TRUE(file_util::PathExists(dir_name_from));
914
915 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04916 FilePath file_name_from =
917 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38918 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
919 ASSERT_TRUE(file_util::PathExists(file_name_from));
920
921 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04922 FilePath subdir_name_from =
923 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
924 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38925 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
926
927 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04928 FilePath file_name2_from =
929 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38930 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
931 ASSERT_TRUE(file_util::PathExists(file_name2_from));
932
933 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04934 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47935 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04936 FilePath file_name_to =
937 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
938 FilePath subdir_name_to =
939 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
940 FilePath file_name2_to =
941 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38942
943 ASSERT_FALSE(file_util::PathExists(dir_name_to));
944
945 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
946
947 // Check everything has been copied.
948 EXPECT_TRUE(file_util::PathExists(dir_name_from));
949 EXPECT_TRUE(file_util::PathExists(file_name_from));
950 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
951 EXPECT_TRUE(file_util::PathExists(file_name2_from));
952 EXPECT_TRUE(file_util::PathExists(dir_name_to));
953 EXPECT_TRUE(file_util::PathExists(file_name_to));
954 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
955 EXPECT_TRUE(file_util::PathExists(file_name2_to));
956}
957
[email protected]abbc5732009-10-13 17:57:27958TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
959 // Create a directory.
960 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47961 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:27962 file_util::CreateDirectory(dir_name_from);
963 ASSERT_TRUE(file_util::PathExists(dir_name_from));
964
965 // Create a file under the directory.
966 FilePath file_name_from =
967 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
968 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
969 ASSERT_TRUE(file_util::PathExists(file_name_from));
970
971 // Create a subdirectory.
972 FilePath subdir_name_from =
973 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
974 file_util::CreateDirectory(subdir_name_from);
975 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
976
977 // Create a file under the subdirectory.
978 FilePath file_name2_from =
979 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
980 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
981 ASSERT_TRUE(file_util::PathExists(file_name2_from));
982
983 // Copy the directory recursively.
984 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:47985 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:27986
987 FilePath dir_name_to =
988 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
989 FilePath file_name_to =
990 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
991 FilePath subdir_name_to =
992 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
993 FilePath file_name2_to =
994 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
995
996 // Create the destination directory.
997 file_util::CreateDirectory(dir_name_exists);
998 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
999
1000 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1001
1002 // Check everything has been copied.
1003 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1004 EXPECT_TRUE(file_util::PathExists(file_name_from));
1005 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1006 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1007 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1008 EXPECT_TRUE(file_util::PathExists(file_name_to));
1009 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1010 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1011}
1012
1013TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381014 // Create a directory.
[email protected]640517f2008-10-30 23:54:041015 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471016 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041017 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381018 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1019
1020 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041021 FilePath file_name_from =
1022 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381023 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1024 ASSERT_TRUE(file_util::PathExists(file_name_from));
1025
1026 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041027 FilePath subdir_name_from =
1028 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1029 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381030 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1031
1032 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041033 FilePath file_name2_from =
1034 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381035 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1036 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1037
1038 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041039 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471040 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:041041 FilePath file_name_to =
1042 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1043 FilePath subdir_name_to =
1044 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381045
1046 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1047
1048 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1049
1050 // Check everything has been copied.
1051 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1052 EXPECT_TRUE(file_util::PathExists(file_name_from));
1053 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1054 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1055 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1056 EXPECT_TRUE(file_util::PathExists(file_name_to));
1057 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1058}
1059
[email protected]abbc5732009-10-13 17:57:271060TEST_F(FileUtilTest, CopyDirectoryExists) {
1061 // Create a directory.
1062 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471063 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:271064 file_util::CreateDirectory(dir_name_from);
1065 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1066
1067 // Create a file under the directory.
1068 FilePath file_name_from =
1069 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1070 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1071 ASSERT_TRUE(file_util::PathExists(file_name_from));
1072
1073 // Create a subdirectory.
1074 FilePath subdir_name_from =
1075 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1076 file_util::CreateDirectory(subdir_name_from);
1077 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1078
1079 // Create a file under the subdirectory.
1080 FilePath file_name2_from =
1081 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1082 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1083 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1084
1085 // Copy the directory not recursively.
1086 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471087 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]abbc5732009-10-13 17:57:271088 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"));
1092
1093 // Create the destination directory.
1094 file_util::CreateDirectory(dir_name_to);
1095 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1096
1097 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1098
1099 // Check everything has been copied.
1100 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1101 EXPECT_TRUE(file_util::PathExists(file_name_from));
1102 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1103 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1104 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1105 EXPECT_TRUE(file_util::PathExists(file_name_to));
1106 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1107}
1108
[email protected]bc6a9012009-10-15 01:11:441109TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1110 // Create a file
1111 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471112 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441113 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1114 ASSERT_TRUE(file_util::PathExists(file_name_from));
1115
1116 // The destination name
[email protected]90314c0a82010-09-15 20:40:471117 FilePath file_name_to = temp_dir_.path().Append(
1118 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441119 ASSERT_FALSE(file_util::PathExists(file_name_to));
1120
1121 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1122
1123 // Check the has been copied
1124 EXPECT_TRUE(file_util::PathExists(file_name_to));
1125}
1126
1127TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1128 // Create a file
1129 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471130 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441131 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1132 ASSERT_TRUE(file_util::PathExists(file_name_from));
1133
1134 // The destination name
[email protected]90314c0a82010-09-15 20:40:471135 FilePath file_name_to = temp_dir_.path().Append(
1136 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441137 CreateTextFile(file_name_to, L"Old file content");
1138 ASSERT_TRUE(file_util::PathExists(file_name_to));
1139
1140 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1141
1142 // Check the has been copied
1143 EXPECT_TRUE(file_util::PathExists(file_name_to));
1144 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1145}
1146
1147TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1148 // Create a file
1149 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471150 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441151 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1152 ASSERT_TRUE(file_util::PathExists(file_name_from));
1153
1154 // The destination
1155 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471156 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:441157 file_util::CreateDirectory(dir_name_to);
1158 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1159 FilePath file_name_to =
1160 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1161
1162 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1163
1164 // Check the has been copied
1165 EXPECT_TRUE(file_util::PathExists(file_name_to));
1166}
1167
initial.commitd7cae122008-07-26 21:49:381168TEST_F(FileUtilTest, CopyFile) {
1169 // Create a directory
[email protected]640517f2008-10-30 23:54:041170 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471171 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041172 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381173 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1174
1175 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041176 FilePath file_name_from =
1177 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381178 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1179 CreateTextFile(file_name_from, file_contents);
1180 ASSERT_TRUE(file_util::PathExists(file_name_from));
1181
1182 // Copy the file.
[email protected]640517f2008-10-30 23:54:041183 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381184 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111185
[email protected]37088fef2008-08-15 17:32:101186 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141187 FilePath dest_file2(dir_name_from);
1188 dest_file2 = dest_file2.AppendASCII("..");
1189 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1190 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1191
1192 FilePath dest_file2_test(dir_name_from);
1193 dest_file2_test = dest_file2_test.DirName();
1194 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381195
1196 // Check everything has been copied.
1197 EXPECT_TRUE(file_util::PathExists(file_name_from));
1198 EXPECT_TRUE(file_util::PathExists(dest_file));
1199 const std::wstring read_contents = ReadTextFile(dest_file);
1200 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141201 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1202 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381203}
1204
[email protected]37088fef2008-08-15 17:32:101205// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131206#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381207TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]90314c0a82010-09-15 20:40:471208 FilePath file_name = temp_dir_.path().Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381209
1210 SYSTEMTIME start_time;
1211 GetLocalTime(&start_time);
1212 Sleep(100);
1213 CreateTextFile(file_name, L"New file!");
1214 Sleep(100);
1215 SYSTEMTIME end_time;
1216 GetLocalTime(&end_time);
1217
1218 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041219 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381220
1221 FILETIME start_filetime;
1222 SystemTimeToFileTime(&start_time, &start_filetime);
1223 FILETIME end_filetime;
1224 SystemTimeToFileTime(&end_time, &end_filetime);
1225 FILETIME file_creation_filetime;
1226 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1227
1228 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1229 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1230 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1231
1232 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1233 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1234 "end time: " << FileTimeAsUint64(end_filetime);
1235
[email protected]640517f2008-10-30 23:54:041236 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381237}
[email protected]37088fef2008-08-15 17:32:101238#endif
initial.commitd7cae122008-07-26 21:49:381239
[email protected]ed2f2332008-08-20 15:59:491240// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041241// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491242typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381243
[email protected]ed2f2332008-08-20 15:59:491244TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041245 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381246 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041247 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1248 .Append(FILE_PATH_LITERAL("data"))
1249 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381250 ASSERT_TRUE(file_util::PathExists(data_dir));
1251
[email protected]640517f2008-10-30 23:54:041252 FilePath original_file =
1253 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1254 FilePath same_file =
1255 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1256 FilePath same_length_file =
1257 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1258 FilePath different_file =
1259 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1260 FilePath different_first_file =
1261 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1262 FilePath different_last_file =
1263 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1264 FilePath empty1_file =
1265 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1266 FilePath empty2_file =
1267 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1268 FilePath shortened_file =
1269 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1270 FilePath binary_file =
1271 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1272 FilePath binary_file_same =
1273 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1274 FilePath binary_file_diff =
1275 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381276
1277 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1278 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1279 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1280 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221281 EXPECT_FALSE(file_util::ContentsEqual(
1282 FilePath(FILE_PATH_LITERAL("bogusname")),
1283 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381284 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1285 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1286 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1287 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1288 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1289 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1290 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1291}
1292
[email protected]b81637c32009-06-26 21:17:241293TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1294 FilePath data_dir;
1295 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1296 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1297 .Append(FILE_PATH_LITERAL("data"))
1298 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1299 ASSERT_TRUE(file_util::PathExists(data_dir));
1300
1301 FilePath original_file =
1302 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1303 FilePath same_file =
1304 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1305 FilePath crlf_file =
1306 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1307 FilePath shortened_file =
1308 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1309 FilePath different_file =
1310 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1311 FilePath different_first_file =
1312 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1313 FilePath different_last_file =
1314 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1315 FilePath first1_file =
1316 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1317 FilePath first2_file =
1318 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1319 FilePath empty1_file =
1320 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1321 FilePath empty2_file =
1322 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1323 FilePath blank_line_file =
1324 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1325 FilePath blank_line_crlf_file =
1326 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1327
1328 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1329 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1330 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1331 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1332 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1333 different_first_file));
1334 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1335 different_last_file));
1336 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1337 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1338 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1339 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1340 blank_line_crlf_file));
1341}
1342
[email protected]37088fef2008-08-15 17:32:101343// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131344#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381345TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]90314c0a82010-09-15 20:40:471346 FilePath target_file = temp_dir_.path().Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381347 CreateTextFile(target_file, L"This is the target.");
1348
[email protected]90314c0a82010-09-15 20:40:471349 FilePath link_file = temp_dir_.path().Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381350
1351 HRESULT result;
1352 IShellLink *shell = NULL;
1353 IPersistFile *persist = NULL;
1354
1355 CoInitialize(NULL);
1356 // Temporarily create a shortcut for test
1357 result = CoCreateInstance(CLSID_ShellLink, NULL,
1358 CLSCTX_INPROC_SERVER, IID_IShellLink,
1359 reinterpret_cast<LPVOID*>(&shell));
1360 EXPECT_TRUE(SUCCEEDED(result));
1361 result = shell->QueryInterface(IID_IPersistFile,
1362 reinterpret_cast<LPVOID*>(&persist));
1363 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041364 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381365 EXPECT_TRUE(SUCCEEDED(result));
1366 result = shell->SetDescription(L"ResolveShortcutTest");
1367 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041368 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381369 EXPECT_TRUE(SUCCEEDED(result));
1370 if (persist)
1371 persist->Release();
1372 if (shell)
1373 shell->Release();
1374
1375 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441376 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381377 EXPECT_TRUE(is_solved);
1378 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441379 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381380 EXPECT_EQ(L"This is the target.", contents);
1381
[email protected]d324ab332008-08-18 16:00:381382 // Cleaning
[email protected]640517f2008-10-30 23:54:041383 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441384 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381385 CoUninitialize();
1386}
1387
1388TEST_F(FileUtilTest, CreateShortcutTest) {
1389 const wchar_t file_contents[] = L"This is another target.";
[email protected]90314c0a82010-09-15 20:40:471390 FilePath target_file = temp_dir_.path().Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381391 CreateTextFile(target_file, file_contents);
1392
[email protected]90314c0a82010-09-15 20:40:471393 FilePath link_file = temp_dir_.path().Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381394
1395 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041396 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1397 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501398 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441399 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381400 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441401 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381402 EXPECT_EQ(file_contents, read_contents);
1403
[email protected]640517f2008-10-30 23:54:041404 DeleteFile(target_file.value().c_str());
1405 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381406 CoUninitialize();
1407}
[email protected]2c59af7dca2009-03-11 18:37:481408
1409TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1410 // Create a directory
1411 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471412 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
[email protected]2c59af7dca2009-03-11 18:37:481413 file_util::CreateDirectory(dir_name_from);
1414 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1415
1416 // Create a file under the directory
1417 FilePath file_name_from =
1418 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1419 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1420 ASSERT_TRUE(file_util::PathExists(file_name_from));
1421
1422 // Move the directory by using CopyAndDeleteDirectory
[email protected]90314c0a82010-09-15 20:40:471423 FilePath dir_name_to = temp_dir_.path().Append(
[email protected]2c59af7dca2009-03-11 18:37:481424 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1425 FilePath file_name_to =
1426 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1427
1428 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1429
1430 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1431
1432 // Check everything has been moved.
1433 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1434 EXPECT_FALSE(file_util::PathExists(file_name_from));
1435 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1436 EXPECT_TRUE(file_util::PathExists(file_name_to));
1437}
[email protected]7d95aae2009-10-09 07:33:391438
1439TEST_F(FileUtilTest, GetTempDirTest) {
1440 static const TCHAR* kTmpKey = _T("TMP");
1441 static const TCHAR* kTmpValues[] = {
1442 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1443 };
1444 // Save the original $TMP.
1445 size_t original_tmp_size;
1446 TCHAR* original_tmp;
1447 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1448 // original_tmp may be NULL.
1449
1450 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1451 FilePath path;
1452 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1453 file_util::GetTempDir(&path);
1454 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1455 " result=" << path.value();
1456 }
1457
1458 // Restore the original $TMP.
1459 if (original_tmp) {
1460 ::_tputenv_s(kTmpKey, original_tmp);
1461 free(original_tmp);
1462 } else {
1463 ::_tputenv_s(kTmpKey, _T(""));
1464 }
1465}
1466#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381467
[email protected]33edeab2009-08-18 16:07:551468TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1469 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391470 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551471 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391472 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1473 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1474 }
1475 for (int i = 0; i < 3; i++)
1476 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1477 for (int i = 0; i < 3; i++)
1478 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1479}
1480
[email protected]33edeab2009-08-18 16:07:551481TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391482 FilePath names[3];
1483 FILE *fps[3];
1484 int i;
1485
1486 // Create; make sure they are open and exist.
1487 for (i = 0; i < 3; ++i) {
1488 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1489 ASSERT_TRUE(fps[i]);
1490 EXPECT_TRUE(file_util::PathExists(names[i]));
1491 }
1492
1493 // Make sure all names are unique.
1494 for (i = 0; i < 3; ++i) {
1495 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1496 }
1497
1498 // Close and delete.
1499 for (i = 0; i < 3; ++i) {
1500 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1501 EXPECT_TRUE(file_util::Delete(names[i], false));
1502 }
initial.commitd7cae122008-07-26 21:49:381503}
1504
1505TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141506 FilePath temp_dir;
1507 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1508 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111509 EXPECT_TRUE(file_util::PathExists(temp_dir));
1510 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381511}
1512
[email protected]b0b3abd92010-04-30 17:00:091513TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1514 FilePath new_dir;
1515 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
[email protected]90314c0a82010-09-15 20:40:471516 temp_dir_.path(),
[email protected]b0b3abd92010-04-30 17:00:091517 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
[email protected]046062e82010-06-30 07:19:111518 &new_dir));
[email protected]b0b3abd92010-04-30 17:00:091519 EXPECT_TRUE(file_util::PathExists(new_dir));
[email protected]90314c0a82010-09-15 20:40:471520 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
[email protected]b0b3abd92010-04-30 17:00:091521 EXPECT_TRUE(file_util::Delete(new_dir, false));
1522}
1523
[email protected]9e51af92009-02-04 00:58:391524TEST_F(FileUtilTest, GetShmemTempDirTest) {
1525 FilePath dir;
1526 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1527 EXPECT_TRUE(file_util::DirectoryExists(dir));
1528}
1529
initial.commitd7cae122008-07-26 21:49:381530TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041531 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471532 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131533#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041534 FilePath test_path =
1535 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101536#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041537 FilePath test_path =
1538 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101539#endif
[email protected]806b9c62008-09-11 16:09:111540
1541 EXPECT_FALSE(file_util::PathExists(test_path));
1542 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1543 EXPECT_TRUE(file_util::PathExists(test_path));
1544 // CreateDirectory returns true if the DirectoryExists returns true.
1545 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1546
1547 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041548 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111549 EXPECT_FALSE(file_util::PathExists(test_path));
1550 CreateTextFile(test_path, L"test file");
1551 EXPECT_TRUE(file_util::PathExists(test_path));
1552 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1553
1554 EXPECT_TRUE(file_util::Delete(test_root, true));
1555 EXPECT_FALSE(file_util::PathExists(test_root));
1556 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411557
1558 // Verify assumptions made by the Windows implementation:
1559 // 1. The current directory always exists.
1560 // 2. The root directory always exists.
1561 ASSERT_TRUE(file_util::DirectoryExists(
1562 FilePath(FilePath::kCurrentDirectory)));
1563 FilePath top_level = test_root;
1564 while (top_level != top_level.DirName()) {
1565 top_level = top_level.DirName();
1566 }
1567 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1568
1569 // Given these assumptions hold, it should be safe to
1570 // test that "creating" these directories succeeds.
1571 EXPECT_TRUE(file_util::CreateDirectory(
1572 FilePath(FilePath::kCurrentDirectory)));
1573 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401574
1575#if defined(OS_WIN)
1576 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1577 FilePath invalid_path =
1578 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1579 if (!file_util::PathExists(invalid_drive)) {
1580 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1581 }
1582#endif
[email protected]806b9c62008-09-11 16:09:111583}
1584
1585TEST_F(FileUtilTest, DetectDirectoryTest) {
1586 // Check a directory
[email protected]640517f2008-10-30 23:54:041587 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471588 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111589 EXPECT_FALSE(file_util::PathExists(test_root));
1590 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1591 EXPECT_TRUE(file_util::PathExists(test_root));
1592 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1593
1594 // Check a file
[email protected]640517f2008-10-30 23:54:041595 FilePath test_path =
1596 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111597 EXPECT_FALSE(file_util::PathExists(test_path));
1598 CreateTextFile(test_path, L"test file");
1599 EXPECT_TRUE(file_util::PathExists(test_path));
1600 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1601 EXPECT_TRUE(file_util::Delete(test_path, false));
1602
1603 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381604}
1605
initial.commitd7cae122008-07-26 21:49:381606TEST_F(FileUtilTest, FileEnumeratorTest) {
1607 // Test an empty directory.
[email protected]90314c0a82010-09-15 20:40:471608 file_util::FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121609 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1610 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381611
[email protected]8199b3a2009-06-09 05:57:381612 // Test an empty directory, non-recursively, including "..".
[email protected]90314c0a82010-09-15 20:40:471613 file_util::FileEnumerator f0_dotdot(temp_dir_.path(), false,
[email protected]8199b3a2009-06-09 05:57:381614 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1615 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
[email protected]90314c0a82010-09-15 20:40:471616 EXPECT_EQ(temp_dir_.path().Append(FILE_PATH_LITERAL("..")).value(),
[email protected]8199b3a2009-06-09 05:57:381617 f0_dotdot.Next().value());
1618 EXPECT_EQ(FILE_PATH_LITERAL(""),
1619 f0_dotdot.Next().value());
1620
[email protected]37088fef2008-08-15 17:32:101621 // create the directories
[email protected]90314c0a82010-09-15 20:40:471622 FilePath dir1 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101623 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]90314c0a82010-09-15 20:40:471624 FilePath dir2 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101625 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041626 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101627 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041628
[email protected]37088fef2008-08-15 17:32:101629 // create the files
[email protected]640517f2008-10-30 23:54:041630 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101631 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041632 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101633 CreateTextFile(dir2innerfile, L"");
[email protected]90314c0a82010-09-15 20:40:471634 FilePath file1 = temp_dir_.path().Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101635 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041636 FilePath file2_rel =
1637 dir2.Append(FilePath::kParentDirectory)
1638 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101639 CreateTextFile(file2_rel, L"");
[email protected]90314c0a82010-09-15 20:40:471640 FilePath file2_abs = temp_dir_.path().Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381641
1642 // Only enumerate files.
[email protected]90314c0a82010-09-15 20:40:471643 file_util::FileEnumerator f1(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381644 file_util::FileEnumerator::FILES);
1645 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101646 EXPECT_TRUE(c1.HasFile(file1));
1647 EXPECT_TRUE(c1.HasFile(file2_abs));
1648 EXPECT_TRUE(c1.HasFile(dir2file));
1649 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1650 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381651
1652 // Only enumerate directories.
[email protected]90314c0a82010-09-15 20:40:471653 file_util::FileEnumerator f2(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381654 file_util::FileEnumerator::DIRECTORIES);
1655 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101656 EXPECT_TRUE(c2.HasFile(dir1));
1657 EXPECT_TRUE(c2.HasFile(dir2));
1658 EXPECT_TRUE(c2.HasFile(dir2inner));
1659 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381660
[email protected]3bc9ffaf2008-10-16 02:42:451661 // Only enumerate directories non-recursively.
1662 file_util::FileEnumerator f2_non_recursive(
[email protected]90314c0a82010-09-15 20:40:471663 temp_dir_.path(), false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451664 FindResultCollector c2_non_recursive(f2_non_recursive);
1665 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1666 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1667 EXPECT_EQ(c2_non_recursive.size(), 2);
1668
[email protected]8199b3a2009-06-09 05:57:381669 // Only enumerate directories, non-recursively, including "..".
1670 file_util::FileEnumerator f2_dotdot(
[email protected]90314c0a82010-09-15 20:40:471671 temp_dir_.path(), false,
[email protected]8199b3a2009-06-09 05:57:381672 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1673 file_util::FileEnumerator::DIRECTORIES |
1674 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1675 FindResultCollector c2_dotdot(f2_dotdot);
1676 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1677 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
[email protected]90314c0a82010-09-15 20:40:471678 EXPECT_TRUE(c2_dotdot.HasFile(
1679 temp_dir_.path().Append(FILE_PATH_LITERAL(".."))));
[email protected]8199b3a2009-06-09 05:57:381680 EXPECT_EQ(c2_dotdot.size(), 3);
1681
initial.commitd7cae122008-07-26 21:49:381682 // Enumerate files and directories.
[email protected]90314c0a82010-09-15 20:40:471683 file_util::FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381684 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101685 EXPECT_TRUE(c3.HasFile(dir1));
1686 EXPECT_TRUE(c3.HasFile(dir2));
1687 EXPECT_TRUE(c3.HasFile(file1));
1688 EXPECT_TRUE(c3.HasFile(file2_abs));
1689 EXPECT_TRUE(c3.HasFile(dir2file));
1690 EXPECT_TRUE(c3.HasFile(dir2inner));
1691 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1692 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381693
1694 // Non-recursive operation.
[email protected]90314c0a82010-09-15 20:40:471695 file_util::FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381696 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101697 EXPECT_TRUE(c4.HasFile(dir2));
1698 EXPECT_TRUE(c4.HasFile(dir2));
1699 EXPECT_TRUE(c4.HasFile(file1));
1700 EXPECT_TRUE(c4.HasFile(file2_abs));
1701 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381702
1703 // Enumerate with a pattern.
[email protected]90314c0a82010-09-15 20:40:471704 file_util::FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121705 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381706 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101707 EXPECT_TRUE(c5.HasFile(dir1));
1708 EXPECT_TRUE(c5.HasFile(dir2));
1709 EXPECT_TRUE(c5.HasFile(dir2file));
1710 EXPECT_TRUE(c5.HasFile(dir2inner));
1711 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1712 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381713
1714 // Make sure the destructor closes the find handle while in the middle of a
1715 // query to allow TearDown to delete the directory.
[email protected]90314c0a82010-09-15 20:40:471716 file_util::FileEnumerator f6(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121717 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1718 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381719}
license.botbf09a502008-08-24 00:55:551720
[email protected]ee5c29da2009-01-09 22:14:271721TEST_F(FileUtilTest, Contains) {
[email protected]90314c0a82010-09-15 20:40:471722 FilePath data_dir =
1723 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271724
1725 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571726 if (file_util::PathExists(data_dir)) {
1727 ASSERT_TRUE(file_util::Delete(data_dir, true));
1728 }
[email protected]ee5c29da2009-01-09 22:14:271729 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1730
1731 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1732 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1733 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1734 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1735
1736 // Annoyingly, the directories must actually exist in order for realpath(),
1737 // which Contains() relies on in posix, to work.
1738 ASSERT_TRUE(file_util::CreateDirectory(foo));
1739 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581740 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1741 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1742 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271743
1744 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1745 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1746 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1747 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1748
[email protected]e43eddf12009-12-29 00:32:521749 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271750 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1751#if defined(OS_WIN)
1752 EXPECT_TRUE(file_util::ContainsPath(foo,
1753 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391754 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271755 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521756#elif defined(OS_MACOSX)
1757 // We can't really do this test on OS X since the case-sensitivity of the
1758 // filesystem is configurable.
1759#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271760 EXPECT_FALSE(file_util::ContainsPath(foo,
1761 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271762#endif
1763}
1764
[email protected]507fb9a2010-09-23 23:28:221765TEST_F(FileUtilTest, TouchFile) {
[email protected]90314c0a82010-09-15 20:40:471766 FilePath data_dir =
1767 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ec3d1452010-02-18 10:02:261768
1769 // Create a fresh, empty copy of this directory.
1770 if (file_util::PathExists(data_dir)) {
1771 ASSERT_TRUE(file_util::Delete(data_dir, true));
1772 }
1773 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1774
1775 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1776 std::string data("hello");
1777 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1778
[email protected]507fb9a2010-09-23 23:28:221779 base::Time access_time;
1780 // This timestamp is divisible by one day (in local timezone),
1781 // to make it work on FAT too.
[email protected]916654122011-04-26 19:07:081782 ASSERT_TRUE(base::Time::FromString(L"Wed, 16 Nov 1994, 00:00:00",
[email protected]507fb9a2010-09-23 23:28:221783 &access_time));
1784
[email protected]ec3d1452010-02-18 10:02:261785 base::Time modification_time;
1786 // Note that this timestamp is divisible by two (seconds) - FAT stores
1787 // modification times with 2s resolution.
[email protected]916654122011-04-26 19:07:081788 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
[email protected]ec3d1452010-02-18 10:02:261789 &modification_time));
[email protected]507fb9a2010-09-23 23:28:221790
1791 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
[email protected]2f0193c22010-09-03 02:28:371792 base::PlatformFileInfo file_info;
[email protected]ec3d1452010-02-18 10:02:261793 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
[email protected]507fb9a2010-09-23 23:28:221794 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
1795 access_time.ToInternalValue());
1796 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
1797 modification_time.ToInternalValue());
[email protected]ec3d1452010-02-18 10:02:261798}
1799
[email protected]b33f1d92010-05-26 01:40:121800TEST_F(FileUtilTest, IsDirectoryEmpty) {
[email protected]90314c0a82010-09-15 20:40:471801 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
[email protected]b33f1d92010-05-26 01:40:121802
1803 ASSERT_FALSE(file_util::PathExists(empty_dir));
1804
1805 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1806
1807 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1808
1809 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1810 std::string bar("baz");
1811 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1812
1813 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1814}
1815
[email protected]11b901ee2008-09-10 00:16:281816} // namespace