Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 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> |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 26 | #include <selinux/selinux.h> |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 27 | |
| 28 | #include "builtin_arguments.h" |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame] | 29 | #include "util.h" |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 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 | |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 42 | template <typename F> |
| 43 | void RunTest(F&& test_function) { |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 44 | auto subcontext = Subcontext({"dummy_path"}, kTestContext); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 45 | ASSERT_NE(0, subcontext.pid()); |
| 46 | |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 47 | test_function(subcontext); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 48 | |
| 49 | if (subcontext.pid() > 0) { |
| 50 | kill(subcontext.pid(), SIGTERM); |
| 51 | kill(subcontext.pid(), SIGKILL); |
| 52 | } |
| 53 | } |
| 54 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 55 | TEST(subcontext, CheckDifferentPid) { |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 56 | RunTest([](auto& subcontext) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 57 | auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"}); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 58 | ASSERT_FALSE(result.ok()); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 59 | |
Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 60 | auto pids = Split(result.error().message(), " "); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 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 | }); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST(subcontext, SetProp) { |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 69 | if (getuid() != 0) { |
| 70 | GTEST_SKIP() << "Skipping test, must be run as root."; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | RunTest([](auto& subcontext) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 75 | SetProperty("init.test.subcontext", "fail"); |
| 76 | WaitForProperty("init.test.subcontext", "fail"); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 77 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 78 | auto args = std::vector<std::string>{ |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 79 | "setprop", |
| 80 | "init.test.subcontext", |
| 81 | "success", |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 82 | }; |
| 83 | auto result = subcontext.Execute(args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 84 | ASSERT_RESULT_OK(result); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 85 | |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s)); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | TEST(subcontext, MultipleCommands) { |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 91 | RunTest([](auto& subcontext) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 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); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 107 | ASSERT_RESULT_OK(result); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"}); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 111 | ASSERT_FALSE(result.ok()); |
Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 112 | EXPECT_EQ(Join(expected_words, " "), result.error().message()); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 113 | EXPECT_EQ(first_pid, subcontext.pid()); |
| 114 | }); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST(subcontext, RecoverAfterAbort) { |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 118 | RunTest([](auto& subcontext) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 119 | auto first_pid = subcontext.pid(); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 120 | |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 121 | auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"}); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 122 | ASSERT_FALSE(result.ok()); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 123 | |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 124 | auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"}); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 125 | ASSERT_FALSE(result2.ok()); |
Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 126 | EXPECT_EQ("Sane error!", result2.error().message()); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 127 | EXPECT_NE(subcontext.pid(), first_pid); |
| 128 | }); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | TEST(subcontext, ContextString) { |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 132 | RunTest([](auto& subcontext) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 133 | auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"}); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 134 | ASSERT_FALSE(result.ok()); |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 135 | ASSERT_EQ(kTestContext, result.error().message()); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 136 | }); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame] | 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; }; |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 143 | RunTest([](auto& subcontext) { |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame] | 144 | auto result = subcontext.Execute( |
| 145 | std::vector<std::string>{"trigger_shutdown", kTestShutdownCommand}); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 146 | ASSERT_RESULT_OK(result); |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame] | 147 | }); |
| 148 | EXPECT_EQ(kTestShutdownCommand, trigger_shutdown_command); |
| 149 | } |
| 150 | |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 151 | TEST(subcontext, ExpandArgs) { |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 152 | RunTest([](auto& subcontext) { |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 153 | auto args = std::vector<std::string>{ |
| 154 | "first", |
| 155 | "${ro.hardware}", |
| 156 | "$$third", |
| 157 | }; |
| 158 | auto result = subcontext.ExpandArgs(args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 159 | ASSERT_RESULT_OK(result); |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 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 | |
| 167 | TEST(subcontext, ExpandArgsFailure) { |
Tom Cherry | 1c005f3 | 2019-11-20 15:51:36 -0800 | [diff] [blame] | 168 | RunTest([](auto& subcontext) { |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 169 | auto args = std::vector<std::string>{ |
| 170 | "first", |
| 171 | "${", |
| 172 | }; |
| 173 | auto result = subcontext.ExpandArgs(args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 174 | ASSERT_FALSE(result.ok()); |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 175 | EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }", |
| 176 | result.error().message()); |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 177 | }); |
| 178 | } |
| 179 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 180 | BuiltinFunctionMap BuildTestFunctionMap() { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 181 | // For CheckDifferentPid |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 182 | auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> { |
| 183 | return Error() << getpid() << " " << getppid(); |
| 184 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 185 | |
| 186 | // For SetProp |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 187 | auto do_setprop = [](const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 188 | android::base::SetProperty(args[1], args[2]); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 189 | return Result<void>{}; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 190 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 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>>(); |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 195 | auto do_add_word = [words](const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 196 | words->emplace_back(args[1]); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 197 | return Result<void>{}; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 198 | }; |
| 199 | auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> { |
| 200 | return Error() << Join(*words, " "); |
| 201 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 202 | |
| 203 | // For RecoverAfterAbort |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 204 | auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> { |
| 205 | return Error() << std::string(4097, 'f'); |
| 206 | }; |
| 207 | auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> { |
| 208 | return Error() << "Sane error!"; |
| 209 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 210 | |
| 211 | // For ContextString |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 212 | auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> { |
| 213 | return Error() << args.context; |
| 214 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 215 | |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame] | 216 | auto do_trigger_shutdown = [](const BuiltinArguments& args) -> Result<void> { |
| 217 | trigger_shutdown(args[1]); |
| 218 | return {}; |
| 219 | }; |
| 220 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 221 | // clang-format off |
| 222 | BuiltinFunctionMap test_function_map = { |
| 223 | {"return_pids_as_error", {0, 0, {true, do_return_pids_as_error}}}, |
| 224 | {"setprop", {2, 2, {true, do_setprop}}}, |
| 225 | {"add_word", {1, 1, {true, do_add_word}}}, |
| 226 | {"return_words_as_error", {0, 0, {true, do_return_words_as_error}}}, |
| 227 | {"cause_log_fatal", {0, 0, {true, do_cause_log_fatal}}}, |
| 228 | {"generate_sane_error", {0, 0, {true, do_generate_sane_error}}}, |
| 229 | {"return_context_as_error", {0, 0, {true, do_return_context_as_error}}}, |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame] | 230 | {"trigger_shutdown", {1, 1, {true, do_trigger_shutdown}}}, |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 231 | }; |
| 232 | // clang-format on |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 233 | return test_function_map; |
| 234 | } |
| 235 | |
| 236 | } // namespace init |
| 237 | } // namespace android |
| 238 | |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 239 | // init_test.cpp contains the main entry point for all init tests. |
| 240 | int SubcontextTestChildMain(int argc, char** argv) { |
| 241 | auto test_function_map = android::init::BuildTestFunctionMap(); |
| 242 | return android::init::SubcontextMain(argc, argv, &test_function_map); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 243 | } |