2020-06-23 00:10:31 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
"""Tests for ndkstubgen.py."""
|
|
|
|
import io
|
|
|
|
import textwrap
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
import symbolfile
|
2021-06-04 23:30:40 +02:00
|
|
|
from symbolfile import Arch, Tags
|
|
|
|
|
|
|
|
import ndkstubgen
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=missing-docstring
|
|
|
|
|
|
|
|
|
|
|
|
class GeneratorTest(unittest.TestCase):
|
2020-06-23 20:21:21 +02:00
|
|
|
def test_omit_version(self) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
# Thorough testing of the cases involved here is handled by
|
|
|
|
# OmitVersionTest, PrivateVersionTest, and SymbolPresenceTest.
|
|
|
|
src_file = io.StringIO()
|
|
|
|
version_file = io.StringIO()
|
2020-07-30 23:32:55 +02:00
|
|
|
symbol_list_file = io.StringIO()
|
|
|
|
generator = ndkstubgen.Generator(src_file,
|
|
|
|
version_file, symbol_list_file,
|
|
|
|
Arch('arm'), 9, False, False)
|
2020-06-23 00:10:31 +02:00
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
version = symbolfile.Version('VERSION_PRIVATE', None, Tags(), [
|
|
|
|
symbolfile.Symbol('foo', Tags()),
|
2020-06-23 00:10:31 +02:00
|
|
|
])
|
|
|
|
generator.write_version(version)
|
|
|
|
self.assertEqual('', src_file.getvalue())
|
|
|
|
self.assertEqual('', version_file.getvalue())
|
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
version = symbolfile.Version('VERSION', None, Tags.from_strs(['x86']),
|
|
|
|
[
|
|
|
|
symbolfile.Symbol('foo', Tags()),
|
|
|
|
])
|
2020-06-23 00:10:31 +02:00
|
|
|
generator.write_version(version)
|
|
|
|
self.assertEqual('', src_file.getvalue())
|
|
|
|
self.assertEqual('', version_file.getvalue())
|
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
version = symbolfile.Version('VERSION', None,
|
|
|
|
Tags.from_strs(['introduced=14']), [
|
|
|
|
symbolfile.Symbol('foo', Tags()),
|
|
|
|
])
|
2020-06-23 00:10:31 +02:00
|
|
|
generator.write_version(version)
|
|
|
|
self.assertEqual('', src_file.getvalue())
|
|
|
|
self.assertEqual('', version_file.getvalue())
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def test_omit_symbol(self) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
# Thorough testing of the cases involved here is handled by
|
|
|
|
# SymbolPresenceTest.
|
|
|
|
src_file = io.StringIO()
|
|
|
|
version_file = io.StringIO()
|
2020-07-30 23:32:55 +02:00
|
|
|
symbol_list_file = io.StringIO()
|
|
|
|
generator = ndkstubgen.Generator(src_file,
|
|
|
|
version_file, symbol_list_file,
|
|
|
|
Arch('arm'), 9, False, False)
|
2020-06-23 00:10:31 +02:00
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
version = symbolfile.Version('VERSION_1', None, Tags(), [
|
|
|
|
symbolfile.Symbol('foo', Tags.from_strs(['x86'])),
|
2020-06-23 00:10:31 +02:00
|
|
|
])
|
|
|
|
generator.write_version(version)
|
|
|
|
self.assertEqual('', src_file.getvalue())
|
|
|
|
self.assertEqual('', version_file.getvalue())
|
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
version = symbolfile.Version('VERSION_1', None, Tags(), [
|
|
|
|
symbolfile.Symbol('foo', Tags.from_strs(['introduced=14'])),
|
2020-06-23 00:10:31 +02:00
|
|
|
])
|
|
|
|
generator.write_version(version)
|
|
|
|
self.assertEqual('', src_file.getvalue())
|
|
|
|
self.assertEqual('', version_file.getvalue())
|
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
version = symbolfile.Version('VERSION_1', None, Tags(), [
|
|
|
|
symbolfile.Symbol('foo', Tags.from_strs(['llndk'])),
|
2020-06-23 00:10:31 +02:00
|
|
|
])
|
|
|
|
generator.write_version(version)
|
|
|
|
self.assertEqual('', src_file.getvalue())
|
|
|
|
self.assertEqual('', version_file.getvalue())
|
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
version = symbolfile.Version('VERSION_1', None, Tags(), [
|
|
|
|
symbolfile.Symbol('foo', Tags.from_strs(['apex'])),
|
2020-06-23 00:10:31 +02:00
|
|
|
])
|
|
|
|
generator.write_version(version)
|
|
|
|
self.assertEqual('', src_file.getvalue())
|
|
|
|
self.assertEqual('', version_file.getvalue())
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def test_write(self) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
src_file = io.StringIO()
|
|
|
|
version_file = io.StringIO()
|
2020-07-30 23:32:55 +02:00
|
|
|
symbol_list_file = io.StringIO()
|
|
|
|
generator = ndkstubgen.Generator(src_file,
|
|
|
|
version_file, symbol_list_file,
|
|
|
|
Arch('arm'), 9, False, False)
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
versions = [
|
2021-06-04 23:30:40 +02:00
|
|
|
symbolfile.Version('VERSION_1', None, Tags(), [
|
|
|
|
symbolfile.Symbol('foo', Tags()),
|
|
|
|
symbolfile.Symbol('bar', Tags.from_strs(['var'])),
|
|
|
|
symbolfile.Symbol('woodly', Tags.from_strs(['weak'])),
|
|
|
|
symbolfile.Symbol('doodly', Tags.from_strs(['weak', 'var'])),
|
2020-06-23 00:10:31 +02:00
|
|
|
]),
|
2021-06-04 23:30:40 +02:00
|
|
|
symbolfile.Version('VERSION_2', 'VERSION_1', Tags(), [
|
|
|
|
symbolfile.Symbol('baz', Tags()),
|
2020-06-23 00:10:31 +02:00
|
|
|
]),
|
2021-06-04 23:30:40 +02:00
|
|
|
symbolfile.Version('VERSION_3', 'VERSION_1', Tags(), [
|
|
|
|
symbolfile.Symbol('qux', Tags.from_strs(['versioned=14'])),
|
2020-06-23 00:10:31 +02:00
|
|
|
]),
|
|
|
|
]
|
|
|
|
|
|
|
|
generator.write(versions)
|
|
|
|
expected_src = textwrap.dedent("""\
|
|
|
|
void foo() {}
|
|
|
|
int bar = 0;
|
|
|
|
__attribute__((weak)) void woodly() {}
|
|
|
|
__attribute__((weak)) int doodly = 0;
|
|
|
|
void baz() {}
|
|
|
|
void qux() {}
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_src, src_file.getvalue())
|
|
|
|
|
|
|
|
expected_version = textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
global:
|
|
|
|
foo;
|
|
|
|
bar;
|
|
|
|
woodly;
|
|
|
|
doodly;
|
|
|
|
};
|
|
|
|
VERSION_2 {
|
|
|
|
global:
|
|
|
|
baz;
|
|
|
|
} VERSION_1;
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_version, version_file.getvalue())
|
|
|
|
|
2020-07-30 23:32:55 +02:00
|
|
|
expected_allowlist = textwrap.dedent("""\
|
|
|
|
[abi_symbol_list]
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
woodly
|
|
|
|
doodly
|
|
|
|
baz
|
|
|
|
qux
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_allowlist, symbol_list_file.getvalue())
|
|
|
|
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
class IntegrationTest(unittest.TestCase):
|
2020-06-23 20:21:21 +02:00
|
|
|
def test_integration(self) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
api_map = {
|
|
|
|
'O': 9000,
|
|
|
|
'P': 9001,
|
|
|
|
}
|
|
|
|
|
|
|
|
input_file = io.StringIO(textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
global:
|
|
|
|
foo; # var
|
|
|
|
bar; # x86
|
|
|
|
fizz; # introduced=O
|
|
|
|
buzz; # introduced=P
|
|
|
|
local:
|
|
|
|
*;
|
|
|
|
};
|
|
|
|
|
|
|
|
VERSION_2 { # arm
|
|
|
|
baz; # introduced=9
|
|
|
|
qux; # versioned=14
|
|
|
|
} VERSION_1;
|
|
|
|
|
|
|
|
VERSION_3 { # introduced=14
|
|
|
|
woodly;
|
|
|
|
doodly; # var
|
|
|
|
} VERSION_2;
|
|
|
|
|
|
|
|
VERSION_4 { # versioned=9
|
|
|
|
wibble;
|
|
|
|
wizzes; # llndk
|
|
|
|
waggle; # apex
|
|
|
|
} VERSION_2;
|
|
|
|
|
|
|
|
VERSION_5 { # versioned=14
|
|
|
|
wobble;
|
|
|
|
} VERSION_4;
|
|
|
|
"""))
|
2020-06-23 20:21:21 +02:00
|
|
|
parser = symbolfile.SymbolFileParser(input_file, api_map, Arch('arm'),
|
|
|
|
9, False, False)
|
2020-06-23 00:10:31 +02:00
|
|
|
versions = parser.parse()
|
|
|
|
|
|
|
|
src_file = io.StringIO()
|
|
|
|
version_file = io.StringIO()
|
2020-07-30 23:32:55 +02:00
|
|
|
symbol_list_file = io.StringIO()
|
|
|
|
generator = ndkstubgen.Generator(src_file,
|
|
|
|
version_file, symbol_list_file,
|
|
|
|
Arch('arm'), 9, False, False)
|
2020-06-23 00:10:31 +02:00
|
|
|
generator.write(versions)
|
|
|
|
|
|
|
|
expected_src = textwrap.dedent("""\
|
|
|
|
int foo = 0;
|
|
|
|
void baz() {}
|
|
|
|
void qux() {}
|
|
|
|
void wibble() {}
|
|
|
|
void wobble() {}
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_src, src_file.getvalue())
|
|
|
|
|
|
|
|
expected_version = textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
global:
|
|
|
|
foo;
|
|
|
|
};
|
|
|
|
VERSION_2 {
|
|
|
|
global:
|
|
|
|
baz;
|
|
|
|
} VERSION_1;
|
|
|
|
VERSION_4 {
|
|
|
|
global:
|
|
|
|
wibble;
|
|
|
|
} VERSION_2;
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_version, version_file.getvalue())
|
|
|
|
|
2020-07-30 23:32:55 +02:00
|
|
|
expected_allowlist = textwrap.dedent("""\
|
|
|
|
[abi_symbol_list]
|
|
|
|
foo
|
|
|
|
baz
|
|
|
|
qux
|
|
|
|
wibble
|
|
|
|
wobble
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_allowlist, symbol_list_file.getvalue())
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def test_integration_future_api(self) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
api_map = {
|
|
|
|
'O': 9000,
|
|
|
|
'P': 9001,
|
|
|
|
'Q': 9002,
|
|
|
|
}
|
|
|
|
|
|
|
|
input_file = io.StringIO(textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
global:
|
|
|
|
foo; # introduced=O
|
|
|
|
bar; # introduced=P
|
|
|
|
baz; # introduced=Q
|
|
|
|
local:
|
|
|
|
*;
|
|
|
|
};
|
|
|
|
"""))
|
2020-06-23 20:21:21 +02:00
|
|
|
parser = symbolfile.SymbolFileParser(input_file, api_map, Arch('arm'),
|
|
|
|
9001, False, False)
|
2020-06-23 00:10:31 +02:00
|
|
|
versions = parser.parse()
|
|
|
|
|
|
|
|
src_file = io.StringIO()
|
|
|
|
version_file = io.StringIO()
|
2020-07-30 23:32:55 +02:00
|
|
|
symbol_list_file = io.StringIO()
|
|
|
|
generator = ndkstubgen.Generator(src_file,
|
|
|
|
version_file, symbol_list_file,
|
|
|
|
Arch('arm'), 9001, False, False)
|
2020-06-23 00:10:31 +02:00
|
|
|
generator.write(versions)
|
|
|
|
|
|
|
|
expected_src = textwrap.dedent("""\
|
|
|
|
void foo() {}
|
|
|
|
void bar() {}
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_src, src_file.getvalue())
|
|
|
|
|
|
|
|
expected_version = textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
global:
|
|
|
|
foo;
|
|
|
|
bar;
|
|
|
|
};
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_version, version_file.getvalue())
|
|
|
|
|
2020-07-30 23:32:55 +02:00
|
|
|
expected_allowlist = textwrap.dedent("""\
|
|
|
|
[abi_symbol_list]
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_allowlist, symbol_list_file.getvalue())
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def test_multiple_definition(self) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
input_file = io.StringIO(textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
global:
|
|
|
|
foo;
|
|
|
|
foo;
|
|
|
|
bar;
|
|
|
|
baz;
|
|
|
|
qux; # arm
|
|
|
|
local:
|
|
|
|
*;
|
|
|
|
};
|
|
|
|
|
|
|
|
VERSION_2 {
|
|
|
|
global:
|
|
|
|
bar;
|
|
|
|
qux; # arm64
|
|
|
|
} VERSION_1;
|
|
|
|
|
|
|
|
VERSION_PRIVATE {
|
|
|
|
global:
|
|
|
|
baz;
|
|
|
|
} VERSION_2;
|
|
|
|
|
|
|
|
"""))
|
2020-06-23 20:21:21 +02:00
|
|
|
parser = symbolfile.SymbolFileParser(input_file, {}, Arch('arm'), 16,
|
|
|
|
False, False)
|
2020-06-23 00:10:31 +02:00
|
|
|
|
|
|
|
with self.assertRaises(
|
|
|
|
symbolfile.MultiplyDefinedSymbolError) as ex_context:
|
|
|
|
parser.parse()
|
|
|
|
self.assertEqual(['bar', 'foo'],
|
|
|
|
ex_context.exception.multiply_defined_symbols)
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def test_integration_with_apex(self) -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
api_map = {
|
|
|
|
'O': 9000,
|
|
|
|
'P': 9001,
|
|
|
|
}
|
|
|
|
|
|
|
|
input_file = io.StringIO(textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
global:
|
|
|
|
foo; # var
|
|
|
|
bar; # x86
|
|
|
|
fizz; # introduced=O
|
|
|
|
buzz; # introduced=P
|
|
|
|
local:
|
|
|
|
*;
|
|
|
|
};
|
|
|
|
|
|
|
|
VERSION_2 { # arm
|
|
|
|
baz; # introduced=9
|
|
|
|
qux; # versioned=14
|
|
|
|
} VERSION_1;
|
|
|
|
|
|
|
|
VERSION_3 { # introduced=14
|
|
|
|
woodly;
|
|
|
|
doodly; # var
|
|
|
|
} VERSION_2;
|
|
|
|
|
|
|
|
VERSION_4 { # versioned=9
|
|
|
|
wibble;
|
|
|
|
wizzes; # llndk
|
|
|
|
waggle; # apex
|
|
|
|
bubble; # apex llndk
|
|
|
|
duddle; # llndk apex
|
|
|
|
} VERSION_2;
|
|
|
|
|
|
|
|
VERSION_5 { # versioned=14
|
|
|
|
wobble;
|
|
|
|
} VERSION_4;
|
|
|
|
"""))
|
2020-06-23 20:21:21 +02:00
|
|
|
parser = symbolfile.SymbolFileParser(input_file, api_map, Arch('arm'),
|
|
|
|
9, False, True)
|
2020-06-23 00:10:31 +02:00
|
|
|
versions = parser.parse()
|
|
|
|
|
|
|
|
src_file = io.StringIO()
|
|
|
|
version_file = io.StringIO()
|
2020-07-30 23:32:55 +02:00
|
|
|
symbol_list_file = io.StringIO()
|
|
|
|
generator = ndkstubgen.Generator(src_file,
|
|
|
|
version_file, symbol_list_file,
|
|
|
|
Arch('arm'), 9, False, True)
|
2020-06-23 00:10:31 +02:00
|
|
|
generator.write(versions)
|
|
|
|
|
|
|
|
expected_src = textwrap.dedent("""\
|
|
|
|
int foo = 0;
|
|
|
|
void baz() {}
|
|
|
|
void qux() {}
|
|
|
|
void wibble() {}
|
|
|
|
void waggle() {}
|
|
|
|
void bubble() {}
|
|
|
|
void duddle() {}
|
|
|
|
void wobble() {}
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_src, src_file.getvalue())
|
|
|
|
|
|
|
|
expected_version = textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
global:
|
|
|
|
foo;
|
|
|
|
};
|
|
|
|
VERSION_2 {
|
|
|
|
global:
|
|
|
|
baz;
|
|
|
|
} VERSION_1;
|
|
|
|
VERSION_4 {
|
|
|
|
global:
|
|
|
|
wibble;
|
|
|
|
waggle;
|
|
|
|
bubble;
|
|
|
|
duddle;
|
|
|
|
} VERSION_2;
|
|
|
|
""")
|
|
|
|
self.assertEqual(expected_version, version_file.getvalue())
|
|
|
|
|
2021-04-08 23:50:05 +02:00
|
|
|
def test_empty_stub(self) -> None:
|
|
|
|
"""Tests that empty stubs can be generated.
|
|
|
|
|
|
|
|
This is not a common case, but libraries whose only behavior is to
|
|
|
|
interpose symbols to alter existing behavior do not need to expose
|
|
|
|
their interposing symbols as API, so it's possible for the stub to be
|
|
|
|
empty while still needing a stub to link against. libsigchain is an
|
|
|
|
example of this.
|
|
|
|
"""
|
|
|
|
input_file = io.StringIO(textwrap.dedent("""\
|
|
|
|
VERSION_1 {
|
|
|
|
local:
|
|
|
|
*;
|
|
|
|
};
|
|
|
|
"""))
|
|
|
|
parser = symbolfile.SymbolFileParser(input_file, {}, Arch('arm'),
|
|
|
|
9, llndk=False, apex=True)
|
|
|
|
versions = parser.parse()
|
|
|
|
|
|
|
|
src_file = io.StringIO()
|
|
|
|
version_file = io.StringIO()
|
|
|
|
symbol_list_file = io.StringIO()
|
|
|
|
generator = ndkstubgen.Generator(src_file,
|
|
|
|
version_file,
|
|
|
|
symbol_list_file,
|
|
|
|
Arch('arm'),
|
|
|
|
9,
|
|
|
|
llndk=False,
|
|
|
|
apex=True)
|
|
|
|
generator.write(versions)
|
|
|
|
|
|
|
|
self.assertEqual('', src_file.getvalue())
|
|
|
|
self.assertEqual('', version_file.getvalue())
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
|
|
|
|
def main() -> None:
|
2020-06-23 00:10:31 +02:00
|
|
|
suite = unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
unittest.TextTestRunner(verbosity=3).run(suite)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|