Changeset 391 for python/trunk/Lib/test/test_posix.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_posix.py
r2 r391 3 3 from test import test_support 4 4 5 try: 6 import posix 7 except ImportError: 8 raise test_support.TestSkipped, "posix is not available" 9 5 # Skip these tests if there is no posix module. 6 posix = test_support.import_module('posix') 7 8 import errno 9 import sys 10 10 import time 11 11 import os 12 import platform 12 13 import pwd 13 14 import shutil 15 import stat 16 import sys 17 import tempfile 14 18 import unittest 15 19 import warnings 20 21 _DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(), 22 test_support.TESTFN + '-dummy-symlink') 23 16 24 warnings.filterwarnings('ignore', '.* potential security risk .*', 17 25 RuntimeWarning) … … 23 31 fp = open(test_support.TESTFN, 'w+') 24 32 fp.close() 33 self.teardown_files = [ test_support.TESTFN ] 25 34 26 35 def tearDown(self): 27 os.unlink(test_support.TESTFN) 36 for teardown_file in self.teardown_files: 37 os.unlink(teardown_file) 28 38 29 39 def testNoArgFunctions(self): … … 36 46 ] 37 47 38 for name in NO_ARG_FUNCTIONS: 39 posix_func = getattr(posix, name, None) 40 if posix_func is not None: 41 posix_func() 42 self.assertRaises(TypeError, posix_func, 1) 48 with warnings.catch_warnings(): 49 warnings.filterwarnings("ignore", "", DeprecationWarning) 50 for name in NO_ARG_FUNCTIONS: 51 posix_func = getattr(posix, name, None) 52 if posix_func is not None: 53 posix_func() 54 self.assertRaises(TypeError, posix_func, 1) 55 56 if hasattr(posix, 'getresuid'): 57 def test_getresuid(self): 58 user_ids = posix.getresuid() 59 self.assertEqual(len(user_ids), 3) 60 for val in user_ids: 61 self.assertGreaterEqual(val, 0) 62 63 if hasattr(posix, 'getresgid'): 64 def test_getresgid(self): 65 group_ids = posix.getresgid() 66 self.assertEqual(len(group_ids), 3) 67 for val in group_ids: 68 self.assertGreaterEqual(val, 0) 69 70 if hasattr(posix, 'setresuid'): 71 def test_setresuid(self): 72 current_user_ids = posix.getresuid() 73 self.assertIsNone(posix.setresuid(*current_user_ids)) 74 # -1 means don't change that value. 75 self.assertIsNone(posix.setresuid(-1, -1, -1)) 76 77 def test_setresuid_exception(self): 78 # Don't do this test if someone is silly enough to run us as root. 79 current_user_ids = posix.getresuid() 80 if 0 not in current_user_ids: 81 new_user_ids = (current_user_ids[0]+1, -1, -1) 82 self.assertRaises(OSError, posix.setresuid, *new_user_ids) 83 84 if hasattr(posix, 'setresgid'): 85 def test_setresgid(self): 86 current_group_ids = posix.getresgid() 87 self.assertIsNone(posix.setresgid(*current_group_ids)) 88 # -1 means don't change that value. 89 self.assertIsNone(posix.setresgid(-1, -1, -1)) 90 91 def test_setresgid_exception(self): 92 # Don't do this test if someone is silly enough to run us as root. 93 current_group_ids = posix.getresgid() 94 if 0 not in current_group_ids: 95 new_group_ids = (current_group_ids[0]+1, -1, -1) 96 self.assertRaises(OSError, posix.setresgid, *new_group_ids) 97 98 @unittest.skipUnless(hasattr(posix, 'initgroups'), 99 "test needs os.initgroups()") 100 def test_initgroups(self): 101 # It takes a string and an integer; check that it raises a TypeError 102 # for other argument lists. 103 self.assertRaises(TypeError, posix.initgroups) 104 self.assertRaises(TypeError, posix.initgroups, None) 105 self.assertRaises(TypeError, posix.initgroups, 3, "foo") 106 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object()) 107 108 # If a non-privileged user invokes it, it should fail with OSError 109 # EPERM. 110 if os.getuid() != 0: 111 try: 112 name = pwd.getpwuid(posix.getuid()).pw_name 113 except KeyError: 114 # the current UID may not have a pwd entry 115 raise unittest.SkipTest("need a pwd entry") 116 try: 117 posix.initgroups(name, 13) 118 except OSError as e: 119 self.assertEqual(e.errno, errno.EPERM) 120 else: 121 self.fail("Expected OSError to be raised by initgroups") 43 122 44 123 def test_statvfs(self): 45 124 if hasattr(posix, 'statvfs'): 46 self.assert _(posix.statvfs(os.curdir))125 self.assertTrue(posix.statvfs(os.curdir)) 47 126 48 127 def test_fstatvfs(self): … … 50 129 fp = open(test_support.TESTFN) 51 130 try: 52 self.assert _(posix.fstatvfs(fp.fileno()))131 self.assertTrue(posix.fstatvfs(fp.fileno())) 53 132 finally: 54 133 fp.close() … … 70 149 try: 71 150 fd = posix.dup(fp.fileno()) 72 self.assert _(isinstance(fd, int))151 self.assertIsInstance(fd, int) 73 152 os.close(fd) 74 153 finally: … … 136 215 fp = open(test_support.TESTFN) 137 216 try: 138 self.assert _(posix.fstat(fp.fileno()))217 self.assertTrue(posix.fstat(fp.fileno())) 139 218 finally: 140 219 fp.close() … … 142 221 def test_stat(self): 143 222 if hasattr(posix, 'stat'): 144 self.assert _(posix.stat(test_support.TESTFN))145 146 def _test_all_chown_common(self, chown_func, first_param ):223 self.assertTrue(posix.stat(test_support.TESTFN)) 224 225 def _test_all_chown_common(self, chown_func, first_param, stat_func): 147 226 """Common code for chown, fchown and lchown tests.""" 148 if os.getuid() == 0: 149 try: 150 # Many linux distros have a nfsnobody user as MAX_UID-2 151 # that makes a good test case for signedness issues. 152 # http://bugs.python.org/issue1747858 153 # This part of the test only runs when run as root. 154 # Only scary people run their tests as root. 155 ent = pwd.getpwnam('nfsnobody') 156 chown_func(first_param, ent.pw_uid, ent.pw_gid) 157 except KeyError: 158 pass 227 def check_stat(uid, gid): 228 if stat_func is not None: 229 stat = stat_func(first_param) 230 self.assertEqual(stat.st_uid, uid) 231 self.assertEqual(stat.st_gid, gid) 232 uid = os.getuid() 233 gid = os.getgid() 234 # test a successful chown call 235 chown_func(first_param, uid, gid) 236 check_stat(uid, gid) 237 chown_func(first_param, -1, gid) 238 check_stat(uid, gid) 239 chown_func(first_param, uid, -1) 240 check_stat(uid, gid) 241 242 if uid == 0: 243 # Try an amusingly large uid/gid to make sure we handle 244 # large unsigned values. (chown lets you use any 245 # uid/gid you like, even if they aren't defined.) 246 # 247 # This problem keeps coming up: 248 # http://bugs.python.org/issue1747858 249 # http://bugs.python.org/issue4591 250 # http://bugs.python.org/issue15301 251 # Hopefully the fix in 4591 fixes it for good! 252 # 253 # This part of the test only runs when run as root. 254 # Only scary people run their tests as root. 255 256 big_value = 2**31 257 chown_func(first_param, big_value, big_value) 258 check_stat(big_value, big_value) 259 chown_func(first_param, -1, -1) 260 check_stat(big_value, big_value) 261 chown_func(first_param, uid, gid) 262 check_stat(uid, gid) 263 elif platform.system() in ('HP-UX', 'SunOS'): 264 # HP-UX and Solaris can allow a non-root user to chown() to root 265 # (issue #5113) 266 raise unittest.SkipTest("Skipping because of non-standard chown() " 267 "behavior") 159 268 else: 160 269 # non-root cannot chown to root, raises OSError 161 self.assertRaises(OSError, chown_func, 162 first_param, 0, 0) 163 164 # test a successful chown call 165 chown_func(first_param, os.getuid(), os.getgid()) 166 167 def _test_chown(self): 270 self.assertRaises(OSError, chown_func, first_param, 0, 0) 271 check_stat(uid, gid) 272 self.assertRaises(OSError, chown_func, first_param, 0, -1) 273 check_stat(uid, gid) 274 if 0 not in os.getgroups(): 275 self.assertRaises(OSError, chown_func, first_param, -1, 0) 276 check_stat(uid, gid) 277 # test illegal types 278 for t in str, float: 279 self.assertRaises(TypeError, chown_func, first_param, t(uid), gid) 280 check_stat(uid, gid) 281 self.assertRaises(TypeError, chown_func, first_param, uid, t(gid)) 282 check_stat(uid, gid) 283 284 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") 285 def test_chown(self): 168 286 # raise an OSError if the file does not exist 169 287 os.unlink(test_support.TESTFN) … … 172 290 # re-create the file 173 291 open(test_support.TESTFN, 'w').close() 174 self._test_all_chown_common(posix.chown, test_support.TESTFN) 175 176 if hasattr(posix, 'chown'): 177 test_chown = _test_chown 178 179 def _test_fchown(self): 292 self._test_all_chown_common(posix.chown, test_support.TESTFN, 293 getattr(posix, 'stat', None)) 294 295 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()") 296 def test_fchown(self): 180 297 os.unlink(test_support.TESTFN) 181 298 … … 184 301 try: 185 302 fd = test_file.fileno() 186 self._test_all_chown_common(posix.fchown, fd) 303 self._test_all_chown_common(posix.fchown, fd, 304 getattr(posix, 'fstat', None)) 187 305 finally: 188 306 test_file.close() 189 307 190 if hasattr(posix, 'fchown'): 191 test_fchown = _test_fchown 192 193 def _test_lchown(self): 308 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()") 309 def test_lchown(self): 194 310 os.unlink(test_support.TESTFN) 195 311 # create a symlink 196 os.symlink('/tmp/dummy-symlink-target', test_support.TESTFN) 197 self._test_all_chown_common(posix.lchown, test_support.TESTFN) 198 199 if hasattr(posix, 'lchown'): 200 test_lchown = _test_lchown 312 os.symlink(_DUMMY_SYMLINK, test_support.TESTFN) 313 self._test_all_chown_common(posix.lchown, test_support.TESTFN, 314 getattr(posix, 'lstat', None)) 201 315 202 316 def test_chdir(self): … … 207 321 def test_lsdir(self): 208 322 if hasattr(posix, 'lsdir'): 209 self.assert _(test_support.TESTFN inposix.lsdir(os.curdir))323 self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir)) 210 324 211 325 def test_access(self): 212 326 if hasattr(posix, 'access'): 213 self.assert _(posix.access(test_support.TESTFN, os.R_OK))327 self.assertTrue(posix.access(test_support.TESTFN, os.R_OK)) 214 328 215 329 def test_umask(self): 216 330 if hasattr(posix, 'umask'): 217 331 old_mask = posix.umask(0) 218 self.assert _(isinstance(old_mask, int))332 self.assertIsInstance(old_mask, int) 219 333 posix.umask(old_mask) 220 334 221 335 def test_strerror(self): 222 336 if hasattr(posix, 'strerror'): 223 self.assert _(posix.strerror(0))337 self.assertTrue(posix.strerror(0)) 224 338 225 339 def test_pipe(self): … … 231 345 def test_tempnam(self): 232 346 if hasattr(posix, 'tempnam'): 233 self.assert_(posix.tempnam()) 234 self.assert_(posix.tempnam(os.curdir)) 235 self.assert_(posix.tempnam(os.curdir, 'blah')) 347 with warnings.catch_warnings(): 348 warnings.filterwarnings("ignore", "tempnam", DeprecationWarning) 349 self.assertTrue(posix.tempnam()) 350 self.assertTrue(posix.tempnam(os.curdir)) 351 self.assertTrue(posix.tempnam(os.curdir, 'blah')) 236 352 237 353 def test_tmpfile(self): 238 354 if hasattr(posix, 'tmpfile'): 239 fp = posix.tmpfile() 240 fp.close() 355 with warnings.catch_warnings(): 356 warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning) 357 fp = posix.tmpfile() 358 fp.close() 241 359 242 360 def test_utime(self): … … 250 368 posix.utime(test_support.TESTFN, (now, now)) 251 369 370 def _test_chflags_regular_file(self, chflags_func, target_file): 371 st = os.stat(target_file) 372 self.assertTrue(hasattr(st, 'st_flags')) 373 374 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. 375 try: 376 chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE) 377 except OSError as err: 378 if err.errno != errno.EOPNOTSUPP: 379 raise 380 msg = 'chflag UF_IMMUTABLE not supported by underlying fs' 381 self.skipTest(msg) 382 383 try: 384 new_st = os.stat(target_file) 385 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags) 386 try: 387 fd = open(target_file, 'w+') 388 except IOError as e: 389 self.assertEqual(e.errno, errno.EPERM) 390 finally: 391 posix.chflags(target_file, st.st_flags) 392 393 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') 252 394 def test_chflags(self): 253 if hasattr(posix, 'chflags'): 254 st = os.stat(test_support.TESTFN) 255 if hasattr(st, 'st_flags'): 256 posix.chflags(test_support.TESTFN, st.st_flags) 257 258 def test_lchflags(self): 259 if hasattr(posix, 'lchflags'): 260 st = os.stat(test_support.TESTFN) 261 if hasattr(st, 'st_flags'): 262 posix.lchflags(test_support.TESTFN, st.st_flags) 395 self._test_chflags_regular_file(posix.chflags, test_support.TESTFN) 396 397 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') 398 def test_lchflags_regular_file(self): 399 self._test_chflags_regular_file(posix.lchflags, test_support.TESTFN) 400 401 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') 402 def test_lchflags_symlink(self): 403 testfn_st = os.stat(test_support.TESTFN) 404 405 self.assertTrue(hasattr(testfn_st, 'st_flags')) 406 407 os.symlink(test_support.TESTFN, _DUMMY_SYMLINK) 408 self.teardown_files.append(_DUMMY_SYMLINK) 409 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) 410 411 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. 412 try: 413 posix.lchflags(_DUMMY_SYMLINK, 414 dummy_symlink_st.st_flags | stat.UF_IMMUTABLE) 415 except OSError as err: 416 if err.errno != errno.EOPNOTSUPP: 417 raise 418 msg = 'chflag UF_IMMUTABLE not supported by underlying fs' 419 self.skipTest(msg) 420 421 try: 422 new_testfn_st = os.stat(test_support.TESTFN) 423 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) 424 425 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags) 426 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE, 427 new_dummy_symlink_st.st_flags) 428 finally: 429 posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags) 263 430 264 431 def test_getcwd_long_pathnames(self): … … 272 439 os.chdir(base_path) 273 440 except: 274 # Just returning nothing instead of the TestSkippedexception,441 # Just returning nothing instead of the SkipTest exception, 275 442 # because the test results in Error in that case. 276 443 # Is that ok? 277 # raise test_support.TestSkipped, "cannot create directory for testing"444 # raise unittest.SkipTest, "cannot create directory for testing" 278 445 return 279 446 … … 283 450 os.mkdir(dirname) 284 451 except: 285 raise test_support.TestSkipped, "mkdir cannot create directory sufficiently deep for getcwd test"452 raise unittest.SkipTest, "mkdir cannot create directory sufficiently deep for getcwd test" 286 453 287 454 os.chdir(dirname) 288 455 try: 289 456 os.getcwd() 290 if current_path_length < 1027:457 if current_path_length < 4099: 291 458 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1) 459 except OSError as e: 460 expected_errno = errno.ENAMETOOLONG 461 # The following platforms have quirky getcwd() 462 # behaviour -- see issue 9185 and 15765 for 463 # more information. 464 quirky_platform = ( 465 'sunos' in sys.platform or 466 'netbsd' in sys.platform or 467 'openbsd' in sys.platform 468 ) 469 if quirky_platform: 470 expected_errno = errno.ERANGE 471 self.assertEqual(e.errno, expected_errno) 292 472 finally: 293 473 os.chdir('..') … … 300 480 shutil.rmtree(base_path) 301 481 482 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()") 483 def test_getgroups(self): 484 with os.popen('id -G') as idg: 485 groups = idg.read().strip() 486 ret = idg.close() 487 488 if ret != None or not groups: 489 raise unittest.SkipTest("need working 'id -G'") 490 491 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups() 492 if sys.platform == 'darwin': 493 import sysconfig 494 dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0' 495 if float(dt) < 10.6: 496 raise unittest.SkipTest("getgroups(2) is broken prior to 10.6") 497 498 # 'id -G' and 'os.getgroups()' should return the same 499 # groups, ignoring order and duplicates. 500 # #10822 - it is implementation defined whether posix.getgroups() 501 # includes the effective gid so we include it anyway, since id -G does 502 self.assertEqual( 503 set([int(x) for x in groups.split()]), 504 set(posix.getgroups() + [posix.getegid()])) 505 506 class PosixGroupsTester(unittest.TestCase): 507 508 def setUp(self): 509 if posix.getuid() != 0: 510 raise unittest.SkipTest("not enough privileges") 511 if not hasattr(posix, 'getgroups'): 512 raise unittest.SkipTest("need posix.getgroups") 513 if sys.platform == 'darwin': 514 raise unittest.SkipTest("getgroups(2) is broken on OSX") 515 self.saved_groups = posix.getgroups() 516 517 def tearDown(self): 518 if hasattr(posix, 'setgroups'): 519 posix.setgroups(self.saved_groups) 520 elif hasattr(posix, 'initgroups'): 521 name = pwd.getpwuid(posix.getuid()).pw_name 522 posix.initgroups(name, self.saved_groups[0]) 523 524 @unittest.skipUnless(hasattr(posix, 'initgroups'), 525 "test needs posix.initgroups()") 526 def test_initgroups(self): 527 # find missing group 528 529 g = max(self.saved_groups) + 1 530 name = pwd.getpwuid(posix.getuid()).pw_name 531 posix.initgroups(name, g) 532 self.assertIn(g, posix.getgroups()) 533 534 @unittest.skipUnless(hasattr(posix, 'setgroups'), 535 "test needs posix.setgroups()") 536 def test_setgroups(self): 537 for groups in [[0], range(16)]: 538 posix.setgroups(groups) 539 self.assertListEqual(groups, posix.getgroups()) 540 302 541 303 542 def test_main(): 304 test_support.run_unittest(PosixTester )543 test_support.run_unittest(PosixTester, PosixGroupsTester) 305 544 306 545 if __name__ == '__main__':
Note:
See TracChangeset
for help on using the changeset viewer.