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