Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include "action.h" |
Tom Cherry | 7fd3bc2 | 2018-02-13 15:36:14 -0800 | [diff] [blame] | 23 | #include "action_manager.h" |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 24 | #include "action_parser.h" |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 25 | #include "builtin_arguments.h" |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 26 | #include "builtins.h" |
| 27 | #include "import_parser.h" |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 28 | #include "keyword_map.h" |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 29 | #include "parser.h" |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 30 | #include "service.h" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 31 | #include "service_list.h" |
| 32 | #include "service_parser.h" |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 33 | #include "util.h" |
| 34 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace init { |
| 37 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 38 | using ActionManagerCommand = std::function<void(ActionManager&)>; |
| 39 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 40 | void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map, |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 41 | const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 42 | ActionManager am; |
| 43 | |
| 44 | Action::set_function_map(&test_function_map); |
| 45 | |
| 46 | Parser parser; |
Daniel Norman | 3df8dc5 | 2019-06-27 12:18:08 -0700 | [diff] [blame] | 47 | parser.AddSectionParser("service", |
| 48 | std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt)); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 49 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr)); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 50 | parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser)); |
| 51 | |
| 52 | ASSERT_TRUE(parser.ParseConfig(init_script_file)); |
| 53 | |
| 54 | for (const auto& command : commands) { |
| 55 | command(am); |
| 56 | } |
| 57 | |
| 58 | while (am.HasMoreCommands()) { |
| 59 | am.ExecuteOneCommand(); |
| 60 | } |
| 61 | } |
| 62 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 63 | void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map, |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 64 | const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 65 | TemporaryFile tf; |
| 66 | ASSERT_TRUE(tf.fd != -1); |
| 67 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 68 | TestInit(tf.path, test_function_map, commands, service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TEST(init, SimpleEventTrigger) { |
| 72 | bool expect_true = false; |
| 73 | std::string init_script = |
| 74 | R"init( |
| 75 | on boot |
| 76 | pass_test |
| 77 | )init"; |
| 78 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 79 | auto do_pass_test = [&expect_true](const BuiltinArguments&) { |
| 80 | expect_true = true; |
| 81 | return Result<void>{}; |
| 82 | }; |
| 83 | BuiltinFunctionMap test_function_map = { |
| 84 | {"pass_test", {0, 0, {false, do_pass_test}}}, |
| 85 | }; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 86 | |
| 87 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 88 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 89 | |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 90 | ServiceList service_list; |
| 91 | TestInitText(init_script, test_function_map, commands, &service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 92 | |
| 93 | EXPECT_TRUE(expect_true); |
| 94 | } |
| 95 | |
| 96 | TEST(init, EventTriggerOrder) { |
| 97 | std::string init_script = |
| 98 | R"init( |
| 99 | on boot |
| 100 | execute_first |
| 101 | |
| 102 | on boot && property:ro.hardware=* |
| 103 | execute_second |
| 104 | |
| 105 | on boot |
| 106 | execute_third |
| 107 | |
| 108 | )init"; |
| 109 | |
| 110 | int num_executed = 0; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 111 | auto do_execute_first = [&num_executed](const BuiltinArguments&) { |
| 112 | EXPECT_EQ(0, num_executed++); |
| 113 | return Result<void>{}; |
| 114 | }; |
| 115 | auto do_execute_second = [&num_executed](const BuiltinArguments&) { |
| 116 | EXPECT_EQ(1, num_executed++); |
| 117 | return Result<void>{}; |
| 118 | }; |
| 119 | auto do_execute_third = [&num_executed](const BuiltinArguments&) { |
| 120 | EXPECT_EQ(2, num_executed++); |
| 121 | return Result<void>{}; |
| 122 | }; |
| 123 | |
| 124 | BuiltinFunctionMap test_function_map = { |
| 125 | {"execute_first", {0, 0, {false, do_execute_first}}}, |
| 126 | {"execute_second", {0, 0, {false, do_execute_second}}}, |
| 127 | {"execute_third", {0, 0, {false, do_execute_third}}}, |
| 128 | }; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 129 | |
| 130 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 131 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 132 | |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 133 | ServiceList service_list; |
| 134 | TestInitText(init_script, test_function_map, commands, &service_list); |
| 135 | } |
| 136 | |
| 137 | TEST(init, OverrideService) { |
| 138 | std::string init_script = R"init( |
| 139 | service A something |
| 140 | class first |
| 141 | |
| 142 | service A something |
| 143 | class second |
| 144 | override |
| 145 | |
| 146 | )init"; |
| 147 | |
| 148 | ServiceList service_list; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 149 | TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list); |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 150 | ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end())); |
| 151 | |
| 152 | auto service = service_list.begin()->get(); |
| 153 | ASSERT_NE(nullptr, service); |
| 154 | EXPECT_EQ(std::set<std::string>({"second"}), service->classnames()); |
| 155 | EXPECT_EQ("A", service->name()); |
| 156 | EXPECT_TRUE(service->is_override()); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST(init, EventTriggerOrderMultipleFiles) { |
| 160 | // 6 total files, which should have their triggers executed in the following order: |
| 161 | // 1: start - original script parsed |
| 162 | // 2: first_import - immediately imported by first_script |
| 163 | // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import |
| 164 | // 4: a_import - file imported by dir_a |
| 165 | // 5: dir_b - file named 'b.rc' in dir |
| 166 | // 6: last_import - imported after dir is imported |
| 167 | |
| 168 | TemporaryFile first_import; |
| 169 | ASSERT_TRUE(first_import.fd != -1); |
| 170 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd)); |
| 171 | |
| 172 | TemporaryFile dir_a_import; |
| 173 | ASSERT_TRUE(dir_a_import.fd != -1); |
| 174 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd)); |
| 175 | |
| 176 | TemporaryFile last_import; |
| 177 | ASSERT_TRUE(last_import.fd != -1); |
| 178 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd)); |
| 179 | |
| 180 | TemporaryDir dir; |
| 181 | // clang-format off |
| 182 | std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n" |
| 183 | "on boot\n" |
| 184 | "execute 3"; |
| 185 | // clang-format on |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 186 | // WriteFile() ensures the right mode is set |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 187 | ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script)); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 188 | |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 189 | ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5")); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 190 | |
| 191 | // clang-format off |
| 192 | std::string start_script = "import " + std::string(first_import.path) + "\n" |
| 193 | "import " + std::string(dir.path) + "\n" |
| 194 | "import " + std::string(last_import.path) + "\n" |
| 195 | "on boot\n" |
| 196 | "execute 1"; |
| 197 | // clang-format on |
| 198 | TemporaryFile start; |
| 199 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); |
| 200 | |
| 201 | int num_executed = 0; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 202 | auto execute_command = [&num_executed](const BuiltinArguments& args) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 203 | EXPECT_EQ(2U, args.size()); |
| 204 | EXPECT_EQ(++num_executed, std::stoi(args[1])); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 205 | return Result<void>{}; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame^] | 208 | BuiltinFunctionMap test_function_map = { |
| 209 | {"execute", {1, 1, {false, execute_command}}}, |
| 210 | }; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 211 | |
| 212 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 213 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 214 | |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 215 | ServiceList service_list; |
| 216 | |
| 217 | TestInit(start.path, test_function_map, commands, &service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 218 | |
| 219 | EXPECT_EQ(6, num_executed); |
| 220 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 221 | |
| 222 | } // namespace init |
| 223 | } // namespace android |