Commit 74abdd83 authored by Zhangyi Chen's avatar Zhangyi Chen

Fix the failure of FileUtilTest when the user is root

parent e5e737c3
...@@ -276,7 +276,7 @@ static void* string_appender(void* arg) { ...@@ -276,7 +276,7 @@ static void* string_appender(void* arg) {
int count = 0; int count = 0;
std::string id = butil::string_printf("%lld", (long long)pthread_self()); std::string id = butil::string_printf("%lld", (long long)pthread_self());
std::string tmp = "a"; std::string tmp = "a";
for (count = 0; !g_stop; ++count) { for (count = 0; !count || !g_stop; ++count) {
*cater << id << ":"; *cater << id << ":";
for (char c = 'a'; c <= 'z'; ++c) { for (char c = 'a'; c <= 'z'; ++c) {
tmp[0] = c; tmp[0] = c;
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#endif #endif
#include <algorithm> #include <algorithm>
...@@ -182,11 +183,33 @@ const int FILES_AND_DIRECTORIES = ...@@ -182,11 +183,33 @@ const int FILES_AND_DIRECTORIES =
class FileUtilTest : public testing::Test { class FileUtilTest : public testing::Test {
protected: protected:
virtual void SetUp() OVERRIDE { virtual void SetUp() OVERRIDE {
#if defined(OS_POSIX)
if (getuid() == 0) {
is_root_ = true;
ASSERT_EQ(0, setegid(65534));
ASSERT_EQ(0, seteuid(65534));
} else {
is_root_ = false;
}
#endif
testing::Test::SetUp(); testing::Test::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
} }
#if defined(OS_POSIX)
virtual void TearDown() OVERRIDE {
if (is_root_) {
ASSERT_EQ(0, seteuid(0));
ASSERT_EQ(0, setegid(0));
is_root_ = false;
}
testing::Test::TearDown();
}
#endif
ScopedTempDir temp_dir_; ScopedTempDir temp_dir_;
#if defined(OS_POSIX)
bool is_root_;
#endif
}; };
// Collects all the results from the given file enumerator, and provides an // Collects all the results from the given file enumerator, and provides an
...@@ -767,7 +790,9 @@ TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) { ...@@ -767,7 +790,9 @@ TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER); EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
// Make sure the file can't be write. // Make sure the file can't be write.
EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length())); EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length()));
if (!is_root_) {
EXPECT_FALSE(PathIsWritable(file_name)); EXPECT_FALSE(PathIsWritable(file_name));
}
// Give read permission. // Give read permission.
EXPECT_TRUE(SetPosixFilePermissions(file_name, EXPECT_TRUE(SetPosixFilePermissions(file_name,
...@@ -2176,7 +2201,7 @@ class VerifyPathControlledByUserTest : public FileUtilTest { ...@@ -2176,7 +2201,7 @@ class VerifyPathControlledByUserTest : public FileUtilTest {
ok_gids_.insert(stat_buf.st_gid); ok_gids_.insert(stat_buf.st_gid);
bad_gids_.insert(stat_buf.st_gid + 1); bad_gids_.insert(stat_buf.st_gid + 1);
ASSERT_EQ(uid_, getuid()); // This process should be the owner. ASSERT_EQ(uid_, geteuid()); // This process should be the owner.
// To ensure that umask settings do not cause the initial state // To ensure that umask settings do not cause the initial state
// of permissions to be different from what we expect, explicitly // of permissions to be different from what we expect, explicitly
...@@ -2196,6 +2221,11 @@ class VerifyPathControlledByUserTest : public FileUtilTest { ...@@ -2196,6 +2221,11 @@ class VerifyPathControlledByUserTest : public FileUtilTest {
ChangePosixFilePermissions( ChangePosixFilePermissions(
sub_dir_, enabled_permissions, disabled_permissions)); sub_dir_, enabled_permissions, disabled_permissions));
} }
#if defined(OS_POSIX)
virtual void TearDown() OVERRIDE {
FileUtilTest::TearDown();
}
#endif
FilePath base_dir_; FilePath base_dir_;
FilePath sub_dir_; FilePath sub_dir_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment