Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1638)

Side by Side Diff: ipc/attachment_broker_mac_unittest.cc

Issue 1418113003: mac: Add auto-close and share-read-only functionality to SharedMemory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from mark. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/shared_memory.h" 16 #include "base/memory/shared_memory.h"
16 #include "ipc/attachment_broker_privileged_mac.h" 17 #include "ipc/attachment_broker_privileged_mac.h"
17 #include "ipc/attachment_broker_unprivileged_mac.h" 18 #include "ipc/attachment_broker_unprivileged_mac.h"
18 #include "ipc/ipc_listener.h" 19 #include "ipc/ipc_listener.h"
19 #include "ipc/ipc_message.h" 20 #include "ipc/ipc_message.h"
20 #include "ipc/ipc_test_base.h" 21 #include "ipc/ipc_test_base.h"
21 #include "ipc/ipc_test_messages.h" 22 #include "ipc/ipc_test_messages.h"
22 #include "ipc/test_util_mac.h" 23 #include "ipc/test_util_mac.h"
23 24
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 return false; 105 return false;
105 } 106 }
106 107
107 *handle1 = base::get<0>(p); 108 *handle1 = base::get<0>(p);
108 *handle2 = base::get<1>(p); 109 *handle2 = base::get<1>(p);
109 return true; 110 return true;
110 } 111 }
111 112
112 // Returns |nullptr| on error. 113 // Returns |nullptr| on error.
113 scoped_ptr<base::SharedMemory> MapSharedMemoryHandle( 114 scoped_ptr<base::SharedMemory> MapSharedMemoryHandle(
114 const base::SharedMemoryHandle& shm) { 115 const base::SharedMemoryHandle& shm,
116 bool read_only) {
115 if (!shm.IsValid()) { 117 if (!shm.IsValid()) {
116 LOG(ERROR) << "Invalid SharedMemoryHandle"; 118 LOG(ERROR) << "Invalid SharedMemoryHandle";
117 return nullptr; 119 return nullptr;
118 } 120 }
119 121
120 size_t size; 122 size_t size;
121 if (!shm.GetSize(&size)) { 123 if (!shm.GetSize(&size)) {
122 LOG(ERROR) << "Couldn't get size of SharedMemoryHandle"; 124 LOG(ERROR) << "Couldn't get size of SharedMemoryHandle";
123 return nullptr; 125 return nullptr;
124 } 126 }
125 127
126 scoped_ptr<base::SharedMemory> shared_memory( 128 scoped_ptr<base::SharedMemory> shared_memory(
127 new base::SharedMemory(shm, false)); 129 new base::SharedMemory(shm, read_only));
128 shared_memory->Map(size); 130 shared_memory->Map(size);
129 return shared_memory; 131 return shared_memory;
130 } 132 }
131 133
132 // This method maps the SharedMemoryHandle, checks the contents, and then 134 // This method maps the SharedMemoryHandle, checks the contents, and then
133 // consumes a reference to the underlying Mach port. 135 // consumes a reference to the underlying Mach port.
134 bool CheckContentsOfSharedMemoryHandle(const base::SharedMemoryHandle& shm, 136 bool CheckContentsOfSharedMemoryHandle(const base::SharedMemoryHandle& shm,
135 const std::string& contents) { 137 const std::string& contents) {
136 scoped_ptr<base::SharedMemory> shared_memory(MapSharedMemoryHandle(shm)); 138 scoped_ptr<base::SharedMemory> shared_memory(
139 MapSharedMemoryHandle(shm, false));
137 140
138 if (memcmp(shared_memory->memory(), contents.c_str(), contents.size()) != 0) { 141 if (memcmp(shared_memory->memory(), contents.c_str(), contents.size()) != 0) {
139 LOG(ERROR) << "Shared Memory contents not equivalent"; 142 LOG(ERROR) << "Shared Memory contents not equivalent";
140 return false; 143 return false;
141 } 144 }
142 return true; 145 return true;
143 } 146 }
144 147
145 // This method mmaps the FileDescriptor, checks the contents, and then munmaps 148 // This method mmaps the FileDescriptor, checks the contents, and then munmaps
146 // the FileDescriptor and closes the underlying fd. 149 // the FileDescriptor and closes the underlying fd.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 return base::FileDescriptor(fd_closer.release(), true); 184 return base::FileDescriptor(fd_closer.release(), true);
182 } 185 }
183 186
184 // Maps both handles, then checks that their contents matches |contents|. Then 187 // Maps both handles, then checks that their contents matches |contents|. Then
185 // checks that changes to one are reflected in the other. Then consumes 188 // checks that changes to one are reflected in the other. Then consumes
186 // references to both underlying Mach ports. 189 // references to both underlying Mach ports.
187 bool CheckContentsOfTwoEquivalentSharedMemoryHandles( 190 bool CheckContentsOfTwoEquivalentSharedMemoryHandles(
188 const base::SharedMemoryHandle& handle1, 191 const base::SharedMemoryHandle& handle1,
189 const base::SharedMemoryHandle& handle2, 192 const base::SharedMemoryHandle& handle2,
190 const std::string& contents) { 193 const std::string& contents) {
191 scoped_ptr<base::SharedMemory> shared_memory1(MapSharedMemoryHandle(handle1)); 194 scoped_ptr<base::SharedMemory> shared_memory1(
192 scoped_ptr<base::SharedMemory> shared_memory2(MapSharedMemoryHandle(handle2)); 195 MapSharedMemoryHandle(handle1, false));
196 scoped_ptr<base::SharedMemory> shared_memory2(
197 MapSharedMemoryHandle(handle2, false));
193 198
194 if (memcmp(shared_memory1->memory(), contents.c_str(), contents.size()) != 199 if (memcmp(shared_memory1->memory(), contents.c_str(), contents.size()) !=
195 0) { 200 0) {
196 LOG(ERROR) << "Incorrect contents in shared_memory1"; 201 LOG(ERROR) << "Incorrect contents in shared_memory1";
197 return false; 202 return false;
198 } 203 }
199 204
200 if (memcmp(shared_memory1->memory(), shared_memory2->memory(), 205 if (memcmp(shared_memory1->memory(), shared_memory2->memory(),
201 contents.size()) != 0) { 206 contents.size()) != 0) {
202 LOG(ERROR) << "Incorrect contents in shared_memory2"; 207 LOG(ERROR) << "Incorrect contents in shared_memory2";
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 LOG(INFO) << "Memory leak!."; 536 LOG(INFO) << "Memory leak!.";
532 } 537 }
533 LOG(INFO) << "Privileged process end."; 538 LOG(INFO) << "Privileged process end.";
534 return 0; 539 return 0;
535 } 540 }
536 541
537 // An unprivileged process makes a shared memory region, and writes a string to 542 // An unprivileged process makes a shared memory region, and writes a string to
538 // it. The SharedMemoryHandle is sent to the privileged process using Chrome 543 // it. The SharedMemoryHandle is sent to the privileged process using Chrome
539 // IPC. The privileged process checks that it received the same memory region. 544 // IPC. The privileged process checks that it received the same memory region.
540 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandle) { 545 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandle) {
546 // Mach-based SharedMemory isn't support on OSX 10.6.
547 if (base::mac::IsOSSnowLeopard())
548 return;
549
541 CommonSetUp("SendSharedMemoryHandle"); 550 CommonSetUp("SendSharedMemoryHandle");
542 551
543 SendMessage1(kDataBuffer1); 552 SendMessage1(kDataBuffer1);
544 base::MessageLoop::current()->Run(); 553 base::MessageLoop::current()->Run();
545 CommonTearDown(); 554 CommonTearDown();
546 } 555 }
547 556
548 void SendSharedMemoryHandleCallback(IPC::Sender* sender, 557 void SendSharedMemoryHandleCallback(IPC::Sender* sender,
549 const IPC::Message& message) { 558 const IPC::Message& message) {
550 bool success = CheckContentsOfMessage1(message, kDataBuffer1); 559 bool success = CheckContentsOfMessage1(message, kDataBuffer1);
551 SendControlMessage(sender, success); 560 SendControlMessage(sender, success);
552 } 561 }
553 562
554 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandle) { 563 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandle) {
555 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleCallback, 564 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleCallback,
556 "SendSharedMemoryHandle"); 565 "SendSharedMemoryHandle");
557 } 566 }
558 567
559 // Similar to SendSharedMemoryHandle, but sends a very long shared memory 568 // Similar to SendSharedMemoryHandle, but sends a very long shared memory
560 // region. 569 // region.
561 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleLong) { 570 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleLong) {
571 // Mach-based SharedMemory isn't support on OSX 10.6.
572 if (base::mac::IsOSSnowLeopard())
573 return;
574
562 CommonSetUp("SendSharedMemoryHandleLong"); 575 CommonSetUp("SendSharedMemoryHandleLong");
563 576
564 std::string buffer(1 << 23, 'a'); 577 std::string buffer(1 << 23, 'a');
565 SendMessage1(buffer); 578 SendMessage1(buffer);
566 base::MessageLoop::current()->Run(); 579 base::MessageLoop::current()->Run();
567 CommonTearDown(); 580 CommonTearDown();
568 } 581 }
569 582
570 void SendSharedMemoryHandleLongCallback(IPC::Sender* sender, 583 void SendSharedMemoryHandleLongCallback(IPC::Sender* sender,
571 const IPC::Message& message) { 584 const IPC::Message& message) {
572 std::string buffer(1 << 23, 'a'); 585 std::string buffer(1 << 23, 'a');
573 bool success = CheckContentsOfMessage1(message, buffer); 586 bool success = CheckContentsOfMessage1(message, buffer);
574 SendControlMessage(sender, success); 587 SendControlMessage(sender, success);
575 } 588 }
576 589
577 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleLong) { 590 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleLong) {
578 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleLongCallback, 591 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleLongCallback,
579 "SendSharedMemoryHandleLong"); 592 "SendSharedMemoryHandleLong");
580 } 593 }
581 594
582 // Similar to SendSharedMemoryHandle, but sends two different shared memory 595 // Similar to SendSharedMemoryHandle, but sends two different shared memory
583 // regions in two messages. 596 // regions in two messages.
584 TEST_F(IPCAttachmentBrokerMacTest, SendTwoMessagesDifferentSharedMemoryHandle) { 597 TEST_F(IPCAttachmentBrokerMacTest, SendTwoMessagesDifferentSharedMemoryHandle) {
598 // Mach-based SharedMemory isn't support on OSX 10.6.
599 if (base::mac::IsOSSnowLeopard())
600 return;
601
585 CommonSetUp("SendTwoMessagesDifferentSharedMemoryHandle"); 602 CommonSetUp("SendTwoMessagesDifferentSharedMemoryHandle");
586 603
587 SendMessage1(kDataBuffer1); 604 SendMessage1(kDataBuffer1);
588 SendMessage1(kDataBuffer2); 605 SendMessage1(kDataBuffer2);
589 base::MessageLoop::current()->Run(); 606 base::MessageLoop::current()->Run();
590 CommonTearDown(); 607 CommonTearDown();
591 } 608 }
592 609
593 void SendTwoMessagesDifferentSharedMemoryHandleCallback( 610 void SendTwoMessagesDifferentSharedMemoryHandleCallback(
594 IPC::Sender* sender, 611 IPC::Sender* sender,
(...skipping 11 matching lines...) Expand all
606 623
607 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoMessagesDifferentSharedMemoryHandle) { 624 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoMessagesDifferentSharedMemoryHandle) {
608 return CommonPrivilegedProcessMain( 625 return CommonPrivilegedProcessMain(
609 &SendTwoMessagesDifferentSharedMemoryHandleCallback, 626 &SendTwoMessagesDifferentSharedMemoryHandleCallback,
610 "SendTwoMessagesDifferentSharedMemoryHandle"); 627 "SendTwoMessagesDifferentSharedMemoryHandle");
611 } 628 }
612 629
613 // Similar to SendSharedMemoryHandle, but sends the same shared memory region in 630 // Similar to SendSharedMemoryHandle, but sends the same shared memory region in
614 // two messages. 631 // two messages.
615 TEST_F(IPCAttachmentBrokerMacTest, SendTwoMessagesSameSharedMemoryHandle) { 632 TEST_F(IPCAttachmentBrokerMacTest, SendTwoMessagesSameSharedMemoryHandle) {
633 // Mach-based SharedMemory isn't support on OSX 10.6.
634 if (base::mac::IsOSSnowLeopard())
635 return;
636
616 CommonSetUp("SendTwoMessagesSameSharedMemoryHandle"); 637 CommonSetUp("SendTwoMessagesSameSharedMemoryHandle");
617 638
618 { 639 {
619 scoped_ptr<base::SharedMemory> shared_memory( 640 scoped_ptr<base::SharedMemory> shared_memory(
620 MakeSharedMemory(kDataBuffer1)); 641 MakeSharedMemory(kDataBuffer1));
621 642
622 for (int i = 0; i < 2; ++i) { 643 for (int i = 0; i < 2; ++i) {
623 IPC::Message* message = 644 IPC::Message* message =
624 new TestSharedMemoryHandleMsg1(100, shared_memory->handle(), 200); 645 new TestSharedMemoryHandleMsg1(100, shared_memory->handle(), 200);
625 sender()->Send(message); 646 sender()->Send(message);
(...skipping 25 matching lines...) Expand all
651 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoMessagesSameSharedMemoryHandle) { 672 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoMessagesSameSharedMemoryHandle) {
652 return CommonPrivilegedProcessMain( 673 return CommonPrivilegedProcessMain(
653 &SendTwoMessagesSameSharedMemoryHandleCallback, 674 &SendTwoMessagesSameSharedMemoryHandleCallback,
654 "SendTwoMessagesSameSharedMemoryHandle"); 675 "SendTwoMessagesSameSharedMemoryHandle");
655 } 676 }
656 677
657 // Similar to SendSharedMemoryHandle, but sends one message with two different 678 // Similar to SendSharedMemoryHandle, but sends one message with two different
658 // memory regions. 679 // memory regions.
659 TEST_F(IPCAttachmentBrokerMacTest, 680 TEST_F(IPCAttachmentBrokerMacTest,
660 SendOneMessageWithTwoDifferentSharedMemoryHandles) { 681 SendOneMessageWithTwoDifferentSharedMemoryHandles) {
682 // Mach-based SharedMemory isn't support on OSX 10.6.
683 if (base::mac::IsOSSnowLeopard())
684 return;
685
661 CommonSetUp("SendOneMessageWithTwoDifferentSharedMemoryHandles"); 686 CommonSetUp("SendOneMessageWithTwoDifferentSharedMemoryHandles");
662 687
663 { 688 {
664 scoped_ptr<base::SharedMemory> shared_memory1( 689 scoped_ptr<base::SharedMemory> shared_memory1(
665 MakeSharedMemory(kDataBuffer1)); 690 MakeSharedMemory(kDataBuffer1));
666 scoped_ptr<base::SharedMemory> shared_memory2( 691 scoped_ptr<base::SharedMemory> shared_memory2(
667 MakeSharedMemory(kDataBuffer2)); 692 MakeSharedMemory(kDataBuffer2));
668 IPC::Message* message = new TestSharedMemoryHandleMsg2( 693 IPC::Message* message = new TestSharedMemoryHandleMsg2(
669 shared_memory1->handle(), shared_memory2->handle()); 694 shared_memory1->handle(), shared_memory2->handle());
670 sender()->Send(message); 695 sender()->Send(message);
(...skipping 22 matching lines...) Expand all
693 SendOneMessageWithTwoDifferentSharedMemoryHandles) { 718 SendOneMessageWithTwoDifferentSharedMemoryHandles) {
694 return CommonPrivilegedProcessMain( 719 return CommonPrivilegedProcessMain(
695 &SendOneMessageWithTwoDifferentSharedMemoryHandlesCallback, 720 &SendOneMessageWithTwoDifferentSharedMemoryHandlesCallback,
696 "SendOneMessageWithTwoDifferentSharedMemoryHandles"); 721 "SendOneMessageWithTwoDifferentSharedMemoryHandles");
697 } 722 }
698 723
699 // Similar to SendSharedMemoryHandle, but sends one message that contains the 724 // Similar to SendSharedMemoryHandle, but sends one message that contains the
700 // same memory region twice. 725 // same memory region twice.
701 TEST_F(IPCAttachmentBrokerMacTest, 726 TEST_F(IPCAttachmentBrokerMacTest,
702 SendOneMessageWithTwoSameSharedMemoryHandles) { 727 SendOneMessageWithTwoSameSharedMemoryHandles) {
728 // Mach-based SharedMemory isn't support on OSX 10.6.
729 if (base::mac::IsOSSnowLeopard())
730 return;
731
703 CommonSetUp("SendOneMessageWithTwoSameSharedMemoryHandles"); 732 CommonSetUp("SendOneMessageWithTwoSameSharedMemoryHandles");
704 733
705 { 734 {
706 scoped_ptr<base::SharedMemory> shared_memory( 735 scoped_ptr<base::SharedMemory> shared_memory(
707 MakeSharedMemory(kDataBuffer1)); 736 MakeSharedMemory(kDataBuffer1));
708 IPC::Message* message = new TestSharedMemoryHandleMsg2( 737 IPC::Message* message = new TestSharedMemoryHandleMsg2(
709 shared_memory->handle(), shared_memory->handle()); 738 shared_memory->handle(), shared_memory->handle());
710 sender()->Send(message); 739 sender()->Send(message);
711 } 740 }
712 base::MessageLoop::current()->Run(); 741 base::MessageLoop::current()->Run();
(...skipping 18 matching lines...) Expand all
731 760
732 MULTIPROCESS_IPC_TEST_CLIENT_MAIN( 761 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(
733 SendOneMessageWithTwoSameSharedMemoryHandles) { 762 SendOneMessageWithTwoSameSharedMemoryHandles) {
734 return CommonPrivilegedProcessMain( 763 return CommonPrivilegedProcessMain(
735 &SendOneMessageWithTwoSameSharedMemoryHandlesCallback, 764 &SendOneMessageWithTwoSameSharedMemoryHandlesCallback,
736 "SendOneMessageWithTwoSameSharedMemoryHandles"); 765 "SendOneMessageWithTwoSameSharedMemoryHandles");
737 } 766 }
738 767
739 // Sends one message with two Posix FDs and two Mach ports. 768 // Sends one message with two Posix FDs and two Mach ports.
740 TEST_F(IPCAttachmentBrokerMacTest, SendPosixFDAndMachPort) { 769 TEST_F(IPCAttachmentBrokerMacTest, SendPosixFDAndMachPort) {
770 // Mach-based SharedMemory isn't support on OSX 10.6.
771 if (base::mac::IsOSSnowLeopard())
772 return;
773
741 base::ScopedTempDir temp_dir; 774 base::ScopedTempDir temp_dir;
742 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 775 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
743 base::FilePath fp1, fp2; 776 base::FilePath fp1, fp2;
744 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &fp1)); 777 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &fp1));
745 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &fp2)); 778 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &fp2));
746 779
747 CommonSetUp("SendPosixFDAndMachPort"); 780 CommonSetUp("SendPosixFDAndMachPort");
748 781
749 { 782 {
750 scoped_ptr<base::SharedMemory> shared_memory1( 783 scoped_ptr<base::SharedMemory> shared_memory1(
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
794 827
795 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendPosixFDAndMachPort) { 828 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendPosixFDAndMachPort) {
796 return CommonPrivilegedProcessMain(&SendPosixFDAndMachPortCallback, 829 return CommonPrivilegedProcessMain(&SendPosixFDAndMachPortCallback,
797 "SendPosixFDAndMachPort"); 830 "SendPosixFDAndMachPort");
798 } 831 }
799 832
800 // Similar to SendHandle, except the attachment's destination process is this 833 // Similar to SendHandle, except the attachment's destination process is this
801 // process. This is an unrealistic scenario, but simulates an unprivileged 834 // process. This is an unrealistic scenario, but simulates an unprivileged
802 // process sending an attachment to another unprivileged process. 835 // process sending an attachment to another unprivileged process.
803 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleToSelf) { 836 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleToSelf) {
837 // Mach-based SharedMemory isn't support on OSX 10.6.
838 if (base::mac::IsOSSnowLeopard())
839 return;
840
804 SetBroker(new MockBroker); 841 SetBroker(new MockBroker);
805 CommonSetUp("SendSharedMemoryHandleToSelf"); 842 CommonSetUp("SendSharedMemoryHandleToSelf");
806 843
807 // Technically, the channel is an endpoint, but we need the proxy listener to 844 // Technically, the channel is an endpoint, but we need the proxy listener to
808 // receive the messages so that it can quit the message loop. 845 // receive the messages so that it can quit the message loop.
809 channel()->SetAttachmentBrokerEndpoint(false); 846 channel()->SetAttachmentBrokerEndpoint(false);
810 get_proxy_listener()->set_listener(get_broker()); 847 get_proxy_listener()->set_listener(get_broker());
811 848
812 { 849 {
813 scoped_ptr<base::SharedMemory> shared_memory( 850 scoped_ptr<base::SharedMemory> shared_memory(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 } 882 }
846 883
847 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleToSelf) { 884 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleToSelf) {
848 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleToSelfCallback, 885 return CommonPrivilegedProcessMain(&SendSharedMemoryHandleToSelfCallback,
849 "SendSharedMemoryHandleToSelf"); 886 "SendSharedMemoryHandleToSelf");
850 } 887 }
851 888
852 // Similar to SendSharedMemoryHandle, but uses a ChannelProxy instead of a 889 // Similar to SendSharedMemoryHandle, but uses a ChannelProxy instead of a
853 // Channel. 890 // Channel.
854 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleChannelProxy) { 891 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleChannelProxy) {
892 // Mach-based SharedMemory isn't support on OSX 10.6.
893 if (base::mac::IsOSSnowLeopard())
894 return;
895
855 Init("SendSharedMemoryHandleChannelProxy"); 896 Init("SendSharedMemoryHandleChannelProxy");
856 MachPreForkSetUp(); 897 MachPreForkSetUp();
857 898
858 SetBroker(new IPC::AttachmentBrokerUnprivilegedMac); 899 SetBroker(new IPC::AttachmentBrokerUnprivilegedMac);
859 get_broker()->AddObserver(get_observer()); 900 get_broker()->AddObserver(get_observer());
860 901
861 scoped_ptr<base::Thread> thread( 902 scoped_ptr<base::Thread> thread(
862 new base::Thread("ChannelProxyTestServerThread")); 903 new base::Thread("ChannelProxyTestServerThread"));
863 base::Thread::Options options; 904 base::Thread::Options options;
864 options.message_loop_type = base::MessageLoop::TYPE_IO; 905 options.message_loop_type = base::MessageLoop::TYPE_IO;
(...skipping 28 matching lines...) Expand all
893 bool success = CheckContentsOfMessage1(message, kDataBuffer1); 934 bool success = CheckContentsOfMessage1(message, kDataBuffer1);
894 SendControlMessage(sender, success); 935 SendControlMessage(sender, success);
895 } 936 }
896 937
897 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleChannelProxy) { 938 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandleChannelProxy) {
898 return CommonPrivilegedProcessMain( 939 return CommonPrivilegedProcessMain(
899 &SendSharedMemoryHandleChannelProxyCallback, 940 &SendSharedMemoryHandleChannelProxyCallback,
900 "SendSharedMemoryHandleChannelProxy"); 941 "SendSharedMemoryHandleChannelProxy");
901 } 942 }
902 943
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 &current_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
903 } // namespace 1028 } // namespace
OLDNEW
« no previous file with comments | « base/memory/shared_memory_mac_unittest.cc ('k') | ipc/attachment_broker_privileged_mac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698