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"
25 #include "base/base_paths.h"
26 #include "base/file_util.h"
27 #include "base/files/file_enumerator.h"
28 #include "base/files/file_path.h"
29 #include "base/files/scoped_file.h"
30 #include "base/files/scoped_temp_dir.h"
31 #include "base/path_service.h"
32 #include "base/strings/utf_string_conversions.h"
33 #include "base/test/test_file_util.h"
34 #include "base/threading/platform_thread.h"
35 #include "testing/gtest/include/gtest/gtest.h"
36 #include "testing/platform_test.h"
39 #include "base/win/scoped_handle.h"
40 #include "base/win/windows_version.h"
43 #if defined(OS_ANDROID)
44 #include "base/android/content_uri_utils.h"
47 // This macro helps avoid wrapped lines in the test structs.
48 #define FPL(x) FILE_PATH_LITERAL(x)
54 // To test that file_util::Normalize FilePath() deals with NTFS reparse points
55 // correctly, we need functions to create and delete reparse points.
57 typedef struct _REPARSE_DATA_BUFFER
{
59 USHORT ReparseDataLength
;
63 USHORT SubstituteNameOffset
;
64 USHORT SubstituteNameLength
;
65 USHORT PrintNameOffset
;
66 USHORT PrintNameLength
;
69 } SymbolicLinkReparseBuffer
;
71 USHORT SubstituteNameOffset
;
72 USHORT SubstituteNameLength
;
73 USHORT PrintNameOffset
;
74 USHORT PrintNameLength
;
76 } MountPointReparseBuffer
;
79 } GenericReparseBuffer
;
81 } REPARSE_DATA_BUFFER
, *PREPARSE_DATA_BUFFER
;
83 // Sets a reparse point. |source| will now point to |target|. Returns true if
84 // the call succeeds, false otherwise.
85 bool SetReparsePoint(HANDLE source
, const FilePath
& target_path
) {
86 std::wstring kPathPrefix
= L
"\\??\\";
87 std::wstring target_str
;
88 // The juction will not work if the target path does not start with \??\ .
89 if (kPathPrefix
!= target_path
.value().substr(0, kPathPrefix
.size()))
90 target_str
+= kPathPrefix
;
91 target_str
+= target_path
.value();
92 const wchar_t* target
= target_str
.c_str();
93 USHORT size_target
= static_cast<USHORT
>(wcslen(target
)) * sizeof(target
[0]);
94 char buffer
[2000] = {0};
97 REPARSE_DATA_BUFFER
* data
= reinterpret_cast<REPARSE_DATA_BUFFER
*>(buffer
);
99 data
->ReparseTag
= 0xa0000003;
100 memcpy(data
->MountPointReparseBuffer
.PathBuffer
, target
, size_target
+ 2);
102 data
->MountPointReparseBuffer
.SubstituteNameLength
= size_target
;
103 data
->MountPointReparseBuffer
.PrintNameOffset
= size_target
+ 2;
104 data
->ReparseDataLength
= size_target
+ 4 + 8;
106 int data_size
= data
->ReparseDataLength
+ 8;
108 if (!DeviceIoControl(source
, FSCTL_SET_REPARSE_POINT
, &buffer
, data_size
,
109 NULL
, 0, &returned
, NULL
)) {
115 // Delete the reparse point referenced by |source|. Returns true if the call
116 // succeeds, false otherwise.
117 bool DeleteReparsePoint(HANDLE source
) {
119 REPARSE_DATA_BUFFER data
= {0};
120 data
.ReparseTag
= 0xa0000003;
121 if (!DeviceIoControl(source
, FSCTL_DELETE_REPARSE_POINT
, &data
, 8, NULL
, 0,
128 // Manages a reparse point for a test.
131 // Creates a reparse point from |source| (an empty directory) to |target|.
132 ReparsePoint(const FilePath
& source
, const FilePath
& target
) {
134 ::CreateFile(source
.value().c_str(),
136 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
139 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
141 created_
= dir_
.IsValid() && SetReparsePoint(dir_
, target
);
146 DeleteReparsePoint(dir_
);
149 bool IsValid() { return created_
; }
152 win::ScopedHandle dir_
;
154 DISALLOW_COPY_AND_ASSIGN(ReparsePoint
);
159 #if defined(OS_POSIX)
160 // Provide a simple way to change the permissions bits on |path| in tests.
161 // ASSERT failures will return, but not stop the test. Caller should wrap
162 // calls to this function in ASSERT_NO_FATAL_FAILURE().
163 void ChangePosixFilePermissions(const FilePath
& path
,
164 int mode_bits_to_set
,
165 int mode_bits_to_clear
) {
166 ASSERT_FALSE(mode_bits_to_set
& mode_bits_to_clear
)
167 << "Can't set and clear the same bits.";
170 ASSERT_TRUE(GetPosixFilePermissions(path
, &mode
));
171 mode
|= mode_bits_to_set
;
172 mode
&= ~mode_bits_to_clear
;
173 ASSERT_TRUE(SetPosixFilePermissions(path
, mode
));
175 #endif // defined(OS_POSIX)
177 const wchar_t bogus_content
[] = L
"I'm cannon fodder.";
179 const int FILES_AND_DIRECTORIES
=
180 FileEnumerator::FILES
| FileEnumerator::DIRECTORIES
;
182 // file_util winds up using autoreleased objects on the Mac, so this needs
183 // to be a PlatformTest
184 class FileUtilTest
: public PlatformTest
{
186 virtual void SetUp() OVERRIDE
{
187 PlatformTest::SetUp();
188 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
191 ScopedTempDir temp_dir_
;
194 // Collects all the results from the given file enumerator, and provides an
195 // interface to query whether a given file is present.
196 class FindResultCollector
{
198 explicit FindResultCollector(FileEnumerator
& enumerator
) {
200 while (!(cur_file
= enumerator
.Next()).value().empty()) {
201 FilePath::StringType path
= cur_file
.value();
202 // The file should not be returned twice.
203 EXPECT_TRUE(files_
.end() == files_
.find(path
))
204 << "Same file returned twice";
211 // Returns true if the enumerator found the file.
212 bool HasFile(const FilePath
& file
) const {
213 return files_
.find(file
.value()) != files_
.end();
217 return static_cast<int>(files_
.size());
221 std::set
<FilePath::StringType
> files_
;
224 // Simple function to dump some text into a new file.
225 void CreateTextFile(const FilePath
& filename
,
226 const std::wstring
& contents
) {
228 file
.open(filename
.value().c_str());
229 ASSERT_TRUE(file
.is_open());
234 // Simple function to take out some text from a file.
235 std::wstring
ReadTextFile(const FilePath
& filename
) {
236 wchar_t contents
[64];
238 file
.open(filename
.value().c_str());
239 EXPECT_TRUE(file
.is_open());
240 file
.getline(contents
, arraysize(contents
));
242 return std::wstring(contents
);
246 uint64
FileTimeAsUint64(const FILETIME
& ft
) {
248 u
.LowPart
= ft
.dwLowDateTime
;
249 u
.HighPart
= ft
.dwHighDateTime
;
254 TEST_F(FileUtilTest
, FileAndDirectorySize
) {
255 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
256 // should return 53 bytes.
257 FilePath file_01
= temp_dir_
.path().Append(FPL("The file 01.txt"));
258 CreateTextFile(file_01
, L
"12345678901234567890");
260 ASSERT_TRUE(GetFileSize(file_01
, &size_f1
));
261 EXPECT_EQ(20ll, size_f1
);
263 FilePath subdir_path
= temp_dir_
.path().Append(FPL("Level2"));
264 CreateDirectory(subdir_path
);
266 FilePath file_02
= subdir_path
.Append(FPL("The file 02.txt"));
267 CreateTextFile(file_02
, L
"123456789012345678901234567890");
269 ASSERT_TRUE(GetFileSize(file_02
, &size_f2
));
270 EXPECT_EQ(30ll, size_f2
);
272 FilePath subsubdir_path
= subdir_path
.Append(FPL("Level3"));
273 CreateDirectory(subsubdir_path
);
275 FilePath file_03
= subsubdir_path
.Append(FPL("The file 03.txt"));
276 CreateTextFile(file_03
, L
"123");
278 int64 computed_size
= ComputeDirectorySize(temp_dir_
.path());
279 EXPECT_EQ(size_f1
+ size_f2
+ 3, computed_size
);
282 TEST_F(FileUtilTest
, NormalizeFilePathBasic
) {
283 // Create a directory under the test dir. Because we create it,
284 // we know it is not a link.
285 FilePath file_a_path
= temp_dir_
.path().Append(FPL("file_a"));
286 FilePath dir_path
= temp_dir_
.path().Append(FPL("dir"));
287 FilePath file_b_path
= dir_path
.Append(FPL("file_b"));
288 CreateDirectory(dir_path
);
290 FilePath normalized_file_a_path
, normalized_file_b_path
;
291 ASSERT_FALSE(PathExists(file_a_path
));
292 ASSERT_FALSE(NormalizeFilePath(file_a_path
, &normalized_file_a_path
))
293 << "NormalizeFilePath() should fail on nonexistent paths.";
295 CreateTextFile(file_a_path
, bogus_content
);
296 ASSERT_TRUE(PathExists(file_a_path
));
297 ASSERT_TRUE(NormalizeFilePath(file_a_path
, &normalized_file_a_path
));
299 CreateTextFile(file_b_path
, bogus_content
);
300 ASSERT_TRUE(PathExists(file_b_path
));
301 ASSERT_TRUE(NormalizeFilePath(file_b_path
, &normalized_file_b_path
));
303 // Beacuse this test created |dir_path|, we know it is not a link
304 // or junction. So, the real path of the directory holding file a
305 // must be the parent of the path holding file b.
306 ASSERT_TRUE(normalized_file_a_path
.DirName()
307 .IsParent(normalized_file_b_path
.DirName()));
312 TEST_F(FileUtilTest
, NormalizeFilePathReparsePoints
) {
313 // Build the following directory structure:
319 // | |-> long_name___... (Very long name.)
323 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
324 // |-> to_base_b (reparse point to temp_dir\base_b)
325 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
327 FilePath base_a
= temp_dir_
.path().Append(FPL("base_a"));
328 ASSERT_TRUE(CreateDirectory(base_a
));
330 FilePath sub_a
= base_a
.Append(FPL("sub_a"));
331 ASSERT_TRUE(CreateDirectory(sub_a
));
333 FilePath file_txt
= sub_a
.Append(FPL("file.txt"));
334 CreateTextFile(file_txt
, bogus_content
);
336 // Want a directory whose name is long enough to make the path to the file
337 // inside just under MAX_PATH chars. This will be used to test that when
338 // a junction expands to a path over MAX_PATH chars in length,
339 // NormalizeFilePath() fails without crashing.
340 FilePath
sub_long_rel(FPL("sub_long"));
341 FilePath
deep_txt(FPL("deep.txt"));
343 int target_length
= MAX_PATH
;
344 target_length
-= (sub_a
.value().length() + 1); // +1 for the sepperator '\'.
345 target_length
-= (sub_long_rel
.Append(deep_txt
).value().length() + 1);
346 // Without making the path a bit shorter, CreateDirectory() fails.
347 // the resulting path is still long enough to hit the failing case in
349 const int kCreateDirLimit
= 4;
350 target_length
-= kCreateDirLimit
;
351 FilePath::StringType long_name_str
= FPL("long_name_");
352 long_name_str
.resize(target_length
, '_');
354 FilePath long_name
= sub_a
.Append(FilePath(long_name_str
));
355 FilePath deep_file
= long_name
.Append(sub_long_rel
).Append(deep_txt
);
356 ASSERT_EQ(MAX_PATH
- kCreateDirLimit
, deep_file
.value().length());
358 FilePath sub_long
= deep_file
.DirName();
359 ASSERT_TRUE(CreateDirectory(sub_long
));
360 CreateTextFile(deep_file
, bogus_content
);
362 FilePath base_b
= temp_dir_
.path().Append(FPL("base_b"));
363 ASSERT_TRUE(CreateDirectory(base_b
));
365 FilePath to_sub_a
= base_b
.Append(FPL("to_sub_a"));
366 ASSERT_TRUE(CreateDirectory(to_sub_a
));
367 FilePath normalized_path
;
369 ReparsePoint
reparse_to_sub_a(to_sub_a
, sub_a
);
370 ASSERT_TRUE(reparse_to_sub_a
.IsValid());
372 FilePath to_base_b
= base_b
.Append(FPL("to_base_b"));
373 ASSERT_TRUE(CreateDirectory(to_base_b
));
374 ReparsePoint
reparse_to_base_b(to_base_b
, base_b
);
375 ASSERT_TRUE(reparse_to_base_b
.IsValid());
377 FilePath to_sub_long
= base_b
.Append(FPL("to_sub_long"));
378 ASSERT_TRUE(CreateDirectory(to_sub_long
));
379 ReparsePoint
reparse_to_sub_long(to_sub_long
, sub_long
);
380 ASSERT_TRUE(reparse_to_sub_long
.IsValid());
382 // Normalize a junction free path: base_a\sub_a\file.txt .
383 ASSERT_TRUE(NormalizeFilePath(file_txt
, &normalized_path
));
384 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
386 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
387 // the junction to_sub_a.
388 ASSERT_TRUE(NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
390 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
392 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
393 // normalized to exclude junctions to_base_b and to_sub_a .
394 ASSERT_TRUE(NormalizeFilePath(base_b
.Append(FPL("to_base_b"))
395 .Append(FPL("to_base_b"))
396 .Append(FPL("to_sub_a"))
397 .Append(FPL("file.txt")),
399 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
401 // A long enough path will cause NormalizeFilePath() to fail. Make a long
402 // path using to_base_b many times, and check that paths long enough to fail
403 // do not cause a crash.
404 FilePath long_path
= base_b
;
405 const int kLengthLimit
= MAX_PATH
+ 200;
406 while (long_path
.value().length() <= kLengthLimit
) {
407 long_path
= long_path
.Append(FPL("to_base_b"));
409 long_path
= long_path
.Append(FPL("to_sub_a"))
410 .Append(FPL("file.txt"));
412 ASSERT_FALSE(NormalizeFilePath(long_path
, &normalized_path
));
414 // Normalizing the junction to deep.txt should fail, because the expanded
415 // path to deep.txt is longer than MAX_PATH.
416 ASSERT_FALSE(NormalizeFilePath(to_sub_long
.Append(deep_txt
),
419 // Delete the reparse points, and see that NormalizeFilePath() fails
423 ASSERT_FALSE(NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
427 TEST_F(FileUtilTest
, DevicePathToDriveLetter
) {
428 // Get a drive letter.
429 std::wstring real_drive_letter
= temp_dir_
.path().value().substr(0, 2);
430 if (!isalpha(real_drive_letter
[0]) || ':' != real_drive_letter
[1]) {
431 LOG(ERROR
) << "Can't get a drive letter to test with.";
435 // Get the NT style path to that drive.
436 wchar_t device_path
[MAX_PATH
] = {'\0'};
438 ::QueryDosDevice(real_drive_letter
.c_str(), device_path
, MAX_PATH
));
439 FilePath
actual_device_path(device_path
);
442 // Run DevicePathToDriveLetterPath() on the NT style path we got from
443 // QueryDosDevice(). Expect the drive letter we started with.
444 ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path
, &win32_path
));
445 ASSERT_EQ(real_drive_letter
, win32_path
.value());
447 // Add some directories to the path. Expect those extra path componenets
449 FilePath
kRelativePath(FPL("dir1\\dir2\\file.txt"));
450 ASSERT_TRUE(DevicePathToDriveLetterPath(
451 actual_device_path
.Append(kRelativePath
),
453 EXPECT_EQ(FilePath(real_drive_letter
+ L
"\\").Append(kRelativePath
).value(),
456 // Deform the real path so that it is invalid by removing the last four
457 // characters. The way windows names devices that are hard disks
458 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
459 // than three characters. The only way the truncated string could be a
460 // real drive is if more than 10^3 disks are mounted:
461 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
462 // Check that DevicePathToDriveLetterPath fails.
463 int path_length
= actual_device_path
.value().length();
464 int new_length
= path_length
- 4;
465 ASSERT_LT(0, new_length
);
466 FilePath
prefix_of_real_device_path(
467 actual_device_path
.value().substr(0, new_length
));
468 ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path
,
471 ASSERT_FALSE(DevicePathToDriveLetterPath(
472 prefix_of_real_device_path
.Append(kRelativePath
),
475 // Deform the real path so that it is invalid by adding some characters. For
476 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
477 // request for the drive letter whose native path is
478 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
479 // because drives are numbered in order and mounting 112345 hard disks will
481 const FilePath::StringType kExtraChars
= FPL("12345");
483 FilePath
real_device_path_plus_numbers(
484 actual_device_path
.value() + kExtraChars
);
486 ASSERT_FALSE(DevicePathToDriveLetterPath(
487 real_device_path_plus_numbers
,
490 ASSERT_FALSE(DevicePathToDriveLetterPath(
491 real_device_path_plus_numbers
.Append(kRelativePath
),
495 TEST_F(FileUtilTest
, CreateTemporaryFileInDirLongPathTest
) {
496 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
497 // if it is available. This test requires that:
498 // - the filesystem at |temp_dir_| supports long filenames.
499 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
500 // directories of |temp_dir_|.
501 const FilePath::CharType kLongDirName
[] = FPL("A long path");
502 const FilePath::CharType kTestSubDirName
[] = FPL("test");
503 FilePath long_test_dir
= temp_dir_
.path().Append(kLongDirName
);
504 ASSERT_TRUE(CreateDirectory(long_test_dir
));
506 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
507 // different short name.
508 WCHAR path_buffer
[MAX_PATH
];
509 DWORD path_buffer_length
= GetShortPathName(long_test_dir
.value().c_str(),
510 path_buffer
, MAX_PATH
);
511 ASSERT_LT(path_buffer_length
, DWORD(MAX_PATH
));
512 ASSERT_NE(DWORD(0), path_buffer_length
);
513 FilePath
short_test_dir(path_buffer
);
514 ASSERT_STRNE(kLongDirName
, short_test_dir
.BaseName().value().c_str());
517 ASSERT_TRUE(CreateTemporaryFileInDir(short_test_dir
, &temp_file
));
518 EXPECT_STREQ(kLongDirName
, temp_file
.DirName().BaseName().value().c_str());
519 EXPECT_TRUE(PathExists(temp_file
));
521 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
522 // unreadable. We should still be able to create a temp file in the
523 // subdirectory, but we won't be able to determine the long path for it. This
524 // mimics the environment that some users run where their user profiles reside
525 // in a location where the don't have full access to the higher level
526 // directories. (Note that this assumption is true for NTFS, but not for some
527 // network file systems. E.g. AFS).
528 FilePath access_test_dir
= long_test_dir
.Append(kTestSubDirName
);
529 ASSERT_TRUE(CreateDirectory(access_test_dir
));
530 file_util::PermissionRestorer
long_test_dir_restorer(long_test_dir
);
531 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir
));
533 // Use the short form of the directory to create a temporary filename.
534 ASSERT_TRUE(CreateTemporaryFileInDir(
535 short_test_dir
.Append(kTestSubDirName
), &temp_file
));
536 EXPECT_TRUE(PathExists(temp_file
));
537 EXPECT_TRUE(short_test_dir
.IsParent(temp_file
.DirName()));
539 // Check that the long path can't be determined for |temp_file|.
540 path_buffer_length
= GetLongPathName(temp_file
.value().c_str(),
541 path_buffer
, MAX_PATH
);
542 EXPECT_EQ(DWORD(0), path_buffer_length
);
545 #endif // defined(OS_WIN)
547 #if defined(OS_POSIX)
549 TEST_F(FileUtilTest
, CreateAndReadSymlinks
) {
550 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
551 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
552 CreateTextFile(link_to
, bogus_content
);
554 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
555 << "Failed to create file symlink.";
557 // If we created the link properly, we should be able to read the contents
559 std::wstring contents
= ReadTextFile(link_from
);
560 EXPECT_EQ(bogus_content
, contents
);
563 ASSERT_TRUE(ReadSymbolicLink(link_from
, &result
));
564 EXPECT_EQ(link_to
.value(), result
.value());
566 // Link to a directory.
567 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
568 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
569 ASSERT_TRUE(CreateDirectory(link_to
));
570 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
571 << "Failed to create directory symlink.";
574 EXPECT_FALSE(CreateSymbolicLink(link_to
, link_to
));
575 EXPECT_FALSE(ReadSymbolicLink(link_to
, &result
));
576 FilePath missing
= temp_dir_
.path().Append(FPL("missing"));
577 EXPECT_FALSE(ReadSymbolicLink(missing
, &result
));
580 // The following test of NormalizeFilePath() require that we create a symlink.
581 // This can not be done on Windows before Vista. On Vista, creating a symlink
582 // requires privilege "SeCreateSymbolicLinkPrivilege".
583 // TODO(skerner): Investigate the possibility of giving base_unittests the
584 // privileges required to create a symlink.
585 TEST_F(FileUtilTest
, NormalizeFilePathSymlinks
) {
586 // Link one file to another.
587 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
588 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
589 CreateTextFile(link_to
, bogus_content
);
591 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
592 << "Failed to create file symlink.";
594 // Check that NormalizeFilePath sees the link.
595 FilePath normalized_path
;
596 ASSERT_TRUE(NormalizeFilePath(link_from
, &normalized_path
));
597 EXPECT_NE(link_from
, link_to
);
598 EXPECT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
599 EXPECT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
601 // Link to a directory.
602 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
603 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
604 ASSERT_TRUE(CreateDirectory(link_to
));
605 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
606 << "Failed to create directory symlink.";
608 EXPECT_FALSE(NormalizeFilePath(link_from
, &normalized_path
))
609 << "Links to directories should return false.";
611 // Test that a loop in the links causes NormalizeFilePath() to return false.
612 link_from
= temp_dir_
.path().Append(FPL("link_a"));
613 link_to
= temp_dir_
.path().Append(FPL("link_b"));
614 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
615 << "Failed to create loop symlink a.";
616 ASSERT_TRUE(CreateSymbolicLink(link_from
, link_to
))
617 << "Failed to create loop symlink b.";
620 EXPECT_FALSE(NormalizeFilePath(link_from
, &normalized_path
));
622 #endif // defined(OS_POSIX)
624 TEST_F(FileUtilTest
, DeleteNonExistent
) {
625 FilePath non_existent
= temp_dir_
.path().AppendASCII("bogus_file_dne.foobar");
626 ASSERT_FALSE(PathExists(non_existent
));
628 EXPECT_TRUE(DeleteFile(non_existent
, false));
629 ASSERT_FALSE(PathExists(non_existent
));
630 EXPECT_TRUE(DeleteFile(non_existent
, true));
631 ASSERT_FALSE(PathExists(non_existent
));
634 TEST_F(FileUtilTest
, DeleteFile
) {
636 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 1.txt"));
637 CreateTextFile(file_name
, bogus_content
);
638 ASSERT_TRUE(PathExists(file_name
));
640 // Make sure it's deleted
641 EXPECT_TRUE(DeleteFile(file_name
, false));
642 EXPECT_FALSE(PathExists(file_name
));
644 // Test recursive case, create a new file
645 file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
646 CreateTextFile(file_name
, bogus_content
);
647 ASSERT_TRUE(PathExists(file_name
));
649 // Make sure it's deleted
650 EXPECT_TRUE(DeleteFile(file_name
, true));
651 EXPECT_FALSE(PathExists(file_name
));
654 #if defined(OS_POSIX)
655 TEST_F(FileUtilTest
, DeleteSymlinkToExistentFile
) {
657 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
658 CreateTextFile(file_name
, bogus_content
);
659 ASSERT_TRUE(PathExists(file_name
));
661 // Create a symlink to the file.
662 FilePath file_link
= temp_dir_
.path().Append("file_link_2");
663 ASSERT_TRUE(CreateSymbolicLink(file_name
, file_link
))
664 << "Failed to create symlink.";
666 // Delete the symbolic link.
667 EXPECT_TRUE(DeleteFile(file_link
, false));
669 // Make sure original file is not deleted.
670 EXPECT_FALSE(PathExists(file_link
));
671 EXPECT_TRUE(PathExists(file_name
));
674 TEST_F(FileUtilTest
, DeleteSymlinkToNonExistentFile
) {
675 // Create a non-existent file path.
676 FilePath non_existent
= temp_dir_
.path().Append(FPL("Test DeleteFile 3.txt"));
677 EXPECT_FALSE(PathExists(non_existent
));
679 // Create a symlink to the non-existent file.
680 FilePath file_link
= temp_dir_
.path().Append("file_link_3");
681 ASSERT_TRUE(CreateSymbolicLink(non_existent
, file_link
))
682 << "Failed to create symlink.";
684 // Make sure the symbolic link is exist.
685 EXPECT_TRUE(IsLink(file_link
));
686 EXPECT_FALSE(PathExists(file_link
));
688 // Delete the symbolic link.
689 EXPECT_TRUE(DeleteFile(file_link
, false));
691 // Make sure the symbolic link is deleted.
692 EXPECT_FALSE(IsLink(file_link
));
695 TEST_F(FileUtilTest
, ChangeFilePermissionsAndRead
) {
696 // Create a file path.
697 FilePath file_name
= temp_dir_
.path().Append(FPL("Test Readable File.txt"));
698 EXPECT_FALSE(PathExists(file_name
));
700 const std::string
kData("hello");
702 int buffer_size
= kData
.length();
703 char* buffer
= new char[buffer_size
];
706 EXPECT_EQ(static_cast<int>(kData
.length()),
707 WriteFile(file_name
, kData
.data(), kData
.length()));
708 EXPECT_TRUE(PathExists(file_name
));
710 // Make sure the file is readable.
712 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
713 EXPECT_TRUE(mode
& FILE_PERMISSION_READ_BY_USER
);
715 // Get rid of the read permission.
716 EXPECT_TRUE(SetPosixFilePermissions(file_name
, 0u));
717 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
718 EXPECT_FALSE(mode
& FILE_PERMISSION_READ_BY_USER
);
719 // Make sure the file can't be read.
720 EXPECT_EQ(-1, ReadFile(file_name
, buffer
, buffer_size
));
722 // Give the read permission.
723 EXPECT_TRUE(SetPosixFilePermissions(file_name
, FILE_PERMISSION_READ_BY_USER
));
724 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
725 EXPECT_TRUE(mode
& FILE_PERMISSION_READ_BY_USER
);
726 // Make sure the file can be read.
727 EXPECT_EQ(static_cast<int>(kData
.length()),
728 ReadFile(file_name
, buffer
, buffer_size
));
731 EXPECT_TRUE(DeleteFile(file_name
, false));
732 EXPECT_FALSE(PathExists(file_name
));
737 TEST_F(FileUtilTest
, ChangeFilePermissionsAndWrite
) {
738 // Create a file path.
739 FilePath file_name
= temp_dir_
.path().Append(FPL("Test Readable File.txt"));
740 EXPECT_FALSE(PathExists(file_name
));
742 const std::string
kData("hello");
745 EXPECT_EQ(static_cast<int>(kData
.length()),
746 WriteFile(file_name
, kData
.data(), kData
.length()));
747 EXPECT_TRUE(PathExists(file_name
));
749 // Make sure the file is writable.
751 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
752 EXPECT_TRUE(mode
& FILE_PERMISSION_WRITE_BY_USER
);
753 EXPECT_TRUE(PathIsWritable(file_name
));
755 // Get rid of the write permission.
756 EXPECT_TRUE(SetPosixFilePermissions(file_name
, 0u));
757 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
758 EXPECT_FALSE(mode
& FILE_PERMISSION_WRITE_BY_USER
);
759 // Make sure the file can't be write.
760 EXPECT_EQ(-1, WriteFile(file_name
, kData
.data(), kData
.length()));
761 EXPECT_FALSE(PathIsWritable(file_name
));
763 // Give read permission.
764 EXPECT_TRUE(SetPosixFilePermissions(file_name
,
765 FILE_PERMISSION_WRITE_BY_USER
));
766 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
767 EXPECT_TRUE(mode
& FILE_PERMISSION_WRITE_BY_USER
);
768 // Make sure the file can be write.
769 EXPECT_EQ(static_cast<int>(kData
.length()),
770 WriteFile(file_name
, kData
.data(), kData
.length()));
771 EXPECT_TRUE(PathIsWritable(file_name
));
774 EXPECT_TRUE(DeleteFile(file_name
, false));
775 EXPECT_FALSE(PathExists(file_name
));
778 TEST_F(FileUtilTest
, ChangeDirectoryPermissionsAndEnumerate
) {
779 // Create a directory path.
780 FilePath subdir_path
=
781 temp_dir_
.path().Append(FPL("PermissionTest1"));
782 CreateDirectory(subdir_path
);
783 ASSERT_TRUE(PathExists(subdir_path
));
785 // Create a dummy file to enumerate.
786 FilePath file_name
= subdir_path
.Append(FPL("Test Readable File.txt"));
787 EXPECT_FALSE(PathExists(file_name
));
788 const std::string
kData("hello");
789 EXPECT_EQ(static_cast<int>(kData
.length()),
790 WriteFile(file_name
, kData
.data(), kData
.length()));
791 EXPECT_TRUE(PathExists(file_name
));
793 // Make sure the directory has the all permissions.
795 EXPECT_TRUE(GetPosixFilePermissions(subdir_path
, &mode
));
796 EXPECT_EQ(FILE_PERMISSION_USER_MASK
, mode
& FILE_PERMISSION_USER_MASK
);
798 // Get rid of the permissions from the directory.
799 EXPECT_TRUE(SetPosixFilePermissions(subdir_path
, 0u));
800 EXPECT_TRUE(GetPosixFilePermissions(subdir_path
, &mode
));
801 EXPECT_FALSE(mode
& FILE_PERMISSION_USER_MASK
);
803 // Make sure the file in the directory can't be enumerated.
804 FileEnumerator
f1(subdir_path
, true, FileEnumerator::FILES
);
805 EXPECT_TRUE(PathExists(subdir_path
));
806 FindResultCollector
c1(f1
);
807 EXPECT_EQ(c1
.size(), 0);
808 EXPECT_FALSE(GetPosixFilePermissions(file_name
, &mode
));
810 // Give the permissions to the directory.
811 EXPECT_TRUE(SetPosixFilePermissions(subdir_path
, FILE_PERMISSION_USER_MASK
));
812 EXPECT_TRUE(GetPosixFilePermissions(subdir_path
, &mode
));
813 EXPECT_EQ(FILE_PERMISSION_USER_MASK
, mode
& FILE_PERMISSION_USER_MASK
);
815 // Make sure the file in the directory can be enumerated.
816 FileEnumerator
f2(subdir_path
, true, FileEnumerator::FILES
);
817 FindResultCollector
c2(f2
);
818 EXPECT_TRUE(c2
.HasFile(file_name
));
819 EXPECT_EQ(c2
.size(), 1);
822 EXPECT_TRUE(DeleteFile(subdir_path
, true));
823 EXPECT_FALSE(PathExists(subdir_path
));
826 #endif // defined(OS_POSIX)
829 // Tests that the Delete function works for wild cards, especially
830 // with the recursion flag. Also coincidentally tests PathExists.
831 // TODO(erikkay): see if anyone's actually using this feature of the API
832 TEST_F(FileUtilTest
, DeleteWildCard
) {
833 // Create a file and a directory
834 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteWildCard.txt"));
835 CreateTextFile(file_name
, bogus_content
);
836 ASSERT_TRUE(PathExists(file_name
));
838 FilePath subdir_path
= temp_dir_
.path().Append(FPL("DeleteWildCardDir"));
839 CreateDirectory(subdir_path
);
840 ASSERT_TRUE(PathExists(subdir_path
));
842 // Create the wildcard path
843 FilePath directory_contents
= temp_dir_
.path();
844 directory_contents
= directory_contents
.Append(FPL("*"));
846 // Delete non-recursively and check that only the file is deleted
847 EXPECT_TRUE(DeleteFile(directory_contents
, false));
848 EXPECT_FALSE(PathExists(file_name
));
849 EXPECT_TRUE(PathExists(subdir_path
));
851 // Delete recursively and make sure all contents are deleted
852 EXPECT_TRUE(DeleteFile(directory_contents
, true));
853 EXPECT_FALSE(PathExists(file_name
));
854 EXPECT_FALSE(PathExists(subdir_path
));
857 // TODO(erikkay): see if anyone's actually using this feature of the API
858 TEST_F(FileUtilTest
, DeleteNonExistantWildCard
) {
859 // Create a file and a directory
860 FilePath subdir_path
=
861 temp_dir_
.path().Append(FPL("DeleteNonExistantWildCard"));
862 CreateDirectory(subdir_path
);
863 ASSERT_TRUE(PathExists(subdir_path
));
865 // Create the wildcard path
866 FilePath directory_contents
= subdir_path
;
867 directory_contents
= directory_contents
.Append(FPL("*"));
869 // Delete non-recursively and check nothing got deleted
870 EXPECT_TRUE(DeleteFile(directory_contents
, false));
871 EXPECT_TRUE(PathExists(subdir_path
));
873 // Delete recursively and check nothing got deleted
874 EXPECT_TRUE(DeleteFile(directory_contents
, true));
875 EXPECT_TRUE(PathExists(subdir_path
));
879 // Tests non-recursive Delete() for a directory.
880 TEST_F(FileUtilTest
, DeleteDirNonRecursive
) {
881 // Create a subdirectory and put a file and two directories inside.
882 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirNonRecursive"));
883 CreateDirectory(test_subdir
);
884 ASSERT_TRUE(PathExists(test_subdir
));
886 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDir.txt"));
887 CreateTextFile(file_name
, bogus_content
);
888 ASSERT_TRUE(PathExists(file_name
));
890 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
891 CreateDirectory(subdir_path1
);
892 ASSERT_TRUE(PathExists(subdir_path1
));
894 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
895 CreateDirectory(subdir_path2
);
896 ASSERT_TRUE(PathExists(subdir_path2
));
898 // Delete non-recursively and check that the empty dir got deleted
899 EXPECT_TRUE(DeleteFile(subdir_path2
, false));
900 EXPECT_FALSE(PathExists(subdir_path2
));
902 // Delete non-recursively and check that nothing got deleted
903 EXPECT_FALSE(DeleteFile(test_subdir
, false));
904 EXPECT_TRUE(PathExists(test_subdir
));
905 EXPECT_TRUE(PathExists(file_name
));
906 EXPECT_TRUE(PathExists(subdir_path1
));
909 // Tests recursive Delete() for a directory.
910 TEST_F(FileUtilTest
, DeleteDirRecursive
) {
911 // Create a subdirectory and put a file and two directories inside.
912 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirRecursive"));
913 CreateDirectory(test_subdir
);
914 ASSERT_TRUE(PathExists(test_subdir
));
916 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDirRecursive.txt"));
917 CreateTextFile(file_name
, bogus_content
);
918 ASSERT_TRUE(PathExists(file_name
));
920 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
921 CreateDirectory(subdir_path1
);
922 ASSERT_TRUE(PathExists(subdir_path1
));
924 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
925 CreateDirectory(subdir_path2
);
926 ASSERT_TRUE(PathExists(subdir_path2
));
928 // Delete recursively and check that the empty dir got deleted
929 EXPECT_TRUE(DeleteFile(subdir_path2
, true));
930 EXPECT_FALSE(PathExists(subdir_path2
));
932 // Delete recursively and check that everything got deleted
933 EXPECT_TRUE(DeleteFile(test_subdir
, true));
934 EXPECT_FALSE(PathExists(file_name
));
935 EXPECT_FALSE(PathExists(subdir_path1
));
936 EXPECT_FALSE(PathExists(test_subdir
));
939 TEST_F(FileUtilTest
, MoveFileNew
) {
941 FilePath file_name_from
=
942 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
943 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
944 ASSERT_TRUE(PathExists(file_name_from
));
947 FilePath file_name_to
= temp_dir_
.path().Append(
948 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
949 ASSERT_FALSE(PathExists(file_name_to
));
951 EXPECT_TRUE(Move(file_name_from
, file_name_to
));
953 // Check everything has been moved.
954 EXPECT_FALSE(PathExists(file_name_from
));
955 EXPECT_TRUE(PathExists(file_name_to
));
958 TEST_F(FileUtilTest
, MoveFileExists
) {
960 FilePath file_name_from
=
961 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
962 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
963 ASSERT_TRUE(PathExists(file_name_from
));
965 // The destination name.
966 FilePath file_name_to
= temp_dir_
.path().Append(
967 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
968 CreateTextFile(file_name_to
, L
"Old file content");
969 ASSERT_TRUE(PathExists(file_name_to
));
971 EXPECT_TRUE(Move(file_name_from
, file_name_to
));
973 // Check everything has been moved.
974 EXPECT_FALSE(PathExists(file_name_from
));
975 EXPECT_TRUE(PathExists(file_name_to
));
976 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
979 TEST_F(FileUtilTest
, MoveFileDirExists
) {
981 FilePath file_name_from
=
982 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
983 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
984 ASSERT_TRUE(PathExists(file_name_from
));
986 // The destination directory
987 FilePath dir_name_to
=
988 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
989 CreateDirectory(dir_name_to
);
990 ASSERT_TRUE(PathExists(dir_name_to
));
992 EXPECT_FALSE(Move(file_name_from
, dir_name_to
));
996 TEST_F(FileUtilTest
, MoveNew
) {
997 // Create a directory
998 FilePath dir_name_from
=
999 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1000 CreateDirectory(dir_name_from
);
1001 ASSERT_TRUE(PathExists(dir_name_from
));
1003 // Create a file under the directory
1004 FilePath
txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1005 FilePath file_name_from
= dir_name_from
.Append(txt_file_name
);
1006 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1007 ASSERT_TRUE(PathExists(file_name_from
));
1009 // Move the directory.
1010 FilePath dir_name_to
=
1011 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1012 FilePath file_name_to
=
1013 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1015 ASSERT_FALSE(PathExists(dir_name_to
));
1017 EXPECT_TRUE(Move(dir_name_from
, dir_name_to
));
1019 // Check everything has been moved.
1020 EXPECT_FALSE(PathExists(dir_name_from
));
1021 EXPECT_FALSE(PathExists(file_name_from
));
1022 EXPECT_TRUE(PathExists(dir_name_to
));
1023 EXPECT_TRUE(PathExists(file_name_to
));
1025 // Test path traversal.
1026 file_name_from
= dir_name_to
.Append(txt_file_name
);
1027 file_name_to
= dir_name_to
.Append(FILE_PATH_LITERAL(".."));
1028 file_name_to
= file_name_to
.Append(txt_file_name
);
1029 EXPECT_FALSE(Move(file_name_from
, file_name_to
));
1030 EXPECT_TRUE(PathExists(file_name_from
));
1031 EXPECT_FALSE(PathExists(file_name_to
));
1032 EXPECT_TRUE(internal::MoveUnsafe(file_name_from
, file_name_to
));
1033 EXPECT_FALSE(PathExists(file_name_from
));
1034 EXPECT_TRUE(PathExists(file_name_to
));
1037 TEST_F(FileUtilTest
, MoveExist
) {
1038 // Create a directory
1039 FilePath dir_name_from
=
1040 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1041 CreateDirectory(dir_name_from
);
1042 ASSERT_TRUE(PathExists(dir_name_from
));
1044 // Create a file under the directory
1045 FilePath file_name_from
=
1046 dir_name_from
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1047 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1048 ASSERT_TRUE(PathExists(file_name_from
));
1050 // Move the directory
1051 FilePath dir_name_exists
=
1052 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1054 FilePath dir_name_to
=
1055 dir_name_exists
.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1056 FilePath file_name_to
=
1057 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1059 // Create the destination directory.
1060 CreateDirectory(dir_name_exists
);
1061 ASSERT_TRUE(PathExists(dir_name_exists
));
1063 EXPECT_TRUE(Move(dir_name_from
, dir_name_to
));
1065 // Check everything has been moved.
1066 EXPECT_FALSE(PathExists(dir_name_from
));
1067 EXPECT_FALSE(PathExists(file_name_from
));
1068 EXPECT_TRUE(PathExists(dir_name_to
));
1069 EXPECT_TRUE(PathExists(file_name_to
));
1072 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyNew
) {
1073 // Create a directory.
1074 FilePath dir_name_from
=
1075 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1076 CreateDirectory(dir_name_from
);
1077 ASSERT_TRUE(PathExists(dir_name_from
));
1079 // Create a file under the directory.
1080 FilePath file_name_from
=
1081 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1082 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1083 ASSERT_TRUE(PathExists(file_name_from
));
1085 // Create a subdirectory.
1086 FilePath subdir_name_from
=
1087 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1088 CreateDirectory(subdir_name_from
);
1089 ASSERT_TRUE(PathExists(subdir_name_from
));
1091 // Create a file under the subdirectory.
1092 FilePath file_name2_from
=
1093 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1094 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1095 ASSERT_TRUE(PathExists(file_name2_from
));
1097 // Copy the directory recursively.
1098 FilePath dir_name_to
=
1099 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1100 FilePath file_name_to
=
1101 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1102 FilePath subdir_name_to
=
1103 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1104 FilePath file_name2_to
=
1105 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1107 ASSERT_FALSE(PathExists(dir_name_to
));
1109 EXPECT_TRUE(CopyDirectory(dir_name_from
, dir_name_to
, true));
1111 // Check everything has been copied.
1112 EXPECT_TRUE(PathExists(dir_name_from
));
1113 EXPECT_TRUE(PathExists(file_name_from
));
1114 EXPECT_TRUE(PathExists(subdir_name_from
));
1115 EXPECT_TRUE(PathExists(file_name2_from
));
1116 EXPECT_TRUE(PathExists(dir_name_to
));
1117 EXPECT_TRUE(PathExists(file_name_to
));
1118 EXPECT_TRUE(PathExists(subdir_name_to
));
1119 EXPECT_TRUE(PathExists(file_name2_to
));
1122 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyExists
) {
1123 // Create a directory.
1124 FilePath dir_name_from
=
1125 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1126 CreateDirectory(dir_name_from
);
1127 ASSERT_TRUE(PathExists(dir_name_from
));
1129 // Create a file under the directory.
1130 FilePath file_name_from
=
1131 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1132 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1133 ASSERT_TRUE(PathExists(file_name_from
));
1135 // Create a subdirectory.
1136 FilePath subdir_name_from
=
1137 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1138 CreateDirectory(subdir_name_from
);
1139 ASSERT_TRUE(PathExists(subdir_name_from
));
1141 // Create a file under the subdirectory.
1142 FilePath file_name2_from
=
1143 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1144 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1145 ASSERT_TRUE(PathExists(file_name2_from
));
1147 // Copy the directory recursively.
1148 FilePath dir_name_exists
=
1149 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1151 FilePath dir_name_to
=
1152 dir_name_exists
.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1153 FilePath file_name_to
=
1154 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1155 FilePath subdir_name_to
=
1156 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1157 FilePath file_name2_to
=
1158 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1160 // Create the destination directory.
1161 CreateDirectory(dir_name_exists
);
1162 ASSERT_TRUE(PathExists(dir_name_exists
));
1164 EXPECT_TRUE(CopyDirectory(dir_name_from
, dir_name_exists
, true));
1166 // Check everything has been copied.
1167 EXPECT_TRUE(PathExists(dir_name_from
));
1168 EXPECT_TRUE(PathExists(file_name_from
));
1169 EXPECT_TRUE(PathExists(subdir_name_from
));
1170 EXPECT_TRUE(PathExists(file_name2_from
));
1171 EXPECT_TRUE(PathExists(dir_name_to
));
1172 EXPECT_TRUE(PathExists(file_name_to
));
1173 EXPECT_TRUE(PathExists(subdir_name_to
));
1174 EXPECT_TRUE(PathExists(file_name2_to
));
1177 TEST_F(FileUtilTest
, CopyDirectoryNew
) {
1178 // Create a directory.
1179 FilePath dir_name_from
=
1180 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1181 CreateDirectory(dir_name_from
);
1182 ASSERT_TRUE(PathExists(dir_name_from
));
1184 // Create a file under the directory.
1185 FilePath file_name_from
=
1186 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1187 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1188 ASSERT_TRUE(PathExists(file_name_from
));
1190 // Create a subdirectory.
1191 FilePath subdir_name_from
=
1192 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1193 CreateDirectory(subdir_name_from
);
1194 ASSERT_TRUE(PathExists(subdir_name_from
));
1196 // Create a file under the subdirectory.
1197 FilePath file_name2_from
=
1198 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1199 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1200 ASSERT_TRUE(PathExists(file_name2_from
));
1202 // Copy the directory not recursively.
1203 FilePath dir_name_to
=
1204 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1205 FilePath file_name_to
=
1206 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1207 FilePath subdir_name_to
=
1208 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1210 ASSERT_FALSE(PathExists(dir_name_to
));
1212 EXPECT_TRUE(CopyDirectory(dir_name_from
, dir_name_to
, false));
1214 // Check everything has been copied.
1215 EXPECT_TRUE(PathExists(dir_name_from
));
1216 EXPECT_TRUE(PathExists(file_name_from
));
1217 EXPECT_TRUE(PathExists(subdir_name_from
));
1218 EXPECT_TRUE(PathExists(file_name2_from
));
1219 EXPECT_TRUE(PathExists(dir_name_to
));
1220 EXPECT_TRUE(PathExists(file_name_to
));
1221 EXPECT_FALSE(PathExists(subdir_name_to
));
1224 TEST_F(FileUtilTest
, CopyDirectoryExists
) {
1225 // Create a directory.
1226 FilePath dir_name_from
=
1227 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1228 CreateDirectory(dir_name_from
);
1229 ASSERT_TRUE(PathExists(dir_name_from
));
1231 // Create a file under the directory.
1232 FilePath file_name_from
=
1233 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1234 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1235 ASSERT_TRUE(PathExists(file_name_from
));
1237 // Create a subdirectory.
1238 FilePath subdir_name_from
=
1239 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1240 CreateDirectory(subdir_name_from
);
1241 ASSERT_TRUE(PathExists(subdir_name_from
));
1243 // Create a file under the subdirectory.
1244 FilePath file_name2_from
=
1245 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1246 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1247 ASSERT_TRUE(PathExists(file_name2_from
));
1249 // Copy the directory not recursively.
1250 FilePath dir_name_to
=
1251 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1252 FilePath file_name_to
=
1253 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1254 FilePath subdir_name_to
=
1255 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1257 // Create the destination directory.
1258 CreateDirectory(dir_name_to
);
1259 ASSERT_TRUE(PathExists(dir_name_to
));
1261 EXPECT_TRUE(CopyDirectory(dir_name_from
, dir_name_to
, false));
1263 // Check everything has been copied.
1264 EXPECT_TRUE(PathExists(dir_name_from
));
1265 EXPECT_TRUE(PathExists(file_name_from
));
1266 EXPECT_TRUE(PathExists(subdir_name_from
));
1267 EXPECT_TRUE(PathExists(file_name2_from
));
1268 EXPECT_TRUE(PathExists(dir_name_to
));
1269 EXPECT_TRUE(PathExists(file_name_to
));
1270 EXPECT_FALSE(PathExists(subdir_name_to
));
1273 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToNew
) {
1275 FilePath file_name_from
=
1276 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1277 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1278 ASSERT_TRUE(PathExists(file_name_from
));
1280 // The destination name
1281 FilePath file_name_to
= temp_dir_
.path().Append(
1282 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1283 ASSERT_FALSE(PathExists(file_name_to
));
1285 EXPECT_TRUE(CopyDirectory(file_name_from
, file_name_to
, true));
1287 // Check the has been copied
1288 EXPECT_TRUE(PathExists(file_name_to
));
1291 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExisting
) {
1293 FilePath file_name_from
=
1294 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1295 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1296 ASSERT_TRUE(PathExists(file_name_from
));
1298 // The destination name
1299 FilePath file_name_to
= temp_dir_
.path().Append(
1300 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1301 CreateTextFile(file_name_to
, L
"Old file content");
1302 ASSERT_TRUE(PathExists(file_name_to
));
1304 EXPECT_TRUE(CopyDirectory(file_name_from
, file_name_to
, true));
1306 // Check the has been copied
1307 EXPECT_TRUE(PathExists(file_name_to
));
1308 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
1311 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExistingDirectory
) {
1313 FilePath file_name_from
=
1314 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1315 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1316 ASSERT_TRUE(PathExists(file_name_from
));
1319 FilePath dir_name_to
=
1320 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1321 CreateDirectory(dir_name_to
);
1322 ASSERT_TRUE(PathExists(dir_name_to
));
1323 FilePath file_name_to
=
1324 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1326 EXPECT_TRUE(CopyDirectory(file_name_from
, dir_name_to
, true));
1328 // Check the has been copied
1329 EXPECT_TRUE(PathExists(file_name_to
));
1332 TEST_F(FileUtilTest
, CopyDirectoryWithTrailingSeparators
) {
1333 // Create a directory.
1334 FilePath dir_name_from
=
1335 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1336 CreateDirectory(dir_name_from
);
1337 ASSERT_TRUE(PathExists(dir_name_from
));
1339 // Create a file under the directory.
1340 FilePath file_name_from
=
1341 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1342 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1343 ASSERT_TRUE(PathExists(file_name_from
));
1345 // Copy the directory recursively.
1346 FilePath dir_name_to
=
1347 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1348 FilePath file_name_to
=
1349 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1351 // Create from path with trailing separators.
1353 FilePath from_path
=
1354 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1355 #elif defined (OS_POSIX)
1356 FilePath from_path
=
1357 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1360 EXPECT_TRUE(CopyDirectory(from_path
, dir_name_to
, true));
1362 // Check everything has been copied.
1363 EXPECT_TRUE(PathExists(dir_name_from
));
1364 EXPECT_TRUE(PathExists(file_name_from
));
1365 EXPECT_TRUE(PathExists(dir_name_to
));
1366 EXPECT_TRUE(PathExists(file_name_to
));
1369 // Sets the source file to read-only.
1370 void SetReadOnly(const FilePath
& path
) {
1372 // On Windows, it involves setting a bit.
1373 DWORD attrs
= GetFileAttributes(path
.value().c_str());
1374 ASSERT_NE(INVALID_FILE_ATTRIBUTES
, attrs
);
1375 ASSERT_TRUE(SetFileAttributes(
1376 path
.value().c_str(), attrs
| FILE_ATTRIBUTE_READONLY
));
1377 attrs
= GetFileAttributes(path
.value().c_str());
1378 // Files in the temporary directory should not be indexed ever. If this
1379 // assumption change, fix this unit test accordingly.
1380 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1381 DWORD expected
= FILE_ATTRIBUTE_ARCHIVE
| FILE_ATTRIBUTE_READONLY
;
1382 if (win::GetVersion() >= win::VERSION_VISTA
)
1383 expected
|= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
1384 ASSERT_EQ(expected
, attrs
);
1386 // On all other platforms, it involves removing the write bit.
1387 EXPECT_TRUE(SetPosixFilePermissions(path
, S_IRUSR
));
1391 bool IsReadOnly(const FilePath
& path
) {
1393 DWORD attrs
= GetFileAttributes(path
.value().c_str());
1394 EXPECT_NE(INVALID_FILE_ATTRIBUTES
, attrs
);
1395 return attrs
& FILE_ATTRIBUTE_READONLY
;
1398 EXPECT_TRUE(GetPosixFilePermissions(path
, &mode
));
1399 return !(mode
& S_IWUSR
);
1403 TEST_F(FileUtilTest
, CopyDirectoryACL
) {
1404 // Create a directory.
1405 FilePath src
= temp_dir_
.path().Append(FILE_PATH_LITERAL("src"));
1406 CreateDirectory(src
);
1407 ASSERT_TRUE(PathExists(src
));
1409 // Create a file under the directory.
1410 FilePath src_file
= src
.Append(FILE_PATH_LITERAL("src.txt"));
1411 CreateTextFile(src_file
, L
"Gooooooooooooooooooooogle");
1412 SetReadOnly(src_file
);
1413 ASSERT_TRUE(IsReadOnly(src_file
));
1415 // Copy the directory recursively.
1416 FilePath dst
= temp_dir_
.path().Append(FILE_PATH_LITERAL("dst"));
1417 FilePath dst_file
= dst
.Append(FILE_PATH_LITERAL("src.txt"));
1418 EXPECT_TRUE(CopyDirectory(src
, dst
, true));
1420 ASSERT_FALSE(IsReadOnly(dst_file
));
1423 TEST_F(FileUtilTest
, CopyFile
) {
1424 // Create a directory
1425 FilePath dir_name_from
=
1426 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1427 CreateDirectory(dir_name_from
);
1428 ASSERT_TRUE(PathExists(dir_name_from
));
1430 // Create a file under the directory
1431 FilePath file_name_from
=
1432 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1433 const std::wstring
file_contents(L
"Gooooooooooooooooooooogle");
1434 CreateTextFile(file_name_from
, file_contents
);
1435 ASSERT_TRUE(PathExists(file_name_from
));
1438 FilePath dest_file
= dir_name_from
.Append(FILE_PATH_LITERAL("DestFile.txt"));
1439 ASSERT_TRUE(CopyFile(file_name_from
, dest_file
));
1441 // Copy the file to another location using '..' in the path.
1442 FilePath
dest_file2(dir_name_from
);
1443 dest_file2
= dest_file2
.AppendASCII("..");
1444 dest_file2
= dest_file2
.AppendASCII("DestFile.txt");
1445 ASSERT_FALSE(CopyFile(file_name_from
, dest_file2
));
1446 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from
, dest_file2
));
1448 FilePath
dest_file2_test(dir_name_from
);
1449 dest_file2_test
= dest_file2_test
.DirName();
1450 dest_file2_test
= dest_file2_test
.AppendASCII("DestFile.txt");
1452 // Check everything has been copied.
1453 EXPECT_TRUE(PathExists(file_name_from
));
1454 EXPECT_TRUE(PathExists(dest_file
));
1455 const std::wstring read_contents
= ReadTextFile(dest_file
);
1456 EXPECT_EQ(file_contents
, read_contents
);
1457 EXPECT_TRUE(PathExists(dest_file2_test
));
1458 EXPECT_TRUE(PathExists(dest_file2
));
1461 TEST_F(FileUtilTest
, CopyFileACL
) {
1462 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1463 // this test case asserts the access control bits are meeting expectations in
1464 // CopyFileUnsafe().
1465 FilePath src
= temp_dir_
.path().Append(FILE_PATH_LITERAL("src.txt"));
1466 const std::wstring
file_contents(L
"Gooooooooooooooooooooogle");
1467 CreateTextFile(src
, file_contents
);
1469 // Set the source file to read-only.
1470 ASSERT_FALSE(IsReadOnly(src
));
1472 ASSERT_TRUE(IsReadOnly(src
));
1475 FilePath dst
= temp_dir_
.path().Append(FILE_PATH_LITERAL("dst.txt"));
1476 ASSERT_TRUE(CopyFile(src
, dst
));
1477 EXPECT_EQ(file_contents
, ReadTextFile(dst
));
1479 ASSERT_FALSE(IsReadOnly(dst
));
1482 // file_util winds up using autoreleased objects on the Mac, so this needs
1483 // to be a PlatformTest.
1484 typedef PlatformTest ReadOnlyFileUtilTest
;
1486 TEST_F(ReadOnlyFileUtilTest
, ContentsEqual
) {
1488 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &data_dir
));
1489 data_dir
= data_dir
.AppendASCII("file_util");
1490 ASSERT_TRUE(PathExists(data_dir
));
1492 FilePath original_file
=
1493 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1494 FilePath same_file
=
1495 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1496 FilePath same_length_file
=
1497 data_dir
.Append(FILE_PATH_LITERAL("same_length.txt"));
1498 FilePath different_file
=
1499 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1500 FilePath different_first_file
=
1501 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1502 FilePath different_last_file
=
1503 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1504 FilePath empty1_file
=
1505 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1506 FilePath empty2_file
=
1507 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1508 FilePath shortened_file
=
1509 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1510 FilePath binary_file
=
1511 data_dir
.Append(FILE_PATH_LITERAL("binary_file.bin"));
1512 FilePath binary_file_same
=
1513 data_dir
.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1514 FilePath binary_file_diff
=
1515 data_dir
.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
1517 EXPECT_TRUE(ContentsEqual(original_file
, original_file
));
1518 EXPECT_TRUE(ContentsEqual(original_file
, same_file
));
1519 EXPECT_FALSE(ContentsEqual(original_file
, same_length_file
));
1520 EXPECT_FALSE(ContentsEqual(original_file
, different_file
));
1521 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1522 FilePath(FILE_PATH_LITERAL("bogusname"))));
1523 EXPECT_FALSE(ContentsEqual(original_file
, different_first_file
));
1524 EXPECT_FALSE(ContentsEqual(original_file
, different_last_file
));
1525 EXPECT_TRUE(ContentsEqual(empty1_file
, empty2_file
));
1526 EXPECT_FALSE(ContentsEqual(original_file
, shortened_file
));
1527 EXPECT_FALSE(ContentsEqual(shortened_file
, original_file
));
1528 EXPECT_TRUE(ContentsEqual(binary_file
, binary_file_same
));
1529 EXPECT_FALSE(ContentsEqual(binary_file
, binary_file_diff
));
1532 TEST_F(ReadOnlyFileUtilTest
, TextContentsEqual
) {
1534 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &data_dir
));
1535 data_dir
= data_dir
.AppendASCII("file_util");
1536 ASSERT_TRUE(PathExists(data_dir
));
1538 FilePath original_file
=
1539 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1540 FilePath same_file
=
1541 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1542 FilePath crlf_file
=
1543 data_dir
.Append(FILE_PATH_LITERAL("crlf.txt"));
1544 FilePath shortened_file
=
1545 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1546 FilePath different_file
=
1547 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1548 FilePath different_first_file
=
1549 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1550 FilePath different_last_file
=
1551 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1552 FilePath first1_file
=
1553 data_dir
.Append(FILE_PATH_LITERAL("first1.txt"));
1554 FilePath first2_file
=
1555 data_dir
.Append(FILE_PATH_LITERAL("first2.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 blank_line_file
=
1561 data_dir
.Append(FILE_PATH_LITERAL("blank_line.txt"));
1562 FilePath blank_line_crlf_file
=
1563 data_dir
.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1565 EXPECT_TRUE(TextContentsEqual(original_file
, same_file
));
1566 EXPECT_TRUE(TextContentsEqual(original_file
, crlf_file
));
1567 EXPECT_FALSE(TextContentsEqual(original_file
, shortened_file
));
1568 EXPECT_FALSE(TextContentsEqual(original_file
, different_file
));
1569 EXPECT_FALSE(TextContentsEqual(original_file
, different_first_file
));
1570 EXPECT_FALSE(TextContentsEqual(original_file
, different_last_file
));
1571 EXPECT_FALSE(TextContentsEqual(first1_file
, first2_file
));
1572 EXPECT_TRUE(TextContentsEqual(empty1_file
, empty2_file
));
1573 EXPECT_FALSE(TextContentsEqual(original_file
, empty1_file
));
1574 EXPECT_TRUE(TextContentsEqual(blank_line_file
, blank_line_crlf_file
));
1577 // We don't need equivalent functionality outside of Windows.
1579 TEST_F(FileUtilTest
, CopyAndDeleteDirectoryTest
) {
1580 // Create a directory
1581 FilePath dir_name_from
=
1582 temp_dir_
.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1583 CreateDirectory(dir_name_from
);
1584 ASSERT_TRUE(PathExists(dir_name_from
));
1586 // Create a file under the directory
1587 FilePath file_name_from
=
1588 dir_name_from
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1589 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1590 ASSERT_TRUE(PathExists(file_name_from
));
1592 // Move the directory by using CopyAndDeleteDirectory
1593 FilePath dir_name_to
= temp_dir_
.path().Append(
1594 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1595 FilePath file_name_to
=
1596 dir_name_to
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1598 ASSERT_FALSE(PathExists(dir_name_to
));
1600 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from
,
1603 // Check everything has been moved.
1604 EXPECT_FALSE(PathExists(dir_name_from
));
1605 EXPECT_FALSE(PathExists(file_name_from
));
1606 EXPECT_TRUE(PathExists(dir_name_to
));
1607 EXPECT_TRUE(PathExists(file_name_to
));
1610 TEST_F(FileUtilTest
, GetTempDirTest
) {
1611 static const TCHAR
* kTmpKey
= _T("TMP");
1612 static const TCHAR
* kTmpValues
[] = {
1613 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1615 // Save the original $TMP.
1616 size_t original_tmp_size
;
1617 TCHAR
* original_tmp
;
1618 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp
, &original_tmp_size
, kTmpKey
));
1619 // original_tmp may be NULL.
1621 for (unsigned int i
= 0; i
< arraysize(kTmpValues
); ++i
) {
1623 ::_tputenv_s(kTmpKey
, kTmpValues
[i
]);
1625 EXPECT_TRUE(path
.IsAbsolute()) << "$TMP=" << kTmpValues
[i
] <<
1626 " result=" << path
.value();
1629 // Restore the original $TMP.
1631 ::_tputenv_s(kTmpKey
, original_tmp
);
1634 ::_tputenv_s(kTmpKey
, _T(""));
1639 TEST_F(FileUtilTest
, CreateTemporaryFileTest
) {
1640 FilePath temp_files
[3];
1641 for (int i
= 0; i
< 3; i
++) {
1642 ASSERT_TRUE(CreateTemporaryFile(&(temp_files
[i
])));
1643 EXPECT_TRUE(PathExists(temp_files
[i
]));
1644 EXPECT_FALSE(DirectoryExists(temp_files
[i
]));
1646 for (int i
= 0; i
< 3; i
++)
1647 EXPECT_FALSE(temp_files
[i
] == temp_files
[(i
+1)%3]);
1648 for (int i
= 0; i
< 3; i
++)
1649 EXPECT_TRUE(DeleteFile(temp_files
[i
], false));
1652 TEST_F(FileUtilTest
, CreateAndOpenTemporaryFileTest
) {
1657 // Create; make sure they are open and exist.
1658 for (i
= 0; i
< 3; ++i
) {
1659 fps
[i
] = CreateAndOpenTemporaryFile(&(names
[i
]));
1660 ASSERT_TRUE(fps
[i
]);
1661 EXPECT_TRUE(PathExists(names
[i
]));
1664 // Make sure all names are unique.
1665 for (i
= 0; i
< 3; ++i
) {
1666 EXPECT_FALSE(names
[i
] == names
[(i
+1)%3]);
1669 // Close and delete.
1670 for (i
= 0; i
< 3; ++i
) {
1671 EXPECT_TRUE(CloseFile(fps
[i
]));
1672 EXPECT_TRUE(DeleteFile(names
[i
], false));
1676 TEST_F(FileUtilTest
, CreateNewTempDirectoryTest
) {
1678 ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir
));
1679 EXPECT_TRUE(PathExists(temp_dir
));
1680 EXPECT_TRUE(DeleteFile(temp_dir
, false));
1683 TEST_F(FileUtilTest
, CreateNewTemporaryDirInDirTest
) {
1685 ASSERT_TRUE(CreateTemporaryDirInDir(
1687 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1689 EXPECT_TRUE(PathExists(new_dir
));
1690 EXPECT_TRUE(temp_dir_
.path().IsParent(new_dir
));
1691 EXPECT_TRUE(DeleteFile(new_dir
, false));
1694 #if defined(OS_POSIX)
1695 TEST_F(FileUtilTest
, GetShmemTempDirTest
) {
1697 EXPECT_TRUE(GetShmemTempDir(false, &dir
));
1698 EXPECT_TRUE(DirectoryExists(dir
));
1702 TEST_F(FileUtilTest
, GetHomeDirTest
) {
1703 #if !defined(OS_ANDROID) // Not implemented on Android.
1704 // We don't actually know what the home directory is supposed to be without
1705 // calling some OS functions which would just duplicate the implementation.
1706 // So here we just test that it returns something "reasonable".
1707 FilePath home
= GetHomeDir();
1708 ASSERT_FALSE(home
.empty());
1709 ASSERT_TRUE(home
.IsAbsolute());
1713 TEST_F(FileUtilTest
, CreateDirectoryTest
) {
1714 FilePath test_root
=
1715 temp_dir_
.path().Append(FILE_PATH_LITERAL("create_directory_test"));
1717 FilePath test_path
=
1718 test_root
.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
1719 #elif defined(OS_POSIX)
1720 FilePath test_path
=
1721 test_root
.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
1724 EXPECT_FALSE(PathExists(test_path
));
1725 EXPECT_TRUE(CreateDirectory(test_path
));
1726 EXPECT_TRUE(PathExists(test_path
));
1727 // CreateDirectory returns true if the DirectoryExists returns true.
1728 EXPECT_TRUE(CreateDirectory(test_path
));
1730 // Doesn't work to create it on top of a non-dir
1731 test_path
= test_path
.Append(FILE_PATH_LITERAL("foobar.txt"));
1732 EXPECT_FALSE(PathExists(test_path
));
1733 CreateTextFile(test_path
, L
"test file");
1734 EXPECT_TRUE(PathExists(test_path
));
1735 EXPECT_FALSE(CreateDirectory(test_path
));
1737 EXPECT_TRUE(DeleteFile(test_root
, true));
1738 EXPECT_FALSE(PathExists(test_root
));
1739 EXPECT_FALSE(PathExists(test_path
));
1741 // Verify assumptions made by the Windows implementation:
1742 // 1. The current directory always exists.
1743 // 2. The root directory always exists.
1744 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory
)));
1745 FilePath top_level
= test_root
;
1746 while (top_level
!= top_level
.DirName()) {
1747 top_level
= top_level
.DirName();
1749 ASSERT_TRUE(DirectoryExists(top_level
));
1751 // Given these assumptions hold, it should be safe to
1752 // test that "creating" these directories succeeds.
1753 EXPECT_TRUE(CreateDirectory(
1754 FilePath(FilePath::kCurrentDirectory
)));
1755 EXPECT_TRUE(CreateDirectory(top_level
));
1758 FilePath
invalid_drive(FILE_PATH_LITERAL("o:\\"));
1759 FilePath invalid_path
=
1760 invalid_drive
.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1761 if (!PathExists(invalid_drive
)) {
1762 EXPECT_FALSE(CreateDirectory(invalid_path
));
1767 TEST_F(FileUtilTest
, DetectDirectoryTest
) {
1768 // Check a directory
1769 FilePath test_root
=
1770 temp_dir_
.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
1771 EXPECT_FALSE(PathExists(test_root
));
1772 EXPECT_TRUE(CreateDirectory(test_root
));
1773 EXPECT_TRUE(PathExists(test_root
));
1774 EXPECT_TRUE(DirectoryExists(test_root
));
1776 FilePath test_path
=
1777 test_root
.Append(FILE_PATH_LITERAL("foobar.txt"));
1778 EXPECT_FALSE(PathExists(test_path
));
1779 CreateTextFile(test_path
, L
"test file");
1780 EXPECT_TRUE(PathExists(test_path
));
1781 EXPECT_FALSE(DirectoryExists(test_path
));
1782 EXPECT_TRUE(DeleteFile(test_path
, false));
1784 EXPECT_TRUE(DeleteFile(test_root
, true));
1787 TEST_F(FileUtilTest
, FileEnumeratorTest
) {
1788 // Test an empty directory.
1789 FileEnumerator
f0(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1790 EXPECT_EQ(f0
.Next().value(), FPL(""));
1791 EXPECT_EQ(f0
.Next().value(), FPL(""));
1793 // Test an empty directory, non-recursively, including "..".
1794 FileEnumerator
f0_dotdot(temp_dir_
.path(), false,
1795 FILES_AND_DIRECTORIES
| FileEnumerator::INCLUDE_DOT_DOT
);
1796 EXPECT_EQ(temp_dir_
.path().Append(FPL("..")).value(),
1797 f0_dotdot
.Next().value());
1798 EXPECT_EQ(FPL(""), f0_dotdot
.Next().value());
1800 // create the directories
1801 FilePath dir1
= temp_dir_
.path().Append(FPL("dir1"));
1802 EXPECT_TRUE(CreateDirectory(dir1
));
1803 FilePath dir2
= temp_dir_
.path().Append(FPL("dir2"));
1804 EXPECT_TRUE(CreateDirectory(dir2
));
1805 FilePath dir2inner
= dir2
.Append(FPL("inner"));
1806 EXPECT_TRUE(CreateDirectory(dir2inner
));
1809 FilePath dir2file
= dir2
.Append(FPL("dir2file.txt"));
1810 CreateTextFile(dir2file
, std::wstring());
1811 FilePath dir2innerfile
= dir2inner
.Append(FPL("innerfile.txt"));
1812 CreateTextFile(dir2innerfile
, std::wstring());
1813 FilePath file1
= temp_dir_
.path().Append(FPL("file1.txt"));
1814 CreateTextFile(file1
, std::wstring());
1815 FilePath file2_rel
= dir2
.Append(FilePath::kParentDirectory
)
1816 .Append(FPL("file2.txt"));
1817 CreateTextFile(file2_rel
, std::wstring());
1818 FilePath file2_abs
= temp_dir_
.path().Append(FPL("file2.txt"));
1820 // Only enumerate files.
1821 FileEnumerator
f1(temp_dir_
.path(), true, FileEnumerator::FILES
);
1822 FindResultCollector
c1(f1
);
1823 EXPECT_TRUE(c1
.HasFile(file1
));
1824 EXPECT_TRUE(c1
.HasFile(file2_abs
));
1825 EXPECT_TRUE(c1
.HasFile(dir2file
));
1826 EXPECT_TRUE(c1
.HasFile(dir2innerfile
));
1827 EXPECT_EQ(c1
.size(), 4);
1829 // Only enumerate directories.
1830 FileEnumerator
f2(temp_dir_
.path(), true, FileEnumerator::DIRECTORIES
);
1831 FindResultCollector
c2(f2
);
1832 EXPECT_TRUE(c2
.HasFile(dir1
));
1833 EXPECT_TRUE(c2
.HasFile(dir2
));
1834 EXPECT_TRUE(c2
.HasFile(dir2inner
));
1835 EXPECT_EQ(c2
.size(), 3);
1837 // Only enumerate directories non-recursively.
1838 FileEnumerator
f2_non_recursive(
1839 temp_dir_
.path(), false, FileEnumerator::DIRECTORIES
);
1840 FindResultCollector
c2_non_recursive(f2_non_recursive
);
1841 EXPECT_TRUE(c2_non_recursive
.HasFile(dir1
));
1842 EXPECT_TRUE(c2_non_recursive
.HasFile(dir2
));
1843 EXPECT_EQ(c2_non_recursive
.size(), 2);
1845 // Only enumerate directories, non-recursively, including "..".
1846 FileEnumerator
f2_dotdot(temp_dir_
.path(), false,
1847 FileEnumerator::DIRECTORIES
|
1848 FileEnumerator::INCLUDE_DOT_DOT
);
1849 FindResultCollector
c2_dotdot(f2_dotdot
);
1850 EXPECT_TRUE(c2_dotdot
.HasFile(dir1
));
1851 EXPECT_TRUE(c2_dotdot
.HasFile(dir2
));
1852 EXPECT_TRUE(c2_dotdot
.HasFile(temp_dir_
.path().Append(FPL(".."))));
1853 EXPECT_EQ(c2_dotdot
.size(), 3);
1855 // Enumerate files and directories.
1856 FileEnumerator
f3(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1857 FindResultCollector
c3(f3
);
1858 EXPECT_TRUE(c3
.HasFile(dir1
));
1859 EXPECT_TRUE(c3
.HasFile(dir2
));
1860 EXPECT_TRUE(c3
.HasFile(file1
));
1861 EXPECT_TRUE(c3
.HasFile(file2_abs
));
1862 EXPECT_TRUE(c3
.HasFile(dir2file
));
1863 EXPECT_TRUE(c3
.HasFile(dir2inner
));
1864 EXPECT_TRUE(c3
.HasFile(dir2innerfile
));
1865 EXPECT_EQ(c3
.size(), 7);
1867 // Non-recursive operation.
1868 FileEnumerator
f4(temp_dir_
.path(), false, FILES_AND_DIRECTORIES
);
1869 FindResultCollector
c4(f4
);
1870 EXPECT_TRUE(c4
.HasFile(dir2
));
1871 EXPECT_TRUE(c4
.HasFile(dir2
));
1872 EXPECT_TRUE(c4
.HasFile(file1
));
1873 EXPECT_TRUE(c4
.HasFile(file2_abs
));
1874 EXPECT_EQ(c4
.size(), 4);
1876 // Enumerate with a pattern.
1877 FileEnumerator
f5(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
, FPL("dir*"));
1878 FindResultCollector
c5(f5
);
1879 EXPECT_TRUE(c5
.HasFile(dir1
));
1880 EXPECT_TRUE(c5
.HasFile(dir2
));
1881 EXPECT_TRUE(c5
.HasFile(dir2file
));
1882 EXPECT_TRUE(c5
.HasFile(dir2inner
));
1883 EXPECT_TRUE(c5
.HasFile(dir2innerfile
));
1884 EXPECT_EQ(c5
.size(), 5);
1888 // Make dir1 point to dir2.
1889 ReparsePoint
reparse_point(dir1
, dir2
);
1890 EXPECT_TRUE(reparse_point
.IsValid());
1892 if ((win::GetVersion() >= win::VERSION_VISTA
)) {
1893 // There can be a delay for the enumeration code to see the change on
1894 // the file system so skip this test for XP.
1895 // Enumerate the reparse point.
1896 FileEnumerator
f6(dir1
, true, FILES_AND_DIRECTORIES
);
1897 FindResultCollector
c6(f6
);
1898 FilePath inner2
= dir1
.Append(FPL("inner"));
1899 EXPECT_TRUE(c6
.HasFile(inner2
));
1900 EXPECT_TRUE(c6
.HasFile(inner2
.Append(FPL("innerfile.txt"))));
1901 EXPECT_TRUE(c6
.HasFile(dir1
.Append(FPL("dir2file.txt"))));
1902 EXPECT_EQ(c6
.size(), 3);
1905 // No changes for non recursive operation.
1906 FileEnumerator
f7(temp_dir_
.path(), false, FILES_AND_DIRECTORIES
);
1907 FindResultCollector
c7(f7
);
1908 EXPECT_TRUE(c7
.HasFile(dir2
));
1909 EXPECT_TRUE(c7
.HasFile(dir2
));
1910 EXPECT_TRUE(c7
.HasFile(file1
));
1911 EXPECT_TRUE(c7
.HasFile(file2_abs
));
1912 EXPECT_EQ(c7
.size(), 4);
1914 // Should not enumerate inside dir1 when using recursion.
1915 FileEnumerator
f8(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1916 FindResultCollector
c8(f8
);
1917 EXPECT_TRUE(c8
.HasFile(dir1
));
1918 EXPECT_TRUE(c8
.HasFile(dir2
));
1919 EXPECT_TRUE(c8
.HasFile(file1
));
1920 EXPECT_TRUE(c8
.HasFile(file2_abs
));
1921 EXPECT_TRUE(c8
.HasFile(dir2file
));
1922 EXPECT_TRUE(c8
.HasFile(dir2inner
));
1923 EXPECT_TRUE(c8
.HasFile(dir2innerfile
));
1924 EXPECT_EQ(c8
.size(), 7);
1928 // Make sure the destructor closes the find handle while in the middle of a
1929 // query to allow TearDown to delete the directory.
1930 FileEnumerator
f9(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1931 EXPECT_FALSE(f9
.Next().value().empty()); // Should have found something
1932 // (we don't care what).
1935 TEST_F(FileUtilTest
, AppendToFile
) {
1937 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1939 // Create a fresh, empty copy of this directory.
1940 if (PathExists(data_dir
)) {
1941 ASSERT_TRUE(DeleteFile(data_dir
, true));
1943 ASSERT_TRUE(CreateDirectory(data_dir
));
1945 // Create a fresh, empty copy of this directory.
1946 if (PathExists(data_dir
)) {
1947 ASSERT_TRUE(DeleteFile(data_dir
, true));
1949 ASSERT_TRUE(CreateDirectory(data_dir
));
1950 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
1952 std::string
data("hello");
1953 EXPECT_EQ(-1, AppendToFile(foobar
, data
.c_str(), data
.length()));
1954 EXPECT_EQ(static_cast<int>(data
.length()),
1955 WriteFile(foobar
, data
.c_str(), data
.length()));
1956 EXPECT_EQ(static_cast<int>(data
.length()),
1957 AppendToFile(foobar
, data
.c_str(), data
.length()));
1959 const std::wstring read_content
= ReadTextFile(foobar
);
1960 EXPECT_EQ(L
"hellohello", read_content
);
1963 TEST_F(FileUtilTest
, ReadFileToString
) {
1964 const char kTestData
[] = "0123";
1967 FilePath file_path
=
1968 temp_dir_
.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
1970 ASSERT_EQ(4, WriteFile(file_path
, kTestData
, 4));
1972 EXPECT_TRUE(ReadFileToString(file_path
, &data
));
1973 EXPECT_EQ(kTestData
, data
);
1976 EXPECT_FALSE(ReadFileToString(file_path
, &data
, 0));
1977 EXPECT_EQ(data
.length(), 0u);
1980 EXPECT_FALSE(ReadFileToString(file_path
, &data
, 2));
1981 EXPECT_EQ("01", data
);
1984 EXPECT_FALSE(ReadFileToString(file_path
, &data
, 3));
1985 EXPECT_EQ("012", data
);
1988 EXPECT_TRUE(ReadFileToString(file_path
, &data
, 4));
1989 EXPECT_EQ("0123", data
);
1992 EXPECT_TRUE(ReadFileToString(file_path
, &data
, 6));
1993 EXPECT_EQ("0123", data
);
1995 EXPECT_TRUE(ReadFileToString(file_path
, NULL
, 6));
1997 EXPECT_TRUE(ReadFileToString(file_path
, NULL
));
1999 EXPECT_TRUE(base::DeleteFile(file_path
, false));
2002 EXPECT_FALSE(ReadFileToString(file_path
, &data
));
2003 EXPECT_EQ(data
.length(), 0u);
2006 EXPECT_FALSE(ReadFileToString(file_path
, &data
, 6));
2007 EXPECT_EQ(data
.length(), 0u);
2010 TEST_F(FileUtilTest
, TouchFile
) {
2012 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
2014 // Create a fresh, empty copy of this directory.
2015 if (PathExists(data_dir
)) {
2016 ASSERT_TRUE(DeleteFile(data_dir
, true));
2018 ASSERT_TRUE(CreateDirectory(data_dir
));
2020 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
2021 std::string
data("hello");
2022 ASSERT_TRUE(WriteFile(foobar
, data
.c_str(), data
.length()));
2025 // This timestamp is divisible by one day (in local timezone),
2026 // to make it work on FAT too.
2027 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
2030 Time modification_time
;
2031 // Note that this timestamp is divisible by two (seconds) - FAT stores
2032 // modification times with 2s resolution.
2033 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
2034 &modification_time
));
2036 ASSERT_TRUE(TouchFile(foobar
, access_time
, modification_time
));
2037 File::Info file_info
;
2038 ASSERT_TRUE(GetFileInfo(foobar
, &file_info
));
2039 EXPECT_EQ(file_info
.last_accessed
.ToInternalValue(),
2040 access_time
.ToInternalValue());
2041 EXPECT_EQ(file_info
.last_modified
.ToInternalValue(),
2042 modification_time
.ToInternalValue());
2045 TEST_F(FileUtilTest
, IsDirectoryEmpty
) {
2046 FilePath empty_dir
= temp_dir_
.path().Append(FILE_PATH_LITERAL("EmptyDir"));
2048 ASSERT_FALSE(PathExists(empty_dir
));
2050 ASSERT_TRUE(CreateDirectory(empty_dir
));
2052 EXPECT_TRUE(IsDirectoryEmpty(empty_dir
));
2054 FilePath
foo(empty_dir
.Append(FILE_PATH_LITERAL("foo.txt")));
2055 std::string
bar("baz");
2056 ASSERT_TRUE(WriteFile(foo
, bar
.c_str(), bar
.length()));
2058 EXPECT_FALSE(IsDirectoryEmpty(empty_dir
));
2061 #if defined(OS_POSIX)
2063 // Testing VerifyPathControlledByAdmin() is hard, because there is no
2064 // way a test can make a file owned by root, or change file paths
2065 // at the root of the file system. VerifyPathControlledByAdmin()
2066 // is implemented as a call to VerifyPathControlledByUser, which gives
2067 // us the ability to test with paths under the test's temp directory,
2068 // using a user id we control.
2069 // Pull tests of VerifyPathControlledByUserTest() into a separate test class
2070 // with a common SetUp() method.
2071 class VerifyPathControlledByUserTest
: public FileUtilTest
{
2073 virtual void SetUp() OVERRIDE
{
2074 FileUtilTest::SetUp();
2076 // Create a basic structure used by each test.
2081 base_dir_
= temp_dir_
.path().AppendASCII("base_dir");
2082 ASSERT_TRUE(CreateDirectory(base_dir_
));
2084 sub_dir_
= base_dir_
.AppendASCII("sub_dir");
2085 ASSERT_TRUE(CreateDirectory(sub_dir_
));
2087 text_file_
= sub_dir_
.AppendASCII("file.txt");
2088 CreateTextFile(text_file_
, L
"This text file has some text in it.");
2090 // Get the user and group files are created with from |base_dir_|.
2091 struct stat stat_buf
;
2092 ASSERT_EQ(0, stat(base_dir_
.value().c_str(), &stat_buf
));
2093 uid_
= stat_buf
.st_uid
;
2094 ok_gids_
.insert(stat_buf
.st_gid
);
2095 bad_gids_
.insert(stat_buf
.st_gid
+ 1);
2097 ASSERT_EQ(uid_
, getuid()); // This process should be the owner.
2099 // To ensure that umask settings do not cause the initial state
2100 // of permissions to be different from what we expect, explicitly
2101 // set permissions on the directories we create.
2102 // Make all files and directories non-world-writable.
2104 // Users and group can read, write, traverse
2105 int enabled_permissions
=
2106 FILE_PERMISSION_USER_MASK
| FILE_PERMISSION_GROUP_MASK
;
2107 // Other users can't read, write, traverse
2108 int disabled_permissions
= FILE_PERMISSION_OTHERS_MASK
;
2110 ASSERT_NO_FATAL_FAILURE(
2111 ChangePosixFilePermissions(
2112 base_dir_
, enabled_permissions
, disabled_permissions
));
2113 ASSERT_NO_FATAL_FAILURE(
2114 ChangePosixFilePermissions(
2115 sub_dir_
, enabled_permissions
, disabled_permissions
));
2120 FilePath text_file_
;
2123 std::set
<gid_t
> ok_gids_
;
2124 std::set
<gid_t
> bad_gids_
;
2127 TEST_F(VerifyPathControlledByUserTest
, BadPaths
) {
2128 // File does not exist.
2129 FilePath does_not_exist
= base_dir_
.AppendASCII("does")
2131 .AppendASCII("exist");
2133 base::VerifyPathControlledByUser(
2134 base_dir_
, does_not_exist
, uid_
, ok_gids_
));
2136 // |base| not a subpath of |path|.
2138 base::VerifyPathControlledByUser(
2139 sub_dir_
, base_dir_
, uid_
, ok_gids_
));
2141 // An empty base path will fail to be a prefix for any path.
2144 base::VerifyPathControlledByUser(
2145 empty
, base_dir_
, uid_
, ok_gids_
));
2147 // Finding that a bad call fails proves nothing unless a good call succeeds.
2149 base::VerifyPathControlledByUser(
2150 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2153 TEST_F(VerifyPathControlledByUserTest
, Symlinks
) {
2154 // Symlinks in the path should cause failure.
2156 // Symlink to the file at the end of the path.
2157 FilePath file_link
= base_dir_
.AppendASCII("file_link");
2158 ASSERT_TRUE(CreateSymbolicLink(text_file_
, file_link
))
2159 << "Failed to create symlink.";
2162 base::VerifyPathControlledByUser(
2163 base_dir_
, file_link
, uid_
, ok_gids_
));
2165 base::VerifyPathControlledByUser(
2166 file_link
, file_link
, uid_
, ok_gids_
));
2168 // Symlink from one directory to another within the path.
2169 FilePath link_to_sub_dir
= base_dir_
.AppendASCII("link_to_sub_dir");
2170 ASSERT_TRUE(CreateSymbolicLink(sub_dir_
, link_to_sub_dir
))
2171 << "Failed to create symlink.";
2173 FilePath file_path_with_link
= link_to_sub_dir
.AppendASCII("file.txt");
2174 ASSERT_TRUE(PathExists(file_path_with_link
));
2177 base::VerifyPathControlledByUser(
2178 base_dir_
, file_path_with_link
, uid_
, ok_gids_
));
2181 base::VerifyPathControlledByUser(
2182 link_to_sub_dir
, file_path_with_link
, uid_
, ok_gids_
));
2184 // Symlinks in parents of base path are allowed.
2186 base::VerifyPathControlledByUser(
2187 file_path_with_link
, file_path_with_link
, uid_
, ok_gids_
));
2190 TEST_F(VerifyPathControlledByUserTest
, OwnershipChecks
) {
2191 // Get a uid that is not the uid of files we create.
2192 uid_t bad_uid
= uid_
+ 1;
2194 // Make all files and directories non-world-writable.
2195 ASSERT_NO_FATAL_FAILURE(
2196 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2197 ASSERT_NO_FATAL_FAILURE(
2198 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2199 ASSERT_NO_FATAL_FAILURE(
2200 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2202 // We control these paths.
2204 base::VerifyPathControlledByUser(
2205 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2207 base::VerifyPathControlledByUser(
2208 base_dir_
, text_file_
, uid_
, ok_gids_
));
2210 base::VerifyPathControlledByUser(
2211 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2213 // Another user does not control these paths.
2215 base::VerifyPathControlledByUser(
2216 base_dir_
, sub_dir_
, bad_uid
, ok_gids_
));
2218 base::VerifyPathControlledByUser(
2219 base_dir_
, text_file_
, bad_uid
, ok_gids_
));
2221 base::VerifyPathControlledByUser(
2222 sub_dir_
, text_file_
, bad_uid
, ok_gids_
));
2224 // Another group does not control the paths.
2226 base::VerifyPathControlledByUser(
2227 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2229 base::VerifyPathControlledByUser(
2230 base_dir_
, text_file_
, uid_
, bad_gids_
));
2232 base::VerifyPathControlledByUser(
2233 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2236 TEST_F(VerifyPathControlledByUserTest
, GroupWriteTest
) {
2237 // Make all files and directories writable only by their owner.
2238 ASSERT_NO_FATAL_FAILURE(
2239 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2240 ASSERT_NO_FATAL_FAILURE(
2241 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2242 ASSERT_NO_FATAL_FAILURE(
2243 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
|S_IWGRP
));
2245 // Any group is okay because the path is not group-writable.
2247 base::VerifyPathControlledByUser(
2248 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2250 base::VerifyPathControlledByUser(
2251 base_dir_
, text_file_
, uid_
, ok_gids_
));
2253 base::VerifyPathControlledByUser(
2254 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2257 base::VerifyPathControlledByUser(
2258 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2260 base::VerifyPathControlledByUser(
2261 base_dir_
, text_file_
, uid_
, bad_gids_
));
2263 base::VerifyPathControlledByUser(
2264 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2266 // No group is okay, because we don't check the group
2267 // if no group can write.
2268 std::set
<gid_t
> no_gids
; // Empty set of gids.
2270 base::VerifyPathControlledByUser(
2271 base_dir_
, sub_dir_
, uid_
, no_gids
));
2273 base::VerifyPathControlledByUser(
2274 base_dir_
, text_file_
, uid_
, no_gids
));
2276 base::VerifyPathControlledByUser(
2277 sub_dir_
, text_file_
, uid_
, no_gids
));
2280 // Make all files and directories writable by their group.
2281 ASSERT_NO_FATAL_FAILURE(
2282 ChangePosixFilePermissions(base_dir_
, S_IWGRP
, 0u));
2283 ASSERT_NO_FATAL_FAILURE(
2284 ChangePosixFilePermissions(sub_dir_
, S_IWGRP
, 0u));
2285 ASSERT_NO_FATAL_FAILURE(
2286 ChangePosixFilePermissions(text_file_
, S_IWGRP
, 0u));
2288 // Now |ok_gids_| works, but |bad_gids_| fails.
2290 base::VerifyPathControlledByUser(
2291 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2293 base::VerifyPathControlledByUser(
2294 base_dir_
, text_file_
, uid_
, ok_gids_
));
2296 base::VerifyPathControlledByUser(
2297 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2300 base::VerifyPathControlledByUser(
2301 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2303 base::VerifyPathControlledByUser(
2304 base_dir_
, text_file_
, uid_
, bad_gids_
));
2306 base::VerifyPathControlledByUser(
2307 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2309 // Because any group in the group set is allowed,
2310 // the union of good and bad gids passes.
2312 std::set
<gid_t
> multiple_gids
;
2314 ok_gids_
.begin(), ok_gids_
.end(),
2315 bad_gids_
.begin(), bad_gids_
.end(),
2316 std::inserter(multiple_gids
, multiple_gids
.begin()));
2319 base::VerifyPathControlledByUser(
2320 base_dir_
, sub_dir_
, uid_
, multiple_gids
));
2322 base::VerifyPathControlledByUser(
2323 base_dir_
, text_file_
, uid_
, multiple_gids
));
2325 base::VerifyPathControlledByUser(
2326 sub_dir_
, text_file_
, uid_
, multiple_gids
));
2329 TEST_F(VerifyPathControlledByUserTest
, WriteBitChecks
) {
2330 // Make all files and directories non-world-writable.
2331 ASSERT_NO_FATAL_FAILURE(
2332 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2333 ASSERT_NO_FATAL_FAILURE(
2334 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2335 ASSERT_NO_FATAL_FAILURE(
2336 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2338 // Initialy, we control all parts of the path.
2340 base::VerifyPathControlledByUser(
2341 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2343 base::VerifyPathControlledByUser(
2344 base_dir_
, text_file_
, uid_
, ok_gids_
));
2346 base::VerifyPathControlledByUser(
2347 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2349 // Make base_dir_ world-writable.
2350 ASSERT_NO_FATAL_FAILURE(
2351 ChangePosixFilePermissions(base_dir_
, S_IWOTH
, 0u));
2353 base::VerifyPathControlledByUser(
2354 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2356 base::VerifyPathControlledByUser(
2357 base_dir_
, text_file_
, uid_
, ok_gids_
));
2359 base::VerifyPathControlledByUser(
2360 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2362 // Make sub_dir_ world writable.
2363 ASSERT_NO_FATAL_FAILURE(
2364 ChangePosixFilePermissions(sub_dir_
, S_IWOTH
, 0u));
2366 base::VerifyPathControlledByUser(
2367 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2369 base::VerifyPathControlledByUser(
2370 base_dir_
, text_file_
, uid_
, ok_gids_
));
2372 base::VerifyPathControlledByUser(
2373 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2375 // Make text_file_ world writable.
2376 ASSERT_NO_FATAL_FAILURE(
2377 ChangePosixFilePermissions(text_file_
, S_IWOTH
, 0u));
2379 base::VerifyPathControlledByUser(
2380 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2382 base::VerifyPathControlledByUser(
2383 base_dir_
, text_file_
, uid_
, ok_gids_
));
2385 base::VerifyPathControlledByUser(
2386 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2388 // Make sub_dir_ non-world writable.
2389 ASSERT_NO_FATAL_FAILURE(
2390 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2392 base::VerifyPathControlledByUser(
2393 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2395 base::VerifyPathControlledByUser(
2396 base_dir_
, text_file_
, uid_
, ok_gids_
));
2398 base::VerifyPathControlledByUser(
2399 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2401 // Make base_dir_ non-world-writable.
2402 ASSERT_NO_FATAL_FAILURE(
2403 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2405 base::VerifyPathControlledByUser(
2406 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2408 base::VerifyPathControlledByUser(
2409 base_dir_
, text_file_
, uid_
, ok_gids_
));
2411 base::VerifyPathControlledByUser(
2412 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2414 // Back to the initial state: Nothing is writable, so every path
2416 ASSERT_NO_FATAL_FAILURE(
2417 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2419 base::VerifyPathControlledByUser(
2420 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2422 base::VerifyPathControlledByUser(
2423 base_dir_
, text_file_
, uid_
, ok_gids_
));
2425 base::VerifyPathControlledByUser(
2426 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2429 #if defined(OS_ANDROID)
2430 TEST_F(FileUtilTest
, ValidContentUriTest
) {
2431 // Get the test image path.
2433 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &data_dir
));
2434 data_dir
= data_dir
.AppendASCII("file_util");
2435 ASSERT_TRUE(PathExists(data_dir
));
2436 FilePath image_file
= data_dir
.Append(FILE_PATH_LITERAL("red.png"));
2438 GetFileSize(image_file
, &image_size
);
2439 EXPECT_LT(0, image_size
);
2441 // Insert the image into MediaStore. MediaStore will do some conversions, and
2442 // return the content URI.
2443 FilePath path
= file_util::InsertImageIntoMediaStore(image_file
);
2444 EXPECT_TRUE(path
.IsContentUri());
2445 EXPECT_TRUE(PathExists(path
));
2446 // The file size may not equal to the input image as MediaStore may convert
2448 int64 content_uri_size
;
2449 GetFileSize(path
, &content_uri_size
);
2450 EXPECT_EQ(image_size
, content_uri_size
);
2452 // We should be able to read the file.
2453 char* buffer
= new char[image_size
];
2454 File file
= OpenContentUriForRead(path
);
2455 EXPECT_TRUE(file
.IsValid());
2456 EXPECT_TRUE(file
.ReadAtCurrentPos(buffer
, image_size
));
2460 TEST_F(FileUtilTest
, NonExistentContentUriTest
) {
2461 FilePath
path("content://foo.bar");
2462 EXPECT_TRUE(path
.IsContentUri());
2463 EXPECT_FALSE(PathExists(path
));
2464 // Size should be smaller than 0.
2466 EXPECT_FALSE(GetFileSize(path
, &size
));
2468 // We should not be able to read the file.
2469 File file
= OpenContentUriForRead(path
);
2470 EXPECT_FALSE(file
.IsValid());
2474 TEST(ScopedFD
, ScopedFDDoesClose
) {
2477 ASSERT_EQ(0, pipe(fds
));
2478 const int write_end
= fds
[1];
2479 base::ScopedFD
read_end_closer(fds
[0]);
2481 base::ScopedFD
write_end_closer(fds
[1]);
2483 // This is the only thread. This file descriptor should no longer be valid.
2484 int ret
= close(write_end
);
2486 EXPECT_EQ(EBADF
, errno
);
2487 // Make sure read(2) won't block.
2488 ASSERT_EQ(0, fcntl(fds
[0], F_SETFL
, O_NONBLOCK
));
2489 // Reading the pipe should EOF.
2490 EXPECT_EQ(0, read(fds
[0], &c
, 1));
2493 #if defined(GTEST_HAS_DEATH_TEST)
2494 void CloseWithScopedFD(int fd
) {
2495 base::ScopedFD
fd_closer(fd
);
2499 TEST(ScopedFD
, ScopedFDCrashesOnCloseFailure
) {
2501 ASSERT_EQ(0, pipe(fds
));
2502 base::ScopedFD
read_end_closer(fds
[0]);
2503 EXPECT_EQ(0, IGNORE_EINTR(close(fds
[1])));
2504 #if defined(GTEST_HAS_DEATH_TEST)
2505 // This is the only thread. This file descriptor should no longer be valid.
2506 // Trying to close it should crash. This is important for security.
2507 EXPECT_DEATH(CloseWithScopedFD(fds
[1]), "");
2511 #endif // defined(OS_POSIX)