| 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 | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | using namespace std::literals; | 
|  | 31 |  | 
|  | 32 | using android::base::GetProperty; | 
|  | 33 | using android::base::Join; | 
|  | 34 | using android::base::SetProperty; | 
|  | 35 | using android::base::Split; | 
|  | 36 | using android::base::WaitForProperty; | 
|  | 37 |  | 
|  | 38 | namespace android { | 
|  | 39 | namespace init { | 
|  | 40 |  | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 41 | // I would use test fixtures, but I cannot skip the test if not root with them, so instead we have | 
|  | 42 | // this test runner. | 
|  | 43 | template <typename F> | 
|  | 44 | void RunTest(F&& test_function) { | 
|  | 45 | if (getuid() != 0) { | 
| Tom Cherry | 17b2be0 | 2019-08-20 10:43:48 -0700 | [diff] [blame] | 46 | GTEST_SKIP() << "Skipping test, must be run as root."; | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 47 | return; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | char* context; | 
|  | 51 | ASSERT_EQ(0, getcon(&context)); | 
|  | 52 | auto context_string = std::string(context); | 
|  | 53 | free(context); | 
|  | 54 |  | 
| Tom Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 55 | auto subcontext = Subcontext({"dummy_path"}, context_string); | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 56 | ASSERT_NE(0, subcontext.pid()); | 
|  | 57 |  | 
|  | 58 | test_function(subcontext, context_string); | 
|  | 59 |  | 
|  | 60 | if (subcontext.pid() > 0) { | 
|  | 61 | kill(subcontext.pid(), SIGTERM); | 
|  | 62 | kill(subcontext.pid(), SIGKILL); | 
|  | 63 | } | 
|  | 64 | } | 
|  | 65 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 66 | TEST(subcontext, CheckDifferentPid) { | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 67 | RunTest([](auto& subcontext, auto& context_string) { | 
|  | 68 | auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"}); | 
|  | 69 | ASSERT_FALSE(result); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 70 |  | 
| Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 71 | auto pids = Split(result.error().message(), " "); | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 72 | ASSERT_EQ(2U, pids.size()); | 
|  | 73 | auto our_pid = std::to_string(getpid()); | 
|  | 74 | EXPECT_NE(our_pid, pids[0]); | 
|  | 75 | EXPECT_EQ(our_pid, pids[1]); | 
|  | 76 | }); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | TEST(subcontext, SetProp) { | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 80 | RunTest([](auto& subcontext, auto& context_string) { | 
|  | 81 | SetProperty("init.test.subcontext", "fail"); | 
|  | 82 | WaitForProperty("init.test.subcontext", "fail"); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 83 |  | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 84 | auto args = std::vector<std::string>{ | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 85 | "setprop", | 
|  | 86 | "init.test.subcontext", | 
|  | 87 | "success", | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 88 | }; | 
|  | 89 | auto result = subcontext.Execute(args); | 
|  | 90 | ASSERT_TRUE(result) << result.error(); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 91 |  | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s)); | 
|  | 93 | }); | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | TEST(subcontext, MultipleCommands) { | 
|  | 97 | RunTest([](auto& subcontext, auto& context_string) { | 
|  | 98 | auto first_pid = subcontext.pid(); | 
|  | 99 |  | 
|  | 100 | auto expected_words = std::vector<std::string>{ | 
|  | 101 | "this", | 
|  | 102 | "is", | 
|  | 103 | "a", | 
|  | 104 | "test", | 
|  | 105 | }; | 
|  | 106 |  | 
|  | 107 | for (const auto& word : expected_words) { | 
|  | 108 | auto args = std::vector<std::string>{ | 
|  | 109 | "add_word", | 
|  | 110 | word, | 
|  | 111 | }; | 
|  | 112 | auto result = subcontext.Execute(args); | 
|  | 113 | ASSERT_TRUE(result) << result.error(); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"}); | 
|  | 117 | ASSERT_FALSE(result); | 
| Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 118 | EXPECT_EQ(Join(expected_words, " "), result.error().message()); | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 119 | EXPECT_EQ(first_pid, subcontext.pid()); | 
|  | 120 | }); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
|  | 123 | TEST(subcontext, RecoverAfterAbort) { | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 124 | RunTest([](auto& subcontext, auto& context_string) { | 
|  | 125 | auto first_pid = subcontext.pid(); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 126 |  | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 127 | auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"}); | 
|  | 128 | ASSERT_FALSE(result); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 129 |  | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 130 | auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"}); | 
|  | 131 | ASSERT_FALSE(result2); | 
| Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 132 | EXPECT_EQ("Sane error!", result2.error().message()); | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 133 | EXPECT_NE(subcontext.pid(), first_pid); | 
|  | 134 | }); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
|  | 137 | TEST(subcontext, ContextString) { | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 138 | RunTest([](auto& subcontext, auto& context_string) { | 
|  | 139 | auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"}); | 
|  | 140 | ASSERT_FALSE(result); | 
| Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 141 | ASSERT_EQ(context_string, result.error().message()); | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 142 | }); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 145 | TEST(subcontext, ExpandArgs) { | 
|  | 146 | RunTest([](auto& subcontext, auto& context_string) { | 
|  | 147 | auto args = std::vector<std::string>{ | 
|  | 148 | "first", | 
|  | 149 | "${ro.hardware}", | 
|  | 150 | "$$third", | 
|  | 151 | }; | 
|  | 152 | auto result = subcontext.ExpandArgs(args); | 
|  | 153 | ASSERT_TRUE(result) << result.error(); | 
|  | 154 | ASSERT_EQ(3U, result->size()); | 
|  | 155 | EXPECT_EQ(args[0], result->at(0)); | 
|  | 156 | EXPECT_EQ(GetProperty("ro.hardware", ""), result->at(1)); | 
|  | 157 | EXPECT_EQ("$third", result->at(2)); | 
|  | 158 | }); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | TEST(subcontext, ExpandArgsFailure) { | 
|  | 162 | RunTest([](auto& subcontext, auto& context_string) { | 
|  | 163 | auto args = std::vector<std::string>{ | 
|  | 164 | "first", | 
|  | 165 | "${", | 
|  | 166 | }; | 
|  | 167 | auto result = subcontext.ExpandArgs(args); | 
|  | 168 | ASSERT_FALSE(result); | 
| Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 169 | EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }", | 
|  | 170 | result.error().message()); | 
| Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 171 | }); | 
|  | 172 | } | 
|  | 173 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 174 | BuiltinFunctionMap BuildTestFunctionMap() { | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 175 | // For CheckDifferentPid | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 176 | auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> { | 
|  | 177 | return Error() << getpid() << " " << getppid(); | 
|  | 178 | }; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 179 |  | 
|  | 180 | // For SetProp | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 181 | auto do_setprop = [](const BuiltinArguments& args) { | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 182 | android::base::SetProperty(args[1], args[2]); | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 183 | return Result<void>{}; | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 184 | }; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 185 |  | 
|  | 186 | // For MultipleCommands | 
|  | 187 | // Using a shared_ptr to extend lifetime of words to both lambdas | 
|  | 188 | auto words = std::make_shared<std::vector<std::string>>(); | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 189 | auto do_add_word = [words](const BuiltinArguments& args) { | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 190 | words->emplace_back(args[1]); | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 191 | return Result<void>{}; | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 192 | }; | 
|  | 193 | auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> { | 
|  | 194 | return Error() << Join(*words, " "); | 
|  | 195 | }; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 196 |  | 
|  | 197 | // For RecoverAfterAbort | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 198 | auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> { | 
|  | 199 | return Error() << std::string(4097, 'f'); | 
|  | 200 | }; | 
|  | 201 | auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> { | 
|  | 202 | return Error() << "Sane error!"; | 
|  | 203 | }; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 204 |  | 
|  | 205 | // For ContextString | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 206 | auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> { | 
|  | 207 | return Error() << args.context; | 
|  | 208 | }; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 209 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 210 | // clang-format off | 
|  | 211 | BuiltinFunctionMap test_function_map = { | 
|  | 212 | {"return_pids_as_error",        {0,     0,      {true,  do_return_pids_as_error}}}, | 
|  | 213 | {"setprop",                     {2,     2,      {true,  do_setprop}}}, | 
|  | 214 | {"add_word",                    {1,     1,      {true,  do_add_word}}}, | 
|  | 215 | {"return_words_as_error",       {0,     0,      {true,  do_return_words_as_error}}}, | 
|  | 216 | {"cause_log_fatal",             {0,     0,      {true,  do_cause_log_fatal}}}, | 
|  | 217 | {"generate_sane_error",         {0,     0,      {true,  do_generate_sane_error}}}, | 
|  | 218 | {"return_context_as_error",     {0,     0,      {true,  do_return_context_as_error}}}, | 
|  | 219 | }; | 
|  | 220 | // clang-format on | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 221 | return test_function_map; | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | }  // namespace init | 
|  | 225 | }  // namespace android | 
|  | 226 |  | 
| Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 227 | // init_test.cpp contains the main entry point for all init tests. | 
|  | 228 | int SubcontextTestChildMain(int argc, char** argv) { | 
|  | 229 | auto test_function_map = android::init::BuildTestFunctionMap(); | 
|  | 230 | return android::init::SubcontextMain(argc, argv, &test_function_map); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 231 | } |