3 from test
import test_support
5 # Skip these tests if there is no posix module.
6 posix
= test_support
.import_module('posix')
17 warnings
.filterwarnings('ignore', '.* potential security risk .*',
20 class PosixTester(unittest
.TestCase
):
24 fp
= open(test_support
.TESTFN
, 'w+')
28 os
.unlink(test_support
.TESTFN
)
30 def testNoArgFunctions(self
):
31 # test posix functions which take no arguments and have
32 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
33 NO_ARG_FUNCTIONS
= [ "ctermid", "getcwd", "getcwdu", "uname",
34 "times", "getloadavg", "tmpnam",
35 "getegid", "geteuid", "getgid", "getgroups",
36 "getpid", "getpgrp", "getppid", "getuid",
39 for name
in NO_ARG_FUNCTIONS
:
40 posix_func
= getattr(posix
, name
, None)
41 if posix_func
is not None:
43 self
.assertRaises(TypeError, posix_func
, 1)
45 if hasattr(posix
, 'getresuid'):
46 def test_getresuid(self
):
47 user_ids
= posix
.getresuid()
48 self
.assertEqual(len(user_ids
), 3)
50 self
.assertGreaterEqual(val
, 0)
52 if hasattr(posix
, 'getresgid'):
53 def test_getresgid(self
):
54 group_ids
= posix
.getresgid()
55 self
.assertEqual(len(group_ids
), 3)
57 self
.assertGreaterEqual(val
, 0)
59 if hasattr(posix
, 'setresuid'):
60 def test_setresuid(self
):
61 current_user_ids
= posix
.getresuid()
62 self
.assertIsNone(posix
.setresuid(*current_user_ids
))
63 # -1 means don't change that value.
64 self
.assertIsNone(posix
.setresuid(-1, -1, -1))
66 def test_setresuid_exception(self
):
67 # Don't do this test if someone is silly enough to run us as root.
68 current_user_ids
= posix
.getresuid()
69 if 0 not in current_user_ids
:
70 new_user_ids
= (current_user_ids
[0]+1, -1, -1)
71 self
.assertRaises(OSError, posix
.setresuid
, *new_user_ids
)
73 if hasattr(posix
, 'setresgid'):
74 def test_setresgid(self
):
75 current_group_ids
= posix
.getresgid()
76 self
.assertIsNone(posix
.setresgid(*current_group_ids
))
77 # -1 means don't change that value.
78 self
.assertIsNone(posix
.setresgid(-1, -1, -1))
80 def test_setresgid_exception(self
):
81 # Don't do this test if someone is silly enough to run us as root.
82 current_group_ids
= posix
.getresgid()
83 if 0 not in current_group_ids
:
84 new_group_ids
= (current_group_ids
[0]+1, -1, -1)
85 self
.assertRaises(OSError, posix
.setresgid
, *new_group_ids
)
87 @unittest.skipUnless(hasattr(posix
, 'initgroups'),
88 "test needs os.initgroups()")
89 def test_initgroups(self
):
90 # It takes a string and an integer; check that it raises a TypeError
91 # for other argument lists.
92 self
.assertRaises(TypeError, posix
.initgroups
)
93 self
.assertRaises(TypeError, posix
.initgroups
, None)
94 self
.assertRaises(TypeError, posix
.initgroups
, 3, "foo")
95 self
.assertRaises(TypeError, posix
.initgroups
, "foo", 3, object())
97 # If a non-privileged user invokes it, it should fail with OSError
100 name
= pwd
.getpwuid(posix
.getuid()).pw_name
102 posix
.initgroups(name
, 13)
104 self
.assertEquals(e
.errno
, errno
.EPERM
)
106 self
.fail("Expected OSError to be raised by initgroups")
108 def test_statvfs(self
):
109 if hasattr(posix
, 'statvfs'):
110 self
.assertTrue(posix
.statvfs(os
.curdir
))
112 def test_fstatvfs(self
):
113 if hasattr(posix
, 'fstatvfs'):
114 fp
= open(test_support
.TESTFN
)
116 self
.assertTrue(posix
.fstatvfs(fp
.fileno()))
120 def test_ftruncate(self
):
121 if hasattr(posix
, 'ftruncate'):
122 fp
= open(test_support
.TESTFN
, 'w+')
124 # we need to have some data to truncate
127 posix
.ftruncate(fp
.fileno(), 0)
132 if hasattr(posix
, 'dup'):
133 fp
= open(test_support
.TESTFN
)
135 fd
= posix
.dup(fp
.fileno())
136 self
.assertTrue(isinstance(fd
, int))
141 def test_confstr(self
):
142 if hasattr(posix
, 'confstr'):
143 self
.assertRaises(ValueError, posix
.confstr
, "CS_garbage")
144 self
.assertEqual(len(posix
.confstr("CS_PATH")) > 0, True)
147 if hasattr(posix
, 'dup2'):
148 fp1
= open(test_support
.TESTFN
)
149 fp2
= open(test_support
.TESTFN
)
151 posix
.dup2(fp1
.fileno(), fp2
.fileno())
156 def fdopen_helper(self
, *args
):
157 fd
= os
.open(test_support
.TESTFN
, os
.O_RDONLY
)
158 fp2
= posix
.fdopen(fd
, *args
)
161 def test_fdopen(self
):
162 if hasattr(posix
, 'fdopen'):
164 self
.fdopen_helper('r')
165 self
.fdopen_helper('r', 100)
167 def test_osexlock(self
):
168 if hasattr(posix
, "O_EXLOCK"):
169 fd
= os
.open(test_support
.TESTFN
,
170 os
.O_WRONLY|os
.O_EXLOCK|os
.O_CREAT
)
171 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
172 os
.O_WRONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
175 if hasattr(posix
, "O_SHLOCK"):
176 fd
= os
.open(test_support
.TESTFN
,
177 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
178 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
179 os
.O_WRONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
182 def test_osshlock(self
):
183 if hasattr(posix
, "O_SHLOCK"):
184 fd1
= os
.open(test_support
.TESTFN
,
185 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
186 fd2
= os
.open(test_support
.TESTFN
,
187 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
191 if hasattr(posix
, "O_EXLOCK"):
192 fd
= os
.open(test_support
.TESTFN
,
193 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
194 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
195 os
.O_RDONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
198 def test_fstat(self
):
199 if hasattr(posix
, 'fstat'):
200 fp
= open(test_support
.TESTFN
)
202 self
.assertTrue(posix
.fstat(fp
.fileno()))
207 if hasattr(posix
, 'stat'):
208 self
.assertTrue(posix
.stat(test_support
.TESTFN
))
210 if hasattr(posix
, 'chown'):
211 def test_chown(self
):
212 # raise an OSError if the file does not exist
213 os
.unlink(test_support
.TESTFN
)
214 self
.assertRaises(OSError, posix
.chown
, test_support
.TESTFN
, -1, -1)
217 open(test_support
.TESTFN
, 'w').close()
220 # Many linux distros have a nfsnobody user as MAX_UID-2
221 # that makes a good test case for signedness issues.
222 # http://bugs.python.org/issue1747858
223 # This part of the test only runs when run as root.
224 # Only scary people run their tests as root.
225 ent
= pwd
.getpwnam('nfsnobody')
226 posix
.chown(test_support
.TESTFN
, ent
.pw_uid
, ent
.pw_gid
)
230 # non-root cannot chown to root, raises OSError
231 self
.assertRaises(OSError, posix
.chown
,
232 test_support
.TESTFN
, 0, 0)
234 # test a successful chown call
235 posix
.chown(test_support
.TESTFN
, os
.getuid(), os
.getgid())
237 def test_chdir(self
):
238 if hasattr(posix
, 'chdir'):
239 posix
.chdir(os
.curdir
)
240 self
.assertRaises(OSError, posix
.chdir
, test_support
.TESTFN
)
242 def test_lsdir(self
):
243 if hasattr(posix
, 'lsdir'):
244 self
.assertTrue(test_support
.TESTFN
in posix
.lsdir(os
.curdir
))
246 def test_access(self
):
247 if hasattr(posix
, 'access'):
248 self
.assertTrue(posix
.access(test_support
.TESTFN
, os
.R_OK
))
250 def test_umask(self
):
251 if hasattr(posix
, 'umask'):
252 old_mask
= posix
.umask(0)
253 self
.assertTrue(isinstance(old_mask
, int))
254 posix
.umask(old_mask
)
256 def test_strerror(self
):
257 if hasattr(posix
, 'strerror'):
258 self
.assertTrue(posix
.strerror(0))
261 if hasattr(posix
, 'pipe'):
262 reader
, writer
= posix
.pipe()
266 def test_tempnam(self
):
267 if hasattr(posix
, 'tempnam'):
268 self
.assertTrue(posix
.tempnam())
269 self
.assertTrue(posix
.tempnam(os
.curdir
))
270 self
.assertTrue(posix
.tempnam(os
.curdir
, 'blah'))
272 def test_tmpfile(self
):
273 if hasattr(posix
, 'tmpfile'):
277 def test_utime(self
):
278 if hasattr(posix
, 'utime'):
280 posix
.utime(test_support
.TESTFN
, None)
281 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (None, None))
282 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (now
, None))
283 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (None, now
))
284 posix
.utime(test_support
.TESTFN
, (int(now
), int(now
)))
285 posix
.utime(test_support
.TESTFN
, (now
, now
))
287 def test_chflags(self
):
288 if hasattr(posix
, 'chflags'):
289 st
= os
.stat(test_support
.TESTFN
)
290 if hasattr(st
, 'st_flags'):
291 posix
.chflags(test_support
.TESTFN
, st
.st_flags
)
293 def test_lchflags(self
):
294 if hasattr(posix
, 'lchflags'):
295 st
= os
.stat(test_support
.TESTFN
)
296 if hasattr(st
, 'st_flags'):
297 posix
.lchflags(test_support
.TESTFN
, st
.st_flags
)
299 def test_getcwd_long_pathnames(self
):
300 if hasattr(posix
, 'getcwd'):
301 dirname
= 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
303 base_path
= os
.path
.abspath(test_support
.TESTFN
) + '.getcwd'
309 # Just returning nothing instead of the SkipTest exception,
310 # because the test results in Error in that case.
312 # raise unittest.SkipTest, "cannot create directory for testing"
316 def _create_and_do_getcwd(dirname
, current_path_length
= 0):
320 raise unittest
.SkipTest
, "mkdir cannot create directory sufficiently deep for getcwd test"
325 if current_path_length
< 1027:
326 _create_and_do_getcwd(dirname
, current_path_length
+ len(dirname
) + 1)
331 _create_and_do_getcwd(dirname
)
335 shutil
.rmtree(base_path
)
339 test_support
.run_unittest(PosixTester
)
341 if __name__
== '__main__':