raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 1 | #include "mock_user_mgr.hpp" |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 2 | |
Ravi Teja | 417c089 | 2020-08-22 08:04:01 -0500 | [diff] [blame] | 3 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 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> |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 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: |
Ravi Teja | 417c089 | 2020-08-22 08:04:01 -0500 | [diff] [blame] | 24 | sdbusplus::SdBusMock sdbusMock; |
Patrick Williams | b3ef4e1 | 2022-07-22 19:26:55 -0500 | [diff] [blame^] | 25 | sdbusplus::bus_t bus; |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 26 | MockManager mockManager; |
| 27 | |
| 28 | TestUserMgr() : |
Ravi Teja | 417c089 | 2020-08-22 08:04:01 -0500 | [diff] [blame] | 29 | bus(sdbusplus::get_mocked_new(&sdbusMock)), mockManager(bus, objpath) |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 30 | {} |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 31 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 32 | void createLocalUser(const std::string& userName, |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 33 | std::vector<std::string> groupNames, |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 34 | const std::string& priv, bool enabled) |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 35 | { |
P Dheeraj Srujan Kumar | b01e2fe | 2021-12-13 09:43:28 +0530 | [diff] [blame] | 36 | sdbusplus::message::object_path tempObjPath(usersObjPath); |
| 37 | tempObjPath /= userName; |
| 38 | std::string userObj(tempObjPath); |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 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; |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 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"; |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 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 | } |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 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 | } |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 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"])); |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 112 | EXPECT_EQ(false, std::get<bool>(userInfo["UserPasswordExpired"])); |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 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"; |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 138 | |
| 139 | EXPECT_CALL(mockManager, getLdapGroupName(userName)) |
| 140 | .WillRepeatedly(Return(ldapGroup)); |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 141 | // Create LDAP config object without privilege mapper |
| 142 | DbusUserObj object = createLdapConfigObjectWithoutPrivilegeMapper(); |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 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 |