blob: 529bbdf9dabea9bdd3eef6bcdf1937a4dd551343 [file] [log] [blame]
Tom Cherryad54d092017-04-19 16:18:50 -07001/*
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 Ren07595e12022-07-15 22:02:14 +000018#include <string_view>
19#include <type_traits>
Tom Cherryad54d092017-04-19 16:18:50 -070020
21#include <android-base/file.h>
Nikita Ioffe9e4b1112020-12-11 17:59:38 +000022#include <android-base/logging.h>
Tom Cherry0e40ba32020-07-09 08:47:24 -070023#include <android-base/properties.h>
Tom Cherryad54d092017-04-19 16:18:50 -070024#include <gtest/gtest.h>
Deyao Ren07595e12022-07-15 22:02:14 +000025#include <selinux/selinux.h>
Tom Cherryad54d092017-04-19 16:18:50 -070026
27#include "action.h"
Tom Cherry7fd3bc22018-02-13 15:36:14 -080028#include "action_manager.h"
Tom Cherry0f6417f2018-02-13 15:25:29 -080029#include "action_parser.h"
Tom Cherryd52a5b32019-07-22 16:05:36 -070030#include "builtin_arguments.h"
Tom Cherryad54d092017-04-19 16:18:50 -070031#include "builtins.h"
32#include "import_parser.h"
Deyao Ren07595e12022-07-15 22:02:14 +000033#include "init.h"
Tom Cherryad54d092017-04-19 16:18:50 -070034#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070035#include "parser.h"
Steven Moreland6f5333a2017-11-13 15:31:54 -080036#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070037#include "service_list.h"
38#include "service_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070039#include "util.h"
40
Tom Cherry0e40ba32020-07-09 08:47:24 -070041using android::base::GetIntProperty;
Jooyung Han678f0b42022-07-07 15:25:02 +090042using android::base::GetProperty;
43using android::base::SetProperty;
Deyao Ren07595e12022-07-15 22:02:14 +000044using android::base::StringReplace;
Jooyung Han678f0b42022-07-07 15:25:02 +090045using android::base::WaitForProperty;
46using namespace std::literals;
Tom Cherry0e40ba32020-07-09 08:47:24 -070047
Tom Cherry81f5d3e2017-06-22 12:53:17 -070048namespace android {
49namespace init {
50
Tom Cherryad54d092017-04-19 16:18:50 -070051using ActionManagerCommand = std::function<void(ActionManager&)>;
52
Tom Cherryd52a5b32019-07-22 16:05:36 -070053void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
Jooyung Hanbadb7de2022-05-10 03:16:51 +090054 const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
55 ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070056 Action::set_function_map(&test_function_map);
57
58 Parser parser;
Daniel Norman3df8dc52019-06-27 12:18:08 -070059 parser.AddSectionParser("service",
60 std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
Jooyung Hanbadb7de2022-05-10 03:16:51 +090061 parser.AddSectionParser("on", std::make_unique<ActionParser>(action_manager, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070062 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 Hanbadb7de2022-05-10 03:16:51 +090067 command(*action_manager);
Tom Cherryad54d092017-04-19 16:18:50 -070068 }
69
Jooyung Hanbadb7de2022-05-10 03:16:51 +090070 while (action_manager->HasMoreCommands()) {
71 action_manager->ExecuteOneCommand();
Tom Cherryad54d092017-04-19 16:18:50 -070072 }
73}
74
Tom Cherryd52a5b32019-07-22 16:05:36 -070075void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
Jooyung Hanbadb7de2022-05-10 03:16:51 +090076 const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
77 ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070078 TemporaryFile tf;
79 ASSERT_TRUE(tf.fd != -1);
80 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Jooyung Hanbadb7de2022-05-10 03:16:51 +090081 TestInit(tf.path, test_function_map, commands, action_manager, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070082}
83
84TEST(init, SimpleEventTrigger) {
85 bool expect_true = false;
86 std::string init_script =
87 R"init(
88on boot
89pass_test
90)init";
91
Tom Cherryd52a5b32019-07-22 16:05:36 -070092 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 Cherryad54d092017-04-19 16:18:50 -070099
100 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
101 std::vector<ActionManagerCommand> commands{trigger_boot};
102
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900103 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800104 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900105 TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700106
107 EXPECT_TRUE(expect_true);
108}
109
Nikita Ioffeaaab5962019-10-10 20:42:37 +0100110TEST(init, WrongEventTrigger) {
111 std::string init_script =
112 R"init(
113on boot:
114pass_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 Cherryad54d092017-04-19 16:18:50 -0700130TEST(init, EventTriggerOrder) {
131 std::string init_script =
132 R"init(
133on boot
134execute_first
135
136on boot && property:ro.hardware=*
137execute_second
138
139on boot
140execute_third
141
142)init";
143
144 int num_executed = 0;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700145 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 Cherryad54d092017-04-19 16:18:50 -0700163
164 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
165 std::vector<ActionManagerCommand> commands{trigger_boot};
166
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900167 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800168 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900169 TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
Florian Mayer6268f6a2022-05-11 01:02:01 +0000170 EXPECT_EQ(3, num_executed);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800171}
172
173TEST(init, OverrideService) {
174 std::string init_script = R"init(
175service A something
176 class first
177
178service A something
179 class second
180 override
181
182)init";
183
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900184 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800185 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900186 TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800187 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 Cherryad54d092017-04-19 16:18:50 -0700194}
195
Deyao Ren07595e12022-07-15 22:02:14 +0000196static 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
206void 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
226void 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 Ren238e9092022-07-21 23:05:13 +0000232}
233
234void 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 Ren07595e12022-07-15 22:02:14 +0000239}
240
241void InitApexService(const std::string_view& init_template) {
242 std::string init_script = StringReplace(init_template, "$selabel",
243 GetSecurityContext(), true);
244
Deyao Ren238e9092022-07-21 23:05:13 +0000245 TestInitText(init_script, BuiltinFunctionMap(), {}, &ActionManager::GetInstance(),
Deyao Ren07595e12022-07-15 22:02:14 +0000246 &ServiceList::GetInstance());
247}
248
249void 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 Ren238e9092022-07-21 23:05:13 +0000253 ASSERT_EQ(num_svc, ServiceList::GetInstance().size());
Deyao Ren07595e12022-07-15 22:02:14 +0000254
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 Ren238e9092022-07-21 23:05:13 +0000264 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 Ren07595e12022-07-15 22:02:14 +0000280}
281
282TEST(init, StopServiceByApexName) {
283 std::string_view script_template = R"init(
284service apex_test_service /system/bin/yes
285 user shell
286 group shell
287 seclabel $selabel
288)init";
289 InitApexService(script_template);
290 TestApexServicesInit({"apex_test_service"}, {}, {});
291}
292
293TEST(init, StopMultipleServicesByApexName) {
294 std::string_view script_template = R"init(
295service apex_test_service_multiple_a /system/bin/yes
296 user shell
297 group shell
298 seclabel $selabel
299service apex_test_service_multiple_b /system/bin/id
300 user shell
301 group shell
302 seclabel $selabel
303)init";
304 InitApexService(script_template);
305 TestApexServicesInit({"apex_test_service_multiple_a",
306 "apex_test_service_multiple_b"}, {}, {});
307}
308
309TEST(init, StopServicesFromMultipleApexes) {
310 std::string_view apex_script_template = R"init(
311service apex_test_service_multi_apex_a /system/bin/yes
312 user shell
313 group shell
314 seclabel $selabel
315service apex_test_service_multi_apex_b /system/bin/id
316 user shell
317 group shell
318 seclabel $selabel
319)init";
320 InitApexService(apex_script_template);
321
322 std::string_view other_apex_script_template = R"init(
323service apex_test_service_multi_apex_c /system/bin/yes
324 user shell
325 group shell
326 seclabel $selabel
327)init";
328 InitApexService(other_apex_script_template);
329
330 TestApexServicesInit({"apex_test_service_multi_apex_a",
331 "apex_test_service_multi_apex_b"}, {"apex_test_service_multi_apex_c"}, {});
332}
333
334TEST(init, StopServicesFromApexAndNonApex) {
335 std::string_view apex_script_template = R"init(
336service apex_test_service_apex_a /system/bin/yes
337 user shell
338 group shell
339 seclabel $selabel
340service apex_test_service_apex_b /system/bin/id
341 user shell
342 group shell
343 seclabel $selabel
344)init";
345 InitApexService(apex_script_template);
346
347 std::string_view non_apex_script_template = R"init(
348service apex_test_service_non_apex /system/bin/yes
349 user shell
350 group shell
351 seclabel $selabel
352)init";
353 InitApexService(non_apex_script_template);
354
355 TestApexServicesInit({"apex_test_service_apex_a",
356 "apex_test_service_apex_b"}, {}, {"apex_test_service_non_apex"});
357}
358
359TEST(init, StopServicesFromApexMixed) {
360 std::string_view script_template = R"init(
361service apex_test_service_mixed_a /system/bin/yes
362 user shell
363 group shell
364 seclabel $selabel
365)init";
366 InitApexService(script_template);
367
368 std::string_view other_apex_script_template = R"init(
369service apex_test_service_mixed_b /system/bin/yes
370 user shell
371 group shell
372 seclabel $selabel
373)init";
374 InitApexService(other_apex_script_template);
375
376 std::string_view non_apex_script_template = R"init(
377service apex_test_service_mixed_c /system/bin/yes
378 user shell
379 group shell
380 seclabel $selabel
381)init";
382 InitApexService(non_apex_script_template);
383
384 TestApexServicesInit({"apex_test_service_mixed_a"},
385 {"apex_test_service_mixed_b"}, {"apex_test_service_mixed_c"});
386}
387
Tom Cherryad54d092017-04-19 16:18:50 -0700388TEST(init, EventTriggerOrderMultipleFiles) {
389 // 6 total files, which should have their triggers executed in the following order:
390 // 1: start - original script parsed
391 // 2: first_import - immediately imported by first_script
392 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
393 // 4: a_import - file imported by dir_a
394 // 5: dir_b - file named 'b.rc' in dir
395 // 6: last_import - imported after dir is imported
396
397 TemporaryFile first_import;
398 ASSERT_TRUE(first_import.fd != -1);
399 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
400
401 TemporaryFile dir_a_import;
402 ASSERT_TRUE(dir_a_import.fd != -1);
403 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
404
405 TemporaryFile last_import;
406 ASSERT_TRUE(last_import.fd != -1);
407 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
408
409 TemporaryDir dir;
410 // clang-format off
411 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
412 "on boot\n"
413 "execute 3";
414 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700415 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900416 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700417
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900418 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700419
420 // clang-format off
421 std::string start_script = "import " + std::string(first_import.path) + "\n"
422 "import " + std::string(dir.path) + "\n"
423 "import " + std::string(last_import.path) + "\n"
424 "on boot\n"
425 "execute 1";
426 // clang-format on
427 TemporaryFile start;
428 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
429
430 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700431 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700432 EXPECT_EQ(2U, args.size());
433 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700434 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700435 };
436
Tom Cherryd52a5b32019-07-22 16:05:36 -0700437 BuiltinFunctionMap test_function_map = {
438 {"execute", {1, 1, {false, execute_command}}},
439 };
Tom Cherryad54d092017-04-19 16:18:50 -0700440
441 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
442 std::vector<ActionManagerCommand> commands{trigger_boot};
443
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900444 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800445 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900446 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700447
448 EXPECT_EQ(6, num_executed);
449}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700450
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900451BuiltinFunctionMap GetTestFunctionMapForLazyLoad(int& num_executed, ActionManager& action_manager) {
452 auto execute_command = [&num_executed](const BuiltinArguments& args) {
453 EXPECT_EQ(2U, args.size());
454 EXPECT_EQ(++num_executed, std::stoi(args[1]));
455 return Result<void>{};
456 };
457 auto load_command = [&action_manager](const BuiltinArguments& args) -> Result<void> {
458 EXPECT_EQ(2U, args.size());
459 Parser parser;
460 parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, nullptr));
461 if (!parser.ParseConfig(args[1])) {
462 return Error() << "Failed to load";
463 }
464 return Result<void>{};
465 };
466 auto trigger_command = [&action_manager](const BuiltinArguments& args) {
467 EXPECT_EQ(2U, args.size());
468 LOG(INFO) << "Queue event trigger: " << args[1];
469 action_manager.QueueEventTrigger(args[1]);
470 return Result<void>{};
471 };
472 BuiltinFunctionMap test_function_map = {
473 {"execute", {1, 1, {false, execute_command}}},
474 {"load", {1, 1, {false, load_command}}},
475 {"trigger", {1, 1, {false, trigger_command}}},
476 };
477 return test_function_map;
478}
479
480TEST(init, LazilyLoadedActionsCantBeTriggeredByTheSameTrigger) {
481 // "start" script loads "lazy" script. Even though "lazy" scripts
482 // defines "on boot" action, it's not executed by the current "boot"
483 // event because it's already processed.
484 TemporaryFile lazy;
485 ASSERT_TRUE(lazy.fd != -1);
486 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", lazy.fd));
487
488 TemporaryFile start;
489 // clang-format off
490 std::string start_script = "on boot\n"
491 "load " + std::string(lazy.path) + "\n"
492 "execute 1";
493 // clang-format on
494 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
495
496 int num_executed = 0;
497 ActionManager action_manager;
498 ServiceList service_list;
499 BuiltinFunctionMap test_function_map =
500 GetTestFunctionMapForLazyLoad(num_executed, action_manager);
501
502 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
503 std::vector<ActionManagerCommand> commands{trigger_boot};
504 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
505
506 EXPECT_EQ(1, num_executed);
507}
508
509TEST(init, LazilyLoadedActionsCanBeTriggeredByTheNextTrigger) {
510 // "start" script loads "lazy" script and then triggers "next" event
511 // which executes "on next" action loaded by the previous command.
512 TemporaryFile lazy;
513 ASSERT_TRUE(lazy.fd != -1);
514 ASSERT_TRUE(android::base::WriteStringToFd("on next\nexecute 2", lazy.fd));
515
516 TemporaryFile start;
517 // clang-format off
518 std::string start_script = "on boot\n"
519 "load " + std::string(lazy.path) + "\n"
520 "execute 1\n"
521 "trigger next";
522 // clang-format on
523 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
524
525 int num_executed = 0;
526 ActionManager action_manager;
527 ServiceList service_list;
528 BuiltinFunctionMap test_function_map =
529 GetTestFunctionMapForLazyLoad(num_executed, action_manager);
530
531 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
532 std::vector<ActionManagerCommand> commands{trigger_boot};
533 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
534
535 EXPECT_EQ(2, num_executed);
536}
537
Jooyung Han678f0b42022-07-07 15:25:02 +0900538TEST(init, RespondToCtlApexMessages) {
539 if (getuid() != 0) {
540 GTEST_SKIP() << "Skipping test, must be run as root.";
541 return;
542 }
543
544 std::string apex_name = "com.android.apex.cts.shim";
545 SetProperty("ctl.apex_unload", apex_name);
546 EXPECT_TRUE(WaitForProperty("init.apex." + apex_name, "unloaded", 10s));
547
548 SetProperty("ctl.apex_load", apex_name);
549 EXPECT_TRUE(WaitForProperty("init.apex." + apex_name, "loaded", 10s));
550}
551
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100552TEST(init, RejectsCriticalAndOneshotService) {
Tom Cherry0e40ba32020-07-09 08:47:24 -0700553 if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
554 GTEST_SKIP() << "Test only valid for devices launching with R or later";
555 }
556
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100557 std::string init_script =
558 R"init(
559service A something
560 class first
561 critical
562 oneshot
563)init";
564
565 TemporaryFile tf;
566 ASSERT_TRUE(tf.fd != -1);
567 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
568
569 ServiceList service_list;
570 Parser parser;
571 parser.AddSectionParser("service",
572 std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt));
573
574 ASSERT_TRUE(parser.ParseConfig(tf.path));
575 ASSERT_EQ(1u, parser.parse_error_count());
576}
577
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000578class TestCaseLogger : public ::testing::EmptyTestEventListener {
579 void OnTestStart(const ::testing::TestInfo& test_info) override {
580#ifdef __ANDROID__
581 LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " ("
582 << test_info.file() << ":" << test_info.line() << ")";
583#else
584 UNUSED(test_info);
585#endif
586 }
587};
588
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700589} // namespace init
590} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700591
592int SubcontextTestChildMain(int, char**);
593int FirmwareTestChildMain(int, char**);
594
595int main(int argc, char** argv) {
596 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
597 return SubcontextTestChildMain(argc, argv);
598 }
599
600 if (argc > 1 && !strcmp(argv[1], "firmware")) {
601 return FirmwareTestChildMain(argc, argv);
602 }
603
604 testing::InitGoogleTest(&argc, argv);
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000605 testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
Tom Cherrydcb3d152019-08-07 16:02:28 -0700606 return RUN_ALL_TESTS();
607}