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 <android-base/properties.h>
18 #include <fs_mgr.h>
19 #include <fstab/fstab.h>
20 #include <gtest/gtest.h>
21 #include <liblp/liblp.h>
22 #include <liblp/metadata_format.h>
23 #include <liblp/partition_opener.h>
24 #include <liblp/property_fetcher.h>
25 
26 #include "liblp_test.h"
27 
28 using namespace android::fs_mgr;
29 using namespace android::fs_mgr::testing;
30 using ::testing::Return;
31 
32 // Compliance test on the actual device with dynamic partitions.
33 class DeviceTest : public LiblpTest {
34   public:
SetUp()35     void SetUp() override {
36         // Read real properties.
37         IPropertyFetcher::OverrideForTesting(std::make_unique<PropertyFetcher>());
38         if (!IPropertyFetcher::GetInstance()->GetBoolProperty("ro.boot.dynamic_partitions",
39                                                               false)) {
40             GTEST_SKIP() << "Device doesn't have dynamic partitions enabled, skipping";
41         }
42     }
43 };
44 
TEST_F(DeviceTest,BlockDeviceInfo)45 TEST_F(DeviceTest, BlockDeviceInfo) {
46     PartitionOpener opener;
47     BlockDeviceInfo device_info;
48     ASSERT_TRUE(opener.GetInfo(fs_mgr_get_super_partition_name(), &device_info));
49 
50     // Check that the device doesn't give us some weird inefficient
51     // alignment.
52     EXPECT_EQ(device_info.alignment % LP_SECTOR_SIZE, 0);
53     EXPECT_EQ(device_info.logical_block_size % LP_SECTOR_SIZE, 0);
54 }
55 
TEST_F(DeviceTest,ReadSuperPartitionCurrentSlot)56 TEST_F(DeviceTest, ReadSuperPartitionCurrentSlot) {
57     auto slot_suffix = fs_mgr_get_slot_suffix();
58     auto slot_number = SlotNumberForSlotSuffix(slot_suffix);
59     auto super_name = fs_mgr_get_super_partition_name(slot_number);
60     auto metadata = ReadMetadata(super_name, slot_number);
61     EXPECT_NE(metadata, nullptr);
62 }
63 
TEST_F(DeviceTest,ReadSuperPartitionOtherSlot)64 TEST_F(DeviceTest, ReadSuperPartitionOtherSlot) {
65     auto other_slot_suffix = fs_mgr_get_other_slot_suffix();
66     if (other_slot_suffix.empty()) {
67         GTEST_SKIP() << "No other slot, skipping";
68     }
69     if (IPropertyFetcher::GetInstance()->GetBoolProperty("ro.boot.dynamic_partitions_retrofit",
70                                                          false)) {
71         GTEST_SKIP() << "Device with retrofit dynamic partition may not have metadata at other "
72                      << "slot, skipping";
73     }
74 
75     auto other_slot_number = SlotNumberForSlotSuffix(other_slot_suffix);
76     auto other_super_name = fs_mgr_get_super_partition_name(other_slot_number);
77     auto other_metadata = ReadMetadata(other_super_name, other_slot_number);
78     EXPECT_NE(other_metadata, nullptr);
79 }
80