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