summaryrefslogtreecommitdiff
path: root/sandbox/mac/xpc_message_server_unittest.cc
blob: 4c4fcf9c9412c28ff682209b93ce5bec04c0b521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sandbox/mac/xpc_message_server.h"

#include <Block.h>
#include <mach/mach.h>
#include <servers/bootstrap.h>

#include "base/command_line.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_mach_port.h"
#include "base/process/kill.h"
#include "base/test/multiprocess_test.h"
#include "sandbox/mac/xpc.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"

#if defined(MAC_OS_X_VERSION_10_7) && \
    MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
// Redeclare methods that only exist on 10.7+ to suppress
// -Wpartial-availability warnings.
extern "C" {
XPC_EXPORT XPC_WARN_RESULT XPC_NONNULL_ALL int64_t
xpc_dictionary_get_int64(xpc_object_t xdict, const char* key);

XPC_EXPORT XPC_WARN_RESULT XPC_NONNULL_ALL uint64_t
xpc_dictionary_get_uint64(xpc_object_t xdict, const char* key);

XPC_EXPORT XPC_NONNULL1 XPC_NONNULL2 void
xpc_dictionary_set_uint64(xpc_object_t xdict, const char* key, uint64_t value);

XPC_EXPORT XPC_MALLOC XPC_RETURNS_RETAINED XPC_WARN_RESULT xpc_object_t
xpc_dictionary_create(const char* const* keys,
                      const xpc_object_t* values,
                      size_t count);
}  // extern "C"
#endif

namespace sandbox {

class XPCMessageServerTest : public testing::Test {
 public:
  void SetUp() override {
    if (!RunXPCTest())
      return;
    ASSERT_TRUE(InitializeXPC());
  }

  bool RunXPCTest() {
    return base::mac::IsOSMountainLionOrLater();
  }
};

// A MessageDemuxer that manages a test server and executes a block for every
// message.
class BlockDemuxer : public MessageDemuxer {
 public:
  BlockDemuxer()
      : demux_block_(NULL),
        server_(this, MACH_PORT_NULL),
        pipe_(NULL) {
  }

  ~BlockDemuxer() override {
    if (pipe_)
      xpc_release(pipe_);
    if (demux_block_)
      Block_release(demux_block_);
  }

  // Starts running the server, given a block to handle incoming IPC messages.
  bool Initialize(void (^demux_block)(IPCMessage request)) {
    if (!server_.Initialize())
      return false;

    // Create a send right on the port so that the XPC pipe can be created.
    if (mach_port_insert_right(mach_task_self(), server_.GetServerPort(),
            server_.GetServerPort(), MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) {
      return false;
    }
    scoped_send_right_.reset(server_.GetServerPort());

    demux_block_ = Block_copy(demux_block);
    pipe_ = xpc_pipe_create_from_port(server_.GetServerPort(), 0);

    return true;
  }

  void DemuxMessage(IPCMessage request) override {
    demux_block_(request);
  }

  xpc_pipe_t pipe() { return pipe_; }

  XPCMessageServer* server() { return &server_; }

 private:
  void (^demux_block_)(IPCMessage request);

  XPCMessageServer server_;

  base::mac::ScopedMachSendRight scoped_send_right_;

  xpc_pipe_t pipe_;
};

#define XPC_TEST_F(name) TEST_F(XPCMessageServerTest, name) { \
    if (!RunXPCTest()) \
      return; \

XPC_TEST_F(ReceiveMessage)  // {
  BlockDemuxer fixture;
  XPCMessageServer* server = fixture.server();

  uint64_t __block value = 0;
  ASSERT_TRUE(fixture.Initialize(^(IPCMessage request) {
      value = xpc_dictionary_get_uint64(request.xpc, "test_value");
      server->SendReply(server->CreateReply(request));
  }));

  xpc_object_t request = xpc_dictionary_create(NULL, NULL, 0);
  xpc_dictionary_set_uint64(request, "test_value", 42);

  xpc_object_t reply;
  EXPECT_EQ(0, xpc_pipe_routine(fixture.pipe(), request, &reply));

  EXPECT_EQ(42u, value);

