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