crashpad/util/mach/child_port_server_test.cc
Mark Mentovai 04aaa36026 Add ChildPortServer, a MachMessageServer::Interface implementation for
the child_port subsystem.

Common routines shared with the ExcServer family of classes have been
moved to a new file, where they can be shared between different
MachMessageServer::Interface implementations.

TEST=util_test ChildPortServer.*:MachMessageUtil.*
R=rsesek@chromium.org

Review URL: https://codereview.chromium.org/754123002
2014-11-25 14:29:46 -05:00

134 lines
4.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 arent used as ports in these tests, theyre 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 isnt 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 {
ChildPortCheckInRequest() {
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;
}
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;
};
struct __attribute__((packed, aligned(4))) MIGReply {
MIGReply() {
memset(this, 0x5a, sizeof(*this));
RetCode = KERN_FAILURE;
}
mach_msg_header_t Head;
NDR_record_t NDR;
kern_return_t RetCode;
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) {
MockChildPortServerInterface server_interface;
ChildPortServer server(&server_interface);
ChildPortCheckInRequest request;
EXPECT_LE(sizeof(request), server.MachMessageServerRequestSize());
MIGReply reply;
EXPECT_LE(sizeof(reply), server.MachMessageServerReplySize());
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