summaryrefslogtreecommitdiff
path: root/tests/analysis/scan/expr.py
blob: dbe8c55aa3fcd0a25ec38cca29eb9a6b60a9c2d5 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169


from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.scan import ScanExpression
from pychrysalide.glibext import ComparableItem


class TestScanExpression(ChrysalideTestCase):
    """TestCase for analysis.scan.ScanExpression."""


    def testDirectInstances(self):
        """Reject direct instances."""

        with self.assertRaisesRegex(RuntimeError, 'pychrysalide.analysis.scan.ScanExpression is an abstract class'):

            e = ScanExpression()


    def testBooleanComparison(self):
        """Compare custom scan expressions."""

        class StrLenExpr(ScanExpression):

            def __init__(self, value):
                super().__init__(ScanExpression.ExprValueType.STRING)
                self._value = value

            def _cmp_rich(self, other, op):

                if op == ComparableItem.RichCmpOperation.EQ:
                    return len(self._value) == len(other._value)


        e0 = StrLenExpr('00000000000')

        e1 = StrLenExpr('00000000000')

        e2 = StrLenExpr('000000000000000000000000000')

        self.assertTrue(e0 == e1)

        # !?
        # Python teste e0 != e1 (non implémenté), puis e1 != e0 (pareil) et en déduit une différence !
        # self.assertFalse(e0 != e1)

        self.assertFalse(e0 == e2)

        # TypeError: '<' not supported between instances of 'StrLenExpr' and 'StrLenExpr'
        with self.assertRaisesRegex(TypeError, '\'<\' not supported between instances'):
            self.assertTrue(e0 < e1)






    # def testTypeSubclassing(self):
    #     """Verify the data type subclassing is working."""

    #     class MyType(DataType):

    #         def __init__(self, num):
    #             super(MyType, self).__init__()
    #             self._num = num

    #         def _to_string(self, include):
    #             return '%x' % self._num

    #         def _dup(self):
    #             return MyType(self._num)

    #     tp = MyType(0x123)

    #     self.assertEqual(str(tp), '123')

    #     tp2 = tp.dup()

    #     self.assertEqual(str(tp), str(tp2))


    # def testTypeDefaultProperties(self):
    #     """Check for default values of some type properties."""

    #     class MyPropType(DataType):
    #         pass

    #     tp = MyPropType()

    #     self.assertTrue(tp.handle_namespaces)

    #     self.assertFalse(tp.is_pointer)

    #     self.assertFalse(tp.is_reference)

    #     class MyPropType2(DataType):

    #         def _handle_namespaces(self):
    #             return True

    #         def _is_pointer(self):
    #             return 123 < 1234

    #         def _is_reference(self):
    #             return False

    #     tp2 = MyPropType2()

    #     self.assertTrue(tp.handle_namespaces)

    #     self.assertTrue(tp2.is_pointer)

    #     self.assertFalse(tp2.is_reference)


    # def testTypeNamespaces(self):
    #     """Test the type namespace property."""

    #     class MyNSType(DataType):

    #         def __init__(self, name):
    #             super(MyNSType, self).__init__()
    #             self._name = name

    #         def _to_string(self, include):
    #             return self._name

    #     tp = MyNSType('TP')
    #     ns = MyNSType('NS')

    #     self.assertIsNone(tp.namespace)

    #     tp.namespace = (ns, '.')

    #     self.assertEqual(str(tp), 'NS.TP')

    #     self.assertEqual(tp.namespace, (ns, '.'))


    # def testTypeHash(self):
    #     """Hash a user-defined type."""

    #     class MyUserType(DataType):

    #         def __init__(self, name):
    #             super(MyUserType, self).__init__()
    #             self._name = name

    #         def _hash(self):
    #             return hash(self._name)

    #     tp = MyUserType('random')

    #     self.assertEqual(tp.hash, hash('random') & ((1 << 32) - 1))

    #     class MyOutOfRangeUserType(DataType):

    #         hard_coded_hash = -8752470794866657507

    #         def __init__(self, name):
    #             super(MyOutOfRangeUserType, self).__init__()
    #             self._name = name

    #         def _hash(self):
    #             return self.hard_coded_hash

    #     tp = MyOutOfRangeUserType('out-of-range')

    #     self.assertEqual(tp.hash, MyOutOfRangeUserType.hard_coded_hash & ((1 << 32) - 1))