1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/windows_services/elevated_tracing_service/with_child_test.h"
#include "base/command_line.h"
#include "base/process/launch.h"
#include "base/strings/string_number_conversions.h"
#include "base/win/windows_handle_util.h"
#include "testing/multiprocess_func_list.h"
namespace elevated_tracing_service {
namespace {
constexpr std::string_view kStartEventHandle = "start-event-handle";
constexpr std::string_view kExitEventHandle = "exit-event-handle";
void AppendSwitchHandle(base::CommandLine& command_line,
std::string_view switch_name,
HANDLE handle) {
command_line.AppendSwitchNative(
switch_name, base::NumberToWString(base::win::HandleToUint32(handle)));
}
// Returns a handle from the value of a switch on the process's command line,
// or an invalid handle in case of error.
base::win::ScopedHandle HandleFromSwitch(const base::CommandLine& command_line,
std::string_view switch_name) {
if (auto switch_value = command_line.GetSwitchValueNative(switch_name);
!switch_value.empty()) {
uint32_t handle_value;
if (base::StringToUint(switch_value, &handle_value)) {
return base::win::ScopedHandle(base::win::Uint32ToHandle(handle_value));
}
}
return base::win::ScopedHandle();
}
// A child process's main function. It signals to the parent that it has
// started, and then waits for the parent to signal that it's time to terminate.
MULTIPROCESS_TEST_MAIN(ExitWhenSignaled) {
WithChildTest::SignalChildStart();
return WithChildTest::WaitForChildTermination() ? 0 : 1;
}
} // namespace
// static
void WithChildTest::SignalChildStart() {
// Notify the test process that the child has started.
if (auto handle = HandleFromSwitch(*base::CommandLine::ForCurrentProcess(),
kStartEventHandle);
handle.is_valid()) {
base::WaitableEvent(std::move(handle)).Signal();
}
}
// static
bool WithChildTest::WaitForChildTermination() {
// Notify the test process that the child has started.
if (auto handle = HandleFromSwitch(*base::CommandLine::ForCurrentProcess(),
kExitEventHandle);
handle.is_valid()) {
base::WaitableEvent(std::move(handle)).Wait();
return true;
}
return false;
}
base::Process WithChildTest::SpawnChildWithEventHandles(
std::string_view procname) {
base::LaunchOptions launch_options;
launch_options.start_hidden = true;
launch_options.feedback_cursor_off = true;
launch_options.handles_to_inherit.push_back(child_started_event_.handle());
launch_options.handles_to_inherit.push_back(exit_child_event_.handle());
return SpawnChildWithOptions(std::string(procname), launch_options);
}
base::CommandLine WithChildTest::MakeCmdLine(const std::string& procname) {
auto result = base::MultiProcessTest::MakeCmdLine(procname);
AppendSwitchHandle(result, kStartEventHandle, child_started_event_.handle());
AppendSwitchHandle(result, kExitEventHandle, exit_child_event_.handle());
return result;
}
} // namespace elevated_tracing_service
|