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

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

Issue 1350023003: Add a Mojo EDK for Chrome that uses one OS pipe per message pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more cleanup Created 5 years, 2 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
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 "third_party/mojo/src/mojo/edk/embedder/embedder.h" 5 #include "mojo/edk/embedder/embedder.h"
6
7 #include <string.h>
8 6
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/command_line.h" 8 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/logging.h" 9 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
14 #include "base/synchronization/waitable_event.h" 11 #include "base/synchronization/waitable_event.h"
15 #include "base/test/test_io_thread.h"
16 #include "base/test/test_timeouts.h" 12 #include "base/test/test_timeouts.h"
13 #include "mojo/edk/embedder/embedder.h"
14 #include "mojo/edk/embedder/platform_channel_pair.h"
15 #include "mojo/edk/embedder/simple_platform_support.h"
16 #include "mojo/edk/embedder/test_embedder.h"
17 #include "mojo/edk/system/test_utils.h"
18 #include "mojo/edk/test/multiprocess_test_helper.h"
19 #include "mojo/message_pump/message_pump_mojo.h"
17 #include "mojo/public/c/system/core.h" 20 #include "mojo/public/c/system/core.h"
18 #include "mojo/public/cpp/system/handle.h" 21 #include "mojo/public/cpp/system/handle.h"
19 #include "mojo/public/cpp/system/macros.h" 22 #include "mojo/public/cpp/system/macros.h"
20 #include "mojo/public/cpp/system/message_pipe.h" 23 #include "mojo/public/cpp/system/message_pipe.h"
21 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h"
23 #include "third_party/mojo/src/mojo/edk/embedder/test_embedder.h"
24 #include "third_party/mojo/src/mojo/edk/system/mutex.h"
25 #include "third_party/mojo/src/mojo/edk/system/test_utils.h"
26 #include "third_party/mojo/src/mojo/edk/test/multiprocess_test_helper.h"
27 #include "third_party/mojo/src/mojo/edk/test/scoped_ipc_support.h"
28 25
29 namespace mojo { 26 namespace mojo {
30 namespace embedder { 27 namespace edk {
31 namespace { 28 namespace {
32 29
33 const MojoHandleSignals kSignalReadadableWritable = 30 const MojoHandleSignals kSignalReadadableWritable =
34 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; 31 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE;
35 32
36 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | 33 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE |
37 MOJO_HANDLE_SIGNAL_WRITABLE | 34 MOJO_HANDLE_SIGNAL_WRITABLE |
38 MOJO_HANDLE_SIGNAL_PEER_CLOSED; 35 MOJO_HANDLE_SIGNAL_PEER_CLOSED;
39 36
40 const char kConnectionIdFlag[] = "test-connection-id"; 37 class EmbedderTest : public test::MojoSystemTest {
41
42 void DoNothing() {}
43
44 class ScopedTestChannel {
45 public: 38 public:
46 // Creates a channel, which lives on the I/O thread given to 39 EmbedderTest() {}
47 // |InitIPCSupport()|. After construction, |bootstrap_message_pipe()| gives 40 ~EmbedderTest() override {}
48 // the Mojo handle for the bootstrap message pipe on this channel; it is up to
49 // the caller to close this handle. Note: The I/O thread must outlive this
50 // object (and its message loop must continue pumping messages while this
51 // object is alive).
52 explicit ScopedTestChannel(ScopedPlatformHandle platform_handle)
53 : bootstrap_message_pipe_(MOJO_HANDLE_INVALID),
54 event_(true, false), // Manual reset.
55 channel_info_(nullptr),
56 wait_on_shutdown_(true) {
57 bootstrap_message_pipe_ =
58 CreateChannel(platform_handle.Pass(),
59 base::Bind(&ScopedTestChannel::DidCreateChannel,
60 base::Unretained(this)),
61 nullptr)
62 .release()
63 .value();
64 CHECK_NE(bootstrap_message_pipe_, MOJO_HANDLE_INVALID);
65 }
66
67 // Destructor: Shuts down the channel. (As noted above, for this to happen,
68 // the I/O thread must be alive and pumping messages.)
69 ~ScopedTestChannel() {
70 // |WaitForChannelCreationCompletion()| must be called before destruction.
71 CHECK(event_.IsSignaled());
72 event_.Reset();
73 if (wait_on_shutdown_) {
74 DestroyChannel(channel_info_,
75 base::Bind(&ScopedTestChannel::DidDestroyChannel,
76 base::Unretained(this)),
77 nullptr);
78 event_.Wait();
79 } else {
80 DestroyChannel(channel_info_, base::Bind(&DoNothing), nullptr);
81 }
82 }
83
84 // Waits for channel creation to be completed.
85 void WaitForChannelCreationCompletion() { event_.Wait(); }
86
87 MojoHandle bootstrap_message_pipe() const { return bootstrap_message_pipe_; }
88
89 // Call only after |WaitForChannelCreationCompletion()|. Use only to check
90 // that it's not null.
91 const ChannelInfo* channel_info() const { return channel_info_; }
92
93 // Don't wait for the channel shutdown to finish on destruction. Used to
94 // exercise races.
95 void NoWaitOnShutdown() { wait_on_shutdown_ = false; }
96 41
97 private: 42 private:
98 void DidCreateChannel(ChannelInfo* channel_info) {
99 CHECK(channel_info);
100 CHECK(!channel_info_);
101 channel_info_ = channel_info;
102 event_.Signal();
103 }
104
105 void DidDestroyChannel() { event_.Signal(); }
106
107 // Valid from creation until whenever it gets closed (by the "owner" of this
108 // object).
109 // Note: We don't want use the C++ wrappers here, since we want to test the
110 // API at the lowest level.
111 MojoHandle bootstrap_message_pipe_;
112
113 // Set after channel creation has been completed (i.e., the callback to
114 // |CreateChannel()| has been called). Also used in the destructor to wait for
115 // |DestroyChannel()| completion.
116 base::WaitableEvent event_;
117
118 // Valid after channel creation completion until destruction.
119 ChannelInfo* channel_info_;
120
121 // Whether the destructor should wait until the channel is destroyed.
122 bool wait_on_shutdown_;
123
124 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedTestChannel);
125 };
126
127 class EmbedderTest : public testing::Test {
128 public:
129 EmbedderTest() : test_io_thread_(base::TestIOThread::kAutoStart) {}
130 ~EmbedderTest() override {}
131
132 protected:
133 base::TestIOThread& test_io_thread() { return test_io_thread_; }
134 scoped_refptr<base::TaskRunner> test_io_task_runner() {
135 return test_io_thread_.task_runner();
136 }
137
138 private:
139 void SetUp() override { test::InitWithSimplePlatformSupport(); }
140
141 void TearDown() override { EXPECT_TRUE(test::Shutdown()); }
142
143 base::TestIOThread test_io_thread_;
144
145 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); 43 MOJO_DISALLOW_COPY_AND_ASSIGN(EmbedderTest);
146 }; 44 };
147 45
148 TEST_F(EmbedderTest, ChannelsBasic) { 46 TEST_F(EmbedderTest, ChannelBasic) {
149 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); 47 MojoHandle server_mp, client_mp;
150 48 ASSERT_EQ(MOJO_RESULT_OK,
151 PlatformChannelPair channel_pair; 49 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp));
152 ScopedTestChannel server_channel(channel_pair.PassServerHandle());
153 MojoHandle server_mp = server_channel.bootstrap_message_pipe();
154 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
155 ScopedTestChannel client_channel(channel_pair.PassClientHandle());
156 MojoHandle client_mp = client_channel.bootstrap_message_pipe();
157 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
158 50
159 // We can write to a message pipe handle immediately. 51 // We can write to a message pipe handle immediately.
160 const char kHello[] = "hello"; 52 const char kHello[] = "hello";
161 EXPECT_EQ( 53
162 MOJO_RESULT_OK, 54 size_t write_size = sizeof(kHello);
163 MojoWriteMessage(server_mp, kHello, static_cast<uint32_t>(sizeof(kHello)), 55 const char* write_buffer = kHello;
164 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); 56 EXPECT_EQ(MOJO_RESULT_OK,
57 MojoWriteMessage(server_mp, write_buffer,
58 static_cast<uint32_t>(write_size), nullptr, 0,
59 MOJO_WRITE_MESSAGE_FLAG_NONE));
165 60
166 // Now wait for the other side to become readable. 61 // Now wait for the other side to become readable.
167 MojoHandleSignalsState state; 62 MojoHandleSignalsState state;
168 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, 63 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
169 MOJO_DEADLINE_INDEFINITE, &state)); 64 MOJO_DEADLINE_INDEFINITE, &state));
170 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); 65 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
171 EXPECT_EQ(kSignalAll, state.satisfiable_signals); 66 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
172 67
173 char buffer[1000] = {}; 68 char read_buffer[1000] = {};
174 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); 69 uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer));
175 EXPECT_EQ(MOJO_RESULT_OK, 70 EXPECT_EQ(MOJO_RESULT_OK,
176 MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr, 71 MojoReadMessage(client_mp, read_buffer, &num_bytes, nullptr,
177 MOJO_READ_MESSAGE_FLAG_NONE)); 72 nullptr, MOJO_READ_MESSAGE_FLAG_NONE));
178 EXPECT_EQ(sizeof(kHello), num_bytes); 73 EXPECT_EQ(write_size, num_bytes);
179 EXPECT_STREQ(kHello, buffer); 74 EXPECT_STREQ(kHello, read_buffer);
180 75
181 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); 76 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
182 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); 77 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
183
184 // By this point, these waits should basically be no-ops (since we've waited
185 // for the client message pipe to become readable, which implies that both
186 // the server and client channels were completely created).
187 server_channel.WaitForChannelCreationCompletion();
188 client_channel.WaitForChannelCreationCompletion();
189 EXPECT_TRUE(server_channel.channel_info());
190 EXPECT_TRUE(client_channel.channel_info());
191 } 78 }
192 79
193 class TestAsyncWaiter { 80 // Test sending a MP which has read messages out of the OS pipe but which have
194 public: 81 // not been consumed using MojoReadMessage yet.
195 TestAsyncWaiter() : event_(true, false), wait_result_(MOJO_RESULT_UNKNOWN) {} 82 TEST_F(EmbedderTest, SendReadableMessagePipe) {
83 MojoHandle server_mp, client_mp;
84 ASSERT_EQ(MOJO_RESULT_OK,
85 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp));
196 86
197 void Awake(MojoResult result) { 87 MojoHandle server_mp2, client_mp2;
198 system::MutexLocker l(&wait_result_mutex_); 88 ASSERT_EQ(MOJO_RESULT_OK,
199 wait_result_ = result; 89 MojoCreateMessagePipe(nullptr, &server_mp2, &client_mp2));
200 event_.Signal(); 90
91 // Write to server2 and wait for client2 to be readable before sending it.
92 // client2's MessagePipeDispatcher will have the message below in its
93 // message_queue_. For extra measures, also verify that this pending message
94 // can contain a message pipe.
95 MojoHandle server_mp3, client_mp3;
96 ASSERT_EQ(MOJO_RESULT_OK,
97 MojoCreateMessagePipe(nullptr, &server_mp3, &client_mp3));
98 const char kHello[] = "hello";
99 size_t write_size;
100 const char* write_buffer;
101 write_buffer = kHello;
102 write_size = sizeof(kHello);
103 EXPECT_EQ(MOJO_RESULT_OK,
104 MojoWriteMessage(server_mp2, write_buffer,
105 static_cast<uint32_t>(write_size), &client_mp3, 1,
106 MOJO_WRITE_MESSAGE_FLAG_NONE));
107
108 MojoHandleSignalsState state;
109 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE,
110 MOJO_DEADLINE_INDEFINITE, &state));
111 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
112 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
113
114 // Now send client2
115 EXPECT_EQ(MOJO_RESULT_OK,
116 MojoWriteMessage(server_mp, write_buffer,
117 static_cast<uint32_t>(write_size), &client_mp2, 1,
118 MOJO_WRITE_MESSAGE_FLAG_NONE));
119
120
121 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
122 MOJO_DEADLINE_INDEFINITE, &state));
123 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
124 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
125
126 char read_buffer[20000] = {};
127 uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer));
128 MojoHandle ports[10];
129 uint32_t num_ports;
130 EXPECT_EQ(MOJO_RESULT_OK,
131 MojoReadMessage(client_mp, read_buffer, &num_bytes, &ports[0],
132 &num_ports, MOJO_READ_MESSAGE_FLAG_NONE));
133 EXPECT_EQ(write_size, num_bytes);
134 EXPECT_STREQ(kHello, read_buffer);
135 EXPECT_EQ(1, num_ports);
136
137
138 client_mp2 = ports[0];
139 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp2, MOJO_HANDLE_SIGNAL_READABLE,
140 MOJO_DEADLINE_INDEFINITE, &state));
141 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
142 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
143
144
145 EXPECT_EQ(MOJO_RESULT_OK,
146 MojoReadMessage(client_mp2, read_buffer, &num_bytes, &client_mp3,
147 &num_ports, MOJO_READ_MESSAGE_FLAG_NONE));
148 EXPECT_EQ(write_size, num_bytes);
149 EXPECT_STREQ(kHello, read_buffer);
150 EXPECT_EQ(1, num_ports);
151
152
153 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp3));
154 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp3));
155 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp2));
156 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp2));
157 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
158 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
159 }
160
161 // TODO(jam): fix and renable
162 TEST_F(EmbedderTest, DISABLED_SendMessagePipeWithWriteQueue) {
163 MojoHandle server_mp, client_mp;
164 ASSERT_EQ(MOJO_RESULT_OK,
165 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp));
166
167 MojoHandle server_mp2, client_mp2;
168 ASSERT_EQ(MOJO_RESULT_OK,
169 MojoCreateMessagePipe(nullptr, &server_mp2, &client_mp2));
170
171 static const size_t kNumMessages = 1001;
172 for (size_t i = 0; i < kNumMessages; i++) {
173 std::string write_buffer(i, 'A' + (i % 26));
174 ASSERT_EQ(MOJO_RESULT_OK,
175 MojoWriteMessage(client_mp2, write_buffer.data(),
176 write_buffer.size(), nullptr, 0,
177 MOJO_WRITE_MESSAGE_FLAG_NONE));
201 } 178 }
202 179
203 bool TryWait() { return event_.TimedWait(TestTimeouts::action_timeout()); } 180 // Now send client2
181 EXPECT_EQ(MOJO_RESULT_OK,
182 MojoWriteMessage(server_mp, nullptr, 0, &client_mp2, 1,
183 MOJO_WRITE_MESSAGE_FLAG_NONE));
204 184
205 MojoResult wait_result() const { 185 MojoHandleSignalsState state;
206 system::MutexLocker l(&wait_result_mutex_); 186 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
207 return wait_result_; 187 MOJO_DEADLINE_INDEFINITE, &state));
208 } 188 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
189 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
209 190
210 private: 191 /// todo: read all msgs written..
211 base::WaitableEvent event_;
212 192
213 mutable system::Mutex wait_result_mutex_; 193 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp2));
214 MojoResult wait_result_ MOJO_GUARDED_BY(wait_result_mutex_); 194 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp2));
215 195
216 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter);
217 };
218 196
219 void WriteHello(MessagePipeHandle pipe) { 197 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
220 static const char kHello[] = "hello"; 198 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
221 CHECK_EQ(MOJO_RESULT_OK,
222 WriteMessageRaw(pipe, kHello, static_cast<uint32_t>(sizeof(kHello)),
223 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
224 }
225
226 void CloseScopedHandle(ScopedMessagePipeHandle handle) {
227 // Do nothing and the destructor will close it.
228 }
229
230 TEST_F(EmbedderTest, AsyncWait) {
231 ScopedMessagePipeHandle client_mp;
232 ScopedMessagePipeHandle server_mp;
233 EXPECT_EQ(MOJO_RESULT_OK, CreateMessagePipe(nullptr, &client_mp, &server_mp));
234
235 TestAsyncWaiter waiter;
236 EXPECT_EQ(MOJO_RESULT_OK,
237 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
238 base::Bind(&TestAsyncWaiter::Awake,
239 base::Unretained(&waiter))));
240
241 test_io_task_runner()->PostTask(FROM_HERE,
242 base::Bind(&WriteHello, server_mp.get()));
243 EXPECT_TRUE(waiter.TryWait());
244 EXPECT_EQ(MOJO_RESULT_OK, waiter.wait_result());
245
246 // If message is in the queue, it does't allow us to wait.
247 TestAsyncWaiter waiter_that_doesnt_wait;
248 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
249 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
250 base::Bind(&TestAsyncWaiter::Awake,
251 base::Unretained(&waiter_that_doesnt_wait))));
252
253 char buffer[1000];
254 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
255 CHECK_EQ(MOJO_RESULT_OK,
256 ReadMessageRaw(client_mp.get(), buffer, &num_bytes, nullptr, nullptr,
257 MOJO_READ_MESSAGE_FLAG_NONE));
258
259 TestAsyncWaiter unsatisfiable_waiter;
260 EXPECT_EQ(MOJO_RESULT_OK,
261 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
262 base::Bind(&TestAsyncWaiter::Awake,
263 base::Unretained(&unsatisfiable_waiter))));
264
265 test_io_task_runner()->PostTask(
266 FROM_HERE,
267 base::Bind(&CloseScopedHandle, base::Passed(server_mp.Pass())));
268
269 EXPECT_TRUE(unsatisfiable_waiter.TryWait());
270 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
271 unsatisfiable_waiter.wait_result());
272 } 199 }
273 200
274 TEST_F(EmbedderTest, ChannelsHandlePassing) { 201 TEST_F(EmbedderTest, ChannelsHandlePassing) {
275 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); 202 MojoHandle server_mp, client_mp;
276 203 ASSERT_EQ(MOJO_RESULT_OK,
277 PlatformChannelPair channel_pair; 204 MojoCreateMessagePipe(nullptr, &server_mp, &client_mp));
278 ScopedTestChannel server_channel(channel_pair.PassServerHandle());
279 MojoHandle server_mp = server_channel.bootstrap_message_pipe();
280 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); 205 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
281 ScopedTestChannel client_channel(channel_pair.PassClientHandle());
282 MojoHandle client_mp = client_channel.bootstrap_message_pipe();
283 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); 206 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
284 207
285 MojoHandle h0, h1; 208 MojoHandle h0, h1;
286 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); 209 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1));
287 210
288 // Write a message to |h0| (attaching nothing). 211 // Write a message to |h0| (attaching nothing).
289 const char kHello[] = "hello"; 212 const char kHello[] = "hello";
290 EXPECT_EQ(MOJO_RESULT_OK, 213 EXPECT_EQ(MOJO_RESULT_OK,
291 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)), 214 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)),
292 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); 215 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 EXPECT_EQ(MOJO_RESULT_OK, 301 EXPECT_EQ(MOJO_RESULT_OK,
379 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr, 302 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr,
380 MOJO_READ_MESSAGE_FLAG_NONE)); 303 MOJO_READ_MESSAGE_FLAG_NONE));
381 EXPECT_EQ(sizeof(kBarBaz), num_bytes); 304 EXPECT_EQ(sizeof(kBarBaz), num_bytes);
382 EXPECT_STREQ(kBarBaz, buffer); 305 EXPECT_STREQ(kBarBaz, buffer);
383 306
384 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); 307 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
385 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); 308 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
386 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); 309 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0));
387 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); 310 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1));
388
389 server_channel.WaitForChannelCreationCompletion();
390 client_channel.WaitForChannelCreationCompletion();
391 EXPECT_TRUE(server_channel.channel_info());
392 EXPECT_TRUE(client_channel.channel_info());
393 }
394
395 #if defined(OS_ANDROID)
396 // Android multi-process tests are not executing the new process. This is flaky.
397 // TODO(vtl): I'm guessing this is true of this test too?
398 #define MAYBE_MultiprocessMasterSlave DISABLED_MultiprocessMasterSlave
399 #else
400 #define MAYBE_MultiprocessMasterSlave MultiprocessMasterSlave
401 #endif // defined(OS_ANDROID)
402 TEST_F(EmbedderTest, MAYBE_MultiprocessMasterSlave) {
403 mojo::test::ScopedMasterIPCSupport ipc_support(test_io_task_runner());
404
405 mojo::test::MultiprocessTestHelper multiprocess_test_helper;
406 std::string connection_id;
407 base::WaitableEvent event(true, false);
408 ChannelInfo* channel_info = nullptr;
409 ScopedMessagePipeHandle mp = ConnectToSlave(
410 nullptr, multiprocess_test_helper.server_platform_handle.Pass(),
411 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)),
412 nullptr, &connection_id, &channel_info);
413 ASSERT_TRUE(mp.is_valid());
414 EXPECT_TRUE(channel_info);
415 ASSERT_FALSE(connection_id.empty());
416
417 multiprocess_test_helper.StartChildWithExtraSwitch(
418 "MultiprocessMasterSlave", kConnectionIdFlag, connection_id);
419
420 // Send a message saying "hello".
421 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "hello", 5, nullptr, 0,
422 MOJO_WRITE_MESSAGE_FLAG_NONE));
423
424 // Wait for a response.
425 EXPECT_EQ(MOJO_RESULT_OK,
426 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE,
427 mojo::system::test::ActionDeadline(), nullptr));
428
429 // The response message should say "world".
430 char buffer[100];
431 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
432 EXPECT_EQ(MOJO_RESULT_OK,
433 ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr,
434 MOJO_READ_MESSAGE_FLAG_NONE));
435 EXPECT_EQ(5u, num_bytes);
436 EXPECT_EQ(0, memcmp(buffer, "world", 5));
437
438 mp.reset();
439
440 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown());
441
442 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
443 test_io_thread().PostTaskAndWait(
444 FROM_HERE,
445 base::Bind(&DestroyChannelOnIOThread, base::Unretained(channel_info)));
446 }
447
448 TEST_F(EmbedderTest, ChannelShutdownRace_MessagePipeClose) {
449 const size_t kIterations = 1000;
450 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner());
451
452 for (size_t i = 0; i < kIterations; i++) {
453 PlatformChannelPair channel_pair;
454 scoped_ptr<ScopedTestChannel> server_channel(
455 new ScopedTestChannel(channel_pair.PassServerHandle()));
456 server_channel->WaitForChannelCreationCompletion();
457 server_channel->NoWaitOnShutdown();
458
459 MojoHandle server_mp = server_channel->bootstrap_message_pipe();
460 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
461
462 // Race between channel shutdown and closing a message pipe. The message
463 // pipe doesn't have to be the bootstrap pipe. It just has to be bound to
464 // the channel.
465 server_channel.reset();
466 MojoClose(server_mp);
467 }
468 }
469
470 TEST_F(EmbedderTest, ChannelShutdownRace_MessagePipePassing) {
471 const size_t kIterations = 1000;
472 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner());
473
474 for (size_t i = 0; i < kIterations; i++) {
475 PlatformChannelPair channel_pair;
476 scoped_ptr<ScopedTestChannel> server_channel(
477 new ScopedTestChannel(channel_pair.PassServerHandle()));
478 server_channel->WaitForChannelCreationCompletion();
479 server_channel->NoWaitOnShutdown();
480
481 MojoHandle server_mp = server_channel->bootstrap_message_pipe();
482 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
483
484 MessagePipe test_pipe;
485 MojoHandle passing_handle = test_pipe.handle0.release().value();
486
487 // Race between channel shutdown and passing a message pipe.
488 server_channel.reset();
489 MojoWriteMessage(server_mp, nullptr, 0, &passing_handle, 1,
490 MOJO_WRITE_MESSAGE_FLAG_NONE);
491 MojoClose(server_mp);
492 MojoClose(passing_handle);
493 }
494 }
495
496 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessMasterSlave) {
497 ScopedPlatformHandle client_platform_handle =
498 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass();
499 EXPECT_TRUE(client_platform_handle.is_valid());
500
501 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart);
502 test::InitWithSimplePlatformSupport();
503
504 {
505 mojo::test::ScopedSlaveIPCSupport ipc_support(
506 test_io_thread.task_runner(), client_platform_handle.Pass());
507
508 const base::CommandLine& command_line =
509 *base::CommandLine::ForCurrentProcess();
510 ASSERT_TRUE(command_line.HasSwitch(kConnectionIdFlag));
511 std::string connection_id =
512 command_line.GetSwitchValueASCII(kConnectionIdFlag);
513 ASSERT_FALSE(connection_id.empty());
514 base::WaitableEvent event(true, false);
515 ChannelInfo* channel_info = nullptr;
516 ScopedMessagePipeHandle mp = ConnectToMaster(
517 connection_id,
518 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)),
519 nullptr, &channel_info);
520 ASSERT_TRUE(mp.is_valid());
521 EXPECT_TRUE(channel_info);
522
523 // Wait for the master to send us a message.
524 EXPECT_EQ(MOJO_RESULT_OK,
525 Wait(mp.get(), MOJO_HANDLE_SIGNAL_READABLE,
526 mojo::system::test::ActionDeadline(), nullptr));
527
528 // It should say "hello".
529 char buffer[100];
530 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
531 EXPECT_EQ(MOJO_RESULT_OK,
532 ReadMessageRaw(mp.get(), buffer, &num_bytes, nullptr, nullptr,
533 MOJO_READ_MESSAGE_FLAG_NONE));
534 EXPECT_EQ(5u, num_bytes);
535 EXPECT_EQ(0, memcmp(buffer, "hello", 5));
536
537 // In response send a message saying "world".
538 EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(mp.get(), "world", 5, nullptr, 0,
539 MOJO_WRITE_MESSAGE_FLAG_NONE));
540
541 mp.reset();
542
543 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
544 test_io_thread.PostTaskAndWait(
545 FROM_HERE,
546 base::Bind(&DestroyChannelOnIOThread, base::Unretained(channel_info)));
547 }
548
549 EXPECT_TRUE(test::Shutdown());
550 } 311 }
551 312
552 // The sequence of messages sent is: 313 // The sequence of messages sent is:
553 // server_mp client_mp mp0 mp1 mp2 mp3 314 // server_mp client_mp mp0 mp1 mp2 mp3
554 // 1. "hello" 315 // 1. "hello"
555 // 2. "world!" 316 // 2. "world!"
556 // 3. "FOO" 317 // 3. "FOO"
557 // 4. "Bar"+mp1 318 // 4. "Bar"+mp1
558 // 5. (close) 319 // 5. (close)
559 // 6. (close) 320 // 6. (close)
560 // 7. "baz" 321 // 7. "baz"
561 // 8. (closed) 322 // 8. (closed)
562 // 9. "quux"+mp2 323 // 9. "quux"+mp2
563 // 10. (close) 324 // 10. (close)
564 // 11. (wait/cl.) 325 // 11. (wait/cl.)
565 // 12. (wait/cl.) 326 // 12. (wait/cl.)
566 327
567 #if defined(OS_ANDROID) 328 #if defined(OS_ANDROID)
568 // Android multi-process tests are not executing the new process. This is flaky. 329 // Android multi-process tests are not executing the new process. This is flaky.
569 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels 330 #define MAYBE_MultiprocessChannels DISABLED_MultiprocessChannels
570 #else 331 #else
571 #define MAYBE_MultiprocessChannels MultiprocessChannels 332 #define MAYBE_MultiprocessChannels MultiprocessChannels
572 #endif // defined(OS_ANDROID) 333 #endif // defined(OS_ANDROID)
573 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) { 334 TEST_F(EmbedderTest, MAYBE_MultiprocessChannels) {
574 // TODO(vtl): This should eventually initialize a master process instead, 335 test::MultiprocessTestHelper multiprocess_test_helper;
575 // probably.
576 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner());
577
578 mojo::test::MultiprocessTestHelper multiprocess_test_helper;
579 multiprocess_test_helper.StartChild("MultiprocessChannelsClient"); 336 multiprocess_test_helper.StartChild("MultiprocessChannelsClient");
580 337
581 { 338 {
582 ScopedTestChannel server_channel( 339 MojoHandle server_mp = CreateMessagePipe(
583 multiprocess_test_helper.server_platform_handle.Pass()); 340 multiprocess_test_helper.server_platform_handle.Pass()).release().
584 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); 341 value();
585 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
586 server_channel.WaitForChannelCreationCompletion();
587 EXPECT_TRUE(server_channel.channel_info());
588 342
589 // 1. Write a message to |server_mp| (attaching nothing). 343 // 1. Write a message to |server_mp| (attaching nothing).
590 const char kHello[] = "hello"; 344 const char kHello[] = "hello";
591 EXPECT_EQ(MOJO_RESULT_OK, 345 EXPECT_EQ(MOJO_RESULT_OK,
592 MojoWriteMessage(server_mp, kHello, 346 MojoWriteMessage(server_mp, kHello,
593 static_cast<uint32_t>(sizeof(kHello)), nullptr, 347 static_cast<uint32_t>(sizeof(kHello)), nullptr,
594 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); 348 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
595 349
596 // TODO(vtl): If the scope were ended immediately here (maybe after closing 350 // TODO(vtl): If the scope were ended immediately here (maybe after closing
597 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|. 351 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 EXPECT_EQ(MOJO_RESULT_OK, 400 EXPECT_EQ(MOJO_RESULT_OK,
647 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles, 401 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles,
648 MOJO_READ_MESSAGE_FLAG_NONE)); 402 MOJO_READ_MESSAGE_FLAG_NONE));
649 const char kQuux[] = "quux"; 403 const char kQuux[] = "quux";
650 EXPECT_EQ(sizeof(kQuux), num_bytes); 404 EXPECT_EQ(sizeof(kQuux), num_bytes);
651 EXPECT_STREQ(kQuux, buffer); 405 EXPECT_STREQ(kQuux, buffer);
652 EXPECT_EQ(1u, num_handles); 406 EXPECT_EQ(1u, num_handles);
653 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); 407 EXPECT_NE(mp2, MOJO_HANDLE_INVALID);
654 408
655 // 7. Read a message from |mp2|. 409 // 7. Read a message from |mp2|.
656 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, 410 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
657 MOJO_DEADLINE_INDEFINITE, &state)); 411 MOJO_DEADLINE_INDEFINITE, &state));
658 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 412 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
659 state.satisfied_signals); 413 state.satisfied_signals);
660 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, 414 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
661 state.satisfiable_signals); 415 state.satisfiable_signals);
662 416
663 memset(buffer, 0, sizeof(buffer)); 417 memset(buffer, 0, sizeof(buffer));
664 num_bytes = static_cast<uint32_t>(sizeof(buffer)); 418 num_bytes = static_cast<uint32_t>(sizeof(buffer));
665 EXPECT_EQ(MOJO_RESULT_OK, 419 EXPECT_EQ(MOJO_RESULT_OK,
666 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr, 420 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr,
(...skipping 16 matching lines...) Expand all
683 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, state.satisfiable_signals); 437 EXPECT_EQ(MOJO_HANDLE_SIGNAL_NONE, state.satisfiable_signals);
684 #endif 438 #endif
685 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp2)); 439 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp2));
686 } 440 }
687 441
688 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown()); 442 EXPECT_TRUE(multiprocess_test_helper.WaitForChildTestShutdown());
689 } 443 }
690 444
691 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) { 445 MOJO_MULTIPROCESS_TEST_CHILD_TEST(MultiprocessChannelsClient) {
692 ScopedPlatformHandle client_platform_handle = 446 ScopedPlatformHandle client_platform_handle =
693 mojo::test::MultiprocessTestHelper::client_platform_handle.Pass(); 447 test::MultiprocessTestHelper::client_platform_handle.Pass();
694 EXPECT_TRUE(client_platform_handle.is_valid()); 448 EXPECT_TRUE(client_platform_handle.is_valid());
695 449
696 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart); 450 base::TestIOThread test_io_thread(base::TestIOThread::kAutoStart);
697 test::InitWithSimplePlatformSupport();
698 451
699 { 452 {
700 // TODO(vtl): This should eventually initialize a slave process instead, 453 test::ScopedIPCSupport ipc_support(test_io_thread.task_runner());
701 // probably. 454 MojoHandle client_mp = CreateMessagePipe(
702 mojo::test::ScopedIPCSupport ipc_support(test_io_thread.task_runner()); 455 client_platform_handle.Pass()).release().value();
703
704 ScopedTestChannel client_channel(client_platform_handle.Pass());
705 MojoHandle client_mp = client_channel.bootstrap_message_pipe();
706 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
707 client_channel.WaitForChannelCreationCompletion();
708 CHECK(client_channel.channel_info() != nullptr);
709 456
710 // 1. Read the first message from |client_mp|. 457 // 1. Read the first message from |client_mp|.
711 MojoHandleSignalsState state; 458 MojoHandleSignalsState state;
712 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, 459 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE,
713 MOJO_DEADLINE_INDEFINITE, &state)); 460 MOJO_DEADLINE_INDEFINITE, &state));
714 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals); 461 EXPECT_EQ(kSignalReadadableWritable, state.satisfied_signals);
715 EXPECT_EQ(kSignalAll, state.satisfiable_signals); 462 EXPECT_EQ(kSignalAll, state.satisfiable_signals);
716 463
717 char buffer[1000] = {}; 464 char buffer[1000] = {};
718 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); 465 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 EXPECT_STREQ(kFoo, buffer); 543 EXPECT_STREQ(kFoo, buffer);
797 544
798 // 11. Wait on |mp1| (which should eventually fail) and then close it. 545 // 11. Wait on |mp1| (which should eventually fail) and then close it.
799 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 546 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
800 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, 547 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE,
801 MOJO_DEADLINE_INDEFINITE, &state)); 548 MOJO_DEADLINE_INDEFINITE, &state));
802 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); 549 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals);
803 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); 550 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals);
804 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); 551 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1));
805 } 552 }
806
807 EXPECT_TRUE(test::Shutdown());
808 } 553 }
809 554
810 // TODO(vtl): Test immediate write & close. 555 // TODO(vtl): Test immediate write & close.
811 // TODO(vtl): Test broken-connection cases. 556 // TODO(vtl): Test broken-connection cases.
812 557
813 } // namespace 558 } // namespace
814 } // namespace embedder 559 } // namespace edk
815 } // namespace mojo 560 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698