  xpc_release(request);
  xpc_release(reply);
}

XPC_TEST_F(RejectMessage)  // {
  BlockDemuxer fixture;
  XPCMessageServer* server = fixture.server();
  ASSERT_TRUE(fixture.Initialize(^(IPCMessage request) {
      server->RejectMessage(request, EPERM);
  }));

  xpc_object_t request = xpc_dictionary_create(NULL, NULL, 0);
  xpc_object_t reply;
  EXPECT_EQ(0, xpc_pipe_routine(fixture.pipe(), request, &reply));

  EXPECT_EQ(EPERM, xpc_dictionary_get_int64(reply, "error"));

  xpc_release(request);
  xpc_release(reply);
}

char kGetSenderPID[] = "org.chromium.sandbox.test.GetSenderPID";

XPC_TEST_F(GetSenderPID)  // {
  BlockDemuxer fixture;
  XPCMessageServer* server = fixture.server();

  pid_t __block sender_pid = 0;
  int64_t __block child_pid = 0;
  ASSERT_TRUE(fixture.Initialize(^(IPCMessage request) {
      sender_pid = server->GetMessageSenderPID(request);
      child_pid = xpc_dictionary_get_int64(request.xpc, "child_pid");
  }));

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  kern_return_t kr = bootstrap_register(bootstrap_port, kGetSenderPID,
      server->GetServerPort());
#pragma GCC diagnostic pop
  ASSERT_EQ(KERN_SUCCESS, kr);

  base::Process child = base::SpawnMultiProcessTestChild(
      "GetSenderPID",
      base::GetMultiProcessTestChildBaseCommandLine(),
      base::LaunchOptions());
  ASSERT_TRUE(child.IsValid());

  int exit_code = -1;
  ASSERT_TRUE(child.WaitForExit(&exit_code));
  EXPECT_EQ(0, exit_code);

  EXPECT_EQ(child.Pid(), sender_pid);
  EXPECT_EQ(child.Pid(), child_pid);
  EXPECT_EQ(sender_pid, child_pid);
}

MULTIPROCESS_TEST_MAIN(GetSenderPID) {
  CHECK(sandbox::InitializeXPC());

  mach_port_t port = MACH_PORT_NULL;
  CHECK_EQ(KERN_SUCCESS, bootstrap_look_up(bootstrap_port, kGetSenderPID,
      &port));
  base::mac::ScopedMachSendRight scoped_port(port);

  xpc_pipe_t pipe = xpc_pipe_create_from_port(port, 0);

  xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
  xpc_dictionary_set_int64(message, "child_pid", getpid());
  CHECK_EQ(0, xpc_pipe_simpleroutine(pipe, message));

  xpc_release(message);
  xpc_release(pipe);

  return 0;
}

XPC_TEST_F(ForwardMessage)  // {
  BlockDemuxer first;
  XPCMessageServer* first_server = first.server();

  BlockDemuxer second;
  XPCMessageServer* second_server = second.server();

  ASSERT_TRUE(first.Initialize(^(IPCMessage request) {
      xpc_dictionary_set_int64(request.xpc, "seen_by_first", 1);
      first_server->ForwardMessage(request, second_server->GetServerPort());
  }));
  ASSERT_TRUE(second.Initialize(^(IPCMessage request) {
      IPCMessage reply = second_server->CreateReply(request);
      xpc_dictionary_set_int64(reply.xpc, "seen_by_first",
          xpc_dictionary_get_int64(request.xpc, "seen_by_first"));
      xpc_dictionary_set_int64(reply.xpc, "seen_by_second", 2);
      second_server->SendReply(reply);
  }));

  xpc_object_t request = xpc_dictionary_create(NULL, NULL, 0);
  xpc_object_t reply;
  ASSERT_EQ(0, xpc_pipe_routine(first.pipe(), request, &reply));

  EXPECT_EQ(1, xpc_dictionary_get_int64(reply, "seen_by_first"));
  EXPECT_EQ(2, xpc_dictionary_get_int64(reply, "seen_by_second"));

  xpc_release(request);
  xpc_release(reply);
}

}  // namespace sandbox