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> |
| 20 | #include <android-base/test_utils.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #include "action.h" |
| 24 | #include "builtins.h" |
| 25 | #include "import_parser.h" |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 26 | #include "keyword_map.h" |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 27 | #include "parser.h" |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame^] | 28 | #include "service.h" |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 29 | #include "test_function_map.h" |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 30 | #include "util.h" |
| 31 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace init { |
| 34 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 35 | using ActionManagerCommand = std::function<void(ActionManager&)>; |
| 36 | |
| 37 | void TestInit(const std::string& init_script_file, const TestFunctionMap& test_function_map, |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame^] | 38 | const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 39 | ActionManager am; |
| 40 | |
| 41 | Action::set_function_map(&test_function_map); |
| 42 | |
| 43 | Parser parser; |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame^] | 44 | parser.AddSectionParser("service", std::make_unique<ServiceParser>(service_list, nullptr)); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 45 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr)); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 46 | parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser)); |
| 47 | |
| 48 | ASSERT_TRUE(parser.ParseConfig(init_script_file)); |
| 49 | |
| 50 | for (const auto& command : commands) { |
| 51 | command(am); |
| 52 | } |
| 53 | |
| 54 | while (am.HasMoreCommands()) { |
| 55 | am.ExecuteOneCommand(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map, |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame^] | 60 | const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 61 | TemporaryFile tf; |
| 62 | ASSERT_TRUE(tf.fd != -1); |
| 63 | ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd)); |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame^] | 64 | TestInit(tf.path, test_function_map, commands, service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST(init, SimpleEventTrigger) { |
| 68 | bool expect_true = false; |
| 69 | std::string init_script = |
| 70 | R"init( |
| 71 | on boot |
| 72 | pass_test |
| 73 | )init"; |
| 74 | |
| 75 | TestFunctionMap test_function_map; |
| 76 | test_function_map.Add("pass_test", [&expect_true]() { expect_true = true; }); |
| 77 | |
| 78 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 79 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 80 | |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame^] | 81 | ServiceList service_list; |
| 82 | TestInitText(init_script, test_function_map, commands, &service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 83 | |
| 84 | EXPECT_TRUE(expect_true); |
| 85 | } |
| 86 | |
| 87 | TEST(init, EventTriggerOrder) { |
| 88 | std::string init_script = |
| 89 | R"init( |
| 90 | on boot |
| 91 | execute_first |
| 92 | |
| 93 | on boot && property:ro.hardware=* |
| 94 | execute_second |
| 95 | |
| 96 | on boot |
| 97 | execute_third |
| 98 | |
| 99 | )init"; |
| 100 | |
| 101 | int num_executed = 0; |
| 102 | TestFunctionMap test_function_map; |
| 103 | test_function_map.Add("execute_first", [&num_executed]() { EXPECT_EQ(0, num_executed++); }); |
| 104 | test_function_map.Add("execute_second", [&num_executed]() { EXPECT_EQ(1, num_executed++); }); |
| 105 | test_function_map.Add("execute_third", [&num_executed]() { EXPECT_EQ(2, num_executed++); }); |
| 106 | |
| 107 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 108 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 109 | |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame^] | 110 | ServiceList service_list; |
| 111 | TestInitText(init_script, test_function_map, commands, &service_list); |
| 112 | } |
| 113 | |
| 114 | TEST(init, OverrideService) { |
| 115 | std::string init_script = R"init( |
| 116 | service A something |
| 117 | class first |
| 118 | |
| 119 | service A something |
| 120 | class second |
| 121 | override |
| 122 | |
| 123 | )init"; |
| 124 | |
| 125 | ServiceList service_list; |
| 126 | TestInitText(init_script, TestFunctionMap(), {}, &service_list); |
| 127 | ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end())); |
| 128 | |
| 129 | auto service = service_list.begin()->get(); |
| 130 | ASSERT_NE(nullptr, service); |
| 131 | EXPECT_EQ(std::set<std::string>({"second"}), service->classnames()); |
| 132 | EXPECT_EQ("A", service->name()); |
| 133 | EXPECT_TRUE(service->is_override()); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST(init, EventTriggerOrderMultipleFiles) { |
| 137 | // 6 total files, which should have their triggers executed in the following order: |
| 138 | // 1: start - original script parsed |
| 139 | // 2: first_import - immediately imported by first_script |
| 140 | // 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import |
| 141 | // 4: a_import - file imported by dir_a |
| 142 | // 5: dir_b - file named 'b.rc' in dir |
| 143 | // 6: last_import - imported after dir is imported |
| 144 | |
| 145 | TemporaryFile first_import; |
| 146 | ASSERT_TRUE(first_import.fd != -1); |
| 147 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd)); |
| 148 | |
| 149 | TemporaryFile dir_a_import; |
| 150 | ASSERT_TRUE(dir_a_import.fd != -1); |
| 151 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd)); |
| 152 | |
| 153 | TemporaryFile last_import; |
| 154 | ASSERT_TRUE(last_import.fd != -1); |
| 155 | ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd)); |
| 156 | |
| 157 | TemporaryDir dir; |
| 158 | // clang-format off |
| 159 | std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n" |
| 160 | "on boot\n" |
| 161 | "execute 3"; |
| 162 | // clang-format on |
Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 163 | // WriteFile() ensures the right mode is set |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 164 | ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script)); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 165 | |
Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 166 | ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5")); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 167 | |
| 168 | // clang-format off |
| 169 | std::string start_script = "import " + std::string(first_import.path) + "\n" |
| 170 | "import " + std::string(dir.path) + "\n" |
| 171 | "import " + std::string(last_import.path) + "\n" |
| 172 | "on boot\n" |
| 173 | "execute 1"; |
| 174 | // clang-format on |
| 175 | TemporaryFile start; |
| 176 | ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd)); |
| 177 | |
| 178 | int num_executed = 0; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 179 | auto execute_command = [&num_executed](const BuiltinArguments& args) { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 180 | EXPECT_EQ(2U, args.size()); |
| 181 | EXPECT_EQ(++num_executed, std::stoi(args[1])); |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 182 | return Success(); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | TestFunctionMap test_function_map; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 186 | test_function_map.Add("execute", 1, 1, false, execute_command); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 187 | |
| 188 | ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); }; |
| 189 | std::vector<ActionManagerCommand> commands{trigger_boot}; |
| 190 | |
Steven Moreland | 6f5333a | 2017-11-13 15:31:54 -0800 | [diff] [blame^] | 191 | ServiceList service_list; |
| 192 | |
| 193 | TestInit(start.path, test_function_map, commands, &service_list); |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 194 | |
| 195 | EXPECT_EQ(6, num_executed); |
| 196 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 197 | |
| 198 | } // namespace init |
| 199 | } // namespace android |