diff --git a/utils/apparmor/profile_storage.py b/utils/apparmor/profile_storage.py index df0eb2d21..ce4802026 100644 --- a/utils/apparmor/profile_storage.py +++ b/utils/apparmor/profile_storage.py @@ -132,8 +132,11 @@ class ProfileStorage: raise AppArmorBug('Attempt to overwrite "%s" with %s, type %s' % (key, value, type(value))) def __repr__(self): - name = type(self).__name__ - return '\n<%s>\n%s\n\n' % (name, '\n'.join(self.get_rules_clean(1)), name) + classname = type(self).__name__ + header = '\n'.join(self.get_header(0, self['name'], False)) + rules = '\n'.join(self.get_rules_clean(1)) + endprofile = '}' + return f'\n<{classname}>\n{header}\n{rules}\n{endprofile}\n\n' def get(self, key, fallback=None): if key in self.data: diff --git a/utils/test/test-profile-storage.py b/utils/test/test-profile-storage.py index a970a1457..1ce6e298b 100644 --- a/utils/test/test-profile-storage.py +++ b/utils/test/test-profile-storage.py @@ -13,6 +13,7 @@ import unittest from apparmor.common import AppArmorBug, AppArmorException from apparmor.profile_storage import ProfileStorage, add_or_remove_flag, split_flags, var_transform +from apparmor.rule.capability import CapabilityRule from common_test import AATest, setup_all_loops @@ -121,6 +122,22 @@ class TestSetInvalid(AATest): with self.assertRaises(expected): self.storage[params[0]] = params[1] + def testInvalidTypeChange(self): + storage = ProfileStorage('/test/foo', 'hat', 'TEST') + storage.data['invalid'] = 42 # manually set behind __setitem__'s back to avoid checks + with self.assertRaises(AppArmorBug): + storage['invalid'] = 'foo' # attempt to change type from int to str + + +class AaTest_repr(AATest): + def testRepr(self): + prof_storage = ProfileStorage('/test/foo', 'hat', 'TEST') + prof_storage['name'] = 'foo' + prof_storage['xattrs'] = 'user.bar=bar' + prof_storage['capability'].add(CapabilityRule('dac_override')) + + self.assertEqual(str(prof_storage), '\n\nprofile foo xattrs=(user.bar=bar) {\n capability dac_override,\n\n}\n\n') + class AaTest_parse_profile_start(AATest): tests = (