Chromium Code Reviews| Index: test/win/win_multiprocess.cc |
| diff --git a/test/win/win_multiprocess.cc b/test/win/win_multiprocess.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2e9df1b2926c5e77bae4c6902e4d3f0bffdc9304 |
| --- /dev/null |
| +++ b/test/win/win_multiprocess.cc |
| @@ -0,0 +1,188 @@ |
| +// Copyright 2015 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include "test/win/win_multiprocess.h" |
| + |
| +#include <shellapi.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "gtest/gtest.h" |
| +#include "util/string/split_string.h" |
| +#include "test/paths.h" |
| + |
| +namespace crashpad { |
| +namespace test { |
| + |
| +namespace { |
| + |
| +const char kIsMultiprocessChild[] = "--is-multiprocess-child"; |
| + |
| +bool GetSwitch(const char* switch_name, std::string* value) { |
| + int num_args; |
| + wchar_t** args = CommandLineToArgvW(GetCommandLine(), &num_args); |
| + if (!args) { |
|
cpu_(ooo_6.6-7.5)
2015/05/22 16:15:23
needs a localfree(args)
alternatively we can do s
scottmg
2015/05/25 19:04:57
Since this is test code only, I will stick to free
|
| + PLOG(ERROR) << "couldn't parse command line"; |
| + return false; |
| + } |
| + |
| + std::string switch_name_with_equals(switch_name); |
| + switch_name_with_equals += "="; |
| + for (size_t i = 1; i < num_args; ++i) { |
| + const wchar_t* arg = args[i]; |
| + std::string arg_as_utf8 = base::UTF16ToUTF8(arg); |
| + if (arg_as_utf8.compare( |
| + 0, switch_name_with_equals.size(), switch_name_with_equals) == 0) { |
| + *value = arg_as_utf8.substr(switch_name_with_equals.size()); |
| + return true; |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +WinMultiprocess::WinMultiprocess() { |
| +} |
| + |
| +void WinMultiprocess::Run() { |
| + std::string switch_value; |
| + if (GetSwitch(kIsMultiprocessChild, &switch_value)) { |
| + // If we're in the child, then set up the handles we inherited from the |
| + // parent. These are inherited from the parent and so are open and have the |
| + // same value as in the parent. The values are passed to the child on the |
| + // command line. |
| + unsigned int c2p_write, p2c_read; |
| + sscanf(switch_value.c_str(), "%x|%x", &c2p_write, &p2c_read); |
|
cpu_(ooo_6.6-7.5)
2015/05/22 16:15:23
the way brettw made me do these things is with Spl
scottmg
2015/05/25 19:04:56
Probably better, done. (I thought I originally I w
|
| + pipe_c2p_write_.reset(reinterpret_cast<HANDLE>(c2p_write)); |
| + pipe_p2c_read_.reset(reinterpret_cast<HANDLE>(p2c_read)); |
| + |
| + // Notify the parent that it's OK to proceed. We only need to wait to get to |
| + // the process entry point, but this is the easiest place we can notify. |
| + char c = ' '; |
| + CheckedWriteFile(WritePipeHandle(), &c, sizeof(c)); |
| + |
| + // Invoke the child side of the test. |
| + WinMultiprocessChild(); |
| + exit(0); |
| + } else { |
| + // If we're in the parent, make pipes for child-to-parent and |
| + // parent-to-child communication. Mark them as inheritable via the |
| + // SECURITY_ATTRIBUTES, but use SetHandleInformation to ensure that the |
| + // parent sides are not inherited. |
| + SECURITY_ATTRIBUTES security_attributes = {0}; |
| + security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); |
| + security_attributes.bInheritHandle = true; |
| + |
| + HANDLE c2p_read, c2p_write; |
| + PCHECK(CreatePipe(&c2p_read, &c2p_write, &security_attributes, 0)); |
| + PCHECK(SetHandleInformation(c2p_read, HANDLE_FLAG_INHERIT, 0)); |
| + pipe_c2p_read_.reset(c2p_read); |
| + pipe_c2p_write_.reset(c2p_write); |
| + |
| + HANDLE p2c_read, p2c_write; |
| + PCHECK(CreatePipe(&p2c_read, &p2c_write, &security_attributes, 0)); |
| + PCHECK(SetHandleInformation(p2c_write, HANDLE_FLAG_INHERIT, 0)); |
| + pipe_p2c_read_.reset(p2c_read); |
| + pipe_p2c_write_.reset(p2c_write); |
| + |
| + // Build a command line for the child process that tells it only to run the |
| + // current test, and to pass down the values of the pipe handles. |
| + std::wstring command_line; |
|
cpu_(ooo_6.6-7.5)
2015/05/22 16:15:23
104 can be in 107
scottmg
2015/05/25 19:04:57
Done.
|
| + const ::testing::TestInfo* const test_info = |
| + ::testing::UnitTest::GetInstance()->current_test_info(); |
| + command_line = |
| + Paths::Executable().value() + L" " + |
| + base::UTF8ToUTF16(base::StringPrintf("--gtest_filter=%s.%s %s=%x|%x", |
| + test_info->test_case_name(), |
| + test_info->name(), |
| + kIsMultiprocessChild, |
| + c2p_write, |
| + p2c_read)); |
| + STARTUPINFO startup_info = {0}; |
| + startup_info.cb = sizeof(startup_info); |
| + startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| + startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| + startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| + startup_info.dwFlags = STARTF_USESTDHANDLES; |
| + PROCESS_INFORMATION process_info; |
| + PCHECK( |
| + CreateProcess(Paths::Executable().value().c_str(), |
| + &command_line[0], // This cannot be constant, per MSDN. |
|
cpu_(ooo_6.6-7.5)
2015/05/22 16:15:23
it will in some cases write a 0 to separate things
scottmg
2015/05/25 19:04:56
Acknowledged.
|
| + nullptr, |
| + nullptr, |
| + true, // Inherit handles. |
| + 0, |
| + nullptr, |
| + nullptr, |
| + &startup_info, |
| + &process_info)); |
| + child_handle_.reset(process_info.hProcess); |
| + CloseHandle(process_info.hThread); |
| + |
| + // Block until the child process has launched. CreateProcess() returns |
| + // immediately, and test code expects process initialization to have |
| + // completed so it can, for example, use the process handle. |
| + char c; |
| + CheckedReadFile(pipe_c2p_read_.get(), &c, sizeof(c)); |
| + ASSERT_EQ(' ', c); |
| + |
| + // These have been passed to the child, close our side. |
| + pipe_c2p_write_.reset(); |
| + pipe_p2c_read_.reset(); |
| + |
| + WinMultiprocessParent(); |
| + } |
| +} |
| + |
| +WinMultiprocess::~WinMultiprocess() { |
| +} |
| + |
| +FileHandle WinMultiprocess::ReadPipeHandle() const { |
| + FileHandle handle = |
| + child_handle_.get() ? pipe_c2p_read_.get() : pipe_p2c_read_.get(); |
| + CHECK(handle != nullptr); |
| + return handle; |
| +} |
| + |
| +FileHandle WinMultiprocess::WritePipeHandle() const { |
| + FileHandle handle = |
| + child_handle_.get() ? pipe_p2c_write_.get() : pipe_c2p_write_.get(); |
| + CHECK(handle != nullptr); |
| + return handle; |
| +} |
| + |
| +void WinMultiprocess::CloseReadPipe() { |
| + if (child_handle_.get()) |
| + pipe_c2p_read_.reset(); |
| + else |
| + pipe_p2c_read_.reset(); |
| +} |
| + |
| +void WinMultiprocess::CloseWritePipe() { |
| + if (child_handle_.get()) |
| + pipe_p2c_write_.reset(); |
| + else |
| + pipe_c2p_write_.reset(); |
| +} |
| + |
| +HANDLE WinMultiprocess::ChildProcess() const { |
| + EXPECT_NE(nullptr, child_handle_.get()); |
| + return child_handle_.get(); |
| +} |
| + |
| +} // namespace test |
| +} // namespace crashpad |