blob: 8550ec8f33665817efba1d2e4b06bd40b3c36e07 [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);
159}
160
161TEST(init, OverrideService) {
162 std::string init_script = R"init(
163service A something
164 class first
165
166service A something
167 class second
168 override
169
170)init";
171
172 ServiceList service_list;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700173 TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800174 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());
Tom Cherryad54d092017-04-19 16:18:50 -0700181}
182
183TEST(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
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700210 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900211 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700212
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900213 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700214
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;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700226 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700227 EXPECT_EQ(2U, args.size());
228 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700229 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700230 };
231
Tom Cherryd52a5b32019-07-22 16:05:36 -0700232 BuiltinFunctionMap test_function_map = {
233 {"execute", {1, 1, {false, execute_command}}},
234 };
Tom Cherryad54d092017-04-19 16:18:50 -0700235
236 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
237 std::vector<ActionManagerCommand> commands{trigger_boot};
238
Steven Moreland6f5333a2017-11-13 15:31:54 -0800239 ServiceList service_list;
240
241 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700242
243 EXPECT_EQ(6, num_executed);
244}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700245
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100246TEST(init, RejectsCriticalAndOneshotService) {
Tom Cherry0e40ba32020-07-09 08:47:24 -0700247 if (GetIntProperty("ro.product.first_api_level", 10000) < 30) {
248 GTEST_SKIP() << "Test only valid for devices launching with R or later";
249 }
250
Nikita Ioffe51c251c2020-04-30 19:40:39 +0100251 std::string init_script =
252 R"init(
253service 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
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000272class TestCaseLogger : public ::testing::EmptyTestEventListener {
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
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700283} // namespace init
284} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700285
286int SubcontextTestChildMain(int, char**);
287int FirmwareTestChildMain(int, char**);
288
289int 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);
Nikita Ioffe9e4b1112020-12-11 17:59:38 +0000299 testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger());
Tom Cherrydcb3d152019-08-07 16:02:28 -0700300 return RUN_ALL_TESTS();
301}