2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-29 05:17:59 +00:00
apparmor/utils/test/test-baserule.py
Christian Boltz 6dade51f92 Add match() and _match() class methods to rule classes
Add match() and _match() class methods to rule classes:
- _match() returns a regex match object for the given raw_rule
- match() converts the _match() result to True or False

The primary usage is to get an answer to the question "is this raw_rule
your job?". (For a moment, I thought about naming the function
*Rule.myjob() instead of *Rule.match() ;-)

My next patch will change aa.py to use *Rule.match() instead of directly
using RE_*, which will make the import list much shorter and hide
another implementation detail inside the rule classes.

Also change _parse() to use _match() instead of the regex, and add some
tests for match() and _match().


Acked-by: Seth Arnold <seth.arnold@canonical.com>
2015-04-26 21:59:12 +02:00

59 lines
1.7 KiB
Python

#! /usr/bin/env python
# ------------------------------------------------------------------
#
# 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 unittest
from common_test import AATest, setup_all_loops
from apparmor.common import AppArmorBug
from apparmor.rule import BaseRule, parse_modifiers
import re
class TestBaserule(AATest):
def test_abstract__parse(self):
with self.assertRaises(AppArmorBug):
BaseRule._parse('foo')
def test_abstract__parse_2(self):
with self.assertRaises(AppArmorBug):
BaseRule.parse('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_is_equal_localvars(self):
obj = BaseRule()
with self.assertRaises(AppArmorBug):
obj.is_equal_localvars(BaseRule())
def test_is_covered_localvars(self):
obj = BaseRule()
with self.assertRaises(AppArmorBug):
obj.is_covered_localvars(None)
def test_parse_modifiers_invalid(self):
regex = re.compile('^\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)
setup_all_loops(__name__)
if __name__ == '__main__':
unittest.main(verbosity=2)