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

Side by Side Diff: mojo/edk/embedder/embedder_unittest.cc

Issue 2744943002: Mojo: Move waiting APIs to public library (Closed)
Patch Set: . Created 3 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
« no previous file with comments | « mojo/edk/embedder/configuration.h ('k') | mojo/edk/embedder/entrypoints.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 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
(...skipping 17 matching lines...) Expand all
28 #include "mojo/edk/embedder/named_platform_handle.h" 28 #include "mojo/edk/embedder/named_platform_handle.h"
29 #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" 30 #include "mojo/edk/embedder/pending_process_connection.h"
31 #include "mojo/edk/embedder/platform_channel_pair.h" 31 #include "mojo/edk/embedder/platform_channel_pair.h"
32 #include "mojo/edk/embedder/test_embedder.h" 32 #include "mojo/edk/embedder/test_embedder.h"
33 #include "mojo/edk/system/test_utils.h" 33 #include "mojo/edk/system/test_utils.h"
34 #include "mojo/edk/test/mojo_test_base.h" 34 #include "mojo/edk/test/mojo_test_base.h"
35 #include "mojo/public/c/system/core.h" 35 #include "mojo/public/c/system/core.h"
36 #include "mojo/public/cpp/system/handle.h" 36 #include "mojo/public/cpp/system/handle.h"
37 #include "mojo/public/cpp/system/message_pipe.h" 37 #include "mojo/public/cpp/system/message_pipe.h"
38 #include "mojo/public/cpp/system/wait.h"
38 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
39 40
40 namespace mojo { 41 namespace mojo {
41 namespace edk { 42 namespace edk {
42 namespace { 43 namespace {
43 44
44 const MojoHandleSignals kSignalReadadableWritable =
45 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE;
46
47 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE |
48 MOJO_HANDLE_SIGNAL_WRITABLE |
49 MOJO_HANDLE_SIGNAL_PEER_CLOSED;
50
51 // The multiprocess tests that use these don't compile on iOS. 45 // The multiprocess tests that use these don't compile on iOS.
52 #if !defined(OS_IOS) 46 #if !defined(OS_IOS)
53 const char kHelloWorld[] = "hello world"; 47 const char kHelloWorld[] = "hello world";
54 const char kByeWorld[] = "bye world"; 48 const char kByeWorld[] = "bye world";
55 #endif 49 #endif
56 50
57 using EmbedderTest = test::MojoTestBase; 51 using EmbedderTest = test::MojoTestBase;
58 52
59 TEST_F(EmbedderTest, ChannelBasic) { 53 TEST_F(EmbedderTest, ChannelBasic) {
60 MojoHandle server_mp, client_mp; 54 MojoHandle server_mp, client_mp;
61 CreateMessagePipe(&server_mp, &client_mp); 55 CreateMessagePipe(&server_mp, &client_mp);
62 56
63 const std::string kHello = "hello"; 57 const std::string kHello = "hello";
64 58
65 // We can write to a message pipe handle immediately. 59 // We can write to a message pipe handle immediately.
66 WriteMessage(server_mp, kHello); 60 WriteMessage(server_mp, kHello);
67 EXPECT_EQ(kHello, ReadMessage(client_mp)); 61 EXPECT_EQ(kHello, ReadMessage(client_mp));
68 62
69 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); 63 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
70 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); 64 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
71 } 65 }
72 66
73 // Test sending a MP which has read messages out of the OS pipe but which have
74 // not been consumed using MojoReadMessage yet.
75 TEST_F(EmbedderTest, SendReadableMessagePipe) {
76 MojoHandle server_mp, client_mp;
77 CreateMessagePipe(&server_mp, &client_mp);
78
79 MojoHandle server_mp2, client_mp2;
80 CreateMessagePipe(&server_mp2, &client_mp2);
81
82 // Write to server2 and wait for client2 to be readable before sending it.
83 // client2's MessagePipeDispatcher will have the message below in its
84 // message_queue_. For extra measures, also verify that this pending message
85 // can contain a message pipe.
86 MojoHandle server_mp3, client_mp3;
87 CreateMessagePipe(&server_mp3, &client_mp3);
88
89 const std::string kHello = "hello";
90 WriteMessageWithHandles(server_mp2, kHello, &client_mp3, 1);
91
92 MojoHandleSignalsState state;
93 ASSERT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE,
94 MOJO_DEADLINE_INDEFINITE, &state));
95 ASSERT_EQ(kSignalReadadableWritable, state.satisfied_signals);
96 ASSERT_EQ(kSignalAll, state.satisfiable_signals);
97
98 // Now send client2
99 WriteMessageWithHandles(server_mp, kHello, &client_mp2, 1);
100
101 MojoHandle port;
102 std::string message = ReadMessageWithHandles(client_mp, &port, 1);
103 EXPECT_EQ(kHello, message);
104
105 client_mp2 = port;
106 message = ReadMessageWithHandles(client_mp2, &client_mp3, 1);
107 EXPECT_EQ(kHello, message);
108
109 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(server_mp3));
110 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(client_mp3));
111 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(server_mp2));
112 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(client_mp2));
113 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
114 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
115 }
116
117 // Verifies that a MP with pending messages to be written can be sent and the 67 // Verifies that a MP with pending messages to be written can be sent and the
118 // pending messages aren't dropped. 68 // pending messages aren't dropped.
119 TEST_F(EmbedderTest, SendMessagePipeWithWriteQueue) { 69 TEST_F(EmbedderTest, SendMessagePipeWithWriteQueue) {
120 MojoHandle server_mp, client_mp; 70 MojoHandle server_mp, client_mp;
121 CreateMessagePipe(&server_mp, &client_mp); 71 CreateMessagePipe(&server_mp, &client_mp);
122 72
123 MojoHandle server_mp2, client_mp2; 73 MojoHandle server_mp2, client_mp2;
124 CreateMessagePipe(&server_mp2, &client_mp2); 74 CreateMessagePipe(&server_mp2, &client_mp2);
125 75
126 static const size_t kNumMessages = 1001; 76 static const size_t kNumMessages = 1001;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 PendingProcessConnection process; 160 PendingProcessConnection process;
211 std::string pipe_token; 161 std::string pipe_token;
212 ScopedMessagePipeHandle parent_mp = process.CreateMessagePipe(&pipe_token); 162 ScopedMessagePipeHandle parent_mp = process.CreateMessagePipe(&pipe_token);
213 process.Connect(base::GetCurrentProcessHandle(), 163 process.Connect(base::GetCurrentProcessHandle(),
214 ConnectionParams(pair.PassServerHandle())); 164 ConnectionParams(pair.PassServerHandle()));
215 165
216 // Close the remote end, simulating child death before the child connects to 166 // Close the remote end, simulating child death before the child connects to
217 // the reserved port. 167 // the reserved port.
218 ignore_result(pair.PassClientHandle()); 168 ignore_result(pair.PassClientHandle());
219 169
220 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(parent_mp.get().value(), 170 EXPECT_EQ(MOJO_RESULT_OK, WaitForSignals(parent_mp.get().value(),
221 MOJO_HANDLE_SIGNAL_PEER_CLOSED, 171 MOJO_HANDLE_SIGNAL_PEER_CLOSED));
222 MOJO_DEADLINE_INDEFINITE,
223 nullptr));
224 } 172 }
225 173
226 TEST_F(EmbedderTest, PipeSetup_LaunchFailure) { 174 TEST_F(EmbedderTest, PipeSetup_LaunchFailure) {
227 PlatformChannelPair pair; 175 PlatformChannelPair pair;
228 176
229 auto process = base::MakeUnique<PendingProcessConnection>(); 177 auto process = base::MakeUnique<PendingProcessConnection>();
230 std::string pipe_token; 178 std::string pipe_token;
231 ScopedMessagePipeHandle parent_mp = process->CreateMessagePipe(&pipe_token); 179 ScopedMessagePipeHandle parent_mp = process->CreateMessagePipe(&pipe_token);
232 180
233 // Ensure that if a PendingProcessConnection goes away before Connect() is 181 // Ensure that if a PendingProcessConnection goes away before Connect() is
234 // called, any message pipes associated with it detect peer closure. 182 // called, any message pipes associated with it detect peer closure.
235 process.reset(); 183 process.reset();
236 184
237 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(parent_mp.get().value(), 185 EXPECT_EQ(MOJO_RESULT_OK, WaitForSignals(parent_mp.get().value(),
238 MOJO_HANDLE_SIGNAL_PEER_CLOSED, 186 MOJO_HANDLE_SIGNAL_PEER_CLOSED));
239 MOJO_DEADLINE_INDEFINITE,
240 nullptr));
241 } 187 }
242 188
243 // The sequence of messages sent is: 189 // The sequence of messages sent is:
244 // server_mp client_mp mp0 mp1 mp2 mp3 190 // server_mp client_mp mp0 mp1 mp2 mp3
245 // 1. "hello" 191 // 1. "hello"
246 // 2. "world!" 192 // 2. "world!"
247 // 3. "FOO" 193 // 3. "FOO"
248 // 4. "Bar"+mp1 194 // 4. "Bar"+mp1
249 // 5. (close) 195 // 5. (close)
250 // 6. (close) 196 // 6. (close)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 231
286 // 8. Close |mp0|. 232 // 8. Close |mp0|.
287 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(mp0)); 233 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(mp0));
288 234
289 // 9. Tell the client to quit. 235 // 9. Tell the client to quit.
290 WriteMessage(server_mp, "quit"); 236 WriteMessage(server_mp, "quit");
291 237
292 // 10. Wait on |mp2| (which should eventually fail) and then close it. 238 // 10. Wait on |mp2| (which should eventually fail) and then close it.
293 MojoHandleSignalsState state; 239 MojoHandleSignalsState state;
294 ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 240 ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
295 MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, 241 WaitForSignals(mp2, MOJO_HANDLE_SIGNAL_READABLE, &state));
296 MOJO_DEADLINE_INDEFINITE,
297 &state));
298 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); 242 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals);
299 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); 243 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals);
300 244
301 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(mp2)); 245 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(mp2));
302 END_CHILD() 246 END_CHILD()
303 } 247 }
304 248
305 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(MultiprocessChannelsClient, EmbedderTest, 249 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(MultiprocessChannelsClient, EmbedderTest,
306 client_mp) { 250 client_mp) {
307 // 1. Read the first message from |client_mp|. 251 // 1. Read the first message from |client_mp|.
(...skipping 21 matching lines...) Expand all
329 mp2 = MOJO_HANDLE_INVALID; 273 mp2 = MOJO_HANDLE_INVALID;
330 274
331 // 9. Read a message from |mp1|. 275 // 9. Read a message from |mp1|.
332 EXPECT_EQ("FOO", ReadMessage(mp1)); 276 EXPECT_EQ("FOO", ReadMessage(mp1));
333 277
334 EXPECT_EQ("quit", ReadMessage(client_mp)); 278 EXPECT_EQ("quit", ReadMessage(client_mp));
335 279
336 // 10. Wait on |mp1| (which should eventually fail) and then close it. 280 // 10. Wait on |mp1| (which should eventually fail) and then close it.
337 MojoHandleSignalsState state; 281 MojoHandleSignalsState state;
338 ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 282 ASSERT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
339 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, 283 WaitForSignals(mp1, MOJO_HANDLE_SIGNAL_READABLE, &state));
340 MOJO_DEADLINE_INDEFINITE, &state));
341 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); 284 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals);
342 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); 285 ASSERT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals);
343 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); 286 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(mp1));
344 } 287 }
345 288
346 TEST_F(EmbedderTest, MultiprocessBaseSharedMemory) { 289 TEST_F(EmbedderTest, MultiprocessBaseSharedMemory) {
347 RUN_CHILD_ON_PIPE(MultiprocessSharedMemoryClient, server_mp) 290 RUN_CHILD_ON_PIPE(MultiprocessSharedMemoryClient, server_mp)
348 // 1. Create a base::SharedMemory object and create a mojo shared buffer 291 // 1. Create a base::SharedMemory object and create a mojo shared buffer
349 // from it. 292 // from it.
350 base::SharedMemoryCreateOptions options; 293 base::SharedMemoryCreateOptions options;
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 *output = CreateClientHandle(named_handle); 522 *output = CreateClientHandle(named_handle);
580 } 523 }
581 524
582 TEST_F(EmbedderTest, ClosePendingPeerConnection) { 525 TEST_F(EmbedderTest, ClosePendingPeerConnection) {
583 NamedPlatformHandle named_handle = GenerateChannelName(); 526 NamedPlatformHandle named_handle = GenerateChannelName();
584 std::string peer_token = GenerateRandomToken(); 527 std::string peer_token = GenerateRandomToken();
585 ScopedMessagePipeHandle server_pipe = 528 ScopedMessagePipeHandle server_pipe =
586 ConnectToPeerProcess(CreateServerHandle(named_handle), peer_token); 529 ConnectToPeerProcess(CreateServerHandle(named_handle), peer_token);
587 ClosePeerConnection(peer_token); 530 ClosePeerConnection(peer_token);
588 EXPECT_EQ(MOJO_RESULT_OK, 531 EXPECT_EQ(MOJO_RESULT_OK,
589 Wait(server_pipe.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED, 532 Wait(server_pipe.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED));
590 MOJO_DEADLINE_INDEFINITE, nullptr));
591 base::MessageLoop message_loop; 533 base::MessageLoop message_loop;
592 base::RunLoop run_loop; 534 base::RunLoop run_loop;
593 ScopedPlatformHandle client_handle; 535 ScopedPlatformHandle client_handle;
594 // Closing the channel involves posting a task to the IO thread to do the 536 // Closing the channel involves posting a task to the IO thread to do the
595 // work. By the time the local message pipe has been observerd as closed, 537 // work. By the time the local message pipe has been observerd as closed,
596 // that task will have been posted. Therefore, a task to create the client 538 // that task will have been posted. Therefore, a task to create the client
597 // connection should be handled after the channel is closed. 539 // connection should be handled after the channel is closed.
598 GetIOTaskRunner()->PostTaskAndReply( 540 GetIOTaskRunner()->PostTaskAndReply(
599 FROM_HERE, 541 FROM_HERE,
600 base::Bind(&CreateClientHandleOnIoThread, named_handle, &client_handle), 542 base::Bind(&CreateClientHandleOnIoThread, named_handle, &client_handle),
601 run_loop.QuitClosure()); 543 run_loop.QuitClosure());
602 run_loop.Run(); 544 run_loop.Run();
603 EXPECT_FALSE(client_handle.is_valid()); 545 EXPECT_FALSE(client_handle.is_valid());
604 } 546 }
605 547
606 #if !defined(OS_IOS) 548 #if !defined(OS_IOS)
607 549
608 TEST_F(EmbedderTest, ClosePipeToConnectedPeer) { 550 TEST_F(EmbedderTest, ClosePipeToConnectedPeer) {
609 set_launch_type(LaunchType::PEER); 551 set_launch_type(LaunchType::PEER);
610 auto& controller = StartClient("ClosePipeToConnectedPeerClient"); 552 auto& controller = StartClient("ClosePipeToConnectedPeerClient");
611 MojoHandle server_mp = controller.pipe(); 553 MojoHandle server_mp = controller.pipe();
612 // 1. Write a message to |server_mp| (attaching nothing). 554 // 1. Write a message to |server_mp| (attaching nothing).
613 WriteMessage(server_mp, "hello"); 555 WriteMessage(server_mp, "hello");
614 556
615 // 2. Read a message from |server_mp|. 557 // 2. Read a message from |server_mp|.
616 EXPECT_EQ("world!", ReadMessage(server_mp)); 558 EXPECT_EQ("world!", ReadMessage(server_mp));
617 559
618 controller.ClosePeerConnection(); 560 controller.ClosePeerConnection();
619 561
620 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(server_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 562 EXPECT_EQ(MOJO_RESULT_OK,
621 MOJO_DEADLINE_INDEFINITE, nullptr)); 563 WaitForSignals(server_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED));
622 564
623 EXPECT_EQ(0, controller.WaitForShutdown()); 565 EXPECT_EQ(0, controller.WaitForShutdown());
624 } 566 }
625 567
626 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ClosePipeToConnectedPeerClient, EmbedderTest, 568 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ClosePipeToConnectedPeerClient, EmbedderTest,
627 client_mp) { 569 client_mp) {
628 // 1. Read the first message from |client_mp|. 570 // 1. Read the first message from |client_mp|.
629 EXPECT_EQ("hello", ReadMessage(client_mp)); 571 EXPECT_EQ("hello", ReadMessage(client_mp));
630 572
631 // 2. Write a message to |client_mp| (attaching nothing). 573 // 2. Write a message to |client_mp| (attaching nothing).
632 WriteMessage(client_mp, "world!"); 574 WriteMessage(client_mp, "world!");
633 575
634 ASSERT_EQ(MOJO_RESULT_OK, 576 ASSERT_EQ(MOJO_RESULT_OK,
635 MojoWait(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 577 WaitForSignals(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED));
636 MOJO_DEADLINE_INDEFINITE, nullptr));
637 } 578 }
638 579
639 TEST_F(EmbedderTest, ClosePipeToConnectingPeer) { 580 TEST_F(EmbedderTest, ClosePipeToConnectingPeer) {
640 set_launch_type(LaunchType::PEER); 581 set_launch_type(LaunchType::PEER);
641 auto& controller = StartClient("ClosePipeToConnectingPeerClient"); 582 auto& controller = StartClient("ClosePipeToConnectingPeerClient");
642 controller.ClosePeerConnection(); 583 controller.ClosePeerConnection();
643 584
644 MojoHandle server_mp = controller.pipe(); 585 MojoHandle server_mp = controller.pipe();
645 586
646 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(server_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 587 EXPECT_EQ(MOJO_RESULT_OK,
647 MOJO_DEADLINE_INDEFINITE, nullptr)); 588 WaitForSignals(server_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED));
648 589
649 EXPECT_EQ(0, controller.WaitForShutdown()); 590 EXPECT_EQ(0, controller.WaitForShutdown());
650 } 591 }
651 592
652 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ClosePipeToConnectingPeerClient, EmbedderTest, 593 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ClosePipeToConnectingPeerClient, EmbedderTest,
653 client_mp) { 594 client_mp) {
654 ASSERT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, 595 ASSERT_EQ(MOJO_RESULT_OK,
655 MOJO_DEADLINE_INDEFINITE, nullptr)); 596 WaitForSignals(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED));
656 } 597 }
657 598
658 #endif // !defined(OS_IOS) 599 #endif // !defined(OS_IOS)
659 600
660 } // namespace 601 } // namespace
661 } // namespace edk 602 } // namespace edk
662 } // namespace mojo 603 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/embedder/configuration.h ('k') | mojo/edk/embedder/entrypoints.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698