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; |
Jooyung Han | 678f0b4 | 2022-07-07 15:25:02 +0900 | [diff] [blame^] | 38 | using android::base::GetProperty; |
| 39 | using android::base::SetProperty; |
| 40 | using android::base::WaitForProperty; |
| 41 | using namespace std::literals; |
Tom Cherry | 0e40ba3 | 2020-07-09 08:47:24 -0700 | [diff] [blame] | 42 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace init { |
| 45 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 46 | using ActionManagerCommand = std::function<void(ActionManager&)>; |
| 47 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 48 | void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map, |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 49 | const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager, |
| 50 | ServiceList* service_list) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 51 | Action::set_function_map(&test_function_map); |
| 52 | |
| 53 | Parser parser; |
Daniel Norman | 3df8dc5 | 2019-06-27 12:18:08 -0700 | [diff] [blame] | 54 | parser.AddSectionParser("service", |
| 55 | std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt)); |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 56 | parser.AddSectionParser("on", std::make_unique<ActionParser>(action_manager, nullptr)); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 57 | parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser)); |
| 58 | |
| 59 | ASSERT_TRUE(parser.ParseConfig(init_script_file)); |
| 60 | |
| 61 | for (const auto& command : commands) { |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 62 | command(*action_manager); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 65 | while (action_manager->HasMoreCommands()) { |
| 66 | action_manager->ExecuteOneCommand(); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 70 | void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map, |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 71 | const std::vector<ActionManagerCommand>& commands, ActionManager* action_manager, |
| 72 | ServiceList* service_list) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 73 | TemporaryFile tf; |
| 74 | ASSERT_TRUE(tf.fd != -1); |
| 75 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 76 | TestInit(tf.path, test_function_map, commands, action_manager, service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST(init, SimpleEventTrigger) { |
| 80 | bool expect_true = false; |
| 81 | std::string init_script = |
| 82 | R"init( |
| 83 | on boot |
| 84 | pass_test |
| 85 | )init"; |
| 86 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 87 | auto do_pass_test = [&expect_true](const BuiltinArguments&) { |
| 88 | expect_true = true; |
| 89 | return Result<void>{}; |
| 90 | }; |
| 91 | BuiltinFunctionMap test_function_map = { |
| 92 | {"pass_test", {0, 0, {false, do_pass_test}}}, |
| 93 | }; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 94 | |
| 95 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 96 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 97 | |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 98 | ActionManager action_manager; |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 99 | ServiceList service_list; |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 100 | TestInitText(init_script, test_function_map, commands, &action_manager, &service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 101 | |
| 102 | EXPECT_TRUE(expect_true); |
| 103 | } |
| 104 | |
Nikita Ioffe | aaab596 | 2019-10-10 20:42:37 +0100 | [diff] [blame] | 105 | TEST(init, WrongEventTrigger) { |
| 106 | std::string init_script = |
| 107 | R"init( |
| 108 | on boot: |
| 109 | pass_test |
| 110 | )init"; |
| 111 | |
| 112 | TemporaryFile tf; |
| 113 | ASSERT_TRUE(tf.fd != -1); |
| 114 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); |
| 115 | |
| 116 | ActionManager am; |
| 117 | |
| 118 | Parser parser; |
| 119 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr)); |
| 120 | |
| 121 | ASSERT_TRUE(parser.ParseConfig(tf.path)); |
| 122 | ASSERT_EQ(1u, parser.parse_error_count()); |
| 123 | } |
| 124 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 125 | TEST(init, EventTriggerOrder) { |
| 126 | std::string init_script = |
| 127 | R"init( |
| 128 | on boot |
| 129 | execute_first |
| 130 | |
| 131 | on boot && property:ro.hardware=* |
| 132 | execute_second |
| 133 | |
| 134 | on boot |
| 135 | execute_third |
| 136 | |
| 137 | )init"; |
| 138 | |
| 139 | int num_executed = 0; |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 140 | auto do_execute_first = [&num_executed](const BuiltinArguments&) { |
| 141 | EXPECT_EQ(0, num_executed++); |
| 142 | return Result<void>{}; |
| 143 | }; |
| 144 | auto do_execute_second = [&num_executed](const BuiltinArguments&) { |
| 145 | EXPECT_EQ(1, num_executed++); |
| 146 | return Result<void>{}; |
| 147 | }; |
| 148 | auto do_execute_third = [&num_executed](const BuiltinArguments&) { |
| 149 | EXPECT_EQ(2, num_executed++); |
| 150 | return Result<void>{}; |
| 151 | }; |
| 152 | |
| 153 | BuiltinFunctionMap test_function_map = { |
| 154 | {"execute_first", {0, 0, {false, do_execute_first}}}, |
| 155 | {"execute_second", {0, 0, {false, do_execute_second}}}, |
| 156 | {"execute_third", {0, 0, {false, do_execute_third}}}, |
| 157 | }; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 158 | |
| 159 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 160 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 161 | |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 162 | ActionManager action_manager; |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 163 | ServiceList service_list; |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 164 | TestInitText(init_script, test_function_map, commands, &action_manager, &service_list); |
Florian Mayer | 6268f6a | 2022-05-11 01:02:01 +0000 | [diff] [blame] | 165 | EXPECT_EQ(3, num_executed); |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST(init, OverrideService) { |
| 169 | std::string init_script = R"init( |
| 170 | service A something |
| 171 | class first |
| 172 | |
| 173 | service A something |
| 174 | class second |
| 175 | override |
| 176 | |
| 177 | )init"; |
| 178 | |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 179 | ActionManager action_manager; |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 180 | ServiceList service_list; |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 181 | TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list); |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 182 | ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end())); |
| 183 | |
| 184 | auto service = service_list.begin()->get(); |
| 185 | ASSERT_NE(nullptr, service); |
| 186 | EXPECT_EQ(std::set<std::string>({"second"}), service->classnames()); |
| 187 | EXPECT_EQ("A", service->name()); |
| 188 | EXPECT_TRUE(service->is_override()); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | TEST(init, EventTriggerOrderMultipleFiles) { |
| 192 | // 6 total files, which should have their triggers executed in the following order: |
| 193 | // 1: start - original script parsed |
| 194 | // 2: first_import - immediately imported by first_script |
| 195 | // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import |
| 196 | // 4: a_import - file imported by dir_a |
| 197 | // 5: dir_b - file named 'b.rc' in dir |
| 198 | // 6: last_import - imported after dir is imported |
| 199 | |
| 200 | TemporaryFile first_import; |
| 201 | ASSERT_TRUE(first_import.fd != -1); |
| 202 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd)); |
| 203 | |
| 204 | TemporaryFile dir_a_import; |
| 205 | ASSERT_TRUE(dir_a_import.fd != -1); |
| 206 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd)); |
| 207 | |
| 208 | TemporaryFile last_import; |
| 209 | ASSERT_TRUE(last_import.fd != -1); |
| 210 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd)); |
| 211 | |
| 212 | TemporaryDir dir; |
| 213 | // clang-format off |
| 214 | std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n" |
| 215 | "on boot\n" |
| 216 | "execute 3"; |
| 217 | // clang-format on |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 218 | // WriteFile() ensures the right mode is set |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 219 | 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] | 220 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 221 | 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] | 222 | |
| 223 | // clang-format off |
| 224 | std::string start_script = "import " + std::string(first_import.path) + "\n" |
| 225 | "import " + std::string(dir.path) + "\n" |
| 226 | "import " + std::string(last_import.path) + "\n" |
| 227 | "on boot\n" |
| 228 | "execute 1"; |
| 229 | // clang-format on |
| 230 | TemporaryFile start; |
| 231 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); |
| 232 | |
| 233 | int num_executed = 0; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 234 | auto execute_command = [&num_executed](const BuiltinArguments& args) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 235 | EXPECT_EQ(2U, args.size()); |
| 236 | EXPECT_EQ(++num_executed, std::stoi(args[1])); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 237 | return Result<void>{}; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 240 | BuiltinFunctionMap test_function_map = { |
| 241 | {"execute", {1, 1, {false, execute_command}}}, |
| 242 | }; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 243 | |
| 244 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 245 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 246 | |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 247 | ActionManager action_manager; |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame] | 248 | ServiceList service_list; |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 249 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 250 | |
| 251 | EXPECT_EQ(6, num_executed); |
| 252 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 253 | |
Jooyung Han | badb7de | 2022-05-10 03:16:51 +0900 | [diff] [blame] | 254 | BuiltinFunctionMap GetTestFunctionMapForLazyLoad(int& num_executed, ActionManager& action_manager) { |
| 255 | auto execute_command = [&num_executed](const BuiltinArguments& args) { |
| 256 | EXPECT_EQ(2U, args.size()); |
| 257 | EXPECT_EQ(++num_executed, std::stoi(args[1])); |
| 258 | return Result<void>{}; |
| 259 | }; |
| 260 | auto load_command = [&action_manager](const BuiltinArguments& args) -> Result<void> { |
| 261 | EXPECT_EQ(2U, args.size()); |
| 262 | Parser parser; |
| 263 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, nullptr)); |
| 264 | if (!parser.ParseConfig(args[1])) { |
| 265 | return Error() << "Failed to load"; |
| 266 | } |
| 267 | return Result<void>{}; |
| 268 | }; |
| 269 | auto trigger_command = [&action_manager](const BuiltinArguments& args) { |
| 270 | EXPECT_EQ(2U, args.size()); |
| 271 | LOG(INFO) << "Queue event trigger: " << args[1]; |
| 272 | action_manager.QueueEventTrigger(args[1]); |
| 273 | return Result<void>{}; |
| 274 | }; |
| 275 | BuiltinFunctionMap test_function_map = { |
| 276 | {"execute", {1, 1, {false, execute_command}}}, |
| 277 | {"load", {1, 1, {false, load_command}}}, |
| 278 | {"trigger", {1, 1, {false, trigger_command}}}, |
| 279 | }; |
| 280 | return test_function_map; |
| 281 | } |
| 282 | |
| 283 | TEST(init, LazilyLoadedActionsCantBeTriggeredByTheSameTrigger) { |
| 284 | // "start" script loads "lazy" script. Even though "lazy" scripts |
| 285 | // defines "on boot" action, it's not executed by the current "boot" |
| 286 | // event because it's already processed. |
| 287 | TemporaryFile lazy; |
| 288 | ASSERT_TRUE(lazy.fd != -1); |
| 289 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", lazy.fd)); |
| 290 | |
| 291 | TemporaryFile start; |
| 292 | // clang-format off |
| 293 | std::string start_script = "on boot\n" |
| 294 | "load " + std::string(lazy.path) + "\n" |
| 295 | "execute 1"; |
| 296 | // clang-format on |
| 297 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); |
| 298 | |
| 299 | int num_executed = 0; |
| 300 | ActionManager action_manager; |
| 301 | ServiceList service_list; |
| 302 | BuiltinFunctionMap test_function_map = |
| 303 | GetTestFunctionMapForLazyLoad(num_executed, action_manager); |
| 304 | |
| 305 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 306 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 307 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); |
| 308 | |
| 309 | EXPECT_EQ(1, num_executed); |
| 310 | } |
| 311 | |
| 312 | TEST(init, LazilyLoadedActionsCanBeTriggeredByTheNextTrigger) { |
| 313 | // "start" script loads "lazy" script and then triggers "next" event |
| 314 | // which executes "on next" action loaded by the previous command. |
| 315 | TemporaryFile lazy; |
| 316 | ASSERT_TRUE(lazy.fd != -1); |
| 317 | ASSERT_TRUE(android::base::WriteStringToFd("on next\nexecute 2", lazy.fd)); |
| 318 | |
| 319 | TemporaryFile start; |
| 320 | // clang-format off |
| 321 | std::string start_script = "on boot\n" |
| 322 | "load " + std::string(lazy.path) + "\n" |
| 323 | "execute 1\n" |
| 324 | "trigger next"; |
| 325 | // clang-format on |
| 326 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); |
| 327 | |
| 328 | int num_executed = 0; |
| 329 | ActionManager action_manager; |
| 330 | ServiceList service_list; |
| 331 | BuiltinFunctionMap test_function_map = |
| 332 | GetTestFunctionMapForLazyLoad(num_executed, action_manager); |
| 333 | |
| 334 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 335 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 336 | TestInit(start.path, test_function_map, commands, &action_manager, &service_list); |
| 337 | |
| 338 | EXPECT_EQ(2, num_executed); |
| 339 | } |
| 340 | |
Jooyung Han | 678f0b4 | 2022-07-07 15:25:02 +0900 | [diff] [blame^] | 341 | TEST(init, RespondToCtlApexMessages) { |
| 342 | if (getuid() != 0) { |
| 343 | GTEST_SKIP() << "Skipping test, must be run as root."; |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | std::string apex_name = "com.android.apex.cts.shim"; |
| 348 | SetProperty("ctl.apex_unload", apex_name); |
| 349 | EXPECT_TRUE(WaitForProperty("init.apex." + apex_name, "unloaded", 10s)); |
| 350 | |
| 351 | SetProperty("ctl.apex_load", apex_name); |
| 352 | EXPECT_TRUE(WaitForProperty("init.apex." + apex_name, "loaded", 10s)); |
| 353 | } |
| 354 | |
Nikita Ioffe | 51c251c | 2020-04-30 19:40:39 +0100 | [diff] [blame] | 355 | TEST(init, RejectsCriticalAndOneshotService) { |
Tom Cherry | 0e40ba3 | 2020-07-09 08:47:24 -0700 | [diff] [blame] | 356 | if (GetIntProperty("ro.product.first_api_level", 10000) < 30) { |
| 357 | GTEST_SKIP() << "Test only valid for devices launching with R or later"; |
| 358 | } |
| 359 | |
Nikita Ioffe | 51c251c | 2020-04-30 19:40:39 +0100 | [diff] [blame] | 360 | std::string init_script = |
| 361 | R"init( |
| 362 | service A something |
| 363 | class first |
| 364 | critical |
| 365 | oneshot |
| 366 | )init"; |
| 367 | |
| 368 | TemporaryFile tf; |
| 369 | ASSERT_TRUE(tf.fd != -1); |
| 370 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); |
| 371 | |
| 372 | ServiceList service_list; |
| 373 | Parser parser; |
| 374 | parser.AddSectionParser("service", |
| 375 | std::make_unique<ServiceParser>(&service_list, nullptr, std::nullopt)); |
| 376 | |
| 377 | ASSERT_TRUE(parser.ParseConfig(tf.path)); |
| 378 | ASSERT_EQ(1u, parser.parse_error_count()); |
| 379 | } |
| 380 | |
Nikita Ioffe | 9e4b111 | 2020-12-11 17:59:38 +0000 | [diff] [blame] | 381 | class TestCaseLogger : public ::testing::EmptyTestEventListener { |
| 382 | void OnTestStart(const ::testing::TestInfo& test_info) override { |
| 383 | #ifdef __ANDROID__ |
| 384 | LOG(INFO) << "===== " << test_info.test_suite_name() << "::" << test_info.name() << " (" |
| 385 | << test_info.file() << ":" << test_info.line() << ")"; |
| 386 | #else |
| 387 | UNUSED(test_info); |
| 388 | #endif |
| 389 | } |
| 390 | }; |
| 391 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 392 | } // namespace init |
| 393 | } // namespace android |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 394 | |
| 395 | int SubcontextTestChildMain(int, char**); |
| 396 | int FirmwareTestChildMain(int, char**); |
| 397 | |
| 398 | int main(int argc, char** argv) { |
| 399 | if (argc > 1 && !strcmp(argv[1], "subcontext")) { |
| 400 | return SubcontextTestChildMain(argc, argv); |
| 401 | } |
| 402 | |
| 403 | if (argc > 1 && !strcmp(argv[1], "firmware")) { |
| 404 | return FirmwareTestChildMain(argc, argv); |
| 405 | } |
| 406 | |
| 407 | testing::InitGoogleTest(&argc, argv); |
Nikita Ioffe | 9e4b111 | 2020-12-11 17:59:38 +0000 | [diff] [blame] | 408 | testing::UnitTest::GetInstance()->listeners().Append(new android::init::TestCaseLogger()); |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 409 | return RUN_ALL_TESTS(); |
| 410 | } |