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 | |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 196 | static std::string GetSecurityContext() { |
| 197 | char* ctx; |
| 198 | if (getcon(&ctx) == -1) { |
| 199 | ADD_FAILURE() << "Failed to call getcon : " << strerror(errno); |
| 200 | } |
| 201 | std::string result = std::string(ctx); |
| 202 | freecon(ctx); |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | void TestStartApexServices(const std::vector<std::string>& service_names, |
| 207 | const std::string& apex_name) { |
| 208 | for (auto const& svc : service_names) { |
| 209 | auto service = ServiceList::GetInstance().FindService(svc); |
| 210 | ASSERT_NE(nullptr, service); |
| 211 | ASSERT_RESULT_OK(service->Start()); |
| 212 | ASSERT_TRUE(service->IsRunning()); |
| 213 | LOG(INFO) << "Service " << svc << " is running"; |
| 214 | if (!apex_name.empty()) { |
| 215 | service->set_filename("/apex/" + apex_name + "/init_test.rc"); |
| 216 | } else { |
| 217 | service->set_filename(""); |
| 218 | } |
| 219 | } |
| 220 | if (!apex_name.empty()) { |
| 221 | auto apex_services = ServiceList::GetInstance().FindServicesByApexName(apex_name); |
| 222 | EXPECT_EQ(service_names.size(), apex_services.size()); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void TestStopApexServices(const std::vector<std::string>& service_names, bool expect_to_run) { |
| 227 | for (auto const& svc : service_names) { |
| 228 | auto service = ServiceList::GetInstance().FindService(svc); |
| 229 | ASSERT_NE(nullptr, service); |
| 230 | EXPECT_EQ(expect_to_run, service->IsRunning()); |
| 231 | } |
Deyao Ren | 238e909 | 2022-07-21 23:05:13 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void TestRemoveApexService(const std::vector<std::string>& service_names, bool exist) { |
| 235 | for (auto const& svc : service_names) { |
| 236 | auto service = ServiceList::GetInstance().FindService(svc); |
| 237 | ASSERT_EQ(exist, service != nullptr); |
| 238 | } |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void InitApexService(const std::string_view& init_template) { |
| 242 | std::string init_script = StringReplace(init_template, "$selabel", |
| 243 | GetSecurityContext(), true); |
| 244 | |
Deyao Ren | 238e909 | 2022-07-21 23:05:13 +0000 | [diff] [blame] | 245 | TestInitText(init_script, BuiltinFunctionMap(), {}, &ActionManager::GetInstance(), |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 246 | &ServiceList::GetInstance()); |
| 247 | } |
| 248 | |
| 249 | void TestApexServicesInit(const std::vector<std::string>& apex_services, |
| 250 | const std::vector<std::string>& other_apex_services, |
| 251 | const std::vector<std::string> non_apex_services) { |
| 252 | 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] | 253 | ASSERT_EQ(num_svc, ServiceList::GetInstance().size()); |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 254 | |
| 255 | TestStartApexServices(apex_services, "com.android.apex.test_service"); |
| 256 | TestStartApexServices(other_apex_services, "com.android.other_apex.test_service"); |
| 257 | TestStartApexServices(non_apex_services, /*apex_anme=*/ ""); |
| 258 | |
| 259 | StopServicesFromApex("com.android.apex.test_service"); |
| 260 | TestStopApexServices(apex_services, /*expect_to_run=*/ false); |
| 261 | TestStopApexServices(other_apex_services, /*expect_to_run=*/ true); |
| 262 | TestStopApexServices(non_apex_services, /*expect_to_run=*/ true); |
| 263 | |
Deyao Ren | 238e909 | 2022-07-21 23:05:13 +0000 | [diff] [blame] | 264 | RemoveServiceAndActionFromApex("com.android.apex.test_service"); |
| 265 | ASSERT_EQ(other_apex_services.size() + non_apex_services.size(), |
| 266 | ServiceList::GetInstance().size()); |
| 267 | |
| 268 | // TODO(b/244232142): Add test to check if actions are removed |
| 269 | TestRemoveApexService(apex_services, /*exist*/ false); |
| 270 | TestRemoveApexService(other_apex_services, /*exist*/ true); |
| 271 | TestRemoveApexService(non_apex_services, /*exist*/ true); |
| 272 | |
| 273 | ServiceList::GetInstance().RemoveServiceIf([&](const std::unique_ptr<Service>& s) -> bool { |
| 274 | return true; |
| 275 | }); |
| 276 | |
| 277 | ActionManager::GetInstance().RemoveActionIf([&](const std::unique_ptr<Action>& s) -> bool { |
| 278 | return true; |
| 279 | }); |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | TEST(init, StopServiceByApexName) { |
Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame^] | 283 | if (getuid() != 0) { |
| 284 | GTEST_SKIP() << "Must be run as root."; |
| 285 | return; |
| 286 | } |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 287 | std::string_view script_template = R"init( |
| 288 | service apex_test_service /system/bin/yes |
| 289 | user shell |
| 290 | group shell |
| 291 | seclabel $selabel |
| 292 | )init"; |
| 293 | InitApexService(script_template); |
| 294 | TestApexServicesInit({"apex_test_service"}, {}, {}); |
| 295 | } |
| 296 | |
| 297 | TEST(init, StopMultipleServicesByApexName) { |
Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame^] | 298 | if (getuid() != 0) { |
| 299 | GTEST_SKIP() << "Must be run as root."; |
| 300 | return; |
| 301 | } |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 302 | std::string_view script_template = R"init( |
| 303 | service apex_test_service_multiple_a /system/bin/yes |
| 304 | user shell |
| 305 | group shell |
| 306 | seclabel $selabel |
| 307 | service apex_test_service_multiple_b /system/bin/id |
| 308 | user shell |
| 309 | group shell |
| 310 | seclabel $selabel |
| 311 | )init"; |
| 312 | InitApexService(script_template); |
| 313 | TestApexServicesInit({"apex_test_service_multiple_a", |
| 314 | "apex_test_service_multiple_b"}, {}, {}); |
| 315 | } |
| 316 | |
| 317 | TEST(init, StopServicesFromMultipleApexes) { |
Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame^] | 318 | if (getuid() != 0) { |
| 319 | GTEST_SKIP() << "Must be run as root."; |
| 320 | return; |
| 321 | } |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 322 | std::string_view apex_script_template = R"init( |
| 323 | service apex_test_service_multi_apex_a /system/bin/yes |
| 324 | user shell |
| 325 | group shell |
| 326 | seclabel $selabel |
| 327 | service apex_test_service_multi_apex_b /system/bin/id |
| 328 | user shell |
| 329 | group shell |
| 330 | seclabel $selabel |
| 331 | )init"; |
| 332 | InitApexService(apex_script_template); |
| 333 | |
| 334 | std::string_view other_apex_script_template = R"init( |
| 335 | service apex_test_service_multi_apex_c /system/bin/yes |
| 336 | user shell |
| 337 | group shell |
| 338 | seclabel $selabel |
| 339 | )init"; |
| 340 | InitApexService(other_apex_script_template); |
| 341 | |
| 342 | TestApexServicesInit({"apex_test_service_multi_apex_a", |
| 343 | "apex_test_service_multi_apex_b"}, {"apex_test_service_multi_apex_c"}, {}); |
| 344 | } |
| 345 | |
| 346 | TEST(init, StopServicesFromApexAndNonApex) { |
Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame^] | 347 | if (getuid() != 0) { |
| 348 | GTEST_SKIP() << "Must be run as root."; |
| 349 | return; |
| 350 | } |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 351 | std::string_view apex_script_template = R"init( |
| 352 | service apex_test_service_apex_a /system/bin/yes |
| 353 | user shell |
| 354 | group shell |
| 355 | seclabel $selabel |
| 356 | service apex_test_service_apex_b /system/bin/id |
| 357 | user shell |
| 358 | group shell |
| 359 | seclabel $selabel |
| 360 | )init"; |
| 361 | InitApexService(apex_script_template); |
| 362 | |
| 363 | std::string_view non_apex_script_template = R"init( |
| 364 | service apex_test_service_non_apex /system/bin/yes |
| 365 | user shell |
| 366 | group shell |
| 367 | seclabel $selabel |
| 368 | )init"; |
| 369 | InitApexService(non_apex_script_template); |
| 370 | |
| 371 | TestApexServicesInit({"apex_test_service_apex_a", |
| 372 | "apex_test_service_apex_b"}, {}, {"apex_test_service_non_apex"}); |
| 373 | } |
| 374 | |
| 375 | TEST(init, StopServicesFromApexMixed) { |
Jooyung Han | 93c24d7 | 2022-09-06 09:58:47 +0900 | [diff] [blame^] | 376 | if (getuid() != 0) { |
| 377 | GTEST_SKIP() << "Must be run as root."; |
| 378 | return; |
| 379 | } |
Deyao Ren | 07595e1 | 2022-07-15 22:02:14 +0000 | [diff] [blame] | 380 | std::string_view script_template = R"init( |
| 381 | service apex_test_service_mixed_a /system/bin/yes |
| 382 | user shell |
| 383 | group shell |
| 384 | seclabel $selabel |
| 385 | )init"; |
| 386 | InitApexService(script_template); |
| 387 | |
| 388 | std::string_view other_apex_script_template = R"init( |
| 389 | service apex_test_service_mixed_b /system/bin/yes |
| 390 | user shell |
| 391 | group shell |
| 392 | seclabel $selabel |
| 393 | )init"; |
| 394 | InitApexService(other_apex_script_template); |
| 395 | |
| 396 | std::string_view non_apex_script_template = R"init( |
| 397 | service apex_test_service_mixed_c /system/bin/yes |
| 398 | user shell |
| 399 | group shell |
| 400 | seclabel $selabel |
| 401 | )init"; |
| 402 | InitApexService(non_apex_script_template); |
| 403 | |
| 404 | TestApexServicesInit({"apex_test_service_mixed_a"}, |
| 405 | {"apex_test_service_mixed_b"}, {"apex_test_service_mixed_c"}); |
| 406 | } |
| 407 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 408 | TEST(init, EventTriggerOrderMultipleFiles) { |
| 409 | // 6 total files, which should have their triggers executed in the following order: |
| 410 | // 1: start - original script parsed |
| 411 | // 2: first_import - immediately imported by first_script |
| 412 | // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import |
| 413 | // 4: a_import - file imported by dir_a |
| 414 | // 5: dir_b - file named 'b.rc' in dir |
| 415 | // 6: last_import - imported after dir is imported |
| 416 | |
| 417 | TemporaryFile first_import; |
| 418 | ASSERT_TRUE(first_import.fd != -1); |
| 419 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd)); |
| 420 | |
| 421 | TemporaryFile dir_a_import; |
| 422 | ASSERT_TRUE(dir_a_import.fd != -1); |
| 423 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd)); |
| 424 | |
| 425 | TemporaryFile last_import; |
| 426 | ASSERT_TRUE(last_import.fd != -1); |
| 427 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd)); |
| 428 | |
| 429 | TemporaryDir dir; |
| 430 | // clang-format off |
| 431 | std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n" |
| 432 | "on boot\n" |
| 433 | "execute 3"; |
| 434 | // clang-format on |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 435 | // WriteFile() ensures the right mode is set |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 436 | 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] | 437 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 438 | 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] | 439 | |
| 440 | // clang-format off |
| 441 | std::string start_script = "import " + std::string(first_import.path) + "\n" |
| 442 | "import " + std::string(dir.path) + "\n" |
| 443 | "import " + std::string(last_import.path) + "\n" |
| 444 | "on boot\n" |
| 445 | "execute 1"; |
| 446 | // clang-format on |
| 447 | TemporaryFile start; |
| 448 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); |
| 449 | |
| 450 | int num_executed = 0; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 451 | auto execute_command = [&num_executed](const BuiltinArguments& args) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 452 | EXPECT_EQ(2U, args.size()); |
| 453 | EXPECT_EQ(++num_executed, std::stoi(args[1])); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 454 | return Result<void>{}; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 455 | }; |
| 456 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 457 | BuiltinFunctionMap test_function_map = { |
| 458 | {"execute", {1, 1, {false, execute_command}}}, |
| 459 | }; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 460 | |
| 461 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 462 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 463 | |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 464 | ActionManager action_manager; |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 465 | ServiceList service_list; |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 466 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 467 | |
| 468 | EXPECT_EQ(6, num_executed); |
| 469 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 470 | |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 471 | BuiltinFunctionMap GetTestFunctionMapForLazyLoad(int& num_executed, ActionManager& action_manager) { |
| 472 | auto execute_command = [&num_executed](const BuiltinArguments& args) { |
| 473 | EXPECT_EQ(2U, args.size()); |
| 474 | EXPECT_EQ(++num_executed, std::stoi(args[1])); |
| 475 | return Result<void>{}; |
| 476 | }; |
| 477 | auto load_command = [&action_manager](const BuiltinArguments& args) -> Result<void> { |
| 478 | EXPECT_EQ(2U, args.size()); |
| 479 | Parser parser; |
| 480 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, nullptr)); |
| 481 | if (!parser.ParseConfig(args[1])) { |
| 482 | return Error() << "Failed to load"; |
| 483 | } |
| 484 | return Result<void>{}; |
| 485 | }; |
| 486 | auto trigger_command = [&action_manager](const BuiltinArguments& args) { |
| 487 | EXPECT_EQ(2U, args.size()); |
| 488 | LOG(INFO) << "Queue event trigger: " << args[1]; |
| 489 | action_manager.QueueEventTrigger(args[1]); |
| 490 | return Result<void>{}; |
| 491 | }; |
| 492 | BuiltinFunctionMap test_function_map = { |
| 493 | {"execute", {1, 1, {false, execute_command}}}, |
| 494 | {"load", {1, 1, {false, load_command}}}, |
| 495 | {"trigger", {1, 1, {false, trigger_command}}}, |
| 496 | }; |
| 497 | return test_function_map; |
| 498 | } |
| 499 | |
| 500 | TEST(init, LazilyLoadedActionsCantBeTriggeredByTheSameTrigger) { |
| 501 | // "start" script loads "lazy" script. Even though "lazy" scripts |
| 502 | // defines "on boot" action, it's not executed by the current "boot" |
| 503 | // event because it's already processed. |
| 504 | TemporaryFile lazy; |
| 505 | ASSERT_TRUE(lazy.fd != -1); |
| 506 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", lazy.fd)); |
| 507 | |
| 508 | TemporaryFile start; |
| 509 | // clang-format off |
| 510 | std::string start_script = "on boot\n" |
| 511 | "load " + std::string(lazy.path) + "\n" |
| 512 | "execute 1"; |
| 513 | // clang-format on |
| 514 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); |
| 515 | |
| 516 | int num_executed = 0; |
| 517 | ActionManager action_manager; |
| 518 | ServiceList service_list; |
| 519 | BuiltinFunctionMap test_function_map = |
| 520 | GetTestFunctionMapForLazyLoad(num_executed, action_manager); |
| 521 | |
| 522 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 523 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 524 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); |
| 525 | |
| 526 | EXPECT_EQ(1, num_executed); |
| 527 | } |
| 528 | |
| 529 | TEST(init, LazilyLoadedActionsCanBeTriggeredByTheNextTrigger) { |
| 530 | // "start" script loads "lazy" script and then triggers "next" event |
| 531 | // which executes "on next" action loaded by the previous command. |
| 532 | TemporaryFile lazy; |
| 533 | ASSERT_TRUE(lazy.fd != -1); |
| 534 | ASSERT_TRUE(android::base::WriteStringToFd("on next\nexecute 2", lazy.fd)); |
| 535 | |
| 536 | TemporaryFile start; |
| 537 | // clang-format off |
| 538 | std::string start_script = "on boot\n" |
| 539 | "load " + std::string(lazy.path) + "\n" |
| 540 | "execute 1\n" |
| 541 | "trigger next"; |
| 542 | // clang-format on |
| 543 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); |
| 544 | |
| 545 | int num_executed = 0; |
| 546 | ActionManager action_manager; |
| 547 | ServiceList service_list; |
| 548 | BuiltinFunctionMap test_function_map = |
| 549 | GetTestFunctionMapForLazyLoad(num_executed, action_manager); |
| 550 | |
| 551 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 552 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 553 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); |
| 554 | |
| 555 | EXPECT_EQ(2, num_executed); |
| 556 | } |
| 557 | |
Nikita Ioffe | 51c251c | 2020-04-30 19:40:39 +0100 | [diff] [blame] | 558 | TEST(init, RejectsCriticalAndOneshotService) { |
Tom Cherry | 0e40ba3 | 2020-07-09 08:47:24 -0700 | [diff] [blame] | 559 | if (GetIntProperty("ro.product.first_api_level", 10000) < 30) { |
| 560 | GTEST_SKIP() << "Test only valid for devices launching with R or later"; |
| 561 | } |
| 562 | |
Nikita Ioffe | 51c251c | 2020-04-30 19:40:39 +0100 | [diff] [blame] | 563 | std::string init_script = |
| 564 | R"init( |
| 565 | service A something |
| 566 | class first |
| 567 | critical |
| 568 | oneshot |
| 569 | )init"; |
| 570 | |
| 571 | TemporaryFile tf; |
| 572 | ASSERT_TRUE(tf.fd != -1); |
| 573 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); |
| 574 | |
| 575 | ServiceList service_list; |
| 576 | Parser parser; |
| 577 | parser.AddSectionParser("service", |
| 578 | std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt)); |
| 579 | |
| 580 | ASSERT_TRUE(parser.ParseConfig(tf.path)); |
| 581 | ASSERT_EQ(1u, parser.parse_error_count()); |
| 582 | } |
| 583 | |
Nikita Ioffe | 9e4b111 | 2020-12-11 17:59:38 +0000 | [diff] [blame] | 584 | class TestCaseLogger : public ::testing::EmptyTestEventListener { |
| 585 | void OnTestStart(const ::testing::TestInfo& test_info) override { |
| 586 | #ifdef __ANDROID__ |
| 587 | LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " (" |
| 588 | << test_info.file() << ":" << test_info.line() << ")"; |
| 589 | #else |
| 590 | UNUSED(test_info); |
| 591 | #endif |
| 592 | } |
| 593 | }; |
| 594 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 595 | } // namespace init |
| 596 | } // namespace android |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 597 | |
| 598 | int SubcontextTestChildMain(int, char**); |
| 599 | int FirmwareTestChildMain(int, char**); |
| 600 | |
| 601 | int main(int argc, char** argv) { |
| 602 | if (argc > 1 && !strcmp(argv[1], "subcontext")) { |
| 603 | return SubcontextTestChildMain(argc, argv); |
| 604 | } |
| 605 | |
| 606 | if (argc > 1 && !strcmp(argv[1], "firmware")) { |
| 607 | return FirmwareTestChildMain(argc, argv); |
| 608 | } |
| 609 | |
| 610 | testing::InitGoogleTest(&argc, argv); |
Nikita Ioffe | 9e4b111 | 2020-12-11 17:59:38 +0000 | [diff] [blame] | 611 | testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger()); |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 612 | return RUN_ALL_TESTS(); |
| 613 | } |