OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/edk/test/multiprocess_test_helper.h" | 5 #include "mojo/edk/test/multiprocess_test_helper.h" |
6 | 6 |
7 #include <functional> | 7 #include <functional> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
| 11 #include "base/base_paths.h" |
11 #include "base/base_switches.h" | 12 #include "base/base_switches.h" |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/command_line.h" | 14 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/path_service.h" |
17 #include "base/process/kill.h" | 19 #include "base/process/kill.h" |
18 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
19 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
20 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
21 #include "base/task_runner.h" | 23 #include "base/task_runner.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
23 #include "build/build_config.h" | 25 #include "build/build_config.h" |
24 #include "mojo/edk/embedder/embedder.h" | 26 #include "mojo/edk/embedder/embedder.h" |
| 27 #include "mojo/edk/embedder/named_platform_handle.h" |
| 28 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
25 #include "mojo/edk/embedder/platform_channel_pair.h" | 29 #include "mojo/edk/embedder/platform_channel_pair.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
27 | 31 |
28 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
29 #include "base/win/windows_version.h" | 33 #include "base/win/windows_version.h" |
30 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 34 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
31 #include "base/mac/mach_port_broker.h" | 35 #include "base/mac/mach_port_broker.h" |
32 #endif | 36 #endif |
33 | 37 |
34 namespace mojo { | 38 namespace mojo { |
35 namespace edk { | 39 namespace edk { |
36 namespace test { | 40 namespace test { |
37 | 41 |
38 namespace { | 42 namespace { |
39 | 43 |
40 const char kMojoPrimordialPipeToken[] = "mojo-primordial-pipe-token"; | 44 const char kMojoPrimordialPipeToken[] = "mojo-primordial-pipe-token"; |
| 45 const char kMojoNamedPipeName[] = "mojo-named-pipe-name"; |
41 | 46 |
42 template <typename Func> | 47 template <typename Func> |
43 int RunClientFunction(Func handler) { | 48 int RunClientFunction(Func handler) { |
44 CHECK(MultiprocessTestHelper::primordial_pipe.is_valid()); | 49 CHECK(MultiprocessTestHelper::primordial_pipe.is_valid()); |
45 ScopedMessagePipeHandle pipe = | 50 ScopedMessagePipeHandle pipe = |
46 std::move(MultiprocessTestHelper::primordial_pipe); | 51 std::move(MultiprocessTestHelper::primordial_pipe); |
47 return handler(pipe.get().value()); | 52 return handler(pipe.get().value()); |
48 } | 53 } |
49 | 54 |
50 } // namespace | 55 } // namespace |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // Copy commandline switches from the parent process, except for the | 89 // Copy commandline switches from the parent process, except for the |
85 // multiprocess client name and mojo message pipe handle; this allows test | 90 // multiprocess client name and mojo message pipe handle; this allows test |
86 // clients to spawn other test clients. | 91 // clients to spawn other test clients. |
87 for (const auto& entry : | 92 for (const auto& entry : |
88 base::CommandLine::ForCurrentProcess()->GetSwitches()) { | 93 base::CommandLine::ForCurrentProcess()->GetSwitches()) { |
89 if (uninherited_args.find(entry.first) == uninherited_args.end()) | 94 if (uninherited_args.find(entry.first) == uninherited_args.end()) |
90 command_line.AppendSwitchNative(entry.first, entry.second); | 95 command_line.AppendSwitchNative(entry.first, entry.second); |
91 } | 96 } |
92 | 97 |
93 PlatformChannelPair channel; | 98 PlatformChannelPair channel; |
| 99 NamedPlatformHandle named_pipe; |
94 HandlePassingInformation handle_passing_info; | 100 HandlePassingInformation handle_passing_info; |
95 channel.PrepareToPassClientHandleToChildProcess(&command_line, | 101 if (launch_type == LaunchType::CHILD || launch_type == LaunchType::PEER) { |
96 &handle_passing_info); | 102 channel.PrepareToPassClientHandleToChildProcess(&command_line, |
| 103 &handle_passing_info); |
| 104 } else if (launch_type == LaunchType::NAMED_CHILD || |
| 105 launch_type == LaunchType::NAMED_PEER) { |
| 106 #if defined(OS_POSIX) |
| 107 base::FilePath temp_dir; |
| 108 CHECK(base::PathService::Get(base::DIR_TEMP, &temp_dir)); |
| 109 named_pipe = NamedPlatformHandle( |
| 110 temp_dir.AppendASCII(GenerateRandomToken()).value()); |
| 111 #else |
| 112 named_pipe = NamedPlatformHandle(GenerateRandomToken()); |
| 113 #endif |
| 114 command_line.AppendSwitchNative(kMojoNamedPipeName, named_pipe.name); |
| 115 } |
97 | 116 |
98 std::string pipe_token = mojo::edk::GenerateRandomToken(); | 117 std::string pipe_token = mojo::edk::GenerateRandomToken(); |
99 if (launch_type == LaunchType::CHILD) | 118 if (launch_type == LaunchType::CHILD || |
| 119 launch_type == LaunchType::NAMED_CHILD) |
100 command_line.AppendSwitchASCII(kMojoPrimordialPipeToken, pipe_token); | 120 command_line.AppendSwitchASCII(kMojoPrimordialPipeToken, pipe_token); |
101 | 121 |
102 if (!switch_string.empty()) { | 122 if (!switch_string.empty()) { |
103 CHECK(!command_line.HasSwitch(switch_string)); | 123 CHECK(!command_line.HasSwitch(switch_string)); |
104 if (!switch_value.empty()) | 124 if (!switch_value.empty()) |
105 command_line.AppendSwitchASCII(switch_string, switch_value); | 125 command_line.AppendSwitchASCII(switch_string, switch_value); |
106 else | 126 else |
107 command_line.AppendSwitch(switch_string); | 127 command_line.AppendSwitch(switch_string); |
108 } | 128 } |
109 | 129 |
110 base::LaunchOptions options; | 130 base::LaunchOptions options; |
111 #if defined(OS_POSIX) | 131 #if defined(OS_POSIX) |
112 options.fds_to_remap = &handle_passing_info; | 132 options.fds_to_remap = &handle_passing_info; |
113 #elif defined(OS_WIN) | 133 #elif defined(OS_WIN) |
114 options.start_hidden = true; | 134 options.start_hidden = true; |
115 if (base::win::GetVersion() >= base::win::VERSION_VISTA) | 135 if (base::win::GetVersion() >= base::win::VERSION_VISTA) |
116 options.handles_to_inherit = &handle_passing_info; | 136 options.handles_to_inherit = &handle_passing_info; |
117 else | 137 else |
118 options.inherit_handles = true; | 138 options.inherit_handles = true; |
119 #else | 139 #else |
120 #error "Not supported yet." | 140 #error "Not supported yet." |
121 #endif | 141 #endif |
122 | 142 |
123 ScopedMessagePipeHandle pipe; | 143 ScopedMessagePipeHandle pipe; |
124 std::string child_token = mojo::edk::GenerateRandomToken(); | 144 std::string child_token = mojo::edk::GenerateRandomToken(); |
125 if (launch_type == LaunchType::CHILD) | 145 if (launch_type == LaunchType::CHILD || |
| 146 launch_type == LaunchType::NAMED_CHILD) { |
126 pipe = CreateParentMessagePipe(pipe_token, child_token); | 147 pipe = CreateParentMessagePipe(pipe_token, child_token); |
127 else if (launch_type == LaunchType::PEER) | 148 } else if (launch_type == LaunchType::PEER) { |
128 pipe = ConnectToPeerProcess(channel.PassServerHandle()); | 149 pipe = ConnectToPeerProcess(channel.PassServerHandle()); |
| 150 } else if (launch_type == LaunchType::NAMED_PEER) { |
| 151 pipe = ConnectToPeerProcess(CreateServerHandle(named_pipe, false)); |
| 152 } |
129 | 153 |
130 test_child_ = | 154 test_child_ = |
131 base::SpawnMultiProcessTestChild(test_child_main, command_line, options); | 155 base::SpawnMultiProcessTestChild(test_child_main, command_line, options); |
132 channel.ChildProcessLaunched(); | 156 if (launch_type == LaunchType::CHILD || launch_type == LaunchType::PEER) |
| 157 channel.ChildProcessLaunched(); |
133 | 158 |
134 if (launch_type == LaunchType::CHILD) { | 159 if (launch_type == LaunchType::CHILD) { |
135 ChildProcessLaunched(test_child_.Handle(), channel.PassServerHandle(), | 160 ChildProcessLaunched(test_child_.Handle(), channel.PassServerHandle(), |
136 child_token, process_error_callback_); | 161 child_token, process_error_callback_); |
| 162 } else if (launch_type == LaunchType::NAMED_CHILD) { |
| 163 ChildProcessLaunched(test_child_.Handle(), |
| 164 CreateServerHandle(named_pipe, false), child_token, |
| 165 process_error_callback_); |
137 } | 166 } |
138 | 167 |
139 CHECK(test_child_.IsValid()); | 168 CHECK(test_child_.IsValid()); |
140 return pipe; | 169 return pipe; |
141 } | 170 } |
142 | 171 |
143 int MultiprocessTestHelper::WaitForChildShutdown() { | 172 int MultiprocessTestHelper::WaitForChildShutdown() { |
144 CHECK(test_child_.IsValid()); | 173 CHECK(test_child_.IsValid()); |
145 | 174 |
146 int rv = -1; | 175 int rv = -1; |
(...skipping 13 matching lines...) Expand all Loading... |
160 return WaitForChildShutdown() == 0; | 189 return WaitForChildShutdown() == 0; |
161 } | 190 } |
162 | 191 |
163 // static | 192 // static |
164 void MultiprocessTestHelper::ChildSetup() { | 193 void MultiprocessTestHelper::ChildSetup() { |
165 CHECK(base::CommandLine::InitializedForCurrentProcess()); | 194 CHECK(base::CommandLine::InitializedForCurrentProcess()); |
166 | 195 |
167 std::string primordial_pipe_token = | 196 std::string primordial_pipe_token = |
168 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 197 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
169 kMojoPrimordialPipeToken); | 198 kMojoPrimordialPipeToken); |
| 199 NamedPlatformHandle named_pipe( |
| 200 base::CommandLine::ForCurrentProcess()->GetSwitchValueNative( |
| 201 kMojoNamedPipeName)); |
170 if (!primordial_pipe_token.empty()) { | 202 if (!primordial_pipe_token.empty()) { |
171 primordial_pipe = CreateChildMessagePipe(primordial_pipe_token); | 203 primordial_pipe = CreateChildMessagePipe(primordial_pipe_token); |
172 #if defined(OS_MACOSX) && !defined(OS_IOS) | 204 #if defined(OS_MACOSX) && !defined(OS_IOS) |
173 CHECK(base::MachPortBroker::ChildSendTaskPortToParent("mojo_test")); | 205 CHECK(base::MachPortBroker::ChildSendTaskPortToParent("mojo_test")); |
174 #endif | 206 #endif |
175 SetParentPipeHandle(PlatformChannelPair::PassClientHandleFromParentProcess( | 207 if (named_pipe.is_valid()) { |
176 *base::CommandLine::ForCurrentProcess())); | 208 SetParentPipeHandle(CreateClientHandle(named_pipe)); |
| 209 } else { |
| 210 SetParentPipeHandle( |
| 211 PlatformChannelPair::PassClientHandleFromParentProcess( |
| 212 *base::CommandLine::ForCurrentProcess())); |
| 213 } |
177 } else { | 214 } else { |
178 primordial_pipe = ConnectToPeerProcess( | 215 if (named_pipe.is_valid()) { |
179 PlatformChannelPair::PassClientHandleFromParentProcess( | 216 primordial_pipe = ConnectToPeerProcess(CreateClientHandle(named_pipe)); |
180 *base::CommandLine::ForCurrentProcess())); | 217 } else { |
| 218 primordial_pipe = ConnectToPeerProcess( |
| 219 PlatformChannelPair::PassClientHandleFromParentProcess( |
| 220 *base::CommandLine::ForCurrentProcess())); |
| 221 } |
181 } | 222 } |
182 } | 223 } |
183 | 224 |
184 // static | 225 // static |
185 int MultiprocessTestHelper::RunClientMain( | 226 int MultiprocessTestHelper::RunClientMain( |
186 const base::Callback<int(MojoHandle)>& main) { | 227 const base::Callback<int(MojoHandle)>& main) { |
187 return RunClientFunction([main](MojoHandle handle){ | 228 return RunClientFunction([main](MojoHandle handle){ |
188 return main.Run(handle); | 229 return main.Run(handle); |
189 }); | 230 }); |
190 } | 231 } |
191 | 232 |
192 // static | 233 // static |
193 int MultiprocessTestHelper::RunClientTestMain( | 234 int MultiprocessTestHelper::RunClientTestMain( |
194 const base::Callback<void(MojoHandle)>& main) { | 235 const base::Callback<void(MojoHandle)>& main) { |
195 return RunClientFunction([main](MojoHandle handle) { | 236 return RunClientFunction([main](MojoHandle handle) { |
196 main.Run(handle); | 237 main.Run(handle); |
197 return (::testing::Test::HasFatalFailure() || | 238 return (::testing::Test::HasFatalFailure() || |
198 ::testing::Test::HasNonfatalFailure()) ? 1 : 0; | 239 ::testing::Test::HasNonfatalFailure()) ? 1 : 0; |
199 }); | 240 }); |
200 } | 241 } |
201 | 242 |
202 // static | 243 // static |
203 mojo::ScopedMessagePipeHandle MultiprocessTestHelper::primordial_pipe; | 244 mojo::ScopedMessagePipeHandle MultiprocessTestHelper::primordial_pipe; |
204 | 245 |
205 } // namespace test | 246 } // namespace test |
206 } // namespace edk | 247 } // namespace edk |
207 } // namespace mojo | 248 } // namespace mojo |
OLD | NEW |