Daniel Zheng | 76aa257 | 2023-03-30 15:48:23 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2023 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 "task.h" |
| 18 | #include "fastboot.h" |
Daniel Zheng | 32dc6dd | 2023-04-10 11:16:33 -0700 | [diff] [blame] | 19 | #include "fastboot_driver_mock.h" |
Daniel Zheng | 76aa257 | 2023-03-30 15:48:23 -0700 | [diff] [blame] | 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <fstream> |
| 23 | #include <iostream> |
| 24 | #include <memory> |
| 25 | #include <unordered_map> |
| 26 | #include "android-base/strings.h" |
Daniel Zheng | eabfe27 | 2023-06-21 14:51:54 -0700 | [diff] [blame^] | 27 | |
Daniel Zheng | 76aa257 | 2023-03-30 15:48:23 -0700 | [diff] [blame] | 28 | using android::base::Split; |
Daniel Zheng | 32dc6dd | 2023-04-10 11:16:33 -0700 | [diff] [blame] | 29 | using testing::_; |
Daniel Zheng | 76aa257 | 2023-03-30 15:48:23 -0700 | [diff] [blame] | 30 | |
| 31 | class ParseTest : public ::testing ::Test { |
| 32 | protected: |
| 33 | void SetUp() override { |
| 34 | fp = std::make_unique<FlashingPlan>(); |
| 35 | fp->slot_override = "b"; |
| 36 | fp->secondary_slot = "a"; |
| 37 | fp->wants_wipe = false; |
| 38 | } |
| 39 | void TearDown() override {} |
| 40 | |
| 41 | std::unique_ptr<FlashingPlan> fp; |
| 42 | |
| 43 | private: |
| 44 | }; |
| 45 | |
| 46 | static std::vector<std::unique_ptr<Task>> collectTasks(FlashingPlan* fp, |
| 47 | const std::vector<std::string>& commands) { |
| 48 | std::vector<std::vector<std::string>> vec_commands; |
| 49 | for (auto& command : commands) { |
| 50 | vec_commands.emplace_back(android::base::Split(command, " ")); |
| 51 | } |
| 52 | std::vector<std::unique_ptr<Task>> tasks; |
| 53 | for (auto& command : vec_commands) { |
| 54 | tasks.emplace_back(ParseFastbootInfoLine(fp, command)); |
| 55 | } |
| 56 | return tasks; |
| 57 | } |
| 58 | |
Daniel Zheng | 935ee1f | 2023-04-13 11:03:34 -0700 | [diff] [blame] | 59 | std::unique_ptr<Task> ParseCommand(FlashingPlan* fp, std::string command) { |
| 60 | std::vector<std::string> vec_command = android::base::Split(command, " "); |
| 61 | return ParseFastbootInfoLine(fp, vec_command); |
| 62 | } |
| 63 | |
Daniel Zheng | eabfe27 | 2023-06-21 14:51:54 -0700 | [diff] [blame^] | 64 | // tests if tasks_a is a superset of tasks_b. Used for checking to ensure all partitions flashed |
| 65 | // from hardcoded image list is also flashed in new fastboot-info.txt |
| 66 | static bool compareTaskList(std::vector<std::unique_ptr<Task>>& tasks_a, |
| 67 | std::vector<std::unique_ptr<Task>>& tasks_b) { |
| 68 | std::set<std::string> list; |
| 69 | for (auto& task : tasks_a) { |
| 70 | list.insert(task->ToString()); |
| 71 | } |
| 72 | for (auto& task : tasks_b) { |
| 73 | if (list.find(task->ToString()) == list.end()) { |
| 74 | std::cout << "ERROR: " << task->ToString() |
| 75 | << " not found in task list created by fastboot-info.txt"; |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | static std::string tasksToString(std::vector<std::unique_ptr<Task>>& tasks) { |
| 83 | std::string output; |
| 84 | for (auto& task : tasks) { |
| 85 | output.append(task->ToString()); |
| 86 | output.append("\n"); |
| 87 | } |
| 88 | return output; |
| 89 | } |
| 90 | |
Daniel Zheng | 207c0a3 | 2023-04-20 14:13:15 -0700 | [diff] [blame] | 91 | TEST_F(ParseTest, CorrectFlashTaskFormed) { |
Daniel Zheng | 76aa257 | 2023-03-30 15:48:23 -0700 | [diff] [blame] | 92 | std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img", |
| 93 | "flash system", "flash --apply-vbmeta vbmeta"}; |
| 94 | |
| 95 | std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands); |
| 96 | |
| 97 | std::vector<std::vector<std::string>> expected_values{ |
| 98 | {"dtbo", "dtbo_b", "b", "dtbo.img"}, |
| 99 | {"system", "system_a", "a", "system_other.img"}, |
| 100 | {"system", "system_b", "b", "system.img"}, |
| 101 | {"vbmeta", "vbmeta_b", "b", "vbmeta.img"} |
| 102 | |
| 103 | }; |
| 104 | |
| 105 | for (auto& task : tasks) { |
| 106 | ASSERT_TRUE(task != nullptr); |
| 107 | } |
| 108 | |
| 109 | for (size_t i = 0; i < tasks.size(); i++) { |
| 110 | auto task = tasks[i]->AsFlashTask(); |
| 111 | ASSERT_TRUE(task != nullptr); |
| 112 | ASSERT_EQ(task->GetPartition(), expected_values[i][0]); |
| 113 | ASSERT_EQ(task->GetPartitionAndSlot(), expected_values[i][1]); |
| 114 | ASSERT_EQ(task->GetSlot(), expected_values[i][2]); |
| 115 | ASSERT_EQ(task->GetImageName(), expected_values[i][3]); |
| 116 | } |
| 117 | } |
Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 118 | |
Daniel Zheng | 207c0a3 | 2023-04-20 14:13:15 -0700 | [diff] [blame] | 119 | TEST_F(ParseTest, VersionCheckCorrect) { |
Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 120 | std::vector<std::string> correct_versions = {"version 1", "version 22", "version 5", |
| 121 | "version 17"}; |
Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 122 | |
Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 123 | std::vector<std::string> bad_versions = {"version", "version .01", "version x1", |
| 124 | "version 1.0.1", "version 1.", "s 1.0", |
| 125 | "version 1.0 2.0", "version 100.00", "version 1 2"}; |
Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 126 | |
| 127 | for (auto& version : correct_versions) { |
Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 128 | ASSERT_TRUE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 26)) |
| 129 | << version; |
Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 130 | } |
Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 131 | |
| 132 | // returning False for failing version check |
| 133 | for (auto& version : correct_versions) { |
| 134 | ASSERT_FALSE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 0)) |
| 135 | << version; |
| 136 | } |
| 137 | // returning False for bad format |
Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 138 | for (auto& version : bad_versions) { |
Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 139 | ASSERT_FALSE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 100)) |
| 140 | << version; |
Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 141 | } |
Daniel Zheng | 935ee1f | 2023-04-13 11:03:34 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Zheng | 207c0a3 | 2023-04-20 14:13:15 -0700 | [diff] [blame] | 144 | TEST_F(ParseTest, BadFastbootInput) { |
Daniel Zheng | 935ee1f | 2023-04-13 11:03:34 -0700 | [diff] [blame] | 145 | ASSERT_EQ(ParseCommand(fp.get(), "flash"), nullptr); |
| 146 | ASSERT_EQ(ParseCommand(fp.get(), "flash --slot-other --apply-vbmeta"), nullptr); |
| 147 | ASSERT_EQ(ParseCommand(fp.get(), "flash --apply-vbmeta"), nullptr); |
| 148 | ASSERT_EQ(ParseCommand(fp.get(), "if-wipe"), nullptr); |
| 149 | ASSERT_EQ(ParseCommand(fp.get(), "if-wipe flash"), nullptr); |
| 150 | ASSERT_EQ(ParseCommand(fp.get(), "wipe dtbo"), nullptr); |
| 151 | ASSERT_EQ(ParseCommand(fp.get(), "update-super dtbo"), nullptr); |
| 152 | ASSERT_EQ(ParseCommand(fp.get(), "flash system system.img system"), nullptr); |
| 153 | ASSERT_EQ(ParseCommand(fp.get(), "reboot bootloader fastboot"), nullptr); |
| 154 | ASSERT_EQ(ParseCommand(fp.get(), |
| 155 | "flash --slot-other --apply-vbmeta system system_other.img system"), |
| 156 | nullptr); |
| 157 | ASSERT_EQ(ParseCommand(fp.get(), "erase"), nullptr); |
| 158 | ASSERT_EQ(ParseCommand(fp.get(), "erase dtbo dtbo"), nullptr); |
| 159 | ASSERT_EQ(ParseCommand(fp.get(), "wipe this"), nullptr); |
| 160 | } |
Daniel Zheng | 32dc6dd | 2023-04-10 11:16:33 -0700 | [diff] [blame] | 161 | |
Daniel Zheng | 207c0a3 | 2023-04-20 14:13:15 -0700 | [diff] [blame] | 162 | TEST_F(ParseTest, CorrectTaskFormed) { |
Daniel Zheng | 32dc6dd | 2023-04-10 11:16:33 -0700 | [diff] [blame] | 163 | std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img", |
| 164 | "reboot bootloader", "update-super", "erase cache"}; |
| 165 | std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands); |
| 166 | |
| 167 | ASSERT_TRUE(tasks[0]->AsFlashTask()); |
| 168 | ASSERT_TRUE(tasks[0]->AsFlashTask()); |
| 169 | ASSERT_TRUE(tasks[1]->AsFlashTask()); |
| 170 | ASSERT_TRUE(tasks[2]->AsRebootTask()); |
| 171 | ASSERT_TRUE(tasks[3]->AsUpdateSuperTask()); |
| 172 | ASSERT_TRUE(tasks[4]->AsWipeTask()); |
| 173 | } |
Daniel Zheng | 0d2d631 | 2023-04-11 09:09:35 -0700 | [diff] [blame] | 174 | |
| 175 | TEST_F(ParseTest, CorrectDriverCalls) { |
| 176 | fastboot::MockFastbootDriver fb; |
| 177 | fp->fb = &fb; |
| 178 | |
| 179 | EXPECT_CALL(fb, RebootTo(_, _, _)).Times(1); |
| 180 | EXPECT_CALL(fb, Reboot(_, _)).Times(1); |
| 181 | EXPECT_CALL(fb, WaitForDisconnect()).Times(2); |
| 182 | |
| 183 | std::vector<std::string> commands = {"reboot bootloader", "reboot"}; |
| 184 | std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands); |
| 185 | |
| 186 | for (auto& task : tasks) { |
| 187 | task->Run(); |
| 188 | } |
| 189 | } |
Daniel Zheng | eabfe27 | 2023-06-21 14:51:54 -0700 | [diff] [blame^] | 190 | |
| 191 | TEST_F(ParseTest, CorrectTaskLists) { |
| 192 | if (!get_android_product_out()) { |
| 193 | GTEST_SKIP(); |
| 194 | } |
| 195 | |
| 196 | LocalImageSource s; |
| 197 | fp->source = &s; |
| 198 | fp->sparse_limit = std::numeric_limits<int64_t>::max(); |
| 199 | |
| 200 | fastboot::MockFastbootDriver fb; |
| 201 | fp->fb = &fb; |
| 202 | fp->should_optimize_flash_super = false; |
| 203 | |
| 204 | ON_CALL(fb, GetVar("super-partition-name", _, _)) |
| 205 | .WillByDefault(testing::Return(fastboot::BAD_ARG)); |
| 206 | |
| 207 | FlashAllTool tool(fp.get()); |
| 208 | |
| 209 | fp->should_use_fastboot_info = false; |
| 210 | auto hardcoded_tasks = tool.CollectTasks(); |
| 211 | fp->should_use_fastboot_info = true; |
| 212 | auto fastboot_info_tasks = tool.CollectTasks(); |
| 213 | |
| 214 | auto is_non_flash_task = [](const auto& task) -> bool { |
| 215 | return task->AsFlashTask() == nullptr; |
| 216 | }; |
| 217 | |
| 218 | // remove non flash tasks for testing purposes |
| 219 | hardcoded_tasks.erase( |
| 220 | std::remove_if(hardcoded_tasks.begin(), hardcoded_tasks.end(), is_non_flash_task), |
| 221 | hardcoded_tasks.end()); |
| 222 | fastboot_info_tasks.erase(std::remove_if(fastboot_info_tasks.begin(), fastboot_info_tasks.end(), |
| 223 | is_non_flash_task), |
| 224 | fastboot_info_tasks.end()); |
| 225 | |
| 226 | if (!compareTaskList(fastboot_info_tasks, hardcoded_tasks)) { |
| 227 | std::cout << "\n\n---Hardcoded Task List---\n" |
| 228 | << tasksToString(hardcoded_tasks) << "\n---Fastboot-Info Task List---\n" |
| 229 | << tasksToString(fastboot_info_tasks); |
| 230 | } |
| 231 | |
| 232 | ASSERT_TRUE(compareTaskList(fastboot_info_tasks, hardcoded_tasks)); |
| 233 | |
| 234 | ASSERT_TRUE(fastboot_info_tasks.size() >= hardcoded_tasks.size()) |
| 235 | << "size of fastboot-info task list: " << fastboot_info_tasks.size() |
| 236 | << " size of hardcoded task list: " << hardcoded_tasks.size(); |
| 237 | } |