blob: 0a4071b98c1bd89052c5d67006fe54d493ebbd71 [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"
26#include "init_parser.h"
27#include "keyword_map.h"
28#include "util.h"
29
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030namespace android {
31namespace init {
32
Tom Cherryad54d092017-04-19 16:18:50 -070033class TestFunctionMap : public KeywordMap<BuiltinFunction> {
34 public:
35 // Helper for argument-less functions
36 using BuiltinFunctionNoArgs = std::function<void(void)>;
37 void Add(const std::string& name, const BuiltinFunctionNoArgs function) {
38 Add(name, 0, 0, [function](const std::vector<std::string>&) {
39 function();
40 return 0;
41 });
42 }
43
44 void Add(const std::string& name, std::size_t min_parameters, std::size_t max_parameters,
45 const BuiltinFunction function) {
46 builtin_functions_[name] = make_tuple(min_parameters, max_parameters, function);
47 }
48
49 private:
50 Map builtin_functions_ = {};
51
52 const Map& map() const override { return builtin_functions_; }
53};
54
55using ActionManagerCommand = std::function<void(ActionManager&)>;
56
57void TestInit(const std::string& init_script_file, const TestFunctionMap& test_function_map,
58 const std::vector<ActionManagerCommand>& commands) {
59 ActionManager am;
60
61 Action::set_function_map(&test_function_map);
62
63 Parser parser;
64 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am));
65 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
66
67 ASSERT_TRUE(parser.ParseConfig(init_script_file));
68
69 for (const auto& command : commands) {
70 command(am);
71 }
72
73 while (am.HasMoreCommands()) {
74 am.ExecuteOneCommand();
75 }
76}
77
78void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map,
79 const std::vector<ActionManagerCommand>& commands) {
80 TemporaryFile tf;
81 ASSERT_TRUE(tf.fd != -1);
82 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
83 TestInit(tf.path, test_function_map, commands);
84}
85
86TEST(init, SimpleEventTrigger) {
87 bool expect_true = false;
88 std::string init_script =
89 R"init(
90on boot
91pass_test
92)init";
93
94 TestFunctionMap test_function_map;
95 test_function_map.Add("pass_test", [&expect_true]() { expect_true = true; });
96
97 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
98 std::vector<ActionManagerCommand> commands{trigger_boot};
99
100 TestInitText(init_script, test_function_map, commands);
101
102 EXPECT_TRUE(expect_true);
103}
104
105TEST(init, EventTriggerOrder) {
106 std::string init_script =
107 R"init(
108on boot
109execute_first
110
111on boot && property:ro.hardware=*
112execute_second
113
114on boot
115execute_third
116
117)init";
118
119 int num_executed = 0;
120 TestFunctionMap test_function_map;
121 test_function_map.Add("execute_first", [&num_executed]() { EXPECT_EQ(0, num_executed++); });
122 test_function_map.Add("execute_second", [&num_executed]() { EXPECT_EQ(1, num_executed++); });
123 test_function_map.Add("execute_third", [&num_executed]() { EXPECT_EQ(2, num_executed++); });
124
125 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
126 std::vector<ActionManagerCommand> commands{trigger_boot};
127
128 TestInitText(init_script, test_function_map, commands);
129}
130
131TEST(init, EventTriggerOrderMultipleFiles) {
132 // 6 total files, which should have their triggers executed in the following order:
133 // 1: start - original script parsed
134 // 2: first_import - immediately imported by first_script
135 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
136 // 4: a_import - file imported by dir_a
137 // 5: dir_b - file named 'b.rc' in dir
138 // 6: last_import - imported after dir is imported
139
140 TemporaryFile first_import;
141 ASSERT_TRUE(first_import.fd != -1);
142 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
143
144 TemporaryFile dir_a_import;
145 ASSERT_TRUE(dir_a_import.fd != -1);
146 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
147
148 TemporaryFile last_import;
149 ASSERT_TRUE(last_import.fd != -1);
150 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
151
152 TemporaryDir dir;
153 // clang-format off
154 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
155 "on boot\n"
156 "execute 3";
157 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700158 // WriteFile() ensures the right mode is set
159 std::string err;
160 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script, &err));
Tom Cherryad54d092017-04-19 16:18:50 -0700161
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700162 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5", &err));
Tom Cherryad54d092017-04-19 16:18:50 -0700163
164 // clang-format off
165 std::string start_script = "import " + std::string(first_import.path) + "\n"
166 "import " + std::string(dir.path) + "\n"
167 "import " + std::string(last_import.path) + "\n"
168 "on boot\n"
169 "execute 1";
170 // clang-format on
171 TemporaryFile start;
172 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
173
174 int num_executed = 0;
175 auto execute_command = [&num_executed](const std::vector<std::string>& args) {
176 EXPECT_EQ(2U, args.size());
177 EXPECT_EQ(++num_executed, std::stoi(args[1]));
178 return 0;
179 };
180
181 TestFunctionMap test_function_map;
182 test_function_map.Add("execute", 1, 1, execute_command);
183
184 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
185 std::vector<ActionManagerCommand> commands{trigger_boot};
186
187 TestInit(start.path, test_function_map, commands);
188
189 EXPECT_EQ(6, num_executed);
190}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700191
192} // namespace init
193} // namespace android