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.


[ 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'))


Regards,

Christian Boltz
-- 
> Und fuer die Jahre-Hiersein finde ich die zwei Ergebnisse
> (unechte Mini-FAQ und Etikette) recht duenn!!!!!!!!
Ich glaub es hackt. Du kannst ja das Geld zurück verlangen, wenn es Dir
nicht paßt.       [> toRBEN pOLLmann und Bernd Brodesser in suse-linux]

Attachment: signature.asc
Description: This is a digitally signed message part.

-- 
AppArmor mailing list
AppArmor@lists.ubuntu.com
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/apparmor

Reply via email to