1 /*
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>
26 #include <selinux/selinux.h>
27
28 #include "builtin_arguments.h"
29 #include "util.h"
30
31 using namespace std::literals;
32
33 using android::base::GetProperty;
34 using android::base::Join;
35 using android::base::SetProperty;
36 using android::base::Split;
37 using android::base::WaitForProperty;
38
39 namespace android {
40 namespace init {
41
42 template <typename F>
RunTest(F && test_function)43 void RunTest(F&& test_function) {
44 auto subcontext = Subcontext({"dummy_path"}, kTestContext);
45 ASSERT_NE(0, subcontext.pid());
46
47 test_function(subcontext);
48
49 if (subcontext.pid() > 0) {
50 kill(subcontext.pid(), SIGTERM);
51 kill(subcontext.pid(), SIGKILL);
52 }
53 }
54
TEST(subcontext,CheckDifferentPid)55 TEST(subcontext, CheckDifferentPid) {
56 RunTest([](auto& subcontext) {
57 auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
58 ASSERT_FALSE(result.ok());
59
60 auto pids = Split(result.error().message(), " ");
61 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 });
66 }
67
TEST(subcontext,SetProp)68 TEST(subcontext, SetProp) {
69 if (getuid() != 0) {
70 GTEST_SKIP() << "Skipping test, must be run as root.";
71 return;
72 }
73
74 RunTest([](auto& subcontext) {
75 SetProperty("init.test.subcontext", "fail");
76 WaitForProperty("init.test.subcontext", "fail");
77
78 auto args = std::vector<std::string>{
79 "setprop",
80 "init.test.subcontext",
81 "success",
82 };
83 auto result = subcontext.Execute(args);
84 ASSERT_RESULT_OK(result);
85
86 EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
87 });
88 }
89
TEST(subcontext,MultipleCommands)90 TEST(subcontext, MultipleCommands) {
91 RunTest([](auto& subcontext) {
92 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);
107 ASSERT_RESULT_OK(result);
108 }
109
110 auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
111 ASSERT_FALSE(result.ok());
112 EXPECT_EQ(Join(expected_words, " "), result.error().message());
113 EXPECT_EQ(first_pid, subcontext.pid());
114 });
115 }
116
TEST(subcontext,RecoverAfterAbort)117 TEST(subcontext, RecoverAfterAbort) {
118 RunTest([](auto& subcontext) {
119 auto first_pid = subcontext.pid();
120
121 auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
122 ASSERT_FALSE(result.ok());
123
124 auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
125 ASSERT_FALSE(result2.ok());
126 EXPECT_EQ("Sane error!", result2.error().message());
127 EXPECT_NE(subcontext.pid(), first_pid);
128 });
129 }
130
TEST(subcontext,ContextString)131 TEST(subcontext, ContextString) {
132 RunTest([](auto& subcontext) {
133 auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
134 ASSERT_FALSE(result.ok());
135 ASSERT_EQ(kTestContext, result.error().message());
136 });
137 }
138
TEST(subcontext,TriggerShutdown)139 TEST(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; };
143 RunTest([](auto& subcontext) {
144 auto result = subcontext.Execute(
145 std::vector<std::string>{"trigger_shutdown", kTestShutdownCommand});
146 ASSERT_RESULT_OK(result);
147 });
148 EXPECT_EQ(kTestShutdownCommand, trigger_shutdown_command);
149 }
150
TEST(subcontext,ExpandArgs)151 TEST(subcontext, ExpandArgs) {
152 RunTest([](auto& subcontext) {
153 auto args = std::vector<std::string>{
154 "first",
155 "${ro.hardware}",
156 "$$third",
157 };
158 auto result = subcontext.ExpandArgs(args);
159 ASSERT_RESULT_OK(result);
160 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
TEST(subcontext,ExpandArgsFailure)167 TEST(subcontext, ExpandArgsFailure) {
168 RunTest([](auto& subcontext) {
169 auto args = std::vector<std::string>{
170 "first",
171 "${",
172 };
173 auto result = subcontext.ExpandArgs(args);
174 ASSERT_FALSE(result.ok());
175 EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }",
176 result.error().message());
177 });
178 }
179
BuildTestFunctionMap()180 BuiltinFunctionMap BuildTestFunctionMap() {
181 // For CheckDifferentPid
182 auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {
183 return Error() << getpid() << " " << getppid();
184 };
185
186 // For SetProp
187 auto do_setprop = [](const BuiltinArguments& args) {
188 android::base::SetProperty(args[1], args[2]);
189 return Result<void>{};
190 };
191
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>>();
195 auto do_add_word = [words](const BuiltinArguments& args) {
196 words->emplace_back(args[1]);
197 return Result<void>{};
198 };
199 auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> {
200 return Error() << Join(*words, " ");
201 };
202
203 // For RecoverAfterAbort
204 auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> {
205 // Since this is an expected failure, disable debuggerd to not generate a tombstone.
206 signal(SIGABRT, SIG_DFL);
207 return Error() << std::string(4097, 'f');
208 };
209 auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> {
210 return Error() << "Sane error!";
211 };
212
213 // For ContextString
214 auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> {
215 return Error() << args.context;
216 };
217
218 auto do_trigger_shutdown = [](const BuiltinArguments& args) -> Result<void> {
219 trigger_shutdown(args[1]);
220 return {};
221 };
222
223 // 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}}},
232 {"trigger_shutdown", {1, 1, {true, do_trigger_shutdown}}},
233 };
234 // clang-format on
235 return test_function_map;
236 }
237
238 } // namespace init
239 } // namespace android
240
241 // init_test.cpp contains the main entry point for all init tests.
SubcontextTestChildMain(int argc,char ** argv)242 int SubcontextTestChildMain(int argc, char** argv) {
243 auto test_function_map = android::init::BuildTestFunctionMap();
244 return android::init::SubcontextMain(argc, argv, &test_function_map);
245 }
246