| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -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 <functional> | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 18 | #include <string_view> | 
|  | 19 | #include <type_traits> | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | #include <android-base/file.h> | 
| Nikita Ioffe | 9e4b111 | 2020-12-11 17:59:38 +0000 | [diff] [blame] | 22 | #include <android-base/logging.h> | 
| Tom Cherry | 0e40ba3 | 2020-07-09 08:47:24 -0700 | [diff] [blame] | 23 | #include <android-base/properties.h> | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 24 | #include <gtest/gtest.h> | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 25 | #include <selinux/selinux.h> | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | #include "action.h" | 
| Tom Cherry | 7fd3bc2 | 2018-02-13 15:36:14 -0800 | [diff] [blame] | 28 | #include "action_manager.h" | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 29 | #include "action_parser.h" | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 30 | #include "builtin_arguments.h" | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 31 | #include "builtins.h" | 
|  | 32 | #include "import_parser.h" | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 33 | #include "init.h" | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 34 | #include "keyword_map.h" | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 35 | #include "parser.h" | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 36 | #include "service.h" | 
| Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 37 | #include "service_list.h" | 
|  | 38 | #include "service_parser.h" | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 39 | #include "util.h" | 
|  | 40 |  | 
| Tom Cherry | 0e40ba3 | 2020-07-09 08:47:24 -0700 | [diff] [blame] | 41 | using android::base::GetIntProperty; | 
| Jooyung Han | 678f0b4 | 2022-07-07 15:25:02 +0900 | [diff] [blame] | 42 | using android::base::GetProperty; | 
|  | 43 | using android::base::SetProperty; | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 44 | using android::base::StringReplace; | 
| Jooyung Han | 678f0b4 | 2022-07-07 15:25:02 +0900 | [diff] [blame] | 45 | using android::base::WaitForProperty; | 
|  | 46 | using namespace std::literals; | 
| Tom Cherry | 0e40ba3 | 2020-07-09 08:47:24 -0700 | [diff] [blame] | 47 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 48 | namespace android { | 
|  | 49 | namespace init { | 
|  | 50 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 51 | using ActionManagerCommand = std::function<void(ActionManager&)>; | 
|  | 52 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 53 | void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map, | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 54 | const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager, | 
|  | 55 | ServiceList* service_list) { | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 56 | Action::set_function_map(&test_function_map); | 
|  | 57 |  | 
|  | 58 | Parser parser; | 
| Daniel Norman | 3df8dc5 | 2019-06-27 12:18:08 -0700 | [diff] [blame] | 59 | parser.AddSectionParser("service", | 
|  | 60 | std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt)); | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 61 | parser.AddSectionParser("on", std::make_unique<ActionParser>(action_manager, nullptr)); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 62 | parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser)); | 
|  | 63 |  | 
|  | 64 | ASSERT_TRUE(parser.ParseConfig(init_script_file)); | 
|  | 65 |  | 
|  | 66 | for (const auto& command : commands) { | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 67 | command(*action_manager); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 70 | while (action_manager->HasMoreCommands()) { | 
|  | 71 | action_manager->ExecuteOneCommand(); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 72 | } | 
|  | 73 | } | 
|  | 74 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 75 | void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map, | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 76 | const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager, | 
|  | 77 | ServiceList* service_list) { | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 78 | TemporaryFile tf; | 
|  | 79 | ASSERT_TRUE(tf.fd != -1); | 
|  | 80 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 81 | TestInit(tf.path, test_function_map, commands, action_manager, service_list); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | TEST(init, SimpleEventTrigger) { | 
|  | 85 | bool expect_true = false; | 
|  | 86 | std::string init_script = | 
|  | 87 | R"init( | 
|  | 88 | on boot | 
|  | 89 | pass_test | 
|  | 90 | )init"; | 
|  | 91 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 92 | auto do_pass_test = [&expect_true](const BuiltinArguments&) { | 
|  | 93 | expect_true = true; | 
|  | 94 | return Result<void>{}; | 
|  | 95 | }; | 
|  | 96 | BuiltinFunctionMap test_function_map = { | 
|  | 97 | {"pass_test", {0, 0, {false, do_pass_test}}}, | 
|  | 98 | }; | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 99 |  | 
|  | 100 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; | 
|  | 101 | std::vector<ActionManagerCommand> commands{trigger_boot}; | 
|  | 102 |  | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 103 | ActionManager action_manager; | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 104 | ServiceList service_list; | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 105 | TestInitText(init_script, test_function_map, commands, &action_manager, &service_list); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 106 |  | 
|  | 107 | EXPECT_TRUE(expect_true); | 
|  | 108 | } | 
|  | 109 |  | 
| Nikita Ioffe | aaab596 | 2019-10-10 20:42:37 +0100 | [diff] [blame] | 110 | TEST(init, WrongEventTrigger) { | 
|  | 111 | std::string init_script = | 
|  | 112 | R"init( | 
|  | 113 | on boot: | 
|  | 114 | pass_test | 
|  | 115 | )init"; | 
|  | 116 |  | 
|  | 117 | TemporaryFile tf; | 
|  | 118 | ASSERT_TRUE(tf.fd != -1); | 
|  | 119 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); | 
|  | 120 |  | 
|  | 121 | ActionManager am; | 
|  | 122 |  | 
|  | 123 | Parser parser; | 
|  | 124 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr)); | 
|  | 125 |  | 
|  | 126 | ASSERT_TRUE(parser.ParseConfig(tf.path)); | 
|  | 127 | ASSERT_EQ(1u, parser.parse_error_count()); | 
|  | 128 | } | 
|  | 129 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 130 | TEST(init, EventTriggerOrder) { | 
|  | 131 | std::string init_script = | 
|  | 132 | R"init( | 
|  | 133 | on boot | 
|  | 134 | execute_first | 
|  | 135 |  | 
|  | 136 | on boot && property:ro.hardware=* | 
|  | 137 | execute_second | 
|  | 138 |  | 
|  | 139 | on boot | 
|  | 140 | execute_third | 
|  | 141 |  | 
|  | 142 | )init"; | 
|  | 143 |  | 
|  | 144 | int num_executed = 0; | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 145 | auto do_execute_first = [&num_executed](const BuiltinArguments&) { | 
|  | 146 | EXPECT_EQ(0, num_executed++); | 
|  | 147 | return Result<void>{}; | 
|  | 148 | }; | 
|  | 149 | auto do_execute_second = [&num_executed](const BuiltinArguments&) { | 
|  | 150 | EXPECT_EQ(1, num_executed++); | 
|  | 151 | return Result<void>{}; | 
|  | 152 | }; | 
|  | 153 | auto do_execute_third = [&num_executed](const BuiltinArguments&) { | 
|  | 154 | EXPECT_EQ(2, num_executed++); | 
|  | 155 | return Result<void>{}; | 
|  | 156 | }; | 
|  | 157 |  | 
|  | 158 | BuiltinFunctionMap test_function_map = { | 
|  | 159 | {"execute_first", {0, 0, {false, do_execute_first}}}, | 
|  | 160 | {"execute_second", {0, 0, {false, do_execute_second}}}, | 
|  | 161 | {"execute_third", {0, 0, {false, do_execute_third}}}, | 
|  | 162 | }; | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 163 |  | 
|  | 164 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; | 
|  | 165 | std::vector<ActionManagerCommand> commands{trigger_boot}; | 
|  | 166 |  | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 167 | ActionManager action_manager; | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 168 | ServiceList service_list; | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 169 | TestInitText(init_script, test_function_map, commands, &action_manager, &service_list); | 
| Florian Mayer | 6268f6a | 2022-05-11 01:02:01 +0000 | [diff] [blame] | 170 | EXPECT_EQ(3, num_executed); | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
|  | 173 | TEST(init, OverrideService) { | 
|  | 174 | std::string init_script = R"init( | 
|  | 175 | service A something | 
|  | 176 | class first | 
|  | 177 |  | 
|  | 178 | service A something | 
|  | 179 | class second | 
|  | 180 | override | 
|  | 181 |  | 
|  | 182 | )init"; | 
|  | 183 |  | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 184 | ActionManager action_manager; | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 185 | ServiceList service_list; | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 186 | TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list); | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 187 | ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end())); | 
|  | 188 |  | 
|  | 189 | auto service = service_list.begin()->get(); | 
|  | 190 | ASSERT_NE(nullptr, service); | 
|  | 191 | EXPECT_EQ(std::set<std::string>({"second"}), service->classnames()); | 
|  | 192 | EXPECT_EQ("A", service->name()); | 
|  | 193 | EXPECT_TRUE(service->is_override()); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
| Bart Van Assche | 5d18891 | 2022-11-14 09:30:51 -0800 | [diff] [blame] | 196 | TEST(init, StartConsole) { | 
| Bart Van Assche | 3b21d95 | 2022-11-22 16:53:05 -0800 | [diff] [blame] | 197 | if (access("/dev/console", F_OK) < 0) { | 
|  | 198 | GTEST_SKIP() << "/dev/console not found"; | 
|  | 199 | } | 
| Bart Van Assche | 5d18891 | 2022-11-14 09:30:51 -0800 | [diff] [blame] | 200 | std::string init_script = R"init( | 
|  | 201 | service console /system/bin/sh | 
|  | 202 | class core | 
|  | 203 | console console | 
|  | 204 | disabled | 
|  | 205 | user root | 
|  | 206 | group root shell log readproc | 
| Bart Van Assche | 3b21d95 | 2022-11-22 16:53:05 -0800 | [diff] [blame] | 207 | seclabel u:r:shell:s0 | 
| Bart Van Assche | 5d18891 | 2022-11-14 09:30:51 -0800 | [diff] [blame] | 208 | setenv HOSTNAME console | 
|  | 209 | )init"; | 
|  | 210 |  | 
|  | 211 | ActionManager action_manager; | 
|  | 212 | ServiceList service_list; | 
|  | 213 | TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list); | 
|  | 214 | ASSERT_EQ(std::distance(service_list.begin(), service_list.end()), 1); | 
|  | 215 |  | 
|  | 216 | auto service = service_list.begin()->get(); | 
|  | 217 | ASSERT_NE(service, nullptr); | 
|  | 218 | ASSERT_RESULT_OK(service->Start()); | 
|  | 219 | const pid_t pid = service->pid(); | 
|  | 220 | ASSERT_GT(pid, 0); | 
| Bart Van Assche | 3b21d95 | 2022-11-22 16:53:05 -0800 | [diff] [blame] | 221 | EXPECT_NE(getsid(pid), 0); | 
| Bart Van Assche | 5d18891 | 2022-11-14 09:30:51 -0800 | [diff] [blame] | 222 | service->Stop(); | 
|  | 223 | } | 
|  | 224 |  | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 225 | static std::string GetSecurityContext() { | 
|  | 226 | char* ctx; | 
|  | 227 | if (getcon(&ctx) == -1) { | 
|  | 228 | ADD_FAILURE() << "Failed to call getcon : " << strerror(errno); | 
|  | 229 | } | 
|  | 230 | std::string result = std::string(ctx); | 
|  | 231 | freecon(ctx); | 
|  | 232 | return result; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | void TestStartApexServices(const std::vector<std::string>& service_names, | 
|  | 236 | const std::string& apex_name) { | 
|  | 237 | for (auto const& svc : service_names) { | 
|  | 238 | auto service = ServiceList::GetInstance().FindService(svc); | 
|  | 239 | ASSERT_NE(nullptr, service); | 
|  | 240 | ASSERT_RESULT_OK(service->Start()); | 
|  | 241 | ASSERT_TRUE(service->IsRunning()); | 
|  | 242 | LOG(INFO) << "Service " << svc << " is running"; | 
|  | 243 | if (!apex_name.empty()) { | 
|  | 244 | service->set_filename("/apex/" + apex_name + "/init_test.rc"); | 
|  | 245 | } else { | 
|  | 246 | service->set_filename(""); | 
|  | 247 | } | 
|  | 248 | } | 
|  | 249 | if (!apex_name.empty()) { | 
|  | 250 | auto apex_services = ServiceList::GetInstance().FindServicesByApexName(apex_name); | 
|  | 251 | EXPECT_EQ(service_names.size(), apex_services.size()); | 
|  | 252 | } | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | void TestStopApexServices(const std::vector<std::string>& service_names, bool expect_to_run) { | 
|  | 256 | for (auto const& svc : service_names) { | 
|  | 257 | auto service = ServiceList::GetInstance().FindService(svc); | 
|  | 258 | ASSERT_NE(nullptr, service); | 
|  | 259 | EXPECT_EQ(expect_to_run, service->IsRunning()); | 
|  | 260 | } | 
| Deyao Ren | 238e909 | 2022-07-21 23:05:13 +0000 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
|  | 263 | void TestRemoveApexService(const std::vector<std::string>& service_names, bool exist) { | 
|  | 264 | for (auto const& svc : service_names) { | 
|  | 265 | auto service = ServiceList::GetInstance().FindService(svc); | 
|  | 266 | ASSERT_EQ(exist, service != nullptr); | 
|  | 267 | } | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
|  | 270 | void InitApexService(const std::string_view& init_template) { | 
|  | 271 | std::string init_script = StringReplace(init_template, "$selabel", | 
|  | 272 | GetSecurityContext(), true); | 
|  | 273 |  | 
| Deyao Ren | 238e909 | 2022-07-21 23:05:13 +0000 | [diff] [blame] | 274 | TestInitText(init_script, BuiltinFunctionMap(), {}, &ActionManager::GetInstance(), | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 275 | &ServiceList::GetInstance()); | 
|  | 276 | } | 
|  | 277 |  | 
| deyaoren@google.com | 909bc47 | 2022-09-07 22:25:44 +0000 | [diff] [blame] | 278 | void CleanupApexServices() { | 
|  | 279 | std::vector<std::string> names; | 
|  | 280 | for (const auto& s : ServiceList::GetInstance()) { | 
|  | 281 | names.push_back(s->name()); | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | for (const auto& name : names) { | 
|  | 285 | auto s = ServiceList::GetInstance().FindService(name); | 
|  | 286 | auto pid = s->pid(); | 
|  | 287 | ServiceList::GetInstance().RemoveService(*s); | 
|  | 288 | if (pid > 0) { | 
|  | 289 | kill(pid, SIGTERM); | 
|  | 290 | kill(pid, SIGKILL); | 
|  | 291 | } | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | ActionManager::GetInstance().RemoveActionIf([&](const std::unique_ptr<Action>& s) -> bool { | 
|  | 295 | return true; | 
|  | 296 | }); | 
|  | 297 | } | 
|  | 298 |  | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 299 | void TestApexServicesInit(const std::vector<std::string>& apex_services, | 
|  | 300 | const std::vector<std::string>& other_apex_services, | 
|  | 301 | const std::vector<std::string> non_apex_services) { | 
|  | 302 | auto num_svc = apex_services.size() + other_apex_services.size() + non_apex_services.size(); | 
| Deyao Ren | 238e909 | 2022-07-21 23:05:13 +0000 | [diff] [blame] | 303 | ASSERT_EQ(num_svc, ServiceList::GetInstance().size()); | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 304 |  | 
|  | 305 | TestStartApexServices(apex_services, "com.android.apex.test_service"); | 
|  | 306 | TestStartApexServices(other_apex_services, "com.android.other_apex.test_service"); | 
|  | 307 | TestStartApexServices(non_apex_services, /*apex_anme=*/ ""); | 
|  | 308 |  | 
|  | 309 | StopServicesFromApex("com.android.apex.test_service"); | 
|  | 310 | TestStopApexServices(apex_services, /*expect_to_run=*/ false); | 
|  | 311 | TestStopApexServices(other_apex_services, /*expect_to_run=*/ true); | 
|  | 312 | TestStopApexServices(non_apex_services, /*expect_to_run=*/ true); | 
|  | 313 |  | 
| Deyao Ren | 238e909 | 2022-07-21 23:05:13 +0000 | [diff] [blame] | 314 | RemoveServiceAndActionFromApex("com.android.apex.test_service"); | 
|  | 315 | ASSERT_EQ(other_apex_services.size() + non_apex_services.size(), | 
|  | 316 | ServiceList::GetInstance().size()); | 
|  | 317 |  | 
|  | 318 | // TODO(b/244232142): Add test to check if actions are removed | 
|  | 319 | TestRemoveApexService(apex_services, /*exist*/ false); | 
|  | 320 | TestRemoveApexService(other_apex_services, /*exist*/ true); | 
|  | 321 | TestRemoveApexService(non_apex_services, /*exist*/ true); | 
|  | 322 |  | 
| deyaoren@google.com | 909bc47 | 2022-09-07 22:25:44 +0000 | [diff] [blame] | 323 | CleanupApexServices(); | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
|  | 326 | TEST(init, StopServiceByApexName) { | 
| Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame] | 327 | if (getuid() != 0) { | 
|  | 328 | GTEST_SKIP() << "Must be run as root."; | 
|  | 329 | return; | 
|  | 330 | } | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 331 | std::string_view script_template = R"init( | 
|  | 332 | service apex_test_service /system/bin/yes | 
|  | 333 | user shell | 
|  | 334 | group shell | 
|  | 335 | seclabel $selabel | 
|  | 336 | )init"; | 
|  | 337 | InitApexService(script_template); | 
|  | 338 | TestApexServicesInit({"apex_test_service"}, {}, {}); | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | TEST(init, StopMultipleServicesByApexName) { | 
| Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame] | 342 | if (getuid() != 0) { | 
|  | 343 | GTEST_SKIP() << "Must be run as root."; | 
|  | 344 | return; | 
|  | 345 | } | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 346 | std::string_view script_template = R"init( | 
|  | 347 | service apex_test_service_multiple_a /system/bin/yes | 
|  | 348 | user shell | 
|  | 349 | group shell | 
|  | 350 | seclabel $selabel | 
|  | 351 | service apex_test_service_multiple_b /system/bin/id | 
|  | 352 | user shell | 
|  | 353 | group shell | 
|  | 354 | seclabel $selabel | 
|  | 355 | )init"; | 
|  | 356 | InitApexService(script_template); | 
|  | 357 | TestApexServicesInit({"apex_test_service_multiple_a", | 
|  | 358 | "apex_test_service_multiple_b"}, {}, {}); | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | TEST(init, StopServicesFromMultipleApexes) { | 
| Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame] | 362 | if (getuid() != 0) { | 
|  | 363 | GTEST_SKIP() << "Must be run as root."; | 
|  | 364 | return; | 
|  | 365 | } | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 366 | std::string_view apex_script_template = R"init( | 
|  | 367 | service apex_test_service_multi_apex_a /system/bin/yes | 
|  | 368 | user shell | 
|  | 369 | group shell | 
|  | 370 | seclabel $selabel | 
|  | 371 | service apex_test_service_multi_apex_b /system/bin/id | 
|  | 372 | user shell | 
|  | 373 | group shell | 
|  | 374 | seclabel $selabel | 
|  | 375 | )init"; | 
|  | 376 | InitApexService(apex_script_template); | 
|  | 377 |  | 
|  | 378 | std::string_view other_apex_script_template = R"init( | 
|  | 379 | service apex_test_service_multi_apex_c /system/bin/yes | 
|  | 380 | user shell | 
|  | 381 | group shell | 
|  | 382 | seclabel $selabel | 
|  | 383 | )init"; | 
|  | 384 | InitApexService(other_apex_script_template); | 
|  | 385 |  | 
|  | 386 | TestApexServicesInit({"apex_test_service_multi_apex_a", | 
|  | 387 | "apex_test_service_multi_apex_b"}, {"apex_test_service_multi_apex_c"}, {}); | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | TEST(init, StopServicesFromApexAndNonApex) { | 
| Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame] | 391 | if (getuid() != 0) { | 
|  | 392 | GTEST_SKIP() << "Must be run as root."; | 
|  | 393 | return; | 
|  | 394 | } | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 395 | std::string_view apex_script_template = R"init( | 
|  | 396 | service apex_test_service_apex_a /system/bin/yes | 
|  | 397 | user shell | 
|  | 398 | group shell | 
|  | 399 | seclabel $selabel | 
|  | 400 | service apex_test_service_apex_b /system/bin/id | 
|  | 401 | user shell | 
|  | 402 | group shell | 
|  | 403 | seclabel $selabel | 
|  | 404 | )init"; | 
|  | 405 | InitApexService(apex_script_template); | 
|  | 406 |  | 
|  | 407 | std::string_view non_apex_script_template = R"init( | 
|  | 408 | service apex_test_service_non_apex /system/bin/yes | 
|  | 409 | user shell | 
|  | 410 | group shell | 
|  | 411 | seclabel $selabel | 
|  | 412 | )init"; | 
|  | 413 | InitApexService(non_apex_script_template); | 
|  | 414 |  | 
|  | 415 | TestApexServicesInit({"apex_test_service_apex_a", | 
|  | 416 | "apex_test_service_apex_b"}, {}, {"apex_test_service_non_apex"}); | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | TEST(init, StopServicesFromApexMixed) { | 
| Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame] | 420 | if (getuid() != 0) { | 
|  | 421 | GTEST_SKIP() << "Must be run as root."; | 
|  | 422 | return; | 
|  | 423 | } | 
| Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 424 | std::string_view script_template = R"init( | 
|  | 425 | service apex_test_service_mixed_a /system/bin/yes | 
|  | 426 | user shell | 
|  | 427 | group shell | 
|  | 428 | seclabel $selabel | 
|  | 429 | )init"; | 
|  | 430 | InitApexService(script_template); | 
|  | 431 |  | 
|  | 432 | std::string_view other_apex_script_template = R"init( | 
|  | 433 | service apex_test_service_mixed_b /system/bin/yes | 
|  | 434 | user shell | 
|  | 435 | group shell | 
|  | 436 | seclabel $selabel | 
|  | 437 | )init"; | 
|  | 438 | InitApexService(other_apex_script_template); | 
|  | 439 |  | 
|  | 440 | std::string_view non_apex_script_template = R"init( | 
|  | 441 | service apex_test_service_mixed_c /system/bin/yes | 
|  | 442 | user shell | 
|  | 443 | group shell | 
|  | 444 | seclabel $selabel | 
|  | 445 | )init"; | 
|  | 446 | InitApexService(non_apex_script_template); | 
|  | 447 |  | 
|  | 448 | TestApexServicesInit({"apex_test_service_mixed_a"}, | 
|  | 449 | {"apex_test_service_mixed_b"}, {"apex_test_service_mixed_c"}); | 
|  | 450 | } | 
|  | 451 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 452 | TEST(init, EventTriggerOrderMultipleFiles) { | 
|  | 453 | // 6 total files, which should have their triggers executed in the following order: | 
|  | 454 | // 1: start - original script parsed | 
|  | 455 | // 2: first_import - immediately imported by first_script | 
|  | 456 | // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import | 
|  | 457 | // 4: a_import - file imported by dir_a | 
|  | 458 | // 5: dir_b - file named 'b.rc' in dir | 
|  | 459 | // 6: last_import - imported after dir is imported | 
|  | 460 |  | 
|  | 461 | TemporaryFile first_import; | 
|  | 462 | ASSERT_TRUE(first_import.fd != -1); | 
|  | 463 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd)); | 
|  | 464 |  | 
|  | 465 | TemporaryFile dir_a_import; | 
|  | 466 | ASSERT_TRUE(dir_a_import.fd != -1); | 
|  | 467 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd)); | 
|  | 468 |  | 
|  | 469 | TemporaryFile last_import; | 
|  | 470 | ASSERT_TRUE(last_import.fd != -1); | 
|  | 471 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd)); | 
|  | 472 |  | 
|  | 473 | TemporaryDir dir; | 
|  | 474 | // clang-format off | 
|  | 475 | std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n" | 
|  | 476 | "on boot\n" | 
|  | 477 | "execute 3"; | 
|  | 478 | // clang-format on | 
| Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 479 | // WriteFile() ensures the right mode is set | 
| Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 480 | ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script)); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 481 |  | 
| Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 482 | ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5")); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 483 |  | 
|  | 484 | // clang-format off | 
|  | 485 | std::string start_script = "import " + std::string(first_import.path) + "\n" | 
|  | 486 | "import " + std::string(dir.path) + "\n" | 
|  | 487 | "import " + std::string(last_import.path) + "\n" | 
|  | 488 | "on boot\n" | 
|  | 489 | "execute 1"; | 
|  | 490 | // clang-format on | 
|  | 491 | TemporaryFile start; | 
|  | 492 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); | 
|  | 493 |  | 
|  | 494 | int num_executed = 0; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 495 | auto execute_command = [&num_executed](const BuiltinArguments& args) { | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 496 | EXPECT_EQ(2U, args.size()); | 
|  | 497 | EXPECT_EQ(++num_executed, std::stoi(args[1])); | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 498 | return Result<void>{}; | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 499 | }; | 
|  | 500 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 501 | BuiltinFunctionMap test_function_map = { | 
|  | 502 | {"execute", {1, 1, {false, execute_command}}}, | 
|  | 503 | }; | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 504 |  | 
|  | 505 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; | 
|  | 506 | std::vector<ActionManagerCommand> commands{trigger_boot}; | 
|  | 507 |  | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 508 | ActionManager action_manager; | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 509 | ServiceList service_list; | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 510 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 511 |  | 
|  | 512 | EXPECT_EQ(6, num_executed); | 
|  | 513 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 514 |  | 
| Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 515 | BuiltinFunctionMap GetTestFunctionMapForLazyLoad(int& num_executed, ActionManager& action_manager) { | 
|  | 516 | auto execute_command = [&num_executed](const BuiltinArguments& args) { | 
|  | 517 | EXPECT_EQ(2U, args.size()); | 
|  | 518 | EXPECT_EQ(++num_executed, std::stoi(args[1])); | 
|  | 519 | return Result<void>{}; | 
|  | 520 | }; | 
|  | 521 | auto load_command = [&action_manager](const BuiltinArguments& args) -> Result<void> { | 
|  | 522 | EXPECT_EQ(2U, args.size()); | 
|  | 523 | Parser parser; | 
|  | 524 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, nullptr)); | 
|  | 525 | if (!parser.ParseConfig(args[1])) { | 
|  | 526 | return Error() << "Failed to load"; | 
|  | 527 | } | 
|  | 528 | return Result<void>{}; | 
|  | 529 | }; | 
|  | 530 | auto trigger_command = [&action_manager](const BuiltinArguments& args) { | 
|  | 531 | EXPECT_EQ(2U, args.size()); | 
|  | 532 | LOG(INFO) << "Queue event trigger: " << args[1]; | 
|  | 533 | action_manager.QueueEventTrigger(args[1]); | 
|  | 534 | return Result<void>{}; | 
|  | 535 | }; | 
|  | 536 | BuiltinFunctionMap test_function_map = { | 
|  | 537 | {"execute", {1, 1, {false, execute_command}}}, | 
|  | 538 | {"load", {1, 1, {false, load_command}}}, | 
|  | 539 | {"trigger", {1, 1, {false, trigger_command}}}, | 
|  | 540 | }; | 
|  | 541 | return test_function_map; | 
|  | 542 | } | 
|  | 543 |  | 
|  | 544 | TEST(init, LazilyLoadedActionsCantBeTriggeredByTheSameTrigger) { | 
|  | 545 | // "start" script loads "lazy" script. Even though "lazy" scripts | 
|  | 546 | // defines "on boot" action, it's not executed by the current "boot" | 
|  | 547 | // event because it's already processed. | 
|  | 548 | TemporaryFile lazy; | 
|  | 549 | ASSERT_TRUE(lazy.fd != -1); | 
|  | 550 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", lazy.fd)); | 
|  | 551 |  | 
|  | 552 | TemporaryFile start; | 
|  | 553 | // clang-format off | 
|  | 554 | std::string start_script = "on boot\n" | 
|  | 555 | "load " + std::string(lazy.path) + "\n" | 
|  | 556 | "execute 1"; | 
|  | 557 | // clang-format on | 
|  | 558 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); | 
|  | 559 |  | 
|  | 560 | int num_executed = 0; | 
|  | 561 | ActionManager action_manager; | 
|  | 562 | ServiceList service_list; | 
|  | 563 | BuiltinFunctionMap test_function_map = | 
|  | 564 | GetTestFunctionMapForLazyLoad(num_executed, action_manager); | 
|  | 565 |  | 
|  | 566 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; | 
|  | 567 | std::vector<ActionManagerCommand> commands{trigger_boot}; | 
|  | 568 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); | 
|  | 569 |  | 
|  | 570 | EXPECT_EQ(1, num_executed); | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | TEST(init, LazilyLoadedActionsCanBeTriggeredByTheNextTrigger) { | 
|  | 574 | // "start" script loads "lazy" script and then triggers "next" event | 
|  | 575 | // which executes "on next" action loaded by the previous command. | 
|  | 576 | TemporaryFile lazy; | 
|  | 577 | ASSERT_TRUE(lazy.fd != -1); | 
|  | 578 | ASSERT_TRUE(android::base::WriteStringToFd("on next\nexecute 2", lazy.fd)); | 
|  | 579 |  | 
|  | 580 | TemporaryFile start; | 
|  | 581 | // clang-format off | 
|  | 582 | std::string start_script = "on boot\n" | 
|  | 583 | "load " + std::string(lazy.path) + "\n" | 
|  | 584 | "execute 1\n" | 
|  | 585 | "trigger next"; | 
|  | 586 | // clang-format on | 
|  | 587 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); | 
|  | 588 |  | 
|  | 589 | int num_executed = 0; | 
|  | 590 | ActionManager action_manager; | 
|  | 591 | ServiceList service_list; | 
|  | 592 | BuiltinFunctionMap test_function_map = | 
|  | 593 | GetTestFunctionMapForLazyLoad(num_executed, action_manager); | 
|  | 594 |  | 
|  | 595 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; | 
|  | 596 | std::vector<ActionManagerCommand> commands{trigger_boot}; | 
|  | 597 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); | 
|  | 598 |  | 
|  | 599 | EXPECT_EQ(2, num_executed); | 
|  | 600 | } | 
|  | 601 |  | 
| Nikita Ioffe | 51c251c | 2020-04-30 19:40:39 +0100 | [diff] [blame] | 602 | TEST(init, RejectsCriticalAndOneshotService) { | 
| Tom Cherry | 0e40ba3 | 2020-07-09 08:47:24 -0700 | [diff] [blame] | 603 | if (GetIntProperty("ro.product.first_api_level", 10000) < 30) { | 
|  | 604 | GTEST_SKIP() << "Test only valid for devices launching with R or later"; | 
|  | 605 | } | 
|  | 606 |  | 
| Nikita Ioffe | 51c251c | 2020-04-30 19:40:39 +0100 | [diff] [blame] | 607 | std::string init_script = | 
|  | 608 | R"init( | 
|  | 609 | service A something | 
|  | 610 | class first | 
|  | 611 | critical | 
|  | 612 | oneshot | 
|  | 613 | )init"; | 
|  | 614 |  | 
|  | 615 | TemporaryFile tf; | 
|  | 616 | ASSERT_TRUE(tf.fd != -1); | 
|  | 617 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); | 
|  | 618 |  | 
|  | 619 | ServiceList service_list; | 
|  | 620 | Parser parser; | 
|  | 621 | parser.AddSectionParser("service", | 
|  | 622 | std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt)); | 
|  | 623 |  | 
|  | 624 | ASSERT_TRUE(parser.ParseConfig(tf.path)); | 
|  | 625 | ASSERT_EQ(1u, parser.parse_error_count()); | 
|  | 626 | } | 
|  | 627 |  | 
| Nikita Ioffe | 9e4b111 | 2020-12-11 17:59:38 +0000 | [diff] [blame] | 628 | class TestCaseLogger : public ::testing::EmptyTestEventListener { | 
|  | 629 | void OnTestStart(const ::testing::TestInfo& test_info) override { | 
|  | 630 | #ifdef __ANDROID__ | 
|  | 631 | LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " (" | 
|  | 632 | << test_info.file() << ":" << test_info.line() << ")"; | 
|  | 633 | #else | 
|  | 634 | UNUSED(test_info); | 
|  | 635 | #endif | 
|  | 636 | } | 
|  | 637 | }; | 
|  | 638 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 639 | }  // namespace init | 
|  | 640 | }  // namespace android | 
| Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 641 |  | 
|  | 642 | int SubcontextTestChildMain(int, char**); | 
|  | 643 | int FirmwareTestChildMain(int, char**); | 
|  | 644 |  | 
|  | 645 | int main(int argc, char** argv) { | 
|  | 646 | if (argc > 1 && !strcmp(argv[1], "subcontext")) { | 
|  | 647 | return SubcontextTestChildMain(argc, argv); | 
|  | 648 | } | 
|  | 649 |  | 
|  | 650 | if (argc > 1 && !strcmp(argv[1], "firmware")) { | 
|  | 651 | return FirmwareTestChildMain(argc, argv); | 
|  | 652 | } | 
|  | 653 |  | 
|  | 654 | testing::InitGoogleTest(&argc, argv); | 
| Nikita Ioffe | 9e4b111 | 2020-12-11 17:59:38 +0000 | [diff] [blame] | 655 | testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger()); | 
| Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 656 | return RUN_ALL_TESTS(); | 
|  | 657 | } |