| 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" | 
|  | 29 | #include "test_function_map.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 |  | 
| 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) { | 
|  | 47 | GTEST_LOG_(INFO) << "Skipping test, must be run as root."; | 
|  | 48 | return; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | char* context; | 
|  | 52 | ASSERT_EQ(0, getcon(&context)); | 
|  | 53 | auto context_string = std::string(context); | 
|  | 54 | free(context); | 
|  | 55 |  | 
|  | 56 | auto subcontext = Subcontext("dummy_path", context_string); | 
|  | 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 |  | 
| Tom Cherry | e6d37cd | 2017-10-19 14:15:37 -0700 | [diff] [blame] | 72 | auto pids = Split(result.error_string(), " "); | 
|  | 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); | 
|  | 119 | EXPECT_EQ(Join(expected_words, " "), result.error_string()); | 
|  | 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); | 
|  | 133 | EXPECT_EQ("Sane error!", result2.error_string()); | 
|  | 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); | 
|  | 142 | ASSERT_EQ(context_string, result.error_string()); | 
|  | 143 | }); | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | TestFunctionMap BuildTestFunctionMap() { | 
|  | 147 | TestFunctionMap test_function_map; | 
|  | 148 | // For CheckDifferentPid | 
|  | 149 | test_function_map.Add("return_pids_as_error", 0, 0, true, | 
|  | 150 | [](const BuiltinArguments& args) -> Result<Success> { | 
|  | 151 | return Error() << getpid() << " " << getppid(); | 
|  | 152 | }); | 
|  | 153 |  | 
|  | 154 | // For SetProp | 
|  | 155 | test_function_map.Add("setprop", 2, 2, true, [](const BuiltinArguments& args) { | 
|  | 156 | android::base::SetProperty(args[1], args[2]); | 
|  | 157 | return Success(); | 
|  | 158 | }); | 
|  | 159 |  | 
|  | 160 | // For MultipleCommands | 
|  | 161 | // Using a shared_ptr to extend lifetime of words to both lambdas | 
|  | 162 | auto words = std::make_shared<std::vector<std::string>>(); | 
|  | 163 | test_function_map.Add("add_word", 1, 1, true, [words](const BuiltinArguments& args) { | 
|  | 164 | words->emplace_back(args[1]); | 
|  | 165 | return Success(); | 
|  | 166 | }); | 
|  | 167 | test_function_map.Add("return_words_as_error", 0, 0, true, | 
|  | 168 | [words](const BuiltinArguments& args) -> Result<Success> { | 
|  | 169 | return Error() << Join(*words, " "); | 
|  | 170 | }); | 
|  | 171 |  | 
|  | 172 | // For RecoverAfterAbort | 
|  | 173 | test_function_map.Add("cause_log_fatal", 0, 0, true, | 
|  | 174 | [](const BuiltinArguments& args) -> Result<Success> { | 
|  | 175 | return Error() << std::string(4097, 'f'); | 
|  | 176 | }); | 
|  | 177 | test_function_map.Add( | 
|  | 178 | "generate_sane_error", 0, 0, true, | 
|  | 179 | [](const BuiltinArguments& args) -> Result<Success> { return Error() << "Sane error!"; }); | 
|  | 180 |  | 
|  | 181 | // For ContextString | 
|  | 182 | test_function_map.Add( | 
|  | 183 | "return_context_as_error", 0, 0, true, | 
|  | 184 | [](const BuiltinArguments& args) -> Result<Success> { return Error() << args.context; }); | 
|  | 185 |  | 
|  | 186 | return test_function_map; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | }  // namespace init | 
|  | 190 | }  // namespace android | 
|  | 191 |  | 
|  | 192 | int main(int argc, char** argv) { | 
|  | 193 | if (argc > 1 && !strcmp(basename(argv[1]), "subcontext")) { | 
|  | 194 | auto test_function_map = android::init::BuildTestFunctionMap(); | 
|  | 195 | return android::init::SubcontextMain(argc, argv, &test_function_map); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | testing::InitGoogleTest(&argc, argv); | 
|  | 199 | return RUN_ALL_TESTS(); | 
|  | 200 | } |