1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2020-2022 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 void
test_suppressions_are_consistent(const suppressions_type & suppr,const std::string & expr)46 test_suppressions_are_consistent(const suppressions_type& suppr,
47 const std::string& expr)
48 {
49 REQUIRE(suppr.size() == 2);
50
51 function_suppression_sptr left = is_function_suppression(suppr[0]);
52 variable_suppression_sptr right = is_variable_suppression(suppr[1]);
53
54 // correctly casted
55 REQUIRE(left);
56 REQUIRE(right);
57 // same label
58 REQUIRE(left->get_label() == right->get_label());
59 // same mode
60 REQUIRE(left->get_drops_artifact_from_ir()
61 == right->get_drops_artifact_from_ir());
62 // same regex
63 REQUIRE(left->get_symbol_name_not_regex_str()
64 == right->get_symbol_name_not_regex_str());
65 // regex as expected
66 REQUIRE(left->get_symbol_name_not_regex_str() == expr);
67 }
68
69 TEST_CASE("NoWhitelists", "[whitelists]")
70 {
71 const std::vector<std::string> abi_whitelist_paths;
72 suppressions_type suppr =
73 gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
74 REQUIRE(suppr.empty());
75 }
76
77 TEST_CASE("WhitelistWithASingleEntry", "[whitelists]")
78 {
79 std::vector<std::string> abi_whitelist_paths;
80 abi_whitelist_paths.push_back(whitelist_with_single_entry);
81 suppressions_type suppr
82 = gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
83 REQUIRE(!suppr.empty());
84 test_suppressions_are_consistent(suppr, "^(test_symbol)$");
85 }
86
87 TEST_CASE("WhitelistWithADuplicateEntry", "[whitelists]")
88 {
89 std::vector<std::string> abi_whitelist_paths;
90 abi_whitelist_paths.push_back(whitelist_with_duplicate_entry);
91 suppressions_type suppr
92 = gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
93 REQUIRE(!suppr.empty());
94 test_suppressions_are_consistent(suppr, "^(test_symbol)$");
95 }
96
97 TEST_CASE("TwoWhitelists", "[whitelists]")
98 {
99 std::vector<std::string> abi_whitelist_paths;
100 abi_whitelist_paths.push_back(whitelist_with_single_entry);
101 abi_whitelist_paths.push_back(whitelist_with_another_single_entry);
102 suppressions_type suppr =
103 gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
104 REQUIRE(!suppr.empty());
105 test_suppressions_are_consistent(suppr,
106 "^(test_another_symbol|test_symbol)$");
107 }
108
109 TEST_CASE("TwoWhitelistsWithDuplicates", "[whitelists]")
110 {
111 std::vector<std::string> abi_whitelist_paths;
112 abi_whitelist_paths.push_back(whitelist_with_duplicate_entry);
113 abi_whitelist_paths.push_back(whitelist_with_another_single_entry);
114 suppressions_type suppr
115 = gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
116 REQUIRE(!suppr.empty());
117 test_suppressions_are_consistent(suppr,
118 "^(test_another_symbol|test_symbol)$");
119 }
120
121 TEST_CASE("WhitelistWithTwoSections", "[whitelists]")
122 {
123 std::vector<std::string> abi_whitelist_paths;
124 abi_whitelist_paths.push_back(whitelist_with_two_sections);
125 suppressions_type suppr
126 = gen_suppr_spec_from_kernel_abi_whitelists(abi_whitelist_paths);
127 REQUIRE(!suppr.empty());
128 test_suppressions_are_consistent(suppr, "^(test_symbol1|test_symbol2)$");
129 }
130