[apparmor] [patch] [2/9] Add strip_parenthesis() to regex.py
Seth Arnold
seth.arnold at canonical.com
Fri May 20 22:58:01 UTC 2016
On Sun, Dec 27, 2015 at 04:04:07PM +0100, Christian Boltz wrote:
> Hello,
>
> some dbus rule conditionals come with optional parenthesis. Instead of
> making the regex even more complicated, use a small function to strip
> those parenthesis.
>
> Also add some tests for strip_parenthesis() to test-regex.py.
How will this handle '() foo ()' or '(((())))'?
Thanks
>
>
> [ 53-add-strip_parenthesis.diff ]
>
> === modified file ./utils/apparmor/regex.py
> --- utils/apparmor/regex.py 2015-12-21 00:13:57.207799592 +0100
> +++ utils/apparmor/regex.py 2015-12-24 01:19:47.916978461 +0100
> @@ -128,6 +128,15 @@
>
> return matches.group('magicpath')
>
> +def strip_parenthesis(data):
> + '''strips parenthesis from the given string and returns the strip()ped result.
> + The parenthesis must be the first and last char, otherwise they won't be removed.
> + Even if no parenthesis get removed, the result will be strip()ped.
> + '''
> + if data[0] + data[-1] == '()':
> + return data[1:-1].strip()
> + else:
> + return data.strip()
>
> def strip_quotes(data):
> if data[0] + data[-1] == '""':
> === modified file ./utils/test/test-regex_matches.py
> --- utils/test/test-regex_matches.py 2015-12-21 00:13:57.207799592 +0100
> +++ utils/test/test-regex_matches.py 2015-12-24 00:56:50.382751188 +0100
> @@ -14,7 +14,7 @@
> from common_test import AATest, setup_all_loops
> from apparmor.common import AppArmorBug, AppArmorException
>
> -from apparmor.regex import strip_quotes, parse_profile_start_line, re_match_include, RE_PROFILE_START, RE_PROFILE_CAP, RE_PROFILE_PTRACE, RE_PROFILE_SIGNAL
> +from apparmor.regex import strip_parenthesis, strip_quotes, parse_profile_start_line, re_match_include, RE_PROFILE_START, RE_PROFILE_CAP, RE_PROFILE_PTRACE, RE_PROFILE_SIGNAL
>
>
> class AARegexTest(AATest):
> @@ -501,6 +501,24 @@
> re_match_include(params)
>
>
> +class TestStripParenthesis(AATest):
> + tests = [
> + ('foo', 'foo' ),
> + ('(foo)', 'foo' ),
> + ('( foo )', 'foo' ),
> + ('(foo', '(foo' ),
> + ('foo )', 'foo )' ),
> + ('foo ()', 'foo ()' ),
> + ('()', '' ),
> + ('( )', '' ),
> + ('(())', '()' ),
> + (' (foo)', '(foo)' ), # parenthesis not first char, whitespace stripped nevertheless
> + ('(foo) ', '(foo)' ), # parenthesis not last char, whitespace stripped nevertheless
> + ]
> +
> + def _run_test(self, params, expected):
> + self.assertEqual(strip_parenthesis(params), expected)
> +
> class TestStripQuotes(AATest):
> def test_strip_quotes_01(self):
> self.assertEqual('foo', strip_quotes('foo'))
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20160520/e367eaad/attachment.pgp>
More information about the AppArmor
mailing list