blob: 1bcc5eff2b0c8c097936251bdaf13031fdadd1b2 [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;
Steven Moreland6f5333a2017-11-13 15:31:54 -080047 parser.AddSectionParser("service", std::make_unique<ServiceParser>(service_list, nullptr));
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070048 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070049 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
50
51 ASSERT_TRUE(parser.ParseConfig(init_script_file));
52
53 for (const auto& command : commands) {
54 command(am);
55 }
56
57 while (am.HasMoreCommands()) {
58 am.ExecuteOneCommand();
59 }
60}
61
62void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080063 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070064 TemporaryFile tf;
65 ASSERT_TRUE(tf.fd != -1);
66 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Steven Moreland6f5333a2017-11-13 15:31:54 -080067 TestInit(tf.path, test_function_map, commands, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070068}
69
70TEST(init, SimpleEventTrigger) {
71 bool expect_true = false;
72 std::string init_script =
73 R"init(
74on boot
75pass_test
76)init";
77
78 TestFunctionMap test_function_map;
79 test_function_map.Add("pass_test", [&expect_true]() { expect_true = true; });
80
81 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
82 std::vector<ActionManagerCommand> commands{trigger_boot};
83
Steven Moreland6f5333a2017-11-13 15:31:54 -080084 ServiceList service_list;
85 TestInitText(init_script, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070086
87 EXPECT_TRUE(expect_true);
88}
89
90TEST(init, EventTriggerOrder) {
91 std::string init_script =
92 R"init(
93on boot
94execute_first
95
96on boot && property:ro.hardware=*
97execute_second
98
99on boot
100execute_third
101
102)init";
103
104 int num_executed = 0;
105 TestFunctionMap test_function_map;
106 test_function_map.Add("execute_first", [&num_executed]() { EXPECT_EQ(0, num_executed++); });
107 test_function_map.Add("execute_second", [&num_executed]() { EXPECT_EQ(1, num_executed++); });
108 test_function_map.Add("execute_third", [&num_executed]() { EXPECT_EQ(2, num_executed++); });
109
110 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
111 std::vector<ActionManagerCommand> commands{trigger_boot};
112
Steven Moreland6f5333a2017-11-13 15:31:54 -0800113 ServiceList service_list;
114 TestInitText(init_script, test_function_map, commands, &service_list);
115}
116
117TEST(init, OverrideService) {
118 std::string init_script = R"init(
119service A something
120 class first
121
122service A something
123 class second
124 override
125
126)init";
127
128 ServiceList service_list;
129 TestInitText(init_script, TestFunctionMap(), {}, &service_list);
130 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
131
132 auto service = service_list.begin()->get();
133 ASSERT_NE(nullptr, service);
134 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
135 EXPECT_EQ("A", service->name());
136 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700137}
138
139TEST(init, EventTriggerOrderMultipleFiles) {
140 // 6 total files, which should have their triggers executed in the following order:
141 // 1: start - original script parsed
142 // 2: first_import - immediately imported by first_script
143 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
144 // 4: a_import - file imported by dir_a
145 // 5: dir_b - file named 'b.rc' in dir
146 // 6: last_import - imported after dir is imported
147
148 TemporaryFile first_import;
149 ASSERT_TRUE(first_import.fd != -1);
150 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
151
152 TemporaryFile dir_a_import;
153 ASSERT_TRUE(dir_a_import.fd != -1);
154 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
155
156 TemporaryFile last_import;
157 ASSERT_TRUE(last_import.fd != -1);
158 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
159
160 TemporaryDir dir;
161 // clang-format off
162 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
163 "on boot\n"
164 "execute 3";
165 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700166 // WriteFile() ensures the right mode is set
Tom Cherry11a3aee2017-08-03 12:54:07 -0700167 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700168
Tom Cherry11a3aee2017-08-03 12:54:07 -0700169 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700170
171 // clang-format off
172 std::string start_script = "import " + std::string(first_import.path) + "\n"
173 "import " + std::string(dir.path) + "\n"
174 "import " + std::string(last_import.path) + "\n"
175 "on boot\n"
176 "execute 1";
177 // clang-format on
178 TemporaryFile start;
179 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
180
181 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700182 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700183 EXPECT_EQ(2U, args.size());
184 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700185 return Result<void>{};
Tom Cherryad54d092017-04-19 16:18:50 -0700186 };
187
188 TestFunctionMap test_function_map;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700189 test_function_map.Add("execute", 1, 1, false, execute_command);
Tom Cherryad54d092017-04-19 16:18:50 -0700190
191 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
192 std::vector<ActionManagerCommand> commands{trigger_boot};
193
Steven Moreland6f5333a2017-11-13 15:31:54 -0800194 ServiceList service_list;
195
196 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700197
198 EXPECT_EQ(6, num_executed);
199}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700200
201} // namespace init
202} // namespace android