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 "mojo/edk/embedder/embedder.h" | 5 #include "mojo/edk/embedder/embedder.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/files/file.h" | 16 #include "base/files/file.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/memory/ptr_util.h" |
19 #include "base/memory/shared_memory.h" | 20 #include "base/memory/shared_memory.h" |
20 #include "base/message_loop/message_loop.h" | 21 #include "base/message_loop/message_loop.h" |
21 #include "base/path_service.h" | 22 #include "base/path_service.h" |
22 #include "base/process/process_handle.h" | 23 #include "base/process/process_handle.h" |
23 #include "base/run_loop.h" | 24 #include "base/run_loop.h" |
24 #include "base/synchronization/waitable_event.h" | 25 #include "base/synchronization/waitable_event.h" |
25 #include "base/test/test_timeouts.h" | 26 #include "base/test/test_timeouts.h" |
26 #include "mojo/edk/embedder/embedder.h" | 27 #include "mojo/edk/embedder/embedder.h" |
27 #include "mojo/edk/embedder/named_platform_handle.h" | 28 #include "mojo/edk/embedder/named_platform_handle.h" |
28 #include "mojo/edk/embedder/named_platform_handle_utils.h" | 29 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 30 #include "mojo/edk/embedder/pending_process_connection.h" |
29 #include "mojo/edk/embedder/platform_channel_pair.h" | 31 #include "mojo/edk/embedder/platform_channel_pair.h" |
30 #include "mojo/edk/embedder/test_embedder.h" | 32 #include "mojo/edk/embedder/test_embedder.h" |
31 #include "mojo/edk/system/test_utils.h" | 33 #include "mojo/edk/system/test_utils.h" |
32 #include "mojo/edk/test/mojo_test_base.h" | 34 #include "mojo/edk/test/mojo_test_base.h" |
33 #include "mojo/public/c/system/core.h" | 35 #include "mojo/public/c/system/core.h" |
34 #include "mojo/public/cpp/system/handle.h" | 36 #include "mojo/public/cpp/system/handle.h" |
35 #include "mojo/public/cpp/system/message_pipe.h" | 37 #include "mojo/public/cpp/system/message_pipe.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
37 | 39 |
38 namespace mojo { | 40 namespace mojo { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 // Wait for |h0| to become readable and read a message from it. | 184 // Wait for |h0| to become readable and read a message from it. |
183 EXPECT_EQ(kBarBaz, ReadMessage(h0)); | 185 EXPECT_EQ(kBarBaz, ReadMessage(h0)); |
184 | 186 |
185 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | 187 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
186 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | 188 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); |
187 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(h0)); | 189 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(h0)); |
188 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(h1)); | 190 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(h1)); |
189 } | 191 } |
190 | 192 |
191 TEST_F(EmbedderTest, PipeSetup) { | 193 TEST_F(EmbedderTest, PipeSetup) { |
192 std::string child_token = GenerateRandomToken(); | 194 // Ensures that a pending process connection's message pipe can be claimed by |
193 std::string pipe_token = GenerateRandomToken(); | 195 // the host process itself. |
194 | 196 PendingProcessConnection process; |
195 ScopedMessagePipeHandle parent_mp = | 197 std::string pipe_token; |
196 CreateParentMessagePipe(pipe_token, child_token); | 198 ScopedMessagePipeHandle parent_mp = process.CreateMessagePipe(&pipe_token); |
197 ScopedMessagePipeHandle child_mp = | 199 ScopedMessagePipeHandle child_mp = CreateChildMessagePipe(pipe_token); |
198 CreateChildMessagePipe(pipe_token); | |
199 | 200 |
200 const std::string kHello = "hello"; | 201 const std::string kHello = "hello"; |
201 WriteMessage(parent_mp.get().value(), kHello); | 202 WriteMessage(parent_mp.get().value(), kHello); |
202 | 203 |
203 EXPECT_EQ(kHello, ReadMessage(child_mp.get().value())); | 204 EXPECT_EQ(kHello, ReadMessage(child_mp.get().value())); |
204 } | 205 } |
205 | 206 |
206 TEST_F(EmbedderTest, PipeSetup_LaunchDeath) { | 207 TEST_F(EmbedderTest, PipeSetup_LaunchDeath) { |
207 PlatformChannelPair pair; | 208 PlatformChannelPair pair; |
208 | 209 |
209 std::string child_token = GenerateRandomToken(); | 210 PendingProcessConnection process; |
210 std::string pipe_token = GenerateRandomToken(); | 211 std::string pipe_token; |
211 | 212 ScopedMessagePipeHandle parent_mp = process.CreateMessagePipe(&pipe_token); |
212 ScopedMessagePipeHandle parent_mp = | 213 process.Connect(base::GetCurrentProcessHandle(), pair.PassServerHandle()); |
213 CreateParentMessagePipe(pipe_token, child_token); | |
214 ChildProcessLaunched(base::GetCurrentProcessHandle(), pair.PassServerHandle(), | |
215 child_token); | |
216 | 214 |
217 // Close the remote end, simulating child death before the child connects to | 215 // Close the remote end, simulating child death before the child connects to |
218 // the reserved port. | 216 // the reserved port. |
219 ignore_result(pair.PassClientHandle()); | 217 ignore_result(pair.PassClientHandle()); |
220 | 218 |
221 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(parent_mp.get().value(), | 219 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(parent_mp.get().value(), |
222 MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 220 MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
223 MOJO_DEADLINE_INDEFINITE, | 221 MOJO_DEADLINE_INDEFINITE, |
224 nullptr)); | 222 nullptr)); |
225 } | 223 } |
226 | 224 |
227 TEST_F(EmbedderTest, PipeSetup_LaunchFailure) { | 225 TEST_F(EmbedderTest, PipeSetup_LaunchFailure) { |
228 PlatformChannelPair pair; | 226 PlatformChannelPair pair; |
229 | 227 |
230 std::string child_token = GenerateRandomToken(); | 228 auto process = base::MakeUnique<PendingProcessConnection>(); |
231 std::string pipe_token = GenerateRandomToken(); | 229 std::string pipe_token; |
| 230 ScopedMessagePipeHandle parent_mp = process->CreateMessagePipe(&pipe_token); |
232 | 231 |
233 ScopedMessagePipeHandle parent_mp = | 232 // Ensure that if a PendingProcessConnection goes away before Connect() is |
234 CreateParentMessagePipe(pipe_token, child_token); | 233 // called, any message pipes associated with it detect peer closure. |
| 234 process.reset(); |
235 | 235 |
236 ChildProcessLaunchFailed(child_token); | |
237 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(parent_mp.get().value(), | 236 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(parent_mp.get().value(), |
238 MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 237 MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
239 MOJO_DEADLINE_INDEFINITE, | 238 MOJO_DEADLINE_INDEFINITE, |
240 nullptr)); | 239 nullptr)); |
241 } | 240 } |
242 | 241 |
243 // The sequence of messages sent is: | 242 // The sequence of messages sent is: |
244 // server_mp client_mp mp0 mp1 mp2 mp3 | 243 // server_mp client_mp mp0 mp1 mp2 mp3 |
245 // 1. "hello" | 244 // 1. "hello" |
246 // 2. "world!" | 245 // 2. "world!" |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 client_mp) { | 652 client_mp) { |
654 ASSERT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 653 ASSERT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
655 MOJO_DEADLINE_INDEFINITE, nullptr)); | 654 MOJO_DEADLINE_INDEFINITE, nullptr)); |
656 } | 655 } |
657 | 656 |
658 #endif // !defined(OS_IOS) | 657 #endif // !defined(OS_IOS) |
659 | 658 |
660 } // namespace | 659 } // namespace |
661 } // namespace edk | 660 } // namespace edk |
662 } // namespace mojo | 661 } // namespace mojo |
OLD | NEW |