3 from test
import test_support
5 # Skip these tests if there is no posix module.
6 posix
= test_support
.import_module('posix')
16 warnings
.filterwarnings('ignore', '.* potential security risk .*',
19 class PosixTester(unittest
.TestCase
):
23 fp
= open(test_support
.TESTFN
, 'w+')
27 os
.unlink(test_support
.TESTFN
)
29 def testNoArgFunctions(self
):
30 # test posix functions which take no arguments and have
31 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
32 NO_ARG_FUNCTIONS
= [ "ctermid", "getcwd", "getcwdu", "uname",
33 "times", "getloadavg", "tmpnam",
34 "getegid", "geteuid", "getgid", "getgroups",
35 "getpid", "getpgrp", "getppid", "getuid",
38 for name
in NO_ARG_FUNCTIONS
:
39 posix_func
= getattr(posix
, name
, None)
40 if posix_func
is not None:
42 self
.assertRaises(TypeError, posix_func
, 1)
44 def test_statvfs(self
):
45 if hasattr(posix
, 'statvfs'):
46 self
.assertTrue(posix
.statvfs(os
.curdir
))
48 def test_fstatvfs(self
):
49 if hasattr(posix
, 'fstatvfs'):
50 fp
= open(test_support
.TESTFN
)
52 self
.assertTrue(posix
.fstatvfs(fp
.fileno()))
56 def test_ftruncate(self
):
57 if hasattr(posix
, 'ftruncate'):
58 fp
= open(test_support
.TESTFN
, 'w+')
60 # we need to have some data to truncate
63 posix
.ftruncate(fp
.fileno(), 0)
68 if hasattr(posix
, 'dup'):
69 fp
= open(test_support
.TESTFN
)
71 fd
= posix
.dup(fp
.fileno())
72 self
.assertTrue(isinstance(fd
, int))
77 def test_confstr(self
):
78 if hasattr(posix
, 'confstr'):
79 self
.assertRaises(ValueError, posix
.confstr
, "CS_garbage")
80 self
.assertEqual(len(posix
.confstr("CS_PATH")) > 0, True)
83 if hasattr(posix
, 'dup2'):
84 fp1
= open(test_support
.TESTFN
)
85 fp2
= open(test_support
.TESTFN
)
87 posix
.dup2(fp1
.fileno(), fp2
.fileno())
92 def fdopen_helper(self
, *args
):
93 fd
= os
.open(test_support
.TESTFN
, os
.O_RDONLY
)
94 fp2
= posix
.fdopen(fd
, *args
)
97 def test_fdopen(self
):
98 if hasattr(posix
, 'fdopen'):
100 self
.fdopen_helper('r')
101 self
.fdopen_helper('r', 100)
103 def test_osexlock(self
):
104 if hasattr(posix
, "O_EXLOCK"):
105 fd
= os
.open(test_support
.TESTFN
,
106 os
.O_WRONLY|os
.O_EXLOCK|os
.O_CREAT
)
107 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
108 os
.O_WRONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
111 if hasattr(posix
, "O_SHLOCK"):
112 fd
= os
.open(test_support
.TESTFN
,
113 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
114 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
115 os
.O_WRONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
118 def test_osshlock(self
):
119 if hasattr(posix
, "O_SHLOCK"):
120 fd1
= os
.open(test_support
.TESTFN
,
121 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
122 fd2
= os
.open(test_support
.TESTFN
,
123 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
127 if hasattr(posix
, "O_EXLOCK"):
128 fd
= os
.open(test_support
.TESTFN
,
129 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
130 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
131 os
.O_RDONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
134 def test_fstat(self
):
135 if hasattr(posix
, 'fstat'):
136 fp
= open(test_support
.TESTFN
)
138 self
.assertTrue(posix
.fstat(fp
.fileno()))
143 if hasattr(posix
, 'stat'):
144 self
.assertTrue(posix
.stat(test_support
.TESTFN
))
146 if hasattr(posix
, 'chown'):
147 def test_chown(self
):
148 # raise an OSError if the file does not exist
149 os
.unlink(test_support
.TESTFN
)
150 self
.assertRaises(OSError, posix
.chown
, test_support
.TESTFN
, -1, -1)
153 open(test_support
.TESTFN
, 'w').close()
156 # Many linux distros have a nfsnobody user as MAX_UID-2
157 # that makes a good test case for signedness issues.
158 # http://bugs.python.org/issue1747858
159 # This part of the test only runs when run as root.
160 # Only scary people run their tests as root.
161 ent
= pwd
.getpwnam('nfsnobody')
162 posix
.chown(test_support
.TESTFN
, ent
.pw_uid
, ent
.pw_gid
)
166 # non-root cannot chown to root, raises OSError
167 self
.assertRaises(OSError, posix
.chown
,
168 test_support
.TESTFN
, 0, 0)
170 # test a successful chown call
171 posix
.chown(test_support
.TESTFN
, os
.getuid(), os
.getgid())
173 def test_chdir(self
):
174 if hasattr(posix
, 'chdir'):
175 posix
.chdir(os
.curdir
)
176 self
.assertRaises(OSError, posix
.chdir
, test_support
.TESTFN
)
178 def test_lsdir(self
):
179 if hasattr(posix
, 'lsdir'):
180 self
.assertTrue(test_support
.TESTFN
in posix
.lsdir(os
.curdir
))
182 def test_access(self
):
183 if hasattr(posix
, 'access'):
184 self
.assertTrue(posix
.access(test_support
.TESTFN
, os
.R_OK
))
186 def test_umask(self
):
187 if hasattr(posix
, 'umask'):
188 old_mask
= posix
.umask(0)
189 self
.assertTrue(isinstance(old_mask
, int))
190 posix
.umask(old_mask
)
192 def test_strerror(self
):
193 if hasattr(posix
, 'strerror'):
194 self
.assertTrue(posix
.strerror(0))
197 if hasattr(posix
, 'pipe'):
198 reader
, writer
= posix
.pipe()
202 def test_tempnam(self
):
203 if hasattr(posix
, 'tempnam'):
204 self
.assertTrue(posix
.tempnam())
205 self
.assertTrue(posix
.tempnam(os
.curdir
))
206 self
.assertTrue(posix
.tempnam(os
.curdir
, 'blah'))
208 def test_tmpfile(self
):
209 if hasattr(posix
, 'tmpfile'):
213 def test_utime(self
):
214 if hasattr(posix
, 'utime'):
216 posix
.utime(test_support
.TESTFN
, None)
217 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (None, None))
218 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (now
, None))
219 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (None, now
))
220 posix
.utime(test_support
.TESTFN
, (int(now
), int(now
)))
221 posix
.utime(test_support
.TESTFN
, (now
, now
))
223 def test_chflags(self
):
224 if hasattr(posix
, 'chflags'):
225 st
= os
.stat(test_support
.TESTFN
)
226 if hasattr(st
, 'st_flags'):
227 posix
.chflags(test_support
.TESTFN
, st
.st_flags
)
229 def test_lchflags(self
):
230 if hasattr(posix
, 'lchflags'):
231 st
= os
.stat(test_support
.TESTFN
)
232 if hasattr(st
, 'st_flags'):
233 posix
.lchflags(test_support
.TESTFN
, st
.st_flags
)
235 def test_getcwd_long_pathnames(self
):
236 if hasattr(posix
, 'getcwd'):
237 dirname
= 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
239 base_path
= os
.path
.abspath(test_support
.TESTFN
) + '.getcwd'
245 # Just returning nothing instead of the SkipTest exception,
246 # because the test results in Error in that case.
248 # raise unittest.SkipTest, "cannot create directory for testing"
252 def _create_and_do_getcwd(dirname
, current_path_length
= 0):
256 raise unittest
.SkipTest
, "mkdir cannot create directory sufficiently deep for getcwd test"
261 if current_path_length
< 1027:
262 _create_and_do_getcwd(dirname
, current_path_length
+ len(dirname
) + 1)
267 _create_and_do_getcwd(dirname
)
271 shutil
.rmtree(base_path
)
275 test_support
.run_unittest(PosixTester
)
277 if __name__
== '__main__':