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

Side by Side Diff: services/native_support/process_controller_impl_unittest.cc

Issue 1919503003: native_support service: SynchronousInterfacePtr conversion in tests. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 8 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
« no previous file with comments | « services/native_support/BUILD.gn ('k') | services/native_support/process_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 <signal.h> 5 #include <signal.h>
6 #include <string.h> 6 #include <string.h>
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "mojo/public/cpp/bindings/interface_request.h" 12 #include "mojo/public/cpp/bindings/interface_request.h"
13 #include "mojo/public/cpp/bindings/synchronous_interface_ptr.h"
13 #include "mojo/services/files/cpp/input_stream_file.h" 14 #include "mojo/services/files/cpp/input_stream_file.h"
14 #include "mojo/services/files/cpp/output_stream_file.h" 15 #include "mojo/services/files/cpp/output_stream_file.h"
15 #include "mojo/services/files/interfaces/types.mojom.h" 16 #include "mojo/services/files/interfaces/types.mojom.h"
16 #include "services/native_support/process_test_base.h" 17 #include "services/native_support/process_test_base.h"
17 18
19 using mojo::SynchronousInterfacePtr;
20
18 namespace native_support { 21 namespace native_support {
19 namespace { 22 namespace {
20 23
21 using ProcessControllerImplTest = ProcessTestBase; 24 using ProcessControllerImplTest = ProcessTestBase;
22 25
23 mojo::Array<uint8_t> ToByteArray(const std::string& s) { 26 mojo::Array<uint8_t> ToByteArray(const std::string& s) {
24 auto rv = mojo::Array<uint8_t>::New(s.size()); 27 auto rv = mojo::Array<uint8_t>::New(s.size());
25 memcpy(rv.data(), s.data(), s.size()); 28 memcpy(rv.data(), s.data(), s.size());
26 return rv; 29 return rv;
27 } 30 }
28 31
29 void QuitMessageLoop() { 32 void QuitMessageLoop() {
30 base::MessageLoop::current()->QuitWhenIdle(); 33 base::MessageLoop::current()->QuitWhenIdle();
31 } 34 }
32 35
33 void RunMessageLoop() { 36 void RunMessageLoop() {
34 base::MessageLoop::current()->Run(); 37 base::MessageLoop::current()->Run();
35 } 38 }
36 39
37 // Note: We already tested success (zero) versus failure (non-zero) exit 40 // Note: We already tested success (zero) versus failure (non-zero) exit
38 // statuses in |ProcessControllerTest.Spawn|. 41 // statuses in |ProcessControllerTest.Spawn|.
39 TEST_F(ProcessControllerImplTest, Wait) { 42 TEST_F(ProcessControllerImplTest, Wait) {
40 mojo::files::Error error; 43 mojo::files::Error error;
41 44
42 { 45 {
43 ProcessControllerPtr process_controller; 46 SynchronousInterfacePtr<ProcessController> process_controller;
44 error = mojo::files::Error::INTERNAL; 47 error = mojo::files::Error::INTERNAL;
45 const char kPath[] = "/bin/sh"; 48 const char kPath[] = "/bin/sh";
46 mojo::Array<mojo::Array<uint8_t>> argv; 49 mojo::Array<mojo::Array<uint8_t>> argv;
47 argv.push_back(ToByteArray(kPath)); 50 argv.push_back(ToByteArray(kPath));
48 argv.push_back(ToByteArray("-c")); 51 argv.push_back(ToByteArray("-c"));
49 argv.push_back(ToByteArray("exit 42")); 52 argv.push_back(ToByteArray("exit 42"));
50 process()->Spawn(ToByteArray(kPath), argv.Pass(), nullptr, nullptr, 53 ASSERT_TRUE(process()->Spawn(
51 nullptr, nullptr, GetProxy(&process_controller), 54 ToByteArray(kPath), argv.Pass(), nullptr, nullptr, nullptr, nullptr,
52 Capture(&error)); 55 GetSynchronousProxy(&process_controller), &error));
53 ASSERT_TRUE(process().WaitForIncomingResponse());
54 EXPECT_EQ(mojo::files::Error::OK, error); 56 EXPECT_EQ(mojo::files::Error::OK, error);
55 57
56 error = mojo::files::Error::INTERNAL; 58 error = mojo::files::Error::INTERNAL;
57 int32_t exit_status = 0; 59 int32_t exit_status = 0;
58 process_controller->Wait(Capture(&error, &exit_status)); 60 ASSERT_TRUE(process_controller->Wait(&error, &exit_status));
59 ASSERT_TRUE(process_controller.WaitForIncomingResponse());
60 EXPECT_EQ(mojo::files::Error::OK, error); 61 EXPECT_EQ(mojo::files::Error::OK, error);
61 EXPECT_EQ(42, exit_status); 62 EXPECT_EQ(42, exit_status);
62 } 63 }
63 } 64 }
64 65
65 // An output file stream that captures output and quits when "ready" is seen. 66 // An output file stream that captures output and quits when "ready" is seen.
66 class QuitOnReadyFile : public files_impl::OutputStreamFile::Client { 67 class QuitOnReadyFile : public files_impl::OutputStreamFile::Client {
67 public: 68 public:
68 explicit QuitOnReadyFile(mojo::InterfaceRequest<mojo::files::File> request) 69 explicit QuitOnReadyFile(mojo::InterfaceRequest<mojo::files::File> request)
69 : impl_(files_impl::OutputStreamFile::Create(this, request.Pass())) {} 70 : impl_(files_impl::OutputStreamFile::Create(this, request.Pass())) {}
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 // "sleep" since it's not a builtin (so bash would wait for it before dying). 126 // "sleep" since it's not a builtin (so bash would wait for it before dying).
126 // So instead we use "read" with a timeout (note that "-t" is a bash 127 // So instead we use "read" with a timeout (note that "-t" is a bash
127 // extension, and not specified by POSIX for /bin/sh). Thus we have to give 128 // extension, and not specified by POSIX for /bin/sh). Thus we have to give
128 // something for stdin (otherwise, it'd come from /dev/null and the read would 129 // something for stdin (otherwise, it'd come from /dev/null and the read would
129 // be completed immediately). 130 // be completed immediately).
130 mojo::files::FilePtr ifile; 131 mojo::files::FilePtr ifile;
131 IgnoreReadsFile ifile_impl(GetProxy(&ifile)); 132 IgnoreReadsFile ifile_impl(GetProxy(&ifile));
132 mojo::files::FilePtr ofile; 133 mojo::files::FilePtr ofile;
133 QuitOnReadyFile ofile_impl(GetProxy(&ofile)); 134 QuitOnReadyFile ofile_impl(GetProxy(&ofile));
134 135
135 ProcessControllerPtr process_controller; 136 SynchronousInterfacePtr<ProcessController> process_controller;
136 mojo::files::Error error = mojo::files::Error::INTERNAL; 137 mojo::files::Error error = mojo::files::Error::INTERNAL;
137 const char kPath[] = "/bin/bash"; 138 const char kPath[] = "/bin/bash";
138 mojo::Array<mojo::Array<uint8_t>> argv; 139 mojo::Array<mojo::Array<uint8_t>> argv;
139 argv.push_back(ToByteArray(kPath)); 140 argv.push_back(ToByteArray(kPath));
140 argv.push_back(ToByteArray("-c")); 141 argv.push_back(ToByteArray("-c"));
141 argv.push_back( 142 argv.push_back(
142 ToByteArray("trap 'exit 42' INT; echo ready; read -t30; exit 1")); 143 ToByteArray("trap 'exit 42' INT; echo ready; read -t30; exit 1"));
143 process()->Spawn(ToByteArray(kPath), argv.Pass(), nullptr, ifile.Pass(), 144 ASSERT_TRUE(process()->Spawn(
144 ofile.Pass(), nullptr, GetProxy(&process_controller), 145 ToByteArray(kPath), argv.Pass(), nullptr, ifile.Pass(), ofile.Pass(),
145 Capture(&error)); 146 nullptr, GetSynchronousProxy(&process_controller), &error));
146 ASSERT_TRUE(process().WaitForIncomingResponse());
147 EXPECT_EQ(mojo::files::Error::OK, error); 147 EXPECT_EQ(mojo::files::Error::OK, error);
148 148
149 // |ofile_impl| will quit the message loop once it sees "ready". 149 // |ofile_impl| will quit the message loop once it sees "ready".
150 RunMessageLoop(); 150 RunMessageLoop();
151 ASSERT_TRUE(ofile_impl.got_ready()); 151 ASSERT_TRUE(ofile_impl.got_ready());
152 152
153 // Send SIGINT. 153 // Send SIGINT.
154 error = mojo::files::Error::INTERNAL; 154 error = mojo::files::Error::INTERNAL;
155 process_controller->Kill(static_cast<int32_t>(SIGINT), Capture(&error)); 155 ASSERT_TRUE(process_controller->Kill(static_cast<int32_t>(SIGINT), &error));
156 ASSERT_TRUE(process_controller.WaitForIncomingResponse());
157 EXPECT_EQ(mojo::files::Error::OK, error); 156 EXPECT_EQ(mojo::files::Error::OK, error);
158 157
159 error = mojo::files::Error::INTERNAL; 158 error = mojo::files::Error::INTERNAL;
160 int32_t exit_status = 0; 159 int32_t exit_status = 0;
161 process_controller->Wait(Capture(&error, &exit_status)); 160 ASSERT_TRUE(process_controller->Wait(&error, &exit_status));
162 ASSERT_TRUE(process_controller.WaitForIncomingResponse());
163 EXPECT_EQ(mojo::files::Error::OK, error); 161 EXPECT_EQ(mojo::files::Error::OK, error);
164 EXPECT_EQ(42, exit_status); 162 EXPECT_EQ(42, exit_status);
165 } 163 }
166 164
167 TEST_F(ProcessControllerImplTest, DestroyingControllerKills) { 165 TEST_F(ProcessControllerImplTest, DestroyingControllerKills) {
168 // We want to make sure that we've exec-ed before killing, so we do what we do 166 // We want to make sure that we've exec-ed before killing, so we do what we do
169 // in |ProcessControllerImplTest.Kill| (without the trap). 167 // in |ProcessControllerImplTest.Kill| (without the trap).
170 { 168 {
171 mojo::files::FilePtr ifile; 169 mojo::files::FilePtr ifile;
172 IgnoreReadsFile ifile_impl(GetProxy(&ifile)); 170 IgnoreReadsFile ifile_impl(GetProxy(&ifile));
173 mojo::files::FilePtr ofile; 171 mojo::files::FilePtr ofile;
174 QuitOnReadyFile ofile_impl(GetProxy(&ofile)); 172 QuitOnReadyFile ofile_impl(GetProxy(&ofile));
175 173
176 ProcessControllerPtr process_controller; 174 SynchronousInterfacePtr<ProcessController> process_controller;
177 mojo::files::Error error = mojo::files::Error::INTERNAL; 175 mojo::files::Error error = mojo::files::Error::INTERNAL;
178 const char kPath[] = "/bin/bash"; 176 const char kPath[] = "/bin/bash";
179 mojo::Array<mojo::Array<uint8_t>> argv; 177 mojo::Array<mojo::Array<uint8_t>> argv;
180 argv.push_back(ToByteArray(kPath)); 178 argv.push_back(ToByteArray(kPath));
181 argv.push_back(ToByteArray("-c")); 179 argv.push_back(ToByteArray("-c"));
182 argv.push_back(ToByteArray("echo ready; read -t30")); 180 argv.push_back(ToByteArray("echo ready; read -t30"));
183 process()->Spawn(ToByteArray(kPath), argv.Pass(), nullptr, ifile.Pass(), 181 ASSERT_TRUE(process()->Spawn(
184 ofile.Pass(), nullptr, GetProxy(&process_controller), 182 ToByteArray(kPath), argv.Pass(), nullptr, ifile.Pass(), ofile.Pass(),
185 Capture(&error)); 183 nullptr, GetSynchronousProxy(&process_controller), &error));
186 ASSERT_TRUE(process().WaitForIncomingResponse());
187 EXPECT_EQ(mojo::files::Error::OK, error); 184 EXPECT_EQ(mojo::files::Error::OK, error);
188 185
189 // |ofile_impl| will quit the message loop once it sees "ready". 186 // |ofile_impl| will quit the message loop once it sees "ready".
190 RunMessageLoop(); 187 RunMessageLoop();
191 ASSERT_TRUE(ofile_impl.got_ready()); 188 ASSERT_TRUE(ofile_impl.got_ready());
192 } 189 }
193 190
194 // The child should be killed. 191 // The child should be killed.
195 // TODO(vtl): It's pretty hard to verify that the child process was actually 192 // TODO(vtl): It's pretty hard to verify that the child process was actually
196 // killed. This could be done, e.g., by having the child trap SIGTERM and 193 // killed. This could be done, e.g., by having the child trap SIGTERM and
197 // writing something to a file, and then separately checking for that file. 194 // writing something to a file, and then separately checking for that file.
198 // For now now, just be happy if it doesn't crash. (I've actually verified it 195 // For now now, just be happy if it doesn't crash. (I've actually verified it
199 // "manually", but automation is hard.) 196 // "manually", but automation is hard.)
200 } 197 }
201 198
202 } // namespace 199 } // namespace
203 } // namespace native_support 200 } // namespace native_support
OLDNEW
« no previous file with comments | « services/native_support/BUILD.gn ('k') | services/native_support/process_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698