diff options
Diffstat (limited to 'Lib/test/test_posix.py')
-rw-r--r-- | Lib/test/test_posix.py | 117 |
1 files changed, 108 insertions, 9 deletions
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 51cc23ce05..45b3afc72d 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -5,16 +5,16 @@ from test import support # Skip these tests if there is no posix module. posix = support.import_module('posix') +import errno import sys import time import os import pwd import shutil +import stat import unittest import warnings -warnings.filterwarnings('ignore', '.* potential security risk .*', - RuntimeWarning) class PosixTester(unittest.TestCase): @@ -22,9 +22,14 @@ class PosixTester(unittest.TestCase): # create empty file fp = open(support.TESTFN, 'w+') fp.close() + self._warnings_manager = support.check_warnings() + self._warnings_manager.__enter__() + warnings.filterwarnings('ignore', '.* potential security risk .*', + RuntimeWarning) def tearDown(self): support.unlink(support.TESTFN) + self._warnings_manager.__exit__(None, None, None) def testNoArgFunctions(self): # test posix functions which take no arguments and have @@ -41,6 +46,69 @@ class PosixTester(unittest.TestCase): posix_func() self.assertRaises(TypeError, posix_func, 1) + if hasattr(posix, 'getresuid'): + def test_getresuid(self): + user_ids = posix.getresuid() + self.assertEqual(len(user_ids), 3) + for val in user_ids: + self.assertGreaterEqual(val, 0) + + if hasattr(posix, 'getresgid'): + def test_getresgid(self): + group_ids = posix.getresgid() + self.assertEqual(len(group_ids), 3) + for val in group_ids: + self.assertGreaterEqual(val, 0) + + if hasattr(posix, 'setresuid'): + def test_setresuid(self): + current_user_ids = posix.getresuid() + self.assertIsNone(posix.setresuid(*current_user_ids)) + # -1 means don't change that value. + self.assertIsNone(posix.setresuid(-1, -1, -1)) + + def test_setresuid_exception(self): + # Don't do this test if someone is silly enough to run us as root. + current_user_ids = posix.getresuid() + if 0 not in current_user_ids: + new_user_ids = (current_user_ids[0]+1, -1, -1) + self.assertRaises(OSError, posix.setresuid, *new_user_ids) + + if hasattr(posix, 'setresgid'): + def test_setresgid(self): + current_group_ids = posix.getresgid() + self.assertIsNone(posix.setresgid(*current_group_ids)) + # -1 means don't change that value. + self.assertIsNone(posix.setresgid(-1, -1, -1)) + + def test_setresgid_exception(self): + # Don't do this test if someone is silly enough to run us as root. + current_group_ids = posix.getresgid() + if 0 not in current_group_ids: + new_group_ids = (current_group_ids[0]+1, -1, -1) + self.assertRaises(OSError, posix.setresgid, *new_group_ids) + + @unittest.skipUnless(hasattr(posix, 'initgroups'), + "test needs os.initgroups()") + def test_initgroups(self): + # It takes a string and an integer; check that it raises a TypeError + # for other argument lists. + self.assertRaises(TypeError, posix.initgroups) + self.assertRaises(TypeError, posix.initgroups, None) + self.assertRaises(TypeError, posix.initgroups, 3, "foo") + self.assertRaises(TypeError, posix.initgroups, "foo", 3, object()) + + # If a non-privileged user invokes it, it should fail with OSError + # EPERM. + if os.getuid() != 0: + name = pwd.getpwuid(posix.getuid()).pw_name + try: + posix.initgroups(name, 13) + except OSError as e: + self.assertEqual(e.errno, errno.EPERM) + else: + self.fail("Expected OSError to be raised by initgroups") + def test_statvfs(self): if hasattr(posix, 'statvfs'): self.assertTrue(posix.statvfs(os.curdir)) @@ -69,7 +137,7 @@ class PosixTester(unittest.TestCase): fp = open(support.TESTFN) try: fd = posix.dup(fp.fileno()) - self.assertTrue(isinstance(fd, int)) + self.assertIsInstance(fd, int) os.close(fd) finally: fp.close() @@ -132,6 +200,28 @@ class PosixTester(unittest.TestCase): if hasattr(posix, 'stat'): self.assertTrue(posix.stat(support.TESTFN)) + @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()") + def test_mkfifo(self): + support.unlink(support.TESTFN) + posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) + self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) + + @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'), + "don't have mknod()/S_IFIFO") + def test_mknod(self): + # Test using mknod() to create a FIFO (the only use specified + # by POSIX). + support.unlink(support.TESTFN) + mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR + try: + posix.mknod(support.TESTFN, mode, 0) + except OSError as e: + # Some old systems don't allow unprivileged users to use + # mknod(), or only support creating device nodes. + self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) + else: + self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) + def _test_all_chown_common(self, chown_func, first_param): """Common code for chown, fchown and lchown tests.""" if os.getuid() == 0: @@ -186,9 +276,14 @@ class PosixTester(unittest.TestCase): posix.chdir(os.curdir) self.assertRaises(OSError, posix.chdir, support.TESTFN) - def test_lsdir(self): - if hasattr(posix, 'lsdir'): - self.assertTrue(support.TESTFN in posix.lsdir(os.curdir)) + def test_listdir(self): + if hasattr(posix, 'listdir'): + self.assertTrue(support.TESTFN in posix.listdir(os.curdir)) + + def test_listdir_default(self): + # When listdir is called without argument, it's the same as listdir(os.curdir) + if hasattr(posix, 'listdir'): + self.assertTrue(support.TESTFN in posix.listdir()) def test_access(self): if hasattr(posix, 'access'): @@ -197,7 +292,7 @@ class PosixTester(unittest.TestCase): def test_umask(self): if hasattr(posix, 'umask'): old_mask = posix.umask(0) - self.assertTrue(isinstance(old_mask, int)) + self.assertIsInstance(old_mask, int) posix.umask(old_mask) def test_strerror(self): @@ -233,9 +328,13 @@ class PosixTester(unittest.TestCase): posix.lchflags(support.TESTFN, st.st_flags) def test_environ(self): + if os.name == "nt": + item_type = str + else: + item_type = bytes for k, v in posix.environ.items(): - self.assertEqual(type(k), str) - self.assertEqual(type(v), str) + self.assertEqual(type(k), item_type) + self.assertEqual(type(v), item_type) def test_getcwd_long_pathnames(self): if hasattr(posix, 'getcwd'): |