summaryrefslogtreecommitdiff
path: root/tests/analysis/scan/exprs.py
blob: c89dc5908d2feceeab7e8d3f3487564e93a78ed3 (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

from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.contents import MemoryContent
from pychrysalide.analysis.scan import ContentScanner
from pychrysalide.analysis.scan import ScanOptions
from pychrysalide.analysis.scan.patterns.backends import AcismBackend


class TestScanExpressions(ChrysalideTestCase):
    """TestCase for analysis.scan.exprs.*."""

    @classmethod
    def setUpClass(cls):

        super(TestScanExpressions, cls).setUpClass()

        cls._options = ScanOptions()
        cls._options.backend_for_data = AcismBackend


    def testBasicStringOperations(self):
        """Evaluate basic string operations."""

        cnt = MemoryContent(b'empty')

        rule = '''
rule test {

   condition:
      "123abc456" contains "abc"

}
'''

        scanner = ContentScanner(rule)
        ctx = scanner.analyze(self._options, cnt)

        self.assertTrue(ctx.has_match_for_rule('test'))

        rule = '''
rule test {

   condition:
      "123\t456" contains "\t"

}
'''

        scanner = ContentScanner(rule)
        ctx = scanner.analyze(self._options, cnt)

        self.assertTrue(ctx.has_match_for_rule('test'))

        rule = '''
rule test {

   condition:
      "123-456" startswith "1"

}
'''

        scanner = ContentScanner(rule)
        ctx = scanner.analyze(self._options, cnt)

        self.assertTrue(ctx.has_match_for_rule('test'))

        rule = '''
rule test {

   condition:
      "123-456" startswith "1234"

}
'''

        scanner = ContentScanner(rule)
        ctx = scanner.analyze(self._options, cnt)

        self.assertFalse(ctx.has_match_for_rule('test'))

        rule = '''
rule test {

   condition:
      "123-456" endswith "6"

}
'''

        scanner = ContentScanner(rule)
        ctx = scanner.analyze(self._options, cnt)

        self.assertTrue(ctx.has_match_for_rule('test'))

        rule = '''
rule test {

   condition:
      "123-456" endswith "3456"

}
'''

        scanner = ContentScanner(rule)
        ctx = scanner.analyze(self._options, cnt)

        self.assertFalse(ctx.has_match_for_rule('test'))

        rule = '''
rule test {

   condition:
      "ABCD" iequals "AbCd"

}
'''

        scanner = ContentScanner(rule)
        ctx = scanner.analyze(self._options, cnt)

        self.assertTrue(ctx.has_match_for_rule('test'))