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 | // I would use test fixtures, but I cannot skip the test if not root with them, so instead we have |
| 43 | // this test runner. |
| 44 | template <typename F> |
| 45 | void RunTest(F&& test_function) { |
| 46 | if (getuid() != 0) { |
Tom Cherry | 17b2be0 | 2019-08-20 10:43:48 -0700 | [diff] [blame] | 47 | GTEST_SKIP() << "Skipping test, must be run as root."; |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
| 51 | char* context; |
| 52 | ASSERT_EQ(0, getcon(&context)); |
| 53 | auto context_string = std::string(context); |
| 54 | free(context); |
| 55 | |
Tom Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 56 | auto subcontext = Subcontext({"dummy_path"}, context_string); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 57 | 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 Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 67 | TEST(subcontext, CheckDifferentPid) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 68 | RunTest([](auto& subcontext, auto& context_string) { |
| 69 | auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"}); |
| 70 | ASSERT_FALSE(result); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 71 | |
Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 72 | auto pids = Split(result.error().message(), " "); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 73 | 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 Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST(subcontext, SetProp) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 81 | RunTest([](auto& subcontext, auto& context_string) { |
| 82 | SetProperty("init.test.subcontext", "fail"); |
| 83 | WaitForProperty("init.test.subcontext", "fail"); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 84 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 85 | auto args = std::vector<std::string>{ |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 86 | "setprop", |
| 87 | "init.test.subcontext", |
| 88 | "success", |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 89 | }; |
| 90 | auto result = subcontext.Execute(args); |
| 91 | ASSERT_TRUE(result) << result.error(); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 92 | |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 93 | EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s)); |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | TEST(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 Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 119 | EXPECT_EQ(Join(expected_words, " "), result.error().message()); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 120 | EXPECT_EQ(first_pid, subcontext.pid()); |
| 121 | }); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST(subcontext, RecoverAfterAbort) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 125 | RunTest([](auto& subcontext, auto& context_string) { |
| 126 | auto first_pid = subcontext.pid(); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 127 | |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 128 | auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"}); |
| 129 | ASSERT_FALSE(result); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 130 | |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 131 | auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"}); |
| 132 | ASSERT_FALSE(result2); |
Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 133 | EXPECT_EQ("Sane error!", result2.error().message()); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 134 | EXPECT_NE(subcontext.pid(), first_pid); |
| 135 | }); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | TEST(subcontext, ContextString) { |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 139 | RunTest([](auto& subcontext, auto& context_string) { |
| 140 | auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"}); |
| 141 | ASSERT_FALSE(result); |
Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 142 | ASSERT_EQ(context_string, result.error().message()); |
Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 143 | }); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame^] | 146 | TEST(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 Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 158 | TEST(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 | |
| 174 | TEST(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 Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 182 | EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }", |
| 183 | result.error().message()); |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 184 | }); |
| 185 | } |
| 186 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 187 | BuiltinFunctionMap BuildTestFunctionMap() { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 188 | // For CheckDifferentPid |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 189 | auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> { |
| 190 | return Error() << getpid() << " " << getppid(); |
| 191 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 192 | |
| 193 | // For SetProp |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 194 | auto do_setprop = [](const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 195 | android::base::SetProperty(args[1], args[2]); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 196 | return Result<void>{}; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 197 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 198 | |
| 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 Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 202 | auto do_add_word = [words](const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 203 | words->emplace_back(args[1]); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 204 | return Result<void>{}; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 205 | }; |
| 206 | auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> { |
| 207 | return Error() << Join(*words, " "); |
| 208 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 209 | |
| 210 | // For RecoverAfterAbort |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 211 | 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 Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 217 | |
| 218 | // For ContextString |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 219 | auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> { |
| 220 | return Error() << args.context; |
| 221 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 222 | |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame^] | 223 | auto do_trigger_shutdown = [](const BuiltinArguments& args) -> Result<void> { |
| 224 | trigger_shutdown(args[1]); |
| 225 | return {}; |
| 226 | }; |
| 227 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 228 | // 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 Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame^] | 237 | {"trigger_shutdown", {1, 1, {true, do_trigger_shutdown}}}, |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 238 | }; |
| 239 | // clang-format on |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 240 | return test_function_map; |
| 241 | } |
| 242 | |
| 243 | } // namespace init |
| 244 | } // namespace android |
| 245 | |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 246 | // init_test.cpp contains the main entry point for all init tests. |
| 247 | int SubcontextTestChildMain(int argc, char** argv) { |
| 248 | auto test_function_map = android::init::BuildTestFunctionMap(); |
| 249 | return android::init::SubcontextMain(argc, argv, &test_function_map); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 250 | } |