Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: sandbox/linux/services/scoped_process_unittest.cc

Issue 188193002: Linux sandbox: add basic Yama support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Better comments. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sandbox/linux/services/scoped_process.cc ('k') | sandbox/linux/services/thread_helpers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "base/threading/platform_thread.h"
19 #include "base/time/time.h"
20 #include "sandbox/linux/services/scoped_process.h"
21 #include "sandbox/linux/tests/unit_tests.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace sandbox {
25
26 namespace {
27
28 void DoExit() { _exit(0); }
29
30 void ExitWithCode(int exit_code) { _exit(exit_code); }
31
32 void RaiseAndExit(int signal) {
33 PCHECK(0 == raise(signal));
34 _exit(0);
35 }
36
37 void DoNothing() {}
38
39 TEST(ScopedProcess, ScopedProcessNormalExit) {
40 const int kCustomExitCode = 12;
41 ScopedProcess process(base::Bind(&ExitWithCode, kCustomExitCode));
42 bool got_signaled = true;
43 int exit_code = process.WaitForExit(&got_signaled);
44 EXPECT_FALSE(got_signaled);
45 EXPECT_EQ(kCustomExitCode, exit_code);
46
47 // Verify that WaitForExit() can be called multiple times on the same
48 // process.
49 bool got_signaled2 = true;
50 int exit_code2 = process.WaitForExit(&got_signaled2);
51 EXPECT_FALSE(got_signaled2);
52 EXPECT_EQ(kCustomExitCode, exit_code2);
53 }
54
55 // Disable this test on Android, SIGABRT is funky there.
56 TEST(ScopedProcess, DISABLE_ON_ANDROID(ScopedProcessAbort)) {
57 ScopedProcess process(base::Bind(&RaiseAndExit, SIGABRT));
58 bool got_signaled = false;
59 int exit_code = process.WaitForExit(&got_signaled);
60 EXPECT_TRUE(got_signaled);
61 EXPECT_EQ(SIGABRT, exit_code);
62 }
63
64 TEST(ScopedProcess, ScopedProcessSignaled) {
65 ScopedProcess process(base::Bind(&DoNothing));
66 bool got_signaled = false;
67 ASSERT_EQ(0, kill(process.GetPid(), SIGKILL));
68 int exit_code = process.WaitForExit(&got_signaled);
69 EXPECT_TRUE(got_signaled);
70 EXPECT_EQ(SIGKILL, exit_code);
71 }
72
73 TEST(ScopedProcess, DiesForReal) {
74 int pipe_fds[2];
75 ASSERT_EQ(0, pipe(pipe_fds));
76 file_util::ScopedFDCloser read_end_closer(pipe_fds);
77 file_util::ScopedFDCloser write_end_closer(pipe_fds + 1);
78
79 { ScopedProcess process(base::Bind(&DoExit)); }
80
81 // Close writing end of the pipe.
82 ASSERT_EQ(0, IGNORE_EINTR(close(pipe_fds[1])));
83 pipe_fds[1] = -1;
84
85 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK));
86 char c;
87 // If the child process is dead for real, there will be no writing end
88 // for this pipe left and read will EOF instead of returning EWOULDBLOCK.
89 ASSERT_EQ(0, read(pipe_fds[0], &c, 1));
90 }
91
92 TEST(ScopedProcess, SynchronizationBasic) {
93 ScopedProcess process1(base::Bind(&DoNothing));
94 EXPECT_TRUE(process1.WaitForClosureToRun());
95
96 ScopedProcess process2(base::Bind(&DoExit));
97 // The closure didn't finish running normally. This case is simple enough
98 // that process.WaitForClosureToRun() should return false, even though the
99 // API does not guarantees that it will return at all.
100 EXPECT_FALSE(process2.WaitForClosureToRun());
101 }
102
103 void SleepInMsAndWriteOneByte(int time_to_sleep, int fd) {
104 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(time_to_sleep));
105 CHECK(1 == write(fd, "1", 1));
106 }
107
108 TEST(ScopedProcess, SynchronizationWorks) {
109 int pipe_fds[2];
110 ASSERT_EQ(0, pipe(pipe_fds));
111 file_util::ScopedFDCloser read_end_closer(pipe_fds);
112 file_util::ScopedFDCloser write_end_closer(pipe_fds + 1);
113
114 // Start a process with a closure that takes a little bit to run.
115 ScopedProcess process(
116 base::Bind(&SleepInMsAndWriteOneByte, 100, pipe_fds[1]));
117 EXPECT_TRUE(process.WaitForClosureToRun());
118
119 // Verify that the closure did, indeed, run.
120 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK));
121 char c = 0;
122 EXPECT_EQ(1, read(pipe_fds[0], &c, 1));
123 EXPECT_EQ('1', c);
124 }
125
126 } // namespace
127
128 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/services/scoped_process.cc ('k') | sandbox/linux/services/thread_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698