diff options
Diffstat (limited to 'common/subprocess_unittest.cc')
-rw-r--r-- | common/subprocess_unittest.cc | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/common/subprocess_unittest.cc b/common/subprocess_unittest.cc index 104ef419..bc52b83e 100644 --- a/common/subprocess_unittest.cc +++ b/common/subprocess_unittest.cc @@ -45,6 +45,7 @@ using base::TimeDelta; using brillo::MessageLoop; using std::string; +using std::unique_ptr; using std::vector; namespace { @@ -73,6 +74,10 @@ class SubprocessTest : public ::testing::Test { brillo::BaseMessageLoop loop_{&base_loop_}; brillo::AsynchronousSignalHandler async_signal_handler_; Subprocess subprocess_; +#ifndef __ANDROID__ + unique_ptr<base::FileDescriptorWatcher::Controller> watcher_; +#endif // __ANDROID__ + }; namespace { @@ -193,7 +198,7 @@ TEST_F(SubprocessTest, EnvVarsAreFiltered) { TEST_F(SubprocessTest, SynchronousTrueSearchsOnPath) { int rc = -1; EXPECT_TRUE(Subprocess::SynchronousExecFlags( - {"true"}, Subprocess::kSearchPath, &rc, nullptr)); + {"true"}, Subprocess::kSearchPath, &rc, nullptr, nullptr)); EXPECT_EQ(0, rc); } @@ -201,16 +206,17 @@ TEST_F(SubprocessTest, SynchronousEchoTest) { vector<string> cmd = { kBinPath "/sh", "-c", "echo -n stdout-here; echo -n stderr-there >&2"}; int rc = -1; - string stdout; - ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, &stdout)); + string stdout, stderr; + ASSERT_TRUE(Subprocess::SynchronousExec(cmd, &rc, &stdout, &stderr)); EXPECT_EQ(0, rc); - EXPECT_EQ("stdout-herestderr-there", stdout); + EXPECT_EQ("stdout-here", stdout); + EXPECT_EQ("stderr-there", stderr); } TEST_F(SubprocessTest, SynchronousEchoNoOutputTest) { int rc = -1; ASSERT_TRUE(Subprocess::SynchronousExec( - {kBinPath "/sh", "-c", "echo test"}, &rc, nullptr)); + {kBinPath "/sh", "-c", "echo test"}, &rc, nullptr, nullptr)); EXPECT_EQ(0, rc); } @@ -255,6 +261,7 @@ TEST_F(SubprocessTest, CancelTest) { int fifo_fd = HANDLE_EINTR(open(fifo_path.c_str(), O_RDONLY)); EXPECT_GE(fifo_fd, 0); +#ifdef __ANDROID__ loop_.WatchFileDescriptor(FROM_HERE, fifo_fd, MessageLoop::WatchMode::kWatchRead, @@ -270,6 +277,25 @@ TEST_F(SubprocessTest, CancelTest) { }, fifo_fd, tag)); +#else + watcher_ = base::FileDescriptorWatcher::WatchReadable( + fifo_fd, + base::Bind( + [](unique_ptr<base::FileDescriptorWatcher::Controller>* watcher, + int fifo_fd, + uint32_t tag) { + char c; + EXPECT_EQ(1, HANDLE_EINTR(read(fifo_fd, &c, 1))); + EXPECT_EQ('X', c); + LOG(INFO) << "Killing tag " << tag; + Subprocess::Get().KillExec(tag); + *watcher = nullptr; + }, + // watcher_ is no longer used outside the clousure. + base::Unretained(&watcher_), + fifo_fd, + tag)); +#endif // __ANDROID__ // This test would leak a callback that runs when the child process exits // unless we wait for it to run. |