blob: 4cd6f03dd7f437cb30640f8c93d540b4c0467a02 [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
[email protected]aead7d32011-10-25 00:08:2815#include <algorithm>
initial.commitd7cae122008-07-26 21:49:3816#include <fstream>
[email protected]37088fef2008-08-15 17:32:1017#include <set>
initial.commitd7cae122008-07-26 21:49:3818
19#include "base/base_paths.h"
[email protected]640517f2008-10-30 23:54:0420#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3821#include "base/file_util.h"
initial.commitd7cae122008-07-26 21:49:3822#include "base/path_service.h"
[email protected]e0785902011-05-19 23:34:1723#include "base/scoped_temp_dir.h"
[email protected]ce072a72010-12-31 20:02:1624#include "base/threading/platform_thread.h"
[email protected]85786f92009-04-18 00:42:4825#include "base/time.h"
[email protected]047a03f2009-10-07 02:10:2026#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3827#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1528#include "testing/platform_test.h"
initial.commitd7cae122008-07-26 21:49:3829
[email protected]b90d7e802011-01-09 16:32:2030#if defined(OS_WIN)
31#include "base/win/scoped_handle.h"
32#endif
33
[email protected]4e9f6be2009-04-03 17:17:5834// This macro helps avoid wrapped lines in the test structs.
35#define FPL(x) FILE_PATH_LITERAL(x)
36
initial.commitd7cae122008-07-26 21:49:3837namespace {
38
[email protected]6f5f4322010-06-09 22:56:4839// To test that file_util::Normalize FilePath() deals with NTFS reparse points
40// correctly, we need functions to create and delete reparse points.
41#if defined(OS_WIN)
42typedef struct _REPARSE_DATA_BUFFER {
43 ULONG ReparseTag;
44 USHORT ReparseDataLength;
45 USHORT Reserved;
46 union {
47 struct {
48 USHORT SubstituteNameOffset;
49 USHORT SubstituteNameLength;
50 USHORT PrintNameOffset;
51 USHORT PrintNameLength;
52 ULONG Flags;
53 WCHAR PathBuffer[1];
54 } SymbolicLinkReparseBuffer;
55 struct {
56 USHORT SubstituteNameOffset;
57 USHORT SubstituteNameLength;
58 USHORT PrintNameOffset;
59 USHORT PrintNameLength;
60 WCHAR PathBuffer[1];
61 } MountPointReparseBuffer;
62 struct {
63 UCHAR DataBuffer[1];
64 } GenericReparseBuffer;
65 };
66} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
67
68// Sets a reparse point. |source| will now point to |target|. Returns true if
69// the call succeeds, false otherwise.
70bool SetReparsePoint(HANDLE source, const FilePath& target_path) {
71 std::wstring kPathPrefix = L"\\??\\";
72 std::wstring target_str;
73 // The juction will not work if the target path does not start with \??\ .
74 if (kPathPrefix != target_path.value().substr(0, kPathPrefix.size()))
75 target_str += kPathPrefix;
76 target_str += target_path.value();
77 const wchar_t* target = target_str.c_str();
78 USHORT size_target = static_cast<USHORT>(wcslen(target)) * sizeof(target[0]);
79 char buffer[2000] = {0};
80 DWORD returned;
81
82 REPARSE_DATA_BUFFER* data = reinterpret_cast<REPARSE_DATA_BUFFER*>(buffer);
83
84 data->ReparseTag = 0xa0000003;
85 memcpy(data->MountPointReparseBuffer.PathBuffer, target, size_target + 2);
86
87 data->MountPointReparseBuffer.SubstituteNameLength = size_target;
88 data->MountPointReparseBuffer.PrintNameOffset = size_target + 2;
89 data->ReparseDataLength = size_target + 4 + 8;
90
91 int data_size = data->ReparseDataLength + 8;
92
93 if (!DeviceIoControl(source, FSCTL_SET_REPARSE_POINT, &buffer, data_size,
94 NULL, 0, &returned, NULL)) {
95 return false;
96 }
97 return true;
98}
99
100// Delete the reparse point referenced by |source|. Returns true if the call
101// succeeds, false otherwise.
102bool DeleteReparsePoint(HANDLE source) {
103 DWORD returned;
104 REPARSE_DATA_BUFFER data = {0};
105 data.ReparseTag = 0xa0000003;
106 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0,
107 &returned, NULL)) {
108 return false;
109 }
110 return true;
111}
112#endif
113
[email protected]73e4c362011-09-22 14:47:18114#if defined(OS_POSIX)
115// Provide a simple way to change the permissions bits on |path| in tests.
116// ASSERT failures will return, but not stop the test. Caller should wrap
117// calls to this function in ASSERT_NO_FATAL_FAILURE().
118void ChangePosixFilePermissions(const FilePath& path,
119 mode_t mode_bits_to_set,
120 mode_t mode_bits_to_clear) {
121 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
122 << "Can't set and clear the same bits.";
123
124 struct stat stat_buf;
125 ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf));
126
127 mode_t updated_mode_bits = stat_buf.st_mode;
128 updated_mode_bits |= mode_bits_to_set;
129 updated_mode_bits &= ~mode_bits_to_clear;
130
131 ASSERT_EQ(0, chmod(path.value().c_str(), updated_mode_bits));
132}
133#endif // defined(OS_POSIX)
134
[email protected]83d86252010-05-07 18:58:45135const wchar_t bogus_content[] = L"I'm cannon fodder.";
136
[email protected]58b7c5a62011-08-15 13:09:27137const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES =
138 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:38139 file_util::FileEnumerator::FILES |
140 file_util::FileEnumerator::DIRECTORIES);
141
[email protected]ed2f2332008-08-20 15:59:49142// file_util winds up using autoreleased objects on the Mac, so this needs
143// to be a PlatformTest
144class FileUtilTest : public PlatformTest {
initial.commitd7cae122008-07-26 21:49:38145 protected:
146 virtual void SetUp() {
[email protected]ed2f2332008-08-20 15:59:49147 PlatformTest::SetUp();
[email protected]90314c0a82010-09-15 20:40:47148 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
initial.commitd7cae122008-07-26 21:49:38149 }
150
[email protected]90314c0a82010-09-15 20:40:47151 ScopedTempDir temp_dir_;
initial.commitd7cae122008-07-26 21:49:38152};
153
154// Collects all the results from the given file enumerator, and provides an
155// interface to query whether a given file is present.
156class FindResultCollector {
157 public:
[email protected]53c58042009-08-26 20:00:14158 explicit FindResultCollector(file_util::FileEnumerator& enumerator) {
[email protected]0b733222008-12-11 14:55:12159 FilePath cur_file;
160 while (!(cur_file = enumerator.Next()).value().empty()) {
161 FilePath::StringType path = cur_file.value();
initial.commitd7cae122008-07-26 21:49:38162 // The file should not be returned twice.
[email protected]640517f2008-10-30 23:54:04163 EXPECT_TRUE(files_.end() == files_.find(path))
initial.commitd7cae122008-07-26 21:49:38164 << "Same file returned twice";
165
166 // Save for later.
[email protected]640517f2008-10-30 23:54:04167 files_.insert(path);
initial.commitd7cae122008-07-26 21:49:38168 }
169 }
170
171 // Returns true if the enumerator found the file.
[email protected]640517f2008-10-30 23:54:04172 bool HasFile(const FilePath& file) const {
173 return files_.find(file.value()) != files_.end();
initial.commitd7cae122008-07-26 21:49:38174 }
[email protected]640517f2008-10-30 23:54:04175
[email protected]37088fef2008-08-15 17:32:10176 int size() {
[email protected]f35d39b2008-08-15 17:50:10177 return static_cast<int>(files_.size());
[email protected]37088fef2008-08-15 17:32:10178 }
initial.commitd7cae122008-07-26 21:49:38179
180 private:
[email protected]640517f2008-10-30 23:54:04181 std::set<FilePath::StringType> files_;
initial.commitd7cae122008-07-26 21:49:38182};
183
184// Simple function to dump some text into a new file.
[email protected]640517f2008-10-30 23:54:04185void CreateTextFile(const FilePath& filename,
initial.commitd7cae122008-07-26 21:49:38186 const std::wstring& contents) {
[email protected]76c551c2011-03-25 20:49:54187 std::wofstream file;
[email protected]8a205c02011-02-04 20:41:33188 file.open(filename.value().c_str());
initial.commitd7cae122008-07-26 21:49:38189 ASSERT_TRUE(file.is_open());
190 file << contents;
191 file.close();
192}
193
194// Simple function to take out some text from a file.
[email protected]640517f2008-10-30 23:54:04195std::wstring ReadTextFile(const FilePath& filename) {
[email protected]37088fef2008-08-15 17:32:10196 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38197 std::wifstream file;
[email protected]8a205c02011-02-04 20:41:33198 file.open(filename.value().c_str());
initial.commitd7cae122008-07-26 21:49:38199 EXPECT_TRUE(file.is_open());
[email protected]76c551c2011-03-25 20:49:54200 file.getline(contents, arraysize(contents));
initial.commitd7cae122008-07-26 21:49:38201 file.close();
202 return std::wstring(contents);
203}
204
[email protected]8541bb82008-08-15 17:45:13205#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38206uint64 FileTimeAsUint64(const FILETIME& ft) {
207 ULARGE_INTEGER u;
208 u.LowPart = ft.dwLowDateTime;
209 u.HighPart = ft.dwHighDateTime;
210 return u.QuadPart;
211}
[email protected]37088fef2008-08-15 17:32:10212#endif
initial.commitd7cae122008-07-26 21:49:38213
214const struct append_case {
215 const wchar_t* path;
216 const wchar_t* ending;
217 const wchar_t* result;
218} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13219#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38220 {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"path", L"c:\\colon\\backslash\\\\path"},
223 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
224 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
225 {L"", L"path", L"\\path"},
226 {L"", L"", L"\\"},
[email protected]37088fef2008-08-15 17:32:10227#elif defined(OS_POSIX)
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"path", L"/foo/bar//path"},
231 {L"/foo/bar/", L"", L"/foo/bar/"},
232 {L"/foo/bar", L"", L"/foo/bar/"},
233 {L"", L"path", L"/path"},
234 {L"", L"", L"/"},
235#endif
initial.commitd7cae122008-07-26 21:49:38236};
237
[email protected]1840cfc2010-02-26 15:11:55238#if defined(OS_WIN)
239// This function is deprecated, but still used on Windows.
initial.commitd7cae122008-07-26 21:49:38240TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10241 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38242 const append_case& value = append_cases[i];
243 std::wstring result = value.path;
244 file_util::AppendToPath(&result, value.ending);
245 EXPECT_EQ(value.result, result);
246 }
247
[email protected]20960e072011-09-20 20:59:01248#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
initial.commitd7cae122008-07-26 21:49:38249 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
250#endif
251}
[email protected]1840cfc2010-02-26 15:11:55252#endif // defined(OS_WIN)
253
initial.commitd7cae122008-07-26 21:49:38254static const struct filename_case {
255 const wchar_t* path;
256 const wchar_t* filename;
257} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13258#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38259 {L"c:\\colon\\backslash", L"backslash"},
260 {L"c:\\colon\\backslash\\", L""},
261 {L"\\\\filename.exe", L"filename.exe"},
262 {L"filename.exe", L"filename.exe"},
263 {L"", L""},
264 {L"\\\\\\", L""},
265 {L"c:/colon/backslash", L"backslash"},
266 {L"c:/colon/backslash/", L""},
267 {L"//////", L""},
268 {L"///filename.exe", L"filename.exe"},
[email protected]37088fef2008-08-15 17:32:10269#elif defined(OS_POSIX)
270 {L"/foo/bar", L"bar"},
271 {L"/foo/bar/", L""},
272 {L"/filename.exe", L"filename.exe"},
273 {L"filename.exe", L"filename.exe"},
274 {L"", L""},
275 {L"/", L""},
276#endif
initial.commitd7cae122008-07-26 21:49:38277};
278
initial.commitd7cae122008-07-26 21:49:38279// Test finding the file type from a path name
280static const struct extension_case {
281 const wchar_t* path;
282 const wchar_t* extension;
283} extension_cases[] = {
[email protected]8541bb82008-08-15 17:45:13284#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38285 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
286 {L"C:\\colon\\backslash\\filename.", L""},
287 {L"C:\\colon\\backslash\\filename", L""},
288 {L"C:\\colon\\backslash\\", L""},
289 {L"C:\\colon\\backslash.\\", L""},
290 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
[email protected]37088fef2008-08-15 17:32:10291#elif defined(OS_POSIX)
292 {L"/foo/bar/filename.extension", L"extension"},
293 {L"/foo/bar/filename.", L""},
294 {L"/foo/bar/filename", L""},
295 {L"/foo/bar/", L""},
296 {L"/foo/bar./", L""},
297 {L"/foo/bar/filename.extension.extension2", L"extension2"},
298 {L".", L""},
299 {L"..", L""},
300 {L"./foo", L""},
301 {L"./foo.extension", L"extension"},
302 {L"/foo.extension1/bar.extension2", L"extension2"},
303#endif
initial.commitd7cae122008-07-26 21:49:38304};
305
[email protected]63597e4e2010-07-08 17:49:05306#if defined(OS_WIN)
307// This function has been deprecated on non-Windows.
initial.commitd7cae122008-07-26 21:49:38308TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10309 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38310 const extension_case& ext = extension_cases[i];
311 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
312 EXPECT_EQ(ext.extension, fext);
313 }
314}
[email protected]63597e4e2010-07-08 17:49:05315#endif
initial.commitd7cae122008-07-26 21:49:38316
317// Test finding the directory component of a path
318static const struct dir_case {
319 const wchar_t* full_path;
320 const wchar_t* directory;
321} dir_cases[] = {
[email protected]8541bb82008-08-15 17:45:13322#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38323 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
324 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
325 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
326 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
327 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
328 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
[email protected]2c8088a42009-10-15 05:00:25329 {L"C:\\", L"C:\\"},
[email protected]37088fef2008-08-15 17:32:10330#elif defined(OS_POSIX)
331 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
332 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
333 {L"/foo/bar/", L"/foo/bar"},
334 {L"/foo/bar//", L"/foo/bar"},
335 {L"/foo/bar", L"/foo"},
336 {L"/foo/bar./", L"/foo/bar."},
337 {L"/", L"/"},
338 {L".", L"."},
[email protected]53c58042009-08-26 20:00:14339 {L"..", L"."}, // yes, ".." technically lives in "."
[email protected]37088fef2008-08-15 17:32:10340#endif
initial.commitd7cae122008-07-26 21:49:38341};
342
[email protected]2ec79f72010-06-10 13:05:26343// Flaky, http://crbug.com/46246
[email protected]552b3152010-06-10 12:40:52344TEST_F(FileUtilTest, FLAKY_CountFilesCreatedAfter) {
initial.commitd7cae122008-07-26 21:49:38345 // Create old file (that we don't want to count)
[email protected]90314c0a82010-09-15 20:40:47346 FilePath old_file_name =
347 temp_dir_.path().Append(FILE_PATH_LITERAL("Old File.txt"));
initial.commitd7cae122008-07-26 21:49:38348 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
349
350 // Age to perfection
[email protected]4b7743de2009-04-21 01:50:39351#if defined(OS_WIN)
[email protected]ce072a72010-12-31 20:02:16352 base::PlatformThread::Sleep(100);
[email protected]4b7743de2009-04-21 01:50:39353#elif defined(OS_POSIX)
354 // We need to wait at least one second here because the precision of
355 // file creation time is one second.
[email protected]ce072a72010-12-31 20:02:16356 base::PlatformThread::Sleep(1500);
[email protected]4b7743de2009-04-21 01:50:39357#endif
initial.commitd7cae122008-07-26 21:49:38358
359 // Establish our cutoff time
[email protected]85786f92009-04-18 00:42:48360 base::Time now(base::Time::NowFromSystemTime());
[email protected]90314c0a82010-09-15 20:40:47361 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38362
363 // Create a new file (that we do want to count)
[email protected]90314c0a82010-09-15 20:40:47364 FilePath new_file_name =
365 temp_dir_.path().Append(FILE_PATH_LITERAL("New File.txt"));
initial.commitd7cae122008-07-26 21:49:38366 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
367
368 // We should see only the new file.
[email protected]90314c0a82010-09-15 20:40:47369 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38370
371 // Delete new file, we should see no files after cutoff now
372 EXPECT_TRUE(file_util::Delete(new_file_name, false));
[email protected]90314c0a82010-09-15 20:40:47373 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_.path(), now));
initial.commitd7cae122008-07-26 21:49:38374}
375
[email protected]c2c132c62010-03-24 21:56:26376TEST_F(FileUtilTest, FileAndDirectorySize) {
377 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
378 // should return 53 bytes.
[email protected]90314c0a82010-09-15 20:40:47379 FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
[email protected]c2c132c62010-03-24 21:56:26380 CreateTextFile(file_01, L"12345678901234567890");
381 int64 size_f1 = 0;
382 ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
383 EXPECT_EQ(20ll, size_f1);
384
[email protected]90314c0a82010-09-15 20:40:47385 FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
[email protected]c2c132c62010-03-24 21:56:26386 file_util::CreateDirectory(subdir_path);
387
388 FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
389 CreateTextFile(file_02, L"123456789012345678901234567890");
390 int64 size_f2 = 0;
391 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
392 EXPECT_EQ(30ll, size_f2);
393
394 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
395 file_util::CreateDirectory(subsubdir_path);
396
397 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
398 CreateTextFile(file_03, L"123");
399
[email protected]90314c0a82010-09-15 20:40:47400 int64 computed_size = file_util::ComputeDirectorySize(temp_dir_.path());
[email protected]c2c132c62010-03-24 21:56:26401 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
[email protected]a04876b92010-06-11 22:53:43402
[email protected]90314c0a82010-09-15 20:40:47403 computed_size =
404 file_util::ComputeFilesSize(temp_dir_.path(), FPL("The file*"));
[email protected]a04876b92010-06-11 22:53:43405 EXPECT_EQ(size_f1, computed_size);
406
[email protected]90314c0a82010-09-15 20:40:47407 computed_size = file_util::ComputeFilesSize(temp_dir_.path(), FPL("bla*"));
[email protected]a04876b92010-06-11 22:53:43408 EXPECT_EQ(0, computed_size);
[email protected]c2c132c62010-03-24 21:56:26409}
410
[email protected]6f5f4322010-06-09 22:56:48411TEST_F(FileUtilTest, NormalizeFilePathBasic) {
412 // Create a directory under the test dir. Because we create it,
413 // we know it is not a link.
[email protected]90314c0a82010-09-15 20:40:47414 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
415 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
[email protected]6f5f4322010-06-09 22:56:48416 FilePath file_b_path = dir_path.Append(FPL("file_b"));
417 file_util::CreateDirectory(dir_path);
[email protected]01e2a1f2010-05-12 15:13:57418
[email protected]6f5f4322010-06-09 22:56:48419 FilePath normalized_file_a_path, normalized_file_b_path;
420 ASSERT_FALSE(file_util::PathExists(file_a_path));
421 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
422 &normalized_file_a_path))
[email protected]e7afe2452010-08-22 16:19:13423 << "NormalizeFilePath() should fail on nonexistent paths.";
[email protected]6f5f4322010-06-09 22:56:48424
425 CreateTextFile(file_a_path, bogus_content);
426 ASSERT_TRUE(file_util::PathExists(file_a_path));
427 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
428 &normalized_file_a_path));
429
430 CreateTextFile(file_b_path, bogus_content);
431 ASSERT_TRUE(file_util::PathExists(file_b_path));
432 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
433 &normalized_file_b_path));
434
435 // Beacuse this test created |dir_path|, we know it is not a link
436 // or junction. So, the real path of the directory holding file a
437 // must be the parent of the path holding file b.
438 ASSERT_TRUE(normalized_file_a_path.DirName()
439 .IsParent(normalized_file_b_path.DirName()));
440}
441
442#if defined(OS_WIN)
443
444TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
445 // Build the following directory structure:
446 //
[email protected]90314c0a82010-09-15 20:40:47447 // temp_dir
[email protected]6f5f4322010-06-09 22:56:48448 // |-> base_a
449 // | |-> sub_a
450 // | |-> file.txt
451 // | |-> long_name___... (Very long name.)
452 // | |-> sub_long
453 // | |-> deep.txt
454 // |-> base_b
[email protected]90314c0a82010-09-15 20:40:47455 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
456 // |-> to_base_b (reparse point to temp_dir\base_b)
457 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
[email protected]6f5f4322010-06-09 22:56:48458
[email protected]90314c0a82010-09-15 20:40:47459 FilePath base_a = temp_dir_.path().Append(FPL("base_a"));
[email protected]6f5f4322010-06-09 22:56:48460 ASSERT_TRUE(file_util::CreateDirectory(base_a));
461
462 FilePath sub_a = base_a.Append(FPL("sub_a"));
463 ASSERT_TRUE(file_util::CreateDirectory(sub_a));
464
465 FilePath file_txt = sub_a.Append(FPL("file.txt"));
466 CreateTextFile(file_txt, bogus_content);
467
468 // Want a directory whose name is long enough to make the path to the file
469 // inside just under MAX_PATH chars. This will be used to test that when
470 // a junction expands to a path over MAX_PATH chars in length,
471 // NormalizeFilePath() fails without crashing.
472 FilePath sub_long_rel(FPL("sub_long"));
473 FilePath deep_txt(FPL("deep.txt"));
474
475 int target_length = MAX_PATH;
476 target_length -= (sub_a.value().length() + 1); // +1 for the sepperator '\'.
477 target_length -= (sub_long_rel.Append(deep_txt).value().length() + 1);
[email protected]552b3152010-06-10 12:40:52478 // Without making the path a bit shorter, CreateDirectory() fails.
[email protected]6f5f4322010-06-09 22:56:48479 // the resulting path is still long enough to hit the failing case in
480 // NormalizePath().
481 const int kCreateDirLimit = 4;
482 target_length -= kCreateDirLimit;
483 FilePath::StringType long_name_str = FPL("long_name_");
484 long_name_str.resize(target_length, '_');
485
486 FilePath long_name = sub_a.Append(FilePath(long_name_str));
487 FilePath deep_file = long_name.Append(sub_long_rel).Append(deep_txt);
488 ASSERT_EQ(MAX_PATH - kCreateDirLimit, deep_file.value().length());
489
490 FilePath sub_long = deep_file.DirName();
491 ASSERT_TRUE(file_util::CreateDirectory(sub_long));
492 CreateTextFile(deep_file, bogus_content);
493
[email protected]90314c0a82010-09-15 20:40:47494 FilePath base_b = temp_dir_.path().Append(FPL("base_b"));
[email protected]6f5f4322010-06-09 22:56:48495 ASSERT_TRUE(file_util::CreateDirectory(base_b));
496
497 FilePath to_sub_a = base_b.Append(FPL("to_sub_a"));
498 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a));
[email protected]b90d7e802011-01-09 16:32:20499 base::win::ScopedHandle reparse_to_sub_a(
[email protected]6f5f4322010-06-09 22:56:48500 ::CreateFile(to_sub_a.value().c_str(),
501 FILE_ALL_ACCESS,
502 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
503 NULL,
504 OPEN_EXISTING,
505 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
506 NULL));
[email protected]094f9762010-08-03 03:51:56507 ASSERT_TRUE(reparse_to_sub_a.IsValid());
[email protected]6f5f4322010-06-09 22:56:48508 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a, sub_a));
509
510 FilePath to_base_b = base_b.Append(FPL("to_base_b"));
511 ASSERT_TRUE(file_util::CreateDirectory(to_base_b));
[email protected]b90d7e802011-01-09 16:32:20512 base::win::ScopedHandle reparse_to_base_b(
[email protected]6f5f4322010-06-09 22:56:48513 ::CreateFile(to_base_b.value().c_str(),
514 FILE_ALL_ACCESS,
515 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
516 NULL,
517 OPEN_EXISTING,
518 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
519 NULL));
[email protected]094f9762010-08-03 03:51:56520 ASSERT_TRUE(reparse_to_base_b.IsValid());
[email protected]6f5f4322010-06-09 22:56:48521 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b, base_b));
522
523 FilePath to_sub_long = base_b.Append(FPL("to_sub_long"));
524 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long));
[email protected]b90d7e802011-01-09 16:32:20525 base::win::ScopedHandle reparse_to_sub_long(
[email protected]6f5f4322010-06-09 22:56:48526 ::CreateFile(to_sub_long.value().c_str(),
527 FILE_ALL_ACCESS,
528 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
529 NULL,
530 OPEN_EXISTING,
531 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
532 NULL));
[email protected]094f9762010-08-03 03:51:56533 ASSERT_TRUE(reparse_to_sub_long.IsValid());
[email protected]6f5f4322010-06-09 22:56:48534 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long, sub_long));
535
536 // Normalize a junction free path: base_a\sub_a\file.txt .
537 FilePath normalized_path;
538 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
539 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
540
541 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
542 // the junction to_sub_a.
543 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
544 &normalized_path));
545 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
546
547 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
548 // normalized to exclude junctions to_base_b and to_sub_a .
549 ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
550 .Append(FPL("to_base_b"))
551 .Append(FPL("to_sub_a"))
552 .Append(FPL("file.txt")),
553 &normalized_path));
554 ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());
555
556 // A long enough path will cause NormalizeFilePath() to fail. Make a long
557 // path using to_base_b many times, and check that paths long enough to fail
558 // do not cause a crash.
559 FilePath long_path = base_b;
560 const int kLengthLimit = MAX_PATH + 200;
561 while (long_path.value().length() <= kLengthLimit) {
562 long_path = long_path.Append(FPL("to_base_b"));
563 }
564 long_path = long_path.Append(FPL("to_sub_a"))
565 .Append(FPL("file.txt"));
566
567 ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
568
569 // Normalizing the junction to deep.txt should fail, because the expanded
570 // path to deep.txt is longer than MAX_PATH.
571 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
572 &normalized_path));
573
574 // Delete the reparse points, and see that NormalizeFilePath() fails
575 // to traverse them.
576 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
577 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
578 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
579
580 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
581 &normalized_path));
582}
583
[email protected]23b59d22011-12-29 22:59:22584TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
585 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test"));
586 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
587 base::win::ScopedHandle dir(
588 ::CreateFile(empty_dir.value().c_str(),
589 FILE_ALL_ACCESS,
590 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
591 NULL,
592 OPEN_EXISTING,
593 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
594 NULL));
595 ASSERT_TRUE(dir.IsValid());
596 base::PlatformFileInfo info;
597 EXPECT_TRUE(base::GetPlatformFileInfo(dir.Get(), &info));
598 EXPECT_TRUE(info.is_directory);
599 EXPECT_FALSE(info.is_symbolic_link);
600 EXPECT_EQ(0, info.size);
601}
602
[email protected]6f5f4322010-06-09 22:56:48603#endif // defined(OS_WIN)
604
[email protected]2e733d102010-11-30 00:43:37605#if defined(OS_POSIX)
606
607TEST_F(FileUtilTest, CreateAndReadSymlinks) {
608 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
609 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
610 CreateTextFile(link_to, bogus_content);
611
612 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
613 << "Failed to create file symlink.";
614
615 // If we created the link properly, we should be able to read the
616 // contents through it.
617 std::wstring contents = ReadTextFile(link_from);
618 ASSERT_EQ(contents, bogus_content);
619
620 FilePath result;
621 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from, &result));
622 ASSERT_EQ(link_to.value(), result.value());
623
624 // Link to a directory.
625 link_from = temp_dir_.path().Append(FPL("from_dir"));
626 link_to = temp_dir_.path().Append(FPL("to_dir"));
627 file_util::CreateDirectory(link_to);
628
629 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
630 << "Failed to create directory symlink.";
631
632 // Test failures.
633 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to));
634 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result));
635 FilePath missing = temp_dir_.path().Append(FPL("missing"));
636 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result));
637}
638
639
[email protected]6f5f4322010-06-09 22:56:48640// The following test of NormalizeFilePath() require that we create a symlink.
[email protected]2e733d102010-11-30 00:43:37641// This can not be done on Windows before Vista. On Vista, creating a symlink
[email protected]6f5f4322010-06-09 22:56:48642// requires privilege "SeCreateSymbolicLinkPrivilege".
643// TODO(skerner): Investigate the possibility of giving base_unittests the
644// privileges required to create a symlink.
[email protected]6f5f4322010-06-09 22:56:48645TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
646 FilePath normalized_path;
[email protected]01e2a1f2010-05-12 15:13:57647
648 // Link one file to another.
[email protected]90314c0a82010-09-15 20:40:47649 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
650 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
[email protected]01e2a1f2010-05-12 15:13:57651 CreateTextFile(link_to, bogus_content);
652
[email protected]2e733d102010-11-30 00:43:37653 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57654 << "Failed to create file symlink.";
655
[email protected]6f5f4322010-06-09 22:56:48656 // Check that NormalizeFilePath sees the link.
657 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57658 ASSERT_TRUE(link_to != link_from);
[email protected]6f5f4322010-06-09 22:56:48659 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
660 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
[email protected]01e2a1f2010-05-12 15:13:57661
662 // Link to a directory.
[email protected]90314c0a82010-09-15 20:40:47663 link_from = temp_dir_.path().Append(FPL("from_dir"));
664 link_to = temp_dir_.path().Append(FPL("to_dir"));
[email protected]01e2a1f2010-05-12 15:13:57665 file_util::CreateDirectory(link_to);
666
[email protected]2e733d102010-11-30 00:43:37667 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57668 << "Failed to create directory symlink.";
669
[email protected]6f5f4322010-06-09 22:56:48670 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
671 << "Links to directories should return false.";
[email protected]01e2a1f2010-05-12 15:13:57672
[email protected]6f5f4322010-06-09 22:56:48673 // Test that a loop in the links causes NormalizeFilePath() to return false.
[email protected]90314c0a82010-09-15 20:40:47674 link_from = temp_dir_.path().Append(FPL("link_a"));
675 link_to = temp_dir_.path().Append(FPL("link_b"));
[email protected]2e733d102010-11-30 00:43:37676 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
[email protected]01e2a1f2010-05-12 15:13:57677 << "Failed to create loop symlink a.";
[email protected]2e733d102010-11-30 00:43:37678 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from, link_to))
[email protected]01e2a1f2010-05-12 15:13:57679 << "Failed to create loop symlink b.";
680
681 // Infinite loop!
[email protected]6f5f4322010-06-09 22:56:48682 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
[email protected]01e2a1f2010-05-12 15:13:57683}
684#endif // defined(OS_POSIX)
685
[email protected]83d86252010-05-07 18:58:45686TEST_F(FileUtilTest, DeleteNonExistent) {
[email protected]90314c0a82010-09-15 20:40:47687 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
[email protected]83d86252010-05-07 18:58:45688 ASSERT_FALSE(file_util::PathExists(non_existent));
[email protected]3a51cfdd2010-05-07 00:05:36689
[email protected]83d86252010-05-07 18:58:45690 EXPECT_TRUE(file_util::Delete(non_existent, false));
691 ASSERT_FALSE(file_util::PathExists(non_existent));
692 EXPECT_TRUE(file_util::Delete(non_existent, true));
693 ASSERT_FALSE(file_util::PathExists(non_existent));
694}
695
696TEST_F(FileUtilTest, DeleteFile) {
697 // Create a file
[email protected]90314c0a82010-09-15 20:40:47698 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
[email protected]83d86252010-05-07 18:58:45699 CreateTextFile(file_name, bogus_content);
initial.commitd7cae122008-07-26 21:49:38700 ASSERT_TRUE(file_util::PathExists(file_name));
701
[email protected]83d86252010-05-07 18:58:45702 // Make sure it's deleted
703 EXPECT_TRUE(file_util::Delete(file_name, false));
704 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36705
[email protected]83d86252010-05-07 18:58:45706 // Test recursive case, create a new file
[email protected]90314c0a82010-09-15 20:40:47707 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
[email protected]83d86252010-05-07 18:58:45708 CreateTextFile(file_name, bogus_content);
709 ASSERT_TRUE(file_util::PathExists(file_name));
710
711 // Make sure it's deleted
712 EXPECT_TRUE(file_util::Delete(file_name, true));
713 EXPECT_FALSE(file_util::PathExists(file_name));
714}
715
716#if defined(OS_WIN)
717// Tests that the Delete function works for wild cards, especially
718// with the recursion flag. Also coincidentally tests PathExists.
719// TODO(erikkay): see if anyone's actually using this feature of the API
720TEST_F(FileUtilTest, DeleteWildCard) {
721 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47722 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
[email protected]83d86252010-05-07 18:58:45723 CreateTextFile(file_name, bogus_content);
724 ASSERT_TRUE(file_util::PathExists(file_name));
725
[email protected]90314c0a82010-09-15 20:40:47726 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
[email protected]83d86252010-05-07 18:58:45727 file_util::CreateDirectory(subdir_path);
initial.commitd7cae122008-07-26 21:49:38728 ASSERT_TRUE(file_util::PathExists(subdir_path));
729
[email protected]83d86252010-05-07 18:58:45730 // Create the wildcard path
[email protected]90314c0a82010-09-15 20:40:47731 FilePath directory_contents = temp_dir_.path();
[email protected]83d86252010-05-07 18:58:45732 directory_contents = directory_contents.Append(FPL("*"));
733
initial.commitd7cae122008-07-26 21:49:38734 // Delete non-recursively and check that only the file is deleted
[email protected]83d86252010-05-07 18:58:45735 EXPECT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10736 EXPECT_FALSE(file_util::PathExists(file_name));
737 EXPECT_TRUE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40738
[email protected]3a51cfdd2010-05-07 00:05:36739 // Delete recursively and make sure all contents are deleted
[email protected]83d86252010-05-07 18:58:45740 EXPECT_TRUE(file_util::Delete(directory_contents, true));
[email protected]dde46b62010-05-06 21:56:40741 EXPECT_FALSE(file_util::PathExists(file_name));
[email protected]3a51cfdd2010-05-07 00:05:36742 EXPECT_FALSE(file_util::PathExists(subdir_path));
[email protected]dde46b62010-05-06 21:56:40743}
744
[email protected]83d86252010-05-07 18:58:45745// TODO(erikkay): see if anyone's actually using this feature of the API
746TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
747 // Create a file and a directory
[email protected]90314c0a82010-09-15 20:40:47748 FilePath subdir_path =
749 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
[email protected]83d86252010-05-07 18:58:45750 file_util::CreateDirectory(subdir_path);
751 ASSERT_TRUE(file_util::PathExists(subdir_path));
752
753 // Create the wildcard path
754 FilePath directory_contents = subdir_path;
755 directory_contents = directory_contents.Append(FPL("*"));
756
757 // Delete non-recursively and check nothing got deleted
758 EXPECT_TRUE(file_util::Delete(directory_contents, false));
759 EXPECT_TRUE(file_util::PathExists(subdir_path));
760
761 // Delete recursively and check nothing got deleted
762 EXPECT_TRUE(file_util::Delete(directory_contents, true));
763 EXPECT_TRUE(file_util::PathExists(subdir_path));
764}
765#endif
766
767// Tests non-recursive Delete() for a directory.
768TEST_F(FileUtilTest, DeleteDirNonRecursive) {
769 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47770 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
[email protected]83d86252010-05-07 18:58:45771 file_util::CreateDirectory(test_subdir);
772 ASSERT_TRUE(file_util::PathExists(test_subdir));
773
774 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
775 CreateTextFile(file_name, bogus_content);
776 ASSERT_TRUE(file_util::PathExists(file_name));
777
778 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
779 file_util::CreateDirectory(subdir_path1);
780 ASSERT_TRUE(file_util::PathExists(subdir_path1));
781
782 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
783 file_util::CreateDirectory(subdir_path2);
784 ASSERT_TRUE(file_util::PathExists(subdir_path2));
785
786 // Delete non-recursively and check that the empty dir got deleted
787 EXPECT_TRUE(file_util::Delete(subdir_path2, false));
788 EXPECT_FALSE(file_util::PathExists(subdir_path2));
789
790 // Delete non-recursively and check that nothing got deleted
791 EXPECT_FALSE(file_util::Delete(test_subdir, false));
792 EXPECT_TRUE(file_util::PathExists(test_subdir));
793 EXPECT_TRUE(file_util::PathExists(file_name));
794 EXPECT_TRUE(file_util::PathExists(subdir_path1));
795}
796
797// Tests recursive Delete() for a directory.
798TEST_F(FileUtilTest, DeleteDirRecursive) {
799 // Create a subdirectory and put a file and two directories inside.
[email protected]90314c0a82010-09-15 20:40:47800 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
[email protected]83d86252010-05-07 18:58:45801 file_util::CreateDirectory(test_subdir);
802 ASSERT_TRUE(file_util::PathExists(test_subdir));
803
804 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
805 CreateTextFile(file_name, bogus_content);
806 ASSERT_TRUE(file_util::PathExists(file_name));
807
808 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
809 file_util::CreateDirectory(subdir_path1);
810 ASSERT_TRUE(file_util::PathExists(subdir_path1));
811
812 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
813 file_util::CreateDirectory(subdir_path2);
814 ASSERT_TRUE(file_util::PathExists(subdir_path2));
815
816 // Delete recursively and check that the empty dir got deleted
817 EXPECT_TRUE(file_util::Delete(subdir_path2, true));
818 EXPECT_FALSE(file_util::PathExists(subdir_path2));
819
820 // Delete recursively and check that everything got deleted
821 EXPECT_TRUE(file_util::Delete(test_subdir, true));
822 EXPECT_FALSE(file_util::PathExists(file_name));
823 EXPECT_FALSE(file_util::PathExists(subdir_path1));
824 EXPECT_FALSE(file_util::PathExists(test_subdir));
825}
826
[email protected]bc6a9012009-10-15 01:11:44827TEST_F(FileUtilTest, MoveFileNew) {
828 // Create a file
829 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47830 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44831 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
832 ASSERT_TRUE(file_util::PathExists(file_name_from));
833
[email protected]90314c0a82010-09-15 20:40:47834 // The destination.
835 FilePath file_name_to = temp_dir_.path().Append(
836 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44837 ASSERT_FALSE(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}
845
846TEST_F(FileUtilTest, MoveFileExists) {
847 // Create a file
848 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47849 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44850 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
851 ASSERT_TRUE(file_util::PathExists(file_name_from));
852
[email protected]90314c0a82010-09-15 20:40:47853 // The destination name.
854 FilePath file_name_to = temp_dir_.path().Append(
855 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:44856 CreateTextFile(file_name_to, L"Old file content");
857 ASSERT_TRUE(file_util::PathExists(file_name_to));
858
859 EXPECT_TRUE(file_util::Move(file_name_from, file_name_to));
860
861 // Check everything has been moved.
862 EXPECT_FALSE(file_util::PathExists(file_name_from));
863 EXPECT_TRUE(file_util::PathExists(file_name_to));
864 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
865}
866
867TEST_F(FileUtilTest, MoveFileDirExists) {
868 // Create a file
869 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:47870 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:44871 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
872 ASSERT_TRUE(file_util::PathExists(file_name_from));
873
874 // The destination directory
875 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47876 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:44877 file_util::CreateDirectory(dir_name_to);
878 ASSERT_TRUE(file_util::PathExists(dir_name_to));
879
880 EXPECT_FALSE(file_util::Move(file_name_from, dir_name_to));
881}
882
883
[email protected]abbc5732009-10-13 17:57:27884TEST_F(FileUtilTest, MoveNew) {
initial.commitd7cae122008-07-26 21:49:38885 // Create a directory
[email protected]640517f2008-10-30 23:54:04886 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47887 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04888 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38889 ASSERT_TRUE(file_util::PathExists(dir_name_from));
890
891 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:04892 FilePath file_name_from =
893 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38894 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
895 ASSERT_TRUE(file_util::PathExists(file_name_from));
896
[email protected]90314c0a82010-09-15 20:40:47897 // Move the directory.
898 FilePath dir_name_to =
899 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04900 FilePath file_name_to =
901 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38902
903 ASSERT_FALSE(file_util::PathExists(dir_name_to));
904
905 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
906
907 // Check everything has been moved.
908 EXPECT_FALSE(file_util::PathExists(dir_name_from));
909 EXPECT_FALSE(file_util::PathExists(file_name_from));
910 EXPECT_TRUE(file_util::PathExists(dir_name_to));
911 EXPECT_TRUE(file_util::PathExists(file_name_to));
912}
913
[email protected]abbc5732009-10-13 17:57:27914TEST_F(FileUtilTest, MoveExist) {
915 // Create a directory
916 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47917 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:27918 file_util::CreateDirectory(dir_name_from);
919 ASSERT_TRUE(file_util::PathExists(dir_name_from));
920
921 // Create a file under the directory
922 FilePath file_name_from =
923 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
924 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
925 ASSERT_TRUE(file_util::PathExists(file_name_from));
926
927 // Move the directory
928 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:47929 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:27930
931 FilePath dir_name_to =
932 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
933 FilePath file_name_to =
934 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
935
936 // Create the destination directory.
937 file_util::CreateDirectory(dir_name_exists);
938 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
939
940 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
941
942 // Check everything has been moved.
943 EXPECT_FALSE(file_util::PathExists(dir_name_from));
944 EXPECT_FALSE(file_util::PathExists(file_name_from));
945 EXPECT_TRUE(file_util::PathExists(dir_name_to));
946 EXPECT_TRUE(file_util::PathExists(file_name_to));
947}
948
949TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
initial.commitd7cae122008-07-26 21:49:38950 // Create a directory.
[email protected]640517f2008-10-30 23:54:04951 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:47952 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:04953 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:38954 ASSERT_TRUE(file_util::PathExists(dir_name_from));
955
956 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:04957 FilePath file_name_from =
958 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38959 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
960 ASSERT_TRUE(file_util::PathExists(file_name_from));
961
962 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:04963 FilePath subdir_name_from =
964 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
965 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:38966 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
967
968 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:04969 FilePath file_name2_from =
970 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38971 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
972 ASSERT_TRUE(file_util::PathExists(file_name2_from));
973
974 // Copy the directory recursively.
[email protected]640517f2008-10-30 23:54:04975 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:47976 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:04977 FilePath file_name_to =
978 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
979 FilePath subdir_name_to =
980 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
981 FilePath file_name2_to =
982 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:38983
984 ASSERT_FALSE(file_util::PathExists(dir_name_to));
985
986 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
987
988 // Check everything has been copied.
989 EXPECT_TRUE(file_util::PathExists(dir_name_from));
990 EXPECT_TRUE(file_util::PathExists(file_name_from));
991 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
992 EXPECT_TRUE(file_util::PathExists(file_name2_from));
993 EXPECT_TRUE(file_util::PathExists(dir_name_to));
994 EXPECT_TRUE(file_util::PathExists(file_name_to));
995 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
996 EXPECT_TRUE(file_util::PathExists(file_name2_to));
997}
998
[email protected]abbc5732009-10-13 17:57:27999TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1000 // Create a directory.
1001 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471002 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:271003 file_util::CreateDirectory(dir_name_from);
1004 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1005
1006 // Create a file under the directory.
1007 FilePath file_name_from =
1008 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1009 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1010 ASSERT_TRUE(file_util::PathExists(file_name_from));
1011
1012 // Create a subdirectory.
1013 FilePath subdir_name_from =
1014 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1015 file_util::CreateDirectory(subdir_name_from);
1016 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1017
1018 // Create a file under the subdirectory.
1019 FilePath file_name2_from =
1020 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1021 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1022 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1023
1024 // Copy the directory recursively.
1025 FilePath dir_name_exists =
[email protected]90314c0a82010-09-15 20:40:471026 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]abbc5732009-10-13 17:57:271027
1028 FilePath dir_name_to =
1029 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1030 FilePath file_name_to =
1031 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1032 FilePath subdir_name_to =
1033 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1034 FilePath file_name2_to =
1035 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1036
1037 // Create the destination directory.
1038 file_util::CreateDirectory(dir_name_exists);
1039 ASSERT_TRUE(file_util::PathExists(dir_name_exists));
1040
1041 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_exists, true));
1042
1043 // Check everything has been copied.
1044 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1045 EXPECT_TRUE(file_util::PathExists(file_name_from));
1046 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1047 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1048 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1049 EXPECT_TRUE(file_util::PathExists(file_name_to));
1050 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
1051 EXPECT_TRUE(file_util::PathExists(file_name2_to));
1052}
1053
1054TEST_F(FileUtilTest, CopyDirectoryNew) {
initial.commitd7cae122008-07-26 21:49:381055 // Create a directory.
[email protected]640517f2008-10-30 23:54:041056 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471057 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041058 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381059 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1060
1061 // Create a file under the directory.
[email protected]640517f2008-10-30 23:54:041062 FilePath file_name_from =
1063 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381064 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1065 ASSERT_TRUE(file_util::PathExists(file_name_from));
1066
1067 // Create a subdirectory.
[email protected]640517f2008-10-30 23:54:041068 FilePath subdir_name_from =
1069 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1070 file_util::CreateDirectory(subdir_name_from);
initial.commitd7cae122008-07-26 21:49:381071 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1072
1073 // Create a file under the subdirectory.
[email protected]640517f2008-10-30 23:54:041074 FilePath file_name2_from =
1075 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381076 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1077 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1078
1079 // Copy the directory not recursively.
[email protected]640517f2008-10-30 23:54:041080 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471081 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]640517f2008-10-30 23:54:041082 FilePath file_name_to =
1083 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1084 FilePath subdir_name_to =
1085 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
initial.commitd7cae122008-07-26 21:49:381086
1087 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1088
1089 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1090
1091 // Check everything has been copied.
1092 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1093 EXPECT_TRUE(file_util::PathExists(file_name_from));
1094 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1095 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1096 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1097 EXPECT_TRUE(file_util::PathExists(file_name_to));
1098 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1099}
1100
[email protected]abbc5732009-10-13 17:57:271101TEST_F(FileUtilTest, CopyDirectoryExists) {
1102 // Create a directory.
1103 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471104 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]abbc5732009-10-13 17:57:271105 file_util::CreateDirectory(dir_name_from);
1106 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1107
1108 // Create a file under the directory.
1109 FilePath file_name_from =
1110 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1111 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1112 ASSERT_TRUE(file_util::PathExists(file_name_from));
1113
1114 // Create a subdirectory.
1115 FilePath subdir_name_from =
1116 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1117 file_util::CreateDirectory(subdir_name_from);
1118 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
1119
1120 // Create a file under the subdirectory.
1121 FilePath file_name2_from =
1122 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1123 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1124 ASSERT_TRUE(file_util::PathExists(file_name2_from));
1125
1126 // Copy the directory not recursively.
1127 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471128 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
[email protected]abbc5732009-10-13 17:57:271129 FilePath file_name_to =
1130 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1131 FilePath subdir_name_to =
1132 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1133
1134 // Create the destination directory.
1135 file_util::CreateDirectory(dir_name_to);
1136 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1137
1138 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
1139
1140 // Check everything has been copied.
1141 EXPECT_TRUE(file_util::PathExists(dir_name_from));
1142 EXPECT_TRUE(file_util::PathExists(file_name_from));
1143 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
1144 EXPECT_TRUE(file_util::PathExists(file_name2_from));
1145 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1146 EXPECT_TRUE(file_util::PathExists(file_name_to));
1147 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
1148}
1149
[email protected]bc6a9012009-10-15 01:11:441150TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1151 // Create a file
1152 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471153 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441154 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1155 ASSERT_TRUE(file_util::PathExists(file_name_from));
1156
1157 // The destination name
[email protected]90314c0a82010-09-15 20:40:471158 FilePath file_name_to = temp_dir_.path().Append(
1159 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441160 ASSERT_FALSE(file_util::PathExists(file_name_to));
1161
1162 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1163
1164 // Check the has been copied
1165 EXPECT_TRUE(file_util::PathExists(file_name_to));
1166}
1167
1168TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
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 name
[email protected]90314c0a82010-09-15 20:40:471176 FilePath file_name_to = temp_dir_.path().Append(
1177 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
[email protected]bc6a9012009-10-15 01:11:441178 CreateTextFile(file_name_to, L"Old file content");
1179 ASSERT_TRUE(file_util::PathExists(file_name_to));
1180
1181 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, file_name_to, true));
1182
1183 // Check the has been copied
1184 EXPECT_TRUE(file_util::PathExists(file_name_to));
1185 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1186}
1187
1188TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1189 // Create a file
1190 FilePath file_name_from =
[email protected]90314c0a82010-09-15 20:40:471191 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
[email protected]bc6a9012009-10-15 01:11:441192 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1193 ASSERT_TRUE(file_util::PathExists(file_name_from));
1194
1195 // The destination
1196 FilePath dir_name_to =
[email protected]90314c0a82010-09-15 20:40:471197 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
[email protected]bc6a9012009-10-15 01:11:441198 file_util::CreateDirectory(dir_name_to);
1199 ASSERT_TRUE(file_util::PathExists(dir_name_to));
1200 FilePath file_name_to =
1201 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1202
1203 EXPECT_TRUE(file_util::CopyDirectory(file_name_from, dir_name_to, true));
1204
1205 // Check the has been copied
1206 EXPECT_TRUE(file_util::PathExists(file_name_to));
1207}
1208
initial.commitd7cae122008-07-26 21:49:381209TEST_F(FileUtilTest, CopyFile) {
1210 // Create a directory
[email protected]640517f2008-10-30 23:54:041211 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471212 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
[email protected]640517f2008-10-30 23:54:041213 file_util::CreateDirectory(dir_name_from);
initial.commitd7cae122008-07-26 21:49:381214 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1215
1216 // Create a file under the directory
[email protected]640517f2008-10-30 23:54:041217 FilePath file_name_from =
1218 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
initial.commitd7cae122008-07-26 21:49:381219 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1220 CreateTextFile(file_name_from, file_contents);
1221 ASSERT_TRUE(file_util::PathExists(file_name_from));
1222
1223 // Copy the file.
[email protected]640517f2008-10-30 23:54:041224 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
initial.commitd7cae122008-07-26 21:49:381225 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]806b9c62008-09-11 16:09:111226
[email protected]37088fef2008-08-15 17:32:101227 // Copy the file to another location using '..' in the path.
[email protected]53c58042009-08-26 20:00:141228 FilePath dest_file2(dir_name_from);
1229 dest_file2 = dest_file2.AppendASCII("..");
1230 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1231 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
1232
1233 FilePath dest_file2_test(dir_name_from);
1234 dest_file2_test = dest_file2_test.DirName();
1235 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
initial.commitd7cae122008-07-26 21:49:381236
1237 // Check everything has been copied.
1238 EXPECT_TRUE(file_util::PathExists(file_name_from));
1239 EXPECT_TRUE(file_util::PathExists(dest_file));
1240 const std::wstring read_contents = ReadTextFile(dest_file);
1241 EXPECT_EQ(file_contents, read_contents);
[email protected]53c58042009-08-26 20:00:141242 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
1243 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:381244}
1245
[email protected]37088fef2008-08-15 17:32:101246// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:131247#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381248TEST_F(FileUtilTest, GetFileCreationLocalTime) {
[email protected]90314c0a82010-09-15 20:40:471249 FilePath file_name = temp_dir_.path().Append(L"Test File.txt");
initial.commitd7cae122008-07-26 21:49:381250
1251 SYSTEMTIME start_time;
1252 GetLocalTime(&start_time);
1253 Sleep(100);
1254 CreateTextFile(file_name, L"New file!");
1255 Sleep(100);
1256 SYSTEMTIME end_time;
1257 GetLocalTime(&end_time);
1258
1259 SYSTEMTIME file_creation_time;
[email protected]640517f2008-10-30 23:54:041260 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
initial.commitd7cae122008-07-26 21:49:381261
1262 FILETIME start_filetime;
1263 SystemTimeToFileTime(&start_time, &start_filetime);
1264 FILETIME end_filetime;
1265 SystemTimeToFileTime(&end_time, &end_filetime);
1266 FILETIME file_creation_filetime;
1267 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
1268
1269 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
1270 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
1271 "creation time: " << FileTimeAsUint64(file_creation_filetime);
1272
1273 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
1274 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
1275 "end time: " << FileTimeAsUint64(end_filetime);
1276
[email protected]640517f2008-10-30 23:54:041277 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
initial.commitd7cae122008-07-26 21:49:381278}
[email protected]37088fef2008-08-15 17:32:101279#endif
initial.commitd7cae122008-07-26 21:49:381280
[email protected]ed2f2332008-08-20 15:59:491281// file_util winds up using autoreleased objects on the Mac, so this needs
[email protected]640517f2008-10-30 23:54:041282// to be a PlatformTest.
[email protected]ed2f2332008-08-20 15:59:491283typedef PlatformTest ReadOnlyFileUtilTest;
initial.commitd7cae122008-07-26 21:49:381284
[email protected]ed2f2332008-08-20 15:59:491285TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
[email protected]640517f2008-10-30 23:54:041286 FilePath data_dir;
initial.commitd7cae122008-07-26 21:49:381287 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
[email protected]640517f2008-10-30 23:54:041288 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1289 .Append(FILE_PATH_LITERAL("data"))
1290 .Append(FILE_PATH_LITERAL("file_util_unittest"));
initial.commitd7cae122008-07-26 21:49:381291 ASSERT_TRUE(file_util::PathExists(data_dir));
1292
[email protected]640517f2008-10-30 23:54:041293 FilePath original_file =
1294 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1295 FilePath same_file =
1296 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1297 FilePath same_length_file =
1298 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1299 FilePath different_file =
1300 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1301 FilePath different_first_file =
1302 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1303 FilePath different_last_file =
1304 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1305 FilePath empty1_file =
1306 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1307 FilePath empty2_file =
1308 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1309 FilePath shortened_file =
1310 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1311 FilePath binary_file =
1312 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
1313 FilePath binary_file_same =
1314 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1315 FilePath binary_file_diff =
1316 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
initial.commitd7cae122008-07-26 21:49:381317
1318 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
1319 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
1320 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
1321 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
[email protected]3f4b5712009-12-01 22:14:221322 EXPECT_FALSE(file_util::ContentsEqual(
1323 FilePath(FILE_PATH_LITERAL("bogusname")),
1324 FilePath(FILE_PATH_LITERAL("bogusname"))));
initial.commitd7cae122008-07-26 21:49:381325 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
1326 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
1327 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
1328 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
1329 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
1330 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
1331 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
1332}
1333
[email protected]b81637c32009-06-26 21:17:241334TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1335 FilePath data_dir;
1336 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
1337 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
1338 .Append(FILE_PATH_LITERAL("data"))
1339 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1340 ASSERT_TRUE(file_util::PathExists(data_dir));
1341
1342 FilePath original_file =
1343 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1344 FilePath same_file =
1345 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1346 FilePath crlf_file =
1347 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1348 FilePath shortened_file =
1349 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1350 FilePath different_file =
1351 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1352 FilePath different_first_file =
1353 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
1354 FilePath different_last_file =
1355 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
1356 FilePath first1_file =
1357 data_dir.Append(FILE_PATH_LITERAL("first1.txt"));
1358 FilePath first2_file =
1359 data_dir.Append(FILE_PATH_LITERAL("first2.txt"));
1360 FilePath empty1_file =
1361 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
1362 FilePath empty2_file =
1363 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
1364 FilePath blank_line_file =
1365 data_dir.Append(FILE_PATH_LITERAL("blank_line.txt"));
1366 FilePath blank_line_crlf_file =
1367 data_dir.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1368
1369 EXPECT_TRUE(file_util::TextContentsEqual(original_file, same_file));
1370 EXPECT_TRUE(file_util::TextContentsEqual(original_file, crlf_file));
1371 EXPECT_FALSE(file_util::TextContentsEqual(original_file, shortened_file));
1372 EXPECT_FALSE(file_util::TextContentsEqual(original_file, different_file));
1373 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1374 different_first_file));
1375 EXPECT_FALSE(file_util::TextContentsEqual(original_file,
1376 different_last_file));
1377 EXPECT_FALSE(file_util::TextContentsEqual(first1_file, first2_file));
1378 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file, empty2_file));
1379 EXPECT_FALSE(file_util::TextContentsEqual(original_file, empty1_file));
1380 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file,
1381 blank_line_crlf_file));
1382}
1383
[email protected]37088fef2008-08-15 17:32:101384// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:131385#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:381386TEST_F(FileUtilTest, ResolveShortcutTest) {
[email protected]90314c0a82010-09-15 20:40:471387 FilePath target_file = temp_dir_.path().Append(L"Target.txt");
initial.commitd7cae122008-07-26 21:49:381388 CreateTextFile(target_file, L"This is the target.");
1389
[email protected]90314c0a82010-09-15 20:40:471390 FilePath link_file = temp_dir_.path().Append(L"Link.lnk");
initial.commitd7cae122008-07-26 21:49:381391
1392 HRESULT result;
1393 IShellLink *shell = NULL;
1394 IPersistFile *persist = NULL;
1395
1396 CoInitialize(NULL);
1397 // Temporarily create a shortcut for test
1398 result = CoCreateInstance(CLSID_ShellLink, NULL,
1399 CLSCTX_INPROC_SERVER, IID_IShellLink,
1400 reinterpret_cast<LPVOID*>(&shell));
1401 EXPECT_TRUE(SUCCEEDED(result));
1402 result = shell->QueryInterface(IID_IPersistFile,
1403 reinterpret_cast<LPVOID*>(&persist));
1404 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041405 result = shell->SetPath(target_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381406 EXPECT_TRUE(SUCCEEDED(result));
1407 result = shell->SetDescription(L"ResolveShortcutTest");
1408 EXPECT_TRUE(SUCCEEDED(result));
[email protected]640517f2008-10-30 23:54:041409 result = persist->Save(link_file.value().c_str(), TRUE);
initial.commitd7cae122008-07-26 21:49:381410 EXPECT_TRUE(SUCCEEDED(result));
1411 if (persist)
1412 persist->Release();
1413 if (shell)
1414 shell->Release();
1415
1416 bool is_solved;
[email protected]fd061a62009-08-25 01:51:441417 is_solved = file_util::ResolveShortcut(&link_file);
initial.commitd7cae122008-07-26 21:49:381418 EXPECT_TRUE(is_solved);
1419 std::wstring contents;
[email protected]fd061a62009-08-25 01:51:441420 contents = ReadTextFile(link_file);
initial.commitd7cae122008-07-26 21:49:381421 EXPECT_EQ(L"This is the target.", contents);
1422
[email protected]d324ab332008-08-18 16:00:381423 // Cleaning
[email protected]640517f2008-10-30 23:54:041424 DeleteFile(target_file.value().c_str());
[email protected]fd061a62009-08-25 01:51:441425 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381426 CoUninitialize();
1427}
1428
1429TEST_F(FileUtilTest, CreateShortcutTest) {
1430 const wchar_t file_contents[] = L"This is another target.";
[email protected]90314c0a82010-09-15 20:40:471431 FilePath target_file = temp_dir_.path().Append(L"Target1.txt");
initial.commitd7cae122008-07-26 21:49:381432 CreateTextFile(target_file, file_contents);
1433
[email protected]90314c0a82010-09-15 20:40:471434 FilePath link_file = temp_dir_.path().Append(L"Link1.lnk");
initial.commitd7cae122008-07-26 21:49:381435
1436 CoInitialize(NULL);
[email protected]640517f2008-10-30 23:54:041437 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
1438 link_file.value().c_str(),
[email protected]86b54012009-11-19 09:18:501439 NULL, NULL, NULL, NULL, 0, NULL));
[email protected]fd061a62009-08-25 01:51:441440 FilePath resolved_name = link_file;
initial.commitd7cae122008-07-26 21:49:381441 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
[email protected]fd061a62009-08-25 01:51:441442 std::wstring read_contents = ReadTextFile(resolved_name);
initial.commitd7cae122008-07-26 21:49:381443 EXPECT_EQ(file_contents, read_contents);
1444
[email protected]640517f2008-10-30 23:54:041445 DeleteFile(target_file.value().c_str());
1446 DeleteFile(link_file.value().c_str());
initial.commitd7cae122008-07-26 21:49:381447 CoUninitialize();
1448}
[email protected]2c59af7dca2009-03-11 18:37:481449
1450TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1451 // Create a directory
1452 FilePath dir_name_from =
[email protected]90314c0a82010-09-15 20:40:471453 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
[email protected]2c59af7dca2009-03-11 18:37:481454 file_util::CreateDirectory(dir_name_from);
1455 ASSERT_TRUE(file_util::PathExists(dir_name_from));
1456
1457 // Create a file under the directory
1458 FilePath file_name_from =
1459 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1460 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1461 ASSERT_TRUE(file_util::PathExists(file_name_from));
1462
1463 // Move the directory by using CopyAndDeleteDirectory
[email protected]90314c0a82010-09-15 20:40:471464 FilePath dir_name_to = temp_dir_.path().Append(
[email protected]2c59af7dca2009-03-11 18:37:481465 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1466 FilePath file_name_to =
1467 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1468
1469 ASSERT_FALSE(file_util::PathExists(dir_name_to));
1470
1471 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from, dir_name_to));
1472
1473 // Check everything has been moved.
1474 EXPECT_FALSE(file_util::PathExists(dir_name_from));
1475 EXPECT_FALSE(file_util::PathExists(file_name_from));
1476 EXPECT_TRUE(file_util::PathExists(dir_name_to));
1477 EXPECT_TRUE(file_util::PathExists(file_name_to));
1478}
[email protected]7d95aae2009-10-09 07:33:391479
1480TEST_F(FileUtilTest, GetTempDirTest) {
1481 static const TCHAR* kTmpKey = _T("TMP");
1482 static const TCHAR* kTmpValues[] = {
1483 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1484 };
1485 // Save the original $TMP.
1486 size_t original_tmp_size;
1487 TCHAR* original_tmp;
1488 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp, &original_tmp_size, kTmpKey));
1489 // original_tmp may be NULL.
1490
1491 for (unsigned int i = 0; i < arraysize(kTmpValues); ++i) {
1492 FilePath path;
1493 ::_tputenv_s(kTmpKey, kTmpValues[i]);
1494 file_util::GetTempDir(&path);
1495 EXPECT_TRUE(path.IsAbsolute()) << "$TMP=" << kTmpValues[i] <<
1496 " result=" << path.value();
1497 }
1498
1499 // Restore the original $TMP.
1500 if (original_tmp) {
1501 ::_tputenv_s(kTmpKey, original_tmp);
1502 free(original_tmp);
1503 } else {
1504 ::_tputenv_s(kTmpKey, _T(""));
1505 }
1506}
1507#endif // OS_WIN
initial.commitd7cae122008-07-26 21:49:381508
[email protected]33edeab2009-08-18 16:07:551509TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1510 FilePath temp_files[3];
[email protected]9e51af92009-02-04 00:58:391511 for (int i = 0; i < 3; i++) {
[email protected]33edeab2009-08-18 16:07:551512 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
[email protected]9e51af92009-02-04 00:58:391513 EXPECT_TRUE(file_util::PathExists(temp_files[i]));
1514 EXPECT_FALSE(file_util::DirectoryExists(temp_files[i]));
1515 }
1516 for (int i = 0; i < 3; i++)
1517 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1518 for (int i = 0; i < 3; i++)
1519 EXPECT_TRUE(file_util::Delete(temp_files[i], false));
1520}
1521
[email protected]33edeab2009-08-18 16:07:551522TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
[email protected]9e51af92009-02-04 00:58:391523 FilePath names[3];
1524 FILE *fps[3];
1525 int i;
1526
1527 // Create; make sure they are open and exist.
1528 for (i = 0; i < 3; ++i) {
1529 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1530 ASSERT_TRUE(fps[i]);
1531 EXPECT_TRUE(file_util::PathExists(names[i]));
1532 }
1533
1534 // Make sure all names are unique.
1535 for (i = 0; i < 3; ++i) {
1536 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1537 }
1538
1539 // Close and delete.
1540 for (i = 0; i < 3; ++i) {
1541 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1542 EXPECT_TRUE(file_util::Delete(names[i], false));
1543 }
initial.commitd7cae122008-07-26 21:49:381544}
1545
1546TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
[email protected]53c58042009-08-26 20:00:141547 FilePath temp_dir;
1548 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1549 &temp_dir));
[email protected]806b9c62008-09-11 16:09:111550 EXPECT_TRUE(file_util::PathExists(temp_dir));
1551 EXPECT_TRUE(file_util::Delete(temp_dir, false));
initial.commitd7cae122008-07-26 21:49:381552}
1553
[email protected]b0b3abd92010-04-30 17:00:091554TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1555 FilePath new_dir;
1556 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
[email protected]90314c0a82010-09-15 20:40:471557 temp_dir_.path(),
[email protected]b0b3abd92010-04-30 17:00:091558 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
[email protected]046062e82010-06-30 07:19:111559 &new_dir));
[email protected]b0b3abd92010-04-30 17:00:091560 EXPECT_TRUE(file_util::PathExists(new_dir));
[email protected]90314c0a82010-09-15 20:40:471561 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
[email protected]b0b3abd92010-04-30 17:00:091562 EXPECT_TRUE(file_util::Delete(new_dir, false));
1563}
1564
[email protected]9e51af92009-02-04 00:58:391565TEST_F(FileUtilTest, GetShmemTempDirTest) {
1566 FilePath dir;
[email protected]103dccfa2011-12-06 18:07:051567 EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false));
[email protected]9e51af92009-02-04 00:58:391568 EXPECT_TRUE(file_util::DirectoryExists(dir));
1569}
1570
initial.commitd7cae122008-07-26 21:49:381571TEST_F(FileUtilTest, CreateDirectoryTest) {
[email protected]640517f2008-10-30 23:54:041572 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471573 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
[email protected]8541bb82008-08-15 17:45:131574#if defined(OS_WIN)
[email protected]640517f2008-10-30 23:54:041575 FilePath test_path =
1576 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
[email protected]37088fef2008-08-15 17:32:101577#elif defined(OS_POSIX)
[email protected]640517f2008-10-30 23:54:041578 FilePath test_path =
1579 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
[email protected]37088fef2008-08-15 17:32:101580#endif
[email protected]806b9c62008-09-11 16:09:111581
1582 EXPECT_FALSE(file_util::PathExists(test_path));
1583 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1584 EXPECT_TRUE(file_util::PathExists(test_path));
1585 // CreateDirectory returns true if the DirectoryExists returns true.
1586 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1587
1588 // Doesn't work to create it on top of a non-dir
[email protected]640517f2008-10-30 23:54:041589 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111590 EXPECT_FALSE(file_util::PathExists(test_path));
1591 CreateTextFile(test_path, L"test file");
1592 EXPECT_TRUE(file_util::PathExists(test_path));
1593 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1594
1595 EXPECT_TRUE(file_util::Delete(test_root, true));
1596 EXPECT_FALSE(file_util::PathExists(test_root));
1597 EXPECT_FALSE(file_util::PathExists(test_path));
[email protected]40676ab2009-11-27 14:54:411598
1599 // Verify assumptions made by the Windows implementation:
1600 // 1. The current directory always exists.
1601 // 2. The root directory always exists.
1602 ASSERT_TRUE(file_util::DirectoryExists(
1603 FilePath(FilePath::kCurrentDirectory)));
1604 FilePath top_level = test_root;
1605 while (top_level != top_level.DirName()) {
1606 top_level = top_level.DirName();
1607 }
1608 ASSERT_TRUE(file_util::DirectoryExists(top_level));
1609
1610 // Given these assumptions hold, it should be safe to
1611 // test that "creating" these directories succeeds.
1612 EXPECT_TRUE(file_util::CreateDirectory(
1613 FilePath(FilePath::kCurrentDirectory)));
1614 EXPECT_TRUE(file_util::CreateDirectory(top_level));
[email protected]89b9ae092009-12-17 20:42:401615
1616#if defined(OS_WIN)
1617 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1618 FilePath invalid_path =
1619 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1620 if (!file_util::PathExists(invalid_drive)) {
1621 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1622 }
1623#endif
[email protected]806b9c62008-09-11 16:09:111624}
1625
1626TEST_F(FileUtilTest, DetectDirectoryTest) {
1627 // Check a directory
[email protected]640517f2008-10-30 23:54:041628 FilePath test_root =
[email protected]90314c0a82010-09-15 20:40:471629 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
[email protected]806b9c62008-09-11 16:09:111630 EXPECT_FALSE(file_util::PathExists(test_root));
1631 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1632 EXPECT_TRUE(file_util::PathExists(test_root));
1633 EXPECT_TRUE(file_util::DirectoryExists(test_root));
[email protected]806b9c62008-09-11 16:09:111634 // Check a file
[email protected]640517f2008-10-30 23:54:041635 FilePath test_path =
1636 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
[email protected]806b9c62008-09-11 16:09:111637 EXPECT_FALSE(file_util::PathExists(test_path));
1638 CreateTextFile(test_path, L"test file");
1639 EXPECT_TRUE(file_util::PathExists(test_path));
1640 EXPECT_FALSE(file_util::DirectoryExists(test_path));
1641 EXPECT_TRUE(file_util::Delete(test_path, false));
1642
1643 EXPECT_TRUE(file_util::Delete(test_root, true));
initial.commitd7cae122008-07-26 21:49:381644}
1645
initial.commitd7cae122008-07-26 21:49:381646TEST_F(FileUtilTest, FileEnumeratorTest) {
1647 // Test an empty directory.
[email protected]90314c0a82010-09-15 20:40:471648 file_util::FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121649 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
1650 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
initial.commitd7cae122008-07-26 21:49:381651
[email protected]8199b3a2009-06-09 05:57:381652 // Test an empty directory, non-recursively, including "..".
[email protected]90314c0a82010-09-15 20:40:471653 file_util::FileEnumerator f0_dotdot(temp_dir_.path(), false,
[email protected]58b7c5a62011-08-15 13:09:271654 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:381655 FILES_AND_DIRECTORIES | file_util::FileEnumerator::INCLUDE_DOT_DOT));
[email protected]90314c0a82010-09-15 20:40:471656 EXPECT_EQ(temp_dir_.path().Append(FILE_PATH_LITERAL("..")).value(),
[email protected]8199b3a2009-06-09 05:57:381657 f0_dotdot.Next().value());
1658 EXPECT_EQ(FILE_PATH_LITERAL(""),
1659 f0_dotdot.Next().value());
1660
[email protected]37088fef2008-08-15 17:32:101661 // create the directories
[email protected]90314c0a82010-09-15 20:40:471662 FilePath dir1 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir1"));
[email protected]37088fef2008-08-15 17:32:101663 EXPECT_TRUE(file_util::CreateDirectory(dir1));
[email protected]90314c0a82010-09-15 20:40:471664 FilePath dir2 = temp_dir_.path().Append(FILE_PATH_LITERAL("dir2"));
[email protected]37088fef2008-08-15 17:32:101665 EXPECT_TRUE(file_util::CreateDirectory(dir2));
[email protected]640517f2008-10-30 23:54:041666 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
[email protected]37088fef2008-08-15 17:32:101667 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
[email protected]640517f2008-10-30 23:54:041668
[email protected]37088fef2008-08-15 17:32:101669 // create the files
[email protected]640517f2008-10-30 23:54:041670 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
[email protected]37088fef2008-08-15 17:32:101671 CreateTextFile(dir2file, L"");
[email protected]640517f2008-10-30 23:54:041672 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
[email protected]37088fef2008-08-15 17:32:101673 CreateTextFile(dir2innerfile, L"");
[email protected]90314c0a82010-09-15 20:40:471674 FilePath file1 = temp_dir_.path().Append(FILE_PATH_LITERAL("file1.txt"));
[email protected]37088fef2008-08-15 17:32:101675 CreateTextFile(file1, L"");
[email protected]640517f2008-10-30 23:54:041676 FilePath file2_rel =
1677 dir2.Append(FilePath::kParentDirectory)
1678 .Append(FILE_PATH_LITERAL("file2.txt"));
[email protected]37088fef2008-08-15 17:32:101679 CreateTextFile(file2_rel, L"");
[email protected]90314c0a82010-09-15 20:40:471680 FilePath file2_abs = temp_dir_.path().Append(FILE_PATH_LITERAL("file2.txt"));
initial.commitd7cae122008-07-26 21:49:381681
1682 // Only enumerate files.
[email protected]90314c0a82010-09-15 20:40:471683 file_util::FileEnumerator f1(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381684 file_util::FileEnumerator::FILES);
1685 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:101686 EXPECT_TRUE(c1.HasFile(file1));
1687 EXPECT_TRUE(c1.HasFile(file2_abs));
1688 EXPECT_TRUE(c1.HasFile(dir2file));
1689 EXPECT_TRUE(c1.HasFile(dir2innerfile));
1690 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:381691
1692 // Only enumerate directories.
[email protected]90314c0a82010-09-15 20:40:471693 file_util::FileEnumerator f2(temp_dir_.path(), true,
initial.commitd7cae122008-07-26 21:49:381694 file_util::FileEnumerator::DIRECTORIES);
1695 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:101696 EXPECT_TRUE(c2.HasFile(dir1));
1697 EXPECT_TRUE(c2.HasFile(dir2));
1698 EXPECT_TRUE(c2.HasFile(dir2inner));
1699 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:381700
[email protected]3bc9ffaf2008-10-16 02:42:451701 // Only enumerate directories non-recursively.
1702 file_util::FileEnumerator f2_non_recursive(
[email protected]90314c0a82010-09-15 20:40:471703 temp_dir_.path(), false, file_util::FileEnumerator::DIRECTORIES);
[email protected]3bc9ffaf2008-10-16 02:42:451704 FindResultCollector c2_non_recursive(f2_non_recursive);
1705 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
1706 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
1707 EXPECT_EQ(c2_non_recursive.size(), 2);
1708
[email protected]8199b3a2009-06-09 05:57:381709 // Only enumerate directories, non-recursively, including "..".
1710 file_util::FileEnumerator f2_dotdot(
[email protected]90314c0a82010-09-15 20:40:471711 temp_dir_.path(), false,
[email protected]58b7c5a62011-08-15 13:09:271712 static_cast<file_util::FileEnumerator::FileType>(
[email protected]8199b3a2009-06-09 05:57:381713 file_util::FileEnumerator::DIRECTORIES |
1714 file_util::FileEnumerator::INCLUDE_DOT_DOT));
1715 FindResultCollector c2_dotdot(f2_dotdot);
1716 EXPECT_TRUE(c2_dotdot.HasFile(dir1));
1717 EXPECT_TRUE(c2_dotdot.HasFile(dir2));
[email protected]90314c0a82010-09-15 20:40:471718 EXPECT_TRUE(c2_dotdot.HasFile(
1719 temp_dir_.path().Append(FILE_PATH_LITERAL(".."))));
[email protected]8199b3a2009-06-09 05:57:381720 EXPECT_EQ(c2_dotdot.size(), 3);
1721
initial.commitd7cae122008-07-26 21:49:381722 // Enumerate files and directories.
[email protected]90314c0a82010-09-15 20:40:471723 file_util::FileEnumerator f3(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381724 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:101725 EXPECT_TRUE(c3.HasFile(dir1));
1726 EXPECT_TRUE(c3.HasFile(dir2));
1727 EXPECT_TRUE(c3.HasFile(file1));
1728 EXPECT_TRUE(c3.HasFile(file2_abs));
1729 EXPECT_TRUE(c3.HasFile(dir2file));
1730 EXPECT_TRUE(c3.HasFile(dir2inner));
1731 EXPECT_TRUE(c3.HasFile(dir2innerfile));
1732 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:381733
1734 // Non-recursive operation.
[email protected]90314c0a82010-09-15 20:40:471735 file_util::FileEnumerator f4(temp_dir_.path(), false, FILES_AND_DIRECTORIES);
initial.commitd7cae122008-07-26 21:49:381736 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:101737 EXPECT_TRUE(c4.HasFile(dir2));
1738 EXPECT_TRUE(c4.HasFile(dir2));
1739 EXPECT_TRUE(c4.HasFile(file1));
1740 EXPECT_TRUE(c4.HasFile(file2_abs));
1741 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:381742
1743 // Enumerate with a pattern.
[email protected]90314c0a82010-09-15 20:40:471744 file_util::FileEnumerator f5(temp_dir_.path(), true, FILES_AND_DIRECTORIES,
[email protected]0b733222008-12-11 14:55:121745 FILE_PATH_LITERAL("dir*"));
initial.commitd7cae122008-07-26 21:49:381746 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:101747 EXPECT_TRUE(c5.HasFile(dir1));
1748 EXPECT_TRUE(c5.HasFile(dir2));
1749 EXPECT_TRUE(c5.HasFile(dir2file));
1750 EXPECT_TRUE(c5.HasFile(dir2inner));
1751 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1752 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:381753
1754 // Make sure the destructor closes the find handle while in the middle of a
1755 // query to allow TearDown to delete the directory.
[email protected]90314c0a82010-09-15 20:40:471756 file_util::FileEnumerator f6(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
[email protected]0b733222008-12-11 14:55:121757 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something
1758 // (we don't care what).
initial.commitd7cae122008-07-26 21:49:381759}
license.botbf09a502008-08-24 00:55:551760
[email protected]ee5c29da2009-01-09 22:14:271761TEST_F(FileUtilTest, Contains) {
[email protected]90314c0a82010-09-15 20:40:471762 FilePath data_dir =
1763 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ee5c29da2009-01-09 22:14:271764
1765 // Create a fresh, empty copy of this directory.
[email protected]a92d2fd2009-01-31 01:19:571766 if (file_util::PathExists(data_dir)) {
1767 ASSERT_TRUE(file_util::Delete(data_dir, true));
1768 }
[email protected]ee5c29da2009-01-09 22:14:271769 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1770
1771 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1772 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1773 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1774 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1775
1776 // Annoyingly, the directories must actually exist in order for realpath(),
1777 // which Contains() relies on in posix, to work.
1778 ASSERT_TRUE(file_util::CreateDirectory(foo));
1779 std::string data("hello");
[email protected]4e9f6be2009-04-03 17:17:581780 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1781 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1782 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
[email protected]ee5c29da2009-01-09 22:14:271783
1784 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1785 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1786 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1787 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1788
[email protected]e43eddf12009-12-29 00:32:521789 // Platform-specific concerns.
[email protected]ee5c29da2009-01-09 22:14:271790 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1791#if defined(OS_WIN)
1792 EXPECT_TRUE(file_util::ContainsPath(foo,
1793 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]9e51af92009-02-04 00:58:391794 EXPECT_TRUE(file_util::ContainsPath(foo,
[email protected]ee5c29da2009-01-09 22:14:271795 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
[email protected]e43eddf12009-12-29 00:32:521796#elif defined(OS_MACOSX)
1797 // We can't really do this test on OS X since the case-sensitivity of the
1798 // filesystem is configurable.
1799#elif defined(OS_POSIX)
[email protected]ee5c29da2009-01-09 22:14:271800 EXPECT_FALSE(file_util::ContainsPath(foo,
1801 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
[email protected]ee5c29da2009-01-09 22:14:271802#endif
1803}
1804
[email protected]507fb9a2010-09-23 23:28:221805TEST_F(FileUtilTest, TouchFile) {
[email protected]90314c0a82010-09-15 20:40:471806 FilePath data_dir =
1807 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
[email protected]ec3d1452010-02-18 10:02:261808
1809 // Create a fresh, empty copy of this directory.
1810 if (file_util::PathExists(data_dir)) {
1811 ASSERT_TRUE(file_util::Delete(data_dir, true));
1812 }
1813 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1814
1815 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1816 std::string data("hello");
1817 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1818
[email protected]507fb9a2010-09-23 23:28:221819 base::Time access_time;
1820 // This timestamp is divisible by one day (in local timezone),
1821 // to make it work on FAT too.
[email protected]46470aa2011-08-03 05:28:101822 ASSERT_TRUE(base::Time::FromString("Wed, 16 Nov 1994, 00:00:00",
[email protected]507fb9a2010-09-23 23:28:221823 &access_time));
1824
[email protected]ec3d1452010-02-18 10:02:261825 base::Time modification_time;
1826 // Note that this timestamp is divisible by two (seconds) - FAT stores
1827 // modification times with 2s resolution.
[email protected]46470aa2011-08-03 05:28:101828 ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
[email protected]ec3d1452010-02-18 10:02:261829 &modification_time));
[email protected]507fb9a2010-09-23 23:28:221830
1831 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
[email protected]2f0193c22010-09-03 02:28:371832 base::PlatformFileInfo file_info;
[email protected]ec3d1452010-02-18 10:02:261833 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
[email protected]507fb9a2010-09-23 23:28:221834 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
1835 access_time.ToInternalValue());
1836 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
1837 modification_time.ToInternalValue());
[email protected]ec3d1452010-02-18 10:02:261838}
1839
[email protected]b33f1d92010-05-26 01:40:121840TEST_F(FileUtilTest, IsDirectoryEmpty) {
[email protected]90314c0a82010-09-15 20:40:471841 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
[email protected]b33f1d92010-05-26 01:40:121842
1843 ASSERT_FALSE(file_util::PathExists(empty_dir));
1844
1845 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1846
1847 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1848
1849 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1850 std::string bar("baz");
1851 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1852
1853 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1854}
1855
[email protected]73e4c362011-09-22 14:47:181856#if defined(OS_POSIX)
1857
1858// Testing VerifyPathControlledByAdmin() is hard, because there is no
1859// way a test can make a file owned by root, or change file paths
1860// at the root of the file system. VerifyPathControlledByAdmin()
1861// is implemented as a call to VerifyPathControlledByUser, which gives
1862// us the ability to test with paths under the test's temp directory,
1863// using a user id we control.
1864// Pull tests of VerifyPathControlledByUserTest() into a separate test class
1865// with a common SetUp() method.
1866class VerifyPathControlledByUserTest : public FileUtilTest {
1867 protected:
1868 virtual void SetUp() {
1869 FileUtilTest::SetUp();
1870
1871 // Create a basic structure used by each test.
1872 // base_dir_
1873 // |-> sub_dir_
1874 // |-> text_file_
1875
1876 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
1877 ASSERT_TRUE(file_util::CreateDirectory(base_dir_));
1878
1879 sub_dir_ = base_dir_.AppendASCII("sub_dir");
1880 ASSERT_TRUE(file_util::CreateDirectory(sub_dir_));
1881
1882 text_file_ = sub_dir_.AppendASCII("file.txt");
1883 CreateTextFile(text_file_, L"This text file has some text in it.");
1884
[email protected]30fcaa492011-09-26 17:18:431885 // Get the user and group files are created with from |base_dir_|.
1886 struct stat stat_buf;
1887 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
1888 uid_ = stat_buf.st_uid;
[email protected]7312f42a2011-10-17 21:30:291889 ok_gids_.insert(stat_buf.st_gid);
1890 bad_gids_.insert(stat_buf.st_gid + 1);
1891
[email protected]30fcaa492011-09-26 17:18:431892 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
[email protected]73e4c362011-09-22 14:47:181893
1894 // To ensure that umask settings do not cause the initial state
1895 // of permissions to be different from what we expect, explicitly
1896 // set permissions on the directories we create.
1897 // Make all files and directories non-world-writable.
1898 mode_t enabled_permissions =
1899 S_IRWXU | // User can read, write, traverse
1900 S_IRWXG; // Group can read, write, traverse
1901 mode_t disabled_permissions =
1902 S_IRWXO; // Other users can't read, write, traverse.
1903
1904 ASSERT_NO_FATAL_FAILURE(
1905 ChangePosixFilePermissions(
1906 base_dir_, enabled_permissions, disabled_permissions));
1907 ASSERT_NO_FATAL_FAILURE(
1908 ChangePosixFilePermissions(
1909 sub_dir_, enabled_permissions, disabled_permissions));
1910 }
1911
1912 FilePath base_dir_;
1913 FilePath sub_dir_;
1914 FilePath text_file_;
1915 uid_t uid_;
[email protected]7312f42a2011-10-17 21:30:291916
1917 std::set<gid_t> ok_gids_;
1918 std::set<gid_t> bad_gids_;
[email protected]73e4c362011-09-22 14:47:181919};
1920
[email protected]30fcaa492011-09-26 17:18:431921TEST_F(VerifyPathControlledByUserTest, BadPaths) {
[email protected]73e4c362011-09-22 14:47:181922 // File does not exist.
1923 FilePath does_not_exist = base_dir_.AppendASCII("does")
1924 .AppendASCII("not")
1925 .AppendASCII("exist");
[email protected]73e4c362011-09-22 14:47:181926 EXPECT_FALSE(
1927 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:291928 base_dir_, does_not_exist, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181929
1930 // |base| not a subpath of |path|.
1931 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:291932 file_util::VerifyPathControlledByUser(
1933 sub_dir_, base_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181934
1935 // An empty base path will fail to be a prefix for any path.
1936 FilePath empty;
1937 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:291938 file_util::VerifyPathControlledByUser(
1939 empty, base_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181940
1941 // Finding that a bad call fails proves nothing unless a good call succeeds.
1942 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:291943 file_util::VerifyPathControlledByUser(
1944 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181945}
1946
1947TEST_F(VerifyPathControlledByUserTest, Symlinks) {
1948 // Symlinks in the path should cause failure.
1949
1950 // Symlink to the file at the end of the path.
1951 FilePath file_link = base_dir_.AppendASCII("file_link");
1952 ASSERT_TRUE(file_util::CreateSymbolicLink(text_file_, file_link))
1953 << "Failed to create symlink.";
1954
1955 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:291956 file_util::VerifyPathControlledByUser(
1957 base_dir_, file_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181958 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:291959 file_util::VerifyPathControlledByUser(
1960 file_link, file_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181961
1962 // Symlink from one directory to another within the path.
1963 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
1964 ASSERT_TRUE(file_util::CreateSymbolicLink(sub_dir_, link_to_sub_dir))
1965 << "Failed to create symlink.";
1966
1967 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
1968 ASSERT_TRUE(file_util::PathExists(file_path_with_link));
1969
1970 EXPECT_FALSE(
1971 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:291972 base_dir_, file_path_with_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181973
1974 EXPECT_FALSE(
1975 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:291976 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181977
1978 // Symlinks in parents of base path are allowed.
1979 EXPECT_TRUE(
1980 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:291981 file_path_with_link, file_path_with_link, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:181982}
1983
[email protected]30fcaa492011-09-26 17:18:431984TEST_F(VerifyPathControlledByUserTest, OwnershipChecks) {
[email protected]73e4c362011-09-22 14:47:181985 // Get a uid that is not the uid of files we create.
1986 uid_t bad_uid = uid_ + 1;
1987
[email protected]73e4c362011-09-22 14:47:181988 // Make all files and directories non-world-writable.
1989 ASSERT_NO_FATAL_FAILURE(
1990 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
1991 ASSERT_NO_FATAL_FAILURE(
1992 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
1993 ASSERT_NO_FATAL_FAILURE(
1994 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
1995
1996 // We control these paths.
1997 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:291998 file_util::VerifyPathControlledByUser(
1999 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182000 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292001 file_util::VerifyPathControlledByUser(
2002 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182003 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292004 file_util::VerifyPathControlledByUser(
2005 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182006
2007 // Another user does not control these paths.
2008 EXPECT_FALSE(
2009 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292010 base_dir_, sub_dir_, bad_uid, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182011 EXPECT_FALSE(
2012 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292013 base_dir_, text_file_, bad_uid, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182014 EXPECT_FALSE(
2015 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292016 sub_dir_, text_file_, bad_uid, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182017
2018 // Another group does not control the paths.
2019 EXPECT_FALSE(
2020 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292021 base_dir_, sub_dir_, uid_, bad_gids_));
[email protected]73e4c362011-09-22 14:47:182022 EXPECT_FALSE(
2023 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292024 base_dir_, text_file_, uid_, bad_gids_));
[email protected]73e4c362011-09-22 14:47:182025 EXPECT_FALSE(
2026 file_util::VerifyPathControlledByUser(
[email protected]7312f42a2011-10-17 21:30:292027 sub_dir_, text_file_, uid_, bad_gids_));
2028}
2029
2030TEST_F(VerifyPathControlledByUserTest, GroupWriteTest) {
2031 // Make all files and directories writable only by their owner.
2032 ASSERT_NO_FATAL_FAILURE(
2033 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH|S_IWGRP));
2034 ASSERT_NO_FATAL_FAILURE(
2035 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH|S_IWGRP));
2036 ASSERT_NO_FATAL_FAILURE(
2037 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH|S_IWGRP));
2038
2039 // Any group is okay because the path is not group-writable.
2040 EXPECT_TRUE(
2041 file_util::VerifyPathControlledByUser(
2042 base_dir_, sub_dir_, uid_, ok_gids_));
2043 EXPECT_TRUE(
2044 file_util::VerifyPathControlledByUser(
2045 base_dir_, text_file_, uid_, ok_gids_));
2046 EXPECT_TRUE(
2047 file_util::VerifyPathControlledByUser(
2048 sub_dir_, text_file_, uid_, ok_gids_));
2049
2050 EXPECT_TRUE(
2051 file_util::VerifyPathControlledByUser(
2052 base_dir_, sub_dir_, uid_, bad_gids_));
2053 EXPECT_TRUE(
2054 file_util::VerifyPathControlledByUser(
2055 base_dir_, text_file_, uid_, bad_gids_));
2056 EXPECT_TRUE(
2057 file_util::VerifyPathControlledByUser(
2058 sub_dir_, text_file_, uid_, bad_gids_));
2059
2060 // No group is okay, because we don't check the group
2061 // if no group can write.
2062 std::set<gid_t> no_gids; // Empty set of gids.
2063 EXPECT_TRUE(
2064 file_util::VerifyPathControlledByUser(
2065 base_dir_, sub_dir_, uid_, no_gids));
2066 EXPECT_TRUE(
2067 file_util::VerifyPathControlledByUser(
2068 base_dir_, text_file_, uid_, no_gids));
2069 EXPECT_TRUE(
2070 file_util::VerifyPathControlledByUser(
2071 sub_dir_, text_file_, uid_, no_gids));
2072
2073
2074 // Make all files and directories writable by their group.
2075 ASSERT_NO_FATAL_FAILURE(
2076 ChangePosixFilePermissions(base_dir_, S_IWGRP, 0u));
2077 ASSERT_NO_FATAL_FAILURE(
2078 ChangePosixFilePermissions(sub_dir_, S_IWGRP, 0u));
2079 ASSERT_NO_FATAL_FAILURE(
2080 ChangePosixFilePermissions(text_file_, S_IWGRP, 0u));
2081
2082 // Now |ok_gids_| works, but |bad_gids_| fails.
2083 EXPECT_TRUE(
2084 file_util::VerifyPathControlledByUser(
2085 base_dir_, sub_dir_, uid_, ok_gids_));
2086 EXPECT_TRUE(
2087 file_util::VerifyPathControlledByUser(
2088 base_dir_, text_file_, uid_, ok_gids_));
2089 EXPECT_TRUE(
2090 file_util::VerifyPathControlledByUser(
2091 sub_dir_, text_file_, uid_, ok_gids_));
2092
2093 EXPECT_FALSE(
2094 file_util::VerifyPathControlledByUser(
2095 base_dir_, sub_dir_, uid_, bad_gids_));
2096 EXPECT_FALSE(
2097 file_util::VerifyPathControlledByUser(
2098 base_dir_, text_file_, uid_, bad_gids_));
2099 EXPECT_FALSE(
2100 file_util::VerifyPathControlledByUser(
2101 sub_dir_, text_file_, uid_, bad_gids_));
2102
2103 // Because any group in the group set is allowed,
2104 // the union of good and bad gids passes.
2105
2106 std::set<gid_t> multiple_gids;
2107 std::set_union(
2108 ok_gids_.begin(), ok_gids_.end(),
2109 bad_gids_.begin(), bad_gids_.end(),
2110 std::inserter(multiple_gids, multiple_gids.begin()));
2111
2112 EXPECT_TRUE(
2113 file_util::VerifyPathControlledByUser(
2114 base_dir_, sub_dir_, uid_, multiple_gids));
2115 EXPECT_TRUE(
2116 file_util::VerifyPathControlledByUser(
2117 base_dir_, text_file_, uid_, multiple_gids));
2118 EXPECT_TRUE(
2119 file_util::VerifyPathControlledByUser(
2120 sub_dir_, text_file_, uid_, multiple_gids));
2121
[email protected]73e4c362011-09-22 14:47:182122}
2123
[email protected]30fcaa492011-09-26 17:18:432124TEST_F(VerifyPathControlledByUserTest, WriteBitChecks) {
[email protected]73e4c362011-09-22 14:47:182125 // Make all files and directories non-world-writable.
2126 ASSERT_NO_FATAL_FAILURE(
2127 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2128 ASSERT_NO_FATAL_FAILURE(
2129 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2130 ASSERT_NO_FATAL_FAILURE(
2131 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2132
2133 // Initialy, we control all parts of the path.
2134 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292135 file_util::VerifyPathControlledByUser(
2136 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182137 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292138 file_util::VerifyPathControlledByUser(
2139 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182140 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292141 file_util::VerifyPathControlledByUser(
2142 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182143
2144 // Make base_dir_ world-writable.
2145 ASSERT_NO_FATAL_FAILURE(
2146 ChangePosixFilePermissions(base_dir_, S_IWOTH, 0u));
2147 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292148 file_util::VerifyPathControlledByUser(
2149 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182150 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292151 file_util::VerifyPathControlledByUser(
2152 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182153 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292154 file_util::VerifyPathControlledByUser(
2155 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182156
2157 // Make sub_dir_ world writable.
2158 ASSERT_NO_FATAL_FAILURE(
2159 ChangePosixFilePermissions(sub_dir_, S_IWOTH, 0u));
2160 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292161 file_util::VerifyPathControlledByUser(
2162 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182163 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292164 file_util::VerifyPathControlledByUser(
2165 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182166 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292167 file_util::VerifyPathControlledByUser(
2168 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182169
2170 // Make text_file_ world writable.
2171 ASSERT_NO_FATAL_FAILURE(
2172 ChangePosixFilePermissions(text_file_, S_IWOTH, 0u));
2173 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292174 file_util::VerifyPathControlledByUser(
2175 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182176 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292177 file_util::VerifyPathControlledByUser(
2178 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182179 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292180 file_util::VerifyPathControlledByUser(
2181 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182182
2183 // Make sub_dir_ non-world writable.
2184 ASSERT_NO_FATAL_FAILURE(
2185 ChangePosixFilePermissions(sub_dir_, 0u, S_IWOTH));
2186 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292187 file_util::VerifyPathControlledByUser(
2188 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182189 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292190 file_util::VerifyPathControlledByUser(
2191 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182192 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292193 file_util::VerifyPathControlledByUser(
2194 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182195
2196 // Make base_dir_ non-world-writable.
2197 ASSERT_NO_FATAL_FAILURE(
2198 ChangePosixFilePermissions(base_dir_, 0u, S_IWOTH));
2199 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292200 file_util::VerifyPathControlledByUser(
2201 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182202 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292203 file_util::VerifyPathControlledByUser(
2204 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182205 EXPECT_FALSE(
[email protected]7312f42a2011-10-17 21:30:292206 file_util::VerifyPathControlledByUser(
2207 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182208
2209 // Back to the initial state: Nothing is writable, so every path
2210 // should pass.
2211 ASSERT_NO_FATAL_FAILURE(
2212 ChangePosixFilePermissions(text_file_, 0u, S_IWOTH));
2213 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292214 file_util::VerifyPathControlledByUser(
2215 base_dir_, sub_dir_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182216 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292217 file_util::VerifyPathControlledByUser(
2218 base_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182219 EXPECT_TRUE(
[email protected]7312f42a2011-10-17 21:30:292220 file_util::VerifyPathControlledByUser(
2221 sub_dir_, text_file_, uid_, ok_gids_));
[email protected]73e4c362011-09-22 14:47:182222}
2223
2224#endif // defined(OS_POSIX)
2225
[email protected]11b901ee2008-09-10 00:16:282226} // namespace