blob: 9c1a7881883fcd20aa34b871c84bd84d91cc412e [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 -070042// I would use test fixtures, but I cannot skip the test if not root with them, so instead we have
43// this test runner.
44template <typename F>
45void RunTest(F&& test_function) {
46 if (getuid() != 0) {
Tom Cherry17b2be02019-08-20 10:43:48 -070047 GTEST_SKIP() << "Skipping test, must be run as root.";
Tom Cherrye6d37cd2017-10-19 14:15:37 -070048 return;
49 }
50
51 char* context;
52 ASSERT_EQ(0, getcon(&context));
53 auto context_string = std::string(context);
54 free(context);
55
Tom Cherry14c24722019-09-18 13:47:19 -070056 auto subcontext = Subcontext({"dummy_path"}, context_string);
Tom Cherrye6d37cd2017-10-19 14:15:37 -070057 ASSERT_NE(0, subcontext.pid());
58
59 test_function(subcontext, context_string);
60
61 if (subcontext.pid() > 0) {
62 kill(subcontext.pid(), SIGTERM);
63 kill(subcontext.pid(), SIGKILL);
64 }
65}
66
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070067TEST(subcontext, CheckDifferentPid) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070068 RunTest([](auto& subcontext, auto& context_string) {
69 auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
70 ASSERT_FALSE(result);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070071
Jiyong Park8fd64c82019-05-31 03:43:34 +090072 auto pids = Split(result.error().message(), " ");
Tom Cherrye6d37cd2017-10-19 14:15:37 -070073 ASSERT_EQ(2U, pids.size());
74 auto our_pid = std::to_string(getpid());
75 EXPECT_NE(our_pid, pids[0]);
76 EXPECT_EQ(our_pid, pids[1]);
77 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070078}
79
80TEST(subcontext, SetProp) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070081 RunTest([](auto& subcontext, auto& context_string) {
82 SetProperty("init.test.subcontext", "fail");
83 WaitForProperty("init.test.subcontext", "fail");
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070084
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070085 auto args = std::vector<std::string>{
Tom Cherrye6d37cd2017-10-19 14:15:37 -070086 "setprop",
87 "init.test.subcontext",
88 "success",
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070089 };
90 auto result = subcontext.Execute(args);
91 ASSERT_TRUE(result) << result.error();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070092
Tom Cherrye6d37cd2017-10-19 14:15:37 -070093 EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
94 });
95}
96
97TEST(subcontext, MultipleCommands) {
98 RunTest([](auto& subcontext, auto& context_string) {
99 auto first_pid = subcontext.pid();
100
101 auto expected_words = std::vector<std::string>{
102 "this",
103 "is",
104 "a",
105 "test",
106 };
107
108 for (const auto& word : expected_words) {
109 auto args = std::vector<std::string>{
110 "add_word",
111 word,
112 };
113 auto result = subcontext.Execute(args);
114 ASSERT_TRUE(result) << result.error();
115 }
116
117 auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
118 ASSERT_FALSE(result);
Jiyong Park8fd64c82019-05-31 03:43:34 +0900119 EXPECT_EQ(Join(expected_words, " "), result.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700120 EXPECT_EQ(first_pid, subcontext.pid());
121 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700122}
123
124TEST(subcontext, RecoverAfterAbort) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700125 RunTest([](auto& subcontext, auto& context_string) {
126 auto first_pid = subcontext.pid();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700127
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700128 auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
129 ASSERT_FALSE(result);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700130
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700131 auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
132 ASSERT_FALSE(result2);
Jiyong Park8fd64c82019-05-31 03:43:34 +0900133 EXPECT_EQ("Sane error!", result2.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700134 EXPECT_NE(subcontext.pid(), first_pid);
135 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700136}
137
138TEST(subcontext, ContextString) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700139 RunTest([](auto& subcontext, auto& context_string) {
140 auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
141 ASSERT_FALSE(result);
Jiyong Park8fd64c82019-05-31 03:43:34 +0900142 ASSERT_EQ(context_string, result.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700143 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700144}
145
Tom Cherry18278d22019-11-12 16:21:20 -0800146TEST(subcontext, TriggerShutdown) {
147 static constexpr const char kTestShutdownCommand[] = "reboot,test-shutdown-command";
148 static std::string trigger_shutdown_command;
149 trigger_shutdown = [](const std::string& command) { trigger_shutdown_command = command; };
150 RunTest([](auto& subcontext, auto& context_string) {
151 auto result = subcontext.Execute(
152 std::vector<std::string>{"trigger_shutdown", kTestShutdownCommand});
153 ASSERT_TRUE(result);
154 });
155 EXPECT_EQ(kTestShutdownCommand, trigger_shutdown_command);
156}
157
Tom Cherryc49719f2018-01-10 11:04:34 -0800158TEST(subcontext, ExpandArgs) {
159 RunTest([](auto& subcontext, auto& context_string) {
160 auto args = std::vector<std::string>{
161 "first",
162 "${ro.hardware}",
163 "$$third",
164 };
165 auto result = subcontext.ExpandArgs(args);
166 ASSERT_TRUE(result) << result.error();
167 ASSERT_EQ(3U, result->size());
168 EXPECT_EQ(args[0], result->at(0));
169 EXPECT_EQ(GetProperty("ro.hardware", ""), result->at(1));
170 EXPECT_EQ("$third", result->at(2));
171 });
172}
173
174TEST(subcontext, ExpandArgsFailure) {
175 RunTest([](auto& subcontext, auto& context_string) {
176 auto args = std::vector<std::string>{
177 "first",
178 "${",
179 };
180 auto result = subcontext.ExpandArgs(args);
181 ASSERT_FALSE(result);
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700182 EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }",
183 result.error().message());
Tom Cherryc49719f2018-01-10 11:04:34 -0800184 });
185}
186
Tom Cherryd52a5b32019-07-22 16:05:36 -0700187BuiltinFunctionMap BuildTestFunctionMap() {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700188 // For CheckDifferentPid
Tom Cherryd52a5b32019-07-22 16:05:36 -0700189 auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {
190 return Error() << getpid() << " " << getppid();
191 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700192
193 // For SetProp
Tom Cherryd52a5b32019-07-22 16:05:36 -0700194 auto do_setprop = [](const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700195 android::base::SetProperty(args[1], args[2]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700196 return Result<void>{};
Tom Cherryd52a5b32019-07-22 16:05:36 -0700197 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700198
199 // For MultipleCommands
200 // Using a shared_ptr to extend lifetime of words to both lambdas
201 auto words = std::make_shared<std::vector<std::string>>();
Tom Cherryd52a5b32019-07-22 16:05:36 -0700202 auto do_add_word = [words](const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700203 words->emplace_back(args[1]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700204 return Result<void>{};
Tom Cherryd52a5b32019-07-22 16:05:36 -0700205 };
206 auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> {
207 return Error() << Join(*words, " ");
208 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700209
210 // For RecoverAfterAbort
Tom Cherryd52a5b32019-07-22 16:05:36 -0700211 auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> {
212 return Error() << std::string(4097, 'f');
213 };
214 auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> {
215 return Error() << "Sane error!";
216 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700217
218 // For ContextString
Tom Cherryd52a5b32019-07-22 16:05:36 -0700219 auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> {
220 return Error() << args.context;
221 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700222
Tom Cherry18278d22019-11-12 16:21:20 -0800223 auto do_trigger_shutdown = [](const BuiltinArguments& args) -> Result<void> {
224 trigger_shutdown(args[1]);
225 return {};
226 };
227
Tom Cherryd52a5b32019-07-22 16:05:36 -0700228 // clang-format off
229 BuiltinFunctionMap test_function_map = {
230 {"return_pids_as_error", {0, 0, {true, do_return_pids_as_error}}},
231 {"setprop", {2, 2, {true, do_setprop}}},
232 {"add_word", {1, 1, {true, do_add_word}}},
233 {"return_words_as_error", {0, 0, {true, do_return_words_as_error}}},
234 {"cause_log_fatal", {0, 0, {true, do_cause_log_fatal}}},
235 {"generate_sane_error", {0, 0, {true, do_generate_sane_error}}},
236 {"return_context_as_error", {0, 0, {true, do_return_context_as_error}}},
Tom Cherry18278d22019-11-12 16:21:20 -0800237 {"trigger_shutdown", {1, 1, {true, do_trigger_shutdown}}},
Tom Cherryd52a5b32019-07-22 16:05:36 -0700238 };
239 // clang-format on
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700240 return test_function_map;
241}
242
243} // namespace init
244} // namespace android
245
Tom Cherrydcb3d152019-08-07 16:02:28 -0700246// init_test.cpp contains the main entry point for all init tests.
247int SubcontextTestChildMain(int argc, char** argv) {
248 auto test_function_map = android::init::BuildTestFunctionMap();
249 return android::init::SubcontextMain(argc, argv, &test_function_map);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700250}