Implement __deepcopy__() for aare

Thanks to http://bugs.python.org/issue10076, we need to implement this
ourself :-/

Also add some tests to ensure __deepcopy__() works as expected.

I found this bug while testing the dbus patch series, which crashed
aa-cleanprof with
    TypeError: cannot deepcopy this pattern object


Acked-by: John Johansen <john.johansen@canonical.com>
This commit is contained in:
Christian Boltz 2015-12-27 16:15:08 +01:00
parent 7a25fef5f6
commit 514977f779
2 changed files with 27 additions and 0 deletions

View file

@ -46,6 +46,13 @@ class AARE(object):
'''returns a "printable" representation of AARE'''
return "AARE('%s')" % self.regex
def __deepcopy__(self, memo):
# thanks to http://bugs.python.org/issue10076, we need to implement this ourself
if self.orig_regex:
return AARE(self.orig_regex, is_path=False, log_event=True)
else:
return AARE(self.regex, is_path=False)
def match(self, expression):
'''check if the given expression (string or AARE) matches the regex'''

View file

@ -13,6 +13,7 @@
import unittest
from common_test import AATest, setup_all_loops
from copy import deepcopy
import re
from apparmor.common import convert_regexp, AppArmorBug, AppArmorException
from apparmor.aare import AARE, convert_expression_to_aare
@ -223,6 +224,25 @@ class TestAARERepr(AATest):
obj = AARE('/foo', True)
self.assertEqual(str(obj), "AARE('/foo')")
class TestAAREDeepcopy(AATest):
tests = [
# regex is path? log event expected (dummy value)
(AARE('/foo', False) , True),
(AARE('/foo', False, True) , True),
(AARE('/foo', True) , True),
(AARE('/foo', True, True) , True),
]
def _run_test(self, params, expected):
dup = deepcopy(params)
self.assertTrue(params.match('/foo'))
self.assertTrue(dup.match('/foo'))
self.assertEqual(params.regex, dup.regex)
self.assertEqual(params.orig_regex, dup.orig_regex)
self.assertEqual(params.orig_regex, dup.orig_regex)
setup_all_loops(__name__)
if __name__ == '__main__':