Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2019 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #include "update_engine/dynamic_partition_control_android.h" |
| 18 | |
| 19 | #include <set> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <base/logging.h> |
| 23 | #include <base/strings/string_util.h> |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #include "update_engine/dynamic_partition_test_utils.h" |
| 28 | #include "update_engine/mock_dynamic_partition_control.h" |
| 29 | |
| 30 | using std::string; |
| 31 | using testing::_; |
| 32 | using testing::AnyNumber; |
| 33 | using testing::Invoke; |
| 34 | using testing::NiceMock; |
| 35 | using testing::Not; |
| 36 | using testing::Return; |
| 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | |
| 40 | class DynamicPartitionControlAndroidTest : public ::testing::Test { |
| 41 | public: |
| 42 | void SetUp() override { |
| 43 | module_ = std::make_unique<NiceMock<MockDynamicPartitionControlAndroid>>(); |
| 44 | |
| 45 | ON_CALL(dynamicControl(), GetDynamicPartitionsFeatureFlag()) |
| 46 | .WillByDefault(Return(FeatureFlag(FeatureFlag::Value::LAUNCH))); |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 47 | ON_CALL(dynamicControl(), GetVirtualAbFeatureFlag()) |
| 48 | .WillByDefault(Return(FeatureFlag(FeatureFlag::Value::NONE))); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 49 | |
| 50 | ON_CALL(dynamicControl(), GetDeviceDir(_)) |
| 51 | .WillByDefault(Invoke([](auto path) { |
| 52 | *path = kFakeDevicePath; |
| 53 | return true; |
| 54 | })); |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 55 | |
| 56 | ON_CALL(dynamicControl(), GetSuperPartitionName(_)) |
| 57 | .WillByDefault(Return(kFakeSuper)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Return the mocked DynamicPartitionControlInterface. |
| 61 | NiceMock<MockDynamicPartitionControlAndroid>& dynamicControl() { |
| 62 | return static_cast<NiceMock<MockDynamicPartitionControlAndroid>&>(*module_); |
| 63 | } |
| 64 | |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 65 | std::string GetSuperDevice(uint32_t slot) { |
| 66 | return GetDevice(dynamicControl().GetSuperPartitionName(slot)); |
| 67 | } |
| 68 | |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 69 | uint32_t source() { return slots_.source; } |
| 70 | uint32_t target() { return slots_.target; } |
| 71 | |
| 72 | // Return partition names with suffix of source(). |
| 73 | std::string S(const std::string& name) { |
| 74 | return name + kSlotSuffixes[source()]; |
| 75 | } |
| 76 | |
| 77 | // Return partition names with suffix of target(). |
| 78 | std::string T(const std::string& name) { |
| 79 | return name + kSlotSuffixes[target()]; |
| 80 | } |
| 81 | |
| 82 | // Set the fake metadata to return when LoadMetadataBuilder is called on |
| 83 | // |slot|. |
| 84 | void SetMetadata(uint32_t slot, const PartitionSuffixSizes& sizes) { |
| 85 | EXPECT_CALL(dynamicControl(), |
| 86 | LoadMetadataBuilder(GetSuperDevice(slot), slot, _)) |
| 87 | .Times(AnyNumber()) |
| 88 | .WillRepeatedly(Invoke([sizes](auto, auto, auto) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 89 | return NewFakeMetadata(PartitionSuffixSizesToManifest(sizes)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 90 | })); |
| 91 | } |
| 92 | |
| 93 | void ExpectStoreMetadata(const PartitionSuffixSizes& partition_sizes) { |
| 94 | EXPECT_CALL(dynamicControl(), |
| 95 | StoreMetadata(GetSuperDevice(target()), |
| 96 | MetadataMatches(partition_sizes), |
| 97 | target())) |
| 98 | .WillOnce(Return(true)); |
| 99 | } |
| 100 | |
| 101 | // Expect that UnmapPartitionOnDeviceMapper is called on target() metadata |
| 102 | // slot with each partition in |partitions|. |
| 103 | void ExpectUnmap(const std::set<std::string>& partitions) { |
| 104 | // Error when UnmapPartitionOnDeviceMapper is called on unknown arguments. |
| 105 | ON_CALL(dynamicControl(), UnmapPartitionOnDeviceMapper(_)) |
| 106 | .WillByDefault(Return(false)); |
| 107 | |
| 108 | for (const auto& partition : partitions) { |
| 109 | EXPECT_CALL(dynamicControl(), UnmapPartitionOnDeviceMapper(partition)) |
| 110 | .WillOnce(Return(true)); |
| 111 | } |
| 112 | } |
| 113 | bool PreparePartitionsForUpdate(const PartitionSizes& partition_sizes) { |
| 114 | return dynamicControl().PreparePartitionsForUpdate( |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 115 | source(), target(), PartitionSizesToManifest(partition_sizes), true); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 116 | } |
| 117 | void SetSlots(const TestParam& slots) { slots_ = slots; } |
| 118 | |
| 119 | struct Listener : public ::testing::MatchResultListener { |
| 120 | explicit Listener(std::ostream* os) : MatchResultListener(os) {} |
| 121 | }; |
| 122 | |
| 123 | testing::AssertionResult UpdatePartitionMetadata( |
| 124 | const PartitionSuffixSizes& source_metadata, |
| 125 | const PartitionSizes& update_metadata, |
| 126 | const PartitionSuffixSizes& expected) { |
| 127 | return UpdatePartitionMetadata( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 128 | PartitionSuffixSizesToManifest(source_metadata), |
| 129 | PartitionSizesToManifest(update_metadata), |
| 130 | PartitionSuffixSizesToManifest(expected)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 131 | } |
| 132 | testing::AssertionResult UpdatePartitionMetadata( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 133 | const DeltaArchiveManifest& source_manifest, |
| 134 | const DeltaArchiveManifest& update_manifest, |
| 135 | const DeltaArchiveManifest& expected) { |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 136 | return UpdatePartitionMetadata( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 137 | source_manifest, update_manifest, MetadataMatches(expected)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 138 | } |
| 139 | testing::AssertionResult UpdatePartitionMetadata( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 140 | const DeltaArchiveManifest& source_manifest, |
| 141 | const DeltaArchiveManifest& update_manifest, |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 142 | const Matcher<MetadataBuilder*>& matcher) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 143 | auto super_metadata = NewFakeMetadata(source_manifest); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 144 | if (!module_->UpdatePartitionMetadata( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 145 | super_metadata.get(), target(), update_manifest)) { |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 146 | return testing::AssertionFailure() |
| 147 | << "UpdatePartitionMetadataInternal failed"; |
| 148 | } |
| 149 | std::stringstream ss; |
| 150 | Listener listener(&ss); |
| 151 | if (matcher.MatchAndExplain(super_metadata.get(), &listener)) { |
| 152 | return testing::AssertionSuccess() << ss.str(); |
| 153 | } else { |
| 154 | return testing::AssertionFailure() << ss.str(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | std::unique_ptr<DynamicPartitionControlAndroid> module_; |
| 159 | TestParam slots_; |
| 160 | }; |
| 161 | |
| 162 | class DynamicPartitionControlAndroidTestP |
| 163 | : public DynamicPartitionControlAndroidTest, |
| 164 | public ::testing::WithParamInterface<TestParam> { |
| 165 | public: |
| 166 | void SetUp() override { |
| 167 | DynamicPartitionControlAndroidTest::SetUp(); |
| 168 | SetSlots(GetParam()); |
| 169 | } |
| 170 | }; |
| 171 | |
| 172 | // Test resize case. Grow if target metadata contains a partition with a size |
| 173 | // less than expected. |
| 174 | TEST_P(DynamicPartitionControlAndroidTestP, |
| 175 | NeedGrowIfSizeNotMatchWhenResizing) { |
| 176 | PartitionSuffixSizes source_metadata{{S("system"), 2_GiB}, |
| 177 | {S("vendor"), 1_GiB}, |
| 178 | {T("system"), 2_GiB}, |
| 179 | {T("vendor"), 1_GiB}}; |
| 180 | PartitionSuffixSizes expected{{S("system"), 2_GiB}, |
| 181 | {S("vendor"), 1_GiB}, |
| 182 | {T("system"), 3_GiB}, |
| 183 | {T("vendor"), 1_GiB}}; |
| 184 | PartitionSizes update_metadata{{"system", 3_GiB}, {"vendor", 1_GiB}}; |
| 185 | EXPECT_TRUE( |
| 186 | UpdatePartitionMetadata(source_metadata, update_metadata, expected)); |
| 187 | } |
| 188 | |
| 189 | // Test resize case. Shrink if target metadata contains a partition with a size |
| 190 | // greater than expected. |
| 191 | TEST_P(DynamicPartitionControlAndroidTestP, |
| 192 | NeedShrinkIfSizeNotMatchWhenResizing) { |
| 193 | PartitionSuffixSizes source_metadata{{S("system"), 2_GiB}, |
| 194 | {S("vendor"), 1_GiB}, |
| 195 | {T("system"), 2_GiB}, |
| 196 | {T("vendor"), 1_GiB}}; |
| 197 | PartitionSuffixSizes expected{{S("system"), 2_GiB}, |
| 198 | {S("vendor"), 1_GiB}, |
| 199 | {T("system"), 2_GiB}, |
| 200 | {T("vendor"), 150_MiB}}; |
| 201 | PartitionSizes update_metadata{{"system", 2_GiB}, {"vendor", 150_MiB}}; |
| 202 | EXPECT_TRUE( |
| 203 | UpdatePartitionMetadata(source_metadata, update_metadata, expected)); |
| 204 | } |
| 205 | |
| 206 | // Test adding partitions on the first run. |
| 207 | TEST_P(DynamicPartitionControlAndroidTestP, AddPartitionToEmptyMetadata) { |
| 208 | PartitionSuffixSizes source_metadata{}; |
| 209 | PartitionSuffixSizes expected{{T("system"), 2_GiB}, {T("vendor"), 1_GiB}}; |
| 210 | PartitionSizes update_metadata{{"system", 2_GiB}, {"vendor", 1_GiB}}; |
| 211 | EXPECT_TRUE( |
| 212 | UpdatePartitionMetadata(source_metadata, update_metadata, expected)); |
| 213 | } |
| 214 | |
| 215 | // Test subsequent add case. |
| 216 | TEST_P(DynamicPartitionControlAndroidTestP, AddAdditionalPartition) { |
| 217 | PartitionSuffixSizes source_metadata{{S("system"), 2_GiB}, |
| 218 | {T("system"), 2_GiB}}; |
| 219 | PartitionSuffixSizes expected{ |
| 220 | {S("system"), 2_GiB}, {T("system"), 2_GiB}, {T("vendor"), 1_GiB}}; |
| 221 | PartitionSizes update_metadata{{"system", 2_GiB}, {"vendor", 1_GiB}}; |
| 222 | EXPECT_TRUE( |
| 223 | UpdatePartitionMetadata(source_metadata, update_metadata, expected)); |
| 224 | } |
| 225 | |
| 226 | // Test delete one partition. |
| 227 | TEST_P(DynamicPartitionControlAndroidTestP, DeletePartition) { |
| 228 | PartitionSuffixSizes source_metadata{{S("system"), 2_GiB}, |
| 229 | {S("vendor"), 1_GiB}, |
| 230 | {T("system"), 2_GiB}, |
| 231 | {T("vendor"), 1_GiB}}; |
| 232 | // No T("vendor") |
| 233 | PartitionSuffixSizes expected{ |
| 234 | {S("system"), 2_GiB}, {S("vendor"), 1_GiB}, {T("system"), 2_GiB}}; |
| 235 | PartitionSizes update_metadata{{"system", 2_GiB}}; |
| 236 | EXPECT_TRUE( |
| 237 | UpdatePartitionMetadata(source_metadata, update_metadata, expected)); |
| 238 | } |
| 239 | |
| 240 | // Test delete all partitions. |
| 241 | TEST_P(DynamicPartitionControlAndroidTestP, DeleteAll) { |
| 242 | PartitionSuffixSizes source_metadata{{S("system"), 2_GiB}, |
| 243 | {S("vendor"), 1_GiB}, |
| 244 | {T("system"), 2_GiB}, |
| 245 | {T("vendor"), 1_GiB}}; |
| 246 | PartitionSuffixSizes expected{{S("system"), 2_GiB}, {S("vendor"), 1_GiB}}; |
| 247 | PartitionSizes update_metadata{}; |
| 248 | EXPECT_TRUE( |
| 249 | UpdatePartitionMetadata(source_metadata, update_metadata, expected)); |
| 250 | } |
| 251 | |
| 252 | // Test corrupt source metadata case. |
| 253 | TEST_P(DynamicPartitionControlAndroidTestP, CorruptedSourceMetadata) { |
| 254 | EXPECT_CALL(dynamicControl(), |
| 255 | LoadMetadataBuilder(GetSuperDevice(source()), source(), _)) |
| 256 | .WillOnce(Invoke([](auto, auto, auto) { return nullptr; })); |
| 257 | ExpectUnmap({T("system")}); |
| 258 | |
| 259 | EXPECT_FALSE(PreparePartitionsForUpdate({{"system", 1_GiB}})) |
| 260 | << "Should not be able to continue with corrupt source metadata"; |
| 261 | } |
| 262 | |
| 263 | // Test that UpdatePartitionMetadata fails if there is not enough space on the |
| 264 | // device. |
| 265 | TEST_P(DynamicPartitionControlAndroidTestP, NotEnoughSpace) { |
| 266 | PartitionSuffixSizes source_metadata{{S("system"), 3_GiB}, |
| 267 | {S("vendor"), 2_GiB}, |
| 268 | {T("system"), 0}, |
| 269 | {T("vendor"), 0}}; |
| 270 | PartitionSizes update_metadata{{"system", 3_GiB}, {"vendor", 3_GiB}}; |
| 271 | |
| 272 | EXPECT_FALSE(UpdatePartitionMetadata(source_metadata, update_metadata, {})) |
| 273 | << "Should not be able to fit 11GiB data into 10GiB space"; |
| 274 | } |
| 275 | |
| 276 | TEST_P(DynamicPartitionControlAndroidTestP, NotEnoughSpaceForSlot) { |
| 277 | PartitionSuffixSizes source_metadata{{S("system"), 1_GiB}, |
| 278 | {S("vendor"), 1_GiB}, |
| 279 | {T("system"), 0}, |
| 280 | {T("vendor"), 0}}; |
| 281 | PartitionSizes update_metadata{{"system", 3_GiB}, {"vendor", 3_GiB}}; |
| 282 | EXPECT_FALSE(UpdatePartitionMetadata(source_metadata, update_metadata, {})) |
| 283 | << "Should not be able to grow over size of super / 2"; |
| 284 | } |
| 285 | |
| 286 | INSTANTIATE_TEST_CASE_P(DynamicPartitionControlAndroidTest, |
| 287 | DynamicPartitionControlAndroidTestP, |
| 288 | testing::Values(TestParam{0, 1}, TestParam{1, 0})); |
| 289 | |
| 290 | class DynamicPartitionControlAndroidGroupTestP |
| 291 | : public DynamicPartitionControlAndroidTestP { |
| 292 | public: |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 293 | DeltaArchiveManifest source_manifest; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 294 | void SetUp() override { |
| 295 | DynamicPartitionControlAndroidTestP::SetUp(); |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 296 | AddGroupAndPartition( |
| 297 | &source_manifest, S("android"), 3_GiB, S("system"), 2_GiB); |
| 298 | AddGroupAndPartition(&source_manifest, S("oem"), 2_GiB, S("vendor"), 1_GiB); |
| 299 | AddGroupAndPartition(&source_manifest, T("android"), 3_GiB, T("system"), 0); |
| 300 | AddGroupAndPartition(&source_manifest, T("oem"), 2_GiB, T("vendor"), 0); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 303 | void AddGroupAndPartition(DeltaArchiveManifest* manifest, |
| 304 | const string& group, |
| 305 | uint64_t group_size, |
| 306 | const string& partition, |
| 307 | uint64_t partition_size) { |
| 308 | auto* g = AddGroup(manifest, group, group_size); |
| 309 | AddPartition(manifest, g, partition, partition_size); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 310 | } |
| 311 | }; |
| 312 | |
| 313 | // Allow to resize within group. |
| 314 | TEST_P(DynamicPartitionControlAndroidGroupTestP, ResizeWithinGroup) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 315 | DeltaArchiveManifest expected; |
| 316 | AddGroupAndPartition(&expected, T("android"), 3_GiB, T("system"), 3_GiB); |
| 317 | AddGroupAndPartition(&expected, T("oem"), 2_GiB, T("vendor"), 2_GiB); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 318 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 319 | DeltaArchiveManifest update_manifest; |
| 320 | AddGroupAndPartition(&update_manifest, "android", 3_GiB, "system", 3_GiB); |
| 321 | AddGroupAndPartition(&update_manifest, "oem", 2_GiB, "vendor", 2_GiB); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 322 | |
| 323 | EXPECT_TRUE( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 324 | UpdatePartitionMetadata(source_manifest, update_manifest, expected)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | TEST_P(DynamicPartitionControlAndroidGroupTestP, NotEnoughSpaceForGroup) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 328 | DeltaArchiveManifest update_manifest; |
| 329 | AddGroupAndPartition(&update_manifest, "android", 3_GiB, "system", 1_GiB), |
| 330 | AddGroupAndPartition(&update_manifest, "oem", 2_GiB, "vendor", 3_GiB); |
| 331 | EXPECT_FALSE(UpdatePartitionMetadata(source_manifest, update_manifest, {})) |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 332 | << "Should not be able to grow over maximum size of group"; |
| 333 | } |
| 334 | |
| 335 | TEST_P(DynamicPartitionControlAndroidGroupTestP, GroupTooBig) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 336 | DeltaArchiveManifest update_manifest; |
| 337 | AddGroup(&update_manifest, "android", 3_GiB); |
| 338 | AddGroup(&update_manifest, "oem", 3_GiB); |
| 339 | EXPECT_FALSE(UpdatePartitionMetadata(source_manifest, update_manifest, {})) |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 340 | << "Should not be able to grow over size of super / 2"; |
| 341 | } |
| 342 | |
| 343 | TEST_P(DynamicPartitionControlAndroidGroupTestP, AddPartitionToGroup) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 344 | DeltaArchiveManifest expected; |
| 345 | auto* g = AddGroup(&expected, T("android"), 3_GiB); |
| 346 | AddPartition(&expected, g, T("system"), 2_GiB); |
| 347 | AddPartition(&expected, g, T("system_ext"), 1_GiB); |
| 348 | |
| 349 | DeltaArchiveManifest update_manifest; |
| 350 | g = AddGroup(&update_manifest, "android", 3_GiB); |
| 351 | AddPartition(&update_manifest, g, "system", 2_GiB); |
| 352 | AddPartition(&update_manifest, g, "system_ext", 1_GiB); |
| 353 | AddGroupAndPartition(&update_manifest, "oem", 2_GiB, "vendor", 2_GiB); |
| 354 | |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 355 | EXPECT_TRUE( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 356 | UpdatePartitionMetadata(source_manifest, update_manifest, expected)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | TEST_P(DynamicPartitionControlAndroidGroupTestP, RemovePartitionFromGroup) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 360 | DeltaArchiveManifest expected; |
| 361 | AddGroup(&expected, T("android"), 3_GiB); |
| 362 | |
| 363 | DeltaArchiveManifest update_manifest; |
| 364 | AddGroup(&update_manifest, "android", 3_GiB); |
| 365 | AddGroupAndPartition(&update_manifest, "oem", 2_GiB, "vendor", 2_GiB); |
| 366 | |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 367 | EXPECT_TRUE( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 368 | UpdatePartitionMetadata(source_manifest, update_manifest, expected)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | TEST_P(DynamicPartitionControlAndroidGroupTestP, AddGroup) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 372 | DeltaArchiveManifest expected; |
| 373 | AddGroupAndPartition( |
| 374 | &expected, T("new_group"), 2_GiB, T("new_partition"), 2_GiB); |
| 375 | |
| 376 | DeltaArchiveManifest update_manifest; |
| 377 | AddGroupAndPartition(&update_manifest, "android", 2_GiB, "system", 2_GiB); |
| 378 | AddGroupAndPartition(&update_manifest, "oem", 1_GiB, "vendor", 1_GiB); |
| 379 | AddGroupAndPartition( |
| 380 | &update_manifest, "new_group", 2_GiB, "new_partition", 2_GiB); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 381 | EXPECT_TRUE( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 382 | UpdatePartitionMetadata(source_manifest, update_manifest, expected)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | TEST_P(DynamicPartitionControlAndroidGroupTestP, RemoveGroup) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 386 | DeltaArchiveManifest update_manifest; |
| 387 | AddGroupAndPartition(&update_manifest, "android", 2_GiB, "system", 2_GiB); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 388 | |
| 389 | EXPECT_TRUE(UpdatePartitionMetadata( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 390 | source_manifest, update_manifest, Not(HasGroup(T("oem"))))); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | TEST_P(DynamicPartitionControlAndroidGroupTestP, ResizeGroup) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 394 | DeltaArchiveManifest expected; |
| 395 | AddGroupAndPartition(&expected, T("android"), 2_GiB, T("system"), 2_GiB); |
| 396 | AddGroupAndPartition(&expected, T("oem"), 3_GiB, T("vendor"), 3_GiB); |
| 397 | DeltaArchiveManifest update_manifest; |
| 398 | AddGroupAndPartition(&update_manifest, "android", 2_GiB, "system", 2_GiB), |
| 399 | AddGroupAndPartition(&update_manifest, "oem", 3_GiB, "vendor", 3_GiB); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 400 | EXPECT_TRUE( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 401 | UpdatePartitionMetadata(source_manifest, update_manifest, expected)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | INSTANTIATE_TEST_CASE_P(DynamicPartitionControlAndroidTest, |
| 405 | DynamicPartitionControlAndroidGroupTestP, |
| 406 | testing::Values(TestParam{0, 1}, TestParam{1, 0})); |
| 407 | |
| 408 | const PartitionSuffixSizes update_sizes_0() { |
| 409 | // Initial state is 0 for "other" slot. |
| 410 | return { |
| 411 | {"grown_a", 2_GiB}, |
| 412 | {"shrunk_a", 1_GiB}, |
| 413 | {"same_a", 100_MiB}, |
| 414 | {"deleted_a", 150_MiB}, |
| 415 | // no added_a |
| 416 | {"grown_b", 200_MiB}, |
| 417 | // simulate system_other |
| 418 | {"shrunk_b", 0}, |
| 419 | {"same_b", 0}, |
| 420 | {"deleted_b", 0}, |
| 421 | // no added_b |
| 422 | }; |
| 423 | } |
| 424 | |
| 425 | const PartitionSuffixSizes update_sizes_1() { |
| 426 | return { |
| 427 | {"grown_a", 2_GiB}, |
| 428 | {"shrunk_a", 1_GiB}, |
| 429 | {"same_a", 100_MiB}, |
| 430 | {"deleted_a", 150_MiB}, |
| 431 | // no added_a |
| 432 | {"grown_b", 3_GiB}, |
| 433 | {"shrunk_b", 150_MiB}, |
| 434 | {"same_b", 100_MiB}, |
| 435 | {"added_b", 150_MiB}, |
| 436 | // no deleted_b |
| 437 | }; |
| 438 | } |
| 439 | |
| 440 | const PartitionSuffixSizes update_sizes_2() { |
| 441 | return { |
| 442 | {"grown_a", 4_GiB}, |
| 443 | {"shrunk_a", 100_MiB}, |
| 444 | {"same_a", 100_MiB}, |
| 445 | {"deleted_a", 64_MiB}, |
| 446 | // no added_a |
| 447 | {"grown_b", 3_GiB}, |
| 448 | {"shrunk_b", 150_MiB}, |
| 449 | {"same_b", 100_MiB}, |
| 450 | {"added_b", 150_MiB}, |
| 451 | // no deleted_b |
| 452 | }; |
| 453 | } |
| 454 | |
| 455 | // Test case for first update after the device is manufactured, in which |
| 456 | // case the "other" slot is likely of size "0" (except system, which is |
| 457 | // non-zero because of system_other partition) |
| 458 | TEST_F(DynamicPartitionControlAndroidTest, SimulatedFirstUpdate) { |
| 459 | SetSlots({0, 1}); |
| 460 | |
| 461 | SetMetadata(source(), update_sizes_0()); |
| 462 | SetMetadata(target(), update_sizes_0()); |
| 463 | ExpectStoreMetadata(update_sizes_1()); |
| 464 | ExpectUnmap({"grown_b", "shrunk_b", "same_b", "added_b"}); |
| 465 | |
| 466 | EXPECT_TRUE(PreparePartitionsForUpdate({{"grown", 3_GiB}, |
| 467 | {"shrunk", 150_MiB}, |
| 468 | {"same", 100_MiB}, |
| 469 | {"added", 150_MiB}})); |
| 470 | } |
| 471 | |
| 472 | // After first update, test for the second update. In the second update, the |
| 473 | // "added" partition is deleted and "deleted" partition is re-added. |
| 474 | TEST_F(DynamicPartitionControlAndroidTest, SimulatedSecondUpdate) { |
| 475 | SetSlots({1, 0}); |
| 476 | |
| 477 | SetMetadata(source(), update_sizes_1()); |
| 478 | SetMetadata(target(), update_sizes_0()); |
| 479 | |
| 480 | ExpectStoreMetadata(update_sizes_2()); |
| 481 | ExpectUnmap({"grown_a", "shrunk_a", "same_a", "deleted_a"}); |
| 482 | |
| 483 | EXPECT_TRUE(PreparePartitionsForUpdate({{"grown", 4_GiB}, |
| 484 | {"shrunk", 100_MiB}, |
| 485 | {"same", 100_MiB}, |
| 486 | {"deleted", 64_MiB}})); |
| 487 | } |
| 488 | |
| 489 | } // namespace chromeos_update_engine |