| 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 |  | 
| Nikita Ioffe | aaab596 | 2019-10-10 20:42:37 +0100 | [diff] [blame] | 96 | TEST(init, WrongEventTrigger) { | 
|  | 97 | std::string init_script = | 
|  | 98 | R"init( | 
|  | 99 | on boot: | 
|  | 100 | pass_test | 
|  | 101 | )init"; | 
|  | 102 |  | 
|  | 103 | TemporaryFile tf; | 
|  | 104 | ASSERT_TRUE(tf.fd != -1); | 
|  | 105 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); | 
|  | 106 |  | 
|  | 107 | ActionManager am; | 
|  | 108 |  | 
|  | 109 | Parser parser; | 
|  | 110 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr)); | 
|  | 111 |  | 
|  | 112 | ASSERT_TRUE(parser.ParseConfig(tf.path)); | 
|  | 113 | ASSERT_EQ(1u, parser.parse_error_count()); | 
|  | 114 | } | 
|  | 115 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 116 | TEST(init, EventTriggerOrder) { | 
|  | 117 | std::string init_script = | 
|  | 118 | R"init( | 
|  | 119 | on boot | 
|  | 120 | execute_first | 
|  | 121 |  | 
|  | 122 | on boot && property:ro.hardware=* | 
|  | 123 | execute_second | 
|  | 124 |  | 
|  | 125 | on boot | 
|  | 126 | execute_third | 
|  | 127 |  | 
|  | 128 | )init"; | 
|  | 129 |  | 
|  | 130 | int num_executed = 0; | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 131 | auto do_execute_first = [&num_executed](const BuiltinArguments&) { | 
|  | 132 | EXPECT_EQ(0, num_executed++); | 
|  | 133 | return Result<void>{}; | 
|  | 134 | }; | 
|  | 135 | auto do_execute_second = [&num_executed](const BuiltinArguments&) { | 
|  | 136 | EXPECT_EQ(1, num_executed++); | 
|  | 137 | return Result<void>{}; | 
|  | 138 | }; | 
|  | 139 | auto do_execute_third = [&num_executed](const BuiltinArguments&) { | 
|  | 140 | EXPECT_EQ(2, num_executed++); | 
|  | 141 | return Result<void>{}; | 
|  | 142 | }; | 
|  | 143 |  | 
|  | 144 | BuiltinFunctionMap test_function_map = { | 
|  | 145 | {"execute_first", {0, 0, {false, do_execute_first}}}, | 
|  | 146 | {"execute_second", {0, 0, {false, do_execute_second}}}, | 
|  | 147 | {"execute_third", {0, 0, {false, do_execute_third}}}, | 
|  | 148 | }; | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 149 |  | 
|  | 150 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; | 
|  | 151 | std::vector<ActionManagerCommand> commands{trigger_boot}; | 
|  | 152 |  | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 153 | ServiceList service_list; | 
|  | 154 | TestInitText(init_script, test_function_map, commands, &service_list); | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | TEST(init, OverrideService) { | 
|  | 158 | std::string init_script = R"init( | 
|  | 159 | service A something | 
|  | 160 | class first | 
|  | 161 |  | 
|  | 162 | service A something | 
|  | 163 | class second | 
|  | 164 | override | 
|  | 165 |  | 
|  | 166 | )init"; | 
|  | 167 |  | 
|  | 168 | ServiceList service_list; | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 169 | TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list); | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 170 | ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end())); | 
|  | 171 |  | 
|  | 172 | auto service = service_list.begin()->get(); | 
|  | 173 | ASSERT_NE(nullptr, service); | 
|  | 174 | EXPECT_EQ(std::set<std::string>({"second"}), service->classnames()); | 
|  | 175 | EXPECT_EQ("A", service->name()); | 
|  | 176 | EXPECT_TRUE(service->is_override()); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
|  | 179 | TEST(init, EventTriggerOrderMultipleFiles) { | 
|  | 180 | // 6 total files, which should have their triggers executed in the following order: | 
|  | 181 | // 1: start - original script parsed | 
|  | 182 | // 2: first_import - immediately imported by first_script | 
|  | 183 | // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import | 
|  | 184 | // 4: a_import - file imported by dir_a | 
|  | 185 | // 5: dir_b - file named 'b.rc' in dir | 
|  | 186 | // 6: last_import - imported after dir is imported | 
|  | 187 |  | 
|  | 188 | TemporaryFile first_import; | 
|  | 189 | ASSERT_TRUE(first_import.fd != -1); | 
|  | 190 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd)); | 
|  | 191 |  | 
|  | 192 | TemporaryFile dir_a_import; | 
|  | 193 | ASSERT_TRUE(dir_a_import.fd != -1); | 
|  | 194 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd)); | 
|  | 195 |  | 
|  | 196 | TemporaryFile last_import; | 
|  | 197 | ASSERT_TRUE(last_import.fd != -1); | 
|  | 198 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd)); | 
|  | 199 |  | 
|  | 200 | TemporaryDir dir; | 
|  | 201 | // clang-format off | 
|  | 202 | std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n" | 
|  | 203 | "on boot\n" | 
|  | 204 | "execute 3"; | 
|  | 205 | // clang-format on | 
| Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 206 | // WriteFile() ensures the right mode is set | 
| Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 207 | ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script)); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 208 |  | 
| Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 209 | ASSERT_RESULT_OK(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5")); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 210 |  | 
|  | 211 | // clang-format off | 
|  | 212 | std::string start_script = "import " + std::string(first_import.path) + "\n" | 
|  | 213 | "import " + std::string(dir.path) + "\n" | 
|  | 214 | "import " + std::string(last_import.path) + "\n" | 
|  | 215 | "on boot\n" | 
|  | 216 | "execute 1"; | 
|  | 217 | // clang-format on | 
|  | 218 | TemporaryFile start; | 
|  | 219 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); | 
|  | 220 |  | 
|  | 221 | int num_executed = 0; | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 222 | auto execute_command = [&num_executed](const BuiltinArguments& args) { | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 223 | EXPECT_EQ(2U, args.size()); | 
|  | 224 | EXPECT_EQ(++num_executed, std::stoi(args[1])); | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 225 | return Result<void>{}; | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 226 | }; | 
|  | 227 |  | 
| Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 228 | BuiltinFunctionMap test_function_map = { | 
|  | 229 | {"execute", {1, 1, {false, execute_command}}}, | 
|  | 230 | }; | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 231 |  | 
|  | 232 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; | 
|  | 233 | std::vector<ActionManagerCommand> commands{trigger_boot}; | 
|  | 234 |  | 
| Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 235 | ServiceList service_list; | 
|  | 236 |  | 
|  | 237 | TestInit(start.path, test_function_map, commands, &service_list); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 238 |  | 
|  | 239 | EXPECT_EQ(6, num_executed); | 
|  | 240 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 241 |  | 
|  | 242 | }  // namespace init | 
|  | 243 | }  // namespace android | 
| Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 244 |  | 
|  | 245 | int SubcontextTestChildMain(int, char**); | 
|  | 246 | int FirmwareTestChildMain(int, char**); | 
|  | 247 |  | 
|  | 248 | int main(int argc, char** argv) { | 
|  | 249 | if (argc > 1 && !strcmp(argv[1], "subcontext")) { | 
|  | 250 | return SubcontextTestChildMain(argc, argv); | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | if (argc > 1 && !strcmp(argv[1], "firmware")) { | 
|  | 254 | return FirmwareTestChildMain(argc, argv); | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | testing::InitGoogleTest(&argc, argv); | 
|  | 258 | return RUN_ALL_TESTS(); | 
|  | 259 | } |