blob: 145b4d77df043031a2368993920e75a573c74ecd [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"
19
20#include <gtest/gtest.h>
21#include <fstream>
22#include <iostream>
23#include <memory>
24#include <unordered_map>
25#include "android-base/strings.h"
26using android::base::Split;
27
28class ParseTest : public ::testing ::Test {
29 protected:
30 void SetUp() override {
31 fp = std::make_unique<FlashingPlan>();
32 fp->slot_override = "b";
33 fp->secondary_slot = "a";
34 fp->wants_wipe = false;
35 }
36 void TearDown() override {}
37
38 std::unique_ptr<FlashingPlan> fp;
39
40 private:
41};
42
43static std::vector<std::unique_ptr<Task>> collectTasks(FlashingPlan* fp,
44 const std::vector<std::string>& commands) {
45 std::vector<std::vector<std::string>> vec_commands;
46 for (auto& command : commands) {
47 vec_commands.emplace_back(android::base::Split(command, " "));
48 }
49 std::vector<std::unique_ptr<Task>> tasks;
50 for (auto& command : vec_commands) {
51 tasks.emplace_back(ParseFastbootInfoLine(fp, command));
52 }
53 return tasks;
54}
55
Daniel Zheng935ee1f2023-04-13 11:03:34 -070056std::unique_ptr<Task> ParseCommand(FlashingPlan* fp, std::string command) {
57 std::vector<std::string> vec_command = android::base::Split(command, " ");
58 return ParseFastbootInfoLine(fp, vec_command);
59}
60
Daniel Zheng76aa2572023-03-30 15:48:23 -070061TEST_F(ParseTest, CORRECT_FlASH_TASK_FORMED) {
62 std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img",
63 "flash system", "flash --apply-vbmeta vbmeta"};
64
65 std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands);
66
67 std::vector<std::vector<std::string>> expected_values{
68 {"dtbo", "dtbo_b", "b", "dtbo.img"},
69 {"system", "system_a", "a", "system_other.img"},
70 {"system", "system_b", "b", "system.img"},
71 {"vbmeta", "vbmeta_b", "b", "vbmeta.img"}
72
73 };
74
75 for (auto& task : tasks) {
76 ASSERT_TRUE(task != nullptr);
77 }
78
79 for (size_t i = 0; i < tasks.size(); i++) {
80 auto task = tasks[i]->AsFlashTask();
81 ASSERT_TRUE(task != nullptr);
82 ASSERT_EQ(task->GetPartition(), expected_values[i][0]);
83 ASSERT_EQ(task->GetPartitionAndSlot(), expected_values[i][1]);
84 ASSERT_EQ(task->GetSlot(), expected_values[i][2]);
85 ASSERT_EQ(task->GetImageName(), expected_values[i][3]);
86 }
87}
Daniel Zheng68b84df2023-04-13 11:39:26 -070088
89TEST_F(ParseTest, VERSION_CHECK_CORRRECT) {
90 std::vector<std::string> correct_versions = {
91 "version 1.0",
92 "version 22.00",
93 };
94
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"};
98
99 for (auto& version : correct_versions) {
100 ASSERT_TRUE(CheckFastbootInfoRequirements(android::base::Split(version, " "))) << version;
101 }
102 for (auto& version : bad_versions) {
103 ASSERT_FALSE(CheckFastbootInfoRequirements(android::base::Split(version, " "))) << version;
104 }
Daniel Zheng935ee1f2023-04-13 11:03:34 -0700105}
106
107TEST_F(ParseTest, BAD_FASTBOOT_INFO_INPUT) {
108 ASSERT_EQ(ParseCommand(fp.get(), "flash"), nullptr);
109 ASSERT_EQ(ParseCommand(fp.get(), "flash --slot-other --apply-vbmeta"), nullptr);
110 ASSERT_EQ(ParseCommand(fp.get(), "flash --apply-vbmeta"), nullptr);
111 ASSERT_EQ(ParseCommand(fp.get(), "if-wipe"), nullptr);
112 ASSERT_EQ(ParseCommand(fp.get(), "if-wipe flash"), nullptr);
113 ASSERT_EQ(ParseCommand(fp.get(), "wipe dtbo"), nullptr);
114 ASSERT_EQ(ParseCommand(fp.get(), "update-super dtbo"), nullptr);
115 ASSERT_EQ(ParseCommand(fp.get(), "flash system system.img system"), nullptr);
116 ASSERT_EQ(ParseCommand(fp.get(), "reboot bootloader fastboot"), nullptr);
117 ASSERT_EQ(ParseCommand(fp.get(),
118 "flash --slot-other --apply-vbmeta system system_other.img system"),
119 nullptr);
120 ASSERT_EQ(ParseCommand(fp.get(), "erase"), nullptr);
121 ASSERT_EQ(ParseCommand(fp.get(), "erase dtbo dtbo"), nullptr);
122 ASSERT_EQ(ParseCommand(fp.get(), "wipe this"), nullptr);
123}