| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sandbox/linux/services/scoped_process.h" | 5 #include "sandbox/linux/services/scoped_process.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <sys/wait.h> | 11 #include <sys/wait.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/bind_helpers.h" |
| 15 #include "base/callback.h" | 16 #include "base/callback.h" |
| 16 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_file.h" | 18 #include "base/files/scoped_file.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 19 #include "base/posix/eintr_wrapper.h" | 20 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 #include "sandbox/linux/tests/unit_tests.h" | 23 #include "sandbox/linux/tests/unit_tests.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 namespace sandbox { | 26 namespace sandbox { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 void DoExit() { _exit(0); } | 30 void DoExit() { _exit(0); } |
| 30 | 31 |
| 31 void ExitWithCode(int exit_code) { _exit(exit_code); } | 32 void ExitWithCode(int exit_code) { _exit(exit_code); } |
| 32 | 33 |
| 33 void RaiseAndExit(int signal) { | 34 void RaiseAndExit(int signal) { |
| 34 PCHECK(0 == raise(signal)); | 35 PCHECK(0 == raise(signal)); |
| 35 _exit(0); | 36 _exit(0); |
| 36 } | 37 } |
| 37 | 38 |
| 38 void DoNothing() {} | |
| 39 | |
| 40 TEST(ScopedProcess, ScopedProcessNormalExit) { | 39 TEST(ScopedProcess, ScopedProcessNormalExit) { |
| 41 const int kCustomExitCode = 12; | 40 const int kCustomExitCode = 12; |
| 42 ScopedProcess process(base::Bind(&ExitWithCode, kCustomExitCode)); | 41 ScopedProcess process(base::Bind(&ExitWithCode, kCustomExitCode)); |
| 43 bool got_signaled = true; | 42 bool got_signaled = true; |
| 44 int exit_code = process.WaitForExit(&got_signaled); | 43 int exit_code = process.WaitForExit(&got_signaled); |
| 45 EXPECT_FALSE(got_signaled); | 44 EXPECT_FALSE(got_signaled); |
| 46 EXPECT_EQ(kCustomExitCode, exit_code); | 45 EXPECT_EQ(kCustomExitCode, exit_code); |
| 47 | 46 |
| 48 // Verify that WaitForExit() can be called multiple times on the same | 47 // Verify that WaitForExit() can be called multiple times on the same |
| 49 // process. | 48 // process. |
| 50 bool got_signaled2 = true; | 49 bool got_signaled2 = true; |
| 51 int exit_code2 = process.WaitForExit(&got_signaled2); | 50 int exit_code2 = process.WaitForExit(&got_signaled2); |
| 52 EXPECT_FALSE(got_signaled2); | 51 EXPECT_FALSE(got_signaled2); |
| 53 EXPECT_EQ(kCustomExitCode, exit_code2); | 52 EXPECT_EQ(kCustomExitCode, exit_code2); |
| 54 } | 53 } |
| 55 | 54 |
| 56 // Disable this test on Android, SIGABRT is funky there. | 55 // Disable this test on Android, SIGABRT is funky there. |
| 57 TEST(ScopedProcess, DISABLE_ON_ANDROID(ScopedProcessAbort)) { | 56 TEST(ScopedProcess, DISABLE_ON_ANDROID(ScopedProcessAbort)) { |
| 58 PCHECK(SIG_ERR != signal(SIGABRT, SIG_DFL)); | 57 PCHECK(SIG_ERR != signal(SIGABRT, SIG_DFL)); |
| 59 ScopedProcess process(base::Bind(&RaiseAndExit, SIGABRT)); | 58 ScopedProcess process(base::Bind(&RaiseAndExit, SIGABRT)); |
| 60 bool got_signaled = false; | 59 bool got_signaled = false; |
| 61 int exit_code = process.WaitForExit(&got_signaled); | 60 int exit_code = process.WaitForExit(&got_signaled); |
| 62 EXPECT_TRUE(got_signaled); | 61 EXPECT_TRUE(got_signaled); |
| 63 EXPECT_EQ(SIGABRT, exit_code); | 62 EXPECT_EQ(SIGABRT, exit_code); |
| 64 } | 63 } |
| 65 | 64 |
| 66 TEST(ScopedProcess, ScopedProcessSignaled) { | 65 TEST(ScopedProcess, ScopedProcessSignaled) { |
| 67 ScopedProcess process(base::Bind(&DoNothing)); | 66 ScopedProcess process(base::Bind(&base::DoNothing)); |
| 68 bool got_signaled = false; | 67 bool got_signaled = false; |
| 69 ASSERT_EQ(0, kill(process.GetPid(), SIGKILL)); | 68 ASSERT_EQ(0, kill(process.GetPid(), SIGKILL)); |
| 70 int exit_code = process.WaitForExit(&got_signaled); | 69 int exit_code = process.WaitForExit(&got_signaled); |
| 71 EXPECT_TRUE(got_signaled); | 70 EXPECT_TRUE(got_signaled); |
| 72 EXPECT_EQ(SIGKILL, exit_code); | 71 EXPECT_EQ(SIGKILL, exit_code); |
| 73 } | 72 } |
| 74 | 73 |
| 75 TEST(ScopedProcess, DiesForReal) { | 74 TEST(ScopedProcess, DiesForReal) { |
| 76 int pipe_fds[2]; | 75 int pipe_fds[2]; |
| 77 ASSERT_EQ(0, pipe(pipe_fds)); | 76 ASSERT_EQ(0, pipe(pipe_fds)); |
| 78 base::ScopedFD read_end_closer(pipe_fds[0]); | 77 base::ScopedFD read_end_closer(pipe_fds[0]); |
| 79 base::ScopedFD write_end_closer(pipe_fds[1]); | 78 base::ScopedFD write_end_closer(pipe_fds[1]); |
| 80 | 79 |
| 81 { ScopedProcess process(base::Bind(&DoExit)); } | 80 { ScopedProcess process(base::Bind(&DoExit)); } |
| 82 | 81 |
| 83 // Close writing end of the pipe. | 82 // Close writing end of the pipe. |
| 84 write_end_closer.reset(); | 83 write_end_closer.reset(); |
| 85 pipe_fds[1] = -1; | 84 pipe_fds[1] = -1; |
| 86 | 85 |
| 87 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); | 86 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); |
| 88 char c; | 87 char c; |
| 89 // If the child process is dead for real, there will be no writing end | 88 // If the child process is dead for real, there will be no writing end |
| 90 // for this pipe left and read will EOF instead of returning EWOULDBLOCK. | 89 // for this pipe left and read will EOF instead of returning EWOULDBLOCK. |
| 91 ASSERT_EQ(0, read(pipe_fds[0], &c, 1)); | 90 ASSERT_EQ(0, read(pipe_fds[0], &c, 1)); |
| 92 } | 91 } |
| 93 | 92 |
| 94 TEST(ScopedProcess, SynchronizationBasic) { | 93 TEST(ScopedProcess, SynchronizationBasic) { |
| 95 ScopedProcess process1(base::Bind(&DoNothing)); | 94 ScopedProcess process1(base::Bind(&base::DoNothing)); |
| 96 EXPECT_TRUE(process1.WaitForClosureToRun()); | 95 EXPECT_TRUE(process1.WaitForClosureToRun()); |
| 97 | 96 |
| 98 ScopedProcess process2(base::Bind(&DoExit)); | 97 ScopedProcess process2(base::Bind(&DoExit)); |
| 99 // The closure didn't finish running normally. This case is simple enough | 98 // The closure didn't finish running normally. This case is simple enough |
| 100 // that process.WaitForClosureToRun() should return false, even though the | 99 // that process.WaitForClosureToRun() should return false, even though the |
| 101 // API does not guarantees that it will return at all. | 100 // API does not guarantees that it will return at all. |
| 102 EXPECT_FALSE(process2.WaitForClosureToRun()); | 101 EXPECT_FALSE(process2.WaitForClosureToRun()); |
| 103 } | 102 } |
| 104 | 103 |
| 105 void SleepInMsAndWriteOneByte(int time_to_sleep, int fd) { | 104 void SleepInMsAndWriteOneByte(int time_to_sleep, int fd) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 121 // Verify that the closure did, indeed, run. | 120 // Verify that the closure did, indeed, run. |
| 122 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); | 121 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); |
| 123 char c = 0; | 122 char c = 0; |
| 124 EXPECT_EQ(1, read(pipe_fds[0], &c, 1)); | 123 EXPECT_EQ(1, read(pipe_fds[0], &c, 1)); |
| 125 EXPECT_EQ('1', c); | 124 EXPECT_EQ('1', c); |
| 126 } | 125 } |
| 127 | 126 |
| 128 } // namespace | 127 } // namespace |
| 129 | 128 |
| 130 } // namespace sandbox | 129 } // namespace sandbox |
| OLD | NEW |