1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
//
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/boot_control_android.h"
#include <set>
#include <vector>
#include <base/logging.h>
#include <base/strings/string_util.h>
#include <fs_mgr.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <libdm/dm.h>
#include "update_engine/dynamic_partition_test_utils.h"
#include "update_engine/mock_boot_control_hal.h"
#include "update_engine/mock_dynamic_partition_control.h"
using android::dm::DmDeviceState;
using android::hardware::Void;
using std::string;
using testing::_;
using testing::AnyNumber;
using testing::Invoke;
using testing::NiceMock;
using testing::Not;
using testing::Return;
namespace chromeos_update_engine {
class BootControlAndroidTest : public ::testing::Test {
protected:
void SetUp() override {
// Fake init bootctl_
bootctl_.module_ = new NiceMock<MockBootControlHal>();
bootctl_.dynamic_control_ =
std::make_unique<NiceMock<MockDynamicPartitionControl>>();
ON_CALL(module(), getNumberSlots()).WillByDefault(Invoke([] {
return kMaxNumSlots;
}));
ON_CALL(module(), getSuffix(_, _))
.WillByDefault(Invoke([](auto slot, auto cb) {
EXPECT_LE(slot, kMaxNumSlots);
cb(slot < kMaxNumSlots ? kSlotSuffixes[slot] : "");
return Void();
}));
ON_CALL(dynamicControl(), GetDynamicPartitionsFeatureFlag())
.WillByDefault(Return(FeatureFlag(FeatureFlag::Value::LAUNCH)));
ON_CALL(dynamicControl(), DeviceExists(_)).WillByDefault(Return(true));
ON_CALL(dynamicControl(), GetDeviceDir(_))
.WillByDefault(Invoke([](auto path) {
*path = kFakeDevicePath;
return true;
}));
ON_CALL(dynamicControl(), GetDmDevicePathByName(_, _))
.WillByDefault(Invoke([](auto partition_name_suffix, auto device) {
*device = GetDmDevice(partition_name_suffix);
return true;
}));
ON_CALL(dynamicControl(), GetSuperPartitionName(_))
.WillByDefault(Return(kFakeSuper));
}
std::string GetSuperDevice(uint32_t slot) {
return GetDevice(dynamicControl().GetSuperPartitionName(slot));
}
// Return the mocked HAL module.
NiceMock<MockBootControlHal>& module() {
return static_cast<NiceMock<MockBootControlHal>&>(*bootctl_.module_);
}
// Return the mocked DynamicPartitionControlInterface.
NiceMock<MockDynamicPartitionControl>& dynamicControl() {
return static_cast<NiceMock<MockDynamicPartitionControl>&>(
*bootctl_.dynamic_control_);
}
// Set the fake metadata to return when LoadMetadataBuilder is called on
// |slot|.
void SetMetadata(uint32_t slot, const PartitionSuffixSizes& sizes) {
EXPECT_CALL(dynamicControl(),
LoadMetadataBuilder(GetSuperDevice(slot), slot))
.Times(AnyNumber())
.WillRepeatedly(Invoke([sizes](auto, auto) {
return NewFakeMetadata(PartitionSuffixSizesToMetadata(sizes));
}));
}
uint32_t source() { return slots_.source; }
uint32_t target() { return slots_.target; }
// Return partition names with suffix of source().
string S(const string& name) { return name + kSlotSuffixes[source()]; }
// Return partition names with suffix of target().
string T(const string& name) { return name + kSlotSuffixes[target()]; }
// Set source and target slots to use before testing.
void SetSlots(const TestParam& slots) {
slots_ = slots;
ON_CALL(module(), getCurrentSlot()).WillByDefault(Invoke([this] {
return source();
}));
}
bool InitPartitionMetadata(uint32_t slot,
PartitionSizes partition_sizes,
bool update_metadata = true) {
auto m = PartitionSizesToMetadata(partition_sizes);
return bootctl_.InitPartitionMetadata(slot, m, update_metadata);
}
BootControlAndroid bootctl_; // BootControlAndroid under test.
TestParam slots_;
};
class BootControlAndroidTestP
: public BootControlAndroidTest,
public ::testing::WithParamInterface<TestParam> {
public:
void SetUp() override {
BootControlAndroidTest::SetUp();
SetSlots(GetParam());
}
};
// Test applying retrofit update on a build with dynamic partitions enabled.
TEST_P(BootControlAndroidTestP,
ApplyRetrofitUpdateOnDynamicPartitionsEnabledBuild) {
SetMetadata(source(),
{{S("system"), 2_GiB},
{S("vendor"), 1_GiB},
{T("system"), 2_GiB},
{T("vendor"), 1_GiB}});
// Not calling through BootControlAndroidTest::InitPartitionMetadata(), since
// we don't want any default group in the PartitionMetadata.
EXPECT_TRUE(bootctl_.InitPartitionMetadata(target(), {}, true));
// Should use dynamic source partitions.
EXPECT_CALL(dynamicControl(), GetState(S("system")))
.Times(1)
.WillOnce(Return(DmDeviceState::ACTIVE));
string system_device;
EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &system_device));
EXPECT_EQ(GetDmDevice(S("system")), system_device);
// Should use static target partitions without querying dynamic control.
EXPECT_CALL(dynamicControl(), GetState(T("system"))).Times(0);
EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &system_device));
EXPECT_EQ(GetDevice(T("system")), system_device);
// Static partition "bar".
EXPECT_CALL(dynamicControl(), GetState(S("bar"))).Times(0);
std::string bar_device;
EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", source(), &bar_device));
EXPECT_EQ(GetDevice(S("bar")), bar_device);
EXPECT_CALL(dynamicControl(), GetState(T("bar"))).Times(0);
EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", target(), &bar_device));
EXPECT_EQ(GetDevice(T("bar")), bar_device);
}
TEST_P(BootControlAndroidTestP, GetPartitionDeviceWhenResumingUpdate) {
// Both of the two slots contain valid partition metadata, since this is
// resuming an update.
SetMetadata(source(),
{{S("system"), 2_GiB},
{S("vendor"), 1_GiB},
{T("system"), 2_GiB},
{T("vendor"), 1_GiB}});
SetMetadata(target(),
{{S("system"), 2_GiB},
{S("vendor"), 1_GiB},
{T("system"), 2_GiB},
{T("vendor"), 1_GiB}});
EXPECT_TRUE(InitPartitionMetadata(
target(), {{"system", 2_GiB}, {"vendor", 1_GiB}}, false));
// Dynamic partition "system".
EXPECT_CALL(dynamicControl(), GetState(S("system")))
.Times(1)
.WillOnce(Return(DmDeviceState::ACTIVE));
string system_device;
EXPECT_TRUE(bootctl_.GetPartitionDevice("system", source(), &system_device));
EXPECT_EQ(GetDmDevice(S("system")), system_device);
EXPECT_CALL(dynamicControl(), GetState(T("system")))
.Times(AnyNumber())
.WillOnce(Return(DmDeviceState::ACTIVE));
EXPECT_CALL(dynamicControl(),
MapPartitionOnDeviceMapper(
GetSuperDevice(target()), T("system"), target(), _, _))
.Times(AnyNumber())
.WillRepeatedly(
Invoke([](const auto&, const auto& name, auto, auto, auto* device) {
*device = "/fake/remapped/" + name;
return true;
}));
EXPECT_TRUE(bootctl_.GetPartitionDevice("system", target(), &system_device));
EXPECT_EQ("/fake/remapped/" + T("system"), system_device);
// Static partition "bar".
EXPECT_CALL(dynamicControl(), GetState(S("bar"))).Times(0);
std::string bar_device;
EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", source(), &bar_device));
EXPECT_EQ(GetDevice(S("bar")), bar_device);
EXPECT_CALL(dynamicControl(), GetState(T("bar"))).Times(0);
EXPECT_TRUE(bootctl_.GetPartitionDevice("bar", target(), &bar_device));
EXPECT_EQ(GetDevice(T("bar")), bar_device);
}
INSTANTIATE_TEST_CASE_P(BootControlAndroidTest,
BootControlAndroidTestP,
testing::Values(TestParam{0, 1}, TestParam{1, 0}));
TEST_F(BootControlAndroidTest, ApplyingToCurrentSlot) {
SetSlots({1, 1});
EXPECT_FALSE(InitPartitionMetadata(target(), {}))
<< "Should not be able to apply to current slot.";
}
} // namespace chromeos_update_engine
|