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
|
// Copyright (c) 2009 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 "content/browser/mach_broker_mac.h"
#include "base/synchronization/lock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
class MachBrokerTest : public testing::Test {
public:
// Helper function to acquire/release locks and call |PlaceholderForPid()|.
void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id) {
base::AutoLock lock(broker_.GetLock());
broker_.AddPlaceholderForPid(pid, child_process_id);
}
void InvalidateChildProcessId(int child_process_id) {
broker_.InvalidateChildProcessId(child_process_id);
}
int GetChildProcessCount(int child_process_id) {
return broker_.child_process_id_map_.count(child_process_id);
}
// 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);
}
protected:
MachBroker broker_;
};
TEST_F(MachBrokerTest, Locks) {
// Acquire and release the locks. Nothing bad should happen.
base::AutoLock lock(broker_.GetLock());
}
TEST_F(MachBrokerTest, AddPlaceholderAndFinalize) {
// Add a placeholder for PID 1.
AddPlaceholderForPid(1, 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(MachBrokerTest, InvalidateChildProcessId) {
// Add a placeholder for PID 1 and child process id 50.
AddPlaceholderForPid(1, 50);
FinalizePid(1, 100u);
EXPECT_EQ(100u, broker_.TaskForPid(1));
InvalidateChildProcessId(50);
EXPECT_EQ(0u, broker_.TaskForPid(1));
}
TEST_F(MachBrokerTest, ValidateChildProcessIdMap) {
// Add a placeholder for PID 1 and child process id 50.
AddPlaceholderForPid(1, 50);
FinalizePid(1, 100u);
EXPECT_EQ(1, GetChildProcessCount(50));
InvalidateChildProcessId(50);
EXPECT_EQ(0, GetChildProcessCount(50));
}
TEST_F(MachBrokerTest, FinalizeUnknownPid) {
// Finalizing an entry for an unknown pid should not add it to the map.
FinalizePid(1u, 100u);
EXPECT_EQ(0u, broker_.TaskForPid(1u));
}
} // namespace content
|