diff --git a/utils/apparmor/rule/__init__.py b/utils/apparmor/rule/__init__.py index 27cd4b35b..6e0960f02 100644 --- a/utils/apparmor/rule/__init__.py +++ b/utils/apparmor/rule/__init__.py @@ -50,6 +50,10 @@ class BaseRule(object): # Set only in the parse() class method self.raw_rule = None + def __repr__(self): + classname = self.__class__.__name__ + return '<%s> ' % classname + self.get_raw() + @classmethod def match(cls, raw_rule): '''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''' pass + def __repr__(self): + classname = self.__class__.__name__ + return '<%s>\n' % classname + '\n'.join(self.get_raw(1)) + '' % classname + def add(self, rule): '''add a rule object''' self.rules.append(rule) diff --git a/utils/test/test-network.py b/utils/test/test-network.py index 0892376fb..c744c13c6 100644 --- a/utils/test/test-network.py +++ b/utils/test/test-network.py @@ -353,6 +353,15 @@ class NetworkLogprofHeaderTest(AATest): obj = NetworkRule._parse(params) self.assertEqual(obj.logprof_header(), expected) +class NetworkRuleReprTest(AATest): + tests = [ + (NetworkRule('inet', 'stream'), ' network inet stream,'), + (NetworkRule.parse(' allow network inet stream, # foo'), ' allow network inet stream, # foo'), + ] + def _run_test(self, params, expected): + self.assertEqual(str(params), expected) + + ## --- tests for NetworkRuleset --- # class NetworkRulesTest(AATest): @@ -439,6 +448,17 @@ class NetworkGlobTestAATest(AATest): class NetworkDeleteTestAATest(AATest): 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 = '\n network inet stream,\n allow network inet stream, # foo\n' + self.assertEqual(str(obj), expected) + + + setup_all_loops(__name__) if __name__ == '__main__': unittest.main(verbosity=2)