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 <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 <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 MOJO_READ_MESSAGE_FLAG_NONE)); | 472 MOJO_READ_MESSAGE_FLAG_NONE)); |
473 EXPECT_EQ(sizeof(kWorld), static_cast<size_t>(buffer_size)); | 473 EXPECT_EQ(sizeof(kWorld), static_cast<size_t>(buffer_size)); |
474 EXPECT_STREQ(kWorld, buffer); | 474 EXPECT_STREQ(kWorld, buffer); |
475 | 475 |
476 mp0->Close(0); | 476 mp0->Close(0); |
477 mp1->Close(1); | 477 mp1->Close(1); |
478 mp2->Close(0); | 478 mp2->Close(0); |
479 mp3->Close(0); | 479 mp3->Close(0); |
480 } | 480 } |
481 | 481 |
| 482 TEST_F(RemoteMessagePipeTest, CloseBeforeAttachAndRun) { |
| 483 static const char kHello[] = "hello"; |
| 484 char buffer[100] = {0}; |
| 485 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 486 Waiter waiter; |
| 487 HandleSignalsState hss; |
| 488 uint32_t context = 0; |
| 489 |
| 490 // Connect message pipes. MP 0, port 1 will be attached to channel 0 and |
| 491 // connected to MP 1, port 0, which will be attached to channel 1. This leaves |
| 492 // MP 0, port 0 and MP 1, port 1 as the "user-facing" endpoints. |
| 493 |
| 494 scoped_refptr<ChannelEndpoint> ep0; |
| 495 scoped_refptr<MessagePipe> mp0(MessagePipe::CreateLocalProxy(&ep0)); |
| 496 |
| 497 // Write to MP 0, port 0. |
| 498 EXPECT_EQ(MOJO_RESULT_OK, |
| 499 mp0->WriteMessage(0, |
| 500 UserPointer<const void>(kHello), |
| 501 sizeof(kHello), |
| 502 nullptr, |
| 503 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 504 |
| 505 // Close MP 0, port 0 before it's even been attached to the channel and run. |
| 506 mp0->Close(0); |
| 507 |
| 508 BootstrapChannelEndpointNoWait(0, ep0); |
| 509 |
| 510 scoped_refptr<ChannelEndpoint> ep1; |
| 511 scoped_refptr<MessagePipe> mp1(MessagePipe::CreateProxyLocal(&ep1)); |
| 512 |
| 513 // Prepare to wait on MP 1, port 1. (Add the waiter now. Otherwise, if we do |
| 514 // it later, it might already be readable.) |
| 515 waiter.Init(); |
| 516 ASSERT_EQ( |
| 517 MOJO_RESULT_OK, |
| 518 mp1->AddWaiter(1, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); |
| 519 |
| 520 BootstrapChannelEndpointNoWait(1, ep1); |
| 521 |
| 522 // Wait. |
| 523 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(MOJO_DEADLINE_INDEFINITE, &context)); |
| 524 EXPECT_EQ(123u, context); |
| 525 hss = HandleSignalsState(); |
| 526 // Note: MP 1, port 1 should definitely should be readable, but it may or may |
| 527 // not appear as writable (there's a race, and it may not have noticed that |
| 528 // the other side was closed yet -- e.g., inserting a sleep here would make it |
| 529 // much more likely to notice that it's no longer writable). |
| 530 mp1->RemoveWaiter(1, &waiter, &hss); |
| 531 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
| 532 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
| 533 |
| 534 // Read from MP 1, port 1. |
| 535 EXPECT_EQ(MOJO_RESULT_OK, |
| 536 mp1->ReadMessage(1, |
| 537 UserPointer<void>(buffer), |
| 538 MakeUserPointer(&buffer_size), |
| 539 nullptr, |
| 540 nullptr, |
| 541 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 542 EXPECT_EQ(sizeof(kHello), static_cast<size_t>(buffer_size)); |
| 543 EXPECT_STREQ(kHello, buffer); |
| 544 |
| 545 // And MP 1, port 1. |
| 546 mp1->Close(1); |
| 547 } |
| 548 |
482 TEST_F(RemoteMessagePipeTest, CloseBeforeConnect) { | 549 TEST_F(RemoteMessagePipeTest, CloseBeforeConnect) { |
483 static const char kHello[] = "hello"; | 550 static const char kHello[] = "hello"; |
484 char buffer[100] = {0}; | 551 char buffer[100] = {0}; |
485 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 552 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
486 Waiter waiter; | 553 Waiter waiter; |
487 HandleSignalsState hss; | 554 HandleSignalsState hss; |
488 uint32_t context = 0; | 555 uint32_t context = 0; |
489 | 556 |
490 // Connect message pipes. MP 0, port 1 will be attached to channel 0 and | 557 // Connect message pipes. MP 0, port 1 will be attached to channel 0 and |
491 // connected to MP 1, port 0, which will be attached to channel 1. This leaves | 558 // connected to MP 1, port 0, which will be attached to channel 1. This leaves |
(...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1221 mp0->Close(0); | 1288 mp0->Close(0); |
1222 mp1->Close(1); | 1289 mp1->Close(1); |
1223 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 1290 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
1224 // Note that |local_mp|'s port 0 belong to |dispatcher|, which was closed. | 1291 // Note that |local_mp|'s port 0 belong to |dispatcher|, which was closed. |
1225 local_mp->Close(1); | 1292 local_mp->Close(1); |
1226 } | 1293 } |
1227 | 1294 |
1228 } // namespace | 1295 } // namespace |
1229 } // namespace system | 1296 } // namespace system |
1230 } // namespace mojo | 1297 } // namespace mojo |
OLD | NEW |