1import unittest, os, shutil
2from tempfile import mkdtemp
3from subprocess import Popen, PIPE
4
5class SepolicyTests(unittest.TestCase):
6    def assertDenied(self, err):
7        self.assert_('Permission denied' in err,
8                     '"Permission denied" not found in %r' % err)
9    def assertNotFound(self, err):
10        self.assert_('not found' in err,
11                     '"not found" not found in %r' % err)
12
13    def assertFailure(self, status):
14        self.assert_(status != 0,
15                     '"Succeeded when it should have failed')
16
17    def assertSuccess(self, status, err):
18        self.assert_(status == 0,
19                     '"sepolicy should have succeeded for this test %r' %  err)
20
21    def test_man_domain(self):
22        "Verify sepolicy manpage -d works"
23        p = Popen(['sepolicy', 'manpage', '-d', 'httpd_t'], stdout = PIPE)
24        out, err = p.communicate()
25        print out, err
26        self.assertSuccess(p.returncode, err)
27
28    def test_man_all(self):
29        "Verify sepolicy manpage -a works"
30        p = Popen(['sepolicy', 'manpage', '-a'], stdout = PIPE)
31        out, err = p.communicate()
32        self.assertSuccess(p.returncode, err)
33
34    def test_network_l(self):
35        "Verify sepolicy network -l works"
36        p = Popen(['sepolicy', 'network', '-l'], stdout = PIPE)
37        out, err = p.communicate()
38        self.assertSuccess(p.returncode, err)
39
40    def test_network_t(self):
41        "Verify sepolicy network -t works"
42        p = Popen(['sepolicy', 'network', '-t', 'http_port_t'], stdout = PIPE)
43        out, err = p.communicate()
44        self.assertSuccess(p.returncode, err)
45
46    def test_network_p(self):
47        "Verify sepolicy network -p works"
48        p = Popen(['sepolicy', 'network', '-p', '80'], stdout = PIPE)
49        out, err = p.communicate()
50        self.assertSuccess(p.returncode, err)
51
52    def test_network_d(self):
53        "Verify sepolicy network -d works"
54        p = Popen(['sepolicy', 'network', '-d', 'httpd_t'], stdout = PIPE)
55        out, err = p.communicate()
56        self.assertSuccess(p.returncode, err)
57
58    def test_transition_s(self):
59        "Verify sepolicy transition -l works"
60        p = Popen(['sepolicy', 'transition', '-s', 'httpd_t'], stdout = PIPE)
61        out, err = p.communicate()
62        self.assertSuccess(p.returncode, err)
63
64    def test_transition_t(self):
65        "Verify sepolicy transition -t works"
66        p = Popen(['sepolicy', 'transition', '-s', 'httpd_t', '-t', 'sendmail_t'], stdout = PIPE)
67        out, err = p.communicate()
68        self.assertSuccess(p.returncode, err)
69
70    def test_booleans_a(self):
71        "Verify sepolicy booleans -a works"
72        p = Popen(['sepolicy', 'booleans', '-a'], stdout = PIPE)
73        out, err = p.communicate()
74        self.assertSuccess(p.returncode, err)
75
76    def test_booleans_b_alias(self):
77        "Verify sepolicy booleans -b works"
78        p = Popen(['sepolicy', 'booleans', '-b', 'allow_ypbind'], stdout = PIPE)
79        out, err = p.communicate()
80        self.assertSuccess(p.returncode, err)
81
82    def test_booleans_b(self):
83        "Verify sepolicy booleans -b works"
84        p = Popen(['sepolicy', 'booleans', '-b', 'nis_enabled'], stdout = PIPE)
85        out, err = p.communicate()
86        self.assertSuccess(p.returncode, err)
87
88    def test_interface_l(self):
89        "Verify sepolicy interface -l works"
90        p = Popen(['sepolicy', 'interface', '-l'], stdout = PIPE)
91        out, err = p.communicate()
92        self.assertSuccess(p.returncode, err)
93
94    def test_interface_a(self):
95        "Verify sepolicy interface -a works"
96        p = Popen(['sepolicy', 'interface', '-a'], stdout = PIPE)
97        out, err = p.communicate()
98        self.assertSuccess(p.returncode, err)
99
100    def test_interface_p(self):
101        "Verify sepolicy interface -u works"
102        p = Popen(['sepolicy', 'interface', '-u'], stdout = PIPE)
103        out, err = p.communicate()
104        self.assertSuccess(p.returncode, err)
105
106    def test_interface_ci(self):
107        "Verify sepolicy interface -c -i works"
108        p = Popen(['sepolicy', 'interface', '-c', '-i', 'apache_admin'], stdout = PIPE)
109        out, err = p.communicate()
110        self.assertSuccess(p.returncode, err)
111
112if __name__ == "__main__":
113    import selinux
114    if selinux.security_getenforce() == 1:
115        unittest.main()
116    else:
117        print "SELinux must be in enforcing mode for this test"
118