summaryrefslogtreecommitdiff
path: root/base/mac/mach_port_broker_unittest.cc
blob: cb4b82ca47ccf1eb83463f221d4c3b552918e9ef (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
// Copyright 2016 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 "base/mac/mach_port_broker.h"

#include "base/command_line.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"

namespace base {

namespace {
const char kBootstrapPortName[] = "thisisatest";
}

class MachPortBrokerTest : public testing::Test,
                           public base::PortProvider::Observer {
 public:
  MachPortBrokerTest()
      : broker_(kBootstrapPortName),
        event_(base::WaitableEvent::ResetPolicy::MANUAL,
               base::WaitableEvent::InitialState::NOT_SIGNALED),
        received_process_(kNullProcessHandle) {
    broker_.AddObserver(this);
  }
  ~MachPortBrokerTest() override {
    broker_.RemoveObserver(this);
  }

  // Helper function to acquire/release locks and call |PlaceholderForPid()|.
  void AddPlaceholderForPid(base::ProcessHandle pid) {
    base::AutoLock lock(broker_.GetLock());
    broker_.AddPlaceholderForPid(pid);
  }

  // Helper function to acquire/release locks and call |FinalizePid()|.
  void FinalizePid(base::ProcessHandle pid,
                   mach_port_t task_port) {
    base::AutoLock lock(broker_.GetLock());
    broker_.FinalizePid(pid, task_port);
  }

  void WaitForTaskPort() {
    event_.Wait();
  }

  // base::PortProvider::Observer:
  void OnReceivedTaskPort(ProcessHandle process) override {
    received_process_ = process;
    event_.Signal();
  }

 protected:
  MachPortBroker broker_;
  WaitableEvent event_;
  ProcessHandle received_process_;
};

TEST_F(MachPortBrokerTest, Locks) {
  // Acquire and release the locks.  Nothing bad should happen.
  base::AutoLock lock(broker_.GetLock());
}

TEST_F(MachPortBrokerTest, AddPlaceholderAndFinalize) {
  // Add a placeholder for PID 1.
  AddPlaceholderForPid(1);
  EXPECT_EQ(0u, broker_.TaskForPid(1));

  // Finalize PID 1.
  FinalizePid(1, 100u);
  EXPECT_EQ(100u, broker_.TaskForPid(1));

  // Should be no entry for PID 2.
  EXPECT_EQ(0u, broker_.TaskForPid(2));
}

TEST_F(MachPortBrokerTest, FinalizeUnknownPid) {
  // Finalizing an entry for an unknown pid should not add it to the map.
  FinalizePid(1u, 100u);
  EXPECT_EQ(0u, broker_.TaskForPid(1u));
}

MULTIPROCESS_TEST_MAIN(MachPortBrokerTestChild) {
  CHECK(base::MachPortBroker::ChildSendTaskPortToParent(kBootstrapPortName));
  return 0;
}

TEST_F(MachPortBrokerTest, ReceivePortFromChild) {
  ASSERT_TRUE(broker_.Init());
  CommandLine command_line(
      base::GetMultiProcessTestChildBaseCommandLine());
  broker_.GetLock().Acquire();
  base::SpawnChildResult spawn_result = base::SpawnMultiProcessTestChild(
      "MachPortBrokerTestChild", command_line, LaunchOptions());
  broker_.AddPlaceholderForPid(spawn_result.process.Handle());
  broker_.GetLock().Release();

  WaitForTaskPort();
  EXPECT_EQ(spawn_result.process.Handle(), received_process_);

  int rv = -1;
  ASSERT_TRUE(spawn_result.process.WaitForExitWithTimeout(
      TestTimeouts::action_timeout(), &rv));
  EXPECT_EQ(0, rv);

  EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL),
            broker_.TaskForPid(spawn_result.process.Handle()));
}

TEST_F(MachPortBrokerTest, ReceivePortFromChildWithoutAdding) {
  ASSERT_TRUE(broker_.Init());
  CommandLine command_line(
      base::GetMultiProcessTestChildBaseCommandLine());
  broker_.GetLock().Acquire();
  base::SpawnChildResult spawn_result = base::SpawnMultiProcessTestChild(
      "MachPortBrokerTestChild", command_line, LaunchOptions());

  broker_.GetLock().Release();

  int rv = -1;
  ASSERT_TRUE(spawn_result.process.WaitForExitWithTimeout(
      TestTimeouts::action_timeout(), &rv));
  EXPECT_EQ(0, rv);

  EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL),
            broker_.TaskForPid(spawn_result.process.Handle()));
}

}  // namespace base