1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "build/build_config.h"
19 #include "base/base_paths.h"
20 #include "base/file_util.h"
21 #include "base/files/file_path.h"
22 #include "base/files/scoped_temp_dir.h"
23 #include "base/path_service.h"
24 #include "base/test/test_file_util.h"
25 #include "base/threading/platform_thread.h"
26 #include "base/utf_string_conversions.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/platform_test.h"
31 #include "base/win/scoped_handle.h"
34 // This macro helps avoid wrapped lines in the test structs.
35 #define FPL(x) FILE_PATH_LITERAL(x)
41 // To test that file_util::Normalize FilePath() deals with NTFS reparse points
42 // correctly, we need functions to create and delete reparse points.
44 typedef struct _REPARSE_DATA_BUFFER
{
46 USHORT ReparseDataLength
;
50 USHORT SubstituteNameOffset
;
51 USHORT SubstituteNameLength
;
52 USHORT PrintNameOffset
;
53 USHORT PrintNameLength
;
56 } SymbolicLinkReparseBuffer
;
58 USHORT SubstituteNameOffset
;
59 USHORT SubstituteNameLength
;
60 USHORT PrintNameOffset
;
61 USHORT PrintNameLength
;
63 } MountPointReparseBuffer
;
66 } GenericReparseBuffer
;
68 } REPARSE_DATA_BUFFER
, *PREPARSE_DATA_BUFFER
;
70 // Sets a reparse point. |source| will now point to |target|. Returns true if
71 // the call succeeds, false otherwise.
72 bool SetReparsePoint(HANDLE source
, const FilePath
& target_path
) {
73 std::wstring kPathPrefix
= L
"\\??\\";
74 std::wstring target_str
;
75 // The juction will not work if the target path does not start with \??\ .
76 if (kPathPrefix
!= target_path
.value().substr(0, kPathPrefix
.size()))
77 target_str
+= kPathPrefix
;
78 target_str
+= target_path
.value();
79 const wchar_t* target
= target_str
.c_str();
80 USHORT size_target
= static_cast<USHORT
>(wcslen(target
)) * sizeof(target
[0]);
81 char buffer
[2000] = {0};
84 REPARSE_DATA_BUFFER
* data
= reinterpret_cast<REPARSE_DATA_BUFFER
*>(buffer
);
86 data
->ReparseTag
= 0xa0000003;
87 memcpy(data
->MountPointReparseBuffer
.PathBuffer
, target
, size_target
+ 2);
89 data
->MountPointReparseBuffer
.SubstituteNameLength
= size_target
;
90 data
->MountPointReparseBuffer
.PrintNameOffset
= size_target
+ 2;
91 data
->ReparseDataLength
= size_target
+ 4 + 8;
93 int data_size
= data
->ReparseDataLength
+ 8;
95 if (!DeviceIoControl(source
, FSCTL_SET_REPARSE_POINT
, &buffer
, data_size
,
96 NULL
, 0, &returned
, NULL
)) {
102 // Delete the reparse point referenced by |source|. Returns true if the call
103 // succeeds, false otherwise.
104 bool DeleteReparsePoint(HANDLE source
) {
106 REPARSE_DATA_BUFFER data
= {0};
107 data
.ReparseTag
= 0xa0000003;
108 if (!DeviceIoControl(source
, FSCTL_DELETE_REPARSE_POINT
, &data
, 8, NULL
, 0,
116 #if defined(OS_POSIX)
117 // Provide a simple way to change the permissions bits on |path| in tests.
118 // ASSERT failures will return, but not stop the test. Caller should wrap
119 // calls to this function in ASSERT_NO_FATAL_FAILURE().
120 void ChangePosixFilePermissions(const FilePath
& path
,
121 int mode_bits_to_set
,
122 int mode_bits_to_clear
) {
123 ASSERT_FALSE(mode_bits_to_set
& mode_bits_to_clear
)
124 << "Can't set and clear the same bits.";
127 ASSERT_TRUE(file_util::GetPosixFilePermissions(path
, &mode
));
128 mode
|= mode_bits_to_set
;
129 mode
&= ~mode_bits_to_clear
;
130 ASSERT_TRUE(file_util::SetPosixFilePermissions(path
, mode
));
132 #endif // defined(OS_POSIX)
134 const wchar_t bogus_content
[] = L
"I'm cannon fodder.";
136 const int FILES_AND_DIRECTORIES
=
137 file_util::FileEnumerator::FILES
| file_util::FileEnumerator::DIRECTORIES
;
139 // file_util winds up using autoreleased objects on the Mac, so this needs
140 // to be a PlatformTest
141 class FileUtilTest
: public PlatformTest
{
143 virtual void SetUp() OVERRIDE
{
144 PlatformTest::SetUp();
145 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
148 base::ScopedTempDir temp_dir_
;
151 // Collects all the results from the given file enumerator, and provides an
152 // interface to query whether a given file is present.
153 class FindResultCollector
{
155 explicit FindResultCollector(file_util::FileEnumerator
& enumerator
) {
157 while (!(cur_file
= enumerator
.Next()).value().empty()) {
158 FilePath::StringType path
= cur_file
.value();
159 // The file should not be returned twice.
160 EXPECT_TRUE(files_
.end() == files_
.find(path
))
161 << "Same file returned twice";
168 // Returns true if the enumerator found the file.
169 bool HasFile(const FilePath
& file
) const {
170 return files_
.find(file
.value()) != files_
.end();
174 return static_cast<int>(files_
.size());
178 std::set
<FilePath::StringType
> files_
;
181 // Simple function to dump some text into a new file.
182 void CreateTextFile(const FilePath
& filename
,
183 const std::wstring
& contents
) {
185 file
.open(filename
.value().c_str());
186 ASSERT_TRUE(file
.is_open());
191 // Simple function to take out some text from a file.
192 std::wstring
ReadTextFile(const FilePath
& filename
) {
193 wchar_t contents
[64];
195 file
.open(filename
.value().c_str());
196 EXPECT_TRUE(file
.is_open());
197 file
.getline(contents
, arraysize(contents
));
199 return std::wstring(contents
);
203 uint64
FileTimeAsUint64(const FILETIME
& ft
) {
205 u
.LowPart
= ft
.dwLowDateTime
;
206 u
.HighPart
= ft
.dwHighDateTime
;
211 const struct append_case
{
213 const wchar_t* ending
;
214 const wchar_t* result
;
217 {L
"c:\\colon\\backslash", L
"path", L
"c:\\colon\\backslash\\path"},
218 {L
"c:\\colon\\backslash\\", L
"path", L
"c:\\colon\\backslash\\path"},
219 {L
"c:\\colon\\backslash\\\\", L
"path", L
"c:\\colon\\backslash\\\\path"},
220 {L
"c:\\colon\\backslash\\", L
"", L
"c:\\colon\\backslash\\"},
221 {L
"c:\\colon\\backslash", L
"", L
"c:\\colon\\backslash\\"},
222 {L
"", L
"path", L
"\\path"},
224 #elif defined(OS_POSIX)
225 {L
"/foo/bar", L
"path", L
"/foo/bar/path"},
226 {L
"/foo/bar/", L
"path", L
"/foo/bar/path"},
227 {L
"/foo/bar//", L
"path", L
"/foo/bar//path"},
228 {L
"/foo/bar/", L
"", L
"/foo/bar/"},
229 {L
"/foo/bar", L
"", L
"/foo/bar/"},
230 {L
"", L
"path", L
"/path"},
235 static const struct filename_case
{
237 const wchar_t* filename
;
238 } filename_cases
[] = {
240 {L
"c:\\colon\\backslash", L
"backslash"},
241 {L
"c:\\colon\\backslash\\", L
""},
242 {L
"\\\\filename.exe", L
"filename.exe"},
243 {L
"filename.exe", L
"filename.exe"},
246 {L
"c:/colon/backslash", L
"backslash"},
247 {L
"c:/colon/backslash/", L
""},
249 {L
"///filename.exe", L
"filename.exe"},
250 #elif defined(OS_POSIX)
251 {L
"/foo/bar", L
"bar"},
253 {L
"/filename.exe", L
"filename.exe"},
254 {L
"filename.exe", L
"filename.exe"},
260 // Test finding the file type from a path name
261 static const struct extension_case
{
263 const wchar_t* extension
;
264 } extension_cases
[] = {
266 {L
"C:\\colon\\backslash\\filename.extension", L
"extension"},
267 {L
"C:\\colon\\backslash\\filename.", L
""},
268 {L
"C:\\colon\\backslash\\filename", L
""},
269 {L
"C:\\colon\\backslash\\", L
""},
270 {L
"C:\\colon\\backslash.\\", L
""},
271 {L
"C:\\colon\\backslash\filename.extension.extension2", L
"extension2"},
272 #elif defined(OS_POSIX)
273 {L
"/foo/bar/filename.extension", L
"extension"},
274 {L
"/foo/bar/filename.", L
""},
275 {L
"/foo/bar/filename", L
""},
277 {L
"/foo/bar./", L
""},
278 {L
"/foo/bar/filename.extension.extension2", L
"extension2"},
282 {L
"./foo.extension", L
"extension"},
283 {L
"/foo.extension1/bar.extension2", L
"extension2"},
287 // Test finding the directory component of a path
288 static const struct dir_case
{
289 const wchar_t* full_path
;
290 const wchar_t* directory
;
293 {L
"C:\\WINDOWS\\system32\\gdi32.dll", L
"C:\\WINDOWS\\system32"},
294 {L
"C:\\WINDOWS\\system32\\not_exist_thx_1138", L
"C:\\WINDOWS\\system32"},
295 {L
"C:\\WINDOWS\\system32\\", L
"C:\\WINDOWS\\system32"},
296 {L
"C:\\WINDOWS\\system32\\\\", L
"C:\\WINDOWS\\system32"},
297 {L
"C:\\WINDOWS\\system32", L
"C:\\WINDOWS"},
298 {L
"C:\\WINDOWS\\system32.\\", L
"C:\\WINDOWS\\system32."},
300 #elif defined(OS_POSIX)
301 {L
"/foo/bar/gdi32.dll", L
"/foo/bar"},
302 {L
"/foo/bar/not_exist_thx_1138", L
"/foo/bar"},
303 {L
"/foo/bar/", L
"/foo/bar"},
304 {L
"/foo/bar//", L
"/foo/bar"},
305 {L
"/foo/bar", L
"/foo"},
306 {L
"/foo/bar./", L
"/foo/bar."},
309 {L
"..", L
"."}, // yes, ".." technically lives in "."
313 TEST_F(FileUtilTest
, FileAndDirectorySize
) {
314 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
315 // should return 53 bytes.
316 FilePath file_01
= temp_dir_
.path().Append(FPL("The file 01.txt"));
317 CreateTextFile(file_01
, L
"12345678901234567890");
319 ASSERT_TRUE(file_util::GetFileSize(file_01
, &size_f1
));
320 EXPECT_EQ(20ll, size_f1
);
322 FilePath subdir_path
= temp_dir_
.path().Append(FPL("Level2"));
323 file_util::CreateDirectory(subdir_path
);
325 FilePath file_02
= subdir_path
.Append(FPL("The file 02.txt"));
326 CreateTextFile(file_02
, L
"123456789012345678901234567890");
328 ASSERT_TRUE(file_util::GetFileSize(file_02
, &size_f2
));
329 EXPECT_EQ(30ll, size_f2
);
331 FilePath subsubdir_path
= subdir_path
.Append(FPL("Level3"));
332 file_util::CreateDirectory(subsubdir_path
);
334 FilePath file_03
= subsubdir_path
.Append(FPL("The file 03.txt"));
335 CreateTextFile(file_03
, L
"123");
337 int64 computed_size
= file_util::ComputeDirectorySize(temp_dir_
.path());
338 EXPECT_EQ(size_f1
+ size_f2
+ 3, computed_size
);
341 TEST_F(FileUtilTest
, NormalizeFilePathBasic
) {
342 // Create a directory under the test dir. Because we create it,
343 // we know it is not a link.
344 FilePath file_a_path
= temp_dir_
.path().Append(FPL("file_a"));
345 FilePath dir_path
= temp_dir_
.path().Append(FPL("dir"));
346 FilePath file_b_path
= dir_path
.Append(FPL("file_b"));
347 file_util::CreateDirectory(dir_path
);
349 FilePath normalized_file_a_path
, normalized_file_b_path
;
350 ASSERT_FALSE(file_util::PathExists(file_a_path
));
351 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path
,
352 &normalized_file_a_path
))
353 << "NormalizeFilePath() should fail on nonexistent paths.";
355 CreateTextFile(file_a_path
, bogus_content
);
356 ASSERT_TRUE(file_util::PathExists(file_a_path
));
357 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path
,
358 &normalized_file_a_path
));
360 CreateTextFile(file_b_path
, bogus_content
);
361 ASSERT_TRUE(file_util::PathExists(file_b_path
));
362 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path
,
363 &normalized_file_b_path
));
365 // Beacuse this test created |dir_path|, we know it is not a link
366 // or junction. So, the real path of the directory holding file a
367 // must be the parent of the path holding file b.
368 ASSERT_TRUE(normalized_file_a_path
.DirName()
369 .IsParent(normalized_file_b_path
.DirName()));
374 TEST_F(FileUtilTest
, NormalizeFilePathReparsePoints
) {
375 // Build the following directory structure:
381 // | |-> long_name___... (Very long name.)
385 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
386 // |-> to_base_b (reparse point to temp_dir\base_b)
387 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
389 FilePath base_a
= temp_dir_
.path().Append(FPL("base_a"));
390 ASSERT_TRUE(file_util::CreateDirectory(base_a
));
392 FilePath sub_a
= base_a
.Append(FPL("sub_a"));
393 ASSERT_TRUE(file_util::CreateDirectory(sub_a
));
395 FilePath file_txt
= sub_a
.Append(FPL("file.txt"));
396 CreateTextFile(file_txt
, bogus_content
);
398 // Want a directory whose name is long enough to make the path to the file
399 // inside just under MAX_PATH chars. This will be used to test that when
400 // a junction expands to a path over MAX_PATH chars in length,
401 // NormalizeFilePath() fails without crashing.
402 FilePath
sub_long_rel(FPL("sub_long"));
403 FilePath
deep_txt(FPL("deep.txt"));
405 int target_length
= MAX_PATH
;
406 target_length
-= (sub_a
.value().length() + 1); // +1 for the sepperator '\'.
407 target_length
-= (sub_long_rel
.Append(deep_txt
).value().length() + 1);
408 // Without making the path a bit shorter, CreateDirectory() fails.
409 // the resulting path is still long enough to hit the failing case in
411 const int kCreateDirLimit
= 4;
412 target_length
-= kCreateDirLimit
;
413 FilePath::StringType long_name_str
= FPL("long_name_");
414 long_name_str
.resize(target_length
, '_');
416 FilePath long_name
= sub_a
.Append(FilePath(long_name_str
));
417 FilePath deep_file
= long_name
.Append(sub_long_rel
).Append(deep_txt
);
418 ASSERT_EQ(MAX_PATH
- kCreateDirLimit
, deep_file
.value().length());
420 FilePath sub_long
= deep_file
.DirName();
421 ASSERT_TRUE(file_util::CreateDirectory(sub_long
));
422 CreateTextFile(deep_file
, bogus_content
);
424 FilePath base_b
= temp_dir_
.path().Append(FPL("base_b"));
425 ASSERT_TRUE(file_util::CreateDirectory(base_b
));
427 FilePath to_sub_a
= base_b
.Append(FPL("to_sub_a"));
428 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a
));
429 base::win::ScopedHandle
reparse_to_sub_a(
430 ::CreateFile(to_sub_a
.value().c_str(),
432 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
435 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
437 ASSERT_TRUE(reparse_to_sub_a
.IsValid());
438 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a
, sub_a
));
440 FilePath to_base_b
= base_b
.Append(FPL("to_base_b"));
441 ASSERT_TRUE(file_util::CreateDirectory(to_base_b
));
442 base::win::ScopedHandle
reparse_to_base_b(
443 ::CreateFile(to_base_b
.value().c_str(),
445 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
448 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
450 ASSERT_TRUE(reparse_to_base_b
.IsValid());
451 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b
, base_b
));
453 FilePath to_sub_long
= base_b
.Append(FPL("to_sub_long"));
454 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long
));
455 base::win::ScopedHandle
reparse_to_sub_long(
456 ::CreateFile(to_sub_long
.value().c_str(),
458 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
461 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
463 ASSERT_TRUE(reparse_to_sub_long
.IsValid());
464 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long
, sub_long
));
466 // Normalize a junction free path: base_a\sub_a\file.txt .
467 FilePath normalized_path
;
468 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt
, &normalized_path
));
469 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
471 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
472 // the junction to_sub_a.
473 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
475 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
477 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
478 // normalized to exclude junctions to_base_b and to_sub_a .
479 ASSERT_TRUE(file_util::NormalizeFilePath(base_b
.Append(FPL("to_base_b"))
480 .Append(FPL("to_base_b"))
481 .Append(FPL("to_sub_a"))
482 .Append(FPL("file.txt")),
484 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
486 // A long enough path will cause NormalizeFilePath() to fail. Make a long
487 // path using to_base_b many times, and check that paths long enough to fail
488 // do not cause a crash.
489 FilePath long_path
= base_b
;
490 const int kLengthLimit
= MAX_PATH
+ 200;
491 while (long_path
.value().length() <= kLengthLimit
) {
492 long_path
= long_path
.Append(FPL("to_base_b"));
494 long_path
= long_path
.Append(FPL("to_sub_a"))
495 .Append(FPL("file.txt"));
497 ASSERT_FALSE(file_util::NormalizeFilePath(long_path
, &normalized_path
));
499 // Normalizing the junction to deep.txt should fail, because the expanded
500 // path to deep.txt is longer than MAX_PATH.
501 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long
.Append(deep_txt
),
504 // Delete the reparse points, and see that NormalizeFilePath() fails
506 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a
));
507 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b
));
508 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long
));
510 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
514 TEST_F(FileUtilTest
, DevicePathToDriveLetter
) {
515 // Get a drive letter.
516 std::wstring real_drive_letter
= temp_dir_
.path().value().substr(0, 2);
517 if (!isalpha(real_drive_letter
[0]) || ':' != real_drive_letter
[1]) {
518 LOG(ERROR
) << "Can't get a drive letter to test with.";
522 // Get the NT style path to that drive.
523 wchar_t device_path
[MAX_PATH
] = {'\0'};
525 ::QueryDosDevice(real_drive_letter
.c_str(), device_path
, MAX_PATH
));
526 FilePath
actual_device_path(device_path
);
529 // Run DevicePathToDriveLetterPath() on the NT style path we got from
530 // QueryDosDevice(). Expect the drive letter we started with.
531 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path
,
533 ASSERT_EQ(real_drive_letter
, win32_path
.value());
535 // Add some directories to the path. Expect those extra path componenets
537 FilePath
kRelativePath(FPL("dir1\\dir2\\file.txt"));
538 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
539 actual_device_path
.Append(kRelativePath
),
541 EXPECT_EQ(FilePath(real_drive_letter
+ L
"\\").Append(kRelativePath
).value(),
544 // Deform the real path so that it is invalid by removing the last four
545 // characters. The way windows names devices that are hard disks
546 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
547 // than three characters. The only way the truncated string could be a
548 // real drive is if more than 10^3 disks are mounted:
549 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
550 // Check that DevicePathToDriveLetterPath fails.
551 int path_length
= actual_device_path
.value().length();
552 int new_length
= path_length
- 4;
553 ASSERT_LT(0, new_length
);
554 FilePath
prefix_of_real_device_path(
555 actual_device_path
.value().substr(0, new_length
));
556 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
557 prefix_of_real_device_path
,
560 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
561 prefix_of_real_device_path
.Append(kRelativePath
),
564 // Deform the real path so that it is invalid by adding some characters. For
565 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
566 // request for the drive letter whose native path is
567 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
568 // because drives are numbered in order and mounting 112345 hard disks will
570 const FilePath::StringType kExtraChars
= FPL("12345");
572 FilePath
real_device_path_plus_numbers(
573 actual_device_path
.value() + kExtraChars
);
575 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
576 real_device_path_plus_numbers
,
579 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
580 real_device_path_plus_numbers
.Append(kRelativePath
),
584 TEST_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(),
590 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
593 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
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
);
603 TEST_F(FileUtilTest
, CreateTemporaryFileInDirLongPathTest
) {
604 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
605 // if it is available. This test requires that:
606 // - the filesystem at |temp_dir_| supports long filenames.
607 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
608 // directories of |temp_dir_|.
609 const FilePath::CharType kLongDirName
[] = FPL("A long path");
610 const FilePath::CharType kTestSubDirName
[] = FPL("test");
611 FilePath long_test_dir
= temp_dir_
.path().Append(kLongDirName
);
612 ASSERT_TRUE(file_util::CreateDirectory(long_test_dir
));
614 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
615 // different short name.
616 WCHAR path_buffer
[MAX_PATH
];
617 DWORD path_buffer_length
= GetShortPathName(long_test_dir
.value().c_str(),
618 path_buffer
, MAX_PATH
);
619 ASSERT_LT(path_buffer_length
, DWORD(MAX_PATH
));
620 ASSERT_NE(DWORD(0), path_buffer_length
);
621 FilePath
short_test_dir(path_buffer
);
622 ASSERT_STRNE(kLongDirName
, short_test_dir
.BaseName().value().c_str());
625 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir
, &temp_file
));
626 EXPECT_STREQ(kLongDirName
, temp_file
.DirName().BaseName().value().c_str());
627 EXPECT_TRUE(file_util::PathExists(temp_file
));
629 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
630 // unreadable. We should still be able to create a temp file in the
631 // subdirectory, but we won't be able to determine the long path for it. This
632 // mimics the environment that some users run where their user profiles reside
633 // in a location where the don't have full access to the higher level
634 // directories. (Note that this assumption is true for NTFS, but not for some
635 // network file systems. E.g. AFS).
636 FilePath access_test_dir
= long_test_dir
.Append(kTestSubDirName
);
637 ASSERT_TRUE(file_util::CreateDirectory(access_test_dir
));
638 file_util::PermissionRestorer
long_test_dir_restorer(long_test_dir
);
639 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir
));
641 // Use the short form of the directory to create a temporary filename.
642 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
643 short_test_dir
.Append(kTestSubDirName
), &temp_file
));
644 EXPECT_TRUE(file_util::PathExists(temp_file
));
645 EXPECT_TRUE(short_test_dir
.IsParent(temp_file
.DirName()));
647 // Check that the long path can't be determined for |temp_file|.
648 path_buffer_length
= GetLongPathName(temp_file
.value().c_str(),
649 path_buffer
, MAX_PATH
);
650 EXPECT_EQ(DWORD(0), path_buffer_length
);
653 #endif // defined(OS_WIN)
655 #if defined(OS_POSIX)
657 TEST_F(FileUtilTest
, CreateAndReadSymlinks
) {
658 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
659 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
660 CreateTextFile(link_to
, bogus_content
);
662 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
663 << "Failed to create file symlink.";
665 // If we created the link properly, we should be able to read the
666 // contents through it.
667 std::wstring contents
= ReadTextFile(link_from
);
668 ASSERT_EQ(contents
, bogus_content
);
671 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from
, &result
));
672 ASSERT_EQ(link_to
.value(), result
.value());
674 // Link to a directory.
675 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
676 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
677 file_util::CreateDirectory(link_to
);
679 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
680 << "Failed to create directory symlink.";
683 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to
, link_to
));
684 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to
, &result
));
685 FilePath missing
= temp_dir_
.path().Append(FPL("missing"));
686 ASSERT_FALSE(file_util::ReadSymbolicLink(missing
, &result
));
689 // The following test of NormalizeFilePath() require that we create a symlink.
690 // This can not be done on Windows before Vista. On Vista, creating a symlink
691 // requires privilege "SeCreateSymbolicLinkPrivilege".
692 // TODO(skerner): Investigate the possibility of giving base_unittests the
693 // privileges required to create a symlink.
694 TEST_F(FileUtilTest
, NormalizeFilePathSymlinks
) {
695 FilePath normalized_path
;
697 // Link one file to another.
698 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
699 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
700 CreateTextFile(link_to
, bogus_content
);
702 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
703 << "Failed to create file symlink.";
705 // Check that NormalizeFilePath sees the link.
706 ASSERT_TRUE(file_util::NormalizeFilePath(link_from
, &normalized_path
));
707 ASSERT_TRUE(link_to
!= link_from
);
708 ASSERT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
709 ASSERT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
711 // Link to a directory.
712 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
713 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
714 file_util::CreateDirectory(link_to
);
716 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
717 << "Failed to create directory symlink.";
719 ASSERT_FALSE(file_util::NormalizeFilePath(link_from
, &normalized_path
))
720 << "Links to directories should return false.";
722 // Test that a loop in the links causes NormalizeFilePath() to return false.
723 link_from
= temp_dir_
.path().Append(FPL("link_a"));
724 link_to
= temp_dir_
.path().Append(FPL("link_b"));
725 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
726 << "Failed to create loop symlink a.";
727 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from
, link_to
))
728 << "Failed to create loop symlink b.";
731 ASSERT_FALSE(file_util::NormalizeFilePath(link_from
, &normalized_path
));
733 #endif // defined(OS_POSIX)
735 TEST_F(FileUtilTest
, DeleteNonExistent
) {
736 FilePath non_existent
= temp_dir_
.path().AppendASCII("bogus_file_dne.foobar");
737 ASSERT_FALSE(file_util::PathExists(non_existent
));
739 EXPECT_TRUE(file_util::Delete(non_existent
, false));
740 ASSERT_FALSE(file_util::PathExists(non_existent
));
741 EXPECT_TRUE(file_util::Delete(non_existent
, true));
742 ASSERT_FALSE(file_util::PathExists(non_existent
));
745 TEST_F(FileUtilTest
, DeleteFile
) {
747 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 1.txt"));
748 CreateTextFile(file_name
, bogus_content
);
749 ASSERT_TRUE(file_util::PathExists(file_name
));
751 // Make sure it's deleted
752 EXPECT_TRUE(file_util::Delete(file_name
, false));
753 EXPECT_FALSE(file_util::PathExists(file_name
));
755 // Test recursive case, create a new file
756 file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
757 CreateTextFile(file_name
, bogus_content
);
758 ASSERT_TRUE(file_util::PathExists(file_name
));
760 // Make sure it's deleted
761 EXPECT_TRUE(file_util::Delete(file_name
, true));
762 EXPECT_FALSE(file_util::PathExists(file_name
));
765 #if defined(OS_POSIX)
766 TEST_F(FileUtilTest
, DeleteSymlinkToExistentFile
) {
768 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
769 CreateTextFile(file_name
, bogus_content
);
770 ASSERT_TRUE(file_util::PathExists(file_name
));
772 // Create a symlink to the file.
773 FilePath file_link
= temp_dir_
.path().Append("file_link_2");
774 ASSERT_TRUE(file_util::CreateSymbolicLink(file_name
, file_link
))
775 << "Failed to create symlink.";
777 // Delete the symbolic link.
778 EXPECT_TRUE(file_util::Delete(file_link
, false));
780 // Make sure original file is not deleted.
781 EXPECT_FALSE(file_util::PathExists(file_link
));
782 EXPECT_TRUE(file_util::PathExists(file_name
));
785 TEST_F(FileUtilTest
, DeleteSymlinkToNonExistentFile
) {
786 // Create a non-existent file path.
787 FilePath non_existent
= temp_dir_
.path().Append(FPL("Test DeleteFile 3.txt"));
788 EXPECT_FALSE(file_util::PathExists(non_existent
));
790 // Create a symlink to the non-existent file.
791 FilePath file_link
= temp_dir_
.path().Append("file_link_3");
792 ASSERT_TRUE(file_util::CreateSymbolicLink(non_existent
, file_link
))
793 << "Failed to create symlink.";
795 // Make sure the symbolic link is exist.
796 EXPECT_TRUE(file_util::IsLink(file_link
));
797 EXPECT_FALSE(file_util::PathExists(file_link
));
799 // Delete the symbolic link.
800 EXPECT_TRUE(file_util::Delete(file_link
, false));
802 // Make sure the symbolic link is deleted.
803 EXPECT_FALSE(file_util::IsLink(file_link
));
806 TEST_F(FileUtilTest
, ChangeFilePermissionsAndRead
) {
807 // Create a file path.
808 FilePath file_name
= temp_dir_
.path().Append(FPL("Test Readable File.txt"));
809 EXPECT_FALSE(file_util::PathExists(file_name
));
811 const std::string
kData("hello");
813 int buffer_size
= kData
.length();
814 char* buffer
= new char[buffer_size
];
817 EXPECT_EQ(static_cast<int>(kData
.length()),
818 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
819 EXPECT_TRUE(file_util::PathExists(file_name
));
821 // Make sure the file is readable.
823 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
824 EXPECT_TRUE(mode
& file_util::FILE_PERMISSION_READ_BY_USER
);
826 // Get rid of the read permission.
827 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name
, 0u));
828 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
829 EXPECT_FALSE(mode
& file_util::FILE_PERMISSION_READ_BY_USER
);
830 // Make sure the file can't be read.
831 EXPECT_EQ(-1, file_util::ReadFile(file_name
, buffer
, buffer_size
));
833 // Give the read permission.
834 EXPECT_TRUE(file_util::SetPosixFilePermissions(
836 file_util::FILE_PERMISSION_READ_BY_USER
));
837 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
838 EXPECT_TRUE(mode
& file_util::FILE_PERMISSION_READ_BY_USER
);
839 // Make sure the file can be read.
840 EXPECT_EQ(static_cast<int>(kData
.length()),
841 file_util::ReadFile(file_name
, buffer
, buffer_size
));
844 EXPECT_TRUE(file_util::Delete(file_name
, false));
845 EXPECT_FALSE(file_util::PathExists(file_name
));
850 TEST_F(FileUtilTest
, ChangeFilePermissionsAndWrite
) {
851 // Create a file path.
852 FilePath file_name
= temp_dir_
.path().Append(FPL("Test Readable File.txt"));
853 EXPECT_FALSE(file_util::PathExists(file_name
));
855 const std::string
kData("hello");
858 EXPECT_EQ(static_cast<int>(kData
.length()),
859 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
860 EXPECT_TRUE(file_util::PathExists(file_name
));
862 // Make sure the file is writable.
864 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
865 EXPECT_TRUE(mode
& file_util::FILE_PERMISSION_WRITE_BY_USER
);
866 EXPECT_TRUE(file_util::PathIsWritable(file_name
));
868 // Get rid of the write permission.
869 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name
, 0u));
870 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
871 EXPECT_FALSE(mode
& file_util::FILE_PERMISSION_WRITE_BY_USER
);
872 // Make sure the file can't be write.
874 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
875 EXPECT_FALSE(file_util::PathIsWritable(file_name
));
877 // Give read permission.
878 EXPECT_TRUE(file_util::SetPosixFilePermissions(
880 file_util::FILE_PERMISSION_WRITE_BY_USER
));
881 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
882 EXPECT_TRUE(mode
& file_util::FILE_PERMISSION_WRITE_BY_USER
);
883 // Make sure the file can be write.
884 EXPECT_EQ(static_cast<int>(kData
.length()),
885 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
886 EXPECT_TRUE(file_util::PathIsWritable(file_name
));
889 EXPECT_TRUE(file_util::Delete(file_name
, false));
890 EXPECT_FALSE(file_util::PathExists(file_name
));
893 TEST_F(FileUtilTest
, ChangeDirectoryPermissionsAndEnumerate
) {
894 // Create a directory path.
895 FilePath subdir_path
=
896 temp_dir_
.path().Append(FPL("PermissionTest1"));
897 file_util::CreateDirectory(subdir_path
);
898 ASSERT_TRUE(file_util::PathExists(subdir_path
));
900 // Create a dummy file to enumerate.
901 FilePath file_name
= subdir_path
.Append(FPL("Test Readable File.txt"));
902 EXPECT_FALSE(file_util::PathExists(file_name
));
903 const std::string
kData("hello");
904 EXPECT_EQ(static_cast<int>(kData
.length()),
905 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
906 EXPECT_TRUE(file_util::PathExists(file_name
));
908 // Make sure the directory has the all permissions.
910 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path
, &mode
));
911 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK
,
912 mode
& file_util::FILE_PERMISSION_USER_MASK
);
914 // Get rid of the permissions from the directory.
915 EXPECT_TRUE(file_util::SetPosixFilePermissions(subdir_path
, 0u));
916 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path
, &mode
));
917 EXPECT_FALSE(mode
& file_util::FILE_PERMISSION_USER_MASK
);
919 // Make sure the file in the directory can't be enumerated.
920 file_util::FileEnumerator
f1(subdir_path
, true,
921 file_util::FileEnumerator::FILES
);
922 EXPECT_TRUE(file_util::PathExists(subdir_path
));
923 FindResultCollector
c1(f1
);
924 EXPECT_EQ(c1
.size(), 0);
925 EXPECT_FALSE(file_util::GetPosixFilePermissions(file_name
, &mode
));
927 // Give the permissions to the directory.
928 EXPECT_TRUE(file_util::SetPosixFilePermissions(
930 file_util::FILE_PERMISSION_USER_MASK
));
931 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path
, &mode
));
932 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK
,
933 mode
& file_util::FILE_PERMISSION_USER_MASK
);
935 // Make sure the file in the directory can be enumerated.
936 file_util::FileEnumerator
f2(subdir_path
, true,
937 file_util::FileEnumerator::FILES
);
938 FindResultCollector
c2(f2
);
939 EXPECT_TRUE(c2
.HasFile(file_name
));
940 EXPECT_EQ(c2
.size(), 1);
943 EXPECT_TRUE(file_util::Delete(subdir_path
, true));
944 EXPECT_FALSE(file_util::PathExists(subdir_path
));
947 #endif // defined(OS_POSIX)
950 // Tests that the Delete function works for wild cards, especially
951 // with the recursion flag. Also coincidentally tests PathExists.
952 // TODO(erikkay): see if anyone's actually using this feature of the API
953 TEST_F(FileUtilTest
, DeleteWildCard
) {
954 // Create a file and a directory
955 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteWildCard.txt"));
956 CreateTextFile(file_name
, bogus_content
);
957 ASSERT_TRUE(file_util::PathExists(file_name
));
959 FilePath subdir_path
= temp_dir_
.path().Append(FPL("DeleteWildCardDir"));
960 file_util::CreateDirectory(subdir_path
);
961 ASSERT_TRUE(file_util::PathExists(subdir_path
));
963 // Create the wildcard path
964 FilePath directory_contents
= temp_dir_
.path();
965 directory_contents
= directory_contents
.Append(FPL("*"));
967 // Delete non-recursively and check that only the file is deleted
968 EXPECT_TRUE(file_util::Delete(directory_contents
, false));
969 EXPECT_FALSE(file_util::PathExists(file_name
));
970 EXPECT_TRUE(file_util::PathExists(subdir_path
));
972 // Delete recursively and make sure all contents are deleted
973 EXPECT_TRUE(file_util::Delete(directory_contents
, true));
974 EXPECT_FALSE(file_util::PathExists(file_name
));
975 EXPECT_FALSE(file_util::PathExists(subdir_path
));
978 // TODO(erikkay): see if anyone's actually using this feature of the API
979 TEST_F(FileUtilTest
, DeleteNonExistantWildCard
) {
980 // Create a file and a directory
981 FilePath subdir_path
=
982 temp_dir_
.path().Append(FPL("DeleteNonExistantWildCard"));
983 file_util::CreateDirectory(subdir_path
);
984 ASSERT_TRUE(file_util::PathExists(subdir_path
));
986 // Create the wildcard path
987 FilePath directory_contents
= subdir_path
;
988 directory_contents
= directory_contents
.Append(FPL("*"));
990 // Delete non-recursively and check nothing got deleted
991 EXPECT_TRUE(file_util::Delete(directory_contents
, false));
992 EXPECT_TRUE(file_util::PathExists(subdir_path
));
994 // Delete recursively and check nothing got deleted
995 EXPECT_TRUE(file_util::Delete(directory_contents
, true));
996 EXPECT_TRUE(file_util::PathExists(subdir_path
));
1000 // Tests non-recursive Delete() for a directory.
1001 TEST_F(FileUtilTest
, DeleteDirNonRecursive
) {
1002 // Create a subdirectory and put a file and two directories inside.
1003 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirNonRecursive"));
1004 file_util::CreateDirectory(test_subdir
);
1005 ASSERT_TRUE(file_util::PathExists(test_subdir
));
1007 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDir.txt"));
1008 CreateTextFile(file_name
, bogus_content
);
1009 ASSERT_TRUE(file_util::PathExists(file_name
));
1011 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
1012 file_util::CreateDirectory(subdir_path1
);
1013 ASSERT_TRUE(file_util::PathExists(subdir_path1
));
1015 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
1016 file_util::CreateDirectory(subdir_path2
);
1017 ASSERT_TRUE(file_util::PathExists(subdir_path2
));
1019 // Delete non-recursively and check that the empty dir got deleted
1020 EXPECT_TRUE(file_util::Delete(subdir_path2
, false));
1021 EXPECT_FALSE(file_util::PathExists(subdir_path2
));
1023 // Delete non-recursively and check that nothing got deleted
1024 EXPECT_FALSE(file_util::Delete(test_subdir
, false));
1025 EXPECT_TRUE(file_util::PathExists(test_subdir
));
1026 EXPECT_TRUE(file_util::PathExists(file_name
));
1027 EXPECT_TRUE(file_util::PathExists(subdir_path1
));
1030 // Tests recursive Delete() for a directory.
1031 TEST_F(FileUtilTest
, DeleteDirRecursive
) {
1032 // Create a subdirectory and put a file and two directories inside.
1033 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirRecursive"));
1034 file_util::CreateDirectory(test_subdir
);
1035 ASSERT_TRUE(file_util::PathExists(test_subdir
));
1037 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDirRecursive.txt"));
1038 CreateTextFile(file_name
, bogus_content
);
1039 ASSERT_TRUE(file_util::PathExists(file_name
));
1041 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
1042 file_util::CreateDirectory(subdir_path1
);
1043 ASSERT_TRUE(file_util::PathExists(subdir_path1
));
1045 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
1046 file_util::CreateDirectory(subdir_path2
);
1047 ASSERT_TRUE(file_util::PathExists(subdir_path2
));
1049 // Delete recursively and check that the empty dir got deleted
1050 EXPECT_TRUE(file_util::Delete(subdir_path2
, true));
1051 EXPECT_FALSE(file_util::PathExists(subdir_path2
));
1053 // Delete recursively and check that everything got deleted
1054 EXPECT_TRUE(file_util::Delete(test_subdir
, true));
1055 EXPECT_FALSE(file_util::PathExists(file_name
));
1056 EXPECT_FALSE(file_util::PathExists(subdir_path1
));
1057 EXPECT_FALSE(file_util::PathExists(test_subdir
));
1060 TEST_F(FileUtilTest
, MoveFileNew
) {
1062 FilePath file_name_from
=
1063 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1064 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1065 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1068 FilePath file_name_to
= temp_dir_
.path().Append(
1069 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
1070 ASSERT_FALSE(file_util::PathExists(file_name_to
));
1072 EXPECT_TRUE(file_util::Move(file_name_from
, file_name_to
));
1074 // Check everything has been moved.
1075 EXPECT_FALSE(file_util::PathExists(file_name_from
));
1076 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1079 TEST_F(FileUtilTest
, MoveFileExists
) {
1081 FilePath file_name_from
=
1082 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1083 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1084 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1086 // The destination name.
1087 FilePath file_name_to
= temp_dir_
.path().Append(
1088 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
1089 CreateTextFile(file_name_to
, L
"Old file content");
1090 ASSERT_TRUE(file_util::PathExists(file_name_to
));
1092 EXPECT_TRUE(file_util::Move(file_name_from
, file_name_to
));
1094 // Check everything has been moved.
1095 EXPECT_FALSE(file_util::PathExists(file_name_from
));
1096 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1097 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
1100 TEST_F(FileUtilTest
, MoveFileDirExists
) {
1102 FilePath file_name_from
=
1103 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1104 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1105 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1107 // The destination directory
1108 FilePath dir_name_to
=
1109 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1110 file_util::CreateDirectory(dir_name_to
);
1111 ASSERT_TRUE(file_util::PathExists(dir_name_to
));
1113 EXPECT_FALSE(file_util::Move(file_name_from
, dir_name_to
));
1117 TEST_F(FileUtilTest
, MoveNew
) {
1118 // Create a directory
1119 FilePath dir_name_from
=
1120 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1121 file_util::CreateDirectory(dir_name_from
);
1122 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1124 // Create a file under the directory
1125 FilePath
txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1126 FilePath file_name_from
= dir_name_from
.Append(txt_file_name
);
1127 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1128 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1130 // Move the directory.
1131 FilePath dir_name_to
=
1132 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1133 FilePath file_name_to
=
1134 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1136 ASSERT_FALSE(file_util::PathExists(dir_name_to
));
1138 EXPECT_TRUE(file_util::Move(dir_name_from
, dir_name_to
));
1140 // Check everything has been moved.
1141 EXPECT_FALSE(file_util::PathExists(dir_name_from
));
1142 EXPECT_FALSE(file_util::PathExists(file_name_from
));
1143 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1144 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1146 // Test path traversal.
1147 file_name_from
= dir_name_to
.Append(txt_file_name
);
1148 file_name_to
= dir_name_to
.Append(FILE_PATH_LITERAL(".."));
1149 file_name_to
= file_name_to
.Append(txt_file_name
);
1150 EXPECT_FALSE(file_util::Move(file_name_from
, file_name_to
));
1151 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1152 EXPECT_FALSE(file_util::PathExists(file_name_to
));
1153 EXPECT_TRUE(file_util::MoveUnsafe(file_name_from
, file_name_to
));
1154 EXPECT_FALSE(file_util::PathExists(file_name_from
));
1155 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1158 TEST_F(FileUtilTest
, MoveExist
) {
1159 // Create a directory
1160 FilePath dir_name_from
=
1161 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1162 file_util::CreateDirectory(dir_name_from
);
1163 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1165 // Create a file under the directory
1166 FilePath file_name_from
=
1167 dir_name_from
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1168 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1169 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1171 // Move the directory
1172 FilePath dir_name_exists
=
1173 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1175 FilePath dir_name_to
=
1176 dir_name_exists
.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1177 FilePath file_name_to
=
1178 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1180 // Create the destination directory.
1181 file_util::CreateDirectory(dir_name_exists
);
1182 ASSERT_TRUE(file_util::PathExists(dir_name_exists
));
1184 EXPECT_TRUE(file_util::Move(dir_name_from
, dir_name_to
));
1186 // Check everything has been moved.
1187 EXPECT_FALSE(file_util::PathExists(dir_name_from
));
1188 EXPECT_FALSE(file_util::PathExists(file_name_from
));
1189 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1190 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1193 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyNew
) {
1194 // Create a directory.
1195 FilePath dir_name_from
=
1196 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1197 file_util::CreateDirectory(dir_name_from
);
1198 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1200 // Create a file under the directory.
1201 FilePath file_name_from
=
1202 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1203 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1204 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1206 // Create a subdirectory.
1207 FilePath subdir_name_from
=
1208 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1209 file_util::CreateDirectory(subdir_name_from
);
1210 ASSERT_TRUE(file_util::PathExists(subdir_name_from
));
1212 // Create a file under the subdirectory.
1213 FilePath file_name2_from
=
1214 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1215 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1216 ASSERT_TRUE(file_util::PathExists(file_name2_from
));
1218 // Copy the directory recursively.
1219 FilePath dir_name_to
=
1220 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1221 FilePath file_name_to
=
1222 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1223 FilePath subdir_name_to
=
1224 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1225 FilePath file_name2_to
=
1226 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1228 ASSERT_FALSE(file_util::PathExists(dir_name_to
));
1230 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from
, dir_name_to
, true));
1232 // Check everything has been copied.
1233 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1234 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1235 EXPECT_TRUE(file_util::PathExists(subdir_name_from
));
1236 EXPECT_TRUE(file_util::PathExists(file_name2_from
));
1237 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1238 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1239 EXPECT_TRUE(file_util::PathExists(subdir_name_to
));
1240 EXPECT_TRUE(file_util::PathExists(file_name2_to
));
1243 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyExists
) {
1244 // Create a directory.
1245 FilePath dir_name_from
=
1246 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1247 file_util::CreateDirectory(dir_name_from
);
1248 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1250 // Create a file under the directory.
1251 FilePath file_name_from
=
1252 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1253 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1254 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1256 // Create a subdirectory.
1257 FilePath subdir_name_from
=
1258 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1259 file_util::CreateDirectory(subdir_name_from
);
1260 ASSERT_TRUE(file_util::PathExists(subdir_name_from
));
1262 // Create a file under the subdirectory.
1263 FilePath file_name2_from
=
1264 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1265 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1266 ASSERT_TRUE(file_util::PathExists(file_name2_from
));
1268 // Copy the directory recursively.
1269 FilePath dir_name_exists
=
1270 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1272 FilePath dir_name_to
=
1273 dir_name_exists
.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1274 FilePath file_name_to
=
1275 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1276 FilePath subdir_name_to
=
1277 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1278 FilePath file_name2_to
=
1279 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1281 // Create the destination directory.
1282 file_util::CreateDirectory(dir_name_exists
);
1283 ASSERT_TRUE(file_util::PathExists(dir_name_exists
));
1285 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from
, dir_name_exists
, true));
1287 // Check everything has been copied.
1288 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1289 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1290 EXPECT_TRUE(file_util::PathExists(subdir_name_from
));
1291 EXPECT_TRUE(file_util::PathExists(file_name2_from
));
1292 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1293 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1294 EXPECT_TRUE(file_util::PathExists(subdir_name_to
));
1295 EXPECT_TRUE(file_util::PathExists(file_name2_to
));
1298 TEST_F(FileUtilTest
, CopyDirectoryNew
) {
1299 // Create a directory.
1300 FilePath dir_name_from
=
1301 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1302 file_util::CreateDirectory(dir_name_from
);
1303 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1305 // Create a file under the directory.
1306 FilePath file_name_from
=
1307 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1308 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1309 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1311 // Create a subdirectory.
1312 FilePath subdir_name_from
=
1313 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1314 file_util::CreateDirectory(subdir_name_from
);
1315 ASSERT_TRUE(file_util::PathExists(subdir_name_from
));
1317 // Create a file under the subdirectory.
1318 FilePath file_name2_from
=
1319 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1320 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1321 ASSERT_TRUE(file_util::PathExists(file_name2_from
));
1323 // Copy the directory not recursively.
1324 FilePath dir_name_to
=
1325 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1326 FilePath file_name_to
=
1327 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1328 FilePath subdir_name_to
=
1329 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1331 ASSERT_FALSE(file_util::PathExists(dir_name_to
));
1333 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from
, dir_name_to
, false));
1335 // Check everything has been copied.
1336 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1337 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1338 EXPECT_TRUE(file_util::PathExists(subdir_name_from
));
1339 EXPECT_TRUE(file_util::PathExists(file_name2_from
));
1340 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1341 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1342 EXPECT_FALSE(file_util::PathExists(subdir_name_to
));
1345 TEST_F(FileUtilTest
, CopyDirectoryExists
) {
1346 // Create a directory.
1347 FilePath dir_name_from
=
1348 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1349 file_util::CreateDirectory(dir_name_from
);
1350 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1352 // Create a file under the directory.
1353 FilePath file_name_from
=
1354 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1355 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1356 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1358 // Create a subdirectory.
1359 FilePath subdir_name_from
=
1360 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1361 file_util::CreateDirectory(subdir_name_from
);
1362 ASSERT_TRUE(file_util::PathExists(subdir_name_from
));
1364 // Create a file under the subdirectory.
1365 FilePath file_name2_from
=
1366 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1367 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1368 ASSERT_TRUE(file_util::PathExists(file_name2_from
));
1370 // Copy the directory not recursively.
1371 FilePath dir_name_to
=
1372 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1373 FilePath file_name_to
=
1374 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1375 FilePath subdir_name_to
=
1376 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1378 // Create the destination directory.
1379 file_util::CreateDirectory(dir_name_to
);
1380 ASSERT_TRUE(file_util::PathExists(dir_name_to
));
1382 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from
, dir_name_to
, false));
1384 // Check everything has been copied.
1385 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1386 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1387 EXPECT_TRUE(file_util::PathExists(subdir_name_from
));
1388 EXPECT_TRUE(file_util::PathExists(file_name2_from
));
1389 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1390 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1391 EXPECT_FALSE(file_util::PathExists(subdir_name_to
));
1394 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToNew
) {
1396 FilePath file_name_from
=
1397 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1398 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1399 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1401 // The destination name
1402 FilePath file_name_to
= temp_dir_
.path().Append(
1403 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1404 ASSERT_FALSE(file_util::PathExists(file_name_to
));
1406 EXPECT_TRUE(file_util::CopyDirectory(file_name_from
, file_name_to
, true));
1408 // Check the has been copied
1409 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1412 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExisting
) {
1414 FilePath file_name_from
=
1415 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1416 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1417 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1419 // The destination name
1420 FilePath file_name_to
= temp_dir_
.path().Append(
1421 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1422 CreateTextFile(file_name_to
, L
"Old file content");
1423 ASSERT_TRUE(file_util::PathExists(file_name_to
));
1425 EXPECT_TRUE(file_util::CopyDirectory(file_name_from
, file_name_to
, true));
1427 // Check the has been copied
1428 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1429 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
1432 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExistingDirectory
) {
1434 FilePath file_name_from
=
1435 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1436 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1437 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1440 FilePath dir_name_to
=
1441 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1442 file_util::CreateDirectory(dir_name_to
);
1443 ASSERT_TRUE(file_util::PathExists(dir_name_to
));
1444 FilePath file_name_to
=
1445 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1447 EXPECT_TRUE(file_util::CopyDirectory(file_name_from
, dir_name_to
, true));
1449 // Check the has been copied
1450 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1453 TEST_F(FileUtilTest
, CopyDirectoryWithTrailingSeparators
) {
1454 // Create a directory.
1455 FilePath dir_name_from
=
1456 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1457 file_util::CreateDirectory(dir_name_from
);
1458 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1460 // Create a file under the directory.
1461 FilePath file_name_from
=
1462 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1463 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1464 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1466 // Copy the directory recursively.
1467 FilePath dir_name_to
=
1468 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1469 FilePath file_name_to
=
1470 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1472 // Create from path with trailing separators.
1474 FilePath from_path
=
1475 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1476 #elif defined (OS_POSIX)
1477 FilePath from_path
=
1478 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1481 EXPECT_TRUE(file_util::CopyDirectory(from_path
, dir_name_to
, true));
1483 // Check everything has been copied.
1484 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1485 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1486 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1487 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1490 TEST_F(FileUtilTest
, CopyFile
) {
1491 // Create a directory
1492 FilePath dir_name_from
=
1493 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1494 file_util::CreateDirectory(dir_name_from
);
1495 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1497 // Create a file under the directory
1498 FilePath file_name_from
=
1499 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1500 const std::wstring
file_contents(L
"Gooooooooooooooooooooogle");
1501 CreateTextFile(file_name_from
, file_contents
);
1502 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1505 FilePath dest_file
= dir_name_from
.Append(FILE_PATH_LITERAL("DestFile.txt"));
1506 ASSERT_TRUE(file_util::CopyFile(file_name_from
, dest_file
));
1508 // Copy the file to another location using '..' in the path.
1509 FilePath
dest_file2(dir_name_from
);
1510 dest_file2
= dest_file2
.AppendASCII("..");
1511 dest_file2
= dest_file2
.AppendASCII("DestFile.txt");
1512 ASSERT_FALSE(file_util::CopyFile(file_name_from
, dest_file2
));
1513 ASSERT_TRUE(file_util::CopyFileUnsafe(file_name_from
, dest_file2
));
1515 FilePath
dest_file2_test(dir_name_from
);
1516 dest_file2_test
= dest_file2_test
.DirName();
1517 dest_file2_test
= dest_file2_test
.AppendASCII("DestFile.txt");
1519 // Check everything has been copied.
1520 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1521 EXPECT_TRUE(file_util::PathExists(dest_file
));
1522 const std::wstring read_contents
= ReadTextFile(dest_file
);
1523 EXPECT_EQ(file_contents
, read_contents
);
1524 EXPECT_TRUE(file_util::PathExists(dest_file2_test
));
1525 EXPECT_TRUE(file_util::PathExists(dest_file2
));
1528 // file_util winds up using autoreleased objects on the Mac, so this needs
1529 // to be a PlatformTest.
1530 typedef PlatformTest ReadOnlyFileUtilTest
;
1532 TEST_F(ReadOnlyFileUtilTest
, ContentsEqual
) {
1534 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA
, &data_dir
));
1535 data_dir
= data_dir
.AppendASCII("file_util");
1536 ASSERT_TRUE(file_util::PathExists(data_dir
));
1538 FilePath original_file
=
1539 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1540 FilePath same_file
=
1541 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1542 FilePath same_length_file
=
1543 data_dir
.Append(FILE_PATH_LITERAL("same_length.txt"));
1544 FilePath different_file
=
1545 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1546 FilePath different_first_file
=
1547 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1548 FilePath different_last_file
=
1549 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1550 FilePath empty1_file
=
1551 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1552 FilePath empty2_file
=
1553 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1554 FilePath shortened_file
=
1555 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1556 FilePath binary_file
=
1557 data_dir
.Append(FILE_PATH_LITERAL("binary_file.bin"));
1558 FilePath binary_file_same
=
1559 data_dir
.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1560 FilePath binary_file_diff
=
1561 data_dir
.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
1563 EXPECT_TRUE(file_util::ContentsEqual(original_file
, original_file
));
1564 EXPECT_TRUE(file_util::ContentsEqual(original_file
, same_file
));
1565 EXPECT_FALSE(file_util::ContentsEqual(original_file
, same_length_file
));
1566 EXPECT_FALSE(file_util::ContentsEqual(original_file
, different_file
));
1567 EXPECT_FALSE(file_util::ContentsEqual(
1568 FilePath(FILE_PATH_LITERAL("bogusname")),
1569 FilePath(FILE_PATH_LITERAL("bogusname"))));
1570 EXPECT_FALSE(file_util::ContentsEqual(original_file
, different_first_file
));
1571 EXPECT_FALSE(file_util::ContentsEqual(original_file
, different_last_file
));
1572 EXPECT_TRUE(file_util::ContentsEqual(empty1_file
, empty2_file
));
1573 EXPECT_FALSE(file_util::ContentsEqual(original_file
, shortened_file
));
1574 EXPECT_FALSE(file_util::ContentsEqual(shortened_file
, original_file
));
1575 EXPECT_TRUE(file_util::ContentsEqual(binary_file
, binary_file_same
));
1576 EXPECT_FALSE(file_util::ContentsEqual(binary_file
, binary_file_diff
));
1579 TEST_F(ReadOnlyFileUtilTest
, TextContentsEqual
) {
1581 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA
, &data_dir
));
1582 data_dir
= data_dir
.AppendASCII("file_util");
1583 ASSERT_TRUE(file_util::PathExists(data_dir
));
1585 FilePath original_file
=
1586 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1587 FilePath same_file
=
1588 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1589 FilePath crlf_file
=
1590 data_dir
.Append(FILE_PATH_LITERAL("crlf.txt"));
1591 FilePath shortened_file
=
1592 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1593 FilePath different_file
=
1594 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1595 FilePath different_first_file
=
1596 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1597 FilePath different_last_file
=
1598 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1599 FilePath first1_file
=
1600 data_dir
.Append(FILE_PATH_LITERAL("first1.txt"));
1601 FilePath first2_file
=
1602 data_dir
.Append(FILE_PATH_LITERAL("first2.txt"));
1603 FilePath empty1_file
=
1604 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1605 FilePath empty2_file
=
1606 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1607 FilePath blank_line_file
=
1608 data_dir
.Append(FILE_PATH_LITERAL("blank_line.txt"));
1609 FilePath blank_line_crlf_file
=
1610 data_dir
.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1612 EXPECT_TRUE(file_util::TextContentsEqual(original_file
, same_file
));
1613 EXPECT_TRUE(file_util::TextContentsEqual(original_file
, crlf_file
));
1614 EXPECT_FALSE(file_util::TextContentsEqual(original_file
, shortened_file
));
1615 EXPECT_FALSE(file_util::TextContentsEqual(original_file
, different_file
));
1616 EXPECT_FALSE(file_util::TextContentsEqual(original_file
,
1617 different_first_file
));
1618 EXPECT_FALSE(file_util::TextContentsEqual(original_file
,
1619 different_last_file
));
1620 EXPECT_FALSE(file_util::TextContentsEqual(first1_file
, first2_file
));
1621 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file
, empty2_file
));
1622 EXPECT_FALSE(file_util::TextContentsEqual(original_file
, empty1_file
));
1623 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file
,
1624 blank_line_crlf_file
));
1627 // We don't need equivalent functionality outside of Windows.
1629 TEST_F(FileUtilTest
, CopyAndDeleteDirectoryTest
) {
1630 // Create a directory
1631 FilePath dir_name_from
=
1632 temp_dir_
.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1633 file_util::CreateDirectory(dir_name_from
);
1634 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1636 // Create a file under the directory
1637 FilePath file_name_from
=
1638 dir_name_from
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1639 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1640 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1642 // Move the directory by using CopyAndDeleteDirectory
1643 FilePath dir_name_to
= temp_dir_
.path().Append(
1644 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1645 FilePath file_name_to
=
1646 dir_name_to
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1648 ASSERT_FALSE(file_util::PathExists(dir_name_to
));
1650 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from
, dir_name_to
));
1652 // Check everything has been moved.
1653 EXPECT_FALSE(file_util::PathExists(dir_name_from
));
1654 EXPECT_FALSE(file_util::PathExists(file_name_from
));
1655 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1656 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1659 TEST_F(FileUtilTest
, GetTempDirTest
) {
1660 static const TCHAR
* kTmpKey
= _T("TMP");
1661 static const TCHAR
* kTmpValues
[] = {
1662 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1664 // Save the original $TMP.
1665 size_t original_tmp_size
;
1666 TCHAR
* original_tmp
;
1667 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp
, &original_tmp_size
, kTmpKey
));
1668 // original_tmp may be NULL.
1670 for (unsigned int i
= 0; i
< arraysize(kTmpValues
); ++i
) {
1672 ::_tputenv_s(kTmpKey
, kTmpValues
[i
]);
1673 file_util::GetTempDir(&path
);
1674 EXPECT_TRUE(path
.IsAbsolute()) << "$TMP=" << kTmpValues
[i
] <<
1675 " result=" << path
.value();
1678 // Restore the original $TMP.
1680 ::_tputenv_s(kTmpKey
, original_tmp
);
1683 ::_tputenv_s(kTmpKey
, _T(""));
1688 TEST_F(FileUtilTest
, CreateTemporaryFileTest
) {
1689 FilePath temp_files
[3];
1690 for (int i
= 0; i
< 3; i
++) {
1691 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files
[i
])));
1692 EXPECT_TRUE(file_util::PathExists(temp_files
[i
]));
1693 EXPECT_FALSE(file_util::DirectoryExists(temp_files
[i
]));
1695 for (int i
= 0; i
< 3; i
++)
1696 EXPECT_FALSE(temp_files
[i
] == temp_files
[(i
+1)%3]);
1697 for (int i
= 0; i
< 3; i
++)
1698 EXPECT_TRUE(file_util::Delete(temp_files
[i
], false));
1701 TEST_F(FileUtilTest
, CreateAndOpenTemporaryFileTest
) {
1706 // Create; make sure they are open and exist.
1707 for (i
= 0; i
< 3; ++i
) {
1708 fps
[i
] = file_util::CreateAndOpenTemporaryFile(&(names
[i
]));
1709 ASSERT_TRUE(fps
[i
]);
1710 EXPECT_TRUE(file_util::PathExists(names
[i
]));
1713 // Make sure all names are unique.
1714 for (i
= 0; i
< 3; ++i
) {
1715 EXPECT_FALSE(names
[i
] == names
[(i
+1)%3]);
1718 // Close and delete.
1719 for (i
= 0; i
< 3; ++i
) {
1720 EXPECT_TRUE(file_util::CloseFile(fps
[i
]));
1721 EXPECT_TRUE(file_util::Delete(names
[i
], false));
1725 TEST_F(FileUtilTest
, CreateNewTempDirectoryTest
) {
1727 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1729 EXPECT_TRUE(file_util::PathExists(temp_dir
));
1730 EXPECT_TRUE(file_util::Delete(temp_dir
, false));
1733 TEST_F(FileUtilTest
, CreateNewTemporaryDirInDirTest
) {
1735 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1737 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1739 EXPECT_TRUE(file_util::PathExists(new_dir
));
1740 EXPECT_TRUE(temp_dir_
.path().IsParent(new_dir
));
1741 EXPECT_TRUE(file_util::Delete(new_dir
, false));
1744 TEST_F(FileUtilTest
, GetShmemTempDirTest
) {
1746 EXPECT_TRUE(file_util::GetShmemTempDir(&dir
, false));
1747 EXPECT_TRUE(file_util::DirectoryExists(dir
));
1750 TEST_F(FileUtilTest
, CreateDirectoryTest
) {
1751 FilePath test_root
=
1752 temp_dir_
.path().Append(FILE_PATH_LITERAL("create_directory_test"));
1754 FilePath test_path
=
1755 test_root
.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
1756 #elif defined(OS_POSIX)
1757 FilePath test_path
=
1758 test_root
.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
1761 EXPECT_FALSE(file_util::PathExists(test_path
));
1762 EXPECT_TRUE(file_util::CreateDirectory(test_path
));
1763 EXPECT_TRUE(file_util::PathExists(test_path
));
1764 // CreateDirectory returns true if the DirectoryExists returns true.
1765 EXPECT_TRUE(file_util::CreateDirectory(test_path
));
1767 // Doesn't work to create it on top of a non-dir
1768 test_path
= test_path
.Append(FILE_PATH_LITERAL("foobar.txt"));
1769 EXPECT_FALSE(file_util::PathExists(test_path
));
1770 CreateTextFile(test_path
, L
"test file");
1771 EXPECT_TRUE(file_util::PathExists(test_path
));
1772 EXPECT_FALSE(file_util::CreateDirectory(test_path
));
1774 EXPECT_TRUE(file_util::Delete(test_root
, true));
1775 EXPECT_FALSE(file_util::PathExists(test_root
));
1776 EXPECT_FALSE(file_util::PathExists(test_path
));
1778 // Verify assumptions made by the Windows implementation:
1779 // 1. The current directory always exists.
1780 // 2. The root directory always exists.
1781 ASSERT_TRUE(file_util::DirectoryExists(
1782 FilePath(FilePath::kCurrentDirectory
)));
1783 FilePath top_level
= test_root
;
1784 while (top_level
!= top_level
.DirName()) {
1785 top_level
= top_level
.DirName();
1787 ASSERT_TRUE(file_util::DirectoryExists(top_level
));
1789 // Given these assumptions hold, it should be safe to
1790 // test that "creating" these directories succeeds.
1791 EXPECT_TRUE(file_util::CreateDirectory(
1792 FilePath(FilePath::kCurrentDirectory
)));
1793 EXPECT_TRUE(file_util::CreateDirectory(top_level
));
1796 FilePath
invalid_drive(FILE_PATH_LITERAL("o:\\"));
1797 FilePath invalid_path
=
1798 invalid_drive
.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1799 if (!file_util::PathExists(invalid_drive
)) {
1800 EXPECT_FALSE(file_util::CreateDirectory(invalid_path
));
1805 TEST_F(FileUtilTest
, DetectDirectoryTest
) {
1806 // Check a directory
1807 FilePath test_root
=
1808 temp_dir_
.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
1809 EXPECT_FALSE(file_util::PathExists(test_root
));
1810 EXPECT_TRUE(file_util::CreateDirectory(test_root
));
1811 EXPECT_TRUE(file_util::PathExists(test_root
));
1812 EXPECT_TRUE(file_util::DirectoryExists(test_root
));
1814 FilePath test_path
=
1815 test_root
.Append(FILE_PATH_LITERAL("foobar.txt"));
1816 EXPECT_FALSE(file_util::PathExists(test_path
));
1817 CreateTextFile(test_path
, L
"test file");
1818 EXPECT_TRUE(file_util::PathExists(test_path
));
1819 EXPECT_FALSE(file_util::DirectoryExists(test_path
));
1820 EXPECT_TRUE(file_util::Delete(test_path
, false));
1822 EXPECT_TRUE(file_util::Delete(test_root
, true));
1825 TEST_F(FileUtilTest
, FileEnumeratorTest
) {
1826 // Test an empty directory.
1827 file_util::FileEnumerator
f0(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1828 EXPECT_EQ(f0
.Next().value(), FILE_PATH_LITERAL(""));
1829 EXPECT_EQ(f0
.Next().value(), FILE_PATH_LITERAL(""));
1831 // Test an empty directory, non-recursively, including "..".
1832 file_util::FileEnumerator
f0_dotdot(temp_dir_
.path(), false,
1833 FILES_AND_DIRECTORIES
| file_util::FileEnumerator::INCLUDE_DOT_DOT
);
1834 EXPECT_EQ(temp_dir_
.path().Append(FILE_PATH_LITERAL("..")).value(),
1835 f0_dotdot
.Next().value());
1836 EXPECT_EQ(FILE_PATH_LITERAL(""),
1837 f0_dotdot
.Next().value());
1839 // create the directories
1840 FilePath dir1
= temp_dir_
.path().Append(FILE_PATH_LITERAL("dir1"));
1841 EXPECT_TRUE(file_util::CreateDirectory(dir1
));
1842 FilePath dir2
= temp_dir_
.path().Append(FILE_PATH_LITERAL("dir2"));
1843 EXPECT_TRUE(file_util::CreateDirectory(dir2
));
1844 FilePath dir2inner
= dir2
.Append(FILE_PATH_LITERAL("inner"));
1845 EXPECT_TRUE(file_util::CreateDirectory(dir2inner
));
1848 FilePath dir2file
= dir2
.Append(FILE_PATH_LITERAL("dir2file.txt"));
1849 CreateTextFile(dir2file
, std::wstring());
1850 FilePath dir2innerfile
= dir2inner
.Append(FILE_PATH_LITERAL("innerfile.txt"));
1851 CreateTextFile(dir2innerfile
, std::wstring());
1852 FilePath file1
= temp_dir_
.path().Append(FILE_PATH_LITERAL("file1.txt"));
1853 CreateTextFile(file1
, std::wstring());
1854 FilePath file2_rel
= dir2
.Append(FilePath::kParentDirectory
)
1855 .Append(FILE_PATH_LITERAL("file2.txt"));
1856 CreateTextFile(file2_rel
, std::wstring());
1857 FilePath file2_abs
= temp_dir_
.path().Append(FILE_PATH_LITERAL("file2.txt"));
1859 // Only enumerate files.
1860 file_util::FileEnumerator
f1(temp_dir_
.path(), true,
1861 file_util::FileEnumerator::FILES
);
1862 FindResultCollector
c1(f1
);
1863 EXPECT_TRUE(c1
.HasFile(file1
));
1864 EXPECT_TRUE(c1
.HasFile(file2_abs
));
1865 EXPECT_TRUE(c1
.HasFile(dir2file
));
1866 EXPECT_TRUE(c1
.HasFile(dir2innerfile
));
1867 EXPECT_EQ(c1
.size(), 4);
1869 // Only enumerate directories.
1870 file_util::FileEnumerator
f2(temp_dir_
.path(), true,
1871 file_util::FileEnumerator::DIRECTORIES
);
1872 FindResultCollector
c2(f2
);
1873 EXPECT_TRUE(c2
.HasFile(dir1
));
1874 EXPECT_TRUE(c2
.HasFile(dir2
));
1875 EXPECT_TRUE(c2
.HasFile(dir2inner
));
1876 EXPECT_EQ(c2
.size(), 3);
1878 // Only enumerate directories non-recursively.
1879 file_util::FileEnumerator
f2_non_recursive(
1880 temp_dir_
.path(), false, file_util::FileEnumerator::DIRECTORIES
);
1881 FindResultCollector
c2_non_recursive(f2_non_recursive
);
1882 EXPECT_TRUE(c2_non_recursive
.HasFile(dir1
));
1883 EXPECT_TRUE(c2_non_recursive
.HasFile(dir2
));
1884 EXPECT_EQ(c2_non_recursive
.size(), 2);
1886 // Only enumerate directories, non-recursively, including "..".
1887 file_util::FileEnumerator
f2_dotdot(temp_dir_
.path(), false,
1888 file_util::FileEnumerator::DIRECTORIES
|
1889 file_util::FileEnumerator::INCLUDE_DOT_DOT
);
1890 FindResultCollector
c2_dotdot(f2_dotdot
);
1891 EXPECT_TRUE(c2_dotdot
.HasFile(dir1
));
1892 EXPECT_TRUE(c2_dotdot
.HasFile(dir2
));
1893 EXPECT_TRUE(c2_dotdot
.HasFile(
1894 temp_dir_
.path().Append(FILE_PATH_LITERAL(".."))));
1895 EXPECT_EQ(c2_dotdot
.size(), 3);
1897 // Enumerate files and directories.
1898 file_util::FileEnumerator
f3(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1899 FindResultCollector
c3(f3
);
1900 EXPECT_TRUE(c3
.HasFile(dir1
));
1901 EXPECT_TRUE(c3
.HasFile(dir2
));
1902 EXPECT_TRUE(c3
.HasFile(file1
));
1903 EXPECT_TRUE(c3
.HasFile(file2_abs
));
1904 EXPECT_TRUE(c3
.HasFile(dir2file
));
1905 EXPECT_TRUE(c3
.HasFile(dir2inner
));
1906 EXPECT_TRUE(c3
.HasFile(dir2innerfile
));
1907 EXPECT_EQ(c3
.size(), 7);
1909 // Non-recursive operation.
1910 file_util::FileEnumerator
f4(temp_dir_
.path(), false, FILES_AND_DIRECTORIES
);
1911 FindResultCollector
c4(f4
);
1912 EXPECT_TRUE(c4
.HasFile(dir2
));
1913 EXPECT_TRUE(c4
.HasFile(dir2
));
1914 EXPECT_TRUE(c4
.HasFile(file1
));
1915 EXPECT_TRUE(c4
.HasFile(file2_abs
));
1916 EXPECT_EQ(c4
.size(), 4);
1918 // Enumerate with a pattern.
1919 file_util::FileEnumerator
f5(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
,
1920 FILE_PATH_LITERAL("dir*"));
1921 FindResultCollector
c5(f5
);
1922 EXPECT_TRUE(c5
.HasFile(dir1
));
1923 EXPECT_TRUE(c5
.HasFile(dir2
));
1924 EXPECT_TRUE(c5
.HasFile(dir2file
));
1925 EXPECT_TRUE(c5
.HasFile(dir2inner
));
1926 EXPECT_TRUE(c5
.HasFile(dir2innerfile
));
1927 EXPECT_EQ(c5
.size(), 5);
1929 // Make sure the destructor closes the find handle while in the middle of a
1930 // query to allow TearDown to delete the directory.
1931 file_util::FileEnumerator
f6(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1932 EXPECT_FALSE(f6
.Next().value().empty()); // Should have found something
1933 // (we don't care what).
1936 TEST_F(FileUtilTest
, AppendToFile
) {
1938 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1940 // Create a fresh, empty copy of this directory.
1941 if (file_util::PathExists(data_dir
)) {
1942 ASSERT_TRUE(file_util::Delete(data_dir
, true));
1944 ASSERT_TRUE(file_util::CreateDirectory(data_dir
));
1946 // Create a fresh, empty copy of this directory.
1947 if (file_util::PathExists(data_dir
)) {
1948 ASSERT_TRUE(file_util::Delete(data_dir
, true));
1950 ASSERT_TRUE(file_util::CreateDirectory(data_dir
));
1951 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
1953 std::string
data("hello");
1954 EXPECT_EQ(-1, file_util::AppendToFile(foobar
, data
.c_str(), data
.length()));
1955 EXPECT_EQ(static_cast<int>(data
.length()),
1956 file_util::WriteFile(foobar
, data
.c_str(), data
.length()));
1957 EXPECT_EQ(static_cast<int>(data
.length()),
1958 file_util::AppendToFile(foobar
, data
.c_str(), data
.length()));
1960 const std::wstring read_content
= ReadTextFile(foobar
);
1961 EXPECT_EQ(L
"hellohello", read_content
);
1964 TEST_F(FileUtilTest
, TouchFile
) {
1966 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1968 // Create a fresh, empty copy of this directory.
1969 if (file_util::PathExists(data_dir
)) {
1970 ASSERT_TRUE(file_util::Delete(data_dir
, true));
1972 ASSERT_TRUE(file_util::CreateDirectory(data_dir
));
1974 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
1975 std::string
data("hello");
1976 ASSERT_TRUE(file_util::WriteFile(foobar
, data
.c_str(), data
.length()));
1978 base::Time access_time
;
1979 // This timestamp is divisible by one day (in local timezone),
1980 // to make it work on FAT too.
1981 ASSERT_TRUE(base::Time::FromString("Wed, 16 Nov 1994, 00:00:00",
1984 base::Time modification_time
;
1985 // Note that this timestamp is divisible by two (seconds) - FAT stores
1986 // modification times with 2s resolution.
1987 ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
1988 &modification_time
));
1990 ASSERT_TRUE(file_util::TouchFile(foobar
, access_time
, modification_time
));
1991 base::PlatformFileInfo file_info
;
1992 ASSERT_TRUE(file_util::GetFileInfo(foobar
, &file_info
));
1993 EXPECT_EQ(file_info
.last_accessed
.ToInternalValue(),
1994 access_time
.ToInternalValue());
1995 EXPECT_EQ(file_info
.last_modified
.ToInternalValue(),
1996 modification_time
.ToInternalValue());
1999 TEST_F(FileUtilTest
, IsDirectoryEmpty
) {
2000 FilePath empty_dir
= temp_dir_
.path().Append(FILE_PATH_LITERAL("EmptyDir"));
2002 ASSERT_FALSE(file_util::PathExists(empty_dir
));
2004 ASSERT_TRUE(file_util::CreateDirectory(empty_dir
));
2006 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir
));
2008 FilePath
foo(empty_dir
.Append(FILE_PATH_LITERAL("foo.txt")));
2009 std::string
bar("baz");
2010 ASSERT_TRUE(file_util::WriteFile(foo
, bar
.c_str(), bar
.length()));
2012 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir
));
2015 #if defined(OS_POSIX)
2017 // Testing VerifyPathControlledByAdmin() is hard, because there is no
2018 // way a test can make a file owned by root, or change file paths
2019 // at the root of the file system. VerifyPathControlledByAdmin()
2020 // is implemented as a call to VerifyPathControlledByUser, which gives
2021 // us the ability to test with paths under the test's temp directory,
2022 // using a user id we control.
2023 // Pull tests of VerifyPathControlledByUserTest() into a separate test class
2024 // with a common SetUp() method.
2025 class VerifyPathControlledByUserTest
: public FileUtilTest
{
2027 virtual void SetUp() OVERRIDE
{
2028 FileUtilTest::SetUp();
2030 // Create a basic structure used by each test.
2035 base_dir_
= temp_dir_
.path().AppendASCII("base_dir");
2036 ASSERT_TRUE(file_util::CreateDirectory(base_dir_
));
2038 sub_dir_
= base_dir_
.AppendASCII("sub_dir");
2039 ASSERT_TRUE(file_util::CreateDirectory(sub_dir_
));
2041 text_file_
= sub_dir_
.AppendASCII("file.txt");
2042 CreateTextFile(text_file_
, L
"This text file has some text in it.");
2044 // Get the user and group files are created with from |base_dir_|.
2045 struct stat stat_buf
;
2046 ASSERT_EQ(0, stat(base_dir_
.value().c_str(), &stat_buf
));
2047 uid_
= stat_buf
.st_uid
;
2048 ok_gids_
.insert(stat_buf
.st_gid
);
2049 bad_gids_
.insert(stat_buf
.st_gid
+ 1);
2051 ASSERT_EQ(uid_
, getuid()); // This process should be the owner.
2053 // To ensure that umask settings do not cause the initial state
2054 // of permissions to be different from what we expect, explicitly
2055 // set permissions on the directories we create.
2056 // Make all files and directories non-world-writable.
2058 // Users and group can read, write, traverse
2059 int enabled_permissions
=
2060 file_util::FILE_PERMISSION_USER_MASK
|
2061 file_util::FILE_PERMISSION_GROUP_MASK
;
2062 // Other users can't read, write, traverse
2063 int disabled_permissions
=
2064 file_util::FILE_PERMISSION_OTHERS_MASK
;
2066 ASSERT_NO_FATAL_FAILURE(
2067 ChangePosixFilePermissions(
2068 base_dir_
, enabled_permissions
, disabled_permissions
));
2069 ASSERT_NO_FATAL_FAILURE(
2070 ChangePosixFilePermissions(
2071 sub_dir_
, enabled_permissions
, disabled_permissions
));
2076 FilePath text_file_
;
2079 std::set
<gid_t
> ok_gids_
;
2080 std::set
<gid_t
> bad_gids_
;
2083 TEST_F(VerifyPathControlledByUserTest
, BadPaths
) {
2084 // File does not exist.
2085 FilePath does_not_exist
= base_dir_
.AppendASCII("does")
2087 .AppendASCII("exist");
2089 file_util::VerifyPathControlledByUser(
2090 base_dir_
, does_not_exist
, uid_
, ok_gids_
));
2092 // |base| not a subpath of |path|.
2094 file_util::VerifyPathControlledByUser(
2095 sub_dir_
, base_dir_
, uid_
, ok_gids_
));
2097 // An empty base path will fail to be a prefix for any path.
2100 file_util::VerifyPathControlledByUser(
2101 empty
, base_dir_
, uid_
, ok_gids_
));
2103 // Finding that a bad call fails proves nothing unless a good call succeeds.
2105 file_util::VerifyPathControlledByUser(
2106 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2109 TEST_F(VerifyPathControlledByUserTest
, Symlinks
) {
2110 // Symlinks in the path should cause failure.
2112 // Symlink to the file at the end of the path.
2113 FilePath file_link
= base_dir_
.AppendASCII("file_link");
2114 ASSERT_TRUE(file_util::CreateSymbolicLink(text_file_
, file_link
))
2115 << "Failed to create symlink.";
2118 file_util::VerifyPathControlledByUser(
2119 base_dir_
, file_link
, uid_
, ok_gids_
));
2121 file_util::VerifyPathControlledByUser(
2122 file_link
, file_link
, uid_
, ok_gids_
));
2124 // Symlink from one directory to another within the path.
2125 FilePath link_to_sub_dir
= base_dir_
.AppendASCII("link_to_sub_dir");
2126 ASSERT_TRUE(file_util::CreateSymbolicLink(sub_dir_
, link_to_sub_dir
))
2127 << "Failed to create symlink.";
2129 FilePath file_path_with_link
= link_to_sub_dir
.AppendASCII("file.txt");
2130 ASSERT_TRUE(file_util::PathExists(file_path_with_link
));
2133 file_util::VerifyPathControlledByUser(
2134 base_dir_
, file_path_with_link
, uid_
, ok_gids_
));
2137 file_util::VerifyPathControlledByUser(
2138 link_to_sub_dir
, file_path_with_link
, uid_
, ok_gids_
));
2140 // Symlinks in parents of base path are allowed.
2142 file_util::VerifyPathControlledByUser(
2143 file_path_with_link
, file_path_with_link
, uid_
, ok_gids_
));
2146 TEST_F(VerifyPathControlledByUserTest
, OwnershipChecks
) {
2147 // Get a uid that is not the uid of files we create.
2148 uid_t bad_uid
= uid_
+ 1;
2150 // Make all files and directories non-world-writable.
2151 ASSERT_NO_FATAL_FAILURE(
2152 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2153 ASSERT_NO_FATAL_FAILURE(
2154 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2155 ASSERT_NO_FATAL_FAILURE(
2156 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2158 // We control these paths.
2160 file_util::VerifyPathControlledByUser(
2161 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2163 file_util::VerifyPathControlledByUser(
2164 base_dir_
, text_file_
, uid_
, ok_gids_
));
2166 file_util::VerifyPathControlledByUser(
2167 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2169 // Another user does not control these paths.
2171 file_util::VerifyPathControlledByUser(
2172 base_dir_
, sub_dir_
, bad_uid
, ok_gids_
));
2174 file_util::VerifyPathControlledByUser(
2175 base_dir_
, text_file_
, bad_uid
, ok_gids_
));
2177 file_util::VerifyPathControlledByUser(
2178 sub_dir_
, text_file_
, bad_uid
, ok_gids_
));
2180 // Another group does not control the paths.
2182 file_util::VerifyPathControlledByUser(
2183 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2185 file_util::VerifyPathControlledByUser(
2186 base_dir_
, text_file_
, uid_
, bad_gids_
));
2188 file_util::VerifyPathControlledByUser(
2189 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2192 TEST_F(VerifyPathControlledByUserTest
, GroupWriteTest
) {
2193 // Make all files and directories writable only by their owner.
2194 ASSERT_NO_FATAL_FAILURE(
2195 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2196 ASSERT_NO_FATAL_FAILURE(
2197 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2198 ASSERT_NO_FATAL_FAILURE(
2199 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
|S_IWGRP
));
2201 // Any group is okay because the path is not group-writable.
2203 file_util::VerifyPathControlledByUser(
2204 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2206 file_util::VerifyPathControlledByUser(
2207 base_dir_
, text_file_
, uid_
, ok_gids_
));
2209 file_util::VerifyPathControlledByUser(
2210 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2213 file_util::VerifyPathControlledByUser(
2214 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2216 file_util::VerifyPathControlledByUser(
2217 base_dir_
, text_file_
, uid_
, bad_gids_
));
2219 file_util::VerifyPathControlledByUser(
2220 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2222 // No group is okay, because we don't check the group
2223 // if no group can write.
2224 std::set
<gid_t
> no_gids
; // Empty set of gids.
2226 file_util::VerifyPathControlledByUser(
2227 base_dir_
, sub_dir_
, uid_
, no_gids
));
2229 file_util::VerifyPathControlledByUser(
2230 base_dir_
, text_file_
, uid_
, no_gids
));
2232 file_util::VerifyPathControlledByUser(
2233 sub_dir_
, text_file_
, uid_
, no_gids
));
2236 // Make all files and directories writable by their group.
2237 ASSERT_NO_FATAL_FAILURE(
2238 ChangePosixFilePermissions(base_dir_
, S_IWGRP
, 0u));
2239 ASSERT_NO_FATAL_FAILURE(
2240 ChangePosixFilePermissions(sub_dir_
, S_IWGRP
, 0u));
2241 ASSERT_NO_FATAL_FAILURE(
2242 ChangePosixFilePermissions(text_file_
, S_IWGRP
, 0u));
2244 // Now |ok_gids_| works, but |bad_gids_| fails.
2246 file_util::VerifyPathControlledByUser(
2247 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2249 file_util::VerifyPathControlledByUser(
2250 base_dir_
, text_file_
, uid_
, ok_gids_
));
2252 file_util::VerifyPathControlledByUser(
2253 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2256 file_util::VerifyPathControlledByUser(
2257 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2259 file_util::VerifyPathControlledByUser(
2260 base_dir_
, text_file_
, uid_
, bad_gids_
));
2262 file_util::VerifyPathControlledByUser(
2263 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2265 // Because any group in the group set is allowed,
2266 // the union of good and bad gids passes.
2268 std::set
<gid_t
> multiple_gids
;
2270 ok_gids_
.begin(), ok_gids_
.end(),
2271 bad_gids_
.begin(), bad_gids_
.end(),
2272 std::inserter(multiple_gids
, multiple_gids
.begin()));
2275 file_util::VerifyPathControlledByUser(
2276 base_dir_
, sub_dir_
, uid_
, multiple_gids
));
2278 file_util::VerifyPathControlledByUser(
2279 base_dir_
, text_file_
, uid_
, multiple_gids
));
2281 file_util::VerifyPathControlledByUser(
2282 sub_dir_
, text_file_
, uid_
, multiple_gids
));
2285 TEST_F(VerifyPathControlledByUserTest
, WriteBitChecks
) {
2286 // Make all files and directories non-world-writable.
2287 ASSERT_NO_FATAL_FAILURE(
2288 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2289 ASSERT_NO_FATAL_FAILURE(
2290 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2291 ASSERT_NO_FATAL_FAILURE(
2292 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2294 // Initialy, we control all parts of the path.
2296 file_util::VerifyPathControlledByUser(
2297 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2299 file_util::VerifyPathControlledByUser(
2300 base_dir_
, text_file_
, uid_
, ok_gids_
));
2302 file_util::VerifyPathControlledByUser(
2303 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2305 // Make base_dir_ world-writable.
2306 ASSERT_NO_FATAL_FAILURE(
2307 ChangePosixFilePermissions(base_dir_
, S_IWOTH
, 0u));
2309 file_util::VerifyPathControlledByUser(
2310 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2312 file_util::VerifyPathControlledByUser(
2313 base_dir_
, text_file_
, uid_
, ok_gids_
));
2315 file_util::VerifyPathControlledByUser(
2316 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2318 // Make sub_dir_ world writable.
2319 ASSERT_NO_FATAL_FAILURE(
2320 ChangePosixFilePermissions(sub_dir_
, S_IWOTH
, 0u));
2322 file_util::VerifyPathControlledByUser(
2323 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2325 file_util::VerifyPathControlledByUser(
2326 base_dir_
, text_file_
, uid_
, ok_gids_
));
2328 file_util::VerifyPathControlledByUser(
2329 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2331 // Make text_file_ world writable.
2332 ASSERT_NO_FATAL_FAILURE(
2333 ChangePosixFilePermissions(text_file_
, S_IWOTH
, 0u));
2335 file_util::VerifyPathControlledByUser(
2336 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2338 file_util::VerifyPathControlledByUser(
2339 base_dir_
, text_file_
, uid_
, ok_gids_
));
2341 file_util::VerifyPathControlledByUser(
2342 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2344 // Make sub_dir_ non-world writable.
2345 ASSERT_NO_FATAL_FAILURE(
2346 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2348 file_util::VerifyPathControlledByUser(
2349 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2351 file_util::VerifyPathControlledByUser(
2352 base_dir_
, text_file_
, uid_
, ok_gids_
));
2354 file_util::VerifyPathControlledByUser(
2355 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2357 // Make base_dir_ non-world-writable.
2358 ASSERT_NO_FATAL_FAILURE(
2359 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2361 file_util::VerifyPathControlledByUser(
2362 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2364 file_util::VerifyPathControlledByUser(
2365 base_dir_
, text_file_
, uid_
, ok_gids_
));
2367 file_util::VerifyPathControlledByUser(
2368 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2370 // Back to the initial state: Nothing is writable, so every path
2372 ASSERT_NO_FATAL_FAILURE(
2373 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2375 file_util::VerifyPathControlledByUser(
2376 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2378 file_util::VerifyPathControlledByUser(
2379 base_dir_
, text_file_
, uid_
, ok_gids_
));
2381 file_util::VerifyPathControlledByUser(
2382 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2385 #endif // defined(OS_POSIX)