• 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 
GetVsrLevel()26 static int GetVsrLevel() {
27     return android::base::GetIntProperty("ro.vendor.api_level", -1);
28 }
29 
TEST(fs,ErofsSupported)30 TEST(fs, ErofsSupported) {
31     // T-launch GKI kernels and higher must support EROFS.
32     if (GetVsrLevel() < __ANDROID_API_T__) {
33         GTEST_SKIP();
34     }
35 
36     struct utsname uts;
37     ASSERT_EQ(uname(&uts), 0);
38 
39     unsigned int major, minor;
40     ASSERT_EQ(sscanf(uts.release, "%u.%u", &major, &minor), 2);
41 
42     // EROFS support only required in 5.10+
43     if (major < 5 || (major == 5 && minor < 10)) {
44         GTEST_SKIP();
45     }
46 
47     std::string fs;
48     ASSERT_TRUE(android::base::ReadFileToString("/proc/filesystems", &fs));
49     EXPECT_THAT(fs, ::testing::HasSubstr("\terofs\n"));
50 
51     ASSERT_EQ(access("/sys/fs/erofs", F_OK), 0);
52 }
53 
TEST(fs,PartitionTypes)54 TEST(fs, PartitionTypes) {
55     android::fs_mgr::Fstab fstab;
56     ASSERT_TRUE(android::fs_mgr::ReadFstabFromFile("/proc/mounts", &fstab));
57 
58     auto& dm = android::dm::DeviceMapper::Instance();
59 
60     std::string super_bdev, userdata_bdev;
61     ASSERT_TRUE(android::base::Readlink("/dev/block/by-name/super", &super_bdev));
62     ASSERT_TRUE(android::base::Readlink("/dev/block/by-name/userdata", &userdata_bdev));
63 
64     int vsr_level = GetVsrLevel();
65 
66     for (const auto& entry : fstab) {
67         std::string parent_bdev = entry.blk_device;
68         while (true) {
69             auto basename = android::base::Basename(parent_bdev);
70             if (!android::base::StartsWith(basename, "dm-")) {
71                 break;
72             }
73 
74             auto parent = dm.GetParentBlockDeviceByPath(parent_bdev);
75             if (!parent || *parent == parent_bdev) {
76                 break;
77             }
78             parent_bdev = *parent;
79         }
80 
81         if (parent_bdev == userdata_bdev ||
82             android::base::StartsWith(parent_bdev, "/dev/block/loop")) {
83             if (entry.flags & MS_RDONLY) {
84                 // APEXes should not be F2FS.
85                 EXPECT_NE(entry.fs_type, "f2fs");
86             }
87             continue;
88         }
89 
90         if (vsr_level < __ANDROID_API_T__) {
91             continue;
92         }
93         if (vsr_level == __ANDROID_API_T__ && parent_bdev != super_bdev) {
94             // Only check for dynamic partitions at this VSR level.
95             continue;
96         }
97 
98         if (entry.flags & MS_RDONLY) {
99             std::vector<std::string> allowed = {"erofs", "ext4"};
100             if (vsr_level == __ANDROID_API_T__) {
101                 allowed.emplace_back("f2fs");
102             }
103 
104             EXPECT_NE(std::find(allowed.begin(), allowed.end(), entry.fs_type), allowed.end())
105                     << entry.mount_point;
106         } else {
107             EXPECT_NE(entry.fs_type, "ext4") << entry.mount_point;
108         }
109     }
110 }
111 
TEST(fs,NoDtFstab)112 TEST(fs, NoDtFstab) {
113     if (GetVsrLevel() < __ANDROID_API_Q__) {
114         GTEST_SKIP();
115     }
116 
117     android::fs_mgr::Fstab fstab;
118     EXPECT_FALSE(android::fs_mgr::ReadFstabFromDt(&fstab, false));
119 }
120