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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 // pipe on this channel; it is up to the caller to close this handle. | 32 // pipe on this channel; it is up to the caller to close this handle. |
33 // Note: The I/O thread must outlive this object (and its message loop must | 33 // Note: The I/O thread must outlive this object (and its message loop must |
34 // continue pumping messages while this object is alive). | 34 // continue pumping messages while this object is alive). |
35 ScopedTestChannel(scoped_refptr<base::TaskRunner> io_thread_task_runner, | 35 ScopedTestChannel(scoped_refptr<base::TaskRunner> io_thread_task_runner, |
36 ScopedPlatformHandle platform_handle) | 36 ScopedPlatformHandle platform_handle) |
37 : io_thread_task_runner_(io_thread_task_runner), | 37 : io_thread_task_runner_(io_thread_task_runner), |
38 bootstrap_message_pipe_(MOJO_HANDLE_INVALID), | 38 bootstrap_message_pipe_(MOJO_HANDLE_INVALID), |
39 did_create_channel_event_(true, false), | 39 did_create_channel_event_(true, false), |
40 channel_info_(nullptr) { | 40 channel_info_(nullptr) { |
41 bootstrap_message_pipe_ = | 41 bootstrap_message_pipe_ = |
42 CreateChannel(platform_handle.Pass(), | 42 CreateChannel(platform_handle.Pass(), io_thread_task_runner_, |
43 io_thread_task_runner_, | |
44 base::Bind(&ScopedTestChannel::DidCreateChannel, | 43 base::Bind(&ScopedTestChannel::DidCreateChannel, |
45 base::Unretained(this)), | 44 base::Unretained(this)), |
46 nullptr) | 45 nullptr) |
47 .release() | 46 .release() |
48 .value(); | 47 .value(); |
49 CHECK_NE(bootstrap_message_pipe_, MOJO_HANDLE_INVALID); | 48 CHECK_NE(bootstrap_message_pipe_, MOJO_HANDLE_INVALID); |
50 } | 49 } |
51 | 50 |
52 // Destructor: Shuts down the channel. (As noted above, for this to happen, | 51 // Destructor: Shuts down the channel. (As noted above, for this to happen, |
53 // the I/O thread must be alive and pumping messages.) | 52 // the I/O thread must be alive and pumping messages.) |
54 ~ScopedTestChannel() { | 53 ~ScopedTestChannel() { DestroyChannel(channel_info_); } |
55 system::test::PostTaskAndWait( | |
56 io_thread_task_runner_, | |
57 FROM_HERE, | |
58 base::Bind(&ScopedTestChannel::DestroyChannel, base::Unretained(this))); | |
59 } | |
60 | 54 |
61 // Waits for channel creation to be completed. | 55 // Waits for channel creation to be completed. |
62 void WaitForChannelCreationCompletion() { did_create_channel_event_.Wait(); } | 56 void WaitForChannelCreationCompletion() { did_create_channel_event_.Wait(); } |
63 | 57 |
64 MojoHandle bootstrap_message_pipe() const { return bootstrap_message_pipe_; } | 58 MojoHandle bootstrap_message_pipe() const { return bootstrap_message_pipe_; } |
65 | 59 |
66 // Call only after |WaitForChannelCreationCompletion()|. Use only to check | 60 // Call only after |WaitForChannelCreationCompletion()|. Use only to check |
67 // that it's not null. | 61 // that it's not null. |
68 const ChannelInfo* channel_info() const { return channel_info_; } | 62 const ChannelInfo* channel_info() const { return channel_info_; } |
69 | 63 |
70 private: | 64 private: |
71 void DidCreateChannel(ChannelInfo* channel_info) { | 65 void DidCreateChannel(ChannelInfo* channel_info) { |
72 CHECK(channel_info); | 66 CHECK(channel_info); |
73 CHECK(!channel_info_); | 67 CHECK(!channel_info_); |
74 channel_info_ = channel_info; | 68 channel_info_ = channel_info; |
75 did_create_channel_event_.Signal(); | 69 did_create_channel_event_.Signal(); |
76 } | 70 } |
77 | 71 |
78 void DestroyChannel() { | |
79 CHECK(channel_info_); | |
80 DestroyChannelOnIOThread(channel_info_); | |
81 channel_info_ = nullptr; | |
82 } | |
83 | |
84 scoped_refptr<base::TaskRunner> io_thread_task_runner_; | 72 scoped_refptr<base::TaskRunner> io_thread_task_runner_; |
85 | 73 |
86 // Valid from creation until whenever it gets closed (by the "owner" of this | 74 // Valid from creation until whenever it gets closed (by the "owner" of this |
87 // object). | 75 // object). |
88 // Note: We don't want use the C++ wrappers here, since we want to test the | 76 // Note: We don't want use the C++ wrappers here, since we want to test the |
89 // API at the lowest level. | 77 // API at the lowest level. |
90 MojoHandle bootstrap_message_pipe_; | 78 MojoHandle bootstrap_message_pipe_; |
91 | 79 |
92 // Set after channel creation has been completed (i.e., the callback to | 80 // Set after channel creation has been completed (i.e., the callback to |
93 // |CreateChannel()| has been called). | 81 // |CreateChannel()| has been called). |
(...skipping 29 matching lines...) Expand all Loading... |
123 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 111 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
124 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 112 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
125 ScopedTestChannel client_channel(test_io_thread()->task_runner(), | 113 ScopedTestChannel client_channel(test_io_thread()->task_runner(), |
126 channel_pair.PassClientHandle()); | 114 channel_pair.PassClientHandle()); |
127 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | 115 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
128 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | 116 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
129 | 117 |
130 // We can write to a message pipe handle immediately. | 118 // We can write to a message pipe handle immediately. |
131 const char kHello[] = "hello"; | 119 const char kHello[] = "hello"; |
132 EXPECT_EQ(MOJO_RESULT_OK, | 120 EXPECT_EQ(MOJO_RESULT_OK, |
133 MojoWriteMessage(server_mp, | 121 MojoWriteMessage(server_mp, kHello, |
134 kHello, | 122 static_cast<uint32_t>(sizeof(kHello)), nullptr, |
135 static_cast<uint32_t>(sizeof(kHello)), | 123 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
136 nullptr, | |
137 0, | |
138 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
139 | 124 |
140 // Now wait for the other side to become readable. | 125 // Now wait for the other side to become readable. |
141 EXPECT_EQ( | 126 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
142 MOJO_RESULT_OK, | 127 MOJO_DEADLINE_INDEFINITE)); |
143 MojoWait( | |
144 client_mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
145 | 128 |
146 char buffer[1000] = {}; | 129 char buffer[1000] = {}; |
147 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 130 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
148 EXPECT_EQ(MOJO_RESULT_OK, | 131 EXPECT_EQ(MOJO_RESULT_OK, |
149 MojoReadMessage(client_mp, | 132 MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr, |
150 buffer, | |
151 &num_bytes, | |
152 nullptr, | |
153 nullptr, | |
154 MOJO_READ_MESSAGE_FLAG_NONE)); | 133 MOJO_READ_MESSAGE_FLAG_NONE)); |
155 EXPECT_EQ(sizeof(kHello), num_bytes); | 134 EXPECT_EQ(sizeof(kHello), num_bytes); |
156 EXPECT_STREQ(kHello, buffer); | 135 EXPECT_STREQ(kHello, buffer); |
157 | 136 |
158 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | 137 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
159 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | 138 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); |
160 | 139 |
161 // By this point, these waits should basically be no-ops (since we've waited | 140 // By this point, these waits should basically be no-ops (since we've waited |
162 // for the client message pipe to become readable, which implies that both | 141 // for the client message pipe to become readable, which implies that both |
163 // the server and client channels were completely created). | 142 // the server and client channels were completely created). |
(...skipping 18 matching lines...) Expand all Loading... |
182 ScopedTestChannel client_channel(test_io_thread()->task_runner(), | 161 ScopedTestChannel client_channel(test_io_thread()->task_runner(), |
183 channel_pair.PassClientHandle()); | 162 channel_pair.PassClientHandle()); |
184 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | 163 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
185 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | 164 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
186 | 165 |
187 MojoHandle h0, h1; | 166 MojoHandle h0, h1; |
188 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); | 167 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &h0, &h1)); |
189 | 168 |
190 // Write a message to |h0| (attaching nothing). | 169 // Write a message to |h0| (attaching nothing). |
191 const char kHello[] = "hello"; | 170 const char kHello[] = "hello"; |
192 EXPECT_EQ(MOJO_RESULT_OK, | 171 EXPECT_EQ( |
193 MojoWriteMessage(h0, | 172 MOJO_RESULT_OK, |
194 kHello, | 173 MojoWriteMessage(h0, kHello, static_cast<uint32_t>(sizeof(kHello)), |
195 static_cast<uint32_t>(sizeof(kHello)), | 174 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
196 nullptr, | |
197 0, | |
198 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
199 | 175 |
200 // Write one message to |server_mp|, attaching |h1|. | 176 // Write one message to |server_mp|, attaching |h1|. |
201 const char kWorld[] = "world!!!"; | 177 const char kWorld[] = "world!!!"; |
202 EXPECT_EQ(MOJO_RESULT_OK, | 178 EXPECT_EQ(MOJO_RESULT_OK, |
203 MojoWriteMessage(server_mp, | 179 MojoWriteMessage(server_mp, kWorld, |
204 kWorld, | 180 static_cast<uint32_t>(sizeof(kWorld)), &h1, 1, |
205 static_cast<uint32_t>(sizeof(kWorld)), | |
206 &h1, | |
207 1, | |
208 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 181 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
209 h1 = MOJO_HANDLE_INVALID; | 182 h1 = MOJO_HANDLE_INVALID; |
210 | 183 |
211 // Write another message to |h0|. | 184 // Write another message to |h0|. |
212 const char kFoo[] = "foo"; | 185 const char kFoo[] = "foo"; |
213 EXPECT_EQ(MOJO_RESULT_OK, | 186 EXPECT_EQ(MOJO_RESULT_OK, |
214 MojoWriteMessage(h0, | 187 MojoWriteMessage(h0, kFoo, static_cast<uint32_t>(sizeof(kFoo)), |
215 kFoo, | 188 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
216 static_cast<uint32_t>(sizeof(kFoo)), | |
217 nullptr, | |
218 0, | |
219 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
220 | 189 |
221 // Wait for |client_mp| to become readable. | 190 // Wait for |client_mp| to become readable. |
222 EXPECT_EQ( | 191 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
223 MOJO_RESULT_OK, | 192 MOJO_DEADLINE_INDEFINITE)); |
224 MojoWait( | |
225 client_mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
226 | 193 |
227 // Read a message from |client_mp|. | 194 // Read a message from |client_mp|. |
228 char buffer[1000] = {}; | 195 char buffer[1000] = {}; |
229 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 196 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
230 MojoHandle handles[10] = {}; | 197 MojoHandle handles[10] = {}; |
231 uint32_t num_handles = arraysize(handles); | 198 uint32_t num_handles = arraysize(handles); |
232 EXPECT_EQ(MOJO_RESULT_OK, | 199 EXPECT_EQ(MOJO_RESULT_OK, |
233 MojoReadMessage(client_mp, | 200 MojoReadMessage(client_mp, buffer, &num_bytes, handles, |
234 buffer, | 201 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE)); |
235 &num_bytes, | |
236 handles, | |
237 &num_handles, | |
238 MOJO_READ_MESSAGE_FLAG_NONE)); | |
239 EXPECT_EQ(sizeof(kWorld), num_bytes); | 202 EXPECT_EQ(sizeof(kWorld), num_bytes); |
240 EXPECT_STREQ(kWorld, buffer); | 203 EXPECT_STREQ(kWorld, buffer); |
241 EXPECT_EQ(1u, num_handles); | 204 EXPECT_EQ(1u, num_handles); |
242 EXPECT_NE(handles[0], MOJO_HANDLE_INVALID); | 205 EXPECT_NE(handles[0], MOJO_HANDLE_INVALID); |
243 h1 = handles[0]; | 206 h1 = handles[0]; |
244 | 207 |
245 // Wait for |h1| to become readable. | 208 // Wait for |h1| to become readable. |
246 EXPECT_EQ( | 209 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(h1, MOJO_HANDLE_SIGNAL_READABLE, |
247 MOJO_RESULT_OK, | 210 MOJO_DEADLINE_INDEFINITE)); |
248 MojoWait(h1, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
249 | 211 |
250 // Read a message from |h1|. | 212 // Read a message from |h1|. |
251 memset(buffer, 0, sizeof(buffer)); | 213 memset(buffer, 0, sizeof(buffer)); |
252 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 214 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
253 memset(handles, 0, sizeof(handles)); | 215 memset(handles, 0, sizeof(handles)); |
254 num_handles = arraysize(handles); | 216 num_handles = arraysize(handles); |
255 EXPECT_EQ(MOJO_RESULT_OK, | 217 EXPECT_EQ(MOJO_RESULT_OK, |
256 MojoReadMessage(h1, | 218 MojoReadMessage(h1, buffer, &num_bytes, handles, &num_handles, |
257 buffer, | |
258 &num_bytes, | |
259 handles, | |
260 &num_handles, | |
261 MOJO_READ_MESSAGE_FLAG_NONE)); | 219 MOJO_READ_MESSAGE_FLAG_NONE)); |
262 EXPECT_EQ(sizeof(kHello), num_bytes); | 220 EXPECT_EQ(sizeof(kHello), num_bytes); |
263 EXPECT_STREQ(kHello, buffer); | 221 EXPECT_STREQ(kHello, buffer); |
264 EXPECT_EQ(0u, num_handles); | 222 EXPECT_EQ(0u, num_handles); |
265 | 223 |
266 // Wait for |h1| to become readable (again). | 224 // Wait for |h1| to become readable (again). |
267 EXPECT_EQ( | 225 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(h1, MOJO_HANDLE_SIGNAL_READABLE, |
268 MOJO_RESULT_OK, | 226 MOJO_DEADLINE_INDEFINITE)); |
269 MojoWait(h1, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
270 | 227 |
271 // Read the second message from |h1|. | 228 // Read the second message from |h1|. |
272 memset(buffer, 0, sizeof(buffer)); | 229 memset(buffer, 0, sizeof(buffer)); |
273 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 230 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
274 EXPECT_EQ(MOJO_RESULT_OK, | 231 EXPECT_EQ(MOJO_RESULT_OK, |
275 MojoReadMessage(h1, | 232 MojoReadMessage(h1, buffer, &num_bytes, nullptr, nullptr, |
276 buffer, | |
277 &num_bytes, | |
278 nullptr, | |
279 nullptr, | |
280 MOJO_READ_MESSAGE_FLAG_NONE)); | 233 MOJO_READ_MESSAGE_FLAG_NONE)); |
281 EXPECT_EQ(sizeof(kFoo), num_bytes); | 234 EXPECT_EQ(sizeof(kFoo), num_bytes); |
282 EXPECT_STREQ(kFoo, buffer); | 235 EXPECT_STREQ(kFoo, buffer); |
283 | 236 |
284 // Write a message to |h1|. | 237 // Write a message to |h1|. |
285 const char kBarBaz[] = "barbaz"; | 238 const char kBarBaz[] = "barbaz"; |
286 EXPECT_EQ(MOJO_RESULT_OK, | 239 EXPECT_EQ( |
287 MojoWriteMessage(h1, | 240 MOJO_RESULT_OK, |
288 kBarBaz, | 241 MojoWriteMessage(h1, kBarBaz, static_cast<uint32_t>(sizeof(kBarBaz)), |
289 static_cast<uint32_t>(sizeof(kBarBaz)), | 242 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
290 nullptr, | |
291 0, | |
292 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
293 | 243 |
294 // Wait for |h0| to become readable. | 244 // Wait for |h0| to become readable. |
295 EXPECT_EQ( | 245 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(h0, MOJO_HANDLE_SIGNAL_READABLE, |
296 MOJO_RESULT_OK, | 246 MOJO_DEADLINE_INDEFINITE)); |
297 MojoWait(h0, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
298 | 247 |
299 // Read a message from |h0|. | 248 // Read a message from |h0|. |
300 memset(buffer, 0, sizeof(buffer)); | 249 memset(buffer, 0, sizeof(buffer)); |
301 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 250 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
302 EXPECT_EQ(MOJO_RESULT_OK, | 251 EXPECT_EQ(MOJO_RESULT_OK, |
303 MojoReadMessage(h0, | 252 MojoReadMessage(h0, buffer, &num_bytes, nullptr, nullptr, |
304 buffer, | |
305 &num_bytes, | |
306 nullptr, | |
307 nullptr, | |
308 MOJO_READ_MESSAGE_FLAG_NONE)); | 253 MOJO_READ_MESSAGE_FLAG_NONE)); |
309 EXPECT_EQ(sizeof(kBarBaz), num_bytes); | 254 EXPECT_EQ(sizeof(kBarBaz), num_bytes); |
310 EXPECT_STREQ(kBarBaz, buffer); | 255 EXPECT_STREQ(kBarBaz, buffer); |
311 | 256 |
312 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | 257 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
313 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | 258 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); |
314 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); | 259 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0)); |
315 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); | 260 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); |
316 | 261 |
317 server_channel.WaitForChannelCreationCompletion(); | 262 server_channel.WaitForChannelCreationCompletion(); |
(...skipping 29 matching lines...) Expand all Loading... |
347 test_io_thread()->task_runner(), | 292 test_io_thread()->task_runner(), |
348 multiprocess_test_helper.server_platform_handle.Pass()); | 293 multiprocess_test_helper.server_platform_handle.Pass()); |
349 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); | 294 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); |
350 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | 295 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); |
351 server_channel.WaitForChannelCreationCompletion(); | 296 server_channel.WaitForChannelCreationCompletion(); |
352 EXPECT_TRUE(server_channel.channel_info()); | 297 EXPECT_TRUE(server_channel.channel_info()); |
353 | 298 |
354 // 1. Write a message to |server_mp| (attaching nothing). | 299 // 1. Write a message to |server_mp| (attaching nothing). |
355 const char kHello[] = "hello"; | 300 const char kHello[] = "hello"; |
356 EXPECT_EQ(MOJO_RESULT_OK, | 301 EXPECT_EQ(MOJO_RESULT_OK, |
357 MojoWriteMessage(server_mp, | 302 MojoWriteMessage(server_mp, kHello, |
358 kHello, | 303 static_cast<uint32_t>(sizeof(kHello)), nullptr, |
359 static_cast<uint32_t>(sizeof(kHello)), | 304 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
360 nullptr, | |
361 0, | |
362 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
363 | 305 |
364 // TODO(vtl): If the scope were ended immediately here (maybe after closing | 306 // TODO(vtl): If the scope were ended immediately here (maybe after closing |
365 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|. | 307 // |server_mp|), we die with a fatal error in |Channel::HandleLocalError()|. |
366 | 308 |
367 // 2. Read a message from |server_mp|. | 309 // 2. Read a message from |server_mp|. |
368 EXPECT_EQ( | 310 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(server_mp, MOJO_HANDLE_SIGNAL_READABLE, |
369 MOJO_RESULT_OK, | 311 MOJO_DEADLINE_INDEFINITE)); |
370 MojoWait( | |
371 server_mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
372 char buffer[1000] = {}; | 312 char buffer[1000] = {}; |
373 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 313 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
374 EXPECT_EQ(MOJO_RESULT_OK, | 314 EXPECT_EQ(MOJO_RESULT_OK, |
375 MojoReadMessage(server_mp, | 315 MojoReadMessage(server_mp, buffer, &num_bytes, nullptr, nullptr, |
376 buffer, | |
377 &num_bytes, | |
378 nullptr, | |
379 nullptr, | |
380 MOJO_READ_MESSAGE_FLAG_NONE)); | 316 MOJO_READ_MESSAGE_FLAG_NONE)); |
381 const char kWorld[] = "world!"; | 317 const char kWorld[] = "world!"; |
382 EXPECT_EQ(sizeof(kWorld), num_bytes); | 318 EXPECT_EQ(sizeof(kWorld), num_bytes); |
383 EXPECT_STREQ(kWorld, buffer); | 319 EXPECT_STREQ(kWorld, buffer); |
384 | 320 |
385 // Create a new message pipe (endpoints |mp0| and |mp1|). | 321 // Create a new message pipe (endpoints |mp0| and |mp1|). |
386 MojoHandle mp0, mp1; | 322 MojoHandle mp0, mp1; |
387 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &mp0, &mp1)); | 323 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &mp0, &mp1)); |
388 | 324 |
389 // 3. Write something to |mp0|. | 325 // 3. Write something to |mp0|. |
390 const char kFoo[] = "FOO"; | 326 const char kFoo[] = "FOO"; |
391 EXPECT_EQ(MOJO_RESULT_OK, | 327 EXPECT_EQ(MOJO_RESULT_OK, |
392 MojoWriteMessage(mp0, | 328 MojoWriteMessage(mp0, kFoo, static_cast<uint32_t>(sizeof(kFoo)), |
393 kFoo, | 329 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
394 static_cast<uint32_t>(sizeof(kFoo)), | |
395 nullptr, | |
396 0, | |
397 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
398 | 330 |
399 // 4. Write a message to |server_mp|, attaching |mp1|. | 331 // 4. Write a message to |server_mp|, attaching |mp1|. |
400 const char kBar[] = "Bar"; | 332 const char kBar[] = "Bar"; |
401 EXPECT_EQ(MOJO_RESULT_OK, | 333 EXPECT_EQ( |
402 MojoWriteMessage(server_mp, | 334 MOJO_RESULT_OK, |
403 kBar, | 335 MojoWriteMessage(server_mp, kBar, static_cast<uint32_t>(sizeof(kBar)), |
404 static_cast<uint32_t>(sizeof(kBar)), | 336 &mp1, 1, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
405 &mp1, | |
406 1, | |
407 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
408 mp1 = MOJO_HANDLE_INVALID; | 337 mp1 = MOJO_HANDLE_INVALID; |
409 | 338 |
410 // 5. Close |server_mp|. | 339 // 5. Close |server_mp|. |
411 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | 340 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); |
412 | 341 |
413 // 9. Read a message from |mp0|, which should have |mp2| attached. | 342 // 9. Read a message from |mp0|, which should have |mp2| attached. |
414 EXPECT_EQ( | 343 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp0, MOJO_HANDLE_SIGNAL_READABLE, |
415 MOJO_RESULT_OK, | 344 MOJO_DEADLINE_INDEFINITE)); |
416 MojoWait(mp0, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
417 memset(buffer, 0, sizeof(buffer)); | 345 memset(buffer, 0, sizeof(buffer)); |
418 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 346 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
419 MojoHandle mp2 = MOJO_HANDLE_INVALID; | 347 MojoHandle mp2 = MOJO_HANDLE_INVALID; |
420 uint32_t num_handles = 1; | 348 uint32_t num_handles = 1; |
421 EXPECT_EQ(MOJO_RESULT_OK, | 349 EXPECT_EQ(MOJO_RESULT_OK, |
422 MojoReadMessage(mp0, | 350 MojoReadMessage(mp0, buffer, &num_bytes, &mp2, &num_handles, |
423 buffer, | |
424 &num_bytes, | |
425 &mp2, | |
426 &num_handles, | |
427 MOJO_READ_MESSAGE_FLAG_NONE)); | 351 MOJO_READ_MESSAGE_FLAG_NONE)); |
428 const char kQuux[] = "quux"; | 352 const char kQuux[] = "quux"; |
429 EXPECT_EQ(sizeof(kQuux), num_bytes); | 353 EXPECT_EQ(sizeof(kQuux), num_bytes); |
430 EXPECT_STREQ(kQuux, buffer); | 354 EXPECT_STREQ(kQuux, buffer); |
431 EXPECT_EQ(1u, num_handles); | 355 EXPECT_EQ(1u, num_handles); |
432 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); | 356 EXPECT_NE(mp2, MOJO_HANDLE_INVALID); |
433 | 357 |
434 // 7. Read a message from |mp2|. | 358 // 7. Read a message from |mp2|. |
435 EXPECT_EQ( | 359 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, |
436 MOJO_RESULT_OK, | 360 MOJO_DEADLINE_INDEFINITE)); |
437 MojoWait(mp2, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
438 memset(buffer, 0, sizeof(buffer)); | 361 memset(buffer, 0, sizeof(buffer)); |
439 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 362 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
440 EXPECT_EQ(MOJO_RESULT_OK, | 363 EXPECT_EQ(MOJO_RESULT_OK, |
441 MojoReadMessage(mp2, | 364 MojoReadMessage(mp2, buffer, &num_bytes, nullptr, nullptr, |
442 buffer, | |
443 &num_bytes, | |
444 nullptr, | |
445 nullptr, | |
446 MOJO_READ_MESSAGE_FLAG_NONE)); | 365 MOJO_READ_MESSAGE_FLAG_NONE)); |
447 const char kBaz[] = "baz"; | 366 const char kBaz[] = "baz"; |
448 EXPECT_EQ(sizeof(kBaz), num_bytes); | 367 EXPECT_EQ(sizeof(kBaz), num_bytes); |
449 EXPECT_STREQ(kBaz, buffer); | 368 EXPECT_STREQ(kBaz, buffer); |
450 | 369 |
451 // 10. Close |mp0|. | 370 // 10. Close |mp0|. |
452 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp0)); | 371 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp0)); |
453 | 372 |
454 // 12. Wait on |mp2| (which should eventually fail) and then close it. | 373 // 12. Wait on |mp2| (which should eventually fail) and then close it. |
455 // TODO(vtl): crbug.com/351768 | 374 // TODO(vtl): crbug.com/351768 |
(...skipping 19 matching lines...) Expand all Loading... |
475 | 394 |
476 { | 395 { |
477 ScopedTestChannel client_channel(test_io_thread.task_runner(), | 396 ScopedTestChannel client_channel(test_io_thread.task_runner(), |
478 client_platform_handle.Pass()); | 397 client_platform_handle.Pass()); |
479 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); | 398 MojoHandle client_mp = client_channel.bootstrap_message_pipe(); |
480 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | 399 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); |
481 client_channel.WaitForChannelCreationCompletion(); | 400 client_channel.WaitForChannelCreationCompletion(); |
482 CHECK(client_channel.channel_info() != nullptr); | 401 CHECK(client_channel.channel_info() != nullptr); |
483 | 402 |
484 // 1. Read the first message from |client_mp|. | 403 // 1. Read the first message from |client_mp|. |
485 EXPECT_EQ( | 404 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
486 MOJO_RESULT_OK, | 405 MOJO_DEADLINE_INDEFINITE)); |
487 MojoWait( | |
488 client_mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
489 char buffer[1000] = {}; | 406 char buffer[1000] = {}; |
490 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 407 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
491 EXPECT_EQ(MOJO_RESULT_OK, | 408 EXPECT_EQ(MOJO_RESULT_OK, |
492 MojoReadMessage(client_mp, | 409 MojoReadMessage(client_mp, buffer, &num_bytes, nullptr, nullptr, |
493 buffer, | |
494 &num_bytes, | |
495 nullptr, | |
496 nullptr, | |
497 MOJO_READ_MESSAGE_FLAG_NONE)); | 410 MOJO_READ_MESSAGE_FLAG_NONE)); |
498 const char kHello[] = "hello"; | 411 const char kHello[] = "hello"; |
499 EXPECT_EQ(sizeof(kHello), num_bytes); | 412 EXPECT_EQ(sizeof(kHello), num_bytes); |
500 EXPECT_STREQ(kHello, buffer); | 413 EXPECT_STREQ(kHello, buffer); |
501 | 414 |
502 // 2. Write a message to |client_mp| (attaching nothing). | 415 // 2. Write a message to |client_mp| (attaching nothing). |
503 const char kWorld[] = "world!"; | 416 const char kWorld[] = "world!"; |
504 EXPECT_EQ(MOJO_RESULT_OK, | 417 EXPECT_EQ(MOJO_RESULT_OK, |
505 MojoWriteMessage(client_mp, | 418 MojoWriteMessage(client_mp, kWorld, |
506 kWorld, | 419 static_cast<uint32_t>(sizeof(kWorld)), nullptr, |
507 static_cast<uint32_t>(sizeof(kWorld)), | 420 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
508 nullptr, | |
509 0, | |
510 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
511 | 421 |
512 // 4. Read a message from |client_mp|, which should have |mp1| attached. | 422 // 4. Read a message from |client_mp|, which should have |mp1| attached. |
513 EXPECT_EQ( | 423 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_READABLE, |
514 MOJO_RESULT_OK, | 424 MOJO_DEADLINE_INDEFINITE)); |
515 MojoWait( | |
516 client_mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
517 // TODO(vtl): If the scope were to end here (and |client_mp| closed), we'd | 425 // TODO(vtl): If the scope were to end here (and |client_mp| closed), we'd |
518 // die (again due to |Channel::HandleLocalError()|). | 426 // die (again due to |Channel::HandleLocalError()|). |
519 memset(buffer, 0, sizeof(buffer)); | 427 memset(buffer, 0, sizeof(buffer)); |
520 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 428 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
521 MojoHandle mp1 = MOJO_HANDLE_INVALID; | 429 MojoHandle mp1 = MOJO_HANDLE_INVALID; |
522 uint32_t num_handles = 1; | 430 uint32_t num_handles = 1; |
523 EXPECT_EQ(MOJO_RESULT_OK, | 431 EXPECT_EQ(MOJO_RESULT_OK, |
524 MojoReadMessage(client_mp, | 432 MojoReadMessage(client_mp, buffer, &num_bytes, &mp1, &num_handles, |
525 buffer, | |
526 &num_bytes, | |
527 &mp1, | |
528 &num_handles, | |
529 MOJO_READ_MESSAGE_FLAG_NONE)); | 433 MOJO_READ_MESSAGE_FLAG_NONE)); |
530 const char kBar[] = "Bar"; | 434 const char kBar[] = "Bar"; |
531 EXPECT_EQ(sizeof(kBar), num_bytes); | 435 EXPECT_EQ(sizeof(kBar), num_bytes); |
532 EXPECT_STREQ(kBar, buffer); | 436 EXPECT_STREQ(kBar, buffer); |
533 EXPECT_EQ(1u, num_handles); | 437 EXPECT_EQ(1u, num_handles); |
534 EXPECT_NE(mp1, MOJO_HANDLE_INVALID); | 438 EXPECT_NE(mp1, MOJO_HANDLE_INVALID); |
535 // TODO(vtl): If the scope were to end here (and the two handles closed), | 439 // TODO(vtl): If the scope were to end here (and the two handles closed), |
536 // we'd die due to |Channel::RunRemoteMessagePipeEndpoint()| not handling | 440 // we'd die due to |Channel::RunRemoteMessagePipeEndpoint()| not handling |
537 // write errors (assuming the parent had closed the pipe). | 441 // write errors (assuming the parent had closed the pipe). |
538 | 442 |
539 // 6. Close |client_mp|. | 443 // 6. Close |client_mp|. |
540 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | 444 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); |
541 | 445 |
542 // Create a new message pipe (endpoints |mp2| and |mp3|). | 446 // Create a new message pipe (endpoints |mp2| and |mp3|). |
543 MojoHandle mp2, mp3; | 447 MojoHandle mp2, mp3; |
544 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &mp2, &mp3)); | 448 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(nullptr, &mp2, &mp3)); |
545 | 449 |
546 // 7. Write a message to |mp3|. | 450 // 7. Write a message to |mp3|. |
547 const char kBaz[] = "baz"; | 451 const char kBaz[] = "baz"; |
548 EXPECT_EQ(MOJO_RESULT_OK, | 452 EXPECT_EQ(MOJO_RESULT_OK, |
549 MojoWriteMessage(mp3, | 453 MojoWriteMessage(mp3, kBaz, static_cast<uint32_t>(sizeof(kBaz)), |
550 kBaz, | 454 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
551 static_cast<uint32_t>(sizeof(kBaz)), | |
552 nullptr, | |
553 0, | |
554 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
555 | 455 |
556 // 8. Close |mp3|. | 456 // 8. Close |mp3|. |
557 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp3)); | 457 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp3)); |
558 | 458 |
559 // 9. Write a message to |mp1|, attaching |mp2|. | 459 // 9. Write a message to |mp1|, attaching |mp2|. |
560 const char kQuux[] = "quux"; | 460 const char kQuux[] = "quux"; |
561 EXPECT_EQ(MOJO_RESULT_OK, | 461 EXPECT_EQ(MOJO_RESULT_OK, |
562 MojoWriteMessage(mp1, | 462 MojoWriteMessage(mp1, kQuux, static_cast<uint32_t>(sizeof(kQuux)), |
563 kQuux, | 463 &mp2, 1, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
564 static_cast<uint32_t>(sizeof(kQuux)), | |
565 &mp2, | |
566 1, | |
567 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
568 mp2 = MOJO_HANDLE_INVALID; | 464 mp2 = MOJO_HANDLE_INVALID; |
569 | 465 |
570 // 3. Read a message from |mp1|. | 466 // 3. Read a message from |mp1|. |
571 EXPECT_EQ( | 467 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, |
572 MOJO_RESULT_OK, | 468 MOJO_DEADLINE_INDEFINITE)); |
573 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | |
574 memset(buffer, 0, sizeof(buffer)); | 469 memset(buffer, 0, sizeof(buffer)); |
575 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 470 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
576 EXPECT_EQ(MOJO_RESULT_OK, | 471 EXPECT_EQ(MOJO_RESULT_OK, |
577 MojoReadMessage(mp1, | 472 MojoReadMessage(mp1, buffer, &num_bytes, nullptr, nullptr, |
578 buffer, | |
579 &num_bytes, | |
580 nullptr, | |
581 nullptr, | |
582 MOJO_READ_MESSAGE_FLAG_NONE)); | 473 MOJO_READ_MESSAGE_FLAG_NONE)); |
583 const char kFoo[] = "FOO"; | 474 const char kFoo[] = "FOO"; |
584 EXPECT_EQ(sizeof(kFoo), num_bytes); | 475 EXPECT_EQ(sizeof(kFoo), num_bytes); |
585 EXPECT_STREQ(kFoo, buffer); | 476 EXPECT_STREQ(kFoo, buffer); |
586 | 477 |
587 // 11. Wait on |mp1| (which should eventually fail) and then close it. | 478 // 11. Wait on |mp1| (which should eventually fail) and then close it. |
588 EXPECT_EQ( | 479 EXPECT_EQ( |
589 MOJO_RESULT_FAILED_PRECONDITION, | 480 MOJO_RESULT_FAILED_PRECONDITION, |
590 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); | 481 MojoWait(mp1, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE)); |
591 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); | 482 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(mp1)); |
592 } | 483 } |
593 | 484 |
594 EXPECT_TRUE(test::Shutdown()); | 485 EXPECT_TRUE(test::Shutdown()); |
595 } | 486 } |
596 | 487 |
597 // TODO(vtl): Test immediate write & close. | 488 // TODO(vtl): Test immediate write & close. |
598 // TODO(vtl): Test broken-connection cases. | 489 // TODO(vtl): Test broken-connection cases. |
599 | 490 |
600 } // namespace | 491 } // namespace |
601 } // namespace embedder | 492 } // namespace embedder |
602 } // namespace mojo | 493 } // namespace mojo |
OLD | NEW |