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/mach/mach_extensions.h" | |
29 #include "util/test/mac/mach_errors.h" | |
30 | |
31 namespace { | |
32 | |
33 using namespace crashpad; | |
34 using namespace crashpad::test; | |
35 using namespace testing; | |
36 | |
37 TEST(Bootstrap, BootstrapCheckIn) { | |
38 std::string service_name = "com.googlecode.crashpad.test.bootstrap."; | |
39 for (int index = 0; index < 16; ++index) { | |
40 service_name.append(1, base::RandInt('A', 'Z')); | |
41 } | |
42 | |
43 // The service shouldn’t be registered in the bootstrap namespace yet. | |
44 mach_port_t client_port = MACH_PORT_NULL; | |
45 kern_return_t kr = | |
46 bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); | |
47 ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr) | |
48 << BootstrapErrorMessage(kr, "bootstrap_look_up"); | |
49 | |
50 // Check in, getting a receive right. | |
51 mach_port_t server_port; | |
52 kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &server_port); | |
53 ASSERT_EQ(BOOTSTRAP_SUCCESS, kr) | |
54 << BootstrapErrorMessage(kr, "bootstrap_check_in"); | |
55 ASSERT_NE(kMachPortNull, server_port); | |
56 base::mac::ScopedMachReceiveRight server_port_owner(server_port); | |
57 | |
58 // A subsequent checkin attempt should fail. | |
59 mach_port_t fail_port = MACH_PORT_NULL; | |
60 kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &fail_port); | |
61 EXPECT_EQ(BOOTSTRAP_SERVICE_ACTIVE, kr); | |
62 EXPECT_EQ(kMachPortNull, fail_port); | |
63 | |
64 // Look up the service, getting a send right. | |
65 kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); | |
66 ASSERT_EQ(BOOTSTRAP_SUCCESS, kr) | |
67 << BootstrapErrorMessage(kr, "bootstrap_look_up"); | |
68 base::mac::ScopedMachSendRight client_port_owner(client_port); | |
69 EXPECT_NE(kMachPortNull, client_port); | |
70 | |
71 // Have the “client” send a message to the “server”. | |
72 struct SendMessage { | |
73 mach_msg_header_t header; | |
74 char data[64]; | |
75 }; | |
76 SendMessage send_message = {}; | |
77 send_message.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); | |
78 send_message.header.msgh_size = sizeof(send_message); | |
79 send_message.header.msgh_remote_port = client_port; | |
80 const char kMessageData[] = "Mach messaging is not a big truck"; | |
81 const size_t message_data_size = | |
82 std::min(sizeof(kMessageData), sizeof(send_message.data)); | |
83 memcpy(send_message.data, kMessageData, message_data_size); | |
84 | |
85 // The receive operation happens in this same thread after the send, so use a | |
86 // non-blocking send (MACH_SEND_TIMEOUT with MACH_MSG_TIMEOUT_NONE). This is a | |
87 // small and simple message and the destination port’s queue should be empty | |
88 // so a non-blocking send should be successful. | |
89 kr = mach_msg(&send_message.header, | |
90 MACH_SEND_MSG | MACH_SEND_TIMEOUT, | |
91 send_message.header.msgh_size, | |
92 0, | |
93 MACH_PORT_NULL, | |
94 MACH_MSG_TIMEOUT_NONE, | |
95 MACH_PORT_NULL); | |
96 ASSERT_EQ(MACH_MSG_SUCCESS, kr) << MachErrorMessage(kr, "mach_msg"); | |
97 | |
98 struct ReceiveMessage : public SendMessage { | |
99 mach_msg_trailer_t trailer; | |
100 }; | |
101 ReceiveMessage receive_message; | |
102 memset(&receive_message, 0xa5, sizeof(receive_message)); | |
103 | |
104 kr = mach_msg(&receive_message.header, | |
105 MACH_RCV_MSG | MACH_RCV_TIMEOUT, | |
106 0, | |
107 sizeof(receive_message), | |
108 server_port, | |
109 MACH_MSG_TIMEOUT_NONE, | |
110 MACH_PORT_NULL); | |
111 ASSERT_EQ(MACH_MSG_SUCCESS, kr) << MachErrorMessage(kr, "mach_msg"); | |
112 | |
113 EXPECT_EQ(sizeof(send_message), receive_message.header.msgh_size); | |
114 EXPECT_EQ(0, memcmp(receive_message.data, kMessageData, message_data_size)); | |
115 | |
116 // Make sure that the bootstrap server’s mapping disappears if the service | |
117 // does. | |
118 server_port_owner.reset(); | |
119 server_port = MACH_PORT_NULL; | |
120 | |
121 // With the server port gone, the client port should have become a dead name. | |
122 mach_port_type_t client_port_type; | |
123 kr = mach_port_type(mach_task_self(), client_port, &client_port_type); | |
124 ASSERT_EQ(KERN_SUCCESS, kr) << MachErrorMessage(kr, "mach_port_type"); | |
125 EXPECT_EQ(MACH_PORT_TYPE_DEAD_NAME, client_port_type); | |
126 | |
127 client_port_owner.reset(); | |
128 client_port = MACH_PORT_NULL; | |
129 | |
130 kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); | |
131 ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr) | |
132 << BootstrapErrorMessage(kr, "bootstrap_look_up"); | |
133 } | |
134 | |
135 } // namespace | |
OLD | NEW |