2003-02-17 14:17:05 -04:00
|
|
|
"Test posix functions"
|
|
|
|
|
2003-05-01 14:45:56 -03:00
|
|
|
from test import test_support
|
2003-02-17 14:17:05 -04:00
|
|
|
|
2009-04-21 10:06:04 -03:00
|
|
|
# Skip these tests if there is no posix module.
|
|
|
|
posix = test_support.import_module('posix')
|
2003-02-17 14:17:05 -04:00
|
|
|
|
2009-12-02 16:37:54 -04:00
|
|
|
import errno
|
2010-07-24 06:46:41 -03:00
|
|
|
import sys
|
2003-02-17 14:17:05 -04:00
|
|
|
import time
|
|
|
|
import os
|
2012-04-17 14:46:06 -03:00
|
|
|
import platform
|
2008-03-18 16:05:32 -03:00
|
|
|
import pwd
|
2008-06-22 10:36:20 -03:00
|
|
|
import shutil
|
2011-06-28 03:41:53 -03:00
|
|
|
import stat
|
2010-07-13 16:17:08 -03:00
|
|
|
import sys
|
2011-07-26 17:52:14 -03:00
|
|
|
import tempfile
|
2003-02-17 14:17:05 -04:00
|
|
|
import unittest
|
|
|
|
import warnings
|
2009-03-30 16:04:00 -03:00
|
|
|
|
2011-07-26 17:52:14 -03:00
|
|
|
_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
|
|
|
|
test_support.TESTFN + '-dummy-symlink')
|
2009-03-30 16:04:00 -03:00
|
|
|
|
2003-02-17 14:17:05 -04:00
|
|
|
warnings.filterwarnings('ignore', '.* potential security risk .*',
|
|
|
|
RuntimeWarning)
|
|
|
|
|
|
|
|
class PosixTester(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# create empty file
|
2003-05-01 14:45:56 -03:00
|
|
|
fp = open(test_support.TESTFN, 'w+')
|
2003-02-17 14:17:05 -04:00
|
|
|
fp.close()
|
2011-06-28 03:41:53 -03:00
|
|
|
self.teardown_files = [ test_support.TESTFN ]
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
2011-06-28 03:41:53 -03:00
|
|
|
for teardown_file in self.teardown_files:
|
|
|
|
os.unlink(teardown_file)
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def testNoArgFunctions(self):
|
|
|
|
# test posix functions which take no arguments and have
|
|
|
|
# no side-effects which we need to cleanup (e.g., fork, wait, abort)
|
|
|
|
NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
|
2003-02-23 18:12:24 -04:00
|
|
|
"times", "getloadavg", "tmpnam",
|
2003-02-17 14:17:05 -04:00
|
|
|
"getegid", "geteuid", "getgid", "getgroups",
|
|
|
|
"getpid", "getpgrp", "getppid", "getuid",
|
|
|
|
]
|
2003-02-23 18:12:24 -04:00
|
|
|
|
2011-01-02 16:04:52 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", "", DeprecationWarning)
|
|
|
|
for name in NO_ARG_FUNCTIONS:
|
|
|
|
posix_func = getattr(posix, name, None)
|
|
|
|
if posix_func is not None:
|
|
|
|
posix_func()
|
|
|
|
self.assertRaises(TypeError, posix_func, 1)
|
2003-02-17 14:17:05 -04:00
|
|
|
|
2009-11-27 09:56:01 -04:00
|
|
|
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)
|
|
|
|
|
2009-12-02 16:37:54 -04:00
|
|
|
@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:
|
2012-05-02 15:00:37 -03:00
|
|
|
try:
|
|
|
|
name = pwd.getpwuid(posix.getuid()).pw_name
|
|
|
|
except KeyError:
|
|
|
|
# the current UID may not have a pwd entry
|
|
|
|
raise unittest.SkipTest("need a pwd entry")
|
2009-12-02 16:37:54 -04:00
|
|
|
try:
|
|
|
|
posix.initgroups(name, 13)
|
|
|
|
except OSError as e:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(e.errno, errno.EPERM)
|
2009-12-02 16:37:54 -04:00
|
|
|
else:
|
|
|
|
self.fail("Expected OSError to be raised by initgroups")
|
|
|
|
|
2003-02-17 14:17:05 -04:00
|
|
|
def test_statvfs(self):
|
|
|
|
if hasattr(posix, 'statvfs'):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(posix.statvfs(os.curdir))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_fstatvfs(self):
|
|
|
|
if hasattr(posix, 'fstatvfs'):
|
2003-05-01 14:45:56 -03:00
|
|
|
fp = open(test_support.TESTFN)
|
2003-02-17 14:17:05 -04:00
|
|
|
try:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(posix.fstatvfs(fp.fileno()))
|
2003-02-17 14:17:05 -04:00
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def test_ftruncate(self):
|
|
|
|
if hasattr(posix, 'ftruncate'):
|
2003-05-01 14:45:56 -03:00
|
|
|
fp = open(test_support.TESTFN, 'w+')
|
2003-02-17 14:17:05 -04:00
|
|
|
try:
|
|
|
|
# we need to have some data to truncate
|
|
|
|
fp.write('test')
|
|
|
|
fp.flush()
|
|
|
|
posix.ftruncate(fp.fileno(), 0)
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def test_dup(self):
|
|
|
|
if hasattr(posix, 'dup'):
|
2003-05-01 14:45:56 -03:00
|
|
|
fp = open(test_support.TESTFN)
|
2003-02-17 14:17:05 -04:00
|
|
|
try:
|
|
|
|
fd = posix.dup(fp.fileno())
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(fd, int)
|
2003-02-17 14:17:05 -04:00
|
|
|
os.close(fd)
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
2006-04-19 22:29:48 -03:00
|
|
|
def test_confstr(self):
|
|
|
|
if hasattr(posix, 'confstr'):
|
|
|
|
self.assertRaises(ValueError, posix.confstr, "CS_garbage")
|
|
|
|
self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
|
|
|
|
|
2003-02-17 14:17:05 -04:00
|
|
|
def test_dup2(self):
|
|
|
|
if hasattr(posix, 'dup2'):
|
2003-05-01 14:45:56 -03:00
|
|
|
fp1 = open(test_support.TESTFN)
|
|
|
|
fp2 = open(test_support.TESTFN)
|
2003-02-17 14:17:05 -04:00
|
|
|
try:
|
|
|
|
posix.dup2(fp1.fileno(), fp2.fileno())
|
|
|
|
finally:
|
|
|
|
fp1.close()
|
|
|
|
fp2.close()
|
|
|
|
|
|
|
|
def fdopen_helper(self, *args):
|
2003-05-01 14:45:56 -03:00
|
|
|
fd = os.open(test_support.TESTFN, os.O_RDONLY)
|
2003-02-17 14:17:05 -04:00
|
|
|
fp2 = posix.fdopen(fd, *args)
|
|
|
|
fp2.close()
|
|
|
|
|
|
|
|
def test_fdopen(self):
|
|
|
|
if hasattr(posix, 'fdopen'):
|
|
|
|
self.fdopen_helper()
|
|
|
|
self.fdopen_helper('r')
|
|
|
|
self.fdopen_helper('r', 100)
|
|
|
|
|
2005-06-16 22:14:49 -03:00
|
|
|
def test_osexlock(self):
|
|
|
|
if hasattr(posix, "O_EXLOCK"):
|
|
|
|
fd = os.open(test_support.TESTFN,
|
|
|
|
os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
|
|
|
|
self.assertRaises(OSError, os.open, test_support.TESTFN,
|
|
|
|
os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
|
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
if hasattr(posix, "O_SHLOCK"):
|
|
|
|
fd = os.open(test_support.TESTFN,
|
|
|
|
os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
|
|
|
|
self.assertRaises(OSError, os.open, test_support.TESTFN,
|
|
|
|
os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
|
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
def test_osshlock(self):
|
|
|
|
if hasattr(posix, "O_SHLOCK"):
|
|
|
|
fd1 = os.open(test_support.TESTFN,
|
|
|
|
os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
|
|
|
|
fd2 = os.open(test_support.TESTFN,
|
|
|
|
os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
|
|
|
|
os.close(fd2)
|
|
|
|
os.close(fd1)
|
|
|
|
|
|
|
|
if hasattr(posix, "O_EXLOCK"):
|
|
|
|
fd = os.open(test_support.TESTFN,
|
|
|
|
os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
|
|
|
|
self.assertRaises(OSError, os.open, test_support.TESTFN,
|
|
|
|
os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
|
|
|
|
os.close(fd)
|
|
|
|
|
2003-02-17 14:17:05 -04:00
|
|
|
def test_fstat(self):
|
|
|
|
if hasattr(posix, 'fstat'):
|
2003-05-01 14:45:56 -03:00
|
|
|
fp = open(test_support.TESTFN)
|
2003-02-17 14:17:05 -04:00
|
|
|
try:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(posix.fstat(fp.fileno()))
|
2003-02-17 14:17:05 -04:00
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def test_stat(self):
|
|
|
|
if hasattr(posix, 'stat'):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(posix.stat(test_support.TESTFN))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
2009-12-23 05:31:11 -04:00
|
|
|
def _test_all_chown_common(self, chown_func, first_param):
|
|
|
|
"""Common code for chown, fchown and lchown tests."""
|
2012-04-17 14:46:06 -03:00
|
|
|
# test a successful chown call
|
|
|
|
chown_func(first_param, os.getuid(), os.getgid())
|
|
|
|
|
2009-12-23 05:31:11 -04:00
|
|
|
if os.getuid() == 0:
|
|
|
|
try:
|
|
|
|
# Many linux distros have a nfsnobody user as MAX_UID-2
|
|
|
|
# that makes a good test case for signedness issues.
|
|
|
|
# http://bugs.python.org/issue1747858
|
|
|
|
# This part of the test only runs when run as root.
|
|
|
|
# Only scary people run their tests as root.
|
|
|
|
ent = pwd.getpwnam('nfsnobody')
|
|
|
|
chown_func(first_param, ent.pw_uid, ent.pw_gid)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2012-04-17 14:46:06 -03:00
|
|
|
elif platform.system() in ('HP-UX', 'SunOS'):
|
|
|
|
# HP-UX and Solaris can allow a non-root user to chown() to root
|
|
|
|
# (issue #5113)
|
|
|
|
raise unittest.SkipTest("Skipping because of non-standard chown() "
|
|
|
|
"behavior")
|
2009-12-23 05:31:11 -04:00
|
|
|
else:
|
|
|
|
# non-root cannot chown to root, raises OSError
|
|
|
|
self.assertRaises(OSError, chown_func,
|
|
|
|
first_param, 0, 0)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
|
|
|
|
def test_chown(self):
|
|
|
|
# raise an OSError if the file does not exist
|
|
|
|
os.unlink(test_support.TESTFN)
|
|
|
|
self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
|
|
|
|
|
|
|
|
# re-create the file
|
|
|
|
open(test_support.TESTFN, 'w').close()
|
|
|
|
self._test_all_chown_common(posix.chown, test_support.TESTFN)
|
2008-03-18 16:05:32 -03:00
|
|
|
|
2009-12-23 05:31:11 -04:00
|
|
|
@unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
|
|
|
|
def test_fchown(self):
|
|
|
|
os.unlink(test_support.TESTFN)
|
|
|
|
|
|
|
|
# re-create the file
|
|
|
|
test_file = open(test_support.TESTFN, 'w')
|
|
|
|
try:
|
|
|
|
fd = test_file.fileno()
|
|
|
|
self._test_all_chown_common(posix.fchown, fd)
|
|
|
|
finally:
|
|
|
|
test_file.close()
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
|
|
|
|
def test_lchown(self):
|
|
|
|
os.unlink(test_support.TESTFN)
|
|
|
|
# create a symlink
|
2011-06-28 03:41:53 -03:00
|
|
|
os.symlink(_DUMMY_SYMLINK, test_support.TESTFN)
|
2009-12-23 05:31:11 -04:00
|
|
|
self._test_all_chown_common(posix.lchown, test_support.TESTFN)
|
2008-03-18 16:05:32 -03:00
|
|
|
|
2003-02-17 14:17:05 -04:00
|
|
|
def test_chdir(self):
|
|
|
|
if hasattr(posix, 'chdir'):
|
|
|
|
posix.chdir(os.curdir)
|
2003-05-01 14:45:56 -03:00
|
|
|
self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_lsdir(self):
|
|
|
|
if hasattr(posix, 'lsdir'):
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_access(self):
|
|
|
|
if hasattr(posix, 'access'):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(posix.access(test_support.TESTFN, os.R_OK))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_umask(self):
|
|
|
|
if hasattr(posix, 'umask'):
|
|
|
|
old_mask = posix.umask(0)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(old_mask, int)
|
2003-02-17 14:17:05 -04:00
|
|
|
posix.umask(old_mask)
|
|
|
|
|
|
|
|
def test_strerror(self):
|
|
|
|
if hasattr(posix, 'strerror'):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(posix.strerror(0))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_pipe(self):
|
|
|
|
if hasattr(posix, 'pipe'):
|
2010-04-10 20:32:12 -03:00
|
|
|
reader, writer = posix.pipe()
|
|
|
|
os.close(reader)
|
|
|
|
os.close(writer)
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_tempnam(self):
|
|
|
|
if hasattr(posix, 'tempnam'):
|
2011-01-02 16:04:52 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
|
|
|
|
self.assertTrue(posix.tempnam())
|
|
|
|
self.assertTrue(posix.tempnam(os.curdir))
|
|
|
|
self.assertTrue(posix.tempnam(os.curdir, 'blah'))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_tmpfile(self):
|
|
|
|
if hasattr(posix, 'tmpfile'):
|
2011-01-02 16:04:52 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
|
|
|
|
fp = posix.tmpfile()
|
|
|
|
fp.close()
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_utime(self):
|
|
|
|
if hasattr(posix, 'utime'):
|
|
|
|
now = time.time()
|
2003-05-01 14:45:56 -03:00
|
|
|
posix.utime(test_support.TESTFN, None)
|
2004-06-06 17:27:05 -03:00
|
|
|
self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
|
|
|
|
self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
|
|
|
|
self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
|
|
|
|
posix.utime(test_support.TESTFN, (int(now), int(now)))
|
2003-05-01 14:45:56 -03:00
|
|
|
posix.utime(test_support.TESTFN, (now, now))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
2011-06-28 03:41:53 -03:00
|
|
|
def _test_chflags_regular_file(self, chflags_func, target_file):
|
|
|
|
st = os.stat(target_file)
|
|
|
|
self.assertTrue(hasattr(st, 'st_flags'))
|
|
|
|
chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE)
|
|
|
|
try:
|
|
|
|
new_st = os.stat(target_file)
|
|
|
|
self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
|
|
|
|
try:
|
|
|
|
fd = open(target_file, 'w+')
|
|
|
|
except IOError as e:
|
|
|
|
self.assertEqual(e.errno, errno.EPERM)
|
|
|
|
finally:
|
|
|
|
posix.chflags(target_file, st.st_flags)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
|
2007-02-19 06:55:19 -04:00
|
|
|
def test_chflags(self):
|
2011-06-28 03:41:53 -03:00
|
|
|
self._test_chflags_regular_file(posix.chflags, test_support.TESTFN)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
|
|
|
|
def test_lchflags_regular_file(self):
|
|
|
|
self._test_chflags_regular_file(posix.lchflags, test_support.TESTFN)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
|
|
|
|
def test_lchflags_symlink(self):
|
|
|
|
testfn_st = os.stat(test_support.TESTFN)
|
|
|
|
|
|
|
|
self.assertTrue(hasattr(testfn_st, 'st_flags'))
|
|
|
|
|
|
|
|
os.symlink(test_support.TESTFN, _DUMMY_SYMLINK)
|
|
|
|
self.teardown_files.append(_DUMMY_SYMLINK)
|
|
|
|
dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
|
|
|
|
|
|
|
|
posix.lchflags(_DUMMY_SYMLINK,
|
|
|
|
dummy_symlink_st.st_flags | stat.UF_IMMUTABLE)
|
|
|
|
try:
|
|
|
|
new_testfn_st = os.stat(test_support.TESTFN)
|
|
|
|
new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
|
|
|
|
|
|
|
|
self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
|
|
|
|
self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
|
|
|
|
new_dummy_symlink_st.st_flags)
|
|
|
|
finally:
|
|
|
|
posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
|
2007-02-19 06:55:19 -04:00
|
|
|
|
2008-06-22 10:36:20 -03:00
|
|
|
def test_getcwd_long_pathnames(self):
|
|
|
|
if hasattr(posix, 'getcwd'):
|
|
|
|
dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
|
|
|
|
curdir = os.getcwd()
|
2008-06-22 15:23:55 -03:00
|
|
|
base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
|
2008-06-22 10:36:20 -03:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(base_path)
|
|
|
|
os.chdir(base_path)
|
2008-06-22 15:23:55 -03:00
|
|
|
except:
|
2009-03-26 17:48:25 -03:00
|
|
|
# Just returning nothing instead of the SkipTest exception,
|
2008-06-22 16:35:24 -03:00
|
|
|
# because the test results in Error in that case.
|
|
|
|
# Is that ok?
|
2009-03-26 17:48:25 -03:00
|
|
|
# raise unittest.SkipTest, "cannot create directory for testing"
|
2008-06-22 16:35:24 -03:00
|
|
|
return
|
2008-06-22 10:36:20 -03:00
|
|
|
|
2008-06-22 15:23:55 -03:00
|
|
|
try:
|
2008-06-22 10:36:20 -03:00
|
|
|
def _create_and_do_getcwd(dirname, current_path_length = 0):
|
|
|
|
try:
|
|
|
|
os.mkdir(dirname)
|
|
|
|
except:
|
2009-03-26 17:48:25 -03:00
|
|
|
raise unittest.SkipTest, "mkdir cannot create directory sufficiently deep for getcwd test"
|
2008-06-22 10:36:20 -03:00
|
|
|
|
|
|
|
os.chdir(dirname)
|
|
|
|
try:
|
|
|
|
os.getcwd()
|
2010-07-13 16:17:08 -03:00
|
|
|
if current_path_length < 4099:
|
2008-06-22 10:36:20 -03:00
|
|
|
_create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
|
2010-07-13 16:17:08 -03:00
|
|
|
except OSError as e:
|
|
|
|
expected_errno = errno.ENAMETOOLONG
|
|
|
|
if 'sunos' in sys.platform or 'openbsd' in sys.platform:
|
|
|
|
expected_errno = errno.ERANGE # Issue 9185
|
|
|
|
self.assertEqual(e.errno, expected_errno)
|
2008-06-22 10:36:20 -03:00
|
|
|
finally:
|
|
|
|
os.chdir('..')
|
|
|
|
os.rmdir(dirname)
|
|
|
|
|
|
|
|
_create_and_do_getcwd(dirname)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
os.chdir(curdir)
|
2009-07-09 15:41:03 -03:00
|
|
|
shutil.rmtree(base_path)
|
2008-06-22 10:36:20 -03:00
|
|
|
|
2011-01-12 14:56:09 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
|
2010-07-24 06:46:41 -03:00
|
|
|
def test_getgroups(self):
|
|
|
|
with os.popen('id -G') as idg:
|
|
|
|
groups = idg.read().strip()
|
2012-05-02 15:00:37 -03:00
|
|
|
ret = idg.close()
|
2010-07-24 06:46:41 -03:00
|
|
|
|
2012-05-02 15:48:21 -03:00
|
|
|
if ret != None or not groups:
|
2010-07-24 06:46:41 -03:00
|
|
|
raise unittest.SkipTest("need working 'id -G'")
|
|
|
|
|
2010-08-03 04:31:12 -03:00
|
|
|
# 'id -G' and 'os.getgroups()' should return the same
|
|
|
|
# groups, ignoring order and duplicates.
|
2011-01-12 14:56:09 -04:00
|
|
|
# #10822 - it is implementation defined whether posix.getgroups()
|
|
|
|
# includes the effective gid so we include it anyway, since id -G does
|
2010-07-24 11:21:29 -03:00
|
|
|
self.assertEqual(
|
2010-08-03 04:31:12 -03:00
|
|
|
set([int(x) for x in groups.split()]),
|
2011-01-12 14:56:09 -04:00
|
|
|
set(posix.getgroups() + [posix.getegid()]))
|
2010-07-24 06:46:41 -03:00
|
|
|
|
|
|
|
class PosixGroupsTester(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
if posix.getuid() != 0:
|
|
|
|
raise unittest.SkipTest("not enough privileges")
|
|
|
|
if not hasattr(posix, 'getgroups'):
|
|
|
|
raise unittest.SkipTest("need posix.getgroups")
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
raise unittest.SkipTest("getgroups(2) is broken on OSX")
|
|
|
|
self.saved_groups = posix.getgroups()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if hasattr(posix, 'setgroups'):
|
|
|
|
posix.setgroups(self.saved_groups)
|
|
|
|
elif hasattr(posix, 'initgroups'):
|
|
|
|
name = pwd.getpwuid(posix.getuid()).pw_name
|
|
|
|
posix.initgroups(name, self.saved_groups[0])
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(posix, 'initgroups'),
|
|
|
|
"test needs posix.initgroups()")
|
|
|
|
def test_initgroups(self):
|
|
|
|
# find missing group
|
|
|
|
|
2010-09-04 14:34:12 -03:00
|
|
|
g = max(self.saved_groups) + 1
|
2010-07-24 06:46:41 -03:00
|
|
|
name = pwd.getpwuid(posix.getuid()).pw_name
|
|
|
|
posix.initgroups(name, g)
|
|
|
|
self.assertIn(g, posix.getgroups())
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(posix, 'setgroups'),
|
|
|
|
"test needs posix.setgroups()")
|
|
|
|
def test_setgroups(self):
|
|
|
|
for groups in [[0], range(16)]:
|
|
|
|
posix.setgroups(groups)
|
|
|
|
self.assertListEqual(groups, posix.getgroups())
|
|
|
|
|
2008-06-22 10:36:20 -03:00
|
|
|
|
2003-02-17 14:17:05 -04:00
|
|
|
def test_main():
|
2010-07-24 06:46:41 -03:00
|
|
|
test_support.run_unittest(PosixTester, PosixGroupsTester)
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|