summaryrefslogtreecommitdiff
path: root/tests/format/symbol.py
blob: 6c4f573f246adf0c01cb000aba67d59fd33a5394 (plain)
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
#!/usr/bin/python3-dbg
# -*- coding: utf-8 -*-


# Tests pour valider la gestion des symboles


from chrysacase import ChrysalideTestCase
from pychrysalide.arch import vmpa, mrange
from pychrysalide.format import BinSymbol


class TestBinarySymbols(ChrysalideTestCase):
    """TestCase for format.BinSymbol."""


    def testSymbolProperties(self):
        """Validate the basic properties of symbols."""

        saddr = vmpa(0x10, vmpa.VMPA_NO_VIRTUAL)
        srange = mrange(saddr, 0x3)
        symbol = BinSymbol(srange, BinSymbol.SymbolType.ENTRY_POINT)

        self.assertEqual(symbol.range.length, 0x3)

        symbol.range = mrange(saddr, 0x4)

        self.assertEqual(symbol.range.length, 0x4)

        self.assertEqual(symbol.stype, BinSymbol.SymbolType.ENTRY_POINT)

        symbol.stype = BinSymbol.SymbolType.DATA

        self.assertEqual(symbol.stype, BinSymbol.SymbolType.DATA)

        self.assertEqual(symbol.status, BinSymbol.SymbolStatus.INTERNAL)

        symbol.status = BinSymbol.SymbolStatus.EXPORTED

        self.assertEqual(symbol.status, BinSymbol.SymbolStatus.EXPORTED)

        self.assertEqual(symbol.label, None)

        symbol.label = 'AAA'

        self.assertEqual(symbol.label, 'AAA')

        symbol.label = None

        self.assertEqual(symbol.label, None)


    def testSymbolDefaultStatus(self):
        """Validate the default status for symbols."""

        saddr = vmpa(0x10, vmpa.VMPA_NO_VIRTUAL)
        srange = mrange(saddr, 0x3)
        symbol = BinSymbol(srange, BinSymbol.SymbolType.ENTRY_POINT)

        self.assertEqual(symbol.status, BinSymbol.SymbolStatus.INTERNAL)

        self.assertEqual(str(symbol.status), 'SymbolStatus.INTERNAL')


    def testSymbolComparison(self):
        """Compare symbols and check the result."""

        saddr = vmpa(0x100, vmpa.VMPA_NO_VIRTUAL)
        srange = mrange(saddr, 0x3)
        symbol0 = BinSymbol(srange, BinSymbol.SymbolType.ENTRY_POINT)

        saddr = vmpa(0x10, vmpa.VMPA_NO_VIRTUAL)
        srange = mrange(saddr, 0x3)
        symbol1 = BinSymbol(srange, BinSymbol.SymbolType.ENTRY_POINT)

        saddr = vmpa(0x100, vmpa.VMPA_NO_VIRTUAL)
        srange = mrange(saddr, 0x30)
        symbol2 = BinSymbol(srange, BinSymbol.SymbolType.ENTRY_POINT)

        self.assertTrue(symbol0 == symbol0)

        self.assertTrue(symbol0 > symbol1)

        self.assertTrue(symbol1 < symbol2)

        self.assertTrue(symbol0 == symbol2)


    def testSymbolSubclassing(self):
        """Verify the symbol subclassing is working."""

        class MySymbol(BinSymbol):
            def _get_label(self):
                return 'AAA'

        saddr = vmpa(0x100, vmpa.VMPA_NO_VIRTUAL)
        srange = mrange(saddr, 0x3)
        symbol = MySymbol(srange, BinSymbol.SymbolType.ENTRY_POINT)

        self.assertEqual(symbol.label, 'AAA')

        symbol.label = 'BBB'

        self.assertEqual(symbol.label, 'BBB')

        class MyOtherSymbol(BinSymbol):
            pass

        other = MyOtherSymbol(srange, BinSymbol.SymbolType.ENTRY_POINT)

        self.assertEqual(other.label, None)

        other.label = 'CCC'

        self.assertEqual(other.label, 'CCC')