-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_partition_test_utils.h
286 lines (252 loc) · 9.31 KB
/
dynamic_partition_test_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef UPDATE_ENGINE_DYNAMIC_PARTITION_TEST_UTILS_H_
#define UPDATE_ENGINE_DYNAMIC_PARTITION_TEST_UTILS_H_
#include <stdint.h>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <base/strings/string_util.h>
#include <fs_mgr.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <liblp/builder.h>
#include <storage_literals/storage_literals.h>
#include "update_engine/common/boot_control_interface.h"
#include "update_engine/update_metadata.pb.h"
namespace chromeos_update_engine {
using android::fs_mgr::MetadataBuilder;
using testing::_;
using testing::MakeMatcher;
using testing::Matcher;
using testing::MatcherInterface;
using testing::MatchResultListener;
using namespace android::storage_literals; // NOLINT(build/namespaces)
constexpr const uint32_t kMaxNumSlots = 2;
constexpr const char* kSlotSuffixes[kMaxNumSlots] = {"_a", "_b"};
constexpr const char* kFakeDevicePath = "/fake/dev/path/";
constexpr const char* kFakeDmDevicePath = "/fake/dm/dev/path/";
constexpr const uint32_t kFakeMetadataSize = 65536;
constexpr const char* kDefaultGroup = "foo";
constexpr const char* kFakeSuper = "fake_super";
// A map describing the size of each partition.
// "{name, size}"
using PartitionSizes = std::map<std::string, uint64_t>;
// "{name_a, size}"
using PartitionSuffixSizes = std::map<std::string, uint64_t>;
constexpr uint64_t kDefaultGroupSize = 5_GiB;
// Super device size. 1 MiB for metadata.
constexpr uint64_t kDefaultSuperSize = kDefaultGroupSize * 2 + 1_MiB;
template <typename U, typename V>
inline std::ostream& operator<<(std::ostream& os, const std::map<U, V>& param) {
os << "{";
bool first = true;
for (const auto& pair : param) {
if (!first)
os << ", ";
os << pair.first << ":" << pair.second;
first = false;
}
return os << "}";
}
template <typename V>
inline void VectorToStream(std::ostream& os, const V& param) {
os << "[";
bool first = true;
for (const auto& e : param) {
if (!first)
os << ", ";
os << e;
first = false;
}
os << "]";
}
inline std::ostream& operator<<(std::ostream& os, const PartitionUpdate& p) {
return os << "{" << p.partition_name() << ", "
<< p.new_partition_info().size() << "}";
}
inline std::ostream& operator<<(std::ostream& os,
const DynamicPartitionGroup& g) {
os << "{" << g.name() << ", " << g.size() << ", ";
VectorToStream(os, g.partition_names());
return os << "}";
}
inline std::ostream& operator<<(std::ostream& os,
const DeltaArchiveManifest& m) {
os << "{.groups = ";
VectorToStream(os, m.dynamic_partition_metadata().groups());
os << ", .partitions = ";
VectorToStream(os, m.partitions());
return os;
}
inline std::string GetDevice(const std::string& name) {
return kFakeDevicePath + name;
}
inline std::string GetDmDevice(const std::string& name) {
return kFakeDmDevicePath + name;
}
inline DynamicPartitionGroup* AddGroup(DeltaArchiveManifest* manifest,
const std::string& group,
uint64_t group_size) {
auto* g = manifest->mutable_dynamic_partition_metadata()->add_groups();
g->set_name(group);
g->set_size(group_size);
return g;
}
inline void AddPartition(DeltaArchiveManifest* manifest,
DynamicPartitionGroup* group,
const std::string& partition,
uint64_t partition_size) {
group->add_partition_names(partition);
auto* p = manifest->add_partitions();
p->set_partition_name(partition);
p->mutable_new_partition_info()->set_size(partition_size);
}
// To support legacy tests, auto-convert {name_a: size} map to
// DeltaArchiveManifest.
inline DeltaArchiveManifest PartitionSuffixSizesToManifest(
const PartitionSuffixSizes& partition_sizes) {
DeltaArchiveManifest manifest;
for (const char* suffix : kSlotSuffixes) {
AddGroup(&manifest, std::string(kDefaultGroup) + suffix, kDefaultGroupSize);
}
for (const auto& pair : partition_sizes) {
for (size_t suffix_idx = 0; suffix_idx < kMaxNumSlots; ++suffix_idx) {
if (base::EndsWith(pair.first,
kSlotSuffixes[suffix_idx],
base::CompareCase::SENSITIVE)) {
AddPartition(
&manifest,
manifest.mutable_dynamic_partition_metadata()->mutable_groups(
suffix_idx),
pair.first,
pair.second);
}
}
}
return manifest;
}
// To support legacy tests, auto-convert {name: size} map to PartitionMetadata.
inline DeltaArchiveManifest PartitionSizesToManifest(
const PartitionSizes& partition_sizes) {
DeltaArchiveManifest manifest;
auto* g = AddGroup(&manifest, std::string(kDefaultGroup), kDefaultGroupSize);
for (const auto& pair : partition_sizes) {
AddPartition(&manifest, g, pair.first, pair.second);
}
return manifest;
}
inline std::unique_ptr<MetadataBuilder> NewFakeMetadata(
const DeltaArchiveManifest& manifest, uint32_t partition_attr = 0) {
auto builder =
MetadataBuilder::New(kDefaultSuperSize, kFakeMetadataSize, kMaxNumSlots);
for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
EXPECT_TRUE(builder->AddGroup(group.name(), group.size()));
for (const auto& partition_name : group.partition_names()) {
EXPECT_NE(
nullptr,
builder->AddPartition(partition_name, group.name(), partition_attr));
}
}
for (const auto& partition : manifest.partitions()) {
auto p = builder->FindPartition(partition.partition_name());
EXPECT_TRUE(p && builder->ResizePartition(
p, partition.new_partition_info().size()));
}
return builder;
}
class MetadataMatcher : public MatcherInterface<MetadataBuilder*> {
public:
explicit MetadataMatcher(const PartitionSuffixSizes& partition_sizes)
: manifest_(PartitionSuffixSizesToManifest(partition_sizes)) {}
explicit MetadataMatcher(const DeltaArchiveManifest& manifest)
: manifest_(manifest) {}
bool MatchAndExplain(MetadataBuilder* metadata,
MatchResultListener* listener) const override {
bool success = true;
for (const auto& group : manifest_.dynamic_partition_metadata().groups()) {
for (const auto& partition_name : group.partition_names()) {
auto p = metadata->FindPartition(partition_name);
if (p == nullptr) {
if (!success)
*listener << "; ";
*listener << "No partition " << partition_name;
success = false;
continue;
}
const auto& partition_updates = manifest_.partitions();
auto it = std::find_if(partition_updates.begin(),
partition_updates.end(),
[&](const auto& p) {
return p.partition_name() == partition_name;
});
if (it == partition_updates.end()) {
*listener << "Can't find partition update " << partition_name;
success = false;
continue;
}
auto partition_size = it->new_partition_info().size();
if (p->size() != partition_size) {
if (!success)
*listener << "; ";
*listener << "Partition " << partition_name << " has size "
<< p->size() << ", expected " << partition_size;
success = false;
}
if (p->group_name() != group.name()) {
if (!success)
*listener << "; ";
*listener << "Partition " << partition_name << " has group "
<< p->group_name() << ", expected " << group.name();
success = false;
}
}
}
return success;
}
void DescribeTo(std::ostream* os) const override {
*os << "expect: " << manifest_;
}
void DescribeNegationTo(std::ostream* os) const override {
*os << "expect not: " << manifest_;
}
private:
DeltaArchiveManifest manifest_;
};
inline Matcher<MetadataBuilder*> MetadataMatches(
const PartitionSuffixSizes& partition_sizes) {
return MakeMatcher(new MetadataMatcher(partition_sizes));
}
inline Matcher<MetadataBuilder*> MetadataMatches(
const DeltaArchiveManifest& manifest) {
return MakeMatcher(new MetadataMatcher(manifest));
}
MATCHER_P(HasGroup, group, " has group " + group) {
auto groups = arg->ListGroups();
return std::find(groups.begin(), groups.end(), group) != groups.end();
}
struct TestParam {
uint32_t source;
uint32_t target;
};
inline std::ostream& operator<<(std::ostream& os, const TestParam& param) {
return os << "{source: " << param.source << ", target:" << param.target
<< "}";
}
} // namespace chromeos_update_engine
#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_TEST_UTILS_H_