blob: b76938e6d8ddb48fa44074e9c70117d78011dcc0 [file] [log] [blame]
[email protected]b90d7e802011-01-09 16:32:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]f35d39b2008-08-15 17:50:105#include "build/build_config.h"
6
[email protected]8541bb82008-08-15 17:45:137#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:388#include <windows.h>
initial.commitd7cae122008-07-26 21:49:389#include <shellapi.h>
10#include <shlobj.h>
[email protected]7d95aae2009-10-09 07:33:3911#include <tchar.h>
[email protected]e0785902011-05-19 23:34:1712#include <winioctl.h>
[email protected]37088fef2008-08-15 17:32:1013#endif
initial.commitd7cae122008-07-26 21:49:3814
15#include <fstream>
[email protected]37088fef2008-08-15 17:32:1016#include <set>
initial.commitd7cae122008-07-26 21:49:3817
18#include "base/base_paths.h"
[email protected]640517f2008-10-30 23:54:0419#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3820#include "base/file_util.h"
initial.commitd7cae122008-07-26 21:49:3821#include "base/path_service.h"
[email protected]e0785902011-05-19 23:34:1722#include "base/scoped_temp_dir.h"
[email protected]ce072a72010-12-31 20:02:1623#include "base/threading/platform_thread.h"
[email protected]85786f92009-04-18 00:42:4824#include "base/time.h"
[email protected]047a03f2009-10-07 02:10:2025#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3826#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1527#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3828
[email protected]b90d7e802011-01-09 16:32:2029#if defined(OS_WIN)
30#include "base/win/scoped_handle.h"
31#endif
32
[email protected]4e9f6be2009-04-03 17:17:5833// This macro helps avoid wrapped lines in the test structs.
34#define FPL(x) FILE_PATH_LITERAL(x)
35
initial.commitd7cae122008-07-26 21:49:3836namespace {
37
[email protected]6f5f4322010-06-09 22:56:4838// To test that file_util::Normalize FilePath() deals with NTFS reparse points
39// correctly, we need functions to create and delete reparse points.
40#if defined(OS_WIN)
41typedef struct _REPARSE_DATA_BUFFER {
42 ULONG ReparseTag;
43 USHORT ReparseDataLength;
44 USHORT Reserved;
45 union {
46 struct {
47 USHORT SubstituteNameOffset;
48 USHORT SubstituteNameLength;
49 USHORT PrintNameOffset;
50 USHORT PrintNameLength;
51 ULONG Flags;
52 WCHAR PathBuffer[1];
53 } SymbolicLinkReparseBuffer;
54 struct {
55 USHORT SubstituteNameOffset;
56 USHORT SubstituteNameLength;
57 USHORT PrintNameOffset;
58 USHORT PrintNameLength;
59 WCHAR PathBuffer[1];
60 } MountPointReparseBuffer;
61 struct {
62 UCHAR DataBuffer[1];
63 } GenericReparseBuffer;
64 };
65} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
66
67// Sets a reparse point. |source| will now point to |target|. Returns true if
68// the call succeeds, false otherwise.
69bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
70 std::wstring kPathPrefix = L"\\??\\";
71 std::wstring target_str;
72 // The juction will not work if the target path does not start with \??\ .
73 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
74 target_str += kPathPrefix;
75 target_str += target_path.value();
76 const wchar_t* target = target_str.c_str();
77 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
78 char buffer[2000] = {0};
79 DWORD returned;
80
81 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
82
83 data->ReparseTag = 0xa0000003;
84 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
85
86 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
87 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
88 data->ReparseDataLength = size_target + 4 + 8;
89
90 int data_size = data->ReparseDataLength + 8;
91
92 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
93 NULL, 0, &returned, NULL)) {
94 return false;
95 }
96 return true;
97}
98
99// Delete the reparse point referenced by |source|. Returns true if the call
100// succeeds, false otherwise.
101bool DeleteReparsePoint(HANDLE source) {
102 DWORD returned;
103 REPARSE_DATA_BUFFER data = {0};
104 data.ReparseTag = 0xa0000003;
105 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
106 &returned, NULL)) {
107 return false;
108 }
109 return true;
110}
111#endif
112
[email protected]73e4c362011-09-22 14:47:18113#if defined(OS_POSIX)
114// Provide a simple way to change the permissions bits on |path| in tests.
115// ASSERT failures will return, but not stop the test. Caller should wrap
116// calls to this function in ASSERT_NO_FATAL_FAILURE().
117void ChangePosixFilePermissions(const FilePath& path,
118 mode_t mode_bits_to_set,
119 mode_t mode_bits_to_clear) {
120 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
121 << "Can't set and clear the same bits.";
122
123 struct stat stat_buf;
124 ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf));
125
126 mode_t updated_mode_bits = stat_buf.st_mode;
127 updated_mode_bits |= mode_bits_to_set;
128 updated_mode_bits &= ~mode_bits_to_clear;
129
130 ASSERT_EQ(0, chmod(path.value().c_str(), updated_mode_bits));
131}
132#endif // defined(OS_POSIX)
133
[email protected]83d86252010-05-07 18:58:45134const wchar_t bogus_content[] = L"I'm cannon fodder.";
135
[email protected]58b7c5a62011-08-15 13:09:27136const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES =
137 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:38138 file_util::FileEnumerator::FILES |
139 file_util::FileEnumerator::DIRECTORIES);
140
[email protected]ed2f2332008-08-20 15:59:49141// file_util winds up using autoreleased objects on the Mac, so this needs
142// to be a PlatformTest
143class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:38144 protected:
145 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:49146 PlatformTest::SetUp();
[email protected]90314c0a82010-09-15 20:40:47147 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commitd7cae122008-07-26 21:49:38148 }
149
[email protected]90314c0a82010-09-15 20:40:47150 ScopedTempDir temp_dir_;
initial.commitd7cae122008-07-26 21:49:38151};
152
153// Collects all the results from the given file enumerator, and provides an
154// interface to query whether a given file is present.
155class FindResultCollector {
156 public:
[email protected]53c58042009-08-26 20:00:14157 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
[email protected]0b733222008-12-11 14:55:12158 FilePath cur_file;
159 while (!(cur_file = enumerator.Next()).value().empty()) {
160 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:38161 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:04162 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:38163 << "Same file returned twice";
164
165 // Save for later.
[email protected]640517f2008-10-30 23:54:04166 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:38167 }
168 }
169
170 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:04171 bool HasFile(const FilePath& file) const {
172 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:38173 }
[email protected]640517f2008-10-30 23:54:04174
[email protected]37088fef2008-08-15 17:32:10175 int size() {
[email protected]f35d39b2008-08-15 17:50:10176 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:10177 }
initial.commitd7cae122008-07-26 21:49:38178
179 private:
[email protected]640517f2008-10-30 23:54:04180 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:38181};
182
183// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:04184void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:38185 const std::wstring& contents) {
[email protected]76c551c2011-03-25 20:49:54186 std::wofstream file;
[email protected]8a205c02011-02-04 20:41:33187 file.open(filename.value().c_str());
initial.commitd7cae122008-07-26 21:49:38188 ASSERT_TRUE(file.is_open());
189 file << contents;
190 file.close();
191}
192
193// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:04194std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:10195 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38196 std::wifstream file;
[email protected]8a205c02011-02-04 20:41:33197 file.open(filename.value().c_str());
initial.commitd7cae122008-07-26 21:49:38198 EXPECT_TRUE(file.is_open());
[email protected]76c551c2011-03-25 20:49:54199 file.getline(contents, arraysize(contents));
initial.commitd7cae122008-07-26 21:49:38200 file.close();
201 return std::wstring(contents);
202}
203
[email protected]8541bb82008-08-15 17:45:13204#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38205uint64 FileTimeAsUint64(const FILETIME& ft) {
206 ULARGE_INTEGER u;
207 u.LowPart = ft.dwLowDateTime;
208 u.HighPart = ft.dwHighDateTime;
209 return u.QuadPart;
210}
[email protected]37088fef2008-08-15 17:32:10211#endif
initial.commitd7cae122008-07-26 21:49:38212
213const struct append_case {
214 const wchar_t* path;
215 const wchar_t* ending;
216 const wchar_t* result;
217} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13218#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38219 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
220 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
221 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
222 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
223 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
224 {L"", L"path", L"\\path"},
225 {L"", L"", L"\\"},
[email protected]37088fef2008-08-15 17:32:10226#elif defined(OS_POSIX)
227 {L"/foo/bar", L"path", L"/foo/bar/path"},
228 {L"/foo/bar/", L"path", L"/foo/bar/path"},
229 {L"/foo/bar//", L"path", L"/foo/bar//path"},
230 {L"/foo/bar/", L"", L"/foo/bar/"},
231 {L"/foo/bar", L"", L"/foo/bar/"},
232 {L"", L"path", L"/path"},
233 {L"", L"", L"/"},
234#endif
initial.commitd7cae122008-07-26 21:49:38235};
236
[email protected]1840cfc2010-02-26 15:11:55237#if defined(OS_WIN)
238// This function is deprecated, but still used on Windows.
initial.commitd7cae122008-07-26 21:49:38239TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10240 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38241 const append_case& value = append_cases[i];
242 std::wstring result = value.path;
243 file_util::AppendToPath(&result, value.ending);
244 EXPECT_EQ(value.result, result);
245 }
246
[email protected]20960e072011-09-20 20:59:01247#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
initial.commitd7cae122008-07-26 21:49:38248 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
249#endif
250}
[email protected]1840cfc2010-02-26 15:11:55251#endif // defined(OS_WIN)
252
initial.commitd7cae122008-07-26 21:49:38253static const struct filename_case {
254 const wchar_t* path;
255 const wchar_t* filename;
256} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13257#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38258 {L"c:\\colon\\backslash", L"backslash"},
259 {L"c:\\colon\\backslash\\", L""},
260 {L"\\\\filename.exe", L"filename.exe"},
261 {L"filename.exe", L"filename.exe"},
262 {L"", L""},
263 {L"\\\\\\", L""},
264 {L"c:/colon/backslash", L"backslash"},
265 {L"c:/colon/backslash/", L""},
266 {L"//////", L""},
267 {L"///filename.exe", L"filename.exe"},
[email protected]37088fef2008-08-15 17:32:10268#elif defined(OS_POSIX)
269 {L"/foo/bar", L"bar"},
270 {L"/foo/bar/", L""},
271 {L"/filename.exe", L"filename.exe"},
272 {L"filename.exe", L"filename.exe"},
273 {L"", L""},
274 {L"/", L""},
275#endif
initial.commitd7cae122008-07-26 21:49:38276};
277
initial.commitd7cae122008-07-26 21:49:38278// Test finding the file type from a path name
279static const struct extension_case {
280 const wchar_t* path;
281 const wchar_t* extension;
282} extension_cases[] = {
[email protected]8541bb82008-08-15 17:45:13283#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38284 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
285 {L"C:\\colon\\backslash\\filename.", L""},
286 {L"C:\\colon\\backslash\\filename", L""},
287 {L"C:\\colon\\backslash\\", L""},
288 {L"C:\\colon\\backslash.\\", L""},
289 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
[email protected]37088fef2008-08-15 17:32:10290#elif defined(OS_POSIX)
291 {L"/foo/bar/filename.extension", L"extension"},
292 {L"/foo/bar/filename.", L""},
293 {L"/foo/bar/filename", L""},
294 {L"/foo/bar/", L""},
295 {L"/foo/bar./", L""},
296 {L"/foo/bar/filename.extension.extension2", L"extension2"},
297 {L".", L""},
298 {L"..", L""},
299 {L"./foo", L""},
300 {L"./foo.extension", L"extension"},
301 {L"/foo.extension1/bar.extension2", L"extension2"},
302#endif
initial.commitd7cae122008-07-26 21:49:38303};
304
[email protected]63597e4e2010-07-08 17:49:05305#if defined(OS_WIN)
306// This function has been deprecated on non-Windows.
initial.commitd7cae122008-07-26 21:49:38307TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10308 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38309 const extension_case& ext = extension_cases[i];
310 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
311 EXPECT_EQ(ext.extension, fext);
312 }
313}
[email protected]63597e4e2010-07-08 17:49:05314#endif
initial.commitd7cae122008-07-26 21:49:38315
316// Test finding the directory component of a path
317static const struct dir_case {
318 const wchar_t* full_path;
319 const wchar_t* directory;
320} dir_cases[] = {
[email protected]8541bb82008-08-15 17:45:13321#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38322 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
323 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
324 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
325 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
326 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
327 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
[email protected]2c8088a42009-10-15 05:00:25328 {L"C:\\", L"C:\\"},
[email protected]37088fef2008-08-15 17:32:10329#elif defined(OS_POSIX)
330 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
331 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
332 {L"/foo/bar/", L"/foo/bar"},
333 {L"/foo/bar//", L"/foo/bar"},
334 {L"/foo/bar", L"/foo"},
335 {L"/foo/bar./", L"/foo/bar."},
336 {L"/", L"/"},
337 {L".", L"."},
[email protected]53c58042009-08-26 20:00:14338 {L"..", L"."}, // yes, ".." technically lives in "."
[email protected]37088fef2008-08-15 17:32:10339#endif
initial.commitd7cae122008-07-26 21:49:38340};
341
[email protected]2ec79f72010-06-10 13:05:26342// Flaky, http://crbug.com/46246
[email protected]552b3152010-06-10 12:40:52343TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commitd7cae122008-07-26 21:49:38344 // Create old file (that we don't want to count)
[email protected]90314c0a82010-09-15 20:40:47345 FilePath old_file_name =
346 temp_dir_.path().Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commitd7cae122008-07-26 21:49:38347 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
348
349 // Age to perfection
[email protected]4b7743de2009-04-21 01:50:39350#if defined(OS_WIN)
[email protected]ce072a72010-12-31 20:02:16351 base::PlatformThread::Sleep(100);
[email protected]4b7743de2009-04-21 01:50:39352#elif defined(OS_POSIX)
353 // We need to wait at least one second here because the precision of
354 // file creation time is one second.
[email protected]ce072a72010-12-31 20:02:16355 base::PlatformThread::Sleep(1500);
[email protected]4b7743de2009-04-21 01:50:39356#endif
initial.commitd7cae122008-07-26 21:49:38357
358 // Establish our cutoff time
[email protected]85786f92009-04-18 00:42:48359 base::Time now(base::Time::NowFromSystemTime());
[email protected]90314c0a82010-09-15 20:40:47360 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38361
362 // Create a new file (that we do want to count)
[email protected]90314c0a82010-09-15 20:40:47363 FilePath new_file_name =
364 temp_dir_.path().Append(FILE_PATH_LITERAL("New File.txt"));
initial.commitd7cae122008-07-26 21:49:38365 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
366
367 // We should see only the new file.
[email protected]90314c0a82010-09-15 20:40:47368 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38369
370 // Delete new file, we should see no files after cutoff now
371 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]90314c0a82010-09-15 20:40:47372 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38373}
374
[email protected]c2c132c62010-03-24 21:56:26375TEST_F(FileUtilTest, FileAndDirectorySize) {
376 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
377 // should return 53 bytes.
[email protected]90314c0a82010-09-15 20:40:47378 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
[email protected]c2c132c62010-03-24 21:56:26379 CreateTextFile(file_01, L"12345678901234567890");
380 int64 size_f1 = 0;
381 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
382 EXPECT_EQ(20ll, size_f1);
383
[email protected]90314c0a82010-09-15 20:40:47384 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
[email protected]c2c132c62010-03-24 21:56:26385 file_util::CreateDirectory(subdir_path);
386
387 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
388 CreateTextFile(file_02, L"123456789012345678901234567890");
389 int64 size_f2 = 0;
390 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
391 EXPECT_EQ(30ll, size_f2);
392
393 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
394 file_util::CreateDirectory(subsubdir_path);
395
396 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
397 CreateTextFile(file_03, L"123");
398
[email protected]90314c0a82010-09-15 20:40:47399 int64 computed_size = file_util::ComputeDirectorySize(temp_dir_.path());
[email protected]c2c132c62010-03-24 21:56:26400 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
[email protected]a04876b92010-06-11 22:53:43401
[email protected]90314c0a82010-09-15 20:40:47402 computed_size =
403 file_util::ComputeFilesSize(temp_dir_.path(), FPL("The file*"));
[email protected]a04876b92010-06-11 22:53:43404 EXPECT_EQ(size_f1, computed_size);
405
[email protected]90314c0a82010-09-15 20:40:47406 computed_size = file_util::ComputeFilesSize(temp_dir_.path(), FPL("bla*"));
[email protected]a04876b92010-06-11 22:53:43407 EXPECT_EQ(0, computed_size);
[email protected]c2c132c62010-03-24 21:56:26408}
409
[email protected]6f5f4322010-06-09 22:56:48410TEST_F(FileUtilTest, NormalizeFilePathBasic) {
411 // Create a directory under the test dir. Because we create it,
412 // we know it is not a link.
[email protected]90314c0a82010-09-15 20:40:47413 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
414 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
[email protected]6f5f4322010-06-09 22:56:48415 FilePath file_b_path = dir_path.Append(FPL("file_b"));
416 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57417
[email protected]6f5f4322010-06-09 22:56:48418 FilePath normalized_file_a_path, normalized_file_b_path;
419 ASSERT_FALSE(file_util::PathExists(file_a_path));
420 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
421 &normalized_file_a_path))
[email protected]e7afe2452010-08-22 16:19:13422 << "NormalizeFilePath() should fail on nonexistent paths.";
[email protected]6f5f4322010-06-09 22:56:48423
424 CreateTextFile(file_a_path, bogus_content);
425 ASSERT_TRUE(file_util::PathExists(file_a_path));
426 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
427 &normalized_file_a_path));
428
429 CreateTextFile(file_b_path, bogus_content);
430 ASSERT_TRUE(file_util::PathExists(file_b_path));
431 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
432 &normalized_file_b_path));
433
434 // Beacuse this test created |dir_path|, we know it is not a link
435 // or junction. So, the real path of the directory holding file a
436 // must be the parent of the path holding file b.
437 ASSERT_TRUE(normalized_file_a_path.DirName()
438 .IsParent(normalized_file_b_path.DirName()));
439}
440
441#if defined(OS_WIN)
442
443TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
444 // Build the following directory structure:
445 //
[email protected]90314c0a82010-09-15 20:40:47446 // temp_dir
[email protected]6f5f4322010-06-09 22:56:48447 // |-> base_a
448 // | |-> sub_a
449 // | |-> file.txt
450 // | |-> long_name___... (Very long name.)
451 // | |-> sub_long
452 // | |-> deep.txt
453 // |-> base_b
[email protected]90314c0a82010-09-15 20:40:47454 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
455 // |-> to_base_b (reparse point to temp_dir\base_b)
456 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
[email protected]6f5f4322010-06-09 22:56:48457
[email protected]90314c0a82010-09-15 20:40:47458 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
[email protected]6f5f4322010-06-09 22:56:48459 ASSERT_TRUE(file_util::CreateDirectory(base_a));
460
461 FilePath sub_a = base_a.Append(FPL("sub_a"));
462 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
463
464 FilePath file_txt = sub_a.Append(FPL("file.txt"));
465 CreateTextFile(file_txt, bogus_content);
466
467 // Want a directory whose name is long enough to make the path to the file
468 // inside just under MAX_PATH chars. This will be used to test that when
469 // a junction expands to a path over MAX_PATH chars in length,
470 // NormalizeFilePath() fails without crashing.
471 FilePath sub_long_rel(FPL("sub_long"));
472 FilePath deep_txt(FPL("deep.txt"));
473
474 int target_length = MAX_PATH;
475 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
476 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52477 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48478 // the resulting path is still long enough to hit the failing case in
479 // NormalizePath().
480 const int kCreateDirLimit = 4;
481 target_length -= kCreateDirLimit;
482 FilePath::StringType long_name_str = FPL("long_name_");
483 long_name_str.resize(target_length, '_');
484
485 FilePath long_name = sub_a.Append(FilePath(long_name_str));
486 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
487 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
488
489 FilePath sub_long = deep_file.DirName();
490 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
491 CreateTextFile(deep_file, bogus_content);
492
[email protected]90314c0a82010-09-15 20:40:47493 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
[email protected]6f5f4322010-06-09 22:56:48494 ASSERT_TRUE(file_util::CreateDirectory(base_b));
495
496 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
497 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
[email protected]b90d7e802011-01-09 16:32:20498 base::win::ScopedHandle reparse_to_sub_a(
[email protected]6f5f4322010-06-09 22:56:48499 ::CreateFile(to_sub_a.value().c_str(),
500 FILE_ALL_ACCESS,
501 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
502 NULL,
503 OPEN_EXISTING,
504 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
505 NULL));
[email protected]094f9762010-08-03 03:51:56506 ASSERT_TRUE(reparse_to_sub_a.IsValid());
[email protected]6f5f4322010-06-09 22:56:48507 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
508
509 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
510 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
[email protected]b90d7e802011-01-09 16:32:20511 base::win::ScopedHandle reparse_to_base_b(
[email protected]6f5f4322010-06-09 22:56:48512 ::CreateFile(to_base_b.value().c_str(),
513 FILE_ALL_ACCESS,
514 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
515 NULL,
516 OPEN_EXISTING,
517 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
518 NULL));
[email protected]094f9762010-08-03 03:51:56519 ASSERT_TRUE(reparse_to_base_b.IsValid());
[email protected]6f5f4322010-06-09 22:56:48520 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
521
522 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
523 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
[email protected]b90d7e802011-01-09 16:32:20524 base::win::ScopedHandle reparse_to_sub_long(
[email protected]6f5f4322010-06-09 22:56:48525 ::CreateFile(to_sub_long.value().c_str(),
526 FILE_ALL_ACCESS,
527 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
528 NULL,
529 OPEN_EXISTING,
530 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
531 NULL));
[email protected]094f9762010-08-03 03:51:56532 ASSERT_TRUE(reparse_to_sub_long.IsValid());
[email protected]6f5f4322010-06-09 22:56:48533 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
534
535 // Normalize a junction free path: base_a\sub_a\file.txt .
536 FilePath normalized_path;
537 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
538 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
539
540 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
541 // the junction to_sub_a.
542 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
543 &normalized_path));
544 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
545
546 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
547 // normalized to exclude junctions to_base_b and to_sub_a .
548 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
549 .Append(FPL("to_base_b"))
550 .Append(FPL("to_sub_a"))
551 .Append(FPL("file.txt")),
552 &normalized_path));
553 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
554
555 // A long enough path will cause NormalizeFilePath() to fail. Make a long
556 // path using to_base_b many times, and check that paths long enough to fail
557 // do not cause a crash.
558 FilePath long_path = base_b;
559 const int kLengthLimit = MAX_PATH + 200;
560 while (long_path.value().length() <= kLengthLimit) {
561 long_path = long_path.Append(FPL("to_base_b"));
562 }
563 long_path = long_path.Append(FPL("to_sub_a"))
564 .Append(FPL("file.txt"));
565
566 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
567
568 // Normalizing the junction to deep.txt should fail, because the expanded
569 // path to deep.txt is longer than MAX_PATH.
570 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
571 &normalized_path));
572
573 // Delete the reparse points, and see that NormalizeFilePath() fails
574 // to traverse them.
575 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
576 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
577 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
578
579 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
580 &normalized_path));
581}
582
583#endif // defined(OS_WIN)
584
[email protected]2e733d102010-11-30 00:43:37585#if defined(OS_POSIX)
586
587TEST_F(FileUtilTest, CreateAndReadSymlinks) {
588 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
589 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
590 CreateTextFile(link_to, bogus_content);
591
592 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
593 << "Failed to create file symlink.";
594
595 // If we created the link properly, we should be able to read the
596 // contents through it.
597 std::wstring contents = ReadTextFile(link_from);
598 ASSERT_EQ(contents, bogus_content);
599
600 FilePath result;
601 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from, &result));
602 ASSERT_EQ(link_to.value(), result.value());
603
604 // Link to a directory.
605 link_from = temp_dir_.path().Append(FPL("from_dir"));
606 link_to = temp_dir_.path().Append(FPL("to_dir"));
607 file_util::CreateDirectory(link_to);
608
609 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
610 << "Failed to create directory symlink.";
611
612 // Test failures.
613 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to));
614 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result));
615 FilePath missing = temp_dir_.path().Append(FPL("missing"));
616 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result));
617}
618
619
[email protected]6f5f4322010-06-09 22:56:48620// The following test of NormalizeFilePath() require that we create a symlink.
[email protected]2e733d102010-11-30 00:43:37621// This can not be done on Windows before Vista. On Vista, creating a symlink
[email protected]6f5f4322010-06-09 22:56:48622// requires privilege "SeCreateSymbolicLinkPrivilege".
623// TODO(skerner): Investigate the possibility of giving base_unittests the
624// privileges required to create a symlink.
[email protected]6f5f4322010-06-09 22:56:48625TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
626 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57627
628 // Link one file to another.
[email protected]90314c0a82010-09-15 20:40:47629 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
630 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57631 CreateTextFile(link_to, bogus_content);
632
[email protected]2e733d102010-11-30 00:43:37633 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57634 << "Failed to create file symlink.";
635
[email protected]6f5f4322010-06-09 22:56:48636 // Check that NormalizeFilePath sees the link.
637 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57638 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48639 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
640 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57641
642 // Link to a directory.
[email protected]90314c0a82010-09-15 20:40:47643 link_from = temp_dir_.path().Append(FPL("from_dir"));
644 link_to = temp_dir_.path().Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57645 file_util::CreateDirectory(link_to);
646
[email protected]2e733d102010-11-30 00:43:37647 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57648 << "Failed to create directory symlink.";
649
[email protected]6f5f4322010-06-09 22:56:48650 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
651 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57652
[email protected]6f5f4322010-06-09 22:56:48653 // Test that a loop in the links causes NormalizeFilePath() to return false.
[email protected]90314c0a82010-09-15 20:40:47654 link_from = temp_dir_.path().Append(FPL("link_a"));
655 link_to = temp_dir_.path().Append(FPL("link_b"));
[email protected]2e733d102010-11-30 00:43:37656 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57657 << "Failed to create loop symlink a.";
[email protected]2e733d102010-11-30 00:43:37658 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57659 << "Failed to create loop symlink b.";
660
661 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48662 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57663}
664#endif // defined(OS_POSIX)
665
[email protected]83d86252010-05-07 18:58:45666TEST_F(FileUtilTest, DeleteNonExistent) {
[email protected]90314c0a82010-09-15 20:40:47667 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
[email protected]83d86252010-05-07 18:58:45668 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36669
[email protected]83d86252010-05-07 18:58:45670 EXPECT_TRUE(file_util::Delete(non_existent, false));
671 ASSERT_FALSE(file_util::PathExists(non_existent));
672 EXPECT_TRUE(file_util::Delete(non_existent, true));
673 ASSERT_FALSE(file_util::PathExists(non_existent));
674}
675
676TEST_F(FileUtilTest, DeleteFile) {
677 // Create a file
[email protected]90314c0a82010-09-15 20:40:47678 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
[email protected]83d86252010-05-07 18:58:45679 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38680 ASSERT_TRUE(file_util::PathExists(file_name));
681
[email protected]83d86252010-05-07 18:58:45682 // Make sure it's deleted
683 EXPECT_TRUE(file_util::Delete(file_name, false));
684 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36685
[email protected]83d86252010-05-07 18:58:45686 // Test recursive case, create a new file
[email protected]90314c0a82010-09-15 20:40:47687 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
[email protected]83d86252010-05-07 18:58:45688 CreateTextFile(file_name, bogus_content);
689 ASSERT_TRUE(file_util::PathExists(file_name));
690
691 // Make sure it's deleted
692 EXPECT_TRUE(file_util::Delete(file_name, true));
693 EXPECT_FALSE(file_util::PathExists(file_name));
694}
695
696#if defined(OS_WIN)
697// Tests that the Delete function works for wild cards, especially
698// with the recursion flag. Also coincidentally tests PathExists.
699// TODO(erikkay): see if anyone's actually using this feature of the API
700TEST_F(FileUtilTest, DeleteWildCard) {
701 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47702 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
[email protected]83d86252010-05-07 18:58:45703 CreateTextFile(file_name, bogus_content);
704 ASSERT_TRUE(file_util::PathExists(file_name));
705
[email protected]90314c0a82010-09-15 20:40:47706 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
[email protected]83d86252010-05-07 18:58:45707 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38708 ASSERT_TRUE(file_util::PathExists(subdir_path));
709
[email protected]83d86252010-05-07 18:58:45710 // Create the wildcard path
[email protected]90314c0a82010-09-15 20:40:47711 FilePath directory_contents = temp_dir_.path();
[email protected]83d86252010-05-07 18:58:45712 directory_contents = directory_contents.Append(FPL("*"));
713
initial.commitd7cae122008-07-26 21:49:38714 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45715 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10716 EXPECT_FALSE(file_util::PathExists(file_name));
717 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40718
[email protected]3a51cfdd2010-05-07 00:05:36719 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45720 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40721 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36722 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40723}
724
[email protected]83d86252010-05-07 18:58:45725// TODO(erikkay): see if anyone's actually using this feature of the API
726TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
727 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47728 FilePath subdir_path =
729 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
[email protected]83d86252010-05-07 18:58:45730 file_util::CreateDirectory(subdir_path);
731 ASSERT_TRUE(file_util::PathExists(subdir_path));
732
733 // Create the wildcard path
734 FilePath directory_contents = subdir_path;
735 directory_contents = directory_contents.Append(FPL("*"));
736
737 // Delete non-recursively and check nothing got deleted
738 EXPECT_TRUE(file_util::Delete(directory_contents, false));
739 EXPECT_TRUE(file_util::PathExists(subdir_path));
740
741 // Delete recursively and check nothing got deleted
742 EXPECT_TRUE(file_util::Delete(directory_contents, true));
743 EXPECT_TRUE(file_util::PathExists(subdir_path));
744}
745#endif
746
747// Tests non-recursive Delete() for a directory.
748TEST_F(FileUtilTest, DeleteDirNonRecursive) {
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("DeleteDirNonRecursive"));
[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 DeleteDir.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 non-recursively and check that the empty dir got deleted
767 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
768 EXPECT_FALSE(file_util::PathExists(subdir_path2));
769
770 // Delete non-recursively and check that nothing got deleted
771 EXPECT_FALSE(file_util::Delete(test_subdir, false));
772 EXPECT_TRUE(file_util::PathExists(test_subdir));
773 EXPECT_TRUE(file_util::PathExists(file_name));
774 EXPECT_TRUE(file_util::PathExists(subdir_path1));
775}
776
777// Tests recursive Delete() for a directory.
778TEST_F(FileUtilTest, DeleteDirRecursive) {
779 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47780 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
[email protected]83d86252010-05-07 18:58:45781 file_util::CreateDirectory(test_subdir);
782 ASSERT_TRUE(file_util::PathExists(test_subdir));
783
784 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
785 CreateTextFile(file_name, bogus_content);
786 ASSERT_TRUE(file_util::PathExists(file_name));
787
788 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
789 file_util::CreateDirectory(subdir_path1);
790 ASSERT_TRUE(file_util::PathExists(subdir_path1));
791
792 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
793 file_util::CreateDirectory(subdir_path2);
794 ASSERT_TRUE(file_util::PathExists(subdir_path2));
795
796 // Delete recursively and check that the empty dir got deleted
797 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
798 EXPECT_FALSE(file_util::PathExists(subdir_path2));
799
800 // Delete recursively and check that everything got deleted
801 EXPECT_TRUE(file_util::Delete(test_subdir, true));
802 EXPECT_FALSE(file_util::PathExists(file_name));
803 EXPECT_FALSE(file_util::PathExists(subdir_path1));
804 EXPECT_FALSE(file_util::PathExists(test_subdir));
805}
806
[email protected]bc6a9012009-10-15 01:11:44807TEST_F(FileUtilTest, MoveFileNew) {
808 // Create a file
809 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47810 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44811 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
812 ASSERT_TRUE(file_util::PathExists(file_name_from));
813
[email protected]90314c0a82010-09-15 20:40:47814 // The destination.
815 FilePath file_name_to = temp_dir_.path().Append(
816 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44817 ASSERT_FALSE(file_util::PathExists(file_name_to));
818
819 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
820
821 // Check everything has been moved.
822 EXPECT_FALSE(file_util::PathExists(file_name_from));
823 EXPECT_TRUE(file_util::PathExists(file_name_to));
824}
825
826TEST_F(FileUtilTest, MoveFileExists) {
827 // Create a file
828 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47829 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44830 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
831 ASSERT_TRUE(file_util::PathExists(file_name_from));
832
[email protected]90314c0a82010-09-15 20:40:47833 // The destination name.
834 FilePath file_name_to = temp_dir_.path().Append(
835 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44836 CreateTextFile(file_name_to, L"Old file content");
837 ASSERT_TRUE(file_util::PathExists(file_name_to));
838
839 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
840
841 // Check everything has been moved.
842 EXPECT_FALSE(file_util::PathExists(file_name_from));
843 EXPECT_TRUE(file_util::PathExists(file_name_to));
844 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
845}
846
847TEST_F(FileUtilTest, MoveFileDirExists) {
848 // Create a file
849 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47850 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44851 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
852 ASSERT_TRUE(file_util::PathExists(file_name_from));
853
854 // The destination directory
855 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47856 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:44857 file_util::CreateDirectory(dir_name_to);
858 ASSERT_TRUE(file_util::PathExists(dir_name_to));
859
860 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
861}
862
863
[email protected]abbc5732009-10-13 17:57:27864TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38865 // Create a directory
[email protected]640517f2008-10-30 23:54:04866 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47867 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04868 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38869 ASSERT_TRUE(file_util::PathExists(dir_name_from));
870
871 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04872 FilePath file_name_from =
873 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38874 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
875 ASSERT_TRUE(file_util::PathExists(file_name_from));
876
[email protected]90314c0a82010-09-15 20:40:47877 // Move the directory.
878 FilePath dir_name_to =
879 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04880 FilePath file_name_to =
881 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38882
883 ASSERT_FALSE(file_util::PathExists(dir_name_to));
884
885 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
886
887 // Check everything has been moved.
888 EXPECT_FALSE(file_util::PathExists(dir_name_from));
889 EXPECT_FALSE(file_util::PathExists(file_name_from));
890 EXPECT_TRUE(file_util::PathExists(dir_name_to));
891 EXPECT_TRUE(file_util::PathExists(file_name_to));
892}
893
[email protected]abbc5732009-10-13 17:57:27894TEST_F(FileUtilTest, MoveExist) {
895 // Create a directory
896 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47897 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:27898 file_util::CreateDirectory(dir_name_from);
899 ASSERT_TRUE(file_util::PathExists(dir_name_from));
900
901 // Create a file under the directory
902 FilePath file_name_from =
903 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
904 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
905 ASSERT_TRUE(file_util::PathExists(file_name_from));
906
907 // Move the directory
908 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:47909 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:27910
911 FilePath dir_name_to =
912 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
913 FilePath file_name_to =
914 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
915
916 // Create the destination directory.
917 file_util::CreateDirectory(dir_name_exists);
918 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
919
920 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
921
922 // Check everything has been moved.
923 EXPECT_FALSE(file_util::PathExists(dir_name_from));
924 EXPECT_FALSE(file_util::PathExists(file_name_from));
925 EXPECT_TRUE(file_util::PathExists(dir_name_to));
926 EXPECT_TRUE(file_util::PathExists(file_name_to));
927}
928
929TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38930 // Create a directory.
[email protected]640517f2008-10-30 23:54:04931 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47932 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04933 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38934 ASSERT_TRUE(file_util::PathExists(dir_name_from));
935
936 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04937 FilePath file_name_from =
938 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38939 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
940 ASSERT_TRUE(file_util::PathExists(file_name_from));
941
942 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04943 FilePath subdir_name_from =
944 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
945 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38946 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
947
948 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04949 FilePath file_name2_from =
950 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38951 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
952 ASSERT_TRUE(file_util::PathExists(file_name2_from));
953
954 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04955 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47956 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04957 FilePath file_name_to =
958 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
959 FilePath subdir_name_to =
960 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
961 FilePath file_name2_to =
962 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38963
964 ASSERT_FALSE(file_util::PathExists(dir_name_to));
965
966 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
967
968 // Check everything has been copied.
969 EXPECT_TRUE(file_util::PathExists(dir_name_from));
970 EXPECT_TRUE(file_util::PathExists(file_name_from));
971 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
972 EXPECT_TRUE(file_util::PathExists(file_name2_from));
973 EXPECT_TRUE(file_util::PathExists(dir_name_to));
974 EXPECT_TRUE(file_util::PathExists(file_name_to));
975 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
976 EXPECT_TRUE(file_util::PathExists(file_name2_to));
977}
978
[email protected]abbc5732009-10-13 17:57:27979TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
980 // Create a directory.
981 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47982 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:27983 file_util::CreateDirectory(dir_name_from);
984 ASSERT_TRUE(file_util::PathExists(dir_name_from));
985
986 // Create a file under the directory.
987 FilePath file_name_from =
988 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
989 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
990 ASSERT_TRUE(file_util::PathExists(file_name_from));
991
992 // Create a subdirectory.
993 FilePath subdir_name_from =
994 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
995 file_util::CreateDirectory(subdir_name_from);
996 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
997
998 // Create a file under the subdirectory.
999 FilePath file_name2_from =
1000 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1001 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1002 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1003
1004 // Copy the directory recursively.
1005 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:471006 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:271007
1008 FilePath dir_name_to =
1009 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1010 FilePath file_name_to =
1011 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1012 FilePath subdir_name_to =
1013 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1014 FilePath file_name2_to =
1015 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1016
1017 // Create the destination directory.
1018 file_util::CreateDirectory(dir_name_exists);
1019 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1020
1021 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1022
1023 // Check everything has been copied.
1024 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1025 EXPECT_TRUE(file_util::PathExists(file_name_from));
1026 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1027 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1028 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1029 EXPECT_TRUE(file_util::PathExists(file_name_to));
1030 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1031 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1032}
1033
1034TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381035 // Create a directory.
[email protected]640517f2008-10-30 23:54:041036 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471037 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041038 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381039 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1040
1041 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041042 FilePath file_name_from =
1043 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381044 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1045 ASSERT_TRUE(file_util::PathExists(file_name_from));
1046
1047 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041048 FilePath subdir_name_from =
1049 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1050 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381051 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1052
1053 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041054 FilePath file_name2_from =
1055 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381056 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1057 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1058
1059 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041060 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471061 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:041062 FilePath file_name_to =
1063 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1064 FilePath subdir_name_to =
1065 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381066
1067 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1068
1069 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1070
1071 // Check everything has been copied.
1072 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1073 EXPECT_TRUE(file_util::PathExists(file_name_from));
1074 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1075 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1076 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1077 EXPECT_TRUE(file_util::PathExists(file_name_to));
1078 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1079}
1080
[email protected]abbc5732009-10-13 17:57:271081TEST_F(FileUtilTest, CopyDirectoryExists) {
1082 // Create a directory.
1083 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471084 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:271085 file_util::CreateDirectory(dir_name_from);
1086 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1087
1088 // Create a file under the directory.
1089 FilePath file_name_from =
1090 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1091 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1092 ASSERT_TRUE(file_util::PathExists(file_name_from));
1093
1094 // Create a subdirectory.
1095 FilePath subdir_name_from =
1096 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1097 file_util::CreateDirectory(subdir_name_from);
1098 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1099
1100 // Create a file under the subdirectory.
1101 FilePath file_name2_from =
1102 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1103 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1104 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1105
1106 // Copy the directory not recursively.
1107 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471108 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]abbc5732009-10-13 17:57:271109 FilePath file_name_to =
1110 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1111 FilePath subdir_name_to =
1112 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1113
1114 // Create the destination directory.
1115 file_util::CreateDirectory(dir_name_to);
1116 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1117
1118 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1119
1120 // Check everything has been copied.
1121 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1122 EXPECT_TRUE(file_util::PathExists(file_name_from));
1123 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1124 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1125 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1126 EXPECT_TRUE(file_util::PathExists(file_name_to));
1127 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1128}
1129
[email protected]bc6a9012009-10-15 01:11:441130TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1131 // Create a file
1132 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471133 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441134 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1135 ASSERT_TRUE(file_util::PathExists(file_name_from));
1136
1137 // The destination name
[email protected]90314c0a82010-09-15 20:40:471138 FilePath file_name_to = temp_dir_.path().Append(
1139 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441140 ASSERT_FALSE(file_util::PathExists(file_name_to));
1141
1142 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1143
1144 // Check the has been copied
1145 EXPECT_TRUE(file_util::PathExists(file_name_to));
1146}
1147
1148TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1149 // Create a file
1150 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471151 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441152 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1153 ASSERT_TRUE(file_util::PathExists(file_name_from));
1154
1155 // The destination name
[email protected]90314c0a82010-09-15 20:40:471156 FilePath file_name_to = temp_dir_.path().Append(
1157 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441158 CreateTextFile(file_name_to, L"Old file content");
1159 ASSERT_TRUE(file_util::PathExists(file_name_to));
1160
1161 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1162
1163 // Check the has been copied
1164 EXPECT_TRUE(file_util::PathExists(file_name_to));
1165 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1166}
1167
1168TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1169 // Create a file
1170 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471171 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441172 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1173 ASSERT_TRUE(file_util::PathExists(file_name_from));
1174
1175 // The destination
1176 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471177 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:441178 file_util::CreateDirectory(dir_name_to);
1179 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1180 FilePath file_name_to =
1181 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1182
1183 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1184
1185 // Check the has been copied
1186 EXPECT_TRUE(file_util::PathExists(file_name_to));
1187}
1188
initial.commitd7cae122008-07-26 21:49:381189TEST_F(FileUtilTest, CopyFile) {
1190 // Create a directory
[email protected]640517f2008-10-30 23:54:041191 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471192 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041193 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381194 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1195
1196 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041197 FilePath file_name_from =
1198 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381199 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1200 CreateTextFile(file_name_from, file_contents);
1201 ASSERT_TRUE(file_util::PathExists(file_name_from));
1202
1203 // Copy the file.
[email protected]640517f2008-10-30 23:54:041204 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381205 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111206
[email protected]37088fef2008-08-15 17:32:101207 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141208 FilePath dest_file2(dir_name_from);
1209 dest_file2 = dest_file2.AppendASCII("..");
1210 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1211 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1212
1213 FilePath dest_file2_test(dir_name_from);
1214 dest_file2_test = dest_file2_test.DirName();
1215 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381216
1217 // Check everything has been copied.
1218 EXPECT_TRUE(file_util::PathExists(file_name_from));
1219 EXPECT_TRUE(file_util::PathExists(dest_file));
1220 const std::wstring read_contents = ReadTextFile(dest_file);
1221 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141222 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1223 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381224}
1225
[email protected]37088fef2008-08-15 17:32:101226// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131227#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381228TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]90314c0a82010-09-15 20:40:471229 FilePath file_name = temp_dir_.path().Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381230
1231 SYSTEMTIME start_time;
1232 GetLocalTime(&start_time);
1233 Sleep(100);
1234 CreateTextFile(file_name, L"New file!");
1235 Sleep(100);
1236 SYSTEMTIME end_time;
1237 GetLocalTime(&end_time);
1238
1239 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041240 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381241
1242 FILETIME start_filetime;
1243 SystemTimeToFileTime(&start_time, &start_filetime);
1244 FILETIME end_filetime;
1245 SystemTimeToFileTime(&end_time, &end_filetime);
1246 FILETIME file_creation_filetime;
1247 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1248
1249 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1250 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1251 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1252
1253 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1254 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1255 "end time: " << FileTimeAsUint64(end_filetime);
1256
[email protected]640517f2008-10-30 23:54:041257 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381258}
[email protected]37088fef2008-08-15 17:32:101259#endif
initial.commitd7cae122008-07-26 21:49:381260
[email protected]ed2f2332008-08-20 15:59:491261// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041262// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491263typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381264
[email protected]ed2f2332008-08-20 15:59:491265TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041266 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381267 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041268 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1269 .Append(FILE_PATH_LITERAL("data"))
1270 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381271 ASSERT_TRUE(file_util::PathExists(data_dir));
1272
[email protected]640517f2008-10-30 23:54:041273 FilePath original_file =
1274 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1275 FilePath same_file =
1276 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1277 FilePath same_length_file =
1278 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1279 FilePath different_file =
1280 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1281 FilePath different_first_file =
1282 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1283 FilePath different_last_file =
1284 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1285 FilePath empty1_file =
1286 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1287 FilePath empty2_file =
1288 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1289 FilePath shortened_file =
1290 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1291 FilePath binary_file =
1292 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1293 FilePath binary_file_same =
1294 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1295 FilePath binary_file_diff =
1296 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381297
1298 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1299 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1300 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1301 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221302 EXPECT_FALSE(file_util::ContentsEqual(
1303 FilePath(FILE_PATH_LITERAL("bogusname")),
1304 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381305 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1306 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1307 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1308 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1309 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1310 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1311 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1312}
1313
[email protected]b81637c32009-06-26 21:17:241314TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1315 FilePath data_dir;
1316 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1317 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1318 .Append(FILE_PATH_LITERAL("data"))
1319 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1320 ASSERT_TRUE(file_util::PathExists(data_dir));
1321
1322 FilePath original_file =
1323 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1324 FilePath same_file =
1325 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1326 FilePath crlf_file =
1327 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1328 FilePath shortened_file =
1329 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1330 FilePath different_file =
1331 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1332 FilePath different_first_file =
1333 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1334 FilePath different_last_file =
1335 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1336 FilePath first1_file =
1337 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1338 FilePath first2_file =
1339 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1340 FilePath empty1_file =
1341 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1342 FilePath empty2_file =
1343 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1344 FilePath blank_line_file =
1345 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1346 FilePath blank_line_crlf_file =
1347 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1348
1349 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1350 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1351 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1352 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1353 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1354 different_first_file));
1355 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1356 different_last_file));
1357 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1358 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1359 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1360 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1361 blank_line_crlf_file));
1362}
1363
[email protected]37088fef2008-08-15 17:32:101364// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131365#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381366TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]90314c0a82010-09-15 20:40:471367 FilePath target_file = temp_dir_.path().Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381368 CreateTextFile(target_file, L"This is the target.");
1369
[email protected]90314c0a82010-09-15 20:40:471370 FilePath link_file = temp_dir_.path().Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381371
1372 HRESULT result;
1373 IShellLink *shell = NULL;
1374 IPersistFile *persist = NULL;
1375
1376 CoInitialize(NULL);
1377 // Temporarily create a shortcut for test
1378 result = CoCreateInstance(CLSID_ShellLink, NULL,
1379 CLSCTX_INPROC_SERVER, IID_IShellLink,
1380 reinterpret_cast<LPVOID*>(&shell));
1381 EXPECT_TRUE(SUCCEEDED(result));
1382 result = shell->QueryInterface(IID_IPersistFile,
1383 reinterpret_cast<LPVOID*>(&persist));
1384 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041385 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381386 EXPECT_TRUE(SUCCEEDED(result));
1387 result = shell->SetDescription(L"ResolveShortcutTest");
1388 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041389 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381390 EXPECT_TRUE(SUCCEEDED(result));
1391 if (persist)
1392 persist->Release();
1393 if (shell)
1394 shell->Release();
1395
1396 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441397 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381398 EXPECT_TRUE(is_solved);
1399 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441400 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381401 EXPECT_EQ(L"This is the target.", contents);
1402
[email protected]d324ab332008-08-18 16:00:381403 // Cleaning
[email protected]640517f2008-10-30 23:54:041404 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441405 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381406 CoUninitialize();
1407}
1408
1409TEST_F(FileUtilTest, CreateShortcutTest) {
1410 const wchar_t file_contents[] = L"This is another target.";
[email protected]90314c0a82010-09-15 20:40:471411 FilePath target_file = temp_dir_.path().Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381412 CreateTextFile(target_file, file_contents);
1413
[email protected]90314c0a82010-09-15 20:40:471414 FilePath link_file = temp_dir_.path().Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381415
1416 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041417 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1418 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501419 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441420 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381421 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441422 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381423 EXPECT_EQ(file_contents, read_contents);
1424
[email protected]640517f2008-10-30 23:54:041425 DeleteFile(target_file.value().c_str());
1426 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381427 CoUninitialize();
1428}
[email protected]2c59af7dca2009-03-11 18:37:481429
1430TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1431 // Create a directory
1432 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471433 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
[email protected]2c59af7dca2009-03-11 18:37:481434 file_util::CreateDirectory(dir_name_from);
1435 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1436
1437 // Create a file under the directory
1438 FilePath file_name_from =
1439 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1440 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1441 ASSERT_TRUE(file_util::PathExists(file_name_from));
1442
1443 // Move the directory by using CopyAndDeleteDirectory
[email protected]90314c0a82010-09-15 20:40:471444 FilePath dir_name_to = temp_dir_.path().Append(
[email protected]2c59af7dca2009-03-11 18:37:481445 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1446 FilePath file_name_to =
1447 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1448
1449 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1450
1451 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1452
1453 // Check everything has been moved.
1454 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1455 EXPECT_FALSE(file_util::PathExists(file_name_from));
1456 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1457 EXPECT_TRUE(file_util::PathExists(file_name_to));
1458}
[email protected]7d95aae2009-10-09 07:33:391459
1460TEST_F(FileUtilTest, GetTempDirTest) {
1461 static const TCHAR* kTmpKey = _T("TMP");
1462 static const TCHAR* kTmpValues[] = {
1463 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1464 };
1465 // Save the original $TMP.
1466 size_t original_tmp_size;
1467 TCHAR* original_tmp;
1468 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1469 // original_tmp may be NULL.
1470
1471 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1472 FilePath path;
1473 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1474 file_util::GetTempDir(&path);
1475 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1476 " result=" << path.value();
1477 }
1478
1479 // Restore the original $TMP.
1480 if (original_tmp) {
1481 ::_tputenv_s(kTmpKey, original_tmp);
1482 free(original_tmp);
1483 } else {
1484 ::_tputenv_s(kTmpKey, _T(""));
1485 }
1486}
1487#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381488
[email protected]33edeab2009-08-18 16:07:551489TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1490 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391491 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551492 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391493 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1494 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1495 }
1496 for (int i = 0; i < 3; i++)
1497 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1498 for (int i = 0; i < 3; i++)
1499 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1500}
1501
[email protected]33edeab2009-08-18 16:07:551502TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391503 FilePath names[3];
1504 FILE *fps[3];
1505 int i;
1506
1507 // Create; make sure they are open and exist.
1508 for (i = 0; i < 3; ++i) {
1509 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1510 ASSERT_TRUE(fps[i]);
1511 EXPECT_TRUE(file_util::PathExists(names[i]));
1512 }
1513
1514 // Make sure all names are unique.
1515 for (i = 0; i < 3; ++i) {
1516 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1517 }
1518
1519 // Close and delete.
1520 for (i = 0; i < 3; ++i) {
1521 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1522 EXPECT_TRUE(file_util::Delete(names[i], false));
1523 }
initial.commitd7cae122008-07-26 21:49:381524}
1525
1526TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141527 FilePath temp_dir;
1528 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1529 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111530 EXPECT_TRUE(file_util::PathExists(temp_dir));
1531 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381532}
1533
[email protected]b0b3abd92010-04-30 17:00:091534TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1535 FilePath new_dir;
1536 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
[email protected]90314c0a82010-09-15 20:40:471537 temp_dir_.path(),
[email protected]b0b3abd92010-04-30 17:00:091538 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
[email protected]046062e82010-06-30 07:19:111539 &new_dir));
[email protected]b0b3abd92010-04-30 17:00:091540 EXPECT_TRUE(file_util::PathExists(new_dir));
[email protected]90314c0a82010-09-15 20:40:471541 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
[email protected]b0b3abd92010-04-30 17:00:091542 EXPECT_TRUE(file_util::Delete(new_dir, false));
1543}
1544
[email protected]9e51af92009-02-04 00:58:391545TEST_F(FileUtilTest, GetShmemTempDirTest) {
1546 FilePath dir;
1547 EXPECT_TRUE(file_util::GetShmemTempDir(&dir));
1548 EXPECT_TRUE(file_util::DirectoryExists(dir));
1549}
1550
initial.commitd7cae122008-07-26 21:49:381551TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041552 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471553 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131554#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041555 FilePath test_path =
1556 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101557#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041558 FilePath test_path =
1559 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101560#endif
[email protected]806b9c62008-09-11 16:09:111561
1562 EXPECT_FALSE(file_util::PathExists(test_path));
1563 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1564 EXPECT_TRUE(file_util::PathExists(test_path));
1565 // CreateDirectory returns true if the DirectoryExists returns true.
1566 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1567
1568 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041569 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111570 EXPECT_FALSE(file_util::PathExists(test_path));
1571 CreateTextFile(test_path, L"test file");
1572 EXPECT_TRUE(file_util::PathExists(test_path));
1573 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1574
1575 EXPECT_TRUE(file_util::Delete(test_root, true));
1576 EXPECT_FALSE(file_util::PathExists(test_root));
1577 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411578
1579 // Verify assumptions made by the Windows implementation:
1580 // 1. The current directory always exists.
1581 // 2. The root directory always exists.
1582 ASSERT_TRUE(file_util::DirectoryExists(
1583 FilePath(FilePath::kCurrentDirectory)));
1584 FilePath top_level = test_root;
1585 while (top_level != top_level.DirName()) {
1586 top_level = top_level.DirName();
1587 }
1588 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1589
1590 // Given these assumptions hold, it should be safe to
1591 // test that "creating" these directories succeeds.
1592 EXPECT_TRUE(file_util::CreateDirectory(
1593 FilePath(FilePath::kCurrentDirectory)));
1594 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401595
1596#if defined(OS_WIN)
1597 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1598 FilePath invalid_path =
1599 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1600 if (!file_util::PathExists(invalid_drive)) {
1601 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1602 }
1603#endif
[email protected]806b9c62008-09-11 16:09:111604}
1605
1606TEST_F(FileUtilTest, DetectDirectoryTest) {
1607 // Check a directory
[email protected]640517f2008-10-30 23:54:041608 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471609 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111610 EXPECT_FALSE(file_util::PathExists(test_root));
1611 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1612 EXPECT_TRUE(file_util::PathExists(test_root));
1613 EXPECT_TRUE(file_util::DirectoryExists(test_root));
1614
1615 // Check a file
[email protected]640517f2008-10-30 23:54:041616 FilePath test_path =
1617 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111618 EXPECT_FALSE(file_util::PathExists(test_path));
1619 CreateTextFile(test_path, L"test file");
1620 EXPECT_TRUE(file_util::PathExists(test_path));
1621 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1622 EXPECT_TRUE(file_util::Delete(test_path, false));
1623
1624 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381625}
1626
initial.commitd7cae122008-07-26 21:49:381627TEST_F(FileUtilTest, FileEnumeratorTest) {
1628 // Test an empty directory.
[email protected]90314c0a82010-09-15 20:40:471629 file_util::FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121630 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1631 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381632
[email protected]8199b3a2009-06-09 05:57:381633 // Test an empty directory, non-recursively, including "..".
[email protected]90314c0a82010-09-15 20:40:471634 file_util::FileEnumerator f0_dotdot(temp_dir_.path(), false,
[email protected]58b7c5a62011-08-15 13:09:271635 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:381636 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
[email protected]90314c0a82010-09-15 20:40:471637 EXPECT_EQ(temp_dir_.path().Append(FILE_PATH_LITERAL("..")).value(),
[email protected]8199b3a2009-06-09 05:57:381638 f0_dotdot.Next().value());
1639 EXPECT_EQ(FILE_PATH_LITERAL(""),
1640 f0_dotdot.Next().value());
1641
[email protected]37088fef2008-08-15 17:32:101642 // create the directories
[email protected]90314c0a82010-09-15 20:40:471643 FilePath dir1 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101644 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]90314c0a82010-09-15 20:40:471645 FilePath dir2 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101646 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041647 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101648 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041649
[email protected]37088fef2008-08-15 17:32:101650 // create the files
[email protected]640517f2008-10-30 23:54:041651 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101652 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041653 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101654 CreateTextFile(dir2innerfile, L"");
[email protected]90314c0a82010-09-15 20:40:471655 FilePath file1 = temp_dir_.path().Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101656 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041657 FilePath file2_rel =
1658 dir2.Append(FilePath::kParentDirectory)
1659 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101660 CreateTextFile(file2_rel, L"");
[email protected]90314c0a82010-09-15 20:40:471661 FilePath file2_abs = temp_dir_.path().Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381662
1663 // Only enumerate files.
[email protected]90314c0a82010-09-15 20:40:471664 file_util::FileEnumerator f1(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381665 file_util::FileEnumerator::FILES);
1666 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101667 EXPECT_TRUE(c1.HasFile(file1));
1668 EXPECT_TRUE(c1.HasFile(file2_abs));
1669 EXPECT_TRUE(c1.HasFile(dir2file));
1670 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1671 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381672
1673 // Only enumerate directories.
[email protected]90314c0a82010-09-15 20:40:471674 file_util::FileEnumerator f2(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381675 file_util::FileEnumerator::DIRECTORIES);
1676 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101677 EXPECT_TRUE(c2.HasFile(dir1));
1678 EXPECT_TRUE(c2.HasFile(dir2));
1679 EXPECT_TRUE(c2.HasFile(dir2inner));
1680 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381681
[email protected]3bc9ffaf2008-10-16 02:42:451682 // Only enumerate directories non-recursively.
1683 file_util::FileEnumerator f2_non_recursive(
[email protected]90314c0a82010-09-15 20:40:471684 temp_dir_.path(), false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451685 FindResultCollector c2_non_recursive(f2_non_recursive);
1686 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1687 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1688 EXPECT_EQ(c2_non_recursive.size(), 2);
1689
[email protected]8199b3a2009-06-09 05:57:381690 // Only enumerate directories, non-recursively, including "..".
1691 file_util::FileEnumerator f2_dotdot(
[email protected]90314c0a82010-09-15 20:40:471692 temp_dir_.path(), false,
[email protected]58b7c5a62011-08-15 13:09:271693 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:381694 file_util::FileEnumerator::DIRECTORIES |
1695 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1696 FindResultCollector c2_dotdot(f2_dotdot);
1697 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1698 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
[email protected]90314c0a82010-09-15 20:40:471699 EXPECT_TRUE(c2_dotdot.HasFile(
1700 temp_dir_.path().Append(FILE_PATH_LITERAL(".."))));
[email protected]8199b3a2009-06-09 05:57:381701 EXPECT_EQ(c2_dotdot.size(), 3);
1702
initial.commitd7cae122008-07-26 21:49:381703 // Enumerate files and directories.
[email protected]90314c0a82010-09-15 20:40:471704 file_util::FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381705 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101706 EXPECT_TRUE(c3.HasFile(dir1));
1707 EXPECT_TRUE(c3.HasFile(dir2));
1708 EXPECT_TRUE(c3.HasFile(file1));
1709 EXPECT_TRUE(c3.HasFile(file2_abs));
1710 EXPECT_TRUE(c3.HasFile(dir2file));
1711 EXPECT_TRUE(c3.HasFile(dir2inner));
1712 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1713 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381714
1715 // Non-recursive operation.
[email protected]90314c0a82010-09-15 20:40:471716 file_util::FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381717 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101718 EXPECT_TRUE(c4.HasFile(dir2));
1719 EXPECT_TRUE(c4.HasFile(dir2));
1720 EXPECT_TRUE(c4.HasFile(file1));
1721 EXPECT_TRUE(c4.HasFile(file2_abs));
1722 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381723
1724 // Enumerate with a pattern.
[email protected]90314c0a82010-09-15 20:40:471725 file_util::FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121726 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381727 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101728 EXPECT_TRUE(c5.HasFile(dir1));
1729 EXPECT_TRUE(c5.HasFile(dir2));
1730 EXPECT_TRUE(c5.HasFile(dir2file));
1731 EXPECT_TRUE(c5.HasFile(dir2inner));
1732 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1733 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381734
1735 // Make sure the destructor closes the find handle while in the middle of a
1736 // query to allow TearDown to delete the directory.
[email protected]90314c0a82010-09-15 20:40:471737 file_util::FileEnumerator f6(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121738 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1739 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381740}
license.botbf09a502008-08-24 00:55:551741
[email protected]ee5c29da2009-01-09 22:14:271742TEST_F(FileUtilTest, Contains) {
[email protected]90314c0a82010-09-15 20:40:471743 FilePath data_dir =
1744 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271745
1746 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571747 if (file_util::PathExists(data_dir)) {
1748 ASSERT_TRUE(file_util::Delete(data_dir, true));
1749 }
[email protected]ee5c29da2009-01-09 22:14:271750 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1751
1752 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1753 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1754 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1755 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1756
1757 // Annoyingly, the directories must actually exist in order for realpath(),
1758 // which Contains() relies on in posix, to work.
1759 ASSERT_TRUE(file_util::CreateDirectory(foo));
1760 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581761 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1762 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1763 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271764
1765 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1766 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1767 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1768 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1769
[email protected]e43eddf12009-12-29 00:32:521770 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271771 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1772#if defined(OS_WIN)
1773 EXPECT_TRUE(file_util::ContainsPath(foo,
1774 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391775 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271776 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521777#elif defined(OS_MACOSX)
1778 // We can't really do this test on OS X since the case-sensitivity of the
1779 // filesystem is configurable.
1780#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271781 EXPECT_FALSE(file_util::ContainsPath(foo,
1782 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271783#endif
1784}
1785
[email protected]507fb9a2010-09-23 23:28:221786TEST_F(FileUtilTest, TouchFile) {
[email protected]90314c0a82010-09-15 20:40:471787 FilePath data_dir =
1788 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ec3d1452010-02-18 10:02:261789
1790 // Create a fresh, empty copy of this directory.
1791 if (file_util::PathExists(data_dir)) {
1792 ASSERT_TRUE(file_util::Delete(data_dir, true));
1793 }
1794 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1795
1796 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1797 std::string data("hello");
1798 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1799
[email protected]507fb9a2010-09-23 23:28:221800 base::Time access_time;
1801 // This timestamp is divisible by one day (in local timezone),
1802 // to make it work on FAT too.
[email protected]46470aa2011-08-03 05:28:101803 ASSERT_TRUE(base::Time::FromString("Wed, 16 Nov 1994, 00:00:00",
[email protected]507fb9a2010-09-23 23:28:221804 &access_time));
1805
[email protected]ec3d1452010-02-18 10:02:261806 base::Time modification_time;
1807 // Note that this timestamp is divisible by two (seconds) - FAT stores
1808 // modification times with 2s resolution.
[email protected]46470aa2011-08-03 05:28:101809 ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
[email protected]ec3d1452010-02-18 10:02:261810 &modification_time));
[email protected]507fb9a2010-09-23 23:28:221811
1812 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
[email protected]2f0193c22010-09-03 02:28:371813 base::PlatformFileInfo file_info;
[email protected]ec3d1452010-02-18 10:02:261814 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
[email protected]507fb9a2010-09-23 23:28:221815 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
1816 access_time.ToInternalValue());
1817 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
1818 modification_time.ToInternalValue());
[email protected]ec3d1452010-02-18 10:02:261819}
1820
[email protected]b33f1d92010-05-26 01:40:121821TEST_F(FileUtilTest, IsDirectoryEmpty) {
[email protected]90314c0a82010-09-15 20:40:471822 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
[email protected]b33f1d92010-05-26 01:40:121823
1824 ASSERT_FALSE(file_util::PathExists(empty_dir));
1825
1826 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1827
1828 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1829
1830 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1831 std::string bar("baz");
1832 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1833
1834 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1835}
1836
[email protected]73e4c362011-09-22 14:47:181837#if defined(OS_POSIX)
1838
1839// Testing VerifyPathControlledByAdmin() is hard, because there is no
1840// way a test can make a file owned by root, or change file paths
1841// at the root of the file system. VerifyPathControlledByAdmin()
1842// is implemented as a call to VerifyPathControlledByUser, which gives
1843// us the ability to test with paths under the test's temp directory,
1844// using a user id we control.
1845// Pull tests of VerifyPathControlledByUserTest() into a separate test class
1846// with a common SetUp() method.
1847class VerifyPathControlledByUserTest : public FileUtilTest {
1848 protected:
1849 virtual void SetUp() {
1850 FileUtilTest::SetUp();
1851
1852 // Create a basic structure used by each test.
1853 // base_dir_
1854 // |-> sub_dir_
1855 // |-> text_file_
1856
1857 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
1858 ASSERT_TRUE(file_util::CreateDirectory(base_dir_));
1859
1860 sub_dir_ = base_dir_.AppendASCII("sub_dir");
1861 ASSERT_TRUE(file_util::CreateDirectory(sub_dir_));
1862
1863 text_file_ = sub_dir_.AppendASCII("file.txt");
1864 CreateTextFile(text_file_, L"This text file has some text in it.");
1865
[email protected]30fcaa492011-09-26 17:18:431866 // Get the user and group files are created with from |base_dir_|.
1867 struct stat stat_buf;
1868 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
1869 uid_ = stat_buf.st_uid;
1870 gid_ = stat_buf.st_gid;
1871 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
[email protected]73e4c362011-09-22 14:47:181872
1873 // To ensure that umask settings do not cause the initial state
1874 // of permissions to be different from what we expect, explicitly
1875 // set permissions on the directories we create.
1876 // Make all files and directories non-world-writable.
1877 mode_t enabled_permissions =
1878 S_IRWXU | // User can read, write, traverse
1879 S_IRWXG; // Group can read, write, traverse
1880 mode_t disabled_permissions =
1881 S_IRWXO; // Other users can't read, write, traverse.
1882
1883 ASSERT_NO_FATAL_FAILURE(
1884 ChangePosixFilePermissions(
1885 base_dir_, enabled_permissions, disabled_permissions));
1886 ASSERT_NO_FATAL_FAILURE(
1887 ChangePosixFilePermissions(
1888 sub_dir_, enabled_permissions, disabled_permissions));
1889 }
1890
1891 FilePath base_dir_;
1892 FilePath sub_dir_;
1893 FilePath text_file_;
1894 uid_t uid_;
1895 gid_t gid_;
1896};
1897
[email protected]30fcaa492011-09-26 17:18:431898TEST_F(VerifyPathControlledByUserTest, BadPaths) {
[email protected]73e4c362011-09-22 14:47:181899 // File does not exist.
1900 FilePath does_not_exist = base_dir_.AppendASCII("does")
1901 .AppendASCII("not")
1902 .AppendASCII("exist");
1903
1904 EXPECT_FALSE(
1905 file_util::VerifyPathControlledByUser(
1906 base_dir_, does_not_exist, uid_, gid_));
1907
1908 // |base| not a subpath of |path|.
1909 EXPECT_FALSE(
1910 file_util::VerifyPathControlledByUser(sub_dir_, base_dir_, uid_, gid_));
1911
1912 // An empty base path will fail to be a prefix for any path.
1913 FilePath empty;
1914 EXPECT_FALSE(
1915 file_util::VerifyPathControlledByUser(empty, base_dir_, uid_, gid_));
1916
1917 // Finding that a bad call fails proves nothing unless a good call succeeds.
1918 EXPECT_TRUE(
1919 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
1920}
1921
1922TEST_F(VerifyPathControlledByUserTest, Symlinks) {
1923 // Symlinks in the path should cause failure.
1924
1925 // Symlink to the file at the end of the path.
1926 FilePath file_link = base_dir_.AppendASCII("file_link");
1927 ASSERT_TRUE(file_util::CreateSymbolicLink(text_file_, file_link))
1928 << "Failed to create symlink.";
1929
1930 EXPECT_FALSE(
1931 file_util::VerifyPathControlledByUser(base_dir_, file_link, uid_, gid_));
1932 EXPECT_FALSE(
1933 file_util::VerifyPathControlledByUser(file_link, file_link, uid_, gid_));
1934
1935 // Symlink from one directory to another within the path.
1936 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
1937 ASSERT_TRUE(file_util::CreateSymbolicLink(sub_dir_, link_to_sub_dir))
1938 << "Failed to create symlink.";
1939
1940 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
1941 ASSERT_TRUE(file_util::PathExists(file_path_with_link));
1942
1943 EXPECT_FALSE(
1944 file_util::VerifyPathControlledByUser(
1945 base_dir_, file_path_with_link, uid_, gid_));
1946
1947 EXPECT_FALSE(
1948 file_util::VerifyPathControlledByUser(
1949 link_to_sub_dir, file_path_with_link, uid_, gid_));
1950
1951 // Symlinks in parents of base path are allowed.
1952 EXPECT_TRUE(
1953 file_util::VerifyPathControlledByUser(
1954 file_path_with_link, file_path_with_link, uid_, gid_));
1955}
1956
[email protected]30fcaa492011-09-26 17:18:431957TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
[email protected]73e4c362011-09-22 14:47:181958 // Get a uid that is not the uid of files we create.
1959 uid_t bad_uid = uid_ + 1;
1960
1961 // Get a gid that is not ours.
1962 gid_t bad_gid = gid_ + 1;
1963
1964 // Make all files and directories non-world-writable.
1965 ASSERT_NO_FATAL_FAILURE(
1966 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
1967 ASSERT_NO_FATAL_FAILURE(
1968 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
1969 ASSERT_NO_FATAL_FAILURE(
1970 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
1971
1972 // We control these paths.
1973 EXPECT_TRUE(
1974 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
1975 EXPECT_TRUE(
1976 file_util::VerifyPathControlledByUser(base_dir_, text_file_, uid_, gid_));
1977 EXPECT_TRUE(
1978 file_util::VerifyPathControlledByUser(sub_dir_, text_file_, uid_, gid_));
1979
1980 // Another user does not control these paths.
1981 EXPECT_FALSE(
1982 file_util::VerifyPathControlledByUser(
1983 base_dir_, sub_dir_, bad_uid, gid_));
1984 EXPECT_FALSE(
1985 file_util::VerifyPathControlledByUser(
1986 base_dir_, text_file_, bad_uid, gid_));
1987 EXPECT_FALSE(
1988 file_util::VerifyPathControlledByUser(
1989 sub_dir_, text_file_, bad_uid, gid_));
1990
1991 // Another group does not control the paths.
1992 EXPECT_FALSE(
1993 file_util::VerifyPathControlledByUser(
1994 base_dir_, sub_dir_, uid_, bad_gid));
1995 EXPECT_FALSE(
1996 file_util::VerifyPathControlledByUser(
1997 base_dir_, text_file_, uid_, bad_gid));
1998 EXPECT_FALSE(
1999 file_util::VerifyPathControlledByUser(
2000 sub_dir_, text_file_, uid_, bad_gid));
2001}
2002
[email protected]30fcaa492011-09-26 17:18:432003TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
[email protected]73e4c362011-09-22 14:47:182004 // Make all files and directories non-world-writable.
2005 ASSERT_NO_FATAL_FAILURE(
2006 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2007 ASSERT_NO_FATAL_FAILURE(
2008 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2009 ASSERT_NO_FATAL_FAILURE(
2010 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2011
2012 // Initialy, we control all parts of the path.
2013 EXPECT_TRUE(
2014 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
2015 EXPECT_TRUE(
2016 file_util::VerifyPathControlledByUser(base_dir_, text_file_, uid_, gid_));
2017 EXPECT_TRUE(
2018 file_util::VerifyPathControlledByUser(sub_dir_, text_file_, uid_, gid_));
2019
2020 // Make base_dir_ world-writable.
2021 ASSERT_NO_FATAL_FAILURE(
2022 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2023 EXPECT_FALSE(
2024 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
2025 EXPECT_FALSE(
2026 file_util::VerifyPathControlledByUser(base_dir_, text_file_, uid_, gid_));
2027 EXPECT_TRUE(
2028 file_util::VerifyPathControlledByUser(sub_dir_, text_file_, uid_, gid_));
2029
2030 // Make sub_dir_ world writable.
2031 ASSERT_NO_FATAL_FAILURE(
2032 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2033 EXPECT_FALSE(
2034 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
2035 EXPECT_FALSE(
2036 file_util::VerifyPathControlledByUser(base_dir_, text_file_, uid_, gid_));
2037 EXPECT_FALSE(
2038 file_util::VerifyPathControlledByUser(sub_dir_, text_file_, uid_, gid_));
2039
2040 // Make text_file_ world writable.
2041 ASSERT_NO_FATAL_FAILURE(
2042 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2043 EXPECT_FALSE(
2044 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
2045 EXPECT_FALSE(
2046 file_util::VerifyPathControlledByUser(base_dir_, text_file_, uid_, gid_));
2047 EXPECT_FALSE(
2048 file_util::VerifyPathControlledByUser(sub_dir_, text_file_, uid_, gid_));
2049
2050 // Make sub_dir_ non-world writable.
2051 ASSERT_NO_FATAL_FAILURE(
2052 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2053 EXPECT_FALSE(
2054 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
2055 EXPECT_FALSE(
2056 file_util::VerifyPathControlledByUser(base_dir_, text_file_, uid_, gid_));
2057 EXPECT_FALSE(
2058 file_util::VerifyPathControlledByUser(sub_dir_, text_file_, uid_, gid_));
2059
2060 // Make base_dir_ non-world-writable.
2061 ASSERT_NO_FATAL_FAILURE(
2062 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2063 EXPECT_TRUE(
2064 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
2065 EXPECT_FALSE(
2066 file_util::VerifyPathControlledByUser(base_dir_, text_file_, uid_, gid_));
2067 EXPECT_FALSE(
2068 file_util::VerifyPathControlledByUser(sub_dir_, text_file_, uid_, gid_));
2069
2070 // Back to the initial state: Nothing is writable, so every path
2071 // should pass.
2072 ASSERT_NO_FATAL_FAILURE(
2073 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2074 EXPECT_TRUE(
2075 file_util::VerifyPathControlledByUser(base_dir_, sub_dir_, uid_, gid_));
2076 EXPECT_TRUE(
2077 file_util::VerifyPathControlledByUser(base_dir_, text_file_, uid_, gid_));
2078 EXPECT_TRUE(
2079 file_util::VerifyPathControlledByUser(sub_dir_, text_file_, uid_, gid_));
2080}
2081
2082#endif // defined(OS_POSIX)
2083
[email protected]11b901ee2008-09-10 00:16:282084} // namespace