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

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

Issue 191673003: Implement ScopedFD in terms of ScopedGeneric. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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
OLDNEW
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"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "base/posix/eintr_wrapper.h" 18 #include "base/posix/eintr_wrapper.h"
18 #include "base/threading/platform_thread.h" 19 #include "base/threading/platform_thread.h"
19 #include "base/time/time.h" 20 #include "base/time/time.h"
20 #include "sandbox/linux/services/scoped_process.h" 21 #include "sandbox/linux/services/scoped_process.h"
21 #include "sandbox/linux/tests/unit_tests.h" 22 #include "sandbox/linux/tests/unit_tests.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 namespace sandbox { 25 namespace sandbox {
25 26
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 bool got_signaled = false; 67 bool got_signaled = false;
67 ASSERT_EQ(0, kill(process.GetPid(), SIGKILL)); 68 ASSERT_EQ(0, kill(process.GetPid(), SIGKILL));
68 int exit_code = process.WaitForExit(&got_signaled); 69 int exit_code = process.WaitForExit(&got_signaled);
69 EXPECT_TRUE(got_signaled); 70 EXPECT_TRUE(got_signaled);
70 EXPECT_EQ(SIGKILL, exit_code); 71 EXPECT_EQ(SIGKILL, exit_code);
71 } 72 }
72 73
73 TEST(ScopedProcess, DiesForReal) { 74 TEST(ScopedProcess, DiesForReal) {
74 int pipe_fds[2]; 75 int pipe_fds[2];
75 ASSERT_EQ(0, pipe(pipe_fds)); 76 ASSERT_EQ(0, pipe(pipe_fds));
76 file_util::ScopedFDCloser read_end_closer(pipe_fds); 77 base::ScopedFD read_end_closer(pipe_fds[0]);
77 file_util::ScopedFDCloser write_end_closer(pipe_fds + 1); 78 base::ScopedFD write_end_closer(pipe_fds[1]);
78 79
79 { ScopedProcess process(base::Bind(&DoExit)); } 80 { ScopedProcess process(base::Bind(&DoExit)); }
80 81
81 // Close writing end of the pipe. 82 // Close writing end of the pipe.
82 ASSERT_EQ(0, IGNORE_EINTR(close(pipe_fds[1]))); 83 write_end_closer.reset();
83 pipe_fds[1] = -1; 84 pipe_fds[1] = -1;
84 85
85 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); 86 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK));
86 char c; 87 char c;
87 // 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
88 // 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.
89 ASSERT_EQ(0, read(pipe_fds[0], &c, 1)); 90 ASSERT_EQ(0, read(pipe_fds[0], &c, 1));
90 } 91 }
91 92
92 TEST(ScopedProcess, SynchronizationBasic) { 93 TEST(ScopedProcess, SynchronizationBasic) {
93 ScopedProcess process1(base::Bind(&DoNothing)); 94 ScopedProcess process1(base::Bind(&DoNothing));
94 EXPECT_TRUE(process1.WaitForClosureToRun()); 95 EXPECT_TRUE(process1.WaitForClosureToRun());
95 96
96 ScopedProcess process2(base::Bind(&DoExit)); 97 ScopedProcess process2(base::Bind(&DoExit));
97 // 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
98 // that process.WaitForClosureToRun() should return false, even though the 99 // that process.WaitForClosureToRun() should return false, even though the
99 // API does not guarantees that it will return at all. 100 // API does not guarantees that it will return at all.
100 EXPECT_FALSE(process2.WaitForClosureToRun()); 101 EXPECT_FALSE(process2.WaitForClosureToRun());
101 } 102 }
102 103
103 void SleepInMsAndWriteOneByte(int time_to_sleep, int fd) { 104 void SleepInMsAndWriteOneByte(int time_to_sleep, int fd) {
104 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(time_to_sleep)); 105 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(time_to_sleep));
105 CHECK(1 == write(fd, "1", 1)); 106 CHECK(1 == write(fd, "1", 1));
106 } 107 }
107 108
108 TEST(ScopedProcess, SynchronizationWorks) { 109 TEST(ScopedProcess, SynchronizationWorks) {
109 int pipe_fds[2]; 110 int pipe_fds[2];
110 ASSERT_EQ(0, pipe(pipe_fds)); 111 ASSERT_EQ(0, pipe(pipe_fds));
111 file_util::ScopedFDCloser read_end_closer(pipe_fds); 112 base::ScopedFD read_end_closer(pipe_fds[0]);
112 file_util::ScopedFDCloser write_end_closer(pipe_fds + 1); 113 base::ScopedFD write_end_closer(pipe_fds[1]);
113 114
114 // Start a process with a closure that takes a little bit to run. 115 // Start a process with a closure that takes a little bit to run.
115 ScopedProcess process( 116 ScopedProcess process(
116 base::Bind(&SleepInMsAndWriteOneByte, 100, pipe_fds[1])); 117 base::Bind(&SleepInMsAndWriteOneByte, 100, pipe_fds[1]));
117 EXPECT_TRUE(process.WaitForClosureToRun()); 118 EXPECT_TRUE(process.WaitForClosureToRun());
118 119
119 // Verify that the closure did, indeed, run. 120 // Verify that the closure did, indeed, run.
120 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK)); 121 ASSERT_EQ(0, fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK));
121 char c = 0; 122 char c = 0;
122 EXPECT_EQ(1, read(pipe_fds[0], &c, 1)); 123 EXPECT_EQ(1, read(pipe_fds[0], &c, 1));
123 EXPECT_EQ('1', c); 124 EXPECT_EQ('1', c);
124 } 125 }
125 126
126 } // namespace 127 } // namespace
127 128
128 } // namespace sandbox 129 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698