File: test.py

package info (click to toggle)
android-platform-development 10.0.0%2Br36-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 135,564 kB
  • sloc: java: 160,253; xml: 127,434; python: 40,579; cpp: 17,579; sh: 2,569; javascript: 1,612; ansic: 879; lisp: 261; ruby: 183; makefile: 172; sql: 140; perl: 88
file content (352 lines) | stat: -rwxr-xr-x 14,696 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python3

import os
import unittest
import sys
import tempfile

import_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
import_path = os.path.abspath(os.path.join(import_path, 'utils'))
sys.path.insert(1, import_path)

from utils import (AOSP_DIR, read_output_content, run_abi_diff,
                   run_header_abi_dumper)
from module import Module


SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
INPUT_DIR = os.path.join(SCRIPT_DIR, 'input')
EXPECTED_DIR = os.path.join(SCRIPT_DIR, 'expected')
REF_DUMP_DIR = os.path.join(SCRIPT_DIR, 'reference_dumps')


def make_and_copy_reference_dumps(module, reference_dump_dir=REF_DUMP_DIR):
    output_content = module.make_dump()

    dump_dir = os.path.join(reference_dump_dir, module.arch)
    os.makedirs(dump_dir, exist_ok=True)

    dump_path = os.path.join(dump_dir, module.get_dump_name())
    with open(dump_path, 'w') as f:
        f.write(output_content)

    return dump_path


class HeaderCheckerTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.maxDiff = None

    def setUp(self):
        self.tmp_dir = None

    def tearDown(self):
        if self.tmp_dir:
            self.tmp_dir.cleanup()
            self.tmp_dir = None

    def get_tmp_dir(self):
        if not self.tmp_dir:
            self.tmp_dir = tempfile.TemporaryDirectory()
        return self.tmp_dir.name

    def run_and_compare(self, input_path, expected_path, cflags=[]):
        with open(expected_path, 'r') as f:
            expected_output = f.read()
        actual_output = run_header_abi_dumper(input_path, cflags)
        self.assertEqual(actual_output, expected_output)

    def run_and_compare_name(self, name, cflags=[]):
        input_path = os.path.join(INPUT_DIR, name)
        expected_path = os.path.join(EXPECTED_DIR, name)
        self.run_and_compare(input_path, expected_path, cflags)

    def run_and_compare_name_cpp(self, name, cflags=[]):
        self.run_and_compare_name(name, cflags + ['-x', 'c++', '-std=c++11'])

    def run_and_compare_name_c_cpp(self, name, cflags=[]):
        self.run_and_compare_name(name, cflags)
        self.run_and_compare_name_cpp(name, cflags)

    def run_and_compare_abi_diff(self, old_dump, new_dump, lib, arch,
                                 expected_return_code, flags=[]):
        actual_output = run_abi_diff(old_dump, new_dump, arch, lib, flags)
        self.assertEqual(actual_output, expected_return_code)

    def prepare_and_run_abi_diff(self, old_ref_dump_path, new_ref_dump_path,
                                 target_arch, expected_return_code, flags=[]):
        self.run_and_compare_abi_diff(old_ref_dump_path, new_ref_dump_path,
                                      'test', target_arch,
                                      expected_return_code, flags)

    def get_or_create_ref_dump(self, module, create):
        if create:
            return make_and_copy_reference_dumps(module, self.get_tmp_dir())
        return os.path.join(REF_DUMP_DIR, module.arch, module.get_dump_name())

    def prepare_and_run_abi_diff_all_archs(self, old_lib, new_lib,
                                           expected_return_code, flags=[],
                                           create_old=False, create_new=True):
        old_modules = Module.get_test_modules_by_name(old_lib)
        new_modules = Module.get_test_modules_by_name(new_lib)
        self.assertEqual(len(old_modules), len(new_modules))

        for old_module, new_module in zip(old_modules, new_modules):
            self.assertEqual(old_module.arch, new_module.arch)
            old_ref_dump_path = self.get_or_create_ref_dump(old_module,
                                                            create_old)
            new_ref_dump_path = self.get_or_create_ref_dump(new_module,
                                                            create_new)
            self.prepare_and_run_abi_diff(
                old_ref_dump_path, new_ref_dump_path, new_module.arch,
                expected_return_code, flags)

    def prepare_and_absolute_diff_all_archs(self, old_lib, new_lib):
        old_modules = Module.get_test_modules_by_name(old_lib)
        new_modules = Module.get_test_modules_by_name(new_lib)
        self.assertEqual(len(old_modules), len(new_modules))

        for old_module, new_module in zip(old_modules, new_modules):
            self.assertEqual(old_module.arch, new_module.arch)
            old_ref_dump_path = self.get_or_create_ref_dump(old_module, False)
            new_ref_dump_path = self.get_or_create_ref_dump(new_module, True)
            self.assertEqual(
                read_output_content(old_ref_dump_path, AOSP_DIR),
                read_output_content(new_ref_dump_path, AOSP_DIR))

    def test_example1_cpp(self):
        self.run_and_compare_name_cpp('example1.cpp')

    def test_example1_h(self):
        self.run_and_compare_name_cpp('example1.h')

    def test_example2_h(self):
        self.run_and_compare_name_cpp('example2.h')

    def test_example3_h(self):
        self.run_and_compare_name_cpp('example3.h')

    def test_undeclared_types_h(self):
        self.prepare_and_absolute_diff_all_archs(
            'undeclared_types.h', 'undeclared_types.h')

    def test_known_issues_h(self):
        self.prepare_and_absolute_diff_all_archs(
            'known_issues.h', 'known_issues.h')

    def test_libc_and_cpp(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libc_and_cpp", "libc_and_cpp", 0)

    def test_libc_and_cpp_and_libc_and_cpp_with_unused_struct(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libc_and_cpp", "libc_and_cpp_with_unused_struct", 0)

    def test_libc_and_cpp_and_libc_and_cpp_with_unused_struct_allow(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libc_and_cpp", "libc_and_cpp_with_unused_struct", 0,
            ["-allow-unreferenced-changes"])

    def test_libc_and_cpp_and_libc_and_cpp_with_unused_struct_check_all(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libc_and_cpp", "libc_and_cpp_with_unused_struct", 1,
            ['-check-all-apis'])

    def test_libc_and_cpp_with_unused_struct_and_libc_and_cpp_with_unused_cstruct(
            self):
        self.prepare_and_run_abi_diff_all_archs(
            "libc_and_cpp_with_unused_struct",
            "libc_and_cpp_with_unused_cstruct", 0,
            ['-check-all-apis', '-allow-unreferenced-changes'])

    def test_libc_and_cpp_and_libc_and_cpp_with_unused_struct_check_all_advice(
            self):
        self.prepare_and_run_abi_diff_all_archs(
            "libc_and_cpp", "libc_and_cpp_with_unused_struct", 0,
            ['-check-all-apis', '-advice-only'])

    def test_libc_and_cpp_opaque_pointer_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libc_and_cpp_with_opaque_ptr_a",
            "libc_and_cpp_with_opaque_ptr_b", 8,
            ['-consider-opaque-types-different'], True, True)

    def test_libgolden_cpp_return_type_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_return_type_diff", 8)

    def test_libgolden_cpp_add_odr(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_odr", 0,
            ['-check-all-apis', '-allow-unreferenced-changes'])

    def test_libgolden_cpp_add_function(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_add_function", 4)

    def test_libgolden_cpp_add_function_allow_extension(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_add_function", 0,
            ['-allow-extensions'])

    def test_libgolden_cpp_add_function_and_elf_symbol(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_add_function_and_unexported_elf",
            4)

    def test_libgolden_cpp_fabricated_function_ast_removed_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp_add_function_sybmol_only",
            "libgolden_cpp_add_function", 0, [], False, False)

    def test_libgolden_cpp_change_function_access(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_change_function_access", 8)

    def test_libgolden_cpp_add_global_variable(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_add_global_variable", 4)

    def test_libgolden_cpp_change_global_var_access(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp_add_global_variable",
            "libgolden_cpp_add_global_variable_private", 8)

    def test_libgolden_cpp_parameter_type_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_parameter_type_diff", 8)

    def test_libgolden_cpp_with_vtable_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_vtable_diff", 8)

    def test_libgolden_cpp_member_diff_advice_only(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_member_diff", 0, ['-advice-only'])

    def test_libgolden_cpp_member_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_member_diff", 8)

    def test_libgolden_cpp_change_member_access(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_change_member_access", 8)

    def test_libgolden_cpp_enum_extended(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_enum_extended", 4)

    def test_libgolden_cpp_enum_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_enum_diff", 8)

    def test_libgolden_cpp_member_fake_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_member_fake_diff", 0)

    def test_libgolden_cpp_member_integral_type_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_member_integral_type_diff", 8)

    def test_libgolden_cpp_member_cv_diff(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_member_cv_diff", 8)

    def test_libgolden_cpp_unreferenced_elf_symbol_removed(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_unreferenced_elf_symbol_removed",
            16)

    def test_libreproducability(self):
        self.prepare_and_absolute_diff_all_archs(
            "libreproducability", "libreproducability")

    def test_libgolden_cpp_member_name_changed(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_member_name_changed", 0)

    def test_libgolden_cpp_member_function_pointer_changed(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp_function_pointer",
            "libgolden_cpp_function_pointer_parameter_added", 8, [],
            True, True)

    def test_libgolden_cpp_internal_struct_access_upgraded(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp_internal_private_struct",
            "libgolden_cpp_internal_public_struct", 0, [], True, True)

    def test_libgolden_cpp_internal_struct_access_downgraded(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp_internal_public_struct",
            "libgolden_cpp_internal_private_struct", 8, [], True, True)

    def test_libgolden_cpp_inheritance_type_changed(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_inheritance_type_changed", 8, [],
            True, True)

    def test_libpure_virtual_function(self):
        self.prepare_and_absolute_diff_all_archs(
            "libpure_virtual_function", "libpure_virtual_function")

    def test_libc_and_cpp_in_json(self):
        self.prepare_and_absolute_diff_all_archs(
            "libgolden_cpp_json", "libgolden_cpp_json")

    def test_libc_and_cpp_in_protobuf_and_json(self):
        self.prepare_and_run_abi_diff_all_archs(
            "libgolden_cpp", "libgolden_cpp_json", 0,
            ["-input-format-old", "ProtobufTextFormat",
             "-input-format-new", "Json"])

    def test_opaque_type_self_diff(self):
        lsdump = os.path.join(
            SCRIPT_DIR, "abi_dumps", "opaque_ptr_types.lsdump")
        self.run_and_compare_abi_diff(
            lsdump, lsdump, "libexample", "arm64", 0,
            ["-input-format-old", "Json", "-input-format-new", "Json",
             "-consider-opaque-types-different"])

    def test_allow_adding_removing_weak_symbols(self):
        module_old = Module.get_test_modules_by_name("libweak_symbols_old")[0]
        module_new = Module.get_test_modules_by_name("libweak_symbols_new")[0]
        lsdump_old = self.get_or_create_ref_dump(module_old, False)
        lsdump_new = self.get_or_create_ref_dump(module_new, False)

        options = ["-input-format-old", "Json", "-input-format-new", "Json"]

        # If `-allow-adding-removing-weak-symbols` is not specified, removing a
        # weak symbol must be treated as an incompatible change.
        self.run_and_compare_abi_diff(
            lsdump_old, lsdump_new, "libweak_symbols", "arm64", 8, options)

        # If `-allow-adding-removing-weak-symbols` is specified, removing a
        # weak symbol must be fine and mustn't be a fatal error.
        self.run_and_compare_abi_diff(
            lsdump_old, lsdump_new, "libweak_symbols", "arm64", 0,
            options + ["-allow-adding-removing-weak-symbols"])

    def test_linker_shared_object_file_and_version_script(self):
        base_dir = os.path.join(
            SCRIPT_DIR, 'integration', 'version_script_example')

        cases = [
            'libversion_script_example',
            'libversion_script_example_no_mytag',
            'libversion_script_example_no_private',
        ]

        for module_name in cases:
            module = Module.get_test_modules_by_name(module_name)[0]
            example_lsdump_old = self.get_or_create_ref_dump(module, False)
            example_lsdump_new = self.get_or_create_ref_dump(module, True)
            self.run_and_compare_abi_diff(
                example_lsdump_old, example_lsdump_new,
                module_name, "arm64", 0,
                ["-input-format-old", "Json", "-input-format-new", "Json"])


if __name__ == '__main__':
    unittest.main()