summaryrefslogtreecommitdiff
path: root/tests/analysis/contents/endian.py
blob: 05571740effb1e2c92875b60b6807aa521076157 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python3-dbg
# -*- coding: utf-8 -*-


# Tests minimalistes pour valider le boutisme des accès mémoire.


from chrysacase import ChrysalideTestCase
from pychrysalide import SourceEndian
from pychrysalide.analysis import BinContent
from pychrysalide.analysis.contents import FileContent
from pychrysalide.arch import vmpa
import tempfile


class TestEndianness(ChrysalideTestCase):
    """TestCase for analysis.BinContent."""

    @classmethod
    def setUpClass(cls):

        super(TestEndianness, cls).setUpClass()

        cls._out = tempfile.NamedTemporaryFile()

        cls._out.write(b'\x01\x02\x03\x04')
        cls._out.write(b'\x05\x06\x07\x08')
        cls._out.write(b'\x11\x12\x13\x14')
        cls._out.write(b'\x15\x16\x17\x18')
        cls._out.write(b'\x21\x22\x23\x24')
        cls._out.write(b'\x25\x26\x27\x28')
        cls._out.write(b'\x31\x32\x33\x34')
        cls._out.write(b'\x35\x36\x37\x38')

        cls._out.flush()

        cls.log('Using temporary file "%s"' % cls._out.name)


    @classmethod
    def tearDownClass(cls):

        super(TestEndianness, cls).tearDownClass()

        cls.log('Delete file "%s"' % cls._out.name)

        cls._out.close()


    def testMiddleEndianness(self):
        """Test some old endianness."""

        fcnt = FileContent(self._out.name)

        # 16 bits

        start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u16(start, SourceEndian.LITTLE_WORD)
        self.assertEqual(val, 0x1516)

        start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u16(start, SourceEndian.BIG_WORD)
        self.assertEqual(val, 0x1615)

        # 32 bits

        start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u32(start, SourceEndian.LITTLE_WORD)
        self.assertEqual(val, 0x17181516)

        start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u32(start, SourceEndian.BIG_WORD)
        self.assertEqual(val, 0x16151817)

        # 64 bits

        start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u64(start, SourceEndian.LITTLE_WORD)
        self.assertEqual(val, 0x0708050603040102)

        start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u64(start, SourceEndian.BIG_WORD)
        self.assertEqual(val, 0x0201040306050807)


    def testEndianness(self):
        """Test usual endianness."""

        fcnt = FileContent(self._out.name)

        # 16 bits

        start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u16(start, SourceEndian.LITTLE)
        self.assertEqual(val, 0x1615)

        start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u16(start, SourceEndian.BIG)
        self.assertEqual(val, 0x1516)

        # 32 bits

        start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u32(start, SourceEndian.LITTLE)
        self.assertEqual(val, 0x18171615)

        start = vmpa(12, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u32(start, SourceEndian.BIG)
        self.assertEqual(val, 0x15161718)

        # 64 bits

        start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u64(start, SourceEndian.LITTLE)
        self.assertEqual(val, 0x0807060504030201)

        start = vmpa(0, vmpa.VmpaSpecialValue.NO_VIRTUAL)

        val = fcnt.read_u64(start, SourceEndian.BIG)
        self.assertEqual(val, 0x0102030405060708)