Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 data.ReparseTag = 0xa0000003; | 104 data.ReparseTag = 0xa0000003; |
| 105 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0, | 105 if (!DeviceIoControl(source, FSCTL_DELETE_REPARSE_POINT, &data, 8, NULL, 0, |
| 106 &returned, NULL)) { | 106 &returned, NULL)) { |
| 107 return false; | 107 return false; |
| 108 } | 108 } |
| 109 return true; | 109 return true; |
| 110 } | 110 } |
| 111 #endif | 111 #endif |
| 112 | 112 |
| 113 #if defined(OS_POSIX) | 113 #if defined(OS_POSIX) |
| 114 // Provide a simple way to change the permissions bits on |path| in tests. | 114 bool ChangePosixFilePermissions(const FilePath& path, |
| 115 // ASSERT failures will return, but not stop the test. Caller should wrap | 115 int mode_bits_to_set, |
| 116 // calls to this function in ASSERT_NO_FATAL_FAILURE(). | 116 int mode_bits_to_clear) { |
| 117 void ChangePosixFilePermissions(const FilePath& path, | 117 DCHECK((mode_bits_to_set & ~file_util::FILE_PERMISSION_MASK) == 0); |
| 118 mode_t mode_bits_to_set, | 118 DCHECK((mode_bits_to_clear & ~file_util::FILE_PERMISSION_MASK) == 0); |
|
satorux1
2012/07/10 00:06:54
the two lines look unnecessary, as we have a DCHEC
yoshiki
2012/07/10 01:31:17
Done.
| |
| 119 mode_t mode_bits_to_clear) { | 119 DCHECK(!(mode_bits_to_set & mode_bits_to_clear)) |
| 120 ASSERT_FALSE(mode_bits_to_set & mode_bits_to_clear) | |
| 121 << "Can't set and clear the same bits."; | 120 << "Can't set and clear the same bits."; |
| 122 | 121 |
| 123 struct stat stat_buf; | 122 int mode; |
|
satorux1
2012/07/10 00:06:54
mode = 0; just in case.
yoshiki
2012/07/10 01:31:17
Done.
| |
| 124 ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf)); | 123 if (!file_util::GetPosixFilePermissions(path, &mode)) |
| 124 return false; | |
| 125 | 125 |
| 126 mode_t updated_mode_bits = stat_buf.st_mode; | 126 mode |= mode_bits_to_set; |
| 127 updated_mode_bits |= mode_bits_to_set; | 127 mode &= ~mode_bits_to_clear; |
| 128 updated_mode_bits &= ~mode_bits_to_clear; | |
| 129 | 128 |
| 130 ASSERT_EQ(0, chmod(path.value().c_str(), updated_mode_bits)); | 129 if (file_util::SetPosixFilePermissions(path, mode)) |
|
satorux1
2012/07/10 00:06:54
Is this correct? Shouldn't we have ! here?
if (!f
yoshiki
2012/07/10 01:31:17
Oops... Done.
On 2012/07/10 00:06:54, satorux1 wr
| |
| 130 return false; | |
| 131 | |
| 132 return true; | |
| 131 } | 133 } |
| 132 #endif // defined(OS_POSIX) | 134 #endif // defined(OS_POSIX) |
| 133 | 135 |
| 134 const wchar_t bogus_content[] = L"I'm cannon fodder."; | 136 const wchar_t bogus_content[] = L"I'm cannon fodder."; |
| 135 | 137 |
| 136 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = | 138 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = |
| 137 static_cast<file_util::FileEnumerator::FileType>( | 139 static_cast<file_util::FileEnumerator::FileType>( |
| 138 file_util::FileEnumerator::FILES | | 140 file_util::FileEnumerator::FILES | |
| 139 file_util::FileEnumerator::DIRECTORIES); | 141 file_util::FileEnumerator::DIRECTORIES); |
| 140 | 142 |
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 660 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) | 662 ASSERT_TRUE(file_util::CreateSymbolicLink(link_to, link_from)) |
| 661 << "Failed to create directory symlink."; | 663 << "Failed to create directory symlink."; |
| 662 | 664 |
| 663 // Test failures. | 665 // Test failures. |
| 664 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); | 666 ASSERT_FALSE(file_util::CreateSymbolicLink(link_to, link_to)); |
| 665 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); | 667 ASSERT_FALSE(file_util::ReadSymbolicLink(link_to, &result)); |
| 666 FilePath missing = temp_dir_.path().Append(FPL("missing")); | 668 FilePath missing = temp_dir_.path().Append(FPL("missing")); |
| 667 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result)); | 669 ASSERT_FALSE(file_util::ReadSymbolicLink(missing, &result)); |
| 668 } | 670 } |
| 669 | 671 |
| 670 | |
| 671 // The following test of NormalizeFilePath() require that we create a symlink. | 672 // The following test of NormalizeFilePath() require that we create a symlink. |
| 672 // This can not be done on Windows before Vista. On Vista, creating a symlink | 673 // This can not be done on Windows before Vista. On Vista, creating a symlink |
| 673 // requires privilege "SeCreateSymbolicLinkPrivilege". | 674 // requires privilege "SeCreateSymbolicLinkPrivilege". |
| 674 // TODO(skerner): Investigate the possibility of giving base_unittests the | 675 // TODO(skerner): Investigate the possibility of giving base_unittests the |
| 675 // privileges required to create a symlink. | 676 // privileges required to create a symlink. |
| 676 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { | 677 TEST_F(FileUtilTest, NormalizeFilePathSymlinks) { |
| 677 FilePath normalized_path; | 678 FilePath normalized_path; |
| 678 | 679 |
| 679 // Link one file to another. | 680 // Link one file to another. |
| 680 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); | 681 FilePath link_from = temp_dir_.path().Append(FPL("from_file")); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 777 // Make sure the symbolic link is exist | 778 // Make sure the symbolic link is exist |
| 778 EXPECT_TRUE(file_util::IsLink(file_link)); | 779 EXPECT_TRUE(file_util::IsLink(file_link)); |
| 779 EXPECT_FALSE(file_util::PathExists(file_link)); | 780 EXPECT_FALSE(file_util::PathExists(file_link)); |
| 780 | 781 |
| 781 // Delete the symbolic link | 782 // Delete the symbolic link |
| 782 EXPECT_TRUE(file_util::Delete(file_link, false)); | 783 EXPECT_TRUE(file_util::Delete(file_link, false)); |
| 783 | 784 |
| 784 // Make sure the symbolic link is deleted | 785 // Make sure the symbolic link is deleted |
| 785 EXPECT_FALSE(file_util::IsLink(file_link)); | 786 EXPECT_FALSE(file_util::IsLink(file_link)); |
| 786 } | 787 } |
| 788 | |
| 789 TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) { | |
| 790 // Create a file path | |
|
satorux1
2012/07/10 00:06:54
period is missing.
yoshiki
2012/07/10 01:31:17
Done.
| |
| 791 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); | |
| 792 EXPECT_FALSE(file_util::PathExists(file_name)); | |
| 793 | |
| 794 char buffer[32] = "hello"; | |
|
satorux1
2012/07/10 00:06:54
remove 32. even better, remove this variable.
yoshiki
2012/07/10 01:31:17
Removed 32. But not remove the variable because it
| |
| 795 std::string data(buffer); | |
|
satorux1
2012/07/10 00:06:54
const std::string kData = "hello";
yoshiki
2012/07/10 01:31:17
Made it const and changed name, keeping the initia
satorux1
2012/07/10 06:57:17
Reusing 'buffer' at a later time for writing sound
yoshiki
2012/07/10 18:51:35
I think direct manipulation to buffer of std::stri
| |
| 796 | |
| 797 // Write file | |
| 798 EXPECT_EQ(static_cast<int>(data.length()), | |
| 799 file_util::WriteFile(file_name, data.c_str(), data.length())); | |
| 800 EXPECT_TRUE(file_util::PathExists(file_name)); | |
| 801 | |
| 802 // Meke sure the file is readable | |
| 803 int32 mode; | |
|
satorux1
2012/07/10 00:06:54
int mode = 0;
yoshiki
2012/07/10 01:31:17
Done.
| |
| 804 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); | |
| 805 EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER, | |
| 806 mode & file_util::FILE_PERMISSION_READ_BY_USER); | |
|
satorux1
2012/07/10 00:06:54
EXPECT_TRUE(mode & file_util::FILE_PERMISSION_READ
yoshiki
2012/07/10 01:31:17
Done.
| |
| 807 | |
| 808 // Get rid of the read permission | |
| 809 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); | |
| 810 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); | |
| 811 EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_READ_BY_USER); | |
|
satorux1
2012/07/10 00:06:54
EXPECT_FALSE(mode & file_util::FILE_PERMISSION_REA
yoshiki
2012/07/10 01:31:17
Done.
| |
| 812 // Make sure the file can't be read | |
| 813 EXPECT_EQ(-1, file_util::ReadFile(file_name, buffer, sizeof(buffer))); | |
|
satorux1
2012/07/10 00:06:54
kData.data(), kData.size()
yoshiki
2012/07/10 01:31:17
string::data() returns a pointer of const char*, s
satorux1
2012/07/10 06:57:17
oops, you are right.
On 2012/07/10 01:31:17, yosh
| |
| 814 | |
| 815 // Give the read permission | |
| 816 EXPECT_TRUE(file_util::SetPosixFilePermissions( | |
| 817 file_name, | |
| 818 file_util::FILE_PERMISSION_READ_BY_USER)); | |
| 819 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); | |
| 820 EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER, | |
| 821 mode & file_util::FILE_PERMISSION_READ_BY_USER); | |
| 822 // Make sure the file can be read | |
| 823 EXPECT_EQ(static_cast<int>(data.length()), | |
| 824 file_util::ReadFile(file_name, buffer, sizeof(buffer))); | |
| 825 | |
| 826 // Delete the file | |
| 827 EXPECT_TRUE(file_util::Delete(file_name, false)); | |
| 828 EXPECT_FALSE(file_util::PathExists(file_name)); | |
| 829 } | |
| 830 | |
| 831 TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { | |
| 832 // Create a file path | |
| 833 FilePath file_name = temp_dir_.path().Append(FPL("Test Readable File.txt")); | |
| 834 EXPECT_FALSE(file_util::PathExists(file_name)); | |
| 835 | |
| 836 char buffer[32] = "hello"; | |
| 837 std::string data(buffer); | |
| 838 | |
| 839 // Write file | |
| 840 EXPECT_EQ(static_cast<int>(data.length()), | |
| 841 file_util::WriteFile(file_name, data.c_str(), data.length())); | |
| 842 EXPECT_TRUE(file_util::PathExists(file_name)); | |
| 843 | |
| 844 // Meke sure the file is writable | |
| 845 int mode; | |
| 846 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); | |
| 847 EXPECT_EQ(file_util::FILE_PERMISSION_WRITE_BY_USER, | |
| 848 mode & file_util::FILE_PERMISSION_WRITE_BY_USER); | |
| 849 EXPECT_TRUE(file_util::PathIsWritable(file_name)); | |
| 850 | |
| 851 // Get rid of the write permission | |
| 852 EXPECT_TRUE(file_util::SetPosixFilePermissions(file_name, 0u)); | |
| 853 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); | |
| 854 EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_WRITE_BY_USER); | |
| 855 // Make sure the file can't be write | |
| 856 EXPECT_EQ(-1, | |
| 857 file_util::WriteFile(file_name, data.c_str(), data.length())); | |
| 858 EXPECT_FALSE(file_util::PathIsWritable(file_name)); | |
| 859 | |
| 860 // Give read permission | |
| 861 EXPECT_TRUE(file_util::SetPosixFilePermissions( | |
| 862 file_name, | |
| 863 file_util::FILE_PERMISSION_WRITE_BY_USER)); | |
| 864 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_name, &mode)); | |
| 865 EXPECT_EQ(file_util::FILE_PERMISSION_WRITE_BY_USER, | |
| 866 mode & file_util::FILE_PERMISSION_WRITE_BY_USER); | |
| 867 // Make sure the file can be write | |
| 868 EXPECT_EQ(static_cast<int>(data.length()), | |
| 869 file_util::WriteFile(file_name, data.c_str(), data.length())); | |
| 870 EXPECT_TRUE(file_util::PathIsWritable(file_name)); | |
| 871 | |
| 872 // Delete the file | |
| 873 EXPECT_TRUE(file_util::Delete(file_name, false)); | |
| 874 EXPECT_FALSE(file_util::PathExists(file_name)); | |
| 875 } | |
| 876 | |
| 877 TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) { | |
| 878 // Create a directory path | |
| 879 FilePath subdir_path = | |
| 880 temp_dir_.path().Append(FPL("PermissionTest1")); | |
| 881 file_util::CreateDirectory(subdir_path); | |
| 882 ASSERT_TRUE(file_util::PathExists(subdir_path)); | |
| 883 | |
| 884 // Create a dummy file to enumerate | |
| 885 FilePath file_name = subdir_path.Append(FPL("Test Readable File.txt")); | |
| 886 EXPECT_FALSE(file_util::PathExists(file_name)); | |
| 887 char buffer[32] = "hello"; | |
| 888 std::string data(buffer); | |
| 889 EXPECT_EQ(static_cast<int>(data.length()), | |
| 890 file_util::WriteFile(file_name, data.c_str(), data.length())); | |
| 891 EXPECT_TRUE(file_util::PathExists(file_name)); | |
| 892 | |
| 893 // Meke sure the file is the all permissions | |
| 894 int mode; | |
| 895 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); | |
| 896 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, | |
| 897 mode & file_util::FILE_PERMISSION_USER_MASK); | |
| 898 | |
| 899 // Get rid of the permissions from the directory | |
| 900 EXPECT_TRUE(file_util::SetPosixFilePermissions(subdir_path, 0u)); | |
| 901 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); | |
| 902 EXPECT_EQ(0, mode & file_util::FILE_PERMISSION_USER_MASK); | |
| 903 | |
| 904 // Make sure the file in the directory can't be enumerated. | |
| 905 file_util::FileEnumerator f1(subdir_path, true, | |
| 906 file_util::FileEnumerator::FILES); | |
| 907 EXPECT_TRUE(file_util::PathExists(subdir_path)); | |
| 908 FindResultCollector c1(f1); | |
| 909 EXPECT_EQ(c1.size(), 0); | |
| 910 EXPECT_FALSE(file_util::GetPosixFilePermissions(file_name, &mode)); | |
| 911 | |
| 912 // Give the permissions to the directory | |
| 913 EXPECT_TRUE(file_util::SetPosixFilePermissions( | |
| 914 subdir_path, | |
| 915 file_util::FILE_PERMISSION_USER_MASK)); | |
| 916 EXPECT_TRUE(file_util::GetPosixFilePermissions(subdir_path, &mode)); | |
| 917 EXPECT_EQ(file_util::FILE_PERMISSION_USER_MASK, | |
| 918 mode & file_util::FILE_PERMISSION_USER_MASK); | |
| 919 | |
| 920 // Make sure the file in the directory can be enumerated. | |
| 921 file_util::FileEnumerator f2(subdir_path, true, | |
| 922 file_util::FileEnumerator::FILES); | |
| 923 FindResultCollector c2(f2); | |
| 924 EXPECT_TRUE(c2.HasFile(file_name)); | |
| 925 EXPECT_EQ(c2.size(), 1); | |
| 926 | |
| 927 // Delete the file | |
| 928 EXPECT_TRUE(file_util::Delete(subdir_path, true)); | |
| 929 EXPECT_FALSE(file_util::PathExists(subdir_path)); | |
| 930 } | |
| 931 | |
| 787 #endif // defined(OS_POSIX) | 932 #endif // defined(OS_POSIX) |
| 788 | 933 |
| 789 #if defined(OS_WIN) | 934 #if defined(OS_WIN) |
| 790 // Tests that the Delete function works for wild cards, especially | 935 // Tests that the Delete function works for wild cards, especially |
| 791 // with the recursion flag. Also coincidentally tests PathExists. | 936 // with the recursion flag. Also coincidentally tests PathExists. |
| 792 // TODO(erikkay): see if anyone's actually using this feature of the API | 937 // TODO(erikkay): see if anyone's actually using this feature of the API |
| 793 TEST_F(FileUtilTest, DeleteWildCard) { | 938 TEST_F(FileUtilTest, DeleteWildCard) { |
| 794 // Create a file and a directory | 939 // Create a file and a directory |
| 795 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); | 940 FilePath file_name = temp_dir_.path().Append(FPL("Test DeleteWildCard.txt")); |
| 796 CreateTextFile(file_name, bogus_content); | 941 CreateTextFile(file_name, bogus_content); |
| (...skipping 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1990 uid_ = stat_buf.st_uid; | 2135 uid_ = stat_buf.st_uid; |
| 1991 ok_gids_.insert(stat_buf.st_gid); | 2136 ok_gids_.insert(stat_buf.st_gid); |
| 1992 bad_gids_.insert(stat_buf.st_gid + 1); | 2137 bad_gids_.insert(stat_buf.st_gid + 1); |
| 1993 | 2138 |
| 1994 ASSERT_EQ(uid_, getuid()); // This process should be the owner. | 2139 ASSERT_EQ(uid_, getuid()); // This process should be the owner. |
| 1995 | 2140 |
| 1996 // To ensure that umask settings do not cause the initial state | 2141 // To ensure that umask settings do not cause the initial state |
| 1997 // of permissions to be different from what we expect, explicitly | 2142 // of permissions to be different from what we expect, explicitly |
| 1998 // set permissions on the directories we create. | 2143 // set permissions on the directories we create. |
| 1999 // Make all files and directories non-world-writable. | 2144 // Make all files and directories non-world-writable. |
| 2000 mode_t enabled_permissions = | 2145 |
| 2001 S_IRWXU | // User can read, write, traverse | 2146 // Users and group can read, write, traverse |
| 2002 S_IRWXG; // Group can read, write, traverse | 2147 int enabled_permissions = |
| 2003 mode_t disabled_permissions = | 2148 file_util::FILE_PERMISSION_USER_MASK | |
| 2004 S_IRWXO; // Other users can't read, write, traverse. | 2149 file_util::FILE_PERMISSION_GROUP_MASK; |
| 2150 // Other users can't read, write, traverse | |
| 2151 int disabled_permissions = | |
| 2152 file_util::FILE_PERMISSION_OTHERS_MASK; | |
| 2005 | 2153 |
| 2006 ASSERT_NO_FATAL_FAILURE( | 2154 ASSERT_NO_FATAL_FAILURE( |
| 2007 ChangePosixFilePermissions( | 2155 ChangePosixFilePermissions( |
| 2008 base_dir_, enabled_permissions, disabled_permissions)); | 2156 base_dir_, enabled_permissions, disabled_permissions)); |
| 2009 ASSERT_NO_FATAL_FAILURE( | 2157 ASSERT_NO_FATAL_FAILURE( |
| 2010 ChangePosixFilePermissions( | 2158 ChangePosixFilePermissions( |
| 2011 sub_dir_, enabled_permissions, disabled_permissions)); | 2159 sub_dir_, enabled_permissions, disabled_permissions)); |
| 2012 } | 2160 } |
| 2013 | 2161 |
| 2014 FilePath base_dir_; | 2162 FilePath base_dir_; |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2318 file_util::VerifyPathControlledByUser( | 2466 file_util::VerifyPathControlledByUser( |
| 2319 base_dir_, text_file_, uid_, ok_gids_)); | 2467 base_dir_, text_file_, uid_, ok_gids_)); |
| 2320 EXPECT_TRUE( | 2468 EXPECT_TRUE( |
| 2321 file_util::VerifyPathControlledByUser( | 2469 file_util::VerifyPathControlledByUser( |
| 2322 sub_dir_, text_file_, uid_, ok_gids_)); | 2470 sub_dir_, text_file_, uid_, ok_gids_)); |
| 2323 } | 2471 } |
| 2324 | 2472 |
| 2325 #endif // defined(OS_POSIX) | 2473 #endif // defined(OS_POSIX) |
| 2326 | 2474 |
| 2327 } // namespace | 2475 } // namespace |
| OLD | NEW |