OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // This file tests the C API, but using the explicit MojoSystemImpl parameter. | 5 // This file tests the C API, but using the explicit MojoSystemImpl parameter. |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "mojo/public/platform/native/system_impl_private.h" | 9 #include "mojo/public/platform/native/system_impl_private.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 EXPECT_NE(h0, h1); | 56 EXPECT_NE(h0, h1); |
57 | 57 |
58 // Both handles should have the correct rights. | 58 // Both handles should have the correct rights. |
59 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE; | 59 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE; |
60 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, h0, &rights)); | 60 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, h0, &rights)); |
61 EXPECT_EQ(kDefaultMessagePipeHandleRights, rights); | 61 EXPECT_EQ(kDefaultMessagePipeHandleRights, rights); |
62 rights = MOJO_HANDLE_RIGHT_NONE; | 62 rights = MOJO_HANDLE_RIGHT_NONE; |
63 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, h1, &rights)); | 63 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, h1, &rights)); |
64 EXPECT_EQ(kDefaultMessagePipeHandleRights, rights); | 64 EXPECT_EQ(kDefaultMessagePipeHandleRights, rights); |
65 | 65 |
| 66 // Shouldn't be able to duplicate either handle (just test "with reduced |
| 67 // rights" on one, and without on the other). |
| 68 MojoHandle handle_denied = MOJO_HANDLE_INVALID; |
| 69 EXPECT_EQ(MOJO_RESULT_PERMISSION_DENIED, |
| 70 MojoSystemImplDuplicateHandleWithReducedRights( |
| 71 sys0, h0, MOJO_HANDLE_RIGHT_DUPLICATE, &handle_denied)); |
| 72 EXPECT_EQ(MOJO_HANDLE_INVALID, handle_denied); |
| 73 handle_denied = MOJO_HANDLE_INVALID; |
| 74 EXPECT_EQ(MOJO_RESULT_PERMISSION_DENIED, |
| 75 MojoSystemImplDuplicateHandle(sys0, h1, &handle_denied)); |
| 76 EXPECT_EQ(MOJO_HANDLE_INVALID, handle_denied); |
| 77 |
66 // Move the other end of the pipe to a different SystemImpl. | 78 // Move the other end of the pipe to a different SystemImpl. |
67 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplTransferHandle(sys0, h1, sys1, &h1)); | 79 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplTransferHandle(sys0, h1, sys1, &h1)); |
68 EXPECT_NE(h1, MOJO_HANDLE_INVALID); | 80 EXPECT_NE(h1, MOJO_HANDLE_INVALID); |
69 | 81 |
70 // Shouldn't be readable, we haven't written anything. | 82 // Shouldn't be readable, we haven't written anything. |
71 MojoHandleSignalsState state; | 83 MojoHandleSignalsState state; |
72 EXPECT_EQ( | 84 EXPECT_EQ( |
73 MOJO_RESULT_DEADLINE_EXCEEDED, | 85 MOJO_RESULT_DEADLINE_EXCEEDED, |
74 MojoSystemImplWait(sys0, h0, MOJO_HANDLE_SIGNAL_READABLE, 0, &state)); | 86 MojoSystemImplWait(sys0, h0, MOJO_HANDLE_SIGNAL_READABLE, 0, &state)); |
75 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, state.satisfied_signals); | 87 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, state.satisfied_signals); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 EXPECT_NE(hp, hc); | 187 EXPECT_NE(hp, hc); |
176 | 188 |
177 // Both handles should have the correct rights. | 189 // Both handles should have the correct rights. |
178 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE; | 190 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE; |
179 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, hp, &rights)); | 191 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, hp, &rights)); |
180 EXPECT_EQ(kDefaultDataPipeProducerHandleRights, rights); | 192 EXPECT_EQ(kDefaultDataPipeProducerHandleRights, rights); |
181 rights = MOJO_HANDLE_RIGHT_NONE; | 193 rights = MOJO_HANDLE_RIGHT_NONE; |
182 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, hc, &rights)); | 194 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, hc, &rights)); |
183 EXPECT_EQ(kDefaultDataPipeConsumerHandleRights, rights); | 195 EXPECT_EQ(kDefaultDataPipeConsumerHandleRights, rights); |
184 | 196 |
| 197 // Shouldn't be able to duplicate either handle (just test "with reduced |
| 198 // rights" on one, and without on the other). |
| 199 MojoHandle handle_denied = MOJO_HANDLE_INVALID; |
| 200 EXPECT_EQ(MOJO_RESULT_PERMISSION_DENIED, |
| 201 MojoSystemImplDuplicateHandleWithReducedRights( |
| 202 sys0, hp, MOJO_HANDLE_RIGHT_DUPLICATE, &handle_denied)); |
| 203 EXPECT_EQ(MOJO_HANDLE_INVALID, handle_denied); |
| 204 handle_denied = MOJO_HANDLE_INVALID; |
| 205 EXPECT_EQ(MOJO_RESULT_PERMISSION_DENIED, |
| 206 MojoSystemImplDuplicateHandle(sys0, hc, &handle_denied)); |
| 207 EXPECT_EQ(MOJO_HANDLE_INVALID, handle_denied); |
| 208 |
185 // Move the other end of the pipe to a different SystemImpl. | 209 // Move the other end of the pipe to a different SystemImpl. |
186 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplTransferHandle(sys0, hc, sys1, &hc)); | 210 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplTransferHandle(sys0, hc, sys1, &hc)); |
187 EXPECT_NE(hc, MOJO_HANDLE_INVALID); | 211 EXPECT_NE(hc, MOJO_HANDLE_INVALID); |
188 | 212 |
189 // The consumer |hc| shouldn't be readable. | 213 // The consumer |hc| shouldn't be readable. |
190 MojoHandleSignalsState state; | 214 MojoHandleSignalsState state; |
191 EXPECT_EQ( | 215 EXPECT_EQ( |
192 MOJO_RESULT_DEADLINE_EXCEEDED, | 216 MOJO_RESULT_DEADLINE_EXCEEDED, |
193 MojoSystemImplWait(sys1, hc, MOJO_HANDLE_SIGNAL_READABLE, 0, &state)); | 217 MojoSystemImplWait(sys1, hc, MOJO_HANDLE_SIGNAL_READABLE, 0, &state)); |
194 | 218 |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 // 2 SystemImpls are leaked... | 616 // 2 SystemImpls are leaked... |
593 } | 617 } |
594 | 618 |
595 TEST(SystemImplTest, BasicSharedBuffer) { | 619 TEST(SystemImplTest, BasicSharedBuffer) { |
596 const uint64_t kSize = 100u; | 620 const uint64_t kSize = 100u; |
597 | 621 |
598 MojoSystemImpl sys0 = MojoSystemImplCreateImpl(); | 622 MojoSystemImpl sys0 = MojoSystemImplCreateImpl(); |
599 MojoSystemImpl sys1 = MojoSystemImplCreateImpl(); | 623 MojoSystemImpl sys1 = MojoSystemImplCreateImpl(); |
600 EXPECT_NE(sys0, sys1); | 624 EXPECT_NE(sys0, sys1); |
601 | 625 |
602 MojoHandle h0, h1; | |
603 void* pointer; | |
604 | |
605 // Create a shared buffer (|h0|). | 626 // Create a shared buffer (|h0|). |
606 h0 = MOJO_HANDLE_INVALID; | 627 MojoHandle h0 = MOJO_HANDLE_INVALID; |
607 EXPECT_EQ(MOJO_RESULT_OK, | 628 EXPECT_EQ(MOJO_RESULT_OK, |
608 MojoSystemImplCreateSharedBuffer(sys0, nullptr, kSize, &h0)); | 629 MojoSystemImplCreateSharedBuffer(sys0, nullptr, kSize, &h0)); |
609 EXPECT_NE(h0, MOJO_HANDLE_INVALID); | 630 EXPECT_NE(h0, MOJO_HANDLE_INVALID); |
610 | 631 |
611 // The handle should have the correct rights. | 632 // The handle should have the correct rights. |
612 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE; | 633 MojoHandleRights rights = MOJO_HANDLE_RIGHT_NONE; |
613 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, h0, &rights)); | 634 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, h0, &rights)); |
614 EXPECT_EQ(kDefaultSharedBufferHandleRights, rights); | 635 EXPECT_EQ(kDefaultSharedBufferHandleRights, rights); |
615 | 636 |
616 // Check the buffer information. | 637 // Check the buffer information. |
617 { | 638 { |
618 MojoBufferInformation info = {}; | 639 MojoBufferInformation info = {}; |
619 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetBufferInformation( | 640 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetBufferInformation( |
620 sys0, h0, &info, sizeof(info))); | 641 sys0, h0, &info, sizeof(info))); |
621 EXPECT_EQ(sizeof(info), info.struct_size); | 642 EXPECT_EQ(sizeof(info), info.struct_size); |
622 EXPECT_EQ(MOJO_BUFFER_INFORMATION_FLAG_NONE, info.flags); | 643 EXPECT_EQ(MOJO_BUFFER_INFORMATION_FLAG_NONE, info.flags); |
623 EXPECT_EQ(kSize, info.num_bytes); | 644 EXPECT_EQ(kSize, info.num_bytes); |
624 } | 645 } |
625 | 646 |
626 // Map everything. | 647 // Map everything. |
627 pointer = nullptr; | 648 void* pointer = nullptr; |
628 EXPECT_EQ(MOJO_RESULT_OK, | 649 EXPECT_EQ(MOJO_RESULT_OK, |
629 MojoSystemImplMapBuffer(sys0, h0, 0u, kSize, &pointer, | 650 MojoSystemImplMapBuffer(sys0, h0, 0u, kSize, &pointer, |
630 MOJO_MAP_BUFFER_FLAG_NONE)); | 651 MOJO_MAP_BUFFER_FLAG_NONE)); |
631 ASSERT_TRUE(pointer); | 652 ASSERT_TRUE(pointer); |
632 static_cast<char*>(pointer)[kSize / 2] = 'x'; | 653 static_cast<char*>(pointer)[kSize / 2] = 'x'; |
633 | 654 |
634 // Duplicate |h0| to |h1|. | 655 // Duplicate |h0| to |h1|. |
635 h1 = MOJO_HANDLE_INVALID; | 656 MojoHandle h1 = MOJO_HANDLE_INVALID; |
636 EXPECT_EQ(MOJO_RESULT_OK, | 657 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplDuplicateHandle(sys0, h0, &h1)); |
637 MojoSystemImplDuplicateBufferHandle(sys0, h0, nullptr, &h1)); | |
638 EXPECT_NE(h1, MOJO_HANDLE_INVALID); | 658 EXPECT_NE(h1, MOJO_HANDLE_INVALID); |
| 659 EXPECT_NE(h1, h0); |
639 | 660 |
640 // The new handle should have the correct rights. | 661 // The new handle should have the correct rights. |
641 rights = MOJO_HANDLE_RIGHT_NONE; | 662 rights = MOJO_HANDLE_RIGHT_NONE; |
642 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, h1, &rights)); | 663 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys0, h1, &rights)); |
643 EXPECT_EQ(kDefaultSharedBufferHandleRights, rights); | 664 EXPECT_EQ(kDefaultSharedBufferHandleRights, rights); |
644 | 665 |
645 // Move the other end of the pipe to a different SystemImpl. | 666 // Move the other end of the pipe to a different SystemImpl. |
646 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplTransferHandle(sys0, h1, sys1, &h1)); | 667 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplTransferHandle(sys0, h1, sys1, &h1)); |
647 EXPECT_NE(h1, MOJO_HANDLE_INVALID); | 668 EXPECT_NE(h1, MOJO_HANDLE_INVALID); |
648 | 669 |
(...skipping 23 matching lines...) Expand all Loading... |
672 MOJO_MAP_BUFFER_FLAG_NONE)); | 693 MOJO_MAP_BUFFER_FLAG_NONE)); |
673 ASSERT_TRUE(pointer); | 694 ASSERT_TRUE(pointer); |
674 | 695 |
675 // It should have what we wrote. | 696 // It should have what we wrote. |
676 EXPECT_EQ('x', static_cast<char*>(pointer)[0]); | 697 EXPECT_EQ('x', static_cast<char*>(pointer)[0]); |
677 EXPECT_EQ('y', static_cast<char*>(pointer)[1]); | 698 EXPECT_EQ('y', static_cast<char*>(pointer)[1]); |
678 | 699 |
679 // Unmap it. | 700 // Unmap it. |
680 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplUnmapBuffer(sys1, pointer)); | 701 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplUnmapBuffer(sys1, pointer)); |
681 | 702 |
| 703 // Test duplication with reduced rights. |
| 704 MojoHandle h2 = MOJO_HANDLE_INVALID; |
| 705 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplDuplicateHandleWithReducedRights( |
| 706 sys1, h1, MOJO_HANDLE_RIGHT_DUPLICATE, &h2)); |
| 707 EXPECT_NE(h2, MOJO_HANDLE_INVALID); |
| 708 EXPECT_NE(h2, h1); |
| 709 // |h2| should have the correct rights. |
| 710 rights = MOJO_HANDLE_RIGHT_NONE; |
| 711 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetRights(sys1, h2, &rights)); |
| 712 EXPECT_EQ(kDefaultSharedBufferHandleRights & ~MOJO_HANDLE_RIGHT_DUPLICATE, |
| 713 rights); |
| 714 // Trying to duplicate |h2| should fail. |
| 715 MojoHandle h3 = MOJO_HANDLE_INVALID; |
| 716 EXPECT_EQ(MOJO_RESULT_PERMISSION_DENIED, |
| 717 MojoSystemImplDuplicateHandle(sys1, h2, &h3)); |
| 718 EXPECT_EQ(MOJO_HANDLE_INVALID, h3); |
| 719 |
682 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys1, h1)); | 720 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys1, h1)); |
| 721 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys1, h2)); |
683 | 722 |
684 // 2 SystemImpls are leaked... | 723 // 2 SystemImpls are leaked... |
685 } | 724 } |
686 | 725 |
687 } // namespace | 726 } // namespace |
688 } // namespace mojo | 727 } // namespace mojo |
OLD | NEW |