Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_posix.py

    r2 r391  
    33from test import test_support
    44
    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.
     6posix = test_support.import_module('posix')
     7
     8import errno
     9import sys
    1010import time
    1111import os
     12import platform
    1213import pwd
    1314import shutil
     15import stat
     16import sys
     17import tempfile
    1418import unittest
    1519import warnings
     20
     21_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
     22                              test_support.TESTFN + '-dummy-symlink')
     23
    1624warnings.filterwarnings('ignore', '.* potential security risk .*',
    1725                        RuntimeWarning)
     
    2331        fp = open(test_support.TESTFN, 'w+')
    2432        fp.close()
     33        self.teardown_files = [ test_support.TESTFN ]
    2534
    2635    def tearDown(self):
    27         os.unlink(test_support.TESTFN)
     36        for teardown_file in self.teardown_files:
     37            os.unlink(teardown_file)
    2838
    2939    def testNoArgFunctions(self):
     
    3646                           ]
    3747
    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")
    43122
    44123    def test_statvfs(self):
    45124        if hasattr(posix, 'statvfs'):
    46             self.assert_(posix.statvfs(os.curdir))
     125            self.assertTrue(posix.statvfs(os.curdir))
    47126
    48127    def test_fstatvfs(self):
     
    50129            fp = open(test_support.TESTFN)
    51130            try:
    52                 self.assert_(posix.fstatvfs(fp.fileno()))
     131                self.assertTrue(posix.fstatvfs(fp.fileno()))
    53132            finally:
    54133                fp.close()
     
    70149            try:
    71150                fd = posix.dup(fp.fileno())
    72                 self.assert_(isinstance(fd, int))
     151                self.assertIsInstance(fd, int)
    73152                os.close(fd)
    74153            finally:
     
    136215            fp = open(test_support.TESTFN)
    137216            try:
    138                 self.assert_(posix.fstat(fp.fileno()))
     217                self.assertTrue(posix.fstat(fp.fileno()))
    139218            finally:
    140219                fp.close()
     
    142221    def test_stat(self):
    143222        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):
    147226        """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")
    159268        else:
    160269            # 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):
    168286        # raise an OSError if the file does not exist
    169287        os.unlink(test_support.TESTFN)
     
    172290        # re-create the file
    173291        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):
    180297        os.unlink(test_support.TESTFN)
    181298
     
    184301        try:
    185302            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))
    187305        finally:
    188306            test_file.close()
    189307
    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):
    194310        os.unlink(test_support.TESTFN)
    195311        # 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))
    201315
    202316    def test_chdir(self):
     
    207321    def test_lsdir(self):
    208322        if hasattr(posix, 'lsdir'):
    209             self.assert_(test_support.TESTFN in posix.lsdir(os.curdir))
     323            self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir))
    210324
    211325    def test_access(self):
    212326        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))
    214328
    215329    def test_umask(self):
    216330        if hasattr(posix, 'umask'):
    217331            old_mask = posix.umask(0)
    218             self.assert_(isinstance(old_mask, int))
     332            self.assertIsInstance(old_mask, int)
    219333            posix.umask(old_mask)
    220334
    221335    def test_strerror(self):
    222336        if hasattr(posix, 'strerror'):
    223             self.assert_(posix.strerror(0))
     337            self.assertTrue(posix.strerror(0))
    224338
    225339    def test_pipe(self):
     
    231345    def test_tempnam(self):
    232346        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'))
    236352
    237353    def test_tmpfile(self):
    238354        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()
    241359
    242360    def test_utime(self):
     
    250368            posix.utime(test_support.TESTFN, (now, now))
    251369
     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()')
    252394    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)
    263430
    264431    def test_getcwd_long_pathnames(self):
     
    272439                os.chdir(base_path)
    273440            except:
    274 #               Just returning nothing instead of the TestSkipped exception,
     441#               Just returning nothing instead of the SkipTest exception,
    275442#               because the test results in Error in that case.
    276443#               Is that ok?
    277 #                raise test_support.TestSkipped, "cannot create directory for testing"
     444#                raise unittest.SkipTest, "cannot create directory for testing"
    278445                return
    279446
     
    283450                        os.mkdir(dirname)
    284451                    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"
    286453
    287454                    os.chdir(dirname)
    288455                    try:
    289456                        os.getcwd()
    290                         if current_path_length < 1027:
     457                        if current_path_length < 4099:
    291458                            _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)
    292472                    finally:
    293473                        os.chdir('..')
     
    300480                shutil.rmtree(base_path)
    301481
     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
     506class 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
    302541
    303542def test_main():
    304     test_support.run_unittest(PosixTester)
     543    test_support.run_unittest(PosixTester, PosixGroupsTester)
    305544
    306545if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.