blob: 42be3cb653ec484354f36d5da8ddf65ab6d04ae4 [file] [log] [blame]
Daniel Zheng76aa2572023-03-30 15:48:23 -07001//
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 Zheng32dc6dd2023-04-10 11:16:33 -070019#include "fastboot_driver_mock.h"
Daniel Zheng76aa2572023-03-30 15:48:23 -070020
21#include <gtest/gtest.h>
Daniel Zheng76aa2572023-03-30 15:48:23 -070022#include <iostream>
23#include <memory>
Daniel Zheng76aa2572023-03-30 15:48:23 -070024#include "android-base/strings.h"
Daniel Zheng1fff6902023-08-24 09:47:43 -070025#include "gmock/gmock.h"
Daniel Zhengeabfe272023-06-21 14:51:54 -070026
Daniel Zheng76aa2572023-03-30 15:48:23 -070027using android::base::Split;
Daniel Zheng32dc6dd2023-04-10 11:16:33 -070028using testing::_;
Daniel Zheng76aa2572023-03-30 15:48:23 -070029
30class 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
45static 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 Zheng935ee1f2023-04-13 11:03:34 -070058std::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 Zhengeabfe272023-06-21 14:51:54 -070063// tests if tasks_a is a superset of tasks_b. Used for checking to ensure all partitions flashed
64// from hardcoded image list is also flashed in new fastboot-info.txt
65static bool compareTaskList(std::vector<std::unique_ptr<Task>>& tasks_a,
66 std::vector<std::unique_ptr<Task>>& tasks_b) {
67 std::set<std::string> list;
68 for (auto& task : tasks_a) {
69 list.insert(task->ToString());
70 }
71 for (auto& task : tasks_b) {
72 if (list.find(task->ToString()) == list.end()) {
73 std::cout << "ERROR: " << task->ToString()
74 << " not found in task list created by fastboot-info.txt";
75 return false;
76 }
77 }
78 return true;
79}
80
81static std::string tasksToString(std::vector<std::unique_ptr<Task>>& tasks) {
82 std::string output;
83 for (auto& task : tasks) {
84 output.append(task->ToString());
85 output.append("\n");
86 }
87 return output;
88}
89
Daniel Zheng207c0a32023-04-20 14:13:15 -070090TEST_F(ParseTest, CorrectFlashTaskFormed) {
Daniel Zheng76aa2572023-03-30 15:48:23 -070091 std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img",
92 "flash system", "flash --apply-vbmeta vbmeta"};
93
94 std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands);
95
96 std::vector<std::vector<std::string>> expected_values{
97 {"dtbo", "dtbo_b", "b", "dtbo.img"},
98 {"system", "system_a", "a", "system_other.img"},
99 {"system", "system_b", "b", "system.img"},
100 {"vbmeta", "vbmeta_b", "b", "vbmeta.img"}
101
102 };
103
104 for (auto& task : tasks) {
105 ASSERT_TRUE(task != nullptr);
106 }
107
108 for (size_t i = 0; i < tasks.size(); i++) {
109 auto task = tasks[i]->AsFlashTask();
110 ASSERT_TRUE(task != nullptr);
111 ASSERT_EQ(task->GetPartition(), expected_values[i][0]);
112 ASSERT_EQ(task->GetPartitionAndSlot(), expected_values[i][1]);
113 ASSERT_EQ(task->GetSlot(), expected_values[i][2]);
114 ASSERT_EQ(task->GetImageName(), expected_values[i][3]);
115 }
116}
Daniel Zheng68b84df2023-04-13 11:39:26 -0700117
Daniel Zheng207c0a32023-04-20 14:13:15 -0700118TEST_F(ParseTest, VersionCheckCorrect) {
Daniel Zheng630f29e2023-04-28 14:06:45 -0700119 std::vector<std::string> correct_versions = {"version 1", "version 22", "version 5",
120 "version 17"};
Daniel Zheng68b84df2023-04-13 11:39:26 -0700121
Daniel Zheng630f29e2023-04-28 14:06:45 -0700122 std::vector<std::string> bad_versions = {"version", "version .01", "version x1",
123 "version 1.0.1", "version 1.", "s 1.0",
124 "version 1.0 2.0", "version 100.00", "version 1 2"};
Daniel Zheng68b84df2023-04-13 11:39:26 -0700125
126 for (auto& version : correct_versions) {
Daniel Zheng630f29e2023-04-28 14:06:45 -0700127 ASSERT_TRUE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 26))
128 << version;
Daniel Zheng68b84df2023-04-13 11:39:26 -0700129 }
Daniel Zheng630f29e2023-04-28 14:06:45 -0700130
131 // returning False for failing version check
132 for (auto& version : correct_versions) {
133 ASSERT_FALSE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 0))
134 << version;
135 }
136 // returning False for bad format
Daniel Zheng68b84df2023-04-13 11:39:26 -0700137 for (auto& version : bad_versions) {
Daniel Zheng630f29e2023-04-28 14:06:45 -0700138 ASSERT_FALSE(CheckFastbootInfoRequirements(android::base::Split(version, " "), 100))
139 << version;
Daniel Zheng68b84df2023-04-13 11:39:26 -0700140 }
Daniel Zheng935ee1f2023-04-13 11:03:34 -0700141}
142
Daniel Zheng207c0a32023-04-20 14:13:15 -0700143TEST_F(ParseTest, BadFastbootInput) {
Daniel Zheng935ee1f2023-04-13 11:03:34 -0700144 ASSERT_EQ(ParseCommand(fp.get(), "flash"), nullptr);
145 ASSERT_EQ(ParseCommand(fp.get(), "flash --slot-other --apply-vbmeta"), nullptr);
146 ASSERT_EQ(ParseCommand(fp.get(), "flash --apply-vbmeta"), nullptr);
147 ASSERT_EQ(ParseCommand(fp.get(), "if-wipe"), nullptr);
148 ASSERT_EQ(ParseCommand(fp.get(), "if-wipe flash"), nullptr);
149 ASSERT_EQ(ParseCommand(fp.get(), "wipe dtbo"), nullptr);
150 ASSERT_EQ(ParseCommand(fp.get(), "update-super dtbo"), nullptr);
151 ASSERT_EQ(ParseCommand(fp.get(), "flash system system.img system"), nullptr);
152 ASSERT_EQ(ParseCommand(fp.get(), "reboot bootloader fastboot"), nullptr);
153 ASSERT_EQ(ParseCommand(fp.get(),
154 "flash --slot-other --apply-vbmeta system system_other.img system"),
155 nullptr);
156 ASSERT_EQ(ParseCommand(fp.get(), "erase"), nullptr);
157 ASSERT_EQ(ParseCommand(fp.get(), "erase dtbo dtbo"), nullptr);
158 ASSERT_EQ(ParseCommand(fp.get(), "wipe this"), nullptr);
159}
Daniel Zheng32dc6dd2023-04-10 11:16:33 -0700160
Daniel Zheng207c0a32023-04-20 14:13:15 -0700161TEST_F(ParseTest, CorrectTaskFormed) {
Daniel Zheng32dc6dd2023-04-10 11:16:33 -0700162 std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img",
163 "reboot bootloader", "update-super", "erase cache"};
164 std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands);
165
166 ASSERT_TRUE(tasks[0]->AsFlashTask());
167 ASSERT_TRUE(tasks[0]->AsFlashTask());
168 ASSERT_TRUE(tasks[1]->AsFlashTask());
169 ASSERT_TRUE(tasks[2]->AsRebootTask());
170 ASSERT_TRUE(tasks[3]->AsUpdateSuperTask());
171 ASSERT_TRUE(tasks[4]->AsWipeTask());
172}
Daniel Zheng0d2d6312023-04-11 09:09:35 -0700173
174TEST_F(ParseTest, CorrectDriverCalls) {
175 fastboot::MockFastbootDriver fb;
176 fp->fb = &fb;
177
178 EXPECT_CALL(fb, RebootTo(_, _, _)).Times(1);
179 EXPECT_CALL(fb, Reboot(_, _)).Times(1);
180 EXPECT_CALL(fb, WaitForDisconnect()).Times(2);
181
182 std::vector<std::string> commands = {"reboot bootloader", "reboot"};
183 std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands);
184
185 for (auto& task : tasks) {
186 task->Run();
187 }
188}
Daniel Zhengeabfe272023-06-21 14:51:54 -0700189
190TEST_F(ParseTest, CorrectTaskLists) {
191 if (!get_android_product_out()) {
192 GTEST_SKIP();
193 }
194
195 LocalImageSource s;
196 fp->source = &s;
197 fp->sparse_limit = std::numeric_limits<int64_t>::max();
198
199 fastboot::MockFastbootDriver fb;
200 fp->fb = &fb;
201 fp->should_optimize_flash_super = false;
202
203 ON_CALL(fb, GetVar("super-partition-name", _, _))
204 .WillByDefault(testing::Return(fastboot::BAD_ARG));
205
206 FlashAllTool tool(fp.get());
207
208 fp->should_use_fastboot_info = false;
209 auto hardcoded_tasks = tool.CollectTasks();
210 fp->should_use_fastboot_info = true;
211 auto fastboot_info_tasks = tool.CollectTasks();
212
213 auto is_non_flash_task = [](const auto& task) -> bool {
214 return task->AsFlashTask() == nullptr;
215 };
216
217 // remove non flash tasks for testing purposes
218 hardcoded_tasks.erase(
219 std::remove_if(hardcoded_tasks.begin(), hardcoded_tasks.end(), is_non_flash_task),
220 hardcoded_tasks.end());
221 fastboot_info_tasks.erase(std::remove_if(fastboot_info_tasks.begin(), fastboot_info_tasks.end(),
222 is_non_flash_task),
223 fastboot_info_tasks.end());
224
225 if (!compareTaskList(fastboot_info_tasks, hardcoded_tasks)) {
226 std::cout << "\n\n---Hardcoded Task List---\n"
227 << tasksToString(hardcoded_tasks) << "\n---Fastboot-Info Task List---\n"
228 << tasksToString(fastboot_info_tasks);
229 }
230
231 ASSERT_TRUE(compareTaskList(fastboot_info_tasks, hardcoded_tasks));
232
233 ASSERT_TRUE(fastboot_info_tasks.size() >= hardcoded_tasks.size())
234 << "size of fastboot-info task list: " << fastboot_info_tasks.size()
235 << " size of hardcoded task list: " << hardcoded_tasks.size();
236}
Daniel Zheng1fff6902023-08-24 09:47:43 -0700237
238TEST_F(ParseTest, CanOptimizeTest) {
239 if (!get_android_product_out()) {
240 GTEST_SKIP();
241 }
242
243 LocalImageSource s;
244 fp->source = &s;
245 fp->sparse_limit = std::numeric_limits<int64_t>::max();
246
247 fastboot::MockFastbootDriver fb;
248 fp->fb = &fb;
249 fp->should_optimize_flash_super = false;
250 fp->should_use_fastboot_info = true;
251
252 std::vector<std::pair<std::vector<std::string>, bool>> patternmatchtest = {
253 {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
254 "update-super", "flash product", "flash system", "flash system_ext", "flash odm",
255 "if-wipe erase userdata"},
256 true},
257 {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
258 "update-super", "flash product", "flash system", "flash system_ext", "flash odm",
259 "if-wipe erase userdata"},
260 true},
261 {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
262 "flash product", "flash system", "flash system_ext", "flash odm",
263 "if-wipe erase userdata"},
264 false},
265 {{"flash boot", "flash init_boot", "flash vendor_boot", "update-super", "flash product",
266 "flash system", "flash system_ext", "flash odm", "if-wipe erase userdata"},
267 false},
268 };
269
270 auto remove_if_callback = [&](const auto& task) -> bool { return !!task->AsResizeTask(); };
271
272 for (auto& test : patternmatchtest) {
273 std::vector<std::unique_ptr<Task>> tasks = ParseFastbootInfo(fp.get(), test.first);
274 tasks.erase(std::remove_if(tasks.begin(), tasks.end(), remove_if_callback), tasks.end());
275 ASSERT_EQ(OptimizedFlashSuperTask::CanOptimize(fp->source, tasks), test.second);
276 }
277}
278// Note: this test is exclusively testing that optimized flash super pattern matches a given task
279// list and is able to optimized based on a correct sequence of tasks
280TEST_F(ParseTest, OptimizedFlashSuperPatternMatchTest) {
281 if (!get_android_product_out()) {
282 GTEST_SKIP();
283 }
284
285 LocalImageSource s;
286 fp->source = &s;
287 fp->sparse_limit = std::numeric_limits<int64_t>::max();
288
289 fastboot::MockFastbootDriver fb;
290 fp->fb = &fb;
291 fp->should_optimize_flash_super = true;
292 fp->should_use_fastboot_info = true;
293
294 ON_CALL(fb, GetVar("super-partition-name", _, _))
295 .WillByDefault(testing::Return(fastboot::BAD_ARG));
296
297 ON_CALL(fb, GetVar("slot-count", _, _))
298 .WillByDefault(testing::DoAll(testing::SetArgPointee<1>("2"),
299 testing::Return(fastboot::SUCCESS)));
300
301 ON_CALL(fb, GetVar("partition-size:super", _, _))
302 .WillByDefault(testing::DoAll(testing::SetArgPointee<1>("1000"),
303 testing::Return(fastboot::SUCCESS)));
304
305 std::vector<std::pair<std::vector<std::string>, bool>> patternmatchtest = {
306 {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
307 "update-super", "flash product", "flash system", "flash system_ext", "flash odm",
308 "if-wipe erase userdata"},
309 true},
310 {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
311 "update-super", "flash product", "flash system", "flash system_ext", "flash odm",
312 "if-wipe erase userdata"},
313 true},
314 {{"flash boot", "flash init_boot", "flash vendor_boot", "reboot fastboot",
315 "flash product", "flash system", "flash system_ext", "flash odm",
316 "if-wipe erase userdata"},
317 false},
318 {{"flash boot", "flash init_boot", "flash vendor_boot", "update-super", "flash product",
319 "flash system", "flash system_ext", "flash odm", "if-wipe erase userdata"},
320 false},
321 };
322
323 for (auto& test : patternmatchtest) {
324 std::vector<std::unique_ptr<Task>> tasks = ParseFastbootInfo(fp.get(), test.first);
325 // Check to make sure we have an optimized flash super task && no more dynamic partition
326 // flashing tasks
327 auto&& IsOptimized = [](const FlashingPlan* fp,
328 const std::vector<std::unique_ptr<Task>>& tasks) {
329 bool contains_optimized_task = false;
330 for (auto& task : tasks) {
331 if (auto optimized_task = task->AsOptimizedFlashSuperTask()) {
332 contains_optimized_task = true;
333 }
334 if (auto flash_task = task->AsFlashTask()) {
335 if (FlashTask::IsDynamicParitition(fp->source, flash_task)) {
336 return false;
337 }
338 }
339 }
340 return contains_optimized_task;
341 };
342 ASSERT_EQ(IsOptimized(fp.get(), tasks), test.second);
343 }
344}