blob: d701dce8334ffa34e15ac370b175a9d8a86a7ff9 [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
17#ifndef UPDATE_ENGINE_DYNAMIC_PARTITION_TEST_UTILS_H_
18#define UPDATE_ENGINE_DYNAMIC_PARTITION_TEST_UTILS_H_
19
20#include <stdint.h>
21
22#include <iostream>
23#include <map>
24#include <memory>
25#include <string>
26#include <vector>
27
28#include <base/strings/string_util.h>
29#include <fs_mgr.h>
30#include <gmock/gmock.h>
31#include <gtest/gtest.h>
32#include <liblp/builder.h>
Yifan Hong87ea73f2019-09-12 13:07:37 -070033#include <storage_literals/storage_literals.h>
Yifan Hongc049f932019-07-23 15:06:05 -070034
35#include "update_engine/common/boot_control_interface.h"
Yifan Hong13d41cb2019-09-16 13:18:22 -070036#include "update_engine/update_metadata.pb.h"
Yifan Hongc049f932019-07-23 15:06:05 -070037
38namespace chromeos_update_engine {
39
40using android::fs_mgr::MetadataBuilder;
41using testing::_;
42using testing::MakeMatcher;
43using testing::Matcher;
44using testing::MatcherInterface;
45using testing::MatchResultListener;
Yifan Hong87ea73f2019-09-12 13:07:37 -070046using namespace android::storage_literals; // NOLINT(build/namespaces)
Yifan Hongc049f932019-07-23 15:06:05 -070047
48constexpr const uint32_t kMaxNumSlots = 2;
49constexpr const char* kSlotSuffixes[kMaxNumSlots] = {"_a", "_b"};
50constexpr const char* kFakeDevicePath = "/fake/dev/path/";
51constexpr const char* kFakeDmDevicePath = "/fake/dm/dev/path/";
52constexpr const uint32_t kFakeMetadataSize = 65536;
53constexpr const char* kDefaultGroup = "foo";
Yifan Hong700d7c12019-07-23 20:49:16 -070054constexpr const char* kFakeSuper = "fake_super";
Yifan Hongc049f932019-07-23 15:06:05 -070055
56// A map describing the size of each partition.
57// "{name, size}"
58using PartitionSizes = std::map<std::string, uint64_t>;
59
60// "{name_a, size}"
61using PartitionSuffixSizes = std::map<std::string, uint64_t>;
62
Yifan Hongc049f932019-07-23 15:06:05 -070063constexpr uint64_t kDefaultGroupSize = 5_GiB;
64// Super device size. 1 MiB for metadata.
65constexpr uint64_t kDefaultSuperSize = kDefaultGroupSize * 2 + 1_MiB;
66
67template <typename U, typename V>
68inline std::ostream& operator<<(std::ostream& os, const std::map<U, V>& param) {
69 os << "{";
70 bool first = true;
71 for (const auto& pair : param) {
72 if (!first)
73 os << ", ";
74 os << pair.first << ":" << pair.second;
75 first = false;
76 }
77 return os << "}";
78}
79
Yifan Hong13d41cb2019-09-16 13:18:22 -070080template <typename V>
81inline void VectorToStream(std::ostream& os, const V& param) {
Yifan Hongc049f932019-07-23 15:06:05 -070082 os << "[";
83 bool first = true;
84 for (const auto& e : param) {
85 if (!first)
86 os << ", ";
87 os << e;
88 first = false;
89 }
Yifan Hong13d41cb2019-09-16 13:18:22 -070090 os << "]";
91}
92
93inline std::ostream& operator<<(std::ostream& os, const PartitionUpdate& p) {
94 return os << "{" << p.partition_name() << ", "
95 << p.new_partition_info().size() << "}";
Yifan Hongc049f932019-07-23 15:06:05 -070096}
97
98inline std::ostream& operator<<(std::ostream& os,
Yifan Hong13d41cb2019-09-16 13:18:22 -070099 const DynamicPartitionGroup& g) {
100 os << "{" << g.name() << ", " << g.size() << ", ";
101 VectorToStream(os, g.partition_names());
102 return os << "}";
Yifan Hongc049f932019-07-23 15:06:05 -0700103}
104
105inline std::ostream& operator<<(std::ostream& os,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700106 const DeltaArchiveManifest& m) {
107 os << "{.groups = ";
108 VectorToStream(os, m.dynamic_partition_metadata().groups());
109 os << ", .partitions = ";
110 VectorToStream(os, m.partitions());
111 return os;
Yifan Hongc049f932019-07-23 15:06:05 -0700112}
113
114inline std::string GetDevice(const std::string& name) {
115 return kFakeDevicePath + name;
116}
117
118inline std::string GetDmDevice(const std::string& name) {
119 return kFakeDmDevicePath + name;
120}
121
Yifan Hong13d41cb2019-09-16 13:18:22 -0700122inline DynamicPartitionGroup* AddGroup(DeltaArchiveManifest* manifest,
123 const std::string& group,
124 uint64_t group_size) {
125 auto* g = manifest->mutable_dynamic_partition_metadata()->add_groups();
126 g->set_name(group);
127 g->set_size(group_size);
128 return g;
129}
130
131inline void AddPartition(DeltaArchiveManifest* manifest,
132 DynamicPartitionGroup* group,
133 const std::string& partition,
134 uint64_t partition_size) {
135 group->add_partition_names(partition);
136 auto* p = manifest->add_partitions();
137 p->set_partition_name(partition);
138 p->mutable_new_partition_info()->set_size(partition_size);
139}
140
Yifan Hongc049f932019-07-23 15:06:05 -0700141// To support legacy tests, auto-convert {name_a: size} map to
Yifan Hong13d41cb2019-09-16 13:18:22 -0700142// DeltaArchiveManifest.
143inline DeltaArchiveManifest PartitionSuffixSizesToManifest(
Yifan Hongc049f932019-07-23 15:06:05 -0700144 const PartitionSuffixSizes& partition_sizes) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700145 DeltaArchiveManifest manifest;
Yifan Hongc049f932019-07-23 15:06:05 -0700146 for (const char* suffix : kSlotSuffixes) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700147 AddGroup(&manifest, std::string(kDefaultGroup) + suffix, kDefaultGroupSize);
Yifan Hongc049f932019-07-23 15:06:05 -0700148 }
149 for (const auto& pair : partition_sizes) {
150 for (size_t suffix_idx = 0; suffix_idx < kMaxNumSlots; ++suffix_idx) {
151 if (base::EndsWith(pair.first,
152 kSlotSuffixes[suffix_idx],
153 base::CompareCase::SENSITIVE)) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700154 AddPartition(
155 &manifest,
156 manifest.mutable_dynamic_partition_metadata()->mutable_groups(
157 suffix_idx),
158 pair.first,
159 pair.second);
Yifan Hongc049f932019-07-23 15:06:05 -0700160 }
161 }
162 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700163 return manifest;
Yifan Hongc049f932019-07-23 15:06:05 -0700164}
165
166// To support legacy tests, auto-convert {name: size} map to PartitionMetadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700167inline DeltaArchiveManifest PartitionSizesToManifest(
Yifan Hongc049f932019-07-23 15:06:05 -0700168 const PartitionSizes& partition_sizes) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700169 DeltaArchiveManifest manifest;
170 auto* g = AddGroup(&manifest, std::string(kDefaultGroup), kDefaultGroupSize);
Yifan Hongc049f932019-07-23 15:06:05 -0700171 for (const auto& pair : partition_sizes) {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700172 AddPartition(&manifest, g, pair.first, pair.second);
Yifan Hongc049f932019-07-23 15:06:05 -0700173 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700174 return manifest;
Yifan Hongc049f932019-07-23 15:06:05 -0700175}
176
177inline std::unique_ptr<MetadataBuilder> NewFakeMetadata(
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700178 const DeltaArchiveManifest& manifest,
179 uint32_t partition_attr = 0,
180 uint64_t super_size = kDefaultSuperSize) {
Yifan Hongc049f932019-07-23 15:06:05 -0700181 auto builder =
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700182 MetadataBuilder::New(super_size, kFakeMetadataSize, kMaxNumSlots);
Yifan Hong13d41cb2019-09-16 13:18:22 -0700183 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
184 EXPECT_TRUE(builder->AddGroup(group.name(), group.size()));
185 for (const auto& partition_name : group.partition_names()) {
186 EXPECT_NE(
187 nullptr,
Yifan Hong29692902020-03-26 12:47:05 -0700188 builder->AddPartition(partition_name, group.name(), partition_attr));
Yifan Hongc049f932019-07-23 15:06:05 -0700189 }
190 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700191 for (const auto& partition : manifest.partitions()) {
192 auto p = builder->FindPartition(partition.partition_name());
193 EXPECT_TRUE(p && builder->ResizePartition(
194 p, partition.new_partition_info().size()));
195 }
Yifan Hongc049f932019-07-23 15:06:05 -0700196 return builder;
197}
198
199class MetadataMatcher : public MatcherInterface<MetadataBuilder*> {
200 public:
201 explicit MetadataMatcher(const PartitionSuffixSizes& partition_sizes)
Yifan Hong13d41cb2019-09-16 13:18:22 -0700202 : manifest_(PartitionSuffixSizesToManifest(partition_sizes)) {}
203 explicit MetadataMatcher(const DeltaArchiveManifest& manifest)
204 : manifest_(manifest) {}
Yifan Hongc049f932019-07-23 15:06:05 -0700205
206 bool MatchAndExplain(MetadataBuilder* metadata,
207 MatchResultListener* listener) const override {
208 bool success = true;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700209 for (const auto& group : manifest_.dynamic_partition_metadata().groups()) {
210 for (const auto& partition_name : group.partition_names()) {
211 auto p = metadata->FindPartition(partition_name);
Yifan Hongc049f932019-07-23 15:06:05 -0700212 if (p == nullptr) {
213 if (!success)
214 *listener << "; ";
Yifan Hong13d41cb2019-09-16 13:18:22 -0700215 *listener << "No partition " << partition_name;
Yifan Hongc049f932019-07-23 15:06:05 -0700216 success = false;
217 continue;
218 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700219 const auto& partition_updates = manifest_.partitions();
220 auto it = std::find_if(partition_updates.begin(),
221 partition_updates.end(),
222 [&](const auto& p) {
223 return p.partition_name() == partition_name;
224 });
225 if (it == partition_updates.end()) {
226 *listener << "Can't find partition update " << partition_name;
227 success = false;
228 continue;
229 }
230 auto partition_size = it->new_partition_info().size();
231 if (p->size() != partition_size) {
Yifan Hongc049f932019-07-23 15:06:05 -0700232 if (!success)
233 *listener << "; ";
Yifan Hong13d41cb2019-09-16 13:18:22 -0700234 *listener << "Partition " << partition_name << " has size "
235 << p->size() << ", expected " << partition_size;
Yifan Hongc049f932019-07-23 15:06:05 -0700236 success = false;
237 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700238 if (p->group_name() != group.name()) {
Yifan Hongc049f932019-07-23 15:06:05 -0700239 if (!success)
240 *listener << "; ";
Yifan Hong13d41cb2019-09-16 13:18:22 -0700241 *listener << "Partition " << partition_name << " has group "
242 << p->group_name() << ", expected " << group.name();
Yifan Hongc049f932019-07-23 15:06:05 -0700243 success = false;
244 }
245 }
246 }
247 return success;
248 }
249
250 void DescribeTo(std::ostream* os) const override {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700251 *os << "expect: " << manifest_;
Yifan Hongc049f932019-07-23 15:06:05 -0700252 }
253
254 void DescribeNegationTo(std::ostream* os) const override {
Yifan Hong13d41cb2019-09-16 13:18:22 -0700255 *os << "expect not: " << manifest_;
Yifan Hongc049f932019-07-23 15:06:05 -0700256 }
257
258 private:
Yifan Hong13d41cb2019-09-16 13:18:22 -0700259 DeltaArchiveManifest manifest_;
Yifan Hongc049f932019-07-23 15:06:05 -0700260};
261
262inline Matcher<MetadataBuilder*> MetadataMatches(
263 const PartitionSuffixSizes& partition_sizes) {
264 return MakeMatcher(new MetadataMatcher(partition_sizes));
265}
266
267inline Matcher<MetadataBuilder*> MetadataMatches(
Yifan Hong13d41cb2019-09-16 13:18:22 -0700268 const DeltaArchiveManifest& manifest) {
269 return MakeMatcher(new MetadataMatcher(manifest));
Yifan Hongc049f932019-07-23 15:06:05 -0700270}
271
272MATCHER_P(HasGroup, group, " has group " + group) {
273 auto groups = arg->ListGroups();
274 return std::find(groups.begin(), groups.end(), group) != groups.end();
275}
276
277struct TestParam {
278 uint32_t source;
279 uint32_t target;
280};
281inline std::ostream& operator<<(std::ostream& os, const TestParam& param) {
282 return os << "{source: " << param.source << ", target:" << param.target
283 << "}";
284}
285
286} // namespace chromeos_update_engine
287
288#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_TEST_UTILS_H_