Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 1 | "Test posix functions" |
| 2 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 3 | from test import test_support |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4 | |
R. David Murray | 95fb46c | 2009-04-21 13:06:04 +0000 | [diff] [blame] | 5 | # Skip these tests if there is no posix module. |
| 6 | posix = test_support.import_module('posix') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7 | |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 8 | import errno |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 9 | import sys |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 10 | import time |
| 11 | import os |
Charles-François Natali | f838764 | 2012-04-17 19:46:06 +0200 | [diff] [blame] | 12 | import platform |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 13 | import pwd |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 14 | import shutil |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 15 | import stat |
Stefan Krah | 182ae64 | 2010-07-13 19:17:08 +0000 | [diff] [blame] | 16 | import sys |
Ned Deily | d88131a | 2011-07-26 13:52:14 -0700 | [diff] [blame] | 17 | import tempfile |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 18 | import unittest |
| 19 | import warnings |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 20 | |
Ned Deily | d88131a | 2011-07-26 13:52:14 -0700 | [diff] [blame] | 21 | _DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(), |
| 22 | test_support.TESTFN + '-dummy-symlink') |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 23 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 24 | warnings.filterwarnings('ignore', '.* potential security risk .*', |
| 25 | RuntimeWarning) |
| 26 | |
| 27 | class PosixTester(unittest.TestCase): |
| 28 | |
| 29 | def setUp(self): |
| 30 | # create empty file |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 31 | fp = open(test_support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 32 | fp.close() |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 33 | self.teardown_files = [ test_support.TESTFN ] |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 34 | |
| 35 | def tearDown(self): |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 36 | for teardown_file in self.teardown_files: |
| 37 | os.unlink(teardown_file) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 38 | |
| 39 | def testNoArgFunctions(self): |
| 40 | # test posix functions which take no arguments and have |
| 41 | # no side-effects which we need to cleanup (e.g., fork, wait, abort) |
| 42 | NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname", |
Neal Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 43 | "times", "getloadavg", "tmpnam", |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 44 | "getegid", "geteuid", "getgid", "getgroups", |
| 45 | "getpid", "getpgrp", "getppid", "getuid", |
| 46 | ] |
Neal Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 47 | |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 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) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 55 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 56 | @unittest.skipUnless(hasattr(posix, 'getresuid'), |
| 57 | 'test needs posix.getresuid()') |
| 58 | def test_getresuid(self): |
| 59 | user_ids = posix.getresuid() |
| 60 | self.assertEqual(len(user_ids), 3) |
| 61 | for val in user_ids: |
| 62 | self.assertGreaterEqual(val, 0) |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 63 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 64 | @unittest.skipUnless(hasattr(posix, 'getresgid'), |
| 65 | 'test needs posix.getresgid()') |
| 66 | def test_getresgid(self): |
| 67 | group_ids = posix.getresgid() |
| 68 | self.assertEqual(len(group_ids), 3) |
| 69 | for val in group_ids: |
| 70 | self.assertGreaterEqual(val, 0) |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 71 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 72 | @unittest.skipUnless(hasattr(posix, 'setresuid'), |
| 73 | 'test needs posix.setresuid()') |
| 74 | def test_setresuid(self): |
| 75 | current_user_ids = posix.getresuid() |
| 76 | self.assertIsNone(posix.setresuid(*current_user_ids)) |
| 77 | # -1 means don't change that value. |
| 78 | self.assertIsNone(posix.setresuid(-1, -1, -1)) |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 79 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 80 | @unittest.skipUnless(hasattr(posix, 'setresuid'), |
| 81 | 'test needs posix.setresuid()') |
| 82 | def test_setresuid_exception(self): |
| 83 | # Don't do this test if someone is silly enough to run us as root. |
| 84 | current_user_ids = posix.getresuid() |
| 85 | if 0 not in current_user_ids: |
| 86 | new_user_ids = (current_user_ids[0]+1, -1, -1) |
| 87 | self.assertRaises(OSError, posix.setresuid, *new_user_ids) |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 88 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 89 | @unittest.skipUnless(hasattr(posix, 'setresgid'), |
| 90 | 'test needs posix.setresgid()') |
| 91 | def test_setresgid(self): |
| 92 | current_group_ids = posix.getresgid() |
| 93 | self.assertIsNone(posix.setresgid(*current_group_ids)) |
| 94 | # -1 means don't change that value. |
| 95 | self.assertIsNone(posix.setresgid(-1, -1, -1)) |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 96 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 97 | @unittest.skipUnless(hasattr(posix, 'setresgid'), |
| 98 | 'test needs posix.setresgid()') |
| 99 | def test_setresgid_exception(self): |
| 100 | # Don't do this test if someone is silly enough to run us as root. |
| 101 | current_group_ids = posix.getresgid() |
| 102 | if 0 not in current_group_ids: |
| 103 | new_group_ids = (current_group_ids[0]+1, -1, -1) |
| 104 | self.assertRaises(OSError, posix.setresgid, *new_group_ids) |
Martin v. Löwis | 50ea456 | 2009-11-27 13:56:01 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 106 | @unittest.skipUnless(hasattr(posix, 'initgroups'), |
| 107 | "test needs os.initgroups()") |
| 108 | def test_initgroups(self): |
| 109 | # It takes a string and an integer; check that it raises a TypeError |
| 110 | # for other argument lists. |
| 111 | self.assertRaises(TypeError, posix.initgroups) |
| 112 | self.assertRaises(TypeError, posix.initgroups, None) |
| 113 | self.assertRaises(TypeError, posix.initgroups, 3, "foo") |
| 114 | self.assertRaises(TypeError, posix.initgroups, "foo", 3, object()) |
| 115 | |
| 116 | # If a non-privileged user invokes it, it should fail with OSError |
| 117 | # EPERM. |
| 118 | if os.getuid() != 0: |
Charles-François Natali | 666a573 | 2012-05-02 20:00:37 +0200 | [diff] [blame] | 119 | try: |
| 120 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 121 | except KeyError: |
| 122 | # the current UID may not have a pwd entry |
| 123 | raise unittest.SkipTest("need a pwd entry") |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 124 | try: |
| 125 | posix.initgroups(name, 13) |
| 126 | except OSError as e: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 127 | self.assertEqual(e.errno, errno.EPERM) |
Antoine Pitrou | 30b3b35 | 2009-12-02 20:37:54 +0000 | [diff] [blame] | 128 | else: |
| 129 | self.fail("Expected OSError to be raised by initgroups") |
| 130 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 131 | @unittest.skipUnless(hasattr(posix, 'statvfs'), |
| 132 | 'test needs posix.statvfs()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 133 | def test_statvfs(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 134 | self.assertTrue(posix.statvfs(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 135 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 136 | @unittest.skipUnless(hasattr(posix, 'fstatvfs'), |
| 137 | 'test needs posix.fstatvfs()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 138 | def test_fstatvfs(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 139 | fp = open(test_support.TESTFN) |
| 140 | try: |
| 141 | self.assertTrue(posix.fstatvfs(fp.fileno())) |
| 142 | finally: |
| 143 | fp.close() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 144 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 145 | @unittest.skipUnless(hasattr(posix, 'ftruncate'), |
| 146 | 'test needs posix.ftruncate()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 147 | def test_ftruncate(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 148 | fp = open(test_support.TESTFN, 'w+') |
| 149 | try: |
| 150 | # we need to have some data to truncate |
| 151 | fp.write('test') |
| 152 | fp.flush() |
| 153 | posix.ftruncate(fp.fileno(), 0) |
| 154 | finally: |
| 155 | fp.close() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 156 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 157 | @unittest.skipUnless(hasattr(posix, 'dup'), |
| 158 | 'test needs posix.dup()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 159 | def test_dup(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 160 | fp = open(test_support.TESTFN) |
| 161 | try: |
| 162 | fd = posix.dup(fp.fileno()) |
| 163 | self.assertIsInstance(fd, int) |
| 164 | os.close(fd) |
| 165 | finally: |
| 166 | fp.close() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 167 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 168 | @unittest.skipUnless(hasattr(posix, 'confstr'), |
| 169 | 'test needs posix.confstr()') |
Skip Montanaro | 94785ef | 2006-04-20 01:29:48 +0000 | [diff] [blame] | 170 | def test_confstr(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 171 | self.assertRaises(ValueError, posix.confstr, "CS_garbage") |
| 172 | self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True) |
Skip Montanaro | 94785ef | 2006-04-20 01:29:48 +0000 | [diff] [blame] | 173 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 174 | @unittest.skipUnless(hasattr(posix, 'dup2'), |
| 175 | 'test needs posix.dup2()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 176 | def test_dup2(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 177 | fp1 = open(test_support.TESTFN) |
| 178 | fp2 = open(test_support.TESTFN) |
| 179 | try: |
| 180 | posix.dup2(fp1.fileno(), fp2.fileno()) |
| 181 | finally: |
| 182 | fp1.close() |
| 183 | fp2.close() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 184 | |
| 185 | def fdopen_helper(self, *args): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 186 | fd = os.open(test_support.TESTFN, os.O_RDONLY) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 187 | fp2 = posix.fdopen(fd, *args) |
| 188 | fp2.close() |
| 189 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 190 | @unittest.skipUnless(hasattr(posix, 'fdopen'), |
| 191 | 'test needs posix.fdopen()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 192 | def test_fdopen(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 193 | self.fdopen_helper() |
| 194 | self.fdopen_helper('r') |
| 195 | self.fdopen_helper('r', 100) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 196 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 197 | @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'), |
| 198 | 'test needs posix.O_EXLOCK') |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 199 | def test_osexlock(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 200 | fd = os.open(test_support.TESTFN, |
| 201 | os.O_WRONLY|os.O_EXLOCK|os.O_CREAT) |
| 202 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 203 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 204 | os.close(fd) |
| 205 | |
| 206 | if hasattr(posix, "O_SHLOCK"): |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 207 | fd = os.open(test_support.TESTFN, |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 208 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 209 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 210 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 211 | os.close(fd) |
| 212 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 213 | @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'), |
| 214 | 'test needs posix.O_SHLOCK') |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 215 | def test_osshlock(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 216 | fd1 = os.open(test_support.TESTFN, |
| 217 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 218 | fd2 = os.open(test_support.TESTFN, |
| 219 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 220 | os.close(fd2) |
| 221 | os.close(fd1) |
| 222 | |
| 223 | if hasattr(posix, "O_EXLOCK"): |
| 224 | fd = os.open(test_support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 225 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 226 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 227 | os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 228 | os.close(fd) |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 229 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 230 | @unittest.skipUnless(hasattr(posix, 'fstat'), |
| 231 | 'test needs posix.fstat()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 232 | def test_fstat(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 233 | fp = open(test_support.TESTFN) |
| 234 | try: |
| 235 | self.assertTrue(posix.fstat(fp.fileno())) |
| 236 | finally: |
| 237 | fp.close() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 238 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 239 | @unittest.skipUnless(hasattr(posix, 'stat'), |
| 240 | 'test needs posix.stat()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 241 | def test_stat(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 242 | self.assertTrue(posix.stat(test_support.TESTFN)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 243 | |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 244 | def _test_all_chown_common(self, chown_func, first_param, stat_func): |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 245 | """Common code for chown, fchown and lchown tests.""" |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 246 | def check_stat(uid, gid): |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 247 | if stat_func is not None: |
| 248 | stat = stat_func(first_param) |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 249 | self.assertEqual(stat.st_uid, uid) |
| 250 | self.assertEqual(stat.st_gid, gid) |
| 251 | uid = os.getuid() |
| 252 | gid = os.getgid() |
Charles-François Natali | f838764 | 2012-04-17 19:46:06 +0200 | [diff] [blame] | 253 | # test a successful chown call |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 254 | chown_func(first_param, uid, gid) |
| 255 | check_stat(uid, gid) |
| 256 | chown_func(first_param, -1, gid) |
| 257 | check_stat(uid, gid) |
| 258 | chown_func(first_param, uid, -1) |
| 259 | check_stat(uid, gid) |
Charles-François Natali | f838764 | 2012-04-17 19:46:06 +0200 | [diff] [blame] | 260 | |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 261 | if uid == 0: |
| 262 | # Try an amusingly large uid/gid to make sure we handle |
| 263 | # large unsigned values. (chown lets you use any |
| 264 | # uid/gid you like, even if they aren't defined.) |
| 265 | # |
| 266 | # This problem keeps coming up: |
| 267 | # http://bugs.python.org/issue1747858 |
| 268 | # http://bugs.python.org/issue4591 |
| 269 | # http://bugs.python.org/issue15301 |
| 270 | # Hopefully the fix in 4591 fixes it for good! |
| 271 | # |
| 272 | # This part of the test only runs when run as root. |
| 273 | # Only scary people run their tests as root. |
| 274 | |
| 275 | big_value = 2**31 |
| 276 | chown_func(first_param, big_value, big_value) |
| 277 | check_stat(big_value, big_value) |
| 278 | chown_func(first_param, -1, -1) |
| 279 | check_stat(big_value, big_value) |
| 280 | chown_func(first_param, uid, gid) |
| 281 | check_stat(uid, gid) |
Charles-François Natali | f838764 | 2012-04-17 19:46:06 +0200 | [diff] [blame] | 282 | elif platform.system() in ('HP-UX', 'SunOS'): |
| 283 | # HP-UX and Solaris can allow a non-root user to chown() to root |
| 284 | # (issue #5113) |
| 285 | raise unittest.SkipTest("Skipping because of non-standard chown() " |
| 286 | "behavior") |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 287 | else: |
| 288 | # non-root cannot chown to root, raises OSError |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 289 | self.assertRaises(OSError, chown_func, first_param, 0, 0) |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 290 | check_stat(uid, gid) |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 291 | self.assertRaises(OSError, chown_func, first_param, 0, -1) |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 292 | check_stat(uid, gid) |
Serhiy Storchaka | 484dee3 | 2013-02-21 14:33:45 +0200 | [diff] [blame] | 293 | if 0 not in os.getgroups(): |
Serhiy Storchaka | fffc479 | 2013-02-20 19:47:31 +0200 | [diff] [blame] | 294 | self.assertRaises(OSError, chown_func, first_param, -1, 0) |
| 295 | check_stat(uid, gid) |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 296 | # test illegal types |
| 297 | for t in str, float: |
| 298 | self.assertRaises(TypeError, chown_func, first_param, t(uid), gid) |
| 299 | check_stat(uid, gid) |
| 300 | self.assertRaises(TypeError, chown_func, first_param, uid, t(gid)) |
| 301 | check_stat(uid, gid) |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 302 | |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 303 | @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") |
| 304 | def test_chown(self): |
| 305 | # raise an OSError if the file does not exist |
| 306 | os.unlink(test_support.TESTFN) |
| 307 | self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1) |
| 308 | |
| 309 | # re-create the file |
| 310 | open(test_support.TESTFN, 'w').close() |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 311 | self._test_all_chown_common(posix.chown, test_support.TESTFN, |
| 312 | getattr(posix, 'stat', None)) |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 313 | |
| 314 | @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()") |
| 315 | def test_fchown(self): |
| 316 | os.unlink(test_support.TESTFN) |
| 317 | |
| 318 | # re-create the file |
| 319 | test_file = open(test_support.TESTFN, 'w') |
| 320 | try: |
| 321 | fd = test_file.fileno() |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 322 | self._test_all_chown_common(posix.fchown, fd, |
| 323 | getattr(posix, 'fstat', None)) |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 324 | finally: |
| 325 | test_file.close() |
| 326 | |
| 327 | @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()") |
| 328 | def test_lchown(self): |
| 329 | os.unlink(test_support.TESTFN) |
| 330 | # create a symlink |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 331 | os.symlink(_DUMMY_SYMLINK, test_support.TESTFN) |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 332 | self._test_all_chown_common(posix.lchown, test_support.TESTFN, |
| 333 | getattr(posix, 'lstat', None)) |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 334 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 335 | @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 336 | def test_chdir(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 337 | posix.chdir(os.curdir) |
| 338 | self.assertRaises(OSError, posix.chdir, test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 339 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 340 | @unittest.skipUnless(hasattr(posix, 'lsdir'), 'test needs posix.lsdir()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 341 | def test_lsdir(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 342 | self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 343 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 344 | @unittest.skipUnless(hasattr(posix, 'access'), 'test needs posix.access()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 345 | def test_access(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 346 | self.assertTrue(posix.access(test_support.TESTFN, os.R_OK)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 347 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 348 | @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 349 | def test_umask(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 350 | old_mask = posix.umask(0) |
| 351 | self.assertIsInstance(old_mask, int) |
| 352 | posix.umask(old_mask) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 353 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 354 | @unittest.skipUnless(hasattr(posix, 'strerror'), |
| 355 | 'test needs posix.strerror()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 356 | def test_strerror(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 357 | self.assertTrue(posix.strerror(0)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 358 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 359 | @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 360 | def test_pipe(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 361 | reader, writer = posix.pipe() |
| 362 | os.close(reader) |
| 363 | os.close(writer) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 364 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 365 | @unittest.skipUnless(hasattr(posix, 'tempnam'), |
| 366 | 'test needs posix.tempnam()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 367 | def test_tempnam(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 368 | with warnings.catch_warnings(): |
| 369 | warnings.filterwarnings("ignore", "tempnam", DeprecationWarning) |
| 370 | self.assertTrue(posix.tempnam()) |
| 371 | self.assertTrue(posix.tempnam(os.curdir)) |
| 372 | self.assertTrue(posix.tempnam(os.curdir, 'blah')) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 373 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 374 | @unittest.skipUnless(hasattr(posix, 'tmpfile'), |
| 375 | 'test needs posix.tmpfile()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 376 | def test_tmpfile(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 377 | with warnings.catch_warnings(): |
| 378 | warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning) |
| 379 | fp = posix.tmpfile() |
| 380 | fp.close() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 381 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 382 | @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 383 | def test_utime(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 384 | now = time.time() |
| 385 | posix.utime(test_support.TESTFN, None) |
| 386 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None)) |
| 387 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None)) |
| 388 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now)) |
| 389 | posix.utime(test_support.TESTFN, (int(now), int(now))) |
| 390 | posix.utime(test_support.TESTFN, (now, now)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 391 | |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 392 | def _test_chflags_regular_file(self, chflags_func, target_file): |
| 393 | st = os.stat(target_file) |
| 394 | self.assertTrue(hasattr(st, 'st_flags')) |
Victor Stinner | 8c7c697 | 2012-12-04 10:07:16 +0100 | [diff] [blame] | 395 | |
| 396 | # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. |
| 397 | try: |
| 398 | chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE) |
| 399 | except OSError as err: |
| 400 | if err.errno != errno.EOPNOTSUPP: |
| 401 | raise |
| 402 | msg = 'chflag UF_IMMUTABLE not supported by underlying fs' |
| 403 | self.skipTest(msg) |
| 404 | |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 405 | try: |
| 406 | new_st = os.stat(target_file) |
| 407 | self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags) |
| 408 | try: |
| 409 | fd = open(target_file, 'w+') |
| 410 | except IOError as e: |
| 411 | self.assertEqual(e.errno, errno.EPERM) |
| 412 | finally: |
| 413 | posix.chflags(target_file, st.st_flags) |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 414 | |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 415 | @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') |
| 416 | def test_chflags(self): |
| 417 | self._test_chflags_regular_file(posix.chflags, test_support.TESTFN) |
| 418 | |
| 419 | @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') |
| 420 | def test_lchflags_regular_file(self): |
| 421 | self._test_chflags_regular_file(posix.lchflags, test_support.TESTFN) |
| 422 | |
| 423 | @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') |
| 424 | def test_lchflags_symlink(self): |
| 425 | testfn_st = os.stat(test_support.TESTFN) |
| 426 | |
| 427 | self.assertTrue(hasattr(testfn_st, 'st_flags')) |
| 428 | |
| 429 | os.symlink(test_support.TESTFN, _DUMMY_SYMLINK) |
| 430 | self.teardown_files.append(_DUMMY_SYMLINK) |
| 431 | dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) |
| 432 | |
Victor Stinner | 8c7c697 | 2012-12-04 10:07:16 +0100 | [diff] [blame] | 433 | # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. |
| 434 | try: |
| 435 | posix.lchflags(_DUMMY_SYMLINK, |
| 436 | dummy_symlink_st.st_flags | stat.UF_IMMUTABLE) |
| 437 | except OSError as err: |
| 438 | if err.errno != errno.EOPNOTSUPP: |
| 439 | raise |
| 440 | msg = 'chflag UF_IMMUTABLE not supported by underlying fs' |
| 441 | self.skipTest(msg) |
| 442 | |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 443 | try: |
| 444 | new_testfn_st = os.stat(test_support.TESTFN) |
| 445 | new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) |
| 446 | |
| 447 | self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags) |
| 448 | self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE, |
| 449 | new_dummy_symlink_st.st_flags) |
| 450 | finally: |
| 451 | posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags) |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 452 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 453 | @unittest.skipUnless(hasattr(posix, 'getcwd'), |
| 454 | 'test needs posix.getcwd()') |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 455 | def test_getcwd_long_pathnames(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 456 | dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef' |
| 457 | curdir = os.getcwd() |
| 458 | base_path = os.path.abspath(test_support.TESTFN) + '.getcwd' |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 459 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 460 | try: |
| 461 | os.mkdir(base_path) |
| 462 | os.chdir(base_path) |
| 463 | except: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 464 | self.skipTest("cannot create directory for testing") |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 465 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 466 | try: |
| 467 | def _create_and_do_getcwd(dirname, current_path_length = 0): |
| 468 | try: |
| 469 | os.mkdir(dirname) |
| 470 | except: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 471 | self.skipTest("mkdir cannot create directory sufficiently " |
| 472 | "deep for getcwd test") |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 473 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 474 | os.chdir(dirname) |
| 475 | try: |
| 476 | os.getcwd() |
| 477 | if current_path_length < 4099: |
| 478 | _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1) |
| 479 | except OSError as e: |
| 480 | expected_errno = errno.ENAMETOOLONG |
| 481 | # The following platforms have quirky getcwd() |
| 482 | # behaviour -- see issue 9185 and 15765 for |
| 483 | # more information. |
| 484 | quirky_platform = ( |
| 485 | 'sunos' in sys.platform or |
| 486 | 'netbsd' in sys.platform or |
| 487 | 'openbsd' in sys.platform |
| 488 | ) |
| 489 | if quirky_platform: |
| 490 | expected_errno = errno.ERANGE |
| 491 | self.assertEqual(e.errno, expected_errno) |
| 492 | finally: |
| 493 | os.chdir('..') |
| 494 | os.rmdir(dirname) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 495 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 496 | _create_and_do_getcwd(dirname) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 497 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 498 | finally: |
| 499 | os.chdir(curdir) |
| 500 | shutil.rmtree(base_path) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 501 | |
Antoine Pitrou | 8bc59fa | 2011-01-12 18:56:09 +0000 | [diff] [blame] | 502 | @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()") |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 503 | def test_getgroups(self): |
| 504 | with os.popen('id -G') as idg: |
| 505 | groups = idg.read().strip() |
Charles-François Natali | 666a573 | 2012-05-02 20:00:37 +0200 | [diff] [blame] | 506 | ret = idg.close() |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 507 | |
Charles-François Natali | dee8dad | 2012-05-02 20:48:21 +0200 | [diff] [blame] | 508 | if ret != None or not groups: |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 509 | raise unittest.SkipTest("need working 'id -G'") |
| 510 | |
Ned Deily | cc23cc6 | 2013-02-02 15:06:45 -0800 | [diff] [blame] | 511 | # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups() |
| 512 | if sys.platform == 'darwin': |
| 513 | import sysconfig |
| 514 | dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0' |
| 515 | if float(dt) < 10.6: |
| 516 | raise unittest.SkipTest("getgroups(2) is broken prior to 10.6") |
| 517 | |
Ronald Oussoren | ac72e58 | 2010-08-03 07:31:12 +0000 | [diff] [blame] | 518 | # 'id -G' and 'os.getgroups()' should return the same |
| 519 | # groups, ignoring order and duplicates. |
Antoine Pitrou | 8bc59fa | 2011-01-12 18:56:09 +0000 | [diff] [blame] | 520 | # #10822 - it is implementation defined whether posix.getgroups() |
| 521 | # includes the effective gid so we include it anyway, since id -G does |
Ronald Oussoren | 5719c2f | 2010-07-24 14:21:29 +0000 | [diff] [blame] | 522 | self.assertEqual( |
Ronald Oussoren | ac72e58 | 2010-08-03 07:31:12 +0000 | [diff] [blame] | 523 | set([int(x) for x in groups.split()]), |
Antoine Pitrou | 8bc59fa | 2011-01-12 18:56:09 +0000 | [diff] [blame] | 524 | set(posix.getgroups() + [posix.getegid()])) |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 525 | |
| 526 | class PosixGroupsTester(unittest.TestCase): |
| 527 | |
| 528 | def setUp(self): |
| 529 | if posix.getuid() != 0: |
| 530 | raise unittest.SkipTest("not enough privileges") |
| 531 | if not hasattr(posix, 'getgroups'): |
| 532 | raise unittest.SkipTest("need posix.getgroups") |
| 533 | if sys.platform == 'darwin': |
| 534 | raise unittest.SkipTest("getgroups(2) is broken on OSX") |
| 535 | self.saved_groups = posix.getgroups() |
| 536 | |
| 537 | def tearDown(self): |
| 538 | if hasattr(posix, 'setgroups'): |
| 539 | posix.setgroups(self.saved_groups) |
| 540 | elif hasattr(posix, 'initgroups'): |
| 541 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 542 | posix.initgroups(name, self.saved_groups[0]) |
| 543 | |
| 544 | @unittest.skipUnless(hasattr(posix, 'initgroups'), |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 545 | 'test needs posix.initgroups()') |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 546 | def test_initgroups(self): |
| 547 | # find missing group |
| 548 | |
Antoine Pitrou | dd806ce | 2010-09-04 17:34:12 +0000 | [diff] [blame] | 549 | g = max(self.saved_groups) + 1 |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 550 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 551 | posix.initgroups(name, g) |
| 552 | self.assertIn(g, posix.getgroups()) |
| 553 | |
| 554 | @unittest.skipUnless(hasattr(posix, 'setgroups'), |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 555 | 'test needs posix.setgroups()') |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 556 | def test_setgroups(self): |
| 557 | for groups in [[0], range(16)]: |
| 558 | posix.setgroups(groups) |
| 559 | self.assertListEqual(groups, posix.getgroups()) |
| 560 | |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 561 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 562 | def test_main(): |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 563 | test_support.run_unittest(PosixTester, PosixGroupsTester) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 564 | |
| 565 | if __name__ == '__main__': |
| 566 | test_main() |