blob: de795bb5583b19ff29bcc15566e88c8b2b2a7f6b [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
9#include "debugd/src/process_with_id.h"
10#include "debugd/src/subprocess_tool.h"
11
12namespace debugd {
13namespace {
14
15using SubprocessToolTestParam = std::tuple<bool, // sandboxed
16 bool>; // allow_root_mount_ns
17
18class SubprocessToolTest
19 : public testing::TestWithParam<SubprocessToolTestParam> {
20 protected:
21 SubprocessTool tool_;
22};
23
24TEST_P(SubprocessToolTest, CreateProcessAndStop) {
25 bool sandboxed;
26 bool allow_root_mount_ns;
27 std::tie(sandboxed, allow_root_mount_ns) = GetParam();
28
29 ProcessWithId* process = tool_.CreateProcess(sandboxed, allow_root_mount_ns);
30 EXPECT_NE(nullptr, process);
31 EXPECT_FALSE(process->id().empty());
32
33 std::string handle = process->id();
34
35 DBus::Error error;
36 tool_.Stop(handle, &error);
37 EXPECT_FALSE(error);
38 // |process| is now destroyed by SubprocessTool::Stop().
39
40 tool_.Stop(handle, &error);
41 EXPECT_TRUE(error);
42 EXPECT_EQ(handle, error.message());
43}
44
45INSTANTIATE_TEST_CASE_P(SubprocessToolCreateProcess,
46 SubprocessToolTest,
47 testing::Combine(testing::Bool(), testing::Bool()));
48
49TEST_F(SubprocessToolTest, StopInvalidProcessHandle) {
50 std::string invalid_handle = "some_invalid_handle";
51 DBus::Error error;
52 tool_.Stop(invalid_handle, &error);
53 EXPECT_TRUE(error);
54 EXPECT_EQ(invalid_handle, error.message());
55}
56
57} // namespace
58} // namespace debugd