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