Index: sandbox/linux/services/scoped_process_unittest.cc |
diff --git a/sandbox/linux/services/scoped_process_unittest.cc b/sandbox/linux/services/scoped_process_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..49ce18fd9876148b9d6c383d6a4d48250c73d96b |
--- /dev/null |
+++ b/sandbox/linux/services/scoped_process_unittest.cc |
@@ -0,0 +1,93 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <errno.h> |
+#include <fcntl.h> |
+#include <sys/stat.h> |
+#include <sys/types.h> |
+#include <sys/wait.h> |
+#include <unistd.h> |
+ |
+#include "base/basictypes.h" |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/file_util.h" |
+#include "base/logging.h" |
+#include "base/posix/eintr_wrapper.h" |
+#include "sandbox/linux/services/scoped_process.h" |
+#include "sandbox/linux/tests/unit_tests.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace sandbox { |
+ |
+namespace { |
+ |
+void DoNothing() {} |
+ |
+void ExitWithCode(int exit_code) { _exit(exit_code); } |
+ |
+void Raise(int signal) { PCHECK(0 == raise(signal)); } |
+ |
+void WaitForever() { |
+ while (true) { |
+ pause(); |
+ } |
+} |
+ |
+TEST(ScopedProcess, DoNothing) { |
+ ScopedProcess process(base::Bind(&DoNothing)); |
+ bool got_signaled = true; |
+ int exit_code = process.WaitForExit(&got_signaled); |
+ EXPECT_FALSE(got_signaled); |
+ EXPECT_EQ(0, exit_code); |
+} |
+ |
+TEST(ScopedProcess, ScopedProcessNormalExit) { |
+ const int kCustomExitCode = 12; |
+ ScopedProcess process(base::Bind(&ExitWithCode, kCustomExitCode)); |
+ bool got_signaled = true; |
+ int exit_code = process.WaitForExit(&got_signaled); |
+ EXPECT_FALSE(got_signaled); |
+ EXPECT_EQ(kCustomExitCode, exit_code); |
+} |
+ |
+TEST(ScopedProcess, ScopedProcessAbort) { |
+ ScopedProcess process(base::Bind(&Raise, SIGABRT)); |
+ bool got_signaled = false; |
+ int exit_code = process.WaitForExit(&got_signaled); |
+ EXPECT_TRUE(got_signaled); |
+ EXPECT_EQ(SIGABRT, exit_code); |
+} |
+ |
+TEST(ScopedProcess, ScopedProcessSignaled) { |
+ ScopedProcess process(base::Bind(&WaitForever)); |
+ bool got_signaled = false; |
+ ASSERT_EQ(0, kill(process.GetPid(), SIGKILL)); |
+ int exit_code = process.WaitForExit(&got_signaled); |
+ EXPECT_TRUE(got_signaled); |
+ EXPECT_EQ(SIGKILL, exit_code); |
+} |
+ |
+TEST(ScopedProcess, DiesForReal) { |
+ int pipe_fds[2]; |
+ file_util::ScopedFDCloser read_end_closer(pipe_fds); |
+ file_util::ScopedFDCloser write_end_closer(pipe_fds + 1); |
+ |
+ ASSERT_EQ(0, pipe(pipe_fds)); |
+ { ScopedProcess process(base::Bind(&DoNothing)); } |
+ |
+ // Close writing end of the pipe. |
+ ASSERT_EQ(0, IGNORE_EINTR(close(pipe_fds[1]))); |
+ pipe_fds[1] = -1; |
+ |
+ ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); |
+ char c; |
+ // If the child process is dead for real, there will be no writing end |
+ // for this pipe left and read will EOF instead of returning EWOULDBLOCK. |
+ ASSERT_EQ(0, read(pipe_fds[0], &c, 1)); |
+} |
+ |
+} // namespace |
+ |
+} // namespace sandbox |