• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/file_utils.h"
28 #include "perfetto/base/scoped_file.h"
29 #include "perfetto/base/temp_file.h"
30 #include "perfetto/base/utils.h"
31 
32 namespace perfetto {
33 namespace {
34 
35 using testing::Contains;
36 using testing::Pair;
37 
38 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
39     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
TEST(FsMountTest,ParseRealMounts)40 TEST(FsMountTest, ParseRealMounts) {
41   std::multimap<BlockDeviceID, std::string> mounts = ParseMounts();
42   struct stat buf = {};
43   ASSERT_NE(stat("/proc", &buf), -1);
44   EXPECT_THAT(mounts, Contains(Pair(buf.st_dev, "/proc")));
45 }
46 #endif
47 
TEST(FsMountTest,ParseSyntheticMounts)48 TEST(FsMountTest, ParseSyntheticMounts) {
49   const char kMounts[] = R"(
50 sysfs / sysfs rw,nosuid,nodev,noexec,relatime 0 0
51 #INVALIDLINE
52 devfs /dev devfs,local,nobrowse
53 )";
54 
55   base::TempFile tmp_file = base::TempFile::Create();
56   ASSERT_EQ(base::WriteAll(tmp_file.fd(), kMounts, sizeof(kMounts)),
57             sizeof(kMounts));
58   std::multimap<BlockDeviceID, std::string> mounts =
59       ParseMounts(tmp_file.path().c_str());
60   struct stat dev_stat = {}, root_stat = {};
61   ASSERT_NE(stat("/dev", &dev_stat), -1);
62   ASSERT_NE(stat("/", &root_stat), -1);
63   EXPECT_THAT(mounts, Contains(Pair(dev_stat.st_dev, "/dev")));
64   EXPECT_THAT(mounts, Contains(Pair(root_stat.st_dev, "/")));
65 }  // namespace
66 
67 }  // namespace
68 }  // namespace perfetto
69