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 "perfetto/base/build_config.h"
25 #include "perfetto/ext/base/file_utils.h"
26 #include "perfetto/ext/base/scoped_file.h"
27 #include "perfetto/ext/base/temp_file.h"
28 #include "perfetto/ext/base/utils.h"
29 #include "test/gtest_and_gmock.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)
TEST(FsMountTest,ParseRealMounts)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
TEST(FsMountTest,ParseSyntheticMounts)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 ASSERT_EQ(base::WriteAll(tmp_file.fd(), kMounts, sizeof(kMounts)),
56 static_cast<ssize_t>(sizeof(kMounts)));
57 std::multimap<BlockDeviceID, std::string> mounts =
58 ParseMounts(tmp_file.path().c_str());
59 struct stat dev_stat = {}, root_stat = {};
60 ASSERT_NE(stat("/dev", &dev_stat), -1);
61 ASSERT_NE(stat("/", &root_stat), -1);
62 EXPECT_THAT(mounts, Contains(Pair(dev_stat.st_dev, "/dev")));
63 EXPECT_THAT(mounts, Contains(Pair(root_stat.st_dev, "/")));
64 } // namespace
65
66 } // namespace
67 } // namespace perfetto
68