Chromium Code Reviews| Index: util/mach/child_port_server_test.cc |
| diff --git a/util/mach/child_port_server_test.cc b/util/mach/child_port_server_test.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e655a9c742b26d3cab77e60a6e599c9e8a332c5b |
| --- /dev/null |
| +++ b/util/mach/child_port_server_test.cc |
| @@ -0,0 +1,135 @@ |
| +// Copyright 2014 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include "util/mach/child_port_server.h" |
| + |
| +#include <string.h> |
| + |
| +#include "gmock/gmock.h" |
| +#include "gtest/gtest.h" |
| +#include "util/mach/mach_extensions.h" |
| + |
| +namespace crashpad { |
| +namespace test { |
| +namespace { |
| + |
| +using testing::Eq; |
| +using testing::Pointee; |
| +using testing::Return; |
| + |
| +// Fake Mach ports. These aren’t used as ports in these tests, they’re just used |
| +// as cookies to make sure that the correct values get passed to the correct |
| +// places. |
| +const mach_port_t kServerLocalPort = 0x05050505; |
| +const mach_port_t kCheckInPort = 0x06060606; |
| + |
| +// Other fake values. |
| +const mach_msg_type_name_t kCheckInPortRightType = MACH_MSG_TYPE_PORT_SEND; |
| +const child_port_token_t kCheckInToken = 0xfedcba9876543210; |
| + |
| +// The definition of the request structure from child_port.h isn’t available |
| +// here. It needs custom initialization code, so duplicate the expected |
| +// definition of the structure from child_port.h here in this file, and provide |
| +// the initialization code as a method in true object-oriented fashion. |
| + |
| +struct __attribute__((packed, aligned(4))) ChildPortCheckInRequest { |
| + mach_msg_header_t Head; |
| + mach_msg_body_t msgh_body; |
| + mach_msg_port_descriptor_t port; |
| + NDR_record_t NDR; |
| + child_port_token_t token; |
| + |
| + void InitializeForTesting() { |
|
Robert Sesek
2014/11/25 18:56:51
Just make this the constructor instead?
Mark Mentovai
2014/11/25 19:29:36
Robert Sesek wrote:
|
| + memset(this, 0xa5, sizeof(*this)); |
| + Head.msgh_bits = |
| + MACH_MSGH_BITS(0, MACH_MSG_TYPE_PORT_SEND) | MACH_MSGH_BITS_COMPLEX; |
| + Head.msgh_size = sizeof(*this); |
| + Head.msgh_remote_port = MACH_PORT_NULL; |
| + Head.msgh_local_port = kServerLocalPort; |
| + Head.msgh_id = 10011; |
| + msgh_body.msgh_descriptor_count = 1; |
| + port.name = kCheckInPort; |
| + port.disposition = kCheckInPortRightType; |
| + port.type = MACH_MSG_PORT_DESCRIPTOR; |
| + NDR = NDR_record; |
| + token = kCheckInToken; |
| + } |
| +}; |
| + |
| +struct __attribute__((packed, aligned(4))) MIGReply { |
| + mach_msg_header_t Head; |
| + NDR_record_t NDR; |
| + kern_return_t RetCode; |
| + |
| + void InitializeForTesting() { |
| + memset(this, 0x5a, sizeof(*this)); |
| + RetCode = KERN_FAILURE; |
| + } |
| + |
| + void Verify() { |
| + EXPECT_EQ(implicit_cast<mach_msg_bits_t>(MACH_MSGH_BITS(0, 0)), |
| + Head.msgh_bits); |
| + EXPECT_EQ(sizeof(*this), Head.msgh_size); |
| + EXPECT_EQ(kMachPortNull, Head.msgh_remote_port); |
| + EXPECT_EQ(kMachPortNull, Head.msgh_local_port); |
| + EXPECT_EQ(10111, Head.msgh_id); |
| + EXPECT_EQ(0, memcmp(&NDR, &NDR_record, sizeof(NDR))); |
| + EXPECT_EQ(MIG_NO_REPLY, RetCode); |
| + } |
| +}; |
| + |
| +class MockChildPortServerInterface : public ChildPortServer::Interface { |
| + public: |
| + MOCK_METHOD5(HandleChildPortCheckIn, |
| + kern_return_t(child_port_server_t server, |
| + const child_port_token_t token, |
| + mach_port_t port, |
| + mach_msg_type_name_t right_type, |
| + bool* destroy_complex_request)); |
| +}; |
| + |
| +TEST(ChildPortServer, MockChildPortCheckIn) { |
|
Mark Mentovai
2014/11/25 17:27:31
I also added this bonus test.
|
| + MockChildPortServerInterface server_interface; |
| + ChildPortServer server(&server_interface); |
| + |
| + ChildPortCheckInRequest request; |
| + EXPECT_LE(sizeof(request), server.MachMessageServerRequestSize()); |
| + request.InitializeForTesting(); |
| + |
| + MIGReply reply; |
| + EXPECT_LE(sizeof(reply), server.MachMessageServerReplySize()); |
| + reply.InitializeForTesting(); |
| + |
| + EXPECT_CALL(server_interface, |
| + HandleChildPortCheckIn(kServerLocalPort, |
| + kCheckInToken, |
| + kCheckInPort, |
| + kCheckInPortRightType, |
| + Pointee(Eq(false)))) |
| + .WillOnce(Return(MIG_NO_REPLY)) |
| + .RetiresOnSaturation(); |
| + |
| + bool destroy_complex_request = false; |
| + EXPECT_TRUE(server.MachMessageServerFunction( |
| + reinterpret_cast<mach_msg_header_t*>(&request), |
| + reinterpret_cast<mach_msg_header_t*>(&reply), |
| + &destroy_complex_request)); |
| + EXPECT_FALSE(destroy_complex_request); |
| + |
| + reply.Verify(); |
| +} |
| + |
| +} // namespace |
| +} // namespace test |
| +} // namespace crashpad |