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 | |
Benjamin Peterson | 02ab7a8 | 2014-04-09 15:40:18 -0400 | [diff] [blame^] | 197 | fd = os.open(test_support.TESTFN, os.O_RDONLY) |
| 198 | self.assertRaises(OSError, posix.fdopen, fd, 'w') |
| 199 | self.assertRaises(OSError, os.close, fd) # fd should be closed. |
| 200 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 201 | @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'), |
| 202 | 'test needs posix.O_EXLOCK') |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 203 | def test_osexlock(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 204 | fd = os.open(test_support.TESTFN, |
| 205 | os.O_WRONLY|os.O_EXLOCK|os.O_CREAT) |
| 206 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 207 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 208 | os.close(fd) |
| 209 | |
| 210 | if hasattr(posix, "O_SHLOCK"): |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 211 | fd = os.open(test_support.TESTFN, |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 212 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 213 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 214 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 215 | os.close(fd) |
| 216 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 217 | @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'), |
| 218 | 'test needs posix.O_SHLOCK') |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 219 | def test_osshlock(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 220 | fd1 = os.open(test_support.TESTFN, |
| 221 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 222 | fd2 = os.open(test_support.TESTFN, |
| 223 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 224 | os.close(fd2) |
| 225 | os.close(fd1) |
| 226 | |
| 227 | if hasattr(posix, "O_EXLOCK"): |
| 228 | fd = os.open(test_support.TESTFN, |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 229 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 230 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 231 | os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 232 | os.close(fd) |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 233 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 234 | @unittest.skipUnless(hasattr(posix, 'fstat'), |
| 235 | 'test needs posix.fstat()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 236 | def test_fstat(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 237 | fp = open(test_support.TESTFN) |
| 238 | try: |
| 239 | self.assertTrue(posix.fstat(fp.fileno())) |
| 240 | finally: |
| 241 | fp.close() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 242 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 243 | @unittest.skipUnless(hasattr(posix, 'stat'), |
| 244 | 'test needs posix.stat()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 245 | def test_stat(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 246 | self.assertTrue(posix.stat(test_support.TESTFN)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 247 | |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 248 | 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] | 249 | """Common code for chown, fchown and lchown tests.""" |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 250 | def check_stat(uid, gid): |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 251 | if stat_func is not None: |
| 252 | stat = stat_func(first_param) |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 253 | self.assertEqual(stat.st_uid, uid) |
| 254 | self.assertEqual(stat.st_gid, gid) |
| 255 | uid = os.getuid() |
| 256 | gid = os.getgid() |
Charles-François Natali | f838764 | 2012-04-17 19:46:06 +0200 | [diff] [blame] | 257 | # test a successful chown call |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 258 | chown_func(first_param, uid, gid) |
| 259 | check_stat(uid, gid) |
| 260 | chown_func(first_param, -1, gid) |
| 261 | check_stat(uid, gid) |
| 262 | chown_func(first_param, uid, -1) |
| 263 | check_stat(uid, gid) |
Charles-François Natali | f838764 | 2012-04-17 19:46:06 +0200 | [diff] [blame] | 264 | |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 265 | if uid == 0: |
| 266 | # Try an amusingly large uid/gid to make sure we handle |
| 267 | # large unsigned values. (chown lets you use any |
| 268 | # uid/gid you like, even if they aren't defined.) |
| 269 | # |
| 270 | # This problem keeps coming up: |
| 271 | # http://bugs.python.org/issue1747858 |
| 272 | # http://bugs.python.org/issue4591 |
| 273 | # http://bugs.python.org/issue15301 |
| 274 | # Hopefully the fix in 4591 fixes it for good! |
| 275 | # |
| 276 | # This part of the test only runs when run as root. |
| 277 | # Only scary people run their tests as root. |
| 278 | |
| 279 | big_value = 2**31 |
| 280 | chown_func(first_param, big_value, big_value) |
| 281 | check_stat(big_value, big_value) |
| 282 | chown_func(first_param, -1, -1) |
| 283 | check_stat(big_value, big_value) |
| 284 | chown_func(first_param, uid, gid) |
| 285 | check_stat(uid, gid) |
Charles-François Natali | f838764 | 2012-04-17 19:46:06 +0200 | [diff] [blame] | 286 | elif platform.system() in ('HP-UX', 'SunOS'): |
| 287 | # HP-UX and Solaris can allow a non-root user to chown() to root |
| 288 | # (issue #5113) |
| 289 | raise unittest.SkipTest("Skipping because of non-standard chown() " |
| 290 | "behavior") |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 291 | else: |
| 292 | # non-root cannot chown to root, raises OSError |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 293 | self.assertRaises(OSError, chown_func, first_param, 0, 0) |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 294 | check_stat(uid, gid) |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 295 | self.assertRaises(OSError, chown_func, first_param, 0, -1) |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 296 | check_stat(uid, gid) |
Serhiy Storchaka | 484dee3 | 2013-02-21 14:33:45 +0200 | [diff] [blame] | 297 | if 0 not in os.getgroups(): |
Serhiy Storchaka | fffc479 | 2013-02-20 19:47:31 +0200 | [diff] [blame] | 298 | self.assertRaises(OSError, chown_func, first_param, -1, 0) |
| 299 | check_stat(uid, gid) |
Serhiy Storchaka | 3e188c4 | 2013-02-20 19:39:59 +0200 | [diff] [blame] | 300 | # test illegal types |
| 301 | for t in str, float: |
| 302 | self.assertRaises(TypeError, chown_func, first_param, t(uid), gid) |
| 303 | check_stat(uid, gid) |
| 304 | self.assertRaises(TypeError, chown_func, first_param, uid, t(gid)) |
| 305 | check_stat(uid, gid) |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 306 | |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 307 | @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()") |
| 308 | def test_chown(self): |
| 309 | # raise an OSError if the file does not exist |
| 310 | os.unlink(test_support.TESTFN) |
| 311 | self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1) |
| 312 | |
| 313 | # re-create the file |
| 314 | open(test_support.TESTFN, 'w').close() |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 315 | self._test_all_chown_common(posix.chown, test_support.TESTFN, |
| 316 | getattr(posix, 'stat', None)) |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 317 | |
| 318 | @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()") |
| 319 | def test_fchown(self): |
| 320 | os.unlink(test_support.TESTFN) |
| 321 | |
| 322 | # re-create the file |
| 323 | test_file = open(test_support.TESTFN, 'w') |
| 324 | try: |
| 325 | fd = test_file.fileno() |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 326 | self._test_all_chown_common(posix.fchown, fd, |
| 327 | getattr(posix, 'fstat', None)) |
Gregory P. Smith | 9f12d46 | 2009-12-23 09:31:11 +0000 | [diff] [blame] | 328 | finally: |
| 329 | test_file.close() |
| 330 | |
| 331 | @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()") |
| 332 | def test_lchown(self): |
| 333 | os.unlink(test_support.TESTFN) |
| 334 | # create a symlink |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 335 | os.symlink(_DUMMY_SYMLINK, test_support.TESTFN) |
Serhiy Storchaka | da5c2a0 | 2013-02-12 09:27:53 +0200 | [diff] [blame] | 336 | self._test_all_chown_common(posix.lchown, test_support.TESTFN, |
| 337 | getattr(posix, 'lstat', None)) |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 338 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 339 | @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 340 | def test_chdir(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 341 | posix.chdir(os.curdir) |
| 342 | self.assertRaises(OSError, posix.chdir, test_support.TESTFN) |
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, 'lsdir'), 'test needs posix.lsdir()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 345 | def test_lsdir(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 346 | self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir)) |
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, 'access'), 'test needs posix.access()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 349 | def test_access(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 350 | self.assertTrue(posix.access(test_support.TESTFN, os.R_OK)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 351 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 352 | @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 353 | def test_umask(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 354 | old_mask = posix.umask(0) |
| 355 | self.assertIsInstance(old_mask, int) |
| 356 | posix.umask(old_mask) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 357 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 358 | @unittest.skipUnless(hasattr(posix, 'strerror'), |
| 359 | 'test needs posix.strerror()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 360 | def test_strerror(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 361 | self.assertTrue(posix.strerror(0)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 362 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 363 | @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 364 | def test_pipe(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 365 | reader, writer = posix.pipe() |
| 366 | os.close(reader) |
| 367 | os.close(writer) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 368 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 369 | @unittest.skipUnless(hasattr(posix, 'tempnam'), |
| 370 | 'test needs posix.tempnam()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 371 | def test_tempnam(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 372 | with warnings.catch_warnings(): |
| 373 | warnings.filterwarnings("ignore", "tempnam", DeprecationWarning) |
| 374 | self.assertTrue(posix.tempnam()) |
| 375 | self.assertTrue(posix.tempnam(os.curdir)) |
| 376 | self.assertTrue(posix.tempnam(os.curdir, 'blah')) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 377 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 378 | @unittest.skipUnless(hasattr(posix, 'tmpfile'), |
| 379 | 'test needs posix.tmpfile()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 380 | def test_tmpfile(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 381 | with warnings.catch_warnings(): |
| 382 | warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning) |
| 383 | fp = posix.tmpfile() |
| 384 | fp.close() |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 385 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 386 | @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 387 | def test_utime(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 388 | now = time.time() |
| 389 | posix.utime(test_support.TESTFN, None) |
| 390 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None)) |
| 391 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None)) |
| 392 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now)) |
| 393 | posix.utime(test_support.TESTFN, (int(now), int(now))) |
| 394 | posix.utime(test_support.TESTFN, (now, now)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 395 | |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 396 | def _test_chflags_regular_file(self, chflags_func, target_file): |
| 397 | st = os.stat(target_file) |
| 398 | self.assertTrue(hasattr(st, 'st_flags')) |
Victor Stinner | 8c7c697 | 2012-12-04 10:07:16 +0100 | [diff] [blame] | 399 | |
| 400 | # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. |
| 401 | try: |
| 402 | chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE) |
| 403 | except OSError as err: |
| 404 | if err.errno != errno.EOPNOTSUPP: |
| 405 | raise |
| 406 | msg = 'chflag UF_IMMUTABLE not supported by underlying fs' |
| 407 | self.skipTest(msg) |
| 408 | |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 409 | try: |
| 410 | new_st = os.stat(target_file) |
| 411 | self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags) |
| 412 | try: |
| 413 | fd = open(target_file, 'w+') |
| 414 | except IOError as e: |
| 415 | self.assertEqual(e.errno, errno.EPERM) |
| 416 | finally: |
| 417 | posix.chflags(target_file, st.st_flags) |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 418 | |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 419 | @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') |
| 420 | def test_chflags(self): |
| 421 | self._test_chflags_regular_file(posix.chflags, test_support.TESTFN) |
| 422 | |
| 423 | @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') |
| 424 | def test_lchflags_regular_file(self): |
| 425 | self._test_chflags_regular_file(posix.lchflags, test_support.TESTFN) |
| 426 | |
| 427 | @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') |
| 428 | def test_lchflags_symlink(self): |
| 429 | testfn_st = os.stat(test_support.TESTFN) |
| 430 | |
| 431 | self.assertTrue(hasattr(testfn_st, 'st_flags')) |
| 432 | |
| 433 | os.symlink(test_support.TESTFN, _DUMMY_SYMLINK) |
| 434 | self.teardown_files.append(_DUMMY_SYMLINK) |
| 435 | dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) |
| 436 | |
Victor Stinner | 8c7c697 | 2012-12-04 10:07:16 +0100 | [diff] [blame] | 437 | # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE. |
| 438 | try: |
| 439 | posix.lchflags(_DUMMY_SYMLINK, |
| 440 | dummy_symlink_st.st_flags | stat.UF_IMMUTABLE) |
| 441 | except OSError as err: |
| 442 | if err.errno != errno.EOPNOTSUPP: |
| 443 | raise |
| 444 | msg = 'chflag UF_IMMUTABLE not supported by underlying fs' |
| 445 | self.skipTest(msg) |
| 446 | |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 447 | try: |
| 448 | new_testfn_st = os.stat(test_support.TESTFN) |
| 449 | new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) |
| 450 | |
| 451 | self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags) |
| 452 | self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE, |
| 453 | new_dummy_symlink_st.st_flags) |
| 454 | finally: |
| 455 | posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags) |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 456 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 457 | @unittest.skipUnless(hasattr(posix, 'getcwd'), |
| 458 | 'test needs posix.getcwd()') |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 459 | def test_getcwd_long_pathnames(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 460 | dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef' |
| 461 | curdir = os.getcwd() |
| 462 | base_path = os.path.abspath(test_support.TESTFN) + '.getcwd' |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 463 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 464 | try: |
| 465 | os.mkdir(base_path) |
| 466 | os.chdir(base_path) |
| 467 | except: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 468 | self.skipTest("cannot create directory for testing") |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 469 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 470 | try: |
| 471 | def _create_and_do_getcwd(dirname, current_path_length = 0): |
| 472 | try: |
| 473 | os.mkdir(dirname) |
| 474 | except: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 475 | self.skipTest("mkdir cannot create directory sufficiently " |
| 476 | "deep for getcwd test") |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 477 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 478 | os.chdir(dirname) |
| 479 | try: |
| 480 | os.getcwd() |
| 481 | if current_path_length < 4099: |
| 482 | _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1) |
| 483 | except OSError as e: |
| 484 | expected_errno = errno.ENAMETOOLONG |
| 485 | # The following platforms have quirky getcwd() |
| 486 | # behaviour -- see issue 9185 and 15765 for |
| 487 | # more information. |
| 488 | quirky_platform = ( |
| 489 | 'sunos' in sys.platform or |
| 490 | 'netbsd' in sys.platform or |
| 491 | 'openbsd' in sys.platform |
| 492 | ) |
| 493 | if quirky_platform: |
| 494 | expected_errno = errno.ERANGE |
| 495 | self.assertEqual(e.errno, expected_errno) |
| 496 | finally: |
| 497 | os.chdir('..') |
| 498 | os.rmdir(dirname) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 499 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 500 | _create_and_do_getcwd(dirname) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 501 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 502 | finally: |
| 503 | os.chdir(curdir) |
| 504 | shutil.rmtree(base_path) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 505 | |
Antoine Pitrou | 8bc59fa | 2011-01-12 18:56:09 +0000 | [diff] [blame] | 506 | @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()") |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 507 | def test_getgroups(self): |
| 508 | with os.popen('id -G') as idg: |
| 509 | groups = idg.read().strip() |
Charles-François Natali | 666a573 | 2012-05-02 20:00:37 +0200 | [diff] [blame] | 510 | ret = idg.close() |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 511 | |
Charles-François Natali | dee8dad | 2012-05-02 20:48:21 +0200 | [diff] [blame] | 512 | if ret != None or not groups: |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 513 | raise unittest.SkipTest("need working 'id -G'") |
| 514 | |
Ned Deily | cc23cc6 | 2013-02-02 15:06:45 -0800 | [diff] [blame] | 515 | # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups() |
| 516 | if sys.platform == 'darwin': |
| 517 | import sysconfig |
| 518 | dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0' |
| 519 | if float(dt) < 10.6: |
| 520 | raise unittest.SkipTest("getgroups(2) is broken prior to 10.6") |
| 521 | |
Ronald Oussoren | ac72e58 | 2010-08-03 07:31:12 +0000 | [diff] [blame] | 522 | # 'id -G' and 'os.getgroups()' should return the same |
| 523 | # groups, ignoring order and duplicates. |
Antoine Pitrou | 8bc59fa | 2011-01-12 18:56:09 +0000 | [diff] [blame] | 524 | # #10822 - it is implementation defined whether posix.getgroups() |
| 525 | # 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] | 526 | self.assertEqual( |
Ronald Oussoren | ac72e58 | 2010-08-03 07:31:12 +0000 | [diff] [blame] | 527 | set([int(x) for x in groups.split()]), |
Antoine Pitrou | 8bc59fa | 2011-01-12 18:56:09 +0000 | [diff] [blame] | 528 | set(posix.getgroups() + [posix.getegid()])) |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 529 | |
| 530 | class PosixGroupsTester(unittest.TestCase): |
| 531 | |
| 532 | def setUp(self): |
| 533 | if posix.getuid() != 0: |
| 534 | raise unittest.SkipTest("not enough privileges") |
| 535 | if not hasattr(posix, 'getgroups'): |
| 536 | raise unittest.SkipTest("need posix.getgroups") |
| 537 | if sys.platform == 'darwin': |
| 538 | raise unittest.SkipTest("getgroups(2) is broken on OSX") |
| 539 | self.saved_groups = posix.getgroups() |
| 540 | |
| 541 | def tearDown(self): |
| 542 | if hasattr(posix, 'setgroups'): |
| 543 | posix.setgroups(self.saved_groups) |
| 544 | elif hasattr(posix, 'initgroups'): |
| 545 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 546 | posix.initgroups(name, self.saved_groups[0]) |
| 547 | |
| 548 | @unittest.skipUnless(hasattr(posix, 'initgroups'), |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 549 | 'test needs posix.initgroups()') |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 550 | def test_initgroups(self): |
| 551 | # find missing group |
| 552 | |
Benjamin Peterson | bde1cfb | 2014-03-01 19:14:12 -0500 | [diff] [blame] | 553 | g = max(self.saved_groups or [0]) + 1 |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 554 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 555 | posix.initgroups(name, g) |
| 556 | self.assertIn(g, posix.getgroups()) |
| 557 | |
| 558 | @unittest.skipUnless(hasattr(posix, 'setgroups'), |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 559 | 'test needs posix.setgroups()') |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 560 | def test_setgroups(self): |
| 561 | for groups in [[0], range(16)]: |
| 562 | posix.setgroups(groups) |
| 563 | self.assertListEqual(groups, posix.getgroups()) |
| 564 | |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 565 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 566 | def test_main(): |
Ronald Oussoren | 9e7ffae | 2010-07-24 09:46:41 +0000 | [diff] [blame] | 567 | test_support.run_unittest(PosixTester, PosixGroupsTester) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 568 | |
| 569 | if __name__ == '__main__': |
| 570 | test_main() |