blob: ed580c15fdcc8277edf2b024cfa70495e26f7f53 [file] [log] [blame]
Ben Chan17a9b342017-02-15 12:29:37 -08001// Copyright 2017 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <gtest/gtest.h>
6
7#include <tuple>
8
Eric Carusocc7106c2017-04-27 14:22:42 -07009#include <brillo/errors/error.h>
10
Ben Chan17a9b342017-02-15 12:29:37 -080011#include "debugd/src/process_with_id.h"
12#include "debugd/src/subprocess_tool.h"
13
14namespace debugd {
15namespace {
16
17using SubprocessToolTestParam = std::tuple<bool, // sandboxed
18 bool>; // allow_root_mount_ns
19
20class SubprocessToolTest
21 : public testing::TestWithParam<SubprocessToolTestParam> {
22 protected:
23 SubprocessTool tool_;
24};
25
26TEST_P(SubprocessToolTest, CreateProcessAndStop) {
27 bool sandboxed;
28 bool allow_root_mount_ns;
29 std::tie(sandboxed, allow_root_mount_ns) = GetParam();
30
31 ProcessWithId* process = tool_.CreateProcess(sandboxed, allow_root_mount_ns);
32 EXPECT_NE(nullptr, process);
33 EXPECT_FALSE(process->id().empty());
34
35 std::string handle = process->id();
36
Eric Carusocc7106c2017-04-27 14:22:42 -070037 EXPECT_TRUE(tool_.Stop(handle, nullptr));
Ben Chan17a9b342017-02-15 12:29:37 -080038 // |process| is now destroyed by SubprocessTool::Stop().
39
Eric Carusocc7106c2017-04-27 14:22:42 -070040 brillo::ErrorPtr error;
41 EXPECT_FALSE(tool_.Stop(handle, &error));
42 EXPECT_EQ(handle, error->GetMessage());
Ben Chan17a9b342017-02-15 12:29:37 -080043}
44
Jim Pollock2667fca2020-06-11 07:17:59 +010045INSTANTIATE_TEST_SUITE_P(SubprocessToolCreateProcess,
46 SubprocessToolTest,
47 testing::Combine(testing::Bool(), testing::Bool()));
Ben Chan17a9b342017-02-15 12:29:37 -080048
49TEST_F(SubprocessToolTest, StopInvalidProcessHandle) {
50 std::string invalid_handle = "some_invalid_handle";
Eric Carusocc7106c2017-04-27 14:22:42 -070051 brillo::ErrorPtr error;
52 EXPECT_FALSE(tool_.Stop(invalid_handle, &error));
53 EXPECT_EQ(invalid_handle, error->GetMessage());
Ben Chan17a9b342017-02-15 12:29:37 -080054}
55
56} // namespace
57} // namespace debugd