user add/delete: add unit tests
This commit adds the test infrastructure such that we are able to
manipulate groups and users inside the unit test container.
To make it happen, the following changes are made:
1. executeCmd was moved to header
2. made the two userDelete and userAdd function virtual; this is needed
since on BMC the daemon is run as root by default; however on unit test
container, it's run as the current user on VMs. Thus in the unit test,
these two functions are overridden to dummy commands.
It can be shown that the unit test coverage increases a lot.
Tested: unit test pasesd
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: I7be636d8acd487242831d1c74941e848e508b8f9
diff --git a/test/user_mgr_test.cpp b/test/user_mgr_test.cpp
index 588c2fa..16d6617 100644
--- a/test/user_mgr_test.cpp
+++ b/test/user_mgr_test.cpp
@@ -253,6 +253,12 @@
// Set config files to test files
pamPasswdConfigFile = tempPamConfigFile;
pamAuthConfigFile = tempPamConfigFile;
+
+ ON_CALL(*this, executeUserAdd).WillByDefault(testing::Return());
+
+ ON_CALL(*this, executeUserDelete).WillByDefault(testing::Return());
+
+ ON_CALL(*this, getIpmiUsersCount).WillByDefault(testing::Return(0));
}
~UserMgrInTest() override
@@ -260,6 +266,13 @@
EXPECT_NO_THROW(removeFile(tempPamConfigFile));
}
+ MOCK_METHOD(void, executeUserAdd, (const char*, const char*, bool, bool),
+ (override));
+
+ MOCK_METHOD(void, executeUserDelete, (const char*), (override));
+
+ MOCK_METHOD(size_t, getIpmiUsersCount, (), (override));
+
protected:
static sdbusplus::bus_t busInTest;
std::string tempPamConfigFile;
@@ -360,5 +373,54 @@
sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
}
+TEST_F(UserMgrInTest, UserAddNotRootFailedWithInternalFailure)
+{
+ EXPECT_THROW(
+ UserMgr::executeUserAdd("user0", "ipmi,ssh", true, true),
+ sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure);
+}
+
+TEST_F(UserMgrInTest, UserDeleteNotRootFailedWithInternalFailure)
+{
+ EXPECT_THROW(
+ UserMgr::executeUserDelete("user0"),
+ sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure);
+}
+
+TEST_F(UserMgrInTest,
+ ThrowForMaxGrpUserCountThrowsNoResourceWhenIpmiUserExceedLimit)
+{
+ EXPECT_CALL(*this, getIpmiUsersCount()).WillOnce(Return(ipmiMaxUsers));
+ EXPECT_THROW(
+ throwForMaxGrpUserCount({"ipmi"}),
+ sdbusplus::xyz::openbmc_project::User::Common::Error::NoResource);
+}
+
+TEST_F(UserMgrInTest, CreateUserThrowsInternalFailureWhenExecuteUserAddFails)
+{
+ EXPECT_CALL(*this, executeUserAdd)
+ .WillOnce(testing::Throw(
+ sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure()));
+ EXPECT_THROW(
+ createUser("whatever", {"redfish"}, "", true),
+ sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure);
+}
+
+TEST_F(UserMgrInTest, DeleteUserThrowsInternalFailureWhenExecuteUserDeleteFails)
+{
+ std::string username = "user";
+ EXPECT_NO_THROW(
+ UserMgr::createUser(username, {"redfish", "ssh"}, "priv-user", true));
+ EXPECT_CALL(*this, executeUserDelete(testing::StrEq(username)))
+ .WillOnce(testing::Throw(
+ sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure()))
+ .WillOnce(testing::DoDefault());
+
+ EXPECT_THROW(
+ deleteUser(username),
+ sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure);
+ EXPECT_NO_THROW(UserMgr::deleteUser(username));
+}
+
} // namespace user
} // namespace phosphor