• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
18 #include <liblp/liblp.h>
19 
20 #include "utility.h"
21 
22 using namespace android;
23 using namespace android::fs_mgr;
24 
TEST(liblp,SlotNumberForSlotSuffix)25 TEST(liblp, SlotNumberForSlotSuffix) {
26     EXPECT_EQ(SlotNumberForSlotSuffix(""), 0);
27     EXPECT_EQ(SlotNumberForSlotSuffix("a"), 0);
28     EXPECT_EQ(SlotNumberForSlotSuffix("_a"), 0);
29     EXPECT_EQ(SlotNumberForSlotSuffix("b"), 1);
30     EXPECT_EQ(SlotNumberForSlotSuffix("_b"), 1);
31     EXPECT_EQ(SlotNumberForSlotSuffix("_c"), 0);
32     EXPECT_EQ(SlotNumberForSlotSuffix("_d"), 0);
33 }
34 
TEST(liblp,SlotSuffixForSlotNumber)35 TEST(liblp, SlotSuffixForSlotNumber) {
36     EXPECT_EQ(SlotSuffixForSlotNumber(0), "_a");
37     EXPECT_EQ(SlotSuffixForSlotNumber(1), "_b");
38 }
39 
TEST(liblp,GetMetadataOffset)40 TEST(liblp, GetMetadataOffset) {
41     LpMetadataGeometry geometry = {LP_METADATA_GEOMETRY_MAGIC,
42                                    sizeof(geometry),
43                                    {0},
44                                    16384,
45                                    4,
46                                    4096};
47     static const uint64_t start = LP_PARTITION_RESERVED_BYTES;
48     EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 0), start + 8192);
49     EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 1), start + 8192 + 16384);
50     EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 2), start + 8192 + 16384 * 2);
51     EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 3), start + 8192 + 16384 * 3);
52 
53     static const uint64_t backup_start = start + 8192 + 16384 * 4;
54     EXPECT_EQ(GetBackupMetadataOffset(geometry, 3), backup_start + 16384 * 3);
55     EXPECT_EQ(GetBackupMetadataOffset(geometry, 2), backup_start + 16384 * 2);
56     EXPECT_EQ(GetBackupMetadataOffset(geometry, 1), backup_start + 16384 * 1);
57     EXPECT_EQ(GetBackupMetadataOffset(geometry, 0), backup_start + 16384 * 0);
58 }
59 
TEST(liblp,AlignTo)60 TEST(liblp, AlignTo) {
61     EXPECT_EQ(AlignTo(37, 0), 37);
62     EXPECT_EQ(AlignTo(1024, 1024), 1024);
63     EXPECT_EQ(AlignTo(555, 1024), 1024);
64     EXPECT_EQ(AlignTo(555, 1000), 1000);
65     EXPECT_EQ(AlignTo(0, 1024), 0);
66     EXPECT_EQ(AlignTo(54, 32, 30), 62);
67     EXPECT_EQ(AlignTo(32, 32, 30), 62);
68     EXPECT_EQ(AlignTo(17, 32, 30), 30);
69 }
70 
TEST(liblp,GetPartitionSlotSuffix)71 TEST(liblp, GetPartitionSlotSuffix) {
72     EXPECT_EQ(GetPartitionSlotSuffix("system"), "");
73     EXPECT_EQ(GetPartitionSlotSuffix("_"), "");
74     EXPECT_EQ(GetPartitionSlotSuffix("_a"), "");
75     EXPECT_EQ(GetPartitionSlotSuffix("system_a"), "_a");
76     EXPECT_EQ(GetPartitionSlotSuffix("system_b"), "_b");
77 }
78