OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <errno.h> |
| 6 #include <fcntl.h> |
| 7 #include <sys/stat.h> |
| 8 #include <sys/types.h> |
| 9 #include <sys/wait.h> |
| 10 #include <unistd.h> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/bind.h" |
| 14 #include "base/callback.h" |
| 15 #include "base/file_util.h" |
| 16 #include "base/logging.h" |
| 17 #include "base/posix/eintr_wrapper.h" |
| 18 #include "sandbox/linux/services/scoped_process.h" |
| 19 #include "sandbox/linux/tests/unit_tests.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace sandbox { |
| 23 |
| 24 namespace { |
| 25 |
| 26 void DoNothing() {} |
| 27 |
| 28 void ExitWithCode(int exit_code) { _exit(exit_code); } |
| 29 |
| 30 void Raise(int signal) { PCHECK(0 == raise(signal)); } |
| 31 |
| 32 void WaitForever() { |
| 33 while (true) { |
| 34 pause(); |
| 35 } |
| 36 } |
| 37 |
| 38 TEST(ScopedProcess, DoNothing) { |
| 39 ScopedProcess process(base::Bind(&DoNothing)); |
| 40 bool got_signaled = true; |
| 41 int exit_code = process.WaitForExit(&got_signaled); |
| 42 EXPECT_FALSE(got_signaled); |
| 43 EXPECT_EQ(0, exit_code); |
| 44 } |
| 45 |
| 46 TEST(ScopedProcess, ScopedProcessNormalExit) { |
| 47 const int kCustomExitCode = 12; |
| 48 ScopedProcess process(base::Bind(&ExitWithCode, kCustomExitCode)); |
| 49 bool got_signaled = true; |
| 50 int exit_code = process.WaitForExit(&got_signaled); |
| 51 EXPECT_FALSE(got_signaled); |
| 52 EXPECT_EQ(kCustomExitCode, exit_code); |
| 53 } |
| 54 |
| 55 TEST(ScopedProcess, ScopedProcessAbort) { |
| 56 ScopedProcess process(base::Bind(&Raise, SIGABRT)); |
| 57 bool got_signaled = false; |
| 58 int exit_code = process.WaitForExit(&got_signaled); |
| 59 EXPECT_TRUE(got_signaled); |
| 60 EXPECT_EQ(SIGABRT, exit_code); |
| 61 } |
| 62 |
| 63 TEST(ScopedProcess, ScopedProcessSignaled) { |
| 64 ScopedProcess process(base::Bind(&WaitForever)); |
| 65 bool got_signaled = false; |
| 66 ASSERT_EQ(0, kill(process.GetPid(), SIGKILL)); |
| 67 int exit_code = process.WaitForExit(&got_signaled); |
| 68 EXPECT_TRUE(got_signaled); |
| 69 EXPECT_EQ(SIGKILL, exit_code); |
| 70 } |
| 71 |
| 72 TEST(ScopedProcess, DiesForReal) { |
| 73 int pipe_fds[2]; |
| 74 file_util::ScopedFDCloser read_end_closer(pipe_fds); |
| 75 file_util::ScopedFDCloser write_end_closer(pipe_fds + 1); |
| 76 |
| 77 ASSERT_EQ(0, pipe(pipe_fds)); |
| 78 { ScopedProcess process(base::Bind(&DoNothing)); } |
| 79 |
| 80 // Close writing end of the pipe. |
| 81 ASSERT_EQ(0, IGNORE_EINTR(close(pipe_fds[1]))); |
| 82 pipe_fds[1] = -1; |
| 83 |
| 84 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); |
| 85 char c; |
| 86 // If the child process is dead for real, there will be no writing end |
| 87 // for this pipe left and read will EOF instead of returning EWOULDBLOCK. |
| 88 ASSERT_EQ(0, read(pipe_fds[0], &c, 1)); |
| 89 } |
| 90 |
| 91 } // namespace |
| 92 |
| 93 } // namespace sandbox |
OLD | NEW |