mirror of
https://gitlab.com/apparmor/apparmor
synced 2025-08-29 05:17:59 +00:00
Add __repr__() functions to BaseRule and BaseRuleset
This makes print()ing a class object much more helpful - instead of <apparmor.rule.network.NetworkRule object at 0x7f416b239e48> we now get something like <NetworkRule> network inet stream, (based on get_raw()) A NetworkRuleset will be printed as (also based on get_raw()) <NetworkRuleset> network inet stream, allow network inet stream, # comment </NetworkRuleset> Also add tests to test-network.py to ensure that __repr__() works as expected. Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
This commit is contained in:
parent
cc9cf967b2
commit
37ab41bb13
@ -50,6 +50,10 @@ class BaseRule(object):
|
|||||||
# Set only in the parse() class method
|
# Set only in the parse() class method
|
||||||
self.raw_rule = None
|
self.raw_rule = None
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
classname = self.__class__.__name__
|
||||||
|
return '<%s> ' % classname + self.get_raw()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def match(cls, raw_rule):
|
def match(cls, raw_rule):
|
||||||
'''return True if raw_rule matches the class (main) regex, False otherwise
|
'''return True if raw_rule matches the class (main) regex, False otherwise
|
||||||
@ -211,6 +215,10 @@ class BaseRuleset(object):
|
|||||||
'''called by __init__() and delete_all_rules() - override in child class to initialize more variables'''
|
'''called by __init__() and delete_all_rules() - override in child class to initialize more variables'''
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
classname = self.__class__.__name__
|
||||||
|
return '<%s>\n' % classname + '\n'.join(self.get_raw(1)) + '</%s>' % classname
|
||||||
|
|
||||||
def add(self, rule):
|
def add(self, rule):
|
||||||
'''add a rule object'''
|
'''add a rule object'''
|
||||||
self.rules.append(rule)
|
self.rules.append(rule)
|
||||||
|
@ -353,6 +353,15 @@ class NetworkLogprofHeaderTest(AATest):
|
|||||||
obj = NetworkRule._parse(params)
|
obj = NetworkRule._parse(params)
|
||||||
self.assertEqual(obj.logprof_header(), expected)
|
self.assertEqual(obj.logprof_header(), expected)
|
||||||
|
|
||||||
|
class NetworkRuleReprTest(AATest):
|
||||||
|
tests = [
|
||||||
|
(NetworkRule('inet', 'stream'), '<NetworkRule> network inet stream,'),
|
||||||
|
(NetworkRule.parse(' allow network inet stream, # foo'), '<NetworkRule> allow network inet stream, # foo'),
|
||||||
|
]
|
||||||
|
def _run_test(self, params, expected):
|
||||||
|
self.assertEqual(str(params), expected)
|
||||||
|
|
||||||
|
|
||||||
## --- tests for NetworkRuleset --- #
|
## --- tests for NetworkRuleset --- #
|
||||||
|
|
||||||
class NetworkRulesTest(AATest):
|
class NetworkRulesTest(AATest):
|
||||||
@ -439,6 +448,17 @@ class NetworkGlobTestAATest(AATest):
|
|||||||
class NetworkDeleteTestAATest(AATest):
|
class NetworkDeleteTestAATest(AATest):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class NetworkRulesetReprTest(AATest):
|
||||||
|
def test_network_ruleset_repr(self):
|
||||||
|
obj = NetworkRuleset()
|
||||||
|
obj.add(NetworkRule('inet', 'stream'))
|
||||||
|
obj.add(NetworkRule.parse(' allow network inet stream, # foo'))
|
||||||
|
|
||||||
|
expected = '<NetworkRuleset>\n network inet stream,\n allow network inet stream, # foo\n</NetworkRuleset>'
|
||||||
|
self.assertEqual(str(obj), expected)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
setup_all_loops(__name__)
|
setup_all_loops(__name__)
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main(verbosity=2)
|
unittest.main(verbosity=2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user