summaryrefslogtreecommitdiff
path: root/init/subcontext_test.cpp
blob: da1f4555036b1f6397aea384afe21e1ddbf647df (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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 "subcontext.h"

#include <unistd.h>

#include <chrono>

#include <android-base/properties.h>
#include <android-base/strings.h>
#include <gtest/gtest.h>
#include <selinux/selinux.h>

#include "builtin_arguments.h"
#include "util.h"

using namespace std::literals;

using android::base::GetProperty;
using android::base::Join;
using android::base::SetProperty;
using android::base::Split;
using android::base::WaitForProperty;

namespace android {
namespace init {

template <typename F>
void RunTest(F&& test_function) {
    auto subcontext = Subcontext({"dummy_path"}, kTestContext);
    ASSERT_NE(0, subcontext.pid());

    test_function(subcontext);

    if (subcontext.pid() > 0) {
        kill(subcontext.pid(), SIGTERM);
        kill(subcontext.pid(), SIGKILL);
    }
}

TEST(subcontext, CheckDifferentPid) {
    RunTest([](auto& subcontext) {
        auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
        ASSERT_FALSE(result.ok());

        auto pids = Split(result.error().message(), " ");
        ASSERT_EQ(2U, pids.size());
        auto our_pid = std::to_string(getpid());
        EXPECT_NE(our_pid, pids[0]);
        EXPECT_EQ(our_pid, pids[1]);
    });
}

TEST(subcontext, SetProp) {
    if (getuid() != 0) {
        GTEST_SKIP() << "Skipping test, must be run as root.";
        return;
    }

    RunTest([](auto& subcontext) {
        SetProperty("init.test.subcontext", "fail");
        WaitForProperty("init.test.subcontext", "fail");

        auto args = std::vector<std::string>{
            "setprop",
            "init.test.subcontext",
            "success",
        };
        auto result = subcontext.Execute(args);
        ASSERT_RESULT_OK(result);

        EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
    });
}

TEST(subcontext, MultipleCommands) {
    RunTest([](auto& subcontext) {
        auto first_pid = subcontext.pid();

        auto expected_words = std::vector<std::string>{
            "this",
            "is",
            "a",
            "test",
        };

        for (const auto& word : expected_words) {
            auto args = std::vector<std::string>{
                "add_word",
                word,
            };
            auto result = subcontext.Execute(args);
            ASSERT_RESULT_OK(result);
        }

        auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
        ASSERT_FALSE(result.ok());
        EXPECT_EQ(Join(expected_words, " "), result.error().message());
        EXPECT_EQ(first_pid, subcontext.pid());
    });
}

TEST(subcontext, RecoverAfterAbort) {
    RunTest([](auto& subcontext) {
        auto first_pid = subcontext.pid();

        auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
        ASSERT_FALSE(result.ok());

        auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
        ASSERT_FALSE(result2.ok());
        EXPECT_EQ("Sane error!", result2.error().message());
        EXPECT_NE(subcontext.pid(), first_pid);
    });
}

TEST(subcontext, ContextString) {
    RunTest([](auto& subcontext) {
        auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
        ASSERT_FALSE(result.ok());
        ASSERT_EQ(kTestContext, result.error().message());
    });
}

TEST(subcontext, TriggerShutdown) {
    static constexpr const char kTestShutdownCommand[] = "reboot,test-shutdown-command";
    static std::string trigger_shutdown_command;
    trigger_shutdown = [](const std::string& command) { trigger_shutdown_command = command; };
    RunTest([](auto& subcontext) {
        auto result = subcontext.Execute(
                std::vector<std::string>{"trigger_shutdown", kTestShutdownCommand});
        ASSERT_RESULT_OK(result);
    });
    EXPECT_EQ(kTestShutdownCommand, trigger_shutdown_command);
}

TEST(subcontext, ExpandArgs) {
    RunTest([](auto& subcontext) {
        auto args = std::vector<std::string>{
            "first",
            "${ro.hardware}",
            "$$third",
        };
        auto result = subcontext.ExpandArgs(args);
        ASSERT_RESULT_OK(result);
        ASSERT_EQ(3U, result->size());
        EXPECT_EQ(args[0], result->at(0));
        EXPECT_EQ(GetProperty("ro.hardware", ""), result->at(1));
        EXPECT_EQ("$third", result->at(2));
    });
}

TEST(subcontext, ExpandArgsFailure) {
    RunTest([](auto& subcontext) {
        auto args = std::vector<std::string>{
            "first",
            "${",
        };
        auto result = subcontext.ExpandArgs(args);
        ASSERT_FALSE(result.ok());
        EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }",
                  result.error().message());
    });
}

BuiltinFunctionMap BuildTestFunctionMap() {
    // For CheckDifferentPid
    auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {
        return Error() << getpid() << " " << getppid();
    };

    // For SetProp
    auto do_setprop = [](const BuiltinArguments& args) {
        android::base::SetProperty(args[1], args[2]);
        return Result<void>{};
    };

    // For MultipleCommands
    // Using a shared_ptr to extend lifetime of words to both lambdas
    auto words = std::make_shared<std::vector<std::string>>();
    auto do_add_word = [words](const BuiltinArguments& args) {
        words->emplace_back(args[1]);
        return Result<void>{};
    };
    auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> {
        return Error() << Join(*words, " ");
    };

    // For RecoverAfterAbort
    auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> {
        // Since this is an expected failure, disable debuggerd to not generate a tombstone.
        signal(SIGABRT, SIG_DFL);
        return Error() << std::string(4097, 'f');
    };
    auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> {
        return Error() << "Sane error!";
    };

    // For ContextString
    auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> {
        return Error() << args.context;
    };

    auto do_trigger_shutdown = [](const BuiltinArguments& args) -> Result<void> {
        trigger_shutdown(args[1]);
        return {};
    };

    // clang-format off
    BuiltinFunctionMap test_function_map = {
        {"return_pids_as_error",        {0,     0,      {true,  do_return_pids_as_error}}},
        {"setprop",                     {2,     2,      {true,  do_setprop}}},
        {"add_word",                    {1,     1,      {true,  do_add_word}}},
        {"return_words_as_error",       {0,     0,      {true,  do_return_words_as_error}}},
        {"cause_log_fatal",             {0,     0,      {true,  do_cause_log_fatal}}},
        {"generate_sane_error",         {0,     0,      {true,  do_generate_sane_error}}},
        {"return_context_as_error",     {0,     0,      {true,  do_return_context_as_error}}},
        {"trigger_shutdown",            {1,     1,      {true,  do_trigger_shutdown}}},
    };
    // clang-format on
    return test_function_map;
}

}  // namespace init
}  // namespace android

// init_test.cpp contains the main entry point for all init tests.
int SubcontextTestChildMain(int argc, char** argv) {
    auto test_function_map = android::init::BuildTestFunctionMap();
    return android::init::SubcontextMain(argc, argv, &test_function_map);
}