2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-31 14:25:52 +00:00

Speed up list creations, and change lists to tuples where appropriate..

This commit is contained in:
Mark Grassi
2022-06-18 14:30:49 -04:00
parent 36c704ce04
commit 7581c9e113
55 changed files with 1756 additions and 1770 deletions

View File

@@ -23,8 +23,7 @@ from apparmor.common import AppArmorException, AppArmorBug
from apparmor.translations import init_translation
_ = init_translation()
exp = namedtuple('exp', ['comment',
'orig_path', 'target'])
exp = namedtuple('exp', ('comment', 'orig_path', 'target'))
# --- tests for single AliasRule --- #
@@ -40,12 +39,12 @@ class AliasTest(AATest):
self.assertEqual(expected.comment, obj.comment)
class AliasTestParse(AliasTest):
tests = [
tests = (
# rawrule comment orig_path target
('alias /foo -> /bar,', exp('', '/foo', '/bar' )),
(' alias /foo -> /bar , # comment', exp(' # comment', '/foo', '/bar' )),
('alias "/foo 2" -> "/bar 2" ,', exp('', '/foo 2', '/bar 2' )),
]
)
def _run_test(self, rawrule, expected):
self.assertTrue(AliasRule.match(rawrule))
@@ -54,14 +53,14 @@ class AliasTestParse(AliasTest):
self._compare_obj(obj, expected)
class AliasTestParseInvalid(AliasTest):
tests = [
tests = (
# rawrule matches regex exception
('alias ,' , (False, AppArmorException)),
('alias /foo ,' , (False, AppArmorException)),
('alias /foo -> ,' , (True, AppArmorException)),
('alias -> /bar ,' , (True, AppArmorException)),
('/foo -> bar ,' , (False, AppArmorException)),
]
)
def _run_test(self, rawrule, expected):
self.assertEqual(AliasRule.match(rawrule), expected[0])
@@ -69,31 +68,31 @@ class AliasTestParseInvalid(AliasTest):
AliasRule.parse(rawrule)
class AliasFromInit(AliasTest):
tests = [
tests = (
# AliasRule object comment orig_path target
(AliasRule('/foo', '/bar'), exp('', '/foo', '/bar' )),
(AliasRule('/foo', '/bar', comment='# cmt'), exp('# cmt', '/foo', '/bar' )),
]
)
def _run_test(self, obj, expected):
self._compare_obj(obj, expected)
class InvalidAliasInit(AATest):
tests = [
tests = (
# init params expected exception
([None, '/bar' ], AppArmorBug), # orig_path not a str
(['', '/bar' ], AppArmorException), # empty orig_path
(['foo', '/bar' ], AppArmorException), # orig_path not starting with /
((None, '/bar' ), AppArmorBug), # orig_path not a str
(('', '/bar' ), AppArmorException), # empty orig_path
(('foo', '/bar' ), AppArmorException), # orig_path not starting with /
(['/foo', None ], AppArmorBug), # target not a str
(['/foo', '' ], AppArmorException), # empty target
(['/foo', 'bar' ], AppArmorException), # target not starting with /
]
(('/foo', None ), AppArmorBug), # target not a str
(('/foo', '' ), AppArmorException), # empty target
(('/foo', 'bar' ), AppArmorException), # target not starting with /
)
def _run_test(self, params, expected):
with self.assertRaises(expected):
AliasRule(params[0], params[1])
AliasRule(*params)
def test_missing_params_1(self):
with self.assertRaises(TypeError):
@@ -132,13 +131,13 @@ class InvalidAliasTest(AATest):
class WriteAliasTestAATest(AATest):
tests = [
tests = (
# raw rule clean rule
(' alias /foo -> /bar, ', 'alias /foo -> /bar,'),
(' alias /foo -> /bar, # comment', 'alias /foo -> /bar,'),
(' alias "/foo" -> "/bar", ', 'alias /foo -> /bar,'),
(' alias "/foo 2" -> "/bar 2", ', 'alias "/foo 2" -> "/bar 2",'),
]
)
def _run_test(self, rawrule, expected):
self.assertTrue(AliasRule.match(rawrule))
@@ -182,16 +181,16 @@ class AliasCoveredTest(AATest):
class AliasCoveredTest_01(AliasCoveredTest):
rule = 'alias /foo -> /bar,'
tests = [
tests = (
# rule equal strict equal covered covered exact
(' alias /foo -> /bar,' , [ True , True , True , True ]),
(' alias /foo -> /bar , ' , [ True , False , True , True ]),
(' alias /foo -> /bar, # comment' , [ True , False , True , True ]),
(' alias /foo -> /bar, # comment' , [ True , False , True , True ]),
(' alias /foo -> /asdf,' , [ False , False , False , False ]),
(' alias /whatever -> /bar,' , [ False , False , False , False ]),
(' alias /whatever -> /asdf,' , [ False , False , False , False ]),
]
(' alias /foo -> /bar,' , ( True , True , True , True )),
(' alias /foo -> /bar , ' , ( True , False , True , True )),
(' alias /foo -> /bar, # comment' , ( True , False , True , True )),
(' alias /foo -> /bar, # comment' , ( True , False , True , True )),
(' alias /foo -> /asdf,' , ( False , False , False , False )),
(' alias /whatever -> /bar,' , ( False , False , False , False )),
(' alias /whatever -> /asdf,' , ( False , False , False , False )),
)
class AliasCoveredTest_Invalid(AATest):
# def test_borked_obj_is_covered_1(self):
@@ -228,9 +227,9 @@ class AliasCoveredTest_Invalid(AATest):
obj.is_equal(testobj)
class AliasLogprofHeaderTest(AATest):
tests = [
tests = (
('alias /foo -> /bar,', [_('Alias'), '/foo -> /bar' ]),
]
)
def _run_test(self, params, expected):
obj = AliasRule.parse(params)