blob: d78aabd16c2ac8fdcdf0e988a819ea8818d8849b [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>
20#include <android-base/test_utils.h>
21#include <gtest/gtest.h>
22
23#include "action.h"
Tom Cherry0f6417f2018-02-13 15:25:29 -080024#include "action_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070025#include "builtins.h"
26#include "import_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070027#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070028#include "parser.h"
Steven Moreland6f5333a2017-11-13 15:31:54 -080029#include "service.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070030#include "test_function_map.h"
Tom Cherryad54d092017-04-19 16:18:50 -070031#include "util.h"
32
Tom Cherry81f5d3e2017-06-22 12:53:17 -070033namespace android {
34namespace init {
35
Tom Cherryad54d092017-04-19 16:18:50 -070036using ActionManagerCommand = std::function<void(ActionManager&)>;
37
38void TestInit(const std::string& init_script_file, const TestFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080039 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070040 ActionManager am;
41
42 Action::set_function_map(&test_function_map);
43
44 Parser parser;
Steven Moreland6f5333a2017-11-13 15:31:54 -080045 parser.AddSectionParser("service", std::make_unique<ServiceParser>(service_list, nullptr));
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070046 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070047 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
48
49 ASSERT_TRUE(parser.ParseConfig(init_script_file));
50
51 for (const auto& command : commands) {
52 command(am);
53 }
54
55 while (am.HasMoreCommands()) {
56 am.ExecuteOneCommand();
57 }
58}
59
60void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080061 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070062 TemporaryFile tf;
63 ASSERT_TRUE(tf.fd != -1);
64 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Steven Moreland6f5333a2017-11-13 15:31:54 -080065 TestInit(tf.path, test_function_map, commands, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070066}
67
68TEST(init, SimpleEventTrigger) {
69 bool expect_true = false;
70 std::string init_script =
71 R"init(
72on boot
73pass_test
74)init";
75
76 TestFunctionMap test_function_map;
77 test_function_map.Add("pass_test", [&expect_true]() { expect_true = true; });
78
79 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
80 std::vector<ActionManagerCommand> commands{trigger_boot};
81
Steven Moreland6f5333a2017-11-13 15:31:54 -080082 ServiceList service_list;
83 TestInitText(init_script, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070084
85 EXPECT_TRUE(expect_true);
86}
87
88TEST(init, EventTriggerOrder) {
89 std::string init_script =
90 R"init(
91on boot
92execute_first
93
94on boot && property:ro.hardware=*
95execute_second
96
97on boot
98execute_third
99
100)init";
101
102 int num_executed = 0;
103 TestFunctionMap test_function_map;
104 test_function_map.Add("execute_first", [&num_executed]() { EXPECT_EQ(0, num_executed++); });
105 test_function_map.Add("execute_second", [&num_executed]() { EXPECT_EQ(1, num_executed++); });
106 test_function_map.Add("execute_third", [&num_executed]() { EXPECT_EQ(2, num_executed++); });
107
108 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
109 std::vector<ActionManagerCommand> commands{trigger_boot};
110
Steven Moreland6f5333a2017-11-13 15:31:54 -0800111 ServiceList service_list;
112 TestInitText(init_script, test_function_map, commands, &service_list);
113}
114
115TEST(init, OverrideService) {
116 std::string init_script = R"init(
117service A something
118 class first
119
120service A something
121 class second
122 override
123
124)init";
125
126 ServiceList service_list;
127 TestInitText(init_script, TestFunctionMap(), {}, &service_list);
128 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
129
130 auto service = service_list.begin()->get();
131 ASSERT_NE(nullptr, service);
132 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
133 EXPECT_EQ("A", service->name());
134 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700135}
136
137TEST(init, EventTriggerOrderMultipleFiles) {
138 // 6 total files, which should have their triggers executed in the following order:
139 // 1: start - original script parsed
140 // 2: first_import - immediately imported by first_script
141 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
142 // 4: a_import - file imported by dir_a
143 // 5: dir_b - file named 'b.rc' in dir
144 // 6: last_import - imported after dir is imported
145
146 TemporaryFile first_import;
147 ASSERT_TRUE(first_import.fd != -1);
148 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
149
150 TemporaryFile dir_a_import;
151 ASSERT_TRUE(dir_a_import.fd != -1);
152 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
153
154 TemporaryFile last_import;
155 ASSERT_TRUE(last_import.fd != -1);
156 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
157
158 TemporaryDir dir;
159 // clang-format off
160 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
161 "on boot\n"
162 "execute 3";
163 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700164 // WriteFile() ensures the right mode is set
Tom Cherry11a3aee2017-08-03 12:54:07 -0700165 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700166
Tom Cherry11a3aee2017-08-03 12:54:07 -0700167 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700168
169 // clang-format off
170 std::string start_script = "import " + std::string(first_import.path) + "\n"
171 "import " + std::string(dir.path) + "\n"
172 "import " + std::string(last_import.path) + "\n"
173 "on boot\n"
174 "execute 1";
175 // clang-format on
176 TemporaryFile start;
177 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
178
179 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700180 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700181 EXPECT_EQ(2U, args.size());
182 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherry557946e2017-08-01 13:50:23 -0700183 return Success();
Tom Cherryad54d092017-04-19 16:18:50 -0700184 };
185
186 TestFunctionMap test_function_map;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700187 test_function_map.Add("execute", 1, 1, false, execute_command);
Tom Cherryad54d092017-04-19 16:18:50 -0700188
189 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
190 std::vector<ActionManagerCommand> commands{trigger_boot};
191
Steven Moreland6f5333a2017-11-13 15:31:54 -0800192 ServiceList service_list;
193
194 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700195
196 EXPECT_EQ(6, num_executed);
197}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700198
199} // namespace init
200} // namespace android