1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "repr/symbol/exported_symbol_set.h"
16
17 #include "repr/ir_representation.h"
18
19 #include <gtest/gtest.h>
20
21
22 namespace header_checker {
23 namespace repr {
24
25
TEST(ExportedSymbolSetTest,AddFunction)26 TEST(ExportedSymbolSetTest, AddFunction) {
27 ExportedSymbolSet symbols;
28 symbols.AddFunction("global", ElfSymbolIR::ElfSymbolBinding::Global);
29 symbols.AddFunction("weak", ElfSymbolIR::ElfSymbolBinding::Weak);
30
31 const ExportedSymbolSet::FunctionMap &funcs = symbols.GetFunctions();
32
33 ASSERT_NE(funcs.end(), funcs.find("global"));
34 EXPECT_EQ(ElfSymbolIR::ElfSymbolBinding::Global,
35 funcs.at("global").GetBinding());
36
37 ASSERT_NE(funcs.end(), funcs.find("weak"));
38 EXPECT_EQ(ElfSymbolIR::ElfSymbolBinding::Weak,
39 funcs.at("weak").GetBinding());
40 }
41
42
TEST(ExportedSymbolSetTest,AddVar)43 TEST(ExportedSymbolSetTest, AddVar) {
44 ExportedSymbolSet symbols;
45 symbols.AddVar("global", ElfSymbolIR::ElfSymbolBinding::Global);
46 symbols.AddVar("weak", ElfSymbolIR::ElfSymbolBinding::Weak);
47
48 const ExportedSymbolSet::VarMap &vars = symbols.GetVars();
49
50 ASSERT_NE(vars.end(), vars.find("global"));
51 EXPECT_EQ(ElfSymbolIR::ElfSymbolBinding::Global,
52 vars.at("global").GetBinding());
53
54 ASSERT_NE(vars.end(), vars.find("weak"));
55 EXPECT_EQ(ElfSymbolIR::ElfSymbolBinding::Weak,
56 vars.at("weak").GetBinding());
57 }
58
59
TEST(ExportedSymbolSetTest,AddGlobPattern)60 TEST(ExportedSymbolSetTest, AddGlobPattern) {
61 ExportedSymbolSet symbols;
62 symbols.AddGlobPattern("test1*");
63
64 const ExportedSymbolSet::GlobPatternSet &globs = symbols.GetGlobPatterns();
65 ASSERT_NE(globs.end(), globs.find("test1*"));
66 }
67
68
TEST(ExportedSymbolSetTest,AddDemangledCppGlobPattern)69 TEST(ExportedSymbolSetTest, AddDemangledCppGlobPattern) {
70 ExportedSymbolSet symbols;
71 symbols.AddDemangledCppGlobPattern("test1*");
72
73 const ExportedSymbolSet::GlobPatternSet &globs =
74 symbols.GetDemangledCppGlobPatterns();
75 ASSERT_NE(globs.end(), globs.find("test1*"));
76 }
77
78
TEST(ExportedSymbolSetTest,AddDemangledCppSymbol)79 TEST(ExportedSymbolSetTest, AddDemangledCppSymbol) {
80 ExportedSymbolSet symbols;
81 symbols.AddDemangledCppSymbol("Test::test()");
82
83 const ExportedSymbolSet::NameSet &names = symbols.GetDemangledCppSymbols();
84 ASSERT_NE(names.end(), names.find("Test::test()"));
85 }
86
87
TEST(ExportedSymbolSetTest,HasSymbol)88 TEST(ExportedSymbolSetTest, HasSymbol) {
89 ExportedSymbolSet symbols;
90
91 symbols.AddFunction("global_func", ElfSymbolIR::ElfSymbolBinding::Global);
92 symbols.AddVar("global_var", ElfSymbolIR::ElfSymbolBinding::Global);
93
94 symbols.AddGlobPattern("test_glob1_*");
95 symbols.AddGlobPattern("test_glob2_[Aa]");
96 symbols.AddGlobPattern("test_glob3_?");
97
98 symbols.AddDemangledCppGlobPattern("Test1::*");
99 symbols.AddDemangledCppGlobPattern("Test2::[Aa]()");
100 symbols.AddDemangledCppGlobPattern("Test3::?()");
101 symbols.AddDemangledCppSymbol("Test4::test()");
102
103 // Test literal names
104 EXPECT_TRUE(symbols.HasSymbol("global_func"));
105 EXPECT_TRUE(symbols.HasSymbol("global_var"));
106
107 EXPECT_FALSE(symbols.HasSymbol(""));
108 EXPECT_FALSE(symbols.HasSymbol("no_such_function"));
109
110 // Test glob patterns
111 EXPECT_TRUE(symbols.HasSymbol("test_glob1_a"));
112 EXPECT_TRUE(symbols.HasSymbol("test_glob2_A"));
113 EXPECT_TRUE(symbols.HasSymbol("test_glob2_a"));
114 EXPECT_TRUE(symbols.HasSymbol("test_glob3_b"));
115
116 EXPECT_FALSE(symbols.HasSymbol("test_glob2_Ax"));
117 EXPECT_FALSE(symbols.HasSymbol("test_glob2_B"));
118 EXPECT_FALSE(symbols.HasSymbol("test_glob3_Bx"));
119
120 // Test C++ names and patterns
121 EXPECT_TRUE(symbols.HasSymbol("_ZN5Test14testEv"));
122 EXPECT_TRUE(symbols.HasSymbol("_ZN5Test21AEv"));
123 EXPECT_TRUE(symbols.HasSymbol("_ZN5Test21aEv"));
124 EXPECT_TRUE(symbols.HasSymbol("_ZN5Test31bEv"));
125 EXPECT_TRUE(symbols.HasSymbol("_ZN5Test44testEv"));
126
127 EXPECT_FALSE(symbols.HasSymbol("_ZN5Test22AxEv"));
128 EXPECT_FALSE(symbols.HasSymbol("_ZN5Test21bEv"));
129 EXPECT_FALSE(symbols.HasSymbol("_ZN5Test32BxEv"));
130 }
131
132
133 } // namespace repr
134 } // namespace header_checker
135