blob: 3053bd848ce1d68823f1ad973444fa0794977d39 [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 Cherryad54d092017-04-19 16:18:50 -070020#include <gtest/gtest.h>
21
22#include "action.h"
Tom Cherry7fd3bc22018-02-13 15:36:14 -080023#include "action_manager.h"
Tom Cherry0f6417f2018-02-13 15:25:29 -080024#include "action_parser.h"
Tom Cherryd52a5b32019-07-22 16:05:36 -070025#include "builtin_arguments.h"
Tom Cherryad54d092017-04-19 16:18:50 -070026#include "builtins.h"
27#include "import_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070028#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070029#include "parser.h"
Steven Moreland6f5333a2017-11-13 15:31:54 -080030#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070031#include "service_list.h"
32#include "service_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070033#include "util.h"
34
Tom Cherry81f5d3e2017-06-22 12:53:17 -070035namespace android {
36namespace init {
37
Tom Cherryad54d092017-04-19 16:18:50 -070038using ActionManagerCommand = std::function<void(ActionManager&)>;
39
Tom Cherryd52a5b32019-07-22 16:05:36 -070040void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080041 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070042 ActionManager am;
43
44 Action::set_function_map(&test_function_map);
45
46 Parser parser;
Daniel Norman3df8dc52019-06-27 12:18:08 -070047 parser.AddSectionParser("service",
48 std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070049 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070050 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
51
52 ASSERT_TRUE(parser.ParseConfig(init_script_file));
53
54 for (const auto& command : commands) {
55 command(am);
56 }
57
58 while (am.HasMoreCommands()) {
59 am.ExecuteOneCommand();
60 }
61}
62
Tom Cherryd52a5b32019-07-22 16:05:36 -070063void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080064 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070065 TemporaryFile tf;
66 ASSERT_TRUE(tf.fd != -1);
67 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Steven Moreland6f5333a2017-11-13 15:31:54 -080068 TestInit(tf.path, test_function_map, commands, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070069}
70
71TEST(init, SimpleEventTrigger) {
72 bool expect_true = false;
73 std::string init_script =
74 R"init(
75on boot
76pass_test
77)init";
78
Tom Cherryd52a5b32019-07-22 16:05:36 -070079 auto do_pass_test = [&expect_true](const BuiltinArguments&) {
80 expect_true = true;
81 return Result<void>{};
82 };
83 BuiltinFunctionMap test_function_map = {
84 {"pass_test", {0, 0, {false, do_pass_test}}},
85 };
Tom Cherryad54d092017-04-19 16:18:50 -070086
87 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
88 std::vector<ActionManagerCommand> commands{trigger_boot};
89
Steven Moreland6f5333a2017-11-13 15:31:54 -080090 ServiceList service_list;
91 TestInitText(init_script, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070092
93 EXPECT_TRUE(expect_true);
94}
95
Nikita Ioffeaaab5962019-10-10 20:42:37 +010096TEST(init, WrongEventTrigger) {
97 std::string init_script =
98 R"init(
99on boot:
100pass_test
101)init";
102
103 TemporaryFile tf;
104 ASSERT_TRUE(tf.fd != -1);
105 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
106
107 ActionManager am;
108
109 Parser parser;
110 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
111
112 ASSERT_TRUE(parser.ParseConfig(tf.path));
113 ASSERT_EQ(1u, parser.parse_error_count());
114}
115
Tom Cherryad54d092017-04-19 16:18:50 -0700116TEST(init, EventTriggerOrder) {
117 std::string init_script =
118 R"init(
119on boot
120execute_first
121
122on boot && property:ro.hardware=*
123execute_second
124
125on boot
126execute_third
127
128)init";
129
130 int num_executed = 0;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700131 auto do_execute_first = [&num_executed](const BuiltinArguments&) {
132 EXPECT_EQ(0, num_executed++);
133 return Result<void>{};
134 };
135 auto do_execute_second = [&num_executed](const BuiltinArguments&) {
136 EXPECT_EQ(1, num_executed++);
137 return Result<void>{};
138 };
139 auto do_execute_third = [&num_executed](const BuiltinArguments&) {
140 EXPECT_EQ(2, num_executed++);
141 return Result<void>{};
142 };
143
144 BuiltinFunctionMap test_function_map = {
145 {"execute_first", {0, 0, {false, do_execute_first}}},
146 {"execute_second", {0, 0, {false, do_execute_second}}},
147 {"execute_third", {0, 0, {false, do_execute_third}}},
148 };
Tom Cherryad54d092017-04-19 16:18:50 -0700149
150 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
151 std::vector<ActionManagerCommand> commands{trigger_boot};
152
Steven Moreland6f5333a2017-11-13 15:31:54 -0800153 ServiceList service_list;
154 TestInitText(init_script, test_function_map, commands, &service_list);
155}
156
157TEST(init, OverrideService) {
158 std::string init_script = R"init(
159service A something
160 class first
161
162service A something
163 class second
164 override
165
166)init";
167
168 ServiceList service_list;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700169 TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list);
Tom Cherryd2dab832020-01-29 14:09:24 -0800170 auto lock = std::lock_guard{service_lock};
Steven Moreland6f5333a2017-11-13 15:31:54 -0800171 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
172
173 auto service = service_list.begin()->get();
174 ASSERT_NE(nullptr, service);
175 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
176 EXPECT_EQ("A", service->name());
177 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700178}
179
180TEST(init, EventTriggerOrderMultipleFiles) {
181 // 6 total files, which should have their triggers executed in the following order:
182 // 1: start - original script parsed
183 // 2: first_import - immediately imported by first_script
184 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
185 // 4: a_import - file imported by dir_a
186 // 5: dir_b - file named 'b.rc' in dir
187 // 6: last_import - imported after dir is imported
188
189 TemporaryFile first_import;
190 ASSERT_TRUE(first_import.fd != -1);
191 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
192
193 TemporaryFile dir_a_import;
194 ASSERT_TRUE(dir_a_import.fd != -1);
195 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
196
197 TemporaryFile last_import;
198 ASSERT_TRUE(last_import.fd != -1);
199 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
200
201 TemporaryDir dir;
202 // clang-format off
203 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
204 "on boot\n"
205 "execute 3";
206 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700207 // WriteFile() ensures the right mode is set
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900208 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700209
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900210 ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700211
212 // clang-format off
213 std::string start_script = "import " + std::string(first_import.path) + "\n"
214 "import " + std::string(dir.path) + "\n"
215 "import " + std::string(last_import.path) + "\n"
216 "on boot\n"
217 "execute 1";
218 // clang-format on
219 TemporaryFile start;
220 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
221
222 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700223 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700224 EXPECT_EQ(2U, args.size());
225 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700226 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700227 };
228
Tom Cherryd52a5b32019-07-22 16:05:36 -0700229 BuiltinFunctionMap test_function_map = {
230 {"execute", {1, 1, {false, execute_command}}},
231 };
Tom Cherryad54d092017-04-19 16:18:50 -0700232
233 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
234 std::vector<ActionManagerCommand> commands{trigger_boot};
235
Steven Moreland6f5333a2017-11-13 15:31:54 -0800236 ServiceList service_list;
237
238 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700239
240 EXPECT_EQ(6, num_executed);
241}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700242
243} // namespace init
244} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700245
246int SubcontextTestChildMain(int, char**);
247int FirmwareTestChildMain(int, char**);
248
249int main(int argc, char** argv) {
250 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
251 return SubcontextTestChildMain(argc, argv);
252 }
253
254 if (argc > 1 && !strcmp(argv[1], "firmware")) {
255 return FirmwareTestChildMain(argc, argv);
256 }
257
258 testing::InitGoogleTest(&argc, argv);
259 return RUN_ALL_TESTS();
260}