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 <optional>
18
19 #include <gtest/gtest.h>
20 #include <liblp/builder.h>
21 #include <liblp/liblp.h>
22
23 #include "utility.h"
24
25 using namespace android;
26 using namespace android::fs_mgr;
27
TEST(liblp,SlotNumberForSlotSuffix)28 TEST(liblp, SlotNumberForSlotSuffix) {
29 EXPECT_EQ(SlotNumberForSlotSuffix(""), 0);
30 EXPECT_EQ(SlotNumberForSlotSuffix("a"), 0);
31 EXPECT_EQ(SlotNumberForSlotSuffix("_a"), 0);
32 EXPECT_EQ(SlotNumberForSlotSuffix("b"), 1);
33 EXPECT_EQ(SlotNumberForSlotSuffix("_b"), 1);
34 EXPECT_EQ(SlotNumberForSlotSuffix("_c"), 0);
35 EXPECT_EQ(SlotNumberForSlotSuffix("_d"), 0);
36 }
37
TEST(liblp,SlotSuffixForSlotNumber)38 TEST(liblp, SlotSuffixForSlotNumber) {
39 EXPECT_EQ(SlotSuffixForSlotNumber(0), "_a");
40 EXPECT_EQ(SlotSuffixForSlotNumber(1), "_b");
41 }
42
TEST(liblp,GetMetadataOffset)43 TEST(liblp, GetMetadataOffset) {
44 LpMetadataGeometry geometry = {LP_METADATA_GEOMETRY_MAGIC,
45 sizeof(geometry),
46 {0},
47 16384,
48 4,
49 4096};
50 static const uint64_t start = LP_PARTITION_RESERVED_BYTES;
51 EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 0), start + 8192);
52 EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 1), start + 8192 + 16384);
53 EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 2), start + 8192 + 16384 * 2);
54 EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 3), start + 8192 + 16384 * 3);
55
56 static const uint64_t backup_start = start + 8192 + 16384 * 4;
57 EXPECT_EQ(GetBackupMetadataOffset(geometry, 3), backup_start + 16384 * 3);
58 EXPECT_EQ(GetBackupMetadataOffset(geometry, 2), backup_start + 16384 * 2);
59 EXPECT_EQ(GetBackupMetadataOffset(geometry, 1), backup_start + 16384 * 1);
60 EXPECT_EQ(GetBackupMetadataOffset(geometry, 0), backup_start + 16384 * 0);
61 }
62
AlignTo(uint64_t base,uint32_t alignment)63 std::optional<uint64_t> AlignTo(uint64_t base, uint32_t alignment) {
64 uint64_t r;
65 if (!AlignTo(base, alignment, &r)) {
66 return {};
67 }
68 return {r};
69 }
70
TEST(liblp,AlignTo)71 TEST(liblp, AlignTo) {
72 EXPECT_EQ(AlignTo(37, 0), std::optional<uint64_t>(37));
73 EXPECT_EQ(AlignTo(1024, 1024), std::optional<uint64_t>(1024));
74 EXPECT_EQ(AlignTo(555, 1024), std::optional<uint64_t>(1024));
75 EXPECT_EQ(AlignTo(555, 1000), std::optional<uint64_t>(1000));
76 EXPECT_EQ(AlignTo(0, 1024), std::optional<uint64_t>(0));
77 EXPECT_EQ(AlignTo(54, 32), std::optional<uint64_t>(64));
78 EXPECT_EQ(AlignTo(32, 32), std::optional<uint64_t>(32));
79 EXPECT_EQ(AlignTo(17, 32), std::optional<uint64_t>(32));
80
81 auto u32limit = std::numeric_limits<uint32_t>::max();
82 auto u64limit = std::numeric_limits<uint64_t>::max();
83 EXPECT_EQ(AlignTo(u64limit - u32limit + 1, u32limit), std::optional<uint64_t>{u64limit});
84 EXPECT_EQ(AlignTo(std::numeric_limits<uint64_t>::max(), 2), std::optional<uint64_t>{});
85 }
86
TEST(liblp,GetPartitionSlotSuffix)87 TEST(liblp, GetPartitionSlotSuffix) {
88 EXPECT_EQ(GetPartitionSlotSuffix("system"), "");
89 EXPECT_EQ(GetPartitionSlotSuffix("_"), "");
90 EXPECT_EQ(GetPartitionSlotSuffix("_a"), "");
91 EXPECT_EQ(GetPartitionSlotSuffix("system_a"), "_a");
92 EXPECT_EQ(GetPartitionSlotSuffix("system_b"), "_b");
93 }
94
95 namespace android {
96 namespace fs_mgr {
97 // Equality comparison for testing. In reality, equality of device_index doesn't
98 // necessary mean equality of the block device.
operator ==(const LinearExtent & l,const LinearExtent & r)99 bool operator==(const LinearExtent& l, const LinearExtent& r) {
100 return l.device_index() == r.device_index() && l.physical_sector() == r.physical_sector() &&
101 l.end_sector() == r.end_sector();
102 }
103 } // namespace fs_mgr
104 } // namespace android
105
GetPartitionExtents(Partition * p)106 static std::vector<LinearExtent> GetPartitionExtents(Partition* p) {
107 std::vector<LinearExtent> extents;
108 for (auto&& extent : p->extents()) {
109 auto linear_extent = extent->AsLinearExtent();
110 if (!linear_extent) return {};
111 extents.push_back(*linear_extent);
112 }
113 return extents;
114 }
115
TEST(liblp,UpdateMetadataForInPlaceSnapshot)116 TEST(liblp, UpdateMetadataForInPlaceSnapshot) {
117 using std::unique_ptr;
118
119 unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
120 ASSERT_NE(builder, nullptr);
121
122 ASSERT_TRUE(builder->AddGroup("group_a", 256 * 1024));
123 Partition* system_a = builder->AddPartition("system_a", "group_a", LP_PARTITION_ATTR_READONLY);
124 ASSERT_NE(system_a, nullptr);
125 ASSERT_TRUE(builder->ResizePartition(system_a, 40 * 1024));
126 Partition* vendor_a = builder->AddPartition("vendor_a", "group_a", LP_PARTITION_ATTR_READONLY);
127 ASSERT_NE(vendor_a, nullptr);
128 ASSERT_TRUE(builder->ResizePartition(vendor_a, 20 * 1024));
129
130 ASSERT_TRUE(builder->AddGroup("group_b", 258 * 1024));
131 Partition* system_b = builder->AddPartition("system_b", "group_b", LP_PARTITION_ATTR_READONLY);
132 ASSERT_NE(system_b, nullptr);
133 ASSERT_TRUE(builder->ResizePartition(system_b, 36 * 1024));
134 Partition* vendor_b = builder->AddPartition("vendor_b", "group_b", LP_PARTITION_ATTR_READONLY);
135 ASSERT_NE(vendor_b, nullptr);
136 ASSERT_TRUE(builder->ResizePartition(vendor_b, 32 * 1024));
137
138 auto system_a_extents = GetPartitionExtents(system_a);
139 ASSERT_FALSE(system_a_extents.empty());
140
141 auto vendor_a_extents = GetPartitionExtents(vendor_a);
142 ASSERT_FALSE(vendor_a_extents.empty());
143
144 auto metadata = builder->Export();
145 ASSERT_NE(nullptr, metadata);
146
147 ASSERT_TRUE(UpdateMetadataForInPlaceSnapshot(metadata.get(), 0, 1));
148
149 auto new_builder = MetadataBuilder::New(*metadata);
150 ASSERT_NE(nullptr, new_builder);
151
152 EXPECT_EQ(nullptr, new_builder->FindGroup("group_a"));
153 EXPECT_EQ(nullptr, new_builder->FindPartition("system_a"));
154 EXPECT_EQ(nullptr, new_builder->FindPartition("vendor_a"));
155
156 auto group_b = new_builder->FindGroup("group_b");
157 ASSERT_NE(nullptr, group_b);
158 ASSERT_EQ(256 * 1024, group_b->maximum_size());
159
160 auto new_system_b = new_builder->FindPartition("system_b");
161 ASSERT_NE(nullptr, new_system_b);
162 EXPECT_EQ(40 * 1024, new_system_b->size());
163 auto new_system_b_extents = GetPartitionExtents(new_system_b);
164 EXPECT_EQ(system_a_extents, new_system_b_extents);
165
166 auto new_vendor_b = new_builder->FindPartition("vendor_b");
167 ASSERT_NE(nullptr, new_vendor_b);
168 EXPECT_EQ(20 * 1024, new_vendor_b->size());
169 auto new_vendor_b_extents = GetPartitionExtents(new_vendor_b);
170 EXPECT_EQ(vendor_a_extents, new_vendor_b_extents);
171 }
172