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 "snapshot_metadata_updater.h"
18
19 #include <memory>
20 #include <string>
21
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 #include <liblp/builder.h>
25 #include <storage_literals/storage_literals.h>
26
27 #include "test_helpers.h"
28
29 using namespace android::storage_literals;
30 using android::fs_mgr::LpMetadata;
31 using android::fs_mgr::MetadataBuilder;
32 using android::fs_mgr::SlotSuffixForSlotNumber;
33 using chromeos_update_engine::DeltaArchiveManifest;
34 using chromeos_update_engine::DynamicPartitionGroup;
35 using chromeos_update_engine::PartitionUpdate;
36 using testing::AssertionFailure;
37 using testing::AssertionResult;
38 using testing::AssertionSuccess;
39
40 namespace android {
41 namespace snapshot {
42
43 class SnapshotMetadataUpdaterTest : public ::testing::TestWithParam<uint32_t> {
44 public:
SetUp()45 void SetUp() override {
46 target_slot_ = GetParam();
47 target_suffix_ = SlotSuffixForSlotNumber(target_slot_);
48 SnapshotTestPropertyFetcher::SetUp(SlotSuffixForSlotNumber(1 - target_slot_));
49 builder_ = MetadataBuilder::New(4_GiB + 1_MiB, 4_KiB, 2);
50
51 group_ = manifest_.mutable_dynamic_partition_metadata()->add_groups();
52 group_->set_name("group");
53 group_->set_size(4_GiB);
54 group_->add_partition_names("system");
55 group_->add_partition_names("vendor");
56 system_ = manifest_.add_partitions();
57 system_->set_partition_name("system");
58 SetSize(system_, 2_GiB);
59 vendor_ = manifest_.add_partitions();
60 vendor_->set_partition_name("vendor");
61 SetSize(vendor_, 1_GiB);
62
63 ASSERT_TRUE(FillFakeMetadata(builder_.get(), manifest_, target_suffix_));
64 }
65
TearDown()66 void TearDown() override { SnapshotTestPropertyFetcher::TearDown(); }
67
68 // Append suffix to name.
T(std::string_view name)69 std::string T(std::string_view name) { return std::string(name) + target_suffix_; }
70
UpdateAndExport()71 AssertionResult UpdateAndExport() {
72 SnapshotMetadataUpdater updater(builder_.get(), target_slot_, manifest_);
73 if (!updater.Update()) {
74 return AssertionFailure() << "Update failed.";
75 }
76
77 exported_ = builder_->Export();
78 if (exported_ == nullptr) {
79 return AssertionFailure() << "Export failed.";
80 }
81 return AssertionSuccess();
82 }
83
84 // Check that in |builder_|, partition |name| + |target_suffix_| has the given |size|.
CheckSize(std::string_view name,uint64_t size)85 AssertionResult CheckSize(std::string_view name, uint64_t size) {
86 auto p = builder_->FindPartition(T(name));
87 if (p == nullptr) {
88 return AssertionFailure() << "Cannot find partition " << T(name);
89 }
90 if (p->size() != size) {
91 return AssertionFailure() << "Partition " << T(name) << " should be " << size
92 << " bytes, but is " << p->size() << " bytes.";
93 }
94 return AssertionSuccess() << "Partition" << T(name) << " is " << size << " bytes.";
95 }
96
97 // Check that in |builder_|, group |name| + |target_suffix_| has the given |size|.
CheckGroupSize(std::string_view name,uint64_t size)98 AssertionResult CheckGroupSize(std::string_view name, uint64_t size) {
99 auto g = builder_->FindGroup(T(name));
100 if (g == nullptr) {
101 return AssertionFailure() << "Cannot find group " << T(name);
102 }
103 if (g->maximum_size() != size) {
104 return AssertionFailure() << "Group " << T(name) << " should be " << size
105 << " bytes, but is " << g->maximum_size() << " bytes.";
106 }
107 return AssertionSuccess() << "Group" << T(name) << " is " << size << " bytes.";
108 }
109
110 // Check that in |builder_|, partition |partition_name| + |target_suffix_| is in group
111 // |group_name| + |target_suffix_|;
CheckGroupName(std::string_view partition_name,std::string_view group_name)112 AssertionResult CheckGroupName(std::string_view partition_name, std::string_view group_name) {
113 auto p = builder_->FindPartition(T(partition_name));
114 if (p == nullptr) {
115 return AssertionFailure() << "Cannot find partition " << T(partition_name);
116 }
117 if (p->group_name() != T(group_name)) {
118 return AssertionFailure() << "Partition " << T(partition_name) << " should be in "
119 << T(group_name) << ", but is in " << p->group_name() << ".";
120 }
121 return AssertionSuccess() << "Partition" << T(partition_name) << " is in " << T(group_name)
122 << ".";
123 }
124
125 std::unique_ptr<MetadataBuilder> builder_;
126 uint32_t target_slot_;
127 std::string target_suffix_;
128 DeltaArchiveManifest manifest_;
129 std::unique_ptr<LpMetadata> exported_;
130 DynamicPartitionGroup* group_ = nullptr;
131 PartitionUpdate* system_ = nullptr;
132 PartitionUpdate* vendor_ = nullptr;
133 };
134
TEST_P(SnapshotMetadataUpdaterTest,NoChange)135 TEST_P(SnapshotMetadataUpdaterTest, NoChange) {
136 EXPECT_TRUE(UpdateAndExport());
137
138 EXPECT_TRUE(CheckGroupSize("group", 4_GiB));
139 EXPECT_TRUE(CheckSize("system", 2_GiB));
140 EXPECT_TRUE(CheckGroupName("system", "group"));
141 EXPECT_TRUE(CheckSize("vendor", 1_GiB));
142 EXPECT_TRUE(CheckGroupName("vendor", "group"));
143 }
144
TEST_P(SnapshotMetadataUpdaterTest,GrowWithinBounds)145 TEST_P(SnapshotMetadataUpdaterTest, GrowWithinBounds) {
146 SetSize(system_, 2_GiB + 512_MiB);
147 SetSize(vendor_, 1_GiB + 512_MiB);
148
149 ASSERT_TRUE(UpdateAndExport());
150
151 EXPECT_TRUE(CheckSize("system", 2_GiB + 512_MiB));
152 EXPECT_TRUE(CheckSize("vendor", 1_GiB + 512_MiB));
153 }
154
TEST_P(SnapshotMetadataUpdaterTest,GrowOverSuper)155 TEST_P(SnapshotMetadataUpdaterTest, GrowOverSuper) {
156 SetSize(system_, 3_GiB);
157 SetSize(vendor_, 1_GiB + 512_MiB);
158
159 EXPECT_FALSE(UpdateAndExport());
160 }
161
TEST_P(SnapshotMetadataUpdaterTest,GrowOverGroup)162 TEST_P(SnapshotMetadataUpdaterTest, GrowOverGroup) {
163 SetSize(system_, 3_GiB);
164 SetSize(vendor_, 1_GiB + 4_KiB);
165
166 EXPECT_FALSE(UpdateAndExport());
167 }
168
TEST_P(SnapshotMetadataUpdaterTest,Add)169 TEST_P(SnapshotMetadataUpdaterTest, Add) {
170 group_->add_partition_names("product");
171 auto product = manifest_.add_partitions();
172 product->set_partition_name("product");
173 SetSize(product, 1_GiB);
174
175 EXPECT_TRUE(UpdateAndExport());
176
177 EXPECT_TRUE(CheckSize("system", 2_GiB));
178 EXPECT_TRUE(CheckSize("vendor", 1_GiB));
179 EXPECT_TRUE(CheckSize("product", 1_GiB));
180 }
181
TEST_P(SnapshotMetadataUpdaterTest,AddTooBig)182 TEST_P(SnapshotMetadataUpdaterTest, AddTooBig) {
183 group_->add_partition_names("product");
184 auto product = manifest_.add_partitions();
185 product->set_partition_name("product");
186 SetSize(product, 1_GiB + 4_KiB);
187
188 EXPECT_FALSE(UpdateAndExport());
189 }
190
TEST_P(SnapshotMetadataUpdaterTest,ShrinkAll)191 TEST_P(SnapshotMetadataUpdaterTest, ShrinkAll) {
192 SetSize(system_, 1_GiB);
193 SetSize(vendor_, 512_MiB);
194
195 ASSERT_TRUE(UpdateAndExport());
196
197 EXPECT_TRUE(CheckSize("system", 1_GiB));
198 EXPECT_TRUE(CheckSize("vendor", 512_MiB));
199 }
200
TEST_P(SnapshotMetadataUpdaterTest,ShrinkAndGrow)201 TEST_P(SnapshotMetadataUpdaterTest, ShrinkAndGrow) {
202 SetSize(system_, 3_GiB + 512_MiB);
203 SetSize(vendor_, 512_MiB);
204
205 ASSERT_TRUE(UpdateAndExport());
206
207 EXPECT_TRUE(CheckSize("system", 3_GiB + 512_MiB));
208 EXPECT_TRUE(CheckSize("vendor", 512_MiB));
209 }
210
TEST_P(SnapshotMetadataUpdaterTest,ShrinkAndAdd)211 TEST_P(SnapshotMetadataUpdaterTest, ShrinkAndAdd) {
212 SetSize(system_, 2_GiB);
213 SetSize(vendor_, 512_MiB);
214 group_->add_partition_names("product");
215 auto product = manifest_.add_partitions();
216 product->set_partition_name("product");
217 SetSize(product, 1_GiB + 512_MiB);
218
219 ASSERT_TRUE(UpdateAndExport());
220
221 EXPECT_TRUE(CheckSize("system", 2_GiB));
222 EXPECT_TRUE(CheckSize("vendor", 512_MiB));
223 EXPECT_TRUE(CheckSize("product", 1_GiB + 512_MiB));
224 }
225
TEST_P(SnapshotMetadataUpdaterTest,Delete)226 TEST_P(SnapshotMetadataUpdaterTest, Delete) {
227 group_->mutable_partition_names()->RemoveLast();
228 // No need to delete it from manifest.partitions as SnapshotMetadataUpdater
229 // should ignore them (treat them as static partitions).
230
231 EXPECT_TRUE(UpdateAndExport());
232
233 EXPECT_TRUE(CheckSize("system", 2_GiB));
234 EXPECT_EQ(nullptr, builder_->FindPartition(T("vendor")));
235 }
236
TEST_P(SnapshotMetadataUpdaterTest,DeleteAndGrow)237 TEST_P(SnapshotMetadataUpdaterTest, DeleteAndGrow) {
238 group_->mutable_partition_names()->RemoveLast();
239 SetSize(system_, 4_GiB);
240
241 EXPECT_TRUE(UpdateAndExport());
242
243 EXPECT_TRUE(CheckSize("system", 4_GiB));
244 }
245
TEST_P(SnapshotMetadataUpdaterTest,DeleteAndAdd)246 TEST_P(SnapshotMetadataUpdaterTest, DeleteAndAdd) {
247 group_->mutable_partition_names()->RemoveLast();
248 group_->add_partition_names("product");
249 auto product = manifest_.add_partitions();
250 product->set_partition_name("product");
251 SetSize(product, 2_GiB);
252
253 EXPECT_TRUE(UpdateAndExport());
254
255 EXPECT_TRUE(CheckSize("system", 2_GiB));
256 EXPECT_EQ(nullptr, builder_->FindPartition(T("vendor")));
257 EXPECT_TRUE(CheckSize("product", 2_GiB));
258 }
259
TEST_P(SnapshotMetadataUpdaterTest,GrowGroup)260 TEST_P(SnapshotMetadataUpdaterTest, GrowGroup) {
261 group_->set_size(4_GiB + 512_KiB);
262 SetSize(system_, 2_GiB + 256_KiB);
263 SetSize(vendor_, 2_GiB + 256_KiB);
264
265 EXPECT_TRUE(UpdateAndExport());
266
267 EXPECT_TRUE(CheckSize("system", 2_GiB + 256_KiB));
268 EXPECT_TRUE(CheckSize("vendor", 2_GiB + 256_KiB));
269 }
270
TEST_P(SnapshotMetadataUpdaterTest,ShrinkGroup)271 TEST_P(SnapshotMetadataUpdaterTest, ShrinkGroup) {
272 group_->set_size(1_GiB);
273 SetSize(system_, 512_MiB);
274 SetSize(vendor_, 512_MiB);
275
276 EXPECT_TRUE(UpdateAndExport());
277
278 EXPECT_TRUE(CheckSize("system", 512_MiB));
279 EXPECT_TRUE(CheckSize("vendor", 512_MiB));
280 }
281
TEST_P(SnapshotMetadataUpdaterTest,MoveToNewGroup)282 TEST_P(SnapshotMetadataUpdaterTest, MoveToNewGroup) {
283 group_->mutable_partition_names()->RemoveLast();
284 group_->set_size(2_GiB);
285
286 auto another_group = manifest_.mutable_dynamic_partition_metadata()->add_groups();
287 another_group->set_name("another_group");
288 another_group->set_size(2_GiB);
289 another_group->add_partition_names("vendor");
290 SetSize(vendor_, 2_GiB);
291
292 EXPECT_TRUE(UpdateAndExport());
293
294 EXPECT_TRUE(CheckGroupSize("group", 2_GiB));
295 EXPECT_TRUE(CheckGroupSize("another_group", 2_GiB));
296 EXPECT_TRUE(CheckSize("system", 2_GiB));
297 EXPECT_TRUE(CheckGroupName("system", "group"));
298 EXPECT_TRUE(CheckSize("vendor", 2_GiB));
299 EXPECT_TRUE(CheckGroupName("vendor", "another_group"));
300 }
301
TEST_P(SnapshotMetadataUpdaterTest,DeleteAndAddGroup)302 TEST_P(SnapshotMetadataUpdaterTest, DeleteAndAddGroup) {
303 manifest_.mutable_dynamic_partition_metadata()->mutable_groups()->RemoveLast();
304 group_ = nullptr;
305
306 auto another_group = manifest_.mutable_dynamic_partition_metadata()->add_groups();
307 another_group->set_name("another_group");
308 another_group->set_size(4_GiB);
309 another_group->add_partition_names("system");
310 another_group->add_partition_names("vendor");
311 another_group->add_partition_names("product");
312 auto product = manifest_.add_partitions();
313 product->set_partition_name("product");
314 SetSize(product, 1_GiB);
315
316 EXPECT_TRUE(UpdateAndExport());
317
318 EXPECT_EQ(nullptr, builder_->FindGroup(T("group")));
319 EXPECT_TRUE(CheckGroupSize("another_group", 4_GiB));
320 EXPECT_TRUE(CheckSize("system", 2_GiB));
321 EXPECT_TRUE(CheckGroupName("system", "another_group"));
322 EXPECT_TRUE(CheckSize("vendor", 1_GiB));
323 EXPECT_TRUE(CheckGroupName("vendor", "another_group"));
324 EXPECT_TRUE(CheckSize("product", 1_GiB));
325 EXPECT_TRUE(CheckGroupName("product", "another_group"));
326 }
327
328 INSTANTIATE_TEST_SUITE_P(, SnapshotMetadataUpdaterTest, testing::Values(0, 1));
329
330 } // namespace snapshot
331 } // namespace android
332