summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorerikchen <erikchen@chromium.org>2015-10-12 11:24:01 -0700
committerCommit bot <commit-bot@chromium.org>2015-10-12 18:24:43 +0000
commit89d7423309ae226cccf2a160d31275f61da93628 (patch)
treedb0235a29bde004a703419b952569360e6c3132b
parent622249055279172f4b064ed2e0ace8aa0dadddc2 (diff)
downloadchromium_src-89d7423309ae226cccf2a160d31275f61da93628.zip
chromium_src-89d7423309ae226cccf2a160d31275f61da93628.tar.gz
chromium_src-89d7423309ae226cccf2a160d31275f61da93628.tar.bz2
ipc: Move Mac test utilities into their own file.
They are about to be used by a second test suite. This CL is intended as a refactor with no behavioral change. BUG=535711 Review URL: https://codereview.chromium.org/1393723003 Cr-Commit-Position: refs/heads/master@{#353560}
-rw-r--r--ipc/BUILD.gn2
-rw-r--r--ipc/attachment_broker_privileged_mac_unittest.cc100
-rw-r--r--ipc/ipc.gyp2
-rw-r--r--ipc/test_util_mac.cc108
-rw-r--r--ipc/test_util_mac.h50
5 files changed, 163 insertions, 99 deletions
diff --git a/ipc/BUILD.gn b/ipc/BUILD.gn
index 9a2ed26..c837258 100644
--- a/ipc/BUILD.gn
+++ b/ipc/BUILD.gn
@@ -237,6 +237,8 @@ source_set("test_support") {
"ipc_test_channel_listener.h",
"ipc_test_sink.cc",
"ipc_test_sink.h",
+ "test_util_mac.cc",
+ "test_util_mac.h",
]
deps = [
":ipc",
diff --git a/ipc/attachment_broker_privileged_mac_unittest.cc b/ipc/attachment_broker_privileged_mac_unittest.cc
index 8c82b8d..6231ef8 100644
--- a/ipc/attachment_broker_privileged_mac_unittest.cc
+++ b/ipc/attachment_broker_privileged_mac_unittest.cc
@@ -6,7 +6,6 @@
#include <mach/mach.h>
#include <mach/mach_vm.h>
-#include <servers/bootstrap.h>
#include <map>
@@ -16,11 +15,10 @@
#include "base/memory/shared_memory.h"
#include "base/process/port_provider_mac.h"
#include "base/process/process_handle.h"
-#include "base/rand_util.h"
-#include "base/strings/stringprintf.h"
#include "base/sys_info.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
+#include "ipc/test_util_mac.h"
#include "testing/multiprocess_func_list.h"
namespace IPC {
@@ -29,79 +27,6 @@ namespace {
static const std::string g_service_switch_name = "service_name";
-// Structs used to pass a mach port from client to server.
-struct MachSendPortMessage {
- mach_msg_header_t header;
- mach_msg_body_t body;
- mach_msg_port_descriptor_t data;
-};
-struct MachReceivePortMessage : public MachSendPortMessage {
- mach_msg_trailer_t trailer;
-};
-
-// Makes the current process into a Mach Server with the given |service_name|.
-base::mac::ScopedMachSendRight BecomeMachServer(const char* service_name) {
- mach_port_t port;
- kern_return_t kr = bootstrap_check_in(bootstrap_port, service_name, &port);
- MACH_CHECK(kr == KERN_SUCCESS, kr) << "BecomeMachServer";
- return base::mac::ScopedMachSendRight(port);
-}
-
-// Returns the mach port for the Mach Server with the given |service_name|.
-base::mac::ScopedMachSendRight LookupServer(const char* service_name) {
- mach_port_t server_port;
- kern_return_t kr =
- bootstrap_look_up(bootstrap_port, service_name, &server_port);
- MACH_CHECK(kr == KERN_SUCCESS, kr) << "LookupServer";
- return base::mac::ScopedMachSendRight(server_port);
-}
-
-base::mac::ScopedMachReceiveRight MakeReceivingPort() {
- mach_port_t client_port;
- kern_return_t kr =
- mach_port_allocate(mach_task_self(), // our task is acquiring
- MACH_PORT_RIGHT_RECEIVE, // a new receive right
- &client_port); // with this name
- MACH_CHECK(kr == KERN_SUCCESS, kr) << "MakeReceivingPort";
- return base::mac::ScopedMachReceiveRight(client_port);
-}
-
-// Blocks until a mach message is sent to |server_port|. This mach message
-// must contain a mach port. Returns that mach port.
-base::mac::ScopedMachSendRight ReceiveMachPort(mach_port_t port_to_listen_on) {
- MachReceivePortMessage recv_msg;
- mach_msg_header_t* recv_hdr = &recv_msg.header;
- recv_hdr->msgh_local_port = port_to_listen_on;
- recv_hdr->msgh_size = sizeof(recv_msg);
- kern_return_t kr =
- mach_msg(recv_hdr, MACH_RCV_MSG, 0, recv_hdr->msgh_size,
- port_to_listen_on, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
- MACH_CHECK(kr == KERN_SUCCESS, kr) << "ReceiveMachPort";
- mach_port_t other_task_port = recv_msg.data.name;
- return base::mac::ScopedMachSendRight(other_task_port);
-}
-
-// Passes a copy of the send right of |port_to_send| to |receiving_port|.
-void SendMachPort(mach_port_t receiving_port,
- mach_port_t port_to_send,
- int disposition) {
- MachSendPortMessage send_msg;
- send_msg.header.msgh_bits =
- MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
- send_msg.header.msgh_size = sizeof(send_msg);
- send_msg.header.msgh_remote_port = receiving_port;
- send_msg.header.msgh_local_port = MACH_PORT_NULL;
- send_msg.header.msgh_reserved = 0;
- send_msg.header.msgh_id = 0;
- send_msg.body.msgh_descriptor_count = 1;
- send_msg.data.name = port_to_send;
- send_msg.data.disposition = disposition;
- send_msg.data.type = MACH_MSG_PORT_DESCRIPTOR;
- int kr = mach_msg(&send_msg.header, MACH_SEND_MSG, send_msg.header.msgh_size,
- 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
- MACH_CHECK(kr == KERN_SUCCESS, kr) << "SendMachPort";
-}
-
// Sends a uint32_t to a mach port.
void SendUInt32(mach_port_t port, uint32_t message) {
int message_size = sizeof(uint32_t);
@@ -146,29 +71,6 @@ uint32_t ReceiveUInt32(mach_port_t listening_port) {
return response;
}
-std::string CreateRandomServiceName() {
- return base::StringPrintf(
- "AttachmentBrokerPrivilegedMacMultiProcessTest.%llu", base::RandUint64());
-}
-
-// The number of active names in the current task's port name space.
-mach_msg_type_number_t GetActiveNameCount() {
- mach_port_name_array_t name_array;
- mach_msg_type_number_t names_count;
- mach_port_type_array_t type_array;
- mach_msg_type_number_t types_count;
- kern_return_t kr = mach_port_names(mach_task_self(), &name_array,
- &names_count, &type_array, &types_count);
- MACH_CHECK(kr == KERN_SUCCESS, kr) << "GetActiveNameCount";
- return names_count;
-}
-
-// Increments the ref count for the right/name pair.
-void IncrementMachRefCount(mach_port_name_t name, mach_port_right_t right) {
- kern_return_t kr = mach_port_mod_refs(mach_task_self(), name, right, 1);
- MACH_CHECK(kr == KERN_SUCCESS, kr) << "GetRefCount";
-}
-
// Sets up the mach communication ports with the server. Returns a port to which
// the server will send mach objects.
// |original_name_count| is an output variable that describes the number of
diff --git a/ipc/ipc.gyp b/ipc/ipc.gyp
index ba0dd0a..d6e254a 100644
--- a/ipc/ipc.gyp
+++ b/ipc/ipc.gyp
@@ -155,6 +155,8 @@
'ipc_test_channel_listener.h',
'ipc_test_sink.cc',
'ipc_test_sink.h',
+ 'test_util_mac.cc',
+ 'test_util_mac.h',
],
},
],
diff --git a/ipc/test_util_mac.cc b/ipc/test_util_mac.cc
new file mode 100644
index 0000000..59e037b
--- /dev/null
+++ b/ipc/test_util_mac.cc
@@ -0,0 +1,108 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ipc/test_util_mac.h"
+
+#include <servers/bootstrap.h>
+
+#include "base/mac/mach_logging.h"
+#include "base/mac/scoped_mach_port.h"
+#include "base/rand_util.h"
+#include "base/strings/stringprintf.h"
+
+namespace {
+
+// Structs used to pass a Mach port over mach_msg().
+struct MachSendPortMessage {
+ mach_msg_header_t header;
+ mach_msg_body_t body;
+ mach_msg_port_descriptor_t data;
+};
+
+struct MachReceivePortMessage : public MachSendPortMessage {
+ mach_msg_trailer_t trailer;
+};
+
+} // namespace
+
+namespace IPC {
+
+std::string CreateRandomServiceName() {
+ return base::StringPrintf("TestUtilMac.%llu", base::RandUint64());
+}
+
+base::mac::ScopedMachReceiveRight BecomeMachServer(const char* service_name) {
+ mach_port_t port;
+ kern_return_t kr = bootstrap_check_in(bootstrap_port, service_name, &port);
+ MACH_CHECK(kr == KERN_SUCCESS, kr) << "BecomeMachServer ";
+ return base::mac::ScopedMachReceiveRight(port);
+}
+
+base::mac::ScopedMachSendRight LookupServer(const char* service_name) {
+ mach_port_t server_port;
+ kern_return_t kr =
+ bootstrap_look_up(bootstrap_port, service_name, &server_port);
+ MACH_CHECK(kr == KERN_SUCCESS, kr) << "LookupServer";
+ return base::mac::ScopedMachSendRight(server_port);
+}
+
+base::mac::ScopedMachReceiveRight MakeReceivingPort() {
+ mach_port_t client_port;
+ kern_return_t kr = mach_port_allocate(mach_task_self(),
+ MACH_PORT_RIGHT_RECEIVE, &client_port);
+ MACH_CHECK(kr == KERN_SUCCESS, kr) << "MakeReceivingPort";
+ return base::mac::ScopedMachReceiveRight(client_port);
+}
+
+base::mac::ScopedMachSendRight ReceiveMachPort(mach_port_t port_to_listen_on) {
+ MachReceivePortMessage recv_msg;
+ mach_msg_header_t* recv_hdr = &recv_msg.header;
+ recv_hdr->msgh_local_port = port_to_listen_on;
+ recv_hdr->msgh_size = sizeof(recv_msg);
+ kern_return_t kr =
+ mach_msg(recv_hdr, MACH_RCV_MSG, 0, recv_hdr->msgh_size,
+ port_to_listen_on, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
+ MACH_CHECK(kr == KERN_SUCCESS, kr) << "ReceiveMachPort";
+ mach_port_t other_task_port = recv_msg.data.name;
+ return base::mac::ScopedMachSendRight(other_task_port);
+}
+
+// Passes a copy of the send right of |port_to_send| to |receiving_port|.
+void SendMachPort(mach_port_t receiving_port,
+ mach_port_t port_to_send,
+ int disposition) {
+ MachSendPortMessage send_msg;
+ send_msg.header.msgh_bits =
+ MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
+ send_msg.header.msgh_size = sizeof(send_msg);
+ send_msg.header.msgh_remote_port = receiving_port;
+ send_msg.header.msgh_local_port = MACH_PORT_NULL;
+ send_msg.header.msgh_reserved = 0;
+ send_msg.header.msgh_id = 0;
+ send_msg.body.msgh_descriptor_count = 1;
+ send_msg.data.name = port_to_send;
+ send_msg.data.disposition = disposition;
+ send_msg.data.type = MACH_MSG_PORT_DESCRIPTOR;
+ int kr = mach_msg(&send_msg.header, MACH_SEND_MSG, send_msg.header.msgh_size,
+ 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
+ MACH_CHECK(kr == KERN_SUCCESS, kr) << "SendMachPort";
+}
+
+mach_msg_type_number_t GetActiveNameCount() {
+ mach_port_name_array_t name_array;
+ mach_msg_type_number_t names_count;
+ mach_port_type_array_t type_array;
+ mach_msg_type_number_t types_count;
+ kern_return_t kr = mach_port_names(mach_task_self(), &name_array,
+ &names_count, &type_array, &types_count);
+ MACH_CHECK(kr == KERN_SUCCESS, kr) << "GetActiveNameCount";
+ return names_count;
+}
+
+void IncrementMachRefCount(mach_port_name_t name, mach_port_right_t right) {
+ kern_return_t kr = mach_port_mod_refs(mach_task_self(), name, right, 1);
+ MACH_CHECK(kr == KERN_SUCCESS, kr) << "GetRefCount";
+}
+
+} // namespace IPC
diff --git a/ipc/test_util_mac.h b/ipc/test_util_mac.h
new file mode 100644
index 0000000..6dd2131
--- /dev/null
+++ b/ipc/test_util_mac.h
@@ -0,0 +1,50 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file contains Mac-specific utility functions used by ipc tests.
+
+#ifndef IPC_TEST_UTIL_MAC_H_
+#define IPC_TEST_UTIL_MAC_H_
+
+#include <mach/mach.h>
+
+#include <string>
+
+#include "base/mac/scoped_mach_port.h"
+
+namespace IPC {
+
+// Returns a random name suitable for Mach Server registration.
+std::string CreateRandomServiceName();
+
+// Makes the current process into a Mach Server with the given |service_name|.
+// Returns a receive right for the service port.
+base::mac::ScopedMachReceiveRight BecomeMachServer(const char* service_name);
+
+// Returns a send right to the service port for the Mach Server with the given
+// |service_name|.
+base::mac::ScopedMachSendRight LookupServer(const char* service_name);
+
+// Returns the receive right to a newly minted Mach port.
+base::mac::ScopedMachReceiveRight MakeReceivingPort();
+
+// Blocks until a Mach message is sent to |port_to_listen_on|. This Mach message
+// must contain a Mach port. Returns that Mach port.
+base::mac::ScopedMachSendRight ReceiveMachPort(mach_port_t port_to_listen_on);
+
+// Passes a copy of the send right of |port_to_send| to |receiving_port|, using
+// the given |disposition|.
+void SendMachPort(mach_port_t receiving_port,
+ mach_port_t port_to_send,
+ int disposition);
+
+// The number of active names in the current task's port name space.
+mach_msg_type_number_t GetActiveNameCount();
+
+// Increments the ref count for the right/name pair.
+void IncrementMachRefCount(mach_port_name_t name, mach_port_right_t right);
+
+} // namespace IPC
+
+#endif // IPC_TEST_UTIL_MAC_H_