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
132
133
134
135
136
137
138
139
140
141
|
#!/usr/bin/python3-dbg
# -*- coding: utf-8 -*-
# Tests minimalistes pour valider l'intégration des contenus restreints
# depuis Python.
from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.contents import FileContent, RestrictedContent
from pychrysalide.arch import vmpa, mrange
import tempfile
class TestRestrictedContent(ChrysalideTestCase):
"""TestCase for analysis.contents.RestrictedContent."""
@classmethod
def setUpClass(cls):
super(TestRestrictedContent, 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(TestRestrictedContent, cls).tearDownClass()
cls.log('Delete file "%s"' % cls._out.name)
cls._out.close()
def testReadAccess(self):
"""Check valid accesses to restricted content."""
fcnt = FileContent(self._out.name)
start = vmpa(12, vmpa.VMPA_NO_VIRTUAL)
covered = mrange(start, 12) # 0x15 ... 0x28
rcnt = RestrictedContent(fcnt, covered)
self.assertIsNotNone(rcnt)
val = rcnt.read_u8(start)
self.assertEqual(val, b'\x15')
val = rcnt.read_u8(start)
self.assertEqual(val, b'\x16')
val = rcnt.read_u16(start, vmpa.SRE_LITTLE)
self.assertEqual(val, b'\x17\x18')
val = rcnt.read_u32(start, vmpa.SRE_LITTLE)
self.assertEqual(val, b'\x21\x22\x23\x24')
def testBorderLineAccess(self):
"""Check valid border line accesses to restricted content."""
fcnt = FileContent(self._out.name)
start = vmpa(12, vmpa.VMPA_NO_VIRTUAL)
covered = mrange(start, 12) # 0x15 ... 0x28
rcnt = RestrictedContent(fcnt, covered)
self.assertIsNotNone(rcnt)
start = vmpa(12, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u8(start)
self.assertEqual(val, b'\x15')
start = vmpa(12, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u16(start, vmpa.SRE_LITTLE)
self.assertEqual(val, b'\x15\x16')
start = vmpa(12, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u32(start, vmpa.SRE_LITTLE)
self.assertEqual(val, b'\x15\x16\x17\x18')
start = vmpa(12, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u64(start, vmpa.SRE_LITTLE)
self.assertEqual(val, b'\x15\x16\x17\x18\x21\x22\x23\x24')
start = vmpa(23, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u8(start)
self.assertEqual(val, b'\x28')
start = vmpa(22, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u16(start, vmpa.SRE_LITTLE)
self.assertEqual(val, b'\x27\x28')
start = vmpa(20, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u32(start, vmpa.SRE_LITTLE)
self.assertEqual(val, b'\x25\x26\x27\x28')
start = vmpa(16, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u64(start, vmpa.SRE_LITTLE)
self.assertEqual(val, b'\x21\x22\x23\x24\x25\x26\x27\x28')
def testWrongAccess(self):
"""Check invalid accesses to restricted content."""
fcnt = FileContent(self._out.name)
start = vmpa(12, vmpa.VMPA_NO_VIRTUAL)
covered = mrange(start, 12) # 0x15 ... 0x28
rcnt = RestrictedContent(fcnt, covered)
self.assertIsNotNone(rcnt)
with self.assertRaisesRegex(Exception, 'Invalid read access.'):
start = vmpa(1, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u8(start)
with self.assertRaisesRegex(Exception, 'Invalid read access.'):
start = vmpa(11, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u16(start, vmpa.SRE_LITTLE)
with self.assertRaisesRegex(Exception, 'Invalid read access.'):
start = vmpa(23, vmpa.VMPA_NO_VIRTUAL)
val = rcnt.read_u16(start, vmpa.SRE_LITTLE)
|