Ben Chan | 17a9b34 | 2017-02-15 12:29:37 -0800 | [diff] [blame] | 1 | // 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 Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 9 | #include <brillo/errors/error.h> |
| 10 | |
Ben Chan | 17a9b34 | 2017-02-15 12:29:37 -0800 | [diff] [blame] | 11 | #include "debugd/src/process_with_id.h" |
| 12 | #include "debugd/src/subprocess_tool.h" |
| 13 | |
| 14 | namespace debugd { |
| 15 | namespace { |
| 16 | |
| 17 | using SubprocessToolTestParam = std::tuple<bool, // sandboxed |
| 18 | bool>; // allow_root_mount_ns |
| 19 | |
| 20 | class SubprocessToolTest |
| 21 | : public testing::TestWithParam<SubprocessToolTestParam> { |
| 22 | protected: |
| 23 | SubprocessTool tool_; |
| 24 | }; |
| 25 | |
| 26 | TEST_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 Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 37 | EXPECT_TRUE(tool_.Stop(handle, nullptr)); |
Ben Chan | 17a9b34 | 2017-02-15 12:29:37 -0800 | [diff] [blame] | 38 | // |process| is now destroyed by SubprocessTool::Stop(). |
| 39 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 40 | brillo::ErrorPtr error; |
| 41 | EXPECT_FALSE(tool_.Stop(handle, &error)); |
| 42 | EXPECT_EQ(handle, error->GetMessage()); |
Ben Chan | 17a9b34 | 2017-02-15 12:29:37 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Jim Pollock | 2667fca | 2020-06-11 07:17:59 +0100 | [diff] [blame] | 45 | INSTANTIATE_TEST_SUITE_P(SubprocessToolCreateProcess, |
| 46 | SubprocessToolTest, |
| 47 | testing::Combine(testing::Bool(), testing::Bool())); |
Ben Chan | 17a9b34 | 2017-02-15 12:29:37 -0800 | [diff] [blame] | 48 | |
| 49 | TEST_F(SubprocessToolTest, StopInvalidProcessHandle) { |
| 50 | std::string invalid_handle = "some_invalid_handle"; |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 51 | brillo::ErrorPtr error; |
| 52 | EXPECT_FALSE(tool_.Stop(invalid_handle, &error)); |
| 53 | EXPECT_EQ(invalid_handle, error->GetMessage()); |
Ben Chan | 17a9b34 | 2017-02-15 12:29:37 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | } // namespace debugd |