From 373e8e23b13c9ff941939e7dd11042213149e0bb Mon Sep 17 00:00:00 2001 From: Christian Boltz <apparmor@cboltz.de> Date: Sun, 24 May 2020 13:33:12 +0200 Subject: [PATCH] Fix strip_quotes() to handle empty strings strip_quotes() assumed its parameter is at least one character long, and errored out on an empty string. It also converted a string consisting of a single quote to an empty string because that single quote had a quote as first and last char. This commit fixes these two bugs. Also rewrite TestStripQuotes to use tests[], and add some test for an empty string, a one-char path (just a slash) and a single quote. --- utils/apparmor/regex.py | 2 +- utils/test/test-regex_matches.py | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/utils/apparmor/regex.py b/utils/apparmor/regex.py index 7f313b7fe..2c65614de 100644 --- a/utils/apparmor/regex.py +++ b/utils/apparmor/regex.py @@ -219,7 +219,7 @@ def strip_parenthesis(data): return data.strip() def strip_quotes(data): - if data[0] + data[-1] == '""': + if len(data) > 1 and data[0] + data[-1] == '""': return data[1:-1] else: return data diff --git a/utils/test/test-regex_matches.py b/utils/test/test-regex_matches.py index 3530783f5..b0503b653 100644 --- a/utils/test/test-regex_matches.py +++ b/utils/test/test-regex_matches.py @@ -628,23 +628,22 @@ class TestStripParenthesis(AATest): self.assertEqual(strip_parenthesis(params), expected) class TestStripQuotes(AATest): - 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""""')) + tests = [ + ('foo', 'foo'), + ('"foo"', 'foo'), + ('"foo', '"foo'), + ('foo"', 'foo"'), + ('""', ''), + ('foo"bar', 'foo"bar'), + ('"foo"bar"', 'foo"bar'), + ('""""foo"bar""""', '"""foo"bar"""'), + ('', ''), + ('/', '/'), + ('"', '"'), + ] + def _run_test(self, params, expected): + self.assertEqual(strip_quotes(params), expected) setup_aa(aa) -- GitLab