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