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
33 // Returns true iff the device has the specified feature.
DeviceSupportsFeature(const char * feature)34 bool DeviceSupportsFeature(const char* feature) {
35 bool device_supports_feature = false;
36 FILE* p = popen("pm list features", "re");
37 if (p) {
38 char* line = NULL;
39 size_t len = 0;
40 while (getline(&line, &len, p) > 0) {
41 if (strstr(line, feature)) {
42 device_supports_feature = true;
43 break;
44 }
45 }
46 pclose(p);
47 }
48 return device_supports_feature;
49 }
50
TEST(fs,ErofsSupported)51 TEST(fs, ErofsSupported) {
52 // T-launch GKI kernels and higher must support EROFS.
53 if (GetVsrLevel() < __ANDROID_API_T__) {
54 GTEST_SKIP();
55 }
56
57 struct utsname uts;
58 ASSERT_EQ(uname(&uts), 0);
59
60 unsigned int major, minor;
61 ASSERT_EQ(sscanf(uts.release, "%u.%u", &major, &minor), 2);
62
63 // EROFS support only required in 5.10+
64 if (major < 5 || (major == 5 && minor < 10)) {
65 GTEST_SKIP();
66 }
67
68 std::string fs;
69 ASSERT_TRUE(android::base::ReadFileToString("/proc/filesystems", &fs));
70 EXPECT_THAT(fs, ::testing::HasSubstr("\terofs\n"));
71
72 ASSERT_EQ(access("/sys/fs/erofs", F_OK), 0);
73 }
74
TEST(fs,PartitionTypes)75 TEST(fs, PartitionTypes) {
76 // Requirements only apply to Android 13+, 5.10+ devices.
77 int vsr_level = GetVsrLevel();
78 if (vsr_level < __ANDROID_API_T__) {
79 GTEST_SKIP();
80 }
81
82 struct utsname uts;
83 ASSERT_EQ(uname(&uts), 0);
84
85 unsigned int major, minor;
86 ASSERT_EQ(sscanf(uts.release, "%u.%u", &major, &minor), 2);
87 if (major < 5 || (major == 5 && minor < 10)) {
88 GTEST_SKIP();
89 }
90
91 android::fs_mgr::Fstab fstab;
92 ASSERT_TRUE(android::fs_mgr::ReadFstabFromFile("/proc/mounts", &fstab));
93
94 auto& dm = android::dm::DeviceMapper::Instance();
95
96 std::string super_bdev, userdata_bdev;
97 ASSERT_TRUE(android::base::Readlink("/dev/block/by-name/super", &super_bdev));
98 ASSERT_TRUE(android::base::Readlink("/dev/block/by-name/userdata", &userdata_bdev));
99
100 std::vector<std::string> must_be_f2fs = {"/data"};
101 if (vsr_level >= __ANDROID_API_U__ &&
102 !DeviceSupportsFeature("android.hardware.type.automotive")) {
103 must_be_f2fs.emplace_back("/metadata");
104 }
105
106 for (const auto& entry : fstab) {
107 std::string parent_bdev = entry.blk_device;
108 while (true) {
109 auto basename = android::base::Basename(parent_bdev);
110 if (!android::base::StartsWith(basename, "dm-")) {
111 break;
112 }
113
114 auto parent = dm.GetParentBlockDeviceByPath(parent_bdev);
115 if (!parent || *parent == parent_bdev) {
116 break;
117 }
118 parent_bdev = *parent;
119 }
120
121 if (parent_bdev == userdata_bdev ||
122 android::base::StartsWith(parent_bdev, "/dev/block/loop")) {
123 if (entry.flags & MS_RDONLY) {
124 // APEXes should not be F2FS.
125 EXPECT_NE(entry.fs_type, "f2fs");
126 }
127 continue;
128 }
129
130 if (entry.flags & MS_RDONLY) {
131 if (parent_bdev != super_bdev) {
132 // Ignore non-AOSP partitions (eg anything outside of super).
133 continue;
134 }
135
136 std::vector<std::string> allowed = {"erofs", "ext4", "f2fs"};
137 EXPECT_NE(std::find(allowed.begin(), allowed.end(), entry.fs_type), allowed.end())
138 << entry.mount_point;
139 } else {
140 if (std::find(must_be_f2fs.begin(), must_be_f2fs.end(), entry.mount_point) !=
141 must_be_f2fs.end()) {
142 EXPECT_EQ(entry.fs_type, "f2fs") << entry.mount_point;
143 }
144 }
145 }
146 }
147
TEST(fs,NoDtFstab)148 TEST(fs, NoDtFstab) {
149 if (GetVsrLevel() < __ANDROID_API_Q__) {
150 GTEST_SKIP();
151 }
152
153 android::fs_mgr::Fstab fstab;
154 EXPECT_FALSE(android::fs_mgr::ReadFstabFromDt(&fstab, false));
155 }
156
TEST(fs,NoLegacyVerifiedBoot)157 TEST(fs, NoLegacyVerifiedBoot) {
158 if (GetVsrLevel() < __ANDROID_API_T__) {
159 GTEST_SKIP();
160 }
161
162 const auto& default_fstab_path = android::fs_mgr::GetFstabPath();
163 EXPECT_FALSE(default_fstab_path.empty());
164
165 std::string fstab_str;
166 EXPECT_TRUE(android::base::ReadFileToString(default_fstab_path, &fstab_str,
167 /* follow_symlinks = */ true));
168
169 for (const auto& line : android::base::Split(fstab_str, "\n")) {
170 auto fields = android::base::Tokenize(line, " \t");
171 // Ignores empty lines and comments.
172 if (fields.empty() || android::base::StartsWith(fields.front(), '#')) {
173 continue;
174 }
175 // Each line in a fstab should have at least five entries.
176 // <src> <mnt_point> <type> <mnt_flags and options> <fs_mgr_flags>
177 ASSERT_GE(fields.size(), 5);
178 EXPECT_THAT(android::base::Split(fields[4], ","), Not(Contains("verify")))
179 << "AVB 1.0 isn't supported now, but the 'verify' flag is found:\n"
180 << " " << line;
181 }
182 }
183