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