Commit 365b6d60 authored by Steve Beattie's avatar Steve Beattie

utils tests: restructure Unix Parse tests

This patch abstracts out parse tests into a super class to inherit from
and converts the af_unix parse tests to use the super class.
Signed-off-by: Steve Beattie's avatarSteve Beattie <steve@nxnw.org>
Acked-by: Christian Boltz's avatarChristian Boltz <apparmor@cboltz.de>
parent 74da13e7
......@@ -33,6 +33,27 @@ class Test(unittest.TestCase):
# print("Please press the Y button on the keyboard.")
# self.assertEqual(apparmor.common.readkey().lower(), 'y', 'Error reading key from shell!')
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))
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)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.test_RegexParser']
......
......@@ -11,49 +11,29 @@
import apparmor.aa as aa
import unittest
class AAParseUnixTest(unittest.TestCase):
def _test_parse_unix_rule(self, rule):
unix = aa.parse_unix_rule(rule)
self.assertEqual(rule, unix.serialize(),
'ptrace object returned "%s", expected "%s"' % (unix.serialize(), rule))
def test_parse_plain_unix_rule(self):
self._test_parse_unix_rule('unix,')
def test_parse_r_unix_rule(self):
self._test_parse_unix_rule('unix r,')
def test_parse_w_unix_rule(self):
self._test_parse_unix_rule('unix w,')
def test_parse_rw_unix_rule(self):
self._test_parse_unix_rule('unix rw,')
def test_parse_send_unix_rule(self):
self._test_parse_unix_rule('unix send,')
def test_parse_receive_unix_rule(self):
self._test_parse_unix_rule('unix receive,')
def test_parse_r_paren_unix_rule(self):
self._test_parse_unix_rule('unix (r),')
def test_parse_w_paren_unix_rule(self):
self._test_parse_unix_rule('unix (w),')
def test_parse_rw_paren_unix_rule(self):
self._test_parse_unix_rule('unix (rw),')
def test_parse_send_paren_unix_rule(self):
self._test_parse_unix_rule('unix (send),')
def test_parse_receive_paren_unix_rule(self):
self._test_parse_unix_rule('unix (receive),')
def test_parse_complex_unix_rule(self):
self._test_parse_unix_rule('unix (connect, receive, send) type=stream peer=(label=unconfined,addr="@/tmp/.X11-unix/X[0-9]*"),')
from common_test import AAParseTest, setup_regex_tests
class AAParseUnixTest(AAParseTest):
def setUp(self):
self.parse_function = aa.parse_unix_rule
tests = [
('unix,', 'unix base keyword'),
('unix r,', 'unix r rule'),
('unix w,', 'unix w rule'),
('unix rw,', 'unix rw rule'),
('unix send,', 'unix send rule'),
('unix receive,', 'unix receive rule'),
('unix (r),', 'unix (r) rule'),
('unix (w),', 'unix (w) rule'),
('unix (rw),', 'unix (rw) rule'),
('unix (send),', 'unix (send) rule'),
('unix (receive),', 'unix (receive) rule'),
('unix (connect, receive, send) type=stream peer=(label=unconfined,addr="@/tmp/.X11-unix/X[0-9]*"),',
'complex unix rule'),
]
if __name__ == '__main__':
setup_regex_tests(AAParseUnixTest)
unittest.main(verbosity=2)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment