2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-09-05 00:35:13 +00:00

add_or_remove_flag(): allow to add or remove multiple flags

Multiple flags can be given as string (will be split) or as array.

Also add some tests confirming that everything works as expected.
This commit is contained in:
Christian Boltz
2020-09-24 23:03:35 +02:00
parent 692f78cf3f
commit a2d3a382a8
2 changed files with 19 additions and 6 deletions

View File

@@ -181,16 +181,21 @@ def split_flags(flags):
# sort and remove duplicates
return sorted(set(flags_list))
def add_or_remove_flag(flags, flag_to_change, set_flag):
'''add (if set_flag == True) or remove the given flag_to_change to flags'''
def add_or_remove_flag(flags, flags_to_change, set_flag):
'''add (if set_flag == True) or remove the given flags_to_change to flags'''
if type_is_str(flags) or flags is None:
flags = split_flags(flags)
if type_is_str(flags_to_change) or flags_to_change is None:
flags_to_change = split_flags(flags_to_change)
if set_flag:
for flag_to_change in flags_to_change:
if flag_to_change not in flags:
flags.append(flag_to_change)
else:
for flag_to_change in flags_to_change:
if flag_to_change in flags:
flags.remove(flag_to_change)

View File

@@ -50,6 +50,14 @@ class AaTest_add_or_remove_flag(AATest):
([ None, 'audit', False ], [] ),
([ 'complain', 'audit', True ], ['audit', 'complain'] ),
([ ' complain ', 'audit', False ], ['complain'] ),
([ 'audit complain', ['audit', 'complain'], False ], [] ),
([ 'audit complain', 'audit complain', False ], [] ),
([ 'audit complain', ['audit', 'enforce'], False ], ['complain'] ),
([ 'audit complain', 'audit enforce', False ], ['complain'] ),
([ '', ['audit', 'complain'], True ], ['audit', 'complain'] ),
([ '', 'audit complain', True ], ['audit', 'complain'] ),
([ 'audit', ['audit', 'enforce'], True ], ['audit', 'enforce'] ),
([ 'audit', 'audit enforce', True ], ['audit', 'enforce'] ),
]
def _run_test(self, params, expected):