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