1
# ----------------------------------------------------------------------
2
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of version 2 of the GNU General Public
6
# License as published by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# ----------------------------------------------------------------------
19
sys.path.append('../')
21
import apparmor.severity as severity
22
from apparmor.common import AppArmorException
24
class Test(unittest.TestCase):
27
#copy the local profiles to the test directory
28
if os.path.exists('./profiles'):
29
shutil.rmtree('./profiles')
30
shutil.copytree('/etc/apparmor.d/', './profiles/', symlinks=True)
33
#Wipe the local profiles from the test directory
34
shutil.rmtree('./profiles')
36
def testRank_Test(self):
37
sev_db = severity.Severity('severity.db')
38
rank = sev_db.rank('/usr/bin/whatis', 'x')
39
self.assertEqual(rank, 5, 'Wrong rank')
40
rank = sev_db.rank('/etc', 'x')
41
self.assertEqual(rank, 10, 'Wrong rank')
42
rank = sev_db.rank('/dev/doublehit', 'x')
43
self.assertEqual(rank, 0, 'Wrong rank')
44
rank = sev_db.rank('/dev/doublehit', 'rx')
45
self.assertEqual(rank, 4, 'Wrong rank')
46
rank = sev_db.rank('/dev/doublehit', 'rwx')
47
self.assertEqual(rank, 8, 'Wrong rank')
48
rank = sev_db.rank('/dev/tty10', 'rwx')
49
self.assertEqual(rank, 9, 'Wrong rank')
50
rank = sev_db.rank('/var/adm/foo/**', 'rx')
51
self.assertEqual(rank, 3, 'Wrong rank')
52
rank = sev_db.rank('CAP_KILL')
53
self.assertEqual(rank, 8, 'Wrong rank')
54
rank = sev_db.rank('CAP_SETPCAP')
55
self.assertEqual(rank, 9, 'Wrong rank')
56
self.assertEqual(sev_db.rank('/etc/apparmor/**', 'r') , 6, 'Invalid Rank')
57
self.assertEqual(sev_db.rank('/etc/**', 'r') , 10, 'Invalid Rank')
59
# Load all variables for /sbin/klogd and test them
60
sev_db.load_variables('profiles/sbin.klogd')
61
self.assertEqual(sev_db.rank('@{PROC}/sys/vm/overcommit_memory', 'r'), 6, 'Invalid Rank')
62
self.assertEqual(sev_db.rank('@{HOME}/sys/@{PROC}/overcommit_memory', 'r'), 10, 'Invalid Rank')
63
self.assertEqual(sev_db.rank('/overco@{multiarch}mmit_memory', 'r'), 10, 'Invalid Rank')
65
sev_db.unload_variables()
67
sev_db.load_variables('profiles/usr.sbin.dnsmasq')
68
self.assertEqual(sev_db.rank('@{PROC}/sys/@{TFTP_DIR}/overcommit_memory', 'r'), 6, 'Invalid Rank')
69
self.assertEqual(sev_db.rank('@{PROC}/sys/vm/overcommit_memory', 'r'), 6, 'Invalid Rank')
70
self.assertEqual(sev_db.rank('@{HOME}/sys/@{PROC}/overcommit_memory', 'r'), 10, 'Invalid Rank')
71
self.assertEqual(sev_db.rank('/overco@{multiarch}mmit_memory', 'r'), 10, 'Invalid Rank')
73
#self.assertEqual(sev_db.rank('/proc/@{PID}/maps', 'rw'), 9, 'Invalid Rank')
75
def testInvalid(self):
76
sev_db = severity.Severity('severity.db')
77
rank = sev_db.rank('/dev/doublehit', 'i')
78
self.assertEqual(rank, 10, 'Wrong')
80
severity.Severity('severity_broken.db')
81
except AppArmorException:
83
rank = sev_db.rank('CAP_UNKOWN')
84
rank = sev_db.rank('CAP_K*')
88
if __name__ == "__main__":
89
#import sys;sys.argv = ['', 'Test.testName']