blob: 22a88376d28c7481eb5ae7f33b9f9e7986531692 [file] [log] [blame]
raviteja-b8cc44052019-02-27 23:29:36 -06001#include "mock_user_mgr.hpp"
Nan Zhoue47c09d2022-10-25 00:06:41 +00002#include "user_mgr.hpp"
Patrick Williams9638afb2021-02-22 17:16:24 -06003
Ravi Teja417c0892020-08-22 08:04:01 -05004#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06005#include <xyz/openbmc_project/Common/error.hpp>
6#include <xyz/openbmc_project/User/Common/error.hpp>
7
8#include <exception>
9
10#include <gtest/gtest.h>
raviteja-b8cc44052019-02-27 23:29:36 -060011
12namespace phosphor
13{
14namespace user
15{
16
17using ::testing::Return;
18
19using InternalFailure =
20 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
21
22class TestUserMgr : public testing::Test
23{
24 public:
Nan Zhou78d85042022-08-29 17:50:22 +000025 sdbusplus::SdBusMock sdBusMock;
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050026 sdbusplus::bus_t bus;
raviteja-b8cc44052019-02-27 23:29:36 -060027 MockManager mockManager;
28
29 TestUserMgr() :
Nan Zhou78d85042022-08-29 17:50:22 +000030 bus(sdbusplus::get_mocked_new(&sdBusMock)), mockManager(bus, objpath)
Patrick Williams9638afb2021-02-22 17:16:24 -060031 {}
raviteja-b8cc44052019-02-27 23:29:36 -060032
Patrick Williams9638afb2021-02-22 17:16:24 -060033 void createLocalUser(const std::string& userName,
raviteja-b8cc44052019-02-27 23:29:36 -060034 std::vector<std::string> groupNames,
Patrick Williams9638afb2021-02-22 17:16:24 -060035 const std::string& priv, bool enabled)
raviteja-b8cc44052019-02-27 23:29:36 -060036 {
P Dheeraj Srujan Kumarb01e2fe2021-12-13 09:43:28 +053037 sdbusplus::message::object_path tempObjPath(usersObjPath);
38 tempObjPath /= userName;
39 std::string userObj(tempObjPath);
raviteja-b8cc44052019-02-27 23:29:36 -060040 mockManager.usersList.emplace(
Nan Zhou78d85042022-08-29 17:50:22 +000041 userName, std::make_unique<phosphor::user::Users>(
raviteja-b8cc44052019-02-27 23:29:36 -060042 mockManager.bus, userObj.c_str(), groupNames, priv,
Nan Zhou78d85042022-08-29 17:50:22 +000043 enabled, mockManager));
raviteja-b8cc44052019-02-27 23:29:36 -060044 }
45
46 DbusUserObj createPrivilegeMapperDbusObject(void)
47 {
48 DbusUserObj object;
49 DbusUserObjValue objValue;
Ravi Teja5fe724a2019-05-07 05:14:42 -050050
Nan Zhou78d85042022-08-29 17:50:22 +000051 DbusUserObjPath objPath("/xyz/openbmc_project/user/ldap/openldap");
Ravi Teja5fe724a2019-05-07 05:14:42 -050052 DbusUserPropVariant enabled(true);
53 DbusUserObjProperties property = {std::make_pair("Enabled", enabled)};
54 std::string intf = "xyz.openbmc_project.Object.Enable";
55 objValue.emplace(intf, property);
Nan Zhou78d85042022-08-29 17:50:22 +000056 object.emplace(objPath, objValue);
Ravi Teja5fe724a2019-05-07 05:14:42 -050057
Nan Zhou78d85042022-08-29 17:50:22 +000058 DbusUserObjPath objectPath(
Ravi Teja5fe724a2019-05-07 05:14:42 -050059 "/xyz/openbmc_project/user/ldap/openldap/role_map/1");
60 std::string group = "ldapGroup";
61 std::string priv = "priv-admin";
raviteja-b8cc44052019-02-27 23:29:36 -060062 DbusUserObjProperties properties = {std::make_pair("GroupName", group),
63 std::make_pair("Privilege", priv)};
64 std::string interface = "xyz.openbmc_project.User.PrivilegeMapperEntry";
65
66 objValue.emplace(interface, properties);
Nan Zhou78d85042022-08-29 17:50:22 +000067 object.emplace(objectPath, objValue);
raviteja-b8cc44052019-02-27 23:29:36 -060068
69 return object;
70 }
Ravi Teja5fe724a2019-05-07 05:14:42 -050071
72 DbusUserObj createLdapConfigObjectWithoutPrivilegeMapper(void)
73 {
74 DbusUserObj object;
75 DbusUserObjValue objValue;
76
Nan Zhou78d85042022-08-29 17:50:22 +000077 DbusUserObjPath objPath("/xyz/openbmc_project/user/ldap/openldap");
Ravi Teja5fe724a2019-05-07 05:14:42 -050078 DbusUserPropVariant enabled(true);
79 DbusUserObjProperties property = {std::make_pair("Enabled", enabled)};
80 std::string intf = "xyz.openbmc_project.Object.Enable";
81 objValue.emplace(intf, property);
Nan Zhou78d85042022-08-29 17:50:22 +000082 object.emplace(objPath, objValue);
Ravi Teja5fe724a2019-05-07 05:14:42 -050083 return object;
84 }
raviteja-b8cc44052019-02-27 23:29:36 -060085};
86
87TEST_F(TestUserMgr, ldapEntryDoesNotExist)
88{
89 std::string userName = "user";
90 UserInfoMap userInfo;
91
92 EXPECT_CALL(mockManager, getLdapGroupName(userName))
93 .WillRepeatedly(Return(""));
94 EXPECT_THROW(userInfo = mockManager.getUserInfo(userName), InternalFailure);
95}
96
97TEST_F(TestUserMgr, localUser)
98{
99 UserInfoMap userInfo;
100 std::string userName = "testUser";
101 std::string privilege = "priv-admin";
102 std::vector<std::string> groups{"testGroup"};
103 // Create local user
104 createLocalUser(userName, groups, privilege, true);
105 EXPECT_CALL(mockManager, userLockedForFailedAttempt(userName)).Times(1);
106 userInfo = mockManager.getUserInfo(userName);
107
108 EXPECT_EQ(privilege, std::get<std::string>(userInfo["UserPrivilege"]));
109 EXPECT_EQ(groups,
110 std::get<std::vector<std::string>>(userInfo["UserGroups"]));
111 EXPECT_EQ(true, std::get<bool>(userInfo["UserEnabled"]));
112 EXPECT_EQ(false, std::get<bool>(userInfo["UserLockedForFailedAttempt"]));
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600113 EXPECT_EQ(false, std::get<bool>(userInfo["UserPasswordExpired"]));
raviteja-b8cc44052019-02-27 23:29:36 -0600114 EXPECT_EQ(false, std::get<bool>(userInfo["RemoteUser"]));
115}
116
117TEST_F(TestUserMgr, ldapUserWithPrivMapper)
118{
119 UserInfoMap userInfo;
120 std::string userName = "ldapUser";
121 std::string ldapGroup = "ldapGroup";
122
123 EXPECT_CALL(mockManager, getLdapGroupName(userName))
124 .WillRepeatedly(Return(ldapGroup));
125 // Create privilege mapper dbus object
126 DbusUserObj object = createPrivilegeMapperDbusObject();
127 EXPECT_CALL(mockManager, getPrivilegeMapperObject())
128 .WillRepeatedly(Return(object));
129 userInfo = mockManager.getUserInfo(userName);
130 EXPECT_EQ(true, std::get<bool>(userInfo["RemoteUser"]));
131 EXPECT_EQ("priv-admin", std::get<std::string>(userInfo["UserPrivilege"]));
132}
133
134TEST_F(TestUserMgr, ldapUserWithoutPrivMapper)
135{
136 UserInfoMap userInfo;
137 std::string userName = "ldapUser";
138 std::string ldapGroup = "ldapGroup";
raviteja-b8cc44052019-02-27 23:29:36 -0600139
140 EXPECT_CALL(mockManager, getLdapGroupName(userName))
141 .WillRepeatedly(Return(ldapGroup));
Ravi Teja5fe724a2019-05-07 05:14:42 -0500142 // Create LDAP config object without privilege mapper
143 DbusUserObj object = createLdapConfigObjectWithoutPrivilegeMapper();
raviteja-b8cc44052019-02-27 23:29:36 -0600144 EXPECT_CALL(mockManager, getPrivilegeMapperObject())
145 .WillRepeatedly(Return(object));
146 userInfo = mockManager.getUserInfo(userName);
147 EXPECT_EQ(true, std::get<bool>(userInfo["RemoteUser"]));
148 EXPECT_EQ("", std::get<std::string>(userInfo["UserPrivilege"]));
149}
Nan Zhoue47c09d2022-10-25 00:06:41 +0000150
151TEST(GetCSVFromVector, EmptyVectorReturnsEmptyString)
152{
153 EXPECT_EQ(getCSVFromVector({}), "");
154}
155
156TEST(GetCSVFromVector, ElementsAreJoinedByComma)
157{
158 EXPECT_EQ(getCSVFromVector(std::vector<std::string>{"123"}), "123");
159 EXPECT_EQ(getCSVFromVector(std::vector<std::string>{"123", "456"}),
160 "123,456");
161}
162
raviteja-b8cc44052019-02-27 23:29:36 -0600163} // namespace user
164} // namespace phosphor