John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 1 | #include "aos/util/run_command.h" |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 5 | #include "aos/util/thread.h" |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 6 | |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 7 | namespace aos { |
| 8 | namespace util { |
| 9 | namespace testing { |
| 10 | |
| 11 | TEST(RunCommandTest, True) { |
| 12 | int result = RunCommand("true"); |
| 13 | ASSERT_NE(-1, result); |
| 14 | ASSERT_TRUE(WIFEXITED(result)); |
| 15 | EXPECT_EQ(0, WEXITSTATUS(result)); |
| 16 | } |
| 17 | |
| 18 | TEST(RunCommandTest, False) { |
| 19 | int result = RunCommand("false"); |
| 20 | ASSERT_NE(-1, result); |
| 21 | ASSERT_TRUE(WIFEXITED(result)); |
| 22 | EXPECT_EQ(1, WEXITSTATUS(result)); |
| 23 | } |
| 24 | |
| 25 | TEST(RunCommandTest, CommandNotFound) { |
| 26 | int result = RunCommand("ajflkjasdlfa"); |
| 27 | ASSERT_NE(-1, result); |
| 28 | ASSERT_TRUE(WIFEXITED(result)); |
| 29 | EXPECT_EQ(127, WEXITSTATUS(result)); |
| 30 | } |
| 31 | |
| 32 | TEST(RunCommandTest, KilledBySignal) { |
| 33 | int result = RunCommand("kill -QUIT $$"); |
| 34 | ASSERT_NE(-1, result); |
| 35 | ASSERT_TRUE(WIFSIGNALED(result)); |
| 36 | EXPECT_EQ(SIGQUIT, WTERMSIG(result)); |
| 37 | } |
| 38 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 39 | TEST(RunCommandTest, MultipleThreads) { |
| 40 | int result1, result2; |
| 41 | util::FunctionThread t1([&result1](util::Thread *) { |
| 42 | result1 = RunCommand("true"); |
| 43 | }); |
| 44 | util::FunctionThread t2([&result2](util::Thread *) { |
| 45 | result2 = RunCommand("true"); |
| 46 | }); |
| 47 | t1.Start(); |
| 48 | t2.Start(); |
| 49 | t1.WaitUntilDone(); |
| 50 | t2.WaitUntilDone(); |
| 51 | ASSERT_NE(-1, result1); |
| 52 | ASSERT_NE(-1, result2); |
| 53 | ASSERT_TRUE(WIFEXITED(result1)); |
| 54 | ASSERT_TRUE(WIFEXITED(result2)); |
| 55 | EXPECT_EQ(0, WEXITSTATUS(result1)); |
| 56 | EXPECT_EQ(0, WEXITSTATUS(result2)); |
| 57 | } |
| 58 | |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 59 | } // namespace testing |
| 60 | } // namespace util |
| 61 | } // namespace aos |