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


# Tests validant le bon calcul d'empreintes.


from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.contents import FileContent, RestrictedContent
from pychrysalide.arch import vmpa, mrange
import hashlib
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'AAAABBBBCCCCDDDD')

        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 testFullChecksum(self):
        """Check checksum of full content."""

        fcnt = FileContent(self._out.name)
        self.assertIsNotNone(fcnt)

        expected = hashlib.sha256(b'AAAABBBBCCCCDDDD').hexdigest()

        self.assertEqual(fcnt.checksum, expected)


    def testPartialChecksum(self):
        """Check checksum of restricted content."""

        fcnt = FileContent(self._out.name)
        self.assertIsNotNone(fcnt)

        start = vmpa(4, vmpa.VmpaSpecialValue.NO_VIRTUAL)
        covered = mrange(start, 4) # 'BBBB'

        rcnt = RestrictedContent(fcnt, covered)
        self.assertIsNotNone(rcnt)

        expected = hashlib.sha256(b'BBBB').hexdigest()

        self.assertEqual(rcnt.checksum, expected)