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