| 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 #include "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/mac/mac_util.h" | |
| 15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
| 17 #include "ipc/attachment_broker_privileged_mac.h" | 16 #include "ipc/attachment_broker_privileged_mac.h" |
| 18 #include "ipc/attachment_broker_unprivileged_mac.h" | 17 #include "ipc/attachment_broker_unprivileged_mac.h" |
| 19 #include "ipc/ipc_listener.h" | 18 #include "ipc/ipc_listener.h" |
| 20 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
| 21 #include "ipc/ipc_test_base.h" | 20 #include "ipc/ipc_test_base.h" |
| 22 #include "ipc/ipc_test_messages.h" | 21 #include "ipc/ipc_test_messages.h" |
| 23 #include "ipc/test_util_mac.h" | 22 #include "ipc/test_util_mac.h" |
| 24 | 23 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 return false; | 104 return false; |
| 106 } | 105 } |
| 107 | 106 |
| 108 *handle1 = base::get<0>(p); | 107 *handle1 = base::get<0>(p); |
| 109 *handle2 = base::get<1>(p); | 108 *handle2 = base::get<1>(p); |
| 110 return true; | 109 return true; |
| 111 } | 110 } |
| 112 | 111 |
| 113 // Returns |nullptr| on error. | 112 // Returns |nullptr| on error. |
| 114 scoped_ptr<base::SharedMemory> MapSharedMemoryHandle( | 113 scoped_ptr<base::SharedMemory> MapSharedMemoryHandle( |
| 115 const base::SharedMemoryHandle& shm, | 114 const base::SharedMemoryHandle& shm) { |
| 116 bool read_only) { | |
| 117 if (!shm.IsValid()) { | 115 if (!shm.IsValid()) { |
| 118 LOG(ERROR) << "Invalid SharedMemoryHandle"; | 116 LOG(ERROR) << "Invalid SharedMemoryHandle"; |
| 119 return nullptr; | 117 return nullptr; |
| 120 } | 118 } |
| 121 | 119 |
| 122 size_t size; | 120 size_t size; |
| 123 if (!shm.GetSize(&size)) { | 121 if (!shm.GetSize(&size)) { |
| 124 LOG(ERROR) << "Couldn't get size of SharedMemoryHandle"; | 122 LOG(ERROR) << "Couldn't get size of SharedMemoryHandle"; |
| 125 return nullptr; | 123 return nullptr; |
| 126 } | 124 } |
| 127 | 125 |
| 128 scoped_ptr<base::SharedMemory> shared_memory( | 126 scoped_ptr<base::SharedMemory> shared_memory( |
| 129 new base::SharedMemory(shm, read_only)); | 127 new base::SharedMemory(shm, false)); |
| 130 shared_memory->Map(size); | 128 shared_memory->Map(size); |
| 131 return shared_memory; | 129 return shared_memory; |
| 132 } | 130 } |
| 133 | 131 |
| 134 // This method maps the SharedMemoryHandle, checks the contents, and then | 132 // This method maps the SharedMemoryHandle, checks the contents, and then |
| 135 // consumes a reference to the underlying Mach port. | 133 // consumes a reference to the underlying Mach port. |
| 136 bool CheckContentsOfSharedMemoryHandle(const base::SharedMemoryHandle& shm, | 134 bool CheckContentsOfSharedMemoryHandle(const base::SharedMemoryHandle& shm, |
| 137 const std::string& contents) { | 135 const std::string& contents) { |
| 138 scoped_ptr<base::SharedMemory> shared_memory( | 136 scoped_ptr<base::SharedMemory> shared_memory(MapSharedMemoryHandle(shm)); |
| 139 MapSharedMemoryHandle(shm, false)); | |
| 140 | 137 |
| 141 if (memcmp(shared_memory->memory(), contents.c_str(), contents.size()) != 0) { | 138 if (memcmp(shared_memory->memory(), contents.c_str(), contents.size()) != 0) { |
| 142 LOG(ERROR) << "Shared Memory contents not equivalent"; | 139 LOG(ERROR) << "Shared Memory contents not equivalent"; |
| 143 return false; | 140 return false; |
| 144 } | 141 } |
| 145 return true; | 142 return true; |
| 146 } | 143 } |
| 147 | 144 |
| 148 // This method mmaps the FileDescriptor, checks the contents, and then munmaps | 145 // This method mmaps the FileDescriptor, checks the contents, and then munmaps |
| 149 // the FileDescriptor and closes the underlying fd. | 146 // the FileDescriptor and closes the underlying fd. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 return base::FileDescriptor(fd_closer.release(), true); | 181 return base::FileDescriptor(fd_closer.release(), true); |
| 185 } | 182 } |
| 186 | 183 |
| 187 // Maps both handles, then checks that their contents matches |contents|. Then | 184 // Maps both handles, then checks that their contents matches |contents|. Then |
| 188 // checks that changes to one are reflected in the other. Then consumes | 185 // checks that changes to one are reflected in the other. Then consumes |
| 189 // references to both underlying Mach ports. | 186 // references to both underlying Mach ports. |
| 190 bool CheckContentsOfTwoEquivalentSharedMemoryHandles( | 187 bool CheckContentsOfTwoEquivalentSharedMemoryHandles( |
| 191 const base::SharedMemoryHandle& handle1, | 188 const base::SharedMemoryHandle& handle1, |
| 192 const base::SharedMemoryHandle& handle2, | 189 const base::SharedMemoryHandle& handle2, |
| 193 const std::string& contents) { | 190 const std::string& contents) { |
| 194 scoped_ptr<base::SharedMemory> shared_memory1( | 191 scoped_ptr<base::SharedMemory> shared_memory1(MapSharedMemoryHandle(handle1)); |
| 195 MapSharedMemoryHandle(handle1, false)); | 192 scoped_ptr<base::SharedMemory> shared_memory2(MapSharedMemoryHandle(handle2)); |
| 196 scoped_ptr<base::SharedMemory> shared_memory2( | |
| 197 MapSharedMemoryHandle(handle2, false)); | |
| 198 | 193 |
| 199 if (memcmp(shared_memory1->memory(), contents.c_str(), contents.size()) != | 194 if (memcmp(shared_memory1->memory(), contents.c_str(), contents.size()) != |
| 200 0) { | 195 0) { |
| 201 LOG(ERROR) << "Incorrect contents in shared_memory1"; | 196 LOG(ERROR) << "Incorrect contents in shared_memory1"; |
| 202 return false; | 197 return false; |
| 203 } | 198 } |
| 204 | 199 |
| 205 if (memcmp(shared_memory1->memory(), shared_memory2->memory(), | 200 if (memcmp(shared_memory1->memory(), shared_memory2->memory(), |
| 206 contents.size()) != 0) { | 201 contents.size()) != 0) { |
| 207 LOG(ERROR) << "Incorrect contents in shared_memory2"; | 202 LOG(ERROR) << "Incorrect contents in shared_memory2"; |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 LOG(INFO) << "Memory leak!."; | 531 LOG(INFO) << "Memory leak!."; |
| 537 } | 532 } |
| 538 LOG(INFO) << "Privileged process end."; | 533 LOG(INFO) << "Privileged process end."; |
| 539 return 0; | 534 return 0; |
| 540 } | 535 } |
| 541 | 536 |
| 542 // An unprivileged process makes a shared memory region, and writes a string to | 537 // An unprivileged process makes a shared memory region, and writes a string to |
| 543 // it. The SharedMemoryHandle is sent to the privileged process using Chrome | 538 // it. The SharedMemoryHandle is sent to the privileged process using Chrome |
| 544 // IPC. The privileged process checks that it received the same memory region. | 539 // IPC. The privileged process checks that it received the same memory region. |
| 545 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandle) { | 540 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandle) { |
| 546 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 547 if (base::mac::IsOSSnowLeopard()) | |
| 548 return; | |
| 549 | |
| 550 CommonSetUp("SendSharedMemoryHandle"); | 541 CommonSetUp("SendSharedMemoryHandle"); |
| 551 | 542 |
| 552 SendMessage1(kDataBuffer1); | 543 SendMessage1(kDataBuffer1); |
| 553 base::MessageLoop::current()->Run(); | 544 base::MessageLoop::current()->Run(); |
| 554 CommonTearDown(); | 545 CommonTearDown(); |
| 555 } | 546 } |
| 556 | 547 |
| 557 void SendSharedMemoryHandleCallback(IPC::Sender* sender, | 548 void SendSharedMemoryHandleCallback(IPC::Sender* sender, |
| 558 const IPC::Message& message) { | 549 const IPC::Message& message) { |
| 559 bool success = CheckContentsOfMessage1(message, kDataBuffer1); | 550 bool success = CheckContentsOfMessage1(message, kDataBuffer1); |
| 560 SendControlMessage(sender, success); | 551 SendControlMessage(sender, success); |
| 561 } | 552 } |
| 562 | 553 |
| 563 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandle) { | 554 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandle) { |
| 564 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleCallback, | 555 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleCallback, |
| 565 "SendSharedMemoryHandle"); | 556 "SendSharedMemoryHandle"); |
| 566 } | 557 } |
| 567 | 558 |
| 568 // Similar to SendSharedMemoryHandle, but sends a very long shared memory | 559 // Similar to SendSharedMemoryHandle, but sends a very long shared memory |
| 569 // region. | 560 // region. |
| 570 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleLong) { | 561 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleLong) { |
| 571 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 572 if (base::mac::IsOSSnowLeopard()) | |
| 573 return; | |
| 574 | |
| 575 CommonSetUp("SendSharedMemoryHandleLong"); | 562 CommonSetUp("SendSharedMemoryHandleLong"); |
| 576 | 563 |
| 577 std::string buffer(1 << 23, 'a'); | 564 std::string buffer(1 << 23, 'a'); |
| 578 SendMessage1(buffer); | 565 SendMessage1(buffer); |
| 579 base::MessageLoop::current()->Run(); | 566 base::MessageLoop::current()->Run(); |
| 580 CommonTearDown(); | 567 CommonTearDown(); |
| 581 } | 568 } |
| 582 | 569 |
| 583 void SendSharedMemoryHandleLongCallback(IPC::Sender* sender, | 570 void SendSharedMemoryHandleLongCallback(IPC::Sender* sender, |
| 584 const IPC::Message& message) { | 571 const IPC::Message& message) { |
| 585 std::string buffer(1 << 23, 'a'); | 572 std::string buffer(1 << 23, 'a'); |
| 586 bool success = CheckContentsOfMessage1(message, buffer); | 573 bool success = CheckContentsOfMessage1(message, buffer); |
| 587 SendControlMessage(sender, success); | 574 SendControlMessage(sender, success); |
| 588 } | 575 } |
| 589 | 576 |
| 590 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleLong) { | 577 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleLong) { |
| 591 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleLongCallback, | 578 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleLongCallback, |
| 592 "SendSharedMemoryHandleLong"); | 579 "SendSharedMemoryHandleLong"); |
| 593 } | 580 } |
| 594 | 581 |
| 595 // Similar to SendSharedMemoryHandle, but sends two different shared memory | 582 // Similar to SendSharedMemoryHandle, but sends two different shared memory |
| 596 // regions in two messages. | 583 // regions in two messages. |
| 597 TEST_F(IPCAttachmentBrokerMacTest, SendTwoMessagesDifferentSharedMemoryHandle) { | 584 TEST_F(IPCAttachmentBrokerMacTest, SendTwoMessagesDifferentSharedMemoryHandle) { |
| 598 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 599 if (base::mac::IsOSSnowLeopard()) | |
| 600 return; | |
| 601 | |
| 602 CommonSetUp("SendTwoMessagesDifferentSharedMemoryHandle"); | 585 CommonSetUp("SendTwoMessagesDifferentSharedMemoryHandle"); |
| 603 | 586 |
| 604 SendMessage1(kDataBuffer1); | 587 SendMessage1(kDataBuffer1); |
| 605 SendMessage1(kDataBuffer2); | 588 SendMessage1(kDataBuffer2); |
| 606 base::MessageLoop::current()->Run(); | 589 base::MessageLoop::current()->Run(); |
| 607 CommonTearDown(); | 590 CommonTearDown(); |
| 608 } | 591 } |
| 609 | 592 |
| 610 void SendTwoMessagesDifferentSharedMemoryHandleCallback( | 593 void SendTwoMessagesDifferentSharedMemoryHandleCallback( |
| 611 IPC::Sender* sender, | 594 IPC::Sender* sender, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 623 | 606 |
| 624 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoMessagesDifferentSharedMemoryHandle) { | 607 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoMessagesDifferentSharedMemoryHandle) { |
| 625 return CommonPrivilegedProcessMain( | 608 return CommonPrivilegedProcessMain( |
| 626 &SendTwoMessagesDifferentSharedMemoryHandleCallback, | 609 &SendTwoMessagesDifferentSharedMemoryHandleCallback, |
| 627 "SendTwoMessagesDifferentSharedMemoryHandle"); | 610 "SendTwoMessagesDifferentSharedMemoryHandle"); |
| 628 } | 611 } |
| 629 | 612 |
| 630 // Similar to SendSharedMemoryHandle, but sends the same shared memory region in | 613 // Similar to SendSharedMemoryHandle, but sends the same shared memory region in |
| 631 // two messages. | 614 // two messages. |
| 632 TEST_F(IPCAttachmentBrokerMacTest, SendTwoMessagesSameSharedMemoryHandle) { | 615 TEST_F(IPCAttachmentBrokerMacTest, SendTwoMessagesSameSharedMemoryHandle) { |
| 633 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 634 if (base::mac::IsOSSnowLeopard()) | |
| 635 return; | |
| 636 | |
| 637 CommonSetUp("SendTwoMessagesSameSharedMemoryHandle"); | 616 CommonSetUp("SendTwoMessagesSameSharedMemoryHandle"); |
| 638 | 617 |
| 639 { | 618 { |
| 640 scoped_ptr<base::SharedMemory> shared_memory( | 619 scoped_ptr<base::SharedMemory> shared_memory( |
| 641 MakeSharedMemory(kDataBuffer1)); | 620 MakeSharedMemory(kDataBuffer1)); |
| 642 | 621 |
| 643 for (int i = 0; i < 2; ++i) { | 622 for (int i = 0; i < 2; ++i) { |
| 644 IPC::Message* message = | 623 IPC::Message* message = |
| 645 new TestSharedMemoryHandleMsg1(100, shared_memory->handle(), 200); | 624 new TestSharedMemoryHandleMsg1(100, shared_memory->handle(), 200); |
| 646 sender()->Send(message); | 625 sender()->Send(message); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 672 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoMessagesSameSharedMemoryHandle) { | 651 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoMessagesSameSharedMemoryHandle) { |
| 673 return CommonPrivilegedProcessMain( | 652 return CommonPrivilegedProcessMain( |
| 674 &SendTwoMessagesSameSharedMemoryHandleCallback, | 653 &SendTwoMessagesSameSharedMemoryHandleCallback, |
| 675 "SendTwoMessagesSameSharedMemoryHandle"); | 654 "SendTwoMessagesSameSharedMemoryHandle"); |
| 676 } | 655 } |
| 677 | 656 |
| 678 // Similar to SendSharedMemoryHandle, but sends one message with two different | 657 // Similar to SendSharedMemoryHandle, but sends one message with two different |
| 679 // memory regions. | 658 // memory regions. |
| 680 TEST_F(IPCAttachmentBrokerMacTest, | 659 TEST_F(IPCAttachmentBrokerMacTest, |
| 681 SendOneMessageWithTwoDifferentSharedMemoryHandles) { | 660 SendOneMessageWithTwoDifferentSharedMemoryHandles) { |
| 682 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 683 if (base::mac::IsOSSnowLeopard()) | |
| 684 return; | |
| 685 | |
| 686 CommonSetUp("SendOneMessageWithTwoDifferentSharedMemoryHandles"); | 661 CommonSetUp("SendOneMessageWithTwoDifferentSharedMemoryHandles"); |
| 687 | 662 |
| 688 { | 663 { |
| 689 scoped_ptr<base::SharedMemory> shared_memory1( | 664 scoped_ptr<base::SharedMemory> shared_memory1( |
| 690 MakeSharedMemory(kDataBuffer1)); | 665 MakeSharedMemory(kDataBuffer1)); |
| 691 scoped_ptr<base::SharedMemory> shared_memory2( | 666 scoped_ptr<base::SharedMemory> shared_memory2( |
| 692 MakeSharedMemory(kDataBuffer2)); | 667 MakeSharedMemory(kDataBuffer2)); |
| 693 IPC::Message* message = new TestSharedMemoryHandleMsg2( | 668 IPC::Message* message = new TestSharedMemoryHandleMsg2( |
| 694 shared_memory1->handle(), shared_memory2->handle()); | 669 shared_memory1->handle(), shared_memory2->handle()); |
| 695 sender()->Send(message); | 670 sender()->Send(message); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 718 SendOneMessageWithTwoDifferentSharedMemoryHandles) { | 693 SendOneMessageWithTwoDifferentSharedMemoryHandles) { |
| 719 return CommonPrivilegedProcessMain( | 694 return CommonPrivilegedProcessMain( |
| 720 &SendOneMessageWithTwoDifferentSharedMemoryHandlesCallback, | 695 &SendOneMessageWithTwoDifferentSharedMemoryHandlesCallback, |
| 721 "SendOneMessageWithTwoDifferentSharedMemoryHandles"); | 696 "SendOneMessageWithTwoDifferentSharedMemoryHandles"); |
| 722 } | 697 } |
| 723 | 698 |
| 724 // Similar to SendSharedMemoryHandle, but sends one message that contains the | 699 // Similar to SendSharedMemoryHandle, but sends one message that contains the |
| 725 // same memory region twice. | 700 // same memory region twice. |
| 726 TEST_F(IPCAttachmentBrokerMacTest, | 701 TEST_F(IPCAttachmentBrokerMacTest, |
| 727 SendOneMessageWithTwoSameSharedMemoryHandles) { | 702 SendOneMessageWithTwoSameSharedMemoryHandles) { |
| 728 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 729 if (base::mac::IsOSSnowLeopard()) | |
| 730 return; | |
| 731 | |
| 732 CommonSetUp("SendOneMessageWithTwoSameSharedMemoryHandles"); | 703 CommonSetUp("SendOneMessageWithTwoSameSharedMemoryHandles"); |
| 733 | 704 |
| 734 { | 705 { |
| 735 scoped_ptr<base::SharedMemory> shared_memory( | 706 scoped_ptr<base::SharedMemory> shared_memory( |
| 736 MakeSharedMemory(kDataBuffer1)); | 707 MakeSharedMemory(kDataBuffer1)); |
| 737 IPC::Message* message = new TestSharedMemoryHandleMsg2( | 708 IPC::Message* message = new TestSharedMemoryHandleMsg2( |
| 738 shared_memory->handle(), shared_memory->handle()); | 709 shared_memory->handle(), shared_memory->handle()); |
| 739 sender()->Send(message); | 710 sender()->Send(message); |
| 740 } | 711 } |
| 741 base::MessageLoop::current()->Run(); | 712 base::MessageLoop::current()->Run(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 760 | 731 |
| 761 MULTIPROCESS_IPC_TEST_CLIENT_MAIN( | 732 MULTIPROCESS_IPC_TEST_CLIENT_MAIN( |
| 762 SendOneMessageWithTwoSameSharedMemoryHandles) { | 733 SendOneMessageWithTwoSameSharedMemoryHandles) { |
| 763 return CommonPrivilegedProcessMain( | 734 return CommonPrivilegedProcessMain( |
| 764 &SendOneMessageWithTwoSameSharedMemoryHandlesCallback, | 735 &SendOneMessageWithTwoSameSharedMemoryHandlesCallback, |
| 765 "SendOneMessageWithTwoSameSharedMemoryHandles"); | 736 "SendOneMessageWithTwoSameSharedMemoryHandles"); |
| 766 } | 737 } |
| 767 | 738 |
| 768 // Sends one message with two Posix FDs and two Mach ports. | 739 // Sends one message with two Posix FDs and two Mach ports. |
| 769 TEST_F(IPCAttachmentBrokerMacTest, SendPosixFDAndMachPort) { | 740 TEST_F(IPCAttachmentBrokerMacTest, SendPosixFDAndMachPort) { |
| 770 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 771 if (base::mac::IsOSSnowLeopard()) | |
| 772 return; | |
| 773 | |
| 774 base::ScopedTempDir temp_dir; | 741 base::ScopedTempDir temp_dir; |
| 775 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 742 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 776 base::FilePath fp1, fp2; | 743 base::FilePath fp1, fp2; |
| 777 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &fp1)); | 744 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &fp1)); |
| 778 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &fp2)); | 745 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &fp2)); |
| 779 | 746 |
| 780 CommonSetUp("SendPosixFDAndMachPort"); | 747 CommonSetUp("SendPosixFDAndMachPort"); |
| 781 | 748 |
| 782 { | 749 { |
| 783 scoped_ptr<base::SharedMemory> shared_memory1( | 750 scoped_ptr<base::SharedMemory> shared_memory1( |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 827 | 794 |
| 828 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendPosixFDAndMachPort) { | 795 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendPosixFDAndMachPort) { |
| 829 return CommonPrivilegedProcessMain(&SendPosixFDAndMachPortCallback, | 796 return CommonPrivilegedProcessMain(&SendPosixFDAndMachPortCallback, |
| 830 "SendPosixFDAndMachPort"); | 797 "SendPosixFDAndMachPort"); |
| 831 } | 798 } |
| 832 | 799 |
| 833 // Similar to SendHandle, except the attachment's destination process is this | 800 // Similar to SendHandle, except the attachment's destination process is this |
| 834 // process. This is an unrealistic scenario, but simulates an unprivileged | 801 // process. This is an unrealistic scenario, but simulates an unprivileged |
| 835 // process sending an attachment to another unprivileged process. | 802 // process sending an attachment to another unprivileged process. |
| 836 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleToSelf) { | 803 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleToSelf) { |
| 837 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 838 if (base::mac::IsOSSnowLeopard()) | |
| 839 return; | |
| 840 | |
| 841 SetBroker(new MockBroker); | 804 SetBroker(new MockBroker); |
| 842 CommonSetUp("SendSharedMemoryHandleToSelf"); | 805 CommonSetUp("SendSharedMemoryHandleToSelf"); |
| 843 | 806 |
| 844 // Technically, the channel is an endpoint, but we need the proxy listener to | 807 // Technically, the channel is an endpoint, but we need the proxy listener to |
| 845 // receive the messages so that it can quit the message loop. | 808 // receive the messages so that it can quit the message loop. |
| 846 channel()->SetAttachmentBrokerEndpoint(false); | 809 channel()->SetAttachmentBrokerEndpoint(false); |
| 847 get_proxy_listener()->set_listener(get_broker()); | 810 get_proxy_listener()->set_listener(get_broker()); |
| 848 | 811 |
| 849 { | 812 { |
| 850 scoped_ptr<base::SharedMemory> shared_memory( | 813 scoped_ptr<base::SharedMemory> shared_memory( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 882 } | 845 } |
| 883 | 846 |
| 884 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleToSelf) { | 847 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleToSelf) { |
| 885 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleToSelfCallback, | 848 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleToSelfCallback, |
| 886 "SendSharedMemoryHandleToSelf"); | 849 "SendSharedMemoryHandleToSelf"); |
| 887 } | 850 } |
| 888 | 851 |
| 889 // Similar to SendSharedMemoryHandle, but uses a ChannelProxy instead of a | 852 // Similar to SendSharedMemoryHandle, but uses a ChannelProxy instead of a |
| 890 // Channel. | 853 // Channel. |
| 891 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleChannelProxy) { | 854 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleChannelProxy) { |
| 892 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 893 if (base::mac::IsOSSnowLeopard()) | |
| 894 return; | |
| 895 | |
| 896 Init("SendSharedMemoryHandleChannelProxy"); | 855 Init("SendSharedMemoryHandleChannelProxy"); |
| 897 MachPreForkSetUp(); | 856 MachPreForkSetUp(); |
| 898 | 857 |
| 899 SetBroker(new IPC::AttachmentBrokerUnprivilegedMac); | 858 SetBroker(new IPC::AttachmentBrokerUnprivilegedMac); |
| 900 get_broker()->AddObserver(get_observer()); | 859 get_broker()->AddObserver(get_observer()); |
| 901 | 860 |
| 902 scoped_ptr<base::Thread> thread( | 861 scoped_ptr<base::Thread> thread( |
| 903 new base::Thread("ChannelProxyTestServerThread")); | 862 new base::Thread("ChannelProxyTestServerThread")); |
| 904 base::Thread::Options options; | 863 base::Thread::Options options; |
| 905 options.message_loop_type = base::MessageLoop::TYPE_IO; | 864 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 934 bool success = CheckContentsOfMessage1(message, kDataBuffer1); | 893 bool success = CheckContentsOfMessage1(message, kDataBuffer1); |
| 935 SendControlMessage(sender, success); | 894 SendControlMessage(sender, success); |
| 936 } | 895 } |
| 937 | 896 |
| 938 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleChannelProxy) { | 897 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleChannelProxy) { |
| 939 return CommonPrivilegedProcessMain( | 898 return CommonPrivilegedProcessMain( |
| 940 &SendSharedMemoryHandleChannelProxyCallback, | 899 &SendSharedMemoryHandleChannelProxyCallback, |
| 941 "SendSharedMemoryHandleChannelProxy"); | 900 "SendSharedMemoryHandleChannelProxy"); |
| 942 } | 901 } |
| 943 | 902 |
| 944 // Similar to SendSharedMemoryHandle, but first makes a copy of the handle using | |
| 945 // ShareToProcess(). | |
| 946 TEST_F(IPCAttachmentBrokerMacTest, ShareToProcess) { | |
| 947 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 948 if (base::mac::IsOSSnowLeopard()) | |
| 949 return; | |
| 950 | |
| 951 CommonSetUp("ShareToProcess"); | |
| 952 | |
| 953 { | |
| 954 scoped_ptr<base::SharedMemory> shared_memory( | |
| 955 MakeSharedMemory(kDataBuffer1)); | |
| 956 base::SharedMemoryHandle new_handle; | |
| 957 ASSERT_TRUE(shared_memory->ShareToProcess(0, &new_handle)); | |
| 958 IPC::Message* message = | |
| 959 new TestSharedMemoryHandleMsg1(100, new_handle, 200); | |
| 960 sender()->Send(message); | |
| 961 } | |
| 962 | |
| 963 base::MessageLoop::current()->Run(); | |
| 964 CommonTearDown(); | |
| 965 } | |
| 966 | |
| 967 void ShareToProcessCallback(IPC::Sender* sender, const IPC::Message& message) { | |
| 968 bool success = CheckContentsOfMessage1(message, kDataBuffer1); | |
| 969 SendControlMessage(sender, success); | |
| 970 } | |
| 971 | |
| 972 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ShareToProcess) { | |
| 973 return CommonPrivilegedProcessMain(&ShareToProcessCallback, "ShareToProcess"); | |
| 974 } | |
| 975 | |
| 976 // Similar to ShareToProcess, but instead shares the memory object only with | |
| 977 // read permissions. | |
| 978 TEST_F(IPCAttachmentBrokerMacTest, ShareReadOnlyToProcess) { | |
| 979 // Mach-based SharedMemory isn't support on OSX 10.6. | |
| 980 if (base::mac::IsOSSnowLeopard()) | |
| 981 return; | |
| 982 | |
| 983 CommonSetUp("ShareReadOnlyToProcess"); | |
| 984 | |
| 985 { | |
| 986 scoped_ptr<base::SharedMemory> shared_memory( | |
| 987 MakeSharedMemory(kDataBuffer1)); | |
| 988 base::SharedMemoryHandle new_handle; | |
| 989 ASSERT_TRUE(shared_memory->ShareReadOnlyToProcess(0, &new_handle)); | |
| 990 IPC::Message* message = | |
| 991 new TestSharedMemoryHandleMsg1(100, new_handle, 200); | |
| 992 sender()->Send(message); | |
| 993 } | |
| 994 | |
| 995 base::MessageLoop::current()->Run(); | |
| 996 CommonTearDown(); | |
| 997 } | |
| 998 | |
| 999 void ShareReadOnlyToProcessCallback(IPC::Sender* sender, | |
| 1000 const IPC::Message& message) { | |
| 1001 base::SharedMemoryHandle shm(GetSharedMemoryHandleFromMsg1(message)); | |
| 1002 | |
| 1003 // Try to map the memory as writable. | |
| 1004 scoped_ptr<base::SharedMemory> shared_memory( | |
| 1005 MapSharedMemoryHandle(shm, false)); | |
| 1006 ASSERT_EQ(nullptr, shared_memory->memory()); | |
| 1007 | |
| 1008 // Now try as read-only. | |
| 1009 scoped_ptr<base::SharedMemory> shared_memory2( | |
| 1010 MapSharedMemoryHandle(shm.Duplicate(), true)); | |
| 1011 int current_prot, max_prot; | |
| 1012 ASSERT_TRUE(IPC::GetMachProtections(shared_memory2->memory(), | |
| 1013 shared_memory2->mapped_size(), | |
| 1014 ¤t_prot, &max_prot)); | |
| 1015 ASSERT_EQ(VM_PROT_READ, current_prot); | |
| 1016 ASSERT_EQ(VM_PROT_READ, max_prot); | |
| 1017 | |
| 1018 bool success = | |
| 1019 memcmp(shared_memory2->memory(), kDataBuffer1, strlen(kDataBuffer1)) == 0; | |
| 1020 SendControlMessage(sender, success); | |
| 1021 } | |
| 1022 | |
| 1023 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ShareReadOnlyToProcess) { | |
| 1024 return CommonPrivilegedProcessMain(&ShareReadOnlyToProcessCallback, | |
| 1025 "ShareReadOnlyToProcess"); | |
| 1026 } | |
| 1027 | |
| 1028 } // namespace | 903 } // namespace |
| OLD | NEW |