blob: 0dc6ff640b5770c70f4332f8d1941e321cdf67fa [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;
38
Tom Cherry81f5d3e2017-06-22 12:53:17 -070039namespace android {
40namespace init {
41
Tom Cherryad54d092017-04-19 16:18:50 -070042using ActionManagerCommand = std::function<void(ActionManager&)>;
43
Tom Cherryd52a5b32019-07-22 16:05:36 -070044void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
Jooyung Hanbadb7de2022-05-10 03:16:51 +090045 const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
46 ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070047 Action::set_function_map(&test_function_map);
48
49 Parser parser;
Daniel Norman3df8dc52019-06-27 12:18:08 -070050 parser.AddSectionParser("service",
51 std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
Jooyung Hanbadb7de2022-05-10 03:16:51 +090052 parser.AddSectionParser("on", std::make_unique<ActionParser>(action_manager, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070053 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
54
55 ASSERT_TRUE(parser.ParseConfig(init_script_file));
56
57 for (const auto& command : commands) {
Jooyung Hanbadb7de2022-05-10 03:16:51 +090058 command(*action_manager);
Tom Cherryad54d092017-04-19 16:18:50 -070059 }
60
Jooyung Hanbadb7de2022-05-10 03:16:51 +090061 while (action_manager->HasMoreCommands()) {
62 action_manager->ExecuteOneCommand();
Tom Cherryad54d092017-04-19 16:18:50 -070063 }
64}
65
Tom Cherryd52a5b32019-07-22 16:05:36 -070066void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
Jooyung Hanbadb7de2022-05-10 03:16:51 +090067 const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager,
68 ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070069 TemporaryFile tf;
70 ASSERT_TRUE(tf.fd != -1);
71 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Jooyung Hanbadb7de2022-05-10 03:16:51 +090072 TestInit(tf.path, test_function_map, commands, action_manager, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070073}
74
75TEST(init, SimpleEventTrigger) {
76 bool expect_true = false;
77 std::string init_script =
78 R"init(
79on boot
80pass_test
81)init";
82
Tom Cherryd52a5b32019-07-22 16:05:36 -070083 auto do_pass_test = [&expect_true](const BuiltinArguments&) {
84 expect_true = true;
85 return Result<void>{};
86 };
87 BuiltinFunctionMap test_function_map = {
88 {"pass_test", {0, 0, {false, do_pass_test}}},
89 };
Tom Cherryad54d092017-04-19 16:18:50 -070090
91 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
92 std::vector<ActionManagerCommand> commands{trigger_boot};
93
Jooyung Hanbadb7de2022-05-10 03:16:51 +090094 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -080095 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +090096 TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070097
98 EXPECT_TRUE(expect_true);
99}
100
Nikita Ioffeaaab5962019-10-10 20:42:37 +0100101TEST(init, WrongEventTrigger) {
102 std::string init_script =
103 R"init(
104on boot:
105pass_test
106)init";
107
108 TemporaryFile tf;
109 ASSERT_TRUE(tf.fd != -1);
110 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
111
112 ActionManager am;
113
114 Parser parser;
115 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
116
117 ASSERT_TRUE(parser.ParseConfig(tf.path));
118 ASSERT_EQ(1u, parser.parse_error_count());
119}
120
Tom Cherryad54d092017-04-19 16:18:50 -0700121TEST(init, EventTriggerOrder) {
122 std::string init_script =
123 R"init(
124on boot
125execute_first
126
127on boot && property:ro.hardware=*
128execute_second
129
130on boot
131execute_third
132
133)init";
134
135 int num_executed = 0;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700136 auto do_execute_first = [&num_executed](const BuiltinArguments&) {
137 EXPECT_EQ(0, num_executed++);
138 return Result<void>{};
139 };
140 auto do_execute_second = [&num_executed](const BuiltinArguments&) {
141 EXPECT_EQ(1, num_executed++);
142 return Result<void>{};
143 };
144 auto do_execute_third = [&num_executed](const BuiltinArguments&) {
145 EXPECT_EQ(2, num_executed++);
146 return Result<void>{};
147 };
148
149 BuiltinFunctionMap test_function_map = {
150 {"execute_first", {0, 0, {false, do_execute_first}}},
151 {"execute_second", {0, 0, {false, do_execute_second}}},
152 {"execute_third", {0, 0, {false, do_execute_third}}},
153 };
Tom Cherryad54d092017-04-19 16:18:50 -0700154
155 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
156 std::vector<ActionManagerCommand> commands{trigger_boot};
157
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900158 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800159 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900160 TestInitText(init_script, test_function_map, commands, &action_manager, &service_list);
Florian Mayer6268f6a2022-05-11 01:02:01 +0000161 EXPECT_EQ(3, num_executed);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800162}
163
164TEST(init, OverrideService) {
165 std::string init_script = R"init(
166service A something
167 class first
168
169service A something
170 class second
171 override
172
173)init";
174
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900175 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800176 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900177 TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800178 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
179
180 auto service = service_list.begin()->get();
181 ASSERT_NE(nullptr, service);
182 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
183 EXPECT_EQ("A", service->name());
184 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700185}
186
187TEST(init, EventTriggerOrderMultipleFiles) {
188 // 6 total files, which should have their triggers executed in the following order:
189 // 1: start - original script parsed
190 // 2: first_import - immediately imported by first_script
191 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
192 // 4: a_import - file imported by dir_a
193 // 5: dir_b - file named 'b.rc' in dir
194 // 6: last_import - imported after dir is imported
195
196 TemporaryFile first_import;
197 ASSERT_TRUE(first_import.fd != -1);
198 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
199
200 TemporaryFile dir_a_import;
201 ASSERT_TRUE(dir_a_import.fd != -1);
202 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
203
204 TemporaryFile last_import;
205 ASSERT_TRUE(last_import.fd != -1);
206 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
207
208 TemporaryDir dir;
209 // clang-format off
210 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
211 "on boot\n"
212 "execute 3";
213 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700214 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900215 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700216
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900217 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700218
219 // clang-format off
220 std::string start_script = "import " + std::string(first_import.path) + "\n"
221 "import " + std::string(dir.path) + "\n"
222 "import " + std::string(last_import.path) + "\n"
223 "on boot\n"
224 "execute 1";
225 // clang-format on
226 TemporaryFile start;
227 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
228
229 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700230 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700231 EXPECT_EQ(2U, args.size());
232 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700233 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700234 };
235
Tom Cherryd52a5b32019-07-22 16:05:36 -0700236 BuiltinFunctionMap test_function_map = {
237 {"execute", {1, 1, {false, execute_command}}},
238 };
Tom Cherryad54d092017-04-19 16:18:50 -0700239
240 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
241 std::vector<ActionManagerCommand> commands{trigger_boot};
242
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900243 ActionManager action_manager;
Steven Moreland6f5333a2017-11-13 15:31:54 -0800244 ServiceList service_list;
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900245 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700246
247 EXPECT_EQ(6, num_executed);
248}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700249
Jooyung Hanbadb7de2022-05-10 03:16:51 +0900250BuiltinFunctionMap GetTestFunctionMapForLazyLoad(int& num_executed, ActionManager& action_manager) {
251 auto execute_command = [&num_executed](const BuiltinArguments& args) {
252 EXPECT_EQ(2U, args.size());
253 EXPECT_EQ(++num_executed, std::stoi(args[1]));
254 return Result<void>{};
255 };
256 auto load_command = [&action_manager](const BuiltinArguments& args) -> Result<void> {
257 EXPECT_EQ(2U, args.size());
258 Parser parser;
259 parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, nullptr));
260 if (!parser.ParseConfig(args[1])) {
261 return Error() << "Failed to load";
262 }
263 return Result<void>{};
264 };
265 auto trigger_command = [&action_manager](const BuiltinArguments& args) {
266 EXPECT_EQ(2U, args.size());
267 LOG(INFO) << "Queue event trigger: " << args[1];
268 action_manager.QueueEventTrigger(args[1]);
269 return Result<void>{};
270 };
271 BuiltinFunctionMap test_function_map = {
272 {"execute", {1, 1, {false, execute_command}}},
273 {"load", {1, 1, {false, load_command}}},
274 {"trigger", {1, 1, {false, trigger_command}}},
275 };
276 return test_function_map;
277}
278
279TEST(init, LazilyLoadedActionsCantBeTriggeredByTheSameTrigger) {
280 // "start" script loads "lazy" script. Even though "lazy" scripts
281 // defines "on boot" action, it's not executed by the current "boot"
282 // event because it's already processed.
283 TemporaryFile lazy;
284 ASSERT_TRUE(lazy.fd != -1);
285 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", lazy.fd));
286
287 TemporaryFile start;
288 // clang-format off
289 std::string start_script = "on boot\n"
290 "load " + std::string(lazy.path) + "\n"
291 "execute 1";
292 // clang-format on
293 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
294
295 int num_executed = 0;
296 ActionManager action_manager;
297 ServiceList service_list;
298 BuiltinFunctionMap test_function_map =
299 GetTestFunctionMapForLazyLoad(num_executed, action_manager);
300
301 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
302 std::vector<ActionManagerCommand> commands{trigger_boot};
303 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
304
305 EXPECT_EQ(1, num_executed);
306}
307
308TEST(init, LazilyLoadedActionsCanBeTriggeredByTheNextTrigger) {
309 // "start" script loads "lazy" script and then triggers "next" event
310 // which executes "on next" action loaded by the previous command.
311 TemporaryFile lazy;
312 ASSERT_TRUE(lazy.fd != -1);
313 ASSERT_TRUE(android::base::WriteStringToFd("on next\nexecute 2", lazy.fd));
314
315 TemporaryFile start;
316 // clang-format off
317 std::string start_script = "on boot\n"
318 "load " + std::string(lazy.path) + "\n"
319 "execute 1\n"
320 "trigger next";
321 // clang-format on
322 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
323
324 int num_executed = 0;
325 ActionManager action_manager;
326 ServiceList service_list;
327 BuiltinFunctionMap test_function_map =
328 GetTestFunctionMapForLazyLoad(num_executed, action_manager);
329
330 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
331 std::vector<ActionManagerCommand> commands{trigger_boot};
332 TestInit(start.path, test_function_map, commands, &action_manager, &service_list);
333
334 EXPECT_EQ(2, num_executed);
335}
336
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100337TEST(init, RejectsCriticalAndOneshotService) {
Tom Cherry0e40ba32020-07-09 08:47:24 -0700338 if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
339 GTEST_SKIP() << "Test only valid for devices launching with R or later";
340 }
341
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100342 std::string init_script =
343 R"init(
344service A something
345 class first
346 critical
347 oneshot
348)init";
349
350 TemporaryFile tf;
351 ASSERT_TRUE(tf.fd != -1);
352 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
353
354 ServiceList service_list;
355 Parser parser;
356 parser.AddSectionParser("service",
357 std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt));
358
359 ASSERT_TRUE(parser.ParseConfig(tf.path));
360 ASSERT_EQ(1u, parser.parse_error_count());
361}
362
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000363class TestCaseLogger : public ::testing::EmptyTestEventListener {
364 void OnTestStart(const ::testing::TestInfo& test_info) override {
365#ifdef __ANDROID__
366 LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " ("
367 << test_info.file() << ":" << test_info.line() << ")";
368#else
369 UNUSED(test_info);
370#endif
371 }
372};
373
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700374} // namespace init
375} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700376
377int SubcontextTestChildMain(int, char**);
378int FirmwareTestChildMain(int, char**);
379
380int main(int argc, char** argv) {
381 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
382 return SubcontextTestChildMain(argc, argv);
383 }
384
385 if (argc > 1 && !strcmp(argv[1], "firmware")) {
386 return FirmwareTestChildMain(argc, argv);
387 }
388
389 testing::InitGoogleTest(&argc, argv);
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000390 testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
Tom Cherrydcb3d152019-08-07 16:02:28 -0700391 return RUN_ALL_TESTS();
392}