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_enumerator.h"
22 #include "base/files/file_path.h"
23 #include "base/files/scoped_temp_dir.h"
24 #include "base/path_service.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/test/test_file_util.h"
27 #include "base/threading/platform_thread.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "testing/platform_test.h"
32 #include "base/win/scoped_handle.h"
33 #include "base/win/windows_version.h"
36 // This macro helps avoid wrapped lines in the test structs.
37 #define FPL(x) FILE_PATH_LITERAL(x)
39 using base::DirectoryExists
;
40 using base::FileEnumerator
;
42 using base::PathIsWritable
;
43 using base::TextContentsEqual
;
47 // To test that file_util::Normalize FilePath() deals with NTFS reparse points
48 // correctly, we need functions to create and delete reparse points.
50 typedef struct _REPARSE_DATA_BUFFER
{
52 USHORT ReparseDataLength
;
56 USHORT SubstituteNameOffset
;
57 USHORT SubstituteNameLength
;
58 USHORT PrintNameOffset
;
59 USHORT PrintNameLength
;
62 } SymbolicLinkReparseBuffer
;
64 USHORT SubstituteNameOffset
;
65 USHORT SubstituteNameLength
;
66 USHORT PrintNameOffset
;
67 USHORT PrintNameLength
;
69 } MountPointReparseBuffer
;
72 } GenericReparseBuffer
;
74 } REPARSE_DATA_BUFFER
, *PREPARSE_DATA_BUFFER
;
76 // Sets a reparse point. |source| will now point to |target|. Returns true if
77 // the call succeeds, false otherwise.
78 bool SetReparsePoint(HANDLE source
, const FilePath
& target_path
) {
79 std::wstring kPathPrefix
= L
"\\??\\";
80 std::wstring target_str
;
81 // The juction will not work if the target path does not start with \??\ .
82 if (kPathPrefix
!= target_path
.value().substr(0, kPathPrefix
.size()))
83 target_str
+= kPathPrefix
;
84 target_str
+= target_path
.value();
85 const wchar_t* target
= target_str
.c_str();
86 USHORT size_target
= static_cast<USHORT
>(wcslen(target
)) * sizeof(target
[0]);
87 char buffer
[2000] = {0};
90 REPARSE_DATA_BUFFER
* data
= reinterpret_cast<REPARSE_DATA_BUFFER
*>(buffer
);
92 data
->ReparseTag
= 0xa0000003;
93 memcpy(data
->MountPointReparseBuffer
.PathBuffer
, target
, size_target
+ 2);
95 data
->MountPointReparseBuffer
.SubstituteNameLength
= size_target
;
96 data
->MountPointReparseBuffer
.PrintNameOffset
= size_target
+ 2;
97 data
->ReparseDataLength
= size_target
+ 4 + 8;
99 int data_size
= data
->ReparseDataLength
+ 8;
101 if (!DeviceIoControl(source
, FSCTL_SET_REPARSE_POINT
, &buffer
, data_size
,
102 NULL
, 0, &returned
, NULL
)) {
108 // Delete the reparse point referenced by |source|. Returns true if the call
109 // succeeds, false otherwise.
110 bool DeleteReparsePoint(HANDLE source
) {
112 REPARSE_DATA_BUFFER data
= {0};
113 data
.ReparseTag
= 0xa0000003;
114 if (!DeviceIoControl(source
, FSCTL_DELETE_REPARSE_POINT
, &data
, 8, NULL
, 0,
121 // Manages a reparse point for a test.
124 // Creates a reparse point from |source| (an empty directory) to |target|.
125 ReparsePoint(const FilePath
& source
, const FilePath
& target
) {
127 ::CreateFile(source
.value().c_str(),
129 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
132 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
134 created_
= dir_
.IsValid() && SetReparsePoint(dir_
, target
);
139 DeleteReparsePoint(dir_
);
142 bool IsValid() { return created_
; }
145 base::win::ScopedHandle dir_
;
147 DISALLOW_COPY_AND_ASSIGN(ReparsePoint
);
152 #if defined(OS_POSIX)
153 // Provide a simple way to change the permissions bits on |path| in tests.
154 // ASSERT failures will return, but not stop the test. Caller should wrap
155 // calls to this function in ASSERT_NO_FATAL_FAILURE().
156 void ChangePosixFilePermissions(const FilePath
& path
,
157 int mode_bits_to_set
,
158 int mode_bits_to_clear
) {
159 ASSERT_FALSE(mode_bits_to_set
& mode_bits_to_clear
)
160 << "Can't set and clear the same bits.";
163 ASSERT_TRUE(file_util::GetPosixFilePermissions(path
, &mode
));
164 mode
|= mode_bits_to_set
;
165 mode
&= ~mode_bits_to_clear
;
166 ASSERT_TRUE(file_util::SetPosixFilePermissions(path
, mode
));
168 #endif // defined(OS_POSIX)
170 const wchar_t bogus_content
[] = L
"I'm cannon fodder.";
172 const int FILES_AND_DIRECTORIES
=
173 FileEnumerator::FILES
| FileEnumerator::DIRECTORIES
;
175 // file_util winds up using autoreleased objects on the Mac, so this needs
176 // to be a PlatformTest
177 class FileUtilTest
: public PlatformTest
{
179 virtual void SetUp() OVERRIDE
{
180 PlatformTest::SetUp();
181 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
184 base::ScopedTempDir temp_dir_
;
187 // Collects all the results from the given file enumerator, and provides an
188 // interface to query whether a given file is present.
189 class FindResultCollector
{
191 explicit FindResultCollector(FileEnumerator
& enumerator
) {
193 while (!(cur_file
= enumerator
.Next()).value().empty()) {
194 FilePath::StringType path
= cur_file
.value();
195 // The file should not be returned twice.
196 EXPECT_TRUE(files_
.end() == files_
.find(path
))
197 << "Same file returned twice";
204 // Returns true if the enumerator found the file.
205 bool HasFile(const FilePath
& file
) const {
206 return files_
.find(file
.value()) != files_
.end();
210 return static_cast<int>(files_
.size());
214 std::set
<FilePath::StringType
> files_
;
217 // Simple function to dump some text into a new file.
218 void CreateTextFile(const FilePath
& filename
,
219 const std::wstring
& contents
) {
221 file
.open(filename
.value().c_str());
222 ASSERT_TRUE(file
.is_open());
227 // Simple function to take out some text from a file.
228 std::wstring
ReadTextFile(const FilePath
& filename
) {
229 wchar_t contents
[64];
231 file
.open(filename
.value().c_str());
232 EXPECT_TRUE(file
.is_open());
233 file
.getline(contents
, arraysize(contents
));
235 return std::wstring(contents
);
239 uint64
FileTimeAsUint64(const FILETIME
& ft
) {
241 u
.LowPart
= ft
.dwLowDateTime
;
242 u
.HighPart
= ft
.dwHighDateTime
;
247 const struct append_case
{
249 const wchar_t* ending
;
250 const wchar_t* result
;
253 {L
"c:\\colon\\backslash", L
"path", L
"c:\\colon\\backslash\\path"},
254 {L
"c:\\colon\\backslash\\", L
"path", L
"c:\\colon\\backslash\\path"},
255 {L
"c:\\colon\\backslash\\\\", L
"path", L
"c:\\colon\\backslash\\\\path"},
256 {L
"c:\\colon\\backslash\\", L
"", L
"c:\\colon\\backslash\\"},
257 {L
"c:\\colon\\backslash", L
"", L
"c:\\colon\\backslash\\"},
258 {L
"", L
"path", L
"\\path"},
260 #elif defined(OS_POSIX)
261 {L
"/foo/bar", L
"path", L
"/foo/bar/path"},
262 {L
"/foo/bar/", L
"path", L
"/foo/bar/path"},
263 {L
"/foo/bar//", L
"path", L
"/foo/bar//path"},
264 {L
"/foo/bar/", L
"", L
"/foo/bar/"},
265 {L
"/foo/bar", L
"", L
"/foo/bar/"},
266 {L
"", L
"path", L
"/path"},
271 static const struct filename_case
{
273 const wchar_t* filename
;
274 } filename_cases
[] = {
276 {L
"c:\\colon\\backslash", L
"backslash"},
277 {L
"c:\\colon\\backslash\\", L
""},
278 {L
"\\\\filename.exe", L
"filename.exe"},
279 {L
"filename.exe", L
"filename.exe"},
282 {L
"c:/colon/backslash", L
"backslash"},
283 {L
"c:/colon/backslash/", L
""},
285 {L
"///filename.exe", L
"filename.exe"},
286 #elif defined(OS_POSIX)
287 {L
"/foo/bar", L
"bar"},
289 {L
"/filename.exe", L
"filename.exe"},
290 {L
"filename.exe", L
"filename.exe"},
296 // Test finding the file type from a path name
297 static const struct extension_case
{
299 const wchar_t* extension
;
300 } extension_cases
[] = {
302 {L
"C:\\colon\\backslash\\filename.extension", L
"extension"},
303 {L
"C:\\colon\\backslash\\filename.", L
""},
304 {L
"C:\\colon\\backslash\\filename", L
""},
305 {L
"C:\\colon\\backslash\\", L
""},
306 {L
"C:\\colon\\backslash.\\", L
""},
307 {L
"C:\\colon\\backslash\filename.extension.extension2", L
"extension2"},
308 #elif defined(OS_POSIX)
309 {L
"/foo/bar/filename.extension", L
"extension"},
310 {L
"/foo/bar/filename.", L
""},
311 {L
"/foo/bar/filename", L
""},
313 {L
"/foo/bar./", L
""},
314 {L
"/foo/bar/filename.extension.extension2", L
"extension2"},
318 {L
"./foo.extension", L
"extension"},
319 {L
"/foo.extension1/bar.extension2", L
"extension2"},
323 // Test finding the directory component of a path
324 static const struct dir_case
{
325 const wchar_t* full_path
;
326 const wchar_t* directory
;
329 {L
"C:\\WINDOWS\\system32\\gdi32.dll", L
"C:\\WINDOWS\\system32"},
330 {L
"C:\\WINDOWS\\system32\\not_exist_thx_1138", L
"C:\\WINDOWS\\system32"},
331 {L
"C:\\WINDOWS\\system32\\", L
"C:\\WINDOWS\\system32"},
332 {L
"C:\\WINDOWS\\system32\\\\", L
"C:\\WINDOWS\\system32"},
333 {L
"C:\\WINDOWS\\system32", L
"C:\\WINDOWS"},
334 {L
"C:\\WINDOWS\\system32.\\", L
"C:\\WINDOWS\\system32."},
336 #elif defined(OS_POSIX)
337 {L
"/foo/bar/gdi32.dll", L
"/foo/bar"},
338 {L
"/foo/bar/not_exist_thx_1138", L
"/foo/bar"},
339 {L
"/foo/bar/", L
"/foo/bar"},
340 {L
"/foo/bar//", L
"/foo/bar"},
341 {L
"/foo/bar", L
"/foo"},
342 {L
"/foo/bar./", L
"/foo/bar."},
345 {L
"..", L
"."}, // yes, ".." technically lives in "."
349 TEST_F(FileUtilTest
, FileAndDirectorySize
) {
350 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
351 // should return 53 bytes.
352 FilePath file_01
= temp_dir_
.path().Append(FPL("The file 01.txt"));
353 CreateTextFile(file_01
, L
"12345678901234567890");
355 ASSERT_TRUE(file_util::GetFileSize(file_01
, &size_f1
));
356 EXPECT_EQ(20ll, size_f1
);
358 FilePath subdir_path
= temp_dir_
.path().Append(FPL("Level2"));
359 file_util::CreateDirectory(subdir_path
);
361 FilePath file_02
= subdir_path
.Append(FPL("The file 02.txt"));
362 CreateTextFile(file_02
, L
"123456789012345678901234567890");
364 ASSERT_TRUE(file_util::GetFileSize(file_02
, &size_f2
));
365 EXPECT_EQ(30ll, size_f2
);
367 FilePath subsubdir_path
= subdir_path
.Append(FPL("Level3"));
368 file_util::CreateDirectory(subsubdir_path
);
370 FilePath file_03
= subsubdir_path
.Append(FPL("The file 03.txt"));
371 CreateTextFile(file_03
, L
"123");
373 int64 computed_size
= base::ComputeDirectorySize(temp_dir_
.path());
374 EXPECT_EQ(size_f1
+ size_f2
+ 3, computed_size
);
377 TEST_F(FileUtilTest
, NormalizeFilePathBasic
) {
378 // Create a directory under the test dir. Because we create it,
379 // we know it is not a link.
380 FilePath file_a_path
= temp_dir_
.path().Append(FPL("file_a"));
381 FilePath dir_path
= temp_dir_
.path().Append(FPL("dir"));
382 FilePath file_b_path
= dir_path
.Append(FPL("file_b"));
383 file_util::CreateDirectory(dir_path
);
385 FilePath normalized_file_a_path
, normalized_file_b_path
;
386 ASSERT_FALSE(base::PathExists(file_a_path
));
387 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path
,
388 &normalized_file_a_path
))
389 << "NormalizeFilePath() should fail on nonexistent paths.";
391 CreateTextFile(file_a_path
, bogus_content
);
392 ASSERT_TRUE(base::PathExists(file_a_path
));
393 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path
,
394 &normalized_file_a_path
));
396 CreateTextFile(file_b_path
, bogus_content
);
397 ASSERT_TRUE(base::PathExists(file_b_path
));
398 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path
,
399 &normalized_file_b_path
));
401 // Beacuse this test created |dir_path|, we know it is not a link
402 // or junction. So, the real path of the directory holding file a
403 // must be the parent of the path holding file b.
404 ASSERT_TRUE(normalized_file_a_path
.DirName()
405 .IsParent(normalized_file_b_path
.DirName()));
410 TEST_F(FileUtilTest
, NormalizeFilePathReparsePoints
) {
411 // Build the following directory structure:
417 // | |-> long_name___... (Very long name.)
421 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
422 // |-> to_base_b (reparse point to temp_dir\base_b)
423 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
425 FilePath base_a
= temp_dir_
.path().Append(FPL("base_a"));
426 ASSERT_TRUE(file_util::CreateDirectory(base_a
));
428 FilePath sub_a
= base_a
.Append(FPL("sub_a"));
429 ASSERT_TRUE(file_util::CreateDirectory(sub_a
));
431 FilePath file_txt
= sub_a
.Append(FPL("file.txt"));
432 CreateTextFile(file_txt
, bogus_content
);
434 // Want a directory whose name is long enough to make the path to the file
435 // inside just under MAX_PATH chars. This will be used to test that when
436 // a junction expands to a path over MAX_PATH chars in length,
437 // NormalizeFilePath() fails without crashing.
438 FilePath
sub_long_rel(FPL("sub_long"));
439 FilePath
deep_txt(FPL("deep.txt"));
441 int target_length
= MAX_PATH
;
442 target_length
-= (sub_a
.value().length() + 1); // +1 for the sepperator '\'.
443 target_length
-= (sub_long_rel
.Append(deep_txt
).value().length() + 1);
444 // Without making the path a bit shorter, CreateDirectory() fails.
445 // the resulting path is still long enough to hit the failing case in
447 const int kCreateDirLimit
= 4;
448 target_length
-= kCreateDirLimit
;
449 FilePath::StringType long_name_str
= FPL("long_name_");
450 long_name_str
.resize(target_length
, '_');
452 FilePath long_name
= sub_a
.Append(FilePath(long_name_str
));
453 FilePath deep_file
= long_name
.Append(sub_long_rel
).Append(deep_txt
);
454 ASSERT_EQ(MAX_PATH
- kCreateDirLimit
, deep_file
.value().length());
456 FilePath sub_long
= deep_file
.DirName();
457 ASSERT_TRUE(file_util::CreateDirectory(sub_long
));
458 CreateTextFile(deep_file
, bogus_content
);
460 FilePath base_b
= temp_dir_
.path().Append(FPL("base_b"));
461 ASSERT_TRUE(file_util::CreateDirectory(base_b
));
463 FilePath to_sub_a
= base_b
.Append(FPL("to_sub_a"));
464 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a
));
465 FilePath normalized_path
;
467 ReparsePoint
reparse_to_sub_a(to_sub_a
, sub_a
);
468 ASSERT_TRUE(reparse_to_sub_a
.IsValid());
470 FilePath to_base_b
= base_b
.Append(FPL("to_base_b"));
471 ASSERT_TRUE(file_util::CreateDirectory(to_base_b
));
472 ReparsePoint
reparse_to_base_b(to_base_b
, base_b
);
473 ASSERT_TRUE(reparse_to_base_b
.IsValid());
475 FilePath to_sub_long
= base_b
.Append(FPL("to_sub_long"));
476 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long
));
477 ReparsePoint
reparse_to_sub_long(to_sub_long
, sub_long
);
478 ASSERT_TRUE(reparse_to_sub_long
.IsValid());
480 // Normalize a junction free path: base_a\sub_a\file.txt .
481 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt
, &normalized_path
));
482 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
484 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
485 // the junction to_sub_a.
486 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
488 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
490 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
491 // normalized to exclude junctions to_base_b and to_sub_a .
492 ASSERT_TRUE(file_util::NormalizeFilePath(base_b
.Append(FPL("to_base_b"))
493 .Append(FPL("to_base_b"))
494 .Append(FPL("to_sub_a"))
495 .Append(FPL("file.txt")),
497 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
499 // A long enough path will cause NormalizeFilePath() to fail. Make a long
500 // path using to_base_b many times, and check that paths long enough to fail
501 // do not cause a crash.
502 FilePath long_path
= base_b
;
503 const int kLengthLimit
= MAX_PATH
+ 200;
504 while (long_path
.value().length() <= kLengthLimit
) {
505 long_path
= long_path
.Append(FPL("to_base_b"));
507 long_path
= long_path
.Append(FPL("to_sub_a"))
508 .Append(FPL("file.txt"));
510 ASSERT_FALSE(file_util::NormalizeFilePath(long_path
, &normalized_path
));
512 // Normalizing the junction to deep.txt should fail, because the expanded
513 // path to deep.txt is longer than MAX_PATH.
514 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long
.Append(deep_txt
),
517 // Delete the reparse points, and see that NormalizeFilePath() fails
521 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
525 TEST_F(FileUtilTest
, DevicePathToDriveLetter
) {
526 // Get a drive letter.
527 std::wstring real_drive_letter
= temp_dir_
.path().value().substr(0, 2);
528 if (!isalpha(real_drive_letter
[0]) || ':' != real_drive_letter
[1]) {
529 LOG(ERROR
) << "Can't get a drive letter to test with.";
533 // Get the NT style path to that drive.
534 wchar_t device_path
[MAX_PATH
] = {'\0'};
536 ::QueryDosDevice(real_drive_letter
.c_str(), device_path
, MAX_PATH
));
537 FilePath
actual_device_path(device_path
);
540 // Run DevicePathToDriveLetterPath() on the NT style path we got from
541 // QueryDosDevice(). Expect the drive letter we started with.
542 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path
,
544 ASSERT_EQ(real_drive_letter
, win32_path
.value());
546 // Add some directories to the path. Expect those extra path componenets
548 FilePath
kRelativePath(FPL("dir1\\dir2\\file.txt"));
549 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
550 actual_device_path
.Append(kRelativePath
),
552 EXPECT_EQ(FilePath(real_drive_letter
+ L
"\\").Append(kRelativePath
).value(),
555 // Deform the real path so that it is invalid by removing the last four
556 // characters. The way windows names devices that are hard disks
557 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
558 // than three characters. The only way the truncated string could be a
559 // real drive is if more than 10^3 disks are mounted:
560 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
561 // Check that DevicePathToDriveLetterPath fails.
562 int path_length
= actual_device_path
.value().length();
563 int new_length
= path_length
- 4;
564 ASSERT_LT(0, new_length
);
565 FilePath
prefix_of_real_device_path(
566 actual_device_path
.value().substr(0, new_length
));
567 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
568 prefix_of_real_device_path
,
571 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
572 prefix_of_real_device_path
.Append(kRelativePath
),
575 // Deform the real path so that it is invalid by adding some characters. For
576 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
577 // request for the drive letter whose native path is
578 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
579 // because drives are numbered in order and mounting 112345 hard disks will
581 const FilePath::StringType kExtraChars
= FPL("12345");
583 FilePath
real_device_path_plus_numbers(
584 actual_device_path
.value() + kExtraChars
);
586 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
587 real_device_path_plus_numbers
,
590 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
591 real_device_path_plus_numbers
.Append(kRelativePath
),
595 TEST_F(FileUtilTest
, GetPlatformFileInfoForDirectory
) {
596 FilePath empty_dir
= temp_dir_
.path().Append(FPL("gpfi_test"));
597 ASSERT_TRUE(file_util::CreateDirectory(empty_dir
));
598 base::win::ScopedHandle
dir(
599 ::CreateFile(empty_dir
.value().c_str(),
601 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
604 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
606 ASSERT_TRUE(dir
.IsValid());
607 base::PlatformFileInfo info
;
608 EXPECT_TRUE(base::GetPlatformFileInfo(dir
.Get(), &info
));
609 EXPECT_TRUE(info
.is_directory
);
610 EXPECT_FALSE(info
.is_symbolic_link
);
611 EXPECT_EQ(0, info
.size
);
614 TEST_F(FileUtilTest
, CreateTemporaryFileInDirLongPathTest
) {
615 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
616 // if it is available. This test requires that:
617 // - the filesystem at |temp_dir_| supports long filenames.
618 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
619 // directories of |temp_dir_|.
620 const FilePath::CharType kLongDirName
[] = FPL("A long path");
621 const FilePath::CharType kTestSubDirName
[] = FPL("test");
622 FilePath long_test_dir
= temp_dir_
.path().Append(kLongDirName
);
623 ASSERT_TRUE(file_util::CreateDirectory(long_test_dir
));
625 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
626 // different short name.
627 WCHAR path_buffer
[MAX_PATH
];
628 DWORD path_buffer_length
= GetShortPathName(long_test_dir
.value().c_str(),
629 path_buffer
, MAX_PATH
);
630 ASSERT_LT(path_buffer_length
, DWORD(MAX_PATH
));
631 ASSERT_NE(DWORD(0), path_buffer_length
);
632 FilePath
short_test_dir(path_buffer
);
633 ASSERT_STRNE(kLongDirName
, short_test_dir
.BaseName().value().c_str());
636 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir
, &temp_file
));
637 EXPECT_STREQ(kLongDirName
, temp_file
.DirName().BaseName().value().c_str());
638 EXPECT_TRUE(base::PathExists(temp_file
));
640 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
641 // unreadable. We should still be able to create a temp file in the
642 // subdirectory, but we won't be able to determine the long path for it. This
643 // mimics the environment that some users run where their user profiles reside
644 // in a location where the don't have full access to the higher level
645 // directories. (Note that this assumption is true for NTFS, but not for some
646 // network file systems. E.g. AFS).
647 FilePath access_test_dir
= long_test_dir
.Append(kTestSubDirName
);
648 ASSERT_TRUE(file_util::CreateDirectory(access_test_dir
));
649 file_util::PermissionRestorer
long_test_dir_restorer(long_test_dir
);
650 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir
));
652 // Use the short form of the directory to create a temporary filename.
653 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
654 short_test_dir
.Append(kTestSubDirName
), &temp_file
));
655 EXPECT_TRUE(base::PathExists(temp_file
));
656 EXPECT_TRUE(short_test_dir
.IsParent(temp_file
.DirName()));
658 // Check that the long path can't be determined for |temp_file|.
659 path_buffer_length
= GetLongPathName(temp_file
.value().c_str(),
660 path_buffer
, MAX_PATH
);
661 EXPECT_EQ(DWORD(0), path_buffer_length
);
664 #endif // defined(OS_WIN)
666 #if defined(OS_POSIX)
668 TEST_F(FileUtilTest
, CreateAndReadSymlinks
) {
669 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
670 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
671 CreateTextFile(link_to
, bogus_content
);
673 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
674 << "Failed to create file symlink.";
676 // If we created the link properly, we should be able to read the contents
678 std::wstring contents
= ReadTextFile(link_from
);
679 EXPECT_EQ(bogus_content
, contents
);
682 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from
, &result
));
683 EXPECT_EQ(link_to
.value(), result
.value());
685 // Link to a directory.
686 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
687 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
688 ASSERT_TRUE(file_util::CreateDirectory(link_to
));
689 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
690 << "Failed to create directory symlink.";
693 EXPECT_FALSE(file_util::CreateSymbolicLink(link_to
, link_to
));
694 EXPECT_FALSE(file_util::ReadSymbolicLink(link_to
, &result
));
695 FilePath missing
= temp_dir_
.path().Append(FPL("missing"));
696 EXPECT_FALSE(file_util::ReadSymbolicLink(missing
, &result
));
699 // The following test of NormalizeFilePath() require that we create a symlink.
700 // This can not be done on Windows before Vista. On Vista, creating a symlink
701 // requires privilege "SeCreateSymbolicLinkPrivilege".
702 // TODO(skerner): Investigate the possibility of giving base_unittests the
703 // privileges required to create a symlink.
704 TEST_F(FileUtilTest
, NormalizeFilePathSymlinks
) {
705 // Link one file to another.
706 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
707 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
708 CreateTextFile(link_to
, bogus_content
);
710 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
711 << "Failed to create file symlink.";
713 // Check that NormalizeFilePath sees the link.
714 FilePath normalized_path
;
715 ASSERT_TRUE(file_util::NormalizeFilePath(link_from
, &normalized_path
));
716 EXPECT_NE(link_from
, link_to
);
717 EXPECT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
718 EXPECT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
720 // Link to a directory.
721 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
722 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
723 ASSERT_TRUE(file_util::CreateDirectory(link_to
));
724 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
725 << "Failed to create directory symlink.";
727 EXPECT_FALSE(file_util::NormalizeFilePath(link_from
, &normalized_path
))
728 << "Links to directories should return false.";
730 // Test that a loop in the links causes NormalizeFilePath() to return false.
731 link_from
= temp_dir_
.path().Append(FPL("link_a"));
732 link_to
= temp_dir_
.path().Append(FPL("link_b"));
733 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
734 << "Failed to create loop symlink a.";
735 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from
, link_to
))
736 << "Failed to create loop symlink b.";
739 EXPECT_FALSE(file_util::NormalizeFilePath(link_from
, &normalized_path
));
741 #endif // defined(OS_POSIX)
743 TEST_F(FileUtilTest
, DeleteNonExistent
) {
744 FilePath non_existent
= temp_dir_
.path().AppendASCII("bogus_file_dne.foobar");
745 ASSERT_FALSE(base::PathExists(non_existent
));
747 EXPECT_TRUE(base::DeleteFile(non_existent
, false));
748 ASSERT_FALSE(base::PathExists(non_existent
));
749 EXPECT_TRUE(base::DeleteFile(non_existent
, true));
750 ASSERT_FALSE(base::PathExists(non_existent
));
753 TEST_F(FileUtilTest
, DeleteFile
) {
755 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 1.txt"));
756 CreateTextFile(file_name
, bogus_content
);
757 ASSERT_TRUE(base::PathExists(file_name
));
759 // Make sure it's deleted
760 EXPECT_TRUE(base::DeleteFile(file_name
, false));
761 EXPECT_FALSE(base::PathExists(file_name
));
763 // Test recursive case, create a new file
764 file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
765 CreateTextFile(file_name
, bogus_content
);
766 ASSERT_TRUE(base::PathExists(file_name
));
768 // Make sure it's deleted
769 EXPECT_TRUE(base::DeleteFile(file_name
, true));
770 EXPECT_FALSE(base::PathExists(file_name
));
773 #if defined(OS_POSIX)
774 TEST_F(FileUtilTest
, DeleteSymlinkToExistentFile
) {
776 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
777 CreateTextFile(file_name
, bogus_content
);
778 ASSERT_TRUE(base::PathExists(file_name
));
780 // Create a symlink to the file.
781 FilePath file_link
= temp_dir_
.path().Append("file_link_2");
782 ASSERT_TRUE(file_util::CreateSymbolicLink(file_name
, file_link
))
783 << "Failed to create symlink.";
785 // Delete the symbolic link.
786 EXPECT_TRUE(base::DeleteFile(file_link
, false));
788 // Make sure original file is not deleted.
789 EXPECT_FALSE(base::PathExists(file_link
));
790 EXPECT_TRUE(base::PathExists(file_name
));
793 TEST_F(FileUtilTest
, DeleteSymlinkToNonExistentFile
) {
794 // Create a non-existent file path.
795 FilePath non_existent
= temp_dir_
.path().Append(FPL("Test DeleteFile 3.txt"));
796 EXPECT_FALSE(base::PathExists(non_existent
));
798 // Create a symlink to the non-existent file.
799 FilePath file_link
= temp_dir_
.path().Append("file_link_3");
800 ASSERT_TRUE(file_util::CreateSymbolicLink(non_existent
, file_link
))
801 << "Failed to create symlink.";
803 // Make sure the symbolic link is exist.
804 EXPECT_TRUE(file_util::IsLink(file_link
));
805 EXPECT_FALSE(base::PathExists(file_link
));
807 // Delete the symbolic link.
808 EXPECT_TRUE(base::DeleteFile(file_link
, false));
810 // Make sure the symbolic link is deleted.
811 EXPECT_FALSE(file_util::IsLink(file_link
));
814 TEST_F(FileUtilTest
, ChangeFilePermissionsAndRead
) {
815 // Create a file path.
816 FilePath file_name
= temp_dir_
.path().Append(FPL("Test Readable File.txt"));
817 EXPECT_FALSE(base::PathExists(file_name
));
819 const std::string
kData("hello");
821 int buffer_size
= kData
.length();
822 char* buffer
= new char[buffer_size
];
825 EXPECT_EQ(static_cast<int>(kData
.length()),
826 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
827 EXPECT_TRUE(base::PathExists(file_name
));
829 // Make sure the file is readable.
831 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
832 EXPECT_TRUE(mode
& file_util::FILE_PERMISSION_READ_BY_USER
);
834 // Get rid of the read permission.
835 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name
, 0u));
836 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
837 EXPECT_FALSE(mode
& file_util::FILE_PERMISSION_READ_BY_USER
);
838 // Make sure the file can't be read.
839 EXPECT_EQ(-1, file_util::ReadFile(file_name
, buffer
, buffer_size
));
841 // Give the read permission.
842 EXPECT_TRUE(file_util::SetPosixFilePermissions(
844 file_util::FILE_PERMISSION_READ_BY_USER
));
845 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
846 EXPECT_TRUE(mode
& file_util::FILE_PERMISSION_READ_BY_USER
);
847 // Make sure the file can be read.
848 EXPECT_EQ(static_cast<int>(kData
.length()),
849 file_util::ReadFile(file_name
, buffer
, buffer_size
));
852 EXPECT_TRUE(base::DeleteFile(file_name
, false));
853 EXPECT_FALSE(base::PathExists(file_name
));
858 TEST_F(FileUtilTest
, ChangeFilePermissionsAndWrite
) {
859 // Create a file path.
860 FilePath file_name
= temp_dir_
.path().Append(FPL("Test Readable File.txt"));
861 EXPECT_FALSE(base::PathExists(file_name
));
863 const std::string
kData("hello");
866 EXPECT_EQ(static_cast<int>(kData
.length()),
867 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
868 EXPECT_TRUE(base::PathExists(file_name
));
870 // Make sure the file is writable.
872 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
873 EXPECT_TRUE(mode
& file_util::FILE_PERMISSION_WRITE_BY_USER
);
874 EXPECT_TRUE(PathIsWritable(file_name
));
876 // Get rid of the write permission.
877 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name
, 0u));
878 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
879 EXPECT_FALSE(mode
& file_util::FILE_PERMISSION_WRITE_BY_USER
);
880 // Make sure the file can't be write.
882 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
883 EXPECT_FALSE(PathIsWritable(file_name
));
885 // Give read permission.
886 EXPECT_TRUE(file_util::SetPosixFilePermissions(
888 file_util::FILE_PERMISSION_WRITE_BY_USER
));
889 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name
, &mode
));
890 EXPECT_TRUE(mode
& file_util::FILE_PERMISSION_WRITE_BY_USER
);
891 // Make sure the file can be write.
892 EXPECT_EQ(static_cast<int>(kData
.length()),
893 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
894 EXPECT_TRUE(PathIsWritable(file_name
));
897 EXPECT_TRUE(base::DeleteFile(file_name
, false));
898 EXPECT_FALSE(base::PathExists(file_name
));
901 TEST_F(FileUtilTest
, ChangeDirectoryPermissionsAndEnumerate
) {
902 // Create a directory path.
903 FilePath subdir_path
=
904 temp_dir_
.path().Append(FPL("PermissionTest1"));
905 file_util::CreateDirectory(subdir_path
);
906 ASSERT_TRUE(base::PathExists(subdir_path
));
908 // Create a dummy file to enumerate.
909 FilePath file_name
= subdir_path
.Append(FPL("Test Readable File.txt"));
910 EXPECT_FALSE(base::PathExists(file_name
));
911 const std::string
kData("hello");
912 EXPECT_EQ(static_cast<int>(kData
.length()),
913 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
914 EXPECT_TRUE(base::PathExists(file_name
));
916 // Make sure the directory has the all permissions.
918 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path
, &mode
));
919 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK
,
920 mode
& file_util::FILE_PERMISSION_USER_MASK
);
922 // Get rid of the permissions from the directory.
923 EXPECT_TRUE(file_util::SetPosixFilePermissions(subdir_path
, 0u));
924 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path
, &mode
));
925 EXPECT_FALSE(mode
& file_util::FILE_PERMISSION_USER_MASK
);
927 // Make sure the file in the directory can't be enumerated.
928 FileEnumerator
f1(subdir_path
, true, FileEnumerator::FILES
);
929 EXPECT_TRUE(base::PathExists(subdir_path
));
930 FindResultCollector
c1(f1
);
931 EXPECT_EQ(c1
.size(), 0);
932 EXPECT_FALSE(file_util::GetPosixFilePermissions(file_name
, &mode
));
934 // Give the permissions to the directory.
935 EXPECT_TRUE(file_util::SetPosixFilePermissions(
937 file_util::FILE_PERMISSION_USER_MASK
));
938 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path
, &mode
));
939 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK
,
940 mode
& file_util::FILE_PERMISSION_USER_MASK
);
942 // Make sure the file in the directory can be enumerated.
943 FileEnumerator
f2(subdir_path
, true, FileEnumerator::FILES
);
944 FindResultCollector
c2(f2
);
945 EXPECT_TRUE(c2
.HasFile(file_name
));
946 EXPECT_EQ(c2
.size(), 1);
949 EXPECT_TRUE(base::DeleteFile(subdir_path
, true));
950 EXPECT_FALSE(base::PathExists(subdir_path
));
953 #endif // defined(OS_POSIX)
956 // Tests that the Delete function works for wild cards, especially
957 // with the recursion flag. Also coincidentally tests PathExists.
958 // TODO(erikkay): see if anyone's actually using this feature of the API
959 TEST_F(FileUtilTest
, DeleteWildCard
) {
960 // Create a file and a directory
961 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteWildCard.txt"));
962 CreateTextFile(file_name
, bogus_content
);
963 ASSERT_TRUE(base::PathExists(file_name
));
965 FilePath subdir_path
= temp_dir_
.path().Append(FPL("DeleteWildCardDir"));
966 file_util::CreateDirectory(subdir_path
);
967 ASSERT_TRUE(base::PathExists(subdir_path
));
969 // Create the wildcard path
970 FilePath directory_contents
= temp_dir_
.path();
971 directory_contents
= directory_contents
.Append(FPL("*"));
973 // Delete non-recursively and check that only the file is deleted
974 EXPECT_TRUE(base::DeleteFile(directory_contents
, false));
975 EXPECT_FALSE(base::PathExists(file_name
));
976 EXPECT_TRUE(base::PathExists(subdir_path
));
978 // Delete recursively and make sure all contents are deleted
979 EXPECT_TRUE(base::DeleteFile(directory_contents
, true));
980 EXPECT_FALSE(base::PathExists(file_name
));
981 EXPECT_FALSE(base::PathExists(subdir_path
));
984 // TODO(erikkay): see if anyone's actually using this feature of the API
985 TEST_F(FileUtilTest
, DeleteNonExistantWildCard
) {
986 // Create a file and a directory
987 FilePath subdir_path
=
988 temp_dir_
.path().Append(FPL("DeleteNonExistantWildCard"));
989 file_util::CreateDirectory(subdir_path
);
990 ASSERT_TRUE(base::PathExists(subdir_path
));
992 // Create the wildcard path
993 FilePath directory_contents
= subdir_path
;
994 directory_contents
= directory_contents
.Append(FPL("*"));
996 // Delete non-recursively and check nothing got deleted
997 EXPECT_TRUE(base::DeleteFile(directory_contents
, false));
998 EXPECT_TRUE(base::PathExists(subdir_path
));
1000 // Delete recursively and check nothing got deleted
1001 EXPECT_TRUE(base::DeleteFile(directory_contents
, true));
1002 EXPECT_TRUE(base::PathExists(subdir_path
));
1006 // Tests non-recursive Delete() for a directory.
1007 TEST_F(FileUtilTest
, DeleteDirNonRecursive
) {
1008 // Create a subdirectory and put a file and two directories inside.
1009 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirNonRecursive"));
1010 file_util::CreateDirectory(test_subdir
);
1011 ASSERT_TRUE(base::PathExists(test_subdir
));
1013 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDir.txt"));
1014 CreateTextFile(file_name
, bogus_content
);
1015 ASSERT_TRUE(base::PathExists(file_name
));
1017 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
1018 file_util::CreateDirectory(subdir_path1
);
1019 ASSERT_TRUE(base::PathExists(subdir_path1
));
1021 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
1022 file_util::CreateDirectory(subdir_path2
);
1023 ASSERT_TRUE(base::PathExists(subdir_path2
));
1025 // Delete non-recursively and check that the empty dir got deleted
1026 EXPECT_TRUE(base::DeleteFile(subdir_path2
, false));
1027 EXPECT_FALSE(base::PathExists(subdir_path2
));
1029 // Delete non-recursively and check that nothing got deleted
1030 EXPECT_FALSE(base::DeleteFile(test_subdir
, false));
1031 EXPECT_TRUE(base::PathExists(test_subdir
));
1032 EXPECT_TRUE(base::PathExists(file_name
));
1033 EXPECT_TRUE(base::PathExists(subdir_path1
));
1036 // Tests recursive Delete() for a directory.
1037 TEST_F(FileUtilTest
, DeleteDirRecursive
) {
1038 // Create a subdirectory and put a file and two directories inside.
1039 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirRecursive"));
1040 file_util::CreateDirectory(test_subdir
);
1041 ASSERT_TRUE(base::PathExists(test_subdir
));
1043 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDirRecursive.txt"));
1044 CreateTextFile(file_name
, bogus_content
);
1045 ASSERT_TRUE(base::PathExists(file_name
));
1047 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
1048 file_util::CreateDirectory(subdir_path1
);
1049 ASSERT_TRUE(base::PathExists(subdir_path1
));
1051 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
1052 file_util::CreateDirectory(subdir_path2
);
1053 ASSERT_TRUE(base::PathExists(subdir_path2
));
1055 // Delete recursively and check that the empty dir got deleted
1056 EXPECT_TRUE(base::DeleteFile(subdir_path2
, true));
1057 EXPECT_FALSE(base::PathExists(subdir_path2
));
1059 // Delete recursively and check that everything got deleted
1060 EXPECT_TRUE(base::DeleteFile(test_subdir
, true));
1061 EXPECT_FALSE(base::PathExists(file_name
));
1062 EXPECT_FALSE(base::PathExists(subdir_path1
));
1063 EXPECT_FALSE(base::PathExists(test_subdir
));
1066 TEST_F(FileUtilTest
, MoveFileNew
) {
1068 FilePath file_name_from
=
1069 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1070 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1071 ASSERT_TRUE(base::PathExists(file_name_from
));
1074 FilePath file_name_to
= temp_dir_
.path().Append(
1075 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
1076 ASSERT_FALSE(base::PathExists(file_name_to
));
1078 EXPECT_TRUE(base::Move(file_name_from
, file_name_to
));
1080 // Check everything has been moved.
1081 EXPECT_FALSE(base::PathExists(file_name_from
));
1082 EXPECT_TRUE(base::PathExists(file_name_to
));
1085 TEST_F(FileUtilTest
, MoveFileExists
) {
1087 FilePath file_name_from
=
1088 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1089 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1090 ASSERT_TRUE(base::PathExists(file_name_from
));
1092 // The destination name.
1093 FilePath file_name_to
= temp_dir_
.path().Append(
1094 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
1095 CreateTextFile(file_name_to
, L
"Old file content");
1096 ASSERT_TRUE(base::PathExists(file_name_to
));
1098 EXPECT_TRUE(base::Move(file_name_from
, file_name_to
));
1100 // Check everything has been moved.
1101 EXPECT_FALSE(base::PathExists(file_name_from
));
1102 EXPECT_TRUE(base::PathExists(file_name_to
));
1103 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
1106 TEST_F(FileUtilTest
, MoveFileDirExists
) {
1108 FilePath file_name_from
=
1109 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1110 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1111 ASSERT_TRUE(base::PathExists(file_name_from
));
1113 // The destination directory
1114 FilePath dir_name_to
=
1115 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1116 file_util::CreateDirectory(dir_name_to
);
1117 ASSERT_TRUE(base::PathExists(dir_name_to
));
1119 EXPECT_FALSE(base::Move(file_name_from
, dir_name_to
));
1123 TEST_F(FileUtilTest
, MoveNew
) {
1124 // Create a directory
1125 FilePath dir_name_from
=
1126 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1127 file_util::CreateDirectory(dir_name_from
);
1128 ASSERT_TRUE(base::PathExists(dir_name_from
));
1130 // Create a file under the directory
1131 FilePath
txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1132 FilePath file_name_from
= dir_name_from
.Append(txt_file_name
);
1133 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1134 ASSERT_TRUE(base::PathExists(file_name_from
));
1136 // Move the directory.
1137 FilePath dir_name_to
=
1138 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1139 FilePath file_name_to
=
1140 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1142 ASSERT_FALSE(base::PathExists(dir_name_to
));
1144 EXPECT_TRUE(base::Move(dir_name_from
, dir_name_to
));
1146 // Check everything has been moved.
1147 EXPECT_FALSE(base::PathExists(dir_name_from
));
1148 EXPECT_FALSE(base::PathExists(file_name_from
));
1149 EXPECT_TRUE(base::PathExists(dir_name_to
));
1150 EXPECT_TRUE(base::PathExists(file_name_to
));
1152 // Test path traversal.
1153 file_name_from
= dir_name_to
.Append(txt_file_name
);
1154 file_name_to
= dir_name_to
.Append(FILE_PATH_LITERAL(".."));
1155 file_name_to
= file_name_to
.Append(txt_file_name
);
1156 EXPECT_FALSE(base::Move(file_name_from
, file_name_to
));
1157 EXPECT_TRUE(base::PathExists(file_name_from
));
1158 EXPECT_FALSE(base::PathExists(file_name_to
));
1159 EXPECT_TRUE(base::internal::MoveUnsafe(file_name_from
, file_name_to
));
1160 EXPECT_FALSE(base::PathExists(file_name_from
));
1161 EXPECT_TRUE(base::PathExists(file_name_to
));
1164 TEST_F(FileUtilTest
, MoveExist
) {
1165 // Create a directory
1166 FilePath dir_name_from
=
1167 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1168 file_util::CreateDirectory(dir_name_from
);
1169 ASSERT_TRUE(base::PathExists(dir_name_from
));
1171 // Create a file under the directory
1172 FilePath file_name_from
=
1173 dir_name_from
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1174 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1175 ASSERT_TRUE(base::PathExists(file_name_from
));
1177 // Move the directory
1178 FilePath dir_name_exists
=
1179 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1181 FilePath dir_name_to
=
1182 dir_name_exists
.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1183 FilePath file_name_to
=
1184 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1186 // Create the destination directory.
1187 file_util::CreateDirectory(dir_name_exists
);
1188 ASSERT_TRUE(base::PathExists(dir_name_exists
));
1190 EXPECT_TRUE(base::Move(dir_name_from
, dir_name_to
));
1192 // Check everything has been moved.
1193 EXPECT_FALSE(base::PathExists(dir_name_from
));
1194 EXPECT_FALSE(base::PathExists(file_name_from
));
1195 EXPECT_TRUE(base::PathExists(dir_name_to
));
1196 EXPECT_TRUE(base::PathExists(file_name_to
));
1199 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyNew
) {
1200 // Create a directory.
1201 FilePath dir_name_from
=
1202 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1203 file_util::CreateDirectory(dir_name_from
);
1204 ASSERT_TRUE(base::PathExists(dir_name_from
));
1206 // Create a file under the directory.
1207 FilePath file_name_from
=
1208 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1209 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1210 ASSERT_TRUE(base::PathExists(file_name_from
));
1212 // Create a subdirectory.
1213 FilePath subdir_name_from
=
1214 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1215 file_util::CreateDirectory(subdir_name_from
);
1216 ASSERT_TRUE(base::PathExists(subdir_name_from
));
1218 // Create a file under the subdirectory.
1219 FilePath file_name2_from
=
1220 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1221 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1222 ASSERT_TRUE(base::PathExists(file_name2_from
));
1224 // Copy the directory recursively.
1225 FilePath dir_name_to
=
1226 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1227 FilePath file_name_to
=
1228 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1229 FilePath subdir_name_to
=
1230 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1231 FilePath file_name2_to
=
1232 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1234 ASSERT_FALSE(base::PathExists(dir_name_to
));
1236 EXPECT_TRUE(base::CopyDirectory(dir_name_from
, dir_name_to
, true));
1238 // Check everything has been copied.
1239 EXPECT_TRUE(base::PathExists(dir_name_from
));
1240 EXPECT_TRUE(base::PathExists(file_name_from
));
1241 EXPECT_TRUE(base::PathExists(subdir_name_from
));
1242 EXPECT_TRUE(base::PathExists(file_name2_from
));
1243 EXPECT_TRUE(base::PathExists(dir_name_to
));
1244 EXPECT_TRUE(base::PathExists(file_name_to
));
1245 EXPECT_TRUE(base::PathExists(subdir_name_to
));
1246 EXPECT_TRUE(base::PathExists(file_name2_to
));
1249 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyExists
) {
1250 // Create a directory.
1251 FilePath dir_name_from
=
1252 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1253 file_util::CreateDirectory(dir_name_from
);
1254 ASSERT_TRUE(base::PathExists(dir_name_from
));
1256 // Create a file under the directory.
1257 FilePath file_name_from
=
1258 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1259 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1260 ASSERT_TRUE(base::PathExists(file_name_from
));
1262 // Create a subdirectory.
1263 FilePath subdir_name_from
=
1264 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1265 file_util::CreateDirectory(subdir_name_from
);
1266 ASSERT_TRUE(base::PathExists(subdir_name_from
));
1268 // Create a file under the subdirectory.
1269 FilePath file_name2_from
=
1270 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1271 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1272 ASSERT_TRUE(base::PathExists(file_name2_from
));
1274 // Copy the directory recursively.
1275 FilePath dir_name_exists
=
1276 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1278 FilePath dir_name_to
=
1279 dir_name_exists
.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1280 FilePath file_name_to
=
1281 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1282 FilePath subdir_name_to
=
1283 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1284 FilePath file_name2_to
=
1285 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1287 // Create the destination directory.
1288 file_util::CreateDirectory(dir_name_exists
);
1289 ASSERT_TRUE(base::PathExists(dir_name_exists
));
1291 EXPECT_TRUE(base::CopyDirectory(dir_name_from
, dir_name_exists
, true));
1293 // Check everything has been copied.
1294 EXPECT_TRUE(base::PathExists(dir_name_from
));
1295 EXPECT_TRUE(base::PathExists(file_name_from
));
1296 EXPECT_TRUE(base::PathExists(subdir_name_from
));
1297 EXPECT_TRUE(base::PathExists(file_name2_from
));
1298 EXPECT_TRUE(base::PathExists(dir_name_to
));
1299 EXPECT_TRUE(base::PathExists(file_name_to
));
1300 EXPECT_TRUE(base::PathExists(subdir_name_to
));
1301 EXPECT_TRUE(base::PathExists(file_name2_to
));
1304 TEST_F(FileUtilTest
, CopyDirectoryNew
) {
1305 // Create a directory.
1306 FilePath dir_name_from
=
1307 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1308 file_util::CreateDirectory(dir_name_from
);
1309 ASSERT_TRUE(base::PathExists(dir_name_from
));
1311 // Create a file under the directory.
1312 FilePath file_name_from
=
1313 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1314 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1315 ASSERT_TRUE(base::PathExists(file_name_from
));
1317 // Create a subdirectory.
1318 FilePath subdir_name_from
=
1319 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1320 file_util::CreateDirectory(subdir_name_from
);
1321 ASSERT_TRUE(base::PathExists(subdir_name_from
));
1323 // Create a file under the subdirectory.
1324 FilePath file_name2_from
=
1325 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1326 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1327 ASSERT_TRUE(base::PathExists(file_name2_from
));
1329 // Copy the directory not recursively.
1330 FilePath dir_name_to
=
1331 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1332 FilePath file_name_to
=
1333 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1334 FilePath subdir_name_to
=
1335 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1337 ASSERT_FALSE(base::PathExists(dir_name_to
));
1339 EXPECT_TRUE(base::CopyDirectory(dir_name_from
, dir_name_to
, false));
1341 // Check everything has been copied.
1342 EXPECT_TRUE(base::PathExists(dir_name_from
));
1343 EXPECT_TRUE(base::PathExists(file_name_from
));
1344 EXPECT_TRUE(base::PathExists(subdir_name_from
));
1345 EXPECT_TRUE(base::PathExists(file_name2_from
));
1346 EXPECT_TRUE(base::PathExists(dir_name_to
));
1347 EXPECT_TRUE(base::PathExists(file_name_to
));
1348 EXPECT_FALSE(base::PathExists(subdir_name_to
));
1351 TEST_F(FileUtilTest
, CopyDirectoryExists
) {
1352 // Create a directory.
1353 FilePath dir_name_from
=
1354 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1355 file_util::CreateDirectory(dir_name_from
);
1356 ASSERT_TRUE(base::PathExists(dir_name_from
));
1358 // Create a file under the directory.
1359 FilePath file_name_from
=
1360 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1361 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1362 ASSERT_TRUE(base::PathExists(file_name_from
));
1364 // Create a subdirectory.
1365 FilePath subdir_name_from
=
1366 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1367 file_util::CreateDirectory(subdir_name_from
);
1368 ASSERT_TRUE(base::PathExists(subdir_name_from
));
1370 // Create a file under the subdirectory.
1371 FilePath file_name2_from
=
1372 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1373 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1374 ASSERT_TRUE(base::PathExists(file_name2_from
));
1376 // Copy the directory not recursively.
1377 FilePath dir_name_to
=
1378 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1379 FilePath file_name_to
=
1380 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1381 FilePath subdir_name_to
=
1382 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1384 // Create the destination directory.
1385 file_util::CreateDirectory(dir_name_to
);
1386 ASSERT_TRUE(base::PathExists(dir_name_to
));
1388 EXPECT_TRUE(base::CopyDirectory(dir_name_from
, dir_name_to
, false));
1390 // Check everything has been copied.
1391 EXPECT_TRUE(base::PathExists(dir_name_from
));
1392 EXPECT_TRUE(base::PathExists(file_name_from
));
1393 EXPECT_TRUE(base::PathExists(subdir_name_from
));
1394 EXPECT_TRUE(base::PathExists(file_name2_from
));
1395 EXPECT_TRUE(base::PathExists(dir_name_to
));
1396 EXPECT_TRUE(base::PathExists(file_name_to
));
1397 EXPECT_FALSE(base::PathExists(subdir_name_to
));
1400 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToNew
) {
1402 FilePath file_name_from
=
1403 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1404 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1405 ASSERT_TRUE(base::PathExists(file_name_from
));
1407 // The destination name
1408 FilePath file_name_to
= temp_dir_
.path().Append(
1409 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1410 ASSERT_FALSE(base::PathExists(file_name_to
));
1412 EXPECT_TRUE(base::CopyDirectory(file_name_from
, file_name_to
, true));
1414 // Check the has been copied
1415 EXPECT_TRUE(base::PathExists(file_name_to
));
1418 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExisting
) {
1420 FilePath file_name_from
=
1421 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1422 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1423 ASSERT_TRUE(base::PathExists(file_name_from
));
1425 // The destination name
1426 FilePath file_name_to
= temp_dir_
.path().Append(
1427 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1428 CreateTextFile(file_name_to
, L
"Old file content");
1429 ASSERT_TRUE(base::PathExists(file_name_to
));
1431 EXPECT_TRUE(base::CopyDirectory(file_name_from
, file_name_to
, true));
1433 // Check the has been copied
1434 EXPECT_TRUE(base::PathExists(file_name_to
));
1435 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
1438 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExistingDirectory
) {
1440 FilePath file_name_from
=
1441 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1442 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1443 ASSERT_TRUE(base::PathExists(file_name_from
));
1446 FilePath dir_name_to
=
1447 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1448 file_util::CreateDirectory(dir_name_to
);
1449 ASSERT_TRUE(base::PathExists(dir_name_to
));
1450 FilePath file_name_to
=
1451 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1453 EXPECT_TRUE(base::CopyDirectory(file_name_from
, dir_name_to
, true));
1455 // Check the has been copied
1456 EXPECT_TRUE(base::PathExists(file_name_to
));
1459 TEST_F(FileUtilTest
, CopyDirectoryWithTrailingSeparators
) {
1460 // Create a directory.
1461 FilePath dir_name_from
=
1462 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1463 file_util::CreateDirectory(dir_name_from
);
1464 ASSERT_TRUE(base::PathExists(dir_name_from
));
1466 // Create a file under the directory.
1467 FilePath file_name_from
=
1468 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1469 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1470 ASSERT_TRUE(base::PathExists(file_name_from
));
1472 // Copy the directory recursively.
1473 FilePath dir_name_to
=
1474 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1475 FilePath file_name_to
=
1476 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1478 // Create from path with trailing separators.
1480 FilePath from_path
=
1481 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1482 #elif defined (OS_POSIX)
1483 FilePath from_path
=
1484 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1487 EXPECT_TRUE(base::CopyDirectory(from_path
, dir_name_to
, true));
1489 // Check everything has been copied.
1490 EXPECT_TRUE(base::PathExists(dir_name_from
));
1491 EXPECT_TRUE(base::PathExists(file_name_from
));
1492 EXPECT_TRUE(base::PathExists(dir_name_to
));
1493 EXPECT_TRUE(base::PathExists(file_name_to
));
1496 TEST_F(FileUtilTest
, CopyFile
) {
1497 // Create a directory
1498 FilePath dir_name_from
=
1499 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1500 file_util::CreateDirectory(dir_name_from
);
1501 ASSERT_TRUE(base::PathExists(dir_name_from
));
1503 // Create a file under the directory
1504 FilePath file_name_from
=
1505 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1506 const std::wstring
file_contents(L
"Gooooooooooooooooooooogle");
1507 CreateTextFile(file_name_from
, file_contents
);
1508 ASSERT_TRUE(base::PathExists(file_name_from
));
1511 FilePath dest_file
= dir_name_from
.Append(FILE_PATH_LITERAL("DestFile.txt"));
1512 ASSERT_TRUE(base::CopyFile(file_name_from
, dest_file
));
1514 // Copy the file to another location using '..' in the path.
1515 FilePath
dest_file2(dir_name_from
);
1516 dest_file2
= dest_file2
.AppendASCII("..");
1517 dest_file2
= dest_file2
.AppendASCII("DestFile.txt");
1518 ASSERT_FALSE(base::CopyFile(file_name_from
, dest_file2
));
1519 ASSERT_TRUE(base::internal::CopyFileUnsafe(file_name_from
, dest_file2
));
1521 FilePath
dest_file2_test(dir_name_from
);
1522 dest_file2_test
= dest_file2_test
.DirName();
1523 dest_file2_test
= dest_file2_test
.AppendASCII("DestFile.txt");
1525 // Check everything has been copied.
1526 EXPECT_TRUE(base::PathExists(file_name_from
));
1527 EXPECT_TRUE(base::PathExists(dest_file
));
1528 const std::wstring read_contents
= ReadTextFile(dest_file
);
1529 EXPECT_EQ(file_contents
, read_contents
);
1530 EXPECT_TRUE(base::PathExists(dest_file2_test
));
1531 EXPECT_TRUE(base::PathExists(dest_file2
));
1534 // file_util winds up using autoreleased objects on the Mac, so this needs
1535 // to be a PlatformTest.
1536 typedef PlatformTest ReadOnlyFileUtilTest
;
1538 TEST_F(ReadOnlyFileUtilTest
, ContentsEqual
) {
1540 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA
, &data_dir
));
1541 data_dir
= data_dir
.AppendASCII("file_util");
1542 ASSERT_TRUE(base::PathExists(data_dir
));
1544 FilePath original_file
=
1545 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1546 FilePath same_file
=
1547 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1548 FilePath same_length_file
=
1549 data_dir
.Append(FILE_PATH_LITERAL("same_length.txt"));
1550 FilePath different_file
=
1551 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1552 FilePath different_first_file
=
1553 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1554 FilePath different_last_file
=
1555 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1556 FilePath empty1_file
=
1557 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1558 FilePath empty2_file
=
1559 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1560 FilePath shortened_file
=
1561 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1562 FilePath binary_file
=
1563 data_dir
.Append(FILE_PATH_LITERAL("binary_file.bin"));
1564 FilePath binary_file_same
=
1565 data_dir
.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1566 FilePath binary_file_diff
=
1567 data_dir
.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
1569 EXPECT_TRUE(ContentsEqual(original_file
, original_file
));
1570 EXPECT_TRUE(ContentsEqual(original_file
, same_file
));
1571 EXPECT_FALSE(ContentsEqual(original_file
, same_length_file
));
1572 EXPECT_FALSE(ContentsEqual(original_file
, different_file
));
1573 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1574 FilePath(FILE_PATH_LITERAL("bogusname"))));
1575 EXPECT_FALSE(ContentsEqual(original_file
, different_first_file
));
1576 EXPECT_FALSE(ContentsEqual(original_file
, different_last_file
));
1577 EXPECT_TRUE(ContentsEqual(empty1_file
, empty2_file
));
1578 EXPECT_FALSE(ContentsEqual(original_file
, shortened_file
));
1579 EXPECT_FALSE(ContentsEqual(shortened_file
, original_file
));
1580 EXPECT_TRUE(ContentsEqual(binary_file
, binary_file_same
));
1581 EXPECT_FALSE(ContentsEqual(binary_file
, binary_file_diff
));
1584 TEST_F(ReadOnlyFileUtilTest
, TextContentsEqual
) {
1586 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA
, &data_dir
));
1587 data_dir
= data_dir
.AppendASCII("file_util");
1588 ASSERT_TRUE(base::PathExists(data_dir
));
1590 FilePath original_file
=
1591 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1592 FilePath same_file
=
1593 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1594 FilePath crlf_file
=
1595 data_dir
.Append(FILE_PATH_LITERAL("crlf.txt"));
1596 FilePath shortened_file
=
1597 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1598 FilePath different_file
=
1599 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1600 FilePath different_first_file
=
1601 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1602 FilePath different_last_file
=
1603 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1604 FilePath first1_file
=
1605 data_dir
.Append(FILE_PATH_LITERAL("first1.txt"));
1606 FilePath first2_file
=
1607 data_dir
.Append(FILE_PATH_LITERAL("first2.txt"));
1608 FilePath empty1_file
=
1609 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1610 FilePath empty2_file
=
1611 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1612 FilePath blank_line_file
=
1613 data_dir
.Append(FILE_PATH_LITERAL("blank_line.txt"));
1614 FilePath blank_line_crlf_file
=
1615 data_dir
.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1617 EXPECT_TRUE(TextContentsEqual(original_file
, same_file
));
1618 EXPECT_TRUE(TextContentsEqual(original_file
, crlf_file
));
1619 EXPECT_FALSE(TextContentsEqual(original_file
, shortened_file
));
1620 EXPECT_FALSE(TextContentsEqual(original_file
, different_file
));
1621 EXPECT_FALSE(TextContentsEqual(original_file
, different_first_file
));
1622 EXPECT_FALSE(TextContentsEqual(original_file
, different_last_file
));
1623 EXPECT_FALSE(TextContentsEqual(first1_file
, first2_file
));
1624 EXPECT_TRUE(TextContentsEqual(empty1_file
, empty2_file
));
1625 EXPECT_FALSE(TextContentsEqual(original_file
, empty1_file
));
1626 EXPECT_TRUE(TextContentsEqual(blank_line_file
, blank_line_crlf_file
));
1629 // We don't need equivalent functionality outside of Windows.
1631 TEST_F(FileUtilTest
, CopyAndDeleteDirectoryTest
) {
1632 // Create a directory
1633 FilePath dir_name_from
=
1634 temp_dir_
.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1635 file_util::CreateDirectory(dir_name_from
);
1636 ASSERT_TRUE(base::PathExists(dir_name_from
));
1638 // Create a file under the directory
1639 FilePath file_name_from
=
1640 dir_name_from
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1641 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1642 ASSERT_TRUE(base::PathExists(file_name_from
));
1644 // Move the directory by using CopyAndDeleteDirectory
1645 FilePath dir_name_to
= temp_dir_
.path().Append(
1646 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1647 FilePath file_name_to
=
1648 dir_name_to
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1650 ASSERT_FALSE(base::PathExists(dir_name_to
));
1652 EXPECT_TRUE(base::internal::CopyAndDeleteDirectory(dir_name_from
,
1655 // Check everything has been moved.
1656 EXPECT_FALSE(base::PathExists(dir_name_from
));
1657 EXPECT_FALSE(base::PathExists(file_name_from
));
1658 EXPECT_TRUE(base::PathExists(dir_name_to
));
1659 EXPECT_TRUE(base::PathExists(file_name_to
));
1662 TEST_F(FileUtilTest
, GetTempDirTest
) {
1663 static const TCHAR
* kTmpKey
= _T("TMP");
1664 static const TCHAR
* kTmpValues
[] = {
1665 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1667 // Save the original $TMP.
1668 size_t original_tmp_size
;
1669 TCHAR
* original_tmp
;
1670 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp
, &original_tmp_size
, kTmpKey
));
1671 // original_tmp may be NULL.
1673 for (unsigned int i
= 0; i
< arraysize(kTmpValues
); ++i
) {
1675 ::_tputenv_s(kTmpKey
, kTmpValues
[i
]);
1676 file_util::GetTempDir(&path
);
1677 EXPECT_TRUE(path
.IsAbsolute()) << "$TMP=" << kTmpValues
[i
] <<
1678 " result=" << path
.value();
1681 // Restore the original $TMP.
1683 ::_tputenv_s(kTmpKey
, original_tmp
);
1686 ::_tputenv_s(kTmpKey
, _T(""));
1691 TEST_F(FileUtilTest
, CreateTemporaryFileTest
) {
1692 FilePath temp_files
[3];
1693 for (int i
= 0; i
< 3; i
++) {
1694 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files
[i
])));
1695 EXPECT_TRUE(base::PathExists(temp_files
[i
]));
1696 EXPECT_FALSE(DirectoryExists(temp_files
[i
]));
1698 for (int i
= 0; i
< 3; i
++)
1699 EXPECT_FALSE(temp_files
[i
] == temp_files
[(i
+1)%3]);
1700 for (int i
= 0; i
< 3; i
++)
1701 EXPECT_TRUE(base::DeleteFile(temp_files
[i
], false));
1704 TEST_F(FileUtilTest
, CreateAndOpenTemporaryFileTest
) {
1709 // Create; make sure they are open and exist.
1710 for (i
= 0; i
< 3; ++i
) {
1711 fps
[i
] = file_util::CreateAndOpenTemporaryFile(&(names
[i
]));
1712 ASSERT_TRUE(fps
[i
]);
1713 EXPECT_TRUE(base::PathExists(names
[i
]));
1716 // Make sure all names are unique.
1717 for (i
= 0; i
< 3; ++i
) {
1718 EXPECT_FALSE(names
[i
] == names
[(i
+1)%3]);
1721 // Close and delete.
1722 for (i
= 0; i
< 3; ++i
) {
1723 EXPECT_TRUE(file_util::CloseFile(fps
[i
]));
1724 EXPECT_TRUE(base::DeleteFile(names
[i
], false));
1728 TEST_F(FileUtilTest
, CreateNewTempDirectoryTest
) {
1730 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1732 EXPECT_TRUE(base::PathExists(temp_dir
));
1733 EXPECT_TRUE(base::DeleteFile(temp_dir
, false));
1736 TEST_F(FileUtilTest
, CreateNewTemporaryDirInDirTest
) {
1738 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1740 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1742 EXPECT_TRUE(base::PathExists(new_dir
));
1743 EXPECT_TRUE(temp_dir_
.path().IsParent(new_dir
));
1744 EXPECT_TRUE(base::DeleteFile(new_dir
, false));
1747 TEST_F(FileUtilTest
, GetShmemTempDirTest
) {
1749 EXPECT_TRUE(file_util::GetShmemTempDir(&dir
, false));
1750 EXPECT_TRUE(DirectoryExists(dir
));
1753 TEST_F(FileUtilTest
, CreateDirectoryTest
) {
1754 FilePath test_root
=
1755 temp_dir_
.path().Append(FILE_PATH_LITERAL("create_directory_test"));
1757 FilePath test_path
=
1758 test_root
.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
1759 #elif defined(OS_POSIX)
1760 FilePath test_path
=
1761 test_root
.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
1764 EXPECT_FALSE(base::PathExists(test_path
));
1765 EXPECT_TRUE(file_util::CreateDirectory(test_path
));
1766 EXPECT_TRUE(base::PathExists(test_path
));
1767 // CreateDirectory returns true if the DirectoryExists returns true.
1768 EXPECT_TRUE(file_util::CreateDirectory(test_path
));
1770 // Doesn't work to create it on top of a non-dir
1771 test_path
= test_path
.Append(FILE_PATH_LITERAL("foobar.txt"));
1772 EXPECT_FALSE(base::PathExists(test_path
));
1773 CreateTextFile(test_path
, L
"test file");
1774 EXPECT_TRUE(base::PathExists(test_path
));
1775 EXPECT_FALSE(file_util::CreateDirectory(test_path
));
1777 EXPECT_TRUE(base::DeleteFile(test_root
, true));
1778 EXPECT_FALSE(base::PathExists(test_root
));
1779 EXPECT_FALSE(base::PathExists(test_path
));
1781 // Verify assumptions made by the Windows implementation:
1782 // 1. The current directory always exists.
1783 // 2. The root directory always exists.
1784 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory
)));
1785 FilePath top_level
= test_root
;
1786 while (top_level
!= top_level
.DirName()) {
1787 top_level
= top_level
.DirName();
1789 ASSERT_TRUE(DirectoryExists(top_level
));
1791 // Given these assumptions hold, it should be safe to
1792 // test that "creating" these directories succeeds.
1793 EXPECT_TRUE(file_util::CreateDirectory(
1794 FilePath(FilePath::kCurrentDirectory
)));
1795 EXPECT_TRUE(file_util::CreateDirectory(top_level
));
1798 FilePath
invalid_drive(FILE_PATH_LITERAL("o:\\"));
1799 FilePath invalid_path
=
1800 invalid_drive
.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1801 if (!base::PathExists(invalid_drive
)) {
1802 EXPECT_FALSE(file_util::CreateDirectory(invalid_path
));
1807 TEST_F(FileUtilTest
, DetectDirectoryTest
) {
1808 // Check a directory
1809 FilePath test_root
=
1810 temp_dir_
.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
1811 EXPECT_FALSE(base::PathExists(test_root
));
1812 EXPECT_TRUE(file_util::CreateDirectory(test_root
));
1813 EXPECT_TRUE(base::PathExists(test_root
));
1814 EXPECT_TRUE(DirectoryExists(test_root
));
1816 FilePath test_path
=
1817 test_root
.Append(FILE_PATH_LITERAL("foobar.txt"));
1818 EXPECT_FALSE(base::PathExists(test_path
));
1819 CreateTextFile(test_path
, L
"test file");
1820 EXPECT_TRUE(base::PathExists(test_path
));
1821 EXPECT_FALSE(DirectoryExists(test_path
));
1822 EXPECT_TRUE(base::DeleteFile(test_path
, false));
1824 EXPECT_TRUE(base::DeleteFile(test_root
, true));
1827 TEST_F(FileUtilTest
, FileEnumeratorTest
) {
1828 // Test an empty directory.
1829 FileEnumerator
f0(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1830 EXPECT_EQ(f0
.Next().value(), FPL(""));
1831 EXPECT_EQ(f0
.Next().value(), FPL(""));
1833 // Test an empty directory, non-recursively, including "..".
1834 FileEnumerator
f0_dotdot(temp_dir_
.path(), false,
1835 FILES_AND_DIRECTORIES
| FileEnumerator::INCLUDE_DOT_DOT
);
1836 EXPECT_EQ(temp_dir_
.path().Append(FPL("..")).value(),
1837 f0_dotdot
.Next().value());
1838 EXPECT_EQ(FPL(""), f0_dotdot
.Next().value());
1840 // create the directories
1841 FilePath dir1
= temp_dir_
.path().Append(FPL("dir1"));
1842 EXPECT_TRUE(file_util::CreateDirectory(dir1
));
1843 FilePath dir2
= temp_dir_
.path().Append(FPL("dir2"));
1844 EXPECT_TRUE(file_util::CreateDirectory(dir2
));
1845 FilePath dir2inner
= dir2
.Append(FPL("inner"));
1846 EXPECT_TRUE(file_util::CreateDirectory(dir2inner
));
1849 FilePath dir2file
= dir2
.Append(FPL("dir2file.txt"));
1850 CreateTextFile(dir2file
, std::wstring());
1851 FilePath dir2innerfile
= dir2inner
.Append(FPL("innerfile.txt"));
1852 CreateTextFile(dir2innerfile
, std::wstring());
1853 FilePath file1
= temp_dir_
.path().Append(FPL("file1.txt"));
1854 CreateTextFile(file1
, std::wstring());
1855 FilePath file2_rel
= dir2
.Append(FilePath::kParentDirectory
)
1856 .Append(FPL("file2.txt"));
1857 CreateTextFile(file2_rel
, std::wstring());
1858 FilePath file2_abs
= temp_dir_
.path().Append(FPL("file2.txt"));
1860 // Only enumerate files.
1861 FileEnumerator
f1(temp_dir_
.path(), true, 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 FileEnumerator
f2(temp_dir_
.path(), true, FileEnumerator::DIRECTORIES
);
1871 FindResultCollector
c2(f2
);
1872 EXPECT_TRUE(c2
.HasFile(dir1
));
1873 EXPECT_TRUE(c2
.HasFile(dir2
));
1874 EXPECT_TRUE(c2
.HasFile(dir2inner
));
1875 EXPECT_EQ(c2
.size(), 3);
1877 // Only enumerate directories non-recursively.
1878 FileEnumerator
f2_non_recursive(
1879 temp_dir_
.path(), false, FileEnumerator::DIRECTORIES
);
1880 FindResultCollector
c2_non_recursive(f2_non_recursive
);
1881 EXPECT_TRUE(c2_non_recursive
.HasFile(dir1
));
1882 EXPECT_TRUE(c2_non_recursive
.HasFile(dir2
));
1883 EXPECT_EQ(c2_non_recursive
.size(), 2);
1885 // Only enumerate directories, non-recursively, including "..".
1886 FileEnumerator
f2_dotdot(temp_dir_
.path(), false,
1887 FileEnumerator::DIRECTORIES
|
1888 FileEnumerator::INCLUDE_DOT_DOT
);
1889 FindResultCollector
c2_dotdot(f2_dotdot
);
1890 EXPECT_TRUE(c2_dotdot
.HasFile(dir1
));
1891 EXPECT_TRUE(c2_dotdot
.HasFile(dir2
));
1892 EXPECT_TRUE(c2_dotdot
.HasFile(temp_dir_
.path().Append(FPL(".."))));
1893 EXPECT_EQ(c2_dotdot
.size(), 3);
1895 // Enumerate files and directories.
1896 FileEnumerator
f3(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1897 FindResultCollector
c3(f3
);
1898 EXPECT_TRUE(c3
.HasFile(dir1
));
1899 EXPECT_TRUE(c3
.HasFile(dir2
));
1900 EXPECT_TRUE(c3
.HasFile(file1
));
1901 EXPECT_TRUE(c3
.HasFile(file2_abs
));
1902 EXPECT_TRUE(c3
.HasFile(dir2file
));
1903 EXPECT_TRUE(c3
.HasFile(dir2inner
));
1904 EXPECT_TRUE(c3
.HasFile(dir2innerfile
));
1905 EXPECT_EQ(c3
.size(), 7);
1907 // Non-recursive operation.
1908 FileEnumerator
f4(temp_dir_
.path(), false, FILES_AND_DIRECTORIES
);
1909 FindResultCollector
c4(f4
);
1910 EXPECT_TRUE(c4
.HasFile(dir2
));
1911 EXPECT_TRUE(c4
.HasFile(dir2
));
1912 EXPECT_TRUE(c4
.HasFile(file1
));
1913 EXPECT_TRUE(c4
.HasFile(file2_abs
));
1914 EXPECT_EQ(c4
.size(), 4);
1916 // Enumerate with a pattern.
1917 FileEnumerator
f5(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
, FPL("dir*"));
1918 FindResultCollector
c5(f5
);
1919 EXPECT_TRUE(c5
.HasFile(dir1
));
1920 EXPECT_TRUE(c5
.HasFile(dir2
));
1921 EXPECT_TRUE(c5
.HasFile(dir2file
));
1922 EXPECT_TRUE(c5
.HasFile(dir2inner
));
1923 EXPECT_TRUE(c5
.HasFile(dir2innerfile
));
1924 EXPECT_EQ(c5
.size(), 5);
1928 // Make dir1 point to dir2.
1929 ReparsePoint
reparse_point(dir1
, dir2
);
1930 EXPECT_TRUE(reparse_point
.IsValid());
1932 if ((base::win::GetVersion() >= base::win::VERSION_VISTA
)) {
1933 // There can be a delay for the enumeration code to see the change on
1934 // the file system so skip this test for XP.
1935 // Enumerate the reparse point.
1936 FileEnumerator
f6(dir1
, true, FILES_AND_DIRECTORIES
);
1937 FindResultCollector
c6(f6
);
1938 FilePath inner2
= dir1
.Append(FPL("inner"));
1939 EXPECT_TRUE(c6
.HasFile(inner2
));
1940 EXPECT_TRUE(c6
.HasFile(inner2
.Append(FPL("innerfile.txt"))));
1941 EXPECT_TRUE(c6
.HasFile(dir1
.Append(FPL("dir2file.txt"))));
1942 EXPECT_EQ(c6
.size(), 3);
1945 // No changes for non recursive operation.
1946 FileEnumerator
f7(temp_dir_
.path(), false, FILES_AND_DIRECTORIES
);
1947 FindResultCollector
c7(f7
);
1948 EXPECT_TRUE(c7
.HasFile(dir2
));
1949 EXPECT_TRUE(c7
.HasFile(dir2
));
1950 EXPECT_TRUE(c7
.HasFile(file1
));
1951 EXPECT_TRUE(c7
.HasFile(file2_abs
));
1952 EXPECT_EQ(c7
.size(), 4);
1954 // Should not enumerate inside dir1 when using recursion.
1955 FileEnumerator
f8(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1956 FindResultCollector
c8(f8
);
1957 EXPECT_TRUE(c8
.HasFile(dir1
));
1958 EXPECT_TRUE(c8
.HasFile(dir2
));
1959 EXPECT_TRUE(c8
.HasFile(file1
));
1960 EXPECT_TRUE(c8
.HasFile(file2_abs
));
1961 EXPECT_TRUE(c8
.HasFile(dir2file
));
1962 EXPECT_TRUE(c8
.HasFile(dir2inner
));
1963 EXPECT_TRUE(c8
.HasFile(dir2innerfile
));
1964 EXPECT_EQ(c8
.size(), 7);
1968 // Make sure the destructor closes the find handle while in the middle of a
1969 // query to allow TearDown to delete the directory.
1970 FileEnumerator
f9(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1971 EXPECT_FALSE(f9
.Next().value().empty()); // Should have found something
1972 // (we don't care what).
1975 TEST_F(FileUtilTest
, AppendToFile
) {
1977 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1979 // Create a fresh, empty copy of this directory.
1980 if (base::PathExists(data_dir
)) {
1981 ASSERT_TRUE(base::DeleteFile(data_dir
, true));
1983 ASSERT_TRUE(file_util::CreateDirectory(data_dir
));
1985 // Create a fresh, empty copy of this directory.
1986 if (base::PathExists(data_dir
)) {
1987 ASSERT_TRUE(base::DeleteFile(data_dir
, true));
1989 ASSERT_TRUE(file_util::CreateDirectory(data_dir
));
1990 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
1992 std::string
data("hello");
1993 EXPECT_EQ(-1, file_util::AppendToFile(foobar
, data
.c_str(), data
.length()));
1994 EXPECT_EQ(static_cast<int>(data
.length()),
1995 file_util::WriteFile(foobar
, data
.c_str(), data
.length()));
1996 EXPECT_EQ(static_cast<int>(data
.length()),
1997 file_util::AppendToFile(foobar
, data
.c_str(), data
.length()));
1999 const std::wstring read_content
= ReadTextFile(foobar
);
2000 EXPECT_EQ(L
"hellohello", read_content
);
2003 TEST_F(FileUtilTest
, TouchFile
) {
2005 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
2007 // Create a fresh, empty copy of this directory.
2008 if (base::PathExists(data_dir
)) {
2009 ASSERT_TRUE(base::DeleteFile(data_dir
, true));
2011 ASSERT_TRUE(file_util::CreateDirectory(data_dir
));
2013 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
2014 std::string
data("hello");
2015 ASSERT_TRUE(file_util::WriteFile(foobar
, data
.c_str(), data
.length()));
2017 base::Time access_time
;
2018 // This timestamp is divisible by one day (in local timezone),
2019 // to make it work on FAT too.
2020 ASSERT_TRUE(base::Time::FromString("Wed, 16 Nov 1994, 00:00:00",
2023 base::Time modification_time
;
2024 // Note that this timestamp is divisible by two (seconds) - FAT stores
2025 // modification times with 2s resolution.
2026 ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
2027 &modification_time
));
2029 ASSERT_TRUE(file_util::TouchFile(foobar
, access_time
, modification_time
));
2030 base::PlatformFileInfo file_info
;
2031 ASSERT_TRUE(file_util::GetFileInfo(foobar
, &file_info
));
2032 EXPECT_EQ(file_info
.last_accessed
.ToInternalValue(),
2033 access_time
.ToInternalValue());
2034 EXPECT_EQ(file_info
.last_modified
.ToInternalValue(),
2035 modification_time
.ToInternalValue());
2038 TEST_F(FileUtilTest
, IsDirectoryEmpty
) {
2039 FilePath empty_dir
= temp_dir_
.path().Append(FILE_PATH_LITERAL("EmptyDir"));
2041 ASSERT_FALSE(base::PathExists(empty_dir
));
2043 ASSERT_TRUE(file_util::CreateDirectory(empty_dir
));
2045 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir
));
2047 FilePath
foo(empty_dir
.Append(FILE_PATH_LITERAL("foo.txt")));
2048 std::string
bar("baz");
2049 ASSERT_TRUE(file_util::WriteFile(foo
, bar
.c_str(), bar
.length()));
2051 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir
));
2054 #if defined(OS_POSIX)
2056 // Testing VerifyPathControlledByAdmin() is hard, because there is no
2057 // way a test can make a file owned by root, or change file paths
2058 // at the root of the file system. VerifyPathControlledByAdmin()
2059 // is implemented as a call to VerifyPathControlledByUser, which gives
2060 // us the ability to test with paths under the test's temp directory,
2061 // using a user id we control.
2062 // Pull tests of VerifyPathControlledByUserTest() into a separate test class
2063 // with a common SetUp() method.
2064 class VerifyPathControlledByUserTest
: public FileUtilTest
{
2066 virtual void SetUp() OVERRIDE
{
2067 FileUtilTest::SetUp();
2069 // Create a basic structure used by each test.
2074 base_dir_
= temp_dir_
.path().AppendASCII("base_dir");
2075 ASSERT_TRUE(file_util::CreateDirectory(base_dir_
));
2077 sub_dir_
= base_dir_
.AppendASCII("sub_dir");
2078 ASSERT_TRUE(file_util::CreateDirectory(sub_dir_
));
2080 text_file_
= sub_dir_
.AppendASCII("file.txt");
2081 CreateTextFile(text_file_
, L
"This text file has some text in it.");
2083 // Get the user and group files are created with from |base_dir_|.
2084 struct stat stat_buf
;
2085 ASSERT_EQ(0, stat(base_dir_
.value().c_str(), &stat_buf
));
2086 uid_
= stat_buf
.st_uid
;
2087 ok_gids_
.insert(stat_buf
.st_gid
);
2088 bad_gids_
.insert(stat_buf
.st_gid
+ 1);
2090 ASSERT_EQ(uid_
, getuid()); // This process should be the owner.
2092 // To ensure that umask settings do not cause the initial state
2093 // of permissions to be different from what we expect, explicitly
2094 // set permissions on the directories we create.
2095 // Make all files and directories non-world-writable.
2097 // Users and group can read, write, traverse
2098 int enabled_permissions
=
2099 file_util::FILE_PERMISSION_USER_MASK
|
2100 file_util::FILE_PERMISSION_GROUP_MASK
;
2101 // Other users can't read, write, traverse
2102 int disabled_permissions
=
2103 file_util::FILE_PERMISSION_OTHERS_MASK
;
2105 ASSERT_NO_FATAL_FAILURE(
2106 ChangePosixFilePermissions(
2107 base_dir_
, enabled_permissions
, disabled_permissions
));
2108 ASSERT_NO_FATAL_FAILURE(
2109 ChangePosixFilePermissions(
2110 sub_dir_
, enabled_permissions
, disabled_permissions
));
2115 FilePath text_file_
;
2118 std::set
<gid_t
> ok_gids_
;
2119 std::set
<gid_t
> bad_gids_
;
2122 TEST_F(VerifyPathControlledByUserTest
, BadPaths
) {
2123 // File does not exist.
2124 FilePath does_not_exist
= base_dir_
.AppendASCII("does")
2126 .AppendASCII("exist");
2128 file_util::VerifyPathControlledByUser(
2129 base_dir_
, does_not_exist
, uid_
, ok_gids_
));
2131 // |base| not a subpath of |path|.
2133 file_util::VerifyPathControlledByUser(
2134 sub_dir_
, base_dir_
, uid_
, ok_gids_
));
2136 // An empty base path will fail to be a prefix for any path.
2139 file_util::VerifyPathControlledByUser(
2140 empty
, base_dir_
, uid_
, ok_gids_
));
2142 // Finding that a bad call fails proves nothing unless a good call succeeds.
2144 file_util::VerifyPathControlledByUser(
2145 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2148 TEST_F(VerifyPathControlledByUserTest
, Symlinks
) {
2149 // Symlinks in the path should cause failure.
2151 // Symlink to the file at the end of the path.
2152 FilePath file_link
= base_dir_
.AppendASCII("file_link");
2153 ASSERT_TRUE(file_util::CreateSymbolicLink(text_file_
, file_link
))
2154 << "Failed to create symlink.";
2157 file_util::VerifyPathControlledByUser(
2158 base_dir_
, file_link
, uid_
, ok_gids_
));
2160 file_util::VerifyPathControlledByUser(
2161 file_link
, file_link
, uid_
, ok_gids_
));
2163 // Symlink from one directory to another within the path.
2164 FilePath link_to_sub_dir
= base_dir_
.AppendASCII("link_to_sub_dir");
2165 ASSERT_TRUE(file_util::CreateSymbolicLink(sub_dir_
, link_to_sub_dir
))
2166 << "Failed to create symlink.";
2168 FilePath file_path_with_link
= link_to_sub_dir
.AppendASCII("file.txt");
2169 ASSERT_TRUE(base::PathExists(file_path_with_link
));
2172 file_util::VerifyPathControlledByUser(
2173 base_dir_
, file_path_with_link
, uid_
, ok_gids_
));
2176 file_util::VerifyPathControlledByUser(
2177 link_to_sub_dir
, file_path_with_link
, uid_
, ok_gids_
));
2179 // Symlinks in parents of base path are allowed.
2181 file_util::VerifyPathControlledByUser(
2182 file_path_with_link
, file_path_with_link
, uid_
, ok_gids_
));
2185 TEST_F(VerifyPathControlledByUserTest
, OwnershipChecks
) {
2186 // Get a uid that is not the uid of files we create.
2187 uid_t bad_uid
= uid_
+ 1;
2189 // Make all files and directories non-world-writable.
2190 ASSERT_NO_FATAL_FAILURE(
2191 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2192 ASSERT_NO_FATAL_FAILURE(
2193 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2194 ASSERT_NO_FATAL_FAILURE(
2195 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2197 // We control these paths.
2199 file_util::VerifyPathControlledByUser(
2200 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2202 file_util::VerifyPathControlledByUser(
2203 base_dir_
, text_file_
, uid_
, ok_gids_
));
2205 file_util::VerifyPathControlledByUser(
2206 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2208 // Another user does not control these paths.
2210 file_util::VerifyPathControlledByUser(
2211 base_dir_
, sub_dir_
, bad_uid
, ok_gids_
));
2213 file_util::VerifyPathControlledByUser(
2214 base_dir_
, text_file_
, bad_uid
, ok_gids_
));
2216 file_util::VerifyPathControlledByUser(
2217 sub_dir_
, text_file_
, bad_uid
, ok_gids_
));
2219 // Another group does not control the paths.
2221 file_util::VerifyPathControlledByUser(
2222 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2224 file_util::VerifyPathControlledByUser(
2225 base_dir_
, text_file_
, uid_
, bad_gids_
));
2227 file_util::VerifyPathControlledByUser(
2228 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2231 TEST_F(VerifyPathControlledByUserTest
, GroupWriteTest
) {
2232 // Make all files and directories writable only by their owner.
2233 ASSERT_NO_FATAL_FAILURE(
2234 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2235 ASSERT_NO_FATAL_FAILURE(
2236 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2237 ASSERT_NO_FATAL_FAILURE(
2238 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
|S_IWGRP
));
2240 // Any group is okay because the path is not group-writable.
2242 file_util::VerifyPathControlledByUser(
2243 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2245 file_util::VerifyPathControlledByUser(
2246 base_dir_
, text_file_
, uid_
, ok_gids_
));
2248 file_util::VerifyPathControlledByUser(
2249 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2252 file_util::VerifyPathControlledByUser(
2253 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2255 file_util::VerifyPathControlledByUser(
2256 base_dir_
, text_file_
, uid_
, bad_gids_
));
2258 file_util::VerifyPathControlledByUser(
2259 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2261 // No group is okay, because we don't check the group
2262 // if no group can write.
2263 std::set
<gid_t
> no_gids
; // Empty set of gids.
2265 file_util::VerifyPathControlledByUser(
2266 base_dir_
, sub_dir_
, uid_
, no_gids
));
2268 file_util::VerifyPathControlledByUser(
2269 base_dir_
, text_file_
, uid_
, no_gids
));
2271 file_util::VerifyPathControlledByUser(
2272 sub_dir_
, text_file_
, uid_
, no_gids
));
2275 // Make all files and directories writable by their group.
2276 ASSERT_NO_FATAL_FAILURE(
2277 ChangePosixFilePermissions(base_dir_
, S_IWGRP
, 0u));
2278 ASSERT_NO_FATAL_FAILURE(
2279 ChangePosixFilePermissions(sub_dir_
, S_IWGRP
, 0u));
2280 ASSERT_NO_FATAL_FAILURE(
2281 ChangePosixFilePermissions(text_file_
, S_IWGRP
, 0u));
2283 // Now |ok_gids_| works, but |bad_gids_| fails.
2285 file_util::VerifyPathControlledByUser(
2286 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2288 file_util::VerifyPathControlledByUser(
2289 base_dir_
, text_file_
, uid_
, ok_gids_
));
2291 file_util::VerifyPathControlledByUser(
2292 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2295 file_util::VerifyPathControlledByUser(
2296 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2298 file_util::VerifyPathControlledByUser(
2299 base_dir_
, text_file_
, uid_
, bad_gids_
));
2301 file_util::VerifyPathControlledByUser(
2302 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2304 // Because any group in the group set is allowed,
2305 // the union of good and bad gids passes.
2307 std::set
<gid_t
> multiple_gids
;
2309 ok_gids_
.begin(), ok_gids_
.end(),
2310 bad_gids_
.begin(), bad_gids_
.end(),
2311 std::inserter(multiple_gids
, multiple_gids
.begin()));
2314 file_util::VerifyPathControlledByUser(
2315 base_dir_
, sub_dir_
, uid_
, multiple_gids
));
2317 file_util::VerifyPathControlledByUser(
2318 base_dir_
, text_file_
, uid_
, multiple_gids
));
2320 file_util::VerifyPathControlledByUser(
2321 sub_dir_
, text_file_
, uid_
, multiple_gids
));
2324 TEST_F(VerifyPathControlledByUserTest
, WriteBitChecks
) {
2325 // Make all files and directories non-world-writable.
2326 ASSERT_NO_FATAL_FAILURE(
2327 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2328 ASSERT_NO_FATAL_FAILURE(
2329 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2330 ASSERT_NO_FATAL_FAILURE(
2331 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2333 // Initialy, we control all parts of the path.
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 base_dir_ world-writable.
2345 ASSERT_NO_FATAL_FAILURE(
2346 ChangePosixFilePermissions(base_dir_
, S_IWOTH
, 0u));
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 sub_dir_ world writable.
2358 ASSERT_NO_FATAL_FAILURE(
2359 ChangePosixFilePermissions(sub_dir_
, S_IWOTH
, 0u));
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 // Make text_file_ world writable.
2371 ASSERT_NO_FATAL_FAILURE(
2372 ChangePosixFilePermissions(text_file_
, S_IWOTH
, 0u));
2374 file_util::VerifyPathControlledByUser(
2375 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2377 file_util::VerifyPathControlledByUser(
2378 base_dir_
, text_file_
, uid_
, ok_gids_
));
2380 file_util::VerifyPathControlledByUser(
2381 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2383 // Make sub_dir_ non-world writable.
2384 ASSERT_NO_FATAL_FAILURE(
2385 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2387 file_util::VerifyPathControlledByUser(
2388 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2390 file_util::VerifyPathControlledByUser(
2391 base_dir_
, text_file_
, uid_
, ok_gids_
));
2393 file_util::VerifyPathControlledByUser(
2394 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2396 // Make base_dir_ non-world-writable.
2397 ASSERT_NO_FATAL_FAILURE(
2398 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2400 file_util::VerifyPathControlledByUser(
2401 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2403 file_util::VerifyPathControlledByUser(
2404 base_dir_
, text_file_
, uid_
, ok_gids_
));
2406 file_util::VerifyPathControlledByUser(
2407 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2409 // Back to the initial state: Nothing is writable, so every path
2411 ASSERT_NO_FATAL_FAILURE(
2412 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2414 file_util::VerifyPathControlledByUser(
2415 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2417 file_util::VerifyPathControlledByUser(
2418 base_dir_
, text_file_
, uid_
, ok_gids_
));
2420 file_util::VerifyPathControlledByUser(
2421 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2424 #endif // defined(OS_POSIX)