2013-09-28 20:43:06 +05:30
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
|
2015-03-31 22:29:06 +02:00
|
|
|
# Copyright (C) 2015 Christian Boltz <apparmor@cboltz.de>
|
2013-09-28 20:43:06 +05:30
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# ----------------------------------------------------------------------
|
2013-08-05 18:55:34 +05:30
|
|
|
import unittest
|
2015-03-31 22:29:06 +02:00
|
|
|
import inspect
|
2014-11-06 12:32:49 -08:00
|
|
|
import os
|
2013-08-05 18:55:34 +05:30
|
|
|
import re
|
2015-03-31 22:29:06 +02:00
|
|
|
import sys
|
2013-08-05 18:55:34 +05:30
|
|
|
|
|
|
|
import apparmor.common
|
2013-08-10 12:46:22 +05:30
|
|
|
import apparmor.config
|
2013-08-05 18:55:34 +05:30
|
|
|
|
|
|
|
class Test(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
def test_RegexParser(self):
|
2013-12-20 03:12:58 +05:30
|
|
|
tests = apparmor.config.Config('ini')
|
2013-08-10 12:46:22 +05:30
|
|
|
tests.CONF_DIR = '.'
|
|
|
|
regex_tests = tests.read_config('regex_tests.ini')
|
|
|
|
for regex in regex_tests.sections():
|
|
|
|
parsed_regex = re.compile(apparmor.common.convert_regexp(regex))
|
|
|
|
for regex_testcase in regex_tests.options(regex):
|
2013-08-11 15:22:07 +05:30
|
|
|
self.assertEqual(bool(parsed_regex.search(regex_testcase)), eval(regex_tests[regex][regex_testcase]), 'Incorrectly Parsed regex: %s' %regex)
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2013-08-10 12:46:22 +05:30
|
|
|
#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!')
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2015-03-31 22:29:06 +02:00
|
|
|
|
|
|
|
class AATest(unittest.TestCase):
|
|
|
|
tests = []
|
|
|
|
|
2014-09-03 18:24:57 -07:00
|
|
|
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 %s returned "%s", expected "%s"' \
|
|
|
|
%(self.parse_function.__doc__, parsed.serialize(), rule))
|
|
|
|
|
2015-04-22 22:01:34 +02:00
|
|
|
def setup_all_loops(module_name):
|
2015-03-31 22:29:06 +02:00
|
|
|
'''call setup_tests_loop() for each class in module_name'''
|
2015-04-22 22:01:34 +02:00
|
|
|
for name, obj in inspect.getmembers(sys.modules[module_name]):
|
2015-03-31 22:29:06 +02:00
|
|
|
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 '%s'" % (test_data)
|
|
|
|
setattr(test_class, 'test_%d' % (i), stub_test)
|
|
|
|
|
|
|
|
|
2014-09-03 18:24:57 -07:00
|
|
|
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 '%s': %s" % (line, desc)
|
|
|
|
setattr(test_class, 'test_%d' % (i), stub_test)
|
2013-08-05 18:55:34 +05:30
|
|
|
|
2014-11-06 12:32:49 -08:00
|
|
|
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
|
|
|
|
|
2015-04-01 23:43:29 +02:00
|
|
|
def read_file(path):
|
|
|
|
'''read and return file contents'''
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 18:55:34 +05:30
|
|
|
if __name__ == "__main__":
|
|
|
|
#import sys;sys.argv = ['', 'Test.test_RegexParser']
|
2013-12-20 03:12:58 +05:30
|
|
|
unittest.main()
|