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

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 RostTestClass(ChrysalideTestCase):
    """TestCase for analysis.scan.ScanExpression."""

    @classmethod
    def setUpClass(cls):

        super(RostTestClass, cls).setUpClass()

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

        cls._empty_content = MemoryContent(b'')


    def _validate_rule_result(self, rule, content, expected):
        """Check for scan success or failure."""

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

        self.assertIsNotNone(ctx)

        if expected:
            self.assertTrue(ctx.has_match_for_rule('test'))
        else:
            self.assertFalse(ctx.has_match_for_rule('test'))

        return scanner, ctx


    def check_rule_success(self, rule, content = None):
        """Check for scan success."""

        if content is None:
            content = self._empty_content

        self._validate_rule_result(rule, content, True)


    def check_rule_failure(self, rule, content = None):
        """Check for scan failure."""

        if content is None:
            content = self._empty_content

        self._validate_rule_result(rule, content, False)