1import unittest
2import os
3import os.path
4import sys
5from tempfile import mkdtemp
6from subprocess import Popen, PIPE
7
8
9class Audit2allowTests(unittest.TestCase):
10
11    def assertDenied(self, err):
12        self.assertTrue('Permission denied' in err,
13                        '"Permission denied" not found in %r' % err)
14
15    def assertNotFound(self, err):
16        self.assertTrue('not found' in err,
17                        '"not found" not found in %r' % err)
18
19    def assertFailure(self, status):
20        self.assertTrue(status != 0,
21                        '"Succeeded when it should have failed')
22
23    def assertSuccess(self, cmd, status, err):
24        self.assertTrue(status == 0,
25                        '"%s should have succeeded for this test %r' % (cmd, err))
26
27    def test_sepolgen_ifgen(self):
28        "Verify sepolgen-ifgen works"
29        temp_directory = mkdtemp(suffix='audit2allow_test')
30        output_file = os.path.join(temp_directory, 'interface_info')
31        p = Popen([
32            sys.executable, './sepolgen-ifgen', '-p', 'test_dummy_policy', '-o', output_file,
33            '--attr-helper', './sepolgen-ifgen-attr-helper'
34            ], stdout=PIPE)
35        out, err = p.communicate()
36        if err:
37            print(out, err)
38        self.assertSuccess("sepolgen-ifgen", p.returncode, err)
39        os.unlink(output_file)
40        os.rmdir(temp_directory)
41
42    def test_audit2allow(self):
43        "Verify audit2allow works"
44        p = Popen([sys.executable, './audit2allow', '-p', 'test_dummy_policy', '-i', 'test.log'], stdout=PIPE)
45        out, err = p.communicate()
46        if err:
47            print(out, err)
48        self.assertSuccess("audit2allow", p.returncode, err)
49
50    def test_audit2why(self):
51        "Verify audit2why works"
52        p = Popen([sys.executable, './audit2why', '-p', 'test_dummy_policy', '-i', 'test.log'], stdout=PIPE)
53        out, err = p.communicate()
54        if err:
55            print(out, err)
56        self.assertSuccess("audit2why", p.returncode, err)
57
58    def test_xperms(self):
59        "Verify that xperms generation works"
60        p = Popen([sys.executable, './audit2allow', '-x', '-p', 'test_dummy_policy', '-i', 'test.log'], stdout=PIPE)
61        out, err = p.communicate()
62        if err:
63            print(out, err)
64        self.assertTrue(b"allowxperm" in out)
65        self.assertSuccess("xperms", p.returncode, err)
66
67
68if __name__ == "__main__":
69    unittest.main()
70