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

Side by Side Diff: base/file_util_unittest.cc

Issue 7718021: Add external extensions json source in proper mac location. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rev Created 9 years, 4 months 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
OLDNEW
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
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)
Evan Martin 2011/08/24 22:38:53 no space after defined
Sam Kerner (Chrome) 2011/08/25 14:04:37 Done.
114 void ChangePosixFilePermissions(const FilePath& path,
Evan Martin 2011/08/24 22:38:53 can you add doc comments?
Sam Kerner (Chrome) 2011/08/25 14:04:37 Done.
115 mode_t mode_bits_to_set,
116 mode_t mode_bits_to_clear) {
117 ASSERT_EQ(0, mode_bits_to_set & mode_bits_to_clear)
118 << "Can't set and clear the same bit.";
119
120 struct stat stat_buf;
121 ASSERT_EQ(0, stat(path.value().c_str(), &stat_buf));
122
123 mode_t new_mode_bits = stat_buf.st_mode;
124 new_mode_bits |= mode_bits_to_set;
125 new_mode_bits &= ~mode_bits_to_clear;
126
127 ASSERT_EQ(0, chmod(path.value().c_str(), new_mode_bits));
Evan Martin 2011/08/24 22:38:53 ASSERT doesn't owrk in helper functions unless you
Sam Kerner (Chrome) 2011/08/25 14:04:37 Done.
128 }
129 #endif // defined (OS_POSIX)
Evan Martin 2011/08/24 22:38:53 no space after defined
Sam Kerner (Chrome) 2011/08/25 14:04:37 Done.
130
113 const wchar_t bogus_content[] = L"I'm cannon fodder."; 131 const wchar_t bogus_content[] = L"I'm cannon fodder.";
114 132
115 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES = 133 const file_util::FileEnumerator::FileType FILES_AND_DIRECTORIES =
116 static_cast<file_util::FileEnumerator::FileType>( 134 static_cast<file_util::FileEnumerator::FileType>(
117 file_util::FileEnumerator::FILES | 135 file_util::FileEnumerator::FILES |
118 file_util::FileEnumerator::DIRECTORIES); 136 file_util::FileEnumerator::DIRECTORIES);
119 137
120 // file_util winds up using autoreleased objects on the Mac, so this needs 138 // file_util winds up using autoreleased objects on the Mac, so this needs
121 // to be a PlatformTest 139 // to be a PlatformTest
122 class FileUtilTest : public PlatformTest { 140 class FileUtilTest : public PlatformTest {
(...skipping 1683 matching lines...) Expand 10 before | Expand all | Expand 10 after
1806 1824
1807 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir)); 1825 EXPECT_TRUE(file_util::IsDirectoryEmpty(empty_dir));
1808 1826
1809 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt"))); 1827 FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
1810 std::string bar("baz"); 1828 std::string bar("baz");
1811 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length())); 1829 ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
1812 1830
1813 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir)); 1831 EXPECT_FALSE(file_util::IsDirectoryEmpty(empty_dir));
1814 } 1832 }
1815 1833
1834 #if defined(OS_POSIX)
1835 TEST_F(FileUtilTest, IsPathControledByAdmin) {
1836 // Testing IsPathControlledByAdmin() is hard, because there is no
1837 // way a test can make a file owned by root, or change file paths
1838 // at the root of the file system. IsPathControlledByAdmin()
1839 // is implemented as a call to IsPathControlledByUser, which gives
1840 // us the ability to test with paths under the test's temp directory,
1841 // using a user id we control.
Evan Martin 2011/08/24 22:38:53 This comment is good. I'd rename the test to say
Sam Kerner (Chrome) 2011/08/25 14:04:37 Done.
1842
1843 FilePath base_dir = temp_dir_.path().AppendASCII("base_dir");
1844 ASSERT_TRUE(file_util::CreateDirectory(base_dir));
1845
1846 FilePath sub_dir = base_dir.AppendASCII("sub_dir");
1847 ASSERT_TRUE(file_util::CreateDirectory(sub_dir));
1848
1849 FilePath text_file = sub_dir.AppendASCII("file.txt");
1850 CreateTextFile(text_file, L"This text file has some text in it.");
1851
1852 // Get our uid, and another uid, so that we can test both a
1853 // matching and non-matching uid.
1854 uid_t our_uid = getuid();
1855 uid_t not_our_uid = our_uid + 1;
1856
1857 // Make all files and directories non-world-writable.
1858 ChangePosixFilePermissions(base_dir, 0u, S_IWOTH);
1859 ChangePosixFilePermissions(sub_dir, 0u, S_IWOTH);
1860 ChangePosixFilePermissions(text_file, 0u, S_IWOTH);
1861
1862 // We control these paths.
1863 ASSERT_TRUE(
1864 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid));
1865 ASSERT_TRUE(
1866 file_util::IsPathControlledByUser(base_dir, text_file, our_uid));
1867 ASSERT_TRUE(
1868 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid));
1869
1870 // Another user does not control these paths.
1871 ASSERT_FALSE(
1872 file_util::IsPathControlledByUser(base_dir, sub_dir, not_our_uid ));
1873 ASSERT_FALSE(
1874 file_util::IsPathControlledByUser(base_dir, text_file, not_our_uid));
1875 ASSERT_FALSE(
1876 file_util::IsPathControlledByUser(sub_dir, text_file, not_our_uid));
1877
1878 // Make base_dir world-writable. No change, because the base dir should
1879 // not be tested.
1880 ChangePosixFilePermissions(base_dir, S_IWOTH, 0u);
1881 ASSERT_TRUE(
1882 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid));
1883 ASSERT_TRUE(
1884 file_util::IsPathControlledByUser(base_dir, text_file, our_uid));
1885 ASSERT_TRUE(
1886 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid));
1887
1888 // Make sub_dir world writable.
1889 ChangePosixFilePermissions(sub_dir, S_IWOTH, 0u);
1890 ASSERT_FALSE(
1891 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid));
1892 ASSERT_FALSE(
1893 file_util::IsPathControlledByUser(base_dir, text_file, our_uid));
1894 ASSERT_TRUE(
1895 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid));
1896
1897 // Make text_file world writable.
1898 ChangePosixFilePermissions(text_file, S_IWOTH, 0u);
1899 ASSERT_FALSE(
1900 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid));
1901 ASSERT_FALSE(
1902 file_util::IsPathControlledByUser(base_dir, text_file, our_uid));
1903 ASSERT_FALSE(
1904 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid));
1905
1906 // Make sub_dir non-world writable.
1907 ChangePosixFilePermissions(sub_dir, 0u, S_IWOTH);
1908 ASSERT_TRUE(
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 base_dir non-world-writable.
1916 ChangePosixFilePermissions(base_dir, 0u, S_IWOTH);
1917 ASSERT_TRUE(
1918 file_util::IsPathControlledByUser(base_dir, sub_dir, our_uid));
1919 ASSERT_FALSE(
1920 file_util::IsPathControlledByUser(base_dir, text_file, our_uid));
1921 ASSERT_FALSE(
1922 file_util::IsPathControlledByUser(sub_dir, text_file, our_uid));
1923 }
1924 #endif // defined(OS_POSIX)
TVL 2011/08/25 14:07:28 i tend to include tests of bad inputs to make sure
Sam Kerner (Chrome) 2011/08/26 19:59:15 Lots of tests added, including all these cases.
1925
1816 } // namespace 1926 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698