crashpad/test/win/child_launcher.cc
Mark Mentovai 1669ca2bac test: Rework TestPaths interface for obtaining 32-bit build artifacts
The design for running all Crashpad unit tests on Chromium’s try- and
buildbots involves pulling all tests into a single monolithic
crashpad_tests executable. Many Crashpad tests base the name of their
child executables or modules on the name of the main test executable.
Since the main test executable will have a different name in the
in-Chromium build, knowledge of the test executable name (referred to as
“module” here) needs to be added to the tests themselves.

This introduces TestPaths::BuildArtifact(), which allows the module name
to be specified. For Crashpad’s standalone build, the module name is
verified against the main test executable’s name.
TestPaths::BuildArtifact() can also locate paths in the alternate 32-bit
output directory for 64-bit Windows tests, taking on the responsibility
for what the new (5e9ed4cb9f69) TestPaths::Output32BitDirectory(), now
obsolete, did.

Bug: chromium:779790
Change-Id: I64c4a2190b6319e487c999812a7cfc512a75a700
Reviewed-on: https://chromium-review.googlesource.com/747536
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-11-01 16:44:45 +00:00

110 lines
3.9 KiB
C++

// 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/child_launcher.h"
#include "gtest/gtest.h"
#include "test/errors.h"
#include "util/win/command_line.h"
namespace crashpad {
namespace test {
ChildLauncher::ChildLauncher(const base::FilePath& executable,
const std::wstring& command_line)
: executable_(executable),
command_line_(command_line),
process_handle_(),
main_thread_handle_(),
stdout_read_handle_(),
stdin_write_handle_() {}
ChildLauncher::~ChildLauncher() {
if (process_handle_.is_valid())
WaitForExit();
}
void ChildLauncher::Start() {
ASSERT_FALSE(process_handle_.is_valid());
ASSERT_FALSE(main_thread_handle_.is_valid());
ASSERT_FALSE(stdout_read_handle_.is_valid());
// Create pipes for the stdin/stdout of the child.
SECURITY_ATTRIBUTES security_attributes = {0};
security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
security_attributes.bInheritHandle = true;
HANDLE stdout_read;
HANDLE stdout_write;
ASSERT_TRUE(CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
<< ErrorMessage("CreatePipe");
stdout_read_handle_.reset(stdout_read);
ScopedFileHANDLE write_handle(stdout_write);
ASSERT_TRUE(
SetHandleInformation(stdout_read_handle_.get(), HANDLE_FLAG_INHERIT, 0))
<< ErrorMessage("SetHandleInformation");
HANDLE stdin_read;
HANDLE stdin_write;
ASSERT_TRUE(CreatePipe(&stdin_read, &stdin_write, &security_attributes, 0))
<< ErrorMessage("CreatePipe");
stdin_write_handle_.reset(stdin_write);
ScopedFileHANDLE read_handle(stdin_read);
ASSERT_TRUE(
SetHandleInformation(stdin_write_handle_.get(), HANDLE_FLAG_INHERIT, 0))
<< ErrorMessage("SetHandleInformation");
STARTUPINFO startup_info = {0};
startup_info.cb = sizeof(startup_info);
startup_info.hStdInput = read_handle.get();
startup_info.hStdOutput = write_handle.get();
startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
EXPECT_NE(startup_info.hStdError, INVALID_HANDLE_VALUE)
<< ErrorMessage("GetStdHandle");
startup_info.dwFlags = STARTF_USESTDHANDLES;
PROCESS_INFORMATION process_information;
std::wstring command_line;
AppendCommandLineArgument(executable_.value(), &command_line);
command_line += L" ";
command_line += command_line_;
ASSERT_TRUE(CreateProcess(executable_.value().c_str(),
&command_line[0],
nullptr,
nullptr,
true,
0,
nullptr,
nullptr,
&startup_info,
&process_information))
<< ErrorMessage("CreateProcess");
// Take ownership of the two process handles returned.
main_thread_handle_.reset(process_information.hThread);
process_handle_.reset(process_information.hProcess);
}
DWORD ChildLauncher::WaitForExit() {
EXPECT_TRUE(process_handle_.is_valid());
EXPECT_EQ(WaitForSingleObject(process_handle_.get(), INFINITE), WAIT_OBJECT_0)
<< ErrorMessage("WaitForSingleObject");
DWORD exit_code = 0;
EXPECT_TRUE(GetExitCodeProcess(process_handle_.get(), &exit_code))
<< ErrorMessage("GetExitCodeProcess");
process_handle_.reset();
return exit_code;
}
} // namespace test
} // namespace crashpad