blob: 315d584beaae925391a06e0b290122dd5bbc7735 [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
96TEST(init, EventTriggerOrder) {
97 std::string init_script =
98 R"init(
99on boot
100execute_first
101
102on boot && property:ro.hardware=*
103execute_second
104
105on boot
106execute_third
107
108)init";
109
110 int num_executed = 0;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700111 auto do_execute_first = [&num_executed](const BuiltinArguments&) {
112 EXPECT_EQ(0, num_executed++);
113 return Result<void>{};
114 };
115 auto do_execute_second = [&num_executed](const BuiltinArguments&) {
116 EXPECT_EQ(1, num_executed++);
117 return Result<void>{};
118 };
119 auto do_execute_third = [&num_executed](const BuiltinArguments&) {
120 EXPECT_EQ(2, num_executed++);
121 return Result<void>{};
122 };
123
124 BuiltinFunctionMap test_function_map = {
125 {"execute_first", {0, 0, {false, do_execute_first}}},
126 {"execute_second", {0, 0, {false, do_execute_second}}},
127 {"execute_third", {0, 0, {false, do_execute_third}}},
128 };
Tom Cherryad54d092017-04-19 16:18:50 -0700129
130 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
131 std::vector<ActionManagerCommand> commands{trigger_boot};
132
Steven Moreland6f5333a2017-11-13 15:31:54 -0800133 ServiceList service_list;
134 TestInitText(init_script, test_function_map, commands, &service_list);
135}
136
137TEST(init, OverrideService) {
138 std::string init_script = R"init(
139service A something
140 class first
141
142service A something
143 class second
144 override
145
146)init";
147
148 ServiceList service_list;
Tom Cherryd52a5b32019-07-22 16:05:36 -0700149 TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list);
Steven Moreland6f5333a2017-11-13 15:31:54 -0800150 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
151
152 auto service = service_list.begin()->get();
153 ASSERT_NE(nullptr, service);
154 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
155 EXPECT_EQ("A", service->name());
156 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700157}
158
159TEST(init, EventTriggerOrderMultipleFiles) {
160 // 6 total files, which should have their triggers executed in the following order:
161 // 1: start - original script parsed
162 // 2: first_import - immediately imported by first_script
163 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
164 // 4: a_import - file imported by dir_a
165 // 5: dir_b - file named 'b.rc' in dir
166 // 6: last_import - imported after dir is imported
167
168 TemporaryFile first_import;
169 ASSERT_TRUE(first_import.fd != -1);
170 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
171
172 TemporaryFile dir_a_import;
173 ASSERT_TRUE(dir_a_import.fd != -1);
174 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
175
176 TemporaryFile last_import;
177 ASSERT_TRUE(last_import.fd != -1);
178 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
179
180 TemporaryDir dir;
181 // clang-format off
182 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
183 "on boot\n"
184 "execute 3";
185 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700186 // WriteFile() ensures the right mode is set
Tom Cherry11a3aee2017-08-03 12:54:07 -0700187 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700188
Tom Cherry11a3aee2017-08-03 12:54:07 -0700189 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700190
191 // clang-format off
192 std::string start_script = "import " + std::string(first_import.path) + "\n"
193 "import " + std::string(dir.path) + "\n"
194 "import " + std::string(last_import.path) + "\n"
195 "on boot\n"
196 "execute 1";
197 // clang-format on
198 TemporaryFile start;
199 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
200
201 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700202 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700203 EXPECT_EQ(2U, args.size());
204 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700205 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700206 };
207
Tom Cherryd52a5b32019-07-22 16:05:36 -0700208 BuiltinFunctionMap test_function_map = {
209 {"execute", {1, 1, {false, execute_command}}},
210 };
Tom Cherryad54d092017-04-19 16:18:50 -0700211
212 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
213 std::vector<ActionManagerCommand> commands{trigger_boot};
214
Steven Moreland6f5333a2017-11-13 15:31:54 -0800215 ServiceList service_list;
216
217 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700218
219 EXPECT_EQ(6, num_executed);
220}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700221
222} // namespace init
223} // namespace android
Tom Cherrydcb3d152019-08-07 16:02:28 -0700224
225int SubcontextTestChildMain(int, char**);
226int FirmwareTestChildMain(int, char**);
227
228int main(int argc, char** argv) {
229 if (argc > 1 && !strcmp(argv[1], "subcontext")) {
230 return SubcontextTestChildMain(argc, argv);
231 }
232
233 if (argc > 1 && !strcmp(argv[1], "firmware")) {
234 return FirmwareTestChildMain(argc, argv);
235 }
236
237 testing::InitGoogleTest(&argc, argv);
238 return RUN_ALL_TESTS();
239}