blob: 6fc20562a6fa8d8b0edb1b71525346ab7daafc3e [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>
22#include <fstream>
23#include <iostream>
24#include <memory>
25#include <unordered_map>
26#include "android-base/strings.h"
27using 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 Zheng207c0a32023-04-20 14:13:15 -070063TEST_F(ParseTest, CorrectFlashTaskFormed) {
Daniel Zheng76aa2572023-03-30 15:48:23 -070064 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 Zheng68b84df2023-04-13 11:39:26 -070090
Daniel Zheng207c0a32023-04-20 14:13:15 -070091TEST_F(ParseTest, VersionCheckCorrect) {
Daniel Zheng68b84df2023-04-13 11:39:26 -070092 std::vector<std::string> correct_versions = {
93 "version 1.0",
94 "version 22.00",
95 };
96
97 std::vector<std::string> bad_versions = {"version", "version .01", "version x1",
98 "version 1.0.1", "version 1.", "s 1.0",
99 "version 1.0 2.0"};
100
101 for (auto& version : correct_versions) {
102 ASSERT_TRUE(CheckFastbootInfoRequirements(android::base::Split(version, " "))) << version;
103 }
104 for (auto& version : bad_versions) {
105 ASSERT_FALSE(CheckFastbootInfoRequirements(android::base::Split(version, " "))) << version;
106 }
Daniel Zheng935ee1f2023-04-13 11:03:34 -0700107}
108
Daniel Zheng207c0a32023-04-20 14:13:15 -0700109TEST_F(ParseTest, BadFastbootInput) {
Daniel Zheng935ee1f2023-04-13 11:03:34 -0700110 ASSERT_EQ(ParseCommand(fp.get(), "flash"), nullptr);
111 ASSERT_EQ(ParseCommand(fp.get(), "flash --slot-other --apply-vbmeta"), nullptr);
112 ASSERT_EQ(ParseCommand(fp.get(), "flash --apply-vbmeta"), nullptr);
113 ASSERT_EQ(ParseCommand(fp.get(), "if-wipe"), nullptr);
114 ASSERT_EQ(ParseCommand(fp.get(), "if-wipe flash"), nullptr);
115 ASSERT_EQ(ParseCommand(fp.get(), "wipe dtbo"), nullptr);
116 ASSERT_EQ(ParseCommand(fp.get(), "update-super dtbo"), nullptr);
117 ASSERT_EQ(ParseCommand(fp.get(), "flash system system.img system"), nullptr);
118 ASSERT_EQ(ParseCommand(fp.get(), "reboot bootloader fastboot"), nullptr);
119 ASSERT_EQ(ParseCommand(fp.get(),
120 "flash --slot-other --apply-vbmeta system system_other.img system"),
121 nullptr);
122 ASSERT_EQ(ParseCommand(fp.get(), "erase"), nullptr);
123 ASSERT_EQ(ParseCommand(fp.get(), "erase dtbo dtbo"), nullptr);
124 ASSERT_EQ(ParseCommand(fp.get(), "wipe this"), nullptr);
125}
Daniel Zheng32dc6dd2023-04-10 11:16:33 -0700126
Daniel Zheng207c0a32023-04-20 14:13:15 -0700127TEST_F(ParseTest, CorrectTaskFormed) {
Daniel Zheng32dc6dd2023-04-10 11:16:33 -0700128 std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img",
129 "reboot bootloader", "update-super", "erase cache"};
130 std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands);
131
132 ASSERT_TRUE(tasks[0]->AsFlashTask());
133 ASSERT_TRUE(tasks[0]->AsFlashTask());
134 ASSERT_TRUE(tasks[1]->AsFlashTask());
135 ASSERT_TRUE(tasks[2]->AsRebootTask());
136 ASSERT_TRUE(tasks[3]->AsUpdateSuperTask());
137 ASSERT_TRUE(tasks[4]->AsWipeTask());
138}
Daniel Zheng0d2d6312023-04-11 09:09:35 -0700139
140TEST_F(ParseTest, CorrectDriverCalls) {
141 fastboot::MockFastbootDriver fb;
142 fp->fb = &fb;
143
144 EXPECT_CALL(fb, RebootTo(_, _, _)).Times(1);
145 EXPECT_CALL(fb, Reboot(_, _)).Times(1);
146 EXPECT_CALL(fb, WaitForDisconnect()).Times(2);
147
148 std::vector<std::string> commands = {"reboot bootloader", "reboot"};
149 std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands);
150
151 for (auto& task : tasks) {
152 task->Run();
153 }
154}