xref: /aosp_15_r20/system/gsid/tests/boot_tests.cpp (revision 4e2b41f188908a2ae9d9a2089f1f10779d080021)
1*4e2b41f1SAndroid Build Coastguard Worker //
2*4e2b41f1SAndroid Build Coastguard Worker // Copyright (C) 2019 The Android Open Source Project
3*4e2b41f1SAndroid Build Coastguard Worker //
4*4e2b41f1SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*4e2b41f1SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*4e2b41f1SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*4e2b41f1SAndroid Build Coastguard Worker //
8*4e2b41f1SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*4e2b41f1SAndroid Build Coastguard Worker //
10*4e2b41f1SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*4e2b41f1SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*4e2b41f1SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4e2b41f1SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*4e2b41f1SAndroid Build Coastguard Worker // limitations under the License.
15*4e2b41f1SAndroid Build Coastguard Worker //
16*4e2b41f1SAndroid Build Coastguard Worker 
17*4e2b41f1SAndroid Build Coastguard Worker #include <linux/fs.h>
18*4e2b41f1SAndroid Build Coastguard Worker #include <sys/stat.h>
19*4e2b41f1SAndroid Build Coastguard Worker #include <sys/sysmacros.h>
20*4e2b41f1SAndroid Build Coastguard Worker #include <sys/types.h>
21*4e2b41f1SAndroid Build Coastguard Worker #include <sys/vfs.h>
22*4e2b41f1SAndroid Build Coastguard Worker #include <unistd.h>
23*4e2b41f1SAndroid Build Coastguard Worker 
24*4e2b41f1SAndroid Build Coastguard Worker #include <android-base/properties.h>
25*4e2b41f1SAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
26*4e2b41f1SAndroid Build Coastguard Worker #include <android/hardware/weaver/1.0/IWeaver.h>
27*4e2b41f1SAndroid Build Coastguard Worker #include <ext4_utils/ext4_utils.h>
28*4e2b41f1SAndroid Build Coastguard Worker #include <fstab/fstab.h>
29*4e2b41f1SAndroid Build Coastguard Worker #include <gtest/gtest.h>
30*4e2b41f1SAndroid Build Coastguard Worker 
31*4e2b41f1SAndroid Build Coastguard Worker using namespace android::fs_mgr;
32*4e2b41f1SAndroid Build Coastguard Worker 
33*4e2b41f1SAndroid Build Coastguard Worker using android::base::unique_fd;
34*4e2b41f1SAndroid Build Coastguard Worker using android::hardware::weaver::V1_0::IWeaver;
35*4e2b41f1SAndroid Build Coastguard Worker using android::hardware::weaver::V1_0::WeaverConfig;
36*4e2b41f1SAndroid Build Coastguard Worker using android::hardware::weaver::V1_0::WeaverStatus;
37*4e2b41f1SAndroid Build Coastguard Worker 
38*4e2b41f1SAndroid Build Coastguard Worker 
IsAutomotiveDevice()39*4e2b41f1SAndroid Build Coastguard Worker static bool IsAutomotiveDevice() {
40*4e2b41f1SAndroid Build Coastguard Worker     auto hw_type = android::base::GetProperty("ro.hardware.type", "");
41*4e2b41f1SAndroid Build Coastguard Worker     return hw_type == "automotive";
42*4e2b41f1SAndroid Build Coastguard Worker }
43*4e2b41f1SAndroid Build Coastguard Worker 
ShouldRequireMetadata()44*4e2b41f1SAndroid Build Coastguard Worker bool ShouldRequireMetadata() {
45*4e2b41f1SAndroid Build Coastguard Worker     int api_level = android::base::GetIntProperty("ro.product.first_api_level", -1);
46*4e2b41f1SAndroid Build Coastguard Worker     return api_level >= __ANDROID_API_R__;
47*4e2b41f1SAndroid Build Coastguard Worker }
48*4e2b41f1SAndroid Build Coastguard Worker 
TEST(MetadataPartition,FirstStageMount)49*4e2b41f1SAndroid Build Coastguard Worker TEST(MetadataPartition, FirstStageMount) {
50*4e2b41f1SAndroid Build Coastguard Worker     if (!ShouldRequireMetadata()) {
51*4e2b41f1SAndroid Build Coastguard Worker         GTEST_SKIP();
52*4e2b41f1SAndroid Build Coastguard Worker     }
53*4e2b41f1SAndroid Build Coastguard Worker     Fstab fstab;
54*4e2b41f1SAndroid Build Coastguard Worker     if (ReadFstabFromDt(&fstab)) {
55*4e2b41f1SAndroid Build Coastguard Worker         auto entry = GetEntryForMountPoint(&fstab, "/metadata");
56*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_NE(entry, nullptr);
57*4e2b41f1SAndroid Build Coastguard Worker     } else {
58*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_TRUE(ReadDefaultFstab(&fstab));
59*4e2b41f1SAndroid Build Coastguard Worker         auto entry = GetEntryForMountPoint(&fstab, "/metadata");
60*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_NE(entry, nullptr);
61*4e2b41f1SAndroid Build Coastguard Worker         EXPECT_TRUE(entry->fs_mgr_flags.first_stage_mount);
62*4e2b41f1SAndroid Build Coastguard Worker     }
63*4e2b41f1SAndroid Build Coastguard Worker }
64*4e2b41f1SAndroid Build Coastguard Worker 
GetVsrLevel()65*4e2b41f1SAndroid Build Coastguard Worker static int GetVsrLevel() {
66*4e2b41f1SAndroid Build Coastguard Worker     return android::base::GetIntProperty("ro.vendor.api_level", -1);
67*4e2b41f1SAndroid Build Coastguard Worker }
68*4e2b41f1SAndroid Build Coastguard Worker 
TEST(MetadataPartition,MinimumSize)69*4e2b41f1SAndroid Build Coastguard Worker TEST(MetadataPartition, MinimumSize) {
70*4e2b41f1SAndroid Build Coastguard Worker     if (!ShouldRequireMetadata()) {
71*4e2b41f1SAndroid Build Coastguard Worker         GTEST_SKIP();
72*4e2b41f1SAndroid Build Coastguard Worker     }
73*4e2b41f1SAndroid Build Coastguard Worker     Fstab fstab;
74*4e2b41f1SAndroid Build Coastguard Worker     ASSERT_TRUE(ReadDefaultFstab(&fstab));
75*4e2b41f1SAndroid Build Coastguard Worker 
76*4e2b41f1SAndroid Build Coastguard Worker     auto entry = GetEntryForMountPoint(&fstab, "/metadata");
77*4e2b41f1SAndroid Build Coastguard Worker     ASSERT_NE(entry, nullptr);
78*4e2b41f1SAndroid Build Coastguard Worker 
79*4e2b41f1SAndroid Build Coastguard Worker     unique_fd fd(open(entry->blk_device.c_str(), O_RDONLY | O_CLOEXEC));
80*4e2b41f1SAndroid Build Coastguard Worker     ASSERT_GE(fd, 0);
81*4e2b41f1SAndroid Build Coastguard Worker 
82*4e2b41f1SAndroid Build Coastguard Worker     uint64_t size = get_block_device_size(fd);
83*4e2b41f1SAndroid Build Coastguard Worker     ASSERT_GE(size, 16777216);
84*4e2b41f1SAndroid Build Coastguard Worker }
85*4e2b41f1SAndroid Build Coastguard Worker 
TEST(Weaver,MinimumSlots)86*4e2b41f1SAndroid Build Coastguard Worker TEST(Weaver, MinimumSlots) {
87*4e2b41f1SAndroid Build Coastguard Worker     auto weaver = IWeaver::getService();
88*4e2b41f1SAndroid Build Coastguard Worker     if (!weaver) {
89*4e2b41f1SAndroid Build Coastguard Worker         return;
90*4e2b41f1SAndroid Build Coastguard Worker     }
91*4e2b41f1SAndroid Build Coastguard Worker 
92*4e2b41f1SAndroid Build Coastguard Worker     WeaverStatus hw_status;
93*4e2b41f1SAndroid Build Coastguard Worker     WeaverConfig hw_config;
94*4e2b41f1SAndroid Build Coastguard Worker 
95*4e2b41f1SAndroid Build Coastguard Worker     auto res = weaver->getConfig([&](WeaverStatus status, const WeaverConfig& config) {
96*4e2b41f1SAndroid Build Coastguard Worker             hw_status = status;
97*4e2b41f1SAndroid Build Coastguard Worker             hw_config = config;
98*4e2b41f1SAndroid Build Coastguard Worker     });
99*4e2b41f1SAndroid Build Coastguard Worker     ASSERT_TRUE(res.isOk());
100*4e2b41f1SAndroid Build Coastguard Worker     ASSERT_EQ(hw_status, WeaverStatus::OK);
101*4e2b41f1SAndroid Build Coastguard Worker     EXPECT_GE(hw_config.slots, 16);
102*4e2b41f1SAndroid Build Coastguard Worker }
103*4e2b41f1SAndroid Build Coastguard Worker 
TEST(MetadataPartition,FsType)104*4e2b41f1SAndroid Build Coastguard Worker TEST(MetadataPartition, FsType) {
105*4e2b41f1SAndroid Build Coastguard Worker     if (GetVsrLevel() < __ANDROID_API_T__ || IsAutomotiveDevice()) {
106*4e2b41f1SAndroid Build Coastguard Worker         GTEST_SKIP();
107*4e2b41f1SAndroid Build Coastguard Worker     }
108*4e2b41f1SAndroid Build Coastguard Worker 
109*4e2b41f1SAndroid Build Coastguard Worker     Fstab fstab;
110*4e2b41f1SAndroid Build Coastguard Worker     ASSERT_TRUE(ReadDefaultFstab(&fstab));
111*4e2b41f1SAndroid Build Coastguard Worker 
112*4e2b41f1SAndroid Build Coastguard Worker     std::vector<std::string> mount_points = {"/data"};
113*4e2b41f1SAndroid Build Coastguard Worker     for (const auto& mount_point : mount_points) {
114*4e2b41f1SAndroid Build Coastguard Worker         auto path = mount_point + "/gsi";
115*4e2b41f1SAndroid Build Coastguard Worker 
116*4e2b41f1SAndroid Build Coastguard Worker         // These paths should not be symlinks.
117*4e2b41f1SAndroid Build Coastguard Worker         struct stat s;
118*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_GE(lstat(path.c_str(), &s), 0) << path;
119*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_FALSE(S_ISLNK(s.st_mode));
120*4e2b41f1SAndroid Build Coastguard Worker 
121*4e2b41f1SAndroid Build Coastguard Worker         struct statfs64 fs;
122*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_GE(statfs64(path.c_str(), &fs), 0) << path;
123*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_TRUE(fs.f_type == F2FS_SUPER_MAGIC || fs.f_type == EXT4_SUPER_MAGIC)
124*4e2b41f1SAndroid Build Coastguard Worker                 << "Unexpected filesystem type: " << fs.f_type;
125*4e2b41f1SAndroid Build Coastguard Worker 
126*4e2b41f1SAndroid Build Coastguard Worker         auto entry = GetEntryForMountPoint(&fstab, mount_point);
127*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_NE(entry, nullptr);
128*4e2b41f1SAndroid Build Coastguard Worker         ASSERT_TRUE(entry->fs_type == "f2fs" || entry->fs_type == "ext4")
129*4e2b41f1SAndroid Build Coastguard Worker                 << "Unexpected filesystem type: " << entry->fs_type;
130*4e2b41f1SAndroid Build Coastguard Worker     }
131*4e2b41f1SAndroid Build Coastguard Worker }
132