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

Side by Side Diff: test/win/win_multiprocess.cc

Issue 1151953002: win: Add WinMultiprocess for multiprocess Windows tests (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: fixes Created 5 years, 7 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
OLDNEW
(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
19 #include "base/logging.h"
20 #include "base/scoped_generic.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "gtest/gtest.h"
24 #include "util/stdlib/string_number_conversion.h"
25 #include "util/string/split_string.h"
26 #include "test/paths.h"
27
28 namespace crashpad {
29 namespace test {
30
31 namespace {
32
33 const char kIsMultiprocessChild[] = "--is-multiprocess-child";
34
35 struct LocalFreeTraits {
36 static HLOCAL InvalidValue() { return nullptr; }
37 static void Free(HLOCAL mem) {
38 if (LocalFree(mem) != nullptr)
39 PLOG(ERROR) << "LocalFree";
40 }
41 };
42
43 using ScopedLocalFree = base::ScopedGeneric<HLOCAL, LocalFreeTraits>;
44
45 bool GetSwitch(const char* switch_name, std::string* value) {
46 int num_args;
47 wchar_t** args = CommandLineToArgvW(GetCommandLine(), &num_args);
48 ScopedLocalFree scoped_args(args); // Take ownership.
49 if (!args) {
50 PLOG(ERROR) << "couldn't parse command line";
51 return false;
52 }
53
54 std::string switch_name_with_equals(switch_name);
55 switch_name_with_equals += "=";
56 for (size_t i = 1; i < num_args; ++i) {
57 const wchar_t* arg = args[i];
58 std::string arg_as_utf8 = base::UTF16ToUTF8(arg);
59 if (arg_as_utf8.compare(
60 0, switch_name_with_equals.size(), switch_name_with_equals) == 0) {
61 *value = arg_as_utf8.substr(switch_name_with_equals.size());
62 return true;
63 }
64 }
65
66 return false;
67 }
68
69 } // namespace
70
71 WinMultiprocess::WinMultiprocess() {
72 }
73
74 void WinMultiprocess::Run() {
75 std::string switch_value;
76 if (GetSwitch(kIsMultiprocessChild, &switch_value)) {
77 // If we're in the child, then set up the handles we inherited from the
78 // parent. These are inherited from the parent and so are open and have the
79 // same value as in the parent. The values are passed to the child on the
80 // command line.
81 std::string left, right;
82 ASSERT_TRUE(SplitString(switch_value, '|', &left, &right));
83 unsigned int c2p_write, p2c_read;
84 ASSERT_TRUE(StringToNumber(left, &c2p_write));
85 ASSERT_TRUE(StringToNumber(right, &p2c_read));
86 pipe_c2p_write_.reset(reinterpret_cast<HANDLE>(c2p_write));
87 pipe_p2c_read_.reset(reinterpret_cast<HANDLE>(p2c_read));
88
89 // Notify the parent that it's OK to proceed. We only need to wait to get to
90 // the process entry point, but this is the easiest place we can notify.
91 char c = ' ';
92 CheckedWriteFile(WritePipeHandle(), &c, sizeof(c));
93
94 // Invoke the child side of the test.
95 WinMultiprocessChild();
96 exit(0);
Robert Sesek 2015/05/26 15:48:13 Is it safe to use exit() and call exit-time-destru
scottmg 2015/05/26 18:44:28 This will be a completely separate unrelated proce
97 } else {
98 // If we're in the parent, make pipes for child-to-parent and
99 // parent-to-child communication. Mark them as inheritable via the
100 // SECURITY_ATTRIBUTES, but use SetHandleInformation to ensure that the
101 // parent sides are not inherited.
102 SECURITY_ATTRIBUTES security_attributes = {0};
103 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
104 security_attributes.bInheritHandle = true;
105
106 HANDLE c2p_read, c2p_write;
107 PCHECK(CreatePipe(&c2p_read, &c2p_write, &security_attributes, 0));
108 PCHECK(SetHandleInformation(c2p_read, HANDLE_FLAG_INHERIT, 0));
109 pipe_c2p_read_.reset(c2p_read);
110 pipe_c2p_write_.reset(c2p_write);
111
112 HANDLE p2c_read, p2c_write;
113 PCHECK(CreatePipe(&p2c_read, &p2c_write, &security_attributes, 0));
114 PCHECK(SetHandleInformation(p2c_write, HANDLE_FLAG_INHERIT, 0));
115 pipe_p2c_read_.reset(p2c_read);
116 pipe_p2c_write_.reset(p2c_write);
117
118 // Build a command line for the child process that tells it only to run the
119 // current test, and to pass down the values of the pipe handles.
120 const ::testing::TestInfo* const test_info =
121 ::testing::UnitTest::GetInstance()->current_test_info();
122 std::wstring command_line = Paths::Executable().value() + L" " +
123 base::UTF8ToUTF16(base::StringPrintf(
124 "--gtest_filter=%s.%s %s=0x%x|0x%x",
125 test_info->test_case_name(),
126 test_info->name(),
127 kIsMultiprocessChild,
128 c2p_write,
129 p2c_read));
130 STARTUPINFO startup_info = {0};
131 startup_info.cb = sizeof(startup_info);
132 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
133 startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
134 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
135 startup_info.dwFlags = STARTF_USESTDHANDLES;
136 PROCESS_INFORMATION process_info;
137 PCHECK(
138 CreateProcess(Paths::Executable().value().c_str(),
139 &command_line[0], // This cannot be constant, per MSDN.
140 nullptr,
141 nullptr,
142 true, // Inherit handles.
143 0,
144 nullptr,
145 nullptr,
146 &startup_info,
147 &process_info));
148 child_handle_.reset(process_info.hProcess);
149 CloseHandle(process_info.hThread);
150
151 // Block until the child process has launched. CreateProcess() returns
152 // immediately, and test code expects process initialization to have
153 // completed so it can, for example, use the process handle.
154 char c;
155 CheckedReadFile(pipe_c2p_read_.get(), &c, sizeof(c));
156 ASSERT_EQ(' ', c);
157
158 // These have been passed to the child, close our side.
159 pipe_c2p_write_.reset();
160 pipe_p2c_read_.reset();
161
162 WinMultiprocessParent();
163 }
Robert Sesek 2015/05/26 15:48:13 In the POSIX version, Multiprocess then waits for
scottmg 2015/05/26 18:44:29 Done, and added expectation+test for non-zero exit
164 }
165
166 WinMultiprocess::~WinMultiprocess() {
167 }
168
169 FileHandle WinMultiprocess::ReadPipeHandle() const {
170 FileHandle handle =
171 child_handle_.get() ? pipe_c2p_read_.get() : pipe_p2c_read_.get();
172 CHECK(handle != nullptr);
173 return handle;
174 }
175
176 FileHandle WinMultiprocess::WritePipeHandle() const {
177 FileHandle handle =
178 child_handle_.get() ? pipe_p2c_write_.get() : pipe_c2p_write_.get();
179 CHECK(handle != nullptr);
180 return handle;
181 }
182
183 void WinMultiprocess::CloseReadPipe() {
184 if (child_handle_.get())
185 pipe_c2p_read_.reset();
186 else
187 pipe_p2c_read_.reset();
188 }
189
190 void WinMultiprocess::CloseWritePipe() {
191 if (child_handle_.get())
192 pipe_p2c_write_.reset();
193 else
194 pipe_c2p_write_.reset();
195 }
196
197 HANDLE WinMultiprocess::ChildProcess() const {
198 EXPECT_NE(nullptr, child_handle_.get());
199 return child_handle_.get();
200 }
201
202 } // namespace test
203 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698