| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/mach/bootstrap.h" | 15 #include "util/mach/bootstrap.h" |
| 16 | 16 |
| 17 #include <mach/mach.h> | 17 #include <mach/mach.h> |
| 18 #include <servers/bootstrap.h> | 18 #include <servers/bootstrap.h> |
| 19 #include <string.h> | 19 #include <string.h> |
| 20 | 20 |
| 21 #include <algorithm> | 21 #include <algorithm> |
| 22 #include <string> | 22 #include <string> |
| 23 | 23 |
| 24 #include "base/basictypes.h" | 24 #include "base/basictypes.h" |
| 25 #include "base/mac/scoped_mach_port.h" | 25 #include "base/mac/scoped_mach_port.h" |
| 26 #include "base/rand_util.h" | 26 #include "base/rand_util.h" |
| 27 #include "gtest/gtest.h" | 27 #include "gtest/gtest.h" |
| 28 #include "util/mach/mach_extensions.h" |
| 28 #include "util/test/mac/mach_errors.h" | 29 #include "util/test/mac/mach_errors.h" |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 using namespace crashpad; | 33 using namespace crashpad; |
| 33 using namespace crashpad::test; | 34 using namespace crashpad::test; |
| 34 using namespace testing; | 35 using namespace testing; |
| 35 | 36 |
| 36 TEST(Bootstrap, BootstrapCheckIn) { | 37 TEST(Bootstrap, BootstrapCheckIn) { |
| 37 std::string service_name = "com.googlecode.crashpad.test.bootstrap."; | 38 std::string service_name = "com.googlecode.crashpad.test.bootstrap."; |
| 38 for (int index = 0; index < 16; ++index) { | 39 for (int index = 0; index < 16; ++index) { |
| 39 service_name.append(1, base::RandInt('A', 'Z')); | 40 service_name.append(1, base::RandInt('A', 'Z')); |
| 40 } | 41 } |
| 41 | 42 |
| 42 // The service shouldn’t be registered in the bootstrap namespace yet. | 43 // The service shouldn’t be registered in the bootstrap namespace yet. |
| 43 mach_port_t client_port = MACH_PORT_NULL; | 44 mach_port_t client_port = MACH_PORT_NULL; |
| 44 kern_return_t kr = | 45 kern_return_t kr = |
| 45 bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); | 46 bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); |
| 46 ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr) | 47 ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr) |
| 47 << BootstrapErrorMessage(kr, "bootstrap_look_up"); | 48 << BootstrapErrorMessage(kr, "bootstrap_look_up"); |
| 48 | 49 |
| 49 // Check in, getting a receive right. | 50 // Check in, getting a receive right. |
| 50 mach_port_t server_port; | 51 mach_port_t server_port; |
| 51 kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &server_port); | 52 kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &server_port); |
| 52 ASSERT_EQ(BOOTSTRAP_SUCCESS, kr) | 53 ASSERT_EQ(BOOTSTRAP_SUCCESS, kr) |
| 53 << BootstrapErrorMessage(kr, "bootstrap_check_in"); | 54 << BootstrapErrorMessage(kr, "bootstrap_check_in"); |
| 54 ASSERT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), server_port); | 55 ASSERT_NE(kMachPortNull, server_port); |
| 55 base::mac::ScopedMachReceiveRight server_port_owner(server_port); | 56 base::mac::ScopedMachReceiveRight server_port_owner(server_port); |
| 56 | 57 |
| 57 // A subsequent checkin attempt should fail. | 58 // A subsequent checkin attempt should fail. |
| 58 mach_port_t fail_port = MACH_PORT_NULL; | 59 mach_port_t fail_port = MACH_PORT_NULL; |
| 59 kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &fail_port); | 60 kr = BootstrapCheckIn(bootstrap_port, service_name.c_str(), &fail_port); |
| 60 EXPECT_EQ(BOOTSTRAP_SERVICE_ACTIVE, kr); | 61 EXPECT_EQ(BOOTSTRAP_SERVICE_ACTIVE, kr); |
| 61 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), fail_port); | 62 EXPECT_EQ(kMachPortNull, fail_port); |
| 62 | 63 |
| 63 // Look up the service, getting a send right. | 64 // Look up the service, getting a send right. |
| 64 kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); | 65 kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); |
| 65 ASSERT_EQ(BOOTSTRAP_SUCCESS, kr) | 66 ASSERT_EQ(BOOTSTRAP_SUCCESS, kr) |
| 66 << BootstrapErrorMessage(kr, "bootstrap_look_up"); | 67 << BootstrapErrorMessage(kr, "bootstrap_look_up"); |
| 67 base::mac::ScopedMachSendRight client_port_owner(client_port); | 68 base::mac::ScopedMachSendRight client_port_owner(client_port); |
| 68 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), client_port); | 69 EXPECT_NE(kMachPortNull, client_port); |
| 69 | 70 |
| 70 // Have the “client” send a message to the “server”. | 71 // Have the “client” send a message to the “server”. |
| 71 struct SendMessage { | 72 struct SendMessage { |
| 72 mach_msg_header_t header; | 73 mach_msg_header_t header; |
| 73 char data[64]; | 74 char data[64]; |
| 74 }; | 75 }; |
| 75 SendMessage send_message = {}; | 76 SendMessage send_message = {}; |
| 76 send_message.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); | 77 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_size = sizeof(send_message); |
| 78 send_message.header.msgh_remote_port = client_port; | 79 send_message.header.msgh_remote_port = client_port; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 126 |
| 126 client_port_owner.reset(); | 127 client_port_owner.reset(); |
| 127 client_port = MACH_PORT_NULL; | 128 client_port = MACH_PORT_NULL; |
| 128 | 129 |
| 129 kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); | 130 kr = bootstrap_look_up(bootstrap_port, service_name.c_str(), &client_port); |
| 130 ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr) | 131 ASSERT_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr) |
| 131 << BootstrapErrorMessage(kr, "bootstrap_look_up"); | 132 << BootstrapErrorMessage(kr, "bootstrap_look_up"); |
| 132 } | 133 } |
| 133 | 134 |
| 134 } // namespace | 135 } // namespace |
| OLD | NEW |