blob: 5651a835def317e4e5bfadfea867648b29c21d62 [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>
18
19#include <android-base/file.h>
Nikita Ioffe9e4b1112020-12-11 17:59:38 +000020#include <android-base/logging.h>
Tom Cherry0e40ba32020-07-09 08:47:24 -070021#include <android-base/properties.h>
Tom Cherryad54d092017-04-19 16:18:50 -070022#include <gtest/gtest.h>
23
24#include "action.h"
Tom Cherry7fd3bc22018-02-13 15:36:14 -080025#include "action_manager.h"
Tom Cherry0f6417f2018-02-13 15:25:29 -080026#include "action_parser.h"
Tom Cherryd52a5b32019-07-22 16:05:36 -070027#include "builtin_arguments.h"
Tom Cherryad54d092017-04-19 16:18:50 -070028#include "builtins.h"
29#include "import_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070030#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070031#include "parser.h"
Steven Moreland6f5333a2017-11-13 15:31:54 -080032#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070033#include "service_list.h"
34#include "service_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070035#include "util.h"
36
Tom Cherry0e40ba32020-07-09 08:47:24 -070037using android::base::GetIntProperty;
Jooyung Han678f0b42022-07-07 15:25:02 +090038using android::base::GetProperty;
39using android::base::SetProperty;
40using android::base::WaitForProperty;
41using namespace std::literals;
Tom Cherry0e40ba32020-07-09 08:47:24 -070042
Tom Cherry81f5d3e2017-06-22 12:53:17 -070043namespace android {
44namespace init {
45
Tom Cherryad54d092017-04-19 16:18:50 -070046using ActionManagerCommand = std::function<void(ActionManager&)>;
47
Tom Cherryd52a5b32019-07-22 16:05:36 -070048void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
Jooyung Hanbadb7de2022-05-10 03:16:51 +090049 const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
50 ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070051 Action::set_function_map(&test_function_map);
52
53 Parser parser;
Daniel Norman3df8dc52019-06-27 12:18:08 -070054 parser.AddSectionParser("service",
55 std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
Jooyung Hanbadb7de2022-05-10 03:16:51 +090056 parser.AddSectionParser("on", std::make_unique<ActionParser>(action_manager, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070057 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
58
59 ASSERT_TRUE(parser.ParseConfig(init_script_file));
60
61 for (const auto& command : commands) {
Jooyung Hanbadb7de2022-05-10 03:16:51 +090062 command(*action_manager);
Tom Cherryad54d092017-04-19 16:18:50 -070063 }
64
Jooyung Hanbadb7de2022-05-10 03:16:51 +090065 while (action_manager->HasMoreCommands()) {
66 action_manager->ExecuteOneCommand();
Tom Cherryad54d092017-04-19 16:18:50 -070067 }
68}
69
Tom Cherryd52a5b32019-07-22 16:05:36 -070070void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
Jooyung Hanbadb7de2022-05-10 03:16:51 +090071 const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
72 ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070073 TemporaryFile tf;
74 ASSERT_TRUE(tf.fd != -1);
75 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Jooyung Hanbadb7de2022-05-10 03:16:51 +090076 TestInit(tf.path, test_function_map, commands, action_manager, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070077}
78
79TEST(init, SimpleEventTrigger) {
80 bool expect_true = false;
81 std::string init_script =
82 R"init(
83on boot
84pass_test
85)init";
86
Tom Cherryd52a5b32019-07-22 16:05:36 -070087 auto do_pass_test = [&expect_true](const BuiltinArguments&) {
88 expect_true = true;
89 return Result<void>{};
90 };
91 BuiltinFunctionMap test_function_map = {
92 {"pass_test", {0, 0, {false, do_pass_test}}},
93 };
Tom Cherryad54d092017-04-19 16:18:50 -070094
95 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
96 std::vector<ActionManagerCommand> commands{trigger_boot};
97
Jooyung Hanbadb7de2022-05-10 03:16:51 +090098 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -080099 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900100 TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700101
102 EXPECT_TRUE(expect_true);
103}
104
Nikita Ioffeaaab5962019-10-10 20:42:37 +0100105TEST(init, WrongEventTrigger) {
106 std::string init_script =
107 R"init(
108on boot:
109pass_test
110)init";
111
112 TemporaryFile tf;
113 ASSERT_TRUE(tf.fd != -1);
114 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
115
116 ActionManager am;
117
118 Parser parser;
119 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
120
121 ASSERT_TRUE(parser.ParseConfig(tf.path));
122 ASSERT_EQ(1u, parser.parse_error_count());
123}
124
Tom Cherryad54d092017-04-19 16:18:50 -0700125TEST(init, EventTriggerOrder) {
126 std::string init_script =
127 R"init(
128on boot
129execute_first
130
131on boot && property:ro.hardware=*
132execute_second
133
134on boot
135execute_third
136
137)init";
138
139 int num_executed = 0;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700140 auto do_execute_first = [&num_executed](const BuiltinArguments&) {
141 EXPECT_EQ(0, num_executed++);
142 return Result<void>{};
143 };
144 auto do_execute_second = [&num_executed](const BuiltinArguments&) {
145 EXPECT_EQ(1, num_executed++);
146 return Result<void>{};
147 };
148 auto do_execute_third = [&num_executed](const BuiltinArguments&) {
149 EXPECT_EQ(2, num_executed++);
150 return Result<void>{};
151 };
152
153 BuiltinFunctionMap test_function_map = {
154 {"execute_first", {0, 0, {false, do_execute_first}}},
155 {"execute_second", {0, 0, {false, do_execute_second}}},
156 {"execute_third", {0, 0, {false, do_execute_third}}},
157 };
Tom Cherryad54d092017-04-19 16:18:50 -0700158
159 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
160 std::vector<ActionManagerCommand> commands{trigger_boot};
161
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900162 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800163 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900164 TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
Florian Mayer6268f6a2022-05-11 01:02:01 +0000165 EXPECT_EQ(3, num_executed);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800166}
167
168TEST(init, OverrideService) {
169 std::string init_script = R"init(
170service A something
171 class first
172
173service A something
174 class second
175 override
176
177)init";
178
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900179 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800180 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900181 TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800182 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
183
184 auto service = service_list.begin()->get();
185 ASSERT_NE(nullptr, service);
186 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
187 EXPECT_EQ("A", service->name());
188 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700189}
190
191TEST(init, EventTriggerOrderMultipleFiles) {
192 // 6 total files, which should have their triggers executed in the following order:
193 // 1: start - original script parsed
194 // 2: first_import - immediately imported by first_script
195 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
196 // 4: a_import - file imported by dir_a
197 // 5: dir_b - file named 'b.rc' in dir
198 // 6: last_import - imported after dir is imported
199
200 TemporaryFile first_import;
201 ASSERT_TRUE(first_import.fd != -1);
202 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
203
204 TemporaryFile dir_a_import;
205 ASSERT_TRUE(dir_a_import.fd != -1);
206 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
207
208 TemporaryFile last_import;
209 ASSERT_TRUE(last_import.fd != -1);
210 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
211
212 TemporaryDir dir;
213 // clang-format off
214 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
215 "on boot\n"
216 "execute 3";
217 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700218 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900219 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700220
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900221 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700222
223 // clang-format off
224 std::string start_script = "import " + std::string(first_import.path) + "\n"
225 "import " + std::string(dir.path) + "\n"
226 "import " + std::string(last_import.path) + "\n"
227 "on boot\n"
228 "execute 1";
229 // clang-format on
230 TemporaryFile start;
231 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
232
233 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700234 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700235 EXPECT_EQ(2U, args.size());
236 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700237 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700238 };
239
Tom Cherryd52a5b32019-07-22 16:05:36 -0700240 BuiltinFunctionMap test_function_map = {
241 {"execute", {1, 1, {false, execute_command}}},
242 };
Tom Cherryad54d092017-04-19 16:18:50 -0700243
244 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
245 std::vector<ActionManagerCommand> commands{trigger_boot};
246
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900247 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800248 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900249 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700250
251 EXPECT_EQ(6, num_executed);
252}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700253
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900254BuiltinFunctionMap GetTestFunctionMapForLazyLoad(int& num_executed, ActionManager& action_manager) {
255 auto execute_command = [&num_executed](const BuiltinArguments& args) {
256 EXPECT_EQ(2U, args.size());
257 EXPECT_EQ(++num_executed, std::stoi(args[1]));
258 return Result<void>{};
259 };
260 auto load_command = [&action_manager](const BuiltinArguments& args) -> Result<void> {
261 EXPECT_EQ(2U, args.size());
262 Parser parser;
263 parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, nullptr));
264 if (!parser.ParseConfig(args[1])) {
265 return Error() << "Failed to load";
266 }
267 return Result<void>{};
268 };
269 auto trigger_command = [&action_manager](const BuiltinArguments& args) {
270 EXPECT_EQ(2U, args.size());
271 LOG(INFO) << "Queue event trigger: " << args[1];
272 action_manager.QueueEventTrigger(args[1]);
273 return Result<void>{};
274 };
275 BuiltinFunctionMap test_function_map = {
276 {"execute", {1, 1, {false, execute_command}}},
277 {"load", {1, 1, {false, load_command}}},
278 {"trigger", {1, 1, {false, trigger_command}}},
279 };
280 return test_function_map;
281}
282
283TEST(init, LazilyLoadedActionsCantBeTriggeredByTheSameTrigger) {
284 // "start" script loads "lazy" script. Even though "lazy" scripts
285 // defines "on boot" action, it's not executed by the current "boot"
286 // event because it's already processed.
287 TemporaryFile lazy;
288 ASSERT_TRUE(lazy.fd != -1);
289 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", lazy.fd));
290
291 TemporaryFile start;
292 // clang-format off
293 std::string start_script = "on boot\n"
294 "load " + std::string(lazy.path) + "\n"
295 "execute 1";
296 // clang-format on
297 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
298
299 int num_executed = 0;
300 ActionManager action_manager;
301 ServiceList service_list;
302 BuiltinFunctionMap test_function_map =
303 GetTestFunctionMapForLazyLoad(num_executed, action_manager);
304
305 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
306 std::vector<ActionManagerCommand> commands{trigger_boot};
307 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
308
309 EXPECT_EQ(1, num_executed);
310}
311
312TEST(init, LazilyLoadedActionsCanBeTriggeredByTheNextTrigger) {
313 // "start" script loads "lazy" script and then triggers "next" event
314 // which executes "on next" action loaded by the previous command.
315 TemporaryFile lazy;
316 ASSERT_TRUE(lazy.fd != -1);
317 ASSERT_TRUE(android::base::WriteStringToFd("on next\nexecute 2", lazy.fd));
318
319 TemporaryFile start;
320 // clang-format off
321 std::string start_script = "on boot\n"
322 "load " + std::string(lazy.path) + "\n"
323 "execute 1\n"
324 "trigger next";
325 // clang-format on
326 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
327
328 int num_executed = 0;
329 ActionManager action_manager;
330 ServiceList service_list;
331 BuiltinFunctionMap test_function_map =
332 GetTestFunctionMapForLazyLoad(num_executed, action_manager);
333
334 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
335 std::vector<ActionManagerCommand> commands{trigger_boot};
336 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
337
338 EXPECT_EQ(2, num_executed);
339}
340
Jooyung Han678f0b42022-07-07 15:25:02 +0900341TEST(init, RespondToCtlApexMessages) {
342 if (getuid() != 0) {
343 GTEST_SKIP() << "Skipping test, must be run as root.";
344 return;
345 }
346
347 std::string apex_name = "com.android.apex.cts.shim";
348 SetProperty("ctl.apex_unload", apex_name);
349 EXPECT_TRUE(WaitForProperty("init.apex." + apex_name, "unloaded", 10s));
350
351 SetProperty("ctl.apex_load", apex_name);
352 EXPECT_TRUE(WaitForProperty("init.apex." + apex_name, "loaded", 10s));
353}
354
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100355TEST(init, RejectsCriticalAndOneshotService) {
Tom Cherry0e40ba32020-07-09 08:47:24 -0700356 if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
357 GTEST_SKIP() << "Test only valid for devices launching with R or later";
358 }
359
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100360 std::string init_script =
361 R"init(
362service A something
363 class first
364 critical
365 oneshot
366)init";
367
368 TemporaryFile tf;
369 ASSERT_TRUE(tf.fd != -1);
370 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
371
372 ServiceList service_list;
373 Parser parser;
374 parser.AddSectionParser("service",
375 std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt));
376
377 ASSERT_TRUE(parser.ParseConfig(tf.path));
378 ASSERT_EQ(1u, parser.parse_error_count());
379}
380
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000381class TestCaseLogger : public ::testing::EmptyTestEventListener {
382 void OnTestStart(const ::testing::TestInfo& test_info) override {
383#ifdef __ANDROID__
384 LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " ("
385 << test_info.file() << ":" << test_info.line() << ")";
386#else
387 UNUSED(test_info);
388#endif
389 }
390};
391
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700392} // namespace init
393} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700394
395int SubcontextTestChildMain(int, char**);
396int FirmwareTestChildMain(int, char**);
397
398int main(int argc, char** argv) {
399 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
400 return SubcontextTestChildMain(argc, argv);
401 }
402
403 if (argc > 1 && !strcmp(argv[1], "firmware")) {
404 return FirmwareTestChildMain(argc, argv);
405 }
406
407 testing::InitGoogleTest(&argc, argv);
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000408 testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
Tom Cherrydcb3d152019-08-07 16:02:28 -0700409 return RUN_ALL_TESTS();
410}