mirror of
https://gitlab.com/apparmor/apparmor
synced 2025-08-31 06:16:03 +00:00
Move re_match_include() to regex.py and improve it
The function is basically a wrapper around a regex, so regex.py is a much better home. While on it, rename the regex to RE_INCLUDE, change it to named matches, use RE_EOL to handle comments and compile it outside the function, which should result in a (small) performance improvement. Also rewrite re_match_include(), let it check for empty include filenames ("#include <>") and let it raise AppArmorException in that case. Finally, adjust code calling it to the new location, and add some tests for re_match_include() Acked-by: Kshitij Gupta <kgupta8592@gmail.com>
This commit is contained in:
@@ -12,9 +12,9 @@
|
||||
import apparmor.aa as aa
|
||||
import unittest
|
||||
from common_test import AATest, setup_all_loops
|
||||
from apparmor.common import AppArmorBug
|
||||
from apparmor.common import AppArmorBug, AppArmorException
|
||||
|
||||
from apparmor.regex import strip_quotes, parse_profile_start_line, RE_PROFILE_START, RE_PROFILE_CAP
|
||||
from apparmor.regex import strip_quotes, parse_profile_start_line, re_match_include, RE_PROFILE_START, RE_PROFILE_CAP
|
||||
|
||||
|
||||
class AARegexTest(AATest):
|
||||
@@ -465,6 +465,34 @@ class TestInvalid_parse_profile_start_line(AATest):
|
||||
with self.assertRaises(AppArmorBug):
|
||||
parse_profile_start_line(line, 'somefile')
|
||||
|
||||
class Test_re_match_include(AATest):
|
||||
tests = [
|
||||
('#include <abstractions/base>', 'abstractions/base' ),
|
||||
('#include <abstractions/base> # comment', 'abstractions/base' ),
|
||||
('#include<abstractions/base>#comment', 'abstractions/base' ),
|
||||
(' #include <abstractions/base> ', 'abstractions/base' ),
|
||||
('include <abstractions/base>', 'abstractions/base' ), # not supported by parser
|
||||
# ('include foo', 'foo' ), # XXX not supported in tools yet
|
||||
# ('include /foo/bar', '/foo/bar' ), # XXX not supported in tools yet
|
||||
# ('include "foo"', 'foo' ), # XXX not supported in tools yet
|
||||
# ('include "/foo/bar"', '/foo/bar' ), # XXX not supported in tools yet
|
||||
(' some #include <abstractions/base>', None, ),
|
||||
(' /etc/fstab r,', None, ),
|
||||
]
|
||||
|
||||
def _run_test(self, params, expected):
|
||||
self.assertEqual(re_match_include(params), expected)
|
||||
|
||||
class TestInvalid_re_match_include(AATest):
|
||||
tests = [
|
||||
('#include <>', AppArmorException ),
|
||||
('#include < >', AppArmorException ),
|
||||
]
|
||||
|
||||
def _run_test(self, params, expected):
|
||||
with self.assertRaises(expected):
|
||||
re_match_include(params)
|
||||
|
||||
|
||||
class TestStripQuotes(AATest):
|
||||
def test_strip_quotes_01(self):
|
||||
|
Reference in New Issue
Block a user