OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdint.h> | 5 #include <stdint.h> |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 MOJO_RESULT_OK); | 175 MOJO_RESULT_OK); |
176 read_buffer.resize(read_buffer_size); | 176 read_buffer.resize(read_buffer_size); |
177 VLOG(2) << "Child got: " << read_buffer; | 177 VLOG(2) << "Child got: " << read_buffer; |
178 | 178 |
179 if (read_buffer == quitquitquit) { | 179 if (read_buffer == quitquitquit) { |
180 VLOG(2) << "Child quitting."; | 180 VLOG(2) << "Child quitting."; |
181 break; | 181 break; |
182 } | 182 } |
183 | 183 |
184 std::string write_buffer = read_buffer + read_buffer; | 184 std::string write_buffer = read_buffer + read_buffer; |
185 CHECK_EQ(mp->WriteMessage(0, | 185 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(write_buffer.data()), |
186 write_buffer.data(), | 186 static_cast<uint32_t>(write_buffer.size()), NULL, |
187 static_cast<uint32_t>(write_buffer.size()), | |
188 NULL, | |
189 MOJO_WRITE_MESSAGE_FLAG_NONE), | 187 MOJO_WRITE_MESSAGE_FLAG_NONE), |
190 MOJO_RESULT_OK); | 188 MOJO_RESULT_OK); |
191 } | 189 } |
192 | 190 |
193 mp->Close(0); | 191 mp->Close(0); |
194 return rv; | 192 return rv; |
195 } | 193 } |
196 | 194 |
197 // Sends "hello" to child, and expects "hellohello" back. | 195 // Sends "hello" to child, and expects "hellohello" back. |
198 TEST_F(MultiprocessMessagePipeTest, Basic) { | 196 TEST_F(MultiprocessMessagePipeTest, Basic) { |
199 helper()->StartChild("EchoEcho"); | 197 helper()->StartChild("EchoEcho"); |
200 | 198 |
201 scoped_refptr<MessagePipe> mp(new MessagePipe( | 199 scoped_refptr<MessagePipe> mp(new MessagePipe( |
202 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), | 200 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
203 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); | 201 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
204 Init(mp); | 202 Init(mp); |
205 | 203 |
206 std::string hello("hello"); | 204 std::string hello("hello"); |
207 EXPECT_EQ(MOJO_RESULT_OK, | 205 EXPECT_EQ(MOJO_RESULT_OK, |
208 mp->WriteMessage(0, | 206 mp->WriteMessage(0, UserPointer<const void>(hello.data()), |
209 hello.data(), static_cast<uint32_t>(hello.size()), | 207 static_cast<uint32_t>(hello.size()), NULL, |
210 NULL, | |
211 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 208 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
212 | 209 |
213 EXPECT_EQ(MOJO_RESULT_OK, WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); | 210 EXPECT_EQ(MOJO_RESULT_OK, WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); |
214 | 211 |
215 std::string read_buffer(1000, '\0'); | 212 std::string read_buffer(1000, '\0'); |
216 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | 213 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); |
217 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 214 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), |
218 MakeUserPointer(&read_buffer_size), NULL, NULL, | 215 MakeUserPointer(&read_buffer_size), NULL, NULL, |
219 MOJO_READ_MESSAGE_FLAG_NONE), | 216 MOJO_READ_MESSAGE_FLAG_NONE), |
220 MOJO_RESULT_OK); | 217 MOJO_RESULT_OK); |
(...skipping 14 matching lines...) Expand all Loading... |
235 | 232 |
236 scoped_refptr<MessagePipe> mp(new MessagePipe( | 233 scoped_refptr<MessagePipe> mp(new MessagePipe( |
237 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), | 234 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
238 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); | 235 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
239 Init(mp); | 236 Init(mp); |
240 | 237 |
241 static const size_t kNumMessages = 1001; | 238 static const size_t kNumMessages = 1001; |
242 for (size_t i = 0; i < kNumMessages; i++) { | 239 for (size_t i = 0; i < kNumMessages; i++) { |
243 std::string write_buffer(i, 'A' + (i % 26)); | 240 std::string write_buffer(i, 'A' + (i % 26)); |
244 EXPECT_EQ(MOJO_RESULT_OK, | 241 EXPECT_EQ(MOJO_RESULT_OK, |
245 mp->WriteMessage(0, | 242 mp->WriteMessage(0, UserPointer<const void>(write_buffer.data()), |
246 write_buffer.data(), | 243 static_cast<uint32_t>(write_buffer.size()), NULL, |
247 static_cast<uint32_t>(write_buffer.size()), | |
248 NULL, | |
249 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 244 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
250 } | 245 } |
251 | 246 |
252 const std::string quitquitquit("quitquitquit"); | 247 const std::string quitquitquit("quitquitquit"); |
253 EXPECT_EQ(MOJO_RESULT_OK, | 248 EXPECT_EQ(MOJO_RESULT_OK, |
254 mp->WriteMessage(0, | 249 mp->WriteMessage(0, UserPointer<const void>(quitquitquit.data()), |
255 quitquitquit.data(), | 250 static_cast<uint32_t>(quitquitquit.size()), NULL, |
256 static_cast<uint32_t>(quitquitquit.size()), | |
257 NULL, | |
258 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 251 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
259 | 252 |
260 for (size_t i = 0; i < kNumMessages; i++) { | 253 for (size_t i = 0; i < kNumMessages; i++) { |
261 EXPECT_EQ(MOJO_RESULT_OK, WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); | 254 EXPECT_EQ(MOJO_RESULT_OK, WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); |
262 | 255 |
263 std::string read_buffer(kNumMessages * 2, '\0'); | 256 std::string read_buffer(kNumMessages * 2, '\0'); |
264 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | 257 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); |
265 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 258 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), |
266 MakeUserPointer(&read_buffer_size), NULL, NULL, | 259 MakeUserPointer(&read_buffer_size), NULL, NULL, |
267 MOJO_READ_MESSAGE_FLAG_NONE), | 260 MOJO_READ_MESSAGE_FLAG_NONE), |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 | 316 |
324 // Write some stuff to the shared buffer. | 317 // Write some stuff to the shared buffer. |
325 static const char kHello[] = "hello"; | 318 static const char kHello[] = "hello"; |
326 memcpy(mapping->base(), kHello, sizeof(kHello)); | 319 memcpy(mapping->base(), kHello, sizeof(kHello)); |
327 | 320 |
328 // We should be able to close the dispatcher now. | 321 // We should be able to close the dispatcher now. |
329 dispatcher->Close(); | 322 dispatcher->Close(); |
330 | 323 |
331 // And send a message to signal that we've written stuff. | 324 // And send a message to signal that we've written stuff. |
332 const std::string go2("go 2"); | 325 const std::string go2("go 2"); |
333 CHECK_EQ(mp->WriteMessage(0, | 326 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(&go2[0]), |
334 &go2[0], | 327 static_cast<uint32_t>(go2.size()), NULL, |
335 static_cast<uint32_t>(go2.size()), | |
336 NULL, | |
337 MOJO_WRITE_MESSAGE_FLAG_NONE), | 328 MOJO_WRITE_MESSAGE_FLAG_NONE), |
338 MOJO_RESULT_OK); | 329 MOJO_RESULT_OK); |
339 | 330 |
340 // Now wait for our parent to send us a message. | 331 // Now wait for our parent to send us a message. |
341 CHECK_EQ(WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK); | 332 CHECK_EQ(WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK); |
342 | 333 |
343 read_buffer = std::string(100, '\0'); | 334 read_buffer = std::string(100, '\0'); |
344 num_bytes = static_cast<uint32_t>(read_buffer.size()); | 335 num_bytes = static_cast<uint32_t>(read_buffer.size()); |
345 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 336 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), |
346 MakeUserPointer(&num_bytes), NULL, NULL, | 337 MakeUserPointer(&num_bytes), NULL, NULL, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 | 382 |
392 // Send the shared buffer. | 383 // Send the shared buffer. |
393 const std::string go1("go 1"); | 384 const std::string go1("go 1"); |
394 DispatcherTransport transport( | 385 DispatcherTransport transport( |
395 test::DispatcherTryStartTransport(dispatcher.get())); | 386 test::DispatcherTryStartTransport(dispatcher.get())); |
396 ASSERT_TRUE(transport.is_valid()); | 387 ASSERT_TRUE(transport.is_valid()); |
397 | 388 |
398 std::vector<DispatcherTransport> transports; | 389 std::vector<DispatcherTransport> transports; |
399 transports.push_back(transport); | 390 transports.push_back(transport); |
400 EXPECT_EQ(MOJO_RESULT_OK, | 391 EXPECT_EQ(MOJO_RESULT_OK, |
401 mp->WriteMessage(0, | 392 mp->WriteMessage(0, UserPointer<const void>(&go1[0]), |
402 &go1[0], | 393 static_cast<uint32_t>(go1.size()), &transports, |
403 static_cast<uint32_t>(go1.size()), | |
404 &transports, | |
405 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 394 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
406 transport.End(); | 395 transport.End(); |
407 | 396 |
408 EXPECT_TRUE(dispatcher->HasOneRef()); | 397 EXPECT_TRUE(dispatcher->HasOneRef()); |
409 dispatcher = NULL; | 398 dispatcher = NULL; |
410 | 399 |
411 // Wait for a message from the child. | 400 // Wait for a message from the child. |
412 EXPECT_EQ(MOJO_RESULT_OK, WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); | 401 EXPECT_EQ(MOJO_RESULT_OK, WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); |
413 | 402 |
414 std::string read_buffer(100, '\0'); | 403 std::string read_buffer(100, '\0'); |
(...skipping 10 matching lines...) Expand all Loading... |
425 static const char kHello[] = "hello"; | 414 static const char kHello[] = "hello"; |
426 EXPECT_EQ(0, memcmp(mapping->base(), kHello, sizeof(kHello))); | 415 EXPECT_EQ(0, memcmp(mapping->base(), kHello, sizeof(kHello))); |
427 | 416 |
428 // Now we'll write some stuff to the shared buffer. | 417 // Now we'll write some stuff to the shared buffer. |
429 static const char kWorld[] = "world!!!"; | 418 static const char kWorld[] = "world!!!"; |
430 memcpy(mapping->base(), kWorld, sizeof(kWorld)); | 419 memcpy(mapping->base(), kWorld, sizeof(kWorld)); |
431 | 420 |
432 // And send a message to signal that we've written stuff. | 421 // And send a message to signal that we've written stuff. |
433 const std::string go3("go 3"); | 422 const std::string go3("go 3"); |
434 EXPECT_EQ(MOJO_RESULT_OK, | 423 EXPECT_EQ(MOJO_RESULT_OK, |
435 mp->WriteMessage(0, | 424 mp->WriteMessage(0, UserPointer<const void>(&go3[0]), |
436 &go3[0], | 425 static_cast<uint32_t>(go3.size()), NULL, |
437 static_cast<uint32_t>(go3.size()), | |
438 NULL, | |
439 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 426 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
440 | 427 |
441 // Wait for |mp| to become readable, which should fail. | 428 // Wait for |mp| to become readable, which should fail. |
442 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 429 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
443 WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); | 430 WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); |
444 | 431 |
445 mp->Close(0); | 432 mp->Close(0); |
446 | 433 |
447 EXPECT_EQ(0, helper()->WaitForChildShutdown()); | 434 EXPECT_EQ(0, helper()->WaitForChildShutdown()); |
448 } | 435 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 new PlatformHandleDispatcher(h.Pass())); | 509 new PlatformHandleDispatcher(h.Pass())); |
523 | 510 |
524 const std::string hello("hello"); | 511 const std::string hello("hello"); |
525 DispatcherTransport transport( | 512 DispatcherTransport transport( |
526 test::DispatcherTryStartTransport(dispatcher.get())); | 513 test::DispatcherTryStartTransport(dispatcher.get())); |
527 ASSERT_TRUE(transport.is_valid()); | 514 ASSERT_TRUE(transport.is_valid()); |
528 | 515 |
529 std::vector<DispatcherTransport> transports; | 516 std::vector<DispatcherTransport> transports; |
530 transports.push_back(transport); | 517 transports.push_back(transport); |
531 EXPECT_EQ(MOJO_RESULT_OK, | 518 EXPECT_EQ(MOJO_RESULT_OK, |
532 mp->WriteMessage(0, | 519 mp->WriteMessage(0, UserPointer<const void>(&hello[0]), |
533 &hello[0], | 520 static_cast<uint32_t>(hello.size()), &transports, |
534 static_cast<uint32_t>(hello.size()), | |
535 &transports, | |
536 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 521 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
537 transport.End(); | 522 transport.End(); |
538 | 523 |
539 EXPECT_TRUE(dispatcher->HasOneRef()); | 524 EXPECT_TRUE(dispatcher->HasOneRef()); |
540 dispatcher = NULL; | 525 dispatcher = NULL; |
541 | 526 |
542 // Wait for it to become readable, which should fail. | 527 // Wait for it to become readable, which should fail. |
543 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 528 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
544 WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); | 529 WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE)); |
545 | 530 |
546 mp->Close(0); | 531 mp->Close(0); |
547 | 532 |
548 EXPECT_EQ(0, helper()->WaitForChildShutdown()); | 533 EXPECT_EQ(0, helper()->WaitForChildShutdown()); |
549 } | 534 } |
550 | 535 |
551 } // namespace | 536 } // namespace |
552 } // namespace system | 537 } // namespace system |
553 } // namespace mojo | 538 } // namespace mojo |
OLD | NEW |