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