summaryrefslogtreecommitdiff
path: root/tests/analysis/cattribs.py
blob: e388afc9b10bf772774cca3ba22232e37517cd1b (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
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3
# -*- coding: utf-8 -*-


# S'assure du bon fonctionnement des attributs de contenus binaires à charger


from chrysacase import ChrysalideTestCase
from pychrysalide.analysis import ContentAttributes


class TestProjectFeatures(ChrysalideTestCase):
    """TestCase for binary content attributes."""

    def testEmptyContentAttributeSet(self):
        """Check properties of empty content attribute set."""

        attribs, filename = ContentAttributes('')

        self.assertIsNotNone(attribs)
        self.assertIsNone(filename)

        self.assertEqual(len(attribs.keys), 0)


    def testContentAttributeSet(self):
        """Check properties of a basic content attribute set."""

        model = {
            'a': '0',
            'bb': '1',
            'ccc': '2',
            'dddd': '3',
        }

        orig_filename = 'filename'
        path = orig_filename

        for k in model.keys():
            path += '&%s=%s' % (k, model[k])

        attribs, filename = ContentAttributes(path)

        self.assertIsNotNone(attribs)
        self.assertEqual(orig_filename, filename)

        kcount = 0

        for key in attribs.keys:
            self.assertTrue(key in model.keys())
            kcount += 1

        self.assertEqual(kcount, len(model.keys()))


    def testMultiContentAttributeSet(self):
        """Check properties of a multi level content attribute set."""

        model = {
            'a': '0',
            'bb': '1',
            'ccc': '2',
            'dddd': '3',
        }

        path = ''

        for k in model.keys():
            path += '&%s=%s' % (k, model[k])

        path += '&'

        for k in model.keys():
            path += '&e%s=%s' % (k, model[k])

        attribs, filename = ContentAttributes(path)

        self.assertIsNotNone(attribs)
        self.assertIsNone(filename)

        kcount = 0

        for key in attribs.keys:
            self.assertTrue(key in model.keys())
            kcount += 1

        self.assertEqual(kcount, len(model.keys()))


    def testEmptyContentAttributeSet(self):
        """Check properties of empty content attribute sets."""

        path = '&&'

        attribs, filename = ContentAttributes(path)

        self.assertIsNotNone(attribs)
        self.assertIsNone(filename)

        self.assertEqual(len(attribs.keys), 0)

        path = '&&&'

        attribs, filename = ContentAttributes(path)

        self.assertIsNotNone(attribs)
        self.assertIsNone(filename)

        self.assertEqual(len(attribs.keys), 0)

        path = 'filename'

        attribs, filename = ContentAttributes(path)

        self.assertIsNotNone(attribs)
        self.assertEqual(filename, path)

        self.assertEqual(len(attribs.keys), 0)


    def testContentAttributesKeyAccess(self):
        """Test some access keys for content attributes."""

        model = {
            'a': '0',
        }

        path = ''

        for k in model.keys():
            path += '&%s=%s' % (k, model[k])

        attribs, _ = ContentAttributes(path)
        self.assertIsNotNone(attribs)

        with self.assertRaisesRegex(Exception, 'key must be a string value'):

            val = attribs[2]

        with self.assertRaisesRegex(Exception, 'attribute value not found for the provided key'):

            val = attribs['2']