Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: content/browser/mach_broker_mac_unittest.cc

Issue 1755973002: Move non-content specific parts of content::MachBroker into base::MachPortBroker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove libbsm from content build rules. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/mach_broker_mac.mm ('k') | content/content_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/mach_broker_mac.h" 5 #include "content/browser/mach_broker_mac.h"
6 6
7 #include "base/command_line.h"
7 #include "base/synchronization/lock.h" 8 #include "base/synchronization/lock.h"
9 #include "base/test/multiprocess_test.h"
10 #include "base/test/test_timeouts.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
8 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/multiprocess_func_list.h"
9 14
10 namespace content { 15 namespace content {
11 16
12 class MachBrokerTest : public testing::Test { 17 class MachBrokerTest : public testing::Test {
13 public: 18 public:
14 // Helper function to acquire/release locks and call |PlaceholderForPid()|. 19 // Helper function to acquire/release locks and call |PlaceholderForPid()|.
15 void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id) { 20 void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id) {
16 base::AutoLock lock(broker_.GetLock()); 21 base::AutoLock lock(broker_.GetLock());
17 broker_.AddPlaceholderForPid(pid, child_process_id); 22 broker_.AddPlaceholderForPid(pid, child_process_id);
18 } 23 }
19 24
20 void InvalidateChildProcessId(int child_process_id) { 25 void InvalidateChildProcessId(int child_process_id) {
21 broker_.InvalidateChildProcessId(child_process_id); 26 broker_.InvalidateChildProcessId(child_process_id);
22 } 27 }
23 28
24 int GetChildProcessCount(int child_process_id) { 29 int GetChildProcessCount(int child_process_id) {
25 return broker_.child_process_id_map_.count(child_process_id); 30 return broker_.child_process_id_map_.count(child_process_id);
26 } 31 }
27 32
28 // Helper function to acquire/release locks and call |FinalizePid()|. 33 base::Process LaunchTestChild(const std::string& function,
29 void FinalizePid(base::ProcessHandle pid, 34 int child_process_id) {
30 mach_port_t task_port) {
31 base::AutoLock lock(broker_.GetLock()); 35 base::AutoLock lock(broker_.GetLock());
32 broker_.FinalizePid(pid, task_port); 36 base::Process test_child_process = base::SpawnMultiProcessTestChild(
37 function, base::GetMultiProcessTestChildBaseCommandLine(),
38 base::LaunchOptions());
39 broker_.AddPlaceholderForPid(test_child_process.Handle(), child_process_id);
40 return test_child_process;
41 }
42
43 void WaitForChildExit(base::Process& process) {
44 int rv = -1;
45 ASSERT_TRUE(process.WaitForExitWithTimeout(
46 TestTimeouts::action_timeout(), &rv));
47 EXPECT_EQ(0, rv);
33 } 48 }
34 49
35 protected: 50 protected:
36 MachBroker broker_; 51 MachBroker broker_;
52 TestBrowserThreadBundle thread_bundle_;
37 }; 53 };
38 54
55 MULTIPROCESS_TEST_MAIN(MachBrokerTestChild) {
56 CHECK(MachBroker::ChildSendTaskPortToParent());
57 return 0;
58 }
59
39 TEST_F(MachBrokerTest, Locks) { 60 TEST_F(MachBrokerTest, Locks) {
40 // Acquire and release the locks. Nothing bad should happen. 61 // Acquire and release the locks. Nothing bad should happen.
41 base::AutoLock lock(broker_.GetLock()); 62 base::AutoLock lock(broker_.GetLock());
42 } 63 }
43 64
44 TEST_F(MachBrokerTest, AddPlaceholderAndFinalize) { 65 TEST_F(MachBrokerTest, AddChildProcess) {
45 // Add a placeholder for PID 1. 66 {
46 AddPlaceholderForPid(1, 1); 67 base::AutoLock lock(broker_.GetLock());
47 EXPECT_EQ(0u, broker_.TaskForPid(1)); 68 broker_.EnsureRunning();
69 }
70 base::Process child_process = LaunchTestChild("MachBrokerTestChild", 7);
71 WaitForChildExit(child_process);
48 72
49 // Finalize PID 1. 73 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL),
50 FinalizePid(1, 100u); 74 broker_.TaskForPid(child_process.Handle()));
51 EXPECT_EQ(100u, broker_.TaskForPid(1)); 75 EXPECT_EQ(1, GetChildProcessCount(7));
52 76
53 // Should be no entry for PID 2. 77 // Should be no entry for any other PID.
54 EXPECT_EQ(0u, broker_.TaskForPid(2)); 78 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL),
55 } 79 broker_.TaskForPid(child_process.Handle() + 1));
56 80
57 TEST_F(MachBrokerTest, InvalidateChildProcessId) { 81 InvalidateChildProcessId(7);
58 // Add a placeholder for PID 1 and child process id 50. 82 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL),
59 AddPlaceholderForPid(1, 50); 83 broker_.TaskForPid(child_process.Handle()));
60 FinalizePid(1, 100u); 84 EXPECT_EQ(0, GetChildProcessCount(7));
61
62 EXPECT_EQ(100u, broker_.TaskForPid(1));
63 InvalidateChildProcessId(50);
64 EXPECT_EQ(0u, broker_.TaskForPid(1));
65 }
66
67 TEST_F(MachBrokerTest, ValidateChildProcessIdMap) {
68 // Add a placeholder for PID 1 and child process id 50.
69 AddPlaceholderForPid(1, 50);
70 FinalizePid(1, 100u);
71
72 EXPECT_EQ(1, GetChildProcessCount(50));
73 InvalidateChildProcessId(50);
74 EXPECT_EQ(0, GetChildProcessCount(50));
75 }
76
77 TEST_F(MachBrokerTest, FinalizeUnknownPid) {
78 // Finalizing an entry for an unknown pid should not add it to the map.
79 FinalizePid(1u, 100u);
80 EXPECT_EQ(0u, broker_.TaskForPid(1u));
81 } 85 }
82 86
83 } // namespace content 87 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/mach_broker_mac.mm ('k') | content/content_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698