12
12
import apparmor.aa as aa
14
14
from common_test import AATest, setup_all_loops
15
from apparmor.common import AppArmorBug
15
from apparmor.common import AppArmorBug, AppArmorException
17
from apparmor.regex import strip_quotes, parse_profile_start_line, RE_PROFILE_START
17
from apparmor.regex import strip_quotes, parse_profile_start_line, re_match_include, RE_PROFILE_START, RE_PROFILE_CAP
20
20
class AARegexTest(AATest):
203
203
class AARegexCapability(AARegexTest):
204
204
'''Tests for RE_PROFILE_CAP'''
207
self.regex = aa.RE_PROFILE_CAP
207
self.regex = RE_PROFILE_CAP
210
210
(' capability net_raw,', (None, None, 'net_raw', 'net_raw', None)),
403
403
('/bin/foo /bin/bar', False), # missing 'profile' keyword
404
404
('profile {', False), # no attachment
405
405
(' profile foo bar /foo {', False), # missing quotes around "foo bar"
406
('bin/foo {', False), # not starting with '/'
407
('"bin/foo" {', False), # not starting with '/', quoted version
407
409
(' /foo {', { 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': None, 'comment': None }),
408
410
(' "/foo" {', { 'plainprofile': '"/foo"', 'namedprofile': None, 'attachment': None, 'flags': None, 'comment': None }),
415
417
(' /foo (complain) {', { 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': 'complain', 'comment': None }),
416
418
(' /foo flags=(complain) {', { 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': 'complain', 'comment': None }),
417
419
(' /foo (complain) { # x', { 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': 'complain', 'comment': '# x'}),
420
(' /foo flags = ( complain ){#',{ 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': ' complain ', 'comment': '#'}),
419
422
(' /foo {', { 'plainprofile': '/foo', 'namedprofile': None, 'leadingspace': ' ' }),
420
423
('/foo {', { 'plainprofile': '/foo', 'namedprofile': None, 'leadingspace': '' }),
435
438
(' profile "foo bar" /foo {', { 'profile': 'foo bar', 'profile_keyword': True, 'plainprofile': None, 'namedprofile': 'foo bar','attachment': '/foo', 'flags': None, 'comment': None }),
436
439
(' /foo (complain) {', { 'profile': '/foo', 'profile_keyword': False, 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': 'complain', 'comment': None }),
437
440
(' /foo flags=(complain) {', { 'profile': '/foo', 'profile_keyword': False, 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': 'complain', 'comment': None }),
441
(' /foo flags = ( complain ){', { 'profile': '/foo', 'profile_keyword': False, 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': ' complain ', 'comment': None }),
438
442
(' /foo (complain) { # x', { 'profile': '/foo', 'profile_keyword': False, 'plainprofile': '/foo', 'namedprofile': None, 'attachment': None, 'flags': 'complain', 'comment': '# x'}),
440
444
(' /foo {', { 'profile': '/foo', 'plainprofile': '/foo', 'namedprofile': None, 'leadingspace': ' ' }),
463
467
with self.assertRaises(AppArmorBug):
464
468
parse_profile_start_line(line, 'somefile')
470
class Test_re_match_include(AATest):
472
('#include <abstractions/base>', 'abstractions/base' ),
473
('#include <abstractions/base> # comment', 'abstractions/base' ),
474
('#include<abstractions/base>#comment', 'abstractions/base' ),
475
(' #include <abstractions/base> ', 'abstractions/base' ),
476
('include <abstractions/base>', 'abstractions/base' ), # not supported by parser
477
# ('include foo', 'foo' ), # XXX not supported in tools yet
478
# ('include /foo/bar', '/foo/bar' ), # XXX not supported in tools yet
479
# ('include "foo"', 'foo' ), # XXX not supported in tools yet
480
# ('include "/foo/bar"', '/foo/bar' ), # XXX not supported in tools yet
481
(' some #include <abstractions/base>', None, ),
482
(' /etc/fstab r,', None, ),
485
def _run_test(self, params, expected):
486
self.assertEqual(re_match_include(params), expected)
488
class TestInvalid_re_match_include(AATest):
490
('#include <>', AppArmorException ),
491
('#include < >', AppArmorException ),
494
def _run_test(self, params, expected):
495
with self.assertRaises(expected):
496
re_match_include(params)
467
499
class TestStripQuotes(AATest):
468
500
def test_strip_quotes_01(self):