summaryrefslogtreecommitdiff
path: root/tests/analysis/disass/block.py
blob: 8de44e1843063b05aa3573ed41a907f19aa5a4d4 (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
#!/usr/bin/python3
# -*- coding: utf-8 -*-


# S'assure du bon fonctionnement des blocs basiques


from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.contents import FileContent
from pychrysalide.analysis import LoadedBinary
from pychrysalide.format.elf import ElfFormat
import os
import sys


class TestBasicBlocks(ChrysalideTestCase):
    """TestCase for basic blocks."""

    @classmethod
    def setUpClass(cls):

        super(TestBasicBlocks, cls).setUpClass()

        cls.log('Compile binary "hello" if needed...')

        fullname = sys.modules[cls.__module__].__file__
        dirpath = os.path.dirname(fullname)

        os.system('make -C %s hello 2>&1 > /dev/null' % dirpath)


    @classmethod
    def tearDownClass(cls):

        super(TestBasicBlocks, cls).tearDownClass()

        cls.log('Delete built binaries...')

        fullname = sys.modules[cls.__module__].__file__
        dirpath = os.path.dirname(fullname)

        os.system('make -C %s clean 2>&1 > /dev/null' % dirpath)


    def testBlockList(self):
        """Check basic tests for basic block list."""

        fullname = sys.modules[self.__class__.__module__].__file__
        filename = os.path.basename(fullname)

        baselen = len(fullname) - len(filename)

        cnt = FileContent(fullname[:baselen] + 'hello')
        self.assertIsNotNone(cnt)

        fmt = ElfFormat(cnt)
        self.assertIsNotNone(fmt)

        binary = LoadedBinary(fmt)
        self.assertIsNotNone(binary)

        binary.analyze_and_wait()

        sym = fmt.find_symbol_by_label('main')
        self.assertIsNotNone(sym)

        found = sym.basic_blocks.find_by_starting_addr(sym.range.addr)
        self.assertIsNotNone(found)

        self.assertEqual(found, sym.basic_blocks.items[0])

        self.assertEqual(found.index, 0)

        self.assertEqual(found.rank, 0)