2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-09-04 08:15:21 +00:00

move tests for convert_regexp() to (new) test-aare.py

The tests for convert_regexp() were hidden in common_test.py, where they
were never executed.

This patch moves them to the new file test-aare.py and also converts the
regex_tests.ini to a tests[] array to have the test data inside the test
file. (All tests from regex_tests.ini are in test-aare.py, and two tests
with prepended and appended path segments were added.)

Also add some tests that check the raw behaviour of convert_regexp() -
the tests "by example" are probably more useful and for sure more
readable ;-) but I want to have some examples of the converted regexes
available.


Acked-by <timeout>
This commit is contained in:
Christian Boltz
2015-10-11 20:19:35 +02:00
parent 807c2dccf0
commit ea9f9aeff2
3 changed files with 110 additions and 96 deletions

View File

@@ -15,25 +15,10 @@
import unittest import unittest
import inspect import inspect
import os import os
import re
import shutil import shutil
import sys import sys
import tempfile import tempfile
import apparmor.common
import apparmor.config
class Test(unittest.TestCase):
def test_RegexParser(self):
tests = apparmor.config.Config('ini')
tests.CONF_DIR = '.'
regex_tests = tests.read_config('regex_tests.ini')
for regex in regex_tests.sections():
parsed_regex = re.compile(apparmor.common.convert_regexp(regex))
for regex_testcase in regex_tests.options(regex):
self.assertEqual(bool(parsed_regex.search(regex_testcase)), eval(regex_tests[regex][regex_testcase]), 'Incorrectly Parsed regex: %s' %regex)
#def test_readkey(self): #def test_readkey(self):
# print("Please press the Y button on the keyboard.") # print("Please press the Y button on the keyboard.")

View File

