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 | |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 29 | #include "update_engine/dynamic_partition_test_utils.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 30 | #include "update_engine/mock_boot_control_hal.h" |
| 31 | #include "update_engine/mock_dynamic_partition_control.h" |
| 32 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 33 | using android::dm::DmDeviceState; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 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; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 38 | using testing::Invoke; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 39 | using testing::NiceMock; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 40 | using testing::Not; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 41 | using testing::Return; |
| 42 | |
| 43 | namespace chromeos_update_engine { |
| 44 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 45 | class BootControlAndroidTest : public ::testing::Test { |
| 46 | protected: |
| 47 | void SetUp() override { |
| 48 | // Fake init bootctl_ |
| 49 | bootctl_.module_ = new NiceMock<MockBootControlHal>(); |
| 50 | bootctl_.dynamic_control_ = |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 51 | std::make_unique<NiceMock<MockDynamicPartitionControl>>(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 52 | |
| 53 | ON_CALL(module(), getNumberSlots()).WillByDefault(Invoke([] { |
| 54 | return kMaxNumSlots; |
| 55 | })); |
| 56 | ON_CALL(module(), getSuffix(_, _)) |
| 57 | .WillByDefault(Invoke([](auto slot, auto cb) { |
| 58 | EXPECT_LE(slot, kMaxNumSlots); |
| 59 | cb(slot < kMaxNumSlots ? kSlotSuffixes[slot] : ""); |
| 60 | return Void(); |
| 61 | })); |
| 62 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 63 | ON_CALL(dynamicControl(), GetDynamicPartitionsFeatureFlag()) |
| 64 | .WillByDefault(Return(FeatureFlag(FeatureFlag::Value::LAUNCH))); |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 65 | ON_CALL(dynamicControl(), GetVirtualAbFeatureFlag()) |
| 66 | .WillByDefault(Return(FeatureFlag(FeatureFlag::Value::NONE))); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 67 | ON_CALL(dynamicControl(), DeviceExists(_)).WillByDefault(Return(true)); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 68 | ON_CALL(dynamicControl(), GetDeviceDir(_)) |
| 69 | .WillByDefault(Invoke([](auto path) { |
| 70 | *path = kFakeDevicePath; |
| 71 | return true; |
| 72 | })); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 73 | ON_CALL(dynamicControl(), GetDmDevicePathByName(_, _)) |
| 74 | .WillByDefault(Invoke([](auto partition_name_suffix, auto device) { |
| 75 | *device = GetDmDevice(partition_name_suffix); |
| 76 | return true; |
| 77 | })); |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 78 | |
| 79 | ON_CALL(dynamicControl(), GetSuperPartitionName(_)) |
| 80 | .WillByDefault(Return(kFakeSuper)); |
| 81 | } |
| 82 | |
| 83 | std::string GetSuperDevice(uint32_t slot) { |
| 84 | return GetDevice(dynamicControl().GetSuperPartitionName(slot)); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Return the mocked HAL module. |
| 88 | NiceMock<MockBootControlHal>& module() { |
| 89 | return static_cast<NiceMock<MockBootControlHal>&>(*bootctl_.module_); |
| 90 | } |
| 91 | |
| 92 | // Return the mocked DynamicPartitionControlInterface. |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 93 | NiceMock<MockDynamicPartitionControl>& dynamicControl() { |
| 94 | return static_cast<NiceMock<MockDynamicPartitionControl>&>( |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 95 | *bootctl_.dynamic_control_); |
| 96 | } |
| 97 | |
| 98 | // Set the fake metadata to return when LoadMetadataBuilder is called on |
| 99 | // |slot|. |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 100 | void SetMetadata(uint32_t slot, const PartitionSuffixSizes& sizes) { |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 101 | EXPECT_CALL(dynamicControl(), |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 102 | LoadMetadataBuilder(GetSuperDevice(slot), slot)) |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 103 | .Times(AnyNumber()) |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 104 | .WillRepeatedly(Invoke([sizes](auto, auto) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 105 | return NewFakeMetadata(PartitionSuffixSizesToManifest(sizes)); |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 106 | })); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 109 | uint32_t source() { return slots_.source; } |
| 110 | |
| 111 | uint32_t target() { return slots_.target; } |
| 112 | |
| 113 | // Return partition names with suffix of source(). |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 114 | string S(const string& name) { return name + kSlotSuffixes[source()]; } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 115 | |
| 116 | // Return partition names with suffix of target(). |
Dan Albert | fcfbda2 | 2018-12-14 12:57:55 -0800 | [diff] [blame] | 117 | string T(const string& name) { return name + kSlotSuffixes[target()]; } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 118 | |
| 119 | // Set source and target slots to use before testing. |
| 120 | void SetSlots(const TestParam& slots) { |
| 121 | slots_ = slots; |
| 122 | |
| 123 | ON_CALL(module(), getCurrentSlot()).WillByDefault(Invoke([this] { |
| 124 | return source(); |
| 125 | })); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 128 | bool PreparePartitionsForUpdate(uint32_t slot, |
| 129 | PartitionSizes partition_sizes, |
| 130 | bool update_metadata = true) { |
| 131 | auto m = PartitionSizesToManifest(partition_sizes); |
| 132 | return bootctl_.PreparePartitionsForUpdate(slot, m, update_metadata); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | BootControlAndroid bootctl_; // BootControlAndroid under test. |
| 136 | TestParam slots_; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | class BootControlAndroidTestP |
| 140 | : public BootControlAndroidTest, |
| 141 | public ::testing::WithParamInterface<TestParam> { |
| 142 | public: |
| 143 | void SetUp() override { |
| 144 | BootControlAndroidTest::SetUp(); |
| 145 | SetSlots(GetParam()); |
| 146 | } |
| 147 | }; |
| 148 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 149 | // Test applying retrofit update on a build with dynamic partitions enabled. |
| 150 | TEST_P(BootControlAndroidTestP, |
| 151 | ApplyRetrofitUpdateOnDynamicPartitionsEnabledBuild) { |
| 152 | SetMetadata(source(), |
| 153 | {{S("system"), 2_GiB}, |
| 154 | {S("vendor"), 1_GiB}, |
| 155 | {T("system"), 2_GiB}, |
| 156 | {T("vendor"), 1_GiB}}); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 157 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 158 | // Not calling through BootControlAndroidTest::PreparePartitionsForUpdate(), |
| 159 | // since we don't want any default group in the PartitionMetadata. |
| 160 | EXPECT_TRUE(bootctl_.PreparePartitionsForUpdate(target(), {}, true)); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 161 | |
| 162 | // Should use dynamic source partitions. |
| 163 | EXPECT_CALL(dynamicControl(), GetState(S("system"))) |
| 164 | .Times(1) |
| 165 | .WillOnce(Return(DmDeviceState::ACTIVE)); |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 166 | string system_device; |
| 167 | EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &system_device)); |
| 168 | EXPECT_EQ(GetDmDevice(S("system")), system_device); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 169 | |
| 170 | // Should use static target partitions without querying dynamic control. |
| 171 | EXPECT_CALL(dynamicControl(), GetState(T("system"))).Times(0); |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 172 | EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &system_device)); |
| 173 | EXPECT_EQ(GetDevice(T("system")), system_device); |
| 174 | |
| 175 | // Static partition "bar". |
| 176 | EXPECT_CALL(dynamicControl(), GetState(S("bar"))).Times(0); |
| 177 | std::string bar_device; |
| 178 | EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", source(), &bar_device)); |
| 179 | EXPECT_EQ(GetDevice(S("bar")), bar_device); |
| 180 | |
| 181 | EXPECT_CALL(dynamicControl(), GetState(T("bar"))).Times(0); |
| 182 | EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", target(), &bar_device)); |
| 183 | EXPECT_EQ(GetDevice(T("bar")), bar_device); |
| 184 | } |
| 185 | |
| 186 | TEST_P(BootControlAndroidTestP, GetPartitionDeviceWhenResumingUpdate) { |
| 187 | // Both of the two slots contain valid partition metadata, since this is |
| 188 | // resuming an update. |
| 189 | SetMetadata(source(), |
| 190 | {{S("system"), 2_GiB}, |
| 191 | {S("vendor"), 1_GiB}, |
| 192 | {T("system"), 2_GiB}, |
| 193 | {T("vendor"), 1_GiB}}); |
| 194 | SetMetadata(target(), |
| 195 | {{S("system"), 2_GiB}, |
| 196 | {S("vendor"), 1_GiB}, |
| 197 | {T("system"), 2_GiB}, |
| 198 | {T("vendor"), 1_GiB}}); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 199 | |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame^] | 200 | EXPECT_CALL(dynamicControl(), PreparePartitionsForUpdate(_, _, _, false)) |
| 201 | .WillOnce(Return(true)); |
| 202 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 203 | EXPECT_TRUE(PreparePartitionsForUpdate( |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 204 | target(), {{"system", 2_GiB}, {"vendor", 1_GiB}}, false)); |
| 205 | |
| 206 | // Dynamic partition "system". |
| 207 | EXPECT_CALL(dynamicControl(), GetState(S("system"))) |
| 208 | .Times(1) |
| 209 | .WillOnce(Return(DmDeviceState::ACTIVE)); |
| 210 | string system_device; |
| 211 | EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &system_device)); |
| 212 | EXPECT_EQ(GetDmDevice(S("system")), system_device); |
| 213 | |
| 214 | EXPECT_CALL(dynamicControl(), GetState(T("system"))) |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 215 | .Times(AnyNumber()) |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 216 | .WillOnce(Return(DmDeviceState::ACTIVE)); |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 217 | EXPECT_CALL(dynamicControl(), |
| 218 | MapPartitionOnDeviceMapper( |
| 219 | GetSuperDevice(target()), T("system"), target(), _, _)) |
| 220 | .Times(AnyNumber()) |
| 221 | .WillRepeatedly( |
| 222 | Invoke([](const auto&, const auto& name, auto, auto, auto* device) { |
| 223 | *device = "/fake/remapped/" + name; |
| 224 | return true; |
| 225 | })); |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 226 | EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &system_device)); |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 227 | EXPECT_EQ("/fake/remapped/" + T("system"), system_device); |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 228 | |
| 229 | // Static partition "bar". |
| 230 | EXPECT_CALL(dynamicControl(), GetState(S("bar"))).Times(0); |
| 231 | std::string bar_device; |
| 232 | EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", source(), &bar_device)); |
| 233 | EXPECT_EQ(GetDevice(S("bar")), bar_device); |
| 234 | |
| 235 | EXPECT_CALL(dynamicControl(), GetState(T("bar"))).Times(0); |
| 236 | EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", target(), &bar_device)); |
| 237 | EXPECT_EQ(GetDevice(T("bar")), bar_device); |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 240 | INSTANTIATE_TEST_CASE_P(BootControlAndroidTest, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 241 | BootControlAndroidTestP, |
| 242 | testing::Values(TestParam{0, 1}, TestParam{1, 0})); |
| 243 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 244 | TEST_F(BootControlAndroidTest, ApplyingToCurrentSlot) { |
| 245 | SetSlots({1, 1}); |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 246 | EXPECT_FALSE(PreparePartitionsForUpdate(target(), {})) |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 247 | << "Should not be able to apply to current slot."; |
| 248 | } |
| 249 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 250 | } // namespace chromeos_update_engine |