From 3ecb969a1296a6c7dfdc32a01cb9cc5c1ae9fb8c Mon Sep 17 00:00:00 2001 From: Steve Beattie Date: Thu, 20 Mar 2014 12:38:37 -0700 Subject: [PATCH] utils: add simple capability regex tests This patch adds some simple tests of the capability regex in apparmor/aa.py. Signed-off-by: Steve Beattie Acked-by: Tyler Hicks --- utils/test/test-regex_matches.py | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/utils/test/test-regex_matches.py b/utils/test/test-regex_matches.py index ffe20c2f7..167196fcc 100644 --- a/utils/test/test-regex_matches.py +++ b/utils/test/test-regex_matches.py @@ -110,6 +110,50 @@ def setup_split_comment_testcases(): stub_test.__doc__ = "test '%s'" % (test_string) setattr(AARegexSplitComment, 'test_split_comment_%d' % (i), stub_test) +class AARegexCapability(unittest.TestCase): + '''Tests for RE_PROFILE_CAP''' + + def test_simple_capability_01(self): + '''test ' capability net_raw,' ''' + + line = ' capability net_raw,' + result = aa.RE_PROFILE_CAP.search(line) + self.assertTrue(result, 'Couldn\'t find capability rule in "%s"' % line) + cap = result.groups()[2].strip() + self.assertEqual(cap, 'net_raw', 'Expected capability "%s", got "%s"' + % ('net_raw', cap)) + + def test_simple_capability_02(self): + '''test ' capability net_raw , ' ''' + + line = 'capability net_raw , ' + result = aa.RE_PROFILE_CAP.search(line) + self.assertTrue(result, 'Couldn\'t find capability rule in "%s"' % line) + cap = result.groups()[2].strip() + self.assertEqual(cap, 'net_raw', 'Expected capability "%s", got "%s"' + % ('net_raw', cap)) + + def test_capability_all_01(self): + '''test ' capability,' ''' + + line = ' capability,' + result = aa.RE_PROFILE_CAP.search(line) + self.assertTrue(result, 'Couldn\'t find capability rule in "%s"' % line) + + def test_capability_all_02(self): + '''test ' capability , ' ''' + + line = ' capability , ' + result = aa.RE_PROFILE_CAP.search(line) + self.assertTrue(result, 'Couldn\'t find capability rule in "%s"' % line) + + def test_simple_bad_capability_01(self): + '''test ' capabilitynet_raw,' ''' + + line = ' capabilitynet_raw,' + result = aa.RE_PROFILE_CAP.search(line) + self.assertFalse(result, 'Found unexpected capability rule in "%s"' % line) + if __name__ == '__main__': verbosity = 2 @@ -119,6 +163,7 @@ if __name__ == '__main__': test_suite = unittest.TestSuite() test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AARegexHasComma)) test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AARegexSplitComment)) + test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AARegexCapability)) result = unittest.TextTestRunner(verbosity=verbosity).run(test_suite) if not result.wasSuccessful(): exit(1)