• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <sys/mount.h>
16 #include <sys/utsname.h>
17 
18 #include <android-base/file.h>
19 #include <android-base/properties.h>
20 #include <android-base/strings.h>
21 #include <fstab/fstab.h>
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 #include <libdm/dm.h>
25 
26 using testing::Contains;
27 using testing::Not;
28 
GetVsrLevel()29 static int GetVsrLevel() {
30     return android::base::GetIntProperty("ro.vendor.api_level", -1);
31 }
32 
TEST(fs,ErofsSupported)33 TEST(fs, ErofsSupported) {
34     // T-launch GKI kernels and higher must support EROFS.
35     if (GetVsrLevel() < __ANDROID_API_T__) {
36         GTEST_SKIP();
37     }
38 
39     struct utsname uts;
40     ASSERT_EQ(uname(&uts), 0);
41 
42     unsigned int major, minor;
43     ASSERT_EQ(sscanf(uts.release, "%u.%u", &major, &minor), 2);
44 
45     // EROFS support only required in 5.10+
46     if (major < 5 || (major == 5 && minor < 10)) {
47         GTEST_SKIP();
48     }
49 
50     std::string fs;
51     ASSERT_TRUE(android::base::ReadFileToString("/proc/filesystems", &fs));
52     EXPECT_THAT(fs, ::testing::HasSubstr("\terofs\n"));
53 
54     ASSERT_EQ(access("/sys/fs/erofs", F_OK), 0);
55 }
56 
TEST(fs,PartitionTypes)57 TEST(fs, PartitionTypes) {
58     // Requirements only apply to Android 13+, 5.10+ devices.
59     int vsr_level = GetVsrLevel();
60     if (vsr_level < __ANDROID_API_T__) {
61         GTEST_SKIP();
62     }
63 
64     struct utsname uts;
65     ASSERT_EQ(uname(&uts), 0);
66 
67     unsigned int major, minor;
68     ASSERT_EQ(sscanf(uts.release, "%u.%u", &major, &minor), 2);
69     if (major < 5 || (major == 5 && minor < 10)) {
70         GTEST_SKIP();
71     }
72 
73     android::fs_mgr::Fstab fstab;
74     ASSERT_TRUE(android::fs_mgr::ReadFstabFromFile("/proc/mounts", &fstab));
75 
76     auto& dm = android::dm::DeviceMapper::Instance();
77 
78     std::string super_bdev, userdata_bdev;
79     ASSERT_TRUE(android::base::Readlink("/dev/block/by-name/super", &super_bdev));
80     ASSERT_TRUE(android::base::Readlink("/dev/block/by-name/userdata", &userdata_bdev));
81 
82     std::vector<std::string> must_be_f2fs = {"/data"};
83     if (vsr_level >= __ANDROID_API_U__) {
84         must_be_f2fs.emplace_back("/metadata");
85     }
86 
87     for (const auto& entry : fstab) {
88         std::string parent_bdev = entry.blk_device;
89         while (true) {
90             auto basename = android::base::Basename(parent_bdev);
91             if (!android::base::StartsWith(basename, "dm-")) {
92                 break;
93             }
94 
95             auto parent = dm.GetParentBlockDeviceByPath(parent_bdev);
96             if (!parent || *parent == parent_bdev) {
97                 break;
98             }
99             parent_bdev = *parent;
100         }
101 
102         if (parent_bdev == userdata_bdev ||
103             android::base::StartsWith(parent_bdev, "/dev/block/loop")) {
104             if (entry.flags & MS_RDONLY) {
105                 // APEXes should not be F2FS.
106                 EXPECT_NE(entry.fs_type, "f2fs");
107             }
108             continue;
109         }
110 
111         if (entry.flags & MS_RDONLY) {
112             if (parent_bdev != super_bdev) {
113                 // Ignore non-AOSP partitions (eg anything outside of super).
114                 continue;
115             }
116 
117             std::vector<std::string> allowed = {"erofs", "ext4", "f2fs"};
118             EXPECT_NE(std::find(allowed.begin(), allowed.end(), entry.fs_type), allowed.end())
119                     << entry.mount_point;
120         } else {
121             if (std::find(must_be_f2fs.begin(), must_be_f2fs.end(), entry.mount_point) !=
122                 must_be_f2fs.end()) {
123                 EXPECT_EQ(entry.fs_type, "f2fs") << entry.mount_point;
124             }
125         }
126     }
127 }
128 
TEST(fs,NoDtFstab)129 TEST(fs, NoDtFstab) {
130     if (GetVsrLevel() < __ANDROID_API_Q__) {
131         GTEST_SKIP();
132     }
133 
134     android::fs_mgr::Fstab fstab;
135     EXPECT_FALSE(android::fs_mgr::ReadFstabFromDt(&fstab, false));
136 }
137 
TEST(fs,NoLegacyVerifiedBoot)138 TEST(fs, NoLegacyVerifiedBoot) {
139     if (GetVsrLevel() < __ANDROID_API_T__) {
140         GTEST_SKIP();
141     }
142 
143     const auto& default_fstab_path = android::fs_mgr::GetFstabPath();
144     EXPECT_FALSE(default_fstab_path.empty());
145 
146     std::string fstab_str;
147     EXPECT_TRUE(android::base::ReadFileToString(default_fstab_path, &fstab_str,
148                                                 /* follow_symlinks = */ true));
149 
150     for (const auto& line : android::base::Split(fstab_str, "\n")) {
151         auto fields = android::base::Tokenize(line, " \t");
152         // Ignores empty lines and comments.
153         if (fields.empty() || android::base::StartsWith(fields.front(), '#')) {
154             continue;
155         }
156         // Each line in a fstab should have at least five entries.
157         //   <src> <mnt_point> <type> <mnt_flags and options> <fs_mgr_flags>
158         ASSERT_GE(fields.size(), 5);
159         EXPECT_THAT(android::base::Split(fields[4], ","), Not(Contains("verify")))
160                 << "AVB 1.0 isn't supported now, but the 'verify' flag is found:\n"
161                 << "  " << line;
162     }
163 }
164