blob: 18a08c7483c8cf924f867a23f9329fbda56c9355 [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
Bart Van Assche5d188912022-11-14 09:30:51 -0800196TEST(init, StartConsole) {
Jiyong Park5b7a51a2022-12-20 09:18:49 +0900197 if (GetProperty("ro.build.type", "") == "user") {
198 GTEST_SKIP() << "Must run on userdebug/eng builds. b/262090304";
199 return;
200 }
Bart Van Assche5d188912022-11-14 09:30:51 -0800201 std::string init_script = R"init(
202service console /system/bin/sh
203 class core
Jiyong Parkae412802022-12-15 16:29:01 +0900204 console null
Bart Van Assche5d188912022-11-14 09:30:51 -0800205 disabled
206 user root
207 group root shell log readproc
Bart Van Assche3b21d952022-11-22 16:53:05 -0800208 seclabel u:r:shell:s0
Bart Van Assche5d188912022-11-14 09:30:51 -0800209 setenv HOSTNAME console
210)init";
211
212 ActionManager action_manager;
213 ServiceList service_list;
214 TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
215 ASSERT_EQ(std::distance(service_list.begin(), service_list.end()), 1);
216
217 auto service = service_list.begin()->get();
218 ASSERT_NE(service, nullptr);
219 ASSERT_RESULT_OK(service->Start());
220 const pid_t pid = service->pid();
221 ASSERT_GT(pid, 0);
Bart Van Assche3b21d952022-11-22 16:53:05 -0800222 EXPECT_NE(getsid(pid), 0);
Bart Van Assche5d188912022-11-14 09:30:51 -0800223 service->Stop();
224}
225
Deyao Ren07595e12022-07-15 22:02:14 +0000226static std::string GetSecurityContext() {
227 char* ctx;
228 if (getcon(&ctx) == -1) {
229 ADD_FAILURE() << "Failed to call getcon : " << strerror(errno);
230 }
231 std::string result = std::string(ctx);
232 freecon(ctx);
233 return result;
234}
235
236void TestStartApexServices(const std::vector<std::string>& service_names,
237 const std::string& apex_name) {
238 for (auto const& svc : service_names) {
239 auto service = ServiceList::GetInstance().FindService(svc);
240 ASSERT_NE(nullptr, service);
241 ASSERT_RESULT_OK(service->Start());
242 ASSERT_TRUE(service->IsRunning());
243 LOG(INFO) << "Service " << svc << " is running";
244 if (!apex_name.empty()) {
245 service->set_filename("/apex/" + apex_name + "/init_test.rc");
246 } else {
247 service->set_filename("");
248 }
249 }
250 if (!apex_name.empty()) {
251 auto apex_services = ServiceList::GetInstance().FindServicesByApexName(apex_name);
252 EXPECT_EQ(service_names.size(), apex_services.size());
253 }
254}
255
256void TestStopApexServices(const std::vector<std::string>& service_names, bool expect_to_run) {
257 for (auto const& svc : service_names) {
258 auto service = ServiceList::GetInstance().FindService(svc);
259 ASSERT_NE(nullptr, service);
260 EXPECT_EQ(expect_to_run, service->IsRunning());
261 }
Deyao Ren238e9092022-07-21 23:05:13 +0000262}
263
264void TestRemoveApexService(const std::vector<std::string>& service_names, bool exist) {
265 for (auto const& svc : service_names) {
266 auto service = ServiceList::GetInstance().FindService(svc);
267 ASSERT_EQ(exist, service != nullptr);
268 }
Deyao Ren07595e12022-07-15 22:02:14 +0000269}
270
271void InitApexService(const std::string_view& init_template) {
272 std::string init_script = StringReplace(init_template, "$selabel",
273 GetSecurityContext(), true);
274
Deyao Ren238e9092022-07-21 23:05:13 +0000275 TestInitText(init_script, BuiltinFunctionMap(), {}, &ActionManager::GetInstance(),
Deyao Ren07595e12022-07-15 22:02:14 +0000276 &ServiceList::GetInstance());
277}
278
deyaoren@google.com909bc472022-09-07 22:25:44 +0000279void CleanupApexServices() {
280 std::vector<std::string> names;
281 for (const auto& s : ServiceList::GetInstance()) {
282 names.push_back(s->name());
283 }
284
285 for (const auto& name : names) {
286 auto s = ServiceList::GetInstance().FindService(name);
287 auto pid = s->pid();
288 ServiceList::GetInstance().RemoveService(*s);
289 if (pid > 0) {
290 kill(pid, SIGTERM);
291 kill(pid, SIGKILL);
292 }
293 }
294
295 ActionManager::GetInstance().RemoveActionIf([&](const std::unique_ptr<Action>& s) -> bool {
296 return true;
297 });
298}
299
Deyao Ren07595e12022-07-15 22:02:14 +0000300void TestApexServicesInit(const std::vector<std::string>& apex_services,
301 const std::vector<std::string>& other_apex_services,
302 const std::vector<std::string> non_apex_services) {
303 auto num_svc = apex_services.size() + other_apex_services.size() + non_apex_services.size();
Deyao Ren238e9092022-07-21 23:05:13 +0000304 ASSERT_EQ(num_svc, ServiceList::GetInstance().size());
Deyao Ren07595e12022-07-15 22:02:14 +0000305
306 TestStartApexServices(apex_services, "com.android.apex.test_service");
307 TestStartApexServices(other_apex_services, "com.android.other_apex.test_service");
308 TestStartApexServices(non_apex_services, /*apex_anme=*/ "");
309
310 StopServicesFromApex("com.android.apex.test_service");
311 TestStopApexServices(apex_services, /*expect_to_run=*/ false);
312 TestStopApexServices(other_apex_services, /*expect_to_run=*/ true);
313 TestStopApexServices(non_apex_services, /*expect_to_run=*/ true);
314
Deyao Ren238e9092022-07-21 23:05:13 +0000315 RemoveServiceAndActionFromApex("com.android.apex.test_service");
316 ASSERT_EQ(other_apex_services.size() + non_apex_services.size(),
317 ServiceList::GetInstance().size());
318
319 // TODO(b/244232142): Add test to check if actions are removed
320 TestRemoveApexService(apex_services, /*exist*/ false);
321 TestRemoveApexService(other_apex_services, /*exist*/ true);
322 TestRemoveApexService(non_apex_services, /*exist*/ true);
323
deyaoren@google.com909bc472022-09-07 22:25:44 +0000324 CleanupApexServices();
Deyao Ren07595e12022-07-15 22:02:14 +0000325}
326
327TEST(init, StopServiceByApexName) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900328 if (getuid() != 0) {
329 GTEST_SKIP() << "Must be run as root.";
330 return;
331 }
Deyao Ren07595e12022-07-15 22:02:14 +0000332 std::string_view script_template = R"init(
333service apex_test_service /system/bin/yes
334 user shell
335 group shell
336 seclabel $selabel
337)init";
338 InitApexService(script_template);
339 TestApexServicesInit({"apex_test_service"}, {}, {});
340}
341
342TEST(init, StopMultipleServicesByApexName) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900343 if (getuid() != 0) {
344 GTEST_SKIP() << "Must be run as root.";
345 return;
346 }
Deyao Ren07595e12022-07-15 22:02:14 +0000347 std::string_view script_template = R"init(
348service apex_test_service_multiple_a /system/bin/yes
349 user shell
350 group shell
351 seclabel $selabel
352service apex_test_service_multiple_b /system/bin/id
353 user shell
354 group shell
355 seclabel $selabel
356)init";
357 InitApexService(script_template);
358 TestApexServicesInit({"apex_test_service_multiple_a",
359 "apex_test_service_multiple_b"}, {}, {});
360}
361
362TEST(init, StopServicesFromMultipleApexes) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900363 if (getuid() != 0) {
364 GTEST_SKIP() << "Must be run as root.";
365 return;
366 }
Deyao Ren07595e12022-07-15 22:02:14 +0000367 std::string_view apex_script_template = R"init(
368service apex_test_service_multi_apex_a /system/bin/yes
369 user shell
370 group shell
371 seclabel $selabel
372service apex_test_service_multi_apex_b /system/bin/id
373 user shell
374 group shell
375 seclabel $selabel
376)init";
377 InitApexService(apex_script_template);
378
379 std::string_view other_apex_script_template = R"init(
380service apex_test_service_multi_apex_c /system/bin/yes
381 user shell
382 group shell
383 seclabel $selabel
384)init";
385 InitApexService(other_apex_script_template);
386
387 TestApexServicesInit({"apex_test_service_multi_apex_a",
388 "apex_test_service_multi_apex_b"}, {"apex_test_service_multi_apex_c"}, {});
389}
390
391TEST(init, StopServicesFromApexAndNonApex) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900392 if (getuid() != 0) {
393 GTEST_SKIP() << "Must be run as root.";
394 return;
395 }
Deyao Ren07595e12022-07-15 22:02:14 +0000396 std::string_view apex_script_template = R"init(
397service apex_test_service_apex_a /system/bin/yes
398 user shell
399 group shell
400 seclabel $selabel
401service apex_test_service_apex_b /system/bin/id
402 user shell
403 group shell
404 seclabel $selabel
405)init";
406 InitApexService(apex_script_template);
407
408 std::string_view non_apex_script_template = R"init(
409service apex_test_service_non_apex /system/bin/yes
410 user shell
411 group shell
412 seclabel $selabel
413)init";
414 InitApexService(non_apex_script_template);
415
416 TestApexServicesInit({"apex_test_service_apex_a",
417 "apex_test_service_apex_b"}, {}, {"apex_test_service_non_apex"});
418}
419
420TEST(init, StopServicesFromApexMixed) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900421 if (getuid() != 0) {
422 GTEST_SKIP() << "Must be run as root.";
423 return;
424 }
Deyao Ren07595e12022-07-15 22:02:14 +0000425 std::string_view script_template = R"init(
426service apex_test_service_mixed_a /system/bin/yes
427 user shell
428 group shell
429 seclabel $selabel
430)init";
431 InitApexService(script_template);
432
433 std::string_view other_apex_script_template = R"init(
434service apex_test_service_mixed_b /system/bin/yes
435 user shell
436 group shell
437 seclabel $selabel
438)init";
439 InitApexService(other_apex_script_template);
440
441 std::string_view non_apex_script_template = R"init(
442service apex_test_service_mixed_c /system/bin/yes
443 user shell
444 group shell
445 seclabel $selabel
446)init";
447 InitApexService(non_apex_script_template);
448
449 TestApexServicesInit({"apex_test_service_mixed_a"},
450 {"apex_test_service_mixed_b"}, {"apex_test_service_mixed_c"});
451}
452
Tom Cherryad54d092017-04-19 16:18:50 -0700453TEST(init, EventTriggerOrderMultipleFiles) {
454 // 6 total files, which should have their triggers executed in the following order:
455 // 1: start - original script parsed
456 // 2: first_import - immediately imported by first_script
457 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
458 // 4: a_import - file imported by dir_a
459 // 5: dir_b - file named 'b.rc' in dir
460 // 6: last_import - imported after dir is imported
461
462 TemporaryFile first_import;
463 ASSERT_TRUE(first_import.fd != -1);
464 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
465
466 TemporaryFile dir_a_import;
467 ASSERT_TRUE(dir_a_import.fd != -1);
468 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
469
470 TemporaryFile last_import;
471 ASSERT_TRUE(last_import.fd != -1);
472 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
473
474 TemporaryDir dir;
475 // clang-format off
476 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
477 "on boot\n"
478 "execute 3";
479 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700480 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900481 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700482
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900483 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700484
485 // clang-format off
486 std::string start_script = "import " + std::string(first_import.path) + "\n"
487 "import " + std::string(dir.path) + "\n"
488 "import " + std::string(last_import.path) + "\n"
489 "on boot\n"
490 "execute 1";
491 // clang-format on
492 TemporaryFile start;
493 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
494
495 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700496 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700497 EXPECT_EQ(2U, args.size());
498 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700499 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700500 };
501
Tom Cherryd52a5b32019-07-22 16:05:36 -0700502 BuiltinFunctionMap test_function_map = {
503 {"execute", {1, 1, {false, execute_command}}},
504 };
Tom Cherryad54d092017-04-19 16:18:50 -0700505
506 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
507 std::vector<ActionManagerCommand> commands{trigger_boot};
508
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900509 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800510 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900511 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700512
513 EXPECT_EQ(6, num_executed);
514}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700515
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900516BuiltinFunctionMap GetTestFunctionMapForLazyLoad(int& num_executed, ActionManager& action_manager) {
517 auto execute_command = [&num_executed](const BuiltinArguments& args) {
518 EXPECT_EQ(2U, args.size());
519 EXPECT_EQ(++num_executed, std::stoi(args[1]));
520 return Result<void>{};
521 };
522 auto load_command = [&action_manager](const BuiltinArguments& args) -> Result<void> {
523 EXPECT_EQ(2U, args.size());
524 Parser parser;
525 parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, nullptr));
526 if (!parser.ParseConfig(args[1])) {
527 return Error() << "Failed to load";
528 }
529 return Result<void>{};
530 };
531 auto trigger_command = [&action_manager](const BuiltinArguments& args) {
532 EXPECT_EQ(2U, args.size());
533 LOG(INFO) << "Queue event trigger: " << args[1];
534 action_manager.QueueEventTrigger(args[1]);
535 return Result<void>{};
536 };
537 BuiltinFunctionMap test_function_map = {
538 {"execute", {1, 1, {false, execute_command}}},
539 {"load", {1, 1, {false, load_command}}},
540 {"trigger", {1, 1, {false, trigger_command}}},
541 };
542 return test_function_map;
543}
544
545TEST(init, LazilyLoadedActionsCantBeTriggeredByTheSameTrigger) {
546 // "start" script loads "lazy" script. Even though "lazy" scripts
547 // defines "on boot" action, it's not executed by the current "boot"
548 // event because it's already processed.
549 TemporaryFile lazy;
550 ASSERT_TRUE(lazy.fd != -1);
551 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", lazy.fd));
552
553 TemporaryFile start;
554 // clang-format off
555 std::string start_script = "on boot\n"
556 "load " + std::string(lazy.path) + "\n"
557 "execute 1";
558 // clang-format on
559 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
560
561 int num_executed = 0;
562 ActionManager action_manager;
563 ServiceList service_list;
564 BuiltinFunctionMap test_function_map =
565 GetTestFunctionMapForLazyLoad(num_executed, action_manager);
566
567 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
568 std::vector<ActionManagerCommand> commands{trigger_boot};
569 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
570
571 EXPECT_EQ(1, num_executed);
572}
573
574TEST(init, LazilyLoadedActionsCanBeTriggeredByTheNextTrigger) {
575 // "start" script loads "lazy" script and then triggers "next" event
576 // which executes "on next" action loaded by the previous command.
577 TemporaryFile lazy;
578 ASSERT_TRUE(lazy.fd != -1);
579 ASSERT_TRUE(android::base::WriteStringToFd("on next\nexecute 2", lazy.fd));
580
581 TemporaryFile start;
582 // clang-format off
583 std::string start_script = "on boot\n"
584 "load " + std::string(lazy.path) + "\n"
585 "execute 1\n"
586 "trigger next";
587 // clang-format on
588 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
589
590 int num_executed = 0;
591 ActionManager action_manager;
592 ServiceList service_list;
593 BuiltinFunctionMap test_function_map =
594 GetTestFunctionMapForLazyLoad(num_executed, action_manager);
595
596 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
597 std::vector<ActionManagerCommand> commands{trigger_boot};
598 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
599
600 EXPECT_EQ(2, num_executed);
601}
602
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100603TEST(init, RejectsCriticalAndOneshotService) {
Tom Cherry0e40ba32020-07-09 08:47:24 -0700604 if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
605 GTEST_SKIP() << "Test only valid for devices launching with R or later";
606 }
607
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100608 std::string init_script =
609 R"init(
610service A something
611 class first
612 critical
613 oneshot
614)init";
615
616 TemporaryFile tf;
617 ASSERT_TRUE(tf.fd != -1);
618 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
619
620 ServiceList service_list;
621 Parser parser;
622 parser.AddSectionParser("service",
623 std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt));
624
625 ASSERT_TRUE(parser.ParseConfig(tf.path));
626 ASSERT_EQ(1u, parser.parse_error_count());
627}
628
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000629class TestCaseLogger : public ::testing::EmptyTestEventListener {
630 void OnTestStart(const ::testing::TestInfo& test_info) override {
631#ifdef __ANDROID__
632 LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " ("
633 << test_info.file() << ":" << test_info.line() << ")";
634#else
635 UNUSED(test_info);
636#endif
637 }
638};
639
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700640} // namespace init
641} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700642
643int SubcontextTestChildMain(int, char**);
644int FirmwareTestChildMain(int, char**);
645
646int main(int argc, char** argv) {
647 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
648 return SubcontextTestChildMain(argc, argv);
649 }
650
651 if (argc > 1 && !strcmp(argv[1], "firmware")) {
652 return FirmwareTestChildMain(argc, argv);
653 }
654
655 testing::InitGoogleTest(&argc, argv);
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000656 testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
Tom Cherrydcb3d152019-08-07 16:02:28 -0700657 return RUN_ALL_TESTS();
658}