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