OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 REPARSE_DATA_BUFFER data = {0}; | 103 REPARSE_DATA_BUFFER data = {0}; |
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) |
| 114 // Provide a simple way to change the permissions bits on |path| in tests. |
| 115 // ASSERT failures will return, but not stop the test. Caller should wrap |
| 116 // calls to this function in ASSERT_NO_FATAL_FAILURE(). |
| 117 void ChangePosixFilePermissions(const FilePath& path, |
| 118 mode_t mode_bits_to_set, |
| 119 mode_t mode_bits_to_clear) { |
| 120 ASSERT_EQ(0u, mode_bits_to_set & mode_bits_to_clear) |
| 121 << "Can't set and clear the same bit."; |
| 122 |
| 123 struct stat stat_buf; |
| 124 ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf)); |
| 125 |
| 126 mode_t new_mode_bits = stat_buf.st_mode; |
| 127 new_mode_bits |= mode_bits_to_set; |
| 128 new_mode_bits &= ~mode_bits_to_clear; |
| 129 |
| 130 ASSERT_EQ(0, chmod(path.value().c_str(), new_mode_bits)); |
| 131 } |
| 132 #endif // defined(OS_POSIX) |
| 133 |
113 const wchar_t bogus_content[] = L"I'm cannon fodder."; | 134 const wchar_t bogus_content[] = L"I'm cannon fodder."; |
114 | 135 |
115 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = | 136 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = |
116 static_cast<file_util::FileEnumerator::FileType>( | 137 static_cast<file_util::FileEnumerator::FileType>( |
117 file_util::FileEnumerator::FILES | | 138 file_util::FileEnumerator::FILES | |
118 file_util::FileEnumerator::DIRECTORIES); | 139 file_util::FileEnumerator::DIRECTORIES); |
119 | 140 |
120 // file_util winds up using autoreleased objects on the Mac, so this needs | 141 // file_util winds up using autoreleased objects on the Mac, so this needs |
121 // to be a PlatformTest | 142 // to be a PlatformTest |
122 class FileUtilTest : public PlatformTest { | 143 class FileUtilTest : public PlatformTest { |
(...skipping 1683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1806 | 1827 |
1807 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir)); | 1828 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir)); |
1808 | 1829 |
1809 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); | 1830 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); |
1810 std::string bar("baz"); | 1831 std::string bar("baz"); |
1811 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length())); | 1832 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length())); |
1812 | 1833 |
1813 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir)); | 1834 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir)); |
1814 } | 1835 } |
1815 | 1836 |
| 1837 #if defined(OS_POSIX) |
| 1838 TEST_F(FileUtilTest, IsPathControlledByUser) { |
| 1839 // Testing IsPathControlledByAdmin() is hard, because there is no |
| 1840 // way a test can make a file owned by root, or change file paths |
| 1841 // at the root of the file system. IsPathControlledByAdmin() |
| 1842 // is implemented as a call to IsPathControlledByUser, which gives |
| 1843 // us the ability to test with paths under the test's temp directory, |
| 1844 // using a user id we control. |
| 1845 |
| 1846 FilePath base_dir = temp_dir_.path().AppendASCII("base_dir"); |
| 1847 ASSERT_TRUE(file_util::CreateDirectory(base_dir)); |
| 1848 |
| 1849 FilePath sub_dir = base_dir.AppendASCII("sub_dir"); |
| 1850 ASSERT_TRUE(file_util::CreateDirectory(sub_dir)); |
| 1851 |
| 1852 FilePath text_file = sub_dir.AppendASCII("file.txt"); |
| 1853 CreateTextFile(text_file, L"This text file has some text in it."); |
| 1854 |
| 1855 // Get our uid, and another uid, so that we can test both a |
| 1856 // matching and non-matching uid. |
| 1857 uid_t our_uid = getuid(); |
| 1858 uid_t not_our_uid = our_uid + 1; |
| 1859 |
| 1860 // Make all files and directories non-world-writable. |
| 1861 ASSERT_NO_FATAL_FAILURE( |
| 1862 ChangePosixFilePermissions(base_dir, 0u, S_IWOTH)); |
| 1863 ASSERT_NO_FATAL_FAILURE( |
| 1864 ChangePosixFilePermissions(sub_dir, 0u, S_IWOTH)); |
| 1865 ASSERT_NO_FATAL_FAILURE( |
| 1866 ChangePosixFilePermissions(text_file, 0u, S_IWOTH)); |
| 1867 |
| 1868 // We control these paths. |
| 1869 ASSERT_TRUE( |
| 1870 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
| 1871 ASSERT_TRUE( |
| 1872 file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
| 1873 ASSERT_TRUE( |
| 1874 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
| 1875 |
| 1876 // Another user does not control these paths. |
| 1877 ASSERT_FALSE( |
| 1878 file_util::IsPathControlledByUser(base_dir, sub_dir, not_our_uid )); |
| 1879 ASSERT_FALSE( |
| 1880 file_util::IsPathControlledByUser(base_dir, text_file, not_our_uid)); |
| 1881 ASSERT_FALSE( |
| 1882 file_util::IsPathControlledByUser(sub_dir, text_file, not_our_uid)); |
| 1883 |
| 1884 // Make base_dir world-writable. No change, because the base dir should |
| 1885 // not be tested. |
| 1886 ASSERT_NO_FATAL_FAILURE( |
| 1887 ChangePosixFilePermissions(base_dir, S_IWOTH, 0u)); |
| 1888 ASSERT_TRUE( |
| 1889 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
| 1890 ASSERT_TRUE( |
| 1891 file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
| 1892 ASSERT_TRUE( |
| 1893 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
| 1894 |
| 1895 // Make sub_dir world writable. |
| 1896 ASSERT_NO_FATAL_FAILURE( |
| 1897 ChangePosixFilePermissions(sub_dir, S_IWOTH, 0u)); |
| 1898 ASSERT_FALSE( |
| 1899 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
| 1900 ASSERT_FALSE( |
| 1901 file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
| 1902 ASSERT_TRUE( |
| 1903 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
| 1904 |
| 1905 // Make text_file world writable. |
| 1906 ASSERT_NO_FATAL_FAILURE( |
| 1907 ChangePosixFilePermissions(text_file, S_IWOTH, 0u)); |
| 1908 ASSERT_FALSE( |
| 1909 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
| 1910 ASSERT_FALSE( |
| 1911 file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
| 1912 ASSERT_FALSE( |
| 1913 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
| 1914 |
| 1915 // Make sub_dir non-world writable. |
| 1916 ASSERT_NO_FATAL_FAILURE( |
| 1917 ChangePosixFilePermissions(sub_dir, 0u, S_IWOTH)); |
| 1918 ASSERT_TRUE( |
| 1919 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
| 1920 ASSERT_FALSE( |
| 1921 file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
| 1922 ASSERT_FALSE( |
| 1923 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
| 1924 |
| 1925 // Make base_dir non-world-writable. |
| 1926 ASSERT_NO_FATAL_FAILURE( |
| 1927 ChangePosixFilePermissions(base_dir, 0u, S_IWOTH)); |
| 1928 ASSERT_TRUE( |
| 1929 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid)); |
| 1930 ASSERT_FALSE( |
| 1931 file_util::IsPathControlledByUser(base_dir, text_file, our_uid)); |
| 1932 ASSERT_FALSE( |
| 1933 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid)); |
| 1934 } |
| 1935 #endif // defined(OS_POSIX) |
| 1936 |
1816 } // namespace | 1937 } // namespace |
OLD | NEW |