name constraint check: add unit tests
This commit adds unit test for |ThrowForUserNameConstraints|. All lines
are covered by test now.
Tested: unit test passed
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: If1a0ff823ca0767f068030f205efdc1eb33e3029
diff --git a/test/user_mgr_test.cpp b/test/user_mgr_test.cpp
index 15a56fc..588c2fa 100644
--- a/test/user_mgr_test.cpp
+++ b/test/user_mgr_test.cpp
@@ -331,5 +331,34 @@
sdbusplus::xyz::openbmc_project::User::Common::Error::UserNameExists);
}
+TEST_F(
+ UserMgrInTest,
+ ThrowForUserNameConstraintsExceedIpmiMaxUserNameLenThrowsUserNameGroupFail)
+{
+ std::string strWith17Chars(17, 'A');
+ EXPECT_THROW(throwForUserNameConstraints(strWith17Chars, {"ipmi"}),
+ sdbusplus::xyz::openbmc_project::User::Common::Error::
+ UserNameGroupFail);
+}
+
+TEST_F(
+ UserMgrInTest,
+ ThrowForUserNameConstraintsExceedSystemMaxUserNameLenThrowsInvalidArgument)
+{
+ std::string strWith31Chars(31, 'A');
+ EXPECT_THROW(
+ throwForUserNameConstraints(strWith31Chars, {}),
+ sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
+}
+
+TEST_F(UserMgrInTest,
+ ThrowForUserNameConstraintsRegexMismatchThrowsInvalidArgument)
+{
+ std::string startWithNumber = "0ABC";
+ EXPECT_THROW(
+ throwForUserNameConstraints(startWithNumber, {"ipmi"}),
+ sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument);
+}
+
} // namespace user
} // namespace phosphor
diff --git a/user_mgr.hpp b/user_mgr.hpp
index dd73fff..d298bf0 100644
--- a/user_mgr.hpp
+++ b/user_mgr.hpp
@@ -246,6 +246,16 @@
*/
void throwForUserExists(const std::string& userName);
+ /** @brief check user name constraints
+ * method to check user name constraints and throw if failed.
+ *
+ * @param[in] userName - name of the user
+ * @param[in] groupNames - user groups
+ */
+ void
+ throwForUserNameConstraints(const std::string& userName,
+ const std::vector<std::string>& groupNames);
+
private:
/** @brief sdbusplus handler */
sdbusplus::bus_t& bus;
@@ -281,16 +291,6 @@
*/
UserSSHLists getUserAndSshGrpList(void);
- /** @brief check user name constraints
- * method to check user name constraints and throw if failed.
- *
- * @param[in] userName - name of the user
- * @param[in] groupNames - user groups
- */
- void
- throwForUserNameConstraints(const std::string& userName,
- const std::vector<std::string>& groupNames);
-
/** @brief check group user count
* method to check max group user count, and throw if limit reached
*