1 /*
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>
20 #include <android-base/logging.h>
21 #include <android-base/properties.h>
22 #include <gtest/gtest.h>
23
24 #include "action.h"
25 #include "action_manager.h"
26 #include "action_parser.h"
27 #include "builtin_arguments.h"
28 #include "builtins.h"
29 #include "import_parser.h"
30 #include "keyword_map.h"
31 #include "parser.h"
32 #include "service.h"
33 #include "service_list.h"
34 #include "service_parser.h"
35 #include "util.h"
36
37 using android::base::GetIntProperty;
38
39 namespace android {
40 namespace init {
41
42 using ActionManagerCommand = std::function<void(ActionManager&)>;
43
TestInit(const std::string & init_script_file,const BuiltinFunctionMap & test_function_map,const std::vector<ActionManagerCommand> & commands,ServiceList * service_list)44 void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
45 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
46 ActionManager am;
47
48 Action::set_function_map(&test_function_map);
49
50 Parser parser;
51 parser.AddSectionParser("service",
52 std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
53 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
54 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
TestInitText(const std::string & init_script,const BuiltinFunctionMap & test_function_map,const std::vector<ActionManagerCommand> & commands,ServiceList * service_list)67 void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
68 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
69 TemporaryFile tf;
70 ASSERT_TRUE(tf.fd != -1);
71 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
72 TestInit(tf.path, test_function_map, commands, service_list);
73 }
74
TEST(init,SimpleEventTrigger)75 TEST(init, SimpleEventTrigger) {
76 bool expect_true = false;
77 std::string init_script =
78 R"init(
79 on boot
80 pass_test
81 )init";
82
83 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 };
90
91 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
92 std::vector<ActionManagerCommand> commands{trigger_boot};
93
94 ServiceList service_list;
95 TestInitText(init_script, test_function_map, commands, &service_list);
96
97 EXPECT_TRUE(expect_true);
98 }
99
TEST(init,WrongEventTrigger)100 TEST(init, WrongEventTrigger) {
101 std::string init_script =
102 R"init(
103 on boot:
104 pass_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
TEST(init,EventTriggerOrder)120 TEST(init, EventTriggerOrder) {
121 std::string init_script =
122 R"init(
123 on boot
124 execute_first
125
126 on boot && property:ro.hardware=*
127 execute_second
128
129 on boot
130 execute_third
131
132 )init";
133
134 int num_executed = 0;
135 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 };
153
154 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
155 std::vector<ActionManagerCommand> commands{trigger_boot};
156
157 ServiceList service_list;
158 TestInitText(init_script, test_function_map, commands, &service_list);
159 }
160
TEST(init,OverrideService)161 TEST(init, OverrideService) {
162 std::string init_script = R"init(
163 service A something
164 class first
165
166 service A something
167 class second
168 override
169
170 )init";
171
172 ServiceList service_list;
173 TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list);
174 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
175
176 auto service = service_list.begin()->get();
177 ASSERT_NE(nullptr, service);
178 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
179 EXPECT_EQ("A", service->name());
180 EXPECT_TRUE(service->is_override());
181 }
182
TEST(init,EventTriggerOrderMultipleFiles)183 TEST(init, EventTriggerOrderMultipleFiles) {
184 // 6 total files, which should have their triggers executed in the following order:
185 // 1: start - original script parsed
186 // 2: first_import - immediately imported by first_script
187 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
188 // 4: a_import - file imported by dir_a
189 // 5: dir_b - file named 'b.rc' in dir
190 // 6: last_import - imported after dir is imported
191
192 TemporaryFile first_import;
193 ASSERT_TRUE(first_import.fd != -1);
194 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
195
196 TemporaryFile dir_a_import;
197 ASSERT_TRUE(dir_a_import.fd != -1);
198 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
199
200 TemporaryFile last_import;
201 ASSERT_TRUE(last_import.fd != -1);
202 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
203
204 TemporaryDir dir;
205 // clang-format off
206 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
207 "on boot\n"
208 "execute 3";
209 // clang-format on
210 // WriteFile() ensures the right mode is set
211 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
212
213 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
214
215 // clang-format off
216 std::string start_script = "import " + std::string(first_import.path) + "\n"
217 "import " + std::string(dir.path) + "\n"
218 "import " + std::string(last_import.path) + "\n"
219 "on boot\n"
220 "execute 1";
221 // clang-format on
222 TemporaryFile start;
223 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
224
225 int num_executed = 0;
226 auto execute_command = [&num_executed](const BuiltinArguments& args) {
227 EXPECT_EQ(2U, args.size());
228 EXPECT_EQ(++num_executed, std::stoi(args[1]));
229 return Result<void>{};
230 };
231
232 BuiltinFunctionMap test_function_map = {
233 {"execute", {1, 1, {false, execute_command}}},
234 };
235
236 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
237 std::vector<ActionManagerCommand> commands{trigger_boot};
238
239 ServiceList service_list;
240
241 TestInit(start.path, test_function_map, commands, &service_list);
242
243 EXPECT_EQ(6, num_executed);
244 }
245
TEST(init,RejectsCriticalAndOneshotService)246 TEST(init, RejectsCriticalAndOneshotService) {
247 if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
248 GTEST_SKIP() << "Test only valid for devices launching with R or later";
249 }
250
251 std::string init_script =
252 R"init(
253 service A something
254 class first
255 critical
256 oneshot
257 )init";
258
259 TemporaryFile tf;
260 ASSERT_TRUE(tf.fd != -1);
261 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
262
263 ServiceList service_list;
264 Parser parser;
265 parser.AddSectionParser("service",
266 std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt));
267
268 ASSERT_TRUE(parser.ParseConfig(tf.path));
269 ASSERT_EQ(1u, parser.parse_error_count());
270 }
271
272 class TestCaseLogger : public ::testing::EmptyTestEventListener {
OnTestStart(const::testing::TestInfo & test_info)273 void OnTestStart(const ::testing::TestInfo& test_info) override {
274 #ifdef __ANDROID__
275 LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " ("
276 << test_info.file() << ":" << test_info.line() << ")";
277 #else
278 UNUSED(test_info);
279 #endif
280 }
281 };
282
283 } // namespace init
284 } // namespace android
285
286 int SubcontextTestChildMain(int, char**);
287 int FirmwareTestChildMain(int, char**);
288
main(int argc,char ** argv)289 int main(int argc, char** argv) {
290 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
291 return SubcontextTestChildMain(argc, argv);
292 }
293
294 if (argc > 1 && !strcmp(argv[1], "firmware")) {
295 return FirmwareTestChildMain(argc, argv);
296 }
297
298 testing::InitGoogleTest(&argc, argv);
299 testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
300 return RUN_ALL_TESTS();
301 }
302