blob: 400e27f74612c25e8ff400f603db631c17a919ce [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
56TEST_F(ParseTest, CORRECT_FlASH_TASK_FORMED) {
57 std::vector<std::string> commands = {"flash dtbo", "flash --slot-other system system_other.img",
58 "flash system", "flash --apply-vbmeta vbmeta"};
59
60 std::vector<std::unique_ptr<Task>> tasks = collectTasks(fp.get(), commands);
61
62 std::vector<std::vector<std::string>> expected_values{
63 {"dtbo", "dtbo_b", "b", "dtbo.img"},
64 {"system", "system_a", "a", "system_other.img"},
65 {"system", "system_b", "b", "system.img"},
66 {"vbmeta", "vbmeta_b", "b", "vbmeta.img"}
67
68 };
69
70 for (auto& task : tasks) {
71 ASSERT_TRUE(task != nullptr);
72 }
73
74 for (size_t i = 0; i < tasks.size(); i++) {
75 auto task = tasks[i]->AsFlashTask();
76 ASSERT_TRUE(task != nullptr);
77 ASSERT_EQ(task->GetPartition(), expected_values[i][0]);
78 ASSERT_EQ(task->GetPartitionAndSlot(), expected_values[i][1]);
79 ASSERT_EQ(task->GetSlot(), expected_values[i][2]);
80 ASSERT_EQ(task->GetImageName(), expected_values[i][3]);
81 }
82}