blob: c7f1f89a13bcb80aa60254f7dff6069c132010e9 [file] [log] [blame]
raviteja-b8cc44052019-02-27 23:29:36 -06001#include "mock_user_mgr.hpp"
2#include <xyz/openbmc_project/User/Common/error.hpp>
3#include <xyz/openbmc_project/Common/error.hpp>
4#include <gtest/gtest.h>
5#include <exception>
6
7namespace phosphor
8{
9namespace user
10{
11
12using ::testing::Return;
13
14using InternalFailure =
15 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
16
17class TestUserMgr : public testing::Test
18{
19 public:
20 sdbusplus::bus::bus bus;
21 MockManager mockManager;
22
23 TestUserMgr() :
24 bus(sdbusplus::bus::new_default()), mockManager(bus, objpath)
25 {
26 }
27
28 void createLocalUser(const std::string &userName,
29 std::vector<std::string> groupNames,
30 const std::string &priv, bool enabled)
31 {
32 std::string userObj = std::string(usersObjPath) + "/" + userName;
33 mockManager.usersList.emplace(
34 userName, std::move(std::make_unique<phosphor::user::Users>(
35 mockManager.bus, userObj.c_str(), groupNames, priv,
36 enabled, mockManager)));
37 }
38
39 DbusUserObj createPrivilegeMapperDbusObject(void)
40 {
41 DbusUserObj object;
42 DbusUserObjValue objValue;
43 DbusUserObjPath object_path("/xyz/openbmc_project/user/ldap");
44 DbusUserPropVariant group("ldapGroup");
45 DbusUserPropVariant priv("priv-admin");
46 DbusUserObjProperties properties = {std::make_pair("GroupName", group),
47 std::make_pair("Privilege", priv)};
48 std::string interface = "xyz.openbmc_project.User.PrivilegeMapperEntry";
49
50 objValue.emplace(interface, properties);
51 object.emplace(object_path, objValue);
52
53 return object;
54 }
55};
56
57TEST_F(TestUserMgr, ldapEntryDoesNotExist)
58{
59 std::string userName = "user";
60 UserInfoMap userInfo;
61
62 EXPECT_CALL(mockManager, getLdapGroupName(userName))
63 .WillRepeatedly(Return(""));
64 EXPECT_THROW(userInfo = mockManager.getUserInfo(userName), InternalFailure);
65}
66
67TEST_F(TestUserMgr, localUser)
68{
69 UserInfoMap userInfo;
70 std::string userName = "testUser";
71 std::string privilege = "priv-admin";
72 std::vector<std::string> groups{"testGroup"};
73 // Create local user
74 createLocalUser(userName, groups, privilege, true);
75 EXPECT_CALL(mockManager, userLockedForFailedAttempt(userName)).Times(1);
76 userInfo = mockManager.getUserInfo(userName);
77
78 EXPECT_EQ(privilege, std::get<std::string>(userInfo["UserPrivilege"]));
79 EXPECT_EQ(groups,
80 std::get<std::vector<std::string>>(userInfo["UserGroups"]));
81 EXPECT_EQ(true, std::get<bool>(userInfo["UserEnabled"]));
82 EXPECT_EQ(false, std::get<bool>(userInfo["UserLockedForFailedAttempt"]));
83 EXPECT_EQ(false, std::get<bool>(userInfo["RemoteUser"]));
84}
85
86TEST_F(TestUserMgr, ldapUserWithPrivMapper)
87{
88 UserInfoMap userInfo;
89 std::string userName = "ldapUser";
90 std::string ldapGroup = "ldapGroup";
91
92 EXPECT_CALL(mockManager, getLdapGroupName(userName))
93 .WillRepeatedly(Return(ldapGroup));
94 // Create privilege mapper dbus object
95 DbusUserObj object = createPrivilegeMapperDbusObject();
96 EXPECT_CALL(mockManager, getPrivilegeMapperObject())
97 .WillRepeatedly(Return(object));
98 userInfo = mockManager.getUserInfo(userName);
99 EXPECT_EQ(true, std::get<bool>(userInfo["RemoteUser"]));
100 EXPECT_EQ("priv-admin", std::get<std::string>(userInfo["UserPrivilege"]));
101}
102
103TEST_F(TestUserMgr, ldapUserWithoutPrivMapper)
104{
105 UserInfoMap userInfo;
106 std::string userName = "ldapUser";
107 std::string ldapGroup = "ldapGroup";
108 DbusUserObj object;
109
110 EXPECT_CALL(mockManager, getLdapGroupName(userName))
111 .WillRepeatedly(Return(ldapGroup));
112 EXPECT_CALL(mockManager, getPrivilegeMapperObject())
113 .WillRepeatedly(Return(object));
114 userInfo = mockManager.getUserInfo(userName);
115 EXPECT_EQ(true, std::get<bool>(userInfo["RemoteUser"]));
116 EXPECT_EQ("", std::get<std::string>(userInfo["UserPrivilege"]));
117}
118} // namespace user
119} // namespace phosphor