| 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" | 
|  | 27 | using android::base::Split; | 
| Daniel Zheng | 32dc6dd | 2023-04-10 11:16:33 -0700 | [diff] [blame] | 28 | using testing::_; | 
| Daniel Zheng | 76aa257 | 2023-03-30 15:48:23 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | class ParseTest : public ::testing ::Test { | 
|  | 31 | protected: | 
|  | 32 | void SetUp() override { | 
|  | 33 | fp = std::make_unique<FlashingPlan>(); | 
|  | 34 | fp->slot_override = "b"; | 
|  | 35 | fp->secondary_slot = "a"; | 
|  | 36 | fp->wants_wipe = false; | 
|  | 37 | } | 
|  | 38 | void TearDown() override {} | 
|  | 39 |  | 
|  | 40 | std::unique_ptr<FlashingPlan> fp; | 
|  | 41 |  | 
|  | 42 | private: | 
|  | 43 | }; | 
|  | 44 |  | 
|  | 45 | static std::vector<std::unique_ptr<Task>> collectTasks(FlashingPlan* fp, | 
|  | 46 | const std::vector<std::string>& commands) { | 
|  | 47 | std::vector<std::vector<std::string>> vec_commands; | 
|  | 48 | for (auto& command : commands) { | 
|  | 49 | vec_commands.emplace_back(android::base::Split(command, " ")); | 
|  | 50 | } | 
|  | 51 | std::vector<std::unique_ptr<Task>> tasks; | 
|  | 52 | for (auto& command : vec_commands) { | 
|  | 53 | tasks.emplace_back(ParseFastbootInfoLine(fp, command)); | 
|  | 54 | } | 
|  | 55 | return tasks; | 
|  | 56 | } | 
|  | 57 |  | 
| Daniel Zheng | 935ee1f | 2023-04-13 11:03:34 -0700 | [diff] [blame] | 58 | std::unique_ptr<Task> ParseCommand(FlashingPlan* fp, std::string command) { | 
|  | 59 | std::vector<std::string> vec_command = android::base::Split(command, " "); | 
|  | 60 | return ParseFastbootInfoLine(fp, vec_command); | 
|  | 61 | } | 
|  | 62 |  | 
| Daniel Zheng | 207c0a3 | 2023-04-20 14:13:15 -0700 | [diff] [blame] | 63 | TEST_F(ParseTest, CorrectFlashTaskFormed) { | 
| Daniel Zheng | 76aa257 | 2023-03-30 15:48:23 -0700 | [diff] [blame] | 64 | std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img", | 
|  | 65 | "flash system", "flash --apply-vbmeta vbmeta"}; | 
|  | 66 |  | 
|  | 67 | std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands); | 
|  | 68 |  | 
|  | 69 | std::vector<std::vector<std::string>> expected_values{ | 
|  | 70 | {"dtbo", "dtbo_b", "b", "dtbo.img"}, | 
|  | 71 | {"system", "system_a", "a", "system_other.img"}, | 
|  | 72 | {"system", "system_b", "b", "system.img"}, | 
|  | 73 | {"vbmeta", "vbmeta_b", "b", "vbmeta.img"} | 
|  | 74 |  | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 | for (auto& task : tasks) { | 
|  | 78 | ASSERT_TRUE(task != nullptr); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | for (size_t i = 0; i < tasks.size(); i++) { | 
|  | 82 | auto task = tasks[i]->AsFlashTask(); | 
|  | 83 | ASSERT_TRUE(task != nullptr); | 
|  | 84 | ASSERT_EQ(task->GetPartition(), expected_values[i][0]); | 
|  | 85 | ASSERT_EQ(task->GetPartitionAndSlot(), expected_values[i][1]); | 
|  | 86 | ASSERT_EQ(task->GetSlot(), expected_values[i][2]); | 
|  | 87 | ASSERT_EQ(task->GetImageName(), expected_values[i][3]); | 
|  | 88 | } | 
|  | 89 | } | 
| Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 90 |  | 
| Daniel Zheng | 207c0a3 | 2023-04-20 14:13:15 -0700 | [diff] [blame] | 91 | TEST_F(ParseTest, VersionCheckCorrect) { | 
| Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 92 | std::vector<std::string> correct_versions = {"version 1", "version 22", "version 5", | 
|  | 93 | "version 17"}; | 
| Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 94 |  | 
| Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 95 | std::vector<std::string> bad_versions = {"version",         "version .01",    "version x1", | 
|  | 96 | "version 1.0.1",   "version 1.",     "s 1.0", | 
|  | 97 | "version 1.0 2.0", "version 100.00", "version 1 2"}; | 
| Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 98 |  | 
|  | 99 | for (auto& version : correct_versions) { | 
| Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 100 | ASSERT_TRUE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 26)) | 
|  | 101 | << version; | 
| Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 102 | } | 
| Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | // returning False for failing version check | 
|  | 105 | for (auto& version : correct_versions) { | 
|  | 106 | ASSERT_FALSE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 0)) | 
|  | 107 | << version; | 
|  | 108 | } | 
|  | 109 | // returning False for bad format | 
| Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 110 | for (auto& version : bad_versions) { | 
| Daniel Zheng | 630f29e | 2023-04-28 14:06:45 -0700 | [diff] [blame] | 111 | ASSERT_FALSE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 100)) | 
|  | 112 | << version; | 
| Daniel Zheng | 68b84df | 2023-04-13 11:39:26 -0700 | [diff] [blame] | 113 | } | 
| Daniel Zheng | 935ee1f | 2023-04-13 11:03:34 -0700 | [diff] [blame] | 114 | } | 
|  | 115 |  | 
| Daniel Zheng | 207c0a3 | 2023-04-20 14:13:15 -0700 | [diff] [blame] | 116 | TEST_F(ParseTest, BadFastbootInput) { | 
| Daniel Zheng | 935ee1f | 2023-04-13 11:03:34 -0700 | [diff] [blame] | 117 | ASSERT_EQ(ParseCommand(fp.get(), "flash"), nullptr); | 
|  | 118 | ASSERT_EQ(ParseCommand(fp.get(), "flash --slot-other --apply-vbmeta"), nullptr); | 
|  | 119 | ASSERT_EQ(ParseCommand(fp.get(), "flash --apply-vbmeta"), nullptr); | 
|  | 120 | ASSERT_EQ(ParseCommand(fp.get(), "if-wipe"), nullptr); | 
|  | 121 | ASSERT_EQ(ParseCommand(fp.get(), "if-wipe flash"), nullptr); | 
|  | 122 | ASSERT_EQ(ParseCommand(fp.get(), "wipe dtbo"), nullptr); | 
|  | 123 | ASSERT_EQ(ParseCommand(fp.get(), "update-super dtbo"), nullptr); | 
|  | 124 | ASSERT_EQ(ParseCommand(fp.get(), "flash system system.img system"), nullptr); | 
|  | 125 | ASSERT_EQ(ParseCommand(fp.get(), "reboot bootloader fastboot"), nullptr); | 
|  | 126 | ASSERT_EQ(ParseCommand(fp.get(), | 
|  | 127 | "flash --slot-other --apply-vbmeta system system_other.img system"), | 
|  | 128 | nullptr); | 
|  | 129 | ASSERT_EQ(ParseCommand(fp.get(), "erase"), nullptr); | 
|  | 130 | ASSERT_EQ(ParseCommand(fp.get(), "erase dtbo dtbo"), nullptr); | 
|  | 131 | ASSERT_EQ(ParseCommand(fp.get(), "wipe this"), nullptr); | 
|  | 132 | } | 
| Daniel Zheng | 32dc6dd | 2023-04-10 11:16:33 -0700 | [diff] [blame] | 133 |  | 
| Daniel Zheng | 207c0a3 | 2023-04-20 14:13:15 -0700 | [diff] [blame] | 134 | TEST_F(ParseTest, CorrectTaskFormed) { | 
| Daniel Zheng | 32dc6dd | 2023-04-10 11:16:33 -0700 | [diff] [blame] | 135 | std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img", | 
|  | 136 | "reboot bootloader", "update-super", "erase cache"}; | 
|  | 137 | std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands); | 
|  | 138 |  | 
|  | 139 | ASSERT_TRUE(tasks[0]->AsFlashTask()); | 
|  | 140 | ASSERT_TRUE(tasks[0]->AsFlashTask()); | 
|  | 141 | ASSERT_TRUE(tasks[1]->AsFlashTask()); | 
|  | 142 | ASSERT_TRUE(tasks[2]->AsRebootTask()); | 
|  | 143 | ASSERT_TRUE(tasks[3]->AsUpdateSuperTask()); | 
|  | 144 | ASSERT_TRUE(tasks[4]->AsWipeTask()); | 
|  | 145 | } | 
| Daniel Zheng | 0d2d631 | 2023-04-11 09:09:35 -0700 | [diff] [blame] | 146 |  | 
|  | 147 | TEST_F(ParseTest, CorrectDriverCalls) { | 
|  | 148 | fastboot::MockFastbootDriver fb; | 
|  | 149 | fp->fb = &fb; | 
|  | 150 |  | 
|  | 151 | EXPECT_CALL(fb, RebootTo(_, _, _)).Times(1); | 
|  | 152 | EXPECT_CALL(fb, Reboot(_, _)).Times(1); | 
|  | 153 | EXPECT_CALL(fb, WaitForDisconnect()).Times(2); | 
|  | 154 |  | 
|  | 155 | std::vector<std::string> commands = {"reboot bootloader", "reboot"}; | 
|  | 156 | std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands); | 
|  | 157 |  | 
|  | 158 | for (auto& task : tasks) { | 
|  | 159 | task->Run(); | 
|  | 160 | } | 
|  | 161 | } |