blob: fa6574026ec35e368afde0708a02c46445f84a90 [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>
Tom Cherry0e40ba32020-07-09 08:47:24 -070020#include <android-base/properties.h>
Tom Cherryad54d092017-04-19 16:18:50 -070021#include <gtest/gtest.h>
22
23#include "action.h"
Tom Cherry7fd3bc22018-02-13 15:36:14 -080024#include "action_manager.h"
Tom Cherry0f6417f2018-02-13 15:25:29 -080025#include "action_parser.h"
Tom Cherryd52a5b32019-07-22 16:05:36 -070026#include "builtin_arguments.h"
Tom Cherryad54d092017-04-19 16:18:50 -070027#include "builtins.h"
28#include "import_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070029#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070030#include "parser.h"
Steven Moreland6f5333a2017-11-13 15:31:54 -080031#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070032#include "service_list.h"
33#include "service_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070034#include "util.h"
35
Tom Cherry0e40ba32020-07-09 08:47:24 -070036using android::base::GetIntProperty;
37
Tom Cherry81f5d3e2017-06-22 12:53:17 -070038namespace android {
39namespace init {
40
Tom Cherryad54d092017-04-19 16:18:50 -070041using ActionManagerCommand = std::function<void(ActionManager&)>;
42
Tom Cherryd52a5b32019-07-22 16:05:36 -070043void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080044 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070045 ActionManager am;
46
47 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));
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070052 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, 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) {
58 command(am);
59 }
60
61 while (am.HasMoreCommands()) {
62 am.ExecuteOneCommand();
63 }
64}
65
Tom Cherryd52a5b32019-07-22 16:05:36 -070066void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080067 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070068 TemporaryFile tf;
69 ASSERT_TRUE(tf.fd != -1);
70 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Steven Moreland6f5333a2017-11-13 15:31:54 -080071 TestInit(tf.path, test_function_map, commands, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070072}
73
74TEST(init, SimpleEventTrigger) {
75 bool expect_true = false;
76 std::string init_script =
77 R"init(
78on boot
79pass_test
80)init";
81
Tom Cherryd52a5b32019-07-22 16:05:36 -070082 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 };
Tom Cherryad54d092017-04-19 16:18:50 -070089
90 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
91 std::vector<ActionManagerCommand> commands{trigger_boot};
92
Steven Moreland6f5333a2017-11-13 15:31:54 -080093 ServiceList service_list;
94 TestInitText(init_script, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070095
96 EXPECT_TRUE(expect_true);
97}
98
Nikita Ioffeaaab5962019-10-10 20:42:37 +010099TEST(init, WrongEventTrigger) {
100 std::string init_script =
101 R"init(
102on boot:
103pass_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
Tom Cherryad54d092017-04-19 16:18:50 -0700119TEST(init, EventTriggerOrder) {
120 std::string init_script =
121 R"init(
122on boot
123execute_first
124
125on boot && property:ro.hardware=*
126execute_second
127
128on boot
129execute_third
130
131)init";
132
133 int num_executed = 0;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700134 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 };
Tom Cherryad54d092017-04-19 16:18:50 -0700152
153 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
154 std::vector<ActionManagerCommand> commands{trigger_boot};
155
Steven Moreland6f5333a2017-11-13 15:31:54 -0800156 ServiceList service_list;
157 TestInitText(init_script, test_function_map, commands, &service_list);
158}
159
160TEST(init, OverrideService) {
161 std::string init_script = R"init(
162service A something
163 class first
164
165service A something
166 class second
167 override
168
169)init";
170
171 ServiceList service_list;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700172 TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800173 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());
Tom Cherryad54d092017-04-19 16:18:50 -0700180}
181
182TEST(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
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700209 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900210 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700211
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900212 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700213
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;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700225 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700226 EXPECT_EQ(2U, args.size());
227 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700228 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700229 };
230
Tom Cherryd52a5b32019-07-22 16:05:36 -0700231 BuiltinFunctionMap test_function_map = {
232 {"execute", {1, 1, {false, execute_command}}},
233 };
Tom Cherryad54d092017-04-19 16:18:50 -0700234
235 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
236 std::vector<ActionManagerCommand> commands{trigger_boot};
237
Steven Moreland6f5333a2017-11-13 15:31:54 -0800238 ServiceList service_list;
239
240 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700241
242 EXPECT_EQ(6, num_executed);
243}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700244
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100245TEST(init, RejectsCriticalAndOneshotService) {
Tom Cherry0e40ba32020-07-09 08:47:24 -0700246 if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
247 GTEST_SKIP() << "Test only valid for devices launching with R or later";
248 }
249
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100250 std::string init_script =
251 R"init(
252service 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
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700271} // namespace init
272} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700273
274int SubcontextTestChildMain(int, char**);
275int FirmwareTestChildMain(int, char**);
276
277int 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}