1from numbers import Integral
2
3from .base import TestBase
4from ..object import ObjectFile
5from ..object import Relocation
6from ..object import Section
7from ..object import Symbol
8
9class TestObjectFile(TestBase):
10    def get_object_file(self):
11        source = self.get_test_binary()
12        return ObjectFile(filename=source)
13
14    def test_create_from_file(self):
15        self.get_object_file()
16
17    def test_get_sections(self):
18        o = self.get_object_file()
19
20        count = 0
21        for section in o.get_sections():
22            count += 1
23            assert isinstance(section, Section)
24            assert isinstance(section.name, str)
25            assert isinstance(section.size, Integral)
26            assert isinstance(section.contents, str)
27            assert isinstance(section.address, Integral)
28            assert len(section.contents) == section.size
29
30        self.assertGreater(count, 0)
31
32        for section in o.get_sections():
33            section.cache()
34
35    def test_get_symbols(self):
36        o = self.get_object_file()
37
38        count = 0
39        for symbol in o.get_symbols():
40            count += 1
41            assert isinstance(symbol, Symbol)
42            assert isinstance(symbol.name, str)
43            assert isinstance(symbol.address, Integral)
44            assert isinstance(symbol.size, Integral)
45
46        self.assertGreater(count, 0)
47
48        for symbol in o.get_symbols():
49            symbol.cache()
50
51    def test_symbol_section_accessor(self):
52        o = self.get_object_file()
53
54        for symbol in o.get_symbols():
55            section = symbol.section
56            assert isinstance(section, Section)
57
58            break
59
60    def test_get_relocations(self):
61        o = self.get_object_file()
62        for section in o.get_sections():
63            for relocation in section.get_relocations():
64                assert isinstance(relocation, Relocation)
65                assert isinstance(relocation.address, Integral)
66                assert isinstance(relocation.offset, Integral)
67                assert isinstance(relocation.type_number, Integral)
68                assert isinstance(relocation.type_name, str)
69                assert isinstance(relocation.value_string, str)
70