summaryrefslogtreecommitdiff
path: root/tests/format/known.py
blob: 3a51f31fb539361cdbac1c345ccb6610cc9c6bd8 (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

from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.contents import MemoryContent
from pychrysalide.format import KnownFormat


class TestKnownFormat(ChrysalideTestCase):
    """TestCase for format.KnownFormat."""


    def testCustomInstance(self):
        """Validate a full custom KnownFormat implementation."""

        data  = b'\x01\x02\x03'
        cnt = MemoryContent(data)


        class CustomFormat(KnownFormat):

            def _get_key(self):
                return 'tiny'

            def _get_description(self):
                return 'Small description'

        cf = CustomFormat(cnt)

        self.assertEqual(cf.key, 'tiny')
        self.assertEqual(cf.description, 'Small description')


        class EmptyCustomFormat(KnownFormat):
            pass

        cf = EmptyCustomFormat(cnt)

        # NotImplementedError: method implementation is missing for '_get_key'
        with self.assertRaisesRegex(NotImplementedError, "method implementation is missing for '_get_key'"):
            k = cf.key

        # NotImplementedError: method implementation is missing for '_get_description'
        with self.assertRaisesRegex(NotImplementedError, "method implementation is missing for '_get_description'"):
            k = cf.description


        class BadCustomFormat(KnownFormat):

            def _get_key(self):
                return 123

            def _get_description(self):
                return 456

        cf = BadCustomFormat(cnt)

        # ValueError: unexpected value type for known format key
        with self.assertRaisesRegex(ValueError, 'unexpected value type for known format key'):
            k = cf.key

        # ValueError: unexpected value type for known format description
        with self.assertRaisesRegex(ValueError, 'unexpected value type for known format description'):
            k = cf.description


    def testKnownFormatConstructor(self):
        """Load a simple content for a known format."""

        with self.assertRaisesRegex(RuntimeError, 'pychrysalide.format.KnownFormat is an abstract class'):
            fmt = KnownFormat()

        class MyKnownFormat(KnownFormat):
            pass

        with self.assertRaisesRegex(TypeError, 'function takes exactly 1 argument .0 given.'):
            fmt = MyKnownFormat()

        class MyKnownFormat2(KnownFormat):
            pass

        with self.assertRaisesRegex(TypeError, 'unable to convert the provided argument to binary content'):
            fmt = MyKnownFormat2(123)