Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(470)

Side by Side Diff: base/file_util_unittest.cc

Issue 89523002: Move Posix file utils to the base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util_posix.cc ('k') | base/files/file_path_watcher_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 22 matching lines...) Expand all
33 #include "base/win/windows_version.h" 33 #include "base/win/windows_version.h"
34 #endif 34 #endif
35 35
36 #if defined(OS_ANDROID) 36 #if defined(OS_ANDROID)
37 #include "base/android/content_uri_utils.h" 37 #include "base/android/content_uri_utils.h"
38 #endif 38 #endif
39 39
40 // This macro helps avoid wrapped lines in the test structs. 40 // This macro helps avoid wrapped lines in the test structs.
41 #define FPL(x) FILE_PATH_LITERAL(x) 41 #define FPL(x) FILE_PATH_LITERAL(x)
42 42
43 using base::DirectoryExists; 43 namespace base {
44 using base::FileEnumerator;
45 using base::FilePath;
46 using base::PathIsWritable;
47 using base::TextContentsEqual;
48 44
49 namespace { 45 namespace {
50 46
51 // To test that file_util::Normalize FilePath() deals with NTFS reparse points 47 // To test that file_util::Normalize FilePath() deals with NTFS reparse points
52 // correctly, we need functions to create and delete reparse points. 48 // correctly, we need functions to create and delete reparse points.
53 #if defined(OS_WIN) 49 #if defined(OS_WIN)
54 typedef struct _REPARSE_DATA_BUFFER { 50 typedef struct _REPARSE_DATA_BUFFER {
55 ULONG ReparseTag; 51 ULONG ReparseTag;
56 USHORT ReparseDataLength; 52 USHORT ReparseDataLength;
57 USHORT Reserved; 53 USHORT Reserved;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 } 135 }
140 136
141 ~ReparsePoint() { 137 ~ReparsePoint() {
142 if (created_) 138 if (created_)
143 DeleteReparsePoint(dir_); 139 DeleteReparsePoint(dir_);
144 } 140 }
145 141
146 bool IsValid() { return created_; } 142 bool IsValid() { return created_; }
147 143
148 private: 144 private:
149 base::win::ScopedHandle dir_; 145 win::ScopedHandle dir_;
150 bool created_; 146 bool created_;
151 DISALLOW_COPY_AND_ASSIGN(ReparsePoint); 147 DISALLOW_COPY_AND_ASSIGN(ReparsePoint);
152 }; 148 };
153 149
154 #endif 150 #endif
155 151
156 #if defined(OS_POSIX) 152 #if defined(OS_POSIX)
157 // Provide a simple way to change the permissions bits on |path| in tests. 153 // Provide a simple way to change the permissions bits on |path| in tests.
158 // ASSERT failures will return, but not stop the test. Caller should wrap 154 // ASSERT failures will return, but not stop the test. Caller should wrap
159 // calls to this function in ASSERT_NO_FATAL_FAILURE(). 155 // calls to this function in ASSERT_NO_FATAL_FAILURE().
160 void ChangePosixFilePermissions(const FilePath& path, 156 void ChangePosixFilePermissions(const FilePath& path,
161 int mode_bits_to_set, 157 int mode_bits_to_set,
162 int mode_bits_to_clear) { 158 int mode_bits_to_clear) {
163 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear) 159 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear)
164 << "Can't set and clear the same bits."; 160 << "Can't set and clear the same bits.";
165 161
166 int mode = 0; 162 int mode = 0;
167 ASSERT_TRUE(file_util::GetPosixFilePermissions(path, &mode)); 163 ASSERT_TRUE(GetPosixFilePermissions(path, &mode));
168 mode |= mode_bits_to_set; 164 mode |= mode_bits_to_set;
169 mode &= ~mode_bits_to_clear; 165 mode &= ~mode_bits_to_clear;
170 ASSERT_TRUE(file_util::SetPosixFilePermissions(path, mode)); 166 ASSERT_TRUE(SetPosixFilePermissions(path, mode));
171 } 167 }
172 #endif // defined(OS_POSIX) 168 #endif // defined(OS_POSIX)
173 169
174 const wchar_t bogus_content[] = L"I'm cannon fodder."; 170 const wchar_t bogus_content[] = L"I'm cannon fodder.";
175 171
176 const int FILES_AND_DIRECTORIES = 172 const int FILES_AND_DIRECTORIES =
177 FileEnumerator::FILES | FileEnumerator::DIRECTORIES; 173 FileEnumerator::FILES | FileEnumerator::DIRECTORIES;
178 174
179 // file_util winds up using autoreleased objects on the Mac, so this needs 175 // file_util winds up using autoreleased objects on the Mac, so this needs
180 // to be a PlatformTest 176 // to be a PlatformTest
181 class FileUtilTest : public PlatformTest { 177 class FileUtilTest : public PlatformTest {
182 protected: 178 protected:
183 virtual void SetUp() OVERRIDE { 179 virtual void SetUp() OVERRIDE {
184 PlatformTest::SetUp(); 180 PlatformTest::SetUp();
185 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 181 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
186 } 182 }
187 183
188 base::ScopedTempDir temp_dir_; 184 ScopedTempDir temp_dir_;
189 }; 185 };
190 186
191 // Collects all the results from the given file enumerator, and provides an 187 // Collects all the results from the given file enumerator, and provides an
192 // interface to query whether a given file is present. 188 // interface to query whether a given file is present.
193 class FindResultCollector { 189 class FindResultCollector {
194 public: 190 public:
195 explicit FindResultCollector(FileEnumerator& enumerator) { 191 explicit FindResultCollector(FileEnumerator& enumerator) {
196 FilePath cur_file; 192 FilePath cur_file;
197 while (!(cur_file = enumerator.Next()).value().empty()) { 193 while (!(cur_file = enumerator.Next()).value().empty()) {
198 FilePath::StringType path = cur_file.value(); 194 FilePath::StringType path = cur_file.value();
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 int64 size_f2 = 0; 261 int64 size_f2 = 0;
266 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2)); 262 ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
267 EXPECT_EQ(30ll, size_f2); 263 EXPECT_EQ(30ll, size_f2);
268 264
269 FilePath subsubdir_path = subdir_path.Append(FPL("Level3")); 265 FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
270 file_util::CreateDirectory(subsubdir_path); 266 file_util::CreateDirectory(subsubdir_path);
271 267
272 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt")); 268 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt"));
273 CreateTextFile(file_03, L"123"); 269 CreateTextFile(file_03, L"123");
274 270
275 int64 computed_size = base::ComputeDirectorySize(temp_dir_.path()); 271 int64 computed_size = ComputeDirectorySize(temp_dir_.path());
276 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size); 272 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size);
277 } 273 }
278 274
279 TEST_F(FileUtilTest, NormalizeFilePathBasic) { 275 TEST_F(FileUtilTest, NormalizeFilePathBasic) {
280 // Create a directory under the test dir. Because we create it, 276 // Create a directory under the test dir. Because we create it,
281 // we know it is not a link. 277 // we know it is not a link.
282 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a")); 278 FilePath file_a_path = temp_dir_.path().Append(FPL("file_a"));
283 FilePath dir_path = temp_dir_.path().Append(FPL("dir")); 279 FilePath dir_path = temp_dir_.path().Append(FPL("dir"));
284 FilePath file_b_path = dir_path.Append(FPL("file_b")); 280 FilePath file_b_path = dir_path.Append(FPL("file_b"));
285 file_util::CreateDirectory(dir_path); 281 file_util::CreateDirectory(dir_path);
286 282
287 FilePath normalized_file_a_path, normalized_file_b_path; 283 FilePath normalized_file_a_path, normalized_file_b_path;
288 ASSERT_FALSE(base::PathExists(file_a_path)); 284 ASSERT_FALSE(PathExists(file_a_path));
289 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path, 285 ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
290 &normalized_file_a_path)) 286 &normalized_file_a_path))
291 << "NormalizeFilePath() should fail on nonexistent paths."; 287 << "NormalizeFilePath() should fail on nonexistent paths.";
292 288
293 CreateTextFile(file_a_path, bogus_content); 289 CreateTextFile(file_a_path, bogus_content);
294 ASSERT_TRUE(base::PathExists(file_a_path)); 290 ASSERT_TRUE(PathExists(file_a_path));
295 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path, 291 ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
296 &normalized_file_a_path)); 292 &normalized_file_a_path));
297 293
298 CreateTextFile(file_b_path, bogus_content); 294 CreateTextFile(file_b_path, bogus_content);
299 ASSERT_TRUE(base::PathExists(file_b_path)); 295 ASSERT_TRUE(PathExists(file_b_path));
300 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path, 296 ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
301 &normalized_file_b_path)); 297 &normalized_file_b_path));
302 298
303 // Beacuse this test created |dir_path|, we know it is not a link 299 // 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 300 // or junction. So, the real path of the directory holding file a
305 // must be the parent of the path holding file b. 301 // must be the parent of the path holding file b.
306 ASSERT_TRUE(normalized_file_a_path.DirName() 302 ASSERT_TRUE(normalized_file_a_path.DirName()
307 .IsParent(normalized_file_b_path.DirName())); 303 .IsParent(normalized_file_b_path.DirName()));
308 } 304 }
309 305
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 &win32_path)); 486 &win32_path));
491 487
492 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath( 488 ASSERT_FALSE(file_util::DevicePathToDriveLetterPath(
493 real_device_path_plus_numbers.Append(kRelativePath), 489 real_device_path_plus_numbers.Append(kRelativePath),
494 &win32_path)); 490 &win32_path));
495 } 491 }
496 492
497 TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) { 493 TEST_F(FileUtilTest, GetPlatformFileInfoForDirectory) {
498 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test")); 494 FilePath empty_dir = temp_dir_.path().Append(FPL("gpfi_test"));
499 ASSERT_TRUE(file_util::CreateDirectory(empty_dir)); 495 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
500 base::win::ScopedHandle dir( 496 win::ScopedHandle dir(
501 ::CreateFile(empty_dir.value().c_str(), 497 ::CreateFile(empty_dir.value().c_str(),
502 FILE_ALL_ACCESS, 498 FILE_ALL_ACCESS,
503 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 499 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
504 NULL, 500 NULL,
505 OPEN_EXISTING, 501 OPEN_EXISTING,
506 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. 502 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory.
507 NULL)); 503 NULL));
508 ASSERT_TRUE(dir.IsValid()); 504 ASSERT_TRUE(dir.IsValid());
509 base::PlatformFileInfo info; 505 PlatformFileInfo info;
510 EXPECT_TRUE(base::GetPlatformFileInfo(dir.Get(), &info)); 506 EXPECT_TRUE(GetPlatformFileInfo(dir.Get(), &info));
511 EXPECT_TRUE(info.is_directory); 507 EXPECT_TRUE(info.is_directory);
512 EXPECT_FALSE(info.is_symbolic_link); 508 EXPECT_FALSE(info.is_symbolic_link);
513 EXPECT_EQ(0, info.size); 509 EXPECT_EQ(0, info.size);
514 } 510 }
515 511
516 TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) { 512 TEST_F(FileUtilTest, CreateTemporaryFileInDirLongPathTest) {
517 // Test that CreateTemporaryFileInDir() creates a path and returns a long path 513 // Test that CreateTemporaryFileInDir() creates a path and returns a long path
518 // if it is available. This test requires that: 514 // if it is available. This test requires that:
519 // - the filesystem at |temp_dir_| supports long filenames. 515 // - the filesystem at |temp_dir_| supports long filenames.
520 // - the account has FILE_LIST_DIRECTORY permission for all ancestor 516 // - the account has FILE_LIST_DIRECTORY permission for all ancestor
521 // directories of |temp_dir_|. 517 // directories of |temp_dir_|.
522 const FilePath::CharType kLongDirName[] = FPL("A long path"); 518 const FilePath::CharType kLongDirName[] = FPL("A long path");
523 const FilePath::CharType kTestSubDirName[] = FPL("test"); 519 const FilePath::CharType kTestSubDirName[] = FPL("test");
524 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName); 520 FilePath long_test_dir = temp_dir_.path().Append(kLongDirName);
525 ASSERT_TRUE(file_util::CreateDirectory(long_test_dir)); 521 ASSERT_TRUE(file_util::CreateDirectory(long_test_dir));
526 522
527 // kLongDirName is not a 8.3 component. So GetShortName() should give us a 523 // kLongDirName is not a 8.3 component. So GetShortName() should give us a
528 // different short name. 524 // different short name.
529 WCHAR path_buffer[MAX_PATH]; 525 WCHAR path_buffer[MAX_PATH];
530 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(), 526 DWORD path_buffer_length = GetShortPathName(long_test_dir.value().c_str(),
531 path_buffer, MAX_PATH); 527 path_buffer, MAX_PATH);
532 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH)); 528 ASSERT_LT(path_buffer_length, DWORD(MAX_PATH));
533 ASSERT_NE(DWORD(0), path_buffer_length); 529 ASSERT_NE(DWORD(0), path_buffer_length);
534 FilePath short_test_dir(path_buffer); 530 FilePath short_test_dir(path_buffer);
535 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str()); 531 ASSERT_STRNE(kLongDirName, short_test_dir.BaseName().value().c_str());
536 532
537 FilePath temp_file; 533 FilePath temp_file;
538 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir, &temp_file)); 534 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(short_test_dir, &temp_file));
539 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str()); 535 EXPECT_STREQ(kLongDirName, temp_file.DirName().BaseName().value().c_str());
540 EXPECT_TRUE(base::PathExists(temp_file)); 536 EXPECT_TRUE(PathExists(temp_file));
541 537
542 // Create a subdirectory of |long_test_dir| and make |long_test_dir| 538 // Create a subdirectory of |long_test_dir| and make |long_test_dir|
543 // unreadable. We should still be able to create a temp file in the 539 // unreadable. We should still be able to create a temp file in the
544 // subdirectory, but we won't be able to determine the long path for it. This 540 // subdirectory, but we won't be able to determine the long path for it. This
545 // mimics the environment that some users run where their user profiles reside 541 // mimics the environment that some users run where their user profiles reside
546 // in a location where the don't have full access to the higher level 542 // in a location where the don't have full access to the higher level
547 // directories. (Note that this assumption is true for NTFS, but not for some 543 // directories. (Note that this assumption is true for NTFS, but not for some
548 // network file systems. E.g. AFS). 544 // network file systems. E.g. AFS).
549 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName); 545 FilePath access_test_dir = long_test_dir.Append(kTestSubDirName);
550 ASSERT_TRUE(file_util::CreateDirectory(access_test_dir)); 546 ASSERT_TRUE(file_util::CreateDirectory(access_test_dir));
551 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir); 547 file_util::PermissionRestorer long_test_dir_restorer(long_test_dir);
552 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir)); 548 ASSERT_TRUE(file_util::MakeFileUnreadable(long_test_dir));
553 549
554 // Use the short form of the directory to create a temporary filename. 550 // Use the short form of the directory to create a temporary filename.
555 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( 551 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
556 short_test_dir.Append(kTestSubDirName), &temp_file)); 552 short_test_dir.Append(kTestSubDirName), &temp_file));
557 EXPECT_TRUE(base::PathExists(temp_file)); 553 EXPECT_TRUE(PathExists(temp_file));
558 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName())); 554 EXPECT_TRUE(short_test_dir.IsParent(temp_file.DirName()));
559 555
560 // Check that the long path can't be determined for |temp_file|. 556 // Check that the long path can't be determined for |temp_file|.
561 path_buffer_length = GetLongPathName(temp_file.value().c_str(), 557 path_buffer_length = GetLongPathName(temp_file.value().c_str(),
562 path_buffer, MAX_PATH); 558 path_buffer, MAX_PATH);
563 EXPECT_EQ(DWORD(0), path_buffer_length); 559 EXPECT_EQ(DWORD(0), path_buffer_length);
564 } 560 }
565 561
566 #endif // defined(OS_WIN) 562 #endif // defined(OS_WIN)
567 563
568 #if defined(OS_POSIX) 564 #if defined(OS_POSIX)
569 565
570 TEST_F(FileUtilTest, CreateAndReadSymlinks) { 566 TEST_F(FileUtilTest, CreateAndReadSymlinks) {
571 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); 567 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
572 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); 568 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
573 CreateTextFile(link_to, bogus_content); 569 CreateTextFile(link_to, bogus_content);
574 570
575 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) 571 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
576 << "Failed to create file symlink."; 572 << "Failed to create file symlink.";
577 573
578 // If we created the link properly, we should be able to read the contents 574 // If we created the link properly, we should be able to read the contents
579 // through it. 575 // through it.
580 std::wstring contents = ReadTextFile(link_from); 576 std::wstring contents = ReadTextFile(link_from);
581 EXPECT_EQ(bogus_content, contents); 577 EXPECT_EQ(bogus_content, contents);
582 578
583 FilePath result; 579 FilePath result;
584 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from, &result)); 580 ASSERT_TRUE(ReadSymbolicLink(link_from, &result));
585 EXPECT_EQ(link_to.value(), result.value()); 581 EXPECT_EQ(link_to.value(), result.value());
586 582
587 // Link to a directory. 583 // Link to a directory.
588 link_from = temp_dir_.path().Append(FPL("from_dir")); 584 link_from = temp_dir_.path().Append(FPL("from_dir"));
589 link_to = temp_dir_.path().Append(FPL("to_dir")); 585 link_to = temp_dir_.path().Append(FPL("to_dir"));
590 ASSERT_TRUE(file_util::CreateDirectory(link_to)); 586 ASSERT_TRUE(file_util::CreateDirectory(link_to));
591 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) 587 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
592 << "Failed to create directory symlink."; 588 << "Failed to create directory symlink.";
593 589
594 // Test failures. 590 // Test failures.
595 EXPECT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); 591 EXPECT_FALSE(CreateSymbolicLink(link_to, link_to));
596 EXPECT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); 592 EXPECT_FALSE(ReadSymbolicLink(link_to, &result));
597 FilePath missing = temp_dir_.path().Append(FPL("missing")); 593 FilePath missing = temp_dir_.path().Append(FPL("missing"));
598 EXPECT_FALSE(file_util::ReadSymbolicLink(missing, &result)); 594 EXPECT_FALSE(ReadSymbolicLink(missing, &result));
599 } 595 }
600 596
601 // The following test of NormalizeFilePath() require that we create a symlink. 597 // The following test of NormalizeFilePath() require that we create a symlink.
602 // This can not be done on Windows before Vista. On Vista, creating a symlink 598 // This can not be done on Windows before Vista. On Vista, creating a symlink
603 // requires privilege "SeCreateSymbolicLinkPrivilege". 599 // requires privilege "SeCreateSymbolicLinkPrivilege".
604 // TODO(skerner): Investigate the possibility of giving base_unittests the 600 // TODO(skerner): Investigate the possibility of giving base_unittests the
605 // privileges required to create a symlink. 601 // privileges required to create a symlink.
606 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { 602 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
607 // Link one file to another. 603 // Link one file to another.
608 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); 604 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
609 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); 605 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
610 CreateTextFile(link_to, bogus_content); 606 CreateTextFile(link_to, bogus_content);
611 607
612 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) 608 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
613 << "Failed to create file symlink."; 609 << "Failed to create file symlink.";
614 610
615 // Check that NormalizeFilePath sees the link. 611 // Check that NormalizeFilePath sees the link.
616 FilePath normalized_path; 612 FilePath normalized_path;
617 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path)); 613 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
618 EXPECT_NE(link_from, link_to); 614 EXPECT_NE(link_from, link_to);
619 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); 615 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
620 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); 616 EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
621 617
622 // Link to a directory. 618 // Link to a directory.
623 link_from = temp_dir_.path().Append(FPL("from_dir")); 619 link_from = temp_dir_.path().Append(FPL("from_dir"));
624 link_to = temp_dir_.path().Append(FPL("to_dir")); 620 link_to = temp_dir_.path().Append(FPL("to_dir"));
625 ASSERT_TRUE(file_util::CreateDirectory(link_to)); 621 ASSERT_TRUE(file_util::CreateDirectory(link_to));
626 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) 622 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
627 << "Failed to create directory symlink."; 623 << "Failed to create directory symlink.";
628 624
629 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)) 625 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
630 << "Links to directories should return false."; 626 << "Links to directories should return false.";
631 627
632 // Test that a loop in the links causes NormalizeFilePath() to return false. 628 // Test that a loop in the links causes NormalizeFilePath() to return false.
633 link_from = temp_dir_.path().Append(FPL("link_a")); 629 link_from = temp_dir_.path().Append(FPL("link_a"));
634 link_to = temp_dir_.path().Append(FPL("link_b")); 630 link_to = temp_dir_.path().Append(FPL("link_b"));
635 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) 631 ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
636 << "Failed to create loop symlink a."; 632 << "Failed to create loop symlink a.";
637 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from, link_to)) 633 ASSERT_TRUE(CreateSymbolicLink(link_from, link_to))
638 << "Failed to create loop symlink b."; 634 << "Failed to create loop symlink b.";
639 635
640 // Infinite loop! 636 // Infinite loop!
641 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)); 637 EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
642 } 638 }
643 #endif // defined(OS_POSIX) 639 #endif // defined(OS_POSIX)
644 640
645 TEST_F(FileUtilTest, DeleteNonExistent) { 641 TEST_F(FileUtilTest, DeleteNonExistent) {
646 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar"); 642 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
647 ASSERT_FALSE(base::PathExists(non_existent)); 643 ASSERT_FALSE(PathExists(non_existent));
648 644
649 EXPECT_TRUE(base::DeleteFile(non_existent, false)); 645 EXPECT_TRUE(DeleteFile(non_existent, false));
650 ASSERT_FALSE(base::PathExists(non_existent)); 646 ASSERT_FALSE(PathExists(non_existent));
651 EXPECT_TRUE(base::DeleteFile(non_existent, true)); 647 EXPECT_TRUE(DeleteFile(non_existent, true));
652 ASSERT_FALSE(base::PathExists(non_existent)); 648 ASSERT_FALSE(PathExists(non_existent));
653 } 649 }
654 650
655 TEST_F(FileUtilTest, DeleteFile) { 651 TEST_F(FileUtilTest, DeleteFile) {
656 // Create a file 652 // Create a file
657 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt")); 653 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 1.txt"));
658 CreateTextFile(file_name, bogus_content); 654 CreateTextFile(file_name, bogus_content);
659 ASSERT_TRUE(base::PathExists(file_name)); 655 ASSERT_TRUE(PathExists(file_name));
660 656
661 // Make sure it's deleted 657 // Make sure it's deleted
662 EXPECT_TRUE(base::DeleteFile(file_name, false)); 658 EXPECT_TRUE(DeleteFile(file_name, false));
663 EXPECT_FALSE(base::PathExists(file_name)); 659 EXPECT_FALSE(PathExists(file_name));
664 660
665 // Test recursive case, create a new file 661 // Test recursive case, create a new file
666 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt")); 662 file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
667 CreateTextFile(file_name, bogus_content); 663 CreateTextFile(file_name, bogus_content);
668 ASSERT_TRUE(base::PathExists(file_name)); 664 ASSERT_TRUE(PathExists(file_name));
669 665
670 // Make sure it's deleted 666 // Make sure it's deleted
671 EXPECT_TRUE(base::DeleteFile(file_name, true)); 667 EXPECT_TRUE(DeleteFile(file_name, true));
672 EXPECT_FALSE(base::PathExists(file_name)); 668 EXPECT_FALSE(PathExists(file_name));
673 } 669 }
674 670
675 #if defined(OS_POSIX) 671 #if defined(OS_POSIX)
676 TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) { 672 TEST_F(FileUtilTest, DeleteSymlinkToExistentFile) {
677 // Create a file. 673 // Create a file.
678 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt")); 674 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteFile 2.txt"));
679 CreateTextFile(file_name, bogus_content); 675 CreateTextFile(file_name, bogus_content);
680 ASSERT_TRUE(base::PathExists(file_name)); 676 ASSERT_TRUE(PathExists(file_name));
681 677
682 // Create a symlink to the file. 678 // Create a symlink to the file.
683 FilePath file_link = temp_dir_.path().Append("file_link_2"); 679 FilePath file_link = temp_dir_.path().Append("file_link_2");
684 ASSERT_TRUE(file_util::CreateSymbolicLink(file_name, file_link)) 680 ASSERT_TRUE(CreateSymbolicLink(file_name, file_link))
685 << "Failed to create symlink."; 681 << "Failed to create symlink.";
686 682
687 // Delete the symbolic link. 683 // Delete the symbolic link.
688 EXPECT_TRUE(base::DeleteFile(file_link, false)); 684 EXPECT_TRUE(DeleteFile(file_link, false));
689 685
690 // Make sure original file is not deleted. 686 // Make sure original file is not deleted.
691 EXPECT_FALSE(base::PathExists(file_link)); 687 EXPECT_FALSE(PathExists(file_link));
692 EXPECT_TRUE(base::PathExists(file_name)); 688 EXPECT_TRUE(PathExists(file_name));
693 } 689 }
694 690
695 TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) { 691 TEST_F(FileUtilTest, DeleteSymlinkToNonExistentFile) {
696 // Create a non-existent file path. 692 // Create a non-existent file path.
697 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt")); 693 FilePath non_existent = temp_dir_.path().Append(FPL("Test DeleteFile 3.txt"));
698 EXPECT_FALSE(base::PathExists(non_existent)); 694 EXPECT_FALSE(PathExists(non_existent));
699 695
700 // Create a symlink to the non-existent file. 696 // Create a symlink to the non-existent file.
701 FilePath file_link = temp_dir_.path().Append("file_link_3"); 697 FilePath file_link = temp_dir_.path().Append("file_link_3");
702 ASSERT_TRUE(file_util::CreateSymbolicLink(non_existent, file_link)) 698 ASSERT_TRUE(CreateSymbolicLink(non_existent, file_link))
703 << "Failed to create symlink."; 699 << "Failed to create symlink.";
704 700
705 // Make sure the symbolic link is exist. 701 // Make sure the symbolic link is exist.
706 EXPECT_TRUE(file_util::IsLink(file_link)); 702 EXPECT_TRUE(file_util::IsLink(file_link));
707 EXPECT_FALSE(base::PathExists(file_link)); 703 EXPECT_FALSE(PathExists(file_link));
708 704
709 // Delete the symbolic link. 705 // Delete the symbolic link.
710 EXPECT_TRUE(base::DeleteFile(file_link, false)); 706 EXPECT_TRUE(DeleteFile(file_link, false));
711 707
712 // Make sure the symbolic link is deleted. 708 // Make sure the symbolic link is deleted.
713 EXPECT_FALSE(file_util::IsLink(file_link)); 709 EXPECT_FALSE(file_util::IsLink(file_link));
714 } 710 }
715 711
716 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) { 712 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {
717 // Create a file path. 713 // Create a file path.
718 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); 714 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
719 EXPECT_FALSE(base::PathExists(file_name)); 715 EXPECT_FALSE(PathExists(file_name));
720 716
721 const std::string kData("hello"); 717 const std::string kData("hello");
722 718
723 int buffer_size = kData.length(); 719 int buffer_size = kData.length();
724 char* buffer = new char[buffer_size]; 720 char* buffer = new char[buffer_size];
725 721
726 // Write file. 722 // Write file.
727 EXPECT_EQ(static_cast<int>(kData.length()), 723 EXPECT_EQ(static_cast<int>(kData.length()),
728 file_util::WriteFile(file_name, kData.data(), kData.length())); 724 file_util::WriteFile(file_name, kData.data(), kData.length()));
729 EXPECT_TRUE(base::PathExists(file_name)); 725 EXPECT_TRUE(PathExists(file_name));
730 726
731 // Make sure the file is readable. 727 // Make sure the file is readable.
732 int32 mode = 0; 728 int32 mode = 0;
733 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); 729 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
734 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_READ_BY_USER); 730 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
735 731
736 // Get rid of the read permission. 732 // Get rid of the read permission.
737 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); 733 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
738 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); 734 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
739 EXPECT_FALSE(mode & file_util::FILE_PERMISSION_READ_BY_USER); 735 EXPECT_FALSE(mode & FILE_PERMISSION_READ_BY_USER);
740 // Make sure the file can't be read. 736 // Make sure the file can't be read.
741 EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, buffer_size)); 737 EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, buffer_size));
742 738
743 // Give the read permission. 739 // Give the read permission.
744 EXPECT_TRUE(file_util::SetPosixFilePermissions( 740 EXPECT_TRUE(SetPosixFilePermissions(file_name, FILE_PERMISSION_READ_BY_USER));
745 file_name, 741 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
746 file_util::FILE_PERMISSION_READ_BY_USER)); 742 EXPECT_TRUE(mode & FILE_PERMISSION_READ_BY_USER);
747 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode));
748 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_READ_BY_USER);
749 // Make sure the file can be read. 743 // Make sure the file can be read.
750 EXPECT_EQ(static_cast<int>(kData.length()), 744 EXPECT_EQ(static_cast<int>(kData.length()),
751 file_util::ReadFile(file_name, buffer, buffer_size)); 745 file_util::ReadFile(file_name, buffer, buffer_size));
752 746
753 // Delete the file. 747 // Delete the file.
754 EXPECT_TRUE(base::DeleteFile(file_name, false)); 748 EXPECT_TRUE(DeleteFile(file_name, false));
755 EXPECT_FALSE(base::PathExists(file_name)); 749 EXPECT_FALSE(PathExists(file_name));
756 750
757 delete[] buffer; 751 delete[] buffer;
758 } 752 }
759 753
760 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { 754 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
761 // Create a file path. 755 // Create a file path.
762 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); 756 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt"));
763 EXPECT_FALSE(base::PathExists(file_name)); 757 EXPECT_FALSE(PathExists(file_name));
764 758
765 const std::string kData("hello"); 759 const std::string kData("hello");
766 760
767 // Write file. 761 // Write file.
768 EXPECT_EQ(static_cast<int>(kData.length()), 762 EXPECT_EQ(static_cast<int>(kData.length()),
769 file_util::WriteFile(file_name, kData.data(), kData.length())); 763 file_util::WriteFile(file_name, kData.data(), kData.length()));
770 EXPECT_TRUE(base::PathExists(file_name)); 764 EXPECT_TRUE(PathExists(file_name));
771 765
772 // Make sure the file is writable. 766 // Make sure the file is writable.
773 int mode = 0; 767 int mode = 0;
774 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); 768 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
775 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER); 769 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
776 EXPECT_TRUE(PathIsWritable(file_name)); 770 EXPECT_TRUE(PathIsWritable(file_name));
777 771
778 // Get rid of the write permission. 772 // Get rid of the write permission.
779 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); 773 EXPECT_TRUE(SetPosixFilePermissions(file_name, 0u));
780 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); 774 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
781 EXPECT_FALSE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER); 775 EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
782 // Make sure the file can't be write. 776 // Make sure the file can't be write.
783 EXPECT_EQ(-1, 777 EXPECT_EQ(-1,
784 file_util::WriteFile(file_name, kData.data(), kData.length())); 778 file_util::WriteFile(file_name, kData.data(), kData.length()));
785 EXPECT_FALSE(PathIsWritable(file_name)); 779 EXPECT_FALSE(PathIsWritable(file_name));
786 780
787 // Give read permission. 781 // Give read permission.
788 EXPECT_TRUE(file_util::SetPosixFilePermissions( 782 EXPECT_TRUE(SetPosixFilePermissions(file_name,
789 file_name, 783 FILE_PERMISSION_WRITE_BY_USER));
790 file_util::FILE_PERMISSION_WRITE_BY_USER)); 784 EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
791 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); 785 EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
792 EXPECT_TRUE(mode & file_util::FILE_PERMISSION_WRITE_BY_USER);
793 // Make sure the file can be write. 786 // Make sure the file can be write.
794 EXPECT_EQ(static_cast<int>(kData.length()), 787 EXPECT_EQ(static_cast<int>(kData.length()),
795 file_util::WriteFile(file_name, kData.data(), kData.length())); 788 file_util::WriteFile(file_name, kData.data(), kData.length()));
796 EXPECT_TRUE(PathIsWritable(file_name)); 789 EXPECT_TRUE(PathIsWritable(file_name));
797 790
798 // Delete the file. 791 // Delete the file.
799 EXPECT_TRUE(base::DeleteFile(file_name, false)); 792 EXPECT_TRUE(DeleteFile(file_name, false));
800 EXPECT_FALSE(base::PathExists(file_name)); 793 EXPECT_FALSE(PathExists(file_name));
801 } 794 }
802 795
803 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { 796 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
804 // Create a directory path. 797 // Create a directory path.
805 FilePath subdir_path = 798 FilePath subdir_path =
806 temp_dir_.path().Append(FPL("PermissionTest1")); 799 temp_dir_.path().Append(FPL("PermissionTest1"));
807 file_util::CreateDirectory(subdir_path); 800 file_util::CreateDirectory(subdir_path);
808 ASSERT_TRUE(base::PathExists(subdir_path)); 801 ASSERT_TRUE(PathExists(subdir_path));
809 802
810 // Create a dummy file to enumerate. 803 // Create a dummy file to enumerate.
811 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); 804 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt"));
812 EXPECT_FALSE(base::PathExists(file_name)); 805 EXPECT_FALSE(PathExists(file_name));
813 const std::string kData("hello"); 806 const std::string kData("hello");
814 EXPECT_EQ(static_cast<int>(kData.length()), 807 EXPECT_EQ(static_cast<int>(kData.length()),
815 file_util::WriteFile(file_name, kData.data(), kData.length())); 808 file_util::WriteFile(file_name, kData.data(), kData.length()));
816 EXPECT_TRUE(base::PathExists(file_name)); 809 EXPECT_TRUE(PathExists(file_name));
817 810
818 // Make sure the directory has the all permissions. 811 // Make sure the directory has the all permissions.
819 int mode = 0; 812 int mode = 0;
820 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); 813 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
821 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, 814 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
822 mode & file_util::FILE_PERMISSION_USER_MASK);
823 815
824 // Get rid of the permissions from the directory. 816 // Get rid of the permissions from the directory.
825 EXPECT_TRUE(file_util::SetPosixFilePermissions(subdir_path, 0u)); 817 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, 0u));
826 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); 818 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
827 EXPECT_FALSE(mode & file_util::FILE_PERMISSION_USER_MASK); 819 EXPECT_FALSE(mode & FILE_PERMISSION_USER_MASK);
828 820
829 // Make sure the file in the directory can't be enumerated. 821 // Make sure the file in the directory can't be enumerated.
830 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES); 822 FileEnumerator f1(subdir_path, true, FileEnumerator::FILES);
831 EXPECT_TRUE(base::PathExists(subdir_path)); 823 EXPECT_TRUE(PathExists(subdir_path));
832 FindResultCollector c1(f1); 824 FindResultCollector c1(f1);
833 EXPECT_EQ(c1.size(), 0); 825 EXPECT_EQ(c1.size(), 0);
834 EXPECT_FALSE(file_util::GetPosixFilePermissions(file_name, &mode)); 826 EXPECT_FALSE(GetPosixFilePermissions(file_name, &mode));
835 827
836 // Give the permissions to the directory. 828 // Give the permissions to the directory.
837 EXPECT_TRUE(file_util::SetPosixFilePermissions( 829 EXPECT_TRUE(SetPosixFilePermissions(subdir_path, FILE_PERMISSION_USER_MASK));
838 subdir_path, 830 EXPECT_TRUE(GetPosixFilePermissions(subdir_path, &mode));
839 file_util::FILE_PERMISSION_USER_MASK)); 831 EXPECT_EQ(FILE_PERMISSION_USER_MASK, mode & FILE_PERMISSION_USER_MASK);
840 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode));
841 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK,
842 mode & file_util::FILE_PERMISSION_USER_MASK);
843 832
844 // Make sure the file in the directory can be enumerated. 833 // Make sure the file in the directory can be enumerated.
845 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES); 834 FileEnumerator f2(subdir_path, true, FileEnumerator::FILES);
846 FindResultCollector c2(f2); 835 FindResultCollector c2(f2);
847 EXPECT_TRUE(c2.HasFile(file_name)); 836 EXPECT_TRUE(c2.HasFile(file_name));
848 EXPECT_EQ(c2.size(), 1); 837 EXPECT_EQ(c2.size(), 1);
849 838
850 // Delete the file. 839 // Delete the file.
851 EXPECT_TRUE(base::DeleteFile(subdir_path, true)); 840 EXPECT_TRUE(DeleteFile(subdir_path, true));
852 EXPECT_FALSE(base::PathExists(subdir_path)); 841 EXPECT_FALSE(PathExists(subdir_path));
853 } 842 }
854 843
855 #endif // defined(OS_POSIX) 844 #endif // defined(OS_POSIX)
856 845
857 #if defined(OS_WIN) 846 #if defined(OS_WIN)
858 // Tests that the Delete function works for wild cards, especially 847 // Tests that the Delete function works for wild cards, especially
859 // with the recursion flag. Also coincidentally tests PathExists. 848 // with the recursion flag. Also coincidentally tests PathExists.
860 // TODO(erikkay): see if anyone's actually using this feature of the API 849 // TODO(erikkay): see if anyone's actually using this feature of the API
861 TEST_F(FileUtilTest, DeleteWildCard) { 850 TEST_F(FileUtilTest, DeleteWildCard) {
862 // Create a file and a directory 851 // Create a file and a directory
863 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); 852 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt"));
864 CreateTextFile(file_name, bogus_content); 853 CreateTextFile(file_name, bogus_content);
865 ASSERT_TRUE(base::PathExists(file_name)); 854 ASSERT_TRUE(PathExists(file_name));
866 855
867 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir")); 856 FilePath subdir_path = temp_dir_.path().Append(FPL("DeleteWildCardDir"));
868 file_util::CreateDirectory(subdir_path); 857 file_util::CreateDirectory(subdir_path);
869 ASSERT_TRUE(base::PathExists(subdir_path)); 858 ASSERT_TRUE(PathExists(subdir_path));
870 859
871 // Create the wildcard path 860 // Create the wildcard path
872 FilePath directory_contents = temp_dir_.path(); 861 FilePath directory_contents = temp_dir_.path();
873 directory_contents = directory_contents.Append(FPL("*")); 862 directory_contents = directory_contents.Append(FPL("*"));
874 863
875 // Delete non-recursively and check that only the file is deleted 864 // Delete non-recursively and check that only the file is deleted
876 EXPECT_TRUE(base::DeleteFile(directory_contents, false)); 865 EXPECT_TRUE(DeleteFile(directory_contents, false));
877 EXPECT_FALSE(base::PathExists(file_name)); 866 EXPECT_FALSE(PathExists(file_name));
878 EXPECT_TRUE(base::PathExists(subdir_path)); 867 EXPECT_TRUE(PathExists(subdir_path));
879 868
880 // Delete recursively and make sure all contents are deleted 869 // Delete recursively and make sure all contents are deleted
881 EXPECT_TRUE(base::DeleteFile(directory_contents, true)); 870 EXPECT_TRUE(DeleteFile(directory_contents, true));
882 EXPECT_FALSE(base::PathExists(file_name)); 871 EXPECT_FALSE(PathExists(file_name));
883 EXPECT_FALSE(base::PathExists(subdir_path)); 872 EXPECT_FALSE(PathExists(subdir_path));
884 } 873 }
885 874
886 // TODO(erikkay): see if anyone's actually using this feature of the API 875 // TODO(erikkay): see if anyone's actually using this feature of the API
887 TEST_F(FileUtilTest, DeleteNonExistantWildCard) { 876 TEST_F(FileUtilTest, DeleteNonExistantWildCard) {
888 // Create a file and a directory 877 // Create a file and a directory
889 FilePath subdir_path = 878 FilePath subdir_path =
890 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard")); 879 temp_dir_.path().Append(FPL("DeleteNonExistantWildCard"));
891 file_util::CreateDirectory(subdir_path); 880 file_util::CreateDirectory(subdir_path);
892 ASSERT_TRUE(base::PathExists(subdir_path)); 881 ASSERT_TRUE(PathExists(subdir_path));
893 882
894 // Create the wildcard path 883 // Create the wildcard path
895 FilePath directory_contents = subdir_path; 884 FilePath directory_contents = subdir_path;
896 directory_contents = directory_contents.Append(FPL("*")); 885 directory_contents = directory_contents.Append(FPL("*"));
897 886
898 // Delete non-recursively and check nothing got deleted 887 // Delete non-recursively and check nothing got deleted
899 EXPECT_TRUE(base::DeleteFile(directory_contents, false)); 888 EXPECT_TRUE(DeleteFile(directory_contents, false));
900 EXPECT_TRUE(base::PathExists(subdir_path)); 889 EXPECT_TRUE(PathExists(subdir_path));
901 890
902 // Delete recursively and check nothing got deleted 891 // Delete recursively and check nothing got deleted
903 EXPECT_TRUE(base::DeleteFile(directory_contents, true)); 892 EXPECT_TRUE(DeleteFile(directory_contents, true));
904 EXPECT_TRUE(base::PathExists(subdir_path)); 893 EXPECT_TRUE(PathExists(subdir_path));
905 } 894 }
906 #endif 895 #endif
907 896
908 // Tests non-recursive Delete() for a directory. 897 // Tests non-recursive Delete() for a directory.
909 TEST_F(FileUtilTest, DeleteDirNonRecursive) { 898 TEST_F(FileUtilTest, DeleteDirNonRecursive) {
910 // Create a subdirectory and put a file and two directories inside. 899 // Create a subdirectory and put a file and two directories inside.
911 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive")); 900 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirNonRecursive"));
912 file_util::CreateDirectory(test_subdir); 901 file_util::CreateDirectory(test_subdir);
913 ASSERT_TRUE(base::PathExists(test_subdir)); 902 ASSERT_TRUE(PathExists(test_subdir));
914 903
915 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt")); 904 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt"));
916 CreateTextFile(file_name, bogus_content); 905 CreateTextFile(file_name, bogus_content);
917 ASSERT_TRUE(base::PathExists(file_name)); 906 ASSERT_TRUE(PathExists(file_name));
918 907
919 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); 908 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
920 file_util::CreateDirectory(subdir_path1); 909 file_util::CreateDirectory(subdir_path1);
921 ASSERT_TRUE(base::PathExists(subdir_path1)); 910 ASSERT_TRUE(PathExists(subdir_path1));
922 911
923 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); 912 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
924 file_util::CreateDirectory(subdir_path2); 913 file_util::CreateDirectory(subdir_path2);
925 ASSERT_TRUE(base::PathExists(subdir_path2)); 914 ASSERT_TRUE(PathExists(subdir_path2));
926 915
927 // Delete non-recursively and check that the empty dir got deleted 916 // Delete non-recursively and check that the empty dir got deleted
928 EXPECT_TRUE(base::DeleteFile(subdir_path2, false)); 917 EXPECT_TRUE(DeleteFile(subdir_path2, false));
929 EXPECT_FALSE(base::PathExists(subdir_path2)); 918 EXPECT_FALSE(PathExists(subdir_path2));
930 919
931 // Delete non-recursively and check that nothing got deleted 920 // Delete non-recursively and check that nothing got deleted
932 EXPECT_FALSE(base::DeleteFile(test_subdir, false)); 921 EXPECT_FALSE(DeleteFile(test_subdir, false));
933 EXPECT_TRUE(base::PathExists(test_subdir)); 922 EXPECT_TRUE(PathExists(test_subdir));
934 EXPECT_TRUE(base::PathExists(file_name)); 923 EXPECT_TRUE(PathExists(file_name));
935 EXPECT_TRUE(base::PathExists(subdir_path1)); 924 EXPECT_TRUE(PathExists(subdir_path1));
936 } 925 }
937 926
938 // Tests recursive Delete() for a directory. 927 // Tests recursive Delete() for a directory.
939 TEST_F(FileUtilTest, DeleteDirRecursive) { 928 TEST_F(FileUtilTest, DeleteDirRecursive) {
940 // Create a subdirectory and put a file and two directories inside. 929 // Create a subdirectory and put a file and two directories inside.
941 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive")); 930 FilePath test_subdir = temp_dir_.path().Append(FPL("DeleteDirRecursive"));
942 file_util::CreateDirectory(test_subdir); 931 file_util::CreateDirectory(test_subdir);
943 ASSERT_TRUE(base::PathExists(test_subdir)); 932 ASSERT_TRUE(PathExists(test_subdir));
944 933
945 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt")); 934 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt"));
946 CreateTextFile(file_name, bogus_content); 935 CreateTextFile(file_name, bogus_content);
947 ASSERT_TRUE(base::PathExists(file_name)); 936 ASSERT_TRUE(PathExists(file_name));
948 937
949 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); 938 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1"));
950 file_util::CreateDirectory(subdir_path1); 939 file_util::CreateDirectory(subdir_path1);
951 ASSERT_TRUE(base::PathExists(subdir_path1)); 940 ASSERT_TRUE(PathExists(subdir_path1));
952 941
953 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); 942 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2"));
954 file_util::CreateDirectory(subdir_path2); 943 file_util::CreateDirectory(subdir_path2);
955 ASSERT_TRUE(base::PathExists(subdir_path2)); 944 ASSERT_TRUE(PathExists(subdir_path2));
956 945
957 // Delete recursively and check that the empty dir got deleted 946 // Delete recursively and check that the empty dir got deleted
958 EXPECT_TRUE(base::DeleteFile(subdir_path2, true)); 947 EXPECT_TRUE(DeleteFile(subdir_path2, true));
959 EXPECT_FALSE(base::PathExists(subdir_path2)); 948 EXPECT_FALSE(PathExists(subdir_path2));
960 949
961 // Delete recursively and check that everything got deleted 950 // Delete recursively and check that everything got deleted
962 EXPECT_TRUE(base::DeleteFile(test_subdir, true)); 951 EXPECT_TRUE(DeleteFile(test_subdir, true));
963 EXPECT_FALSE(base::PathExists(file_name)); 952 EXPECT_FALSE(PathExists(file_name));
964 EXPECT_FALSE(base::PathExists(subdir_path1)); 953 EXPECT_FALSE(PathExists(subdir_path1));
965 EXPECT_FALSE(base::PathExists(test_subdir)); 954 EXPECT_FALSE(PathExists(test_subdir));
966 } 955 }
967 956
968 TEST_F(FileUtilTest, MoveFileNew) { 957 TEST_F(FileUtilTest, MoveFileNew) {
969 // Create a file 958 // Create a file
970 FilePath file_name_from = 959 FilePath file_name_from =
971 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt")); 960 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
972 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 961 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
973 ASSERT_TRUE(base::PathExists(file_name_from)); 962 ASSERT_TRUE(PathExists(file_name_from));
974 963
975 // The destination. 964 // The destination.
976 FilePath file_name_to = temp_dir_.path().Append( 965 FilePath file_name_to = temp_dir_.path().Append(
977 FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); 966 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
978 ASSERT_FALSE(base::PathExists(file_name_to)); 967 ASSERT_FALSE(PathExists(file_name_to));
979 968
980 EXPECT_TRUE(base::Move(file_name_from, file_name_to)); 969 EXPECT_TRUE(Move(file_name_from, file_name_to));
981 970
982 // Check everything has been moved. 971 // Check everything has been moved.
983 EXPECT_FALSE(base::PathExists(file_name_from)); 972 EXPECT_FALSE(PathExists(file_name_from));
984 EXPECT_TRUE(base::PathExists(file_name_to)); 973 EXPECT_TRUE(PathExists(file_name_to));
985 } 974 }
986 975
987 TEST_F(FileUtilTest, MoveFileExists) { 976 TEST_F(FileUtilTest, MoveFileExists) {
988 // Create a file 977 // Create a file
989 FilePath file_name_from = 978 FilePath file_name_from =
990 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt")); 979 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
991 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 980 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
992 ASSERT_TRUE(base::PathExists(file_name_from)); 981 ASSERT_TRUE(PathExists(file_name_from));
993 982
994 // The destination name. 983 // The destination name.
995 FilePath file_name_to = temp_dir_.path().Append( 984 FilePath file_name_to = temp_dir_.path().Append(
996 FILE_PATH_LITERAL("Move_Test_File_Destination.txt")); 985 FILE_PATH_LITERAL("Move_Test_File_Destination.txt"));
997 CreateTextFile(file_name_to, L"Old file content"); 986 CreateTextFile(file_name_to, L"Old file content");
998 ASSERT_TRUE(base::PathExists(file_name_to)); 987 ASSERT_TRUE(PathExists(file_name_to));
999 988
1000 EXPECT_TRUE(base::Move(file_name_from, file_name_to)); 989 EXPECT_TRUE(Move(file_name_from, file_name_to));
1001 990
1002 // Check everything has been moved. 991 // Check everything has been moved.
1003 EXPECT_FALSE(base::PathExists(file_name_from)); 992 EXPECT_FALSE(PathExists(file_name_from));
1004 EXPECT_TRUE(base::PathExists(file_name_to)); 993 EXPECT_TRUE(PathExists(file_name_to));
1005 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); 994 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1006 } 995 }
1007 996
1008 TEST_F(FileUtilTest, MoveFileDirExists) { 997 TEST_F(FileUtilTest, MoveFileDirExists) {
1009 // Create a file 998 // Create a file
1010 FilePath file_name_from = 999 FilePath file_name_from =
1011 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt")); 1000 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1012 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1001 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1013 ASSERT_TRUE(base::PathExists(file_name_from)); 1002 ASSERT_TRUE(PathExists(file_name_from));
1014 1003
1015 // The destination directory 1004 // The destination directory
1016 FilePath dir_name_to = 1005 FilePath dir_name_to =
1017 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination")); 1006 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
1018 file_util::CreateDirectory(dir_name_to); 1007 file_util::CreateDirectory(dir_name_to);
1019 ASSERT_TRUE(base::PathExists(dir_name_to)); 1008 ASSERT_TRUE(PathExists(dir_name_to));
1020 1009
1021 EXPECT_FALSE(base::Move(file_name_from, dir_name_to)); 1010 EXPECT_FALSE(Move(file_name_from, dir_name_to));
1022 } 1011 }
1023 1012
1024 1013
1025 TEST_F(FileUtilTest, MoveNew) { 1014 TEST_F(FileUtilTest, MoveNew) {
1026 // Create a directory 1015 // Create a directory
1027 FilePath dir_name_from = 1016 FilePath dir_name_from =
1028 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir")); 1017 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1029 file_util::CreateDirectory(dir_name_from); 1018 file_util::CreateDirectory(dir_name_from);
1030 ASSERT_TRUE(base::PathExists(dir_name_from)); 1019 ASSERT_TRUE(PathExists(dir_name_from));
1031 1020
1032 // Create a file under the directory 1021 // Create a file under the directory
1033 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt")); 1022 FilePath txt_file_name(FILE_PATH_LITERAL("Move_Test_File.txt"));
1034 FilePath file_name_from = dir_name_from.Append(txt_file_name); 1023 FilePath file_name_from = dir_name_from.Append(txt_file_name);
1035 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1024 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1036 ASSERT_TRUE(base::PathExists(file_name_from)); 1025 ASSERT_TRUE(PathExists(file_name_from));
1037 1026
1038 // Move the directory. 1027 // Move the directory.
1039 FilePath dir_name_to = 1028 FilePath dir_name_to =
1040 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir")); 1029 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1041 FilePath file_name_to = 1030 FilePath file_name_to =
1042 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); 1031 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1043 1032
1044 ASSERT_FALSE(base::PathExists(dir_name_to)); 1033 ASSERT_FALSE(PathExists(dir_name_to));
1045 1034
1046 EXPECT_TRUE(base::Move(dir_name_from, dir_name_to)); 1035 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
1047 1036
1048 // Check everything has been moved. 1037 // Check everything has been moved.
1049 EXPECT_FALSE(base::PathExists(dir_name_from)); 1038 EXPECT_FALSE(PathExists(dir_name_from));
1050 EXPECT_FALSE(base::PathExists(file_name_from)); 1039 EXPECT_FALSE(PathExists(file_name_from));
1051 EXPECT_TRUE(base::PathExists(dir_name_to)); 1040 EXPECT_TRUE(PathExists(dir_name_to));
1052 EXPECT_TRUE(base::PathExists(file_name_to)); 1041 EXPECT_TRUE(PathExists(file_name_to));
1053 1042
1054 // Test path traversal. 1043 // Test path traversal.
1055 file_name_from = dir_name_to.Append(txt_file_name); 1044 file_name_from = dir_name_to.Append(txt_file_name);
1056 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL("..")); 1045 file_name_to = dir_name_to.Append(FILE_PATH_LITERAL(".."));
1057 file_name_to = file_name_to.Append(txt_file_name); 1046 file_name_to = file_name_to.Append(txt_file_name);
1058 EXPECT_FALSE(base::Move(file_name_from, file_name_to)); 1047 EXPECT_FALSE(Move(file_name_from, file_name_to));
1059 EXPECT_TRUE(base::PathExists(file_name_from)); 1048 EXPECT_TRUE(PathExists(file_name_from));
1060 EXPECT_FALSE(base::PathExists(file_name_to)); 1049 EXPECT_FALSE(PathExists(file_name_to));
1061 EXPECT_TRUE(base::internal::MoveUnsafe(file_name_from, file_name_to)); 1050 EXPECT_TRUE(internal::MoveUnsafe(file_name_from, file_name_to));
1062 EXPECT_FALSE(base::PathExists(file_name_from)); 1051 EXPECT_FALSE(PathExists(file_name_from));
1063 EXPECT_TRUE(base::PathExists(file_name_to)); 1052 EXPECT_TRUE(PathExists(file_name_to));
1064 } 1053 }
1065 1054
1066 TEST_F(FileUtilTest, MoveExist) { 1055 TEST_F(FileUtilTest, MoveExist) {
1067 // Create a directory 1056 // Create a directory
1068 FilePath dir_name_from = 1057 FilePath dir_name_from =
1069 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir")); 1058 temp_dir_.path().Append(FILE_PATH_LITERAL("Move_From_Subdir"));
1070 file_util::CreateDirectory(dir_name_from); 1059 file_util::CreateDirectory(dir_name_from);
1071 ASSERT_TRUE(base::PathExists(dir_name_from)); 1060 ASSERT_TRUE(PathExists(dir_name_from));
1072 1061
1073 // Create a file under the directory 1062 // Create a file under the directory
1074 FilePath file_name_from = 1063 FilePath file_name_from =
1075 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); 1064 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1076 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1065 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1077 ASSERT_TRUE(base::PathExists(file_name_from)); 1066 ASSERT_TRUE(PathExists(file_name_from));
1078 1067
1079 // Move the directory 1068 // Move the directory
1080 FilePath dir_name_exists = 1069 FilePath dir_name_exists =
1081 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination")); 1070 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
1082 1071
1083 FilePath dir_name_to = 1072 FilePath dir_name_to =
1084 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir")); 1073 dir_name_exists.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
1085 FilePath file_name_to = 1074 FilePath file_name_to =
1086 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); 1075 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
1087 1076
1088 // Create the destination directory. 1077 // Create the destination directory.
1089 file_util::CreateDirectory(dir_name_exists); 1078 file_util::CreateDirectory(dir_name_exists);
1090 ASSERT_TRUE(base::PathExists(dir_name_exists)); 1079 ASSERT_TRUE(PathExists(dir_name_exists));
1091 1080
1092 EXPECT_TRUE(base::Move(dir_name_from, dir_name_to)); 1081 EXPECT_TRUE(Move(dir_name_from, dir_name_to));
1093 1082
1094 // Check everything has been moved. 1083 // Check everything has been moved.
1095 EXPECT_FALSE(base::PathExists(dir_name_from)); 1084 EXPECT_FALSE(PathExists(dir_name_from));
1096 EXPECT_FALSE(base::PathExists(file_name_from)); 1085 EXPECT_FALSE(PathExists(file_name_from));
1097 EXPECT_TRUE(base::PathExists(dir_name_to)); 1086 EXPECT_TRUE(PathExists(dir_name_to));
1098 EXPECT_TRUE(base::PathExists(file_name_to)); 1087 EXPECT_TRUE(PathExists(file_name_to));
1099 } 1088 }
1100 1089
1101 TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) { 1090 TEST_F(FileUtilTest, CopyDirectoryRecursivelyNew) {
1102 // Create a directory. 1091 // Create a directory.
1103 FilePath dir_name_from = 1092 FilePath dir_name_from =
1104 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1093 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1105 file_util::CreateDirectory(dir_name_from); 1094 file_util::CreateDirectory(dir_name_from);
1106 ASSERT_TRUE(base::PathExists(dir_name_from)); 1095 ASSERT_TRUE(PathExists(dir_name_from));
1107 1096
1108 // Create a file under the directory. 1097 // Create a file under the directory.
1109 FilePath file_name_from = 1098 FilePath file_name_from =
1110 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1099 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1111 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1100 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1112 ASSERT_TRUE(base::PathExists(file_name_from)); 1101 ASSERT_TRUE(PathExists(file_name_from));
1113 1102
1114 // Create a subdirectory. 1103 // Create a subdirectory.
1115 FilePath subdir_name_from = 1104 FilePath subdir_name_from =
1116 dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); 1105 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1117 file_util::CreateDirectory(subdir_name_from); 1106 file_util::CreateDirectory(subdir_name_from);
1118 ASSERT_TRUE(base::PathExists(subdir_name_from)); 1107 ASSERT_TRUE(PathExists(subdir_name_from));
1119 1108
1120 // Create a file under the subdirectory. 1109 // Create a file under the subdirectory.
1121 FilePath file_name2_from = 1110 FilePath file_name2_from =
1122 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1111 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1123 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); 1112 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1124 ASSERT_TRUE(base::PathExists(file_name2_from)); 1113 ASSERT_TRUE(PathExists(file_name2_from));
1125 1114
1126 // Copy the directory recursively. 1115 // Copy the directory recursively.
1127 FilePath dir_name_to = 1116 FilePath dir_name_to =
1128 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir")); 1117 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1129 FilePath file_name_to = 1118 FilePath file_name_to =
1130 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1119 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1131 FilePath subdir_name_to = 1120 FilePath subdir_name_to =
1132 dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); 1121 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1133 FilePath file_name2_to = 1122 FilePath file_name2_to =
1134 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1123 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1135 1124
1136 ASSERT_FALSE(base::PathExists(dir_name_to)); 1125 ASSERT_FALSE(PathExists(dir_name_to));
1137 1126
1138 EXPECT_TRUE(base::CopyDirectory(dir_name_from, dir_name_to, true)); 1127 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, true));
1139 1128
1140 // Check everything has been copied. 1129 // Check everything has been copied.
1141 EXPECT_TRUE(base::PathExists(dir_name_from)); 1130 EXPECT_TRUE(PathExists(dir_name_from));
1142 EXPECT_TRUE(base::PathExists(file_name_from)); 1131 EXPECT_TRUE(PathExists(file_name_from));
1143 EXPECT_TRUE(base::PathExists(subdir_name_from)); 1132 EXPECT_TRUE(PathExists(subdir_name_from));
1144 EXPECT_TRUE(base::PathExists(file_name2_from)); 1133 EXPECT_TRUE(PathExists(file_name2_from));
1145 EXPECT_TRUE(base::PathExists(dir_name_to)); 1134 EXPECT_TRUE(PathExists(dir_name_to));
1146 EXPECT_TRUE(base::PathExists(file_name_to)); 1135 EXPECT_TRUE(PathExists(file_name_to));
1147 EXPECT_TRUE(base::PathExists(subdir_name_to)); 1136 EXPECT_TRUE(PathExists(subdir_name_to));
1148 EXPECT_TRUE(base::PathExists(file_name2_to)); 1137 EXPECT_TRUE(PathExists(file_name2_to));
1149 } 1138 }
1150 1139
1151 TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) { 1140 TEST_F(FileUtilTest, CopyDirectoryRecursivelyExists) {
1152 // Create a directory. 1141 // Create a directory.
1153 FilePath dir_name_from = 1142 FilePath dir_name_from =
1154 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1143 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1155 file_util::CreateDirectory(dir_name_from); 1144 file_util::CreateDirectory(dir_name_from);
1156 ASSERT_TRUE(base::PathExists(dir_name_from)); 1145 ASSERT_TRUE(PathExists(dir_name_from));
1157 1146
1158 // Create a file under the directory. 1147 // Create a file under the directory.
1159 FilePath file_name_from = 1148 FilePath file_name_from =
1160 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1149 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1161 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1150 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1162 ASSERT_TRUE(base::PathExists(file_name_from)); 1151 ASSERT_TRUE(PathExists(file_name_from));
1163 1152
1164 // Create a subdirectory. 1153 // Create a subdirectory.
1165 FilePath subdir_name_from = 1154 FilePath subdir_name_from =
1166 dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); 1155 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1167 file_util::CreateDirectory(subdir_name_from); 1156 file_util::CreateDirectory(subdir_name_from);
1168 ASSERT_TRUE(base::PathExists(subdir_name_from)); 1157 ASSERT_TRUE(PathExists(subdir_name_from));
1169 1158
1170 // Create a file under the subdirectory. 1159 // Create a file under the subdirectory.
1171 FilePath file_name2_from = 1160 FilePath file_name2_from =
1172 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1161 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1173 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); 1162 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1174 ASSERT_TRUE(base::PathExists(file_name2_from)); 1163 ASSERT_TRUE(PathExists(file_name2_from));
1175 1164
1176 // Copy the directory recursively. 1165 // Copy the directory recursively.
1177 FilePath dir_name_exists = 1166 FilePath dir_name_exists =
1178 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination")); 1167 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
1179 1168
1180 FilePath dir_name_to = 1169 FilePath dir_name_to =
1181 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1170 dir_name_exists.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1182 FilePath file_name_to = 1171 FilePath file_name_to =
1183 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1172 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1184 FilePath subdir_name_to = 1173 FilePath subdir_name_to =
1185 dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); 1174 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1186 FilePath file_name2_to = 1175 FilePath file_name2_to =
1187 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1176 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1188 1177
1189 // Create the destination directory. 1178 // Create the destination directory.
1190 file_util::CreateDirectory(dir_name_exists); 1179 file_util::CreateDirectory(dir_name_exists);
1191 ASSERT_TRUE(base::PathExists(dir_name_exists)); 1180 ASSERT_TRUE(PathExists(dir_name_exists));
1192 1181
1193 EXPECT_TRUE(base::CopyDirectory(dir_name_from, dir_name_exists, true)); 1182 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_exists, true));
1194 1183
1195 // Check everything has been copied. 1184 // Check everything has been copied.
1196 EXPECT_TRUE(base::PathExists(dir_name_from)); 1185 EXPECT_TRUE(PathExists(dir_name_from));
1197 EXPECT_TRUE(base::PathExists(file_name_from)); 1186 EXPECT_TRUE(PathExists(file_name_from));
1198 EXPECT_TRUE(base::PathExists(subdir_name_from)); 1187 EXPECT_TRUE(PathExists(subdir_name_from));
1199 EXPECT_TRUE(base::PathExists(file_name2_from)); 1188 EXPECT_TRUE(PathExists(file_name2_from));
1200 EXPECT_TRUE(base::PathExists(dir_name_to)); 1189 EXPECT_TRUE(PathExists(dir_name_to));
1201 EXPECT_TRUE(base::PathExists(file_name_to)); 1190 EXPECT_TRUE(PathExists(file_name_to));
1202 EXPECT_TRUE(base::PathExists(subdir_name_to)); 1191 EXPECT_TRUE(PathExists(subdir_name_to));
1203 EXPECT_TRUE(base::PathExists(file_name2_to)); 1192 EXPECT_TRUE(PathExists(file_name2_to));
1204 } 1193 }
1205 1194
1206 TEST_F(FileUtilTest, CopyDirectoryNew) { 1195 TEST_F(FileUtilTest, CopyDirectoryNew) {
1207 // Create a directory. 1196 // Create a directory.
1208 FilePath dir_name_from = 1197 FilePath dir_name_from =
1209 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1198 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1210 file_util::CreateDirectory(dir_name_from); 1199 file_util::CreateDirectory(dir_name_from);
1211 ASSERT_TRUE(base::PathExists(dir_name_from)); 1200 ASSERT_TRUE(PathExists(dir_name_from));
1212 1201
1213 // Create a file under the directory. 1202 // Create a file under the directory.
1214 FilePath file_name_from = 1203 FilePath file_name_from =
1215 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1204 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1216 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1205 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1217 ASSERT_TRUE(base::PathExists(file_name_from)); 1206 ASSERT_TRUE(PathExists(file_name_from));
1218 1207
1219 // Create a subdirectory. 1208 // Create a subdirectory.
1220 FilePath subdir_name_from = 1209 FilePath subdir_name_from =
1221 dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); 1210 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1222 file_util::CreateDirectory(subdir_name_from); 1211 file_util::CreateDirectory(subdir_name_from);
1223 ASSERT_TRUE(base::PathExists(subdir_name_from)); 1212 ASSERT_TRUE(PathExists(subdir_name_from));
1224 1213
1225 // Create a file under the subdirectory. 1214 // Create a file under the subdirectory.
1226 FilePath file_name2_from = 1215 FilePath file_name2_from =
1227 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1216 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1228 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); 1217 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1229 ASSERT_TRUE(base::PathExists(file_name2_from)); 1218 ASSERT_TRUE(PathExists(file_name2_from));
1230 1219
1231 // Copy the directory not recursively. 1220 // Copy the directory not recursively.
1232 FilePath dir_name_to = 1221 FilePath dir_name_to =
1233 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir")); 1222 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1234 FilePath file_name_to = 1223 FilePath file_name_to =
1235 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1224 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1236 FilePath subdir_name_to = 1225 FilePath subdir_name_to =
1237 dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); 1226 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1238 1227
1239 ASSERT_FALSE(base::PathExists(dir_name_to)); 1228 ASSERT_FALSE(PathExists(dir_name_to));
1240 1229
1241 EXPECT_TRUE(base::CopyDirectory(dir_name_from, dir_name_to, false)); 1230 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
1242 1231
1243 // Check everything has been copied. 1232 // Check everything has been copied.
1244 EXPECT_TRUE(base::PathExists(dir_name_from)); 1233 EXPECT_TRUE(PathExists(dir_name_from));
1245 EXPECT_TRUE(base::PathExists(file_name_from)); 1234 EXPECT_TRUE(PathExists(file_name_from));
1246 EXPECT_TRUE(base::PathExists(subdir_name_from)); 1235 EXPECT_TRUE(PathExists(subdir_name_from));
1247 EXPECT_TRUE(base::PathExists(file_name2_from)); 1236 EXPECT_TRUE(PathExists(file_name2_from));
1248 EXPECT_TRUE(base::PathExists(dir_name_to)); 1237 EXPECT_TRUE(PathExists(dir_name_to));
1249 EXPECT_TRUE(base::PathExists(file_name_to)); 1238 EXPECT_TRUE(PathExists(file_name_to));
1250 EXPECT_FALSE(base::PathExists(subdir_name_to)); 1239 EXPECT_FALSE(PathExists(subdir_name_to));
1251 } 1240 }
1252 1241
1253 TEST_F(FileUtilTest, CopyDirectoryExists) { 1242 TEST_F(FileUtilTest, CopyDirectoryExists) {
1254 // Create a directory. 1243 // Create a directory.
1255 FilePath dir_name_from = 1244 FilePath dir_name_from =
1256 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1245 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1257 file_util::CreateDirectory(dir_name_from); 1246 file_util::CreateDirectory(dir_name_from);
1258 ASSERT_TRUE(base::PathExists(dir_name_from)); 1247 ASSERT_TRUE(PathExists(dir_name_from));
1259 1248
1260 // Create a file under the directory. 1249 // Create a file under the directory.
1261 FilePath file_name_from = 1250 FilePath file_name_from =
1262 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1251 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1263 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1252 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1264 ASSERT_TRUE(base::PathExists(file_name_from)); 1253 ASSERT_TRUE(PathExists(file_name_from));
1265 1254
1266 // Create a subdirectory. 1255 // Create a subdirectory.
1267 FilePath subdir_name_from = 1256 FilePath subdir_name_from =
1268 dir_name_from.Append(FILE_PATH_LITERAL("Subdir")); 1257 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
1269 file_util::CreateDirectory(subdir_name_from); 1258 file_util::CreateDirectory(subdir_name_from);
1270 ASSERT_TRUE(base::PathExists(subdir_name_from)); 1259 ASSERT_TRUE(PathExists(subdir_name_from));
1271 1260
1272 // Create a file under the subdirectory. 1261 // Create a file under the subdirectory.
1273 FilePath file_name2_from = 1262 FilePath file_name2_from =
1274 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1263 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1275 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); 1264 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
1276 ASSERT_TRUE(base::PathExists(file_name2_from)); 1265 ASSERT_TRUE(PathExists(file_name2_from));
1277 1266
1278 // Copy the directory not recursively. 1267 // Copy the directory not recursively.
1279 FilePath dir_name_to = 1268 FilePath dir_name_to =
1280 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir")); 1269 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1281 FilePath file_name_to = 1270 FilePath file_name_to =
1282 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1271 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1283 FilePath subdir_name_to = 1272 FilePath subdir_name_to =
1284 dir_name_to.Append(FILE_PATH_LITERAL("Subdir")); 1273 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
1285 1274
1286 // Create the destination directory. 1275 // Create the destination directory.
1287 file_util::CreateDirectory(dir_name_to); 1276 file_util::CreateDirectory(dir_name_to);
1288 ASSERT_TRUE(base::PathExists(dir_name_to)); 1277 ASSERT_TRUE(PathExists(dir_name_to));
1289 1278
1290 EXPECT_TRUE(base::CopyDirectory(dir_name_from, dir_name_to, false)); 1279 EXPECT_TRUE(CopyDirectory(dir_name_from, dir_name_to, false));
1291 1280
1292 // Check everything has been copied. 1281 // Check everything has been copied.
1293 EXPECT_TRUE(base::PathExists(dir_name_from)); 1282 EXPECT_TRUE(PathExists(dir_name_from));
1294 EXPECT_TRUE(base::PathExists(file_name_from)); 1283 EXPECT_TRUE(PathExists(file_name_from));
1295 EXPECT_TRUE(base::PathExists(subdir_name_from)); 1284 EXPECT_TRUE(PathExists(subdir_name_from));
1296 EXPECT_TRUE(base::PathExists(file_name2_from)); 1285 EXPECT_TRUE(PathExists(file_name2_from));
1297 EXPECT_TRUE(base::PathExists(dir_name_to)); 1286 EXPECT_TRUE(PathExists(dir_name_to));
1298 EXPECT_TRUE(base::PathExists(file_name_to)); 1287 EXPECT_TRUE(PathExists(file_name_to));
1299 EXPECT_FALSE(base::PathExists(subdir_name_to)); 1288 EXPECT_FALSE(PathExists(subdir_name_to));
1300 } 1289 }
1301 1290
1302 TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) { 1291 TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToNew) {
1303 // Create a file 1292 // Create a file
1304 FilePath file_name_from = 1293 FilePath file_name_from =
1305 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1294 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1306 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1295 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1307 ASSERT_TRUE(base::PathExists(file_name_from)); 1296 ASSERT_TRUE(PathExists(file_name_from));
1308 1297
1309 // The destination name 1298 // The destination name
1310 FilePath file_name_to = temp_dir_.path().Append( 1299 FilePath file_name_to = temp_dir_.path().Append(
1311 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); 1300 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1312 ASSERT_FALSE(base::PathExists(file_name_to)); 1301 ASSERT_FALSE(PathExists(file_name_to));
1313 1302
1314 EXPECT_TRUE(base::CopyDirectory(file_name_from, file_name_to, true)); 1303 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
1315 1304
1316 // Check the has been copied 1305 // Check the has been copied
1317 EXPECT_TRUE(base::PathExists(file_name_to)); 1306 EXPECT_TRUE(PathExists(file_name_to));
1318 } 1307 }
1319 1308
1320 TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) { 1309 TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExisting) {
1321 // Create a file 1310 // Create a file
1322 FilePath file_name_from = 1311 FilePath file_name_from =
1323 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1312 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1324 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1313 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1325 ASSERT_TRUE(base::PathExists(file_name_from)); 1314 ASSERT_TRUE(PathExists(file_name_from));
1326 1315
1327 // The destination name 1316 // The destination name
1328 FilePath file_name_to = temp_dir_.path().Append( 1317 FilePath file_name_to = temp_dir_.path().Append(
1329 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt")); 1318 FILE_PATH_LITERAL("Copy_Test_File_Destination.txt"));
1330 CreateTextFile(file_name_to, L"Old file content"); 1319 CreateTextFile(file_name_to, L"Old file content");
1331 ASSERT_TRUE(base::PathExists(file_name_to)); 1320 ASSERT_TRUE(PathExists(file_name_to));
1332 1321
1333 EXPECT_TRUE(base::CopyDirectory(file_name_from, file_name_to, true)); 1322 EXPECT_TRUE(CopyDirectory(file_name_from, file_name_to, true));
1334 1323
1335 // Check the has been copied 1324 // Check the has been copied
1336 EXPECT_TRUE(base::PathExists(file_name_to)); 1325 EXPECT_TRUE(PathExists(file_name_to));
1337 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to)); 1326 EXPECT_TRUE(L"Gooooooooooooooooooooogle" == ReadTextFile(file_name_to));
1338 } 1327 }
1339 1328
1340 TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) { 1329 TEST_F(FileUtilTest, CopyFileWithCopyDirectoryRecursiveToExistingDirectory) {
1341 // Create a file 1330 // Create a file
1342 FilePath file_name_from = 1331 FilePath file_name_from =
1343 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1332 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1344 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1333 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1345 ASSERT_TRUE(base::PathExists(file_name_from)); 1334 ASSERT_TRUE(PathExists(file_name_from));
1346 1335
1347 // The destination 1336 // The destination
1348 FilePath dir_name_to = 1337 FilePath dir_name_to =
1349 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination")); 1338 temp_dir_.path().Append(FILE_PATH_LITERAL("Destination"));
1350 file_util::CreateDirectory(dir_name_to); 1339 file_util::CreateDirectory(dir_name_to);
1351 ASSERT_TRUE(base::PathExists(dir_name_to)); 1340 ASSERT_TRUE(PathExists(dir_name_to));
1352 FilePath file_name_to = 1341 FilePath file_name_to =
1353 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1342 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1354 1343
1355 EXPECT_TRUE(base::CopyDirectory(file_name_from, dir_name_to, true)); 1344 EXPECT_TRUE(CopyDirectory(file_name_from, dir_name_to, true));
1356 1345
1357 // Check the has been copied 1346 // Check the has been copied
1358 EXPECT_TRUE(base::PathExists(file_name_to)); 1347 EXPECT_TRUE(PathExists(file_name_to));
1359 } 1348 }
1360 1349
1361 TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) { 1350 TEST_F(FileUtilTest, CopyDirectoryWithTrailingSeparators) {
1362 // Create a directory. 1351 // Create a directory.
1363 FilePath dir_name_from = 1352 FilePath dir_name_from =
1364 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1353 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1365 file_util::CreateDirectory(dir_name_from); 1354 file_util::CreateDirectory(dir_name_from);
1366 ASSERT_TRUE(base::PathExists(dir_name_from)); 1355 ASSERT_TRUE(PathExists(dir_name_from));
1367 1356
1368 // Create a file under the directory. 1357 // Create a file under the directory.
1369 FilePath file_name_from = 1358 FilePath file_name_from =
1370 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1359 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1371 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1360 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1372 ASSERT_TRUE(base::PathExists(file_name_from)); 1361 ASSERT_TRUE(PathExists(file_name_from));
1373 1362
1374 // Copy the directory recursively. 1363 // Copy the directory recursively.
1375 FilePath dir_name_to = 1364 FilePath dir_name_to =
1376 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir")); 1365 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
1377 FilePath file_name_to = 1366 FilePath file_name_to =
1378 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1367 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1379 1368
1380 // Create from path with trailing separators. 1369 // Create from path with trailing separators.
1381 #if defined(OS_WIN) 1370 #if defined(OS_WIN)
1382 FilePath from_path = 1371 FilePath from_path =
1383 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\")); 1372 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir\\\\\\"));
1384 #elif defined (OS_POSIX) 1373 #elif defined (OS_POSIX)
1385 FilePath from_path = 1374 FilePath from_path =
1386 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///")); 1375 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir///"));
1387 #endif 1376 #endif
1388 1377
1389 EXPECT_TRUE(base::CopyDirectory(from_path, dir_name_to, true)); 1378 EXPECT_TRUE(CopyDirectory(from_path, dir_name_to, true));
1390 1379
1391 // Check everything has been copied. 1380 // Check everything has been copied.
1392 EXPECT_TRUE(base::PathExists(dir_name_from)); 1381 EXPECT_TRUE(PathExists(dir_name_from));
1393 EXPECT_TRUE(base::PathExists(file_name_from)); 1382 EXPECT_TRUE(PathExists(file_name_from));
1394 EXPECT_TRUE(base::PathExists(dir_name_to)); 1383 EXPECT_TRUE(PathExists(dir_name_to));
1395 EXPECT_TRUE(base::PathExists(file_name_to)); 1384 EXPECT_TRUE(PathExists(file_name_to));
1396 } 1385 }
1397 1386
1398 TEST_F(FileUtilTest, CopyFile) { 1387 TEST_F(FileUtilTest, CopyFile) {
1399 // Create a directory 1388 // Create a directory
1400 FilePath dir_name_from = 1389 FilePath dir_name_from =
1401 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir")); 1390 temp_dir_.path().Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
1402 file_util::CreateDirectory(dir_name_from); 1391 file_util::CreateDirectory(dir_name_from);
1403 ASSERT_TRUE(base::PathExists(dir_name_from)); 1392 ASSERT_TRUE(PathExists(dir_name_from));
1404 1393
1405 // Create a file under the directory 1394 // Create a file under the directory
1406 FilePath file_name_from = 1395 FilePath file_name_from =
1407 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt")); 1396 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
1408 const std::wstring file_contents(L"Gooooooooooooooooooooogle"); 1397 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
1409 CreateTextFile(file_name_from, file_contents); 1398 CreateTextFile(file_name_from, file_contents);
1410 ASSERT_TRUE(base::PathExists(file_name_from)); 1399 ASSERT_TRUE(PathExists(file_name_from));
1411 1400
1412 // Copy the file. 1401 // Copy the file.
1413 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt")); 1402 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
1414 ASSERT_TRUE(base::CopyFile(file_name_from, dest_file)); 1403 ASSERT_TRUE(CopyFile(file_name_from, dest_file));
1415 1404
1416 // Copy the file to another location using '..' in the path. 1405 // Copy the file to another location using '..' in the path.
1417 FilePath dest_file2(dir_name_from); 1406 FilePath dest_file2(dir_name_from);
1418 dest_file2 = dest_file2.AppendASCII(".."); 1407 dest_file2 = dest_file2.AppendASCII("..");
1419 dest_file2 = dest_file2.AppendASCII("DestFile.txt"); 1408 dest_file2 = dest_file2.AppendASCII("DestFile.txt");
1420 ASSERT_FALSE(base::CopyFile(file_name_from, dest_file2)); 1409 ASSERT_FALSE(CopyFile(file_name_from, dest_file2));
1421 ASSERT_TRUE(base::internal::CopyFileUnsafe(file_name_from, dest_file2)); 1410 ASSERT_TRUE(internal::CopyFileUnsafe(file_name_from, dest_file2));
1422 1411
1423 FilePath dest_file2_test(dir_name_from); 1412 FilePath dest_file2_test(dir_name_from);
1424 dest_file2_test = dest_file2_test.DirName(); 1413 dest_file2_test = dest_file2_test.DirName();
1425 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt"); 1414 dest_file2_test = dest_file2_test.AppendASCII("DestFile.txt");
1426 1415
1427 // Check everything has been copied. 1416 // Check everything has been copied.
1428 EXPECT_TRUE(base::PathExists(file_name_from)); 1417 EXPECT_TRUE(PathExists(file_name_from));
1429 EXPECT_TRUE(base::PathExists(dest_file)); 1418 EXPECT_TRUE(PathExists(dest_file));
1430 const std::wstring read_contents = ReadTextFile(dest_file); 1419 const std::wstring read_contents = ReadTextFile(dest_file);
1431 EXPECT_EQ(file_contents, read_contents); 1420 EXPECT_EQ(file_contents, read_contents);
1432 EXPECT_TRUE(base::PathExists(dest_file2_test)); 1421 EXPECT_TRUE(PathExists(dest_file2_test));
1433 EXPECT_TRUE(base::PathExists(dest_file2)); 1422 EXPECT_TRUE(PathExists(dest_file2));
1434 } 1423 }
1435 1424
1436 // file_util winds up using autoreleased objects on the Mac, so this needs 1425 // file_util winds up using autoreleased objects on the Mac, so this needs
1437 // to be a PlatformTest. 1426 // to be a PlatformTest.
1438 typedef PlatformTest ReadOnlyFileUtilTest; 1427 typedef PlatformTest ReadOnlyFileUtilTest;
1439 1428
1440 TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { 1429 TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
1441 FilePath data_dir; 1430 FilePath data_dir;
1442 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir)); 1431 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
1443 data_dir = data_dir.AppendASCII("file_util"); 1432 data_dir = data_dir.AppendASCII("file_util");
1444 ASSERT_TRUE(base::PathExists(data_dir)); 1433 ASSERT_TRUE(PathExists(data_dir));
1445 1434
1446 FilePath original_file = 1435 FilePath original_file =
1447 data_dir.Append(FILE_PATH_LITERAL("original.txt")); 1436 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1448 FilePath same_file = 1437 FilePath same_file =
1449 data_dir.Append(FILE_PATH_LITERAL("same.txt")); 1438 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1450 FilePath same_length_file = 1439 FilePath same_length_file =
1451 data_dir.Append(FILE_PATH_LITERAL("same_length.txt")); 1440 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
1452 FilePath different_file = 1441 FilePath different_file =
1453 data_dir.Append(FILE_PATH_LITERAL("different.txt")); 1442 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
1454 FilePath different_first_file = 1443 FilePath different_first_file =
(...skipping 23 matching lines...) Expand all
1478 EXPECT_FALSE(ContentsEqual(original_file, different_last_file)); 1467 EXPECT_FALSE(ContentsEqual(original_file, different_last_file));
1479 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file)); 1468 EXPECT_TRUE(ContentsEqual(empty1_file, empty2_file));
1480 EXPECT_FALSE(ContentsEqual(original_file, shortened_file)); 1469 EXPECT_FALSE(ContentsEqual(original_file, shortened_file));
1481 EXPECT_FALSE(ContentsEqual(shortened_file, original_file)); 1470 EXPECT_FALSE(ContentsEqual(shortened_file, original_file));
1482 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same)); 1471 EXPECT_TRUE(ContentsEqual(binary_file, binary_file_same));
1483 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff)); 1472 EXPECT_FALSE(ContentsEqual(binary_file, binary_file_diff));
1484 } 1473 }
1485 1474
1486 TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) { 1475 TEST_F(ReadOnlyFileUtilTest, TextContentsEqual) {
1487 FilePath data_dir; 1476 FilePath data_dir;
1488 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir)); 1477 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
1489 data_dir = data_dir.AppendASCII("file_util"); 1478 data_dir = data_dir.AppendASCII("file_util");
1490 ASSERT_TRUE(base::PathExists(data_dir)); 1479 ASSERT_TRUE(PathExists(data_dir));
1491 1480
1492 FilePath original_file = 1481 FilePath original_file =
1493 data_dir.Append(FILE_PATH_LITERAL("original.txt")); 1482 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
1494 FilePath same_file = 1483 FilePath same_file =
1495 data_dir.Append(FILE_PATH_LITERAL("same.txt")); 1484 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
1496 FilePath crlf_file = 1485 FilePath crlf_file =
1497 data_dir.Append(FILE_PATH_LITERAL("crlf.txt")); 1486 data_dir.Append(FILE_PATH_LITERAL("crlf.txt"));
1498 FilePath shortened_file = 1487 FilePath shortened_file =
1499 data_dir.Append(FILE_PATH_LITERAL("shortened.txt")); 1488 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
1500 FilePath different_file = 1489 FilePath different_file =
(...skipping 27 matching lines...) Expand all
1528 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file)); 1517 EXPECT_TRUE(TextContentsEqual(blank_line_file, blank_line_crlf_file));
1529 } 1518 }
1530 1519
1531 // We don't need equivalent functionality outside of Windows. 1520 // We don't need equivalent functionality outside of Windows.
1532 #if defined(OS_WIN) 1521 #if defined(OS_WIN)
1533 TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) { 1522 TEST_F(FileUtilTest, CopyAndDeleteDirectoryTest) {
1534 // Create a directory 1523 // Create a directory
1535 FilePath dir_name_from = 1524 FilePath dir_name_from =
1536 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir")); 1525 temp_dir_.path().Append(FILE_PATH_LITERAL("CopyAndDelete_From_Subdir"));
1537 file_util::CreateDirectory(dir_name_from); 1526 file_util::CreateDirectory(dir_name_from);
1538 ASSERT_TRUE(base::PathExists(dir_name_from)); 1527 ASSERT_TRUE(PathExists(dir_name_from));
1539 1528
1540 // Create a file under the directory 1529 // Create a file under the directory
1541 FilePath file_name_from = 1530 FilePath file_name_from =
1542 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt")); 1531 dir_name_from.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1543 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 1532 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
1544 ASSERT_TRUE(base::PathExists(file_name_from)); 1533 ASSERT_TRUE(PathExists(file_name_from));
1545 1534
1546 // Move the directory by using CopyAndDeleteDirectory 1535 // Move the directory by using CopyAndDeleteDirectory
1547 FilePath dir_name_to = temp_dir_.path().Append( 1536 FilePath dir_name_to = temp_dir_.path().Append(
1548 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir")); 1537 FILE_PATH_LITERAL("CopyAndDelete_To_Subdir"));
1549 FilePath file_name_to = 1538 FilePath file_name_to =
1550 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt")); 1539 dir_name_to.Append(FILE_PATH_LITERAL("CopyAndDelete_Test_File.txt"));
1551 1540
1552 ASSERT_FALSE(base::PathExists(dir_name_to)); 1541 ASSERT_FALSE(PathExists(dir_name_to));
1553 1542
1554 EXPECT_TRUE(base::internal::CopyAndDeleteDirectory(dir_name_from, 1543 EXPECT_TRUE(internal::CopyAndDeleteDirectory(dir_name_from,
1555 dir_name_to)); 1544 dir_name_to));
1556 1545
1557 // Check everything has been moved. 1546 // Check everything has been moved.
1558 EXPECT_FALSE(base::PathExists(dir_name_from)); 1547 EXPECT_FALSE(PathExists(dir_name_from));
1559 EXPECT_FALSE(base::PathExists(file_name_from)); 1548 EXPECT_FALSE(PathExists(file_name_from));
1560 EXPECT_TRUE(base::PathExists(dir_name_to)); 1549 EXPECT_TRUE(PathExists(dir_name_to));
1561 EXPECT_TRUE(base::PathExists(file_name_to)); 1550 EXPECT_TRUE(PathExists(file_name_to));
1562 } 1551 }
1563 1552
1564 TEST_F(FileUtilTest, GetTempDirTest) { 1553 TEST_F(FileUtilTest, GetTempDirTest) {
1565 static const TCHAR* kTmpKey = _T("TMP"); 1554 static const TCHAR* kTmpKey = _T("TMP");
1566 static const TCHAR* kTmpValues[] = { 1555 static const TCHAR* kTmpValues[] = {
1567 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\") 1556 _T(""), _T("C:"), _T("C:\\"), _T("C:\\tmp"), _T("C:\\tmp\\")
1568 }; 1557 };
1569 // Save the original $TMP. 1558 // Save the original $TMP.
1570 size_t original_tmp_size; 1559 size_t original_tmp_size;
1571 TCHAR* original_tmp; 1560 TCHAR* original_tmp;
(...skipping 15 matching lines...) Expand all
1587 } else { 1576 } else {
1588 ::_tputenv_s(kTmpKey, _T("")); 1577 ::_tputenv_s(kTmpKey, _T(""));
1589 } 1578 }
1590 } 1579 }
1591 #endif // OS_WIN 1580 #endif // OS_WIN
1592 1581
1593 TEST_F(FileUtilTest, CreateTemporaryFileTest) { 1582 TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1594 FilePath temp_files[3]; 1583 FilePath temp_files[3];
1595 for (int i = 0; i < 3; i++) { 1584 for (int i = 0; i < 3; i++) {
1596 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i]))); 1585 ASSERT_TRUE(file_util::CreateTemporaryFile(&(temp_files[i])));
1597 EXPECT_TRUE(base::PathExists(temp_files[i])); 1586 EXPECT_TRUE(PathExists(temp_files[i]));
1598 EXPECT_FALSE(DirectoryExists(temp_files[i])); 1587 EXPECT_FALSE(DirectoryExists(temp_files[i]));
1599 } 1588 }
1600 for (int i = 0; i < 3; i++) 1589 for (int i = 0; i < 3; i++)
1601 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); 1590 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1602 for (int i = 0; i < 3; i++) 1591 for (int i = 0; i < 3; i++)
1603 EXPECT_TRUE(base::DeleteFile(temp_files[i], false)); 1592 EXPECT_TRUE(DeleteFile(temp_files[i], false));
1604 } 1593 }
1605 1594
1606 TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) { 1595 TEST_F(FileUtilTest, CreateAndOpenTemporaryFileTest) {
1607 FilePath names[3]; 1596 FilePath names[3];
1608 FILE* fps[3]; 1597 FILE* fps[3];
1609 int i; 1598 int i;
1610 1599
1611 // Create; make sure they are open and exist. 1600 // Create; make sure they are open and exist.
1612 for (i = 0; i < 3; ++i) { 1601 for (i = 0; i < 3; ++i) {
1613 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i])); 1602 fps[i] = file_util::CreateAndOpenTemporaryFile(&(names[i]));
1614 ASSERT_TRUE(fps[i]); 1603 ASSERT_TRUE(fps[i]);
1615 EXPECT_TRUE(base::PathExists(names[i])); 1604 EXPECT_TRUE(PathExists(names[i]));
1616 } 1605 }
1617 1606
1618 // Make sure all names are unique. 1607 // Make sure all names are unique.
1619 for (i = 0; i < 3; ++i) { 1608 for (i = 0; i < 3; ++i) {
1620 EXPECT_FALSE(names[i] == names[(i+1)%3]); 1609 EXPECT_FALSE(names[i] == names[(i+1)%3]);
1621 } 1610 }
1622 1611
1623 // Close and delete. 1612 // Close and delete.
1624 for (i = 0; i < 3; ++i) { 1613 for (i = 0; i < 3; ++i) {
1625 EXPECT_TRUE(file_util::CloseFile(fps[i])); 1614 EXPECT_TRUE(file_util::CloseFile(fps[i]));
1626 EXPECT_TRUE(base::DeleteFile(names[i], false)); 1615 EXPECT_TRUE(DeleteFile(names[i], false));
1627 } 1616 }
1628 } 1617 }
1629 1618
1630 TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { 1619 TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
1631 FilePath temp_dir; 1620 FilePath temp_dir;
1632 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(), 1621 ASSERT_TRUE(file_util::CreateNewTempDirectory(FilePath::StringType(),
1633 &temp_dir)); 1622 &temp_dir));
1634 EXPECT_TRUE(base::PathExists(temp_dir)); 1623 EXPECT_TRUE(PathExists(temp_dir));
1635 EXPECT_TRUE(base::DeleteFile(temp_dir, false)); 1624 EXPECT_TRUE(DeleteFile(temp_dir, false));
1636 } 1625 }
1637 1626
1638 TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) { 1627 TEST_F(FileUtilTest, CreateNewTemporaryDirInDirTest) {
1639 FilePath new_dir; 1628 FilePath new_dir;
1640 ASSERT_TRUE(file_util::CreateTemporaryDirInDir( 1629 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(
1641 temp_dir_.path(), 1630 temp_dir_.path(),
1642 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"), 1631 FILE_PATH_LITERAL("CreateNewTemporaryDirInDirTest"),
1643 &new_dir)); 1632 &new_dir));
1644 EXPECT_TRUE(base::PathExists(new_dir)); 1633 EXPECT_TRUE(PathExists(new_dir));
1645 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir)); 1634 EXPECT_TRUE(temp_dir_.path().IsParent(new_dir));
1646 EXPECT_TRUE(base::DeleteFile(new_dir, false)); 1635 EXPECT_TRUE(DeleteFile(new_dir, false));
1647 } 1636 }
1648 1637
1649 TEST_F(FileUtilTest, GetShmemTempDirTest) { 1638 TEST_F(FileUtilTest, GetShmemTempDirTest) {
1650 FilePath dir; 1639 FilePath dir;
1651 EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false)); 1640 EXPECT_TRUE(file_util::GetShmemTempDir(&dir, false));
1652 EXPECT_TRUE(DirectoryExists(dir)); 1641 EXPECT_TRUE(DirectoryExists(dir));
1653 } 1642 }
1654 1643
1655 TEST_F(FileUtilTest, CreateDirectoryTest) { 1644 TEST_F(FileUtilTest, CreateDirectoryTest) {
1656 FilePath test_root = 1645 FilePath test_root =
1657 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test")); 1646 temp_dir_.path().Append(FILE_PATH_LITERAL("create_directory_test"));
1658 #if defined(OS_WIN) 1647 #if defined(OS_WIN)
1659 FilePath test_path = 1648 FilePath test_path =
1660 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\")); 1649 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
1661 #elif defined(OS_POSIX) 1650 #elif defined(OS_POSIX)
1662 FilePath test_path = 1651 FilePath test_path =
1663 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/")); 1652 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
1664 #endif 1653 #endif
1665 1654
1666 EXPECT_FALSE(base::PathExists(test_path)); 1655 EXPECT_FALSE(PathExists(test_path));
1667 EXPECT_TRUE(file_util::CreateDirectory(test_path)); 1656 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1668 EXPECT_TRUE(base::PathExists(test_path)); 1657 EXPECT_TRUE(PathExists(test_path));
1669 // CreateDirectory returns true if the DirectoryExists returns true. 1658 // CreateDirectory returns true if the DirectoryExists returns true.
1670 EXPECT_TRUE(file_util::CreateDirectory(test_path)); 1659 EXPECT_TRUE(file_util::CreateDirectory(test_path));
1671 1660
1672 // Doesn't work to create it on top of a non-dir 1661 // Doesn't work to create it on top of a non-dir
1673 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt")); 1662 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
1674 EXPECT_FALSE(base::PathExists(test_path)); 1663 EXPECT_FALSE(PathExists(test_path));
1675 CreateTextFile(test_path, L"test file"); 1664 CreateTextFile(test_path, L"test file");
1676 EXPECT_TRUE(base::PathExists(test_path)); 1665 EXPECT_TRUE(PathExists(test_path));
1677 EXPECT_FALSE(file_util::CreateDirectory(test_path)); 1666 EXPECT_FALSE(file_util::CreateDirectory(test_path));
1678 1667
1679 EXPECT_TRUE(base::DeleteFile(test_root, true)); 1668 EXPECT_TRUE(DeleteFile(test_root, true));
1680 EXPECT_FALSE(base::PathExists(test_root)); 1669 EXPECT_FALSE(PathExists(test_root));
1681 EXPECT_FALSE(base::PathExists(test_path)); 1670 EXPECT_FALSE(PathExists(test_path));
1682 1671
1683 // Verify assumptions made by the Windows implementation: 1672 // Verify assumptions made by the Windows implementation:
1684 // 1. The current directory always exists. 1673 // 1. The current directory always exists.
1685 // 2. The root directory always exists. 1674 // 2. The root directory always exists.
1686 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory))); 1675 ASSERT_TRUE(DirectoryExists(FilePath(FilePath::kCurrentDirectory)));
1687 FilePath top_level = test_root; 1676 FilePath top_level = test_root;
1688 while (top_level != top_level.DirName()) { 1677 while (top_level != top_level.DirName()) {
1689 top_level = top_level.DirName(); 1678 top_level = top_level.DirName();
1690 } 1679 }
1691 ASSERT_TRUE(DirectoryExists(top_level)); 1680 ASSERT_TRUE(DirectoryExists(top_level));
1692 1681
1693 // Given these assumptions hold, it should be safe to 1682 // Given these assumptions hold, it should be safe to
1694 // test that "creating" these directories succeeds. 1683 // test that "creating" these directories succeeds.
1695 EXPECT_TRUE(file_util::CreateDirectory( 1684 EXPECT_TRUE(file_util::CreateDirectory(
1696 FilePath(FilePath::kCurrentDirectory))); 1685 FilePath(FilePath::kCurrentDirectory)));
1697 EXPECT_TRUE(file_util::CreateDirectory(top_level)); 1686 EXPECT_TRUE(file_util::CreateDirectory(top_level));
1698 1687
1699 #if defined(OS_WIN) 1688 #if defined(OS_WIN)
1700 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\")); 1689 FilePath invalid_drive(FILE_PATH_LITERAL("o:\\"));
1701 FilePath invalid_path = 1690 FilePath invalid_path =
1702 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir")); 1691 invalid_drive.Append(FILE_PATH_LITERAL("some\\inaccessible\\dir"));
1703 if (!base::PathExists(invalid_drive)) { 1692 if (!PathExists(invalid_drive)) {
1704 EXPECT_FALSE(file_util::CreateDirectory(invalid_path)); 1693 EXPECT_FALSE(file_util::CreateDirectory(invalid_path));
1705 } 1694 }
1706 #endif 1695 #endif
1707 } 1696 }
1708 1697
1709 TEST_F(FileUtilTest, DetectDirectoryTest) { 1698 TEST_F(FileUtilTest, DetectDirectoryTest) {
1710 // Check a directory 1699 // Check a directory
1711 FilePath test_root = 1700 FilePath test_root =
1712 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test")); 1701 temp_dir_.path().Append(FILE_PATH_LITERAL("detect_directory_test"));
1713 EXPECT_FALSE(base::PathExists(test_root)); 1702 EXPECT_FALSE(PathExists(test_root));
1714 EXPECT_TRUE(file_util::CreateDirectory(test_root)); 1703 EXPECT_TRUE(file_util::CreateDirectory(test_root));
1715 EXPECT_TRUE(base::PathExists(test_root)); 1704 EXPECT_TRUE(PathExists(test_root));
1716 EXPECT_TRUE(DirectoryExists(test_root)); 1705 EXPECT_TRUE(DirectoryExists(test_root));
1717 // Check a file 1706 // Check a file
1718 FilePath test_path = 1707 FilePath test_path =
1719 test_root.Append(FILE_PATH_LITERAL("foobar.txt")); 1708 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
1720 EXPECT_FALSE(base::PathExists(test_path)); 1709 EXPECT_FALSE(PathExists(test_path));
1721 CreateTextFile(test_path, L"test file"); 1710 CreateTextFile(test_path, L"test file");
1722 EXPECT_TRUE(base::PathExists(test_path)); 1711 EXPECT_TRUE(PathExists(test_path));
1723 EXPECT_FALSE(DirectoryExists(test_path)); 1712 EXPECT_FALSE(DirectoryExists(test_path));
1724 EXPECT_TRUE(base::DeleteFile(test_path, false)); 1713 EXPECT_TRUE(DeleteFile(test_path, false));
1725 1714
1726 EXPECT_TRUE(base::DeleteFile(test_root, true)); 1715 EXPECT_TRUE(DeleteFile(test_root, true));
1727 } 1716 }
1728 1717
1729 TEST_F(FileUtilTest, FileEnumeratorTest) { 1718 TEST_F(FileUtilTest, FileEnumeratorTest) {
1730 // Test an empty directory. 1719 // Test an empty directory.
1731 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES); 1720 FileEnumerator f0(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1732 EXPECT_EQ(f0.Next().value(), FPL("")); 1721 EXPECT_EQ(f0.Next().value(), FPL(""));
1733 EXPECT_EQ(f0.Next().value(), FPL("")); 1722 EXPECT_EQ(f0.Next().value(), FPL(""));
1734 1723
1735 // Test an empty directory, non-recursively, including "..". 1724 // Test an empty directory, non-recursively, including "..".
1736 FileEnumerator f0_dotdot(temp_dir_.path(), false, 1725 FileEnumerator f0_dotdot(temp_dir_.path(), false,
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
1824 EXPECT_TRUE(c5.HasFile(dir2inner)); 1813 EXPECT_TRUE(c5.HasFile(dir2inner));
1825 EXPECT_TRUE(c5.HasFile(dir2innerfile)); 1814 EXPECT_TRUE(c5.HasFile(dir2innerfile));
1826 EXPECT_EQ(c5.size(), 5); 1815 EXPECT_EQ(c5.size(), 5);
1827 1816
1828 #if defined(OS_WIN) 1817 #if defined(OS_WIN)
1829 { 1818 {
1830 // Make dir1 point to dir2. 1819 // Make dir1 point to dir2.
1831 ReparsePoint reparse_point(dir1, dir2); 1820 ReparsePoint reparse_point(dir1, dir2);
1832 EXPECT_TRUE(reparse_point.IsValid()); 1821 EXPECT_TRUE(reparse_point.IsValid());
1833 1822
1834 if ((base::win::GetVersion() >= base::win::VERSION_VISTA)) { 1823 if ((win::GetVersion() >= base::win::VERSION_VISTA)) {
1835 // There can be a delay for the enumeration code to see the change on 1824 // There can be a delay for the enumeration code to see the change on
1836 // the file system so skip this test for XP. 1825 // the file system so skip this test for XP.
1837 // Enumerate the reparse point. 1826 // Enumerate the reparse point.
1838 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES); 1827 FileEnumerator f6(dir1, true, FILES_AND_DIRECTORIES);
1839 FindResultCollector c6(f6); 1828 FindResultCollector c6(f6);
1840 FilePath inner2 = dir1.Append(FPL("inner")); 1829 FilePath inner2 = dir1.Append(FPL("inner"));
1841 EXPECT_TRUE(c6.HasFile(inner2)); 1830 EXPECT_TRUE(c6.HasFile(inner2));
1842 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt")))); 1831 EXPECT_TRUE(c6.HasFile(inner2.Append(FPL("innerfile.txt"))));
1843 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt")))); 1832 EXPECT_TRUE(c6.HasFile(dir1.Append(FPL("dir2file.txt"))));
1844 EXPECT_EQ(c6.size(), 3); 1833 EXPECT_EQ(c6.size(), 3);
(...skipping 27 matching lines...) Expand all
1872 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES); 1861 FileEnumerator f9(temp_dir_.path(), true, FILES_AND_DIRECTORIES);
1873 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something 1862 EXPECT_FALSE(f9.Next().value().empty()); // Should have found something
1874 // (we don't care what). 1863 // (we don't care what).
1875 } 1864 }
1876 1865
1877 TEST_F(FileUtilTest, AppendToFile) { 1866 TEST_F(FileUtilTest, AppendToFile) {
1878 FilePath data_dir = 1867 FilePath data_dir =
1879 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); 1868 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1880 1869
1881 // Create a fresh, empty copy of this directory. 1870 // Create a fresh, empty copy of this directory.
1882 if (base::PathExists(data_dir)) { 1871 if (PathExists(data_dir)) {
1883 ASSERT_TRUE(base::DeleteFile(data_dir, true)); 1872 ASSERT_TRUE(DeleteFile(data_dir, true));
1884 } 1873 }
1885 ASSERT_TRUE(file_util::CreateDirectory(data_dir)); 1874 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1886 1875
1887 // Create a fresh, empty copy of this directory. 1876 // Create a fresh, empty copy of this directory.
1888 if (base::PathExists(data_dir)) { 1877 if (PathExists(data_dir)) {
1889 ASSERT_TRUE(base::DeleteFile(data_dir, true)); 1878 ASSERT_TRUE(DeleteFile(data_dir, true));
1890 } 1879 }
1891 ASSERT_TRUE(file_util::CreateDirectory(data_dir)); 1880 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1892 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); 1881 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1893 1882
1894 std::string data("hello"); 1883 std::string data("hello");
1895 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length())); 1884 EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length()));
1896 EXPECT_EQ(static_cast<int>(data.length()), 1885 EXPECT_EQ(static_cast<int>(data.length()),
1897 file_util::WriteFile(foobar, data.c_str(), data.length())); 1886 file_util::WriteFile(foobar, data.c_str(), data.length()));
1898 EXPECT_EQ(static_cast<int>(data.length()), 1887 EXPECT_EQ(static_cast<int>(data.length()),
1899 file_util::AppendToFile(foobar, data.c_str(), data.length())); 1888 file_util::AppendToFile(foobar, data.c_str(), data.length()));
1900 1889
1901 const std::wstring read_content = ReadTextFile(foobar); 1890 const std::wstring read_content = ReadTextFile(foobar);
1902 EXPECT_EQ(L"hellohello", read_content); 1891 EXPECT_EQ(L"hellohello", read_content);
1903 } 1892 }
1904 1893
1905 TEST_F(FileUtilTest, TouchFile) { 1894 TEST_F(FileUtilTest, TouchFile) {
1906 FilePath data_dir = 1895 FilePath data_dir =
1907 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); 1896 temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
1908 1897
1909 // Create a fresh, empty copy of this directory. 1898 // Create a fresh, empty copy of this directory.
1910 if (base::PathExists(data_dir)) { 1899 if (PathExists(data_dir)) {
1911 ASSERT_TRUE(base::DeleteFile(data_dir, true)); 1900 ASSERT_TRUE(DeleteFile(data_dir, true));
1912 } 1901 }
1913 ASSERT_TRUE(file_util::CreateDirectory(data_dir)); 1902 ASSERT_TRUE(file_util::CreateDirectory(data_dir));
1914 1903
1915 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); 1904 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1916 std::string data("hello"); 1905 std::string data("hello");
1917 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length())); 1906 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1918 1907
1919 base::Time access_time; 1908 Time access_time;
1920 // This timestamp is divisible by one day (in local timezone), 1909 // This timestamp is divisible by one day (in local timezone),
1921 // to make it work on FAT too. 1910 // to make it work on FAT too.
1922 ASSERT_TRUE(base::Time::FromString("Wed, 16 Nov 1994, 00:00:00", 1911 ASSERT_TRUE(Time::FromString("Wed, 16 Nov 1994, 00:00:00",
1923 &access_time)); 1912 &access_time));
1924 1913
1925 base::Time modification_time; 1914 Time modification_time;
1926 // Note that this timestamp is divisible by two (seconds) - FAT stores 1915 // Note that this timestamp is divisible by two (seconds) - FAT stores
1927 // modification times with 2s resolution. 1916 // modification times with 2s resolution.
1928 ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", 1917 ASSERT_TRUE(Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
1929 &modification_time)); 1918 &modification_time));
1930 1919
1931 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time)); 1920 ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
1932 base::PlatformFileInfo file_info; 1921 PlatformFileInfo file_info;
1933 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info)); 1922 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
1934 EXPECT_EQ(file_info.last_accessed.ToInternalValue(), 1923 EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
1935 access_time.ToInternalValue()); 1924 access_time.ToInternalValue());
1936 EXPECT_EQ(file_info.last_modified.ToInternalValue(), 1925 EXPECT_EQ(file_info.last_modified.ToInternalValue(),
1937 modification_time.ToInternalValue()); 1926 modification_time.ToInternalValue());
1938 } 1927 }
1939 1928
1940 TEST_F(FileUtilTest, IsDirectoryEmpty) { 1929 TEST_F(FileUtilTest, IsDirectoryEmpty) {
1941 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir")); 1930 FilePath empty_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("EmptyDir"));
1942 1931
1943 ASSERT_FALSE(base::PathExists(empty_dir)); 1932 ASSERT_FALSE(PathExists(empty_dir));
1944 1933
1945 ASSERT_TRUE(file_util::CreateDirectory(empty_dir)); 1934 ASSERT_TRUE(file_util::CreateDirectory(empty_dir));
1946 1935
1947 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir)); 1936 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1948 1937
1949 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); 1938 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1950 std::string bar("baz"); 1939 std::string bar("baz");
1951 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length())); 1940 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1952 1941
1953 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir)); 1942 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1991 1980
1992 ASSERT_EQ(uid_, getuid()); // This process should be the owner. 1981 ASSERT_EQ(uid_, getuid()); // This process should be the owner.
1993 1982
1994 // To ensure that umask settings do not cause the initial state 1983 // To ensure that umask settings do not cause the initial state
1995 // of permissions to be different from what we expect, explicitly 1984 // of permissions to be different from what we expect, explicitly
1996 // set permissions on the directories we create. 1985 // set permissions on the directories we create.
1997 // Make all files and directories non-world-writable. 1986 // Make all files and directories non-world-writable.
1998 1987
1999 // Users and group can read, write, traverse 1988 // Users and group can read, write, traverse
2000 int enabled_permissions = 1989 int enabled_permissions =
2001 file_util::FILE_PERMISSION_USER_MASK | 1990 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
2002 file_util::FILE_PERMISSION_GROUP_MASK;
2003 // Other users can't read, write, traverse 1991 // Other users can't read, write, traverse
2004 int disabled_permissions = 1992 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
2005 file_util::FILE_PERMISSION_OTHERS_MASK;
2006 1993
2007 ASSERT_NO_FATAL_FAILURE( 1994 ASSERT_NO_FATAL_FAILURE(
2008 ChangePosixFilePermissions( 1995 ChangePosixFilePermissions(
2009 base_dir_, enabled_permissions, disabled_permissions)); 1996 base_dir_, enabled_permissions, disabled_permissions));
2010 ASSERT_NO_FATAL_FAILURE( 1997 ASSERT_NO_FATAL_FAILURE(
2011 ChangePosixFilePermissions( 1998 ChangePosixFilePermissions(
2012 sub_dir_, enabled_permissions, disabled_permissions)); 1999 sub_dir_, enabled_permissions, disabled_permissions));
2013 } 2000 }
2014 2001
2015 FilePath base_dir_; 2002 FilePath base_dir_;
(...skipping 29 matching lines...) Expand all
2045 EXPECT_TRUE( 2032 EXPECT_TRUE(
2046 file_util::VerifyPathControlledByUser( 2033 file_util::VerifyPathControlledByUser(
2047 base_dir_, sub_dir_, uid_, ok_gids_)); 2034 base_dir_, sub_dir_, uid_, ok_gids_));
2048 } 2035 }
2049 2036
2050 TEST_F(VerifyPathControlledByUserTest, Symlinks) { 2037 TEST_F(VerifyPathControlledByUserTest, Symlinks) {
2051 // Symlinks in the path should cause failure. 2038 // Symlinks in the path should cause failure.
2052 2039
2053 // Symlink to the file at the end of the path. 2040 // Symlink to the file at the end of the path.
2054 FilePath file_link = base_dir_.AppendASCII("file_link"); 2041 FilePath file_link = base_dir_.AppendASCII("file_link");
2055 ASSERT_TRUE(file_util::CreateSymbolicLink(text_file_, file_link)) 2042 ASSERT_TRUE(CreateSymbolicLink(text_file_, file_link))
2056 << "Failed to create symlink."; 2043 << "Failed to create symlink.";
2057 2044
2058 EXPECT_FALSE( 2045 EXPECT_FALSE(
2059 file_util::VerifyPathControlledByUser( 2046 file_util::VerifyPathControlledByUser(
2060 base_dir_, file_link, uid_, ok_gids_)); 2047 base_dir_, file_link, uid_, ok_gids_));
2061 EXPECT_FALSE( 2048 EXPECT_FALSE(
2062 file_util::VerifyPathControlledByUser( 2049 file_util::VerifyPathControlledByUser(
2063 file_link, file_link, uid_, ok_gids_)); 2050 file_link, file_link, uid_, ok_gids_));
2064 2051
2065 // Symlink from one directory to another within the path. 2052 // Symlink from one directory to another within the path.
2066 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir"); 2053 FilePath link_to_sub_dir = base_dir_.AppendASCII("link_to_sub_dir");
2067 ASSERT_TRUE(file_util::CreateSymbolicLink(sub_dir_, link_to_sub_dir)) 2054 ASSERT_TRUE(CreateSymbolicLink(sub_dir_, link_to_sub_dir))
2068 << "Failed to create symlink."; 2055 << "Failed to create symlink.";
2069 2056
2070 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt"); 2057 FilePath file_path_with_link = link_to_sub_dir.AppendASCII("file.txt");
2071 ASSERT_TRUE(base::PathExists(file_path_with_link)); 2058 ASSERT_TRUE(PathExists(file_path_with_link));
2072 2059
2073 EXPECT_FALSE( 2060 EXPECT_FALSE(
2074 file_util::VerifyPathControlledByUser( 2061 file_util::VerifyPathControlledByUser(
2075 base_dir_, file_path_with_link, uid_, ok_gids_)); 2062 base_dir_, file_path_with_link, uid_, ok_gids_));
2076 2063
2077 EXPECT_FALSE( 2064 EXPECT_FALSE(
2078 file_util::VerifyPathControlledByUser( 2065 file_util::VerifyPathControlledByUser(
2079 link_to_sub_dir, file_path_with_link, uid_, ok_gids_)); 2066 link_to_sub_dir, file_path_with_link, uid_, ok_gids_));
2080 2067
2081 // Symlinks in parents of base path are allowed. 2068 // Symlinks in parents of base path are allowed.
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
2320 base_dir_, text_file_, uid_, ok_gids_)); 2307 base_dir_, text_file_, uid_, ok_gids_));
2321 EXPECT_TRUE( 2308 EXPECT_TRUE(
2322 file_util::VerifyPathControlledByUser( 2309 file_util::VerifyPathControlledByUser(
2323 sub_dir_, text_file_, uid_, ok_gids_)); 2310 sub_dir_, text_file_, uid_, ok_gids_));
2324 } 2311 }
2325 2312
2326 #if defined(OS_ANDROID) 2313 #if defined(OS_ANDROID)
2327 TEST_F(FileUtilTest, ValidContentUriTest) { 2314 TEST_F(FileUtilTest, ValidContentUriTest) {
2328 // Get the test image path. 2315 // Get the test image path.
2329 FilePath data_dir; 2316 FilePath data_dir;
2330 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir)); 2317 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
2331 data_dir = data_dir.AppendASCII("file_util"); 2318 data_dir = data_dir.AppendASCII("file_util");
2332 ASSERT_TRUE(base::PathExists(data_dir)); 2319 ASSERT_TRUE(PathExists(data_dir));
2333 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png")); 2320 FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
2334 int64 image_size; 2321 int64 image_size;
2335 file_util::GetFileSize(image_file, &image_size); 2322 file_util::GetFileSize(image_file, &image_size);
2336 EXPECT_LT(0, image_size); 2323 EXPECT_LT(0, image_size);
2337 2324
2338 // Insert the image into MediaStore. MediaStore will do some conversions, and 2325 // Insert the image into MediaStore. MediaStore will do some conversions, and
2339 // return the content URI. 2326 // return the content URI.
2340 base::FilePath path = file_util::InsertImageIntoMediaStore(image_file); 2327 FilePath path = file_util::InsertImageIntoMediaStore(image_file);
2341 EXPECT_TRUE(path.IsContentUri()); 2328 EXPECT_TRUE(path.IsContentUri());
2342 EXPECT_TRUE(base::PathExists(path)); 2329 EXPECT_TRUE(PathExists(path));
2343 // The file size may not equal to the input image as MediaStore may convert 2330 // The file size may not equal to the input image as MediaStore may convert
2344 // the image. 2331 // the image.
2345 int64 content_uri_size; 2332 int64 content_uri_size;
2346 file_util::GetFileSize(path, &content_uri_size); 2333 file_util::GetFileSize(path, &content_uri_size);
2347 EXPECT_EQ(image_size, content_uri_size); 2334 EXPECT_EQ(image_size, content_uri_size);
2348 2335
2349 // We should be able to read the file. 2336 // We should be able to read the file.
2350 char* buffer = new char[image_size]; 2337 char* buffer = new char[image_size];
2351 int fd = base::OpenContentUriForRead(path); 2338 int fd = OpenContentUriForRead(path);
2352 EXPECT_LT(0, fd); 2339 EXPECT_LT(0, fd);
2353 EXPECT_TRUE(file_util::ReadFromFD(fd, buffer, image_size)); 2340 EXPECT_TRUE(ReadFromFD(fd, buffer, image_size));
2354 delete[] buffer; 2341 delete[] buffer;
2355 } 2342 }
2356 2343
2357 TEST_F(FileUtilTest, NonExistentContentUriTest) { 2344 TEST_F(FileUtilTest, NonExistentContentUriTest) {
2358 base::FilePath path("content://foo.bar"); 2345 FilePath path("content://foo.bar");
2359 EXPECT_TRUE(path.IsContentUri()); 2346 EXPECT_TRUE(path.IsContentUri());
2360 EXPECT_FALSE(base::PathExists(path)); 2347 EXPECT_FALSE(PathExists(path));
2361 // Size should be smaller than 0. 2348 // Size should be smaller than 0.
2362 int64 size; 2349 int64 size;
2363 file_util::GetFileSize(path, &size); 2350 file_util::GetFileSize(path, &size);
2364 EXPECT_GT(0, size); 2351 EXPECT_GT(0, size);
2365 2352
2366 // We should not be able to read the file. 2353 // We should not be able to read the file.
2367 int fd = base::OpenContentUriForRead(path); 2354 int fd = OpenContentUriForRead(path);
2368 EXPECT_EQ(-1, fd); 2355 EXPECT_EQ(-1, fd);
2369 } 2356 }
2370 #endif 2357 #endif
2371 2358
2372 #endif // defined(OS_POSIX) 2359 #endif // defined(OS_POSIX)
2373 2360
2374 } // namespace 2361 } // namespace
2362
2363 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_posix.cc ('k') | base/files/file_path_watcher_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698