xref: /openbmc/phosphor-user-manager/test/execute_cmd_test.cpp (revision 16f0efa1d937ffd1565282cfc30231e160b36a42)
1*16f0efa1SPatrick Williams #include "user_mgr.hpp"
2*16f0efa1SPatrick Williams 
3*16f0efa1SPatrick Williams #include <gtest/gtest.h>
4*16f0efa1SPatrick Williams 
5*16f0efa1SPatrick Williams TEST(ExecuteCmdTest, CommandReturnsEmptyOutput)
6*16f0efa1SPatrick Williams {
7*16f0efa1SPatrick Williams     std::vector<std::string> output = phosphor::user::executeCmd("/bin/true");
8*16f0efa1SPatrick Williams     ASSERT_TRUE(output.empty());
9*16f0efa1SPatrick Williams }
10*16f0efa1SPatrick Williams 
11*16f0efa1SPatrick Williams TEST(ExecuteCmdTest, CommandWithArgs)
12*16f0efa1SPatrick Williams {
13*16f0efa1SPatrick Williams     std::vector<std::string> output = phosphor::user::executeCmd(
14*16f0efa1SPatrick Williams         "/bin/echo", "testing", "with", "multiple", "args");
15*16f0efa1SPatrick Williams     ASSERT_EQ(output.size(), 1);
16*16f0efa1SPatrick Williams     EXPECT_EQ(output[0], "testing with multiple args");
17*16f0efa1SPatrick Williams }
18*16f0efa1SPatrick Williams 
19*16f0efa1SPatrick Williams TEST(ExecuteCmdTest, CommandReturnsOutput)
20*16f0efa1SPatrick Williams {
21*16f0efa1SPatrick Williams     std::vector<std::string> output =
22*16f0efa1SPatrick Williams         phosphor::user::executeCmd("/bin/echo", "-e", "hello\\nworld");
23*16f0efa1SPatrick Williams     ASSERT_EQ(output.size(), 2);
24*16f0efa1SPatrick Williams     EXPECT_EQ(output[0], "hello");
25*16f0efa1SPatrick Williams     EXPECT_EQ(output[1], "world");
26*16f0efa1SPatrick Williams }
27*16f0efa1SPatrick Williams 
28*16f0efa1SPatrick Williams TEST(ExecuteCmdTest, NonExistentCommand)
29*16f0efa1SPatrick Williams {
30*16f0efa1SPatrick Williams     EXPECT_THROW(phosphor::user::executeCmd("/path/to/nonexistent_command"),
31*16f0efa1SPatrick Williams                  boost::process::process_error);
32*16f0efa1SPatrick Williams }
33*16f0efa1SPatrick Williams 
34*16f0efa1SPatrick Williams TEST(ExecuteCmdTest, CommandReturnsNonZeroExitCode)
35*16f0efa1SPatrick Williams {
36*16f0efa1SPatrick Williams     EXPECT_THROW(
37*16f0efa1SPatrick Williams         phosphor::user::executeCmd("/bin/false"),
38*16f0efa1SPatrick Williams         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure);
39*16f0efa1SPatrick Williams }
40