raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 1 | #include "mock_user_mgr.hpp" |
Nan Zhou | e47c09d | 2022-10-25 00:06:41 +0000 | [diff] [blame^] | 2 | #include "user_mgr.hpp" |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 3 | |
Ravi Teja | 417c089 | 2020-08-22 08:04:01 -0500 | [diff] [blame] | 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 5 | #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-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace user |
| 15 | { |
| 16 | |
| 17 | using ::testing::Return; |
| 18 | |
| 19 | using InternalFailure = |
| 20 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 21 | |
| 22 | class TestUserMgr : public testing::Test |
| 23 | { |
| 24 | public: |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 25 | sdbusplus::SdBusMock sdBusMock; |
Patrick Williams | b3ef4e1 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 26 | sdbusplus::bus_t bus; |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 27 | MockManager mockManager; |
| 28 | |
| 29 | TestUserMgr() : |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 30 | bus(sdbusplus::get_mocked_new(&sdBusMock)), mockManager(bus, objpath) |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 31 | {} |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 32 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 33 | void createLocalUser(const std::string& userName, |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 34 | std::vector<std::string> groupNames, |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 35 | const std::string& priv, bool enabled) |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 36 | { |
P Dheeraj Srujan Kumar | b01e2fe | 2021-12-13 09:43:28 +0530 | [diff] [blame] | 37 | sdbusplus::message::object_path tempObjPath(usersObjPath); |
| 38 | tempObjPath /= userName; |
| 39 | std::string userObj(tempObjPath); |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 40 | mockManager.usersList.emplace( |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 41 | userName, std::make_unique<phosphor::user::Users>( |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 42 | mockManager.bus, userObj.c_str(), groupNames, priv, |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 43 | enabled, mockManager)); |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | DbusUserObj createPrivilegeMapperDbusObject(void) |
| 47 | { |
| 48 | DbusUserObj object; |
| 49 | DbusUserObjValue objValue; |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 50 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 51 | DbusUserObjPath objPath("/xyz/openbmc_project/user/ldap/openldap"); |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 52 | 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 Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 56 | object.emplace(objPath, objValue); |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 57 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 58 | DbusUserObjPath objectPath( |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 59 | "/xyz/openbmc_project/user/ldap/openldap/role_map/1"); |
| 60 | std::string group = "ldapGroup"; |
| 61 | std::string priv = "priv-admin"; |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 62 | 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 Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 67 | object.emplace(objectPath, objValue); |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 68 | |
| 69 | return object; |
| 70 | } |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 71 | |
| 72 | DbusUserObj createLdapConfigObjectWithoutPrivilegeMapper(void) |
| 73 | { |
| 74 | DbusUserObj object; |
| 75 | DbusUserObjValue objValue; |
| 76 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 77 | DbusUserObjPath objPath("/xyz/openbmc_project/user/ldap/openldap"); |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 78 | 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 Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 82 | object.emplace(objPath, objValue); |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 83 | return object; |
| 84 | } |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | TEST_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 | |
| 97 | TEST_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 Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 113 | EXPECT_EQ(false, std::get<bool>(userInfo["UserPasswordExpired"])); |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 114 | EXPECT_EQ(false, std::get<bool>(userInfo["RemoteUser"])); |
| 115 | } |
| 116 | |
| 117 | TEST_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 | |
| 134 | TEST_F(TestUserMgr, ldapUserWithoutPrivMapper) |
| 135 | { |
| 136 | UserInfoMap userInfo; |
| 137 | std::string userName = "ldapUser"; |
| 138 | std::string ldapGroup = "ldapGroup"; |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 139 | |
| 140 | EXPECT_CALL(mockManager, getLdapGroupName(userName)) |
| 141 | .WillRepeatedly(Return(ldapGroup)); |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 142 | // Create LDAP config object without privilege mapper |
| 143 | DbusUserObj object = createLdapConfigObjectWithoutPrivilegeMapper(); |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 144 | 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 Zhou | e47c09d | 2022-10-25 00:06:41 +0000 | [diff] [blame^] | 150 | |
| 151 | TEST(GetCSVFromVector, EmptyVectorReturnsEmptyString) |
| 152 | { |
| 153 | EXPECT_EQ(getCSVFromVector({}), ""); |
| 154 | } |
| 155 | |
| 156 | TEST(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-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 163 | } // namespace user |
| 164 | } // namespace phosphor |