From 0cfef21713cdf331f65e761d2e2d16acee5b3be6 Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Sat, 16 May 2020 23:24:11 +0200 Subject: [PATCH] IncludeRuleset: add get_all_full_paths() This function returns a list of full paths of all includes. Also add some tests. --- utils/apparmor/rule/include.py | 10 +++++++++- utils/test/test-include.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/utils/apparmor/rule/include.py b/utils/apparmor/rule/include.py index 448cfb139..f7736b115 100644 --- a/utils/apparmor/rule/include.py +++ b/utils/apparmor/rule/include.py @@ -156,4 +156,12 @@ class IncludeRule(BaseRule): class IncludeRuleset(BaseRuleset): '''Class to handle and store a collection of include rules''' - pass + + def get_all_full_paths(self, profile_dir): + ''' get full path of all includes ''' + + paths = [] + for rule_obj in self.rules: + paths += rule_obj.get_full_paths(profile_dir) + + return paths diff --git a/utils/test/test-include.py b/utils/test/test-include.py index ba2b58ea3..3d7e67753 100644 --- a/utils/test/test-include.py +++ b/utils/test/test-include.py @@ -381,6 +381,15 @@ class IncludeFullPathsTest(AATest): ## --- tests for IncludeRuleset --- # class IncludeRulesTest(AATest): + def AASetup(self): + self.createTmpdir() + + #copy the local profiles to the test directory + self.profile_dir = '%s/profiles' % self.tmpdir + shutil.copytree('../../profiles/apparmor.d/', self.profile_dir, symlinks=True) + + write_file(self.profile_dir, 'baz', '/baz r,') + def test_empty_ruleset(self): ruleset = IncludeRuleset() ruleset_2 = IncludeRuleset() @@ -389,6 +398,7 @@ class IncludeRulesTest(AATest): self.assertEqual([], ruleset_2.get_raw(2)) self.assertEqual([], ruleset_2.get_clean(2)) self.assertEqual([], ruleset_2.get_clean_unsorted(2)) + self.assertEqual([], ruleset.get_all_full_paths(self.profile_dir)) def test_ruleset_1(self): ruleset = IncludeRuleset() @@ -415,12 +425,18 @@ class IncludeRulesTest(AATest): '', ] + expected_fullpaths = [ + os.path.join(self.profile_dir, 'foo'), + '/bar' + ] + for rule in rules: ruleset.add(IncludeRule.parse(rule)) self.assertEqual(expected_raw, ruleset.get_raw()) self.assertEqual(expected_clean, ruleset.get_clean()) self.assertEqual(expected_clean_unsorted, ruleset.get_clean_unsorted()) + self.assertEqual(expected_fullpaths, ruleset.get_all_full_paths(self.profile_dir)) def test_ruleset_2(self): ruleset = IncludeRuleset() @@ -455,12 +471,19 @@ class IncludeRulesTest(AATest): '', ] + expected_fullpaths = [ + os.path.join(self.profile_dir, 'baz'), + os.path.join(self.profile_dir, 'foo'), + '/bar', + ] + for rule in rules: ruleset.add(IncludeRule.parse(rule)) self.assertEqual(expected_raw, ruleset.get_raw()) self.assertEqual(expected_clean, ruleset.get_clean()) self.assertEqual(expected_clean_unsorted, ruleset.get_clean_unsorted()) + self.assertEqual(expected_fullpaths, ruleset.get_all_full_paths(self.profile_dir)) class IncludeGlobTestAATest(AATest): def setUp(self):