blob: 3059ea374b92b886a76884e954d0ba80d48e5823 [file] [log] [blame]
raviteja-b8cc44052019-02-27 23:29:36 -06001#include "mock_user_mgr.hpp"
Patrick Williams9638afb2021-02-22 17:16:24 -06002
Ravi Teja417c0892020-08-22 08:04:01 -05003#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06004#include <xyz/openbmc_project/Common/error.hpp>
5#include <xyz/openbmc_project/User/Common/error.hpp>
6
7#include <exception>
8
9#include <gtest/gtest.h>
raviteja-b8cc44052019-02-27 23:29:36 -060010
11namespace phosphor
12{
13namespace user
14{
15
16using ::testing::Return;
17
18using InternalFailure =
19 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
20
21class TestUserMgr : public testing::Test
22{
23 public:
Ravi Teja417c0892020-08-22 08:04:01 -050024 sdbusplus::SdBusMock sdbusMock;
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050025 sdbusplus::bus_t bus;
raviteja-b8cc44052019-02-27 23:29:36 -060026 MockManager mockManager;
27
28 TestUserMgr() :
Ravi Teja417c0892020-08-22 08:04:01 -050029 bus(sdbusplus::get_mocked_new(&sdbusMock)), mockManager(bus, objpath)
Patrick Williams9638afb2021-02-22 17:16:24 -060030 {}
raviteja-b8cc44052019-02-27 23:29:36 -060031
Patrick Williams9638afb2021-02-22 17:16:24 -060032 void createLocalUser(const std::string& userName,
raviteja-b8cc44052019-02-27 23:29:36 -060033 std::vector<std::string> groupNames,
Patrick Williams9638afb2021-02-22 17:16:24 -060034 const std::string& priv, bool enabled)
raviteja-b8cc44052019-02-27 23:29:36 -060035 {
P Dheeraj Srujan Kumarb01e2fe2021-12-13 09:43:28 +053036 sdbusplus::message::object_path tempObjPath(usersObjPath);
37 tempObjPath /= userName;
38 std::string userObj(tempObjPath);
raviteja-b8cc44052019-02-27 23:29:36 -060039 mockManager.usersList.emplace(
40 userName, std::move(std::make_unique<phosphor::user::Users>(
41 mockManager.bus, userObj.c_str(), groupNames, priv,
42 enabled, mockManager)));
43 }
44
45 DbusUserObj createPrivilegeMapperDbusObject(void)
46 {
47 DbusUserObj object;
48 DbusUserObjValue objValue;
Ravi Teja5fe724a2019-05-07 05:14:42 -050049
50 DbusUserObjPath obj_path("/xyz/openbmc_project/user/ldap/openldap");
51 DbusUserPropVariant enabled(true);
52 DbusUserObjProperties property = {std::make_pair("Enabled", enabled)};
53 std::string intf = "xyz.openbmc_project.Object.Enable";
54 objValue.emplace(intf, property);
55 object.emplace(obj_path, objValue);
56
57 DbusUserObjPath object_path(
58 "/xyz/openbmc_project/user/ldap/openldap/role_map/1");
59 std::string group = "ldapGroup";
60 std::string priv = "priv-admin";
raviteja-b8cc44052019-02-27 23:29:36 -060061 DbusUserObjProperties properties = {std::make_pair("GroupName", group),
62 std::make_pair("Privilege", priv)};
63 std::string interface = "xyz.openbmc_project.User.PrivilegeMapperEntry";
64
65 objValue.emplace(interface, properties);
66 object.emplace(object_path, objValue);
67
68 return object;
69 }
Ravi Teja5fe724a2019-05-07 05:14:42 -050070
71 DbusUserObj createLdapConfigObjectWithoutPrivilegeMapper(void)
72 {
73 DbusUserObj object;
74 DbusUserObjValue objValue;
75
76 DbusUserObjPath obj_path("/xyz/openbmc_project/user/ldap/openldap");
77 DbusUserPropVariant enabled(true);
78 DbusUserObjProperties property = {std::make_pair("Enabled", enabled)};
79 std::string intf = "xyz.openbmc_project.Object.Enable";
80 objValue.emplace(intf, property);
81 object.emplace(obj_path, objValue);
82 return object;
83 }
raviteja-b8cc44052019-02-27 23:29:36 -060084};
85
86TEST_F(TestUserMgr, ldapEntryDoesNotExist)
87{
88 std::string userName = "user";
89 UserInfoMap userInfo;
90
91 EXPECT_CALL(mockManager, getLdapGroupName(userName))
92 .WillRepeatedly(Return(""));
93 EXPECT_THROW(userInfo = mockManager.getUserInfo(userName), InternalFailure);
94}
95
96TEST_F(TestUserMgr, localUser)
97{
98 UserInfoMap userInfo;
99 std::string userName = "testUser";
100 std::string privilege = "priv-admin";
101 std::vector<std::string> groups{"testGroup"};
102 // Create local user
103 createLocalUser(userName, groups, privilege, true);
104 EXPECT_CALL(mockManager, userLockedForFailedAttempt(userName)).Times(1);
105 userInfo = mockManager.getUserInfo(userName);
106
107 EXPECT_EQ(privilege, std::get<std::string>(userInfo["UserPrivilege"]));
108 EXPECT_EQ(groups,
109 std::get<std::vector<std::string>>(userInfo["UserGroups"]));
110 EXPECT_EQ(true, std::get<bool>(userInfo["UserEnabled"]));
111 EXPECT_EQ(false, std::get<bool>(userInfo["UserLockedForFailedAttempt"]));
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600112 EXPECT_EQ(false, std::get<bool>(userInfo["UserPasswordExpired"]));
raviteja-b8cc44052019-02-27 23:29:36 -0600113 EXPECT_EQ(false, std::get<bool>(userInfo["RemoteUser"]));
114}
115
116TEST_F(TestUserMgr, ldapUserWithPrivMapper)
117{
118 UserInfoMap userInfo;
119 std::string userName = "ldapUser";
120 std::string ldapGroup = "ldapGroup";
121
122 EXPECT_CALL(mockManager, getLdapGroupName(userName))
123 .WillRepeatedly(Return(ldapGroup));
124 // Create privilege mapper dbus object
125 DbusUserObj object = createPrivilegeMapperDbusObject();
126 EXPECT_CALL(mockManager, getPrivilegeMapperObject())
127 .WillRepeatedly(Return(object));
128 userInfo = mockManager.getUserInfo(userName);
129 EXPECT_EQ(true, std::get<bool>(userInfo["RemoteUser"]));
130 EXPECT_EQ("priv-admin", std::get<std::string>(userInfo["UserPrivilege"]));
131}
132
133TEST_F(TestUserMgr, ldapUserWithoutPrivMapper)
134{
135 UserInfoMap userInfo;
136 std::string userName = "ldapUser";
137 std::string ldapGroup = "ldapGroup";
raviteja-b8cc44052019-02-27 23:29:36 -0600138
139 EXPECT_CALL(mockManager, getLdapGroupName(userName))
140 .WillRepeatedly(Return(ldapGroup));
Ravi Teja5fe724a2019-05-07 05:14:42 -0500141 // Create LDAP config object without privilege mapper
142 DbusUserObj object = createLdapConfigObjectWithoutPrivilegeMapper();
raviteja-b8cc44052019-02-27 23:29:36 -0600143 EXPECT_CALL(mockManager, getPrivilegeMapperObject())
144 .WillRepeatedly(Return(object));
145 userInfo = mockManager.getUserInfo(userName);
146 EXPECT_EQ(true, std::get<bool>(userInfo["RemoteUser"]));
147 EXPECT_EQ("", std::get<std::string>(userInfo["UserPrivilege"]));
148}
149} // namespace user
150} // namespace phosphor