blob: ca36b6a6b89138aebe90384e2447c0fcd0f1b2e7 [file] [log] [blame]
[email protected]ec3d1452010-02-18 10:02:261// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]f35d39b2008-08-15 17:50:105#include "build/build_config.h"
6
[email protected]8541bb82008-08-15 17:45:137#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:388#include <windows.h>
[email protected]6f5f4322010-06-09 22:56:489#include <winioctl.h>
initial.commitd7cae122008-07-26 21:49:3810#include <shellapi.h>
11#include <shlobj.h>
[email protected]7d95aae2009-10-09 07:33:3912#include <tchar.h>
[email protected]37088fef2008-08-15 17:32:1013#endif
initial.commitd7cae122008-07-26 21:49:3814
15#include <fstream>
[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]2126577b2009-04-23 15:05:1922#include "base/platform_thread.h"
[email protected]6f5f4322010-06-09 22:56:4823#include "base/scoped_handle.h"
[email protected]90314c0a82010-09-15 20:40:4724#include "base/scoped_temp_dir.h"
[email protected]85786f92009-04-18 00:42:4825#include "base/time.h"
[email protected]047a03f2009-10-07 02:10:2026#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3827#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1528#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3829
[email protected]4e9f6be2009-04-03 17:17:5830// This macro helps avoid wrapped lines in the test structs.
31#define FPL(x) FILE_PATH_LITERAL(x)
32
initial.commitd7cae122008-07-26 21:49:3833namespace {
34
[email protected]6f5f4322010-06-09 22:56:4835// To test that file_util::Normalize FilePath() deals with NTFS reparse points
36// correctly, we need functions to create and delete reparse points.
37#if defined(OS_WIN)
38typedef struct _REPARSE_DATA_BUFFER {
39 ULONG ReparseTag;
40 USHORT ReparseDataLength;
41 USHORT Reserved;
42 union {
43 struct {
44 USHORT SubstituteNameOffset;
45 USHORT SubstituteNameLength;
46 USHORT PrintNameOffset;
47 USHORT PrintNameLength;
48 ULONG Flags;
49 WCHAR PathBuffer[1];
50 } SymbolicLinkReparseBuffer;
51 struct {
52 USHORT SubstituteNameOffset;
53 USHORT SubstituteNameLength;
54 USHORT PrintNameOffset;
55 USHORT PrintNameLength;
56 WCHAR PathBuffer[1];
57 } MountPointReparseBuffer;
58 struct {
59 UCHAR DataBuffer[1];
60 } GenericReparseBuffer;
61 };
62} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
63
64// Sets a reparse point. |source| will now point to |target|. Returns true if
65// the call succeeds, false otherwise.
66bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
67 std::wstring kPathPrefix = L"\\??\\";
68 std::wstring target_str;
69 // The juction will not work if the target path does not start with \??\ .
70 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
71 target_str += kPathPrefix;
72 target_str += target_path.value();
73 const wchar_t* target = target_str.c_str();
74 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
75 char buffer[2000] = {0};
76 DWORD returned;
77
78 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
79
80 data->ReparseTag = 0xa0000003;
81 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
82
83 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
84 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
85 data->ReparseDataLength = size_target + 4 + 8;
86
87 int data_size = data->ReparseDataLength + 8;
88
89 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
90 NULL, 0, &returned, NULL)) {
91 return false;
92 }
93 return true;
94}
95
96// Delete the reparse point referenced by |source|. Returns true if the call
97// succeeds, false otherwise.
98bool DeleteReparsePoint(HANDLE source) {
99 DWORD returned;
100 REPARSE_DATA_BUFFER data = {0};
101 data.ReparseTag = 0xa0000003;
102 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
103 &returned, NULL)) {
104 return false;
105 }
106 return true;
107}
108#endif
109
[email protected]83d86252010-05-07 18:58:45110const wchar_t bogus_content[] = L"I'm cannon fodder.";
111
[email protected]8199b3a2009-06-09 05:57:38112const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
113 static_cast<file_util::FileEnumerator::FILE_TYPE>(
114 file_util::FileEnumerator::FILES |
115 file_util::FileEnumerator::DIRECTORIES);
116
[email protected]ed2f2332008-08-20 15:59:49117// file_util winds up using autoreleased objects on the Mac, so this needs
118// to be a PlatformTest
119class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:38120 protected:
121 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:49122 PlatformTest::SetUp();
[email protected]90314c0a82010-09-15 20:40:47123 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commitd7cae122008-07-26 21:49:38124 }
125
[email protected]90314c0a82010-09-15 20:40:47126 ScopedTempDir temp_dir_;
initial.commitd7cae122008-07-26 21:49:38127};
128
129// Collects all the results from the given file enumerator, and provides an
130// interface to query whether a given file is present.
131class FindResultCollector {
132 public:
[email protected]53c58042009-08-26 20:00:14133 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
[email protected]0b733222008-12-11 14:55:12134 FilePath cur_file;
135 while (!(cur_file = enumerator.Next()).value().empty()) {
136 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:38137 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:04138 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:38139 << "Same file returned twice";
140
141 // Save for later.
[email protected]640517f2008-10-30 23:54:04142 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:38143 }
144 }
145
146 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:04147 bool HasFile(const FilePath& file) const {
148 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:38149 }
[email protected]640517f2008-10-30 23:54:04150
[email protected]37088fef2008-08-15 17:32:10151 int size() {
[email protected]f35d39b2008-08-15 17:50:10152 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:10153 }
initial.commitd7cae122008-07-26 21:49:38154
155 private:
[email protected]640517f2008-10-30 23:54:04156 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:38157};
158
159// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:04160void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:38161 const std::wstring& contents) {
162 std::ofstream file;
[email protected]640517f2008-10-30 23:54:04163 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:38164 ASSERT_TRUE(file.is_open());
165 file << contents;
166 file.close();
167}
168
169// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:04170std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:10171 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38172 std::wifstream file;
[email protected]640517f2008-10-30 23:54:04173 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:38174 EXPECT_TRUE(file.is_open());
175 file.getline(contents, 64);
176 file.close();
177 return std::wstring(contents);
178}
179
[email protected]8541bb82008-08-15 17:45:13180#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38181uint64 FileTimeAsUint64(const FILETIME& ft) {
182 ULARGE_INTEGER u;
183 u.LowPart = ft.dwLowDateTime;
184 u.HighPart = ft.dwHighDateTime;
185 return u.QuadPart;
186}
[email protected]37088fef2008-08-15 17:32:10187#endif
initial.commitd7cae122008-07-26 21:49:38188
189const struct append_case {
190 const wchar_t* path;
191 const wchar_t* ending;
192 const wchar_t* result;
193} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13194#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38195 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
196 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
197 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
198 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
199 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
200 {L"", L"path", L"\\path"},
201 {L"", L"", L"\\"},
[email protected]37088fef2008-08-15 17:32:10202#elif defined(OS_POSIX)
203 {L"/foo/bar", L"path", L"/foo/bar/path"},
204 {L"/foo/bar/", L"path", L"/foo/bar/path"},
205 {L"/foo/bar//", L"path", L"/foo/bar//path"},
206 {L"/foo/bar/", L"", L"/foo/bar/"},
207 {L"/foo/bar", L"", L"/foo/bar/"},
208 {L"", L"path", L"/path"},
209 {L"", L"", L"/"},
210#endif
initial.commitd7cae122008-07-26 21:49:38211};
212
[email protected]1840cfc2010-02-26 15:11:55213#if defined(OS_WIN)
214// This function is deprecated, but still used on Windows.
initial.commitd7cae122008-07-26 21:49:38215TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10216 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38217 const append_case& value = append_cases[i];
218 std::wstring result = value.path;
219 file_util::AppendToPath(&result, value.ending);
220 EXPECT_EQ(value.result, result);
221 }
222
223#ifdef NDEBUG
224 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
225#endif
226}
[email protected]1840cfc2010-02-26 15:11:55227#endif // defined(OS_WIN)
228
initial.commitd7cae122008-07-26 21:49:38229static const struct filename_case {
230 const wchar_t* path;
231 const wchar_t* filename;
232} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13233#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38234 {L"c:\\colon\\backslash", L"backslash"},
235 {L"c:\\colon\\backslash\\", L""},
236 {L"\\\\filename.exe", L"filename.exe"},
237 {L"filename.exe", L"filename.exe"},
238 {L"", L""},
239 {L"\\\\\\", L""},
240 {L"c:/colon/backslash", L"backslash"},
241 {L"c:/colon/backslash/", L""},
242 {L"//////", L""},
243 {L"///filename.exe", L"filename.exe"},
[email protected]37088fef2008-08-15 17:32:10244#elif defined(OS_POSIX)
245 {L"/foo/bar", L"bar"},
246 {L"/foo/bar/", L""},
247 {L"/filename.exe", L"filename.exe"},
248 {L"filename.exe", L"filename.exe"},
249 {L"", L""},
250 {L"/", L""},
251#endif
initial.commitd7cae122008-07-26 21:49:38252};
253
[email protected]8d19c7d2010-07-01 23:19:02254#if defined(OS_WIN)
255// This function is deprecated on non-Windows.
initial.commitd7cae122008-07-26 21:49:38256TEST_F(FileUtilTest, GetFilenameFromPath) {
[email protected]37088fef2008-08-15 17:32:10257 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38258 const filename_case& value = filename_cases[i];
259 std::wstring result = file_util::GetFilenameFromPath(value.path);
260 EXPECT_EQ(value.filename, result);
261 }
262}
[email protected]8d19c7d2010-07-01 23:19:02263#endif
initial.commitd7cae122008-07-26 21:49:38264
265// Test finding the file type from a path name
266static const struct extension_case {
267 const wchar_t* path;
268 const wchar_t* extension;
269} extension_cases[] = {
[email protected]8541bb82008-08-15 17:45:13270#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38271 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
272 {L"C:\\colon\\backslash\\filename.", L""},
273 {L"C:\\colon\\backslash\\filename", L""},
274 {L"C:\\colon\\backslash\\", L""},
275 {L"C:\\colon\\backslash.\\", L""},
276 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
[email protected]37088fef2008-08-15 17:32:10277#elif defined(OS_POSIX)
278 {L"/foo/bar/filename.extension", L"extension"},
279 {L"/foo/bar/filename.", L""},
280 {L"/foo/bar/filename", L""},
281 {L"/foo/bar/", L""},
282 {L"/foo/bar./", L""},
283 {L"/foo/bar/filename.extension.extension2", L"extension2"},
284 {L".", L""},
285 {L"..", L""},
286 {L"./foo", L""},
287 {L"./foo.extension", L"extension"},
288 {L"/foo.extension1/bar.extension2", L"extension2"},
289#endif
initial.commitd7cae122008-07-26 21:49:38290};
291
[email protected]63597e4e2010-07-08 17:49:05292#if defined(OS_WIN)
293// This function has been deprecated on non-Windows.
initial.commitd7cae122008-07-26 21:49:38294TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10295 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38296 const extension_case& ext = extension_cases[i];
297 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
298 EXPECT_EQ(ext.extension, fext);
299 }
300}
[email protected]63597e4e2010-07-08 17:49:05301#endif
initial.commitd7cae122008-07-26 21:49:38302
303// Test finding the directory component of a path
304static const struct dir_case {
305 const wchar_t* full_path;
306 const wchar_t* directory;
307} dir_cases[] = {
[email protected]8541bb82008-08-15 17:45:13308#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38309 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
310 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
311 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
312 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
313 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
314 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
[email protected]2c8088a42009-10-15 05:00:25315 {L"C:\\", L"C:\\"},
[email protected]37088fef2008-08-15 17:32:10316#elif defined(OS_POSIX)
317 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
318 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
319 {L"/foo/bar/", L"/foo/bar"},
320 {L"/foo/bar//", L"/foo/bar"},
321 {L"/foo/bar", L"/foo"},
322 {L"/foo/bar./", L"/foo/bar."},
323 {L"/", L"/"},
324 {L".", L"."},
[email protected]53c58042009-08-26 20:00:14325 {L"..", L"."}, // yes, ".." technically lives in "."
[email protected]37088fef2008-08-15 17:32:10326#endif
initial.commitd7cae122008-07-26 21:49:38327};
328
[email protected]a18f79a2010-02-23 12:52:11329#if defined(OS_WIN)
330// This function is deprecated, and only exists on Windows anymore.
initial.commitd7cae122008-07-26 21:49:38331TEST_F(FileUtilTest, GetDirectoryFromPath) {
[email protected]37088fef2008-08-15 17:32:10332 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38333 const dir_case& dir = dir_cases[i];
334 const std::wstring parent =
335 file_util::GetDirectoryFromPath(dir.full_path);
336 EXPECT_EQ(dir.directory, parent);
337 }
338}
[email protected]a18f79a2010-02-23 12:52:11339#endif
initial.commitd7cae122008-07-26 21:49:38340
[email protected]2ec79f72010-06-10 13:05:26341// Flaky, http://crbug.com/46246
[email protected]552b3152010-06-10 12:40:52342TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commitd7cae122008-07-26 21:49:38343 // Create old file (that we don't want to count)
[email protected]90314c0a82010-09-15 20:40:47344 FilePath old_file_name =
345 temp_dir_.path().Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commitd7cae122008-07-26 21:49:38346 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
347
348 // Age to perfection
[email protected]4b7743de2009-04-21 01:50:39349#if defined(OS_WIN)
[email protected]2126577b2009-04-23 15:05:19350 PlatformThread::Sleep(100);
[email protected]4b7743de2009-04-21 01:50:39351#elif defined(OS_POSIX)
352 // We need to wait at least one second here because the precision of
353 // file creation time is one second.
[email protected]2126577b2009-04-23 15:05:19354 PlatformThread::Sleep(1500);
[email protected]4b7743de2009-04-21 01:50:39355#endif
initial.commitd7cae122008-07-26 21:49:38356
357 // Establish our cutoff time
[email protected]85786f92009-04-18 00:42:48358 base::Time now(base::Time::NowFromSystemTime());
[email protected]90314c0a82010-09-15 20:40:47359 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38360
361 // Create a new file (that we do want to count)
[email protected]90314c0a82010-09-15 20:40:47362 FilePath new_file_name =
363 temp_dir_.path().Append(FILE_PATH_LITERAL("New File.txt"));
initial.commitd7cae122008-07-26 21:49:38364 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
365
366 // We should see only the new file.
[email protected]90314c0a82010-09-15 20:40:47367 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38368
369 // Delete new file, we should see no files after cutoff now
370 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]90314c0a82010-09-15 20:40:47371 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38372}
373
[email protected]c2c132c62010-03-24 21:56:26374TEST_F(FileUtilTest, FileAndDirectorySize) {
375 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
376 // should return 53 bytes.
[email protected]90314c0a82010-09-15 20:40:47377 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
[email protected]c2c132c62010-03-24 21:56:26378 CreateTextFile(file_01, L"12345678901234567890");
379 int64 size_f1 = 0;
380 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
381 EXPECT_EQ(20ll, size_f1);
382
[email protected]90314c0a82010-09-15 20:40:47383 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
[email protected]c2c132c62010-03-24 21:56:26384 file_util::CreateDirectory(subdir_path);
385
386 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
387 CreateTextFile(file_02, L"123456789012345678901234567890");
388 int64 size_f2 = 0;
389 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
390 EXPECT_EQ(30ll, size_f2);
391
392 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
393 file_util::CreateDirectory(subsubdir_path);
394
395 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
396 CreateTextFile(file_03, L"123");
397
[email protected]90314c0a82010-09-15 20:40:47398 int64 computed_size = file_util::ComputeDirectorySize(temp_dir_.path());
[email protected]c2c132c62010-03-24 21:56:26399 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
[email protected]a04876b92010-06-11 22:53:43400
[email protected]90314c0a82010-09-15 20:40:47401 computed_size =
402 file_util::ComputeFilesSize(temp_dir_.path(), FPL("The file*"));
[email protected]a04876b92010-06-11 22:53:43403 EXPECT_EQ(size_f1, computed_size);
404
[email protected]90314c0a82010-09-15 20:40:47405 computed_size = file_util::ComputeFilesSize(temp_dir_.path(), FPL("bla*"));
[email protected]a04876b92010-06-11 22:53:43406 EXPECT_EQ(0, computed_size);
[email protected]c2c132c62010-03-24 21:56:26407}
408
[email protected]6f5f4322010-06-09 22:56:48409TEST_F(FileUtilTest, NormalizeFilePathBasic) {
410 // Create a directory under the test dir. Because we create it,
411 // we know it is not a link.
[email protected]90314c0a82010-09-15 20:40:47412 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
413 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
[email protected]6f5f4322010-06-09 22:56:48414 FilePath file_b_path = dir_path.Append(FPL("file_b"));
415 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57416
[email protected]6f5f4322010-06-09 22:56:48417 FilePath normalized_file_a_path, normalized_file_b_path;
418 ASSERT_FALSE(file_util::PathExists(file_a_path));
419 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
420 &normalized_file_a_path))
[email protected]e7afe2452010-08-22 16:19:13421 << "NormalizeFilePath() should fail on nonexistent paths.";
[email protected]6f5f4322010-06-09 22:56:48422
423 CreateTextFile(file_a_path, bogus_content);
424 ASSERT_TRUE(file_util::PathExists(file_a_path));
425 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
426 &normalized_file_a_path));
427
428 CreateTextFile(file_b_path, bogus_content);
429 ASSERT_TRUE(file_util::PathExists(file_b_path));
430 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
431 &normalized_file_b_path));
432
433 // Beacuse this test created |dir_path|, we know it is not a link
434 // or junction. So, the real path of the directory holding file a
435 // must be the parent of the path holding file b.
436 ASSERT_TRUE(normalized_file_a_path.DirName()
437 .IsParent(normalized_file_b_path.DirName()));
438}
439
440#if defined(OS_WIN)
441
442TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
443 // Build the following directory structure:
444 //
[email protected]90314c0a82010-09-15 20:40:47445 // temp_dir
[email protected]6f5f4322010-06-09 22:56:48446 // |-> base_a
447 // | |-> sub_a
448 // | |-> file.txt
449 // | |-> long_name___... (Very long name.)
450 // | |-> sub_long
451 // | |-> deep.txt
452 // |-> base_b
[email protected]90314c0a82010-09-15 20:40:47453 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
454 // |-> to_base_b (reparse point to temp_dir\base_b)
455 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
[email protected]6f5f4322010-06-09 22:56:48456
[email protected]90314c0a82010-09-15 20:40:47457 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
[email protected]6f5f4322010-06-09 22:56:48458 ASSERT_TRUE(file_util::CreateDirectory(base_a));
459
460 FilePath sub_a = base_a.Append(FPL("sub_a"));
461 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
462
463 FilePath file_txt = sub_a.Append(FPL("file.txt"));
464 CreateTextFile(file_txt, bogus_content);
465
466 // Want a directory whose name is long enough to make the path to the file
467 // inside just under MAX_PATH chars. This will be used to test that when
468 // a junction expands to a path over MAX_PATH chars in length,
469 // NormalizeFilePath() fails without crashing.
470 FilePath sub_long_rel(FPL("sub_long"));
471 FilePath deep_txt(FPL("deep.txt"));
472
473 int target_length = MAX_PATH;
474 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
475 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52476 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48477 // the resulting path is still long enough to hit the failing case in
478 // NormalizePath().
479 const int kCreateDirLimit = 4;
480 target_length -= kCreateDirLimit;
481 FilePath::StringType long_name_str = FPL("long_name_");
482 long_name_str.resize(target_length, '_');
483
484 FilePath long_name = sub_a.Append(FilePath(long_name_str));
485 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
486 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
487
488 FilePath sub_long = deep_file.DirName();
489 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
490 CreateTextFile(deep_file, bogus_content);
491
[email protected]90314c0a82010-09-15 20:40:47492 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
[email protected]6f5f4322010-06-09 22:56:48493 ASSERT_TRUE(file_util::CreateDirectory(base_b));
494
495 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
496 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
497 ScopedHandle reparse_to_sub_a(
498 ::CreateFile(to_sub_a.value().c_str(),
499 FILE_ALL_ACCESS,
500 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
501 NULL,
502 OPEN_EXISTING,
503 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
504 NULL));
[email protected]094f9762010-08-03 03:51:56505 ASSERT_TRUE(reparse_to_sub_a.IsValid());
[email protected]6f5f4322010-06-09 22:56:48506 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
507
508 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
509 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
510 ScopedHandle reparse_to_base_b(
511 ::CreateFile(to_base_b.value().c_str(),
512 FILE_ALL_ACCESS,
513 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
514 NULL,
515 OPEN_EXISTING,
516 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
517 NULL));
[email protected]094f9762010-08-03 03:51:56518 ASSERT_TRUE(reparse_to_base_b.IsValid());
[email protected]6f5f4322010-06-09 22:56:48519 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
520
521 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
522 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
523 ScopedHandle reparse_to_sub_long(
524 ::CreateFile(to_sub_long.value().c_str(),
525 FILE_ALL_ACCESS,
526 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
527 NULL,
528 OPEN_EXISTING,
529 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
530 NULL));
[email protected]094f9762010-08-03 03:51:56531 ASSERT_TRUE(reparse_to_sub_long.IsValid());
[email protected]6f5f4322010-06-09 22:56:48532 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
533
534 // Normalize a junction free path: base_a\sub_a\file.txt .
535 FilePath normalized_path;
536 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
537 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
538
539 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
540 // the junction to_sub_a.
541 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
542 &normalized_path));
543 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
544
545 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
546 // normalized to exclude junctions to_base_b and to_sub_a .
547 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
548 .Append(FPL("to_base_b"))
549 .Append(FPL("to_sub_a"))
550 .Append(FPL("file.txt")),
551 &normalized_path));
552 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
553
554 // A long enough path will cause NormalizeFilePath() to fail. Make a long
555 // path using to_base_b many times, and check that paths long enough to fail
556 // do not cause a crash.
557 FilePath long_path = base_b;
558 const int kLengthLimit = MAX_PATH + 200;
559 while (long_path.value().length() <= kLengthLimit) {
560 long_path = long_path.Append(FPL("to_base_b"));
561 }
562 long_path = long_path.Append(FPL("to_sub_a"))
563 .Append(FPL("file.txt"));
564
565 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
566
567 // Normalizing the junction to deep.txt should fail, because the expanded
568 // path to deep.txt is longer than MAX_PATH.
569 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
570 &normalized_path));
571
572 // Delete the reparse points, and see that NormalizeFilePath() fails
573 // to traverse them.
574 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
575 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
576 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
577
578 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
579 &normalized_path));
580}
581
582#endif // defined(OS_WIN)
583
584// The following test of NormalizeFilePath() require that we create a symlink.
585// This can not be done on windows before vista. On vista, creating a symlink
586// requires privilege "SeCreateSymbolicLinkPrivilege".
587// TODO(skerner): Investigate the possibility of giving base_unittests the
588// privileges required to create a symlink.
589#if defined(OS_POSIX)
590
591bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
592 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
593}
594
595TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
596 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57597
598 // Link one file to another.
[email protected]90314c0a82010-09-15 20:40:47599 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
600 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57601 CreateTextFile(link_to, bogus_content);
602
[email protected]6f5f4322010-06-09 22:56:48603 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57604 << "Failed to create file symlink.";
605
[email protected]6f5f4322010-06-09 22:56:48606 // Check that NormalizeFilePath sees the link.
607 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57608 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48609 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
610 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57611
612 // Link to a directory.
[email protected]90314c0a82010-09-15 20:40:47613 link_from = temp_dir_.path().Append(FPL("from_dir"));
614 link_to = temp_dir_.path().Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57615 file_util::CreateDirectory(link_to);
616
[email protected]6f5f4322010-06-09 22:56:48617 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57618 << "Failed to create directory symlink.";
619
[email protected]6f5f4322010-06-09 22:56:48620 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
621 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57622
[email protected]6f5f4322010-06-09 22:56:48623 // Test that a loop in the links causes NormalizeFilePath() to return false.
[email protected]90314c0a82010-09-15 20:40:47624 link_from = temp_dir_.path().Append(FPL("link_a"));
625 link_to = temp_dir_.path().Append(FPL("link_b"));
[email protected]6f5f4322010-06-09 22:56:48626 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57627 << "Failed to create loop symlink a.";
[email protected]6f5f4322010-06-09 22:56:48628 ASSERT_TRUE(MakeSymlink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57629 << "Failed to create loop symlink b.";
630
631 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48632 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57633}
634#endif // defined(OS_POSIX)
635
[email protected]83d86252010-05-07 18:58:45636TEST_F(FileUtilTest, DeleteNonExistent) {
[email protected]90314c0a82010-09-15 20:40:47637 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
[email protected]83d86252010-05-07 18:58:45638 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36639
[email protected]83d86252010-05-07 18:58:45640 EXPECT_TRUE(file_util::Delete(non_existent, false));
641 ASSERT_FALSE(file_util::PathExists(non_existent));
642 EXPECT_TRUE(file_util::Delete(non_existent, true));
643 ASSERT_FALSE(file_util::PathExists(non_existent));
644}
645
646TEST_F(FileUtilTest, DeleteFile) {
647 // Create a file
[email protected]90314c0a82010-09-15 20:40:47648 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
[email protected]83d86252010-05-07 18:58:45649 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38650 ASSERT_TRUE(file_util::PathExists(file_name));
651
[email protected]83d86252010-05-07 18:58:45652 // Make sure it's deleted
653 EXPECT_TRUE(file_util::Delete(file_name, false));
654 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36655
[email protected]83d86252010-05-07 18:58:45656 // Test recursive case, create a new file
[email protected]90314c0a82010-09-15 20:40:47657 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
[email protected]83d86252010-05-07 18:58:45658 CreateTextFile(file_name, bogus_content);
659 ASSERT_TRUE(file_util::PathExists(file_name));
660
661 // Make sure it's deleted
662 EXPECT_TRUE(file_util::Delete(file_name, true));
663 EXPECT_FALSE(file_util::PathExists(file_name));
664}
665
666#if defined(OS_WIN)
667// Tests that the Delete function works for wild cards, especially
668// with the recursion flag. Also coincidentally tests PathExists.
669// TODO(erikkay): see if anyone's actually using this feature of the API
670TEST_F(FileUtilTest, DeleteWildCard) {
671 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47672 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
[email protected]83d86252010-05-07 18:58:45673 CreateTextFile(file_name, bogus_content);
674 ASSERT_TRUE(file_util::PathExists(file_name));
675
[email protected]90314c0a82010-09-15 20:40:47676 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
[email protected]83d86252010-05-07 18:58:45677 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38678 ASSERT_TRUE(file_util::PathExists(subdir_path));
679
[email protected]83d86252010-05-07 18:58:45680 // Create the wildcard path
[email protected]90314c0a82010-09-15 20:40:47681 FilePath directory_contents = temp_dir_.path();
[email protected]83d86252010-05-07 18:58:45682 directory_contents = directory_contents.Append(FPL("*"));
683
initial.commitd7cae122008-07-26 21:49:38684 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45685 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10686 EXPECT_FALSE(file_util::PathExists(file_name));
687 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40688
[email protected]3a51cfdd2010-05-07 00:05:36689 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45690 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40691 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36692 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40693}
694
[email protected]83d86252010-05-07 18:58:45695// TODO(erikkay): see if anyone's actually using this feature of the API
696TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
697 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47698 FilePath subdir_path =
699 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
[email protected]83d86252010-05-07 18:58:45700 file_util::CreateDirectory(subdir_path);
701 ASSERT_TRUE(file_util::PathExists(subdir_path));
702
703 // Create the wildcard path
704 FilePath directory_contents = subdir_path;
705 directory_contents = directory_contents.Append(FPL("*"));
706
707 // Delete non-recursively and check nothing got deleted
708 EXPECT_TRUE(file_util::Delete(directory_contents, false));
709 EXPECT_TRUE(file_util::PathExists(subdir_path));
710
711 // Delete recursively and check nothing got deleted
712 EXPECT_TRUE(file_util::Delete(directory_contents, true));
713 EXPECT_TRUE(file_util::PathExists(subdir_path));
714}
715#endif
716
717// Tests non-recursive Delete() for a directory.
718TEST_F(FileUtilTest, DeleteDirNonRecursive) {
719 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47720 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
[email protected]83d86252010-05-07 18:58:45721 file_util::CreateDirectory(test_subdir);
722 ASSERT_TRUE(file_util::PathExists(test_subdir));
723
724 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
725 CreateTextFile(file_name, bogus_content);
726 ASSERT_TRUE(file_util::PathExists(file_name));
727
728 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
729 file_util::CreateDirectory(subdir_path1);
730 ASSERT_TRUE(file_util::PathExists(subdir_path1));
731
732 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
733 file_util::CreateDirectory(subdir_path2);
734 ASSERT_TRUE(file_util::PathExists(subdir_path2));
735
736 // Delete non-recursively and check that the empty dir got deleted
737 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
738 EXPECT_FALSE(file_util::PathExists(subdir_path2));
739
740 // Delete non-recursively and check that nothing got deleted
741 EXPECT_FALSE(file_util::Delete(test_subdir, false));
742 EXPECT_TRUE(file_util::PathExists(test_subdir));
743 EXPECT_TRUE(file_util::PathExists(file_name));
744 EXPECT_TRUE(file_util::PathExists(subdir_path1));
745}
746
747// Tests recursive Delete() for a directory.
748TEST_F(FileUtilTest, DeleteDirRecursive) {
749 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47750 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
[email protected]83d86252010-05-07 18:58:45751 file_util::CreateDirectory(test_subdir);
752 ASSERT_TRUE(file_util::PathExists(test_subdir));
753
754 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
755 CreateTextFile(file_name, bogus_content);
756 ASSERT_TRUE(file_util::PathExists(file_name));
757
758 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
759 file_util::CreateDirectory(subdir_path1);
760 ASSERT_TRUE(file_util::PathExists(subdir_path1));
761
762 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
763 file_util::CreateDirectory(subdir_path2);
764 ASSERT_TRUE(file_util::PathExists(subdir_path2));
765
766 // Delete recursively and check that the empty dir got deleted
767 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
768 EXPECT_FALSE(file_util::PathExists(subdir_path2));
769
770 // Delete recursively and check that everything got deleted
771 EXPECT_TRUE(file_util::Delete(test_subdir, true));
772 EXPECT_FALSE(file_util::PathExists(file_name));
773 EXPECT_FALSE(file_util::PathExists(subdir_path1));
774 EXPECT_FALSE(file_util::PathExists(test_subdir));
775}
776
[email protected]bc6a9012009-10-15 01:11:44777TEST_F(FileUtilTest, MoveFileNew) {
778 // Create a file
779 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47780 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44781 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
782 ASSERT_TRUE(file_util::PathExists(file_name_from));
783
[email protected]90314c0a82010-09-15 20:40:47784 // The destination.
785 FilePath file_name_to = temp_dir_.path().Append(
786 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44787 ASSERT_FALSE(file_util::PathExists(file_name_to));
788
789 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
790
791 // Check everything has been moved.
792 EXPECT_FALSE(file_util::PathExists(file_name_from));
793 EXPECT_TRUE(file_util::PathExists(file_name_to));
794}
795
796TEST_F(FileUtilTest, MoveFileExists) {
797 // Create a file
798 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47799 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44800 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
801 ASSERT_TRUE(file_util::PathExists(file_name_from));
802
[email protected]90314c0a82010-09-15 20:40:47803 // The destination name.
804 FilePath file_name_to = temp_dir_.path().Append(
805 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44806 CreateTextFile(file_name_to, L"Old file content");
807 ASSERT_TRUE(file_util::PathExists(file_name_to));
808
809 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
810
811 // Check everything has been moved.
812 EXPECT_FALSE(file_util::PathExists(file_name_from));
813 EXPECT_TRUE(file_util::PathExists(file_name_to));
814 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
815}
816
817TEST_F(FileUtilTest, MoveFileDirExists) {
818 // Create a file
819 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47820 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44821 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
822 ASSERT_TRUE(file_util::PathExists(file_name_from));
823
824 // The destination directory
825 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47826 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:44827 file_util::CreateDirectory(dir_name_to);
828 ASSERT_TRUE(file_util::PathExists(dir_name_to));
829
830 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
831}
832
833
[email protected]abbc5732009-10-13 17:57:27834TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38835 // Create a directory
[email protected]640517f2008-10-30 23:54:04836 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47837 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04838 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38839 ASSERT_TRUE(file_util::PathExists(dir_name_from));
840
841 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04842 FilePath file_name_from =
843 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38844 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
845 ASSERT_TRUE(file_util::PathExists(file_name_from));
846
[email protected]90314c0a82010-09-15 20:40:47847 // Move the directory.
848 FilePath dir_name_to =
849 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04850 FilePath file_name_to =
851 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38852
853 ASSERT_FALSE(file_util::PathExists(dir_name_to));
854
855 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
856
857 // Check everything has been moved.
858 EXPECT_FALSE(file_util::PathExists(dir_name_from));
859 EXPECT_FALSE(file_util::PathExists(file_name_from));
860 EXPECT_TRUE(file_util::PathExists(dir_name_to));
861 EXPECT_TRUE(file_util::PathExists(file_name_to));
862}
863
[email protected]abbc5732009-10-13 17:57:27864TEST_F(FileUtilTest, MoveExist) {
865 // Create a directory
866 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47867 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:27868 file_util::CreateDirectory(dir_name_from);
869 ASSERT_TRUE(file_util::PathExists(dir_name_from));
870
871 // Create a file under the directory
872 FilePath file_name_from =
873 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
874 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
875 ASSERT_TRUE(file_util::PathExists(file_name_from));
876
877 // Move the directory
878 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:47879 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:27880
881 FilePath dir_name_to =
882 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
883 FilePath file_name_to =
884 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
885
886 // Create the destination directory.
887 file_util::CreateDirectory(dir_name_exists);
888 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
889
890 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
891
892 // Check everything has been moved.
893 EXPECT_FALSE(file_util::PathExists(dir_name_from));
894 EXPECT_FALSE(file_util::PathExists(file_name_from));
895 EXPECT_TRUE(file_util::PathExists(dir_name_to));
896 EXPECT_TRUE(file_util::PathExists(file_name_to));
897}
898
899TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38900 // Create a directory.
[email protected]640517f2008-10-30 23:54:04901 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47902 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04903 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38904 ASSERT_TRUE(file_util::PathExists(dir_name_from));
905
906 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04907 FilePath file_name_from =
908 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38909 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
910 ASSERT_TRUE(file_util::PathExists(file_name_from));
911
912 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04913 FilePath subdir_name_from =
914 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
915 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38916 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
917
918 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04919 FilePath file_name2_from =
920 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38921 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
922 ASSERT_TRUE(file_util::PathExists(file_name2_from));
923
924 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04925 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47926 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04927 FilePath file_name_to =
928 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
929 FilePath subdir_name_to =
930 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
931 FilePath file_name2_to =
932 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38933
934 ASSERT_FALSE(file_util::PathExists(dir_name_to));
935
936 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
937
938 // Check everything has been copied.
939 EXPECT_TRUE(file_util::PathExists(dir_name_from));
940 EXPECT_TRUE(file_util::PathExists(file_name_from));
941 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
942 EXPECT_TRUE(file_util::PathExists(file_name2_from));
943 EXPECT_TRUE(file_util::PathExists(dir_name_to));
944 EXPECT_TRUE(file_util::PathExists(file_name_to));
945 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
946 EXPECT_TRUE(file_util::PathExists(file_name2_to));
947}
948
[email protected]abbc5732009-10-13 17:57:27949TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
950 // Create a directory.
951 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47952 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:27953 file_util::CreateDirectory(dir_name_from);
954 ASSERT_TRUE(file_util::PathExists(dir_name_from));
955
956 // Create a file under the directory.
957 FilePath file_name_from =
958 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
959 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
960 ASSERT_TRUE(file_util::PathExists(file_name_from));
961
962 // Create a subdirectory.
963 FilePath subdir_name_from =
964 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
965 file_util::CreateDirectory(subdir_name_from);
966 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
967
968 // Create a file under the subdirectory.
969 FilePath file_name2_from =
970 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
971 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
972 ASSERT_TRUE(file_util::PathExists(file_name2_from));
973
974 // Copy the directory recursively.
975 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:47976 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:27977
978 FilePath dir_name_to =
979 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
980 FilePath file_name_to =
981 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
982 FilePath subdir_name_to =
983 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
984 FilePath file_name2_to =
985 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
986
987 // Create the destination directory.
988 file_util::CreateDirectory(dir_name_exists);
989 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
990
991 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
992
993 // Check everything has been copied.
994 EXPECT_TRUE(file_util::PathExists(dir_name_from));
995 EXPECT_TRUE(file_util::PathExists(file_name_from));
996 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
997 EXPECT_TRUE(file_util::PathExists(file_name2_from));
998 EXPECT_TRUE(file_util::PathExists(dir_name_to));
999 EXPECT_TRUE(file_util::PathExists(file_name_to));
1000 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1001 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1002}
1003
1004TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381005 // Create a directory.
[email protected]640517f2008-10-30 23:54:041006 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471007 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041008 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381009 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1010
1011 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041012 FilePath file_name_from =
1013 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381014 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1015 ASSERT_TRUE(file_util::PathExists(file_name_from));
1016
1017 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041018 FilePath subdir_name_from =
1019 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1020 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381021 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1022
1023 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041024 FilePath file_name2_from =
1025 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381026 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1027 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1028
1029 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041030 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471031 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:041032 FilePath file_name_to =
1033 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1034 FilePath subdir_name_to =
1035 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381036
1037 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1038
1039 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1040
1041 // Check everything has been copied.
1042 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1043 EXPECT_TRUE(file_util::PathExists(file_name_from));
1044 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1045 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1046 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1047 EXPECT_TRUE(file_util::PathExists(file_name_to));
1048 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1049}
1050
[email protected]abbc5732009-10-13 17:57:271051TEST_F(FileUtilTest, CopyDirectoryExists) {
1052 // Create a directory.
1053 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471054 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:271055 file_util::CreateDirectory(dir_name_from);
1056 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1057
1058 // Create a file under the directory.
1059 FilePath file_name_from =
1060 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1061 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1062 ASSERT_TRUE(file_util::PathExists(file_name_from));
1063
1064 // Create a subdirectory.
1065 FilePath subdir_name_from =
1066 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1067 file_util::CreateDirectory(subdir_name_from);
1068 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1069
1070 // Create a file under the subdirectory.
1071 FilePath file_name2_from =
1072 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1073 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1074 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1075
1076 // Copy the directory not recursively.
1077 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471078 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]abbc5732009-10-13 17:57:271079 FilePath file_name_to =
1080 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1081 FilePath subdir_name_to =
1082 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1083
1084 // Create the destination directory.
1085 file_util::CreateDirectory(dir_name_to);
1086 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1087
1088 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1089
1090 // Check everything has been copied.
1091 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1092 EXPECT_TRUE(file_util::PathExists(file_name_from));
1093 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1094 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1095 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1096 EXPECT_TRUE(file_util::PathExists(file_name_to));
1097 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1098}
1099
[email protected]bc6a9012009-10-15 01:11:441100TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1101 // Create a file
1102 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471103 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441104 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1105 ASSERT_TRUE(file_util::PathExists(file_name_from));
1106
1107 // The destination name
[email protected]90314c0a82010-09-15 20:40:471108 FilePath file_name_to = temp_dir_.path().Append(
1109 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441110 ASSERT_FALSE(file_util::PathExists(file_name_to));
1111
1112 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1113
1114 // Check the has been copied
1115 EXPECT_TRUE(file_util::PathExists(file_name_to));
1116}
1117
1118TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1119 // Create a file
1120 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471121 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441122 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1123 ASSERT_TRUE(file_util::PathExists(file_name_from));
1124
1125 // The destination name
[email protected]90314c0a82010-09-15 20:40:471126 FilePath file_name_to = temp_dir_.path().Append(
1127 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441128 CreateTextFile(file_name_to, L"Old file content");
1129 ASSERT_TRUE(file_util::PathExists(file_name_to));
1130
1131 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1132
1133 // Check the has been copied
1134 EXPECT_TRUE(file_util::PathExists(file_name_to));
1135 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1136}
1137
1138TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1139 // Create a file
1140 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471141 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441142 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1143 ASSERT_TRUE(file_util::PathExists(file_name_from));
1144
1145 // The destination
1146 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471147 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:441148 file_util::CreateDirectory(dir_name_to);
1149 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1150 FilePath file_name_to =
1151 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1152
1153 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1154
1155 // Check the has been copied
1156 EXPECT_TRUE(file_util::PathExists(file_name_to));
1157}
1158
initial.commitd7cae122008-07-26 21:49:381159TEST_F(FileUtilTest, CopyFile) {
1160 // Create a directory
[email protected]640517f2008-10-30 23:54:041161 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471162 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041163 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381164 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1165
1166 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041167 FilePath file_name_from =
1168 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381169 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1170 CreateTextFile(file_name_from, file_contents);
1171 ASSERT_TRUE(file_util::PathExists(file_name_from));
1172
1173 // Copy the file.
[email protected]640517f2008-10-30 23:54:041174 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381175 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111176
[email protected]37088fef2008-08-15 17:32:101177 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141178 FilePath dest_file2(dir_name_from);
1179 dest_file2 = dest_file2.AppendASCII("..");
1180 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1181 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1182
1183 FilePath dest_file2_test(dir_name_from);
1184 dest_file2_test = dest_file2_test.DirName();
1185 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381186
1187 // Check everything has been copied.
1188 EXPECT_TRUE(file_util::PathExists(file_name_from));
1189 EXPECT_TRUE(file_util::PathExists(dest_file));
1190 const std::wstring read_contents = ReadTextFile(dest_file);
1191 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141192 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1193 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381194}
1195
[email protected]37088fef2008-08-15 17:32:101196// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131197#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381198TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]90314c0a82010-09-15 20:40:471199 FilePath file_name = temp_dir_.path().Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381200
1201 SYSTEMTIME start_time;
1202 GetLocalTime(&start_time);
1203 Sleep(100);
1204 CreateTextFile(file_name, L"New file!");
1205 Sleep(100);
1206 SYSTEMTIME end_time;
1207 GetLocalTime(&end_time);
1208
1209 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041210 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381211
1212 FILETIME start_filetime;
1213 SystemTimeToFileTime(&start_time, &start_filetime);
1214 FILETIME end_filetime;
1215 SystemTimeToFileTime(&end_time, &end_filetime);
1216 FILETIME file_creation_filetime;
1217 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1218
1219 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1220 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1221 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1222
1223 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1224 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1225 "end time: " << FileTimeAsUint64(end_filetime);
1226
[email protected]640517f2008-10-30 23:54:041227 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381228}
[email protected]37088fef2008-08-15 17:32:101229#endif
initial.commitd7cae122008-07-26 21:49:381230
[email protected]ed2f2332008-08-20 15:59:491231// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041232// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491233typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381234
[email protected]ed2f2332008-08-20 15:59:491235TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041236 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381237 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041238 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1239 .Append(FILE_PATH_LITERAL("data"))
1240 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381241 ASSERT_TRUE(file_util::PathExists(data_dir));
1242
[email protected]640517f2008-10-30 23:54:041243 FilePath original_file =
1244 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1245 FilePath same_file =
1246 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1247 FilePath same_length_file =
1248 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1249 FilePath different_file =
1250 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1251 FilePath different_first_file =
1252 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1253 FilePath different_last_file =
1254 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1255 FilePath empty1_file =
1256 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1257 FilePath empty2_file =
1258 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1259 FilePath shortened_file =
1260 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1261 FilePath binary_file =
1262 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1263 FilePath binary_file_same =
1264 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1265 FilePath binary_file_diff =
1266 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381267
1268 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1269 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1270 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1271 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221272 EXPECT_FALSE(file_util::ContentsEqual(
1273 FilePath(FILE_PATH_LITERAL("bogusname")),
1274 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381275 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1276 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1277 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1278 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1279 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1280 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1281 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1282}
1283
[email protected]b81637c32009-06-26 21:17:241284TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1285 FilePath data_dir;
1286 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1287 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1288 .Append(FILE_PATH_LITERAL("data"))
1289 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1290 ASSERT_TRUE(file_util::PathExists(data_dir));
1291
1292 FilePath original_file =
1293 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1294 FilePath same_file =
1295 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1296 FilePath crlf_file =
1297 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1298 FilePath shortened_file =
1299 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1300 FilePath different_file =
1301 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1302 FilePath different_first_file =
1303 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1304 FilePath different_last_file =
1305 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1306 FilePath first1_file =
1307 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1308 FilePath first2_file =
1309 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1310 FilePath empty1_file =
1311 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1312 FilePath empty2_file =
1313 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1314 FilePath blank_line_file =
1315 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1316 FilePath blank_line_crlf_file =
1317 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1318
1319 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1320 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1321 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1322 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1323 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1324 different_first_file));
1325 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1326 different_last_file));
1327 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1328 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1329 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1330 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1331 blank_line_crlf_file));
1332}
1333
[email protected]37088fef2008-08-15 17:32:101334// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131335#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381336TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]90314c0a82010-09-15 20:40:471337 FilePath target_file = temp_dir_.path().Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381338 CreateTextFile(target_file, L"This is the target.");
1339
[email protected]90314c0a82010-09-15 20:40:471340 FilePath link_file = temp_dir_.path().Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381341
1342 HRESULT result;
1343 IShellLink *shell = NULL;
1344 IPersistFile *persist = NULL;
1345
1346 CoInitialize(NULL);
1347 // Temporarily create a shortcut for test
1348 result = CoCreateInstance(CLSID_ShellLink, NULL,
1349 CLSCTX_INPROC_SERVER, IID_IShellLink,
1350 reinterpret_cast<LPVOID*>(&shell));
1351 EXPECT_TRUE(SUCCEEDED(result));
1352 result = shell->QueryInterface(IID_IPersistFile,
1353 reinterpret_cast<LPVOID*>(&persist));
1354 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041355 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381356 EXPECT_TRUE(SUCCEEDED(result));
1357 result = shell->SetDescription(L"ResolveShortcutTest");
1358 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041359 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381360 EXPECT_TRUE(SUCCEEDED(result));
1361 if (persist)
1362 persist->Release();
1363 if (shell)
1364 shell->Release();
1365
1366 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441367 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381368 EXPECT_TRUE(is_solved);
1369 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441370 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381371 EXPECT_EQ(L"This is the target.", contents);
1372
[email protected]d324ab332008-08-18 16:00:381373 // Cleaning
[email protected]640517f2008-10-30 23:54:041374 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441375 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381376 CoUninitialize();
1377}
1378
1379TEST_F(FileUtilTest, CreateShortcutTest) {
1380 const wchar_t file_contents[] = L"This is another target.";
[email protected]90314c0a82010-09-15 20:40:471381 FilePath target_file = temp_dir_.path().Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381382 CreateTextFile(target_file, file_contents);
1383
[email protected]90314c0a82010-09-15 20:40:471384 FilePath link_file = temp_dir_.path().Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381385
1386 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041387 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1388 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501389 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441390 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381391 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441392 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381393 EXPECT_EQ(file_contents, read_contents);
1394
[email protected]640517f2008-10-30 23:54:041395 DeleteFile(target_file.value().c_str());
1396 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381397 CoUninitialize();
1398}
[email protected]2c59af7dca2009-03-11 18:37:481399
1400TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1401 // Create a directory
1402 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471403 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
[email protected]2c59af7dca2009-03-11 18:37:481404 file_util::CreateDirectory(dir_name_from);
1405 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1406
1407 // Create a file under the directory
1408 FilePath file_name_from =
1409 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1410 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1411 ASSERT_TRUE(file_util::PathExists(file_name_from));
1412
1413 // Move the directory by using CopyAndDeleteDirectory
[email protected]90314c0a82010-09-15 20:40:471414 FilePath dir_name_to = temp_dir_.path().Append(
[email protected]2c59af7dca2009-03-11 18:37:481415 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1416 FilePath file_name_to =
1417 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1418
1419 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1420
1421 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1422
1423 // Check everything has been moved.
1424 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1425 EXPECT_FALSE(file_util::PathExists(file_name_from));
1426 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1427 EXPECT_TRUE(file_util::PathExists(file_name_to));
1428}
[email protected]7d95aae2009-10-09 07:33:391429
1430TEST_F(FileUtilTest, GetTempDirTest) {
1431 static const TCHAR* kTmpKey = _T("TMP");
1432 static const TCHAR* kTmpValues[] = {
1433 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1434 };
1435 // Save the original $TMP.
1436 size_t original_tmp_size;
1437 TCHAR* original_tmp;
1438 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1439 // original_tmp may be NULL.
1440
1441 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1442 FilePath path;
1443 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1444 file_util::GetTempDir(&path);
1445 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1446 " result=" << path.value();
1447 }
1448
1449 // Restore the original $TMP.
1450 if (original_tmp) {
1451 ::_tputenv_s(kTmpKey, original_tmp);
1452 free(original_tmp);
1453 } else {
1454 ::_tputenv_s(kTmpKey, _T(""));
1455 }
1456}
1457#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381458
[email protected]33edeab2009-08-18 16:07:551459TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1460 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391461 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551462 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391463 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1464 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1465 }
1466 for (int i = 0; i < 3; i++)
1467 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1468 for (int i = 0; i < 3; i++)
1469 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1470}
1471
[email protected]33edeab2009-08-18 16:07:551472TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391473 FilePath names[3];
1474 FILE *fps[3];
1475 int i;
1476
1477 // Create; make sure they are open and exist.
1478 for (i = 0; i < 3; ++i) {
1479 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1480 ASSERT_TRUE(fps[i]);
1481 EXPECT_TRUE(file_util::PathExists(names[i]));
1482 }
1483
1484 // Make sure all names are unique.
1485 for (i = 0; i < 3; ++i) {
1486 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1487 }
1488
1489 // Close and delete.
1490 for (i = 0; i < 3; ++i) {
1491 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1492 EXPECT_TRUE(file_util::Delete(names[i], false));
1493 }
initial.commitd7cae122008-07-26 21:49:381494}
1495
1496TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141497 FilePath temp_dir;
1498 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1499 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111500 EXPECT_TRUE(file_util::PathExists(temp_dir));
1501 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381502}
1503
[email protected]b0b3abd92010-04-30 17:00:091504TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1505 FilePath new_dir;
1506 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
[email protected]90314c0a82010-09-15 20:40:471507 temp_dir_.path(),
[email protected]b0b3abd92010-04-30 17:00:091508 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
[email protected]046062e82010-06-30 07:19:111509 &new_dir));
[email protected]b0b3abd92010-04-30 17:00:091510 EXPECT_TRUE(file_util::PathExists(new_dir));
[email protected]90314c0a82010-09-15 20:40:471511 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
[email protected]b0b3abd92010-04-30 17:00:091512 EXPECT_TRUE(file_util::Delete(new_dir, false));
1513}
1514
[email protected]9e51af92009-02-04 00:58:391515TEST_F(FileUtilTest, GetShmemTempDirTest) {
1516 FilePath dir;
1517 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1518 EXPECT_TRUE(file_util::DirectoryExists(dir));
1519}
1520
initial.commitd7cae122008-07-26 21:49:381521TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041522 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471523 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131524#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041525 FilePath test_path =
1526 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101527#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041528 FilePath test_path =
1529 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101530#endif
[email protected]806b9c62008-09-11 16:09:111531
1532 EXPECT_FALSE(file_util::PathExists(test_path));
1533 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1534 EXPECT_TRUE(file_util::PathExists(test_path));
1535 // CreateDirectory returns true if the DirectoryExists returns true.
1536 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1537
1538 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041539 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111540 EXPECT_FALSE(file_util::PathExists(test_path));
1541 CreateTextFile(test_path, L"test file");
1542 EXPECT_TRUE(file_util::PathExists(test_path));
1543 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1544
1545 EXPECT_TRUE(file_util::Delete(test_root, true));
1546 EXPECT_FALSE(file_util::PathExists(test_root));
1547 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411548
1549 // Verify assumptions made by the Windows implementation:
1550 // 1. The current directory always exists.
1551 // 2. The root directory always exists.
1552 ASSERT_TRUE(file_util::DirectoryExists(
1553 FilePath(FilePath::kCurrentDirectory)));
1554 FilePath top_level = test_root;
1555 while (top_level != top_level.DirName()) {
1556 top_level = top_level.DirName();
1557 }
1558 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1559
1560 // Given these assumptions hold, it should be safe to
1561 // test that "creating" these directories succeeds.
1562 EXPECT_TRUE(file_util::CreateDirectory(
1563 FilePath(FilePath::kCurrentDirectory)));
1564 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401565
1566#if defined(OS_WIN)
1567 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1568 FilePath invalid_path =
1569 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1570 if (!file_util::PathExists(invalid_drive)) {
1571 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1572 }
1573#endif
[email protected]806b9c62008-09-11 16:09:111574}
1575
1576TEST_F(FileUtilTest, DetectDirectoryTest) {
1577 // Check a directory
[email protected]640517f2008-10-30 23:54:041578 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471579 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111580 EXPECT_FALSE(file_util::PathExists(test_root));
1581 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1582 EXPECT_TRUE(file_util::PathExists(test_root));
1583 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1584
1585 // Check a file
[email protected]640517f2008-10-30 23:54:041586 FilePath test_path =
1587 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111588 EXPECT_FALSE(file_util::PathExists(test_path));
1589 CreateTextFile(test_path, L"test file");
1590 EXPECT_TRUE(file_util::PathExists(test_path));
1591 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1592 EXPECT_TRUE(file_util::Delete(test_path, false));
1593
1594 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381595}
1596
initial.commitd7cae122008-07-26 21:49:381597TEST_F(FileUtilTest, FileEnumeratorTest) {
1598 // Test an empty directory.
[email protected]90314c0a82010-09-15 20:40:471599 file_util::FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121600 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1601 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381602
[email protected]8199b3a2009-06-09 05:57:381603 // Test an empty directory, non-recursively, including "..".
[email protected]90314c0a82010-09-15 20:40:471604 file_util::FileEnumerator f0_dotdot(temp_dir_.path(), false,
[email protected]8199b3a2009-06-09 05:57:381605 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1606 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
[email protected]90314c0a82010-09-15 20:40:471607 EXPECT_EQ(temp_dir_.path().Append(FILE_PATH_LITERAL("..")).value(),
[email protected]8199b3a2009-06-09 05:57:381608 f0_dotdot.Next().value());
1609 EXPECT_EQ(FILE_PATH_LITERAL(""),
1610 f0_dotdot.Next().value());
1611
[email protected]37088fef2008-08-15 17:32:101612 // create the directories
[email protected]90314c0a82010-09-15 20:40:471613 FilePath dir1 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101614 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]90314c0a82010-09-15 20:40:471615 FilePath dir2 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101616 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041617 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101618 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041619
[email protected]37088fef2008-08-15 17:32:101620 // create the files
[email protected]640517f2008-10-30 23:54:041621 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101622 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041623 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101624 CreateTextFile(dir2innerfile, L"");
[email protected]90314c0a82010-09-15 20:40:471625 FilePath file1 = temp_dir_.path().Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101626 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041627 FilePath file2_rel =
1628 dir2.Append(FilePath::kParentDirectory)
1629 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101630 CreateTextFile(file2_rel, L"");
[email protected]90314c0a82010-09-15 20:40:471631 FilePath file2_abs = temp_dir_.path().Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381632
1633 // Only enumerate files.
[email protected]90314c0a82010-09-15 20:40:471634 file_util::FileEnumerator f1(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381635 file_util::FileEnumerator::FILES);
1636 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101637 EXPECT_TRUE(c1.HasFile(file1));
1638 EXPECT_TRUE(c1.HasFile(file2_abs));
1639 EXPECT_TRUE(c1.HasFile(dir2file));
1640 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1641 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381642
1643 // Only enumerate directories.
[email protected]90314c0a82010-09-15 20:40:471644 file_util::FileEnumerator f2(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381645 file_util::FileEnumerator::DIRECTORIES);
1646 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101647 EXPECT_TRUE(c2.HasFile(dir1));
1648 EXPECT_TRUE(c2.HasFile(dir2));
1649 EXPECT_TRUE(c2.HasFile(dir2inner));
1650 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381651
[email protected]3bc9ffaf2008-10-16 02:42:451652 // Only enumerate directories non-recursively.
1653 file_util::FileEnumerator f2_non_recursive(
[email protected]90314c0a82010-09-15 20:40:471654 temp_dir_.path(), false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451655 FindResultCollector c2_non_recursive(f2_non_recursive);
1656 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1657 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1658 EXPECT_EQ(c2_non_recursive.size(), 2);
1659
[email protected]8199b3a2009-06-09 05:57:381660 // Only enumerate directories, non-recursively, including "..".
1661 file_util::FileEnumerator f2_dotdot(
[email protected]90314c0a82010-09-15 20:40:471662 temp_dir_.path(), false,
[email protected]8199b3a2009-06-09 05:57:381663 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1664 file_util::FileEnumerator::DIRECTORIES |
1665 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1666 FindResultCollector c2_dotdot(f2_dotdot);
1667 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1668 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
[email protected]90314c0a82010-09-15 20:40:471669 EXPECT_TRUE(c2_dotdot.HasFile(
1670 temp_dir_.path().Append(FILE_PATH_LITERAL(".."))));
[email protected]8199b3a2009-06-09 05:57:381671 EXPECT_EQ(c2_dotdot.size(), 3);
1672
initial.commitd7cae122008-07-26 21:49:381673 // Enumerate files and directories.
[email protected]90314c0a82010-09-15 20:40:471674 file_util::FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381675 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101676 EXPECT_TRUE(c3.HasFile(dir1));
1677 EXPECT_TRUE(c3.HasFile(dir2));
1678 EXPECT_TRUE(c3.HasFile(file1));
1679 EXPECT_TRUE(c3.HasFile(file2_abs));
1680 EXPECT_TRUE(c3.HasFile(dir2file));
1681 EXPECT_TRUE(c3.HasFile(dir2inner));
1682 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1683 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381684
1685 // Non-recursive operation.
[email protected]90314c0a82010-09-15 20:40:471686 file_util::FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381687 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101688 EXPECT_TRUE(c4.HasFile(dir2));
1689 EXPECT_TRUE(c4.HasFile(dir2));
1690 EXPECT_TRUE(c4.HasFile(file1));
1691 EXPECT_TRUE(c4.HasFile(file2_abs));
1692 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381693
1694 // Enumerate with a pattern.
[email protected]90314c0a82010-09-15 20:40:471695 file_util::FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121696 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381697 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101698 EXPECT_TRUE(c5.HasFile(dir1));
1699 EXPECT_TRUE(c5.HasFile(dir2));
1700 EXPECT_TRUE(c5.HasFile(dir2file));
1701 EXPECT_TRUE(c5.HasFile(dir2inner));
1702 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1703 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381704
1705 // Make sure the destructor closes the find handle while in the middle of a
1706 // query to allow TearDown to delete the directory.
[email protected]90314c0a82010-09-15 20:40:471707 file_util::FileEnumerator f6(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121708 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1709 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381710}
license.botbf09a502008-08-24 00:55:551711
[email protected]ee5c29da2009-01-09 22:14:271712TEST_F(FileUtilTest, Contains) {
[email protected]90314c0a82010-09-15 20:40:471713 FilePath data_dir =
1714 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271715
1716 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571717 if (file_util::PathExists(data_dir)) {
1718 ASSERT_TRUE(file_util::Delete(data_dir, true));
1719 }
[email protected]ee5c29da2009-01-09 22:14:271720 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1721
1722 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1723 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1724 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1725 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1726
1727 // Annoyingly, the directories must actually exist in order for realpath(),
1728 // which Contains() relies on in posix, to work.
1729 ASSERT_TRUE(file_util::CreateDirectory(foo));
1730 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581731 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1732 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1733 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271734
1735 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1736 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1737 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1738 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1739
[email protected]e43eddf12009-12-29 00:32:521740 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271741 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1742#if defined(OS_WIN)
1743 EXPECT_TRUE(file_util::ContainsPath(foo,
1744 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391745 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271746 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521747#elif defined(OS_MACOSX)
1748 // We can't really do this test on OS X since the case-sensitivity of the
1749 // filesystem is configurable.
1750#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271751 EXPECT_FALSE(file_util::ContainsPath(foo,
1752 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271753#endif
1754}
1755
[email protected]507fb9a2010-09-23 23:28:221756TEST_F(FileUtilTest, TouchFile) {
[email protected]90314c0a82010-09-15 20:40:471757 FilePath data_dir =
1758 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ec3d1452010-02-18 10:02:261759
1760 // Create a fresh, empty copy of this directory.
1761 if (file_util::PathExists(data_dir)) {
1762 ASSERT_TRUE(file_util::Delete(data_dir, true));
1763 }
1764 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1765
1766 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1767 std::string data("hello");
1768 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1769
[email protected]507fb9a2010-09-23 23:28:221770 base::Time access_time;
1771 // This timestamp is divisible by one day (in local timezone),
1772 // to make it work on FAT too.
1773 ASSERT_TRUE(base::Time::FromString(L"Wed, 16 Nov 1994, 00:00:00",
1774 &access_time));
1775
[email protected]ec3d1452010-02-18 10:02:261776 base::Time modification_time;
1777 // Note that this timestamp is divisible by two (seconds) - FAT stores
1778 // modification times with 2s resolution.
1779 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1780 &modification_time));
[email protected]507fb9a2010-09-23 23:28:221781
1782 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
[email protected]2f0193c22010-09-03 02:28:371783 base::PlatformFileInfo file_info;
[email protected]ec3d1452010-02-18 10:02:261784 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
[email protected]507fb9a2010-09-23 23:28:221785 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
1786 access_time.ToInternalValue());
1787 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
1788 modification_time.ToInternalValue());
[email protected]ec3d1452010-02-18 10:02:261789}
1790
[email protected]b33f1d92010-05-26 01:40:121791TEST_F(FileUtilTest, IsDirectoryEmpty) {
[email protected]90314c0a82010-09-15 20:40:471792 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
[email protected]b33f1d92010-05-26 01:40:121793
1794 ASSERT_FALSE(file_util::PathExists(empty_dir));
1795
1796 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1797
1798 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1799
1800 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1801 std::string bar("baz");
1802 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1803
1804 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1805}
1806
[email protected]11b901ee2008-09-10 00:16:281807} // namespace