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

Side by Side Diff: base/file_util_unittest.cc

Issue 5349007: Some additions to support symlinks better on platforms that support them. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: removing TODO Created 10 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/platform_file.h » ('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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 <winioctl.h> 9 #include <winioctl.h>
10 #include <shellapi.h> 10 #include <shellapi.h>
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a)); 574 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_a));
575 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b)); 575 ASSERT_TRUE(DeleteReparsePoint(reparse_to_base_b));
576 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long)); 576 ASSERT_TRUE(DeleteReparsePoint(reparse_to_sub_long));
577 577
578 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")), 578 ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
579 &normalized_path)); 579 &normalized_path));
580 } 580 }
581 581
582 #endif // defined(OS_WIN) 582 #endif // defined(OS_WIN)
583 583
584 #if defined(OS_POSIX)
585
586 TEST_F(FileUtilTest, CreateAndReadSymlinks) {
587 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
588 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
589 CreateTextFile(link_to, bogus_content);
590
591 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
592 << "Failed to create file symlink.";
593
594 // If we created the link properly, we should be able to read the
595 // contents through it.
596 std::wstring contents = ReadTextFile(link_from);
597 ASSERT_EQ(contents, bogus_content);
598
599 FilePath result;
600 ASSERT_TRUE(file_util::ReadSymbolicLink(link_from, &result));
601 ASSERT_EQ(link_to.value(), result.value());
602
603 // Link to a directory.
604 link_from = temp_dir_.path().Append(FPL("from_dir"));
605 link_to = temp_dir_.path().Append(FPL("to_dir"));
606 file_util::CreateDirectory(link_to);
607
608 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
609 << "Failed to create directory symlink.";
610
611 // Test failures.
612 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to));
613 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result));
614 FilePath missing = temp_dir_.path().Append(FPL("missing"));
615 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result));
616 }
617
618
584 // The following test of NormalizeFilePath() require that we create a symlink. 619 // The following test of NormalizeFilePath() require that we create a symlink.
585 // This can not be done on windows before vista. On vista, creating a symlink 620 // This can not be done on Windows before Vista. On Vista, creating a symlink
586 // requires privilege "SeCreateSymbolicLinkPrivilege". 621 // requires privilege "SeCreateSymbolicLinkPrivilege".
587 // TODO(skerner): Investigate the possibility of giving base_unittests the 622 // TODO(skerner): Investigate the possibility of giving base_unittests the
588 // privileges required to create a symlink. 623 // privileges required to create a symlink.
589 #if defined(OS_POSIX)
590
591 bool MakeSymlink(const FilePath& link_to, const FilePath& link_from) {
592 return (symlink(link_to.value().c_str(), link_from.value().c_str()) == 0);
593 }
594
595 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { 624 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
596 FilePath normalized_path; 625 FilePath normalized_path;
597 626
598 // Link one file to another. 627 // Link one file to another.
599 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); 628 FilePath link_from = temp_dir_.path().Append(FPL("from_file"));
600 FilePath link_to = temp_dir_.path().Append(FPL("to_file")); 629 FilePath link_to = temp_dir_.path().Append(FPL("to_file"));
601 CreateTextFile(link_to, bogus_content); 630 CreateTextFile(link_to, bogus_content);
602 631
603 ASSERT_TRUE(MakeSymlink(link_to, link_from)) 632 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
604 << "Failed to create file symlink."; 633 << "Failed to create file symlink.";
605 634
606 // Check that NormalizeFilePath sees the link. 635 // Check that NormalizeFilePath sees the link.
607 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path)); 636 ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
608 ASSERT_TRUE(link_to != link_from); 637 ASSERT_TRUE(link_to != link_from);
609 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); 638 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
610 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value()); 639 ASSERT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
611 640
612 // Link to a directory. 641 // Link to a directory.
613 link_from = temp_dir_.path().Append(FPL("from_dir")); 642 link_from = temp_dir_.path().Append(FPL("from_dir"));
614 link_to = temp_dir_.path().Append(FPL("to_dir")); 643 link_to = temp_dir_.path().Append(FPL("to_dir"));
615 file_util::CreateDirectory(link_to); 644 file_util::CreateDirectory(link_to);
616 645
617 ASSERT_TRUE(MakeSymlink(link_to, link_from)) 646 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
618 << "Failed to create directory symlink."; 647 << "Failed to create directory symlink.";
619 648
620 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)) 649 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
621 << "Links to directories should return false."; 650 << "Links to directories should return false.";
622 651
623 // Test that a loop in the links causes NormalizeFilePath() to return false. 652 // Test that a loop in the links causes NormalizeFilePath() to return false.
624 link_from = temp_dir_.path().Append(FPL("link_a")); 653 link_from = temp_dir_.path().Append(FPL("link_a"));
625 link_to = temp_dir_.path().Append(FPL("link_b")); 654 link_to = temp_dir_.path().Append(FPL("link_b"));
626 ASSERT_TRUE(MakeSymlink(link_to, link_from)) 655 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from))
627 << "Failed to create loop symlink a."; 656 << "Failed to create loop symlink a.";
628 ASSERT_TRUE(MakeSymlink(link_from, link_to)) 657 ASSERT_TRUE(file_util::CreateSymbolicLink(link_from, link_to))
629 << "Failed to create loop symlink b."; 658 << "Failed to create loop symlink b.";
630 659
631 // Infinite loop! 660 // Infinite loop!
632 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path)); 661 ASSERT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
633 } 662 }
634 #endif // defined(OS_POSIX) 663 #endif // defined(OS_POSIX)
635 664
636 TEST_F(FileUtilTest, DeleteNonExistent) { 665 TEST_F(FileUtilTest, DeleteNonExistent) {
637 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar"); 666 FilePath non_existent = temp_dir_.path().AppendASCII("bogus_file_dne.foobar");
638 ASSERT_FALSE(file_util::PathExists(non_existent)); 667 ASSERT_FALSE(file_util::PathExists(non_existent));
(...skipping 1159 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir)); 1827 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1799 1828
1800 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); 1829 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1801 std::string bar("baz"); 1830 std::string bar("baz");
1802 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length())); 1831 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1803 1832
1804 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir)); 1833 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1805 } 1834 }
1806 1835
1807 } // namespace 1836 } // namespace
OLDNEW
« no previous file with comments | « base/file_util_posix.cc ('k') | base/platform_file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698