]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_posix.py
3 from test
import test_support
5 # Skip these tests if there is no posix module.
6 posix
= test_support
.import_module('posix')
19 warnings
.filterwarnings('ignore', '.* potential security risk .*',
22 class PosixTester(unittest
.TestCase
):
26 fp
= open(test_support
.TESTFN
, 'w+')
30 os
.unlink(test_support
.TESTFN
)
32 def testNoArgFunctions(self
):
33 # test posix functions which take no arguments and have
34 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
35 NO_ARG_FUNCTIONS
= [ "ctermid", "getcwd", "getcwdu", "uname",
36 "times", "getloadavg", "tmpnam",
37 "getegid", "geteuid", "getgid", "getgroups",
38 "getpid", "getpgrp", "getppid", "getuid",
41 with warnings
.catch_warnings():
42 warnings
.filterwarnings("ignore", "", DeprecationWarning)
43 for name
in NO_ARG_FUNCTIONS
:
44 posix_func
= getattr(posix
, name
, None)
45 if posix_func
is not None:
47 self
.assertRaises(TypeError, posix_func
, 1)
49 if hasattr(posix
, 'getresuid'):
50 def test_getresuid(self
):
51 user_ids
= posix
.getresuid()
52 self
.assertEqual(len(user_ids
), 3)
54 self
.assertGreaterEqual(val
, 0)
56 if hasattr(posix
, 'getresgid'):
57 def test_getresgid(self
):
58 group_ids
= posix
.getresgid()
59 self
.assertEqual(len(group_ids
), 3)
61 self
.assertGreaterEqual(val
, 0)
63 if hasattr(posix
, 'setresuid'):
64 def test_setresuid(self
):
65 current_user_ids
= posix
.getresuid()
66 self
.assertIsNone(posix
.setresuid(*current_user_ids
))
67 # -1 means don't change that value.
68 self
.assertIsNone(posix
.setresuid(-1, -1, -1))
70 def test_setresuid_exception(self
):
71 # Don't do this test if someone is silly enough to run us as root.
72 current_user_ids
= posix
.getresuid()
73 if 0 not in current_user_ids
:
74 new_user_ids
= (current_user_ids
[0]+1, -1, -1)
75 self
.assertRaises(OSError, posix
.setresuid
, *new_user_ids
)
77 if hasattr(posix
, 'setresgid'):
78 def test_setresgid(self
):
79 current_group_ids
= posix
.getresgid()
80 self
.assertIsNone(posix
.setresgid(*current_group_ids
))
81 # -1 means don't change that value.
82 self
.assertIsNone(posix
.setresgid(-1, -1, -1))
84 def test_setresgid_exception(self
):
85 # Don't do this test if someone is silly enough to run us as root.
86 current_group_ids
= posix
.getresgid()
87 if 0 not in current_group_ids
:
88 new_group_ids
= (current_group_ids
[0]+1, -1, -1)
89 self
.assertRaises(OSError, posix
.setresgid
, *new_group_ids
)
91 @unittest.skipUnless(hasattr(posix
, 'initgroups'),
92 "test needs os.initgroups()")
93 def test_initgroups(self
):
94 # It takes a string and an integer; check that it raises a TypeError
95 # for other argument lists.
96 self
.assertRaises(TypeError, posix
.initgroups
)
97 self
.assertRaises(TypeError, posix
.initgroups
, None)
98 self
.assertRaises(TypeError, posix
.initgroups
, 3, "foo")
99 self
.assertRaises(TypeError, posix
.initgroups
, "foo", 3, object())
101 # If a non-privileged user invokes it, it should fail with OSError
104 name
= pwd
.getpwuid(posix
.getuid()).pw_name
106 posix
.initgroups(name
, 13)
108 self
.assertEqual(e
.errno
, errno
.EPERM
)
110 self
.fail("Expected OSError to be raised by initgroups")
112 def test_statvfs(self
):
113 if hasattr(posix
, 'statvfs'):
114 self
.assertTrue(posix
.statvfs(os
.curdir
))
116 def test_fstatvfs(self
):
117 if hasattr(posix
, 'fstatvfs'):
118 fp
= open(test_support
.TESTFN
)
120 self
.assertTrue(posix
.fstatvfs(fp
.fileno()))
124 def test_ftruncate(self
):
125 if hasattr(posix
, 'ftruncate'):
126 fp
= open(test_support
.TESTFN
, 'w+')
128 # we need to have some data to truncate
131 posix
.ftruncate(fp
.fileno(), 0)
136 if hasattr(posix
, 'dup'):
137 fp
= open(test_support
.TESTFN
)
139 fd
= posix
.dup(fp
.fileno())
140 self
.assertIsInstance(fd
, int)
145 def test_confstr(self
):
146 if hasattr(posix
, 'confstr'):
147 self
.assertRaises(ValueError, posix
.confstr
, "CS_garbage")
148 self
.assertEqual(len(posix
.confstr("CS_PATH")) > 0, True)
151 if hasattr(posix
, 'dup2'):
152 fp1
= open(test_support
.TESTFN
)
153 fp2
= open(test_support
.TESTFN
)
155 posix
.dup2(fp1
.fileno(), fp2
.fileno())
160 def fdopen_helper(self
, *args
):
161 fd
= os
.open(test_support
.TESTFN
, os
.O_RDONLY
)
162 fp2
= posix
.fdopen(fd
, *args
)
165 def test_fdopen(self
):
166 if hasattr(posix
, 'fdopen'):
168 self
.fdopen_helper('r')
169 self
.fdopen_helper('r', 100)
171 def test_osexlock(self
):
172 if hasattr(posix
, "O_EXLOCK"):
173 fd
= os
.open(test_support
.TESTFN
,
174 os
.O_WRONLY|os
.O_EXLOCK|os
.O_CREAT
)
175 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
176 os
.O_WRONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
179 if hasattr(posix
, "O_SHLOCK"):
180 fd
= os
.open(test_support
.TESTFN
,
181 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
182 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
183 os
.O_WRONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
186 def test_osshlock(self
):
187 if hasattr(posix
, "O_SHLOCK"):
188 fd1
= os
.open(test_support
.TESTFN
,
189 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
190 fd2
= os
.open(test_support
.TESTFN
,
191 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
195 if hasattr(posix
, "O_EXLOCK"):
196 fd
= os
.open(test_support
.TESTFN
,
197 os
.O_WRONLY|os
.O_SHLOCK|os
.O_CREAT
)
198 self
.assertRaises(OSError, os
.open, test_support
.TESTFN
,
199 os
.O_RDONLY|os
.O_EXLOCK|os
.O_NONBLOCK
)
202 def test_fstat(self
):
203 if hasattr(posix
, 'fstat'):
204 fp
= open(test_support
.TESTFN
)
206 self
.assertTrue(posix
.fstat(fp
.fileno()))
211 if hasattr(posix
, 'stat'):
212 self
.assertTrue(posix
.stat(test_support
.TESTFN
))
214 def _test_all_chown_common(self
, chown_func
, first_param
):
215 """Common code for chown, fchown and lchown tests."""
218 # Many linux distros have a nfsnobody user as MAX_UID-2
219 # that makes a good test case for signedness issues.
220 # http://bugs.python.org/issue1747858
221 # This part of the test only runs when run as root.
222 # Only scary people run their tests as root.
223 ent
= pwd
.getpwnam('nfsnobody')
224 chown_func(first_param
, ent
.pw_uid
, ent
.pw_gid
)
228 # non-root cannot chown to root, raises OSError
229 self
.assertRaises(OSError, chown_func
,
232 # test a successful chown call
233 chown_func(first_param
, os
.getuid(), os
.getgid())
235 @unittest.skipUnless(hasattr(posix
, 'chown'), "test needs os.chown()")
236 def test_chown(self
):
237 # raise an OSError if the file does not exist
238 os
.unlink(test_support
.TESTFN
)
239 self
.assertRaises(OSError, posix
.chown
, test_support
.TESTFN
, -1, -1)
242 open(test_support
.TESTFN
, 'w').close()
243 self
._test
_all
_chown
_common
(posix
.chown
, test_support
.TESTFN
)
245 @unittest.skipUnless(hasattr(posix
, 'fchown'), "test needs os.fchown()")
246 def test_fchown(self
):
247 os
.unlink(test_support
.TESTFN
)
250 test_file
= open(test_support
.TESTFN
, 'w')
252 fd
= test_file
.fileno()
253 self
._test
_all
_chown
_common
(posix
.fchown
, fd
)
257 @unittest.skipUnless(hasattr(posix
, 'lchown'), "test needs os.lchown()")
258 def test_lchown(self
):
259 os
.unlink(test_support
.TESTFN
)
261 os
.symlink('/tmp/dummy-symlink-target', test_support
.TESTFN
)
262 self
._test
_all
_chown
_common
(posix
.lchown
, test_support
.TESTFN
)
264 def test_chdir(self
):
265 if hasattr(posix
, 'chdir'):
266 posix
.chdir(os
.curdir
)
267 self
.assertRaises(OSError, posix
.chdir
, test_support
.TESTFN
)
269 def test_lsdir(self
):
270 if hasattr(posix
, 'lsdir'):
271 self
.assertIn(test_support
.TESTFN
, posix
.lsdir(os
.curdir
))
273 def test_access(self
):
274 if hasattr(posix
, 'access'):
275 self
.assertTrue(posix
.access(test_support
.TESTFN
, os
.R_OK
))
277 def test_umask(self
):
278 if hasattr(posix
, 'umask'):
279 old_mask
= posix
.umask(0)
280 self
.assertIsInstance(old_mask
, int)
281 posix
.umask(old_mask
)
283 def test_strerror(self
):
284 if hasattr(posix
, 'strerror'):
285 self
.assertTrue(posix
.strerror(0))
288 if hasattr(posix
, 'pipe'):
289 reader
, writer
= posix
.pipe()
293 def test_tempnam(self
):
294 if hasattr(posix
, 'tempnam'):
295 with warnings
.catch_warnings():
296 warnings
.filterwarnings("ignore", "tempnam", DeprecationWarning)
297 self
.assertTrue(posix
.tempnam())
298 self
.assertTrue(posix
.tempnam(os
.curdir
))
299 self
.assertTrue(posix
.tempnam(os
.curdir
, 'blah'))
301 def test_tmpfile(self
):
302 if hasattr(posix
, 'tmpfile'):
303 with warnings
.catch_warnings():
304 warnings
.filterwarnings("ignore", "tmpfile", DeprecationWarning)
308 def test_utime(self
):
309 if hasattr(posix
, 'utime'):
311 posix
.utime(test_support
.TESTFN
, None)
312 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (None, None))
313 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (now
, None))
314 self
.assertRaises(TypeError, posix
.utime
, test_support
.TESTFN
, (None, now
))
315 posix
.utime(test_support
.TESTFN
, (int(now
), int(now
)))
316 posix
.utime(test_support
.TESTFN
, (now
, now
))
318 def test_chflags(self
):
319 if hasattr(posix
, 'chflags'):
320 st
= os
.stat(test_support
.TESTFN
)
321 if hasattr(st
, 'st_flags'):
322 posix
.chflags(test_support
.TESTFN
, st
.st_flags
)
324 def test_lchflags(self
):
325 if hasattr(posix
, 'lchflags'):
326 st
= os
.stat(test_support
.TESTFN
)
327 if hasattr(st
, 'st_flags'):
328 posix
.lchflags(test_support
.TESTFN
, st
.st_flags
)
330 def test_getcwd_long_pathnames(self
):
331 if hasattr(posix
, 'getcwd'):
332 dirname
= 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
334 base_path
= os
.path
.abspath(test_support
.TESTFN
) + '.getcwd'
340 # Just returning nothing instead of the SkipTest exception,
341 # because the test results in Error in that case.
343 # raise unittest.SkipTest, "cannot create directory for testing"
347 def _create_and_do_getcwd(dirname
, current_path_length
= 0):
351 raise unittest
.SkipTest
, "mkdir cannot create directory sufficiently deep for getcwd test"
356 if current_path_length
< 4099:
357 _create_and_do_getcwd(dirname
, current_path_length
+ len(dirname
) + 1)
359 expected_errno
= errno
.ENAMETOOLONG
360 if 'sunos' in sys
.platform
or 'openbsd' in sys
.platform
:
361 expected_errno
= errno
.ERANGE
# Issue 9185
362 self
.assertEqual(e
.errno
, expected_errno
)
367 _create_and_do_getcwd(dirname
)
371 shutil
.rmtree(base_path
)
373 @unittest.skipUnless(hasattr(os
, 'getegid'), "test needs os.getegid()")
374 def test_getgroups(self
):
375 with os
.popen('id -G') as idg
:
376 groups
= idg
.read().strip()
379 raise unittest
.SkipTest("need working 'id -G'")
381 # 'id -G' and 'os.getgroups()' should return the same
382 # groups, ignoring order and duplicates.
383 # #10822 - it is implementation defined whether posix.getgroups()
384 # includes the effective gid so we include it anyway, since id -G does
386 set([int(x
) for x
in groups
.split()]),
387 set(posix
.getgroups() + [posix
.getegid()]))
389 class PosixGroupsTester(unittest
.TestCase
):
392 if posix
.getuid() != 0:
393 raise unittest
.SkipTest("not enough privileges")
394 if not hasattr(posix
, 'getgroups'):
395 raise unittest
.SkipTest("need posix.getgroups")
396 if sys
.platform
== 'darwin':
397 raise unittest
.SkipTest("getgroups(2) is broken on OSX")
398 self
.saved_groups
= posix
.getgroups()
401 if hasattr(posix
, 'setgroups'):
402 posix
.setgroups(self
.saved_groups
)
403 elif hasattr(posix
, 'initgroups'):
404 name
= pwd
.getpwuid(posix
.getuid()).pw_name
405 posix
.initgroups(name
, self
.saved_groups
[0])
407 @unittest.skipUnless(hasattr(posix
, 'initgroups'),
408 "test needs posix.initgroups()")
409 def test_initgroups(self
):
412 g
= max(self
.saved_groups
) + 1
413 name
= pwd
.getpwuid(posix
.getuid()).pw_name
414 posix
.initgroups(name
, g
)
415 self
.assertIn(g
, posix
.getgroups())
417 @unittest.skipUnless(hasattr(posix
, 'setgroups'),
418 "test needs posix.setgroups()")
419 def test_setgroups(self
):
420 for groups
in [[0], range(16)]:
421 posix
.setgroups(groups
)
422 self
.assertListEqual(groups
, posix
.getgroups())
426 test_support
.run_unittest(PosixTester
, PosixGroupsTester
)
428 if __name__
== '__main__':