blob: 85a2f2a944a4efd3eddba106cf2aa00e43568fd4 [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) {
Chan Wang5996d602024-11-14 13:25:37 +000044 auto subcontext = Subcontext({"dummy_path"}, {"dummy_partition"}, 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
Chan Wang5996d602024-11-14 13:25:37 +0000180TEST(subcontext, PartitionMatchesSubcontext) {
181 RunTest([](auto& subcontext) {
182 static auto& existent_partition = "dummy_partition";
183 static auto& non_existent_partition = "not_dummy_partition";
184
185 auto existent_result = subcontext.PartitionMatchesSubcontext(existent_partition);
186 auto non_existent_result = subcontext.PartitionMatchesSubcontext(non_existent_partition);
187
188 ASSERT_TRUE(existent_result);
189 ASSERT_FALSE(non_existent_result);
190 });
191}
192
Tom Cherryd52a5b32019-07-22 16:05:36 -0700193BuiltinFunctionMap BuildTestFunctionMap() {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700194 // For CheckDifferentPid
Tom Cherryd52a5b32019-07-22 16:05:36 -0700195 auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {
196 return Error() << getpid() << " " << getppid();
197 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700198
199 // For SetProp
Tom Cherryd52a5b32019-07-22 16:05:36 -0700200 auto do_setprop = [](const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700201 android::base::SetProperty(args[1], args[2]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700202 return Result<void>{};
Tom Cherryd52a5b32019-07-22 16:05:36 -0700203 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700204
205 // For MultipleCommands
206 // Using a shared_ptr to extend lifetime of words to both lambdas
207 auto words = std::make_shared<std::vector<std::string>>();
Tom Cherryd52a5b32019-07-22 16:05:36 -0700208 auto do_add_word = [words](const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700209 words->emplace_back(args[1]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700210 return Result<void>{};
Tom Cherryd52a5b32019-07-22 16:05:36 -0700211 };
212 auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> {
213 return Error() << Join(*words, " ");
214 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700215
216 // For RecoverAfterAbort
Tom Cherryd52a5b32019-07-22 16:05:36 -0700217 auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> {
Tom Cherry94b1c572020-12-15 06:38:55 -0800218 // Since this is an expected failure, disable debuggerd to not generate a tombstone.
219 signal(SIGABRT, SIG_DFL);
Tom Cherryd52a5b32019-07-22 16:05:36 -0700220 return Error() << std::string(4097, 'f');
221 };
222 auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> {
223 return Error() << "Sane error!";
224 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700225
226 // For ContextString
Tom Cherryd52a5b32019-07-22 16:05:36 -0700227 auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> {
228 return Error() << args.context;
229 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700230
Tom Cherry18278d22019-11-12 16:21:20 -0800231 auto do_trigger_shutdown = [](const BuiltinArguments& args) -> Result<void> {
232 trigger_shutdown(args[1]);
233 return {};
234 };
235
Tom Cherryd52a5b32019-07-22 16:05:36 -0700236 // clang-format off
237 BuiltinFunctionMap test_function_map = {
238 {"return_pids_as_error", {0, 0, {true, do_return_pids_as_error}}},
239 {"setprop", {2, 2, {true, do_setprop}}},
240 {"add_word", {1, 1, {true, do_add_word}}},
241 {"return_words_as_error", {0, 0, {true, do_return_words_as_error}}},
242 {"cause_log_fatal", {0, 0, {true, do_cause_log_fatal}}},
243 {"generate_sane_error", {0, 0, {true, do_generate_sane_error}}},
244 {"return_context_as_error", {0, 0, {true, do_return_context_as_error}}},
Tom Cherry18278d22019-11-12 16:21:20 -0800245 {"trigger_shutdown", {1, 1, {true, do_trigger_shutdown}}},
Tom Cherryd52a5b32019-07-22 16:05:36 -0700246 };
247 // clang-format on
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700248 return test_function_map;
249}
250
251} // namespace init
252} // namespace android
253
Tom Cherrydcb3d152019-08-07 16:02:28 -0700254// init_test.cpp contains the main entry point for all init tests.
255int SubcontextTestChildMain(int argc, char** argv) {
256 auto test_function_map = android::init::BuildTestFunctionMap();
257 return android::init::SubcontextMain(argc, argv, &test_function_map);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700258}