1#!/usr/bin/env python
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""This file contains unit tests for vtable_dumper."""
18
19import os
20import unittest
21
22from vts.utils.python.library.vtable import vtable_dumper
23
24_VTABLES = [
25    ('_ZTV11VirtualBase',
26     [(0x8, ['_ZTI11VirtualBase']),
27      (0x10, ['__cxa_pure_virtual']),
28      (0x18, ['__cxa_pure_virtual']),
29      (0x20, ['__cxa_pure_virtual']),
30      (0x28, ['_ZN11VirtualBase3barEv']),
31      (0x30, ['_ZN11VirtualBase3bazEv'])]),
32    ('_ZTV14VirtualDerived',
33     [(0x8, ['_ZTI14VirtualDerived']),
34      (0x10, ['_ZN14VirtualDerivedD2Ev']),
35      (0x18, ['_ZN14VirtualDerivedD0Ev']),
36      (0x20, ['_ZN14VirtualDerived3fooEv']),
37      (0x28, ['_ZN11VirtualBase3barEv']),
38      (0x30, ['__cxa_pure_virtual'])]),
39    ('_ZTV15ConcreteDerived',
40     [(0x8, ['_ZTI15ConcreteDerived']),
41      (0x10, ['_ZN15ConcreteDerivedD2Ev']),
42      (0x18, ['_ZN15ConcreteDerivedD0Ev']),
43      (0x20, ['_ZN14VirtualDerived3fooEv']),
44      (0x28, ['_ZN15ConcreteDerived3barEv']),
45      (0x30, ['_ZN15ConcreteDerived3bazEv'])]),
46]
47
48
49class VtableDumperTest(unittest.TestCase):
50    """Unit tests for VtableDumper from vtable_dumper."""
51
52    def setUp(self):
53        """Creates a VtableDumper."""
54        dir_path = os.path.dirname(os.path.realpath(__file__))
55        self.elf_file_path = os.path.join(dir_path, 'testing', 'libtest.so')
56        self.dumper = vtable_dumper.VtableDumper(self.elf_file_path)
57
58    def tearDown(self):
59        """Closes the VtableDumper."""
60        self.dumper.Close()
61
62    def testDumpVtables(self):
63        """Tests that DumpVtables dumps vtable structure correctly."""
64        vtables_dump = []
65        for vtable in self.dumper.DumpVtables():
66            entries = [(entry.offset, entry.names) for entry in vtable.entries]
67            vtables_dump.append((vtable.name, entries))
68        vtables_dump.sort()
69        self.assertEqual(vtables_dump, _VTABLES)
70
71
72if __name__ == '__main__':
73    unittest.main()
74