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) { |
| 46 | GTEST_LOG_(INFO) << "Skipping test, must be run as root."; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | char* context; |
| 51 | ASSERT_EQ(0, getcon(&context)); |
| 52 | auto context_string = std::string(context); |
| 53 | free(context); |
| 54 | |
| 55 | auto subcontext = Subcontext("dummy_path", context_string); |
| 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); |
Jiyong Park | 8fd64c8 | 2019-05-31 03:43:34 +0900 | [diff] [blame] | 169 | EXPECT_EQ("Failed to expand '" + args[1] + "'", result.error().message()); |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 170 | }); |
| 171 | } |
| 172 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 173 | BuiltinFunctionMap BuildTestFunctionMap() { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 174 | // For CheckDifferentPid |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 175 | auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> { |
| 176 | return Error() << getpid() << " " << getppid(); |
| 177 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 178 | |
| 179 | // For SetProp |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 180 | auto do_setprop = [](const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 181 | android::base::SetProperty(args[1], args[2]); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 182 | return Result<void>{}; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 183 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 184 | |
| 185 | // For MultipleCommands |
| 186 | // Using a shared_ptr to extend lifetime of words to both lambdas |
| 187 | auto words = std::make_shared<std::vector<std::string>>(); |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 188 | auto do_add_word = [words](const BuiltinArguments& args) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 189 | words->emplace_back(args[1]); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 190 | return Result<void>{}; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 191 | }; |
| 192 | auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> { |
| 193 | return Error() << Join(*words, " "); |
| 194 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 195 | |
| 196 | // For RecoverAfterAbort |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 197 | auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> { |
| 198 | return Error() << std::string(4097, 'f'); |
| 199 | }; |
| 200 | auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> { |
| 201 | return Error() << "Sane error!"; |
| 202 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 203 | |
| 204 | // For ContextString |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 205 | auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> { |
| 206 | return Error() << args.context; |
| 207 | }; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 208 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 209 | // clang-format off |
| 210 | BuiltinFunctionMap test_function_map = { |
| 211 | {"return_pids_as_error", {0, 0, {true, do_return_pids_as_error}}}, |
| 212 | {"setprop", {2, 2, {true, do_setprop}}}, |
| 213 | {"add_word", {1, 1, {true, do_add_word}}}, |
| 214 | {"return_words_as_error", {0, 0, {true, do_return_words_as_error}}}, |
| 215 | {"cause_log_fatal", {0, 0, {true, do_cause_log_fatal}}}, |
| 216 | {"generate_sane_error", {0, 0, {true, do_generate_sane_error}}}, |
| 217 | {"return_context_as_error", {0, 0, {true, do_return_context_as_error}}}, |
| 218 | }; |
| 219 | // clang-format on |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 220 | return test_function_map; |
| 221 | } |
| 222 | |
| 223 | } // namespace init |
| 224 | } // namespace android |
| 225 | |
| 226 | int main(int argc, char** argv) { |
| 227 | if (argc > 1 && !strcmp(basename(argv[1]), "subcontext")) { |
| 228 | auto test_function_map = android::init::BuildTestFunctionMap(); |
| 229 | return android::init::SubcontextMain(argc, argv, &test_function_map); |
| 230 | } |
| 231 | |
| 232 | testing::InitGoogleTest(&argc, argv); |
| 233 | return RUN_ALL_TESTS(); |
| 234 | } |