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/ftrace/ftrace_procfs.h"
18
19 #include "test/gtest_and_gmock.h"
20
21 using testing::AnyNumber;
22 using testing::IsEmpty;
23 using testing::Return;
24 using testing::UnorderedElementsAre;
25
26 namespace perfetto {
27 namespace {
28
29 class MockFtraceProcfs : public FtraceProcfs {
30 public:
MockFtraceProcfs()31 MockFtraceProcfs() : FtraceProcfs("/root/") {}
32
33 MOCK_METHOD(bool,
34 WriteToFile,
35 (const std::string& path, const std::string& str),
36 (override));
37 MOCK_METHOD(char, ReadOneCharFromFile, (const std::string& path), (override));
38 MOCK_METHOD(bool, ClearFile, (const std::string& path), (override));
39 MOCK_METHOD(std::string,
40 ReadFileIntoString,
41 (const std::string& path),
42 (const, override));
43 MOCK_METHOD(size_t, NumberOfCpus, (), (const, override));
44 };
45
TEST(FtraceProcfsTest,ParseAvailableClocks)46 TEST(FtraceProcfsTest, ParseAvailableClocks) {
47 MockFtraceProcfs ftrace;
48
49 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
50 .WillOnce(Return("[local] global boot"));
51 EXPECT_THAT(ftrace.AvailableClocks(),
52 UnorderedElementsAre("local", "global", "boot"));
53
54 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
55 .WillOnce(Return("[local] global boot"));
56 EXPECT_THAT(ftrace.GetClock(), "local");
57
58 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
59 .WillOnce(Return("local [global] boot"));
60 EXPECT_THAT(ftrace.GetClock(), "global");
61
62 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
63 .WillOnce(Return("local global [boot]"));
64 EXPECT_THAT(ftrace.GetClock(), "boot");
65
66 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
67 .WillOnce(Return(""));
68 EXPECT_THAT(ftrace.AvailableClocks(), IsEmpty());
69
70 // trace_clock text may end in a new line:
71 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
72 .WillOnce(Return("[local] global boot\n"));
73 EXPECT_THAT(ftrace.AvailableClocks(),
74 UnorderedElementsAre("local", "global", "boot"));
75
76 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
77 .WillOnce(Return("local global [boot]\n"));
78 EXPECT_THAT(ftrace.AvailableClocks(),
79 UnorderedElementsAre("local", "global", "boot"));
80
81 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
82 .WillOnce(Return("local global [boot]\n"));
83 EXPECT_THAT(ftrace.GetClock(), "boot");
84
85 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
86 .WillOnce(Return("\n"));
87 EXPECT_THAT(ftrace.AvailableClocks(), IsEmpty());
88
89 // We should handle many newlines (just in case):
90 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
91 .WillOnce(Return("local global [boot]\n\n\n"));
92 EXPECT_THAT(ftrace.GetClock(), "boot");
93
94 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
95 .WillOnce(Return("local global [boot]\n\n"));
96 EXPECT_THAT(ftrace.GetClock(), "boot");
97
98 EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
99 .WillOnce(Return("\n\n\n\n"));
100 EXPECT_THAT(ftrace.AvailableClocks(), IsEmpty());
101 }
102
103 } // namespace
104 } // namespace perfetto
105