1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "build/build_config.h"
19 #include "base/base_paths.h"
20 #include "base/file_util.h"
21 #include "base/files/file_enumerator.h"
22 #include "base/files/file_path.h"
23 #include "base/files/scoped_temp_dir.h"
24 #include "base/path_service.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/test/test_file_util.h"
27 #include "base/threading/platform_thread.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "testing/platform_test.h"
32 #include "base/win/scoped_handle.h"
33 #include "base/win/windows_version.h"
36 #if defined(OS_ANDROID)
37 #include "base/android/content_uri_utils.h"
40 // This macro helps avoid wrapped lines in the test structs.
41 #define FPL(x) FILE_PATH_LITERAL(x)
47 // To test that file_util::Normalize FilePath() deals with NTFS reparse points
48 // correctly, we need functions to create and delete reparse points.
50 typedef struct _REPARSE_DATA_BUFFER
{
52 USHORT ReparseDataLength
;
56 USHORT SubstituteNameOffset
;
57 USHORT SubstituteNameLength
;
58 USHORT PrintNameOffset
;
59 USHORT PrintNameLength
;
62 } SymbolicLinkReparseBuffer
;
64 USHORT SubstituteNameOffset
;
65 USHORT SubstituteNameLength
;
66 USHORT PrintNameOffset
;
67 USHORT PrintNameLength
;
69 } MountPointReparseBuffer
;
72 } GenericReparseBuffer
;
74 } REPARSE_DATA_BUFFER
, *PREPARSE_DATA_BUFFER
;
76 // Sets a reparse point. |source| will now point to |target|. Returns true if
77 // the call succeeds, false otherwise.
78 bool SetReparsePoint(HANDLE source
, const FilePath
& target_path
) {
79 std::wstring kPathPrefix
= L
"\\??\\";
80 std::wstring target_str
;
81 // The juction will not work if the target path does not start with \??\ .
82 if (kPathPrefix
!= target_path
.value().substr(0, kPathPrefix
.size()))
83 target_str
+= kPathPrefix
;
84 target_str
+= target_path
.value();
85 const wchar_t* target
= target_str
.c_str();
86 USHORT size_target
= static_cast<USHORT
>(wcslen(target
)) * sizeof(target
[0]);
87 char buffer
[2000] = {0};
90 REPARSE_DATA_BUFFER
* data
= reinterpret_cast<REPARSE_DATA_BUFFER
*>(buffer
);
92 data
->ReparseTag
= 0xa0000003;
93 memcpy(data
->MountPointReparseBuffer
.PathBuffer
, target
, size_target
+ 2);
95 data
->MountPointReparseBuffer
.SubstituteNameLength
= size_target
;
96 data
->MountPointReparseBuffer
.PrintNameOffset
= size_target
+ 2;
97 data
->ReparseDataLength
= size_target
+ 4 + 8;
99 int data_size
= data
->ReparseDataLength
+ 8;
101 if (!DeviceIoControl(source
, FSCTL_SET_REPARSE_POINT
, &buffer
, data_size
,
102 NULL
, 0, &returned
, NULL
)) {
108 // Delete the reparse point referenced by |source|. Returns true if the call
109 // succeeds, false otherwise.
110 bool DeleteReparsePoint(HANDLE source
) {
112 REPARSE_DATA_BUFFER data
= {0};
113 data
.ReparseTag
= 0xa0000003;
114 if (!DeviceIoControl(source
, FSCTL_DELETE_REPARSE_POINT
, &data
, 8, NULL
, 0,
121 // Manages a reparse point for a test.
124 // Creates a reparse point from |source| (an empty directory) to |target|.
125 ReparsePoint(const FilePath
& source
, const FilePath
& target
) {
127 ::CreateFile(source
.value().c_str(),
129 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
132 FILE_FLAG_BACKUP_SEMANTICS
, // Needed to open a directory.
134 created_
= dir_
.IsValid() && SetReparsePoint(dir_
, target
);
139 DeleteReparsePoint(dir_
);
142 bool IsValid() { return created_
; }
145 win::ScopedHandle dir_
;
147 DISALLOW_COPY_AND_ASSIGN(ReparsePoint
);
152 #if defined(OS_POSIX)
153 // Provide a simple way to change the permissions bits on |path| in tests.
154 // ASSERT failures will return, but not stop the test. Caller should wrap
155 // calls to this function in ASSERT_NO_FATAL_FAILURE().
156 void ChangePosixFilePermissions(const FilePath
& path
,
157 int mode_bits_to_set
,
158 int mode_bits_to_clear
) {
159 ASSERT_FALSE(mode_bits_to_set
& mode_bits_to_clear
)
160 << "Can't set and clear the same bits.";
163 ASSERT_TRUE(GetPosixFilePermissions(path
, &mode
));
164 mode
|= mode_bits_to_set
;
165 mode
&= ~mode_bits_to_clear
;
166 ASSERT_TRUE(SetPosixFilePermissions(path
, mode
));
168 #endif // defined(OS_POSIX)
170 const wchar_t bogus_content
[] = L
"I'm cannon fodder.";
172 const int FILES_AND_DIRECTORIES
=
173 FileEnumerator::FILES
| FileEnumerator::DIRECTORIES
;
175 // file_util winds up using autoreleased objects on the Mac, so this needs
176 // to be a PlatformTest
177 class FileUtilTest
: public PlatformTest
{
179 virtual void SetUp() OVERRIDE
{
180 PlatformTest::SetUp();
181 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
184 ScopedTempDir temp_dir_
;
187 // Collects all the results from the given file enumerator, and provides an
188 // interface to query whether a given file is present.
189 class FindResultCollector
{
191 explicit FindResultCollector(FileEnumerator
& enumerator
) {
193 while (!(cur_file
= enumerator
.Next()).value().empty()) {
194 FilePath::StringType path
= cur_file
.value();
195 // The file should not be returned twice.
196 EXPECT_TRUE(files_
.end() == files_
.find(path
))
197 << "Same file returned twice";
204 // Returns true if the enumerator found the file.
205 bool HasFile(const FilePath
& file
) const {
206 return files_
.find(file
.value()) != files_
.end();
210 return static_cast<int>(files_
.size());
214 std::set
<FilePath::StringType
> files_
;
217 // Simple function to dump some text into a new file.
218 void CreateTextFile(const FilePath
& filename
,
219 const std::wstring
& contents
) {
221 file
.open(filename
.value().c_str());
222 ASSERT_TRUE(file
.is_open());
227 // Simple function to take out some text from a file.
228 std::wstring
ReadTextFile(const FilePath
& filename
) {
229 wchar_t contents
[64];
231 file
.open(filename
.value().c_str());
232 EXPECT_TRUE(file
.is_open());
233 file
.getline(contents
, arraysize(contents
));
235 return std::wstring(contents
);
239 uint64
FileTimeAsUint64(const FILETIME
& ft
) {
241 u
.LowPart
= ft
.dwLowDateTime
;
242 u
.HighPart
= ft
.dwHighDateTime
;
247 TEST_F(FileUtilTest
, FileAndDirectorySize
) {
248 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
249 // should return 53 bytes.
250 FilePath file_01
= temp_dir_
.path().Append(FPL("The file 01.txt"));
251 CreateTextFile(file_01
, L
"12345678901234567890");
253 ASSERT_TRUE(GetFileSize(file_01
, &size_f1
));
254 EXPECT_EQ(20ll, size_f1
);
256 FilePath subdir_path
= temp_dir_
.path().Append(FPL("Level2"));
257 CreateDirectory(subdir_path
);
259 FilePath file_02
= subdir_path
.Append(FPL("The file 02.txt"));
260 CreateTextFile(file_02
, L
"123456789012345678901234567890");
262 ASSERT_TRUE(GetFileSize(file_02
, &size_f2
));
263 EXPECT_EQ(30ll, size_f2
);
265 FilePath subsubdir_path
= subdir_path
.Append(FPL("Level3"));
266 CreateDirectory(subsubdir_path
);
268 FilePath file_03
= subsubdir_path
.Append(FPL("The file 03.txt"));
269 CreateTextFile(file_03
, L
"123");
271 int64 computed_size
= ComputeDirectorySize(temp_dir_
.path());
272 EXPECT_EQ(size_f1
+ size_f2
+ 3, computed_size
);
275 TEST_F(FileUtilTest
, NormalizeFilePathBasic
) {
276 // Create a directory under the test dir. Because we create it,
277 // we know it is not a link.
278 FilePath file_a_path
= temp_dir_
.path().Append(FPL("file_a"));
279 FilePath dir_path
= temp_dir_
.path().Append(FPL("dir"));
280 FilePath file_b_path
= dir_path
.Append(FPL("file_b"));
281 CreateDirectory(dir_path
);
283 FilePath normalized_file_a_path
, normalized_file_b_path
;
284 ASSERT_FALSE(PathExists(file_a_path
));
285 ASSERT_FALSE(NormalizeFilePath(file_a_path
, &normalized_file_a_path
))
286 << "NormalizeFilePath() should fail on nonexistent paths.";
288 CreateTextFile(file_a_path
, bogus_content
);
289 ASSERT_TRUE(PathExists(file_a_path
));
290 ASSERT_TRUE(NormalizeFilePath(file_a_path
, &normalized_file_a_path
));
292 CreateTextFile(file_b_path
, bogus_content
);
293 ASSERT_TRUE(PathExists(file_b_path
));
294 ASSERT_TRUE(NormalizeFilePath(file_b_path
, &normalized_file_b_path
));
296 // Beacuse this test created |dir_path|, we know it is not a link
297 // or junction. So, the real path of the directory holding file a
298 // must be the parent of the path holding file b.
299 ASSERT_TRUE(normalized_file_a_path
.DirName()
300 .IsParent(normalized_file_b_path
.DirName()));
305 TEST_F(FileUtilTest
, NormalizeFilePathReparsePoints
) {
306 // Build the following directory structure:
312 // | |-> long_name___... (Very long name.)
316 // |-> to_sub_a (reparse point to temp_dir\base_a\sub_a)
317 // |-> to_base_b (reparse point to temp_dir\base_b)
318 // |-> to_sub_long (reparse point to temp_dir\sub_a\long_name_\sub_long)
320 FilePath base_a
= temp_dir_
.path().Append(FPL("base_a"));
321 ASSERT_TRUE(CreateDirectory(base_a
));
323 FilePath sub_a
= base_a
.Append(FPL("sub_a"));
324 ASSERT_TRUE(CreateDirectory(sub_a
));
326 FilePath file_txt
= sub_a
.Append(FPL("file.txt"));
327 CreateTextFile(file_txt
, bogus_content
);
329 // Want a directory whose name is long enough to make the path to the file
330 // inside just under MAX_PATH chars. This will be used to test that when
331 // a junction expands to a path over MAX_PATH chars in length,
332 // NormalizeFilePath() fails without crashing.
333 FilePath
sub_long_rel(FPL("sub_long"));
334 FilePath
deep_txt(FPL("deep.txt"));
336 int target_length
= MAX_PATH
;
337 target_length
-= (sub_a
.value().length() + 1); // +1 for the sepperator '\'.
338 target_length
-= (sub_long_rel
.Append(deep_txt
).value().length() + 1);
339 // Without making the path a bit shorter, CreateDirectory() fails.
340 // the resulting path is still long enough to hit the failing case in
342 const int kCreateDirLimit
= 4;
343 target_length
-= kCreateDirLimit
;
344 FilePath::StringType long_name_str
= FPL("long_name_");
345 long_name_str
.resize(target_length
, '_');
347 FilePath long_name
= sub_a
.Append(FilePath(long_name_str
));
348 FilePath deep_file
= long_name
.Append(sub_long_rel
).Append(deep_txt
);
349 ASSERT_EQ(MAX_PATH
- kCreateDirLimit
, deep_file
.value().length());
351 FilePath sub_long
= deep_file
.DirName();
352 ASSERT_TRUE(CreateDirectory(sub_long
));
353 CreateTextFile(deep_file
, bogus_content
);
355 FilePath base_b
= temp_dir_
.path().Append(FPL("base_b"));
356 ASSERT_TRUE(CreateDirectory(base_b
));
358 FilePath to_sub_a
= base_b
.Append(FPL("to_sub_a"));
359 ASSERT_TRUE(CreateDirectory(to_sub_a
));
360 FilePath normalized_path
;
362 ReparsePoint
reparse_to_sub_a(to_sub_a
, sub_a
);
363 ASSERT_TRUE(reparse_to_sub_a
.IsValid());
365 FilePath to_base_b
= base_b
.Append(FPL("to_base_b"));
366 ASSERT_TRUE(CreateDirectory(to_base_b
));
367 ReparsePoint
reparse_to_base_b(to_base_b
, base_b
);
368 ASSERT_TRUE(reparse_to_base_b
.IsValid());
370 FilePath to_sub_long
= base_b
.Append(FPL("to_sub_long"));
371 ASSERT_TRUE(CreateDirectory(to_sub_long
));
372 ReparsePoint
reparse_to_sub_long(to_sub_long
, sub_long
);
373 ASSERT_TRUE(reparse_to_sub_long
.IsValid());
375 // Normalize a junction free path: base_a\sub_a\file.txt .
376 ASSERT_TRUE(NormalizeFilePath(file_txt
, &normalized_path
));
377 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
379 // Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
380 // the junction to_sub_a.
381 ASSERT_TRUE(NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
383 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
385 // Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
386 // normalized to exclude junctions to_base_b and to_sub_a .
387 ASSERT_TRUE(NormalizeFilePath(base_b
.Append(FPL("to_base_b"))
388 .Append(FPL("to_base_b"))
389 .Append(FPL("to_sub_a"))
390 .Append(FPL("file.txt")),
392 ASSERT_STREQ(file_txt
.value().c_str(), normalized_path
.value().c_str());
394 // A long enough path will cause NormalizeFilePath() to fail. Make a long
395 // path using to_base_b many times, and check that paths long enough to fail
396 // do not cause a crash.
397 FilePath long_path
= base_b
;
398 const int kLengthLimit
= MAX_PATH
+ 200;
399 while (long_path
.value().length() <= kLengthLimit
) {
400 long_path
= long_path
.Append(FPL("to_base_b"));
402 long_path
= long_path
.Append(FPL("to_sub_a"))
403 .Append(FPL("file.txt"));
405 ASSERT_FALSE(NormalizeFilePath(long_path
, &normalized_path
));
407 // Normalizing the junction to deep.txt should fail, because the expanded
408 // path to deep.txt is longer than MAX_PATH.
409 ASSERT_FALSE(NormalizeFilePath(to_sub_long
.Append(deep_txt
),
412 // Delete the reparse points, and see that NormalizeFilePath() fails
416 ASSERT_FALSE(NormalizeFilePath(to_sub_a
.Append(FPL("file.txt")),
420 TEST_F(FileUtilTest
, DevicePathToDriveLetter
) {
421 // Get a drive letter.
422 std::wstring real_drive_letter
= temp_dir_
.path().value().substr(0, 2);
423 if (!isalpha(real_drive_letter
[0]) || ':' != real_drive_letter
[1]) {
424 LOG(ERROR
) << "Can't get a drive letter to test with.";
428 // Get the NT style path to that drive.
429 wchar_t device_path
[MAX_PATH
] = {'\0'};
431 ::QueryDosDevice(real_drive_letter
.c_str(), device_path
, MAX_PATH
));
432 FilePath
actual_device_path(device_path
);
435 // Run DevicePathToDriveLetterPath() on the NT style path we got from
436 // QueryDosDevice(). Expect the drive letter we started with.
437 ASSERT_TRUE(DevicePathToDriveLetterPath(actual_device_path
, &win32_path
));
438 ASSERT_EQ(real_drive_letter
, win32_path
.value());
440 // Add some directories to the path. Expect those extra path componenets
442 FilePath
kRelativePath(FPL("dir1\\dir2\\file.txt"));
443 ASSERT_TRUE(DevicePathToDriveLetterPath(
444 actual_device_path
.Append(kRelativePath
),
446 EXPECT_EQ(FilePath(real_drive_letter
+ L
"\\").Append(kRelativePath
).value(),
449 // Deform the real path so that it is invalid by removing the last four
450 // characters. The way windows names devices that are hard disks
451 // (\Device\HardDiskVolume${NUMBER}) guarantees that the string is longer
452 // than three characters. The only way the truncated string could be a
453 // real drive is if more than 10^3 disks are mounted:
454 // \Device\HardDiskVolume10000 would be truncated to \Device\HardDiskVolume1
455 // Check that DevicePathToDriveLetterPath fails.
456 int path_length
= actual_device_path
.value().length();
457 int new_length
= path_length
- 4;
458 ASSERT_LT(0, new_length
);
459 FilePath
prefix_of_real_device_path(
460 actual_device_path
.value().substr(0, new_length
));
461 ASSERT_FALSE(DevicePathToDriveLetterPath(prefix_of_real_device_path
,
464 ASSERT_FALSE(DevicePathToDriveLetterPath(
465 prefix_of_real_device_path
.Append(kRelativePath
),
468 // Deform the real path so that it is invalid by adding some characters. For
469 // example, if C: maps to \Device\HardDiskVolume8, then we simulate a
470 // request for the drive letter whose native path is
471 // \Device\HardDiskVolume812345 . We assume such a device does not exist,
472 // because drives are numbered in order and mounting 112345 hard disks will
474 const FilePath::StringType kExtraChars
= FPL("12345");
476 FilePath
real_device_path_plus_numbers(
477 actual_device_path
.value() + kExtraChars
);
479 ASSERT_FALSE(DevicePathToDriveLetterPath(
480 real_device_path_plus_numbers
,
483 ASSERT_FALSE(DevicePathToDriveLetterPath(
484 real_device_path_plus_numbers
.Append(kRelativePath
),
488 TEST_F(FileUtilTest
, CreateTemporaryFileInDirLongPathTest
) {
489 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
490 // if it is available. This test requires that:
491 // - the filesystem at |temp_dir_| supports long filenames.
492 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
493 // directories of |temp_dir_|.
494 const FilePath::CharType kLongDirName
[] = FPL("A long path");
495 const FilePath::CharType kTestSubDirName
[] = FPL("test");
496 FilePath long_test_dir
= temp_dir_
.path().Append(kLongDirName
);
497 ASSERT_TRUE(CreateDirectory(long_test_dir
));
499 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
500 // different short name.
501 WCHAR path_buffer
[MAX_PATH
];
502 DWORD path_buffer_length
= GetShortPathName(long_test_dir
.value().c_str(),
503 path_buffer
, MAX_PATH
);
504 ASSERT_LT(path_buffer_length
, DWORD(MAX_PATH
));
505 ASSERT_NE(DWORD(0), path_buffer_length
);
506 FilePath
short_test_dir(path_buffer
);
507 ASSERT_STRNE(kLongDirName
, short_test_dir
.BaseName().value().c_str());
510 ASSERT_TRUE(CreateTemporaryFileInDir(short_test_dir
, &temp_file
));
511 EXPECT_STREQ(kLongDirName
, temp_file
.DirName().BaseName().value().c_str());
512 EXPECT_TRUE(PathExists(temp_file
));
514 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
515 // unreadable. We should still be able to create a temp file in the
516 // subdirectory, but we won't be able to determine the long path for it. This
517 // mimics the environment that some users run where their user profiles reside
518 // in a location where the don't have full access to the higher level
519 // directories. (Note that this assumption is true for NTFS, but not for some
520 // network file systems. E.g. AFS).
521 FilePath access_test_dir
= long_test_dir
.Append(kTestSubDirName
);
522 ASSERT_TRUE(CreateDirectory(access_test_dir
));
523 file_util::PermissionRestorer
long_test_dir_restorer(long_test_dir
);
524 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir
));
526 // Use the short form of the directory to create a temporary filename.
527 ASSERT_TRUE(CreateTemporaryFileInDir(
528 short_test_dir
.Append(kTestSubDirName
), &temp_file
));
529 EXPECT_TRUE(PathExists(temp_file
));
530 EXPECT_TRUE(short_test_dir
.IsParent(temp_file
.DirName()));
532 // Check that the long path can't be determined for |temp_file|.
533 path_buffer_length
= GetLongPathName(temp_file
.value().c_str(),
534 path_buffer
, MAX_PATH
);
535 EXPECT_EQ(DWORD(0), path_buffer_length
);
538 #endif // defined(OS_WIN)
540 #if defined(OS_POSIX)
542 TEST_F(FileUtilTest
, CreateAndReadSymlinks
) {
543 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
544 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
545 CreateTextFile(link_to
, bogus_content
);
547 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
548 << "Failed to create file symlink.";
550 // If we created the link properly, we should be able to read the contents
552 std::wstring contents
= ReadTextFile(link_from
);
553 EXPECT_EQ(bogus_content
, contents
);
556 ASSERT_TRUE(ReadSymbolicLink(link_from
, &result
));
557 EXPECT_EQ(link_to
.value(), result
.value());
559 // Link to a directory.
560 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
561 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
562 ASSERT_TRUE(CreateDirectory(link_to
));
563 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
564 << "Failed to create directory symlink.";
567 EXPECT_FALSE(CreateSymbolicLink(link_to
, link_to
));
568 EXPECT_FALSE(ReadSymbolicLink(link_to
, &result
));
569 FilePath missing
= temp_dir_
.path().Append(FPL("missing"));
570 EXPECT_FALSE(ReadSymbolicLink(missing
, &result
));
573 // The following test of NormalizeFilePath() require that we create a symlink.
574 // This can not be done on Windows before Vista. On Vista, creating a symlink
575 // requires privilege "SeCreateSymbolicLinkPrivilege".
576 // TODO(skerner): Investigate the possibility of giving base_unittests the
577 // privileges required to create a symlink.
578 TEST_F(FileUtilTest
, NormalizeFilePathSymlinks
) {
579 // Link one file to another.
580 FilePath link_from
= temp_dir_
.path().Append(FPL("from_file"));
581 FilePath link_to
= temp_dir_
.path().Append(FPL("to_file"));
582 CreateTextFile(link_to
, bogus_content
);
584 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
585 << "Failed to create file symlink.";
587 // Check that NormalizeFilePath sees the link.
588 FilePath normalized_path
;
589 ASSERT_TRUE(NormalizeFilePath(link_from
, &normalized_path
));
590 EXPECT_NE(link_from
, link_to
);
591 EXPECT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
592 EXPECT_EQ(link_to
.BaseName().value(), normalized_path
.BaseName().value());
594 // Link to a directory.
595 link_from
= temp_dir_
.path().Append(FPL("from_dir"));
596 link_to
= temp_dir_
.path().Append(FPL("to_dir"));
597 ASSERT_TRUE(CreateDirectory(link_to
));
598 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
599 << "Failed to create directory symlink.";
601 EXPECT_FALSE(NormalizeFilePath(link_from
, &normalized_path
))
602 << "Links to directories should return false.";
604 // Test that a loop in the links causes NormalizeFilePath() to return false.
605 link_from
= temp_dir_
.path().Append(FPL("link_a"));
606 link_to
= temp_dir_
.path().Append(FPL("link_b"));
607 ASSERT_TRUE(CreateSymbolicLink(link_to
, link_from
))
608 << "Failed to create loop symlink a.";
609 ASSERT_TRUE(CreateSymbolicLink(link_from
, link_to
))
610 << "Failed to create loop symlink b.";
613 EXPECT_FALSE(NormalizeFilePath(link_from
, &normalized_path
));
615 #endif // defined(OS_POSIX)
617 TEST_F(FileUtilTest
, DeleteNonExistent
) {
618 FilePath non_existent
= temp_dir_
.path().AppendASCII("bogus_file_dne.foobar");
619 ASSERT_FALSE(PathExists(non_existent
));
621 EXPECT_TRUE(DeleteFile(non_existent
, false));
622 ASSERT_FALSE(PathExists(non_existent
));
623 EXPECT_TRUE(DeleteFile(non_existent
, true));
624 ASSERT_FALSE(PathExists(non_existent
));
627 TEST_F(FileUtilTest
, DeleteFile
) {
629 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 1.txt"));
630 CreateTextFile(file_name
, bogus_content
);
631 ASSERT_TRUE(PathExists(file_name
));
633 // Make sure it's deleted
634 EXPECT_TRUE(DeleteFile(file_name
, false));
635 EXPECT_FALSE(PathExists(file_name
));
637 // Test recursive case, create a new file
638 file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
639 CreateTextFile(file_name
, bogus_content
);
640 ASSERT_TRUE(PathExists(file_name
));
642 // Make sure it's deleted
643 EXPECT_TRUE(DeleteFile(file_name
, true));
644 EXPECT_FALSE(PathExists(file_name
));
647 #if defined(OS_POSIX)
648 TEST_F(FileUtilTest
, DeleteSymlinkToExistentFile
) {
650 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteFile 2.txt"));
651 CreateTextFile(file_name
, bogus_content
);
652 ASSERT_TRUE(PathExists(file_name
));
654 // Create a symlink to the file.
655 FilePath file_link
= temp_dir_
.path().Append("file_link_2");
656 ASSERT_TRUE(CreateSymbolicLink(file_name
, file_link
))
657 << "Failed to create symlink.";
659 // Delete the symbolic link.
660 EXPECT_TRUE(DeleteFile(file_link
, false));
662 // Make sure original file is not deleted.
663 EXPECT_FALSE(PathExists(file_link
));
664 EXPECT_TRUE(PathExists(file_name
));
667 TEST_F(FileUtilTest
, DeleteSymlinkToNonExistentFile
) {
668 // Create a non-existent file path.
669 FilePath non_existent
= temp_dir_
.path().Append(FPL("Test DeleteFile 3.txt"));
670 EXPECT_FALSE(PathExists(non_existent
));
672 // Create a symlink to the non-existent file.
673 FilePath file_link
= temp_dir_
.path().Append("file_link_3");
674 ASSERT_TRUE(CreateSymbolicLink(non_existent
, file_link
))
675 << "Failed to create symlink.";
677 // Make sure the symbolic link is exist.
678 EXPECT_TRUE(IsLink(file_link
));
679 EXPECT_FALSE(PathExists(file_link
));
681 // Delete the symbolic link.
682 EXPECT_TRUE(DeleteFile(file_link
, false));
684 // Make sure the symbolic link is deleted.
685 EXPECT_FALSE(IsLink(file_link
));
688 TEST_F(FileUtilTest
, ChangeFilePermissionsAndRead
) {
689 // Create a file path.
690 FilePath file_name
= temp_dir_
.path().Append(FPL("Test Readable File.txt"));
691 EXPECT_FALSE(PathExists(file_name
));
693 const std::string
kData("hello");
695 int buffer_size
= kData
.length();
696 char* buffer
= new char[buffer_size
];
699 EXPECT_EQ(static_cast<int>(kData
.length()),
700 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
701 EXPECT_TRUE(PathExists(file_name
));
703 // Make sure the file is readable.
705 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
706 EXPECT_TRUE(mode
& FILE_PERMISSION_READ_BY_USER
);
708 // Get rid of the read permission.
709 EXPECT_TRUE(SetPosixFilePermissions(file_name
, 0u));
710 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
711 EXPECT_FALSE(mode
& FILE_PERMISSION_READ_BY_USER
);
712 // Make sure the file can't be read.
713 EXPECT_EQ(-1, ReadFile(file_name
, buffer
, buffer_size
));
715 // Give the read permission.
716 EXPECT_TRUE(SetPosixFilePermissions(file_name
, FILE_PERMISSION_READ_BY_USER
));
717 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
718 EXPECT_TRUE(mode
& FILE_PERMISSION_READ_BY_USER
);
719 // Make sure the file can be read.
720 EXPECT_EQ(static_cast<int>(kData
.length()),
721 ReadFile(file_name
, buffer
, buffer_size
));
724 EXPECT_TRUE(DeleteFile(file_name
, false));
725 EXPECT_FALSE(PathExists(file_name
));
730 TEST_F(FileUtilTest
, ChangeFilePermissionsAndWrite
) {
731 // Create a file path.
732 FilePath file_name
= temp_dir_
.path().Append(FPL("Test Readable File.txt"));
733 EXPECT_FALSE(PathExists(file_name
));
735 const std::string
kData("hello");
738 EXPECT_EQ(static_cast<int>(kData
.length()),
739 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
740 EXPECT_TRUE(PathExists(file_name
));
742 // Make sure the file is writable.
744 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
745 EXPECT_TRUE(mode
& FILE_PERMISSION_WRITE_BY_USER
);
746 EXPECT_TRUE(PathIsWritable(file_name
));
748 // Get rid of the write permission.
749 EXPECT_TRUE(SetPosixFilePermissions(file_name
, 0u));
750 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
751 EXPECT_FALSE(mode
& FILE_PERMISSION_WRITE_BY_USER
);
752 // Make sure the file can't be write.
754 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
755 EXPECT_FALSE(PathIsWritable(file_name
));
757 // Give read permission.
758 EXPECT_TRUE(SetPosixFilePermissions(file_name
,
759 FILE_PERMISSION_WRITE_BY_USER
));
760 EXPECT_TRUE(GetPosixFilePermissions(file_name
, &mode
));
761 EXPECT_TRUE(mode
& FILE_PERMISSION_WRITE_BY_USER
);
762 // Make sure the file can be write.
763 EXPECT_EQ(static_cast<int>(kData
.length()),
764 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
765 EXPECT_TRUE(PathIsWritable(file_name
));
768 EXPECT_TRUE(DeleteFile(file_name
, false));
769 EXPECT_FALSE(PathExists(file_name
));
772 TEST_F(FileUtilTest
, ChangeDirectoryPermissionsAndEnumerate
) {
773 // Create a directory path.
774 FilePath subdir_path
=
775 temp_dir_
.path().Append(FPL("PermissionTest1"));
776 CreateDirectory(subdir_path
);
777 ASSERT_TRUE(PathExists(subdir_path
));
779 // Create a dummy file to enumerate.
780 FilePath file_name
= subdir_path
.Append(FPL("Test Readable File.txt"));
781 EXPECT_FALSE(PathExists(file_name
));
782 const std::string
kData("hello");
783 EXPECT_EQ(static_cast<int>(kData
.length()),
784 file_util::WriteFile(file_name
, kData
.data(), kData
.length()));
785 EXPECT_TRUE(PathExists(file_name
));
787 // Make sure the directory has the all permissions.
789 EXPECT_TRUE(GetPosixFilePermissions(subdir_path
, &mode
));
790 EXPECT_EQ(FILE_PERMISSION_USER_MASK
, mode
& FILE_PERMISSION_USER_MASK
);
792 // Get rid of the permissions from the directory.
793 EXPECT_TRUE(SetPosixFilePermissions(subdir_path
, 0u));
794 EXPECT_TRUE(GetPosixFilePermissions(subdir_path
, &mode
));
795 EXPECT_FALSE(mode
& FILE_PERMISSION_USER_MASK
);
797 // Make sure the file in the directory can't be enumerated.
798 FileEnumerator
f1(subdir_path
, true, FileEnumerator::FILES
);
799 EXPECT_TRUE(PathExists(subdir_path
));
800 FindResultCollector
c1(f1
);
801 EXPECT_EQ(c1
.size(), 0);
802 EXPECT_FALSE(GetPosixFilePermissions(file_name
, &mode
));
804 // Give the permissions to the directory.
805 EXPECT_TRUE(SetPosixFilePermissions(subdir_path
, FILE_PERMISSION_USER_MASK
));
806 EXPECT_TRUE(GetPosixFilePermissions(subdir_path
, &mode
));
807 EXPECT_EQ(FILE_PERMISSION_USER_MASK
, mode
& FILE_PERMISSION_USER_MASK
);
809 // Make sure the file in the directory can be enumerated.
810 FileEnumerator
f2(subdir_path
, true, FileEnumerator::FILES
);
811 FindResultCollector
c2(f2
);
812 EXPECT_TRUE(c2
.HasFile(file_name
));
813 EXPECT_EQ(c2
.size(), 1);
816 EXPECT_TRUE(DeleteFile(subdir_path
, true));
817 EXPECT_FALSE(PathExists(subdir_path
));
820 #endif // defined(OS_POSIX)
823 // Tests that the Delete function works for wild cards, especially
824 // with the recursion flag. Also coincidentally tests PathExists.
825 // TODO(erikkay): see if anyone's actually using this feature of the API
826 TEST_F(FileUtilTest
, DeleteWildCard
) {
827 // Create a file and a directory
828 FilePath file_name
= temp_dir_
.path().Append(FPL("Test DeleteWildCard.txt"));
829 CreateTextFile(file_name
, bogus_content
);
830 ASSERT_TRUE(PathExists(file_name
));
832 FilePath subdir_path
= temp_dir_
.path().Append(FPL("DeleteWildCardDir"));
833 CreateDirectory(subdir_path
);
834 ASSERT_TRUE(PathExists(subdir_path
));
836 // Create the wildcard path
837 FilePath directory_contents
= temp_dir_
.path();
838 directory_contents
= directory_contents
.Append(FPL("*"));
840 // Delete non-recursively and check that only the file is deleted
841 EXPECT_TRUE(DeleteFile(directory_contents
, false));
842 EXPECT_FALSE(PathExists(file_name
));
843 EXPECT_TRUE(PathExists(subdir_path
));
845 // Delete recursively and make sure all contents are deleted
846 EXPECT_TRUE(DeleteFile(directory_contents
, true));
847 EXPECT_FALSE(PathExists(file_name
));
848 EXPECT_FALSE(PathExists(subdir_path
));
851 // TODO(erikkay): see if anyone's actually using this feature of the API
852 TEST_F(FileUtilTest
, DeleteNonExistantWildCard
) {
853 // Create a file and a directory
854 FilePath subdir_path
=
855 temp_dir_
.path().Append(FPL("DeleteNonExistantWildCard"));
856 CreateDirectory(subdir_path
);
857 ASSERT_TRUE(PathExists(subdir_path
));
859 // Create the wildcard path
860 FilePath directory_contents
= subdir_path
;
861 directory_contents
= directory_contents
.Append(FPL("*"));
863 // Delete non-recursively and check nothing got deleted
864 EXPECT_TRUE(DeleteFile(directory_contents
, false));
865 EXPECT_TRUE(PathExists(subdir_path
));
867 // Delete recursively and check nothing got deleted
868 EXPECT_TRUE(DeleteFile(directory_contents
, true));
869 EXPECT_TRUE(PathExists(subdir_path
));
873 // Tests non-recursive Delete() for a directory.
874 TEST_F(FileUtilTest
, DeleteDirNonRecursive
) {
875 // Create a subdirectory and put a file and two directories inside.
876 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirNonRecursive"));
877 CreateDirectory(test_subdir
);
878 ASSERT_TRUE(PathExists(test_subdir
));
880 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDir.txt"));
881 CreateTextFile(file_name
, bogus_content
);
882 ASSERT_TRUE(PathExists(file_name
));
884 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
885 CreateDirectory(subdir_path1
);
886 ASSERT_TRUE(PathExists(subdir_path1
));
888 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
889 CreateDirectory(subdir_path2
);
890 ASSERT_TRUE(PathExists(subdir_path2
));
892 // Delete non-recursively and check that the empty dir got deleted
893 EXPECT_TRUE(DeleteFile(subdir_path2
, false));
894 EXPECT_FALSE(PathExists(subdir_path2
));
896 // Delete non-recursively and check that nothing got deleted
897 EXPECT_FALSE(DeleteFile(test_subdir
, false));
898 EXPECT_TRUE(PathExists(test_subdir
));
899 EXPECT_TRUE(PathExists(file_name
));
900 EXPECT_TRUE(PathExists(subdir_path1
));
903 // Tests recursive Delete() for a directory.
904 TEST_F(FileUtilTest
, DeleteDirRecursive
) {
905 // Create a subdirectory and put a file and two directories inside.
906 FilePath test_subdir
= temp_dir_
.path().Append(FPL("DeleteDirRecursive"));
907 CreateDirectory(test_subdir
);
908 ASSERT_TRUE(PathExists(test_subdir
));
910 FilePath file_name
= test_subdir
.Append(FPL("Test DeleteDirRecursive.txt"));
911 CreateTextFile(file_name
, bogus_content
);
912 ASSERT_TRUE(PathExists(file_name
));
914 FilePath subdir_path1
= test_subdir
.Append(FPL("TestSubDir1"));
915 CreateDirectory(subdir_path1
);
916 ASSERT_TRUE(PathExists(subdir_path1
));
918 FilePath subdir_path2
= test_subdir
.Append(FPL("TestSubDir2"));
919 CreateDirectory(subdir_path2
);
920 ASSERT_TRUE(PathExists(subdir_path2
));
922 // Delete recursively and check that the empty dir got deleted
923 EXPECT_TRUE(DeleteFile(subdir_path2
, true));
924 EXPECT_FALSE(PathExists(subdir_path2
));
926 // Delete recursively and check that everything got deleted
927 EXPECT_TRUE(DeleteFile(test_subdir
, true));
928 EXPECT_FALSE(PathExists(file_name
));
929 EXPECT_FALSE(PathExists(subdir_path1
));
930 EXPECT_FALSE(PathExists(test_subdir
));
933 TEST_F(FileUtilTest
, MoveFileNew
) {
935 FilePath file_name_from
=
936 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
937 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
938 ASSERT_TRUE(PathExists(file_name_from
));
941 FilePath file_name_to
= temp_dir_
.path().Append(
942 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
943 ASSERT_FALSE(PathExists(file_name_to
));
945 EXPECT_TRUE(Move(file_name_from
, file_name_to
));
947 // Check everything has been moved.
948 EXPECT_FALSE(PathExists(file_name_from
));
949 EXPECT_TRUE(PathExists(file_name_to
));
952 TEST_F(FileUtilTest
, MoveFileExists
) {
954 FilePath file_name_from
=
955 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
956 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
957 ASSERT_TRUE(PathExists(file_name_from
));
959 // The destination name.
960 FilePath file_name_to
= temp_dir_
.path().Append(
961 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
962 CreateTextFile(file_name_to
, L
"Old file content");
963 ASSERT_TRUE(PathExists(file_name_to
));
965 EXPECT_TRUE(Move(file_name_from
, file_name_to
));
967 // Check everything has been moved.
968 EXPECT_FALSE(PathExists(file_name_from
));
969 EXPECT_TRUE(PathExists(file_name_to
));
970 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
973 TEST_F(FileUtilTest
, MoveFileDirExists
) {
975 FilePath file_name_from
=
976 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
977 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
978 ASSERT_TRUE(PathExists(file_name_from
));
980 // The destination directory
981 FilePath dir_name_to
=
982 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
983 CreateDirectory(dir_name_to
);
984 ASSERT_TRUE(PathExists(dir_name_to
));
986 EXPECT_FALSE(Move(file_name_from
, dir_name_to
));
990 TEST_F(FileUtilTest
, MoveNew
) {
991 // Create a directory
992 FilePath dir_name_from
=
993 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
994 CreateDirectory(dir_name_from
);
995 ASSERT_TRUE(PathExists(dir_name_from
));
997 // Create a file under the directory
998 FilePath
txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
999 FilePath file_name_from
= dir_name_from
.Append(txt_file_name
);
1000 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1001 ASSERT_TRUE(PathExists(file_name_from
));
1003 // Move the directory.
1004 FilePath dir_name_to
=
1005 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1006 FilePath file_name_to
=
1007 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1009 ASSERT_FALSE(PathExists(dir_name_to
));
1011 EXPECT_TRUE(Move(dir_name_from
, dir_name_to
));
1013 // Check everything has been moved.
1014 EXPECT_FALSE(PathExists(dir_name_from
));
1015 EXPECT_FALSE(PathExists(file_name_from
));
1016 EXPECT_TRUE(PathExists(dir_name_to
));
1017 EXPECT_TRUE(PathExists(file_name_to
));
1019 // Test path traversal.
1020 file_name_from
= dir_name_to
.Append(txt_file_name
);
1021 file_name_to
= dir_name_to
.Append(FILE_PATH_LITERAL(".."));
1022 file_name_to
= file_name_to
.Append(txt_file_name
);
1023 EXPECT_FALSE(Move(file_name_from
, file_name_to
));
1024 EXPECT_TRUE(PathExists(file_name_from
));
1025 EXPECT_FALSE(PathExists(file_name_to
));
1026 EXPECT_TRUE(internal::MoveUnsafe(file_name_from
, file_name_to
));
1027 EXPECT_FALSE(PathExists(file_name_from
));
1028 EXPECT_TRUE(PathExists(file_name_to
));
1031 TEST_F(FileUtilTest
, MoveExist
) {
1032 // Create a directory
1033 FilePath dir_name_from
=
1034 temp_dir_
.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1035 CreateDirectory(dir_name_from
);
1036 ASSERT_TRUE(PathExists(dir_name_from
));
1038 // Create a file under the directory
1039 FilePath file_name_from
=
1040 dir_name_from
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1041 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1042 ASSERT_TRUE(PathExists(file_name_from
));
1044 // Move the directory
1045 FilePath dir_name_exists
=
1046 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1048 FilePath dir_name_to
=
1049 dir_name_exists
.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1050 FilePath file_name_to
=
1051 dir_name_to
.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1053 // Create the destination directory.
1054 CreateDirectory(dir_name_exists
);
1055 ASSERT_TRUE(PathExists(dir_name_exists
));
1057 EXPECT_TRUE(Move(dir_name_from
, dir_name_to
));
1059 // Check everything has been moved.
1060 EXPECT_FALSE(PathExists(dir_name_from
));
1061 EXPECT_FALSE(PathExists(file_name_from
));
1062 EXPECT_TRUE(PathExists(dir_name_to
));
1063 EXPECT_TRUE(PathExists(file_name_to
));
1066 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyNew
) {
1067 // Create a directory.
1068 FilePath dir_name_from
=
1069 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1070 CreateDirectory(dir_name_from
);
1071 ASSERT_TRUE(PathExists(dir_name_from
));
1073 // Create a file under the directory.
1074 FilePath file_name_from
=
1075 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1076 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1077 ASSERT_TRUE(PathExists(file_name_from
));
1079 // Create a subdirectory.
1080 FilePath subdir_name_from
=
1081 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1082 CreateDirectory(subdir_name_from
);
1083 ASSERT_TRUE(PathExists(subdir_name_from
));
1085 // Create a file under the subdirectory.
1086 FilePath file_name2_from
=
1087 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1088 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1089 ASSERT_TRUE(PathExists(file_name2_from
));
1091 // Copy the directory recursively.
1092 FilePath dir_name_to
=
1093 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1094 FilePath file_name_to
=
1095 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1096 FilePath subdir_name_to
=
1097 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1098 FilePath file_name2_to
=
1099 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1101 ASSERT_FALSE(PathExists(dir_name_to
));
1103 EXPECT_TRUE(CopyDirectory(dir_name_from
, dir_name_to
, true));
1105 // Check everything has been copied.
1106 EXPECT_TRUE(PathExists(dir_name_from
));
1107 EXPECT_TRUE(PathExists(file_name_from
));
1108 EXPECT_TRUE(PathExists(subdir_name_from
));
1109 EXPECT_TRUE(PathExists(file_name2_from
));
1110 EXPECT_TRUE(PathExists(dir_name_to
));
1111 EXPECT_TRUE(PathExists(file_name_to
));
1112 EXPECT_TRUE(PathExists(subdir_name_to
));
1113 EXPECT_TRUE(PathExists(file_name2_to
));
1116 TEST_F(FileUtilTest
, CopyDirectoryRecursivelyExists
) {
1117 // Create a directory.
1118 FilePath dir_name_from
=
1119 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1120 CreateDirectory(dir_name_from
);
1121 ASSERT_TRUE(PathExists(dir_name_from
));
1123 // Create a file under the directory.
1124 FilePath file_name_from
=
1125 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1126 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1127 ASSERT_TRUE(PathExists(file_name_from
));
1129 // Create a subdirectory.
1130 FilePath subdir_name_from
=
1131 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1132 CreateDirectory(subdir_name_from
);
1133 ASSERT_TRUE(PathExists(subdir_name_from
));
1135 // Create a file under the subdirectory.
1136 FilePath file_name2_from
=
1137 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1138 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1139 ASSERT_TRUE(PathExists(file_name2_from
));
1141 // Copy the directory recursively.
1142 FilePath dir_name_exists
=
1143 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1145 FilePath dir_name_to
=
1146 dir_name_exists
.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1147 FilePath file_name_to
=
1148 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1149 FilePath subdir_name_to
=
1150 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1151 FilePath file_name2_to
=
1152 subdir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1154 // Create the destination directory.
1155 CreateDirectory(dir_name_exists
);
1156 ASSERT_TRUE(PathExists(dir_name_exists
));
1158 EXPECT_TRUE(CopyDirectory(dir_name_from
, dir_name_exists
, true));
1160 // Check everything has been copied.
1161 EXPECT_TRUE(PathExists(dir_name_from
));
1162 EXPECT_TRUE(PathExists(file_name_from
));
1163 EXPECT_TRUE(PathExists(subdir_name_from
));
1164 EXPECT_TRUE(PathExists(file_name2_from
));
1165 EXPECT_TRUE(PathExists(dir_name_to
));
1166 EXPECT_TRUE(PathExists(file_name_to
));
1167 EXPECT_TRUE(PathExists(subdir_name_to
));
1168 EXPECT_TRUE(PathExists(file_name2_to
));
1171 TEST_F(FileUtilTest
, CopyDirectoryNew
) {
1172 // Create a directory.
1173 FilePath dir_name_from
=
1174 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1175 CreateDirectory(dir_name_from
);
1176 ASSERT_TRUE(PathExists(dir_name_from
));
1178 // Create a file under the directory.
1179 FilePath file_name_from
=
1180 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1181 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1182 ASSERT_TRUE(PathExists(file_name_from
));
1184 // Create a subdirectory.
1185 FilePath subdir_name_from
=
1186 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1187 CreateDirectory(subdir_name_from
);
1188 ASSERT_TRUE(PathExists(subdir_name_from
));
1190 // Create a file under the subdirectory.
1191 FilePath file_name2_from
=
1192 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1193 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1194 ASSERT_TRUE(PathExists(file_name2_from
));
1196 // Copy the directory not recursively.
1197 FilePath dir_name_to
=
1198 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1199 FilePath file_name_to
=
1200 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1201 FilePath subdir_name_to
=
1202 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1204 ASSERT_FALSE(PathExists(dir_name_to
));
1206 EXPECT_TRUE(CopyDirectory(dir_name_from
, dir_name_to
, false));
1208 // Check everything has been copied.
1209 EXPECT_TRUE(PathExists(dir_name_from
));
1210 EXPECT_TRUE(PathExists(file_name_from
));
1211 EXPECT_TRUE(PathExists(subdir_name_from
));
1212 EXPECT_TRUE(PathExists(file_name2_from
));
1213 EXPECT_TRUE(PathExists(dir_name_to
));
1214 EXPECT_TRUE(PathExists(file_name_to
));
1215 EXPECT_FALSE(PathExists(subdir_name_to
));
1218 TEST_F(FileUtilTest
, CopyDirectoryExists
) {
1219 // Create a directory.
1220 FilePath dir_name_from
=
1221 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1222 CreateDirectory(dir_name_from
);
1223 ASSERT_TRUE(PathExists(dir_name_from
));
1225 // Create a file under the directory.
1226 FilePath file_name_from
=
1227 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1228 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1229 ASSERT_TRUE(PathExists(file_name_from
));
1231 // Create a subdirectory.
1232 FilePath subdir_name_from
=
1233 dir_name_from
.Append(FILE_PATH_LITERAL("Subdir"));
1234 CreateDirectory(subdir_name_from
);
1235 ASSERT_TRUE(PathExists(subdir_name_from
));
1237 // Create a file under the subdirectory.
1238 FilePath file_name2_from
=
1239 subdir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1240 CreateTextFile(file_name2_from
, L
"Gooooooooooooooooooooogle");
1241 ASSERT_TRUE(PathExists(file_name2_from
));
1243 // Copy the directory not recursively.
1244 FilePath dir_name_to
=
1245 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1246 FilePath file_name_to
=
1247 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1248 FilePath subdir_name_to
=
1249 dir_name_to
.Append(FILE_PATH_LITERAL("Subdir"));
1251 // Create the destination directory.
1252 CreateDirectory(dir_name_to
);
1253 ASSERT_TRUE(PathExists(dir_name_to
));
1255 EXPECT_TRUE(CopyDirectory(dir_name_from
, dir_name_to
, false));
1257 // Check everything has been copied.
1258 EXPECT_TRUE(PathExists(dir_name_from
));
1259 EXPECT_TRUE(PathExists(file_name_from
));
1260 EXPECT_TRUE(PathExists(subdir_name_from
));
1261 EXPECT_TRUE(PathExists(file_name2_from
));
1262 EXPECT_TRUE(PathExists(dir_name_to
));
1263 EXPECT_TRUE(PathExists(file_name_to
));
1264 EXPECT_FALSE(PathExists(subdir_name_to
));
1267 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToNew
) {
1269 FilePath file_name_from
=
1270 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1271 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1272 ASSERT_TRUE(PathExists(file_name_from
));
1274 // The destination name
1275 FilePath file_name_to
= temp_dir_
.path().Append(
1276 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1277 ASSERT_FALSE(PathExists(file_name_to
));
1279 EXPECT_TRUE(CopyDirectory(file_name_from
, file_name_to
, true));
1281 // Check the has been copied
1282 EXPECT_TRUE(PathExists(file_name_to
));
1285 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExisting
) {
1287 FilePath file_name_from
=
1288 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1289 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1290 ASSERT_TRUE(PathExists(file_name_from
));
1292 // The destination name
1293 FilePath file_name_to
= temp_dir_
.path().Append(
1294 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1295 CreateTextFile(file_name_to
, L
"Old file content");
1296 ASSERT_TRUE(PathExists(file_name_to
));
1298 EXPECT_TRUE(CopyDirectory(file_name_from
, file_name_to
, true));
1300 // Check the has been copied
1301 EXPECT_TRUE(PathExists(file_name_to
));
1302 EXPECT_TRUE(L
"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to
));
1305 TEST_F(FileUtilTest
, CopyFileWithCopyDirectoryRecursiveToExistingDirectory
) {
1307 FilePath file_name_from
=
1308 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1309 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1310 ASSERT_TRUE(PathExists(file_name_from
));
1313 FilePath dir_name_to
=
1314 temp_dir_
.path().Append(FILE_PATH_LITERAL("Destination"));
1315 CreateDirectory(dir_name_to
);
1316 ASSERT_TRUE(PathExists(dir_name_to
));
1317 FilePath file_name_to
=
1318 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1320 EXPECT_TRUE(CopyDirectory(file_name_from
, dir_name_to
, true));
1322 // Check the has been copied
1323 EXPECT_TRUE(PathExists(file_name_to
));
1326 TEST_F(FileUtilTest
, CopyDirectoryWithTrailingSeparators
) {
1327 // Create a directory.
1328 FilePath dir_name_from
=
1329 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1330 CreateDirectory(dir_name_from
);
1331 ASSERT_TRUE(PathExists(dir_name_from
));
1333 // Create a file under the directory.
1334 FilePath file_name_from
=
1335 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1336 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1337 ASSERT_TRUE(PathExists(file_name_from
));
1339 // Copy the directory recursively.
1340 FilePath dir_name_to
=
1341 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1342 FilePath file_name_to
=
1343 dir_name_to
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1345 // Create from path with trailing separators.
1347 FilePath from_path
=
1348 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1349 #elif defined (OS_POSIX)
1350 FilePath from_path
=
1351 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1354 EXPECT_TRUE(CopyDirectory(from_path
, dir_name_to
, true));
1356 // Check everything has been copied.
1357 EXPECT_TRUE(PathExists(dir_name_from
));
1358 EXPECT_TRUE(PathExists(file_name_from
));
1359 EXPECT_TRUE(PathExists(dir_name_to
));
1360 EXPECT_TRUE(PathExists(file_name_to
));
1363 // Sets the source file to read-only.
1364 void SetReadOnly(const FilePath
& path
) {
1366 // On Windows, it involves setting a bit.
1367 DWORD attrs
= GetFileAttributes(path
.value().c_str());
1368 ASSERT_NE(INVALID_FILE_ATTRIBUTES
, attrs
);
1369 ASSERT_TRUE(SetFileAttributes(
1370 path
.value().c_str(), attrs
| FILE_ATTRIBUTE_READONLY
));
1371 attrs
= GetFileAttributes(path
.value().c_str());
1372 // Files in the temporary directory should not be indexed ever. If this
1373 // assumption change, fix this unit test accordingly.
1374 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED doesn't exist on XP.
1375 DWORD expected
= FILE_ATTRIBUTE_ARCHIVE
| FILE_ATTRIBUTE_READONLY
;
1376 if (win::GetVersion() >= win::VERSION_VISTA
)
1377 expected
|= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
1378 ASSERT_EQ(expected
, attrs
);
1380 // On all other platforms, it involves removing the write bit.
1381 EXPECT_TRUE(SetPosixFilePermissions(path
, S_IRUSR
));
1385 bool IsReadOnly(const FilePath
& path
) {
1387 DWORD attrs
= GetFileAttributes(path
.value().c_str());
1388 EXPECT_NE(INVALID_FILE_ATTRIBUTES
, attrs
);
1389 return attrs
& FILE_ATTRIBUTE_READONLY
;
1392 EXPECT_TRUE(GetPosixFilePermissions(path
, &mode
));
1393 return !(mode
& S_IWUSR
);
1397 TEST_F(FileUtilTest
, CopyDirectoryACL
) {
1398 // Create a directory.
1399 FilePath src
= temp_dir_
.path().Append(FILE_PATH_LITERAL("src"));
1400 CreateDirectory(src
);
1401 ASSERT_TRUE(PathExists(src
));
1403 // Create a file under the directory.
1404 FilePath src_file
= src
.Append(FILE_PATH_LITERAL("src.txt"));
1405 CreateTextFile(src_file
, L
"Gooooooooooooooooooooogle");
1406 SetReadOnly(src_file
);
1407 ASSERT_TRUE(IsReadOnly(src_file
));
1409 // Copy the directory recursively.
1410 FilePath dst
= temp_dir_
.path().Append(FILE_PATH_LITERAL("dst"));
1411 FilePath dst_file
= dst
.Append(FILE_PATH_LITERAL("src.txt"));
1412 EXPECT_TRUE(CopyDirectory(src
, dst
, true));
1414 ASSERT_FALSE(IsReadOnly(dst_file
));
1417 TEST_F(FileUtilTest
, CopyFile
) {
1418 // Create a directory
1419 FilePath dir_name_from
=
1420 temp_dir_
.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1421 CreateDirectory(dir_name_from
);
1422 ASSERT_TRUE(PathExists(dir_name_from
));
1424 // Create a file under the directory
1425 FilePath file_name_from
=
1426 dir_name_from
.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1427 const std::wstring
file_contents(L
"Gooooooooooooooooooooogle");
1428 CreateTextFile(file_name_from
, file_contents
);
1429 ASSERT_TRUE(PathExists(file_name_from
));
1432 FilePath dest_file
= dir_name_from
.Append(FILE_PATH_LITERAL("DestFile.txt"));
1433 ASSERT_TRUE(CopyFile(file_name_from
, dest_file
));
1435 // Copy the file to another location using '..' in the path.
1436 FilePath
dest_file2(dir_name_from
);
1437 dest_file2
= dest_file2
.AppendASCII("..");
1438 dest_file2
= dest_file2
.AppendASCII("DestFile.txt");
1439 ASSERT_FALSE(CopyFile(file_name_from
, dest_file2
));
1440 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from
, dest_file2
));
1442 FilePath
dest_file2_test(dir_name_from
);
1443 dest_file2_test
= dest_file2_test
.DirName();
1444 dest_file2_test
= dest_file2_test
.AppendASCII("DestFile.txt");
1446 // Check everything has been copied.
1447 EXPECT_TRUE(PathExists(file_name_from
));
1448 EXPECT_TRUE(PathExists(dest_file
));
1449 const std::wstring read_contents
= ReadTextFile(dest_file
);
1450 EXPECT_EQ(file_contents
, read_contents
);
1451 EXPECT_TRUE(PathExists(dest_file2_test
));
1452 EXPECT_TRUE(PathExists(dest_file2
));
1455 TEST_F(FileUtilTest
, CopyFileACL
) {
1456 // While FileUtilTest.CopyFile asserts the content is correctly copied over,
1457 // this test case asserts the access control bits are meeting expectations in
1458 // CopyFileUnsafe().
1459 FilePath src
= temp_dir_
.path().Append(FILE_PATH_LITERAL("src.txt"));
1460 const std::wstring
file_contents(L
"Gooooooooooooooooooooogle");
1461 CreateTextFile(src
, file_contents
);
1463 // Set the source file to read-only.
1464 ASSERT_FALSE(IsReadOnly(src
));
1466 ASSERT_TRUE(IsReadOnly(src
));
1469 FilePath dst
= temp_dir_
.path().Append(FILE_PATH_LITERAL("dst.txt"));
1470 ASSERT_TRUE(CopyFile(src
, dst
));
1471 EXPECT_EQ(file_contents
, ReadTextFile(dst
));
1473 ASSERT_FALSE(IsReadOnly(dst
));
1476 // file_util winds up using autoreleased objects on the Mac, so this needs
1477 // to be a PlatformTest.
1478 typedef PlatformTest ReadOnlyFileUtilTest
;
1480 TEST_F(ReadOnlyFileUtilTest
, ContentsEqual
) {
1482 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &data_dir
));
1483 data_dir
= data_dir
.AppendASCII("file_util");
1484 ASSERT_TRUE(PathExists(data_dir
));
1486 FilePath original_file
=
1487 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1488 FilePath same_file
=
1489 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1490 FilePath same_length_file
=
1491 data_dir
.Append(FILE_PATH_LITERAL("same_length.txt"));
1492 FilePath different_file
=
1493 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1494 FilePath different_first_file
=
1495 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1496 FilePath different_last_file
=
1497 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1498 FilePath empty1_file
=
1499 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1500 FilePath empty2_file
=
1501 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1502 FilePath shortened_file
=
1503 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1504 FilePath binary_file
=
1505 data_dir
.Append(FILE_PATH_LITERAL("binary_file.bin"));
1506 FilePath binary_file_same
=
1507 data_dir
.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
1508 FilePath binary_file_diff
=
1509 data_dir
.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
1511 EXPECT_TRUE(ContentsEqual(original_file
, original_file
));
1512 EXPECT_TRUE(ContentsEqual(original_file
, same_file
));
1513 EXPECT_FALSE(ContentsEqual(original_file
, same_length_file
));
1514 EXPECT_FALSE(ContentsEqual(original_file
, different_file
));
1515 EXPECT_FALSE(ContentsEqual(FilePath(FILE_PATH_LITERAL("bogusname")),
1516 FilePath(FILE_PATH_LITERAL("bogusname"))));
1517 EXPECT_FALSE(ContentsEqual(original_file
, different_first_file
));
1518 EXPECT_FALSE(ContentsEqual(original_file
, different_last_file
));
1519 EXPECT_TRUE(ContentsEqual(empty1_file
, empty2_file
));
1520 EXPECT_FALSE(ContentsEqual(original_file
, shortened_file
));
1521 EXPECT_FALSE(ContentsEqual(shortened_file
, original_file
));
1522 EXPECT_TRUE(ContentsEqual(binary_file
, binary_file_same
));
1523 EXPECT_FALSE(ContentsEqual(binary_file
, binary_file_diff
));
1526 TEST_F(ReadOnlyFileUtilTest
, TextContentsEqual
) {
1528 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &data_dir
));
1529 data_dir
= data_dir
.AppendASCII("file_util");
1530 ASSERT_TRUE(PathExists(data_dir
));
1532 FilePath original_file
=
1533 data_dir
.Append(FILE_PATH_LITERAL("original.txt"));
1534 FilePath same_file
=
1535 data_dir
.Append(FILE_PATH_LITERAL("same.txt"));
1536 FilePath crlf_file
=
1537 data_dir
.Append(FILE_PATH_LITERAL("crlf.txt"));
1538 FilePath shortened_file
=
1539 data_dir
.Append(FILE_PATH_LITERAL("shortened.txt"));
1540 FilePath different_file
=
1541 data_dir
.Append(FILE_PATH_LITERAL("different.txt"));
1542 FilePath different_first_file
=
1543 data_dir
.Append(FILE_PATH_LITERAL("different_first.txt"));
1544 FilePath different_last_file
=
1545 data_dir
.Append(FILE_PATH_LITERAL("different_last.txt"));
1546 FilePath first1_file
=
1547 data_dir
.Append(FILE_PATH_LITERAL("first1.txt"));
1548 FilePath first2_file
=
1549 data_dir
.Append(FILE_PATH_LITERAL("first2.txt"));
1550 FilePath empty1_file
=
1551 data_dir
.Append(FILE_PATH_LITERAL("empty1.txt"));
1552 FilePath empty2_file
=
1553 data_dir
.Append(FILE_PATH_LITERAL("empty2.txt"));
1554 FilePath blank_line_file
=
1555 data_dir
.Append(FILE_PATH_LITERAL("blank_line.txt"));
1556 FilePath blank_line_crlf_file
=
1557 data_dir
.Append(FILE_PATH_LITERAL("blank_line_crlf.txt"));
1559 EXPECT_TRUE(TextContentsEqual(original_file
, same_file
));
1560 EXPECT_TRUE(TextContentsEqual(original_file
, crlf_file
));
1561 EXPECT_FALSE(TextContentsEqual(original_file
, shortened_file
));
1562 EXPECT_FALSE(TextContentsEqual(original_file
, different_file
));
1563 EXPECT_FALSE(TextContentsEqual(original_file
, different_first_file
));
1564 EXPECT_FALSE(TextContentsEqual(original_file
, different_last_file
));
1565 EXPECT_FALSE(TextContentsEqual(first1_file
, first2_file
));
1566 EXPECT_TRUE(TextContentsEqual(empty1_file
, empty2_file
));
1567 EXPECT_FALSE(TextContentsEqual(original_file
, empty1_file
));
1568 EXPECT_TRUE(TextContentsEqual(blank_line_file
, blank_line_crlf_file
));
1571 // We don't need equivalent functionality outside of Windows.
1573 TEST_F(FileUtilTest
, CopyAndDeleteDirectoryTest
) {
1574 // Create a directory
1575 FilePath dir_name_from
=
1576 temp_dir_
.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1577 CreateDirectory(dir_name_from
);
1578 ASSERT_TRUE(PathExists(dir_name_from
));
1580 // Create a file under the directory
1581 FilePath file_name_from
=
1582 dir_name_from
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1583 CreateTextFile(file_name_from
, L
"Gooooooooooooooooooooogle");
1584 ASSERT_TRUE(PathExists(file_name_from
));
1586 // Move the directory by using CopyAndDeleteDirectory
1587 FilePath dir_name_to
= temp_dir_
.path().Append(
1588 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1589 FilePath file_name_to
=
1590 dir_name_to
.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1592 ASSERT_FALSE(PathExists(dir_name_to
));
1594 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from
,
1597 // Check everything has been moved.
1598 EXPECT_FALSE(PathExists(dir_name_from
));
1599 EXPECT_FALSE(PathExists(file_name_from
));
1600 EXPECT_TRUE(PathExists(dir_name_to
));
1601 EXPECT_TRUE(PathExists(file_name_to
));
1604 TEST_F(FileUtilTest
, GetTempDirTest
) {
1605 static const TCHAR
* kTmpKey
= _T("TMP");
1606 static const TCHAR
* kTmpValues
[] = {
1607 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1609 // Save the original $TMP.
1610 size_t original_tmp_size
;
1611 TCHAR
* original_tmp
;
1612 ASSERT_EQ(0, ::_tdupenv_s(&original_tmp
, &original_tmp_size
, kTmpKey
));
1613 // original_tmp may be NULL.
1615 for (unsigned int i
= 0; i
< arraysize(kTmpValues
); ++i
) {
1617 ::_tputenv_s(kTmpKey
, kTmpValues
[i
]);
1619 EXPECT_TRUE(path
.IsAbsolute()) << "$TMP=" << kTmpValues
[i
] <<
1620 " result=" << path
.value();
1623 // Restore the original $TMP.
1625 ::_tputenv_s(kTmpKey
, original_tmp
);
1628 ::_tputenv_s(kTmpKey
, _T(""));
1633 TEST_F(FileUtilTest
, CreateTemporaryFileTest
) {
1634 FilePath temp_files
[3];
1635 for (int i
= 0; i
< 3; i
++) {
1636 ASSERT_TRUE(CreateTemporaryFile(&(temp_files
[i
])));
1637 EXPECT_TRUE(PathExists(temp_files
[i
]));
1638 EXPECT_FALSE(DirectoryExists(temp_files
[i
]));
1640 for (int i
= 0; i
< 3; i
++)
1641 EXPECT_FALSE(temp_files
[i
] == temp_files
[(i
+1)%3]);
1642 for (int i
= 0; i
< 3; i
++)
1643 EXPECT_TRUE(DeleteFile(temp_files
[i
], false));
1646 TEST_F(FileUtilTest
, CreateAndOpenTemporaryFileTest
) {
1651 // Create; make sure they are open and exist.
1652 for (i
= 0; i
< 3; ++i
) {
1653 fps
[i
] = CreateAndOpenTemporaryFile(&(names
[i
]));
1654 ASSERT_TRUE(fps
[i
]);
1655 EXPECT_TRUE(PathExists(names
[i
]));
1658 // Make sure all names are unique.
1659 for (i
= 0; i
< 3; ++i
) {
1660 EXPECT_FALSE(names
[i
] == names
[(i
+1)%3]);
1663 // Close and delete.
1664 for (i
= 0; i
< 3; ++i
) {
1665 EXPECT_TRUE(CloseFile(fps
[i
]));
1666 EXPECT_TRUE(DeleteFile(names
[i
], false));
1670 TEST_F(FileUtilTest
, CreateNewTempDirectoryTest
) {
1672 ASSERT_TRUE(CreateNewTempDirectory(FilePath::StringType(), &temp_dir
));
1673 EXPECT_TRUE(PathExists(temp_dir
));
1674 EXPECT_TRUE(DeleteFile(temp_dir
, false));
1677 TEST_F(FileUtilTest
, CreateNewTemporaryDirInDirTest
) {
1679 ASSERT_TRUE(CreateTemporaryDirInDir(
1681 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1683 EXPECT_TRUE(PathExists(new_dir
));
1684 EXPECT_TRUE(temp_dir_
.path().IsParent(new_dir
));
1685 EXPECT_TRUE(DeleteFile(new_dir
, false));
1688 TEST_F(FileUtilTest
, GetShmemTempDirTest
) {
1690 EXPECT_TRUE(GetShmemTempDir(false, &dir
));
1691 EXPECT_TRUE(DirectoryExists(dir
));
1694 TEST_F(FileUtilTest
, GetHomeDirTest
) {
1695 #if !defined(OS_ANDROID) // Not implemented on Android.
1696 // We don't actually know what the home directory is supposed to be without
1697 // calling some OS functions which would just duplicate the implementation.
1698 // So here we just test that it returns something "reasonable".
1699 FilePath home
= GetHomeDir();
1700 ASSERT_FALSE(home
.empty());
1701 ASSERT_TRUE(home
.IsAbsolute());
1705 TEST_F(FileUtilTest
, CreateDirectoryTest
) {
1706 FilePath test_root
=
1707 temp_dir_
.path().Append(FILE_PATH_LITERAL("create_directory_test"));
1709 FilePath test_path
=
1710 test_root
.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
1711 #elif defined(OS_POSIX)
1712 FilePath test_path
=
1713 test_root
.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
1716 EXPECT_FALSE(PathExists(test_path
));
1717 EXPECT_TRUE(CreateDirectory(test_path
));
1718 EXPECT_TRUE(PathExists(test_path
));
1719 // CreateDirectory returns true if the DirectoryExists returns true.
1720 EXPECT_TRUE(CreateDirectory(test_path
));
1722 // Doesn't work to create it on top of a non-dir
1723 test_path
= test_path
.Append(FILE_PATH_LITERAL("foobar.txt"));
1724 EXPECT_FALSE(PathExists(test_path
));
1725 CreateTextFile(test_path
, L
"test file");
1726 EXPECT_TRUE(PathExists(test_path
));
1727 EXPECT_FALSE(CreateDirectory(test_path
));
1729 EXPECT_TRUE(DeleteFile(test_root
, true));
1730 EXPECT_FALSE(PathExists(test_root
));
1731 EXPECT_FALSE(PathExists(test_path
));
1733 // Verify assumptions made by the Windows implementation:
1734 // 1. The current directory always exists.
1735 // 2. The root directory always exists.
1736 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory
)));
1737 FilePath top_level
= test_root
;
1738 while (top_level
!= top_level
.DirName()) {
1739 top_level
= top_level
.DirName();
1741 ASSERT_TRUE(DirectoryExists(top_level
));
1743 // Given these assumptions hold, it should be safe to
1744 // test that "creating" these directories succeeds.
1745 EXPECT_TRUE(CreateDirectory(
1746 FilePath(FilePath::kCurrentDirectory
)));
1747 EXPECT_TRUE(CreateDirectory(top_level
));
1750 FilePath
invalid_drive(FILE_PATH_LITERAL("o:\\"));
1751 FilePath invalid_path
=
1752 invalid_drive
.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1753 if (!PathExists(invalid_drive
)) {
1754 EXPECT_FALSE(CreateDirectory(invalid_path
));
1759 TEST_F(FileUtilTest
, DetectDirectoryTest
) {
1760 // Check a directory
1761 FilePath test_root
=
1762 temp_dir_
.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
1763 EXPECT_FALSE(PathExists(test_root
));
1764 EXPECT_TRUE(CreateDirectory(test_root
));
1765 EXPECT_TRUE(PathExists(test_root
));
1766 EXPECT_TRUE(DirectoryExists(test_root
));
1768 FilePath test_path
=
1769 test_root
.Append(FILE_PATH_LITERAL("foobar.txt"));
1770 EXPECT_FALSE(PathExists(test_path
));
1771 CreateTextFile(test_path
, L
"test file");
1772 EXPECT_TRUE(PathExists(test_path
));
1773 EXPECT_FALSE(DirectoryExists(test_path
));
1774 EXPECT_TRUE(DeleteFile(test_path
, false));
1776 EXPECT_TRUE(DeleteFile(test_root
, true));
1779 TEST_F(FileUtilTest
, FileEnumeratorTest
) {
1780 // Test an empty directory.
1781 FileEnumerator
f0(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1782 EXPECT_EQ(f0
.Next().value(), FPL(""));
1783 EXPECT_EQ(f0
.Next().value(), FPL(""));
1785 // Test an empty directory, non-recursively, including "..".
1786 FileEnumerator
f0_dotdot(temp_dir_
.path(), false,
1787 FILES_AND_DIRECTORIES
| FileEnumerator::INCLUDE_DOT_DOT
);
1788 EXPECT_EQ(temp_dir_
.path().Append(FPL("..")).value(),
1789 f0_dotdot
.Next().value());
1790 EXPECT_EQ(FPL(""), f0_dotdot
.Next().value());
1792 // create the directories
1793 FilePath dir1
= temp_dir_
.path().Append(FPL("dir1"));
1794 EXPECT_TRUE(CreateDirectory(dir1
));
1795 FilePath dir2
= temp_dir_
.path().Append(FPL("dir2"));
1796 EXPECT_TRUE(CreateDirectory(dir2
));
1797 FilePath dir2inner
= dir2
.Append(FPL("inner"));
1798 EXPECT_TRUE(CreateDirectory(dir2inner
));
1801 FilePath dir2file
= dir2
.Append(FPL("dir2file.txt"));
1802 CreateTextFile(dir2file
, std::wstring());
1803 FilePath dir2innerfile
= dir2inner
.Append(FPL("innerfile.txt"));
1804 CreateTextFile(dir2innerfile
, std::wstring());
1805 FilePath file1
= temp_dir_
.path().Append(FPL("file1.txt"));
1806 CreateTextFile(file1
, std::wstring());
1807 FilePath file2_rel
= dir2
.Append(FilePath::kParentDirectory
)
1808 .Append(FPL("file2.txt"));
1809 CreateTextFile(file2_rel
, std::wstring());
1810 FilePath file2_abs
= temp_dir_
.path().Append(FPL("file2.txt"));
1812 // Only enumerate files.
1813 FileEnumerator
f1(temp_dir_
.path(), true, FileEnumerator::FILES
);
1814 FindResultCollector
c1(f1
);
1815 EXPECT_TRUE(c1
.HasFile(file1
));
1816 EXPECT_TRUE(c1
.HasFile(file2_abs
));
1817 EXPECT_TRUE(c1
.HasFile(dir2file
));
1818 EXPECT_TRUE(c1
.HasFile(dir2innerfile
));
1819 EXPECT_EQ(c1
.size(), 4);
1821 // Only enumerate directories.
1822 FileEnumerator
f2(temp_dir_
.path(), true, FileEnumerator::DIRECTORIES
);
1823 FindResultCollector
c2(f2
);
1824 EXPECT_TRUE(c2
.HasFile(dir1
));
1825 EXPECT_TRUE(c2
.HasFile(dir2
));
1826 EXPECT_TRUE(c2
.HasFile(dir2inner
));
1827 EXPECT_EQ(c2
.size(), 3);
1829 // Only enumerate directories non-recursively.
1830 FileEnumerator
f2_non_recursive(
1831 temp_dir_
.path(), false, FileEnumerator::DIRECTORIES
);
1832 FindResultCollector
c2_non_recursive(f2_non_recursive
);
1833 EXPECT_TRUE(c2_non_recursive
.HasFile(dir1
));
1834 EXPECT_TRUE(c2_non_recursive
.HasFile(dir2
));
1835 EXPECT_EQ(c2_non_recursive
.size(), 2);
1837 // Only enumerate directories, non-recursively, including "..".
1838 FileEnumerator
f2_dotdot(temp_dir_
.path(), false,
1839 FileEnumerator::DIRECTORIES
|
1840 FileEnumerator::INCLUDE_DOT_DOT
);
1841 FindResultCollector
c2_dotdot(f2_dotdot
);
1842 EXPECT_TRUE(c2_dotdot
.HasFile(dir1
));
1843 EXPECT_TRUE(c2_dotdot
.HasFile(dir2
));
1844 EXPECT_TRUE(c2_dotdot
.HasFile(temp_dir_
.path().Append(FPL(".."))));
1845 EXPECT_EQ(c2_dotdot
.size(), 3);
1847 // Enumerate files and directories.
1848 FileEnumerator
f3(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1849 FindResultCollector
c3(f3
);
1850 EXPECT_TRUE(c3
.HasFile(dir1
));
1851 EXPECT_TRUE(c3
.HasFile(dir2
));
1852 EXPECT_TRUE(c3
.HasFile(file1
));
1853 EXPECT_TRUE(c3
.HasFile(file2_abs
));
1854 EXPECT_TRUE(c3
.HasFile(dir2file
));
1855 EXPECT_TRUE(c3
.HasFile(dir2inner
));
1856 EXPECT_TRUE(c3
.HasFile(dir2innerfile
));
1857 EXPECT_EQ(c3
.size(), 7);
1859 // Non-recursive operation.
1860 FileEnumerator
f4(temp_dir_
.path(), false, FILES_AND_DIRECTORIES
);
1861 FindResultCollector
c4(f4
);
1862 EXPECT_TRUE(c4
.HasFile(dir2
));
1863 EXPECT_TRUE(c4
.HasFile(dir2
));
1864 EXPECT_TRUE(c4
.HasFile(file1
));
1865 EXPECT_TRUE(c4
.HasFile(file2_abs
));
1866 EXPECT_EQ(c4
.size(), 4);
1868 // Enumerate with a pattern.
1869 FileEnumerator
f5(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
, FPL("dir*"));
1870 FindResultCollector
c5(f5
);
1871 EXPECT_TRUE(c5
.HasFile(dir1
));
1872 EXPECT_TRUE(c5
.HasFile(dir2
));
1873 EXPECT_TRUE(c5
.HasFile(dir2file
));
1874 EXPECT_TRUE(c5
.HasFile(dir2inner
));
1875 EXPECT_TRUE(c5
.HasFile(dir2innerfile
));
1876 EXPECT_EQ(c5
.size(), 5);
1880 // Make dir1 point to dir2.
1881 ReparsePoint
reparse_point(dir1
, dir2
);
1882 EXPECT_TRUE(reparse_point
.IsValid());
1884 if ((win::GetVersion() >= win::VERSION_VISTA
)) {
1885 // There can be a delay for the enumeration code to see the change on
1886 // the file system so skip this test for XP.
1887 // Enumerate the reparse point.
1888 FileEnumerator
f6(dir1
, true, FILES_AND_DIRECTORIES
);
1889 FindResultCollector
c6(f6
);
1890 FilePath inner2
= dir1
.Append(FPL("inner"));
1891 EXPECT_TRUE(c6
.HasFile(inner2
));
1892 EXPECT_TRUE(c6
.HasFile(inner2
.Append(FPL("innerfile.txt"))));
1893 EXPECT_TRUE(c6
.HasFile(dir1
.Append(FPL("dir2file.txt"))));
1894 EXPECT_EQ(c6
.size(), 3);
1897 // No changes for non recursive operation.
1898 FileEnumerator
f7(temp_dir_
.path(), false, FILES_AND_DIRECTORIES
);
1899 FindResultCollector
c7(f7
);
1900 EXPECT_TRUE(c7
.HasFile(dir2
));
1901 EXPECT_TRUE(c7
.HasFile(dir2
));
1902 EXPECT_TRUE(c7
.HasFile(file1
));
1903 EXPECT_TRUE(c7
.HasFile(file2_abs
));
1904 EXPECT_EQ(c7
.size(), 4);
1906 // Should not enumerate inside dir1 when using recursion.
1907 FileEnumerator
f8(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1908 FindResultCollector
c8(f8
);
1909 EXPECT_TRUE(c8
.HasFile(dir1
));
1910 EXPECT_TRUE(c8
.HasFile(dir2
));
1911 EXPECT_TRUE(c8
.HasFile(file1
));
1912 EXPECT_TRUE(c8
.HasFile(file2_abs
));
1913 EXPECT_TRUE(c8
.HasFile(dir2file
));
1914 EXPECT_TRUE(c8
.HasFile(dir2inner
));
1915 EXPECT_TRUE(c8
.HasFile(dir2innerfile
));
1916 EXPECT_EQ(c8
.size(), 7);
1920 // Make sure the destructor closes the find handle while in the middle of a
1921 // query to allow TearDown to delete the directory.
1922 FileEnumerator
f9(temp_dir_
.path(), true, FILES_AND_DIRECTORIES
);
1923 EXPECT_FALSE(f9
.Next().value().empty()); // Should have found something
1924 // (we don't care what).
1927 TEST_F(FileUtilTest
, AppendToFile
) {
1929 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1931 // Create a fresh, empty copy of this directory.
1932 if (PathExists(data_dir
)) {
1933 ASSERT_TRUE(DeleteFile(data_dir
, true));
1935 ASSERT_TRUE(CreateDirectory(data_dir
));
1937 // Create a fresh, empty copy of this directory.
1938 if (PathExists(data_dir
)) {
1939 ASSERT_TRUE(DeleteFile(data_dir
, true));
1941 ASSERT_TRUE(CreateDirectory(data_dir
));
1942 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
1944 std::string
data("hello");
1945 EXPECT_EQ(-1, file_util::AppendToFile(foobar
, data
.c_str(), data
.length()));
1946 EXPECT_EQ(static_cast<int>(data
.length()),
1947 file_util::WriteFile(foobar
, data
.c_str(), data
.length()));
1948 EXPECT_EQ(static_cast<int>(data
.length()),
1949 file_util::AppendToFile(foobar
, data
.c_str(), data
.length()));
1951 const std::wstring read_content
= ReadTextFile(foobar
);
1952 EXPECT_EQ(L
"hellohello", read_content
);
1955 TEST_F(FileUtilTest
, ReadFileToString
) {
1956 const char kTestData
[] = "0123";
1959 FilePath file_path
=
1960 temp_dir_
.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));
1962 ASSERT_EQ(4, file_util::WriteFile(file_path
, kTestData
, 4));
1964 EXPECT_TRUE(ReadFileToString(file_path
, &data
));
1965 EXPECT_EQ(kTestData
, data
);
1968 EXPECT_FALSE(ReadFileToString(file_path
, &data
, 0));
1969 EXPECT_EQ(data
.length(), 0u);
1972 EXPECT_FALSE(ReadFileToString(file_path
, &data
, 2));
1973 EXPECT_EQ("01", data
);
1976 EXPECT_FALSE(ReadFileToString(file_path
, &data
, 3));
1977 EXPECT_EQ("012", data
);
1980 EXPECT_TRUE(ReadFileToString(file_path
, &data
, 4));
1981 EXPECT_EQ("0123", data
);
1984 EXPECT_TRUE(ReadFileToString(file_path
, &data
, 6));
1985 EXPECT_EQ("0123", data
);
1987 EXPECT_TRUE(ReadFileToString(file_path
, NULL
, 6));
1989 EXPECT_TRUE(ReadFileToString(file_path
, NULL
));
1991 EXPECT_TRUE(base::DeleteFile(file_path
, false));
1993 EXPECT_FALSE(ReadFileToString(file_path
, &data
));
1994 EXPECT_EQ(data
.length(), 0u);
1996 EXPECT_FALSE(ReadFileToString(file_path
, &data
, 6));
1997 EXPECT_EQ(data
.length(), 0u);
2000 TEST_F(FileUtilTest
, TouchFile
) {
2002 temp_dir_
.path().Append(FILE_PATH_LITERAL("FilePathTest"));
2004 // Create a fresh, empty copy of this directory.
2005 if (PathExists(data_dir
)) {
2006 ASSERT_TRUE(DeleteFile(data_dir
, true));
2008 ASSERT_TRUE(CreateDirectory(data_dir
));
2010 FilePath
foobar(data_dir
.Append(FILE_PATH_LITERAL("foobar.txt")));
2011 std::string
data("hello");
2012 ASSERT_TRUE(file_util::WriteFile(foobar
, data
.c_str(), data
.length()));
2015 // This timestamp is divisible by one day (in local timezone),
2016 // to make it work on FAT too.
2017 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
2020 Time modification_time
;
2021 // Note that this timestamp is divisible by two (seconds) - FAT stores
2022 // modification times with 2s resolution.
2023 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
2024 &modification_time
));
2026 ASSERT_TRUE(TouchFile(foobar
, access_time
, modification_time
));
2027 File::Info file_info
;
2028 ASSERT_TRUE(GetFileInfo(foobar
, &file_info
));
2029 EXPECT_EQ(file_info
.last_accessed
.ToInternalValue(),
2030 access_time
.ToInternalValue());
2031 EXPECT_EQ(file_info
.last_modified
.ToInternalValue(),
2032 modification_time
.ToInternalValue());
2035 TEST_F(FileUtilTest
, IsDirectoryEmpty
) {
2036 FilePath empty_dir
= temp_dir_
.path().Append(FILE_PATH_LITERAL("EmptyDir"));
2038 ASSERT_FALSE(PathExists(empty_dir
));
2040 ASSERT_TRUE(CreateDirectory(empty_dir
));
2042 EXPECT_TRUE(IsDirectoryEmpty(empty_dir
));
2044 FilePath
foo(empty_dir
.Append(FILE_PATH_LITERAL("foo.txt")));
2045 std::string
bar("baz");
2046 ASSERT_TRUE(file_util::WriteFile(foo
, bar
.c_str(), bar
.length()));
2048 EXPECT_FALSE(IsDirectoryEmpty(empty_dir
));
2051 #if defined(OS_POSIX)
2053 // Testing VerifyPathControlledByAdmin() is hard, because there is no
2054 // way a test can make a file owned by root, or change file paths
2055 // at the root of the file system. VerifyPathControlledByAdmin()
2056 // is implemented as a call to VerifyPathControlledByUser, which gives
2057 // us the ability to test with paths under the test's temp directory,
2058 // using a user id we control.
2059 // Pull tests of VerifyPathControlledByUserTest() into a separate test class
2060 // with a common SetUp() method.
2061 class VerifyPathControlledByUserTest
: public FileUtilTest
{
2063 virtual void SetUp() OVERRIDE
{
2064 FileUtilTest::SetUp();
2066 // Create a basic structure used by each test.
2071 base_dir_
= temp_dir_
.path().AppendASCII("base_dir");
2072 ASSERT_TRUE(CreateDirectory(base_dir_
));
2074 sub_dir_
= base_dir_
.AppendASCII("sub_dir");
2075 ASSERT_TRUE(CreateDirectory(sub_dir_
));
2077 text_file_
= sub_dir_
.AppendASCII("file.txt");
2078 CreateTextFile(text_file_
, L
"This text file has some text in it.");
2080 // Get the user and group files are created with from |base_dir_|.
2081 struct stat stat_buf
;
2082 ASSERT_EQ(0, stat(base_dir_
.value().c_str(), &stat_buf
));
2083 uid_
= stat_buf
.st_uid
;
2084 ok_gids_
.insert(stat_buf
.st_gid
);
2085 bad_gids_
.insert(stat_buf
.st_gid
+ 1);
2087 ASSERT_EQ(uid_
, getuid()); // This process should be the owner.
2089 // To ensure that umask settings do not cause the initial state
2090 // of permissions to be different from what we expect, explicitly
2091 // set permissions on the directories we create.
2092 // Make all files and directories non-world-writable.
2094 // Users and group can read, write, traverse
2095 int enabled_permissions
=
2096 FILE_PERMISSION_USER_MASK
| FILE_PERMISSION_GROUP_MASK
;
2097 // Other users can't read, write, traverse
2098 int disabled_permissions
= FILE_PERMISSION_OTHERS_MASK
;
2100 ASSERT_NO_FATAL_FAILURE(
2101 ChangePosixFilePermissions(
2102 base_dir_
, enabled_permissions
, disabled_permissions
));
2103 ASSERT_NO_FATAL_FAILURE(
2104 ChangePosixFilePermissions(
2105 sub_dir_
, enabled_permissions
, disabled_permissions
));
2110 FilePath text_file_
;
2113 std::set
<gid_t
> ok_gids_
;
2114 std::set
<gid_t
> bad_gids_
;
2117 TEST_F(VerifyPathControlledByUserTest
, BadPaths
) {
2118 // File does not exist.
2119 FilePath does_not_exist
= base_dir_
.AppendASCII("does")
2121 .AppendASCII("exist");
2123 file_util::VerifyPathControlledByUser(
2124 base_dir_
, does_not_exist
, uid_
, ok_gids_
));
2126 // |base| not a subpath of |path|.
2128 file_util::VerifyPathControlledByUser(
2129 sub_dir_
, base_dir_
, uid_
, ok_gids_
));
2131 // An empty base path will fail to be a prefix for any path.
2134 file_util::VerifyPathControlledByUser(
2135 empty
, base_dir_
, uid_
, ok_gids_
));
2137 // Finding that a bad call fails proves nothing unless a good call succeeds.
2139 file_util::VerifyPathControlledByUser(
2140 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2143 TEST_F(VerifyPathControlledByUserTest
, Symlinks
) {
2144 // Symlinks in the path should cause failure.
2146 // Symlink to the file at the end of the path.
2147 FilePath file_link
= base_dir_
.AppendASCII("file_link");
2148 ASSERT_TRUE(CreateSymbolicLink(text_file_
, file_link
))
2149 << "Failed to create symlink.";
2152 file_util::VerifyPathControlledByUser(
2153 base_dir_
, file_link
, uid_
, ok_gids_
));
2155 file_util::VerifyPathControlledByUser(
2156 file_link
, file_link
, uid_
, ok_gids_
));
2158 // Symlink from one directory to another within the path.
2159 FilePath link_to_sub_dir
= base_dir_
.AppendASCII("link_to_sub_dir");
2160 ASSERT_TRUE(CreateSymbolicLink(sub_dir_
, link_to_sub_dir
))
2161 << "Failed to create symlink.";
2163 FilePath file_path_with_link
= link_to_sub_dir
.AppendASCII("file.txt");
2164 ASSERT_TRUE(PathExists(file_path_with_link
));
2167 file_util::VerifyPathControlledByUser(
2168 base_dir_
, file_path_with_link
, uid_
, ok_gids_
));
2171 file_util::VerifyPathControlledByUser(
2172 link_to_sub_dir
, file_path_with_link
, uid_
, ok_gids_
));
2174 // Symlinks in parents of base path are allowed.
2176 file_util::VerifyPathControlledByUser(
2177 file_path_with_link
, file_path_with_link
, uid_
, ok_gids_
));
2180 TEST_F(VerifyPathControlledByUserTest
, OwnershipChecks
) {
2181 // Get a uid that is not the uid of files we create.
2182 uid_t bad_uid
= uid_
+ 1;
2184 // Make all files and directories non-world-writable.
2185 ASSERT_NO_FATAL_FAILURE(
2186 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2187 ASSERT_NO_FATAL_FAILURE(
2188 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2189 ASSERT_NO_FATAL_FAILURE(
2190 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2192 // We control these paths.
2194 file_util::VerifyPathControlledByUser(
2195 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2197 file_util::VerifyPathControlledByUser(
2198 base_dir_
, text_file_
, uid_
, ok_gids_
));
2200 file_util::VerifyPathControlledByUser(
2201 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2203 // Another user does not control these paths.
2205 file_util::VerifyPathControlledByUser(
2206 base_dir_
, sub_dir_
, bad_uid
, ok_gids_
));
2208 file_util::VerifyPathControlledByUser(
2209 base_dir_
, text_file_
, bad_uid
, ok_gids_
));
2211 file_util::VerifyPathControlledByUser(
2212 sub_dir_
, text_file_
, bad_uid
, ok_gids_
));
2214 // Another group does not control the paths.
2216 file_util::VerifyPathControlledByUser(
2217 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2219 file_util::VerifyPathControlledByUser(
2220 base_dir_
, text_file_
, uid_
, bad_gids_
));
2222 file_util::VerifyPathControlledByUser(
2223 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2226 TEST_F(VerifyPathControlledByUserTest
, GroupWriteTest
) {
2227 // Make all files and directories writable only by their owner.
2228 ASSERT_NO_FATAL_FAILURE(
2229 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2230 ASSERT_NO_FATAL_FAILURE(
2231 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
|S_IWGRP
));
2232 ASSERT_NO_FATAL_FAILURE(
2233 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
|S_IWGRP
));
2235 // Any group is okay because the path is not group-writable.
2237 file_util::VerifyPathControlledByUser(
2238 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2240 file_util::VerifyPathControlledByUser(
2241 base_dir_
, text_file_
, uid_
, ok_gids_
));
2243 file_util::VerifyPathControlledByUser(
2244 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2247 file_util::VerifyPathControlledByUser(
2248 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2250 file_util::VerifyPathControlledByUser(
2251 base_dir_
, text_file_
, uid_
, bad_gids_
));
2253 file_util::VerifyPathControlledByUser(
2254 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2256 // No group is okay, because we don't check the group
2257 // if no group can write.
2258 std::set
<gid_t
> no_gids
; // Empty set of gids.
2260 file_util::VerifyPathControlledByUser(
2261 base_dir_
, sub_dir_
, uid_
, no_gids
));
2263 file_util::VerifyPathControlledByUser(
2264 base_dir_
, text_file_
, uid_
, no_gids
));
2266 file_util::VerifyPathControlledByUser(
2267 sub_dir_
, text_file_
, uid_
, no_gids
));
2270 // Make all files and directories writable by their group.
2271 ASSERT_NO_FATAL_FAILURE(
2272 ChangePosixFilePermissions(base_dir_
, S_IWGRP
, 0u));
2273 ASSERT_NO_FATAL_FAILURE(
2274 ChangePosixFilePermissions(sub_dir_
, S_IWGRP
, 0u));
2275 ASSERT_NO_FATAL_FAILURE(
2276 ChangePosixFilePermissions(text_file_
, S_IWGRP
, 0u));
2278 // Now |ok_gids_| works, but |bad_gids_| fails.
2280 file_util::VerifyPathControlledByUser(
2281 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2283 file_util::VerifyPathControlledByUser(
2284 base_dir_
, text_file_
, uid_
, ok_gids_
));
2286 file_util::VerifyPathControlledByUser(
2287 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2290 file_util::VerifyPathControlledByUser(
2291 base_dir_
, sub_dir_
, uid_
, bad_gids_
));
2293 file_util::VerifyPathControlledByUser(
2294 base_dir_
, text_file_
, uid_
, bad_gids_
));
2296 file_util::VerifyPathControlledByUser(
2297 sub_dir_
, text_file_
, uid_
, bad_gids_
));
2299 // Because any group in the group set is allowed,
2300 // the union of good and bad gids passes.
2302 std::set
<gid_t
> multiple_gids
;
2304 ok_gids_
.begin(), ok_gids_
.end(),
2305 bad_gids_
.begin(), bad_gids_
.end(),
2306 std::inserter(multiple_gids
, multiple_gids
.begin()));
2309 file_util::VerifyPathControlledByUser(
2310 base_dir_
, sub_dir_
, uid_
, multiple_gids
));
2312 file_util::VerifyPathControlledByUser(
2313 base_dir_
, text_file_
, uid_
, multiple_gids
));
2315 file_util::VerifyPathControlledByUser(
2316 sub_dir_
, text_file_
, uid_
, multiple_gids
));
2319 TEST_F(VerifyPathControlledByUserTest
, WriteBitChecks
) {
2320 // Make all files and directories non-world-writable.
2321 ASSERT_NO_FATAL_FAILURE(
2322 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2323 ASSERT_NO_FATAL_FAILURE(
2324 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2325 ASSERT_NO_FATAL_FAILURE(
2326 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2328 // Initialy, we control all parts of the path.
2330 file_util::VerifyPathControlledByUser(
2331 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2333 file_util::VerifyPathControlledByUser(
2334 base_dir_
, text_file_
, uid_
, ok_gids_
));
2336 file_util::VerifyPathControlledByUser(
2337 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2339 // Make base_dir_ world-writable.
2340 ASSERT_NO_FATAL_FAILURE(
2341 ChangePosixFilePermissions(base_dir_
, S_IWOTH
, 0u));
2343 file_util::VerifyPathControlledByUser(
2344 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2346 file_util::VerifyPathControlledByUser(
2347 base_dir_
, text_file_
, uid_
, ok_gids_
));
2349 file_util::VerifyPathControlledByUser(
2350 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2352 // Make sub_dir_ world writable.
2353 ASSERT_NO_FATAL_FAILURE(
2354 ChangePosixFilePermissions(sub_dir_
, S_IWOTH
, 0u));
2356 file_util::VerifyPathControlledByUser(
2357 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2359 file_util::VerifyPathControlledByUser(
2360 base_dir_
, text_file_
, uid_
, ok_gids_
));
2362 file_util::VerifyPathControlledByUser(
2363 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2365 // Make text_file_ world writable.
2366 ASSERT_NO_FATAL_FAILURE(
2367 ChangePosixFilePermissions(text_file_
, S_IWOTH
, 0u));
2369 file_util::VerifyPathControlledByUser(
2370 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2372 file_util::VerifyPathControlledByUser(
2373 base_dir_
, text_file_
, uid_
, ok_gids_
));
2375 file_util::VerifyPathControlledByUser(
2376 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2378 // Make sub_dir_ non-world writable.
2379 ASSERT_NO_FATAL_FAILURE(
2380 ChangePosixFilePermissions(sub_dir_
, 0u, S_IWOTH
));
2382 file_util::VerifyPathControlledByUser(
2383 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2385 file_util::VerifyPathControlledByUser(
2386 base_dir_
, text_file_
, uid_
, ok_gids_
));
2388 file_util::VerifyPathControlledByUser(
2389 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2391 // Make base_dir_ non-world-writable.
2392 ASSERT_NO_FATAL_FAILURE(
2393 ChangePosixFilePermissions(base_dir_
, 0u, S_IWOTH
));
2395 file_util::VerifyPathControlledByUser(
2396 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2398 file_util::VerifyPathControlledByUser(
2399 base_dir_
, text_file_
, uid_
, ok_gids_
));
2401 file_util::VerifyPathControlledByUser(
2402 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2404 // Back to the initial state: Nothing is writable, so every path
2406 ASSERT_NO_FATAL_FAILURE(
2407 ChangePosixFilePermissions(text_file_
, 0u, S_IWOTH
));
2409 file_util::VerifyPathControlledByUser(
2410 base_dir_
, sub_dir_
, uid_
, ok_gids_
));
2412 file_util::VerifyPathControlledByUser(
2413 base_dir_
, text_file_
, uid_
, ok_gids_
));
2415 file_util::VerifyPathControlledByUser(
2416 sub_dir_
, text_file_
, uid_
, ok_gids_
));
2419 #if defined(OS_ANDROID)
2420 TEST_F(FileUtilTest
, ValidContentUriTest
) {
2421 // Get the test image path.
2423 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &data_dir
));
2424 data_dir
= data_dir
.AppendASCII("file_util");
2425 ASSERT_TRUE(PathExists(data_dir
));
2426 FilePath image_file
= data_dir
.Append(FILE_PATH_LITERAL("red.png"));
2428 GetFileSize(image_file
, &image_size
);
2429 EXPECT_LT(0, image_size
);
2431 // Insert the image into MediaStore. MediaStore will do some conversions, and
2432 // return the content URI.
2433 FilePath path
= file_util::InsertImageIntoMediaStore(image_file
);
2434 EXPECT_TRUE(path
.IsContentUri());
2435 EXPECT_TRUE(PathExists(path
));
2436 // The file size may not equal to the input image as MediaStore may convert
2438 int64 content_uri_size
;
2439 GetFileSize(path
, &content_uri_size
);
2440 EXPECT_EQ(image_size
, content_uri_size
);
2442 // We should be able to read the file.
2443 char* buffer
= new char[image_size
];
2444 int fd
= OpenContentUriForRead(path
);
2446 EXPECT_TRUE(ReadFromFD(fd
, buffer
, image_size
));
2450 TEST_F(FileUtilTest
, NonExistentContentUriTest
) {
2451 FilePath
path("content://foo.bar");
2452 EXPECT_TRUE(path
.IsContentUri());
2453 EXPECT_FALSE(PathExists(path
));
2454 // Size should be smaller than 0.
2456 EXPECT_FALSE(GetFileSize(path
, &size
));
2458 // We should not be able to read the file.
2459 int fd
= OpenContentUriForRead(path
);
2464 #endif // defined(OS_POSIX)