@@ -1,81 +0,0 @@
# ----------------------------------------------------------------------
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# ----------------------------------------------------------------------
[/foo/**/bar/]
/foo/user/tools/bar/ = True
/foo/apparmor/bar/ = True
/foo/apparmor/bar = False
[/foo/*/bar/]
/foo/apparmor/bar/ = True
/foo/apparmor/tools/bar/ = False
/foo/apparmor/bar = False
[/foo/{foo,bar,user,other}/bar/]
/foo/user/bar/ = True
/foo/bar/bar/ = True
/foo/wrong/bar/ = False
[/foo/{foo,bar,user,other}/test,ca}se/{aa,sd,nd}/bar/]
/foo/user/test,ca}se/aa/bar/ = True
/foo/bar/test,ca}se/sd/bar/ = True
/foo/wrong/user/bar/ = False
/foo/user/wrong/bar/ = False
/foo/wrong/aa/bar/ = False
[/foo/user/ba?/]
/foo/user/bar/ = True
/foo/user/bar/apparmor/ = False
/foo/user/ba/ = False
/foo/user/ba// = False
[/foo/user/bar/**]
/foo/user/bar/apparmor = True
/foo/user/bar/apparmor/tools = True
/foo/user/bar/ = False
[/foo/user/bar/*]
/foo/user/bar/apparmor = True
/foo/user/bar/apparmor/tools = False
/foo/user/bar/ = False
/foo/user/bar/apparmor/ = False
[/foo/**.jpg]
/foo/bar/baz/foobar.jpg = True
/foo/bar/foobar.jpg = True
/foo/bar/*.jpg = True
/foo/bar.jpg = True
/foo/barjpg = False
/foo/.* = False
/foo/**.jpg = True
/foo/*.jpg = True
/bar.jpg = False
/**.jpg = False
/*.jpg = False
/foo/*.bar = False
[/foo/{**,}]
/foo/ = True
/foo/bar = True
/foo/bar/ = True
/foo/bar/baz = True
/foo/bar/baz/ = True
/bar/ = False
[/foo/{,**}]
/foo/ = True
/foo/bar = True
/foo/bar/ = True
/foo/bar/baz = True
/foo/bar/baz/ = True
/bar/ = False

110
utils/test/test-aare.py Normal file
View File

@@ -0,0 +1,110 @@
#! /usr/bin/env python
# ------------------------------------------------------------------
#
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
# Copyright (C) 2015 Christian Boltz <apparmor@cboltz.de>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License published by the Free Software Foundation.
#
# ------------------------------------------------------------------
import unittest
from common_test import AATest, setup_all_loops
import re
from apparmor.common import convert_regexp
class TestConvert_regexp(AATest):
tests = [
('/foo', '^/foo$'),
('/{foo,bar}', '^/(foo|bar)$'),
# ('/\{foo,bar}', '^/\{foo,bar}$'), # XXX gets converted to ^/\(foo|bar)$
('/fo[abc]', '^/fo[abc]$'),
('/foo bar', '^/foo bar$'),
('/x\y', '^/x\y$'),
('/x\[y', '^/x\[y$'),
('/x\\y', '^/x\\y$'),
('/fo?', '^/fo[^/\000]$'),
('/foo/*', '^/foo/(((?<=/)[^/\000]+)|((?<!/)[^/\000]*))$'),
('/foo/**.bar', '^/foo/(((?<=/)[^\000]+)|((?<!/)[^\000]*))\.bar$'),
]
def _run_test(self, params, expected):
self.assertEqual(convert_regexp(params), expected)
class TestExamplesConvert_regexp(AATest):
tests = [
# aare path to check match expected?
(['/foo/**/bar/', '/foo/user/tools/bar/' ], True),
(['/foo/**/bar/', '/foo/apparmor/bar/' ], True),
(['/foo/**/bar/', '/foo/apparmor/bar' ], False),
(['/foo/**/bar/', '/a/foo/apparmor/bar/' ], False),
(['/foo/**/bar/', '/foo/apparmor/bar/baz' ], False),
(['/foo/*/bar/', '/foo/apparmor/bar/' ], True),
(['/foo/*/bar/', '/foo/apparmor/tools/bar/' ], False),
(['/foo/*/bar/', '/foo/apparmor/bar' ], False),
(['/foo/user/ba?/', '/foo/user/bar/' ], True),
(['/foo/user/ba?/', '/foo/user/bar/apparmor/' ], False),
(['/foo/user/ba?/', '/foo/user/ba/' ], False),
(['/foo/user/ba?/', '/foo/user/ba//' ], False),
(['/foo/user/bar/**', '/foo/user/bar/apparmor' ], True),
(['/foo/user/bar/**', '/foo/user/bar/apparmor/tools' ], True),
(['/foo/user/bar/**', '/foo/user/bar/' ], False),
(['/foo/user/bar/*', '/foo/user/bar/apparmor' ], True),
(['/foo/user/bar/*', '/foo/user/bar/apparmor/tools' ], False),
(['/foo/user/bar/*', '/foo/user/bar/' ], False),
(['/foo/user/bar/*', '/foo/user/bar/apparmor/' ], False),
(['/foo/**.jpg', '/foo/bar/baz/foobar.jpg' ], True),
(['/foo/**.jpg', '/foo/bar/foobar.jpg' ], True),
(['/foo/**.jpg', '/foo/bar/*.jpg' ], True),
(['/foo/**.jpg', '/foo/bar.jpg' ], True),
(['/foo/**.jpg', '/foo/**.jpg' ], True),
(['/foo/**.jpg', '/foo/*.jpg' ], True),
(['/foo/**.jpg', '/foo/barjpg' ], False),
(['/foo/**.jpg', '/foo/.*' ], False),
(['/foo/**.jpg', '/bar.jpg' ], False),
(['/foo/**.jpg', '/**.jpg' ], False),
(['/foo/**.jpg', '/*.jpg' ], False),
(['/foo/**.jpg', '/foo/*.bar' ], False),
(['/foo/{**,}', '/foo/' ], True),
(['/foo/{**,}', '/foo/bar' ], True),
(['/foo/{**,}', '/foo/bar/' ], True),
(['/foo/{**,}', '/foo/bar/baz' ], True),
(['/foo/{**,}', '/foo/bar/baz/' ], True),
(['/foo/{**,}', '/bar/' ], False),
(['/foo/{,**}', '/foo/' ], True),
(['/foo/{,**}', '/foo/bar' ], True),
(['/foo/{,**}', '/foo/bar/' ], True),
(['/foo/{,**}', '/foo/bar/baz' ], True),
(['/foo/{,**}', '/foo/bar/baz/' ], True),
(['/foo/{,**}', '/bar/' ], False),
(['/foo/{foo,bar,user,other}/bar/', '/foo/user/bar/' ], True),
(['/foo/{foo,bar,user,other}/bar/', '/foo/bar/bar/' ], True),
(['/foo/{foo,bar,user,other}/bar/', '/foo/wrong/bar/' ], False),
(['/foo/{foo,bar,user,other}/test,ca}se/{aa,sd,nd}/bar/', '/foo/user/test,ca}se/aa/bar/' ], True),
(['/foo/{foo,bar,user,other}/test,ca}se/{aa,sd,nd}/bar/', '/foo/bar/test,ca}se/sd/bar/' ], True),
(['/foo/{foo,bar,user,other}/test,ca}se/{aa,sd,nd}/bar/', '/foo/wrong/user/bar/' ], False),
(['/foo/{foo,bar,user,other}/test,ca}se/{aa,sd,nd}/bar/', '/foo/user/wrong/bar/' ], False),
(['/foo/{foo,bar,user,other}/test,ca}se/{aa,sd,nd}/bar/', '/foo/wrong/aa/bar/' ], False),
]
def _run_test(self, params, expected):
regex, path = params
parsed_regex = re.compile(convert_regexp(regex))
self.assertEqual(bool(parsed_regex.search(path)), expected, 'Incorrectly Parsed regex: %s' %regex)
setup_all_loops(__name__)
if __name__ == '__main__':
unittest.main(verbosity=2)