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 <mach/mach_vm.h> | 8 #include <mach/mach_vm.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 | 415 |
416 // Mach Setup that needs to occur after child processes are forked. | 416 // Mach Setup that needs to occur after child processes are forked. |
417 void MachPostForkSetUp() { | 417 void MachPostForkSetUp() { |
418 client_port_.reset(IPC::ReceiveMachPort(server_port_.get()).release()); | 418 client_port_.reset(IPC::ReceiveMachPort(server_port_.get()).release()); |
419 IPC::SendMachPort( | 419 IPC::SendMachPort( |
420 client_port_.get(), mach_task_self(), MACH_MSG_TYPE_COPY_SEND); | 420 client_port_.get(), mach_task_self(), MACH_MSG_TYPE_COPY_SEND); |
421 } | 421 } |
422 | 422 |
423 // Setup shared between tests. | 423 // Setup shared between tests. |
424 void CommonSetUp(const char* name) { | 424 void CommonSetUp(const char* name) { |
| 425 PreConnectSetUp(name); |
| 426 PostConnectSetUp(); |
| 427 } |
| 428 |
| 429 // All of setup before the channel is connected. |
| 430 void PreConnectSetUp(const char* name) { |
425 Init(name); | 431 Init(name); |
426 MachPreForkSetUp(); | 432 MachPreForkSetUp(); |
427 | 433 |
428 if (!broker_.get()) | 434 if (!broker_.get()) |
429 SetBroker(new IPC::AttachmentBrokerUnprivilegedMac); | 435 SetBroker(new IPC::AttachmentBrokerUnprivilegedMac); |
430 | 436 |
431 broker_->AddObserver(&observer_, task_runner()); | 437 broker_->AddObserver(&observer_, task_runner()); |
432 CreateChannel(&proxy_listener_); | 438 CreateChannel(&proxy_listener_); |
433 broker_->RegisterBrokerCommunicationChannel(channel()); | 439 broker_->RegisterBrokerCommunicationChannel(channel()); |
| 440 } |
| 441 |
| 442 // All of setup including the connection and everything after. |
| 443 void PostConnectSetUp() { |
434 ASSERT_TRUE(ConnectChannel()); | 444 ASSERT_TRUE(ConnectChannel()); |
435 ASSERT_TRUE(StartClient()); | 445 ASSERT_TRUE(StartClient()); |
436 | 446 |
437 MachPostForkSetUp(); | 447 MachPostForkSetUp(); |
438 active_names_at_start_ = IPC::GetActiveNameCount(); | 448 active_names_at_start_ = IPC::GetActiveNameCount(); |
439 get_proxy_listener()->set_listener(&result_listener_); | 449 get_proxy_listener()->set_listener(&result_listener_); |
440 } | 450 } |
441 | 451 |
442 void CheckChildResult() { | 452 void CheckChildResult() { |
443 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 453 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
862 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendPosixFDAndMachPort) { | 872 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendPosixFDAndMachPort) { |
863 return CommonPrivilegedProcessMain(&SendPosixFDAndMachPortCallback, | 873 return CommonPrivilegedProcessMain(&SendPosixFDAndMachPortCallback, |
864 "SendPosixFDAndMachPort"); | 874 "SendPosixFDAndMachPort"); |
865 } | 875 } |
866 | 876 |
867 // Similar to SendHandle, except the attachment's destination process is this | 877 // Similar to SendHandle, except the attachment's destination process is this |
868 // process. This is an unrealistic scenario, but simulates an unprivileged | 878 // process. This is an unrealistic scenario, but simulates an unprivileged |
869 // process sending an attachment to another unprivileged process. | 879 // process sending an attachment to another unprivileged process. |
870 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleToSelf) { | 880 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleToSelf) { |
871 SetBroker(new MockBroker); | 881 SetBroker(new MockBroker); |
872 CommonSetUp("SendSharedMemoryHandleToSelf"); | 882 PreConnectSetUp("SendSharedMemoryHandleToSelf"); |
873 | |
874 // Technically, the channel is an endpoint, but we need the proxy listener to | 883 // Technically, the channel is an endpoint, but we need the proxy listener to |
875 // receive the messages so that it can quit the message loop. | 884 // receive the messages so that it can quit the message loop. |
876 channel()->SetAttachmentBrokerEndpoint(false); | 885 channel()->SetAttachmentBrokerEndpoint(false); |
| 886 PostConnectSetUp(); |
877 get_proxy_listener()->set_listener(get_broker()); | 887 get_proxy_listener()->set_listener(get_broker()); |
878 | 888 |
879 { | 889 { |
880 scoped_ptr<base::SharedMemory> shared_memory( | 890 scoped_ptr<base::SharedMemory> shared_memory( |
881 MakeSharedMemory(kDataBuffer1)); | 891 MakeSharedMemory(kDataBuffer1)); |
882 mach_port_urefs_t ref_count = IPC::GetMachRefCount( | 892 mach_port_urefs_t ref_count = IPC::GetMachRefCount( |
883 shared_memory->handle().GetMemoryObject(), MACH_PORT_RIGHT_SEND); | 893 shared_memory->handle().GetMemoryObject(), MACH_PORT_RIGHT_SEND); |
884 | 894 |
885 IPC::Message* message = | 895 IPC::Message* message = |
886 new TestSharedMemoryHandleMsg1(100, shared_memory->handle(), 200); | 896 new TestSharedMemoryHandleMsg1(100, shared_memory->handle(), 200); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
931 | 941 |
932 SetBroker(new IPC::AttachmentBrokerUnprivilegedMac); | 942 SetBroker(new IPC::AttachmentBrokerUnprivilegedMac); |
933 get_broker()->AddObserver(get_observer(), task_runner()); | 943 get_broker()->AddObserver(get_observer(), task_runner()); |
934 | 944 |
935 scoped_ptr<base::Thread> thread( | 945 scoped_ptr<base::Thread> thread( |
936 new base::Thread("ChannelProxyTestServerThread")); | 946 new base::Thread("ChannelProxyTestServerThread")); |
937 base::Thread::Options options; | 947 base::Thread::Options options; |
938 options.message_loop_type = base::MessageLoop::TYPE_IO; | 948 options.message_loop_type = base::MessageLoop::TYPE_IO; |
939 thread->StartWithOptions(options); | 949 thread->StartWithOptions(options); |
940 | 950 |
941 CreateChannelProxy(get_proxy_listener(), thread->task_runner().get()); | 951 set_channel_proxy(scoped_ptr<IPC::ChannelProxy>(new IPC::ChannelProxy( |
| 952 get_proxy_listener(), thread->task_runner().get()))); |
942 get_broker()->RegisterBrokerCommunicationChannel(channel_proxy()); | 953 get_broker()->RegisterBrokerCommunicationChannel(channel_proxy()); |
| 954 channel_proxy()->Init( |
| 955 CreateChannelFactory(GetTestChannelHandle(), thread->task_runner().get()), |
| 956 true); |
943 | 957 |
944 ASSERT_TRUE(StartClient()); | 958 ASSERT_TRUE(StartClient()); |
945 | 959 |
946 MachPostForkSetUp(); | 960 MachPostForkSetUp(); |
947 active_names_at_start_ = IPC::GetActiveNameCount(); | 961 active_names_at_start_ = IPC::GetActiveNameCount(); |
948 get_proxy_listener()->set_listener(get_result_listener()); | 962 get_proxy_listener()->set_listener(get_result_listener()); |
949 | 963 |
950 SendMessage1(kDataBuffer1); | 964 SendMessage1(kDataBuffer1); |
951 base::MessageLoop::current()->Run(); | 965 base::MessageLoop::current()->Run(); |
952 | 966 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1051 | 1065 |
1052 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ShareReadOnlyToProcess) { | 1066 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ShareReadOnlyToProcess) { |
1053 return CommonPrivilegedProcessMain(&ShareReadOnlyToProcessCallback, | 1067 return CommonPrivilegedProcessMain(&ShareReadOnlyToProcessCallback, |
1054 "ShareReadOnlyToProcess"); | 1068 "ShareReadOnlyToProcess"); |
1055 } | 1069 } |
1056 | 1070 |
1057 // Similar to SendSharedMemoryHandleToSelf, but the child process pretends to | 1071 // Similar to SendSharedMemoryHandleToSelf, but the child process pretends to |
1058 // not have the task port for the parent process. | 1072 // not have the task port for the parent process. |
1059 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleToSelfDelayedPort) { | 1073 TEST_F(IPCAttachmentBrokerMacTest, SendSharedMemoryHandleToSelfDelayedPort) { |
1060 SetBroker(new MockBroker); | 1074 SetBroker(new MockBroker); |
1061 CommonSetUp("SendSharedMemoryHandleToSelfDelayedPort"); | 1075 PreConnectSetUp("SendSharedMemoryHandleToSelfDelayedPort"); |
1062 | |
1063 // Technically, the channel is an endpoint, but we need the proxy listener to | 1076 // Technically, the channel is an endpoint, but we need the proxy listener to |
1064 // receive the messages so that it can quit the message loop. | 1077 // receive the messages so that it can quit the message loop. |
1065 channel()->SetAttachmentBrokerEndpoint(false); | 1078 channel()->SetAttachmentBrokerEndpoint(false); |
| 1079 PostConnectSetUp(); |
1066 get_proxy_listener()->set_listener(get_broker()); | 1080 get_proxy_listener()->set_listener(get_broker()); |
1067 | 1081 |
1068 { | 1082 { |
1069 scoped_ptr<base::SharedMemory> shared_memory( | 1083 scoped_ptr<base::SharedMemory> shared_memory( |
1070 MakeSharedMemory(kDataBuffer1)); | 1084 MakeSharedMemory(kDataBuffer1)); |
1071 mach_port_urefs_t ref_count = IPC::GetMachRefCount( | 1085 mach_port_urefs_t ref_count = IPC::GetMachRefCount( |
1072 shared_memory->handle().GetMemoryObject(), MACH_PORT_RIGHT_SEND); | 1086 shared_memory->handle().GetMemoryObject(), MACH_PORT_RIGHT_SEND); |
1073 | 1087 |
1074 std::vector<IPC::BrokerableAttachment::AttachmentId> ids; | 1088 std::vector<IPC::BrokerableAttachment::AttachmentId> ids; |
1075 const int kMessagesToTest = 3; | 1089 const int kMessagesToTest = 3; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1273 SendControlMessage(sender, success); | 1287 SendControlMessage(sender, success); |
1274 } | 1288 } |
1275 } | 1289 } |
1276 | 1290 |
1277 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(MemoryUsageManyMessages) { | 1291 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(MemoryUsageManyMessages) { |
1278 return CommonPrivilegedProcessMain(&MemoryUsageManyMessagesCallback, | 1292 return CommonPrivilegedProcessMain(&MemoryUsageManyMessagesCallback, |
1279 "MemoryUsageManyMessages"); | 1293 "MemoryUsageManyMessages"); |
1280 } | 1294 } |
1281 | 1295 |
1282 } // namespace | 1296 } // namespace |
OLD | NEW |