blob: 268873c12c69a75cfa7cd76a9c02389de2aa7736 [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"
24#include "builtins.h"
25#include "import_parser.h"
Tom Cherryad54d092017-04-19 16:18:50 -070026#include "keyword_map.h"
Tom Cherry67dee622017-07-27 12:54:48 -070027#include "parser.h"
Steven Moreland6f5333a2017-11-13 15:31:54 -080028#include "service.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070029#include "test_function_map.h"
Tom Cherryad54d092017-04-19 16:18:50 -070030#include "util.h"
31
Tom Cherry81f5d3e2017-06-22 12:53:17 -070032namespace android {
33namespace init {
34
Tom Cherryad54d092017-04-19 16:18:50 -070035using ActionManagerCommand = std::function<void(ActionManager&)>;
36
37void TestInit(const std::string& init_script_file, const TestFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080038 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070039 ActionManager am;
40
41 Action::set_function_map(&test_function_map);
42
43 Parser parser;
Steven Moreland6f5333a2017-11-13 15:31:54 -080044 parser.AddSectionParser("service", std::make_unique<ServiceParser>(service_list, nullptr));
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070045 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070046 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
47
48 ASSERT_TRUE(parser.ParseConfig(init_script_file));
49
50 for (const auto& command : commands) {
51 command(am);
52 }
53
54 while (am.HasMoreCommands()) {
55 am.ExecuteOneCommand();
56 }
57}
58
59void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map,
Steven Moreland6f5333a2017-11-13 15:31:54 -080060 const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
Tom Cherryad54d092017-04-19 16:18:50 -070061 TemporaryFile tf;
62 ASSERT_TRUE(tf.fd != -1);
63 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
Steven Moreland6f5333a2017-11-13 15:31:54 -080064 TestInit(tf.path, test_function_map, commands, service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070065}
66
67TEST(init, SimpleEventTrigger) {
68 bool expect_true = false;
69 std::string init_script =
70 R"init(
71on boot
72pass_test
73)init";
74
75 TestFunctionMap test_function_map;
76 test_function_map.Add("pass_test", [&expect_true]() { expect_true = true; });
77
78 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
79 std::vector<ActionManagerCommand> commands{trigger_boot};
80
Steven Moreland6f5333a2017-11-13 15:31:54 -080081 ServiceList service_list;
82 TestInitText(init_script, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -070083
84 EXPECT_TRUE(expect_true);
85}
86
87TEST(init, EventTriggerOrder) {
88 std::string init_script =
89 R"init(
90on boot
91execute_first
92
93on boot && property:ro.hardware=*
94execute_second
95
96on boot
97execute_third
98
99)init";
100
101 int num_executed = 0;
102 TestFunctionMap test_function_map;
103 test_function_map.Add("execute_first", [&num_executed]() { EXPECT_EQ(0, num_executed++); });
104 test_function_map.Add("execute_second", [&num_executed]() { EXPECT_EQ(1, num_executed++); });
105 test_function_map.Add("execute_third", [&num_executed]() { EXPECT_EQ(2, num_executed++); });
106
107 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
108 std::vector<ActionManagerCommand> commands{trigger_boot};
109
Steven Moreland6f5333a2017-11-13 15:31:54 -0800110 ServiceList service_list;
111 TestInitText(init_script, test_function_map, commands, &service_list);
112}
113
114TEST(init, OverrideService) {
115 std::string init_script = R"init(
116service A something
117 class first
118
119service A something
120 class second
121 override
122
123)init";
124
125 ServiceList service_list;
126 TestInitText(init_script, TestFunctionMap(), {}, &service_list);
127 ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
128
129 auto service = service_list.begin()->get();
130 ASSERT_NE(nullptr, service);
131 EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
132 EXPECT_EQ("A", service->name());
133 EXPECT_TRUE(service->is_override());
Tom Cherryad54d092017-04-19 16:18:50 -0700134}
135
136TEST(init, EventTriggerOrderMultipleFiles) {
137 // 6 total files, which should have their triggers executed in the following order:
138 // 1: start - original script parsed
139 // 2: first_import - immediately imported by first_script
140 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
141 // 4: a_import - file imported by dir_a
142 // 5: dir_b - file named 'b.rc' in dir
143 // 6: last_import - imported after dir is imported
144
145 TemporaryFile first_import;
146 ASSERT_TRUE(first_import.fd != -1);
147 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
148
149 TemporaryFile dir_a_import;
150 ASSERT_TRUE(dir_a_import.fd != -1);
151 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
152
153 TemporaryFile last_import;
154 ASSERT_TRUE(last_import.fd != -1);
155 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
156
157 TemporaryDir dir;
158 // clang-format off
159 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
160 "on boot\n"
161 "execute 3";
162 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700163 // WriteFile() ensures the right mode is set
Tom Cherry11a3aee2017-08-03 12:54:07 -0700164 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700165
Tom Cherry11a3aee2017-08-03 12:54:07 -0700166 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700167
168 // clang-format off
169 std::string start_script = "import " + std::string(first_import.path) + "\n"
170 "import " + std::string(dir.path) + "\n"
171 "import " + std::string(last_import.path) + "\n"
172 "on boot\n"
173 "execute 1";
174 // clang-format on
175 TemporaryFile start;
176 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
177
178 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700179 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700180 EXPECT_EQ(2U, args.size());
181 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherry557946e2017-08-01 13:50:23 -0700182 return Success();
Tom Cherryad54d092017-04-19 16:18:50 -0700183 };
184
185 TestFunctionMap test_function_map;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700186 test_function_map.Add("execute", 1, 1, false, execute_command);
Tom Cherryad54d092017-04-19 16:18:50 -0700187
188 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
189 std::vector<ActionManagerCommand> commands{trigger_boot};
190
Steven Moreland6f5333a2017-11-13 15:31:54 -0800191 ServiceList service_list;
192
193 TestInit(start.path, test_function_map, commands, &service_list);
Tom Cherryad54d092017-04-19 16:18:50 -0700194
195 EXPECT_EQ(6, num_executed);
196}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700197
198} // namespace init
199} // namespace android