blob: 1a875474315e59dfc6704984d46d3d53ff836ab9 [file] [log] [blame]
Yifan Hong537802d2018-08-15 13:15:42 -07001//
2// Copyright (C) 2018 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/boot_control_android.h"
18
19#include <set>
Yifan Hongd4db07e2018-10-18 17:46:27 -070020#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070021
Yifan Hongd4db07e2018-10-18 17:46:27 -070022#include <base/logging.h>
23#include <base/strings/string_util.h>
David Andersond63cb3c2018-10-01 14:15:00 -070024#include <fs_mgr.h>
Yifan Hong537802d2018-08-15 13:15:42 -070025#include <gmock/gmock.h>
26#include <gtest/gtest.h>
Yifan Hong1d9077f2018-12-07 12:09:37 -080027#include <libdm/dm.h>
Yifan Hong537802d2018-08-15 13:15:42 -070028
Yifan Hongc049f932019-07-23 15:06:05 -070029#include "update_engine/dynamic_partition_test_utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070030#include "update_engine/mock_boot_control_hal.h"
31#include "update_engine/mock_dynamic_partition_control.h"
32
Yifan Hong1d9077f2018-12-07 12:09:37 -080033using android::dm::DmDeviceState;
Yifan Hong537802d2018-08-15 13:15:42 -070034using android::hardware::Void;
Dan Albertfcfbda22018-12-14 12:57:55 -080035using std::string;
Yifan Hong537802d2018-08-15 13:15:42 -070036using testing::_;
37using testing::AnyNumber;
Yifan Hong537802d2018-08-15 13:15:42 -070038using testing::Invoke;
Yifan Hong537802d2018-08-15 13:15:42 -070039using testing::NiceMock;
Yifan Hongd4db07e2018-10-18 17:46:27 -070040using testing::Not;
Yifan Hong537802d2018-08-15 13:15:42 -070041using testing::Return;
42
43namespace chromeos_update_engine {
44
Yifan Hong537802d2018-08-15 13:15:42 -070045class 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 Hongc049f932019-07-23 15:06:05 -070051 std::make_unique<NiceMock<MockDynamicPartitionControl>>();
Yifan Hong537802d2018-08-15 13:15:42 -070052
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 Hong186bb682019-07-23 14:04:39 -070063 ON_CALL(dynamicControl(), GetDynamicPartitionsFeatureFlag())
64 .WillByDefault(Return(FeatureFlag(FeatureFlag::Value::LAUNCH)));
Yifan Hong1d9077f2018-12-07 12:09:37 -080065 ON_CALL(dynamicControl(), DeviceExists(_)).WillByDefault(Return(true));
Yifan Hong537802d2018-08-15 13:15:42 -070066 ON_CALL(dynamicControl(), GetDeviceDir(_))
67 .WillByDefault(Invoke([](auto path) {
68 *path = kFakeDevicePath;
69 return true;
70 }));
Yifan Hong1d9077f2018-12-07 12:09:37 -080071 ON_CALL(dynamicControl(), GetDmDevicePathByName(_, _))
72 .WillByDefault(Invoke([](auto partition_name_suffix, auto device) {
73 *device = GetDmDevice(partition_name_suffix);
74 return true;
75 }));
Yifan Hong537802d2018-08-15 13:15:42 -070076 }
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 Hongc049f932019-07-23 15:06:05 -070084 NiceMock<MockDynamicPartitionControl>& dynamicControl() {
85 return static_cast<NiceMock<MockDynamicPartitionControl>&>(
Yifan Hong537802d2018-08-15 13:15:42 -070086 *bootctl_.dynamic_control_);
87 }
88
89 // Set the fake metadata to return when LoadMetadataBuilder is called on
90 // |slot|.
Yifan Hongd4db07e2018-10-18 17:46:27 -070091 void SetMetadata(uint32_t slot, const PartitionSuffixSizes& sizes) {
Yifan Hong6e706b12018-11-09 16:50:51 -080092 EXPECT_CALL(dynamicControl(),
Yifan Hongc049f932019-07-23 15:06:05 -070093 LoadMetadataBuilder(GetSuperDevice(slot), slot))
Yifan Hongd4db07e2018-10-18 17:46:27 -070094 .Times(AnyNumber())
Yifan Hongc049f932019-07-23 15:06:05 -070095 .WillRepeatedly(Invoke([sizes](auto, auto) {
96 return NewFakeMetadata(PartitionSuffixSizesToMetadata(sizes));
Yifan Hong6e706b12018-11-09 16:50:51 -080097 }));
Yifan Hong537802d2018-08-15 13:15:42 -070098 }
99
Yifan Hong537802d2018-08-15 13:15:42 -0700100 uint32_t source() { return slots_.source; }
101
102 uint32_t target() { return slots_.target; }
103
104 // Return partition names with suffix of source().
Dan Albertfcfbda22018-12-14 12:57:55 -0800105 string S(const string& name) { return name + kSlotSuffixes[source()]; }
Yifan Hong537802d2018-08-15 13:15:42 -0700106
107 // Return partition names with suffix of target().
Dan Albertfcfbda22018-12-14 12:57:55 -0800108 string T(const string& name) { return name + kSlotSuffixes[target()]; }
Yifan Hong537802d2018-08-15 13:15:42 -0700109
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 Hongd4db07e2018-10-18 17:46:27 -0700117 }
118
Tao Bao3406c772019-01-02 15:34:35 -0800119 bool InitPartitionMetadata(uint32_t slot,
120 PartitionSizes partition_sizes,
121 bool update_metadata = true) {
Yifan Hongc049f932019-07-23 15:06:05 -0700122 auto m = PartitionSizesToMetadata(partition_sizes);
Tao Bao3406c772019-01-02 15:34:35 -0800123 return bootctl_.InitPartitionMetadata(slot, m, update_metadata);
Yifan Hong537802d2018-08-15 13:15:42 -0700124 }
125
126 BootControlAndroid bootctl_; // BootControlAndroid under test.
127 TestParam slots_;
Yifan Hong537802d2018-08-15 13:15:42 -0700128};
129
130class 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 Hong1d9077f2018-12-07 12:09:37 -0800140// Test applying retrofit update on a build with dynamic partitions enabled.
141TEST_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 Hong1d9077f2018-12-07 12:09:37 -0800148
149 // Not calling through BootControlAndroidTest::InitPartitionMetadata(), since
150 // we don't want any default group in the PartitionMetadata.
Tao Bao3406c772019-01-02 15:34:35 -0800151 EXPECT_TRUE(bootctl_.InitPartitionMetadata(target(), {}, true));
Yifan Hong1d9077f2018-12-07 12:09:37 -0800152
153 // Should use dynamic source partitions.
154 EXPECT_CALL(dynamicControl(), GetState(S("system")))
155 .Times(1)
156 .WillOnce(Return(DmDeviceState::ACTIVE));
Tao Bao3406c772019-01-02 15:34:35 -0800157 string system_device;
158 EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &system_device));
159 EXPECT_EQ(GetDmDevice(S("system")), system_device);
Yifan Hong1d9077f2018-12-07 12:09:37 -0800160
161 // Should use static target partitions without querying dynamic control.
162 EXPECT_CALL(dynamicControl(), GetState(T("system"))).Times(0);
Tao Bao3406c772019-01-02 15:34:35 -0800163 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
177TEST_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 Hongc049f932019-07-23 15:06:05 -0700190
Tao Bao3406c772019-01-02 15:34:35 -0800191 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 Hong8546a712019-03-28 14:42:53 -0700203 .Times(AnyNumber())
Tao Bao3406c772019-01-02 15:34:35 -0800204 .WillOnce(Return(DmDeviceState::ACTIVE));
Yifan Hong8546a712019-03-28 14:42:53 -0700205 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 Bao3406c772019-01-02 15:34:35 -0800214 EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &system_device));
Yifan Hong8546a712019-03-28 14:42:53 -0700215 EXPECT_EQ("/fake/remapped/" + T("system"), system_device);
Tao Bao3406c772019-01-02 15:34:35 -0800216
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 Hong1d9077f2018-12-07 12:09:37 -0800226}
227
Yifan Hongd4db07e2018-10-18 17:46:27 -0700228INSTANTIATE_TEST_CASE_P(BootControlAndroidTest,
Yifan Hong537802d2018-08-15 13:15:42 -0700229 BootControlAndroidTestP,
230 testing::Values(TestParam{0, 1}, TestParam{1, 0}));
231
Yifan Hong537802d2018-08-15 13:15:42 -0700232TEST_F(BootControlAndroidTest, ApplyingToCurrentSlot) {
233 SetSlots({1, 1});
Yifan Hongd4db07e2018-10-18 17:46:27 -0700234 EXPECT_FALSE(InitPartitionMetadata(target(), {}))
Yifan Hong537802d2018-08-15 13:15:42 -0700235 << "Should not be able to apply to current slot.";
236}
237
Yifan Hong537802d2018-08-15 13:15:42 -0700238} // namespace chromeos_update_engine