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

from common import RostTestClass
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
from pychrysalide.analysis.scan.patterns.backends import BitapBackend


class TestRostFuzzingFixes(RostTestClass):
    """TestCases to remember all the fixes for crashes identified by fuzzing."""

    def testEmptyPatternListWithContent(self):
        """Check no backend is run if there is no pattern to look for."""

        content = MemoryContent(b'\n')

        rule = '''
'''

        backends = [
            AcismBackend, # This one was segfaulting
            BitapBackend,
        ]

        for b in backends:

            options = ScanOptions()
            options.backend_for_data = b

            scanner = ContentScanner(rule)
            ctx = scanner.analyze(options, content)

            self.assertIsNotNone(ctx)


    def testMandatoryCondition(self):
        """Ensure a condition section exists in a rule."""

        rule = '''
rule test {

}
'''

        with self.assertRaisesRegex(ValueError, 'Unable to create content scanner'):

            scanner = ContentScanner(rule)


    def testNonExistingPattern(self):
        """Avoid to count the matches of a non-existing pattern."""

        rule = '''
rule test {

   condition:
      #badid

}
'''

        with self.assertRaisesRegex(ValueError, 'Unable to create content scanner'):

            scanner = ContentScanner(rule)


    def testNamespacesWithoutReductionCode(self):
        """Clean the code for ROST namespaces."""

        rule = '''
rule test {

   condition:
      console

}
'''

        self.check_rule_failure(rule)


    def testCallOnNonCallable(self):
        """Reject calls on non callable expressions softly."""

        rule = '''
rule test {

   condition:
      console.log().log()

}
'''

        self.check_rule_failure(rule)


    def testSelfReferencingRule(self):
        """Reject any rule referencing itself as match condition."""

        rule = '''
rule test {

   condition:
      test

}
'''

        self.check_rule_failure(rule)