blob: b0bd2747b7913e7c1ec24c5f95a1913228d721cc [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]85786f92009-04-18 00:42:4824#include "base/time.h"
[email protected]047a03f2009-10-07 02:10:2025#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3826#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1527#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3828
[email protected]4e9f6be2009-04-03 17:17:5829// This macro helps avoid wrapped lines in the test structs.
30#define FPL(x) FILE_PATH_LITERAL(x)
31
initial.commitd7cae122008-07-26 21:49:3832namespace {
33
[email protected]6f5f4322010-06-09 22:56:4834// To test that file_util::Normalize FilePath() deals with NTFS reparse points
35// correctly, we need functions to create and delete reparse points.
36#if defined(OS_WIN)
37typedef struct _REPARSE_DATA_BUFFER {
38 ULONG ReparseTag;
39 USHORT ReparseDataLength;
40 USHORT Reserved;
41 union {
42 struct {
43 USHORT SubstituteNameOffset;
44 USHORT SubstituteNameLength;
45 USHORT PrintNameOffset;
46 USHORT PrintNameLength;
47 ULONG Flags;
48 WCHAR PathBuffer[1];
49 } SymbolicLinkReparseBuffer;
50 struct {
51 USHORT SubstituteNameOffset;
52 USHORT SubstituteNameLength;
53 USHORT PrintNameOffset;
54 USHORT PrintNameLength;
55 WCHAR PathBuffer[1];
56 } MountPointReparseBuffer;
57 struct {
58 UCHAR DataBuffer[1];
59 } GenericReparseBuffer;
60 };
61} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
62
63// Sets a reparse point. |source| will now point to |target|. Returns true if
64// the call succeeds, false otherwise.
65bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
66 std::wstring kPathPrefix = L"\\??\\";
67 std::wstring target_str;
68 // The juction will not work if the target path does not start with \??\ .
69 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
70 target_str += kPathPrefix;
71 target_str += target_path.value();
72 const wchar_t* target = target_str.c_str();
73 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
74 char buffer[2000] = {0};
75 DWORD returned;
76
77 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
78
79 data->ReparseTag = 0xa0000003;
80 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
81
82 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
83 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
84 data->ReparseDataLength = size_target + 4 + 8;
85
86 int data_size = data->ReparseDataLength + 8;
87
88 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
89 NULL, 0, &returned, NULL)) {
90 return false;
91 }
92 return true;
93}
94
95// Delete the reparse point referenced by |source|. Returns true if the call
96// succeeds, false otherwise.
97bool DeleteReparsePoint(HANDLE source) {
98 DWORD returned;
99 REPARSE_DATA_BUFFER data = {0};
100 data.ReparseTag = 0xa0000003;
101 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
102 &returned, NULL)) {
103 return false;
104 }
105 return true;
106}
107#endif
108
[email protected]83d86252010-05-07 18:58:45109const wchar_t bogus_content[] = L"I'm cannon fodder.";
110
[email protected]8199b3a2009-06-09 05:57:38111const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES =
112 static_cast<file_util::FileEnumerator::FILE_TYPE>(
113 file_util::FileEnumerator::FILES |
114 file_util::FileEnumerator::DIRECTORIES);
115
[email protected]ed2f2332008-08-20 15:59:49116// file_util winds up using autoreleased objects on the Mac, so this needs
117// to be a PlatformTest
118class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:38119 protected:
120 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:49121 PlatformTest::SetUp();
initial.commitd7cae122008-07-26 21:49:38122 // Name a subdirectory of the temp directory.
123 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
[email protected]640517f2008-10-30 23:54:04124 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
initial.commitd7cae122008-07-26 21:49:38125
126 // Create a fresh, empty copy of this directory.
127 file_util::Delete(test_dir_, true);
[email protected]640517f2008-10-30 23:54:04128 file_util::CreateDirectory(test_dir_);
initial.commitd7cae122008-07-26 21:49:38129 }
130 virtual void TearDown() {
[email protected]ed2f2332008-08-20 15:59:49131 PlatformTest::TearDown();
initial.commitd7cae122008-07-26 21:49:38132 // Clean up test directory
[email protected]37088fef2008-08-15 17:32:10133 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commitd7cae122008-07-26 21:49:38134 ASSERT_FALSE(file_util::PathExists(test_dir_));
135 }
136
137 // the path to temporary directory used to contain the test operations
[email protected]640517f2008-10-30 23:54:04138 FilePath test_dir_;
initial.commitd7cae122008-07-26 21:49:38139};
140
141// Collects all the results from the given file enumerator, and provides an
142// interface to query whether a given file is present.
143class FindResultCollector {
144 public:
[email protected]53c58042009-08-26 20:00:14145 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
[email protected]0b733222008-12-11 14:55:12146 FilePath cur_file;
147 while (!(cur_file = enumerator.Next()).value().empty()) {
148 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:38149 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:04150 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:38151 << "Same file returned twice";
152
153 // Save for later.
[email protected]640517f2008-10-30 23:54:04154 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:38155 }
156 }
157
158 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:04159 bool HasFile(const FilePath& file) const {
160 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:38161 }
[email protected]640517f2008-10-30 23:54:04162
[email protected]37088fef2008-08-15 17:32:10163 int size() {
[email protected]f35d39b2008-08-15 17:50:10164 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:10165 }
initial.commitd7cae122008-07-26 21:49:38166
167 private:
[email protected]640517f2008-10-30 23:54:04168 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:38169};
170
171// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:04172void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:38173 const std::wstring& contents) {
174 std::ofstream file;
[email protected]640517f2008-10-30 23:54:04175 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:38176 ASSERT_TRUE(file.is_open());
177 file << contents;
178 file.close();
179}
180
181// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:04182std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:10183 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38184 std::wifstream file;
[email protected]640517f2008-10-30 23:54:04185 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
initial.commitd7cae122008-07-26 21:49:38186 EXPECT_TRUE(file.is_open());
187 file.getline(contents, 64);
188 file.close();
189 return std::wstring(contents);
190}
191
[email protected]8541bb82008-08-15 17:45:13192#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38193uint64 FileTimeAsUint64(const FILETIME& ft) {
194 ULARGE_INTEGER u;
195 u.LowPart = ft.dwLowDateTime;
196 u.HighPart = ft.dwHighDateTime;
197 return u.QuadPart;
198}
[email protected]37088fef2008-08-15 17:32:10199#endif
initial.commitd7cae122008-07-26 21:49:38200
201const struct append_case {
202 const wchar_t* path;
203 const wchar_t* ending;
204 const wchar_t* result;
205} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13206#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38207 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
208 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
209 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
210 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
211 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
212 {L"", L"path", L"\\path"},
213 {L"", L"", L"\\"},
[email protected]37088fef2008-08-15 17:32:10214#elif defined(OS_POSIX)
215 {L"/foo/bar", L"path", L"/foo/bar/path"},
216 {L"/foo/bar/", L"path", L"/foo/bar/path"},
217 {L"/foo/bar//", L"path", L"/foo/bar//path"},
218 {L"/foo/bar/", L"", L"/foo/bar/"},
219 {L"/foo/bar", L"", L"/foo/bar/"},
220 {L"", L"path", L"/path"},
221 {L"", L"", L"/"},
222#endif
initial.commitd7cae122008-07-26 21:49:38223};
224
[email protected]1840cfc2010-02-26 15:11:55225#if defined(OS_WIN)
226// This function is deprecated, but still used on Windows.
initial.commitd7cae122008-07-26 21:49:38227TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10228 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38229 const append_case& value = append_cases[i];
230 std::wstring result = value.path;
231 file_util::AppendToPath(&result, value.ending);
232 EXPECT_EQ(value.result, result);
233 }
234
235#ifdef NDEBUG
236 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
237#endif
238}
[email protected]1840cfc2010-02-26 15:11:55239#endif // defined(OS_WIN)
240
initial.commitd7cae122008-07-26 21:49:38241static const struct filename_case {
242 const wchar_t* path;
243 const wchar_t* filename;
244} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13245#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38246 {L"c:\\colon\\backslash", L"backslash"},
247 {L"c:\\colon\\backslash\\", L""},
248 {L"\\\\filename.exe", L"filename.exe"},
249 {L"filename.exe", L"filename.exe"},
250 {L"", L""},
251 {L"\\\\\\", L""},
252 {L"c:/colon/backslash", L"backslash"},
253 {L"c:/colon/backslash/", L""},
254 {L"//////", L""},
255 {L"///filename.exe", L"filename.exe"},
[email protected]37088fef2008-08-15 17:32:10256#elif defined(OS_POSIX)
257 {L"/foo/bar", L"bar"},
258 {L"/foo/bar/", L""},
259 {L"/filename.exe", L"filename.exe"},
260 {L"filename.exe", L"filename.exe"},
261 {L"", L""},
262 {L"/", L""},
263#endif
initial.commitd7cae122008-07-26 21:49:38264};
265
[email protected]8d19c7d2010-07-01 23:19:02266#if defined(OS_WIN)
267// This function is deprecated on non-Windows.
initial.commitd7cae122008-07-26 21:49:38268TEST_F(FileUtilTest, GetFilenameFromPath) {
[email protected]37088fef2008-08-15 17:32:10269 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38270 const filename_case& value = filename_cases[i];
271 std::wstring result = file_util::GetFilenameFromPath(value.path);
272 EXPECT_EQ(value.filename, result);
273 }
274}
[email protected]8d19c7d2010-07-01 23:19:02275#endif
initial.commitd7cae122008-07-26 21:49:38276
277// Test finding the file type from a path name
278static const struct extension_case {
279 const wchar_t* path;
280 const wchar_t* extension;
281} extension_cases[] = {
[email protected]8541bb82008-08-15 17:45:13282#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38283 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
284 {L"C:\\colon\\backslash\\filename.", L""},
285 {L"C:\\colon\\backslash\\filename", L""},
286 {L"C:\\colon\\backslash\\", L""},
287 {L"C:\\colon\\backslash.\\", L""},
288 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
[email protected]37088fef2008-08-15 17:32:10289#elif defined(OS_POSIX)
290 {L"/foo/bar/filename.extension", L"extension"},
291 {L"/foo/bar/filename.", L""},
292 {L"/foo/bar/filename", L""},
293 {L"/foo/bar/", L""},
294 {L"/foo/bar./", L""},
295 {L"/foo/bar/filename.extension.extension2", L"extension2"},
296 {L".", L""},
297 {L"..", L""},
298 {L"./foo", L""},
299 {L"./foo.extension", L"extension"},
300 {L"/foo.extension1/bar.extension2", L"extension2"},
301#endif
initial.commitd7cae122008-07-26 21:49:38302};
303
[email protected]63597e4e2010-07-08 17:49:05304#if defined(OS_WIN)
305// This function has been deprecated on non-Windows.
initial.commitd7cae122008-07-26 21:49:38306TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10307 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38308 const extension_case& ext = extension_cases[i];
309 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
310 EXPECT_EQ(ext.extension, fext);
311 }
312}
[email protected]63597e4e2010-07-08 17:49:05313#endif
initial.commitd7cae122008-07-26 21:49:38314
315// Test finding the directory component of a path
316static const struct dir_case {
317 const wchar_t* full_path;
318 const wchar_t* directory;
319} dir_cases[] = {
[email protected]8541bb82008-08-15 17:45:13320#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38321 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
322 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
323 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
324 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
325 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
326 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
[email protected]2c8088a42009-10-15 05:00:25327 {L"C:\\", L"C:\\"},
[email protected]37088fef2008-08-15 17:32:10328#elif defined(OS_POSIX)
329 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
330 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
331 {L"/foo/bar/", L"/foo/bar"},
332 {L"/foo/bar//", L"/foo/bar"},
333 {L"/foo/bar", L"/foo"},
334 {L"/foo/bar./", L"/foo/bar."},
335 {L"/", L"/"},
336 {L".", L"."},
[email protected]53c58042009-08-26 20:00:14337 {L"..", L"."}, // yes, ".." technically lives in "."
[email protected]37088fef2008-08-15 17:32:10338#endif
initial.commitd7cae122008-07-26 21:49:38339};
340
[email protected]a18f79a2010-02-23 12:52:11341#if defined(OS_WIN)
342// This function is deprecated, and only exists on Windows anymore.
initial.commitd7cae122008-07-26 21:49:38343TEST_F(FileUtilTest, GetDirectoryFromPath) {
[email protected]37088fef2008-08-15 17:32:10344 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38345 const dir_case& dir = dir_cases[i];
346 const std::wstring parent =
347 file_util::GetDirectoryFromPath(dir.full_path);
348 EXPECT_EQ(dir.directory, parent);
349 }
350}
[email protected]a18f79a2010-02-23 12:52:11351#endif
initial.commitd7cae122008-07-26 21:49:38352
[email protected]2ec79f72010-06-10 13:05:26353// Flaky, http://crbug.com/46246
[email protected]552b3152010-06-10 12:40:52354TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commitd7cae122008-07-26 21:49:38355 // Create old file (that we don't want to count)
[email protected]85786f92009-04-18 00:42:48356 FilePath old_file_name = test_dir_.Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commitd7cae122008-07-26 21:49:38357 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
358
359 // Age to perfection
[email protected]4b7743de2009-04-21 01:50:39360#if defined(OS_WIN)
[email protected]2126577b2009-04-23 15:05:19361 PlatformThread::Sleep(100);
[email protected]4b7743de2009-04-21 01:50:39362#elif defined(OS_POSIX)
363 // We need to wait at least one second here because the precision of
364 // file creation time is one second.
[email protected]2126577b2009-04-23 15:05:19365 PlatformThread::Sleep(1500);
[email protected]4b7743de2009-04-21 01:50:39366#endif
initial.commitd7cae122008-07-26 21:49:38367
368 // Establish our cutoff time
[email protected]85786f92009-04-18 00:42:48369 base::Time now(base::Time::NowFromSystemTime());
370 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38371
372 // Create a new file (that we do want to count)
[email protected]85786f92009-04-18 00:42:48373 FilePath new_file_name = test_dir_.Append(FILE_PATH_LITERAL("New File.txt"));
initial.commitd7cae122008-07-26 21:49:38374 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
375
376 // We should see only the new file.
[email protected]85786f92009-04-18 00:42:48377 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38378
379 // Delete new file, we should see no files after cutoff now
380 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]85786f92009-04-18 00:42:48381 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, now));
initial.commitd7cae122008-07-26 21:49:38382}
383
[email protected]c2c132c62010-03-24 21:56:26384TEST_F(FileUtilTest, FileAndDirectorySize) {
385 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
386 // should return 53 bytes.
387 FilePath file_01 = test_dir_.Append(FPL("The file 01.txt"));
388 CreateTextFile(file_01, L"12345678901234567890");
389 int64 size_f1 = 0;
390 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
391 EXPECT_EQ(20ll, size_f1);
392
393 FilePath subdir_path = test_dir_.Append(FPL("Level2"));
394 file_util::CreateDirectory(subdir_path);
395
396 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
397 CreateTextFile(file_02, L"123456789012345678901234567890");
398 int64 size_f2 = 0;
399 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
400 EXPECT_EQ(30ll, size_f2);
401
402 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
403 file_util::CreateDirectory(subsubdir_path);
404
405 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
406 CreateTextFile(file_03, L"123");
407
408 int64 computed_size = file_util::ComputeDirectorySize(test_dir_);
409 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
[email protected]a04876b92010-06-11 22:53:43410
411 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("The file*"));
412 EXPECT_EQ(size_f1, computed_size);
413
414 computed_size = file_util::ComputeFilesSize(test_dir_, FPL("bla*"));
415 EXPECT_EQ(0, computed_size);
[email protected]c2c132c62010-03-24 21:56:26416}
417
[email protected]6f5f4322010-06-09 22:56:48418TEST_F(FileUtilTest, NormalizeFilePathBasic) {
419 // Create a directory under the test dir. Because we create it,
420 // we know it is not a link.
421 FilePath file_a_path = test_dir_.Append(FPL("file_a"));
422 FilePath dir_path = test_dir_.Append(FPL("dir"));
423 FilePath file_b_path = dir_path.Append(FPL("file_b"));
424 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57425
[email protected]6f5f4322010-06-09 22:56:48426 FilePath normalized_file_a_path, normalized_file_b_path;
427 ASSERT_FALSE(file_util::PathExists(file_a_path));
428 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
429 &normalized_file_a_path))
[email protected]e7afe2452010-08-22 16:19:13430 << "NormalizeFilePath() should fail on nonexistent paths.";
[email protected]6f5f4322010-06-09 22:56:48431
432 CreateTextFile(file_a_path, bogus_content);
433 ASSERT_TRUE(file_util::PathExists(file_a_path));
434 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
435 &normalized_file_a_path));
436
437 CreateTextFile(file_b_path, bogus_content);
438 ASSERT_TRUE(file_util::PathExists(file_b_path));
439 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
440 &normalized_file_b_path));
441
442 // Beacuse this test created |dir_path|, we know it is not a link
443 // or junction. So, the real path of the directory holding file a
444 // must be the parent of the path holding file b.
445 ASSERT_TRUE(normalized_file_a_path.DirName()
446 .IsParent(normalized_file_b_path.DirName()));
447}
448
449#if defined(OS_WIN)
450
451TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
452 // Build the following directory structure:
453 //
454 // test_dir_
455 // |-> base_a
456 // | |-> sub_a
457 // | |-> file.txt
458 // | |-> long_name___... (Very long name.)
459 // | |-> sub_long
460 // | |-> deep.txt
461 // |-> base_b
462 // |-> to_sub_a (reparse point to test_dir_\base_a\sub_a)
463 // |-> to_base_b (reparse point to test_dir_\base_b)
464 // |-> to_sub_long (reparse point to test_dir_\sub_a\long_name_\sub_long)
465
466 FilePath base_a = test_dir_.Append(FPL("base_a"));
467 ASSERT_TRUE(file_util::CreateDirectory(base_a));
468
469 FilePath sub_a = base_a.Append(FPL("sub_a"));
470 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
471
472 FilePath file_txt = sub_a.Append(FPL("file.txt"));
473 CreateTextFile(file_txt, bogus_content);
474
475 // Want a directory whose name is long enough to make the path to the file
476 // inside just under MAX_PATH chars. This will be used to test that when
477 // a junction expands to a path over MAX_PATH chars in length,
478 // NormalizeFilePath() fails without crashing.
479 FilePath sub_long_rel(FPL("sub_long"));
480 FilePath deep_txt(FPL("deep.txt"));
481
482 int target_length = MAX_PATH;
483 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
484 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52485 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48486 // the resulting path is still long enough to hit the failing case in
487 // NormalizePath().
488 const int kCreateDirLimit = 4;
489 target_length -= kCreateDirLimit;
490 FilePath::StringType long_name_str = FPL("long_name_");
491 long_name_str.resize(target_length, '_');
492
493 FilePath long_name = sub_a.Append(FilePath(long_name_str));
494 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
495 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
496
497 FilePath sub_long = deep_file.DirName();
498 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
499 CreateTextFile(deep_file, bogus_content);
500
501 FilePath base_b = test_dir_.Append(FPL("base_b"));
502 ASSERT_TRUE(file_util::CreateDirectory(base_b));
503
504 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
505 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
506 ScopedHandle reparse_to_sub_a(
507 ::CreateFile(to_sub_a.value().c_str(),
508 FILE_ALL_ACCESS,
509 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
510 NULL,
511 OPEN_EXISTING,
512 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
513 NULL));
[email protected]094f9762010-08-03 03:51:56514 ASSERT_TRUE(reparse_to_sub_a.IsValid());
[email protected]6f5f4322010-06-09 22:56:48515 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
516
517 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
518 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
519 ScopedHandle reparse_to_base_b(
520 ::CreateFile(to_base_b.value().c_str(),
521 FILE_ALL_ACCESS,
522 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
523 NULL,
524 OPEN_EXISTING,
525 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
526 NULL));
[email protected]094f9762010-08-03 03:51:56527 ASSERT_TRUE(reparse_to_base_b.IsValid());
[email protected]6f5f4322010-06-09 22:56:48528 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
529
530 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
531 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
532 ScopedHandle reparse_to_sub_long(
533 ::CreateFile(to_sub_long.value().c_str(),
534 FILE_ALL_ACCESS,
535 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
536 NULL,
537 OPEN_EXISTING,
538 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
539 NULL));
[email protected]094f9762010-08-03 03:51:56540 ASSERT_TRUE(reparse_to_sub_long.IsValid());
[email protected]6f5f4322010-06-09 22:56:48541 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
542
543 // Normalize a junction free path: base_a\sub_a\file.txt .
544 FilePath normalized_path;
545 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
546 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
547
548 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
549 // the junction to_sub_a.
550 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
551 &normalized_path));
552 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
553
554 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
555 // normalized to exclude junctions to_base_b and to_sub_a .
556 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
557 .Append(FPL("to_base_b"))
558 .Append(FPL("to_sub_a"))
559 .Append(FPL("file.txt")),
560 &normalized_path));
561 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
562
563 // A long enough path will cause NormalizeFilePath() to fail. Make a long
564 // path using to_base_b many times, and check that paths long enough to fail
565 // do not cause a crash.
566 FilePath long_path = base_b;
567 const int kLengthLimit = MAX_PATH + 200;
568 while (long_path.value().length() <= kLengthLimit) {
569 long_path = long_path.Append(FPL("to_base_b"));
570 }
571 long_path = long_path.Append(FPL("to_sub_a"))
572 .Append(FPL("file.txt"));
573
574 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
575
576 // Normalizing the junction to deep.txt should fail, because the expanded
577 // path to deep.txt is longer than MAX_PATH.
578 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
579 &normalized_path));
580
581 // Delete the reparse points, and see that NormalizeFilePath() fails
582 // to traverse them.
583 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
584 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
585 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
586
587 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
588 &normalized_path));
589}
590
591#endif // defined(OS_WIN)
592
593// The following test of NormalizeFilePath() require that we create a symlink.
594// This can not be done on windows before vista. On vista, creating a symlink
595// requires privilege "SeCreateSymbolicLinkPrivilege".
596// TODO(skerner): Investigate the possibility of giving base_unittests the
597// privileges required to create a symlink.
598#if defined(OS_POSIX)
599
600bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
601 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
602}
603
604TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
605 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57606
607 // Link one file to another.
[email protected]6f5f4322010-06-09 22:56:48608 FilePath link_from = test_dir_.Append(FPL("from_file"));
609 FilePath link_to = test_dir_.Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57610 CreateTextFile(link_to, bogus_content);
611
[email protected]6f5f4322010-06-09 22:56:48612 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57613 << "Failed to create file symlink.";
614
[email protected]6f5f4322010-06-09 22:56:48615 // Check that NormalizeFilePath sees the link.
616 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57617 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48618 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
619 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57620
621 // Link to a directory.
[email protected]6f5f4322010-06-09 22:56:48622 link_from = test_dir_.Append(FPL("from_dir"));
623 link_to = test_dir_.Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57624 file_util::CreateDirectory(link_to);
625
[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 directory symlink.";
628
[email protected]6f5f4322010-06-09 22:56:48629 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
630 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57631
[email protected]6f5f4322010-06-09 22:56:48632 // Test that a loop in the links causes NormalizeFilePath() to return false.
633 link_from = test_dir_.Append(FPL("link_a"));
634 link_to = test_dir_.Append(FPL("link_b"));
635 ASSERT_TRUE(MakeSymlink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57636 << "Failed to create loop symlink a.";
[email protected]6f5f4322010-06-09 22:56:48637 ASSERT_TRUE(MakeSymlink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57638 << "Failed to create loop symlink b.";
639
640 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48641 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57642}
643#endif // defined(OS_POSIX)
644
[email protected]83d86252010-05-07 18:58:45645TEST_F(FileUtilTest, DeleteNonExistent) {
646 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar");
647 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36648
[email protected]83d86252010-05-07 18:58:45649 EXPECT_TRUE(file_util::Delete(non_existent, false));
650 ASSERT_FALSE(file_util::PathExists(non_existent));
651 EXPECT_TRUE(file_util::Delete(non_existent, true));
652 ASSERT_FALSE(file_util::PathExists(non_existent));
653}
654
655TEST_F(FileUtilTest, DeleteFile) {
656 // Create a file
657 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt"));
658 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38659 ASSERT_TRUE(file_util::PathExists(file_name));
660
[email protected]83d86252010-05-07 18:58:45661 // Make sure it's deleted
662 EXPECT_TRUE(file_util::Delete(file_name, false));
663 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36664
[email protected]83d86252010-05-07 18:58:45665 // Test recursive case, create a new file
666 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt"));
667 CreateTextFile(file_name, bogus_content);
668 ASSERT_TRUE(file_util::PathExists(file_name));
669
670 // Make sure it's deleted
671 EXPECT_TRUE(file_util::Delete(file_name, true));
672 EXPECT_FALSE(file_util::PathExists(file_name));
673}
674
675#if defined(OS_WIN)
676// Tests that the Delete function works for wild cards, especially
677// with the recursion flag. Also coincidentally tests PathExists.
678// TODO(erikkay): see if anyone's actually using this feature of the API
679TEST_F(FileUtilTest, DeleteWildCard) {
680 // Create a file and a directory
681 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt"));
682 CreateTextFile(file_name, bogus_content);
683 ASSERT_TRUE(file_util::PathExists(file_name));
684
685 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir"));
686 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38687 ASSERT_TRUE(file_util::PathExists(subdir_path));
688
[email protected]83d86252010-05-07 18:58:45689 // Create the wildcard path
[email protected]640517f2008-10-30 23:54:04690 FilePath directory_contents = test_dir_;
[email protected]83d86252010-05-07 18:58:45691 directory_contents = directory_contents.Append(FPL("*"));
692
initial.commitd7cae122008-07-26 21:49:38693 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45694 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10695 EXPECT_FALSE(file_util::PathExists(file_name));
696 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40697
[email protected]3a51cfdd2010-05-07 00:05:36698 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45699 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40700 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36701 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40702}
703
[email protected]83d86252010-05-07 18:58:45704// TODO(erikkay): see if anyone's actually using this feature of the API
705TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
706 // Create a file and a directory
707 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard"));
708 file_util::CreateDirectory(subdir_path);
709 ASSERT_TRUE(file_util::PathExists(subdir_path));
710
711 // Create the wildcard path
712 FilePath directory_contents = subdir_path;
713 directory_contents = directory_contents.Append(FPL("*"));
714
715 // Delete non-recursively and check nothing got deleted
716 EXPECT_TRUE(file_util::Delete(directory_contents, false));
717 EXPECT_TRUE(file_util::PathExists(subdir_path));
718
719 // Delete recursively and check nothing got deleted
720 EXPECT_TRUE(file_util::Delete(directory_contents, true));
721 EXPECT_TRUE(file_util::PathExists(subdir_path));
722}
723#endif
724
725// Tests non-recursive Delete() for a directory.
726TEST_F(FileUtilTest, DeleteDirNonRecursive) {
727 // Create a subdirectory and put a file and two directories inside.
728 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive"));
729 file_util::CreateDirectory(test_subdir);
730 ASSERT_TRUE(file_util::PathExists(test_subdir));
731
732 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
733 CreateTextFile(file_name, bogus_content);
734 ASSERT_TRUE(file_util::PathExists(file_name));
735
736 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
737 file_util::CreateDirectory(subdir_path1);
738 ASSERT_TRUE(file_util::PathExists(subdir_path1));
739
740 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
741 file_util::CreateDirectory(subdir_path2);
742 ASSERT_TRUE(file_util::PathExists(subdir_path2));
743
744 // Delete non-recursively and check that the empty dir got deleted
745 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
746 EXPECT_FALSE(file_util::PathExists(subdir_path2));
747
748 // Delete non-recursively and check that nothing got deleted
749 EXPECT_FALSE(file_util::Delete(test_subdir, false));
750 EXPECT_TRUE(file_util::PathExists(test_subdir));
751 EXPECT_TRUE(file_util::PathExists(file_name));
752 EXPECT_TRUE(file_util::PathExists(subdir_path1));
753}
754
755// Tests recursive Delete() for a directory.
756TEST_F(FileUtilTest, DeleteDirRecursive) {
757 // Create a subdirectory and put a file and two directories inside.
758 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive"));
759 file_util::CreateDirectory(test_subdir);
760 ASSERT_TRUE(file_util::PathExists(test_subdir));
761
762 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
763 CreateTextFile(file_name, bogus_content);
764 ASSERT_TRUE(file_util::PathExists(file_name));
765
766 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
767 file_util::CreateDirectory(subdir_path1);
768 ASSERT_TRUE(file_util::PathExists(subdir_path1));
769
770 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
771 file_util::CreateDirectory(subdir_path2);
772 ASSERT_TRUE(file_util::PathExists(subdir_path2));
773
774 // Delete recursively and check that the empty dir got deleted
775 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
776 EXPECT_FALSE(file_util::PathExists(subdir_path2));
777
778 // Delete recursively and check that everything got deleted
779 EXPECT_TRUE(file_util::Delete(test_subdir, true));
780 EXPECT_FALSE(file_util::PathExists(file_name));
781 EXPECT_FALSE(file_util::PathExists(subdir_path1));
782 EXPECT_FALSE(file_util::PathExists(test_subdir));
783}
784
[email protected]bc6a9012009-10-15 01:11:44785TEST_F(FileUtilTest, MoveFileNew) {
786 // Create a file
787 FilePath file_name_from =
788 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
789 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
790 ASSERT_TRUE(file_util::PathExists(file_name_from));
791
792 // The destination
793 FilePath file_name_to =
794 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
795 ASSERT_FALSE(file_util::PathExists(file_name_to));
796
797 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
798
799 // Check everything has been moved.
800 EXPECT_FALSE(file_util::PathExists(file_name_from));
801 EXPECT_TRUE(file_util::PathExists(file_name_to));
802}
803
804TEST_F(FileUtilTest, MoveFileExists) {
805 // Create a file
806 FilePath file_name_from =
807 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
808 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
809 ASSERT_TRUE(file_util::PathExists(file_name_from));
810
811 // The destination name
812 FilePath file_name_to =
813 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
814 CreateTextFile(file_name_to, L"Old file content");
815 ASSERT_TRUE(file_util::PathExists(file_name_to));
816
817 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
818
819 // Check everything has been moved.
820 EXPECT_FALSE(file_util::PathExists(file_name_from));
821 EXPECT_TRUE(file_util::PathExists(file_name_to));
822 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
823}
824
825TEST_F(FileUtilTest, MoveFileDirExists) {
826 // Create a file
827 FilePath file_name_from =
828 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
829 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
830 ASSERT_TRUE(file_util::PathExists(file_name_from));
831
832 // The destination directory
833 FilePath dir_name_to =
834 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
835 file_util::CreateDirectory(dir_name_to);
836 ASSERT_TRUE(file_util::PathExists(dir_name_to));
837
838 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
839}
840
841
[email protected]abbc5732009-10-13 17:57:27842TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38843 // Create a directory
[email protected]640517f2008-10-30 23:54:04844 FilePath dir_name_from =
845 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
846 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38847 ASSERT_TRUE(file_util::PathExists(dir_name_from));
848
849 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04850 FilePath file_name_from =
851 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38852 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
853 ASSERT_TRUE(file_util::PathExists(file_name_from));
854
855 // Move the directory
[email protected]640517f2008-10-30 23:54:04856 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
857 FilePath file_name_to =
858 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38859
860 ASSERT_FALSE(file_util::PathExists(dir_name_to));
861
862 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
863
864 // Check everything has been moved.
865 EXPECT_FALSE(file_util::PathExists(dir_name_from));
866 EXPECT_FALSE(file_util::PathExists(file_name_from));
867 EXPECT_TRUE(file_util::PathExists(dir_name_to));
868 EXPECT_TRUE(file_util::PathExists(file_name_to));
869}
870
[email protected]abbc5732009-10-13 17:57:27871TEST_F(FileUtilTest, MoveExist) {
872 // Create a directory
873 FilePath dir_name_from =
874 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
875 file_util::CreateDirectory(dir_name_from);
876 ASSERT_TRUE(file_util::PathExists(dir_name_from));
877
878 // Create a file under the directory
879 FilePath file_name_from =
880 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
881 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
882 ASSERT_TRUE(file_util::PathExists(file_name_from));
883
884 // Move the directory
885 FilePath dir_name_exists =
886 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
887
888 FilePath dir_name_to =
889 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
890 FilePath file_name_to =
891 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
892
893 // Create the destination directory.
894 file_util::CreateDirectory(dir_name_exists);
895 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
896
897 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
898
899 // Check everything has been moved.
900 EXPECT_FALSE(file_util::PathExists(dir_name_from));
901 EXPECT_FALSE(file_util::PathExists(file_name_from));
902 EXPECT_TRUE(file_util::PathExists(dir_name_to));
903 EXPECT_TRUE(file_util::PathExists(file_name_to));
904}
905
906TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38907 // Create a directory.
[email protected]640517f2008-10-30 23:54:04908 FilePath dir_name_from =
909 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
910 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38911 ASSERT_TRUE(file_util::PathExists(dir_name_from));
912
913 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04914 FilePath file_name_from =
915 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38916 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
917 ASSERT_TRUE(file_util::PathExists(file_name_from));
918
919 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04920 FilePath subdir_name_from =
921 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
922 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38923 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
924
925 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04926 FilePath file_name2_from =
927 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38928 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
929 ASSERT_TRUE(file_util::PathExists(file_name2_from));
930
931 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04932 FilePath dir_name_to =
933 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
934 FilePath file_name_to =
935 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
936 FilePath subdir_name_to =
937 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
938 FilePath file_name2_to =
939 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38940
941 ASSERT_FALSE(file_util::PathExists(dir_name_to));
942
943 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
944
945 // Check everything has been copied.
946 EXPECT_TRUE(file_util::PathExists(dir_name_from));
947 EXPECT_TRUE(file_util::PathExists(file_name_from));
948 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
949 EXPECT_TRUE(file_util::PathExists(file_name2_from));
950 EXPECT_TRUE(file_util::PathExists(dir_name_to));
951 EXPECT_TRUE(file_util::PathExists(file_name_to));
952 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
953 EXPECT_TRUE(file_util::PathExists(file_name2_to));
954}
955
[email protected]abbc5732009-10-13 17:57:27956TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
957 // Create a directory.
958 FilePath dir_name_from =
959 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
960 file_util::CreateDirectory(dir_name_from);
961 ASSERT_TRUE(file_util::PathExists(dir_name_from));
962
963 // Create a file under the directory.
964 FilePath file_name_from =
965 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
966 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
967 ASSERT_TRUE(file_util::PathExists(file_name_from));
968
969 // Create a subdirectory.
970 FilePath subdir_name_from =
971 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
972 file_util::CreateDirectory(subdir_name_from);
973 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
974
975 // Create a file under the subdirectory.
976 FilePath file_name2_from =
977 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
978 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
979 ASSERT_TRUE(file_util::PathExists(file_name2_from));
980
981 // Copy the directory recursively.
982 FilePath dir_name_exists =
983 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
984
985 FilePath dir_name_to =
986 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
987 FilePath file_name_to =
988 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
989 FilePath subdir_name_to =
990 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
991 FilePath file_name2_to =
992 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
993
994 // Create the destination directory.
995 file_util::CreateDirectory(dir_name_exists);
996 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
997
998 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
999
1000 // Check everything has been copied.
1001 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1002 EXPECT_TRUE(file_util::PathExists(file_name_from));
1003 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1004 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1005 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1006 EXPECT_TRUE(file_util::PathExists(file_name_to));
1007 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1008 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1009}
1010
1011TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381012 // Create a directory.
[email protected]640517f2008-10-30 23:54:041013 FilePath dir_name_from =
1014 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1015 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381016 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1017
1018 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041019 FilePath file_name_from =
1020 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381021 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1022 ASSERT_TRUE(file_util::PathExists(file_name_from));
1023
1024 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041025 FilePath subdir_name_from =
1026 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1027 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381028 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1029
1030 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041031 FilePath file_name2_from =
1032 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381033 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1034 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1035
1036 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041037 FilePath dir_name_to =
1038 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1039 FilePath file_name_to =
1040 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1041 FilePath subdir_name_to =
1042 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381043
1044 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1045
1046 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1047
1048 // Check everything has been copied.
1049 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1050 EXPECT_TRUE(file_util::PathExists(file_name_from));
1051 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1052 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1053 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1054 EXPECT_TRUE(file_util::PathExists(file_name_to));
1055 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1056}
1057
[email protected]abbc5732009-10-13 17:57:271058TEST_F(FileUtilTest, CopyDirectoryExists) {
1059 // Create a directory.
1060 FilePath dir_name_from =
1061 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1062 file_util::CreateDirectory(dir_name_from);
1063 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1064
1065 // Create a file under the directory.
1066 FilePath file_name_from =
1067 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1068 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1069 ASSERT_TRUE(file_util::PathExists(file_name_from));
1070
1071 // Create a subdirectory.
1072 FilePath subdir_name_from =
1073 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1074 file_util::CreateDirectory(subdir_name_from);
1075 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1076
1077 // Create a file under the subdirectory.
1078 FilePath file_name2_from =
1079 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1080 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1081 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1082
1083 // Copy the directory not recursively.
1084 FilePath dir_name_to =
1085 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1086 FilePath file_name_to =
1087 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1088 FilePath subdir_name_to =
1089 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1090
1091 // Create the destination directory.
1092 file_util::CreateDirectory(dir_name_to);
1093 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1094
1095 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1096
1097 // Check everything has been copied.
1098 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1099 EXPECT_TRUE(file_util::PathExists(file_name_from));
1100 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1101 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1102 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1103 EXPECT_TRUE(file_util::PathExists(file_name_to));
1104 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1105}
1106
[email protected]bc6a9012009-10-15 01:11:441107TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1108 // Create a file
1109 FilePath file_name_from =
1110 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1111 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1112 ASSERT_TRUE(file_util::PathExists(file_name_from));
1113
1114 // The destination name
1115 FilePath file_name_to =
1116 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1117 ASSERT_FALSE(file_util::PathExists(file_name_to));
1118
1119 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1120
1121 // Check the has been copied
1122 EXPECT_TRUE(file_util::PathExists(file_name_to));
1123}
1124
1125TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1126 // Create a file
1127 FilePath file_name_from =
1128 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1129 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1130 ASSERT_TRUE(file_util::PathExists(file_name_from));
1131
1132 // The destination name
1133 FilePath file_name_to =
1134 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1135 CreateTextFile(file_name_to, L"Old file content");
1136 ASSERT_TRUE(file_util::PathExists(file_name_to));
1137
1138 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1139
1140 // Check the has been copied
1141 EXPECT_TRUE(file_util::PathExists(file_name_to));
1142 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1143}
1144
1145TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1146 // Create a file
1147 FilePath file_name_from =
1148 test_dir_.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1149 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1150 ASSERT_TRUE(file_util::PathExists(file_name_from));
1151
1152 // The destination
1153 FilePath dir_name_to =
1154 test_dir_.Append(FILE_PATH_LITERAL("Destination"));
1155 file_util::CreateDirectory(dir_name_to);
1156 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1157 FilePath file_name_to =
1158 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1159
1160 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1161
1162 // Check the has been copied
1163 EXPECT_TRUE(file_util::PathExists(file_name_to));
1164}
1165
initial.commitd7cae122008-07-26 21:49:381166TEST_F(FileUtilTest, CopyFile) {
1167 // Create a directory
[email protected]640517f2008-10-30 23:54:041168 FilePath dir_name_from =
1169 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1170 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381171 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1172
1173 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041174 FilePath file_name_from =
1175 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381176 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1177 CreateTextFile(file_name_from, file_contents);
1178 ASSERT_TRUE(file_util::PathExists(file_name_from));
1179
1180 // Copy the file.
[email protected]640517f2008-10-30 23:54:041181 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381182 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111183
[email protected]37088fef2008-08-15 17:32:101184 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141185 FilePath dest_file2(dir_name_from);
1186 dest_file2 = dest_file2.AppendASCII("..");
1187 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1188 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1189
1190 FilePath dest_file2_test(dir_name_from);
1191 dest_file2_test = dest_file2_test.DirName();
1192 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381193
1194 // Check everything has been copied.
1195 EXPECT_TRUE(file_util::PathExists(file_name_from));
1196 EXPECT_TRUE(file_util::PathExists(dest_file));
1197 const std::wstring read_contents = ReadTextFile(dest_file);
1198 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141199 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1200 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381201}
1202
[email protected]37088fef2008-08-15 17:32:101203// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131204#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381205TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]640517f2008-10-30 23:54:041206 FilePath file_name = test_dir_.Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381207
1208 SYSTEMTIME start_time;
1209 GetLocalTime(&start_time);
1210 Sleep(100);
1211 CreateTextFile(file_name, L"New file!");
1212 Sleep(100);
1213 SYSTEMTIME end_time;
1214 GetLocalTime(&end_time);
1215
1216 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041217 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381218
1219 FILETIME start_filetime;
1220 SystemTimeToFileTime(&start_time, &start_filetime);
1221 FILETIME end_filetime;
1222 SystemTimeToFileTime(&end_time, &end_filetime);
1223 FILETIME file_creation_filetime;
1224 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1225
1226 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1227 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1228 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1229
1230 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1231 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1232 "end time: " << FileTimeAsUint64(end_filetime);
1233
[email protected]640517f2008-10-30 23:54:041234 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381235}
[email protected]37088fef2008-08-15 17:32:101236#endif
initial.commitd7cae122008-07-26 21:49:381237
[email protected]ed2f2332008-08-20 15:59:491238// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041239// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491240typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381241
[email protected]ed2f2332008-08-20 15:59:491242TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041243 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381244 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041245 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1246 .Append(FILE_PATH_LITERAL("data"))
1247 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381248 ASSERT_TRUE(file_util::PathExists(data_dir));
1249
[email protected]640517f2008-10-30 23:54:041250 FilePath original_file =
1251 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1252 FilePath same_file =
1253 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1254 FilePath same_length_file =
1255 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1256 FilePath different_file =
1257 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1258 FilePath different_first_file =
1259 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1260 FilePath different_last_file =
1261 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1262 FilePath empty1_file =
1263 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1264 FilePath empty2_file =
1265 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1266 FilePath shortened_file =
1267 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1268 FilePath binary_file =
1269 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1270 FilePath binary_file_same =
1271 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1272 FilePath binary_file_diff =
1273 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381274
1275 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1276 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1277 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1278 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221279 EXPECT_FALSE(file_util::ContentsEqual(
1280 FilePath(FILE_PATH_LITERAL("bogusname")),
1281 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381282 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1283 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1284 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1285 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1286 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1287 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1288 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1289}
1290
[email protected]b81637c32009-06-26 21:17:241291TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1292 FilePath data_dir;
1293 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1294 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1295 .Append(FILE_PATH_LITERAL("data"))
1296 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1297 ASSERT_TRUE(file_util::PathExists(data_dir));
1298
1299 FilePath original_file =
1300 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1301 FilePath same_file =
1302 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1303 FilePath crlf_file =
1304 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1305 FilePath shortened_file =
1306 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1307 FilePath different_file =
1308 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1309 FilePath different_first_file =
1310 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1311 FilePath different_last_file =
1312 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1313 FilePath first1_file =
1314 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1315 FilePath first2_file =
1316 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1317 FilePath empty1_file =
1318 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1319 FilePath empty2_file =
1320 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1321 FilePath blank_line_file =
1322 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1323 FilePath blank_line_crlf_file =
1324 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1325
1326 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1327 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1328 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1329 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1330 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1331 different_first_file));
1332 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1333 different_last_file));
1334 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1335 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1336 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1337 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1338 blank_line_crlf_file));
1339}
1340
[email protected]37088fef2008-08-15 17:32:101341// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131342#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381343TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]640517f2008-10-30 23:54:041344 FilePath target_file = test_dir_.Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381345 CreateTextFile(target_file, L"This is the target.");
1346
[email protected]640517f2008-10-30 23:54:041347 FilePath link_file = test_dir_.Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381348
1349 HRESULT result;
1350 IShellLink *shell = NULL;
1351 IPersistFile *persist = NULL;
1352
1353 CoInitialize(NULL);
1354 // Temporarily create a shortcut for test
1355 result = CoCreateInstance(CLSID_ShellLink, NULL,
1356 CLSCTX_INPROC_SERVER, IID_IShellLink,
1357 reinterpret_cast<LPVOID*>(&shell));
1358 EXPECT_TRUE(SUCCEEDED(result));
1359 result = shell->QueryInterface(IID_IPersistFile,
1360 reinterpret_cast<LPVOID*>(&persist));
1361 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041362 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381363 EXPECT_TRUE(SUCCEEDED(result));
1364 result = shell->SetDescription(L"ResolveShortcutTest");
1365 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041366 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381367 EXPECT_TRUE(SUCCEEDED(result));
1368 if (persist)
1369 persist->Release();
1370 if (shell)
1371 shell->Release();
1372
1373 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441374 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381375 EXPECT_TRUE(is_solved);
1376 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441377 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381378 EXPECT_EQ(L"This is the target.", contents);
1379
[email protected]d324ab332008-08-18 16:00:381380 // Cleaning
[email protected]640517f2008-10-30 23:54:041381 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441382 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381383 CoUninitialize();
1384}
1385
1386TEST_F(FileUtilTest, CreateShortcutTest) {
1387 const wchar_t file_contents[] = L"This is another target.";
[email protected]640517f2008-10-30 23:54:041388 FilePath target_file = test_dir_.Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381389 CreateTextFile(target_file, file_contents);
1390
[email protected]640517f2008-10-30 23:54:041391 FilePath link_file = test_dir_.Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381392
1393 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041394 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1395 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501396 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441397 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381398 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441399 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381400 EXPECT_EQ(file_contents, read_contents);
1401
[email protected]640517f2008-10-30 23:54:041402 DeleteFile(target_file.value().c_str());
1403 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381404 CoUninitialize();
1405}
[email protected]2c59af7dca2009-03-11 18:37:481406
1407TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1408 // Create a directory
1409 FilePath dir_name_from =
1410 test_dir_.Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1411 file_util::CreateDirectory(dir_name_from);
1412 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1413
1414 // Create a file under the directory
1415 FilePath file_name_from =
1416 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1417 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1418 ASSERT_TRUE(file_util::PathExists(file_name_from));
1419
1420 // Move the directory by using CopyAndDeleteDirectory
1421 FilePath dir_name_to = test_dir_.Append(
1422 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1423 FilePath file_name_to =
1424 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1425
1426 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1427
1428 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1429
1430 // Check everything has been moved.
1431 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1432 EXPECT_FALSE(file_util::PathExists(file_name_from));
1433 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1434 EXPECT_TRUE(file_util::PathExists(file_name_to));
1435}
[email protected]7d95aae2009-10-09 07:33:391436
1437TEST_F(FileUtilTest, GetTempDirTest) {
1438 static const TCHAR* kTmpKey = _T("TMP");
1439 static const TCHAR* kTmpValues[] = {
1440 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1441 };
1442 // Save the original $TMP.
1443 size_t original_tmp_size;
1444 TCHAR* original_tmp;
1445 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1446 // original_tmp may be NULL.
1447
1448 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1449 FilePath path;
1450 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1451 file_util::GetTempDir(&path);
1452 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1453 " result=" << path.value();
1454 }
1455
1456 // Restore the original $TMP.
1457 if (original_tmp) {
1458 ::_tputenv_s(kTmpKey, original_tmp);
1459 free(original_tmp);
1460 } else {
1461 ::_tputenv_s(kTmpKey, _T(""));
1462 }
1463}
1464#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381465
[email protected]33edeab2009-08-18 16:07:551466TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1467 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391468 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551469 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391470 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1471 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1472 }
1473 for (int i = 0; i < 3; i++)
1474 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1475 for (int i = 0; i < 3; i++)
1476 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1477}
1478
[email protected]33edeab2009-08-18 16:07:551479TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391480 FilePath names[3];
1481 FILE *fps[3];
1482 int i;
1483
1484 // Create; make sure they are open and exist.
1485 for (i = 0; i < 3; ++i) {
1486 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1487 ASSERT_TRUE(fps[i]);
1488 EXPECT_TRUE(file_util::PathExists(names[i]));
1489 }
1490
1491 // Make sure all names are unique.
1492 for (i = 0; i < 3; ++i) {
1493 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1494 }
1495
1496 // Close and delete.
1497 for (i = 0; i < 3; ++i) {
1498 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1499 EXPECT_TRUE(file_util::Delete(names[i], false));
1500 }
initial.commitd7cae122008-07-26 21:49:381501}
1502
1503TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141504 FilePath temp_dir;
1505 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1506 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111507 EXPECT_TRUE(file_util::PathExists(temp_dir));
1508 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381509}
1510
[email protected]b0b3abd92010-04-30 17:00:091511TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1512 FilePath new_dir;
1513 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1514 test_dir_,
1515 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
[email protected]046062e82010-06-30 07:19:111516 &new_dir));
[email protected]b0b3abd92010-04-30 17:00:091517 EXPECT_TRUE(file_util::PathExists(new_dir));
1518 EXPECT_TRUE(test_dir_.IsParent(new_dir));
1519 EXPECT_TRUE(file_util::Delete(new_dir, false));
1520}
1521
[email protected]9e51af92009-02-04 00:58:391522TEST_F(FileUtilTest, GetShmemTempDirTest) {
1523 FilePath dir;
1524 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1525 EXPECT_TRUE(file_util::DirectoryExists(dir));
1526}
1527
initial.commitd7cae122008-07-26 21:49:381528TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041529 FilePath test_root =
1530 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131531#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041532 FilePath test_path =
1533 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101534#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041535 FilePath test_path =
1536 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101537#endif
[email protected]806b9c62008-09-11 16:09:111538
1539 EXPECT_FALSE(file_util::PathExists(test_path));
1540 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1541 EXPECT_TRUE(file_util::PathExists(test_path));
1542 // CreateDirectory returns true if the DirectoryExists returns true.
1543 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1544
1545 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041546 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111547 EXPECT_FALSE(file_util::PathExists(test_path));
1548 CreateTextFile(test_path, L"test file");
1549 EXPECT_TRUE(file_util::PathExists(test_path));
1550 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1551
1552 EXPECT_TRUE(file_util::Delete(test_root, true));
1553 EXPECT_FALSE(file_util::PathExists(test_root));
1554 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411555
1556 // Verify assumptions made by the Windows implementation:
1557 // 1. The current directory always exists.
1558 // 2. The root directory always exists.
1559 ASSERT_TRUE(file_util::DirectoryExists(
1560 FilePath(FilePath::kCurrentDirectory)));
1561 FilePath top_level = test_root;
1562 while (top_level != top_level.DirName()) {
1563 top_level = top_level.DirName();
1564 }
1565 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1566
1567 // Given these assumptions hold, it should be safe to
1568 // test that "creating" these directories succeeds.
1569 EXPECT_TRUE(file_util::CreateDirectory(
1570 FilePath(FilePath::kCurrentDirectory)));
1571 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401572
1573#if defined(OS_WIN)
1574 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1575 FilePath invalid_path =
1576 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1577 if (!file_util::PathExists(invalid_drive)) {
1578 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1579 }
1580#endif
[email protected]806b9c62008-09-11 16:09:111581}
1582
1583TEST_F(FileUtilTest, DetectDirectoryTest) {
1584 // Check a directory
[email protected]640517f2008-10-30 23:54:041585 FilePath test_root =
1586 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111587 EXPECT_FALSE(file_util::PathExists(test_root));
1588 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1589 EXPECT_TRUE(file_util::PathExists(test_root));
1590 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1591
1592 // Check a file
[email protected]640517f2008-10-30 23:54:041593 FilePath test_path =
1594 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111595 EXPECT_FALSE(file_util::PathExists(test_path));
1596 CreateTextFile(test_path, L"test file");
1597 EXPECT_TRUE(file_util::PathExists(test_path));
1598 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1599 EXPECT_TRUE(file_util::Delete(test_path, false));
1600
1601 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381602}
1603
initial.commitd7cae122008-07-26 21:49:381604TEST_F(FileUtilTest, FileEnumeratorTest) {
1605 // Test an empty directory.
[email protected]8199b3a2009-06-09 05:57:381606 file_util::FileEnumerator f0(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121607 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1608 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381609
[email protected]8199b3a2009-06-09 05:57:381610 // Test an empty directory, non-recursively, including "..".
1611 file_util::FileEnumerator f0_dotdot(test_dir_, false,
1612 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1613 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
1614 EXPECT_EQ(test_dir_.Append(FILE_PATH_LITERAL("..")).value(),
1615 f0_dotdot.Next().value());
1616 EXPECT_EQ(FILE_PATH_LITERAL(""),
1617 f0_dotdot.Next().value());
1618
[email protected]37088fef2008-08-15 17:32:101619 // create the directories
[email protected]640517f2008-10-30 23:54:041620 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101621 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]640517f2008-10-30 23:54:041622 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101623 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041624 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101625 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041626
[email protected]37088fef2008-08-15 17:32:101627 // create the files
[email protected]640517f2008-10-30 23:54:041628 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101629 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041630 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101631 CreateTextFile(dir2innerfile, L"");
[email protected]640517f2008-10-30 23:54:041632 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101633 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041634 FilePath file2_rel =
1635 dir2.Append(FilePath::kParentDirectory)
1636 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101637 CreateTextFile(file2_rel, L"");
[email protected]640517f2008-10-30 23:54:041638 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381639
1640 // Only enumerate files.
[email protected]0b733222008-12-11 14:55:121641 file_util::FileEnumerator f1(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381642 file_util::FileEnumerator::FILES);
1643 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101644 EXPECT_TRUE(c1.HasFile(file1));
1645 EXPECT_TRUE(c1.HasFile(file2_abs));
1646 EXPECT_TRUE(c1.HasFile(dir2file));
1647 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1648 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381649
1650 // Only enumerate directories.
[email protected]0b733222008-12-11 14:55:121651 file_util::FileEnumerator f2(test_dir_, true,
initial.commitd7cae122008-07-26 21:49:381652 file_util::FileEnumerator::DIRECTORIES);
1653 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101654 EXPECT_TRUE(c2.HasFile(dir1));
1655 EXPECT_TRUE(c2.HasFile(dir2));
1656 EXPECT_TRUE(c2.HasFile(dir2inner));
1657 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381658
[email protected]3bc9ffaf2008-10-16 02:42:451659 // Only enumerate directories non-recursively.
1660 file_util::FileEnumerator f2_non_recursive(
[email protected]0b733222008-12-11 14:55:121661 test_dir_, false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451662 FindResultCollector c2_non_recursive(f2_non_recursive);
1663 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1664 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1665 EXPECT_EQ(c2_non_recursive.size(), 2);
1666
[email protected]8199b3a2009-06-09 05:57:381667 // Only enumerate directories, non-recursively, including "..".
1668 file_util::FileEnumerator f2_dotdot(
1669 test_dir_, false,
1670 static_cast<file_util::FileEnumerator::FILE_TYPE>(
1671 file_util::FileEnumerator::DIRECTORIES |
1672 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1673 FindResultCollector c2_dotdot(f2_dotdot);
1674 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1675 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
1676 EXPECT_TRUE(c2_dotdot.HasFile(test_dir_.Append(FILE_PATH_LITERAL(".."))));
1677 EXPECT_EQ(c2_dotdot.size(), 3);
1678
initial.commitd7cae122008-07-26 21:49:381679 // Enumerate files and directories.
[email protected]8199b3a2009-06-09 05:57:381680 file_util::FileEnumerator f3(test_dir_, true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381681 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101682 EXPECT_TRUE(c3.HasFile(dir1));
1683 EXPECT_TRUE(c3.HasFile(dir2));
1684 EXPECT_TRUE(c3.HasFile(file1));
1685 EXPECT_TRUE(c3.HasFile(file2_abs));
1686 EXPECT_TRUE(c3.HasFile(dir2file));
1687 EXPECT_TRUE(c3.HasFile(dir2inner));
1688 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1689 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381690
1691 // Non-recursive operation.
[email protected]8199b3a2009-06-09 05:57:381692 file_util::FileEnumerator f4(test_dir_, false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381693 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101694 EXPECT_TRUE(c4.HasFile(dir2));
1695 EXPECT_TRUE(c4.HasFile(dir2));
1696 EXPECT_TRUE(c4.HasFile(file1));
1697 EXPECT_TRUE(c4.HasFile(file2_abs));
1698 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381699
1700 // Enumerate with a pattern.
[email protected]8199b3a2009-06-09 05:57:381701 file_util::FileEnumerator f5(test_dir_, true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121702 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381703 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101704 EXPECT_TRUE(c5.HasFile(dir1));
1705 EXPECT_TRUE(c5.HasFile(dir2));
1706 EXPECT_TRUE(c5.HasFile(dir2file));
1707 EXPECT_TRUE(c5.HasFile(dir2inner));
1708 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1709 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381710
1711 // Make sure the destructor closes the find handle while in the middle of a
1712 // query to allow TearDown to delete the directory.
[email protected]8199b3a2009-06-09 05:57:381713 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121714 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1715 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381716}
license.botbf09a502008-08-24 00:55:551717
[email protected]ee5c29da2009-01-09 22:14:271718TEST_F(FileUtilTest, Contains) {
[email protected]11e53f62009-03-10 18:20:441719 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271720
1721 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571722 if (file_util::PathExists(data_dir)) {
1723 ASSERT_TRUE(file_util::Delete(data_dir, true));
1724 }
[email protected]ee5c29da2009-01-09 22:14:271725 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1726
1727 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1728 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1729 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1730 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1731
1732 // Annoyingly, the directories must actually exist in order for realpath(),
1733 // which Contains() relies on in posix, to work.
1734 ASSERT_TRUE(file_util::CreateDirectory(foo));
1735 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581736 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1737 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1738 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271739
1740 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1741 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1742 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1743 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1744
[email protected]e43eddf12009-12-29 00:32:521745 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271746 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1747#if defined(OS_WIN)
1748 EXPECT_TRUE(file_util::ContainsPath(foo,
1749 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391750 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271751 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521752#elif defined(OS_MACOSX)
1753 // We can't really do this test on OS X since the case-sensitivity of the
1754 // filesystem is configurable.
1755#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271756 EXPECT_FALSE(file_util::ContainsPath(foo,
1757 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271758#endif
1759}
1760
[email protected]ec3d1452010-02-18 10:02:261761TEST_F(FileUtilTest, LastModified) {
1762 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest"));
1763
1764 // Create a fresh, empty copy of this directory.
1765 if (file_util::PathExists(data_dir)) {
1766 ASSERT_TRUE(file_util::Delete(data_dir, true));
1767 }
1768 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1769
1770 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1771 std::string data("hello");
1772 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1773
1774 base::Time modification_time;
1775 // Note that this timestamp is divisible by two (seconds) - FAT stores
1776 // modification times with 2s resolution.
1777 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
1778 &modification_time));
1779 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
[email protected]2f0193c22010-09-03 02:28:371780 base::PlatformFileInfo file_info;
[email protected]ec3d1452010-02-18 10:02:261781 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1782 ASSERT_TRUE(file_info.last_modified == modification_time);
1783}
1784
[email protected]b33f1d92010-05-26 01:40:121785TEST_F(FileUtilTest, IsDirectoryEmpty) {
1786 FilePath empty_dir = test_dir_.Append(FILE_PATH_LITERAL("EmptyDir"));
1787
1788 ASSERT_FALSE(file_util::PathExists(empty_dir));
1789
1790 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1791
1792 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1793
1794 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1795 std::string bar("baz");
1796 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1797
1798 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1799}
1800
[email protected]11b901ee2008-09-10 00:16:281801} // namespace