Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 1 | // |
| 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 Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 20 | #include <vector> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 21 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 22 | #include <base/logging.h> |
| 23 | #include <base/strings/string_util.h> |
David Anderson | d63cb3c | 2018-10-01 14:15:00 -0700 | [diff] [blame] | 24 | #include <fs_mgr.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 25 | #include <gmock/gmock.h> |
| 26 | #include <gtest/gtest.h> |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 27 | #include <libdm/dm.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 28 | |
| 29 | #include "update_engine/mock_boot_control_hal.h" |
| 30 | #include "update_engine/mock_dynamic_partition_control.h" |
| 31 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 32 | using android::dm::DmDeviceState; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 33 | using android::fs_mgr::MetadataBuilder; |
| 34 | using android::hardware::Void; |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 35 | using std::string; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 36 | using testing::_; |
| 37 | using testing::AnyNumber; |
| 38 | using testing::Contains; |
| 39 | using testing::Eq; |
| 40 | using testing::Invoke; |
| 41 | using testing::Key; |
| 42 | using testing::MakeMatcher; |
| 43 | using testing::Matcher; |
| 44 | using testing::MatcherInterface; |
| 45 | using testing::MatchResultListener; |
| 46 | using testing::NiceMock; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 47 | using testing::Not; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 48 | using testing::Return; |
| 49 | |
| 50 | namespace chromeos_update_engine { |
| 51 | |
| 52 | constexpr const uint32_t kMaxNumSlots = 2; |
| 53 | constexpr const char* kSlotSuffixes[kMaxNumSlots] = {"_a", "_b"}; |
| 54 | constexpr const char* kFakeDevicePath = "/fake/dev/path/"; |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 55 | constexpr const char* kFakeDmDevicePath = "/fake/dm/dev/path/"; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 56 | constexpr const uint32_t kFakeMetadataSize = 65536; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 57 | constexpr const char* kDefaultGroup = "foo"; |
| 58 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 59 | // A map describing the size of each partition. |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 60 | // "{name, size}" |
| 61 | using PartitionSizes = std::map<string, uint64_t>; |
| 62 | |
| 63 | // "{name_a, size}" |
| 64 | using PartitionSuffixSizes = std::map<string, uint64_t>; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 65 | |
| 66 | using PartitionMetadata = BootControlInterface::PartitionMetadata; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 67 | |
| 68 | // C++ standards do not allow uint64_t (aka unsigned long) to be the parameter |
| 69 | // of user-defined literal operators. |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 70 | constexpr unsigned long long operator"" _MiB(unsigned long long x) { // NOLINT |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 71 | return x << 20; |
| 72 | } |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 73 | constexpr unsigned long long operator"" _GiB(unsigned long long x) { // NOLINT |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 74 | return x << 30; |
| 75 | } |
| 76 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 77 | constexpr uint64_t kDefaultGroupSize = 5_GiB; |
| 78 | // Super device size. 1 MiB for metadata. |
| 79 | constexpr uint64_t kDefaultSuperSize = kDefaultGroupSize * 2 + 1_MiB; |
| 80 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 81 | template <typename U, typename V> |
| 82 | std::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 Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 94 | template <typename T> |
| 95 | std::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 | |
| 107 | std::ostream& operator<<(std::ostream& os, |
| 108 | const PartitionMetadata::Partition& p) { |
| 109 | return os << "{" << p.name << ", " << p.size << "}"; |
| 110 | } |
| 111 | |
| 112 | std::ostream& operator<<(std::ostream& os, const PartitionMetadata::Group& g) { |
| 113 | return os << "{" << g.name << ", " << g.size << ", " << g.partitions << "}"; |
| 114 | } |
| 115 | |
| 116 | std::ostream& operator<<(std::ostream& os, const PartitionMetadata& m) { |
| 117 | return os << m.groups; |
| 118 | } |
| 119 | |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 120 | inline string GetDevice(const string& name) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 121 | return kFakeDevicePath + name; |
| 122 | } |
Yifan Hong | 30eb7b5 | 2018-11-20 11:05:46 -0800 | [diff] [blame] | 123 | |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 124 | inline string GetDmDevice(const string& name) { |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 125 | return kFakeDmDevicePath + name; |
| 126 | } |
| 127 | |
Yifan Hong | 30eb7b5 | 2018-11-20 11:05:46 -0800 | [diff] [blame] | 128 | // TODO(elsk): fs_mgr_get_super_partition_name should be mocked. |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 129 | inline string GetSuperDevice(uint32_t slot) { |
Yifan Hong | 30eb7b5 | 2018-11-20 11:05:46 -0800 | [diff] [blame] | 130 | return GetDevice(fs_mgr_get_super_partition_name(slot)); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | struct TestParam { |
| 134 | uint32_t source; |
| 135 | uint32_t target; |
| 136 | }; |
| 137 | std::ostream& operator<<(std::ostream& os, const TestParam& param) { |
| 138 | return os << "{source: " << param.source << ", target:" << param.target |
| 139 | << "}"; |
| 140 | } |
| 141 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 142 | // To support legacy tests, auto-convert {name_a: size} map to |
| 143 | // PartitionMetadata. |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 144 | PartitionMetadata partitionSuffixSizesToMetadata( |
| 145 | const PartitionSuffixSizes& partition_sizes) { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 146 | PartitionMetadata metadata; |
| 147 | for (const char* suffix : kSlotSuffixes) { |
| 148 | metadata.groups.push_back( |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 149 | {string(kDefaultGroup) + suffix, kDefaultGroupSize, {}}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 150 | } |
| 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 Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 165 | PartitionMetadata partitionSizesToMetadata( |
| 166 | const PartitionSizes& partition_sizes) { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 167 | PartitionMetadata metadata; |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 168 | metadata.groups.push_back({string{kDefaultGroup}, kDefaultGroupSize, {}}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 169 | for (const auto& pair : partition_sizes) { |
| 170 | metadata.groups[0].partitions.push_back({pair.first, pair.second}); |
| 171 | } |
| 172 | return metadata; |
| 173 | } |
| 174 | |
| 175 | std::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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 180 | EXPECT_NE(nullptr, builder); |
| 181 | if (builder == nullptr) |
| 182 | return nullptr; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 183 | 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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 189 | } |
| 190 | return builder; |
| 191 | } |
| 192 | |
| 193 | class MetadataMatcher : public MatcherInterface<MetadataBuilder*> { |
| 194 | public: |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 195 | explicit MetadataMatcher(const PartitionSuffixSizes& partition_sizes) |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 196 | : partition_metadata_(partitionSuffixSizesToMetadata(partition_sizes)) {} |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 197 | explicit MetadataMatcher(const PartitionMetadata& partition_metadata) |
| 198 | : partition_metadata_(partition_metadata) {} |
| 199 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 200 | bool MatchAndExplain(MetadataBuilder* metadata, |
| 201 | MatchResultListener* listener) const override { |
| 202 | bool success = true; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 203 | 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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | return success; |
| 230 | } |
| 231 | |
| 232 | void DescribeTo(std::ostream* os) const override { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 233 | *os << "expect: " << partition_metadata_; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void DescribeNegationTo(std::ostream* os) const override { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 237 | *os << "expect not: " << partition_metadata_; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | private: |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 241 | PartitionMetadata partition_metadata_; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | inline Matcher<MetadataBuilder*> MetadataMatches( |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 245 | const PartitionSuffixSizes& partition_sizes) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 246 | return MakeMatcher(new MetadataMatcher(partition_sizes)); |
| 247 | } |
| 248 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 249 | inline Matcher<MetadataBuilder*> MetadataMatches( |
| 250 | const PartitionMetadata& partition_metadata) { |
| 251 | return MakeMatcher(new MetadataMatcher(partition_metadata)); |
| 252 | } |
| 253 | |
| 254 | MATCHER_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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 259 | class 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 Hong | 6e38b35 | 2018-11-19 14:12:37 -0800 | [diff] [blame] | 279 | ON_CALL(dynamicControl(), IsDynamicPartitionsRetrofit()) |
| 280 | .WillByDefault(Return(false)); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 281 | ON_CALL(dynamicControl(), DeviceExists(_)).WillByDefault(Return(true)); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 282 | ON_CALL(dynamicControl(), GetDeviceDir(_)) |
| 283 | .WillByDefault(Invoke([](auto path) { |
| 284 | *path = kFakeDevicePath; |
| 285 | return true; |
| 286 | })); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 287 | ON_CALL(dynamicControl(), GetDmDevicePathByName(_, _)) |
| 288 | .WillByDefault(Invoke([](auto partition_name_suffix, auto device) { |
| 289 | *device = GetDmDevice(partition_name_suffix); |
| 290 | return true; |
| 291 | })); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 292 | } |
| 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 Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 307 | void SetMetadata(uint32_t slot, const PartitionSuffixSizes& sizes) { |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 308 | SetMetadata(slot, partitionSuffixSizesToMetadata(sizes)); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void SetMetadata(uint32_t slot, const PartitionMetadata& metadata) { |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 312 | EXPECT_CALL(dynamicControl(), |
Yifan Hong | 30eb7b5 | 2018-11-20 11:05:46 -0800 | [diff] [blame] | 313 | LoadMetadataBuilder(GetSuperDevice(slot), slot, _)) |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 314 | .Times(AnyNumber()) |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 315 | .WillRepeatedly(Invoke([metadata](auto, auto, auto) { |
| 316 | return NewFakeMetadata(metadata); |
| 317 | })); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 320 | // Expect that UnmapPartitionOnDeviceMapper is called on target() metadata |
| 321 | // slot with each partition in |partitions|. |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 322 | void ExpectUnmap(const std::set<string>& partitions) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 323 | // 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 Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 336 | void ExpectDevicesAreMapped(const std::set<string>& partitions) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 337 | 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 Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 344 | void ExpectStoreMetadata(const PartitionSuffixSizes& partition_sizes) { |
Yifan Hong | 549eb36 | 2018-10-19 15:11:12 -0700 | [diff] [blame] | 345 | ExpectStoreMetadataMatch(MetadataMatches(partition_sizes)); |
| 346 | } |
| 347 | |
| 348 | virtual void ExpectStoreMetadataMatch( |
| 349 | const Matcher<MetadataBuilder*>& matcher) { |
| 350 | EXPECT_CALL(dynamicControl(), |
Yifan Hong | 30eb7b5 | 2018-11-20 11:05:46 -0800 | [diff] [blame] | 351 | StoreMetadata(GetSuperDevice(target()), matcher, target())) |
Yifan Hong | 549eb36 | 2018-10-19 15:11:12 -0700 | [diff] [blame] | 352 | .WillOnce(Return(true)); |
| 353 | } |
| 354 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 355 | uint32_t source() { return slots_.source; } |
| 356 | |
| 357 | uint32_t target() { return slots_.target; } |
| 358 | |
| 359 | // Return partition names with suffix of source(). |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 360 | string S(const string& name) { return name + kSlotSuffixes[source()]; } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 361 | |
| 362 | // Return partition names with suffix of target(). |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 363 | string T(const string& name) { return name + kSlotSuffixes[target()]; } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 364 | |
| 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 Hong | 30eb7b5 | 2018-11-20 11:05:46 -0800 | [diff] [blame] | 373 | EXPECT_CALL(dynamicControl(), |
| 374 | StoreMetadata(GetSuperDevice(source()), _, source())) |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 375 | .Times(0); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 376 | // Should not load metadata from target slot. |
| 377 | EXPECT_CALL(dynamicControl(), |
Yifan Hong | 30eb7b5 | 2018-11-20 11:05:46 -0800 | [diff] [blame] | 378 | LoadMetadataBuilder(GetSuperDevice(target()), target(), _)) |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 379 | .Times(0); |
| 380 | } |
| 381 | |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 382 | bool InitPartitionMetadata(uint32_t slot, |
| 383 | PartitionSizes partition_sizes, |
| 384 | bool update_metadata = true) { |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 385 | auto m = partitionSizesToMetadata(partition_sizes); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 386 | LOG(INFO) << m; |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 387 | return bootctl_.InitPartitionMetadata(slot, m, update_metadata); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | BootControlAndroid bootctl_; // BootControlAndroid under test. |
| 391 | TestParam slots_; |
| 392 | // mapped devices through MapPartitionOnDeviceMapper. |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 393 | std::map<string, string> mapped_devices_; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | class 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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 406 | // Test resize case. Grow if target metadata contains a partition with a size |
| 407 | // less than expected. |
| 408 | TEST_P(BootControlAndroidTestP, NeedGrowIfSizeNotMatchWhenResizing) { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 409 | SetMetadata(source(), |
| 410 | {{S("system"), 2_GiB}, |
| 411 | {S("vendor"), 1_GiB}, |
| 412 | {T("system"), 2_GiB}, |
| 413 | {T("vendor"), 1_GiB}}); |
Yifan Hong | 549eb36 | 2018-10-19 15:11:12 -0700 | [diff] [blame] | 414 | ExpectStoreMetadata({{S("system"), 2_GiB}, |
| 415 | {S("vendor"), 1_GiB}, |
| 416 | {T("system"), 3_GiB}, |
| 417 | {T("vendor"), 1_GiB}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 418 | ExpectUnmap({T("system"), T("vendor")}); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 419 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 420 | EXPECT_TRUE( |
| 421 | InitPartitionMetadata(target(), {{"system", 3_GiB}, {"vendor", 1_GiB}})); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Test resize case. Shrink if target metadata contains a partition with a size |
| 425 | // greater than expected. |
| 426 | TEST_P(BootControlAndroidTestP, NeedShrinkIfSizeNotMatchWhenResizing) { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 427 | SetMetadata(source(), |
| 428 | {{S("system"), 2_GiB}, |
| 429 | {S("vendor"), 1_GiB}, |
| 430 | {T("system"), 2_GiB}, |
| 431 | {T("vendor"), 1_GiB}}); |
Yifan Hong | 549eb36 | 2018-10-19 15:11:12 -0700 | [diff] [blame] | 432 | ExpectStoreMetadata({{S("system"), 2_GiB}, |
| 433 | {S("vendor"), 1_GiB}, |
| 434 | {T("system"), 2_GiB}, |
| 435 | {T("vendor"), 150_MiB}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 436 | ExpectUnmap({T("system"), T("vendor")}); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 437 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 438 | EXPECT_TRUE(InitPartitionMetadata(target(), |
| 439 | {{"system", 2_GiB}, {"vendor", 150_MiB}})); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // Test adding partitions on the first run. |
| 443 | TEST_P(BootControlAndroidTestP, AddPartitionToEmptyMetadata) { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 444 | SetMetadata(source(), PartitionSuffixSizes{}); |
Yifan Hong | 549eb36 | 2018-10-19 15:11:12 -0700 | [diff] [blame] | 445 | ExpectStoreMetadata({{T("system"), 2_GiB}, {T("vendor"), 1_GiB}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 446 | ExpectUnmap({T("system"), T("vendor")}); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 447 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 448 | EXPECT_TRUE( |
| 449 | InitPartitionMetadata(target(), {{"system", 2_GiB}, {"vendor", 1_GiB}})); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Test subsequent add case. |
| 453 | TEST_P(BootControlAndroidTestP, AddAdditionalPartition) { |
| 454 | SetMetadata(source(), {{S("system"), 2_GiB}, {T("system"), 2_GiB}}); |
Yifan Hong | 549eb36 | 2018-10-19 15:11:12 -0700 | [diff] [blame] | 455 | ExpectStoreMetadata( |
| 456 | {{S("system"), 2_GiB}, {T("system"), 2_GiB}, {T("vendor"), 1_GiB}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 457 | ExpectUnmap({T("system"), T("vendor")}); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 458 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 459 | EXPECT_TRUE( |
| 460 | InitPartitionMetadata(target(), {{"system", 2_GiB}, {"vendor", 1_GiB}})); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // Test delete one partition. |
| 464 | TEST_P(BootControlAndroidTestP, DeletePartition) { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 465 | 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 Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 473 | ExpectUnmap({T("system")}); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 474 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 475 | EXPECT_TRUE(InitPartitionMetadata(target(), {{"system", 2_GiB}})); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // Test delete all partitions. |
| 479 | TEST_P(BootControlAndroidTestP, DeleteAll) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 480 | SetMetadata(source(), |
| 481 | {{S("system"), 2_GiB}, |
| 482 | {S("vendor"), 1_GiB}, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 483 | {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 Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | // Test corrupt source metadata case. |
| 491 | TEST_P(BootControlAndroidTestP, CorruptedSourceMetadata) { |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 492 | EXPECT_CALL(dynamicControl(), |
Yifan Hong | 30eb7b5 | 2018-11-20 11:05:46 -0800 | [diff] [blame] | 493 | LoadMetadataBuilder(GetSuperDevice(source()), source(), _)) |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 494 | .WillOnce(Invoke([](auto, auto, auto) { return nullptr; })); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 495 | ExpectUnmap({T("system")}); |
| 496 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 497 | EXPECT_FALSE(InitPartitionMetadata(target(), {{"system", 1_GiB}})) |
| 498 | << "Should not be able to continue with corrupt source metadata"; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Test that InitPartitionMetadata fail if there is not enough space on the |
| 502 | // device. |
| 503 | TEST_P(BootControlAndroidTestP, NotEnoughSpace) { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 504 | 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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 511 | << "Should not be able to fit 11GiB data into 10GiB space"; |
| 512 | } |
| 513 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 514 | TEST_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 Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 525 | // Test applying retrofit update on a build with dynamic partitions enabled. |
| 526 | TEST_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 Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 542 | EXPECT_TRUE(bootctl_.InitPartitionMetadata(target(), {}, true)); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 543 | |
| 544 | // Should use dynamic source partitions. |
| 545 | EXPECT_CALL(dynamicControl(), GetState(S("system"))) |
| 546 | .Times(1) |
| 547 | .WillOnce(Return(DmDeviceState::ACTIVE)); |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 548 | string system_device; |
| 549 | EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &system_device)); |
| 550 | EXPECT_EQ(GetDmDevice(S("system")), system_device); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 551 | |
| 552 | // Should use static target partitions without querying dynamic control. |
| 553 | EXPECT_CALL(dynamicControl(), GetState(T("system"))).Times(0); |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 554 | 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 | |
| 568 | TEST_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"))) |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame^] | 596 | .Times(AnyNumber()) |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 597 | .WillOnce(Return(DmDeviceState::ACTIVE)); |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame^] | 598 | EXPECT_CALL(dynamicControl(), |
| 599 | MapPartitionOnDeviceMapper( |
| 600 | GetSuperDevice(target()), T("system"), target(), _, _)) |
| 601 | .Times(AnyNumber()) |
| 602 | .WillRepeatedly( |
| 603 | Invoke([](const auto&, const auto& name, auto, auto, auto* device) { |
| 604 | *device = "/fake/remapped/" + name; |
| 605 | return true; |
| 606 | })); |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 607 | EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &system_device)); |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame^] | 608 | EXPECT_EQ("/fake/remapped/" + T("system"), system_device); |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 609 | |
| 610 | // Static partition "bar". |
| 611 | EXPECT_CALL(dynamicControl(), GetState(S("bar"))).Times(0); |
| 612 | std::string bar_device; |
| 613 | EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", source(), &bar_device)); |
| 614 | EXPECT_EQ(GetDevice(S("bar")), bar_device); |
| 615 | |
| 616 | EXPECT_CALL(dynamicControl(), GetState(T("bar"))).Times(0); |
| 617 | EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", target(), &bar_device)); |
| 618 | EXPECT_EQ(GetDevice(T("bar")), bar_device); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 621 | INSTANTIATE_TEST_CASE_P(BootControlAndroidTest, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 622 | BootControlAndroidTestP, |
| 623 | testing::Values(TestParam{0, 1}, TestParam{1, 0})); |
| 624 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 625 | const PartitionSuffixSizes update_sizes_0() { |
| 626 | // Initial state is 0 for "other" slot. |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 627 | return { |
| 628 | {"grown_a", 2_GiB}, |
| 629 | {"shrunk_a", 1_GiB}, |
| 630 | {"same_a", 100_MiB}, |
| 631 | {"deleted_a", 150_MiB}, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 632 | // no added_a |
| 633 | {"grown_b", 200_MiB}, |
| 634 | // simulate system_other |
| 635 | {"shrunk_b", 0}, |
| 636 | {"same_b", 0}, |
| 637 | {"deleted_b", 0}, |
| 638 | // no added_b |
| 639 | }; |
| 640 | } |
| 641 | |
| 642 | const PartitionSuffixSizes update_sizes_1() { |
| 643 | return { |
| 644 | {"grown_a", 2_GiB}, |
| 645 | {"shrunk_a", 1_GiB}, |
| 646 | {"same_a", 100_MiB}, |
| 647 | {"deleted_a", 150_MiB}, |
| 648 | // no added_a |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 649 | {"grown_b", 3_GiB}, |
| 650 | {"shrunk_b", 150_MiB}, |
| 651 | {"same_b", 100_MiB}, |
| 652 | {"added_b", 150_MiB}, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 653 | // no deleted_b |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 654 | }; |
| 655 | } |
| 656 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 657 | const PartitionSuffixSizes update_sizes_2() { |
| 658 | return { |
| 659 | {"grown_a", 4_GiB}, |
| 660 | {"shrunk_a", 100_MiB}, |
| 661 | {"same_a", 100_MiB}, |
| 662 | {"deleted_a", 64_MiB}, |
| 663 | // no added_a |
| 664 | {"grown_b", 3_GiB}, |
| 665 | {"shrunk_b", 150_MiB}, |
| 666 | {"same_b", 100_MiB}, |
| 667 | {"added_b", 150_MiB}, |
| 668 | // no deleted_b |
| 669 | }; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | // Test case for first update after the device is manufactured, in which |
| 673 | // case the "other" slot is likely of size "0" (except system, which is |
| 674 | // non-zero because of system_other partition) |
| 675 | TEST_F(BootControlAndroidTest, SimulatedFirstUpdate) { |
| 676 | SetSlots({0, 1}); |
| 677 | |
| 678 | SetMetadata(source(), update_sizes_0()); |
| 679 | SetMetadata(target(), update_sizes_0()); |
Yifan Hong | 549eb36 | 2018-10-19 15:11:12 -0700 | [diff] [blame] | 680 | ExpectStoreMetadata(update_sizes_1()); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 681 | ExpectUnmap({"grown_b", "shrunk_b", "same_b", "added_b"}); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 682 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 683 | EXPECT_TRUE(InitPartitionMetadata(target(), |
| 684 | {{"grown", 3_GiB}, |
| 685 | {"shrunk", 150_MiB}, |
| 686 | {"same", 100_MiB}, |
| 687 | {"added", 150_MiB}})); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | // After first update, test for the second update. In the second update, the |
| 691 | // "added" partition is deleted and "deleted" partition is re-added. |
| 692 | TEST_F(BootControlAndroidTest, SimulatedSecondUpdate) { |
| 693 | SetSlots({1, 0}); |
| 694 | |
| 695 | SetMetadata(source(), update_sizes_1()); |
| 696 | SetMetadata(target(), update_sizes_0()); |
| 697 | |
Yifan Hong | 549eb36 | 2018-10-19 15:11:12 -0700 | [diff] [blame] | 698 | ExpectStoreMetadata(update_sizes_2()); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 699 | ExpectUnmap({"grown_a", "shrunk_a", "same_a", "deleted_a"}); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 700 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 701 | EXPECT_TRUE(InitPartitionMetadata(target(), |
| 702 | {{"grown", 4_GiB}, |
| 703 | {"shrunk", 100_MiB}, |
| 704 | {"same", 100_MiB}, |
| 705 | {"deleted", 64_MiB}})); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | TEST_F(BootControlAndroidTest, ApplyingToCurrentSlot) { |
| 709 | SetSlots({1, 1}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 710 | EXPECT_FALSE(InitPartitionMetadata(target(), {})) |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 711 | << "Should not be able to apply to current slot."; |
| 712 | } |
| 713 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 714 | class BootControlAndroidGroupTestP : public BootControlAndroidTestP { |
| 715 | public: |
| 716 | void SetUp() override { |
| 717 | BootControlAndroidTestP::SetUp(); |
| 718 | SetMetadata( |
| 719 | source(), |
| 720 | {.groups = {SimpleGroup(S("android"), 3_GiB, S("system"), 2_GiB), |
| 721 | SimpleGroup(S("oem"), 2_GiB, S("vendor"), 1_GiB), |
| 722 | SimpleGroup(T("android"), 3_GiB, T("system"), 0), |
| 723 | SimpleGroup(T("oem"), 2_GiB, T("vendor"), 0)}}); |
| 724 | } |
| 725 | |
| 726 | // Return a simple group with only one partition. |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 727 | PartitionMetadata::Group SimpleGroup(const string& group, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 728 | uint64_t group_size, |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 729 | const string& partition, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 730 | uint64_t partition_size) { |
| 731 | return {.name = group, |
| 732 | .size = group_size, |
| 733 | .partitions = {{.name = partition, .size = partition_size}}}; |
| 734 | } |
| 735 | |
| 736 | void ExpectStoreMetadata(const PartitionMetadata& partition_metadata) { |
| 737 | ExpectStoreMetadataMatch(MetadataMatches(partition_metadata)); |
| 738 | } |
| 739 | |
| 740 | // Expect that target slot is stored with target groups. |
| 741 | void ExpectStoreMetadataMatch( |
| 742 | const Matcher<MetadataBuilder*>& matcher) override { |
| 743 | BootControlAndroidTestP::ExpectStoreMetadataMatch(AllOf( |
| 744 | MetadataMatches(PartitionMetadata{ |
| 745 | .groups = {SimpleGroup(S("android"), 3_GiB, S("system"), 2_GiB), |
| 746 | SimpleGroup(S("oem"), 2_GiB, S("vendor"), 1_GiB)}}), |
| 747 | matcher)); |
| 748 | } |
| 749 | }; |
| 750 | |
| 751 | // Allow to resize within group. |
| 752 | TEST_P(BootControlAndroidGroupTestP, ResizeWithinGroup) { |
| 753 | ExpectStoreMetadata(PartitionMetadata{ |
| 754 | .groups = {SimpleGroup(T("android"), 3_GiB, T("system"), 3_GiB), |
| 755 | SimpleGroup(T("oem"), 2_GiB, T("vendor"), 2_GiB)}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 756 | ExpectUnmap({T("system"), T("vendor")}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 757 | |
| 758 | EXPECT_TRUE(bootctl_.InitPartitionMetadata( |
| 759 | target(), |
| 760 | PartitionMetadata{ |
| 761 | .groups = {SimpleGroup("android", 3_GiB, "system", 3_GiB), |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 762 | SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}}, |
| 763 | true)); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | TEST_P(BootControlAndroidGroupTestP, NotEnoughSpaceForGroup) { |
| 767 | EXPECT_FALSE(bootctl_.InitPartitionMetadata( |
| 768 | target(), |
| 769 | PartitionMetadata{ |
| 770 | .groups = {SimpleGroup("android", 3_GiB, "system", 1_GiB), |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 771 | SimpleGroup("oem", 2_GiB, "vendor", 3_GiB)}}, |
| 772 | true)) |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 773 | << "Should not be able to grow over maximum size of group"; |
| 774 | } |
| 775 | |
| 776 | TEST_P(BootControlAndroidGroupTestP, GroupTooBig) { |
| 777 | EXPECT_FALSE(bootctl_.InitPartitionMetadata( |
| 778 | target(), |
| 779 | PartitionMetadata{.groups = {{.name = "android", .size = 3_GiB}, |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 780 | {.name = "oem", .size = 3_GiB}}}, |
| 781 | true)) |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 782 | << "Should not be able to grow over size of super / 2"; |
| 783 | } |
| 784 | |
| 785 | TEST_P(BootControlAndroidGroupTestP, AddPartitionToGroup) { |
| 786 | ExpectStoreMetadata(PartitionMetadata{ |
| 787 | .groups = { |
| 788 | {.name = T("android"), |
| 789 | .size = 3_GiB, |
| 790 | .partitions = {{.name = T("system"), .size = 2_GiB}, |
| 791 | {.name = T("product_services"), .size = 1_GiB}}}}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 792 | ExpectUnmap({T("system"), T("vendor"), T("product_services")}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 793 | |
| 794 | EXPECT_TRUE(bootctl_.InitPartitionMetadata( |
| 795 | target(), |
| 796 | PartitionMetadata{ |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 797 | .groups = {{.name = "android", |
| 798 | .size = 3_GiB, |
| 799 | .partitions = {{.name = "system", .size = 2_GiB}, |
| 800 | {.name = "product_services", |
| 801 | .size = 1_GiB}}}, |
| 802 | SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}}, |
| 803 | true)); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | TEST_P(BootControlAndroidGroupTestP, RemovePartitionFromGroup) { |
| 807 | ExpectStoreMetadata(PartitionMetadata{ |
| 808 | .groups = {{.name = T("android"), .size = 3_GiB, .partitions = {}}}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 809 | ExpectUnmap({T("vendor")}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 810 | |
| 811 | EXPECT_TRUE(bootctl_.InitPartitionMetadata( |
| 812 | target(), |
| 813 | PartitionMetadata{ |
| 814 | .groups = {{.name = "android", .size = 3_GiB, .partitions = {}}, |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 815 | SimpleGroup("oem", 2_GiB, "vendor", 2_GiB)}}, |
| 816 | true)); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | TEST_P(BootControlAndroidGroupTestP, AddGroup) { |
| 820 | ExpectStoreMetadata(PartitionMetadata{ |
| 821 | .groups = { |
| 822 | SimpleGroup(T("new_group"), 2_GiB, T("new_partition"), 2_GiB)}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 823 | ExpectUnmap({T("system"), T("vendor"), T("new_partition")}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 824 | |
| 825 | EXPECT_TRUE(bootctl_.InitPartitionMetadata( |
| 826 | target(), |
| 827 | PartitionMetadata{ |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 828 | .groups = {SimpleGroup("android", 2_GiB, "system", 2_GiB), |
| 829 | SimpleGroup("oem", 1_GiB, "vendor", 1_GiB), |
| 830 | SimpleGroup("new_group", 2_GiB, "new_partition", 2_GiB)}}, |
| 831 | true)); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | TEST_P(BootControlAndroidGroupTestP, RemoveGroup) { |
| 835 | ExpectStoreMetadataMatch(Not(HasGroup(T("oem")))); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 836 | ExpectUnmap({T("system")}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 837 | EXPECT_TRUE(bootctl_.InitPartitionMetadata( |
| 838 | target(), |
| 839 | PartitionMetadata{ |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 840 | .groups = {SimpleGroup("android", 2_GiB, "system", 2_GiB)}}, |
| 841 | true)); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | TEST_P(BootControlAndroidGroupTestP, ResizeGroup) { |
| 845 | ExpectStoreMetadata(PartitionMetadata{ |
| 846 | .groups = {SimpleGroup(T("android"), 2_GiB, T("system"), 2_GiB), |
| 847 | SimpleGroup(T("oem"), 3_GiB, T("vendor"), 3_GiB)}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 848 | ExpectUnmap({T("system"), T("vendor")}); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 849 | |
| 850 | EXPECT_TRUE(bootctl_.InitPartitionMetadata( |
| 851 | target(), |
| 852 | PartitionMetadata{ |
| 853 | .groups = {SimpleGroup("android", 2_GiB, "system", 2_GiB), |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 854 | SimpleGroup("oem", 3_GiB, "vendor", 3_GiB)}}, |
| 855 | true)); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | INSTANTIATE_TEST_CASE_P(BootControlAndroidTest, |
| 859 | BootControlAndroidGroupTestP, |
| 860 | testing::Values(TestParam{0, 1}, TestParam{1, 0})); |
| 861 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 862 | } // namespace chromeos_update_engine |