OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/mach/bootstrap.h" |
| 16 |
| 17 #include <mach/mach.h> |
| 18 #include <servers/bootstrap.h> |
| 19 #include <string.h> |
| 20 |
| 21 #include <algorithm> |
| 22 #include <string> |
| 23 |
| 24 #include "base/basictypes.h" |
| 25 #include "base/mac/scoped_mach_port.h" |
| 26 #include "base/rand_util.h" |
| 27 #include "gtest/gtest.h" |
| 28 #include "util/test/mac/mach_errors.h" |
| 29 |
| 30 namespace { |
| 31 |
| 32 using namespace crashpad; |
| 33 using namespace crashpad::test; |
| 34 using namespace testing; |
| 35 |
| 36 TEST(Bootstrap, BootstrapCheckIn) { |
| 37 std::string service_name = "com.googlecode.crashpad.test.bootstrap."; |
| 38 for (int index = 0; index < 16; ++index) { |
| 39 service_name.append(1, base::RandInt('A', 'Z')); |
| 40 } |
| 41 |
| 42 // The service shouldn’t be registered in the bootstrap namespace yet. |
| 43 mach_port_t client_port = MACH_PORT_NULL; |
| 44 kern_return_t kr = |
| 45 bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); |
| 46 ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr) |
| 47 << BootstrapErrorMessage(kr, "bootstrap_look_up"); |
| 48 |
| 49 // Check in, getting a receive right. |
| 50 mach_port_t server_port; |
| 51 kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &server_port); |
| 52 ASSERT_EQ(BOOTSTRAP_SUCCESS, kr) |
| 53 << BootstrapErrorMessage(kr, "bootstrap_check_in"); |
| 54 ASSERT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), server_port); |
| 55 base::mac::ScopedMachReceiveRight server_port_owner(server_port); |
| 56 |
| 57 // A subsequent checkin attempt should fail. |
| 58 mach_port_t fail_port = MACH_PORT_NULL; |
| 59 kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &fail_port); |
| 60 EXPECT_EQ(BOOTSTRAP_SERVICE_ACTIVE, kr); |
| 61 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), fail_port); |
| 62 |
| 63 // Look up the service, getting a send right. |
| 64 kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); |
| 65 ASSERT_EQ(BOOTSTRAP_SUCCESS, kr) |
| 66 << BootstrapErrorMessage(kr, "bootstrap_look_up"); |
| 67 base::mac::ScopedMachSendRight client_port_owner(client_port); |
| 68 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), client_port); |
| 69 |
| 70 // Have the “client” send a message to the “server”. |
| 71 struct SendMessage { |
| 72 mach_msg_header_t header; |
| 73 char data[64]; |
| 74 }; |
| 75 SendMessage send_message = {}; |
| 76 send_message.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); |
| 77 send_message.header.msgh_size = sizeof(send_message); |
| 78 send_message.header.msgh_remote_port = client_port; |
| 79 const char kMessageData[] = "Mach messaging is not a big truck"; |
| 80 const size_t message_data_size = |
| 81 std::min(sizeof(kMessageData), sizeof(send_message.data)); |
| 82 memcpy(send_message.data, kMessageData, message_data_size); |
| 83 |
| 84 // The receive operation happens in this same thread after the send, so use a |
| 85 // non-blocking send (MACH_SEND_TIMEOUT with MACH_MSG_TIMEOUT_NONE). This is a |
| 86 // small and simple message and the destination port’s queue should be empty |
| 87 // so a non-blocking send should be successful. |
| 88 kr = mach_msg(&send_message.header, |
| 89 MACH_SEND_MSG | MACH_SEND_TIMEOUT, |
| 90 send_message.header.msgh_size, |
| 91 0, |
| 92 MACH_PORT_NULL, |
| 93 MACH_MSG_TIMEOUT_NONE, |
| 94 MACH_PORT_NULL); |
| 95 ASSERT_EQ(MACH_MSG_SUCCESS, kr) << MachErrorMessage(kr, "mach_msg"); |
| 96 |
| 97 struct ReceiveMessage : public SendMessage { |
| 98 mach_msg_trailer_t trailer; |
| 99 }; |
| 100 ReceiveMessage receive_message; |
| 101 memset(&receive_message, 0xa5, sizeof(receive_message)); |
| 102 |
| 103 kr = mach_msg(&receive_message.header, |
| 104 MACH_RCV_MSG | MACH_RCV_TIMEOUT, |
| 105 0, |
| 106 sizeof(receive_message), |
| 107 server_port, |
| 108 MACH_MSG_TIMEOUT_NONE, |
| 109 MACH_PORT_NULL); |
| 110 ASSERT_EQ(MACH_MSG_SUCCESS, kr) << MachErrorMessage(kr, "mach_msg"); |
| 111 |
| 112 EXPECT_EQ(sizeof(send_message), receive_message.header.msgh_size); |
| 113 EXPECT_EQ(0, memcmp(receive_message.data, kMessageData, message_data_size)); |
| 114 |
| 115 // Make sure that the bootstrap server’s mapping disappears if the service |
| 116 // does. |
| 117 server_port_owner.reset(); |
| 118 server_port = MACH_PORT_NULL; |
| 119 |
| 120 // With the server port gone, the client port should have become a dead name. |
| 121 mach_port_type_t client_port_type; |
| 122 kr = mach_port_type(mach_task_self(), client_port, &client_port_type); |
| 123 ASSERT_EQ(KERN_SUCCESS, kr) << MachErrorMessage(kr, "mach_port_type"); |
| 124 EXPECT_EQ(MACH_PORT_TYPE_DEAD_NAME, client_port_type); |
| 125 |
| 126 client_port_owner.reset(); |
| 127 client_port = MACH_PORT_NULL; |
| 128 |
| 129 kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); |
| 130 ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr) |
| 131 << BootstrapErrorMessage(kr, "bootstrap_look_up"); |
| 132 } |
| 133 |
| 134 } // namespace |
OLD | NEW |