1 //
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_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_
18 #define UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_
19
20 #include <stdint.h>
21
22 #include <iostream>
23 #include <map>
24 #include <memory>
25 #include <string>
26
27 #include <fs_mgr.h>
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30 #include <liblp/builder.h>
31 #include <storage_literals/storage_literals.h>
32 #include <android-base/strings.h>
33
34 #include "update_engine/common/boot_control_interface.h"
35 #include "update_engine/update_metadata.pb.h"
36
37 namespace chromeos_update_engine {
38
39 using android::fs_mgr::MetadataBuilder;
40 using testing::_;
41 using testing::MakeMatcher;
42 using testing::Matcher;
43 using testing::MatcherInterface;
44 using testing::MatchResultListener;
45 using namespace android::storage_literals; // NOLINT(build/namespaces)
46
47 constexpr const uint32_t kMaxNumSlots = 2;
48 constexpr const char* kSlotSuffixes[kMaxNumSlots] = {"_a", "_b"};
49 constexpr std::string_view kFakeDevicePath = "/fake/dev/path/";
50 constexpr const char* kFakeDmDevicePath = "/fake/dm/dev/path/";
51 constexpr const uint32_t kFakeMetadataSize = 65536;
52 constexpr const char* kDefaultGroup = "foo";
53 constexpr const char* kFakeSuper = "fake_super";
54
55 // A map describing the size of each partition.
56 // "{name, size}"
57 using PartitionSizes = std::map<std::string, uint64_t>;
58
59 // "{name_a, size}"
60 using PartitionSuffixSizes = std::map<std::string, uint64_t>;
61
62 constexpr uint64_t kDefaultGroupSize = 5_GiB;
63 // Super device size. 1 MiB for metadata.
64 constexpr uint64_t kDefaultSuperSize = kDefaultGroupSize * 2 + 1_MiB;
65
66 template <typename U, typename V>
67 inline 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
79 template <typename V>
VectorToStream(std::ostream & os,const V & param)80 inline void VectorToStream(std::ostream& os, const V& param) {
81 os << "[";
82 bool first = true;
83 for (const auto& e : param) {
84 if (!first)
85 os << ", ";
86 os << e;
87 first = false;
88 }
89 os << "]";
90 }
91
92 inline std::ostream& operator<<(std::ostream& os, const PartitionUpdate& p) {
93 return os << "{" << p.partition_name() << ", "
94 << p.new_partition_info().size() << "}";
95 }
96
97 inline std::ostream& operator<<(std::ostream& os,
98 const DynamicPartitionGroup& g) {
99 os << "{" << g.name() << ", " << g.size() << ", ";
100 VectorToStream(os, g.partition_names());
101 return os << "}";
102 }
103
104 inline std::ostream& operator<<(std::ostream& os,
105 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;
111 }
112
GetDevice(const std::string & name)113 inline std::string GetDevice(const std::string& name) {
114 return std::string(kFakeDevicePath) + name;
115 }
116
GetDmDevice(const std::string & name)117 inline std::string GetDmDevice(const std::string& name) {
118 return kFakeDmDevicePath + name;
119 }
120
AddGroup(DeltaArchiveManifest * manifest,const std::string & group,uint64_t group_size)121 inline 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
AddPartition(DeltaArchiveManifest * manifest,DynamicPartitionGroup * group,const std::string & partition,uint64_t partition_size)130 inline 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
140 // To support legacy tests, auto-convert {name_a: size} map to
141 // DeltaArchiveManifest.
PartitionSuffixSizesToManifest(const PartitionSuffixSizes & partition_sizes)142 inline DeltaArchiveManifest PartitionSuffixSizesToManifest(
143 const PartitionSuffixSizes& partition_sizes) {
144 DeltaArchiveManifest manifest;
145 for (const char* suffix : kSlotSuffixes) {
146 AddGroup(&manifest, std::string(kDefaultGroup) + suffix, kDefaultGroupSize);
147 }
148 for (const auto& pair : partition_sizes) {
149 for (size_t suffix_idx = 0; suffix_idx < kMaxNumSlots; ++suffix_idx) {
150 if (android::base::EndsWith(pair.first, kSlotSuffixes[suffix_idx])) {
151 AddPartition(
152 &manifest,
153 manifest.mutable_dynamic_partition_metadata()->mutable_groups(
154 suffix_idx),
155 pair.first,
156 pair.second);
157 }
158 }
159 }
160 return manifest;
161 }
162
163 // To support legacy tests, auto-convert {name: size} map to PartitionMetadata.
PartitionSizesToManifest(const PartitionSizes & partition_sizes)164 inline DeltaArchiveManifest PartitionSizesToManifest(
165 const PartitionSizes& partition_sizes) {
166 DeltaArchiveManifest manifest;
167 auto* g = AddGroup(&manifest, std::string(kDefaultGroup), kDefaultGroupSize);
168 for (const auto& pair : partition_sizes) {
169 AddPartition(&manifest, g, pair.first, pair.second);
170 }
171 return manifest;
172 }
173
174 inline std::unique_ptr<MetadataBuilder> NewFakeMetadata(
175 const DeltaArchiveManifest& manifest,
176 uint32_t partition_attr = 0,
177 uint64_t super_size = kDefaultSuperSize) {
178 auto builder =
179 MetadataBuilder::New(super_size, kFakeMetadataSize, kMaxNumSlots);
180 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,
185 builder->AddPartition(partition_name, group.name(), partition_attr));
186 }
187 }
188 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 }
193 return builder;
194 }
195
196 class MetadataMatcher : public MatcherInterface<MetadataBuilder*> {
197 public:
MetadataMatcher(const PartitionSuffixSizes & partition_sizes)198 explicit MetadataMatcher(const PartitionSuffixSizes& partition_sizes)
199 : manifest_(PartitionSuffixSizesToManifest(partition_sizes)) {}
MetadataMatcher(const DeltaArchiveManifest & manifest)200 explicit MetadataMatcher(const DeltaArchiveManifest& manifest)
201 : manifest_(manifest) {}
202
MatchAndExplain(MetadataBuilder * metadata,MatchResultListener * listener)203 bool MatchAndExplain(MetadataBuilder* metadata,
204 MatchResultListener* listener) const override {
205 bool success = true;
206 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);
209 if (p == nullptr) {
210 if (!success)
211 *listener << "; ";
212 *listener << "No partition " << partition_name;
213 success = false;
214 continue;
215 }
216 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) {
229 if (!success)
230 *listener << "; ";
231 *listener << "Partition " << partition_name << " has size "
232 << p->size() << ", expected " << partition_size;
233 success = false;
234 }
235 if (p->group_name() != group.name()) {
236 if (!success)
237 *listener << "; ";
238 *listener << "Partition " << partition_name << " has group "
239 << p->group_name() << ", expected " << group.name();
240 success = false;
241 }
242 }
243 }
244 return success;
245 }
246
DescribeTo(std::ostream * os)247 void DescribeTo(std::ostream* os) const override {
248 *os << "expect: " << manifest_;
249 }
250
DescribeNegationTo(std::ostream * os)251 void DescribeNegationTo(std::ostream* os) const override {
252 *os << "expect not: " << manifest_;
253 }
254
255 private:
256 DeltaArchiveManifest manifest_;
257 };
258
MetadataMatches(const PartitionSuffixSizes & partition_sizes)259 inline Matcher<MetadataBuilder*> MetadataMatches(
260 const PartitionSuffixSizes& partition_sizes) {
261 return MakeMatcher(new MetadataMatcher(partition_sizes));
262 }
263
MetadataMatches(const DeltaArchiveManifest & manifest)264 inline Matcher<MetadataBuilder*> MetadataMatches(
265 const DeltaArchiveManifest& manifest) {
266 return MakeMatcher(new MetadataMatcher(manifest));
267 }
268
269 MATCHER_P(HasGroup, group, " has group " + group) {
270 auto groups = arg->ListGroups();
271 return std::find(groups.begin(), groups.end(), group) != groups.end();
272 }
273
274 struct TestParam {
275 uint32_t source;
276 uint32_t target;
277 };
278 inline 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
285 #endif // UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_
286