1 /*
2  * Copyright (C) 2020 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/printk_formats_parser.h"
18 
19 #include "test/gtest_and_gmock.h"
20 
21 using ::testing::Contains;
22 using ::testing::Eq;
23 using ::testing::IsEmpty;
24 using ::testing::Key;
25 using ::testing::Not;
26 using ::testing::Pair;
27 
28 namespace perfetto {
29 namespace {
30 
TEST(PrintkFormatParserTest,AllZeros)31 TEST(PrintkFormatParserTest, AllZeros) {
32   std::string format = R"(0x0 : "Rescheduling interrupts"
33 0x0 : "Function call interrupts"
34 0x0 : "CPU stop interrupts"
35 0x0 : "Timer broadcast interrupts"
36 0x0 : "IRQ work interrupts"
37 0x0 : "CPU wakeup interrupts"
38 0x0 : "CPU backtrace"
39 0x0 : "rcu_sched"
40 0x0 : "rcu_bh"
41 0x0 : "rcu_preempt"
42 )";
43 
44   PrintkMap result = ParsePrintkFormats(format);
45   EXPECT_THAT(result, IsEmpty());
46 }
47 
TEST(PrintkFormatParserTest,VariousAddresses)48 TEST(PrintkFormatParserTest, VariousAddresses) {
49   std::string format = R"(0x1 : "First line"
50 0x1 : "First line"
51 0x2 : "Unfortunate: colon"
52 0x3 : ""
53 0xffffff92349439b8 : "Large address"
54 0x9 : "Last line")";
55 
56   PrintkMap result = ParsePrintkFormats(format);
57   EXPECT_THAT(result.at(1), Eq("First line"));
58   EXPECT_THAT(result.at(2), Eq("Unfortunate: colon"));
59   EXPECT_THAT(result.at(18446743602145278392ULL), Eq("Large address"));
60   EXPECT_THAT(result.at(9), Eq("Last line"));
61   EXPECT_THAT(result.at(3), Eq(""));
62 }
63 
TEST(PrintkFormatParserTest,RobustToRubbish)64 TEST(PrintkFormatParserTest, RobustToRubbish) {
65   std::string format = R"(
66 : leading colon
67 trailing colon:
68 multiple colons: : : : :
69 Empty line:
70 
71 Just colon:
72 :
73 : "No address"
74 No name:
75 0x1 :
76 0xbadhexaddress : "Bad hex address"
77 0x2 : No quotes
78 0x3:"No gap"
79 "Wrong way round" : 0x4
80 )";
81 
82   PrintkMap result = ParsePrintkFormats(format);
83   EXPECT_THAT(result.at(2), Eq("No quotes"));
84   EXPECT_THAT(result.at(3), Eq("No gap"));
85 }
86 
87 }  // namespace
88 }  // namespace perfetto
89