blob: 0eb3fe0bcb4393c6415601318b1563014393f597 [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
382 bool InitPartitionMetadata(uint32_t slot, PartitionSizes partition_sizes) {
Dan Albertfcfbda22018-12-14 12:57:55 -0800383 auto m = partitionSizesToMetadata(partition_sizes);
Yifan Hongd4db07e2018-10-18 17:46:27 -0700384 LOG(INFO) << m;
385 return bootctl_.InitPartitionMetadata(slot, m);
Yifan Hong537802d2018-08-15 13:15:42 -0700386 }
387
388 BootControlAndroid bootctl_; // BootControlAndroid under test.
389 TestParam slots_;
390 // mapped devices through MapPartitionOnDeviceMapper.
Dan Albertfcfbda22018-12-14 12:57:55 -0800391 std::map<string, string> mapped_devices_;
Yifan Hong537802d2018-08-15 13:15:42 -0700392};
393
394class BootControlAndroidTestP
395 : public BootControlAndroidTest,
396 public ::testing::WithParamInterface<TestParam> {
397 public:
398 void SetUp() override {
399 BootControlAndroidTest::SetUp();
400 SetSlots(GetParam());
401 }
402};
403
Yifan Hong537802d2018-08-15 13:15:42 -0700404// Test resize case. Grow if target metadata contains a partition with a size
405// less than expected.
406TEST_P(BootControlAndroidTestP, NeedGrowIfSizeNotMatchWhenResizing) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700407 SetMetadata(source(),
408 {{S("system"), 2_GiB},
409 {S("vendor"), 1_GiB},
410 {T("system"), 2_GiB},
411 {T("vendor"), 1_GiB}});
Yifan Hong549eb362018-10-19 15:11:12 -0700412 ExpectStoreMetadata({{S("system"), 2_GiB},
413 {S("vendor"), 1_GiB},
414 {T("system"), 3_GiB},
415 {T("vendor"), 1_GiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800416 ExpectUnmap({T("system"), T("vendor")});
Yifan Hong537802d2018-08-15 13:15:42 -0700417
Yifan Hongd4db07e2018-10-18 17:46:27 -0700418 EXPECT_TRUE(
419 InitPartitionMetadata(target(), {{"system", 3_GiB}, {"vendor", 1_GiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700420}
421
422// Test resize case. Shrink if target metadata contains a partition with a size
423// greater than expected.
424TEST_P(BootControlAndroidTestP, NeedShrinkIfSizeNotMatchWhenResizing) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700425 SetMetadata(source(),
426 {{S("system"), 2_GiB},
427 {S("vendor"), 1_GiB},
428 {T("system"), 2_GiB},
429 {T("vendor"), 1_GiB}});
Yifan Hong549eb362018-10-19 15:11:12 -0700430 ExpectStoreMetadata({{S("system"), 2_GiB},
431 {S("vendor"), 1_GiB},
432 {T("system"), 2_GiB},
433 {T("vendor"), 150_MiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800434 ExpectUnmap({T("system"), T("vendor")});
Yifan Hong537802d2018-08-15 13:15:42 -0700435
Yifan Hongd4db07e2018-10-18 17:46:27 -0700436 EXPECT_TRUE(InitPartitionMetadata(target(),
437 {{"system", 2_GiB}, {"vendor", 150_MiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700438}
439
440// Test adding partitions on the first run.
441TEST_P(BootControlAndroidTestP, AddPartitionToEmptyMetadata) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700442 SetMetadata(source(), PartitionSuffixSizes{});
Yifan Hong549eb362018-10-19 15:11:12 -0700443 ExpectStoreMetadata({{T("system"), 2_GiB}, {T("vendor"), 1_GiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800444 ExpectUnmap({T("system"), T("vendor")});
Yifan Hong537802d2018-08-15 13:15:42 -0700445
Yifan Hongd4db07e2018-10-18 17:46:27 -0700446 EXPECT_TRUE(
447 InitPartitionMetadata(target(), {{"system", 2_GiB}, {"vendor", 1_GiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700448}
449
450// Test subsequent add case.
451TEST_P(BootControlAndroidTestP, AddAdditionalPartition) {
452 SetMetadata(source(), {{S("system"), 2_GiB}, {T("system"), 2_GiB}});
Yifan Hong549eb362018-10-19 15:11:12 -0700453 ExpectStoreMetadata(
454 {{S("system"), 2_GiB}, {T("system"), 2_GiB}, {T("vendor"), 1_GiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800455 ExpectUnmap({T("system"), T("vendor")});
Yifan Hong537802d2018-08-15 13:15:42 -0700456
Yifan Hongd4db07e2018-10-18 17:46:27 -0700457 EXPECT_TRUE(
458 InitPartitionMetadata(target(), {{"system", 2_GiB}, {"vendor", 1_GiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700459}
460
461// Test delete one partition.
462TEST_P(BootControlAndroidTestP, DeletePartition) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700463 SetMetadata(source(),
464 {{S("system"), 2_GiB},
465 {S("vendor"), 1_GiB},
466 {T("system"), 2_GiB},
467 {T("vendor"), 1_GiB}});
468 // No T("vendor")
469 ExpectStoreMetadata(
470 {{S("system"), 2_GiB}, {S("vendor"), 1_GiB}, {T("system"), 2_GiB}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800471 ExpectUnmap({T("system")});
Yifan Hong537802d2018-08-15 13:15:42 -0700472
Yifan Hongd4db07e2018-10-18 17:46:27 -0700473 EXPECT_TRUE(InitPartitionMetadata(target(), {{"system", 2_GiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700474}
475
476// Test delete all partitions.
477TEST_P(BootControlAndroidTestP, DeleteAll) {
Yifan Hong537802d2018-08-15 13:15:42 -0700478 SetMetadata(source(),
479 {{S("system"), 2_GiB},
480 {S("vendor"), 1_GiB},
Yifan Hongd4db07e2018-10-18 17:46:27 -0700481 {T("system"), 2_GiB},
482 {T("vendor"), 1_GiB}});
483 ExpectStoreMetadata({{S("system"), 2_GiB}, {S("vendor"), 1_GiB}});
484
485 EXPECT_TRUE(InitPartitionMetadata(target(), {}));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700486}
487
488// Test corrupt source metadata case.
489TEST_P(BootControlAndroidTestP, CorruptedSourceMetadata) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800490 EXPECT_CALL(dynamicControl(),
Yifan Hong30eb7b52018-11-20 11:05:46 -0800491 LoadMetadataBuilder(GetSuperDevice(source()), source(), _))
Yifan Hong6e706b12018-11-09 16:50:51 -0800492 .WillOnce(Invoke([](auto, auto, auto) { return nullptr; }));
Yifan Hong1d9077f2018-12-07 12:09:37 -0800493 ExpectUnmap({T("system")});
494
Yifan Hongd4db07e2018-10-18 17:46:27 -0700495 EXPECT_FALSE(InitPartitionMetadata(target(), {{"system", 1_GiB}}))
496 << "Should not be able to continue with corrupt source metadata";
Yifan Hong537802d2018-08-15 13:15:42 -0700497}
498
499// Test that InitPartitionMetadata fail if there is not enough space on the
500// device.
501TEST_P(BootControlAndroidTestP, NotEnoughSpace) {
Yifan Hongd4db07e2018-10-18 17:46:27 -0700502 SetMetadata(source(),
503 {{S("system"), 3_GiB},
504 {S("vendor"), 2_GiB},
505 {T("system"), 0},
506 {T("vendor"), 0}});
507 EXPECT_FALSE(
508 InitPartitionMetadata(target(), {{"system", 3_GiB}, {"vendor", 3_GiB}}))
Yifan Hong537802d2018-08-15 13:15:42 -0700509 << "Should not be able to fit 11GiB data into 10GiB space";
510}
511
Yifan Hongd4db07e2018-10-18 17:46:27 -0700512TEST_P(BootControlAndroidTestP, NotEnoughSpaceForSlot) {
513 SetMetadata(source(),
514 {{S("system"), 1_GiB},
515 {S("vendor"), 1_GiB},
516 {T("system"), 0},
517 {T("vendor"), 0}});
518 EXPECT_FALSE(
519 InitPartitionMetadata(target(), {{"system", 3_GiB}, {"vendor", 3_GiB}}))
520 << "Should not be able to grow over size of super / 2";
521}
522
Yifan Hong1d9077f2018-12-07 12:09:37 -0800523// Test applying retrofit update on a build with dynamic partitions enabled.
524TEST_P(BootControlAndroidTestP,
525 ApplyRetrofitUpdateOnDynamicPartitionsEnabledBuild) {
526 SetMetadata(source(),
527 {{S("system"), 2_GiB},
528 {S("vendor"), 1_GiB},
529 {T("system"), 2_GiB},
530 {T("vendor"), 1_GiB}});
531 // Should not try to unmap any target partition.
532 EXPECT_CALL(dynamicControl(), UnmapPartitionOnDeviceMapper(_, _)).Times(0);
533 // Should not store metadata to target slot.
534 EXPECT_CALL(dynamicControl(),
535 StoreMetadata(GetSuperDevice(target()), _, target()))
536 .Times(0);
537
538 // Not calling through BootControlAndroidTest::InitPartitionMetadata(), since
539 // we don't want any default group in the PartitionMetadata.
540 EXPECT_TRUE(bootctl_.InitPartitionMetadata(target(), {}));
541
542 // Should use dynamic source partitions.
543 EXPECT_CALL(dynamicControl(), GetState(S("system")))
544 .Times(1)
545 .WillOnce(Return(DmDeviceState::ACTIVE));
Dan Albertfcfbda22018-12-14 12:57:55 -0800546 string source_device;
Yifan Hong1d9077f2018-12-07 12:09:37 -0800547 EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &source_device));
548 EXPECT_EQ(GetDmDevice(S("system")), source_device);
549
550 // Should use static target partitions without querying dynamic control.
551 EXPECT_CALL(dynamicControl(), GetState(T("system"))).Times(0);
Dan Albertfcfbda22018-12-14 12:57:55 -0800552 string target_device;
Yifan Hong1d9077f2018-12-07 12:09:37 -0800553 EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &target_device));
554 EXPECT_EQ(GetDevice(T("system")), target_device);
555}
556
Yifan Hongd4db07e2018-10-18 17:46:27 -0700557INSTANTIATE_TEST_CASE_P(BootControlAndroidTest,
Yifan Hong537802d2018-08-15 13:15:42 -0700558 BootControlAndroidTestP,
559 testing::Values(TestParam{0, 1}, TestParam{1, 0}));
560
Yifan Hongd4db07e2018-10-18 17:46:27 -0700561const PartitionSuffixSizes update_sizes_0() {
562 // Initial state is 0 for "other" slot.
Yifan Hong537802d2018-08-15 13:15:42 -0700563 return {
564 {"grown_a", 2_GiB},
565 {"shrunk_a", 1_GiB},
566 {"same_a", 100_MiB},
567 {"deleted_a", 150_MiB},
Yifan Hongd4db07e2018-10-18 17:46:27 -0700568 // no added_a
569 {"grown_b", 200_MiB},
570 // simulate system_other
571 {"shrunk_b", 0},
572 {"same_b", 0},
573 {"deleted_b", 0},
574 // no added_b
575 };
576}
577
578const PartitionSuffixSizes update_sizes_1() {
579 return {
580 {"grown_a", 2_GiB},
581 {"shrunk_a", 1_GiB},
582 {"same_a", 100_MiB},
583 {"deleted_a", 150_MiB},
584 // no added_a
Yifan Hong537802d2018-08-15 13:15:42 -0700585 {"grown_b", 3_GiB},
586 {"shrunk_b", 150_MiB},
587 {"same_b", 100_MiB},
588 {"added_b", 150_MiB},
Yifan Hongd4db07e2018-10-18 17:46:27 -0700589 // no deleted_b
Yifan Hong537802d2018-08-15 13:15:42 -0700590 };
591}
592
Yifan Hongd4db07e2018-10-18 17:46:27 -0700593const PartitionSuffixSizes update_sizes_2() {
594 return {
595 {"grown_a", 4_GiB},
596 {"shrunk_a", 100_MiB},
597 {"same_a", 100_MiB},
598 {"deleted_a", 64_MiB},
599 // no added_a
600 {"grown_b", 3_GiB},
601 {"shrunk_b", 150_MiB},
602 {"same_b", 100_MiB},
603 {"added_b", 150_MiB},
604 // no deleted_b
605 };
Yifan Hong537802d2018-08-15 13:15:42 -0700606}
607
608// Test case for first update after the device is manufactured, in which
609// case the "other" slot is likely of size "0" (except system, which is
610// non-zero because of system_other partition)
611TEST_F(BootControlAndroidTest, SimulatedFirstUpdate) {
612 SetSlots({0, 1});
613
614 SetMetadata(source(), update_sizes_0());
615 SetMetadata(target(), update_sizes_0());
Yifan Hong549eb362018-10-19 15:11:12 -0700616 ExpectStoreMetadata(update_sizes_1());
Yifan Hong1d9077f2018-12-07 12:09:37 -0800617 ExpectUnmap({"grown_b", "shrunk_b", "same_b", "added_b"});
Yifan Hong537802d2018-08-15 13:15:42 -0700618
Yifan Hongd4db07e2018-10-18 17:46:27 -0700619 EXPECT_TRUE(InitPartitionMetadata(target(),
620 {{"grown", 3_GiB},
621 {"shrunk", 150_MiB},
622 {"same", 100_MiB},
623 {"added", 150_MiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700624}
625
626// After first update, test for the second update. In the second update, the
627// "added" partition is deleted and "deleted" partition is re-added.
628TEST_F(BootControlAndroidTest, SimulatedSecondUpdate) {
629 SetSlots({1, 0});
630
631 SetMetadata(source(), update_sizes_1());
632 SetMetadata(target(), update_sizes_0());
633
Yifan Hong549eb362018-10-19 15:11:12 -0700634 ExpectStoreMetadata(update_sizes_2());
Yifan Hong1d9077f2018-12-07 12:09:37 -0800635 ExpectUnmap({"grown_a", "shrunk_a", "same_a", "deleted_a"});
Yifan Hong537802d2018-08-15 13:15:42 -0700636
Yifan Hongd4db07e2018-10-18 17:46:27 -0700637 EXPECT_TRUE(InitPartitionMetadata(target(),
638 {{"grown", 4_GiB},
639 {"shrunk", 100_MiB},
640 {"same", 100_MiB},
641 {"deleted", 64_MiB}}));
Yifan Hong537802d2018-08-15 13:15:42 -0700642}
643
644TEST_F(BootControlAndroidTest, ApplyingToCurrentSlot) {
645 SetSlots({1, 1});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700646 EXPECT_FALSE(InitPartitionMetadata(target(), {}))
Yifan Hong537802d2018-08-15 13:15:42 -0700647 << "Should not be able to apply to current slot.";
648}
649
Yifan Hongd4db07e2018-10-18 17:46:27 -0700650class BootControlAndroidGroupTestP : public BootControlAndroidTestP {
651 public:
652 void SetUp() override {
653 BootControlAndroidTestP::SetUp();
654 SetMetadata(
655 source(),
656 {.groups = {SimpleGroup(S("android"), 3_GiB, S("system"), 2_GiB),
657 SimpleGroup(S("oem"), 2_GiB, S("vendor"), 1_GiB),
658 SimpleGroup(T("android"), 3_GiB, T("system"), 0),
659 SimpleGroup(T("oem"), 2_GiB, T("vendor"), 0)}});
660 }
661
662 // Return a simple group with only one partition.
Dan Albertfcfbda22018-12-14 12:57:55 -0800663 PartitionMetadata::Group SimpleGroup(const string& group,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700664 uint64_t group_size,
Dan Albertfcfbda22018-12-14 12:57:55 -0800665 const string& partition,
Yifan Hongd4db07e2018-10-18 17:46:27 -0700666 uint64_t partition_size) {
667 return {.name = group,
668 .size = group_size,
669 .partitions = {{.name = partition, .size = partition_size}}};
670 }
671
672 void ExpectStoreMetadata(const PartitionMetadata& partition_metadata) {
673 ExpectStoreMetadataMatch(MetadataMatches(partition_metadata));
674 }
675
676 // Expect that target slot is stored with target groups.
677 void ExpectStoreMetadataMatch(
678 const Matcher<MetadataBuilder*>& matcher) override {
679 BootControlAndroidTestP::ExpectStoreMetadataMatch(AllOf(
680 MetadataMatches(PartitionMetadata{
681 .groups = {SimpleGroup(S("android"), 3_GiB, S("system"), 2_GiB),
682 SimpleGroup(S("oem"), 2_GiB, S("vendor"), 1_GiB)}}),
683 matcher));
684 }
685};
686
687// Allow to resize within group.
688TEST_P(BootControlAndroidGroupTestP, ResizeWithinGroup) {
689 ExpectStoreMetadata(PartitionMetadata{
690 .groups = {SimpleGroup(T("android"), 3_GiB, T("system"), 3_GiB),
691 SimpleGroup(T("oem"), 2_GiB, T("vendor"), 2_GiB)}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800692 ExpectUnmap({T("system"), T("vendor")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700693
694 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
695 target(),
696 PartitionMetadata{
697 .groups = {SimpleGroup("android", 3_GiB, "system", 3_GiB),
698 SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}}));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700699}
700
701TEST_P(BootControlAndroidGroupTestP, NotEnoughSpaceForGroup) {
702 EXPECT_FALSE(bootctl_.InitPartitionMetadata(
703 target(),
704 PartitionMetadata{
705 .groups = {SimpleGroup("android", 3_GiB, "system", 1_GiB),
706 SimpleGroup("oem", 2_GiB, "vendor", 3_GiB)}}))
707 << "Should not be able to grow over maximum size of group";
708}
709
710TEST_P(BootControlAndroidGroupTestP, GroupTooBig) {
711 EXPECT_FALSE(bootctl_.InitPartitionMetadata(
712 target(),
713 PartitionMetadata{.groups = {{.name = "android", .size = 3_GiB},
714 {.name = "oem", .size = 3_GiB}}}))
715 << "Should not be able to grow over size of super / 2";
716}
717
718TEST_P(BootControlAndroidGroupTestP, AddPartitionToGroup) {
719 ExpectStoreMetadata(PartitionMetadata{
720 .groups = {
721 {.name = T("android"),
722 .size = 3_GiB,
723 .partitions = {{.name = T("system"), .size = 2_GiB},
724 {.name = T("product_services"), .size = 1_GiB}}}}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800725 ExpectUnmap({T("system"), T("vendor"), T("product_services")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700726
727 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
728 target(),
729 PartitionMetadata{
730 .groups = {
731 {.name = "android",
732 .size = 3_GiB,
733 .partitions = {{.name = "system", .size = 2_GiB},
734 {.name = "product_services", .size = 1_GiB}}},
735 SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}}));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700736}
737
738TEST_P(BootControlAndroidGroupTestP, RemovePartitionFromGroup) {
739 ExpectStoreMetadata(PartitionMetadata{
740 .groups = {{.name = T("android"), .size = 3_GiB, .partitions = {}}}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800741 ExpectUnmap({T("vendor")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700742
743 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
744 target(),
745 PartitionMetadata{
746 .groups = {{.name = "android", .size = 3_GiB, .partitions = {}},
747 SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}}));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700748}
749
750TEST_P(BootControlAndroidGroupTestP, AddGroup) {
751 ExpectStoreMetadata(PartitionMetadata{
752 .groups = {
753 SimpleGroup(T("new_group"), 2_GiB, T("new_partition"), 2_GiB)}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800754 ExpectUnmap({T("system"), T("vendor"), T("new_partition")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700755
756 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
757 target(),
758 PartitionMetadata{
759 .groups = {
760 SimpleGroup("android", 2_GiB, "system", 2_GiB),
761 SimpleGroup("oem", 1_GiB, "vendor", 1_GiB),
762 SimpleGroup("new_group", 2_GiB, "new_partition", 2_GiB)}}));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700763}
764
765TEST_P(BootControlAndroidGroupTestP, RemoveGroup) {
766 ExpectStoreMetadataMatch(Not(HasGroup(T("oem"))));
Yifan Hong1d9077f2018-12-07 12:09:37 -0800767 ExpectUnmap({T("system")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700768 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
769 target(),
770 PartitionMetadata{
771 .groups = {SimpleGroup("android", 2_GiB, "system", 2_GiB)}}));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700772}
773
774TEST_P(BootControlAndroidGroupTestP, ResizeGroup) {
775 ExpectStoreMetadata(PartitionMetadata{
776 .groups = {SimpleGroup(T("android"), 2_GiB, T("system"), 2_GiB),
777 SimpleGroup(T("oem"), 3_GiB, T("vendor"), 3_GiB)}});
Yifan Hong1d9077f2018-12-07 12:09:37 -0800778 ExpectUnmap({T("system"), T("vendor")});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700779
780 EXPECT_TRUE(bootctl_.InitPartitionMetadata(
781 target(),
782 PartitionMetadata{
783 .groups = {SimpleGroup("android", 2_GiB, "system", 2_GiB),
784 SimpleGroup("oem", 3_GiB, "vendor", 3_GiB)}}));
Yifan Hongd4db07e2018-10-18 17:46:27 -0700785}
786
787INSTANTIATE_TEST_CASE_P(BootControlAndroidTest,
788 BootControlAndroidGroupTestP,
789 testing::Values(TestParam{0, 1}, TestParam{1, 0}));
790
Yifan Hong537802d2018-08-15 13:15:42 -0700791} // namespace chromeos_update_engine