Merge 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.

MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/545
Acked-by: John Johansen <john.johansen@canonical.com>
This commit is contained in:
John Johansen 2020-05-26 07:40:56 +00:00
commit 7fa74521fb
2 changed files with 16 additions and 17 deletions

View file

@ -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

View file

@ -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)