[apparmor] [patch] move strip_quotes from aa.py to regex.py

Christian Boltz apparmor at cboltz.de
Mon Mar 2 23:58:49 UTC 2015


Hello,

the upcoming function parse_profile_start() (which is a wrapper around 
the updated RE_PROFILE_START, and will live in regex.py) needs 
strip_profile(), but importing it from aa.py fails with an import loop.
Therefore this patch moves strip_quotes() from aa.py to regex.py and 
re-imports it into aa.py.

As a bonus, the patch also adds some tests for strip_quotes() ;-)


I propose this patch for trunk and 2.9
(which also explains why I move the function to regex.py - 
rule/__init__.py would be a better place, but makes backporting to 2.9
more difficult)


[ move-strip_quotes.diff ]

=== modified file 'utils/apparmor/aa.py'
--- utils/apparmor/aa.py        2015-03-02 20:12:12 +0000
+++ utils/apparmor/aa.py        2015-03-02 23:11:38 +0000
@@ -48,7 +48,8 @@
                             RE_NETWORK_FAMILY_TYPE, RE_NETWORK_FAMILY, RE_PROFILE_CHANGE_HAT,
                             RE_PROFILE_HAT_DEF, RE_PROFILE_DBUS, RE_PROFILE_MOUNT,
                             RE_PROFILE_SIGNAL, RE_PROFILE_PTRACE, RE_PROFILE_PIVOT_ROOT,
-                            RE_PROFILE_UNIX, RE_RULE_HAS_COMMA, RE_HAS_COMMENT_SPLIT )
+                            RE_PROFILE_UNIX, RE_RULE_HAS_COMMA, RE_HAS_COMMENT_SPLIT,
+                            strip_quotes )
 
 import apparmor.rules as aarules
 
@@ -3277,12 +3286,6 @@
         raise AppArmorException(_('Unknown variable operation %(operation)s for variable %(variable)s in %(file)s') % { 'operation': var_operation, 'variable': list_var, 'file': filename })
 
 
-def strip_quotes(data):
-    if data[0] + data[-1] == '""':
-        return data[1:-1]
-    else:
-        return data
-
 def quote_if_needed(data):
     # quote data if it contains whitespace
     if ' ' in data:

=== modified file 'utils/apparmor/regex.py'
--- utils/apparmor/regex.py     2014-11-11 23:05:04 +0000
+++ utils/apparmor/regex.py     2015-03-02 23:38:48 +0000
@@ -55,3 +56,9 @@
 RE_HAS_COMMENT_SPLIT = re.compile('^(?P<not_comment>' + __re_no_or_quoted_hash + ')' + # store in 'not_comment' group
     '(?P<comment>#.*)$')  # match trailing comment and store in 'comment' group
 
+
+def strip_quotes(data):
+    if data[0] + data[-1] == '""':
+        return data[1:-1]
+    else:
+        return data

=== modified file 'utils/test/test-regex_matches.py'
--- utils/test/test-regex_matches.py    2014-10-01 19:45:22 +0000
+++ utils/test/test-regex_matches.py    2015-03-02 23:37:05 +0000
@@ -12,6 +12,7 @@
 import apparmor.aa as aa
 import unittest
 
+from apparmor.regex import strip_quotes
 
 class AARegexHasComma(unittest.TestCase):
     '''Tests for apparmor.aa.RE_RULE_HAS_COMMA'''
@@ -373,6 +436,26 @@
         ('deny unixlike,', False),
     ]
 
+class TestStripQuotes(unittest.TestCase):
+    def test_strip_quotes_01(self):
+        self.assertEqual('foo', strip_quotes('foo'))
+    def test_strip_quotes_02(self):
+        self.assertEqual('foo', strip_quotes('"foo"'))
+    def test_strip_quotes_03(self):
+        self.assertEqual('"foo', strip_quotes('"foo'))
+    def test_strip_quotes_04(self):
+        self.assertEqual('foo"', strip_quotes('foo"'))
+    def test_strip_quotes_05(self):
+        self.assertEqual('', strip_quotes('""'))
+    def test_strip_quotes_06(self):
+        self.assertEqual('foo"bar', strip_quotes('foo"bar'))
+    def test_strip_quotes_07(self):
+        self.assertEqual('foo"bar', strip_quotes('"foo"bar"'))
+    def test_strip_quotes_08(self):
+        self.assertEqual('"""foo"bar"""', strip_quotes('""""foo"bar""""'))
+
+
+
 if __name__ == '__main__':
     verbosity = 2
 


Regards,

Christian Boltz
-- 
Danke für die sehr verständliche und umfassende Antwort, ich seh' jetzt
sehr viel klarer (Böse Zungen sagen, ich hätte seit zwei Monaten 'mal
wieder meine Brille geputzt ... ).   [Ulrich Walter in suse-linux]




More information about the AppArmor mailing list