1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/mach_broker_mac.h"
6
7 #include "base/synchronization/lock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace content {
11
12 class MachBrokerTest : public testing::Test {
13 public:
14 // Helper function to acquire/release locks and call |PlaceholderForPid()|.
AddPlaceholderForPid(base::ProcessHandle pid)15 void AddPlaceholderForPid(base::ProcessHandle pid) {
16 base::AutoLock lock(broker_.GetLock());
17 broker_.AddPlaceholderForPid(pid);
18 }
19
InvalidatePid(base::ProcessHandle pid)20 void InvalidatePid(base::ProcessHandle pid) {
21 broker_.InvalidatePid(pid);
22 }
23
24 // Helper function to acquire/release locks and call |FinalizePid()|.
FinalizePid(base::ProcessHandle pid,mach_port_t task_port)25 void FinalizePid(base::ProcessHandle pid,
26 mach_port_t task_port) {
27 base::AutoLock lock(broker_.GetLock());
28 broker_.FinalizePid(pid, task_port);
29 }
30
31 protected:
32 MachBroker broker_;
33 };
34
TEST_F(MachBrokerTest,Locks)35 TEST_F(MachBrokerTest, Locks) {
36 // Acquire and release the locks. Nothing bad should happen.
37 base::AutoLock lock(broker_.GetLock());
38 }
39
TEST_F(MachBrokerTest,AddPlaceholderAndFinalize)40 TEST_F(MachBrokerTest, AddPlaceholderAndFinalize) {
41 // Add a placeholder for PID 1.
42 AddPlaceholderForPid(1);
43 EXPECT_EQ(0u, broker_.TaskForPid(1));
44
45 // Finalize PID 1.
46 FinalizePid(1, 100u);
47 EXPECT_EQ(100u, broker_.TaskForPid(1));
48
49 // Should be no entry for PID 2.
50 EXPECT_EQ(0u, broker_.TaskForPid(2));
51 }
52
TEST_F(MachBrokerTest,Invalidate)53 TEST_F(MachBrokerTest, Invalidate) {
54 AddPlaceholderForPid(1);
55 FinalizePid(1, 100u);
56
57 EXPECT_EQ(100u, broker_.TaskForPid(1));
58 InvalidatePid(1u);
59 EXPECT_EQ(0u, broker_.TaskForPid(1));
60 }
61
TEST_F(MachBrokerTest,FinalizeUnknownPid)62 TEST_F(MachBrokerTest, FinalizeUnknownPid) {
63 // Finalizing an entry for an unknown pid should not add it to the map.
64 FinalizePid(1u, 100u);
65 EXPECT_EQ(0u, broker_.TaskForPid(1u));
66 }
67
68 } // namespace content
69