getfscreatecon( raw), setfscreatecon(_raw); --- tests/test_fileCreation_labeling.py | 129 ++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100755 tests/test_fileCreation_labeling.py diff --git a/tests/test_fileCreation_labeling.py b/tests/test_fileCreation_labeling.py new file mode 100755 index 0000000..276b307 --- /dev/null +++ b/tests/test_fileCreation_labeling.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python + +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +#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. +# +#For more information see <http://www.gnu.org/licenses/> + +"""Tests for: +getfscreatecon(_raw), setfscreatecon(_con) +""" + +import helper +import selinux +import unittest +import sys + + +class auxiliaryTestCase(unittest.TestCase): + """Auxiliary class. + """ + + def __init__(self, test_method="runTest"): + unittest.TestCase.__init__(self, test_method) + self.raw_con = "unconfined_u:unconfined_r:unconfined_t:s0" + self.trans_con = "unconfined_u:unconfined_r:unconfined_t:SystemLow" + self.wrong_con = "WRONG CONTEXT" + + def tearDown(self): + """Clears the fscreate file after every test + """ + selinux.setfscreatecon_raw(None) + + def read_fscreate(self, pid="self"): + """Reads selinux context from /proc/self/attr/fscreate file. + + Args: + pid: PID of process whose /proc/.../fscreate file should be read. + + Returns: + None if /proc/.../fscreate contains "". + Fscreate selinux context otherwise. + """ + return helper.read_attr_file(pid, "fscreate") + + +class getfscreateconRawTestCase(auxiliaryTestCase): + """TestCase for getfscreatecon_raw() funciton. + """ + + def test_getfscreateconRaw_FscreateIsEmpty_ReturnedNone(self): + #Make sure file is empty + selinux.setfscreatecon_raw(None) + self.assertEqual(self.read_fscreate(), None, "Gathered context from " + "/proc/.../fscreate file should be None!") + + def test_getfscreateconRaw_RawConInFscreate_ReturnsRawCon(self): + #Put raw_con into fscreate file first + selinux.setfscreatecon_raw(self.raw_con) + self.assertEqual(selinux.getfscreatecon_raw()[1], self.raw_con, "raw_con was " + "not returned as expected") + + +class setfscreateconRawTestCase(auxiliaryTestCase): + """TestCase for setfscreatecon_raw() funciton. + """ + + def test_setfscreateconRaw_InsertRawCon_InsertedSuccessfully(self): + selinux.setfscreatecon_raw(self.raw_con) + self.assertEqual(self.read_fscreate(), self.raw_con, "raw_con was " + "not put into /proc/.../fscreate file!") + + def test_setfscreateconRaw_RawConRemoval_RawConRemoved(self): + selinux.setfscreatecon_raw(self.raw_con) + selinux.setfscreatecon_raw(None) + self.assertEqual(self.read_fscreate(), None, "Removal " + "of raw_con has failed! setfscreatecon_raw(None) " + "did not work!") + + def test_setfscreateconRaw_WrongContextUsed_OSErrorRaised(self): + self.assertRaisesRegexp(OSError, '\[Errno 22\]', selinux.setfscreatecon_raw, + self.wrong_con) + + +class getfscreateconTestCase(auxiliaryTestCase): + """TestCase for getfscreatecon() function. + """ + + @unittest.skipIf(helper.contextTranslation(), "Context-trans active!") + def test_getfscreatecon_RawContextInFscreate_ReturnsRawContext(self): + selinux.setfscreatecon_raw(self.raw_con) + self.assertEqual(selinux.getfscreatecon()[1], self.raw_con, "raw_con was " + "not returned as expected!") + + @unittest.skipUnless(helper.contextTranslation(), "Context-trans inactive!") + def test_getfscreatecon_RawContextInFscreate_ReturnsTransContext(self): + selinux.setfscreatecon_raw(self.raw_con) + self.assertEqual(selinux.getfscreatecon()[1], self.trans_con, "trans_con was " + "not returned as expected!") + + +class setfscreateconTestCase(auxiliaryTestCase): + """TestCase for setfscreatecon() funciton. + """ + + @unittest.skipUnless(helper.contextTranslation(), "Context-trans inactive!") + def test_setfscreatecon_InsertTransCon_InsertedSuccessfully(self): + selinux.setfscreatecon(self.trans_con) + self.assertEqual(self.read_fscreate(), self.raw_con, "trans_con was " + "not put into /proc/.../fscreate file!") + + def test_setfscreatecon_InsertRawCon_InsertedSuccessfully(self): + selinux.setfscreatecon(self.raw_con) + self.assertEqual(self.read_fscreate(), self.raw_con, "raw_con was " + "not put into /proc/.../fscreate file!") + + def test_setfscreateconRaw_WrongContextUsed_OSErrorRaised(self): + self.assertRaisesRegexp(OSError, '\[Errno 22\]', selinux.setfscreatecon, + self.wrong_con) + + +if __name__ == "__main__": + suite = unittest.TestLoader().loadTestsFromModule(sys.modules[auxiliaryTestCase.__module__]) + unittest.TextTestRunner(verbosity=2).run(suite) -- 1.9.0 -- selinux mailing list selinux@xxxxxxxxxxxxxxxxxxxxxxx https://admin.fedoraproject.org/mailman/listinfo/selinux