blob: 29a65abb9131e16d04060e2f545fa1355990f330 [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"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070028#include "test_function_map.h"
Tom Cherryad54d092017-04-19 16:18:50 -070029#include "util.h"
30
Tom Cherry81f5d3e2017-06-22 12:53:17 -070031namespace android {
32namespace init {
33
Tom Cherryad54d092017-04-19 16:18:50 -070034using ActionManagerCommand = std::function<void(ActionManager&)>;
35
36void TestInit(const std::string& init_script_file, const TestFunctionMap& test_function_map,
37 const std::vector<ActionManagerCommand>& commands) {
38 ActionManager am;
39
40 Action::set_function_map(&test_function_map);
41
42 Parser parser;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070043 parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
Tom Cherryad54d092017-04-19 16:18:50 -070044 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
45
46 ASSERT_TRUE(parser.ParseConfig(init_script_file));
47
48 for (const auto& command : commands) {
49 command(am);
50 }
51
52 while (am.HasMoreCommands()) {
53 am.ExecuteOneCommand();
54 }
55}
56
57void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map,
58 const std::vector<ActionManagerCommand>& commands) {
59 TemporaryFile tf;
60 ASSERT_TRUE(tf.fd != -1);
61 ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
62 TestInit(tf.path, test_function_map, commands);
63}
64
65TEST(init, SimpleEventTrigger) {
66 bool expect_true = false;
67 std::string init_script =
68 R"init(
69on boot
70pass_test
71)init";
72
73 TestFunctionMap test_function_map;
74 test_function_map.Add("pass_test", [&expect_true]() { expect_true = true; });
75
76 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
77 std::vector<ActionManagerCommand> commands{trigger_boot};
78
79 TestInitText(init_script, test_function_map, commands);
80
81 EXPECT_TRUE(expect_true);
82}
83
84TEST(init, EventTriggerOrder) {
85 std::string init_script =
86 R"init(
87on boot
88execute_first
89
90on boot && property:ro.hardware=*
91execute_second
92
93on boot
94execute_third
95
96)init";
97
98 int num_executed = 0;
99 TestFunctionMap test_function_map;
100 test_function_map.Add("execute_first", [&num_executed]() { EXPECT_EQ(0, num_executed++); });
101 test_function_map.Add("execute_second", [&num_executed]() { EXPECT_EQ(1, num_executed++); });
102 test_function_map.Add("execute_third", [&num_executed]() { EXPECT_EQ(2, num_executed++); });
103
104 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
105 std::vector<ActionManagerCommand> commands{trigger_boot};
106
107 TestInitText(init_script, test_function_map, commands);
108}
109
110TEST(init, EventTriggerOrderMultipleFiles) {
111 // 6 total files, which should have their triggers executed in the following order:
112 // 1: start - original script parsed
113 // 2: first_import - immediately imported by first_script
114 // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
115 // 4: a_import - file imported by dir_a
116 // 5: dir_b - file named 'b.rc' in dir
117 // 6: last_import - imported after dir is imported
118
119 TemporaryFile first_import;
120 ASSERT_TRUE(first_import.fd != -1);
121 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
122
123 TemporaryFile dir_a_import;
124 ASSERT_TRUE(dir_a_import.fd != -1);
125 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
126
127 TemporaryFile last_import;
128 ASSERT_TRUE(last_import.fd != -1);
129 ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
130
131 TemporaryDir dir;
132 // clang-format off
133 std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
134 "on boot\n"
135 "execute 3";
136 // clang-format on
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700137 // WriteFile() ensures the right mode is set
Tom Cherry11a3aee2017-08-03 12:54:07 -0700138 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
Tom Cherryad54d092017-04-19 16:18:50 -0700139
Tom Cherry11a3aee2017-08-03 12:54:07 -0700140 ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
Tom Cherryad54d092017-04-19 16:18:50 -0700141
142 // clang-format off
143 std::string start_script = "import " + std::string(first_import.path) + "\n"
144 "import " + std::string(dir.path) + "\n"
145 "import " + std::string(last_import.path) + "\n"
146 "on boot\n"
147 "execute 1";
148 // clang-format on
149 TemporaryFile start;
150 ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
151
152 int num_executed = 0;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700153 auto execute_command = [&num_executed](const BuiltinArguments& args) {
Tom Cherryad54d092017-04-19 16:18:50 -0700154 EXPECT_EQ(2U, args.size());
155 EXPECT_EQ(++num_executed, std::stoi(args[1]));
Tom Cherry557946e2017-08-01 13:50:23 -0700156 return Success();
Tom Cherryad54d092017-04-19 16:18:50 -0700157 };
158
159 TestFunctionMap test_function_map;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700160 test_function_map.Add("execute", 1, 1, false, execute_command);
Tom Cherryad54d092017-04-19 16:18:50 -0700161
162 ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
163 std::vector<ActionManagerCommand> commands{trigger_boot};
164
165 TestInit(start.path, test_function_map, commands);
166
167 EXPECT_EQ(6, num_executed);
168}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700169
170} // namespace init
171} // namespace android