| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
| 8 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 #include <sys/wait.h> | 9 #include <sys/wait.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 16 #include "base/files/scoped_file.h" | |
| 17 #include "base/logging.h" | 16 #include "base/logging.h" |
| 18 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
| 20 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 21 #include "sandbox/linux/services/scoped_process.h" | 20 #include "sandbox/linux/services/scoped_process.h" |
| 22 #include "sandbox/linux/tests/unit_tests.h" | 21 #include "sandbox/linux/tests/unit_tests.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 23 |
| 25 namespace sandbox { | 24 namespace sandbox { |
| 26 | 25 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 bool got_signaled = false; | 66 bool got_signaled = false; |
| 68 ASSERT_EQ(0, kill(process.GetPid(), SIGKILL)); | 67 ASSERT_EQ(0, kill(process.GetPid(), SIGKILL)); |
| 69 int exit_code = process.WaitForExit(&got_signaled); | 68 int exit_code = process.WaitForExit(&got_signaled); |
| 70 EXPECT_TRUE(got_signaled); | 69 EXPECT_TRUE(got_signaled); |
| 71 EXPECT_EQ(SIGKILL, exit_code); | 70 EXPECT_EQ(SIGKILL, exit_code); |
| 72 } | 71 } |
| 73 | 72 |
| 74 TEST(ScopedProcess, DiesForReal) { | 73 TEST(ScopedProcess, DiesForReal) { |
| 75 int pipe_fds[2]; | 74 int pipe_fds[2]; |
| 76 ASSERT_EQ(0, pipe(pipe_fds)); | 75 ASSERT_EQ(0, pipe(pipe_fds)); |
| 77 base::ScopedFD read_end_closer(pipe_fds[0]); | 76 file_util::ScopedFDCloser read_end_closer(pipe_fds); |
| 78 base::ScopedFD write_end_closer(pipe_fds[1]); | 77 file_util::ScopedFDCloser write_end_closer(pipe_fds + 1); |
| 79 | 78 |
| 80 { ScopedProcess process(base::Bind(&DoExit)); } | 79 { ScopedProcess process(base::Bind(&DoExit)); } |
| 81 | 80 |
| 82 // Close writing end of the pipe. | 81 // Close writing end of the pipe. |
| 83 write_end_closer.reset(); | 82 ASSERT_EQ(0, IGNORE_EINTR(close(pipe_fds[1]))); |
| 84 pipe_fds[1] = -1; | 83 pipe_fds[1] = -1; |
| 85 | 84 |
| 86 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); | 85 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); |
| 87 char c; | 86 char c; |
| 88 // If the child process is dead for real, there will be no writing end | 87 // If the child process is dead for real, there will be no writing end |
| 89 // for this pipe left and read will EOF instead of returning EWOULDBLOCK. | 88 // for this pipe left and read will EOF instead of returning EWOULDBLOCK. |
| 90 ASSERT_EQ(0, read(pipe_fds[0], &c, 1)); | 89 ASSERT_EQ(0, read(pipe_fds[0], &c, 1)); |
| 91 } | 90 } |
| 92 | 91 |
| 93 TEST(ScopedProcess, SynchronizationBasic) { | 92 TEST(ScopedProcess, SynchronizationBasic) { |
| 94 ScopedProcess process1(base::Bind(&DoNothing)); | 93 ScopedProcess process1(base::Bind(&DoNothing)); |
| 95 EXPECT_TRUE(process1.WaitForClosureToRun()); | 94 EXPECT_TRUE(process1.WaitForClosureToRun()); |
| 96 | 95 |
| 97 ScopedProcess process2(base::Bind(&DoExit)); | 96 ScopedProcess process2(base::Bind(&DoExit)); |
| 98 // The closure didn't finish running normally. This case is simple enough | 97 // The closure didn't finish running normally. This case is simple enough |
| 99 // that process.WaitForClosureToRun() should return false, even though the | 98 // that process.WaitForClosureToRun() should return false, even though the |
| 100 // API does not guarantees that it will return at all. | 99 // API does not guarantees that it will return at all. |
| 101 EXPECT_FALSE(process2.WaitForClosureToRun()); | 100 EXPECT_FALSE(process2.WaitForClosureToRun()); |
| 102 } | 101 } |
| 103 | 102 |
| 104 void SleepInMsAndWriteOneByte(int time_to_sleep, int fd) { | 103 void SleepInMsAndWriteOneByte(int time_to_sleep, int fd) { |
| 105 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(time_to_sleep)); | 104 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(time_to_sleep)); |
| 106 CHECK(1 == write(fd, "1", 1)); | 105 CHECK(1 == write(fd, "1", 1)); |
| 107 } | 106 } |
| 108 | 107 |
| 109 TEST(ScopedProcess, SynchronizationWorks) { | 108 TEST(ScopedProcess, SynchronizationWorks) { |
| 110 int pipe_fds[2]; | 109 int pipe_fds[2]; |
| 111 ASSERT_EQ(0, pipe(pipe_fds)); | 110 ASSERT_EQ(0, pipe(pipe_fds)); |
| 112 base::ScopedFD read_end_closer(pipe_fds[0]); | 111 file_util::ScopedFDCloser read_end_closer(pipe_fds); |
| 113 base::ScopedFD write_end_closer(pipe_fds[1]); | 112 file_util::ScopedFDCloser write_end_closer(pipe_fds + 1); |
| 114 | 113 |
| 115 // Start a process with a closure that takes a little bit to run. | 114 // Start a process with a closure that takes a little bit to run. |
| 116 ScopedProcess process( | 115 ScopedProcess process( |
| 117 base::Bind(&SleepInMsAndWriteOneByte, 100, pipe_fds[1])); | 116 base::Bind(&SleepInMsAndWriteOneByte, 100, pipe_fds[1])); |
| 118 EXPECT_TRUE(process.WaitForClosureToRun()); | 117 EXPECT_TRUE(process.WaitForClosureToRun()); |
| 119 | 118 |
| 120 // Verify that the closure did, indeed, run. | 119 // Verify that the closure did, indeed, run. |
| 121 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); | 120 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); |
| 122 char c = 0; | 121 char c = 0; |
| 123 EXPECT_EQ(1, read(pipe_fds[0], &c, 1)); | 122 EXPECT_EQ(1, read(pipe_fds[0], &c, 1)); |
| 124 EXPECT_EQ('1', c); | 123 EXPECT_EQ('1', c); |
| 125 } | 124 } |
| 126 | 125 |
| 127 } // namespace | 126 } // namespace |
| 128 | 127 |
| 129 } // namespace sandbox | 128 } // namespace sandbox |
| OLD | NEW |