[email protected] | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 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 | |
[email protected] | ed0fbe6 | 2011-06-23 18:32:11 | [diff] [blame] | 5 | #include "content/browser/mach_broker_mac.h" |
[email protected] | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 6 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 7 | #include "base/synchronization/lock.h" |
[email protected] | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 10 | namespace content { |
| 11 | |
[email protected] | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 12 | class MachBrokerTest : public testing::Test { |
| 13 | public: |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 14 | // Helper function to acquire/release locks and call |PlaceholderForPid()|. |
| 15 | void AddPlaceholderForPid(base::ProcessHandle pid) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 16 | base::AutoLock lock(broker_.GetLock()); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 17 | broker_.AddPlaceholderForPid(pid); |
| 18 | } |
| 19 | |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 20 | void InvalidatePid(base::ProcessHandle pid) { |
| 21 | broker_.InvalidatePid(pid); |
| 22 | } |
| 23 | |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 24 | // Helper function to acquire/release locks and call |FinalizePid()|. |
| 25 | void FinalizePid(base::ProcessHandle pid, |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 26 | mach_port_t task_port) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 27 | base::AutoLock lock(broker_.GetLock()); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 28 | broker_.FinalizePid(pid, task_port); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | protected: |
[email protected] | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 32 | MachBroker broker_; |
| 33 | }; |
| 34 | |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 35 | TEST_F(MachBrokerTest, Locks) { |
| 36 | // Acquire and release the locks. Nothing bad should happen. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 37 | base::AutoLock lock(broker_.GetLock()); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 38 | } |
| 39 | |
| 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. |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 46 | FinalizePid(1, 100u); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 47 | EXPECT_EQ(100u, broker_.TaskForPid(1)); |
| 48 | |
| 49 | // Should be no entry for PID 2. |
[email protected] | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 50 | EXPECT_EQ(0u, broker_.TaskForPid(2)); |
| 51 | } |
| 52 | |
| 53 | TEST_F(MachBrokerTest, Invalidate) { |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 54 | AddPlaceholderForPid(1); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 55 | FinalizePid(1, 100u); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 56 | |
| 57 | EXPECT_EQ(100u, broker_.TaskForPid(1)); |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 58 | InvalidatePid(1u); |
[email protected] | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 59 | EXPECT_EQ(0u, broker_.TaskForPid(1)); |
| 60 | } |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 61 | |
| 62 | TEST_F(MachBrokerTest, FinalizeUnknownPid) { |
| 63 | // Finalizing an entry for an unknown pid should not add it to the map. |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 64 | FinalizePid(1u, 100u); |
[email protected] | b88a749 | 2010-09-17 12:28:32 | [diff] [blame] | 65 | EXPECT_EQ(0u, broker_.TaskForPid(1u)); |
| 66 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 67 | |
| 68 | } // namespace content |