blob: da1f4555036b1f6397aea384afe21e1ddbf647df [file] [log] [blame]
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "subcontext.h"
18
19#include <unistd.h>
20
21#include <chrono>
22
23#include <android-base/properties.h>
24#include <android-base/strings.h>
25#include <gtest/gtest.h>
Tom Cherrye6d37cd2017-10-19 14:15:37 -070026#include <selinux/selinux.h>
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070027
28#include "builtin_arguments.h"
Tom Cherry18278d22019-11-12 16:21:20 -080029#include "util.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070030
31using namespace std::literals;
32
33using android::base::GetProperty;
34using android::base::Join;
35using android::base::SetProperty;
36using android::base::Split;
37using android::base::WaitForProperty;
38
39namespace android {
40namespace init {
41
Tom Cherrye6d37cd2017-10-19 14:15:37 -070042template <typename F>
43void RunTest(F&& test_function) {
Tom Cherry1c005f32019-11-20 15:51:36 -080044 auto subcontext = Subcontext({"dummy_path"}, kTestContext);
Tom Cherrye6d37cd2017-10-19 14:15:37 -070045 ASSERT_NE(0, subcontext.pid());
46
Tom Cherry1c005f32019-11-20 15:51:36 -080047 test_function(subcontext);
Tom Cherrye6d37cd2017-10-19 14:15:37 -070048
49 if (subcontext.pid() > 0) {
50 kill(subcontext.pid(), SIGTERM);
51 kill(subcontext.pid(), SIGKILL);
52 }
53}
54
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070055TEST(subcontext, CheckDifferentPid) {
Tom Cherry1c005f32019-11-20 15:51:36 -080056 RunTest([](auto& subcontext) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070057 auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
Bernie Innocenticecebbb2020-02-06 03:49:33 +090058 ASSERT_FALSE(result.ok());
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070059
Jiyong Park8fd64c82019-05-31 03:43:34 +090060 auto pids = Split(result.error().message(), " ");
Tom Cherrye6d37cd2017-10-19 14:15:37 -070061 ASSERT_EQ(2U, pids.size());
62 auto our_pid = std::to_string(getpid());
63 EXPECT_NE(our_pid, pids[0]);
64 EXPECT_EQ(our_pid, pids[1]);
65 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070066}
67
68TEST(subcontext, SetProp) {
Tom Cherry1c005f32019-11-20 15:51:36 -080069 if (getuid() != 0) {
70 GTEST_SKIP() << "Skipping test, must be run as root.";
71 return;
72 }
73
74 RunTest([](auto& subcontext) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070075 SetProperty("init.test.subcontext", "fail");
76 WaitForProperty("init.test.subcontext", "fail");
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070077
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070078 auto args = std::vector<std::string>{
Tom Cherrye6d37cd2017-10-19 14:15:37 -070079 "setprop",
80 "init.test.subcontext",
81 "success",
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070082 };
83 auto result = subcontext.Execute(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090084 ASSERT_RESULT_OK(result);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070085
Tom Cherrye6d37cd2017-10-19 14:15:37 -070086 EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
87 });
88}
89
90TEST(subcontext, MultipleCommands) {
Tom Cherry1c005f32019-11-20 15:51:36 -080091 RunTest([](auto& subcontext) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070092 auto first_pid = subcontext.pid();
93
94 auto expected_words = std::vector<std::string>{
95 "this",
96 "is",
97 "a",
98 "test",
99 };
100
101 for (const auto& word : expected_words) {
102 auto args = std::vector<std::string>{
103 "add_word",
104 word,
105 };
106 auto result = subcontext.Execute(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900107 ASSERT_RESULT_OK(result);
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700108 }
109
110 auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900111 ASSERT_FALSE(result.ok());
Jiyong Park8fd64c82019-05-31 03:43:34 +0900112 EXPECT_EQ(Join(expected_words, " "), result.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700113 EXPECT_EQ(first_pid, subcontext.pid());
114 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700115}
116
117TEST(subcontext, RecoverAfterAbort) {
Tom Cherry1c005f32019-11-20 15:51:36 -0800118 RunTest([](auto& subcontext) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700119 auto first_pid = subcontext.pid();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700120
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700121 auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900122 ASSERT_FALSE(result.ok());
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700123
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700124 auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900125 ASSERT_FALSE(result2.ok());
Jiyong Park8fd64c82019-05-31 03:43:34 +0900126 EXPECT_EQ("Sane error!", result2.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700127 EXPECT_NE(subcontext.pid(), first_pid);
128 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700129}
130
131TEST(subcontext, ContextString) {
Tom Cherry1c005f32019-11-20 15:51:36 -0800132 RunTest([](auto& subcontext) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700133 auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900134 ASSERT_FALSE(result.ok());
Tom Cherry1c005f32019-11-20 15:51:36 -0800135 ASSERT_EQ(kTestContext, result.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700136 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700137}
138
Tom Cherry18278d22019-11-12 16:21:20 -0800139TEST(subcontext, TriggerShutdown) {
140 static constexpr const char kTestShutdownCommand[] = "reboot,test-shutdown-command";
141 static std::string trigger_shutdown_command;
142 trigger_shutdown = [](const std::string& command) { trigger_shutdown_command = command; };
Tom Cherry1c005f32019-11-20 15:51:36 -0800143 RunTest([](auto& subcontext) {
Tom Cherry18278d22019-11-12 16:21:20 -0800144 auto result = subcontext.Execute(
145 std::vector<std::string>{"trigger_shutdown", kTestShutdownCommand});
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900146 ASSERT_RESULT_OK(result);
Tom Cherry18278d22019-11-12 16:21:20 -0800147 });
148 EXPECT_EQ(kTestShutdownCommand, trigger_shutdown_command);
149}
150
Tom Cherryc49719f2018-01-10 11:04:34 -0800151TEST(subcontext, ExpandArgs) {
Tom Cherry1c005f32019-11-20 15:51:36 -0800152 RunTest([](auto& subcontext) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800153 auto args = std::vector<std::string>{
154 "first",
155 "${ro.hardware}",
156 "$$third",
157 };
158 auto result = subcontext.ExpandArgs(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900159 ASSERT_RESULT_OK(result);
Tom Cherryc49719f2018-01-10 11:04:34 -0800160 ASSERT_EQ(3U, result->size());
161 EXPECT_EQ(args[0], result->at(0));
162 EXPECT_EQ(GetProperty("ro.hardware", ""), result->at(1));
163 EXPECT_EQ("$third", result->at(2));
164 });
165}
166
167TEST(subcontext, ExpandArgsFailure) {
Tom Cherry1c005f32019-11-20 15:51:36 -0800168 RunTest([](auto& subcontext) {
Tom Cherryc49719f2018-01-10 11:04:34 -0800169 auto args = std::vector<std::string>{
170 "first",
171 "${",
172 };
173 auto result = subcontext.ExpandArgs(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900174 ASSERT_FALSE(result.ok());
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700175 EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }",
176 result.error().message());
Tom Cherryc49719f2018-01-10 11:04:34 -0800177 });
178}
179
Tom Cherryd52a5b32019-07-22 16:05:36 -0700180BuiltinFunctionMap BuildTestFunctionMap() {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700181 // For CheckDifferentPid
Tom Cherryd52a5b32019-07-22 16:05:36 -0700182 auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {
183 return Error() << getpid() << " " << getppid();
184 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700185
186 // For SetProp
Tom Cherryd52a5b32019-07-22 16:05:36 -0700187 auto do_setprop = [](const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700188 android::base::SetProperty(args[1], args[2]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700189 return Result<void>{};
Tom Cherryd52a5b32019-07-22 16:05:36 -0700190 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700191
192 // For MultipleCommands
193 // Using a shared_ptr to extend lifetime of words to both lambdas
194 auto words = std::make_shared<std::vector<std::string>>();
Tom Cherryd52a5b32019-07-22 16:05:36 -0700195 auto do_add_word = [words](const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700196 words->emplace_back(args[1]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700197 return Result<void>{};
Tom Cherryd52a5b32019-07-22 16:05:36 -0700198 };
199 auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> {
200 return Error() << Join(*words, " ");
201 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700202
203 // For RecoverAfterAbort
Tom Cherryd52a5b32019-07-22 16:05:36 -0700204 auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> {
Tom Cherry94b1c572020-12-15 06:38:55 -0800205 // Since this is an expected failure, disable debuggerd to not generate a tombstone.
206 signal(SIGABRT, SIG_DFL);
Tom Cherryd52a5b32019-07-22 16:05:36 -0700207 return Error() << std::string(4097, 'f');
208 };
209 auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> {
210 return Error() << "Sane error!";
211 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700212
213 // For ContextString
Tom Cherryd52a5b32019-07-22 16:05:36 -0700214 auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> {
215 return Error() << args.context;
216 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700217
Tom Cherry18278d22019-11-12 16:21:20 -0800218 auto do_trigger_shutdown = [](const BuiltinArguments& args) -> Result<void> {
219 trigger_shutdown(args[1]);
220 return {};
221 };
222
Tom Cherryd52a5b32019-07-22 16:05:36 -0700223 // clang-format off
224 BuiltinFunctionMap test_function_map = {
225 {"return_pids_as_error", {0, 0, {true, do_return_pids_as_error}}},
226 {"setprop", {2, 2, {true, do_setprop}}},
227 {"add_word", {1, 1, {true, do_add_word}}},
228 {"return_words_as_error", {0, 0, {true, do_return_words_as_error}}},
229 {"cause_log_fatal", {0, 0, {true, do_cause_log_fatal}}},
230 {"generate_sane_error", {0, 0, {true, do_generate_sane_error}}},
231 {"return_context_as_error", {0, 0, {true, do_return_context_as_error}}},
Tom Cherry18278d22019-11-12 16:21:20 -0800232 {"trigger_shutdown", {1, 1, {true, do_trigger_shutdown}}},
Tom Cherryd52a5b32019-07-22 16:05:36 -0700233 };
234 // clang-format on
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700235 return test_function_map;
236}
237
238} // namespace init
239} // namespace android
240
Tom Cherrydcb3d152019-08-07 16:02:28 -0700241// init_test.cpp contains the main entry point for all init tests.
242int SubcontextTestChildMain(int argc, char** argv) {
243 auto test_function_map = android::init::BuildTestFunctionMap();
244 return android::init::SubcontextMain(argc, argv, &test_function_map);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700245}