• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2014-2020 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7 
8 /// @file
9 ///
10 /// Given a program P that links against a library L of version V
11 /// denoted L(V), this program checks if P is still ABI compatible
12 /// with a subsequent version of L denoted L(V+N), N being a positive
13 /// integer.  The result of the check is a report that is compared
14 /// against a reference report.  This program actually performs these
15 /// checks for a variety of tuple {P, L(V), L(V+N)}
16 ///
17 /// The set of input files and reference reports to consider should be
18 /// present in the source distribution.
19 
20 #include <cstring>
21 #include <string>
22 #include <fstream>
23 #include <iostream>
24 #include <cstdlib>
25 #include "abg-tools-utils.h"
26 #include "test-utils.h"
27 
28 using std::string;
29 using std::cerr;
30 using std::cout;
31 
32 struct InOutSpec
33 {
34   const char* in_app_path;
35   const char* in_lib1_path;
36   const char* in_lib2_path;
37   const char* suppressions;
38   const char* options;
39   const char* in_report_path;
40   const char* out_report_path;
41 };
42 
43 InOutSpec in_out_specs[] =
44 {
45   {
46     "data/test-abicompat/test0-fn-changed-app",
47     "data/test-abicompat/libtest0-fn-changed-libapp-v0.so",
48     "data/test-abicompat/libtest0-fn-changed-libapp-v1.so",
49     "",
50     "--show-base-names --no-show-locs --no-redundant",
51     "data/test-abicompat/test0-fn-changed-report-0.txt",
52     "output/test-abicompat/test0-fn-changed-report-0.txt",
53   },
54   {
55     "data/test-abicompat/test0-fn-changed-app",
56     "data/test-abicompat/libtest0-fn-changed-libapp-v0.so",
57     "data/test-abicompat/libtest0-fn-changed-libapp-v1.so",
58     "data/test-abicompat/test0-fn-changed-0.suppr",
59     "--show-base-names --no-show-locs --no-redundant",
60     "data/test-abicompat/test0-fn-changed-report-1.txt",
61     "output/test-abicompat/test0-fn-changed-report-1.txt",
62   },
63   { // Previous test, but emitting loc info.
64     "data/test-abicompat/test0-fn-changed-app",
65     "data/test-abicompat/libtest0-fn-changed-libapp-v0.so",
66     "data/test-abicompat/libtest0-fn-changed-libapp-v1.so",
67     "",
68     "--show-base-names --no-redundant",
69     "data/test-abicompat/test0-fn-changed-report-2.txt",
70     "output/test-abicompat/test0-fn-changed-report-2.txt",
71   },
72   {
73     "data/test-abicompat/test0-fn-changed-app",
74     "data/test-abicompat/libtest0-fn-changed-libapp-v0.so",
75     "data/test-abicompat/libtest0-fn-changed-libapp-v1.so",
76     "data/test-abicompat/test0-fn-changed-1.suppr",
77     "--show-base-names --no-show-locs --no-redundant",
78     "data/test-abicompat/test0-fn-changed-report-3.txt",
79     "output/test-abicompat/test0-fn-changed-report-3.txt",
80   },
81   {
82     "data/test-abicompat/test1-fn-removed-app",
83     "data/test-abicompat/libtest1-fn-removed-v0.so",
84     "data/test-abicompat/libtest1-fn-removed-v1.so",
85     "",
86     "--show-base-names --no-show-locs --no-redundant",
87     "data/test-abicompat/test1-fn-removed-report-0.txt",
88     "output/test-abicompat/test1-fn-removed-report-0.txt",
89   },
90   {
91     "data/test-abicompat/test2-var-removed-app",
92     "data/test-abicompat/libtest2-var-removed-v0.so",
93     "data/test-abicompat/libtest2-var-removed-v1.so",
94     "",
95     "--show-base-names --no-show-locs --no-redundant",
96     "data/test-abicompat/test2-var-removed-report-0.txt",
97     "output/test-abicompat/test2-var-removed-report-0.txt",
98   },
99   {
100     "data/test-abicompat/test3-fn-removed-app",
101     "data/test-abicompat/libtest3-fn-removed-v0.so",
102     "data/test-abicompat/libtest3-fn-removed-v1.so",
103     "",
104     "--show-base-names --no-show-locs --no-redundant",
105     "data/test-abicompat/test3-fn-removed-report-0.txt",
106     "output/test-abicompat/test3-fn-removed-report-0.txt",
107   },
108   {
109     "data/test-abicompat/test4-soname-changed-app",
110     "data/test-abicompat/libtest4-soname-changed-v0.so",
111     "data/test-abicompat/libtest4-soname-changed-v1.so",
112     "",
113     "--show-base-names --no-show-locs --no-redundant",
114     "data/test-abicompat/test4-soname-changed-report-0.txt",
115     "output/test-abicompat/test4-soname-changed-report-0.txt",
116   },
117   {
118     "data/test-abicompat/test5-fn-changed-app",
119     "data/test-abicompat/libtest5-fn-changed-libapp-v1.so",
120     "",
121     "",
122     "--show-base-names --no-show-locs --weak-mode",
123     "data/test-abicompat/test5-fn-changed-report-0.txt",
124     "output/test-abicompat/test5-fn-changed-report-0.txt",
125   },
126   { // Previous test, but emitting loc info.
127     "data/test-abicompat/test5-fn-changed-app",
128     "data/test-abicompat/libtest5-fn-changed-libapp-v1.so",
129     "",
130     "",
131     "--show-base-names --weak-mode",
132     "data/test-abicompat/test5-fn-changed-report-1.txt",
133     "output/test-abicompat/test5-fn-changed-report-1.txt",
134   },
135   {
136     "data/test-abicompat/test6-var-changed-app",
137     "data/test-abicompat/libtest6-var-changed-libapp-v1.so",
138     "",
139     "",
140     "--show-base-names --no-show-locs --weak-mode",
141     "data/test-abicompat/test6-var-changed-report-0.txt",
142     "output/test-abicompat/test6-var-changed-report-0.txt",
143   },
144   { // Previous test, but emitting loc info.
145     "data/test-abicompat/test6-var-changed-app",
146     "data/test-abicompat/libtest6-var-changed-libapp-v1.so",
147     "",
148     "",
149     "--show-base-names --weak-mode",
150     "data/test-abicompat/test6-var-changed-report-1.txt",
151     "output/test-abicompat/test6-var-changed-report-1.txt",
152   },
153   {
154     "data/test-abicompat/test7-fn-changed-app",
155     "data/test-abicompat/libtest7-fn-changed-libapp-v0.so",
156     "data/test-abicompat/libtest7-fn-changed-libapp-v1.so",
157     "",
158     "--show-base-names --no-show-locs --no-redundant",
159     "data/test-abicompat/test7-fn-changed-report-0.txt",
160     "output/test-abicompat/test7-fn-changed-report-0.txt",
161   },
162   {
163     "data/test-abicompat/test7-fn-changed-app",
164     "data/test-abicompat/libtest7-fn-changed-libapp-v1.so",
165     "",
166     "",
167     "--show-base-names --no-show-locs --weak-mode",
168     "data/test-abicompat/test7-fn-changed-report-1.txt",
169     "output/test-abicompat/test7-fn-changed-report-1.txt",
170   },
171   { // Previous test, but emitting loc info.
172     "data/test-abicompat/test7-fn-changed-app",
173     "data/test-abicompat/libtest7-fn-changed-libapp-v1.so",
174     "",
175     "",
176     "--show-base-names --weak-mode",
177     "data/test-abicompat/test7-fn-changed-report-2.txt",
178     "output/test-abicompat/test7-fn-changed-report-2.txt",
179   },
180   {
181     "data/test-abicompat/test8-fn-changed-app",
182     "data/test-abicompat/libtest8-fn-changed-libapp-v1.so",
183     "",
184     "",
185     "--show-base-names --weak-mode",
186     "data/test-abicompat/test8-fn-changed-report-0.txt",
187     "output/test-abicompat/test8-fn-changed-report-0.txt",
188   },
189   {
190     "data/test-abicompat/test9-fn-changed-app",
191     "data/test-abicompat/libtest9-fn-changed-v1.so ",
192     "",
193     "",
194     "--show-base-names --weak-mode",
195     "data/test-abicompat/test9-fn-changed-report-0.txt",
196     "output/test-abicompat/test9-fn-changed-report-0.txt",
197   },
198   // This entry must be the last one.
199   {0, 0, 0, 0, 0, 0, 0}
200 };
201 
202 int
main()203 main()
204 {
205   using abigail::tests::get_src_dir;
206   using abigail::tests::get_build_dir;
207   using abigail::tools_utils::ensure_parent_dir_created;
208   using abigail::tools_utils::abidiff_status;
209 
210   unsigned int cnt_total = 0, cnt_passed = 0, cnt_failed = 0;
211   string in_app_path, in_lib1_path, in_lib2_path, suppression_path,
212     abicompat_options, ref_report_path, out_report_path, abicompat, cmd, diffcmd;
213 
214   for (InOutSpec* s = in_out_specs; s->in_app_path; ++s)
215     {
216       bool is_ok = true;
217       in_app_path = string(get_src_dir()) + "/tests/" + s->in_app_path;
218       in_lib1_path = string(get_src_dir()) + "/tests/" + s->in_lib1_path;
219       if (s->in_lib2_path && strcmp(s->in_lib2_path, ""))
220 	in_lib2_path = string(get_src_dir()) + "/tests/" + s->in_lib2_path;
221       else
222 	in_lib2_path.clear();
223       if (s->suppressions == 0 || !strcmp(s->suppressions, ""))
224 	suppression_path.clear();
225       else
226 	suppression_path = string(get_src_dir()) + "/tests/" + s->suppressions;
227       abicompat_options = s->options;
228       ref_report_path = string(get_src_dir()) + "/tests/" + s->in_report_path;
229       out_report_path =
230 	string(get_build_dir()) + "/tests/" + s->out_report_path;
231 
232       if (!ensure_parent_dir_created(out_report_path))
233 	{
234 	  cerr << "could not create parent directory for "
235 	       << out_report_path;
236 	  is_ok = false;
237 	  continue;
238 	}
239 
240       abicompat = string(get_build_dir()) + "/tools/abicompat";
241       if (!suppression_path.empty())
242 	abicompat += " --suppressions " + suppression_path;
243       abicompat += " " + abicompat_options;
244 
245       cmd = abicompat + " " + in_app_path + " " + in_lib1_path;
246       if (!in_lib2_path.empty())
247 	cmd += string(" ") + in_lib2_path;
248 
249       cmd += " > " + out_report_path;
250 
251       bool abicompat_ok = true;
252       abidiff_status status = static_cast<abidiff_status>(system(cmd.c_str()));
253       if (abigail::tools_utils::abidiff_status_has_error(status))
254 	abicompat_ok = false;
255 
256       if (abicompat_ok)
257 	{
258 	  diffcmd = "diff -u " + ref_report_path + " " + out_report_path;
259 	  if (system(diffcmd.c_str()))
260 	    is_ok = false;
261 	}
262       else
263 	is_ok = false;
264 
265       if (is_ok)
266         {
267 	  cout << BRIGHT_YELLOW_COLOR
268                << "Test Passed:"
269                << DEFAULT_TERMINAL_COLOR
270                << cmd
271                << std::endl;
272 	  cnt_passed++;
273 	}
274       else
275         {
276 	  cout << BRIGHT_RED_COLOR
277                << "Test Failed:"
278                << DEFAULT_TERMINAL_COLOR
279                << cmd
280                << std::endl;
281 	  cnt_failed++;
282 	}
283       cnt_total++;
284     }
285   cout << "Summary: " << cnt_total << " tested!"
286        << " Test Passed: " << cnt_passed
287        << ", Test Failed: " << cnt_failed
288        << ".\n";
289 
290   return cnt_failed;
291 }
292