summaryrefslogtreecommitdiff
path: root/tests/format/elf/strings.py
blob: 0e09d7577d5f87646267cb81c0df15b83bff1f75 (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 que les chaînes présentes sont bien chargées en tant que telles.


from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.contents import FileContent
from pychrysalide.analysis import LoadedBinary
from pychrysalide.arch import RawInstruction
from threading import Event
import os
import sys


class TestElfString(ChrysalideTestCase):
    """TestCase for ELF strings."""

    @classmethod
    def setUpClass(cls):

        super(TestElfString, cls).setUpClass()

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

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

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


    def testElfStrings(self):
        """Ensure available strings are loaded as strings."""

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

        baselen = len(fullname) - len(filename)

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

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

        def disass_done(binary):
            worker.set()

        binary.connect('disassembly-done', disass_done)

        worker = Event()

        binary.analyse()

        worker.wait()

        expected = {
            'hello_arm_str'        : False,
            'no_command_line_str'  : False,
            'got_command_line_str' : False
        }

        for sym in binary.format.symbols:

            if sym.label in expected.keys():

                ins = binary.processor.find_instr_by_addr(sym.range.addr)

                if type(ins) is RawInstruction:
                    expected[sym.label] = ins.is_string

        for k in expected.keys():
            self.assertTrue(expected[k])