1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "src/traced/probes/filesystem/fs_mount.h" 18 19 #include <fcntl.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <sys/stat.h> 23 24 #include "gmock/gmock.h" 25 #include "gtest/gtest.h" 26 #include "perfetto/base/build_config.h" 27 #include "perfetto/base/scoped_file.h" 28 #include "perfetto/base/temp_file.h" 29 #include "perfetto/base/utils.h" 30 31 namespace perfetto { 32 namespace { 33 34 using testing::Contains; 35 using testing::Pair; 36 37 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ 38 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) 39 TEST(FsMountTest, ParseRealMounts) { 40 std::multimap<BlockDeviceID, std::string> mounts = ParseMounts(); 41 struct stat buf = {}; 42 ASSERT_NE(stat("/proc", &buf), -1); 43 EXPECT_THAT(mounts, Contains(Pair(buf.st_dev, "/proc"))); 44 } 45 #endif 46 47 TEST(FsMountTest, ParseSyntheticMounts) { 48 const char kMounts[] = R"( 49 sysfs / sysfs rw,nosuid,nodev,noexec,relatime 0 0 50 #INVALIDLINE 51 devfs /dev devfs,local,nobrowse 52 )"; 53 54 base::TempFile tmp_file = base::TempFile::Create(); 55 base::ignore_result(write(tmp_file.fd(), kMounts, sizeof(kMounts))); 56 std::multimap<BlockDeviceID, std::string> mounts = 57 ParseMounts(tmp_file.path().c_str()); 58 struct stat dev_stat = {}, root_stat = {}; 59 ASSERT_NE(stat("/dev", &dev_stat), -1); 60 ASSERT_NE(stat("/", &root_stat), -1); 61 EXPECT_THAT(mounts, Contains(Pair(dev_stat.st_dev, "/dev"))); 62 EXPECT_THAT(mounts, Contains(Pair(root_stat.st_dev, "/"))); 63 } // namespace 64 65 } // namespace 66 } // namespace perfetto 67