Implement unit test for getUserInfo function
in phosphor-user-manager
added testcases
1.unit test for ldap entry does not exist
2.unit test for local user.
3.unit test for ldap user with privilege mapper entry
4.unit test for ldap user without privilege mapper entry
gerrit link for getUserInfo function
https://gerrit.openbmc-project.xyz/#/c/openbmc/phosphor-user-manager/+/18132/
Change-Id: Idfd7e1ffeb8acfebab590c8c5fd6adc9bcf218dc
Signed-off-by: Ravi Teja <raviteja28031990@gmail.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index a03ce28..581b1eb 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -41,3 +41,11 @@
ldap_mapper_test_LDADD = $(top_builddir)/phosphor-ldap-mapper/ldap_mapper_entry.o \
$(top_builddir)/phosphor-ldap-mapper/ldap_mapper_mgr.o \
$(top_builddir)/phosphor-ldap-mapper/ldap_mapper_serialize.o
+
+check_PROGRAMS += user_mgr_test
+user_mgr_test_CPPFLAGS = $(cppflags)
+user_mgr_test_CXXFLAGS = $(cxxflags) $(GTEST_CPPFLAGS)
+user_mgr_test_LDFLAGS = $(ldflags) $(GTEST_LIBS) $(GMOCK_LIBS) -lgmock
+user_mgr_test_SOURCES = user_mgr_test.cpp
+user_mgr_test_LDADD = $(top_builddir)/user_mgr.o \
+ $(top_builddir)/users.o
diff --git a/test/mock_user_mgr.hpp b/test/mock_user_mgr.hpp
new file mode 100644
index 0000000..81f9065
--- /dev/null
+++ b/test/mock_user_mgr.hpp
@@ -0,0 +1,27 @@
+#include "user_mgr.hpp"
+#include <gmock/gmock.h>
+
+namespace phosphor
+{
+namespace user
+{
+
+constexpr auto objpath = "/dummy/user";
+
+class MockManager : public UserMgr
+{
+ public:
+ MockManager(sdbusplus::bus::bus& bus, const char* path) :
+ UserMgr(bus, objpath)
+ {
+ }
+
+ MOCK_METHOD1(getLdapGroupName, std::string(const std::string& userName));
+ MOCK_METHOD0(getPrivilegeMapperObject, DbusUserObj());
+ MOCK_METHOD1(userLockedForFailedAttempt, bool(const std::string& userName));
+
+ friend class TestUserMgr;
+};
+
+} // namespace user
+} // namespace phosphor
diff --git a/test/user_mgr_test.cpp b/test/user_mgr_test.cpp
new file mode 100644
index 0000000..c7f1f89
--- /dev/null
+++ b/test/user_mgr_test.cpp
@@ -0,0 +1,119 @@
+#include "mock_user_mgr.hpp"
+#include <xyz/openbmc_project/User/Common/error.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+#include <gtest/gtest.h>
+#include <exception>
+
+namespace phosphor
+{
+namespace user
+{
+
+using ::testing::Return;
+
+using InternalFailure =
+ sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+
+class TestUserMgr : public testing::Test
+{
+ public:
+ sdbusplus::bus::bus bus;
+ MockManager mockManager;
+
+ TestUserMgr() :
+ bus(sdbusplus::bus::new_default()), mockManager(bus, objpath)
+ {
+ }
+
+ void createLocalUser(const std::string &userName,
+ std::vector<std::string> groupNames,
+ const std::string &priv, bool enabled)
+ {
+ std::string userObj = std::string(usersObjPath) + "/" + userName;
+ mockManager.usersList.emplace(
+ userName, std::move(std::make_unique<phosphor::user::Users>(
+ mockManager.bus, userObj.c_str(), groupNames, priv,
+ enabled, mockManager)));
+ }
+
+ DbusUserObj createPrivilegeMapperDbusObject(void)
+ {
+ DbusUserObj object;
+ DbusUserObjValue objValue;
+ DbusUserObjPath object_path("/xyz/openbmc_project/user/ldap");
+ DbusUserPropVariant group("ldapGroup");
+ DbusUserPropVariant priv("priv-admin");
+ DbusUserObjProperties properties = {std::make_pair("GroupName", group),
+ std::make_pair("Privilege", priv)};
+ std::string interface = "xyz.openbmc_project.User.PrivilegeMapperEntry";
+
+ objValue.emplace(interface, properties);
+ object.emplace(object_path, objValue);
+
+ return object;
+ }
+};
+
+TEST_F(TestUserMgr, ldapEntryDoesNotExist)
+{
+ std::string userName = "user";
+ UserInfoMap userInfo;
+
+ EXPECT_CALL(mockManager, getLdapGroupName(userName))
+ .WillRepeatedly(Return(""));
+ EXPECT_THROW(userInfo = mockManager.getUserInfo(userName), InternalFailure);
+}
+
+TEST_F(TestUserMgr, localUser)
+{
+ UserInfoMap userInfo;
+ std::string userName = "testUser";
+ std::string privilege = "priv-admin";
+ std::vector<std::string> groups{"testGroup"};
+ // Create local user
+ createLocalUser(userName, groups, privilege, true);
+ EXPECT_CALL(mockManager, userLockedForFailedAttempt(userName)).Times(1);
+ userInfo = mockManager.getUserInfo(userName);
+
+ EXPECT_EQ(privilege, std::get<std::string>(userInfo["UserPrivilege"]));
+ EXPECT_EQ(groups,
+ std::get<std::vector<std::string>>(userInfo["UserGroups"]));
+ EXPECT_EQ(true, std::get<bool>(userInfo["UserEnabled"]));
+ EXPECT_EQ(false, std::get<bool>(userInfo["UserLockedForFailedAttempt"]));
+ EXPECT_EQ(false, std::get<bool>(userInfo["RemoteUser"]));
+}
+
+TEST_F(TestUserMgr, ldapUserWithPrivMapper)
+{
+ UserInfoMap userInfo;
+ std::string userName = "ldapUser";
+ std::string ldapGroup = "ldapGroup";
+
+ EXPECT_CALL(mockManager, getLdapGroupName(userName))
+ .WillRepeatedly(Return(ldapGroup));
+ // Create privilege mapper dbus object
+ DbusUserObj object = createPrivilegeMapperDbusObject();
+ EXPECT_CALL(mockManager, getPrivilegeMapperObject())
+ .WillRepeatedly(Return(object));
+ userInfo = mockManager.getUserInfo(userName);
+ EXPECT_EQ(true, std::get<bool>(userInfo["RemoteUser"]));
+ EXPECT_EQ("priv-admin", std::get<std::string>(userInfo["UserPrivilege"]));
+}
+
+TEST_F(TestUserMgr, ldapUserWithoutPrivMapper)
+{
+ UserInfoMap userInfo;
+ std::string userName = "ldapUser";
+ std::string ldapGroup = "ldapGroup";
+ DbusUserObj object;
+
+ EXPECT_CALL(mockManager, getLdapGroupName(userName))
+ .WillRepeatedly(Return(ldapGroup));
+ EXPECT_CALL(mockManager, getPrivilegeMapperObject())
+ .WillRepeatedly(Return(object));
+ userInfo = mockManager.getUserInfo(userName);
+ EXPECT_EQ(true, std::get<bool>(userInfo["RemoteUser"]));
+ EXPECT_EQ("", std::get<std::string>(userInfo["UserPrivilege"]));
+}
+} // namespace user
+} // namespace phosphor
diff --git a/user_mgr.hpp b/user_mgr.hpp
index c78174d..7409b73 100644
--- a/user_mgr.hpp
+++ b/user_mgr.hpp
@@ -153,7 +153,7 @@
* @param[in] - user name
* @return - true / false indicating user locked / un-locked
**/
- bool userLockedForFailedAttempt(const std::string &userName);
+ virtual bool userLockedForFailedAttempt(const std::string &userName);
/** @brief lists user locked state for failed attempt
*
@@ -318,20 +318,23 @@
*/
std::string getServiceName(std::string &&path, std::string &&intf);
+ protected:
/** @brief get LDAP group name
* method to get LDAP group name for the given LDAP user
*
* @param[in] - userName
* @return - group name
*/
- std::string getLdapGroupName(const std::string &userName);
+ virtual std::string getLdapGroupName(const std::string &userName);
/** @brief get privilege mapper object
* method to get dbus privilege mapper object
*
* @return - map of user object
*/
- DbusUserObj getPrivilegeMapperObject(void);
+ virtual DbusUserObj getPrivilegeMapperObject(void);
+
+ friend class TestUserMgr;
};
} // namespace user