blob: bb9903e2d3800064a0e3566c9251d10a58df8b6e [file] [log] [blame]
Yifan Hong537802d2018-08-15 13:15:42 -07001//
2// Copyright (C) 2018 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#include "update_engine/boot_control_android.h"
18
19#include <set>
Yifan Hongd4db07e2018-10-18 17:46:27 -070020#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070021
Yifan Hongd4db07e2018-10-18 17:46:27 -070022#include <base/logging.h>
23#include <base/strings/string_util.h>
David Andersond63cb3c2018-10-01 14:15:00 -070024#include <fs_mgr.h>
Yifan Hong537802d2018-08-15 13:15:42 -070025#include <gmock/gmock.h>
26#include <gtest/gtest.h>
Yifan Hong1d9077f2018-12-07 12:09:37 -080027#include <libdm/dm.h>
Yifan Hong537802d2018-08-15 13:15:42 -070028
29#include "update_engine/mock_boot_control_hal.h"
30#include "update_engine/mock_dynamic_partition_control.h"
31
Yifan Hong1d9077f2018-12-07 12:09:37 -080032using android::dm::DmDeviceState;
Yifan Hong537802d2018-08-15 13:15:42 -070033using android::fs_mgr::MetadataBuilder;
34using android::hardware::Void;
Dan Albertfcfbda22018-12-14 12:57:55 -080035using std::string;
Yifan Hong537802d2018-08-15 13:15:42 -070036using testing::_;
37using testing::AnyNumber;
38using testing::Contains;
39using testing::Eq;
40using testing::Invoke;
41using testing::Key;
42using testing::MakeMatcher;
43using testing::Matcher;
44using testing::MatcherInterface;
45using testing::MatchResultListener;
46using testing::NiceMock;
Yifan Hongd4db07e2018-10-18 17:46:27 -070047using testing::Not;
Yifan Hong537802d2018-08-15 13:15:42 -070048using testing::Return;
49
50namespace chromeos_update_engine {
51
52constexpr const uint32_t kMaxNumSlots = 2;
53constexpr const char* kSlotSuffixes[kMaxNumSlots] = {"_a", "_b"};
54constexpr const char* kFakeDevicePath = "/fake/dev/path/";
Yifan Hong1d9077f2018-12-07 12:09:37 -080055constexpr const char* kFakeDmDevicePath = "/fake/dm/dev/path/";
Yifan Hong537802d2018-08-15 13:15:42 -070056constexpr const uint32_t kFakeMetadataSize = 65536;
Yifan Hongd4db07e2018-10-18 17:46:27 -070057constexpr const char* kDefaultGroup = "foo";
58
Yifan Hong537802d2018-08-15 13:15:42 -070059// A map describing the size of each partition.
Dan Albertfcfbda22018-12-14 12:57:55 -080060// "{name, size}"
61using PartitionSizes = std::map<string, uint64_t>;
62
63// "{name_a, size}"
64using PartitionSuffixSizes = std::map<string, uint64_t>;
Yifan Hongd4db07e2018-10-18 17:46:27 -070065
66using PartitionMetadata = BootControlInterface::PartitionMetadata;
Yifan Hong537802d2018-08-15 13:15:42 -070067
68// C++ standards do not allow uint64_t (aka unsigned long) to be the parameter
69// of user-defined literal operators.
Yifan Hongd4db07e2018-10-18 17:46:27 -070070constexpr unsigned long long operator"" _MiB(unsigned long long x) { // NOLINT
Yifan Hong537802d2018-08-15 13:15:42 -070071 return x << 20;
72}
Yifan Hongd4db07e2018-10-18 17:46:27 -070073constexpr unsigned long long operator"" _GiB(unsigned long long x) { // NOLINT
Yifan Hong537802d2018-08-15 13:15:42 -070074 return x << 30;
75}
76
Yifan Hongd4db07e2018-10-18 17:46:27 -070077constexpr uint64_t kDefaultGroupSize = 5_GiB;
78// Super device size. 1 MiB for metadata.
79constexpr uint64_t kDefaultSuperSize = kDefaultGroupSize * 2 + 1_MiB;
80
Yifan Hong537802d2018-08-15 13:15:42 -070081template <typename U, typename V>
82std::ostream& operator<<(std::ostream& os, const std::map<U, V>& param) {
83 os << "{";
84 bool first = true;
85 for (const auto& pair : param) {
86 if (!first)
87 os << ", ";
88 os << pair.first << ":" << pair.second;
89 first = false;
90 }
91 return os << "}";
92}
93
Yifan Hongd4db07e2018-10-18 17:46:27 -070094template <typename T>
95std::ostream& operator<<(std::ostream& os, const std::vector<T>& param) {
96 os << "[";
97 bool first = true;
98 for (const auto& e : param) {
99 if (!first)
100 os << ", ";
101 os << e;
102 first = false;
103 }
104 return os << "]";
105}
106
107std::ostream& operator<<(std::ostream& os,
108 const PartitionMetadata::Partition& p) {
109 return os << "{" << p.name << ", " << p.size << "}";
110}
111
112std::ostream& operator<<(std::ostream& os, const PartitionMetadata::Group& g) {
113 return os << "{" << g.name << ", " << g.size << ", " << g.partitions << "}";
114}
115
116std::ostream& operator<<(std::ostream& os, const PartitionMetadata& m) {
117 return os << m.groups;
118}
119
Dan Albertfcfbda22018-12-14 12:57:55 -0800120inline string GetDevice(const string& name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700121 return kFakeDevicePath + name;
122}
Yifan Hong30eb7b52018-11-20 11:05:46 -0800123
Dan Albertfcfbda22018-12-14 12:57:55 -0800124inline string GetDmDevice(const string& name) {
Yifan Hong1d9077f2018-12-07 12:09:37 -0800125 return kFakeDmDevicePath + name;
126}
127
Yifan Hong30eb7b52018-11-20 11:05:46 -0800128// TODO(elsk): fs_mgr_get_super_partition_name should be mocked.
Dan Albertfcfbda22018-12-14 12:57:55 -0800129inline string GetSuperDevice(uint32_t slot) {
Yifan Hong30eb7b52018-11-20 11:05:46 -0800130 return GetDevice(fs_mgr_get_super_partition_name(slot));
Yifan Hong537802d2018-08-15 13:15:42 -0700131}
132
133struct TestParam {
134 uint32_t source;
135 uint32_t target;
136};
137std::ostream& operator<<(std::ostream& os, const TestParam& param) {
138 return os << "{source: " << param.source << ", target:" << param.target
139 << "}";
140}
141
Yifan Hongd4db07e2018-10-18 17:46:27 -0700142// To support legacy tests, auto-convert {name_a: size} map to
143// PartitionMetadata.
Dan Albertfcfbda22018-12-14 12:57:55 -0800144PartitionMetadata partitionSuffixSizesToMetadata(
145 const PartitionSuffixSizes& partition_sizes) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700146 PartitionMetadata metadata;
147 for (const char* suffix : kSlotSuffixes) {
148 metadata.groups.push_back(
Dan Albertfcfbda22018-12-14 12:57:55 -0800149 {string(kDefaultGroup) + suffix, kDefaultGroupSize, {}});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700150 }
151 for (const auto& pair : partition_sizes) {
152 for (size_t suffix_idx = 0; suffix_idx < kMaxNumSlots; ++suffix_idx) {
153 if (base::EndsWith(pair.first,
154 kSlotSuffixes[suffix_idx],
155 base::CompareCase::SENSITIVE)) {
156 metadata.groups[suffix_idx].partitions.push_back(
157 {pair.first, pair.second});
158 }
159 }
160 }
161 return metadata;
162}
163
164// To support legacy tests, auto-convert {name: size} map to PartitionMetadata.
Dan Albertfcfbda22018-12-14 12:57:55 -0800165PartitionMetadata partitionSizesToMetadata(
166 const PartitionSizes& partition_sizes) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700167 PartitionMetadata metadata;
Dan Albertfcfbda22018-12-14 12:57:55 -0800168 metadata.groups.push_back({string{kDefaultGroup}, kDefaultGroupSize, {}});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700169 for (const auto& pair : partition_sizes) {
170 metadata.groups[0].partitions.push_back({pair.first, pair.second});
171 }
172 return metadata;
173}
174
175std::unique_ptr<MetadataBuilder> NewFakeMetadata(
176 const PartitionMetadata& metadata) {
177 auto builder =
178 MetadataBuilder::New(kDefaultSuperSize, kFakeMetadataSize, kMaxNumSlots);
179 EXPECT_GE(builder->AllocatableSpace(), kDefaultGroupSize * 2);
Yifan Hong537802d2018-08-15 13:15:42 -0700180 EXPECT_NE(nullptr, builder);
181 if (builder == nullptr)
182 return nullptr;
Yifan Hongd4db07e2018-10-18 17:46:27 -0700183 for (const auto& group : metadata.groups) {
184 EXPECT_TRUE(builder->AddGroup(group.name, group.size));
185 for (const auto& partition : group.partitions) {
186 auto p = builder->AddPartition(partition.name, group.name, 0 /* attr */);
187 EXPECT_TRUE(p && builder->ResizePartition(p, partition.size));
188 }
Yifan Hong537802d2018-08-15 13:15:42 -0700189 }
190 return builder;
191}
192
193class MetadataMatcher : public MatcherInterface<MetadataBuilder*> {
194 public:
Yifan Hongd4db07e2018-10-18 17:46:27 -0700195 explicit MetadataMatcher(const PartitionSuffixSizes& partition_sizes)
Dan Albertfcfbda22018-12-14 12:57:55 -0800196 : partition_metadata_(partitionSuffixSizesToMetadata(partition_sizes)) {}
Yifan Hongd4db07e2018-10-18 17:46:27 -0700197 explicit MetadataMatcher(const PartitionMetadata& partition_metadata)
198 : partition_metadata_(partition_metadata) {}
199
Yifan Hong537802d2018-08-15 13:15:42 -0700200 bool MatchAndExplain(MetadataBuilder* metadata,
201 MatchResultListener* listener) const override {
202 bool success = true;
Yifan Hongd4db07e2018-10-18 17:46:27 -0700203 for (const auto& group : partition_metadata_.groups) {
204 for (const auto& partition : group.partitions) {
205 auto p = metadata->FindPartition(partition.name);
206 if (p == nullptr) {
207 if (!success)
208 *listener << "; ";
209 *listener << "No partition " << partition.name;
210 success = false;
211 continue;
212 }
213 if (p->size() != partition.size) {
214 if (!success)
215 *listener << "; ";
216 *listener << "Partition " << partition.name << " has size "
217 << p->size() << ", expected " << partition.size;
218 success = false;
219 }
220 if (p->group_name() != group.name) {
221 if (!success)
222 *listener << "; ";
223 *listener << "Partition " << partition.name << " has group "
224 << p->group_name() << ", expected " << group.name;
225 success = false;
226 }
Yifan Hong537802d2018-08-15 13:15:42 -0700227 }
228 }
229 return success;
230 }
231
232 void DescribeTo(std::ostream* os) const override {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700233 *os << "expect: " << partition_metadata_;
Yifan Hong537802d2018-08-15 13:15:42 -0700234 }
235
236 void DescribeNegationTo(std::ostream* os) const override {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700237 *os << "expect not: " << partition_metadata_;
Yifan Hong537802d2018-08-15 13:15:42 -0700238 }
239
240 private:
Yifan Hongd4db07e2018-10-18 17:46:27 -0700241 PartitionMetadata partition_metadata_;
Yifan Hong537802d2018-08-15 13:15:42 -0700242};
243
244inline Matcher<MetadataBuilder*> MetadataMatches(
Yifan Hongd4db07e2018-10-18 17:46:27 -0700245 const PartitionSuffixSizes& partition_sizes) {
Yifan Hong537802d2018-08-15 13:15:42 -0700246 return MakeMatcher(new MetadataMatcher(partition_sizes));
247}
248
Yifan Hongd4db07e2018-10-18 17:46:27 -0700249inline Matcher<MetadataBuilder*> MetadataMatches(
250 const PartitionMetadata& partition_metadata) {
251 return MakeMatcher(new MetadataMatcher(partition_metadata));
252}
253
254MATCHER_P(HasGroup, group, " has group " + group) {
255 auto groups = arg->ListGroups();
256 return std::find(groups.begin(), groups.end(), group) != groups.end();
257}
258
Yifan Hong537802d2018-08-15 13:15:42 -0700259class BootControlAndroidTest : public ::testing::Test {
260 protected:
261 void SetUp() override {
262 // Fake init bootctl_
263 bootctl_.module_ = new NiceMock<MockBootControlHal>();
264 bootctl_.dynamic_control_ =
265 std::make_unique<NiceMock<MockDynamicPartitionControl>>();
266
267 ON_CALL(module(), getNumberSlots()).WillByDefault(Invoke([] {
268 return kMaxNumSlots;
269 }));
270 ON_CALL(module(), getSuffix(_, _))
271 .WillByDefault(Invoke([](auto slot, auto cb) {
272 EXPECT_LE(slot, kMaxNumSlots);
273 cb(slot < kMaxNumSlots ? kSlotSuffixes[slot] : "");
274 return Void();
275 }));
276
277 ON_CALL(dynamicControl(), IsDynamicPartitionsEnabled())
278 .WillByDefault(Return(true));
Yifan Hong6e38b352018-11-19 14:12:37 -0800279 ON_CALL(dynamicControl(), IsDynamicPartitionsRetrofit())
280 .WillByDefault(Return(false));
Yifan Hong1d9077f2018-12-07 12:09:37 -0800281 ON_CALL(dynamicControl(), DeviceExists(_)).WillByDefault(Return(true));
Yifan Hong537802d2018-08-15 13:15:42 -0700282 ON_CALL(dynamicControl(), GetDeviceDir(_))
283 .WillByDefault(Invoke([](auto path) {
284 *path = kFakeDevicePath;
285 return true;
286 }));
Yifan Hong1d9077f2018-12-07 12:09:37 -0800287 ON_CALL(dynamicControl(), GetDmDevicePathByName(_, _))
288 .WillByDefault(Invoke([](auto partition_name_suffix, auto device) {
289 *device = GetDmDevice(partition_name_suffix);
290 return true;
291 }));
Yifan Hong537802d2018-08-15 13:15:42 -0700292 }
293
294 // Return the mocked HAL module.
295 NiceMock<MockBootControlHal>& module() {
296 return static_cast<NiceMock<MockBootControlHal>&>(*bootctl_.module_);
297 }
298
299 // Return the mocked DynamicPartitionControlInterface.
300 NiceMock<MockDynamicPartitionControl>& dynamicControl() {
301 return static_cast<NiceMock<MockDynamicPartitionControl>&>(
302 *bootctl_.dynamic_control_);
303 }
304
305 // Set the fake metadata to return when LoadMetadataBuilder is called on
306 // |slot|.
Yifan Hongd4db07e2018-10-18 17:46:27 -0700307 void SetMetadata(uint32_t slot, const PartitionSuffixSizes& sizes) {
Dan Albertfcfbda22018-12-14 12:57:55 -0800308 SetMetadata(slot, partitionSuffixSizesToMetadata(sizes));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700309 }
310
311 void SetMetadata(uint32_t slot, const PartitionMetadata& metadata) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800312 EXPECT_CALL(dynamicControl(),
Yifan Hong30eb7b52018-11-20 11:05:46 -0800313 LoadMetadataBuilder(GetSuperDevice(slot), slot, _))
Yifan Hongd4db07e2018-10-18 17:46:27 -0700314 .Times(AnyNumber())
Yifan Hong6e706b12018-11-09 16:50:51 -0800315 .WillRepeatedly(Invoke([metadata](auto, auto, auto) {
316 return NewFakeMetadata(metadata);
317 }));
Yifan Hong537802d2018-08-15 13:15:42 -0700318 }
319
Yifan Hong537802d2018-08-15 13:15:42 -0700320 // Expect that UnmapPartitionOnDeviceMapper is called on target() metadata
321 // slot with each partition in |partitions|.
Dan Albertfcfbda22018-12-14 12:57:55 -0800322 void ExpectUnmap(const std::set<string>& partitions) {
Yifan Hong537802d2018-08-15 13:15:42 -0700323 // Error when UnmapPartitionOnDeviceMapper is called on unknown arguments.
324 ON_CALL(dynamicControl(), UnmapPartitionOnDeviceMapper(_, _))
325 .WillByDefault(Return(false));
326
327 for (const auto& partition : partitions) {
328 EXPECT_CALL(dynamicControl(), UnmapPartitionOnDeviceMapper(partition, _))
329 .WillOnce(Invoke([this](auto partition, auto) {
330 mapped_devices_.erase(partition);
331 return true;
332 }));
333 }
334 }
335
Dan Albertfcfbda22018-12-14 12:57:55 -0800336 void ExpectDevicesAreMapped(const std::set<string>& partitions) {
Yifan Hong537802d2018-08-15 13:15:42 -0700337 ASSERT_EQ(partitions.size(), mapped_devices_.size());
338 for (const auto& partition : partitions) {
339 EXPECT_THAT(mapped_devices_, Contains(Key(Eq(partition))))
340 << "Expect that " << partition << " is mapped, but it is not.";
341 }
342 }
343
Yifan Hongd4db07e2018-10-18 17:46:27 -0700344 void ExpectStoreMetadata(const PartitionSuffixSizes& partition_sizes) {
Yifan Hong549eb362018-10-19 15:11:12 -0700345 ExpectStoreMetadataMatch(MetadataMatches(partition_sizes));
346 }
347
348 virtual void ExpectStoreMetadataMatch(
349 const Matcher<MetadataBuilder*>& matcher) {
350 EXPECT_CALL(dynamicControl(),
Yifan Hong30eb7b52018-11-20 11:05:46 -0800351 StoreMetadata(GetSuperDevice(target()), matcher, target()))
Yifan Hong549eb362018-10-19 15:11:12 -0700352 .WillOnce(Return(true));
353 }
354
Yifan Hong537802d2018-08-15 13:15:42 -0700355 uint32_t source() { return slots_.source; }
356
357 uint32_t target() { return slots_.target; }
358
359 // Return partition names with suffix of source().
Dan Albertfcfbda22018-12-14 12:57:55 -0800360 string S(const string& name) { return name + kSlotSuffixes[source()]; }
Yifan Hong537802d2018-08-15 13:15:42 -0700361
362 // Return partition names with suffix of target().
Dan Albertfcfbda22018-12-14 12:57:55 -0800363 string T(const string& name) { return name + kSlotSuffixes[target()]; }
Yifan Hong537802d2018-08-15 13:15:42 -0700364
365 // Set source and target slots to use before testing.
366 void SetSlots(const TestParam& slots) {
367 slots_ = slots;
368
369 ON_CALL(module(), getCurrentSlot()).WillByDefault(Invoke([this] {
370 return source();
371 }));
372 // Should not store metadata to source slot.
Yifan Hong30eb7b52018-11-20 11:05:46 -0800373 EXPECT_CALL(dynamicControl(),
374 StoreMetadata(GetSuperDevice(source()), _, source()))
Yifan Hong537802d2018-08-15 13:15:42 -0700375 .Times(0);
Yifan Hongd4db07e2018-10-18 17:46:27 -0700376 // Should not load metadata from target slot.
377 EXPECT_CALL(dynamicControl(),
Yifan Hong30eb7b52018-11-20 11:05:46 -0800378 LoadMetadataBuilder(GetSuperDevice(target()), target(), _))
Yifan Hongd4db07e2018-10-18 17:46:27 -0700379 .Times(0);
380 }
381
Tao Bao3406c772019-01-02 15:34:35 -0800382 bool InitPartitionMetadata(uint32_t slot,
383 PartitionSizes partition_sizes,
384 bool update_metadata = true) {
Dan Albertfcfbda22018-12-14 12:57:55 -0800385 auto m = partitionSizesToMetadata(partition_sizes);
Yifan Hongd4db07e2018-10-18 17:46:27 -0700386 LOG(INFO) << m;
Tao Bao3406c772019-01-02 15:34:35 -0800387 return bootctl_.InitPartitionMetadata(slot, m, update_metadata);
Yifan Hong537802d2018-08-15 13:15:42 -0700388 }
389
390 BootControlAndroid bootctl_; // BootControlAndroid under test.
391 TestParam slots_;
392 // mapped devices through MapPartitionOnDeviceMapper.
Dan Albertfcfbda22018-12-14 12:57:55 -0800393 std::map<string, string> mapped_devices_;
Yifan Hong537802d2018-08-15 13:15:42 -0700394};
395
396class BootControlAndroidTestP
397 : public BootControlAndroidTest,
398 public ::testing::WithParamInterface<TestParam> {
399 public:
400 void SetUp() override {
401 BootControlAndroidTest::SetUp();
402 SetSlots(GetParam());
403 }
404};
405
Yifan Hong537802d2018-08-15 13:15:42 -0700406// Test resize case. Grow if target metadata contains a partition with a size
407// less than expected.
408TEST_P(BootControlAndroidTestP, NeedGrowIfSizeNotMatchWhenResizing) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700409 SetMetadata(source(),
410 {{S("system"), 2_GiB},
411 {S("vendor"), 1_GiB},
412 {T("system"), 2_GiB},
413 {T("vendor"), 1_GiB}});
Yifan Hong549eb362018-10-19 15:11:12 -0700414 ExpectStoreMetadata({{S("system"), 2_GiB},
415 {S("vendor"), 1_GiB},
416 {T("system"), 3_GiB},
417 {T("vendor"), 1_GiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800418 ExpectUnmap({T("system"), T("vendor")});
Yifan Hong537802d2018-08-15 13:15:42 -0700419
Yifan Hongd4db07e2018-10-18 17:46:27 -0700420 EXPECT_TRUE(
421 InitPartitionMetadata(target(), {{"system", 3_GiB}, {"vendor", 1_GiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700422}
423
424// Test resize case. Shrink if target metadata contains a partition with a size
425// greater than expected.
426TEST_P(BootControlAndroidTestP, NeedShrinkIfSizeNotMatchWhenResizing) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700427 SetMetadata(source(),
428 {{S("system"), 2_GiB},
429 {S("vendor"), 1_GiB},
430 {T("system"), 2_GiB},
431 {T("vendor"), 1_GiB}});
Yifan Hong549eb362018-10-19 15:11:12 -0700432 ExpectStoreMetadata({{S("system"), 2_GiB},
433 {S("vendor"), 1_GiB},
434 {T("system"), 2_GiB},
435 {T("vendor"), 150_MiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800436 ExpectUnmap({T("system"), T("vendor")});
Yifan Hong537802d2018-08-15 13:15:42 -0700437
Yifan Hongd4db07e2018-10-18 17:46:27 -0700438 EXPECT_TRUE(InitPartitionMetadata(target(),
439 {{"system", 2_GiB}, {"vendor", 150_MiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700440}
441
442// Test adding partitions on the first run.
443TEST_P(BootControlAndroidTestP, AddPartitionToEmptyMetadata) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700444 SetMetadata(source(), PartitionSuffixSizes{});
Yifan Hong549eb362018-10-19 15:11:12 -0700445 ExpectStoreMetadata({{T("system"), 2_GiB}, {T("vendor"), 1_GiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800446 ExpectUnmap({T("system"), T("vendor")});
Yifan Hong537802d2018-08-15 13:15:42 -0700447
Yifan Hongd4db07e2018-10-18 17:46:27 -0700448 EXPECT_TRUE(
449 InitPartitionMetadata(target(), {{"system", 2_GiB}, {"vendor", 1_GiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700450}
451
452// Test subsequent add case.
453TEST_P(BootControlAndroidTestP, AddAdditionalPartition) {
454 SetMetadata(source(), {{S("system"), 2_GiB}, {T("system"), 2_GiB}});
Yifan Hong549eb362018-10-19 15:11:12 -0700455 ExpectStoreMetadata(
456 {{S("system"), 2_GiB}, {T("system"), 2_GiB}, {T("vendor"), 1_GiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800457 ExpectUnmap({T("system"), T("vendor")});
Yifan Hong537802d2018-08-15 13:15:42 -0700458
Yifan Hongd4db07e2018-10-18 17:46:27 -0700459 EXPECT_TRUE(
460 InitPartitionMetadata(target(), {{"system", 2_GiB}, {"vendor", 1_GiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700461}
462
463// Test delete one partition.
464TEST_P(BootControlAndroidTestP, DeletePartition) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700465 SetMetadata(source(),
466 {{S("system"), 2_GiB},
467 {S("vendor"), 1_GiB},
468 {T("system"), 2_GiB},
469 {T("vendor"), 1_GiB}});
470 // No T("vendor")
471 ExpectStoreMetadata(
472 {{S("system"), 2_GiB}, {S("vendor"), 1_GiB}, {T("system"), 2_GiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800473 ExpectUnmap({T("system")});
Yifan Hong537802d2018-08-15 13:15:42 -0700474
Yifan Hongd4db07e2018-10-18 17:46:27 -0700475 EXPECT_TRUE(InitPartitionMetadata(target(), {{"system", 2_GiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700476}
477
478// Test delete all partitions.
479TEST_P(BootControlAndroidTestP, DeleteAll) {
Yifan Hong537802d2018-08-15 13:15:42 -0700480 SetMetadata(source(),
481 {{S("system"), 2_GiB},
482 {S("vendor"), 1_GiB},
Yifan Hongd4db07e2018-10-18 17:46:27 -0700483 {T("system"), 2_GiB},
484 {T("vendor"), 1_GiB}});
485 ExpectStoreMetadata({{S("system"), 2_GiB}, {S("vendor"), 1_GiB}});
486
487 EXPECT_TRUE(InitPartitionMetadata(target(), {}));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700488}
489
490// Test corrupt source metadata case.
491TEST_P(BootControlAndroidTestP, CorruptedSourceMetadata) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800492 EXPECT_CALL(dynamicControl(),
Yifan Hong30eb7b52018-11-20 11:05:46 -0800493 LoadMetadataBuilder(GetSuperDevice(source()), source(), _))
Yifan Hong6e706b12018-11-09 16:50:51 -0800494 .WillOnce(Invoke([](auto, auto, auto) { return nullptr; }));
Yifan Hong1d9077f2018-12-07 12:09:37 -0800495 ExpectUnmap({T("system")});
496
Yifan Hongd4db07e2018-10-18 17:46:27 -0700497 EXPECT_FALSE(InitPartitionMetadata(target(), {{"system", 1_GiB}}))
498 << "Should not be able to continue with corrupt source metadata";
Yifan Hong537802d2018-08-15 13:15:42 -0700499}
500
501// Test that InitPartitionMetadata fail if there is not enough space on the
502// device.
503TEST_P(BootControlAndroidTestP, NotEnoughSpace) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700504 SetMetadata(source(),
505 {{S("system"), 3_GiB},
506 {S("vendor"), 2_GiB},
507 {T("system"), 0},
508 {T("vendor"), 0}});
509 EXPECT_FALSE(
510 InitPartitionMetadata(target(), {{"system", 3_GiB}, {"vendor", 3_GiB}}))
Yifan Hong537802d2018-08-15 13:15:42 -0700511 << "Should not be able to fit 11GiB data into 10GiB space";
512}
513
Yifan Hongd4db07e2018-10-18 17:46:27 -0700514TEST_P(BootControlAndroidTestP, NotEnoughSpaceForSlot) {
515 SetMetadata(source(),
516 {{S("system"), 1_GiB},
517 {S("vendor"), 1_GiB},
518 {T("system"), 0},
519 {T("vendor"), 0}});
520 EXPECT_FALSE(
521 InitPartitionMetadata(target(), {{"system", 3_GiB}, {"vendor", 3_GiB}}))
522 << "Should not be able to grow over size of super / 2";
523}
524
Yifan Hong1d9077f2018-12-07 12:09:37 -0800525// Test applying retrofit update on a build with dynamic partitions enabled.
526TEST_P(BootControlAndroidTestP,
527 ApplyRetrofitUpdateOnDynamicPartitionsEnabledBuild) {
528 SetMetadata(source(),
529 {{S("system"), 2_GiB},
530 {S("vendor"), 1_GiB},
531 {T("system"), 2_GiB},
532 {T("vendor"), 1_GiB}});
533 // Should not try to unmap any target partition.
534 EXPECT_CALL(dynamicControl(), UnmapPartitionOnDeviceMapper(_, _)).Times(0);
535 // Should not store metadata to target slot.
536 EXPECT_CALL(dynamicControl(),
537 StoreMetadata(GetSuperDevice(target()), _, target()))
538 .Times(0);
539
540 // Not calling through BootControlAndroidTest::InitPartitionMetadata(), since
541 // we don't want any default group in the PartitionMetadata.
Tao Bao3406c772019-01-02 15:34:35 -0800542 EXPECT_TRUE(bootctl_.InitPartitionMetadata(target(), {}, true));
Yifan Hong1d9077f2018-12-07 12:09:37 -0800543
544 // Should use dynamic source partitions.
545 EXPECT_CALL(dynamicControl(), GetState(S("system")))
546 .Times(1)
547 .WillOnce(Return(DmDeviceState::ACTIVE));
Tao Bao3406c772019-01-02 15:34:35 -0800548 string system_device;
549 EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &system_device));
550 EXPECT_EQ(GetDmDevice(S("system")), system_device);
Yifan Hong1d9077f2018-12-07 12:09:37 -0800551
552 // Should use static target partitions without querying dynamic control.
553 EXPECT_CALL(dynamicControl(), GetState(T("system"))).Times(0);
Tao Bao3406c772019-01-02 15:34:35 -0800554 EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &system_device));
555 EXPECT_EQ(GetDevice(T("system")), system_device);
556
557 // Static partition "bar".
558 EXPECT_CALL(dynamicControl(), GetState(S("bar"))).Times(0);
559 std::string bar_device;
560 EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", source(), &bar_device));
561 EXPECT_EQ(GetDevice(S("bar")), bar_device);
562
563 EXPECT_CALL(dynamicControl(), GetState(T("bar"))).Times(0);
564 EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", target(), &bar_device));
565 EXPECT_EQ(GetDevice(T("bar")), bar_device);
566}
567
568TEST_P(BootControlAndroidTestP, GetPartitionDeviceWhenResumingUpdate) {
569 // Both of the two slots contain valid partition metadata, since this is
570 // resuming an update.
571 SetMetadata(source(),
572 {{S("system"), 2_GiB},
573 {S("vendor"), 1_GiB},
574 {T("system"), 2_GiB},
575 {T("vendor"), 1_GiB}});
576 SetMetadata(target(),
577 {{S("system"), 2_GiB},
578 {S("vendor"), 1_GiB},
579 {T("system"), 2_GiB},
580 {T("vendor"), 1_GiB}});
581 EXPECT_CALL(dynamicControl(),
582 StoreMetadata(GetSuperDevice(target()), _, target()))
583 .Times(0);
584 EXPECT_TRUE(InitPartitionMetadata(
585 target(), {{"system", 2_GiB}, {"vendor", 1_GiB}}, false));
586
587 // Dynamic partition "system".
588 EXPECT_CALL(dynamicControl(), GetState(S("system")))
589 .Times(1)
590 .WillOnce(Return(DmDeviceState::ACTIVE));
591 string system_device;
592 EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &system_device));
593 EXPECT_EQ(GetDmDevice(S("system")), system_device);
594
595 EXPECT_CALL(dynamicControl(), GetState(T("system")))
596 .Times(1)
597 .WillOnce(Return(DmDeviceState::ACTIVE));
598 EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &system_device));
599 EXPECT_EQ(GetDmDevice(T("system")), system_device);
600
601 // Static partition "bar".
602 EXPECT_CALL(dynamicControl(), GetState(S("bar"))).Times(0);
603 std::string bar_device;
604 EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", source(), &bar_device));
605 EXPECT_EQ(GetDevice(S("bar")), bar_device);
606
607 EXPECT_CALL(dynamicControl(), GetState(T("bar"))).Times(0);
608 EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", target(), &bar_device));
609 EXPECT_EQ(GetDevice(T("bar")), bar_device);
Yifan Hong1d9077f2018-12-07 12:09:37 -0800610}
611
Yifan Hongd4db07e2018-10-18 17:46:27 -0700612INSTANTIATE_TEST_CASE_P(BootControlAndroidTest,
Yifan Hong537802d2018-08-15 13:15:42 -0700613 BootControlAndroidTestP,
614 testing::Values(TestParam{0, 1}, TestParam{1, 0}));
615
Yifan Hongd4db07e2018-10-18 17:46:27 -0700616const PartitionSuffixSizes update_sizes_0() {
617 // Initial state is 0 for "other" slot.
Yifan Hong537802d2018-08-15 13:15:42 -0700618 return {
619 {"grown_a", 2_GiB},
620 {"shrunk_a", 1_GiB},
621 {"same_a", 100_MiB},
622 {"deleted_a", 150_MiB},
Yifan Hongd4db07e2018-10-18 17:46:27 -0700623 // no added_a
624 {"grown_b", 200_MiB},
625 // simulate system_other
626 {"shrunk_b", 0},
627 {"same_b", 0},
628 {"deleted_b", 0},
629 // no added_b
630 };
631}
632
633const PartitionSuffixSizes update_sizes_1() {
634 return {
635 {"grown_a", 2_GiB},
636 {"shrunk_a", 1_GiB},
637 {"same_a", 100_MiB},
638 {"deleted_a", 150_MiB},
639 // no added_a
Yifan Hong537802d2018-08-15 13:15:42 -0700640 {"grown_b", 3_GiB},
641 {"shrunk_b", 150_MiB},
642 {"same_b", 100_MiB},
643 {"added_b", 150_MiB},
Yifan Hongd4db07e2018-10-18 17:46:27 -0700644 // no deleted_b
Yifan Hong537802d2018-08-15 13:15:42 -0700645 };
646}
647
Yifan Hongd4db07e2018-10-18 17:46:27 -0700648const PartitionSuffixSizes update_sizes_2() {
649 return {
650 {"grown_a", 4_GiB},
651 {"shrunk_a", 100_MiB},
652 {"same_a", 100_MiB},
653 {"deleted_a", 64_MiB},
654 // no added_a
655 {"grown_b", 3_GiB},
656 {"shrunk_b", 150_MiB},
657 {"same_b", 100_MiB},
658 {"added_b", 150_MiB},
659 // no deleted_b
660 };
Yifan Hong537802d2018-08-15 13:15:42 -0700661}
662
663// Test case for first update after the device is manufactured, in which
664// case the "other" slot is likely of size "0" (except system, which is
665// non-zero because of system_other partition)
666TEST_F(BootControlAndroidTest, SimulatedFirstUpdate) {
667 SetSlots({0, 1});
668
669 SetMetadata(source(), update_sizes_0());
670 SetMetadata(target(), update_sizes_0());
Yifan Hong549eb362018-10-19 15:11:12 -0700671 ExpectStoreMetadata(update_sizes_1());
Yifan Hong1d9077f2018-12-07 12:09:37 -0800672 ExpectUnmap({"grown_b", "shrunk_b", "same_b", "added_b"});
Yifan Hong537802d2018-08-15 13:15:42 -0700673
Yifan Hongd4db07e2018-10-18 17:46:27 -0700674 EXPECT_TRUE(InitPartitionMetadata(target(),
675 {{"grown", 3_GiB},
676 {"shrunk", 150_MiB},
677 {"same", 100_MiB},
678 {"added", 150_MiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700679}
680
681// After first update, test for the second update. In the second update, the
682// "added" partition is deleted and "deleted" partition is re-added.
683TEST_F(BootControlAndroidTest, SimulatedSecondUpdate) {
684 SetSlots({1, 0});
685
686 SetMetadata(source(), update_sizes_1());
687 SetMetadata(target(), update_sizes_0());
688
Yifan Hong549eb362018-10-19 15:11:12 -0700689 ExpectStoreMetadata(update_sizes_2());
Yifan Hong1d9077f2018-12-07 12:09:37 -0800690 ExpectUnmap({"grown_a", "shrunk_a", "same_a", "deleted_a"});
Yifan Hong537802d2018-08-15 13:15:42 -0700691
Yifan Hongd4db07e2018-10-18 17:46:27 -0700692 EXPECT_TRUE(InitPartitionMetadata(target(),
693 {{"grown", 4_GiB},
694 {"shrunk", 100_MiB},
695 {"same", 100_MiB},
696 {"deleted", 64_MiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700697}
698
699TEST_F(BootControlAndroidTest, ApplyingToCurrentSlot) {
700 SetSlots({1, 1});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700701 EXPECT_FALSE(InitPartitionMetadata(target(), {}))
Yifan Hong537802d2018-08-15 13:15:42 -0700702 << "Should not be able to apply to current slot.";
703}
704
Yifan Hongd4db07e2018-10-18 17:46:27 -0700705class BootControlAndroidGroupTestP : public BootControlAndroidTestP {
706 public:
707 void SetUp() override {
708 BootControlAndroidTestP::SetUp();
709 SetMetadata(
710 source(),
711 {.groups = {SimpleGroup(S("android"), 3_GiB, S("system"), 2_GiB),
712 SimpleGroup(S("oem"), 2_GiB, S("vendor"), 1_GiB),
713 SimpleGroup(T("android"), 3_GiB, T("system"), 0),
714 SimpleGroup(T("oem"), 2_GiB, T("vendor"), 0)}});
715 }
716
717 // Return a simple group with only one partition.
Dan Albertfcfbda22018-12-14 12:57:55 -0800718 PartitionMetadata::Group SimpleGroup(const string& group,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700719 uint64_t group_size,
Dan Albertfcfbda22018-12-14 12:57:55 -0800720 const string& partition,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700721 uint64_t partition_size) {
722 return {.name = group,
723 .size = group_size,
724 .partitions = {{.name = partition, .size = partition_size}}};
725 }
726
727 void ExpectStoreMetadata(const PartitionMetadata& partition_metadata) {
728 ExpectStoreMetadataMatch(MetadataMatches(partition_metadata));
729 }
730
731 // Expect that target slot is stored with target groups.
732 void ExpectStoreMetadataMatch(
733 const Matcher<MetadataBuilder*>& matcher) override {
734 BootControlAndroidTestP::ExpectStoreMetadataMatch(AllOf(
735 MetadataMatches(PartitionMetadata{
736 .groups = {SimpleGroup(S("android"), 3_GiB, S("system"), 2_GiB),
737 SimpleGroup(S("oem"), 2_GiB, S("vendor"), 1_GiB)}}),
738 matcher));
739 }
740};
741
742// Allow to resize within group.
743TEST_P(BootControlAndroidGroupTestP, ResizeWithinGroup) {
744 ExpectStoreMetadata(PartitionMetadata{
745 .groups = {SimpleGroup(T("android"), 3_GiB, T("system"), 3_GiB),
746 SimpleGroup(T("oem"), 2_GiB, T("vendor"), 2_GiB)}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800747 ExpectUnmap({T("system"), T("vendor")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700748
749 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
750 target(),
751 PartitionMetadata{
752 .groups = {SimpleGroup("android", 3_GiB, "system", 3_GiB),
Tao Bao3406c772019-01-02 15:34:35 -0800753 SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}},
754 true));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700755}
756
757TEST_P(BootControlAndroidGroupTestP, NotEnoughSpaceForGroup) {
758 EXPECT_FALSE(bootctl_.InitPartitionMetadata(
759 target(),
760 PartitionMetadata{
761 .groups = {SimpleGroup("android", 3_GiB, "system", 1_GiB),
Tao Bao3406c772019-01-02 15:34:35 -0800762 SimpleGroup("oem", 2_GiB, "vendor", 3_GiB)}},
763 true))
Yifan Hongd4db07e2018-10-18 17:46:27 -0700764 << "Should not be able to grow over maximum size of group";
765}
766
767TEST_P(BootControlAndroidGroupTestP, GroupTooBig) {
768 EXPECT_FALSE(bootctl_.InitPartitionMetadata(
769 target(),
770 PartitionMetadata{.groups = {{.name = "android", .size = 3_GiB},
Tao Bao3406c772019-01-02 15:34:35 -0800771 {.name = "oem", .size = 3_GiB}}},
772 true))
Yifan Hongd4db07e2018-10-18 17:46:27 -0700773 << "Should not be able to grow over size of super / 2";
774}
775
776TEST_P(BootControlAndroidGroupTestP, AddPartitionToGroup) {
777 ExpectStoreMetadata(PartitionMetadata{
778 .groups = {
779 {.name = T("android"),
780 .size = 3_GiB,
781 .partitions = {{.name = T("system"), .size = 2_GiB},
782 {.name = T("product_services"), .size = 1_GiB}}}}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800783 ExpectUnmap({T("system"), T("vendor"), T("product_services")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700784
785 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
786 target(),
787 PartitionMetadata{
Tao Bao3406c772019-01-02 15:34:35 -0800788 .groups = {{.name = "android",
789 .size = 3_GiB,
790 .partitions = {{.name = "system", .size = 2_GiB},
791 {.name = "product_services",
792 .size = 1_GiB}}},
793 SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}},
794 true));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700795}
796
797TEST_P(BootControlAndroidGroupTestP, RemovePartitionFromGroup) {
798 ExpectStoreMetadata(PartitionMetadata{
799 .groups = {{.name = T("android"), .size = 3_GiB, .partitions = {}}}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800800 ExpectUnmap({T("vendor")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700801
802 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
803 target(),
804 PartitionMetadata{
805 .groups = {{.name = "android", .size = 3_GiB, .partitions = {}},
Tao Bao3406c772019-01-02 15:34:35 -0800806 SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}},
807 true));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700808}
809
810TEST_P(BootControlAndroidGroupTestP, AddGroup) {
811 ExpectStoreMetadata(PartitionMetadata{
812 .groups = {
813 SimpleGroup(T("new_group"), 2_GiB, T("new_partition"), 2_GiB)}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800814 ExpectUnmap({T("system"), T("vendor"), T("new_partition")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700815
816 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
817 target(),
818 PartitionMetadata{
Tao Bao3406c772019-01-02 15:34:35 -0800819 .groups = {SimpleGroup("android", 2_GiB, "system", 2_GiB),
820 SimpleGroup("oem", 1_GiB, "vendor", 1_GiB),
821 SimpleGroup("new_group", 2_GiB, "new_partition", 2_GiB)}},
822 true));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700823}
824
825TEST_P(BootControlAndroidGroupTestP, RemoveGroup) {
826 ExpectStoreMetadataMatch(Not(HasGroup(T("oem"))));
Yifan Hong1d9077f2018-12-07 12:09:37 -0800827 ExpectUnmap({T("system")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700828 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
829 target(),
830 PartitionMetadata{
Tao Bao3406c772019-01-02 15:34:35 -0800831 .groups = {SimpleGroup("android", 2_GiB, "system", 2_GiB)}},
832 true));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700833}
834
835TEST_P(BootControlAndroidGroupTestP, ResizeGroup) {
836 ExpectStoreMetadata(PartitionMetadata{
837 .groups = {SimpleGroup(T("android"), 2_GiB, T("system"), 2_GiB),
838 SimpleGroup(T("oem"), 3_GiB, T("vendor"), 3_GiB)}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800839 ExpectUnmap({T("system"), T("vendor")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700840
841 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
842 target(),
843 PartitionMetadata{
844 .groups = {SimpleGroup("android", 2_GiB, "system", 2_GiB),
Tao Bao3406c772019-01-02 15:34:35 -0800845 SimpleGroup("oem", 3_GiB, "vendor", 3_GiB)}},
846 true));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700847}
848
849INSTANTIATE_TEST_CASE_P(BootControlAndroidTest,
850 BootControlAndroidGroupTestP,
851 testing::Values(TestParam{0, 1}, TestParam{1, 0}));
852
Yifan Hong537802d2018-08-15 13:15:42 -0700853} // namespace chromeos_update_engine