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