xref: /openbmc/phosphor-user-manager/test/execute_cmd_test.cpp (revision 2746e0c0e6caac95e1b690b3757623e81470c370)
116f0efa1SPatrick Williams #include "user_mgr.hpp"
216f0efa1SPatrick Williams 
316f0efa1SPatrick Williams #include <gtest/gtest.h>
416f0efa1SPatrick Williams 
TEST(ExecuteCmdTest,CommandReturnsEmptyOutput)516f0efa1SPatrick Williams TEST(ExecuteCmdTest, CommandReturnsEmptyOutput)
616f0efa1SPatrick Williams {
716f0efa1SPatrick Williams     std::vector<std::string> output = phosphor::user::executeCmd("/bin/true");
816f0efa1SPatrick Williams     ASSERT_TRUE(output.empty());
916f0efa1SPatrick Williams }
1016f0efa1SPatrick Williams 
TEST(ExecuteCmdTest,CommandWithArgs)1116f0efa1SPatrick Williams TEST(ExecuteCmdTest, CommandWithArgs)
1216f0efa1SPatrick Williams {
1316f0efa1SPatrick Williams     std::vector<std::string> output = phosphor::user::executeCmd(
1416f0efa1SPatrick Williams         "/bin/echo", "testing", "with", "multiple", "args");
1516f0efa1SPatrick Williams     ASSERT_EQ(output.size(), 1);
1616f0efa1SPatrick Williams     EXPECT_EQ(output[0], "testing with multiple args");
1716f0efa1SPatrick Williams }
1816f0efa1SPatrick Williams 
TEST(ExecuteCmdTest,CommandReturnsOutput)1916f0efa1SPatrick Williams TEST(ExecuteCmdTest, CommandReturnsOutput)
2016f0efa1SPatrick Williams {
2116f0efa1SPatrick Williams     std::vector<std::string> output =
2216f0efa1SPatrick Williams         phosphor::user::executeCmd("/bin/echo", "-e", "hello\\nworld");
2316f0efa1SPatrick Williams     ASSERT_EQ(output.size(), 2);
2416f0efa1SPatrick Williams     EXPECT_EQ(output[0], "hello");
2516f0efa1SPatrick Williams     EXPECT_EQ(output[1], "world");
2616f0efa1SPatrick Williams }
2716f0efa1SPatrick Williams 
TEST(ExecuteCmdTest,NonExistentCommand)2816f0efa1SPatrick Williams TEST(ExecuteCmdTest, NonExistentCommand)
2916f0efa1SPatrick Williams {
30*2746e0c0SPatrick Williams     EXPECT_THROW(
31*2746e0c0SPatrick Williams         phosphor::user::executeCmd("/path/to/nonexistent_command"),
32*2746e0c0SPatrick Williams         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure);
3316f0efa1SPatrick Williams }
3416f0efa1SPatrick Williams 
TEST(ExecuteCmdTest,CommandReturnsNonZeroExitCode)3516f0efa1SPatrick Williams TEST(ExecuteCmdTest, CommandReturnsNonZeroExitCode)
3616f0efa1SPatrick Williams {
3716f0efa1SPatrick Williams     EXPECT_THROW(
3816f0efa1SPatrick Williams         phosphor::user::executeCmd("/bin/false"),
3916f0efa1SPatrick Williams         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure);
4016f0efa1SPatrick Williams }
41