blob: 1ab69acb328d988207c3d3f472cf2d742054e0ba [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>
Carlos Galo14471202022-12-07 23:39:05 +000024#include <android/api-level.h>
Tom Cherryad54d092017-04-19 16:18:50 -070025#include <gtest/gtest.h>
Deyao Ren07595e12022-07-15 22:02:14 +000026#include <selinux/selinux.h>
Carlos Galo14471202022-12-07 23:39:05 +000027#include <sys/resource.h>
Tom Cherryad54d092017-04-19 16:18:50 -070028
29#include "action.h"
Tom Cherry7fd3bc22018-02-13 15:36:14 -080030#include "action_manager.h"
Tom Cherry0f6417f2018-02-13 15:25:29 -080031#include "action_parser.h"
Tom Cherryd52a5b32019-07-22 16:05:36 -070032#include "builtin_arguments.h"
Tom Cherryad54d092017-04-19 16:18:50 -070033#include "builtins.h"
34#include "import_parser.h"
Deyao Ren07595e12022-07-15 22:02:14 +000035#include "init.h"
Tom Cherryad54d092017-04-19 16:18:50 -070036#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070037#include "parser.h"
Steven Moreland6f5333a2017-11-13 15:31:54 -080038#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070039#include "service_list.h"
40#include "service_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070041#include "util.h"
42
Tom Cherry0e40ba32020-07-09 08:47:24 -070043using android::base::GetIntProperty;
Jooyung Han678f0b42022-07-07 15:25:02 +090044using android::base::GetProperty;
45using android::base::SetProperty;
Deyao Ren07595e12022-07-15 22:02:14 +000046using android::base::StringReplace;
Jooyung Han678f0b42022-07-07 15:25:02 +090047using android::base::WaitForProperty;
48using namespace std::literals;
Tom Cherry0e40ba32020-07-09 08:47:24 -070049
Tom Cherry81f5d3e2017-06-22 12:53:17 -070050namespace android {
51namespace init {
52
Tom Cherryad54d092017-04-19 16:18:50 -070053using ActionManagerCommand = std::function<void(ActionManager&)>;
54
Tom Cherryd52a5b32019-07-22 16:05:36 -070055void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
Jooyung Hanbadb7de2022-05-10 03:16:51 +090056 const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
57 ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070058 Action::set_function_map(&test_function_map);
59
60 Parser parser;
Daniel Norman3df8dc52019-06-27 12:18:08 -070061 parser.AddSectionParser("service",
62 std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
Jooyung Hanbadb7de2022-05-10 03:16:51 +090063 parser.AddSectionParser("on", std::make_unique<ActionParser>(action_manager, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070064 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 Hanbadb7de2022-05-10 03:16:51 +090069 command(*action_manager);
Tom Cherryad54d092017-04-19 16:18:50 -070070 }
71
Jooyung Hanbadb7de2022-05-10 03:16:51 +090072 while (action_manager->HasMoreCommands()) {
73 action_manager->ExecuteOneCommand();
Tom Cherryad54d092017-04-19 16:18:50 -070074 }
75}
76
Tom Cherryd52a5b32019-07-22 16:05:36 -070077void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
Jooyung Hanbadb7de2022-05-10 03:16:51 +090078 const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
79 ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070080 TemporaryFile tf;
81 ASSERT_TRUE(tf.fd != -1);
82 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Jooyung Hanbadb7de2022-05-10 03:16:51 +090083 TestInit(tf.path, test_function_map, commands, action_manager, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070084}
85
86TEST(init, SimpleEventTrigger) {
87 bool expect_true = false;
88 std::string init_script =
89 R"init(
90on boot
91pass_test
92)init";
93
Tom Cherryd52a5b32019-07-22 16:05:36 -070094 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 Cherryad54d092017-04-19 16:18:50 -0700101
102 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
103 std::vector<ActionManagerCommand> commands{trigger_boot};
104
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900105 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800106 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900107 TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700108
109 EXPECT_TRUE(expect_true);
110}
111
Nikita Ioffeaaab5962019-10-10 20:42:37 +0100112TEST(init, WrongEventTrigger) {
113 std::string init_script =
114 R"init(
115on boot:
116pass_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 Cherryad54d092017-04-19 16:18:50 -0700132TEST(init, EventTriggerOrder) {
133 std::string init_script =
134 R"init(
135on boot
136execute_first
137
138on boot && property:ro.hardware=*
139execute_second
140
141on boot
142execute_third
143
144)init";
145
146 int num_executed = 0;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700147 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 Cherryad54d092017-04-19 16:18:50 -0700165
166 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
167 std::vector<ActionManagerCommand> commands{trigger_boot};
168
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900169 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800170 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900171 TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
Florian Mayer6268f6a2022-05-11 01:02:01 +0000172 EXPECT_EQ(3, num_executed);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800173}
174
175TEST(init, OverrideService) {
176 std::string init_script = R"init(
177service A something
178 class first
179
180service A something
181 class second
182 override
183
184)init";
185
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900186 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800187 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900188 TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800189 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 Cherryad54d092017-04-19 16:18:50 -0700196}
197
Bart Van Assche5d188912022-11-14 09:30:51 -0800198TEST(init, StartConsole) {
Jiyong Park5b7a51a2022-12-20 09:18:49 +0900199 if (GetProperty("ro.build.type", "") == "user") {
200 GTEST_SKIP() << "Must run on userdebug/eng builds. b/262090304";
201 return;
202 }
Bart Van Assche5d188912022-11-14 09:30:51 -0800203 std::string init_script = R"init(
204service console /system/bin/sh
205 class core
Jiyong Parkae412802022-12-15 16:29:01 +0900206 console null
Bart Van Assche5d188912022-11-14 09:30:51 -0800207 disabled
208 user root
209 group root shell log readproc
Bart Van Assche3b21d952022-11-22 16:53:05 -0800210 seclabel u:r:shell:s0
Bart Van Assche5d188912022-11-14 09:30:51 -0800211 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 Assche3b21d952022-11-22 16:53:05 -0800224 EXPECT_NE(getsid(pid), 0);
Bart Van Assche5d188912022-11-14 09:30:51 -0800225 service->Stop();
226}
227
Deyao Ren07595e12022-07-15 22:02:14 +0000228static 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
238void 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
258void 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 Ren238e9092022-07-21 23:05:13 +0000264}
265
266void 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 Ren07595e12022-07-15 22:02:14 +0000271}
272
273void InitApexService(const std::string_view& init_template) {
274 std::string init_script = StringReplace(init_template, "$selabel",
275 GetSecurityContext(), true);
276
Deyao Ren238e9092022-07-21 23:05:13 +0000277 TestInitText(init_script, BuiltinFunctionMap(), {}, &ActionManager::GetInstance(),
Deyao Ren07595e12022-07-15 22:02:14 +0000278 &ServiceList::GetInstance());
279}
280
deyaoren@google.com909bc472022-09-07 22:25:44 +0000281void 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 Ren07595e12022-07-15 22:02:14 +0000302void 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 Ren238e9092022-07-21 23:05:13 +0000306 ASSERT_EQ(num_svc, ServiceList::GetInstance().size());
Deyao Ren07595e12022-07-15 22:02:14 +0000307
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 Ren238e9092022-07-21 23:05:13 +0000317 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.com909bc472022-09-07 22:25:44 +0000326 CleanupApexServices();
Deyao Ren07595e12022-07-15 22:02:14 +0000327}
328
329TEST(init, StopServiceByApexName) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900330 if (getuid() != 0) {
331 GTEST_SKIP() << "Must be run as root.";
332 return;
333 }
Deyao Ren07595e12022-07-15 22:02:14 +0000334 std::string_view script_template = R"init(
335service 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
344TEST(init, StopMultipleServicesByApexName) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900345 if (getuid() != 0) {
346 GTEST_SKIP() << "Must be run as root.";
347 return;
348 }
Deyao Ren07595e12022-07-15 22:02:14 +0000349 std::string_view script_template = R"init(
350service apex_test_service_multiple_a /system/bin/yes
351 user shell
352 group shell
353 seclabel $selabel
354service 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
364TEST(init, StopServicesFromMultipleApexes) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900365 if (getuid() != 0) {
366 GTEST_SKIP() << "Must be run as root.";
367 return;
368 }
Deyao Ren07595e12022-07-15 22:02:14 +0000369 std::string_view apex_script_template = R"init(
370service apex_test_service_multi_apex_a /system/bin/yes
371 user shell
372 group shell
373 seclabel $selabel
374service 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(
382service 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
393TEST(init, StopServicesFromApexAndNonApex) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900394 if (getuid() != 0) {
395 GTEST_SKIP() << "Must be run as root.";
396 return;
397 }
Deyao Ren07595e12022-07-15 22:02:14 +0000398 std::string_view apex_script_template = R"init(
399service apex_test_service_apex_a /system/bin/yes
400 user shell
401 group shell
402 seclabel $selabel
403service 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(
411service 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
422TEST(init, StopServicesFromApexMixed) {
Jooyung Han93c24d72022-09-06 09:58:47 +0900423 if (getuid() != 0) {
424 GTEST_SKIP() << "Must be run as root.";
425 return;
426 }
Deyao Ren07595e12022-07-15 22:02:14 +0000427 std::string_view script_template = R"init(
428service 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(
436service 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(
444service 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 Cherryad54d092017-04-19 16:18:50 -0700455TEST(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 Cherry2cbbe9f2017-05-04 18:17:33 -0700482 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900483 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700484
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900485 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700486
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 Cherrycb0f9bb2017-09-12 15:58:47 -0700498 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700499 EXPECT_EQ(2U, args.size());
500 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700501 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700502 };
503
Tom Cherryd52a5b32019-07-22 16:05:36 -0700504 BuiltinFunctionMap test_function_map = {
505 {"execute", {1, 1, {false, execute_command}}},
506 };
Tom Cherryad54d092017-04-19 16:18:50 -0700507
508 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
509 std::vector<ActionManagerCommand> commands{trigger_boot};
510
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900511 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800512 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900513 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700514
515 EXPECT_EQ(6, num_executed);
516}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700517
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900518BuiltinFunctionMap 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
547TEST(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
576TEST(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 Ioffe51c251c2020-04-30 19:40:39 +0100605TEST(init, RejectsCriticalAndOneshotService) {
Tom Cherry0e40ba32020-07-09 08:47:24 -0700606 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 Ioffe51c251c2020-04-30 19:40:39 +0100610 std::string init_script =
611 R"init(
612service 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 Galo14471202022-12-07 23:39:05 +0000631TEST(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 Ioffe9e4b1112020-12-11 17:59:38 +0000645class 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 Cherry81f5d3e2017-06-22 12:53:17 -0700656} // namespace init
657} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700658
659int SubcontextTestChildMain(int, char**);
660int FirmwareTestChildMain(int, char**);
661
662int 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 Ioffe9e4b1112020-12-11 17:59:38 +0000672 testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
Tom Cherrydcb3d152019-08-07 16:02:28 -0700673 return RUN_ALL_TESTS();
674}