OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "test/win/win_multiprocess.h" | |
16 | |
17 #include <shellapi.h> | |
18 #include <stdlib.h> | |
19 | |
20 #include "base/logging.h" | |
21 #include "base/scoped_generic.h" | |
22 #include "base/strings/stringprintf.h" | |
23 #include "base/strings/utf_string_conversions.h" | |
24 #include "gtest/gtest.h" | |
25 #include "util/stdlib/string_number_conversion.h" | |
26 #include "util/string/split_string.h" | |
27 #include "test/paths.h" | |
28 | |
29 namespace crashpad { | |
30 namespace test { | |
31 | |
32 namespace { | |
33 | |
34 const char kIsMultiprocessChild[] = "--is-multiprocess-child"; | |
35 | |
36 struct LocalFreeTraits { | |
37 static HLOCAL InvalidValue() { return nullptr; } | |
38 static void Free(HLOCAL mem) { | |
39 if (LocalFree(mem) != nullptr) | |
40 PLOG(ERROR) << "LocalFree"; | |
41 } | |
42 }; | |
43 | |
44 using ScopedLocalFree = base::ScopedGeneric<HLOCAL, LocalFreeTraits>; | |
45 | |
46 bool GetSwitch(const char* switch_name, std::string* value) { | |
47 int num_args; | |
48 wchar_t** args = CommandLineToArgvW(GetCommandLine(), &num_args); | |
49 ScopedLocalFree scoped_args(args); // Take ownership. | |
50 if (!args) { | |
51 PLOG(ERROR) << "couldn't parse command line"; | |
52 return false; | |
53 } | |
54 | |
55 std::string switch_name_with_equals(switch_name); | |
56 switch_name_with_equals += "="; | |
57 for (size_t i = 1; i < num_args; ++i) { | |
58 const wchar_t* arg = args[i]; | |
59 std::string arg_as_utf8 = base::UTF16ToUTF8(arg); | |
60 if (arg_as_utf8.compare( | |
61 0, switch_name_with_equals.size(), switch_name_with_equals) == 0) { | |
62 *value = arg_as_utf8.substr(switch_name_with_equals.size()); | |
63 return true; | |
64 } | |
65 } | |
66 | |
67 return false; | |
68 } | |
69 | |
70 } // namespace | |
71 | |
72 WinMultiprocess::WinMultiprocess() | |
73 : pipe_c2p_read_(), | |
74 pipe_c2p_write_(), | |
75 pipe_p2c_read_(), | |
76 pipe_p2c_write_(), | |
Robert Sesek
2015/05/28 15:27:57
child_handle_(),
scottmg
2015/05/28 15:38:19
Done.
| |
77 exit_code_(EXIT_SUCCESS) { | |
78 } | |
79 | |
80 void WinMultiprocess::Run() { | |
81 std::string switch_value; | |
82 if (GetSwitch(kIsMultiprocessChild, &switch_value)) { | |
83 // If we're in the child, then set up the handles we inherited from the | |
84 // parent. These are inherited from the parent and so are open and have the | |
85 // same value as in the parent. The values are passed to the child on the | |
86 // command line. | |
87 std::string left, right; | |
88 ASSERT_TRUE(SplitString(switch_value, '|', &left, &right)); | |
89 unsigned int c2p_write, p2c_read; | |
90 ASSERT_TRUE(StringToNumber(left, &c2p_write)); | |
91 ASSERT_TRUE(StringToNumber(right, &p2c_read)); | |
92 pipe_c2p_write_.reset(reinterpret_cast<HANDLE>(c2p_write)); | |
93 pipe_p2c_read_.reset(reinterpret_cast<HANDLE>(p2c_read)); | |
94 | |
95 // Notify the parent that it's OK to proceed. We only need to wait to get to | |
96 // the process entry point, but this is the easiest place we can notify. | |
97 char c = ' '; | |
98 CheckedWriteFile(WritePipeHandle(), &c, sizeof(c)); | |
99 | |
100 // Invoke the child side of the test. | |
101 WinMultiprocessChild(); | |
102 exit(0); | |
103 } else { | |
104 // If we're in the parent, make pipes for child-to-parent and | |
105 // parent-to-child communication. Mark them as inheritable via the | |
106 // SECURITY_ATTRIBUTES, but use SetHandleInformation to ensure that the | |
107 // parent sides are not inherited. | |
108 SECURITY_ATTRIBUTES security_attributes = {0}; | |
109 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); | |
110 security_attributes.bInheritHandle = true; | |
111 | |
112 HANDLE c2p_read, c2p_write; | |
113 PCHECK(CreatePipe(&c2p_read, &c2p_write, &security_attributes, 0)); | |
114 PCHECK(SetHandleInformation(c2p_read, HANDLE_FLAG_INHERIT, 0)); | |
115 pipe_c2p_read_.reset(c2p_read); | |
116 pipe_c2p_write_.reset(c2p_write); | |
117 | |
118 HANDLE p2c_read, p2c_write; | |
119 PCHECK(CreatePipe(&p2c_read, &p2c_write, &security_attributes, 0)); | |
120 PCHECK(SetHandleInformation(p2c_write, HANDLE_FLAG_INHERIT, 0)); | |
121 pipe_p2c_read_.reset(p2c_read); | |
122 pipe_p2c_write_.reset(p2c_write); | |
123 | |
124 // Build a command line for the child process that tells it only to run the | |
125 // current test, and to pass down the values of the pipe handles. | |
126 const ::testing::TestInfo* const test_info = | |
127 ::testing::UnitTest::GetInstance()->current_test_info(); | |
128 std::wstring command_line = Paths::Executable().value() + L" " + | |
129 base::UTF8ToUTF16(base::StringPrintf( | |
130 "--gtest_filter=%s.%s %s=0x%x|0x%x", | |
131 test_info->test_case_name(), | |
132 test_info->name(), | |
133 kIsMultiprocessChild, | |
134 c2p_write, | |
135 p2c_read)); | |
136 STARTUPINFO startup_info = {0}; | |
137 startup_info.cb = sizeof(startup_info); | |
138 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | |
139 startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | |
140 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); | |
141 startup_info.dwFlags = STARTF_USESTDHANDLES; | |
142 PROCESS_INFORMATION process_info; | |
143 PCHECK( | |
144 CreateProcess(Paths::Executable().value().c_str(), | |
145 &command_line[0], // This cannot be constant, per MSDN. | |
146 nullptr, | |
147 nullptr, | |
148 true, // Inherit handles. | |
149 0, | |
150 nullptr, | |
151 nullptr, | |
152 &startup_info, | |
153 &process_info)); | |
154 child_handle_.reset(process_info.hProcess); | |
155 CloseHandle(process_info.hThread); | |
156 | |
157 // Block until the child process has launched. CreateProcess() returns | |
158 // immediately, and test code expects process initialization to have | |
159 // completed so it can, for example, use the process handle. | |
160 char c; | |
161 CheckedReadFile(pipe_c2p_read_.get(), &c, sizeof(c)); | |
162 ASSERT_EQ(' ', c); | |
163 | |
164 // These have been passed to the child, close our side. | |
165 pipe_c2p_write_.reset(); | |
166 pipe_p2c_read_.reset(); | |
167 | |
168 WinMultiprocessParent(); | |
169 | |
170 // Wait for the child to complete. | |
171 ASSERT_EQ(WAIT_OBJECT_0, | |
172 WaitForSingleObject(child_handle_.get(), INFINITE)); | |
173 | |
174 DWORD exit_code; | |
175 ASSERT_TRUE(GetExitCodeProcess(child_handle_.get(), &exit_code)); | |
176 if (exit_code != exit_code_) | |
Robert Sesek
2015/05/28 15:27:57
Why not ASSERT_EQ(exit_code_, exit_code) ?
The PO
scottmg
2015/05/28 15:38:19
Done.
| |
177 ADD_FAILURE() << base::StringPrintf("exit with code %d", exit_code); | |
178 } | |
179 } | |
180 | |
181 void WinMultiprocess::SetExpectedChildExitCode(unsigned int exit_code) { | |
182 exit_code_ = exit_code; | |
183 } | |
184 | |
185 WinMultiprocess::~WinMultiprocess() { | |
186 } | |
187 | |
188 FileHandle WinMultiprocess::ReadPipeHandle() const { | |
189 FileHandle handle = | |
190 child_handle_.get() ? pipe_c2p_read_.get() : pipe_p2c_read_.get(); | |
191 CHECK(handle != nullptr); | |
192 return handle; | |
193 } | |
194 | |
195 FileHandle WinMultiprocess::WritePipeHandle() const { | |
196 FileHandle handle = | |
197 child_handle_.get() ? pipe_p2c_write_.get() : pipe_c2p_write_.get(); | |
198 CHECK(handle != nullptr); | |
199 return handle; | |
200 } | |
201 | |
202 void WinMultiprocess::CloseReadPipe() { | |
203 if (child_handle_.get()) | |
204 pipe_c2p_read_.reset(); | |
205 else | |
206 pipe_p2c_read_.reset(); | |
207 } | |
208 | |
209 void WinMultiprocess::CloseWritePipe() { | |
210 if (child_handle_.get()) | |
211 pipe_p2c_write_.reset(); | |
212 else | |
213 pipe_c2p_write_.reset(); | |
214 } | |
215 | |
216 HANDLE WinMultiprocess::ChildProcess() const { | |
217 EXPECT_NE(nullptr, child_handle_.get()); | |
218 return child_handle_.get(); | |
219 } | |
220 | |
221 } // namespace test | |
222 } // namespace crashpad | |
OLD | NEW |