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