blob: a09db186ef9db96d3802703c3d47bfa400d5fe39 [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 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 Cherry2aeb1ad2019-06-26 10:46:20 -070030#include "service_list.h"
31#include "service_parser.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070032#include "test_function_map.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
40void TestInit(const std::string& init_script_file, const TestFunctionMap& 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
63void TestInitText(const std::string& init_script, const TestFunctionMap& 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
79 TestFunctionMap test_function_map;
80 test_function_map.Add("pass_test", [&expect_true]() { expect_true = true; });
81
82 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
83 std::vector<ActionManagerCommand> commands{trigger_boot};
84
Steven Moreland6f5333a2017-11-13 15:31:54 -080085 ServiceList service_list;
86 TestInitText(init_script, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070087
88 EXPECT_TRUE(expect_true);
89}
90
91TEST(init, EventTriggerOrder) {
92 std::string init_script =
93 R"init(
94on boot
95execute_first
96
97on boot && property:ro.hardware=*
98execute_second
99
100on boot
101execute_third
102
103)init";
104
105 int num_executed = 0;
106 TestFunctionMap test_function_map;
107 test_function_map.Add("execute_first", [&num_executed]() { EXPECT_EQ(0, num_executed++); });
108 test_function_map.Add("execute_second", [&num_executed]() { EXPECT_EQ(1, num_executed++); });
109 test_function_map.Add("execute_third", [&num_executed]() { EXPECT_EQ(2, num_executed++); });
110
111 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
112 std::vector<ActionManagerCommand> commands{trigger_boot};
113
Steven Moreland6f5333a2017-11-13 15:31:54 -0800114 ServiceList service_list;
115 TestInitText(init_script, test_function_map, commands, &service_list);
116}
117
118TEST(init, OverrideService) {
119 std::string init_script = R"init(
120service A something
121 class first
122
123service A something
124 class second
125 override
126
127)init";
128
129 ServiceList service_list;
130 TestInitText(init_script, TestFunctionMap(), {}, &service_list);
131 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
132
133 auto service = service_list.begin()->get();
134 ASSERT_NE(nullptr, service);
135 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
136 EXPECT_EQ("A", service->name());
137 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700138}
139
140TEST(init, EventTriggerOrderMultipleFiles) {
141 // 6 total files, which should have their triggers executed in the following order:
142 // 1: start - original script parsed
143 // 2: first_import - immediately imported by first_script
144 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
145 // 4: a_import - file imported by dir_a
146 // 5: dir_b - file named 'b.rc' in dir
147 // 6: last_import - imported after dir is imported
148
149 TemporaryFile first_import;
150 ASSERT_TRUE(first_import.fd != -1);
151 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
152
153 TemporaryFile dir_a_import;
154 ASSERT_TRUE(dir_a_import.fd != -1);
155 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
156
157 TemporaryFile last_import;
158 ASSERT_TRUE(last_import.fd != -1);
159 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
160
161 TemporaryDir dir;
162 // clang-format off
163 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
164 "on boot\n"
165 "execute 3";
166 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700167 // WriteFile() ensures the right mode is set
Tom Cherry11a3aee2017-08-03 12:54:07 -0700168 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700169
Tom Cherry11a3aee2017-08-03 12:54:07 -0700170 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700171
172 // clang-format off
173 std::string start_script = "import " + std::string(first_import.path) + "\n"
174 "import " + std::string(dir.path) + "\n"
175 "import " + std::string(last_import.path) + "\n"
176 "on boot\n"
177 "execute 1";
178 // clang-format on
179 TemporaryFile start;
180 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
181
182 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700183 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700184 EXPECT_EQ(2U, args.size());
185 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700186 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700187 };
188
189 TestFunctionMap test_function_map;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700190 test_function_map.Add("execute", 1, 1, false, execute_command);
Tom Cherryad54d092017-04-19 16:18:50 -0700191
192 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
193 std::vector<ActionManagerCommand> commands{trigger_boot};
194
Steven Moreland6f5333a2017-11-13 15:31:54 -0800195 ServiceList service_list;
196
197 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700198
199 EXPECT_EQ(6, num_executed);
200}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700201
202} // namespace init
203} // namespace android