1 #include "mock_user_mgr.hpp"
2 
3 #include <sdbusplus/test/sdbus_mock.hpp>
4 #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>
10 
11 namespace phosphor
12 {
13 namespace user
14 {
15 
16 using ::testing::Return;
17 
18 using InternalFailure =
19     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
20 
21 class TestUserMgr : public testing::Test
22 {
23   public:
24     sdbusplus::SdBusMock sdbusMock;
25     sdbusplus::bus::bus bus;
26     MockManager mockManager;
27 
28     TestUserMgr() :
29         bus(sdbusplus::get_mocked_new(&sdbusMock)), mockManager(bus, objpath)
30     {}
31 
32     void createLocalUser(const std::string& userName,
33                          std::vector<std::string> groupNames,
34                          const std::string& priv, bool enabled)
35     {
36         sdbusplus::message::object_path tempObjPath(usersObjPath);
37         tempObjPath /= userName;
38         std::string userObj(tempObjPath);
39         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;
49 
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";
61         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     }
70 
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     }
84 };
85 
86 TEST_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 
96 TEST_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"]));
112     EXPECT_EQ(false, std::get<bool>(userInfo["UserPasswordExpired"]));
113     EXPECT_EQ(false, std::get<bool>(userInfo["RemoteUser"]));
114 }
115 
116 TEST_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 
133 TEST_F(TestUserMgr, ldapUserWithoutPrivMapper)
134 {
135     UserInfoMap userInfo;
136     std::string userName = "ldapUser";
137     std::string ldapGroup = "ldapGroup";
138 
139     EXPECT_CALL(mockManager, getLdapGroupName(userName))
140         .WillRepeatedly(Return(ldapGroup));
141     // Create LDAP config object without privilege mapper
142     DbusUserObj object = createLdapConfigObjectWithoutPrivilegeMapper();
143     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
151