blob: 8c19d5f5a830edbbfbdcac5dd9b679f9d3126040 [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,
Steven Moreland6f5333a2017-11-13 15:31:54 -080045 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070046 ActionManager am;
47
48 Action::set_function_map(&test_function_map);
49
50 Parser parser;
Daniel Norman3df8dc52019-06-27 12:18:08 -070051 parser.AddSectionParser("service",
52 std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070053 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070054 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
55
56 ASSERT_TRUE(parser.ParseConfig(init_script_file));
57
58 for (const auto& command : commands) {
59 command(am);
60 }
61
62 while (am.HasMoreCommands()) {
63 am.ExecuteOneCommand();
64 }
65}
66
Tom Cherryd52a5b32019-07-22 16:05:36 -070067void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080068 const std::vector<ActionManagerCommand>& commands, 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));
Steven Moreland6f5333a2017-11-13 15:31:54 -080072 TestInit(tf.path, test_function_map, commands, 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
Steven Moreland6f5333a2017-11-13 15:31:54 -080094 ServiceList service_list;
95 TestInitText(init_script, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070096
97 EXPECT_TRUE(expect_true);
98}
99
Nikita Ioffeaaab5962019-10-10 20:42:37 +0100100TEST(init, WrongEventTrigger) {
101 std::string init_script =
102 R"init(
103on boot:
104pass_test
105)init";
106
107 TemporaryFile tf;
108 ASSERT_TRUE(tf.fd != -1);
109 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
110
111 ActionManager am;
112
113 Parser parser;
114 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
115
116 ASSERT_TRUE(parser.ParseConfig(tf.path));
117 ASSERT_EQ(1u, parser.parse_error_count());
118}
119
Tom Cherryad54d092017-04-19 16:18:50 -0700120TEST(init, EventTriggerOrder) {
121 std::string init_script =
122 R"init(
123on boot
124execute_first
125
126on boot && property:ro.hardware=*
127execute_second
128
129on boot
130execute_third
131
132)init";
133
134 int num_executed = 0;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700135 auto do_execute_first = [&num_executed](const BuiltinArguments&) {
136 EXPECT_EQ(0, num_executed++);
137 return Result<void>{};
138 };
139 auto do_execute_second = [&num_executed](const BuiltinArguments&) {
140 EXPECT_EQ(1, num_executed++);
141 return Result<void>{};
142 };
143 auto do_execute_third = [&num_executed](const BuiltinArguments&) {
144 EXPECT_EQ(2, num_executed++);
145 return Result<void>{};
146 };
147
148 BuiltinFunctionMap test_function_map = {
149 {"execute_first", {0, 0, {false, do_execute_first}}},
150 {"execute_second", {0, 0, {false, do_execute_second}}},
151 {"execute_third", {0, 0, {false, do_execute_third}}},
152 };
Tom Cherryad54d092017-04-19 16:18:50 -0700153
154 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
155 std::vector<ActionManagerCommand> commands{trigger_boot};
156
Steven Moreland6f5333a2017-11-13 15:31:54 -0800157 ServiceList service_list;
158 TestInitText(init_script, test_function_map, commands, &service_list);
Florian Mayer6268f6a2022-05-11 01:02:01 +0000159 EXPECT_EQ(3, num_executed);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800160}
161
162TEST(init, OverrideService) {
163 std::string init_script = R"init(
164service A something
165 class first
166
167service A something
168 class second
169 override
170
171)init";
172
173 ServiceList service_list;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700174 TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800175 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
176
177 auto service = service_list.begin()->get();
178 ASSERT_NE(nullptr, service);
179 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
180 EXPECT_EQ("A", service->name());
181 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700182}
183
184TEST(init, EventTriggerOrderMultipleFiles) {
185 // 6 total files, which should have their triggers executed in the following order:
186 // 1: start - original script parsed
187 // 2: first_import - immediately imported by first_script
188 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
189 // 4: a_import - file imported by dir_a
190 // 5: dir_b - file named 'b.rc' in dir
191 // 6: last_import - imported after dir is imported
192
193 TemporaryFile first_import;
194 ASSERT_TRUE(first_import.fd != -1);
195 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
196
197 TemporaryFile dir_a_import;
198 ASSERT_TRUE(dir_a_import.fd != -1);
199 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
200
201 TemporaryFile last_import;
202 ASSERT_TRUE(last_import.fd != -1);
203 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
204
205 TemporaryDir dir;
206 // clang-format off
207 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
208 "on boot\n"
209 "execute 3";
210 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700211 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900212 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700213
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900214 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700215
216 // clang-format off
217 std::string start_script = "import " + std::string(first_import.path) + "\n"
218 "import " + std::string(dir.path) + "\n"
219 "import " + std::string(last_import.path) + "\n"
220 "on boot\n"
221 "execute 1";
222 // clang-format on
223 TemporaryFile start;
224 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
225
226 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700227 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700228 EXPECT_EQ(2U, args.size());
229 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700230 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700231 };
232
Tom Cherryd52a5b32019-07-22 16:05:36 -0700233 BuiltinFunctionMap test_function_map = {
234 {"execute", {1, 1, {false, execute_command}}},
235 };
Tom Cherryad54d092017-04-19 16:18:50 -0700236
237 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
238 std::vector<ActionManagerCommand> commands{trigger_boot};
239
Steven Moreland6f5333a2017-11-13 15:31:54 -0800240 ServiceList service_list;
241
242 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700243
244 EXPECT_EQ(6, num_executed);
245}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700246
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100247TEST(init, RejectsCriticalAndOneshotService) {
Tom Cherry0e40ba32020-07-09 08:47:24 -0700248 if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
249 GTEST_SKIP() << "Test only valid for devices launching with R or later";
250 }
251
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100252 std::string init_script =
253 R"init(
254service A something
255 class first
256 critical
257 oneshot
258)init";
259
260 TemporaryFile tf;
261 ASSERT_TRUE(tf.fd != -1);
262 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
263
264 ServiceList service_list;
265 Parser parser;
266 parser.AddSectionParser("service",
267 std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt));
268
269 ASSERT_TRUE(parser.ParseConfig(tf.path));
270 ASSERT_EQ(1u, parser.parse_error_count());
271}
272
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000273class TestCaseLogger : public ::testing::EmptyTestEventListener {
274 void OnTestStart(const ::testing::TestInfo& test_info) override {
275#ifdef __ANDROID__
276 LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " ("
277 << test_info.file() << ":" << test_info.line() << ")";
278#else
279 UNUSED(test_info);
280#endif
281 }
282};
283
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700284} // namespace init
285} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700286
287int SubcontextTestChildMain(int, char**);
288int FirmwareTestChildMain(int, char**);
289
290int main(int argc, char** argv) {
291 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
292 return SubcontextTestChildMain(argc, argv);
293 }
294
295 if (argc > 1 && !strcmp(argv[1], "firmware")) {
296 return FirmwareTestChildMain(argc, argv);
297 }
298
299 testing::InitGoogleTest(&argc, argv);
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000300 testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
Tom Cherrydcb3d152019-08-07 16:02:28 -0700301 return RUN_ALL_TESTS();
302}