• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env @PYTHON@
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3
4'''Runs tests for the default suppression specifications of libabigail.
5
6This program tries to exercise the reading of default suppression
7files by the abidiff and abipkgdiff programs.
8
9It reads a set of input files to ABI-compare, along with a
10suppressions specification and a reference output.  The test points
11the environment variables LIBABIGAIL_DEFAULT_SYSTEM_SUPPRESSION_FILE
12and LIBABIGAIL_USER_SYSTEM_SUPPRESSION_FILE environment variables at
13the suppression specification and runs abidiff to compare the two
14input binary files.  The result of the comparison should be equal to
15the reference output, meaning that the suppression specification was
16taken into account.
17'''
18
19import sys
20import os
21import subprocess
22
23
24abidiff = '@abs_top_builddir@/tools/abidiff'
25abipkgdiff = '@abs_top_builddir@/tools/abipkgdiff'
26test_src_dir = '@top_srcdir@/tests'
27test_build_dir = '@abs_top_builddir@/tests'
28input_dir = '@top_srcdir@/tests/data/test-default-supprs'
29output_dir = '@abs_top_builddir@/tests/output/test-default-supprs'
30
31# This variable named is a list of 5-uples.  Here is the meaning of
32# the elements of each 5-uples:
33#
34# (first binary to compare,
35#  second binary to compare,
36#  suppression specification,
37#  reference output file,
38#  where to store the result of the comparison)
39#
40abidiff_test_specs = [('data/test-default-supprs/test0-type-suppr-v0.o',
41                       'data/test-default-supprs/test0-type-suppr-v1.o',
42                       'data/test-default-supprs/test0-type-suppr-0.suppr',
43                       'data/test-default-supprs/test0-type-suppr-report-0.txt',
44                       'output/test-default-supprs/test0-type-suppr-report-0.txt'),]
45
46# This variable named is a list of 5-uples.  Here is the meaning of
47# the elements of each 5-uples:
48#
49# (first package to compare,
50#  second package to compare,
51#  suppression specification,
52#  reference output file,
53#  where to store the result of the comparison)
54#
55abipkgdiff_test_specs = [('data/test-default-supprs/dirpkg-1-dir1',
56                          'data/test-default-supprs/dirpkg-1-dir2',
57                          'data/test-default-supprs/dirpkg-1-dir2/dir.abignore',
58                          'data/test-default-supprs/dirpkg-1-dir-report-0.txt',
59                          'output/test-default-supprs/dirpkg-1-dir-report-0.txt')]
60
61
62def ensure_output_dir_created():
63    '''Create output dir if it's not already created.'''
64
65    try:
66        os.makedirs(output_dir)
67    except:
68        pass
69
70        if not os.path.exists(output_dir):
71            sys.exit(1);
72
73def run_abidiff_tests():
74    """Run the abidiff default suppression tests.
75
76    Loop through the test inputs in the abidiff_test_specs global
77    variable and for each of the test input, launch a comparison using
78    abidiff and setting LIBABIGAIL_SYSTEM_SUPPRESSION_FILE and
79    LIBABIGAIL_USER_SYSTEM_SUPPRESSION_FILE environment variables.
80
81    Note that if LIBABIGAIL_SYSTEM_SUPPRESSION_FILE is not set,
82    libabigail tries to load the file
83    $libdir/libabigail/defaul-libabigail.abignore, and then tries to
84    load the file $HOME/.abignore.  This program does not test the
85    case where LIBABIGAIL_SYSTEM_SUPPRESSION_FILE and
86    LIBABIGAIL_USER_SYSTEM_SUPPRESSION_FILE are not set.
87
88    This function returns the exit code of the abidiff program.
89
90    """
91
92    default_suppression = output_dir + "/default.abignore"
93    with open(default_suppression, 'w') as out:
94        out.write('\n');
95
96    result = 0;
97    for test_spec in abidiff_test_specs:
98        binary1 = test_spec[0]
99        binary2 = test_spec[1]
100        suppressions = test_spec[2]
101        reference_report_path = test_spec[3]
102        output_path = test_spec[4]
103
104        binary1 = test_src_dir + "/" + binary1
105        binary2 = test_src_dir + "/" + binary2
106        suppressions = test_src_dir + "/" + suppressions if suppressions else ''
107        reference_report_path = test_src_dir + "/" + reference_report_path
108        output_path = test_build_dir + "/" + output_path
109
110        cmd = [abidiff, binary1, binary2]
111
112        # The environment variables that say where to find the default
113        # suppression specifications loaded by libabigail.
114        envs = {
115            'LIBABIGAIL_DEFAULT_SYSTEM_SUPPRESSION_FILE' : default_suppression,
116            'LIBABIGAIL_DEFAULT_USER_SUPPRESSION_FILE' : default_suppression
117        }
118
119        # Initialize the environment variables above to their default
120        # value.
121        for name, value in envs.items():
122            os.environ[name] = value;
123
124        for env_name in envs:
125            env_vars = os.environ
126            if suppressions:
127                env_vars[env_name] = suppressions
128
129            with open(output_path, 'w') as out_file:
130                subprocess.call(cmd, env=env_vars, stdout=out_file)
131
132            diffcmd = ['diff', '-u', reference_report_path, output_path]
133
134            return_code = subprocess.call(diffcmd)
135            if return_code:
136                result = return_code
137                sys.stderr.write("failed abidiff test "
138                                 "for env var '" + env_name + "'\n");
139
140            del env_vars[env_name];
141
142        try:
143            os.remove(default_suppression)
144        except:
145            pass
146
147        return result;
148
149def run_abipkgdiff_tests():
150    """Run the abipkgdiff default suppression tests.
151
152    Loop through the test inputs in the abipkgdiff_test_specs global
153    variable and for each of the input packages, launch a comparison
154    using abipkgdiff and setting LIBABIGAIL_SYSTEM_SUPPRESSION_FILE
155    and LIBABIGAIL_USER_SYSTEM_SUPPRESSION_FILE environment variables.
156
157    Note that if LIBABIGAIL_SYSTEM_SUPPRESSION_FILE is not set,
158    libabigail tries to load the file
159    $libdir/libabigail/defaul-libabigail.abignore, and then tries to
160    load the file $HOME/.abignore.  This program does not test the
161    case where LIBABIGAIL_SYSTEM_SUPPRESSION_FILE and
162    LIBABIGAIL_USER_SYSTEM_SUPPRESSION_FILE are not set.
163
164    This function returns the exit code of the abipkgdiff program.
165
166    """
167
168    default_suppression = output_dir + "/default.abignore"
169    with open(default_suppression, 'w') as out:
170        out.write('\n');
171
172    result = 0;
173    for test_spec in abipkgdiff_test_specs:
174        pkg1 = test_spec[0]
175        pkg2 = test_spec[1]
176        suppressions = test_spec[2]
177        reference_report_path = test_spec[3]
178        output_path = test_spec[4]
179
180        pkg1 = test_src_dir + "/" + pkg1
181        pkg2 = test_src_dir + "/" + pkg2
182        suppressions = test_src_dir + "/" + suppressions if suppressions else ''
183        reference_report_path = test_src_dir + "/" + reference_report_path
184        output_path = test_build_dir + "/" + output_path
185
186        cmd = [abipkgdiff, '--no-abignore', pkg1, pkg2]
187
188        # The environment variables that say where to find the default
189        # suppression specifications loaded by libabigail.
190        envs = {
191            'LIBABIGAIL_DEFAULT_SYSTEM_SUPPRESSION_FILE' : default_suppression,
192            'LIBABIGAIL_DEFAULT_USER_SUPPRESSION_FILE' : default_suppression
193        }
194
195        # Initialize the environment variables above to their default
196        # value.
197        for name, value in envs.items():
198            os.environ[name] = value;
199
200        for env_name in envs:
201            env_vars = os.environ
202            if suppressions:
203                env_vars[env_name] = suppressions
204
205            with open(output_path, 'w') as out_file:
206                subprocess.call(cmd, env=env_vars, stdout=out_file)
207
208            diffcmd = ['diff', '-u', reference_report_path, output_path]
209
210            return_code = subprocess.call(diffcmd)
211            if return_code:
212                result = return_code
213                sys.stderr.write("failed abipkgdiff test "
214                                 "for env var '" + e + "'\n");
215
216            del env_vars[env_name];
217
218        try:
219            os.remove(default_suppression)
220        except:
221            pass
222
223        return result;
224
225def main():
226    """The main entry point of this program.
227
228    This creates the output directory and launches the tests for the
229    abidiff and abipkgdiff probrams.  It the abidiff programs returns
230    with a non-zero exit code, this function returns that exit code
231    immediatly.  Otherwise, it runs the abipkgdiff tests and returns
232    its exit code.
233
234    """
235
236    ensure_output_dir_created()
237    result = 0;
238    result = run_abidiff_tests()
239    if result:
240        return result;
241    result = run_abipkgdiff_tests()
242    return result;
243
244if __name__ == '__main__':
245    exit_code = main()
246    sys.exit(exit_code)
247