MCPcopy Create free account
hub / github.com/apache/brpc / VerifyPathControlledByUserTest

Class VerifyPathControlledByUserTest

test/file_util_unittest.cc:2176–2235  ·  view source on GitHub ↗

Testing VerifyPathControlledByAdmin() is hard, because there is no way a test can make a file owned by root, or change file paths at the root of the file system. VerifyPathControlledByAdmin() is implemented as a call to VerifyPathControlledByUser, which gives us the ability to test with paths under the test's temp directory, using a user id we control. Pull tests of VerifyPathControlledByUserTest

Source from the content-addressed store, hash-verified

2174// Pull tests of VerifyPathControlledByUserTest() into a separate test class
2175// with a common SetUp() method.
2176class VerifyPathControlledByUserTest : public FileUtilTest {
2177 protected:
2178 virtual void SetUp() OVERRIDE {
2179 FileUtilTest::SetUp();
2180
2181 // Create a basic structure used by each test.
2182 // base_dir_
2183 // |-> sub_dir_
2184 // |-> text_file_
2185
2186 base_dir_ = temp_dir_.path().AppendASCII("base_dir");
2187 ASSERT_TRUE(CreateDirectory(base_dir_));
2188
2189 sub_dir_ = base_dir_.AppendASCII("sub_dir");
2190 ASSERT_TRUE(CreateDirectory(sub_dir_));
2191
2192 text_file_ = sub_dir_.AppendASCII("file.txt");
2193 CreateTextFile(text_file_, L"This text file has some text in it.");
2194
2195 // Get the user and group files are created with from |base_dir_|.
2196 struct stat stat_buf;
2197 ASSERT_EQ(0, stat(base_dir_.value().c_str(), &stat_buf));
2198 uid_ = stat_buf.st_uid;
2199 ok_gids_.insert(stat_buf.st_gid);
2200 bad_gids_.insert(stat_buf.st_gid + 1);
2201
2202 ASSERT_EQ(uid_, geteuid()); // This process should be the owner.
2203
2204 // To ensure that umask settings do not cause the initial state
2205 // of permissions to be different from what we expect, explicitly
2206 // set permissions on the directories we create.
2207 // Make all files and directories non-world-writable.
2208
2209 // Users and group can read, write, traverse
2210 int enabled_permissions =
2211 FILE_PERMISSION_USER_MASK | FILE_PERMISSION_GROUP_MASK;
2212 // Other users can't read, write, traverse
2213 int disabled_permissions = FILE_PERMISSION_OTHERS_MASK;
2214
2215 ASSERT_NO_FATAL_FAILURE(
2216 ChangePosixFilePermissions(
2217 base_dir_, enabled_permissions, disabled_permissions));
2218 ASSERT_NO_FATAL_FAILURE(
2219 ChangePosixFilePermissions(
2220 sub_dir_, enabled_permissions, disabled_permissions));
2221 }
2222#if defined(OS_POSIX)
2223 virtual void TearDown() OVERRIDE {
2224 FileUtilTest::TearDown();
2225 }
2226#endif
2227
2228 FilePath base_dir_;
2229 FilePath sub_dir_;
2230 FilePath text_file_;
2231 uid_t uid_;
2232
2233 std::set<gid_t> ok_gids_;

Callers

nothing calls this directly

Calls 8

CreateDirectoryFunction · 0.85
CreateTextFileFunction · 0.85
AppendASCIIMethod · 0.80
statClass · 0.70
c_strMethod · 0.45
valueMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected