<div dir="ltr">+mailing list<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 22, 2016 at 1:07 AM, Kshitij Gupta <span dir="ltr"><<a href="mailto:kgupta8592@gmail.com" target="_blank">kgupta8592@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello,<br><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Sun, Dec 27, 2015 at 8:34 PM, Christian Boltz <span dir="ltr"><<a href="mailto:apparmor@cboltz.de" target="_blank">apparmor@cboltz.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
some dbus rule conditionals come with optional parenthesis. Instead of<br>
making the regex even more complicated, use a small function to strip<br>
those parenthesis.<br>
<br>
Also add some tests for strip_parenthesis() to test-regex.py.<br>
<br>
<br>
[ 53-add-strip_parenthesis.diff ]<br>
<br>
=== modified file ./utils/apparmor/regex.py<br>
--- utils/apparmor/regex.py     2015-12-21 00:13:57.207799592 +0100<br>
+++ utils/apparmor/regex.py     2015-12-24 01:19:47.916978461 +0100<br>
@@ -128,6 +128,15 @@<br>
<br>
     return matches.group('magicpath')<br>
<br>
+def strip_parenthesis(data):<br>
+    '''strips parenthesis from the given string and returns the strip()ped result.<br>
+       The parenthesis must be the first and last char, otherwise they won't be removed.<br>
+       Even if no parenthesis get removed, the result will be strip()ped.<br>
+       '''<br>
+    if data[0] + data[-1] == '()':<br>
+        return data[1:-1].strip()<br>
+    else:<br>
+        return data.strip()<br></blockquote></span><div>Might be useful to do the data.strip() before the "data[0] + data[-1] == '()'" check, that way you don't miss on something like '(hey) '.<br> </div><div>This is because there doesn't seem to be a strong assumption that the data will already be stripped coz we return data.strip()<br></div><div>Also, I don't see any harm in having the .strip before the check.<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
<br>
 def strip_quotes(data):<br>
     if data[0] + data[-1] == '""':<br>
=== modified file ./utils/test/test-regex_matches.py<br>
--- utils/test/test-regex_matches.py    2015-12-21 00:13:57.207799592 +0100<br>
+++ utils/test/test-regex_matches.py    2015-12-24 00:56:50.382751188 +0100<br>
@@ -14,7 +14,7 @@<br>
 from common_test import AATest, setup_all_loops<br>
 from apparmor.common import AppArmorBug, AppArmorException<br>
<br>
-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<br>
+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<br>
<br>
<br>
 class AARegexTest(AATest):<br>
@@ -501,6 +501,24 @@<br>
             re_match_include(params)<br>
<br>
<br>
+class TestStripParenthesis(AATest):<br>
+    tests = [<br>
+        ('foo',         'foo'       ),<br>
+        ('(foo)',       'foo'       ),<br>
+        ('(  foo )',    'foo'       ),<br>
+        ('(foo',        '(foo'      ),<br>
+        ('foo  )',      'foo  )'    ),<br>
+        ('foo ()',      'foo ()'    ),<br>
+        ('()',          ''          ),<br>
+        ('(  )',        ''          ),<br>
+        ('(())',        '()'        ),<br>
+        (' (foo)',       '(foo)'    ),  # parenthesis not first char, whitespace stripped nevertheless<br>
+        ('(foo) ',       '(foo)'    ),  # parenthesis not last char, whitespace stripped nevertheless<br>
+    ]<br>
+<br>
+    def _run_test(self, params, expected):<br>
+        self.assertEqual(strip_parenthesis(params), expected)<br>
+<br>
 class TestStripQuotes(AATest):<br>
     def test_strip_quotes_01(self):<br>
         self.assertEqual('foo', strip_quotes('foo'))<br>
<br>
<br>
Regards,<br>
<br>
Christian Boltz<br>
<span><font color="#888888">--<br>
> Und fuer die Jahre-Hiersein finde ich die zwei Ergebnisse<br>
> (unechte Mini-FAQ und Etikette) recht duenn!!!!!!!!<br>
Ich glaub es hackt. Du kannst ja das Geld zurück verlangen, wenn es Dir<br>
nicht paßt.       [> toRBEN pOLLmann und Bernd Brodesser in suse-linux]<br>
</font></span><br></div></div><span class="HOEnZb"><font color="#888888">--<br>
AppArmor mailing list<br>
<a href="mailto:AppArmor@lists.ubuntu.com" target="_blank">AppArmor@lists.ubuntu.com</a><br>
Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/apparmor" rel="noreferrer" target="_blank">https://lists.ubuntu.com/mailman/listinfo/apparmor</a><br>
<br></font></span></blockquote></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><br>-- <br><div><div dir="ltr"><div>Regards,<br><br></div>Kshitij Gupta<br></div></div>
</font></span></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div>Regards,<br><br></div>Kshitij Gupta<br></div></div>
</div>