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
|
|
|
|
|
|
|
try:
|
|
|
|
import posix
|
|
|
|
except ImportError:
|
2003-05-01 14:45:56 -03:00
|
|
|
raise test_support.TestSkipped, "posix is not available"
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
import warnings
|
|
|
|
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()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2003-05-01 14:45:56 -03:00
|
|
|
os.unlink(test_support.TESTFN)
|
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
|
|
|
|
2003-02-17 14:17:05 -04:00
|
|
|
for name in NO_ARG_FUNCTIONS:
|
|
|
|
posix_func = getattr(posix, name, None)
|
|
|
|
if posix_func is not None:
|
|
|
|
posix_func()
|
2003-02-17 18:40:31 -04:00
|
|
|
self.assertRaises(TypeError, posix_func, 1)
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_statvfs(self):
|
|
|
|
if hasattr(posix, 'statvfs'):
|
2003-02-17 18:40:31 -04:00
|
|
|
self.assert_(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:
|
2003-02-17 18:40:31 -04:00
|
|
|
self.assert_(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())
|
2003-02-17 18:40:31 -04:00
|
|
|
self.assert_(isinstance(fd, int))
|
2003-02-17 14:17:05 -04:00
|
|
|
os.close(fd)
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
2006-04-21 07:40:58 -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:
|
2003-02-17 18:40:31 -04:00
|
|
|
self.assert_(posix.fstat(fp.fileno()))
|
2003-02-17 14:17:05 -04:00
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def test_stat(self):
|
|
|
|
if hasattr(posix, 'stat'):
|
2003-05-01 14:45:56 -03:00
|
|
|
self.assert_(posix.stat(test_support.TESTFN))
|
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'):
|
2003-05-01 14:45:56 -03:00
|
|
|
self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_access(self):
|
|
|
|
if hasattr(posix, 'access'):
|
2003-05-01 14:45:56 -03:00
|
|
|
self.assert_(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)
|
2003-02-17 18:40:31 -04:00
|
|
|
self.assert_(isinstance(old_mask, int))
|
2003-02-17 14:17:05 -04:00
|
|
|
posix.umask(old_mask)
|
|
|
|
|
|
|
|
def test_strerror(self):
|
|
|
|
if hasattr(posix, 'strerror'):
|
2003-02-17 18:40:31 -04:00
|
|
|
self.assert_(posix.strerror(0))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_pipe(self):
|
|
|
|
if hasattr(posix, 'pipe'):
|
|
|
|
reader, writer = posix.pipe()
|
|
|
|
os.close(reader)
|
|
|
|
os.close(writer)
|
|
|
|
|
|
|
|
def test_tempnam(self):
|
|
|
|
if hasattr(posix, 'tempnam'):
|
2003-02-17 18:40:31 -04:00
|
|
|
self.assert_(posix.tempnam())
|
|
|
|
self.assert_(posix.tempnam(os.curdir))
|
|
|
|
self.assert_(posix.tempnam(os.curdir, 'blah'))
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
def test_tmpfile(self):
|
|
|
|
if hasattr(posix, 'tmpfile'):
|
|
|
|
fp = posix.tmpfile()
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(PosixTester)
|
2003-02-17 14:17:05 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|