user exist: simplify expression, add unit test
Simplified two boolean expression and added unit tests. The unit test
container has the root user by default and can be leveraged in the unit
test.
Tested: unit test passed.
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: Ia335ad9e14e5cdeca5c260ef67f308f7197be349
diff --git a/test/user_mgr_test.cpp b/test/user_mgr_test.cpp
index f16a9d6..15a56fc 100644
--- a/test/user_mgr_test.cpp
+++ b/test/user_mgr_test.cpp
@@ -310,5 +310,26 @@
EXPECT_EQ(setPamModuleArgValue("pam_tally2.so", "minlen", "16"), -1);
}
+TEST_F(UserMgrInTest, IsUserExistEmptyInputThrowsError)
+{
+ EXPECT_THROW(
+ isUserExist(""),
+ sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
+}
+
+TEST_F(UserMgrInTest, ThrowForUserDoesNotExistThrowsError)
+{
+ EXPECT_THROW(throwForUserDoesNotExist("whatever"),
+ sdbusplus::xyz::openbmc_project::User::Common::Error::
+ UserNameDoesNotExist);
+}
+
+TEST_F(UserMgrInTest, ThrowForUserExistsThrowsError)
+{
+ EXPECT_THROW(
+ throwForUserExists("root"),
+ sdbusplus::xyz::openbmc_project::User::Common::Error::UserNameExists);
+}
+
} // namespace user
} // namespace phosphor
diff --git a/user_mgr.cpp b/user_mgr.cpp
index 390981c..f2c500c 100644
--- a/user_mgr.cpp
+++ b/user_mgr.cpp
@@ -186,7 +186,7 @@
void UserMgr::throwForUserDoesNotExist(const std::string& userName)
{
- if (isUserExist(userName) == false)
+ if (!isUserExist(userName))
{
log<level::ERR>("User does not exist",
entry("USER_NAME=%s", userName.c_str()));
@@ -196,7 +196,7 @@
void UserMgr::throwForUserExists(const std::string& userName)
{
- if (isUserExist(userName) == true)
+ if (isUserExist(userName))
{
log<level::ERR>("User already exists",
entry("USER_NAME=%s", userName.c_str()));
@@ -1049,7 +1049,7 @@
{
UserInfoMap userInfo;
// Check whether the given user is local user or not.
- if (isUserExist(userName) == true)
+ if (isUserExist(userName))
{
const auto& user = usersList[userName];
userInfo.emplace("UserPrivilege", user.get()->userPrivilege());
diff --git a/user_mgr.hpp b/user_mgr.hpp
index ac163f7..dd73fff 100644
--- a/user_mgr.hpp
+++ b/user_mgr.hpp
@@ -66,6 +66,10 @@
bool removeStringFromCSV(std::string& csvStr, const std::string& delStr);
+template <typename... ArgTypes>
+static std::vector<std::string> executeCmd(const char* path,
+ ArgTypes&&... tArgs);
+
/** @class UserMgr
* @brief Responsible for managing user accounts over the D-Bus interface.
*/
@@ -220,6 +224,28 @@
const std::string& argName,
const std::string& argValue);
+ /** @brief check for user presence
+ * method to check for user existence
+ *
+ * @param[in] userName - name of the user
+ * @return -true if user exists and false if not.
+ */
+ bool isUserExist(const std::string& userName);
+
+ /** @brief check user exists
+ * method to check whether user exist, and throw if not.
+ *
+ * @param[in] userName - name of the user
+ */
+ void throwForUserDoesNotExist(const std::string& userName);
+
+ /** @brief check user does not exist
+ * method to check whether does not exist, and throw if exists.
+ *
+ * @param[in] userName - name of the user
+ */
+ void throwForUserExists(const std::string& userName);
+
private:
/** @brief sdbusplus handler */
sdbusplus::bus_t& bus;
@@ -255,28 +281,6 @@
*/
UserSSHLists getUserAndSshGrpList(void);
- /** @brief check for user presence
- * method to check for user existence
- *
- * @param[in] userName - name of the user
- * @return -true if user exists and false if not.
- */
- bool isUserExist(const std::string& userName);
-
- /** @brief check user exists
- * method to check whether user exist, and throw if not.
- *
- * @param[in] userName - name of the user
- */
- void throwForUserDoesNotExist(const std::string& userName);
-
- /** @brief check user does not exist
- * method to check whether does not exist, and throw if exists.
- *
- * @param[in] userName - name of the user
- */
- void throwForUserExists(const std::string& userName);
-
/** @brief check user name constraints
* method to check user name constraints and throw if failed.
*