| 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 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "mojo/edk/system/test_utils.h" | 8 #include "mojo/edk/system/test_utils.h" |
| 9 #include "mojo/edk/test/mojo_test_base.h" | 9 #include "mojo/edk/test/mojo_test_base.h" |
| 10 #include "mojo/public/c/system/core.h" | 10 #include "mojo/public/c/system/core.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 may_discard ? MOJO_READ_MESSAGE_FLAG_MAY_DISCARD : | 47 may_discard ? MOJO_READ_MESSAGE_FLAG_MAY_DISCARD : |
| 48 MOJO_READ_MESSAGE_FLAG_NONE); | 48 MOJO_READ_MESSAGE_FLAG_NONE); |
| 49 } | 49 } |
| 50 | 50 |
| 51 MojoHandle pipe0_, pipe1_; | 51 MojoHandle pipe0_, pipe1_; |
| 52 | 52 |
| 53 private: | 53 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(MessagePipeTest); | 54 DISALLOW_COPY_AND_ASSIGN(MessagePipeTest); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 using FuseMessagePipeTest = test::MojoTestBase; |
| 58 |
| 57 TEST_F(MessagePipeTest, WriteData) { | 59 TEST_F(MessagePipeTest, WriteData) { |
| 58 ASSERT_EQ(MOJO_RESULT_OK, | 60 ASSERT_EQ(MOJO_RESULT_OK, |
| 59 WriteMessage(pipe0_, kHelloWorld, sizeof(kHelloWorld))); | 61 WriteMessage(pipe0_, kHelloWorld, sizeof(kHelloWorld))); |
| 60 } | 62 } |
| 61 | 63 |
| 62 // Tests: | 64 // Tests: |
| 63 // - only default flags | 65 // - only default flags |
| 64 // - reading messages from a port | 66 // - reading messages from a port |
| 65 // - when there are no/one/two messages available for that port | 67 // - when there are no/one/two messages available for that port |
| 66 // - with buffer size 0 (and null buffer) -- should get size | 68 // - with buffer size 0 (and null buffer) -- should get size |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 ReadMessageWithHandles(h, buffers, kPingPongHandlesPerIteration); | 484 ReadMessageWithHandles(h, buffers, kPingPongHandlesPerIteration); |
| 483 } | 485 } |
| 484 WriteMessage(h, "quit", 4); | 486 WriteMessage(h, "quit", 4); |
| 485 END_CHILD() | 487 END_CHILD() |
| 486 for (size_t i = 0; i < kPingPongHandlesPerIteration; ++i) | 488 for (size_t i = 0; i < kPingPongHandlesPerIteration; ++i) |
| 487 MojoClose(buffers[i]); | 489 MojoClose(buffers[i]); |
| 488 } | 490 } |
| 489 | 491 |
| 490 #endif // !defined(OS_IOS) | 492 #endif // !defined(OS_IOS) |
| 491 | 493 |
| 494 TEST_F(FuseMessagePipeTest, Basic) { |
| 495 // Test that we can fuse pipes and they still work. |
| 496 |
| 497 MojoHandle a, b, c, d; |
| 498 CreateMessagePipe(&a, &b); |
| 499 CreateMessagePipe(&c, &d); |
| 500 |
| 501 EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(b, c)); |
| 502 |
| 503 // Handles b and c should be closed. |
| 504 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b)); |
| 505 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c)); |
| 506 |
| 507 const std::string kTestMessage1 = "Hello, world!"; |
| 508 const std::string kTestMessage2 = "Goodbye, world!"; |
| 509 |
| 510 WriteMessage(a, kTestMessage1); |
| 511 EXPECT_EQ(kTestMessage1, ReadMessage(d)); |
| 512 |
| 513 WriteMessage(d, kTestMessage2); |
| 514 EXPECT_EQ(kTestMessage2, ReadMessage(a)); |
| 515 |
| 516 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a)); |
| 517 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d)); |
| 518 } |
| 519 |
| 520 TEST_F(FuseMessagePipeTest, FuseAfterPeerWrite) { |
| 521 // Test that messages written before fusion are eventually delivered. |
| 522 |
| 523 MojoHandle a, b, c, d; |
| 524 CreateMessagePipe(&a, &b); |
| 525 CreateMessagePipe(&c, &d); |
| 526 |
| 527 const std::string kTestMessage1 = "Hello, world!"; |
| 528 const std::string kTestMessage2 = "Goodbye, world!"; |
| 529 WriteMessage(a, kTestMessage1); |
| 530 WriteMessage(d, kTestMessage2); |
| 531 |
| 532 EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(b, c)); |
| 533 |
| 534 // Handles b and c should be closed. |
| 535 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b)); |
| 536 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c)); |
| 537 |
| 538 EXPECT_EQ(kTestMessage1, ReadMessage(d)); |
| 539 EXPECT_EQ(kTestMessage2, ReadMessage(a)); |
| 540 |
| 541 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a)); |
| 542 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d)); |
| 543 } |
| 544 |
| 545 TEST_F(FuseMessagePipeTest, NoFuseAfterWrite) { |
| 546 // Test that a pipe endpoint which has been written to cannot be fused. |
| 547 |
| 548 MojoHandle a, b, c, d; |
| 549 CreateMessagePipe(&a, &b); |
| 550 CreateMessagePipe(&c, &d); |
| 551 |
| 552 WriteMessage(b, "shouldn't have done that!"); |
| 553 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, MojoFuseMessagePipes(b, c)); |
| 554 |
| 555 // Handles b and c should be closed. |
| 556 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b)); |
| 557 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c)); |
| 558 |
| 559 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a)); |
| 560 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d)); |
| 561 } |
| 562 |
| 563 TEST_F(FuseMessagePipeTest, NoFuseSelf) { |
| 564 // Test that a pipe's own endpoints can't be fused together. |
| 565 |
| 566 MojoHandle a, b; |
| 567 CreateMessagePipe(&a, &b); |
| 568 |
| 569 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, MojoFuseMessagePipes(a, b)); |
| 570 |
| 571 // Handles a and b should be closed. |
| 572 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(a)); |
| 573 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b)); |
| 574 } |
| 575 |
| 576 TEST_F(FuseMessagePipeTest, FuseInvalidArguments) { |
| 577 MojoHandle a, b, c, d; |
| 578 CreateMessagePipe(&a, &b); |
| 579 CreateMessagePipe(&c, &d); |
| 580 |
| 581 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b)); |
| 582 |
| 583 // Can't fuse an invalid handle. |
| 584 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoFuseMessagePipes(b, c)); |
| 585 |
| 586 // Handle c should be closed. |
| 587 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c)); |
| 588 |
| 589 // Can't fuse a non-message pipe handle. |
| 590 MojoHandle e, f; |
| 591 CreateDataPipe(&e, &f, 16); |
| 592 |
| 593 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoFuseMessagePipes(e, d)); |
| 594 |
| 595 // Handles d and e should be closed. |
| 596 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(d)); |
| 597 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(e)); |
| 598 |
| 599 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a)); |
| 600 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(f)); |
| 601 } |
| 602 |
| 603 TEST_F(FuseMessagePipeTest, FuseAfterPeerClosure) { |
| 604 // Test that peer closure prior to fusion can still be detected after fusion. |
| 605 |
| 606 MojoHandle a, b, c, d; |
| 607 CreateMessagePipe(&a, &b); |
| 608 CreateMessagePipe(&c, &d); |
| 609 |
| 610 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a)); |
| 611 EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(b, c)); |
| 612 |
| 613 // Handles b and c should be closed. |
| 614 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b)); |
| 615 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c)); |
| 616 |
| 617 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(d, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 618 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 619 |
| 620 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d)); |
| 621 } |
| 622 |
| 623 TEST_F(FuseMessagePipeTest, FuseAfterPeerWriteAndClosure) { |
| 624 // Test that peer write and closure prior to fusion still results in the |
| 625 // both message arrival and awareness of peer closure. |
| 626 |
| 627 MojoHandle a, b, c, d; |
| 628 CreateMessagePipe(&a, &b); |
| 629 CreateMessagePipe(&c, &d); |
| 630 |
| 631 const std::string kTestMessage = "ayyy lmao"; |
| 632 WriteMessage(a, kTestMessage); |
| 633 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a)); |
| 634 |
| 635 EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(b, c)); |
| 636 |
| 637 // Handles b and c should be closed. |
| 638 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b)); |
| 639 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c)); |
| 640 |
| 641 EXPECT_EQ(kTestMessage, ReadMessage(d)); |
| 642 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(d, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 643 MOJO_DEADLINE_INDEFINITE, nullptr)); |
| 644 |
| 645 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d)); |
| 646 } |
| 647 |
| 492 } // namespace | 648 } // namespace |
| 493 } // namespace edk | 649 } // namespace edk |
| 494 } // namespace mojo | 650 } // namespace mojo |
| OLD | NEW |