File: test-baserule.py

package info (click to toggle)
apparmor 4.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,096 kB
  • sloc: ansic: 24,943; python: 24,914; cpp: 9,074; sh: 8,166; yacc: 2,061; makefile: 1,923; lex: 1,215; pascal: 1,147; perl: 1,033; ruby: 365; lisp: 282; exp: 250; java: 212; xml: 159
file content (113 lines) | stat: -rw-r--r-- 3,479 bytes parent folder | download
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
#! /usr/bin/python3
# ------------------------------------------------------------------
#
#    Copyright (C) 2015 Christian Boltz <apparmor@cboltz.de>
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of version 2 of the GNU General Public
#    License published by the Free Software Foundation.
#
# ------------------------------------------------------------------

import re
import unittest

import apparmor.severity as severity
from apparmor.common import AppArmorBug, hasher
from apparmor.rule import BaseRule, parse_modifiers
from common_test import AATest, setup_all_loops


class TestBaserule(AATest):

    class ValidSubclass(BaseRule):
        @classmethod
        def _create_instance(cls, raw_rule, matches):
            pass

        def get_clean(self, depth=0):
            pass

        def _is_covered_localvars(self, other_rule):
            pass

        def _is_equal_localvars(self, other_rule, strict):
            pass

        def _logprof_header_localvars(self):
            pass

    def test_implemented_abstract_methods(self):
        self.ValidSubclass()

    def test_unimplemented_abstract_methods(self):
        with self.assertRaises(TypeError):
            BaseRule()

        class InvalidSubclass(BaseRule):
            pass

        with self.assertRaises(TypeError):
            InvalidSubclass()

    def test_abstract__create_instance(self):
        with self.assertRaises(NotImplementedError):
            BaseRule._create_instance('foo', None)

    def test_abstract__create_instance_2(self):
        with self.assertRaises(AppArmorBug):
            BaseRule.create_instance('foo')

    def test_abstract__match(self):
        with self.assertRaises(AppArmorBug):
            BaseRule._match('foo')

    def test_abstract__match2(self):
        with self.assertRaises(AppArmorBug):
            BaseRule.match('foo')

    def test_abstract__match3(self):
        with self.assertRaises(NotImplementedError):
            self.ValidSubclass.match('foo')

    def test_parse_modifiers_invalid(self):
        regex = re.compile(r'^\s*(priority\s*=\s*(?P<priority>[+-]?[0-9]*)\s+)?(?P<audit>audit\s+)?(?P<allow>allow\s+|deny\s+|invalid\s+)?')
        matches = regex.search('audit invalid ')

        with self.assertRaises(AppArmorBug):
            parse_modifiers(matches)

    def test_default_severity(self):
        sev_db = severity.Severity('../severity.db', 'unknown')
        obj = self.ValidSubclass()
        rank = obj.severity(sev_db)
        self.assertEqual(rank, sev_db.NOT_IMPLEMENTED)

    def test_edit_header_localvars(self):
        obj = self.ValidSubclass()
        with self.assertRaises(NotImplementedError):
            obj.edit_header()

    def test_validate_edit_localvars(self):
        obj = self.ValidSubclass()
        with self.assertRaises(NotImplementedError):
            obj.validate_edit('/foo')

    def test_store_edit_localvars(self):
        obj = self.ValidSubclass()
        with self.assertRaises(NotImplementedError):
            obj.store_edit('/foo')

    def test_from_hashlog(self):
        obj = self.ValidSubclass()
        with self.assertRaises(NotImplementedError):
            obj.from_hashlog(hasher())

    def test_hashlog_from_event(self):
        with self.assertRaises(NotImplementedError):
            BaseRule.hashlog_from_event(None, None)


setup_all_loops(__name__)
if __name__ == '__main__':
    unittest.main(verbosity=1)