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