1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2020 Google, Inc.
5 //
6 // Author: Matthias Maennich
7
8 /// @file
9 ///
10 /// This program tests suppression generation from KMI whitelists.
11
12 #include <string>
13
14 #include "lib/catch.hpp"
15
16 #include "abg-fwd.h"
17 #include "abg-suppression.h"
18 #include "abg-tools-utils.h"
19 #include "test-utils.h"
20
21 using abigail::tools_utils::gen_suppr_spec_from_kernel_abi_whitelists;
22 using abigail::suppr::suppression_sptr;
23 using abigail::suppr::suppressions_type;
24 using abigail::suppr::function_suppression_sptr;
25 using abigail::suppr::variable_suppression_sptr;
26 using abigail::suppr::is_function_suppression;
27 using abigail::suppr::is_variable_suppression;
28
29 const static std::string whitelist_with_single_entry
30 = std::string(abigail::tests::get_src_dir())
31 + "/tests/data/test-kmi-whitelist/whitelist-with-single-entry";
32
33 const static std::string whitelist_with_another_single_entry
34 = std::string(abigail::tests::get_src_dir())
35 + "/tests/data/test-kmi-whitelist/whitelist-with-another-single-entry";
36
37 const static std::string whitelist_with_two_sections
38 = std::string(abigail::tests::get_src_dir())
39 + "/tests/data/test-kmi-whitelist/whitelist-with-two-sections";
40
41 const static std::string whitelist_with_duplicate_entry
42 = std::string(abigail::tests::get_src_dir())
43 + "/tests/data/test-kmi-whitelist/whitelist-with-duplicate-entry";
44
45 const static std::string symbol_list_with_single_entry
46 = std::string(abigail::tests::get_src_dir())
47 + "/tests/data/test-kmi-whitelist/symbol-list-with-single-entry";
48
49 const static std::string symbol_list_with_another_single_entry
50 = std::string(abigail::tests::get_src_dir())
51 + "/tests/data/test-kmi-whitelist/symbol-list-with-another-single-entry";
52
53 const static std::string symbol_list_with_two_sections
54 = std::string(abigail::tests::get_src_dir())
55 + "/tests/data/test-kmi-whitelist/symbol-list-with-two-sections";
56
57 const static std::string symbol_list_with_duplicate_entry
58 = std::string(abigail::tests::get_src_dir())
59 + "/tests/data/test-kmi-whitelist/symbol-list-with-duplicate-entry";
60
61 void
test_suppressions_are_consistent(const suppressions_type & suppr,const std::string & expr)62 test_suppressions_are_consistent(const suppressions_type& suppr,
63 const std::string& expr)
64 {
65 REQUIRE(suppr.size() == 2);
66
67 function_suppression_sptr left = is_function_suppression(suppr[0]);
68 variable_suppression_sptr right = is_variable_suppression(suppr[1]);
69
70 // correctly casted
71 REQUIRE(left);
72 REQUIRE(right);
73 // same label
74 REQUIRE(left->get_label() == right->get_label());
75 // same mode
76 REQUIRE(left->get_drops_artifact_from_ir()
77 == right->get_drops_artifact_from_ir());
78 // same regex
79 REQUIRE(left->get_symbol_name_not_regex_str()
80 == right->get_symbol_name_not_regex_str());
81 // regex as expected
82 REQUIRE(left->get_symbol_name_not_regex_str() == expr);
83 }
84
85 TEST_CASE("NoWhitelists", "[whitelists]")
86 {
87 const std::vector<std::string> abi_whitelist_paths;
88 suppressions_type suppr =
89 gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
90 REQUIRE(suppr.empty());
91 }
92
93 TEST_CASE("WhitelistWithASingleEntry", "[whitelists]")
94 {
95 std::vector<std::string> abi_whitelist_paths;
96 abi_whitelist_paths.push_back(whitelist_with_single_entry);
97 suppressions_type suppr
98 = gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
99 REQUIRE(!suppr.empty());
100 test_suppressions_are_consistent(suppr, "^(test_symbol)$");
101 }
102
103 TEST_CASE("WhitelistWithADuplicateEntry", "[whitelists]")
104 {
105 std::vector<std::string> abi_whitelist_paths;
106 abi_whitelist_paths.push_back(whitelist_with_duplicate_entry);
107 suppressions_type suppr
108 = gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
109 REQUIRE(!suppr.empty());
110 test_suppressions_are_consistent(suppr, "^(test_symbol)$");
111 }
112
113 TEST_CASE("TwoWhitelists", "[whitelists]")
114 {
115 std::vector<std::string> abi_whitelist_paths;
116 abi_whitelist_paths.push_back(whitelist_with_single_entry);
117 abi_whitelist_paths.push_back(whitelist_with_another_single_entry);
118 suppressions_type suppr =
119 gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
120 REQUIRE(!suppr.empty());
121 test_suppressions_are_consistent(suppr,
122 "^(test_another_symbol|test_symbol)$");
123 }
124
125 TEST_CASE("TwoWhitelistsWithDuplicates", "[whitelists]")
126 {
127 std::vector<std::string> abi_whitelist_paths;
128 abi_whitelist_paths.push_back(whitelist_with_duplicate_entry);
129 abi_whitelist_paths.push_back(whitelist_with_another_single_entry);
130 suppressions_type suppr
131 = gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
132 REQUIRE(!suppr.empty());
133 test_suppressions_are_consistent(suppr,
134 "^(test_another_symbol|test_symbol)$");
135 }
136
137 TEST_CASE("WhitelistWithTwoSections", "[whitelists]")
138 {
139 std::vector<std::string> abi_whitelist_paths;
140 abi_whitelist_paths.push_back(whitelist_with_two_sections);
141 suppressions_type suppr
142 = gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
143 REQUIRE(!suppr.empty());
144 test_suppressions_are_consistent(suppr, "^(test_symbol1|test_symbol2)$");
145 }
146
147 TEST_CASE("NoSymbolLists", "[symbol_lists]")
148 {
149 const std::vector<std::string> abi_symbol_list_paths;
150 suppressions_type suppr =
151 gen_suppr_spec_from_kernel_abi_whitelists(abi_symbol_list_paths);
152 REQUIRE(suppr.empty());
153 }
154
155 TEST_CASE("SymbolListWithASingleEntry", "[symbol_lists]")
156 {
157 std::vector<std::string> abi_symbol_list_paths;
158 abi_symbol_list_paths.push_back(symbol_list_with_single_entry);
159 suppressions_type suppr
160 = gen_suppr_spec_from_kernel_abi_whitelists(abi_symbol_list_paths);
161 REQUIRE(!suppr.empty());
162 test_suppressions_are_consistent(suppr, "^(test_symbol)$");
163 }
164
165 TEST_CASE("SymbolListWithADuplicateEntry", "[symbol_lists]")
166 {
167 std::vector<std::string> abi_symbol_list_paths;
168 abi_symbol_list_paths.push_back(symbol_list_with_duplicate_entry);
169 suppressions_type suppr
170 = gen_suppr_spec_from_kernel_abi_whitelists(abi_symbol_list_paths);
171 REQUIRE(!suppr.empty());
172 test_suppressions_are_consistent(suppr, "^(test_symbol)$");
173 }
174
175 TEST_CASE("TwoSymbolLists", "[symbol_lists]")
176 {
177 std::vector<std::string> abi_symbol_list_paths;
178 abi_symbol_list_paths.push_back(symbol_list_with_single_entry);
179 abi_symbol_list_paths.push_back(symbol_list_with_another_single_entry);
180 suppressions_type suppr =
181 gen_suppr_spec_from_kernel_abi_whitelists(abi_symbol_list_paths);
182 REQUIRE(!suppr.empty());
183 test_suppressions_are_consistent(suppr,
184 "^(test_another_symbol|test_symbol)$");
185 }
186
187 TEST_CASE("TwoSymbolListsWithDuplicates", "[symbol_lists]")
188 {
189 std::vector<std::string> abi_symbol_list_paths;
190 abi_symbol_list_paths.push_back(symbol_list_with_duplicate_entry);
191 abi_symbol_list_paths.push_back(symbol_list_with_another_single_entry);
192 suppressions_type suppr
193 = gen_suppr_spec_from_kernel_abi_whitelists(abi_symbol_list_paths);
194 REQUIRE(!suppr.empty());
195 test_suppressions_are_consistent(suppr,
196 "^(test_another_symbol|test_symbol)$");
197 }
198
199 TEST_CASE("SymbolListWithTwoSections", "[symbol_lists]")
200 {
201 std::vector<std::string> abi_symbol_list_paths;
202 abi_symbol_list_paths.push_back(symbol_list_with_two_sections);
203 suppressions_type suppr
204 = gen_suppr_spec_from_kernel_abi_whitelists(abi_symbol_list_paths);
205 REQUIRE(!suppr.empty());
206 test_suppressions_are_consistent(suppr, "^(test_symbol1|test_symbol2)$");
207 }
208