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_path.h"
21 #include "base/file_util.h"
22 #include "base/path_service.h"
23 #include "base/scoped_temp_dir.h"
24 #include "base/threading/platform_thread.h"
25 #include "base/utf_string_conversions.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "testing/platform_test.h"
30 #include "base/win/scoped_handle.h"
33 // This macro helps avoid wrapped lines in the test structs.
34 #define FPL(x) FILE_PATH_LITERAL(x)
38 // To test that file_util::Normalize FilePath() deals with NTFS reparse points
39 // correctly, we need functions to create and delete reparse points.
41 typedef struct _REPARSE_DATA_BUFFER
{
43 USHORT ReparseDataLength
;
47 USHORT SubstituteNameOffset
;
48 USHORT SubstituteNameLength
;
49 USHORT PrintNameOffset
;
50 USHORT PrintNameLength
;
53 } SymbolicLinkReparseBuffer
;
55 USHORT SubstituteNameOffset
;
56 USHORT SubstituteNameLength
;
57 USHORT PrintNameOffset
;
58 USHORT PrintNameLength
;
60 } MountPointReparseBuffer
;
63 } GenericReparseBuffer
;
65 } REPARSE_DATA_BUFFER
, *PREPARSE_DATA_BUFFER
;
67 // Sets a reparse point. |source| will now point to |target|. Returns true if
68 // the call succeeds, false otherwise.
69 bool SetReparsePoint(HANDLE source
, const FilePath
& target_path
) {
70 std::wstring kPathPrefix
= L
"\\??\\";
71 std::wstring target_str
;
72 // The juction will not work if the target path does not start with \??\ .
73 if (kPathPrefix
!= target_path
.value().substr(0, kPathPrefix
.size()))
74 target_str
+= kPathPrefix
;
75 target_str
+= target_path
.value();
76 const wchar_t* target
= target_str
.c_str();
77 USHORT size_target
= static_cast<USHORT
>(wcslen(target
)) * sizeof(target
[0]);
78 char buffer
[2000] = {0};
81 REPARSE_DATA_BUFFER
* data
= reinterpret_cast<REPARSE_DATA_BUFFER
*>(buffer
);
83 data
->ReparseTag
= 0xa0000003;
84 memcpy(data
->MountPointReparseBuffer
.PathBuffer
, target
, size_target
+ 2);
86 data
->MountPointReparseBuffer
.SubstituteNameLength
= size_target
;
87 data
->MountPointReparseBuffer
.PrintNameOffset
= size_target
+ 2;
88 data
->ReparseDataLength
= size_target
+ 4 + 8;
90 int data_size
= data
->ReparseDataLength
+ 8;
92 if (!DeviceIoControl(source
, FSCTL_SET_REPARSE_POINT
, &buffer
, data_size
,
93 NULL
, 0, &returned
, NULL
)) {
99 // Delete the reparse point referenced by |source|. Returns true if the call
100 // succeeds, false otherwise.
101 bool DeleteReparsePoint(HANDLE source
) {
103 REPARSE_DATA_BUFFER data
= {0};
104 data
.ReparseTag
= 0xa0000003;
105 if (!DeviceIoControl(source
, FSCTL_DELETE_REPARSE_POINT
, &data
, 8, NULL
, 0,
113 #if defined(OS_POSIX)
114 // Provide a simple way to change the permissions bits on |path| in tests.
115 // ASSERT failures will return, but not stop the test. Caller should wrap
116 // calls to this function in ASSERT_NO_FATAL_FAILURE().
117 void ChangePosixFilePermissions(const FilePath
& path
,
118 mode_t mode_bits_to_set
,
119 mode_t mode_bits_to_clear
) {
120 ASSERT_FALSE(mode_bits_to_set
& mode_bits_to_clear
)
121 << "Can't set and clear the same bits.";
123 struct stat stat_buf
;
124 ASSERT_EQ(0, stat(path
.value().c_str(), &stat_buf
));
126 mode_t updated_mode_bits
= stat_buf
.st_mode
;
127 updated_mode_bits
|= mode_bits_to_set
;
128 updated_mode_bits
&= ~mode_bits_to_clear
;
130 ASSERT_EQ(0, chmod(path
.value().c_str(), updated_mode_bits
));
132 #endif // defined(OS_POSIX)
134 const wchar_t bogus_content
[] = L
"I'm cannon fodder.";
136 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES
=
137 static_cast<file_util::FileEnumerator::FileType
>(
138 file_util::FileEnumerator::FILES
|
139 file_util::FileEnumerator::DIRECTORIES
);
141 // file_util winds up using autoreleased objects on the Mac, so this needs
142 // to be a PlatformTest
143 class FileUtilTest
: public PlatformTest
{
145 virtual void SetUp() {
146 PlatformTest::SetUp();
147 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
150 ScopedTempDir temp_dir_
;
153 // Collects all the results from the given file enumerator, and provides an
154 // interface to query whether a given file is present.
155 class FindResultCollector
{
157 explicit FindResultCollector(file_util::FileEnumerator
& enumerator
) {
159 while (!(cur_file
= enumerator
.Next()).value().empty()) {
160 FilePath::StringType path
= cur_file
.value();
161 // The file should not be returned twice.
162 EXPECT_TRUE(files_
.end() == files_
.find(path
))
163 << "Same file returned twice";
170 // Returns true if the enumerator found the file.
171 bool HasFile(const FilePath
& file
) const {
172 return files_
.find(file
.value()) != files_
.end();
176 return static_cast<int>(files_
.size());
180 std::set
<FilePath::StringType
> files_
;
183 // Simple function to dump some text into a new file.
184 void CreateTextFile(const FilePath
& filename
,
185 const std::wstring
& contents
) {
187 file
.open(filename
.value().c_str());
188 ASSERT_TRUE(file
.is_open());
193 // Simple function to take out some text from a file.
194 std::wstring
ReadTextFile(const FilePath
& filename
) {
195 wchar_t contents
[64];
197 file
.open(filename
.value().c_str());
198 EXPECT_TRUE(file
.is_open());
199 file
.getline(contents
, arraysize(contents
));
201 return std::wstring(contents
);
205 uint64
FileTimeAsUint64(const FILETIME
& ft
) {
207 u
.LowPart
= ft
.dwLowDateTime
;
208 u
.HighPart
= ft
.dwHighDateTime
;
213 const struct append_case
{
215 const wchar_t* ending
;
216 const wchar_t* result
;
219 {L
"c:\\colon\\backslash", L
"path", L
"c:\\colon\\backslash\\path"},
220 {L
"c:\\colon\\backslash\\", L
"path", L
"c:\\colon\\backslash\\path"},
221 {L
"c:\\colon\\backslash\\\\", L
"path", L
"c:\\colon\\backslash\\\\path"},
222 {L
"c:\\colon\\backslash\\", L
"", L
"c:\\colon\\backslash\\"},
223 {L
"c:\\colon\\backslash", L
"", L
"c:\\colon\\backslash\\"},
224 {L
"", L
"path", L
"\\path"},
226 #elif defined(OS_POSIX)
227 {L
"/foo/bar", L
"path", L
"/foo/bar/path"},
228 {L
"/foo/bar/", L
"path", L
"/foo/bar/path"},
229 {L
"/foo/bar//", L
"path", L
"/foo/bar//path"},
230 {L
"/foo/bar/", L
"", L
"/foo/bar/"},
231 {L
"/foo/bar", L
"", L
"/foo/bar/"},
232 {L
"", L
"path", L
"/path"},
238 // This function is deprecated, but still used on Windows.
239 TEST_F(FileUtilTest
, AppendToPath
) {
240 for (unsigned int i
= 0; i
< arraysize(append_cases
); ++i
) {
241 const append_case
& value
= append_cases
[i
];
242 std::wstring result
= value
.path
;
243 file_util::AppendToPath(&result
, value
.ending
);
244 EXPECT_EQ(value
.result
, result
);
247 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
248 file_util::AppendToPath(NULL
, L
"path"); // asserts in debug mode
251 #endif // defined(OS_WIN)
253 static const struct filename_case
{
255 const wchar_t* filename
;
256 } filename_cases
[] = {
258 {L
"c:\\colon\\backslash", L
"backslash"},
259 {L
"c:\\colon\\backslash\\", L
""},
260 {L
"\\\\filename.exe", L
"filename.exe"},
261 {L
"filename.exe", L
"filename.exe"},
264 {L
"c:/colon/backslash", L
"backslash"},
265 {L
"c:/colon/backslash/", L
""},
267 {L
"///filename.exe", L
"filename.exe"},
268 #elif defined(OS_POSIX)
269 {L
"/foo/bar", L
"bar"},
271 {L
"/filename.exe", L
"filename.exe"},
272 {L
"filename.exe", L
"filename.exe"},
278 // Test finding the file type from a path name
279 static const struct extension_case
{
281 const wchar_t* extension
;
282 } extension_cases
[] = {
284 {L
"C:\\colon\\backslash\\filename.extension", L
"extension"},
285 {L
"C:\\colon\\backslash\\filename.", L
""},
286 {L
"C:\\colon\\backslash\\filename", L
""},
287 {L
"C:\\colon\\backslash\\", L
""},
288 {L
"C:\\colon\\backslash.\\", L
""},
289 {L
"C:\\colon\\backslash\filename.extension.extension2", L
"extension2"},
290 #elif defined(OS_POSIX)
291 {L
"/foo/bar/filename.extension", L
"extension"},
292 {L
"/foo/bar/filename.", L
""},
293 {L
"/foo/bar/filename", L
""},
295 {L
"/foo/bar./", L
""},
296 {L
"/foo/bar/filename.extension.extension2", L
"extension2"},
300 {L
"./foo.extension", L
"extension"},
301 {L
"/foo.extension1/bar.extension2", L
"extension2"},
306 // This function has been deprecated on non-Windows.
307 TEST_F(FileUtilTest
, GetFileExtensionFromPath
) {
308 for (unsigned int i
= 0; i
< arraysize(extension_cases
); ++i
) {
309 const extension_case
& ext
= extension_cases
[i
];
310 const std::wstring fext
= file_util::GetFileExtensionFromPath(ext
.path
);
311 EXPECT_EQ(ext
.extension
, fext
);
316 // Test finding the directory component of a path
317 static const struct dir_case
{
318 const wchar_t* full_path
;
319 const wchar_t* directory
;
322 {L
"C:\\WINDOWS\\system32\\gdi32.dll", L
"C:\\WINDOWS\\system32"},
323 {L
"C:\\WINDOWS\\system32\\not_exist_thx_1138", L
"C:\\WINDOWS\\system32"},
324 {L
"C:\\WINDOWS\\system32\\", L
"C:\\WINDOWS\\system32"},
325 {L
"C:\\WINDOWS\\system32\\\\", L
"C:\\WINDOWS\\system32"},
326 {L
"C:\\WINDOWS\\system32", L
"C:\\WINDOWS"},
327 {L
"C:\\WINDOWS\\system32.\\", L
"C:\\WINDOWS\\system32."},
329 #elif defined(OS_POSIX)
330 {L
"/foo/bar/gdi32.dll", L
"/foo/bar"},
331 {L
"/foo/bar/not_exist_thx_1138", L
"/foo/bar"},
332 {L
"/foo/bar/", L
"/foo/bar"},
333 {L
"/foo/bar//", L
"/foo/bar"},
334 {L
"/foo/bar", L
"/foo"},
335 {L
"/foo/bar./", L
"/foo/bar."},
338 {L
"..", L
"."}, // yes, ".." technically lives in "."
342 TEST_F(FileUtilTest
, CountFilesCreatedAfter
) {
344 temp_dir_
.path().Append(FILE_PATH_LITERAL("f.txt"));
345 CreateTextFile(file_name
, L
"test");
347 base::PlatformFileInfo info
;
348 file_util::GetFileInfo(file_name
, &info
);
349 base::Time file_time
= info
.creation_time
;
351 base::TimeDelta two_secs
= base::TimeDelta::FromSeconds(2);
352 base::Time after
= file_time
+ two_secs
;
353 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_
.path(), after
));
355 base::Time before
= file_time
- two_secs
;
356 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(temp_dir_
.path(), before
));
358 // After deleting the file, shouldn't find it any more.
359 EXPECT_TRUE(file_util::Delete(file_name
, false));
360 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_
.path(), before
));
361 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(temp_dir_
.path(), after
));
364 TEST_F(FileUtilTest
, FileAndDirectorySize
) {
365 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
366 // should return 53 bytes.
367 FilePath file_01
= temp_dir_
.path().Append(FPL("The file 01.txt"));
368 CreateTextFile(file_01
, L
"12345678901234567890");
370 ASSERT_TRUE(file_util::GetFileSize(file_01
, &size_f1
));
371 EXPECT_EQ(20ll, size_f1
);
373 FilePath subdir_path
= temp_dir_
.path().Append(FPL("Level2"));
374 file_util::CreateDirectory(subdir_path
);
376 FilePath file_02
= subdir_path
.Append(FPL("The file 02.txt"));
377 CreateTextFile(file_02
, L
"123456789012345678901234567890");
379 ASSERT_TRUE(file_util::GetFileSize(file_02
, &size_f2
));
380 EXPECT_EQ(30ll, size_f2
);
382 FilePath subsubdir_path
= subdir_path
.Append(FPL("Level3"));
383 file_util::CreateDirectory(subsubdir_path
);
385 FilePath file_03
= subsubdir_path
.Append(FPL("The file 03.txt"));
386 CreateTextFile(file_03
, L
"123");
388 int64 computed_size
= file_util::ComputeDirectorySize(temp_dir_
.path());
389 EXPECT_EQ(size_f1
+ size_f2
+ 3, computed_size
);
392 file_util::ComputeFilesSize(temp_dir_
.path(), FPL("The file*"));
393 EXPECT_EQ(size_f1
, computed_size
);
395 computed_size
= file_util::ComputeFilesSize(temp_dir_
.path(), FPL("bla*"));
396 EXPECT_EQ(0, computed_size
);
399 TEST_F(FileUtilTest
, NormalizeFilePathBasic
) {
400 // Create a directory under the test dir. Because we create it,
401 // we know it is not a link.
402 FilePath file_a_path
= temp_dir_
.path().Append(FPL("file_a"));
403 FilePath dir_path
= temp_dir_
.path().Append(FPL("dir"));
404 FilePath file_b_path
= dir_path
.Append(FPL("file_b"));
405 file_util::CreateDirectory(dir_path
);
407 FilePath normalized_file_a_path
, normalized_file_b_path
;
408 ASSERT_FALSE(file_util::PathExists(file_a_path
));
409 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path
,
410 &normalized_file_a_path
))
411 << "NormalizeFilePath() should fail on nonexistent paths.";
413 CreateTextFile(file_a_path
, bogus_content
);
414 ASSERT_TRUE(file_util::PathExists(file_a_path
));
415 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path
,
416 &normalized_file_a_path
));
418 CreateTextFile(file_b_path
, bogus_content
);
419 ASSERT_TRUE(file_util::PathExists(file_b_path
));
420 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path
,
421 &normalized_file_b_path
));
423 // Beacuse this test created |dir_path|, we know it is not a link
424 // or junction. So, the real path of the directory holding file a
425 // must be the parent of the path holding file b.
426 ASSERT_TRUE(normalized_file_a_path
.DirName()
427 .IsParent(normalized_file_b_path
.DirName()));
432 TEST_F(FileUtilTest
, NormalizeFilePathReparsePoints
) {
433 // Build the following directory structure:
439 // | |-> long_name___... (Very long name.)
443 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
444 // |-> to_base_b (reparse point to temp_dir\base_b)
445 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
447 FilePath base_a
= temp_dir_
.path().Append(FPL("base_a"));
448 ASSERT_TRUE(file_util::CreateDirectory(base_a
));
450 FilePath sub_a
= base_a
.Append(FPL("sub_a"));
451 ASSERT_TRUE(file_util::CreateDirectory(sub_a
));
453 FilePath file_txt
= sub_a
.Append(FPL("file.txt"));
454 CreateTextFile(file_txt
, bogus_content
);
456 // Want a directory whose name is long enough to make the path to the file
457 // inside just under MAX_PATH chars. This will be used to test that when
458 // a junction expands to a path over MAX_PATH chars in length,
459 // NormalizeFilePath() fails without crashing.
460 FilePath
sub_long_rel(FPL("sub_long"));
461 FilePath
deep_txt(FPL("deep.txt"));
463 int target_length
= MAX_PATH
;
464 target_length
-= (sub_a
.value().length() + 1); // +1 for the sepperator '\'.
465 target_length
-= (sub_long_rel
.Append(deep_txt
).value().length() + 1);
466 // Without making the path a bit shorter, CreateDirectory() fails.
467 // the resulting path is still long enough to hit the failing case in
469 const int kCreateDirLimit
= 4;
470 target_length
-= kCreateDirLimit
;
471 FilePath::StringType long_name_str
= FPL("long_name_");
472 long_name_str
.resize(target_length
, '_');
474 FilePath long_name
= sub_a
.Append(FilePath(long_name_str
));
475 FilePath deep_file
= long_name
.Append(sub_long_rel
).Append(deep_txt
);
476 ASSERT_EQ(MAX_PATH
- kCreateDirLimit
, deep_file
.value().length());
478 FilePath sub_long
= deep_file
.DirName();
479 ASSERT_TRUE(file_util::CreateDirectory(sub_long
));
480 CreateTextFile(deep_file
, bogus_content
);
482 FilePath base_b
= temp_dir_
.path().Append(FPL("base_b"));
483 ASSERT_TRUE(file_util::CreateDirectory(base_b
));
485 FilePath to_sub_a
= base_b
.Append(FPL("to_sub_a"));
486 ASSERT_TRUE(file_util::CreateDirectory(to_sub_a
));
487 base::win::ScopedHandle
reparse_to_sub_a(
488 ::CreateFile(to_sub_a
.value().c_str(),
490 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
493 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
495 ASSERT_TRUE(reparse_to_sub_a
.IsValid());
496 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_a
, sub_a
));
498 FilePath to_base_b
= base_b
.Append(FPL("to_base_b"));
499 ASSERT_TRUE(file_util::CreateDirectory(to_base_b
));
500 base::win::ScopedHandle
reparse_to_base_b(
501 ::CreateFile(to_base_b
.value().c_str(),
503 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
506 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
508 ASSERT_TRUE(reparse_to_base_b
.IsValid());
509 ASSERT_TRUE(SetReparsePoint(reparse_to_base_b
, base_b
));
511 FilePath to_sub_long
= base_b
.Append(FPL("to_sub_long"));
512 ASSERT_TRUE(file_util::CreateDirectory(to_sub_long
));
513 base::win::ScopedHandle
reparse_to_sub_long(
514 ::CreateFile(to_sub_long
.value().c_str(),
516 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
519 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
521 ASSERT_TRUE(reparse_to_sub_long
.IsValid());
522 ASSERT_TRUE(SetReparsePoint(reparse_to_sub_long
, sub_long
));
524 // Normalize a junction free path: base_a\sub_a\file.txt .
525 FilePath normalized_path
;
526 ASSERT_TRUE(file_util::NormalizeFilePath(file_txt
, &normalized_path
));
527 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
529 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
530 // the junction to_sub_a.
531 ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
533 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
535 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
536 // normalized to exclude junctions to_base_b and to_sub_a .
537 ASSERT_TRUE(file_util::NormalizeFilePath(base_b
.Append(FPL("to_base_b"))
538 .Append(FPL("to_base_b"))
539 .Append(FPL("to_sub_a"))
540 .Append(FPL("file.txt")),
542 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
544 // A long enough path will cause NormalizeFilePath() to fail. Make a long
545 // path using to_base_b many times, and check that paths long enough to fail
546 // do not cause a crash.
547 FilePath long_path
= base_b
;
548 const int kLengthLimit
= MAX_PATH
+ 200;
549 while (long_path
.value().length() <= kLengthLimit
) {
550 long_path
= long_path
.Append(FPL("to_base_b"));
552 long_path
= long_path
.Append(FPL("to_sub_a"))
553 .Append(FPL("file.txt"));
555 ASSERT_FALSE(file_util::NormalizeFilePath(long_path
, &normalized_path
));
557 // Normalizing the junction to deep.txt should fail, because the expanded
558 // path to deep.txt is longer than MAX_PATH.
559 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long
.Append(deep_txt
),
562 // Delete the reparse points, and see that NormalizeFilePath() fails
564 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a
));
565 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b
));
566 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long
));
568 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
572 TEST_F(FileUtilTest
, DevicePathToDriveLetter
) {
573 // Get a drive letter.
574 std::wstring real_drive_letter
= temp_dir_
.path().value().substr(0, 2);
575 if (!isalpha(real_drive_letter
[0]) || ':' != real_drive_letter
[1]) {
576 LOG(ERROR
) << "Can't get a drive letter to test with.";
580 // Get the NT style path to that drive.
581 wchar_t device_path
[MAX_PATH
] = {'\0'};
583 ::QueryDosDevice(real_drive_letter
.c_str(), device_path
, MAX_PATH
));
584 FilePath
actual_device_path(device_path
);
587 // Run DevicePathToDriveLetterPath() on the NT style path we got from
588 // QueryDosDevice(). Expect the drive letter we started with.
589 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(actual_device_path
,
591 ASSERT_EQ(real_drive_letter
, win32_path
.value());
593 // Add some directories to the path. Expect those extra path componenets
595 FilePath
kRelativePath(FPL("dir1\\dir2\\file.txt"));
596 ASSERT_TRUE(file_util::DevicePathToDriveLetterPath(
597 actual_device_path
.Append(kRelativePath
),
599 EXPECT_EQ(FilePath(real_drive_letter
+ L
"\\").Append(kRelativePath
).value(),
602 // Deform the real path so that it is invalid by removing the last four
603 // characters. The way windows names devices that are hard disks
604 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
605 // than three characters. The only way the truncated string could be a
606 // real drive is if more than 10^3 disks are mounted:
607 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
608 // Check that DevicePathToDriveLetterPath fails.
609 int path_length
= actual_device_path
.value().length();
610 int new_length
= path_length
- 4;
611 ASSERT_LT(0, new_length
);
612 FilePath
prefix_of_real_device_path(
613 actual_device_path
.value().substr(0, new_length
));
614 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
615 prefix_of_real_device_path
,
618 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
619 prefix_of_real_device_path
.Append(kRelativePath
),
622 // Deform the real path so that it is invalid by adding some characters. For
623 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
624 // request for the drive letter whose native path is
625 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
626 // because drives are numbered in order and mounting 112345 hard disks will
628 const FilePath::StringType kExtraChars
= FPL("12345");
630 FilePath
real_device_path_plus_numbers(
631 actual_device_path
.value() + kExtraChars
);
633 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
634 real_device_path_plus_numbers
,
637 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
638 real_device_path_plus_numbers
.Append(kRelativePath
),
642 TEST_F(FileUtilTest
, GetPlatformFileInfoForDirectory
) {
643 FilePath empty_dir
= temp_dir_
.path().Append(FPL("gpfi_test"));
644 ASSERT_TRUE(file_util::CreateDirectory(empty_dir
));
645 base::win::ScopedHandle
dir(
646 ::CreateFile(empty_dir
.value().c_str(),
648 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
651 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
653 ASSERT_TRUE(dir
.IsValid());
654 base::PlatformFileInfo info
;
655 EXPECT_TRUE(base::GetPlatformFileInfo(dir
.Get(), &info
));
656 EXPECT_TRUE(info
.is_directory
);
657 EXPECT_FALSE(info
.is_symbolic_link
);
658 EXPECT_EQ(0, info
.size
);
661 #endif // defined(OS_WIN)
663 #if defined(OS_POSIX)
665 TEST_F(FileUtilTest
, CreateAndReadSymlinks
) {
666 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
667 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
668 CreateTextFile(link_to
, bogus_content
);
670 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
671 << "Failed to create file symlink.";
673 // If we created the link properly, we should be able to read the
674 // contents through it.
675 std::wstring contents
= ReadTextFile(link_from
);
676 ASSERT_EQ(contents
, bogus_content
);
679 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from
, &result
));
680 ASSERT_EQ(link_to
.value(), result
.value());
682 // Link to a directory.
683 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
684 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
685 file_util::CreateDirectory(link_to
);
687 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
688 << "Failed to create directory symlink.";
691 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to
, link_to
));
692 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to
, &result
));
693 FilePath missing
= temp_dir_
.path().Append(FPL("missing"));
694 ASSERT_FALSE(file_util::ReadSymbolicLink(missing
, &result
));
698 // The following test of NormalizeFilePath() require that we create a symlink.
699 // This can not be done on Windows before Vista. On Vista, creating a symlink
700 // requires privilege "SeCreateSymbolicLinkPrivilege".
701 // TODO(skerner): Investigate the possibility of giving base_unittests the
702 // privileges required to create a symlink.
703 TEST_F(FileUtilTest
, NormalizeFilePathSymlinks
) {
704 FilePath normalized_path
;
706 // Link one file to another.
707 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
708 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
709 CreateTextFile(link_to
, bogus_content
);
711 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
712 << "Failed to create file symlink.";
714 // Check that NormalizeFilePath sees the link.
715 ASSERT_TRUE(file_util::NormalizeFilePath(link_from
, &normalized_path
));
716 ASSERT_TRUE(link_to
!= link_from
);
717 ASSERT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
718 ASSERT_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 file_util::CreateDirectory(link_to
);
725 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
726 << "Failed to create directory symlink.";
728 ASSERT_FALSE(file_util::NormalizeFilePath(link_from
, &normalized_path
))
729 << "Links to directories should return false.";
731 // Test that a loop in the links causes NormalizeFilePath() to return false.
732 link_from
= temp_dir_
.path().Append(FPL("link_a"));
733 link_to
= temp_dir_
.path().Append(FPL("link_b"));
734 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to
, link_from
))
735 << "Failed to create loop symlink a.";
736 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from
, link_to
))
737 << "Failed to create loop symlink b.";
740 ASSERT_FALSE(file_util::NormalizeFilePath(link_from
, &normalized_path
));
742 #endif // defined(OS_POSIX)
744 TEST_F(FileUtilTest
, DeleteNonExistent
) {
745 FilePath non_existent
= temp_dir_
.path().AppendASCII("bogus_file_dne.foobar");
746 ASSERT_FALSE(file_util::PathExists(non_existent
));
748 EXPECT_TRUE(file_util::Delete(non_existent
, false));
749 ASSERT_FALSE(file_util::PathExists(non_existent
));
750 EXPECT_TRUE(file_util::Delete(non_existent
, true));
751 ASSERT_FALSE(file_util::PathExists(non_existent
));
754 TEST_F(FileUtilTest
, DeleteFile
) {
756 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 1.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
, false));
762 EXPECT_FALSE(file_util::PathExists(file_name
));
764 // Test recursive case, create a new file
765 file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
766 CreateTextFile(file_name
, bogus_content
);
767 ASSERT_TRUE(file_util::PathExists(file_name
));
769 // Make sure it's deleted
770 EXPECT_TRUE(file_util::Delete(file_name
, true));
771 EXPECT_FALSE(file_util::PathExists(file_name
));
775 // Tests that the Delete function works for wild cards, especially
776 // with the recursion flag. Also coincidentally tests PathExists.
777 // TODO(erikkay): see if anyone's actually using this feature of the API
778 TEST_F(FileUtilTest
, DeleteWildCard
) {
779 // Create a file and a directory
780 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteWildCard.txt"));
781 CreateTextFile(file_name
, bogus_content
);
782 ASSERT_TRUE(file_util::PathExists(file_name
));
784 FilePath subdir_path
= temp_dir_
.path().Append(FPL("DeleteWildCardDir"));
785 file_util::CreateDirectory(subdir_path
);
786 ASSERT_TRUE(file_util::PathExists(subdir_path
));
788 // Create the wildcard path
789 FilePath directory_contents
= temp_dir_
.path();
790 directory_contents
= directory_contents
.Append(FPL("*"));
792 // Delete non-recursively and check that only the file is deleted
793 EXPECT_TRUE(file_util::Delete(directory_contents
, false));
794 EXPECT_FALSE(file_util::PathExists(file_name
));
795 EXPECT_TRUE(file_util::PathExists(subdir_path
));
797 // Delete recursively and make sure all contents are deleted
798 EXPECT_TRUE(file_util::Delete(directory_contents
, true));
799 EXPECT_FALSE(file_util::PathExists(file_name
));
800 EXPECT_FALSE(file_util::PathExists(subdir_path
));
803 // TODO(erikkay): see if anyone's actually using this feature of the API
804 TEST_F(FileUtilTest
, DeleteNonExistantWildCard
) {
805 // Create a file and a directory
806 FilePath subdir_path
=
807 temp_dir_
.path().Append(FPL("DeleteNonExistantWildCard"));
808 file_util::CreateDirectory(subdir_path
);
809 ASSERT_TRUE(file_util::PathExists(subdir_path
));
811 // Create the wildcard path
812 FilePath directory_contents
= subdir_path
;
813 directory_contents
= directory_contents
.Append(FPL("*"));
815 // Delete non-recursively and check nothing got deleted
816 EXPECT_TRUE(file_util::Delete(directory_contents
, false));
817 EXPECT_TRUE(file_util::PathExists(subdir_path
));
819 // Delete recursively and check nothing got deleted
820 EXPECT_TRUE(file_util::Delete(directory_contents
, true));
821 EXPECT_TRUE(file_util::PathExists(subdir_path
));
825 // Tests non-recursive Delete() for a directory.
826 TEST_F(FileUtilTest
, DeleteDirNonRecursive
) {
827 // Create a subdirectory and put a file and two directories inside.
828 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirNonRecursive"));
829 file_util::CreateDirectory(test_subdir
);
830 ASSERT_TRUE(file_util::PathExists(test_subdir
));
832 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDir.txt"));
833 CreateTextFile(file_name
, bogus_content
);
834 ASSERT_TRUE(file_util::PathExists(file_name
));
836 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
837 file_util::CreateDirectory(subdir_path1
);
838 ASSERT_TRUE(file_util::PathExists(subdir_path1
));
840 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
841 file_util::CreateDirectory(subdir_path2
);
842 ASSERT_TRUE(file_util::PathExists(subdir_path2
));
844 // Delete non-recursively and check that the empty dir got deleted
845 EXPECT_TRUE(file_util::Delete(subdir_path2
, false));
846 EXPECT_FALSE(file_util::PathExists(subdir_path2
));
848 // Delete non-recursively and check that nothing got deleted
849 EXPECT_FALSE(file_util::Delete(test_subdir
, false));
850 EXPECT_TRUE(file_util::PathExists(test_subdir
));
851 EXPECT_TRUE(file_util::PathExists(file_name
));
852 EXPECT_TRUE(file_util::PathExists(subdir_path1
));
855 // Tests recursive Delete() for a directory.
856 TEST_F(FileUtilTest
, DeleteDirRecursive
) {
857 // Create a subdirectory and put a file and two directories inside.
858 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirRecursive"));
859 file_util::CreateDirectory(test_subdir
);
860 ASSERT_TRUE(file_util::PathExists(test_subdir
));
862 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDirRecursive.txt"));
863 CreateTextFile(file_name
, bogus_content
);
864 ASSERT_TRUE(file_util::PathExists(file_name
));
866 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
867 file_util::CreateDirectory(subdir_path1
);
868 ASSERT_TRUE(file_util::PathExists(subdir_path1
));
870 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
871 file_util::CreateDirectory(subdir_path2
);
872 ASSERT_TRUE(file_util::PathExists(subdir_path2
));
874 // Delete recursively and check that the empty dir got deleted
875 EXPECT_TRUE(file_util::Delete(subdir_path2
, true));
876 EXPECT_FALSE(file_util::PathExists(subdir_path2
));
878 // Delete recursively and check that everything got deleted
879 EXPECT_TRUE(file_util::Delete(test_subdir
, true));
880 EXPECT_FALSE(file_util::PathExists(file_name
));
881 EXPECT_FALSE(file_util::PathExists(subdir_path1
));
882 EXPECT_FALSE(file_util::PathExists(test_subdir
));
885 TEST_F(FileUtilTest
, MoveFileNew
) {
887 FilePath file_name_from
=
888 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
889 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
890 ASSERT_TRUE(file_util::PathExists(file_name_from
));
893 FilePath file_name_to
= temp_dir_
.path().Append(
894 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
895 ASSERT_FALSE(file_util::PathExists(file_name_to
));
897 EXPECT_TRUE(file_util::Move(file_name_from
, file_name_to
));
899 // Check everything has been moved.
900 EXPECT_FALSE(file_util::PathExists(file_name_from
));
901 EXPECT_TRUE(file_util::PathExists(file_name_to
));
904 TEST_F(FileUtilTest
, MoveFileExists
) {
906 FilePath file_name_from
=
907 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
908 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
909 ASSERT_TRUE(file_util::PathExists(file_name_from
));
911 // The destination name.
912 FilePath file_name_to
= temp_dir_
.path().Append(
913 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
914 CreateTextFile(file_name_to
, L
"Old file content");
915 ASSERT_TRUE(file_util::PathExists(file_name_to
));
917 EXPECT_TRUE(file_util::Move(file_name_from
, file_name_to
));
919 // Check everything has been moved.
920 EXPECT_FALSE(file_util::PathExists(file_name_from
));
921 EXPECT_TRUE(file_util::PathExists(file_name_to
));
922 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
925 TEST_F(FileUtilTest
, MoveFileDirExists
) {
927 FilePath file_name_from
=
928 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
929 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
930 ASSERT_TRUE(file_util::PathExists(file_name_from
));
932 // The destination directory
933 FilePath dir_name_to
=
934 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
935 file_util::CreateDirectory(dir_name_to
);
936 ASSERT_TRUE(file_util::PathExists(dir_name_to
));
938 EXPECT_FALSE(file_util::Move(file_name_from
, dir_name_to
));
942 TEST_F(FileUtilTest
, MoveNew
) {
943 // Create a directory
944 FilePath dir_name_from
=
945 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
946 file_util::CreateDirectory(dir_name_from
);
947 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
949 // Create a file under the directory
950 FilePath file_name_from
=
951 dir_name_from
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
952 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
953 ASSERT_TRUE(file_util::PathExists(file_name_from
));
955 // Move the directory.
956 FilePath dir_name_to
=
957 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
958 FilePath file_name_to
=
959 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
961 ASSERT_FALSE(file_util::PathExists(dir_name_to
));
963 EXPECT_TRUE(file_util::Move(dir_name_from
, dir_name_to
));
965 // Check everything has been moved.
966 EXPECT_FALSE(file_util::PathExists(dir_name_from
));
967 EXPECT_FALSE(file_util::PathExists(file_name_from
));
968 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
969 EXPECT_TRUE(file_util::PathExists(file_name_to
));
972 TEST_F(FileUtilTest
, MoveExist
) {
973 // Create a directory
974 FilePath dir_name_from
=
975 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
976 file_util::CreateDirectory(dir_name_from
);
977 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
979 // Create a file under the directory
980 FilePath file_name_from
=
981 dir_name_from
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
982 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
983 ASSERT_TRUE(file_util::PathExists(file_name_from
));
985 // Move the directory
986 FilePath dir_name_exists
=
987 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
989 FilePath dir_name_to
=
990 dir_name_exists
.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
991 FilePath file_name_to
=
992 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
994 // Create the destination directory.
995 file_util::CreateDirectory(dir_name_exists
);
996 ASSERT_TRUE(file_util::PathExists(dir_name_exists
));
998 EXPECT_TRUE(file_util::Move(dir_name_from
, dir_name_to
));
1000 // Check everything has been moved.
1001 EXPECT_FALSE(file_util::PathExists(dir_name_from
));
1002 EXPECT_FALSE(file_util::PathExists(file_name_from
));
1003 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1004 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1007 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyNew
) {
1008 // Create a directory.
1009 FilePath dir_name_from
=
1010 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1011 file_util::CreateDirectory(dir_name_from
);
1012 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1014 // Create a file under the directory.
1015 FilePath file_name_from
=
1016 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1017 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1018 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1020 // Create a subdirectory.
1021 FilePath subdir_name_from
=
1022 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1023 file_util::CreateDirectory(subdir_name_from
);
1024 ASSERT_TRUE(file_util::PathExists(subdir_name_from
));
1026 // Create a file under the subdirectory.
1027 FilePath file_name2_from
=
1028 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1029 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1030 ASSERT_TRUE(file_util::PathExists(file_name2_from
));
1032 // Copy the directory recursively.
1033 FilePath dir_name_to
=
1034 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1035 FilePath file_name_to
=
1036 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1037 FilePath subdir_name_to
=
1038 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1039 FilePath file_name2_to
=
1040 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1042 ASSERT_FALSE(file_util::PathExists(dir_name_to
));
1044 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from
, dir_name_to
, true));
1046 // Check everything has been copied.
1047 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1048 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1049 EXPECT_TRUE(file_util::PathExists(subdir_name_from
));
1050 EXPECT_TRUE(file_util::PathExists(file_name2_from
));
1051 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1052 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1053 EXPECT_TRUE(file_util::PathExists(subdir_name_to
));
1054 EXPECT_TRUE(file_util::PathExists(file_name2_to
));
1057 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyExists
) {
1058 // Create a directory.
1059 FilePath dir_name_from
=
1060 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1061 file_util::CreateDirectory(dir_name_from
);
1062 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1064 // Create a file under the directory.
1065 FilePath file_name_from
=
1066 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1067 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1068 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1070 // Create a subdirectory.
1071 FilePath subdir_name_from
=
1072 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1073 file_util::CreateDirectory(subdir_name_from
);
1074 ASSERT_TRUE(file_util::PathExists(subdir_name_from
));
1076 // Create a file under the subdirectory.
1077 FilePath file_name2_from
=
1078 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1079 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1080 ASSERT_TRUE(file_util::PathExists(file_name2_from
));
1082 // Copy the directory recursively.
1083 FilePath dir_name_exists
=
1084 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1086 FilePath dir_name_to
=
1087 dir_name_exists
.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1088 FilePath file_name_to
=
1089 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1090 FilePath subdir_name_to
=
1091 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1092 FilePath file_name2_to
=
1093 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1095 // Create the destination directory.
1096 file_util::CreateDirectory(dir_name_exists
);
1097 ASSERT_TRUE(file_util::PathExists(dir_name_exists
));
1099 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from
, dir_name_exists
, true));
1101 // Check everything has been copied.
1102 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1103 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1104 EXPECT_TRUE(file_util::PathExists(subdir_name_from
));
1105 EXPECT_TRUE(file_util::PathExists(file_name2_from
));
1106 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1107 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1108 EXPECT_TRUE(file_util::PathExists(subdir_name_to
));
1109 EXPECT_TRUE(file_util::PathExists(file_name2_to
));
1112 TEST_F(FileUtilTest
, CopyDirectoryNew
) {
1113 // Create a directory.
1114 FilePath dir_name_from
=
1115 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1116 file_util::CreateDirectory(dir_name_from
);
1117 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1119 // Create a file under the directory.
1120 FilePath file_name_from
=
1121 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1122 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1123 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1125 // Create a subdirectory.
1126 FilePath subdir_name_from
=
1127 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1128 file_util::CreateDirectory(subdir_name_from
);
1129 ASSERT_TRUE(file_util::PathExists(subdir_name_from
));
1131 // Create a file under the subdirectory.
1132 FilePath file_name2_from
=
1133 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1134 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1135 ASSERT_TRUE(file_util::PathExists(file_name2_from
));
1137 // Copy the directory not recursively.
1138 FilePath dir_name_to
=
1139 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1140 FilePath file_name_to
=
1141 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1142 FilePath subdir_name_to
=
1143 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1145 ASSERT_FALSE(file_util::PathExists(dir_name_to
));
1147 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from
, dir_name_to
, false));
1149 // Check everything has been copied.
1150 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1151 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1152 EXPECT_TRUE(file_util::PathExists(subdir_name_from
));
1153 EXPECT_TRUE(file_util::PathExists(file_name2_from
));
1154 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1155 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1156 EXPECT_FALSE(file_util::PathExists(subdir_name_to
));
1159 TEST_F(FileUtilTest
, CopyDirectoryExists
) {
1160 // Create a directory.
1161 FilePath dir_name_from
=
1162 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1163 file_util::CreateDirectory(dir_name_from
);
1164 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1166 // Create a file under the directory.
1167 FilePath file_name_from
=
1168 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1169 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1170 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1172 // Create a subdirectory.
1173 FilePath subdir_name_from
=
1174 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1175 file_util::CreateDirectory(subdir_name_from
);
1176 ASSERT_TRUE(file_util::PathExists(subdir_name_from
));
1178 // Create a file under the subdirectory.
1179 FilePath file_name2_from
=
1180 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1181 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1182 ASSERT_TRUE(file_util::PathExists(file_name2_from
));
1184 // Copy the directory not recursively.
1185 FilePath dir_name_to
=
1186 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1187 FilePath file_name_to
=
1188 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1189 FilePath subdir_name_to
=
1190 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1192 // Create the destination directory.
1193 file_util::CreateDirectory(dir_name_to
);
1194 ASSERT_TRUE(file_util::PathExists(dir_name_to
));
1196 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from
, dir_name_to
, false));
1198 // Check everything has been copied.
1199 EXPECT_TRUE(file_util::PathExists(dir_name_from
));
1200 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1201 EXPECT_TRUE(file_util::PathExists(subdir_name_from
));
1202 EXPECT_TRUE(file_util::PathExists(file_name2_from
));
1203 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1204 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1205 EXPECT_FALSE(file_util::PathExists(subdir_name_to
));
1208 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToNew
) {
1210 FilePath file_name_from
=
1211 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1212 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1213 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1215 // The destination name
1216 FilePath file_name_to
= temp_dir_
.path().Append(
1217 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1218 ASSERT_FALSE(file_util::PathExists(file_name_to
));
1220 EXPECT_TRUE(file_util::CopyDirectory(file_name_from
, file_name_to
, true));
1222 // Check the has been copied
1223 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1226 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExisting
) {
1228 FilePath file_name_from
=
1229 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1230 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1231 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1233 // The destination name
1234 FilePath file_name_to
= temp_dir_
.path().Append(
1235 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1236 CreateTextFile(file_name_to
, L
"Old file content");
1237 ASSERT_TRUE(file_util::PathExists(file_name_to
));
1239 EXPECT_TRUE(file_util::CopyDirectory(file_name_from
, file_name_to
, true));
1241 // Check the has been copied
1242 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1243 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
1246 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExistingDirectory
) {
1248 FilePath file_name_from
=
1249 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1250 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1251 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1254 FilePath dir_name_to
=
1255 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1256 file_util::CreateDirectory(dir_name_to
);
1257 ASSERT_TRUE(file_util::PathExists(dir_name_to
));
1258 FilePath file_name_to
=
1259 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1261 EXPECT_TRUE(file_util::CopyDirectory(file_name_from
, dir_name_to
, true));
1263 // Check the has been copied
1264 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1267 TEST_F(FileUtilTest
, CopyFile
) {
1268 // Create a directory
1269 FilePath dir_name_from
=
1270 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1271 file_util::CreateDirectory(dir_name_from
);
1272 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1274 // Create a file under the directory
1275 FilePath file_name_from
=
1276 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1277 const std::wstring
file_contents(L
"Gooooooooooooooooooooogle");
1278 CreateTextFile(file_name_from
, file_contents
);
1279 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1282 FilePath dest_file
= dir_name_from
.Append(FILE_PATH_LITERAL("DestFile.txt"));
1283 ASSERT_TRUE(file_util::CopyFile(file_name_from
, dest_file
));
1285 // Copy the file to another location using '..' in the path.
1286 FilePath
dest_file2(dir_name_from
);
1287 dest_file2
= dest_file2
.AppendASCII("..");
1288 dest_file2
= dest_file2
.AppendASCII("DestFile.txt");
1289 ASSERT_TRUE(file_util::CopyFile(file_name_from
, dest_file2
));
1291 FilePath
dest_file2_test(dir_name_from
);
1292 dest_file2_test
= dest_file2_test
.DirName();
1293 dest_file2_test
= dest_file2_test
.AppendASCII("DestFile.txt");
1295 // Check everything has been copied.
1296 EXPECT_TRUE(file_util::PathExists(file_name_from
));
1297 EXPECT_TRUE(file_util::PathExists(dest_file
));
1298 const std::wstring read_contents
= ReadTextFile(dest_file
);
1299 EXPECT_EQ(file_contents
, read_contents
);
1300 EXPECT_TRUE(file_util::PathExists(dest_file2_test
));
1301 EXPECT_TRUE(file_util::PathExists(dest_file2
));
1304 // TODO(erikkay): implement
1306 TEST_F(FileUtilTest
, GetFileCreationLocalTime
) {
1307 FilePath file_name
= temp_dir_
.path().Append(L
"Test File.txt");
1309 SYSTEMTIME start_time
;
1310 GetLocalTime(&start_time
);
1311 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
1312 CreateTextFile(file_name
, L
"New file!");
1313 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
1314 SYSTEMTIME end_time
;
1315 GetLocalTime(&end_time
);
1317 SYSTEMTIME file_creation_time
;
1318 file_util::GetFileCreationLocalTime(file_name
.value(), &file_creation_time
);
1320 FILETIME start_filetime
;
1321 SystemTimeToFileTime(&start_time
, &start_filetime
);
1322 FILETIME end_filetime
;
1323 SystemTimeToFileTime(&end_time
, &end_filetime
);
1324 FILETIME file_creation_filetime
;
1325 SystemTimeToFileTime(&file_creation_time
, &file_creation_filetime
);
1327 EXPECT_EQ(-1, CompareFileTime(&start_filetime
, &file_creation_filetime
)) <<
1328 "start time: " << FileTimeAsUint64(start_filetime
) << ", " <<
1329 "creation time: " << FileTimeAsUint64(file_creation_filetime
);
1331 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime
, &end_filetime
)) <<
1332 "creation time: " << FileTimeAsUint64(file_creation_filetime
) << ", " <<
1333 "end time: " << FileTimeAsUint64(end_filetime
);
1335 ASSERT_TRUE(DeleteFile(file_name
.value().c_str()));
1339 // file_util winds up using autoreleased objects on the Mac, so this needs
1340 // to be a PlatformTest.
1341 typedef PlatformTest ReadOnlyFileUtilTest
;
1343 TEST_F(ReadOnlyFileUtilTest
, ContentsEqual
) {
1345 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &data_dir
));
1346 data_dir
= data_dir
.Append(FILE_PATH_LITERAL("base"))
1347 .Append(FILE_PATH_LITERAL("data"))
1348 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1349 ASSERT_TRUE(file_util::PathExists(data_dir
));
1351 FilePath original_file
=
1352 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1353 FilePath same_file
=
1354 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1355 FilePath same_length_file
=
1356 data_dir
.Append(FILE_PATH_LITERAL("same_length.txt"));
1357 FilePath different_file
=
1358 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1359 FilePath different_first_file
=
1360 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1361 FilePath different_last_file
=
1362 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1363 FilePath empty1_file
=
1364 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1365 FilePath empty2_file
=
1366 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1367 FilePath shortened_file
=
1368 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1369 FilePath binary_file
=
1370 data_dir
.Append(FILE_PATH_LITERAL("binary_file.bin"));
1371 FilePath binary_file_same
=
1372 data_dir
.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1373 FilePath binary_file_diff
=
1374 data_dir
.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
1376 EXPECT_TRUE(file_util::ContentsEqual(original_file
, original_file
));
1377 EXPECT_TRUE(file_util::ContentsEqual(original_file
, same_file
));
1378 EXPECT_FALSE(file_util::ContentsEqual(original_file
, same_length_file
));
1379 EXPECT_FALSE(file_util::ContentsEqual(original_file
, different_file
));
1380 EXPECT_FALSE(file_util::ContentsEqual(
1381 FilePath(FILE_PATH_LITERAL("bogusname")),
1382 FilePath(FILE_PATH_LITERAL("bogusname"))));
1383 EXPECT_FALSE(file_util::ContentsEqual(original_file
, different_first_file
));
1384 EXPECT_FALSE(file_util::ContentsEqual(original_file
, different_last_file
));
1385 EXPECT_TRUE(file_util::ContentsEqual(empty1_file
, empty2_file
));
1386 EXPECT_FALSE(file_util::ContentsEqual(original_file
, shortened_file
));
1387 EXPECT_FALSE(file_util::ContentsEqual(shortened_file
, original_file
));
1388 EXPECT_TRUE(file_util::ContentsEqual(binary_file
, binary_file_same
));
1389 EXPECT_FALSE(file_util::ContentsEqual(binary_file
, binary_file_diff
));
1392 TEST_F(ReadOnlyFileUtilTest
, TextContentsEqual
) {
1394 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &data_dir
));
1395 data_dir
= data_dir
.Append(FILE_PATH_LITERAL("base"))
1396 .Append(FILE_PATH_LITERAL("data"))
1397 .Append(FILE_PATH_LITERAL("file_util_unittest"));
1398 ASSERT_TRUE(file_util::PathExists(data_dir
));
1400 FilePath original_file
=
1401 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1402 FilePath same_file
=
1403 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1404 FilePath crlf_file
=
1405 data_dir
.Append(FILE_PATH_LITERAL("crlf.txt"));
1406 FilePath shortened_file
=
1407 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1408 FilePath different_file
=
1409 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1410 FilePath different_first_file
=
1411 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1412 FilePath different_last_file
=
1413 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1414 FilePath first1_file
=
1415 data_dir
.Append(FILE_PATH_LITERAL("first1.txt"));
1416 FilePath first2_file
=
1417 data_dir
.Append(FILE_PATH_LITERAL("first2.txt"));
1418 FilePath empty1_file
=
1419 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1420 FilePath empty2_file
=
1421 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1422 FilePath blank_line_file
=
1423 data_dir
.Append(FILE_PATH_LITERAL("blank_line.txt"));
1424 FilePath blank_line_crlf_file
=
1425 data_dir
.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1427 EXPECT_TRUE(file_util::TextContentsEqual(original_file
, same_file
));
1428 EXPECT_TRUE(file_util::TextContentsEqual(original_file
, crlf_file
));
1429 EXPECT_FALSE(file_util::TextContentsEqual(original_file
, shortened_file
));
1430 EXPECT_FALSE(file_util::TextContentsEqual(original_file
, different_file
));
1431 EXPECT_FALSE(file_util::TextContentsEqual(original_file
,
1432 different_first_file
));
1433 EXPECT_FALSE(file_util::TextContentsEqual(original_file
,
1434 different_last_file
));
1435 EXPECT_FALSE(file_util::TextContentsEqual(first1_file
, first2_file
));
1436 EXPECT_TRUE(file_util::TextContentsEqual(empty1_file
, empty2_file
));
1437 EXPECT_FALSE(file_util::TextContentsEqual(original_file
, empty1_file
));
1438 EXPECT_TRUE(file_util::TextContentsEqual(blank_line_file
,
1439 blank_line_crlf_file
));
1442 // We don't need equivalent functionality outside of Windows.
1444 TEST_F(FileUtilTest
, ResolveShortcutTest
) {
1445 FilePath target_file
= temp_dir_
.path().Append(L
"Target.txt");
1446 CreateTextFile(target_file
, L
"This is the target.");
1448 FilePath link_file
= temp_dir_
.path().Append(L
"Link.lnk");
1451 IShellLink
*shell
= NULL
;
1452 IPersistFile
*persist
= NULL
;
1455 // Temporarily create a shortcut for test
1456 result
= CoCreateInstance(CLSID_ShellLink
, NULL
,
1457 CLSCTX_INPROC_SERVER
, IID_IShellLink
,
1458 reinterpret_cast<LPVOID
*>(&shell
));
1459 EXPECT_TRUE(SUCCEEDED(result
));
1460 result
= shell
->QueryInterface(IID_IPersistFile
,
1461 reinterpret_cast<LPVOID
*>(&persist
));
1462 EXPECT_TRUE(SUCCEEDED(result
));
1463 result
= shell
->SetPath(target_file
.value().c_str());
1464 EXPECT_TRUE(SUCCEEDED(result
));
1465 result
= shell
->SetDescription(L
"ResolveShortcutTest");
1466 EXPECT_TRUE(SUCCEEDED(result
));
1467 result
= persist
->Save(link_file
.value().c_str(), TRUE
);
1468 EXPECT_TRUE(SUCCEEDED(result
));
1475 is_solved
= file_util::ResolveShortcut(&link_file
);
1476 EXPECT_TRUE(is_solved
);
1477 std::wstring contents
;
1478 contents
= ReadTextFile(link_file
);
1479 EXPECT_EQ(L
"This is the target.", contents
);
1482 DeleteFile(target_file
.value().c_str());
1483 DeleteFile(link_file
.value().c_str());
1487 TEST_F(FileUtilTest
, CreateShortcutTest
) {
1488 const wchar_t file_contents
[] = L
"This is another target.";
1489 FilePath target_file
= temp_dir_
.path().Append(L
"Target1.txt");
1490 CreateTextFile(target_file
, file_contents
);
1492 FilePath link_file
= temp_dir_
.path().Append(L
"Link1.lnk");
1495 EXPECT_TRUE(file_util::CreateShortcutLink(target_file
.value().c_str(),
1496 link_file
.value().c_str(),
1497 NULL
, NULL
, NULL
, NULL
, 0, NULL
));
1498 FilePath resolved_name
= link_file
;
1499 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name
));
1500 std::wstring read_contents
= ReadTextFile(resolved_name
);
1501 EXPECT_EQ(file_contents
, read_contents
);
1503 DeleteFile(target_file
.value().c_str());
1504 DeleteFile(link_file
.value().c_str());
1508 TEST_F(FileUtilTest
, CopyAndDeleteDirectoryTest
) {
1509 // Create a directory
1510 FilePath dir_name_from
=
1511 temp_dir_
.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1512 file_util::CreateDirectory(dir_name_from
);
1513 ASSERT_TRUE(file_util::PathExists(dir_name_from
));
1515 // Create a file under the directory
1516 FilePath file_name_from
=
1517 dir_name_from
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1518 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1519 ASSERT_TRUE(file_util::PathExists(file_name_from
));
1521 // Move the directory by using CopyAndDeleteDirectory
1522 FilePath dir_name_to
= temp_dir_
.path().Append(
1523 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1524 FilePath file_name_to
=
1525 dir_name_to
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1527 ASSERT_FALSE(file_util::PathExists(dir_name_to
));
1529 EXPECT_TRUE(file_util::CopyAndDeleteDirectory(dir_name_from
, dir_name_to
));
1531 // Check everything has been moved.
1532 EXPECT_FALSE(file_util::PathExists(dir_name_from
));
1533 EXPECT_FALSE(file_util::PathExists(file_name_from
));
1534 EXPECT_TRUE(file_util::PathExists(dir_name_to
));
1535 EXPECT_TRUE(file_util::PathExists(file_name_to
));
1538 TEST_F(FileUtilTest
, GetTempDirTest
) {
1539 static const TCHAR
* kTmpKey
= _T("TMP");
1540 static const TCHAR
* kTmpValues
[] = {
1541 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1543 // Save the original $TMP.
1544 size_t original_tmp_size
;
1545 TCHAR
* original_tmp
;
1546 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp
, &original_tmp_size
, kTmpKey
));
1547 // original_tmp may be NULL.
1549 for (unsigned int i
= 0; i
< arraysize(kTmpValues
); ++i
) {
1551 ::_tputenv_s(kTmpKey
, kTmpValues
[i
]);
1552 file_util::GetTempDir(&path
);
1553 EXPECT_TRUE(path
.IsAbsolute()) << "$TMP=" << kTmpValues
[i
] <<
1554 " result=" << path
.value();
1557 // Restore the original $TMP.
1559 ::_tputenv_s(kTmpKey
, original_tmp
);
1562 ::_tputenv_s(kTmpKey
, _T(""));
1567 TEST_F(FileUtilTest
, CreateTemporaryFileTest
) {
1568 FilePath temp_files
[3];
1569 for (int i
= 0; i
< 3; i
++) {
1570 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files
[i
])));
1571 EXPECT_TRUE(file_util::PathExists(temp_files
[i
]));
1572 EXPECT_FALSE(file_util::DirectoryExists(temp_files
[i
]));
1574 for (int i
= 0; i
< 3; i
++)
1575 EXPECT_FALSE(temp_files
[i
] == temp_files
[(i
+1)%3]);
1576 for (int i
= 0; i
< 3; i
++)
1577 EXPECT_TRUE(file_util::Delete(temp_files
[i
], false));
1580 TEST_F(FileUtilTest
, CreateAndOpenTemporaryFileTest
) {
1585 // Create; make sure they are open and exist.
1586 for (i
= 0; i
< 3; ++i
) {
1587 fps
[i
] = file_util::CreateAndOpenTemporaryFile(&(names
[i
]));
1588 ASSERT_TRUE(fps
[i
]);
1589 EXPECT_TRUE(file_util::PathExists(names
[i
]));
1592 // Make sure all names are unique.
1593 for (i
= 0; i
< 3; ++i
) {
1594 EXPECT_FALSE(names
[i
] == names
[(i
+1)%3]);
1597 // Close and delete.
1598 for (i
= 0; i
< 3; ++i
) {
1599 EXPECT_TRUE(file_util::CloseFile(fps
[i
]));
1600 EXPECT_TRUE(file_util::Delete(names
[i
], false));
1604 TEST_F(FileUtilTest
, CreateNewTempDirectoryTest
) {
1606 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1608 EXPECT_TRUE(file_util::PathExists(temp_dir
));
1609 EXPECT_TRUE(file_util::Delete(temp_dir
, false));
1612 TEST_F(FileUtilTest
, CreateNewTemporaryDirInDirTest
) {
1614 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1616 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1618 EXPECT_TRUE(file_util::PathExists(new_dir
));
1619 EXPECT_TRUE(temp_dir_
.path().IsParent(new_dir
));
1620 EXPECT_TRUE(file_util::Delete(new_dir
, false));
1623 TEST_F(FileUtilTest
, GetShmemTempDirTest
) {
1625 EXPECT_TRUE(file_util::GetShmemTempDir(&dir
, false));
1626 EXPECT_TRUE(file_util::DirectoryExists(dir
));
1629 TEST_F(FileUtilTest
, CreateDirectoryTest
) {
1630 FilePath test_root
=
1631 temp_dir_
.path().Append(FILE_PATH_LITERAL("create_directory_test"));
1633 FilePath test_path
=
1634 test_root
.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
1635 #elif defined(OS_POSIX)
1636 FilePath test_path
=
1637 test_root
.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
1640 EXPECT_FALSE(file_util::PathExists(test_path
));
1641 EXPECT_TRUE(file_util::CreateDirectory(test_path
));
1642 EXPECT_TRUE(file_util::PathExists(test_path
));
1643 // CreateDirectory returns true if the DirectoryExists returns true.
1644 EXPECT_TRUE(file_util::CreateDirectory(test_path
));
1646 // Doesn't work to create it on top of a non-dir
1647 test_path
= test_path
.Append(FILE_PATH_LITERAL("foobar.txt"));
1648 EXPECT_FALSE(file_util::PathExists(test_path
));
1649 CreateTextFile(test_path
, L
"test file");
1650 EXPECT_TRUE(file_util::PathExists(test_path
));
1651 EXPECT_FALSE(file_util::CreateDirectory(test_path
));
1653 EXPECT_TRUE(file_util::Delete(test_root
, true));
1654 EXPECT_FALSE(file_util::PathExists(test_root
));
1655 EXPECT_FALSE(file_util::PathExists(test_path
));
1657 // Verify assumptions made by the Windows implementation:
1658 // 1. The current directory always exists.
1659 // 2. The root directory always exists.
1660 ASSERT_TRUE(file_util::DirectoryExists(
1661 FilePath(FilePath::kCurrentDirectory
)));
1662 FilePath top_level
= test_root
;
1663 while (top_level
!= top_level
.DirName()) {
1664 top_level
= top_level
.DirName();
1666 ASSERT_TRUE(file_util::DirectoryExists(top_level
));
1668 // Given these assumptions hold, it should be safe to
1669 // test that "creating" these directories succeeds.
1670 EXPECT_TRUE(file_util::CreateDirectory(
1671 FilePath(FilePath::kCurrentDirectory
)));
1672 EXPECT_TRUE(file_util::CreateDirectory(top_level
));
1675 FilePath
invalid_drive(FILE_PATH_LITERAL("o:\\"));
1676 FilePath invalid_path
=
1677 invalid_drive
.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1678 if (!file_util::PathExists(invalid_drive
)) {
1679 EXPECT_FALSE(file_util::CreateDirectory(invalid_path
));
1684 TEST_F(FileUtilTest
, DetectDirectoryTest
) {
1685 // Check a directory
1686 FilePath test_root
=
1687 temp_dir_
.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
1688 EXPECT_FALSE(file_util::PathExists(test_root
));
1689 EXPECT_TRUE(file_util::CreateDirectory(test_root
));
1690 EXPECT_TRUE(file_util::PathExists(test_root
));
1691 EXPECT_TRUE(file_util::DirectoryExists(test_root
));
1693 FilePath test_path
=
1694 test_root
.Append(FILE_PATH_LITERAL("foobar.txt"));
1695 EXPECT_FALSE(file_util::PathExists(test_path
));
1696 CreateTextFile(test_path
, L
"test file");
1697 EXPECT_TRUE(file_util::PathExists(test_path
));
1698 EXPECT_FALSE(file_util::DirectoryExists(test_path
));
1699 EXPECT_TRUE(file_util::Delete(test_path
, false));
1701 EXPECT_TRUE(file_util::Delete(test_root
, true));
1704 TEST_F(FileUtilTest
, FileEnumeratorTest
) {
1705 // Test an empty directory.
1706 file_util::FileEnumerator
f0(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1707 EXPECT_EQ(f0
.Next().value(), FILE_PATH_LITERAL(""));
1708 EXPECT_EQ(f0
.Next().value(), FILE_PATH_LITERAL(""));
1710 // Test an empty directory, non-recursively, including "..".
1711 file_util::FileEnumerator
f0_dotdot(temp_dir_
.path(), false,
1712 static_cast<file_util::FileEnumerator::FileType
>(
1713 FILES_AND_DIRECTORIES
| file_util::FileEnumerator::INCLUDE_DOT_DOT
));
1714 EXPECT_EQ(temp_dir_
.path().Append(FILE_PATH_LITERAL("..")).value(),
1715 f0_dotdot
.Next().value());
1716 EXPECT_EQ(FILE_PATH_LITERAL(""),
1717 f0_dotdot
.Next().value());
1719 // create the directories
1720 FilePath dir1
= temp_dir_
.path().Append(FILE_PATH_LITERAL("dir1"));
1721 EXPECT_TRUE(file_util::CreateDirectory(dir1
));
1722 FilePath dir2
= temp_dir_
.path().Append(FILE_PATH_LITERAL("dir2"));
1723 EXPECT_TRUE(file_util::CreateDirectory(dir2
));
1724 FilePath dir2inner
= dir2
.Append(FILE_PATH_LITERAL("inner"));
1725 EXPECT_TRUE(file_util::CreateDirectory(dir2inner
));
1728 FilePath dir2file
= dir2
.Append(FILE_PATH_LITERAL("dir2file.txt"));
1729 CreateTextFile(dir2file
, L
"");
1730 FilePath dir2innerfile
= dir2inner
.Append(FILE_PATH_LITERAL("innerfile.txt"));
1731 CreateTextFile(dir2innerfile
, L
"");
1732 FilePath file1
= temp_dir_
.path().Append(FILE_PATH_LITERAL("file1.txt"));
1733 CreateTextFile(file1
, L
"");
1734 FilePath file2_rel
=
1735 dir2
.Append(FilePath::kParentDirectory
)
1736 .Append(FILE_PATH_LITERAL("file2.txt"));
1737 CreateTextFile(file2_rel
, L
"");
1738 FilePath file2_abs
= temp_dir_
.path().Append(FILE_PATH_LITERAL("file2.txt"));
1740 // Only enumerate files.
1741 file_util::FileEnumerator
f1(temp_dir_
.path(), true,
1742 file_util::FileEnumerator::FILES
);
1743 FindResultCollector
c1(f1
);
1744 EXPECT_TRUE(c1
.HasFile(file1
));
1745 EXPECT_TRUE(c1
.HasFile(file2_abs
));
1746 EXPECT_TRUE(c1
.HasFile(dir2file
));
1747 EXPECT_TRUE(c1
.HasFile(dir2innerfile
));
1748 EXPECT_EQ(c1
.size(), 4);
1750 // Only enumerate directories.
1751 file_util::FileEnumerator
f2(temp_dir_
.path(), true,
1752 file_util::FileEnumerator::DIRECTORIES
);
1753 FindResultCollector
c2(f2
);
1754 EXPECT_TRUE(c2
.HasFile(dir1
));
1755 EXPECT_TRUE(c2
.HasFile(dir2
));
1756 EXPECT_TRUE(c2
.HasFile(dir2inner
));
1757 EXPECT_EQ(c2
.size(), 3);
1759 // Only enumerate directories non-recursively.
1760 file_util::FileEnumerator
f2_non_recursive(
1761 temp_dir_
.path(), false, file_util::FileEnumerator::DIRECTORIES
);
1762 FindResultCollector
c2_non_recursive(f2_non_recursive
);
1763 EXPECT_TRUE(c2_non_recursive
.HasFile(dir1
));
1764 EXPECT_TRUE(c2_non_recursive
.HasFile(dir2
));
1765 EXPECT_EQ(c2_non_recursive
.size(), 2);
1767 // Only enumerate directories, non-recursively, including "..".
1768 file_util::FileEnumerator
f2_dotdot(
1769 temp_dir_
.path(), false,
1770 static_cast<file_util::FileEnumerator::FileType
>(
1771 file_util::FileEnumerator::DIRECTORIES
|
1772 file_util::FileEnumerator::INCLUDE_DOT_DOT
));
1773 FindResultCollector
c2_dotdot(f2_dotdot
);
1774 EXPECT_TRUE(c2_dotdot
.HasFile(dir1
));
1775 EXPECT_TRUE(c2_dotdot
.HasFile(dir2
));
1776 EXPECT_TRUE(c2_dotdot
.HasFile(
1777 temp_dir_
.path().Append(FILE_PATH_LITERAL(".."))));
1778 EXPECT_EQ(c2_dotdot
.size(), 3);
1780 // Enumerate files and directories.
1781 file_util::FileEnumerator
f3(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1782 FindResultCollector
c3(f3
);
1783 EXPECT_TRUE(c3
.HasFile(dir1
));
1784 EXPECT_TRUE(c3
.HasFile(dir2
));
1785 EXPECT_TRUE(c3
.HasFile(file1
));
1786 EXPECT_TRUE(c3
.HasFile(file2_abs
));
1787 EXPECT_TRUE(c3
.HasFile(dir2file
));
1788 EXPECT_TRUE(c3
.HasFile(dir2inner
));
1789 EXPECT_TRUE(c3
.HasFile(dir2innerfile
));
1790 EXPECT_EQ(c3
.size(), 7);
1792 // Non-recursive operation.
1793 file_util::FileEnumerator
f4(temp_dir_
.path(), false, FILES_AND_DIRECTORIES
);
1794 FindResultCollector
c4(f4
);
1795 EXPECT_TRUE(c4
.HasFile(dir2
));
1796 EXPECT_TRUE(c4
.HasFile(dir2
));
1797 EXPECT_TRUE(c4
.HasFile(file1
));
1798 EXPECT_TRUE(c4
.HasFile(file2_abs
));
1799 EXPECT_EQ(c4
.size(), 4);
1801 // Enumerate with a pattern.
1802 file_util::FileEnumerator
f5(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
,
1803 FILE_PATH_LITERAL("dir*"));
1804 FindResultCollector
c5(f5
);
1805 EXPECT_TRUE(c5
.HasFile(dir1
));
1806 EXPECT_TRUE(c5
.HasFile(dir2
));
1807 EXPECT_TRUE(c5
.HasFile(dir2file
));
1808 EXPECT_TRUE(c5
.HasFile(dir2inner
));
1809 EXPECT_TRUE(c5
.HasFile(dir2innerfile
));
1810 EXPECT_EQ(c5
.size(), 5);
1812 // Make sure the destructor closes the find handle while in the middle of a
1813 // query to allow TearDown to delete the directory.
1814 file_util::FileEnumerator
f6(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1815 EXPECT_FALSE(f6
.Next().value().empty()); // Should have found something
1816 // (we don't care what).
1819 TEST_F(FileUtilTest
, Contains
) {
1821 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1823 // Create a fresh, empty copy of this directory.
1824 if (file_util::PathExists(data_dir
)) {
1825 ASSERT_TRUE(file_util::Delete(data_dir
, true));
1827 ASSERT_TRUE(file_util::CreateDirectory(data_dir
));
1829 FilePath
foo(data_dir
.Append(FILE_PATH_LITERAL("foo")));
1830 FilePath
bar(foo
.Append(FILE_PATH_LITERAL("bar.txt")));
1831 FilePath
baz(data_dir
.Append(FILE_PATH_LITERAL("baz.txt")));
1832 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
1834 // Annoyingly, the directories must actually exist in order for realpath(),
1835 // which Contains() relies on in posix, to work.
1836 ASSERT_TRUE(file_util::CreateDirectory(foo
));
1837 std::string
data("hello");
1838 ASSERT_TRUE(file_util::WriteFile(bar
, data
.c_str(), data
.length()));
1839 ASSERT_TRUE(file_util::WriteFile(baz
, data
.c_str(), data
.length()));
1840 ASSERT_TRUE(file_util::WriteFile(foobar
, data
.c_str(), data
.length()));
1842 EXPECT_TRUE(file_util::ContainsPath(foo
, bar
));
1843 EXPECT_FALSE(file_util::ContainsPath(foo
, baz
));
1844 EXPECT_FALSE(file_util::ContainsPath(foo
, foobar
));
1845 EXPECT_FALSE(file_util::ContainsPath(foo
, foo
));
1847 // Platform-specific concerns.
1848 FilePath
foo_caps(data_dir
.Append(FILE_PATH_LITERAL("FOO")));
1850 EXPECT_TRUE(file_util::ContainsPath(foo
,
1851 foo_caps
.Append(FILE_PATH_LITERAL("bar.txt"))));
1852 EXPECT_TRUE(file_util::ContainsPath(foo
,
1853 FilePath(foo
.value() + FILE_PATH_LITERAL("/bar.txt"))));
1854 #elif defined(OS_MACOSX)
1855 // We can't really do this test on OS X since the case-sensitivity of the
1856 // filesystem is configurable.
1857 #elif defined(OS_POSIX)
1858 EXPECT_FALSE(file_util::ContainsPath(foo
,
1859 foo_caps
.Append(FILE_PATH_LITERAL("bar.txt"))));
1863 TEST_F(FileUtilTest
, TouchFile
) {
1865 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1867 // Create a fresh, empty copy of this directory.
1868 if (file_util::PathExists(data_dir
)) {
1869 ASSERT_TRUE(file_util::Delete(data_dir
, true));
1871 ASSERT_TRUE(file_util::CreateDirectory(data_dir
));
1873 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
1874 std::string
data("hello");
1875 ASSERT_TRUE(file_util::WriteFile(foobar
, data
.c_str(), data
.length()));
1877 base::Time access_time
;
1878 // This timestamp is divisible by one day (in local timezone),
1879 // to make it work on FAT too.
1880 ASSERT_TRUE(base::Time::FromString("Wed, 16 Nov 1994, 00:00:00",
1883 base::Time modification_time
;
1884 // Note that this timestamp is divisible by two (seconds) - FAT stores
1885 // modification times with 2s resolution.
1886 ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
1887 &modification_time
));
1889 ASSERT_TRUE(file_util::TouchFile(foobar
, access_time
, modification_time
));
1890 base::PlatformFileInfo file_info
;
1891 ASSERT_TRUE(file_util::GetFileInfo(foobar
, &file_info
));
1892 EXPECT_EQ(file_info
.last_accessed
.ToInternalValue(),
1893 access_time
.ToInternalValue());
1894 EXPECT_EQ(file_info
.last_modified
.ToInternalValue(),
1895 modification_time
.ToInternalValue());
1898 TEST_F(FileUtilTest
, IsDirectoryEmpty
) {
1899 FilePath empty_dir
= temp_dir_
.path().Append(FILE_PATH_LITERAL("EmptyDir"));
1901 ASSERT_FALSE(file_util::PathExists(empty_dir
));
1903 ASSERT_TRUE(file_util::CreateDirectory(empty_dir
));
1905 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir
));
1907 FilePath
foo(empty_dir
.Append(FILE_PATH_LITERAL("foo.txt")));
1908 std::string
bar("baz");
1909 ASSERT_TRUE(file_util::WriteFile(foo
, bar
.c_str(), bar
.length()));
1911 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir
));
1914 #if defined(OS_POSIX)
1916 // Testing VerifyPathControlledByAdmin() is hard, because there is no
1917 // way a test can make a file owned by root, or change file paths
1918 // at the root of the file system. VerifyPathControlledByAdmin()
1919 // is implemented as a call to VerifyPathControlledByUser, which gives
1920 // us the ability to test with paths under the test's temp directory,
1921 // using a user id we control.
1922 // Pull tests of VerifyPathControlledByUserTest() into a separate test class
1923 // with a common SetUp() method.
1924 class VerifyPathControlledByUserTest
: public FileUtilTest
{
1926 virtual void SetUp() {
1927 FileUtilTest::SetUp();
1929 // Create a basic structure used by each test.
1934 base_dir_
= temp_dir_
.path().AppendASCII("base_dir");
1935 ASSERT_TRUE(file_util::CreateDirectory(base_dir_
));
1937 sub_dir_
= base_dir_
.AppendASCII("sub_dir");
1938 ASSERT_TRUE(file_util::CreateDirectory(sub_dir_
));
1940 text_file_
= sub_dir_
.AppendASCII("file.txt");
1941 CreateTextFile(text_file_
, L
"This text file has some text in it.");
1943 // Get the user and group files are created with from |base_dir_|.
1944 struct stat stat_buf
;
1945 ASSERT_EQ(0, stat(base_dir_
.value().c_str(), &stat_buf
));
1946 uid_
= stat_buf
.st_uid
;
1947 ok_gids_
.insert(stat_buf
.st_gid
);
1948 bad_gids_
.insert(stat_buf
.st_gid
+ 1);
1950 ASSERT_EQ(uid_
, getuid()); // This process should be the owner.
1952 // To ensure that umask settings do not cause the initial state
1953 // of permissions to be different from what we expect, explicitly
1954 // set permissions on the directories we create.
1955 // Make all files and directories non-world-writable.
1956 mode_t enabled_permissions
=
1957 S_IRWXU
| // User can read, write, traverse
1958 S_IRWXG
; // Group can read, write, traverse
1959 mode_t disabled_permissions
=
1960 S_IRWXO
; // Other users can't read, write, traverse.
1962 ASSERT_NO_FATAL_FAILURE(
1963 ChangePosixFilePermissions(
1964 base_dir_
, enabled_permissions
, disabled_permissions
));
1965 ASSERT_NO_FATAL_FAILURE(
1966 ChangePosixFilePermissions(
1967 sub_dir_
, enabled_permissions
, disabled_permissions
));
1972 FilePath text_file_
;
1975 std::set
<gid_t
> ok_gids_
;
1976 std::set
<gid_t
> bad_gids_
;
1979 TEST_F(VerifyPathControlledByUserTest
, BadPaths
) {
1980 // File does not exist.
1981 FilePath does_not_exist
= base_dir_
.AppendASCII("does")
1983 .AppendASCII("exist");
1985 file_util::VerifyPathControlledByUser(
1986 base_dir_
, does_not_exist
, uid_
, ok_gids_
));
1988 // |base| not a subpath of |path|.
1990 file_util::VerifyPathControlledByUser(
1991 sub_dir_
, base_dir_
, uid_
, ok_gids_
));
1993 // An empty base path will fail to be a prefix for any path.
1996 file_util::VerifyPathControlledByUser(
1997 empty
, base_dir_
, uid_
, ok_gids_
));
1999 // Finding that a bad call fails proves nothing unless a good call succeeds.
2001 file_util::VerifyPathControlledByUser(
2002 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2005 TEST_F(VerifyPathControlledByUserTest
, Symlinks
) {
2006 // Symlinks in the path should cause failure.
2008 // Symlink to the file at the end of the path.
2009 FilePath file_link
= base_dir_
.AppendASCII("file_link");
2010 ASSERT_TRUE(file_util::CreateSymbolicLink(text_file_
, file_link
))
2011 << "Failed to create symlink.";
2014 file_util::VerifyPathControlledByUser(
2015 base_dir_
, file_link
, uid_
, ok_gids_
));
2017 file_util::VerifyPathControlledByUser(
2018 file_link
, file_link
, uid_
, ok_gids_
));
2020 // Symlink from one directory to another within the path.
2021 FilePath link_to_sub_dir
= base_dir_
.AppendASCII("link_to_sub_dir");
2022 ASSERT_TRUE(file_util::CreateSymbolicLink(sub_dir_
, link_to_sub_dir
))
2023 << "Failed to create symlink.";
2025 FilePath file_path_with_link
= link_to_sub_dir
.AppendASCII("file.txt");
2026 ASSERT_TRUE(file_util::PathExists(file_path_with_link
));
2029 file_util::VerifyPathControlledByUser(
2030 base_dir_
, file_path_with_link
, uid_
, ok_gids_
));
2033 file_util::VerifyPathControlledByUser(
2034 link_to_sub_dir
, file_path_with_link
, uid_
, ok_gids_
));
2036 // Symlinks in parents of base path are allowed.
2038 file_util::VerifyPathControlledByUser(
2039 file_path_with_link
, file_path_with_link
, uid_
, ok_gids_
));
2042 TEST_F(VerifyPathControlledByUserTest
, OwnershipChecks
) {
2043 // Get a uid that is not the uid of files we create.
2044 uid_t bad_uid
= uid_
+ 1;
2046 // Make all files and directories non-world-writable.
2047 ASSERT_NO_FATAL_FAILURE(
2048 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2049 ASSERT_NO_FATAL_FAILURE(
2050 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2051 ASSERT_NO_FATAL_FAILURE(
2052 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2054 // We control these paths.
2056 file_util::VerifyPathControlledByUser(
2057 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2059 file_util::VerifyPathControlledByUser(
2060 base_dir_
, text_file_
, uid_
, ok_gids_
));
2062 file_util::VerifyPathControlledByUser(
2063 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2065 // Another user does not control these paths.
2067 file_util::VerifyPathControlledByUser(
2068 base_dir_
, sub_dir_
, bad_uid
, ok_gids_
));
2070 file_util::VerifyPathControlledByUser(
2071 base_dir_
, text_file_
, bad_uid
, ok_gids_
));
2073 file_util::VerifyPathControlledByUser(
2074 sub_dir_
, text_file_
, bad_uid
, ok_gids_
));
2076 // Another group does not control the paths.
2078 file_util::VerifyPathControlledByUser(
2079 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2081 file_util::VerifyPathControlledByUser(
2082 base_dir_
, text_file_
, uid_
, bad_gids_
));
2084 file_util::VerifyPathControlledByUser(
2085 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2088 TEST_F(VerifyPathControlledByUserTest
, GroupWriteTest
) {
2089 // Make all files and directories writable only by their owner.
2090 ASSERT_NO_FATAL_FAILURE(
2091 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2092 ASSERT_NO_FATAL_FAILURE(
2093 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2094 ASSERT_NO_FATAL_FAILURE(
2095 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
|S_IWGRP
));
2097 // Any group is okay because the path is not group-writable.
2099 file_util::VerifyPathControlledByUser(
2100 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2102 file_util::VerifyPathControlledByUser(
2103 base_dir_
, text_file_
, uid_
, ok_gids_
));
2105 file_util::VerifyPathControlledByUser(
2106 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2109 file_util::VerifyPathControlledByUser(
2110 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2112 file_util::VerifyPathControlledByUser(
2113 base_dir_
, text_file_
, uid_
, bad_gids_
));
2115 file_util::VerifyPathControlledByUser(
2116 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2118 // No group is okay, because we don't check the group
2119 // if no group can write.
2120 std::set
<gid_t
> no_gids
; // Empty set of gids.
2122 file_util::VerifyPathControlledByUser(
2123 base_dir_
, sub_dir_
, uid_
, no_gids
));
2125 file_util::VerifyPathControlledByUser(
2126 base_dir_
, text_file_
, uid_
, no_gids
));
2128 file_util::VerifyPathControlledByUser(
2129 sub_dir_
, text_file_
, uid_
, no_gids
));
2132 // Make all files and directories writable by their group.
2133 ASSERT_NO_FATAL_FAILURE(
2134 ChangePosixFilePermissions(base_dir_
, S_IWGRP
, 0u));
2135 ASSERT_NO_FATAL_FAILURE(
2136 ChangePosixFilePermissions(sub_dir_
, S_IWGRP
, 0u));
2137 ASSERT_NO_FATAL_FAILURE(
2138 ChangePosixFilePermissions(text_file_
, S_IWGRP
, 0u));
2140 // Now |ok_gids_| works, but |bad_gids_| fails.
2142 file_util::VerifyPathControlledByUser(
2143 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2145 file_util::VerifyPathControlledByUser(
2146 base_dir_
, text_file_
, uid_
, ok_gids_
));
2148 file_util::VerifyPathControlledByUser(
2149 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2152 file_util::VerifyPathControlledByUser(
2153 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2155 file_util::VerifyPathControlledByUser(
2156 base_dir_
, text_file_
, uid_
, bad_gids_
));
2158 file_util::VerifyPathControlledByUser(
2159 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2161 // Because any group in the group set is allowed,
2162 // the union of good and bad gids passes.
2164 std::set
<gid_t
> multiple_gids
;
2166 ok_gids_
.begin(), ok_gids_
.end(),
2167 bad_gids_
.begin(), bad_gids_
.end(),
2168 std::inserter(multiple_gids
, multiple_gids
.begin()));
2171 file_util::VerifyPathControlledByUser(
2172 base_dir_
, sub_dir_
, uid_
, multiple_gids
));
2174 file_util::VerifyPathControlledByUser(
2175 base_dir_
, text_file_
, uid_
, multiple_gids
));
2177 file_util::VerifyPathControlledByUser(
2178 sub_dir_
, text_file_
, uid_
, multiple_gids
));
2182 TEST_F(VerifyPathControlledByUserTest
, WriteBitChecks
) {
2183 // Make all files and directories non-world-writable.
2184 ASSERT_NO_FATAL_FAILURE(
2185 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2186 ASSERT_NO_FATAL_FAILURE(
2187 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2188 ASSERT_NO_FATAL_FAILURE(
2189 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2191 // Initialy, we control all parts of the path.
2193 file_util::VerifyPathControlledByUser(
2194 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2196 file_util::VerifyPathControlledByUser(
2197 base_dir_
, text_file_
, uid_
, ok_gids_
));
2199 file_util::VerifyPathControlledByUser(
2200 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2202 // Make base_dir_ world-writable.
2203 ASSERT_NO_FATAL_FAILURE(
2204 ChangePosixFilePermissions(base_dir_
, S_IWOTH
, 0u));
2206 file_util::VerifyPathControlledByUser(
2207 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2209 file_util::VerifyPathControlledByUser(
2210 base_dir_
, text_file_
, uid_
, ok_gids_
));
2212 file_util::VerifyPathControlledByUser(
2213 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2215 // Make sub_dir_ world writable.
2216 ASSERT_NO_FATAL_FAILURE(
2217 ChangePosixFilePermissions(sub_dir_
, S_IWOTH
, 0u));
2219 file_util::VerifyPathControlledByUser(
2220 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2222 file_util::VerifyPathControlledByUser(
2223 base_dir_
, text_file_
, uid_
, ok_gids_
));
2225 file_util::VerifyPathControlledByUser(
2226 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2228 // Make text_file_ world writable.
2229 ASSERT_NO_FATAL_FAILURE(
2230 ChangePosixFilePermissions(text_file_
, S_IWOTH
, 0u));
2232 file_util::VerifyPathControlledByUser(
2233 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2235 file_util::VerifyPathControlledByUser(
2236 base_dir_
, text_file_
, uid_
, ok_gids_
));
2238 file_util::VerifyPathControlledByUser(
2239 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2241 // Make sub_dir_ non-world writable.
2242 ASSERT_NO_FATAL_FAILURE(
2243 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2245 file_util::VerifyPathControlledByUser(
2246 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2248 file_util::VerifyPathControlledByUser(
2249 base_dir_
, text_file_
, uid_
, ok_gids_
));
2251 file_util::VerifyPathControlledByUser(
2252 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2254 // Make base_dir_ non-world-writable.
2255 ASSERT_NO_FATAL_FAILURE(
2256 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2258 file_util::VerifyPathControlledByUser(
2259 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2261 file_util::VerifyPathControlledByUser(
2262 base_dir_
, text_file_
, uid_
, ok_gids_
));
2264 file_util::VerifyPathControlledByUser(
2265 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2267 // Back to the initial state: Nothing is writable, so every path
2269 ASSERT_NO_FATAL_FAILURE(
2270 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2272 file_util::VerifyPathControlledByUser(
2273 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2275 file_util::VerifyPathControlledByUser(
2276 base_dir_
, text_file_
, uid_
, ok_gids_
));
2278 file_util::VerifyPathControlledByUser(
2279 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2282 #endif // defined(OS_POSIX)