File: common_test.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 (144 lines) | stat: -rwxr-xr-x 4,765 bytes parent folder | download | duplicates (2)
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
# ----------------------------------------------------------------------
#    Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
#    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 as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
# ----------------------------------------------------------------------
import inspect
import os
import shutil
import sys
import tempfile
import unittest


# def test_readkey(self):
#     print("Please press the Y button on the keyboard.")
#     self.assertEqual(apparmor.common.readkey().lower(), 'y', 'Error reading key from shell!')

# If a profile can't be parsed by the tools, add it to skip_active_profiles or skip_extra_profiles.
# Add only the filename (without path), for example 'usr.bin.foo'.
# These skip lists are meant as a temporary solution, and should be empty on release.
skip_active_profiles = []

skip_extra_profiles = []


class AATest(unittest.TestCase):
    def setUp(self):
        self.maxDiff = None
        self.AASetup()

    def AASetup(self):
        """override this function if a test needs additional setup steps (instead of overriding setUp())"""

    def tearDown(self):
        if self.tmpdir and os.path.exists(self.tmpdir):
            shutil.rmtree(self.tmpdir)

        self.AATeardown()

    def AATeardown(self):
        """override this function if a test needs additional teardown steps (instead of overriding tearDown())"""

    def createTmpdir(self):
        self.tmpdir = tempfile.mkdtemp(prefix='aa-test-')

    def writeTmpfile(self, file, contents):
        if not self.tmpdir:
            self.createTmpdir()
        return write_file(self.tmpdir, file, contents)

    tests = ()
    tmpdir = None


class AAParseTest(unittest.TestCase):
    parse_function = None

    def _test_parse_rule(self, rule):
        self.assertIsNot(self.parse_function, 'Test class did not set a parse_function')
        parsed = self.parse_function(rule)
        self.assertEqual(
            rule, parsed.serialize(),
            'parse object {} returned "{}", expected "{}"'.format(
                self.parse_function.__doc__, parsed.serialize(), rule))


def setup_all_loops(module_name):
    """call setup_tests_loop() for each class in module_name"""
    for name, obj in inspect.getmembers(sys.modules[module_name]):
        if inspect.isclass(obj):
            if issubclass(obj, unittest.TestCase):
                setup_tests_loop(obj)


def setup_tests_loop(test_class):
    """Create tests in test_class using test_class.tests and self._run_test()

    test_class.tests should be tuples of (test_data, expected_results)
    test_data and expected_results can be of any type as long as test_class._run_test()
    know how to handle them.

    A typical definition for _run_test() is:
        def test_class._run_test(self, test_data, expected)
    """

    for (i, (test_data, expected)) in enumerate(test_class.tests):
        def stub_test(self, test_data=test_data, expected=expected):
            self._run_test(test_data, expected)

        stub_test.__doc__ = "test '{}'".format(test_data)
        setattr(test_class, 'test_{}'.format(i), stub_test)


def setup_regex_tests(test_class):
    """Create tests in test_class using test_class.tests and AAParseTest._test_parse_rule()

    test_class.tests should be tuples of (line, description)
    """
    for (i, (line, desc)) in enumerate(test_class.tests):
        def stub_test(self, line=line):
            self._test_parse_rule(line)

        stub_test.__doc__ = "test '{}': {}".format(line, desc)
        setattr(test_class, 'test_{}'.format(i), stub_test)


def setup_aa(aa):
    confdir = os.getenv('__AA_CONFDIR')
    try:
        if confdir:
            aa.init_aa(confdir=confdir)
        else:
            aa.init_aa()
    except AttributeError:
        # apparmor.aa module versions <= 2.11 do not have the init_aa() method
        pass


def write_file(directory, file, contents):
    """construct path, write contents to it, and return the constructed path"""
    path = os.path.join(directory, file)
    with open(path, 'w+') as f:
        f.write(contents)
    return path


def read_file(path):
    """read and return file contents"""
    with open(path, 'r') as f:
        return f.read()


if __name__ == "__main__":
    # import sys;sys.argv = ['', 'Test.test_RegexParser']
    unittest.main()