blob: 07bb3862bcf568f8aefdda56ab1d67d9cd5c4ac4 [file] [log] [blame]
Yifan Hongc049f932019-07-23 15:06:05 -07001//
2// Copyright (C) 2019 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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_
18#define UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_
Yifan Hongc049f932019-07-23 15:06:05 -070019
20#include <stdint.h>
21
22#include <iostream>
23#include <map>
24#include <memory>
25#include <string>
Yifan Hongc049f932019-07-23 15:06:05 -070026
Yifan Hongc049f932019-07-23 15:06:05 -070027#include <fs_mgr.h>
28#include <gmock/gmock.h>
29#include <gtest/gtest.h>
30#include <liblp/builder.h>
Yifan Hong87ea73f2019-09-12 13:07:37 -070031#include <storage_literals/storage_literals.h>
Kelvin Zhang0c184242024-10-25 11:19:27 -070032#include <android-base/strings.h>
Yifan Hongc049f932019-07-23 15:06:05 -070033
34#include "update_engine/common/boot_control_interface.h"
Yifan Hong13d41cb2019-09-16 13:18:22 -070035#include "update_engine/update_metadata.pb.h"
Yifan Hongc049f932019-07-23 15:06:05 -070036
37namespace chromeos_update_engine {
38
39using android::fs_mgr::MetadataBuilder;
40using testing::_;
41using testing::MakeMatcher;
42using testing::Matcher;
43using testing::MatcherInterface;
44using testing::MatchResultListener;
Yifan Hong87ea73f2019-09-12 13:07:37 -070045using namespace android::storage_literals; // NOLINT(build/namespaces)
Yifan Hongc049f932019-07-23 15:06:05 -070046
47constexpr const uint32_t kMaxNumSlots = 2;
48constexpr const char* kSlotSuffixes[kMaxNumSlots] = {"_a", "_b"};
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050049constexpr std::string_view kFakeDevicePath = "/fake/dev/path/";
Yifan Hongc049f932019-07-23 15:06:05 -070050constexpr const char* kFakeDmDevicePath = "/fake/dm/dev/path/";
51constexpr const uint32_t kFakeMetadataSize = 65536;
52constexpr const char* kDefaultGroup = "foo";
Yifan Hong700d7c12019-07-23 20:49:16 -070053constexpr const char* kFakeSuper = "fake_super";
Yifan Hongc049f932019-07-23 15:06:05 -070054
55// A map describing the size of each partition.
56// "{name, size}"
57using PartitionSizes = std::map<std::string, uint64_t>;
58
59// "{name_a, size}"
60using PartitionSuffixSizes = std::map<std::string, uint64_t>;
61
Yifan Hongc049f932019-07-23 15:06:05 -070062constexpr uint64_t kDefaultGroupSize = 5_GiB;
63// Super device size. 1 MiB for metadata.
64constexpr uint64_t kDefaultSuperSize = kDefaultGroupSize * 2 + 1_MiB;
65
66template <typename U, typename V>
67inline std::ostream& operator<<(std::ostream& os, const std::map<U, V>& param) {
68 os << "{";
69 bool first = true;
70 for (const auto& pair : param) {
71 if (!first)
72 os << ", ";
73 os << pair.first << ":" << pair.second;
74 first = false;
75 }
76 return os << "}";
77}
78
Yifan Hong13d41cb2019-09-16 13:18:22 -070079template <typename V>
80inline void VectorToStream(std::ostream& os, const V& param) {
Yifan Hongc049f932019-07-23 15:06:05 -070081 os << "[";
82 bool first = true;
83 for (const auto& e : param) {
84 if (!first)
85 os << ", ";
86 os << e;
87 first = false;
88 }
Yifan Hong13d41cb2019-09-16 13:18:22 -070089 os << "]";
90}
91
92inline std::ostream& operator<<(std::ostream& os, const PartitionUpdate& p) {
93 return os << "{" << p.partition_name() << ", "
94 << p.new_partition_info().size() << "}";
Yifan Hongc049f932019-07-23 15:06:05 -070095}
96
97inline std::ostream& operator<<(std::ostream& os,
Yifan Hong13d41cb2019-09-16 13:18:22 -070098 const DynamicPartitionGroup& g) {
99 os << "{" << g.name() << ", " << g.size() << ", ";
100 VectorToStream(os, g.partition_names());
101 return os << "}";
Yifan Hongc049f932019-07-23 15:06:05 -0700102}
103
104inline std::ostream& operator<<(std::ostream& os,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700105 const DeltaArchiveManifest& m) {
106 os << "{.groups = ";
107 VectorToStream(os, m.dynamic_partition_metadata().groups());
108 os << ", .partitions = ";
109 VectorToStream(os, m.partitions());
110 return os;
Yifan Hongc049f932019-07-23 15:06:05 -0700111}
112
113inline std::string GetDevice(const std::string& name) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500114 return std::string(kFakeDevicePath) + name;
Yifan Hongc049f932019-07-23 15:06:05 -0700115}
116
117inline std::string GetDmDevice(const std::string& name) {
118 return kFakeDmDevicePath + name;
119}
120
Yifan Hong13d41cb2019-09-16 13:18:22 -0700121inline DynamicPartitionGroup* AddGroup(DeltaArchiveManifest* manifest,
122 const std::string& group,
123 uint64_t group_size) {
124 auto* g = manifest->mutable_dynamic_partition_metadata()->add_groups();
125 g->set_name(group);
126 g->set_size(group_size);
127 return g;
128}
129
130inline void AddPartition(DeltaArchiveManifest* manifest,
131 DynamicPartitionGroup* group,
132 const std::string& partition,
133 uint64_t partition_size) {
134 group->add_partition_names(partition);
135 auto* p = manifest->add_partitions();
136 p->set_partition_name(partition);
137 p->mutable_new_partition_info()->set_size(partition_size);
138}
139
Yifan Hongc049f932019-07-23 15:06:05 -0700140// To support legacy tests, auto-convert {name_a: size} map to
Yifan Hong13d41cb2019-09-16 13:18:22 -0700141// DeltaArchiveManifest.
142inline DeltaArchiveManifest PartitionSuffixSizesToManifest(
Yifan Hongc049f932019-07-23 15:06:05 -0700143 const PartitionSuffixSizes& partition_sizes) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700144 DeltaArchiveManifest manifest;
Yifan Hongc049f932019-07-23 15:06:05 -0700145 for (const char* suffix : kSlotSuffixes) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700146 AddGroup(&manifest, std::string(kDefaultGroup) + suffix, kDefaultGroupSize);
Yifan Hongc049f932019-07-23 15:06:05 -0700147 }
148 for (const auto& pair : partition_sizes) {
149 for (size_t suffix_idx = 0; suffix_idx < kMaxNumSlots; ++suffix_idx) {
Kelvin Zhang0c184242024-10-25 11:19:27 -0700150 if (android::base::EndsWith(pair.first, kSlotSuffixes[suffix_idx])) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700151 AddPartition(
152 &manifest,
153 manifest.mutable_dynamic_partition_metadata()->mutable_groups(
154 suffix_idx),
155 pair.first,
156 pair.second);
Yifan Hongc049f932019-07-23 15:06:05 -0700157 }
158 }
159 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700160 return manifest;
Yifan Hongc049f932019-07-23 15:06:05 -0700161}
162
163// To support legacy tests, auto-convert {name: size} map to PartitionMetadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700164inline DeltaArchiveManifest PartitionSizesToManifest(
Yifan Hongc049f932019-07-23 15:06:05 -0700165 const PartitionSizes& partition_sizes) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700166 DeltaArchiveManifest manifest;
167 auto* g = AddGroup(&manifest, std::string(kDefaultGroup), kDefaultGroupSize);
Yifan Hongc049f932019-07-23 15:06:05 -0700168 for (const auto& pair : partition_sizes) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700169 AddPartition(&manifest, g, pair.first, pair.second);
Yifan Hongc049f932019-07-23 15:06:05 -0700170 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700171 return manifest;
Yifan Hongc049f932019-07-23 15:06:05 -0700172}
173
174inline std::unique_ptr<MetadataBuilder> NewFakeMetadata(
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700175 const DeltaArchiveManifest& manifest,
176 uint32_t partition_attr = 0,
177 uint64_t super_size = kDefaultSuperSize) {
Yifan Hongc049f932019-07-23 15:06:05 -0700178 auto builder =
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700179 MetadataBuilder::New(super_size, kFakeMetadataSize, kMaxNumSlots);
Yifan Hong13d41cb2019-09-16 13:18:22 -0700180 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
181 EXPECT_TRUE(builder->AddGroup(group.name(), group.size()));
182 for (const auto& partition_name : group.partition_names()) {
183 EXPECT_NE(
184 nullptr,
Yifan Hong29692902020-03-26 12:47:05 -0700185 builder->AddPartition(partition_name, group.name(), partition_attr));
Yifan Hongc049f932019-07-23 15:06:05 -0700186 }
187 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700188 for (const auto& partition : manifest.partitions()) {
189 auto p = builder->FindPartition(partition.partition_name());
190 EXPECT_TRUE(p && builder->ResizePartition(
191 p, partition.new_partition_info().size()));
192 }
Yifan Hongc049f932019-07-23 15:06:05 -0700193 return builder;
194}
195
196class MetadataMatcher : public MatcherInterface<MetadataBuilder*> {
197 public:
198 explicit MetadataMatcher(const PartitionSuffixSizes& partition_sizes)
Yifan Hong13d41cb2019-09-16 13:18:22 -0700199 : manifest_(PartitionSuffixSizesToManifest(partition_sizes)) {}
200 explicit MetadataMatcher(const DeltaArchiveManifest& manifest)
201 : manifest_(manifest) {}
Yifan Hongc049f932019-07-23 15:06:05 -0700202
203 bool MatchAndExplain(MetadataBuilder* metadata,
204 MatchResultListener* listener) const override {
205 bool success = true;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700206 for (const auto& group : manifest_.dynamic_partition_metadata().groups()) {
207 for (const auto& partition_name : group.partition_names()) {
208 auto p = metadata->FindPartition(partition_name);
Yifan Hongc049f932019-07-23 15:06:05 -0700209 if (p == nullptr) {
210 if (!success)
211 *listener << "; ";
Yifan Hong13d41cb2019-09-16 13:18:22 -0700212 *listener << "No partition " << partition_name;
Yifan Hongc049f932019-07-23 15:06:05 -0700213 success = false;
214 continue;
215 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700216 const auto& partition_updates = manifest_.partitions();
217 auto it = std::find_if(partition_updates.begin(),
218 partition_updates.end(),
219 [&](const auto& p) {
220 return p.partition_name() == partition_name;
221 });
222 if (it == partition_updates.end()) {
223 *listener << "Can't find partition update " << partition_name;
224 success = false;
225 continue;
226 }
227 auto partition_size = it->new_partition_info().size();
228 if (p->size() != partition_size) {
Yifan Hongc049f932019-07-23 15:06:05 -0700229 if (!success)
230 *listener << "; ";
Yifan Hong13d41cb2019-09-16 13:18:22 -0700231 *listener << "Partition " << partition_name << " has size "
232 << p->size() << ", expected " << partition_size;
Yifan Hongc049f932019-07-23 15:06:05 -0700233 success = false;
234 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700235 if (p->group_name() != group.name()) {
Yifan Hongc049f932019-07-23 15:06:05 -0700236 if (!success)
237 *listener << "; ";
Yifan Hong13d41cb2019-09-16 13:18:22 -0700238 *listener << "Partition " << partition_name << " has group "
239 << p->group_name() << ", expected " << group.name();
Yifan Hongc049f932019-07-23 15:06:05 -0700240 success = false;
241 }
242 }
243 }
244 return success;
245 }
246
247 void DescribeTo(std::ostream* os) const override {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700248 *os << "expect: " << manifest_;
Yifan Hongc049f932019-07-23 15:06:05 -0700249 }
250
251 void DescribeNegationTo(std::ostream* os) const override {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700252 *os << "expect not: " << manifest_;
Yifan Hongc049f932019-07-23 15:06:05 -0700253 }
254
255 private:
Yifan Hong13d41cb2019-09-16 13:18:22 -0700256 DeltaArchiveManifest manifest_;
Yifan Hongc049f932019-07-23 15:06:05 -0700257};
258
259inline Matcher<MetadataBuilder*> MetadataMatches(
260 const PartitionSuffixSizes& partition_sizes) {
261 return MakeMatcher(new MetadataMatcher(partition_sizes));
262}
263
264inline Matcher<MetadataBuilder*> MetadataMatches(
Yifan Hong13d41cb2019-09-16 13:18:22 -0700265 const DeltaArchiveManifest& manifest) {
266 return MakeMatcher(new MetadataMatcher(manifest));
Yifan Hongc049f932019-07-23 15:06:05 -0700267}
268
269MATCHER_P(HasGroup, group, " has group " + group) {
270 auto groups = arg->ListGroups();
271 return std::find(groups.begin(), groups.end(), group) != groups.end();
272}
273
274struct TestParam {
275 uint32_t source;
276 uint32_t target;
277};
278inline std::ostream& operator<<(std::ostream& os, const TestParam& param) {
279 return os << "{source: " << param.source << ", target:" << param.target
280 << "}";
281}
282
283} // namespace chromeos_update_engine
284
Amin Hassaniec7bc112020-10-29 16:47:58 -0700285#endif // UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_