apparmor/utils/test/test-baserule.py
Christian Boltz 902f88b0bb Add and use logprof_header() and logprof_header_localvars() in *Rule classes
BaseRule:
- add logprof_header() - sets the 'Qualifier' (audit, allow/deny) header
  if a qualifier is specified, calls logprof_header_localvars() and then
  returns an array of headers to display in aa-logprof and aa-mergeprof
- add logprof_header_localvars() - dummy function that needs to be
  implemented in the child classes

NetworkRule: add logprof_header_localvars() - adds 'Network Family'
and 'Socket Type' to the headers

CapabilityRule: add logprof_header_localvars() - adds 'Capability' to
the headers

Also change aa-mergeprof to use rule_obj.logprof_header() for network
and capability rules. This means deleting lots of lines (that moved to
the *Rule classes) and also deleting the last differences between
capabiltiy and network rules.

Finally add tests for the newly added functions.


Acked-by: Steve Beattie <steve@nxnw.org>
2015-06-06 14:04:11 +02:00

69 lines
2.1 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 apparmor.severity as severity
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)
def test_default_severity(self):
sev_db = severity.Severity('severity.db', 'unknown')
obj = BaseRule()
rank = obj.severity(sev_db)
self.assertEqual(rank, sev_db.NOT_IMPLEMENTED)
def test_logprof_header_localvars(self):
obj = BaseRule()
with self.assertRaises(AppArmorBug):
obj.logprof_header_localvars()
setup_all_loops(__name__)
if __name__ == '__main__':
unittest.main(verbosity=2)