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 | |
| 5 | try: |
| 6 | import posix |
| 7 | except ImportError: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 8 | raise test_support.TestSkipped, "posix is not available" |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9 | |
Stefan Krah | 200888f | 2010-07-19 18:15:41 +0000 | [diff] [blame] | 10 | import errno |
Ronald Oussoren | ac08e30 | 2010-07-24 10:05:19 +0000 | [diff] [blame] | 11 | import sys |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 12 | import time |
| 13 | import os |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 14 | import pwd |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 15 | import shutil |
Stefan Krah | 36db84d | 2010-07-19 15:43:23 +0000 | [diff] [blame] | 16 | import sys |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 17 | import unittest |
| 18 | import warnings |
| 19 | warnings.filterwarnings('ignore', '.* potential security risk .*', |
| 20 | RuntimeWarning) |
| 21 | |
| 22 | class PosixTester(unittest.TestCase): |
| 23 | |
| 24 | def setUp(self): |
| 25 | # create empty file |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 26 | fp = open(test_support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 27 | fp.close() |
| 28 | |
| 29 | def tearDown(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 30 | os.unlink(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 31 | |
| 32 | def testNoArgFunctions(self): |
| 33 | # test posix functions which take no arguments and have |
| 34 | # no side-effects which we need to cleanup (e.g., fork, wait, abort) |
| 35 | NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname", |
Neal Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 36 | "times", "getloadavg", "tmpnam", |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 37 | "getegid", "geteuid", "getgid", "getgroups", |
| 38 | "getpid", "getpgrp", "getppid", "getuid", |
| 39 | ] |
Neal Norwitz | 71b13e8 | 2003-02-23 22:12:24 +0000 | [diff] [blame] | 40 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 41 | for name in NO_ARG_FUNCTIONS: |
| 42 | posix_func = getattr(posix, name, None) |
| 43 | if posix_func is not None: |
| 44 | posix_func() |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 45 | self.assertRaises(TypeError, posix_func, 1) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 46 | |
| 47 | def test_statvfs(self): |
| 48 | if hasattr(posix, 'statvfs'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 49 | self.assert_(posix.statvfs(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 50 | |
| 51 | def test_fstatvfs(self): |
| 52 | if hasattr(posix, 'fstatvfs'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 53 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 54 | try: |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 55 | self.assert_(posix.fstatvfs(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 56 | finally: |
| 57 | fp.close() |
| 58 | |
| 59 | def test_ftruncate(self): |
| 60 | if hasattr(posix, 'ftruncate'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 61 | fp = open(test_support.TESTFN, 'w+') |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 62 | try: |
| 63 | # we need to have some data to truncate |
| 64 | fp.write('test') |
| 65 | fp.flush() |
| 66 | posix.ftruncate(fp.fileno(), 0) |
| 67 | finally: |
| 68 | fp.close() |
| 69 | |
| 70 | def test_dup(self): |
| 71 | if hasattr(posix, 'dup'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 72 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 73 | try: |
| 74 | fd = posix.dup(fp.fileno()) |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 75 | self.assert_(isinstance(fd, int)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 76 | os.close(fd) |
| 77 | finally: |
| 78 | fp.close() |
| 79 | |
Skip Montanaro | 94785ef | 2006-04-20 01:29:48 +0000 | [diff] [blame] | 80 | def test_confstr(self): |
| 81 | if hasattr(posix, 'confstr'): |
| 82 | self.assertRaises(ValueError, posix.confstr, "CS_garbage") |
| 83 | self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True) |
| 84 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 85 | def test_dup2(self): |
| 86 | if hasattr(posix, 'dup2'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 87 | fp1 = open(test_support.TESTFN) |
| 88 | fp2 = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 89 | try: |
| 90 | posix.dup2(fp1.fileno(), fp2.fileno()) |
| 91 | finally: |
| 92 | fp1.close() |
| 93 | fp2.close() |
| 94 | |
| 95 | def fdopen_helper(self, *args): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 96 | fd = os.open(test_support.TESTFN, os.O_RDONLY) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 97 | fp2 = posix.fdopen(fd, *args) |
| 98 | fp2.close() |
| 99 | |
| 100 | def test_fdopen(self): |
| 101 | if hasattr(posix, 'fdopen'): |
| 102 | self.fdopen_helper() |
| 103 | self.fdopen_helper('r') |
| 104 | self.fdopen_helper('r', 100) |
| 105 | |
Skip Montanaro | 9847000 | 2005-06-17 01:14:49 +0000 | [diff] [blame] | 106 | def test_osexlock(self): |
| 107 | if hasattr(posix, "O_EXLOCK"): |
| 108 | fd = os.open(test_support.TESTFN, |
| 109 | os.O_WRONLY|os.O_EXLOCK|os.O_CREAT) |
| 110 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 111 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 112 | os.close(fd) |
| 113 | |
| 114 | if hasattr(posix, "O_SHLOCK"): |
| 115 | fd = os.open(test_support.TESTFN, |
| 116 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 117 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 118 | os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 119 | os.close(fd) |
| 120 | |
| 121 | def test_osshlock(self): |
| 122 | if hasattr(posix, "O_SHLOCK"): |
| 123 | fd1 = os.open(test_support.TESTFN, |
| 124 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 125 | fd2 = os.open(test_support.TESTFN, |
| 126 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 127 | os.close(fd2) |
| 128 | os.close(fd1) |
| 129 | |
| 130 | if hasattr(posix, "O_EXLOCK"): |
| 131 | fd = os.open(test_support.TESTFN, |
| 132 | os.O_WRONLY|os.O_SHLOCK|os.O_CREAT) |
| 133 | self.assertRaises(OSError, os.open, test_support.TESTFN, |
| 134 | os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK) |
| 135 | os.close(fd) |
| 136 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 137 | def test_fstat(self): |
| 138 | if hasattr(posix, 'fstat'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 139 | fp = open(test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 140 | try: |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 141 | self.assert_(posix.fstat(fp.fileno())) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 142 | finally: |
| 143 | fp.close() |
| 144 | |
| 145 | def test_stat(self): |
| 146 | if hasattr(posix, 'stat'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 147 | self.assert_(posix.stat(test_support.TESTFN)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 148 | |
Gregory P. Smith | 21c134d | 2009-12-23 09:46:53 +0000 | [diff] [blame] | 149 | def _test_all_chown_common(self, chown_func, first_param): |
| 150 | """Common code for chown, fchown and lchown tests.""" |
| 151 | if os.getuid() == 0: |
| 152 | try: |
| 153 | # Many linux distros have a nfsnobody user as MAX_UID-2 |
| 154 | # that makes a good test case for signedness issues. |
| 155 | # http://bugs.python.org/issue1747858 |
| 156 | # This part of the test only runs when run as root. |
| 157 | # Only scary people run their tests as root. |
| 158 | ent = pwd.getpwnam('nfsnobody') |
| 159 | chown_func(first_param, ent.pw_uid, ent.pw_gid) |
| 160 | except KeyError: |
| 161 | pass |
| 162 | else: |
| 163 | # non-root cannot chown to root, raises OSError |
| 164 | self.assertRaises(OSError, chown_func, |
| 165 | first_param, 0, 0) |
| 166 | |
| 167 | # test a successful chown call |
| 168 | chown_func(first_param, os.getuid(), os.getgid()) |
| 169 | |
| 170 | def _test_chown(self): |
| 171 | # raise an OSError if the file does not exist |
| 172 | os.unlink(test_support.TESTFN) |
| 173 | self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1) |
| 174 | |
| 175 | # re-create the file |
| 176 | open(test_support.TESTFN, 'w').close() |
| 177 | self._test_all_chown_common(posix.chown, test_support.TESTFN) |
| 178 | |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 179 | if hasattr(posix, 'chown'): |
Gregory P. Smith | 21c134d | 2009-12-23 09:46:53 +0000 | [diff] [blame] | 180 | test_chown = _test_chown |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 181 | |
Gregory P. Smith | 21c134d | 2009-12-23 09:46:53 +0000 | [diff] [blame] | 182 | def _test_fchown(self): |
| 183 | os.unlink(test_support.TESTFN) |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 184 | |
Gregory P. Smith | 21c134d | 2009-12-23 09:46:53 +0000 | [diff] [blame] | 185 | # re-create the file |
| 186 | test_file = open(test_support.TESTFN, 'w') |
| 187 | try: |
| 188 | fd = test_file.fileno() |
| 189 | self._test_all_chown_common(posix.fchown, fd) |
| 190 | finally: |
| 191 | test_file.close() |
| 192 | |
| 193 | if hasattr(posix, 'fchown'): |
| 194 | test_fchown = _test_fchown |
| 195 | |
| 196 | def _test_lchown(self): |
| 197 | os.unlink(test_support.TESTFN) |
| 198 | # create a symlink |
| 199 | os.symlink('/tmp/dummy-symlink-target', test_support.TESTFN) |
| 200 | self._test_all_chown_common(posix.lchown, test_support.TESTFN) |
| 201 | |
| 202 | if hasattr(posix, 'lchown'): |
| 203 | test_lchown = _test_lchown |
Gregory P. Smith | f48da8f | 2008-03-18 19:05:32 +0000 | [diff] [blame] | 204 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 205 | def test_chdir(self): |
| 206 | if hasattr(posix, 'chdir'): |
| 207 | posix.chdir(os.curdir) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 208 | self.assertRaises(OSError, posix.chdir, test_support.TESTFN) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 209 | |
| 210 | def test_lsdir(self): |
| 211 | if hasattr(posix, 'lsdir'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 212 | self.assert_(test_support.TESTFN in posix.lsdir(os.curdir)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 213 | |
| 214 | def test_access(self): |
| 215 | if hasattr(posix, 'access'): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 216 | self.assert_(posix.access(test_support.TESTFN, os.R_OK)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 217 | |
| 218 | def test_umask(self): |
| 219 | if hasattr(posix, 'umask'): |
| 220 | old_mask = posix.umask(0) |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 221 | self.assert_(isinstance(old_mask, int)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 222 | posix.umask(old_mask) |
| 223 | |
| 224 | def test_strerror(self): |
| 225 | if hasattr(posix, 'strerror'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 226 | self.assert_(posix.strerror(0)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 227 | |
| 228 | def test_pipe(self): |
| 229 | if hasattr(posix, 'pipe'): |
| 230 | reader, writer = posix.pipe() |
| 231 | os.close(reader) |
| 232 | os.close(writer) |
| 233 | |
| 234 | def test_tempnam(self): |
| 235 | if hasattr(posix, 'tempnam'): |
Neal Norwitz | 2ff51a8 | 2003-02-17 22:40:31 +0000 | [diff] [blame] | 236 | self.assert_(posix.tempnam()) |
| 237 | self.assert_(posix.tempnam(os.curdir)) |
| 238 | self.assert_(posix.tempnam(os.curdir, 'blah')) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 239 | |
| 240 | def test_tmpfile(self): |
| 241 | if hasattr(posix, 'tmpfile'): |
| 242 | fp = posix.tmpfile() |
| 243 | fp.close() |
| 244 | |
| 245 | def test_utime(self): |
| 246 | if hasattr(posix, 'utime'): |
| 247 | now = time.time() |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 248 | posix.utime(test_support.TESTFN, None) |
Neal Norwitz | c28e7ad | 2004-06-06 20:27:05 +0000 | [diff] [blame] | 249 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None)) |
| 250 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None)) |
| 251 | self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now)) |
| 252 | posix.utime(test_support.TESTFN, (int(now), int(now))) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 253 | posix.utime(test_support.TESTFN, (now, now)) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 254 | |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 255 | def test_chflags(self): |
| 256 | if hasattr(posix, 'chflags'): |
| 257 | st = os.stat(test_support.TESTFN) |
| 258 | if hasattr(st, 'st_flags'): |
| 259 | posix.chflags(test_support.TESTFN, st.st_flags) |
| 260 | |
| 261 | def test_lchflags(self): |
| 262 | if hasattr(posix, 'lchflags'): |
| 263 | st = os.stat(test_support.TESTFN) |
| 264 | if hasattr(st, 'st_flags'): |
| 265 | posix.lchflags(test_support.TESTFN, st.st_flags) |
| 266 | |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 267 | def test_getcwd_long_pathnames(self): |
| 268 | if hasattr(posix, 'getcwd'): |
| 269 | dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef' |
| 270 | curdir = os.getcwd() |
Facundo Batista | 96f3dc3 | 2008-06-22 18:23:55 +0000 | [diff] [blame] | 271 | base_path = os.path.abspath(test_support.TESTFN) + '.getcwd' |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 272 | |
| 273 | try: |
| 274 | os.mkdir(base_path) |
| 275 | os.chdir(base_path) |
Facundo Batista | 96f3dc3 | 2008-06-22 18:23:55 +0000 | [diff] [blame] | 276 | except: |
Facundo Batista | 2694eb0 | 2008-06-22 19:35:24 +0000 | [diff] [blame] | 277 | # Just returning nothing instead of the TestSkipped exception, |
| 278 | # because the test results in Error in that case. |
| 279 | # Is that ok? |
| 280 | # raise test_support.TestSkipped, "cannot create directory for testing" |
| 281 | return |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 282 | |
Facundo Batista | 96f3dc3 | 2008-06-22 18:23:55 +0000 | [diff] [blame] | 283 | try: |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 284 | def _create_and_do_getcwd(dirname, current_path_length = 0): |
| 285 | try: |
| 286 | os.mkdir(dirname) |
| 287 | except: |
| 288 | raise test_support.TestSkipped, "mkdir cannot create directory sufficiently deep for getcwd test" |
| 289 | |
| 290 | os.chdir(dirname) |
| 291 | try: |
| 292 | os.getcwd() |
Stefan Krah | 36db84d | 2010-07-19 15:43:23 +0000 | [diff] [blame] | 293 | if current_path_length < 4099: |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 294 | _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1) |
Stefan Krah | 36db84d | 2010-07-19 15:43:23 +0000 | [diff] [blame] | 295 | except OSError as e: |
| 296 | expected_errno = errno.ENAMETOOLONG |
| 297 | if 'sunos' in sys.platform or 'openbsd' in sys.platform: |
| 298 | expected_errno = errno.ERANGE # Issue 9185 |
| 299 | self.assertEqual(e.errno, expected_errno) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 300 | finally: |
| 301 | os.chdir('..') |
| 302 | os.rmdir(dirname) |
| 303 | |
| 304 | _create_and_do_getcwd(dirname) |
| 305 | |
| 306 | finally: |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 307 | os.chdir(curdir) |
R. David Murray | 8bb57b7 | 2009-07-09 20:13:41 +0000 | [diff] [blame] | 308 | shutil.rmtree(base_path) |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 309 | |
Ronald Oussoren | ac08e30 | 2010-07-24 10:05:19 +0000 | [diff] [blame] | 310 | def test_getgroups(self): |
Ronald Oussoren | 1fa5e05 | 2010-08-01 18:46:05 +0000 | [diff] [blame] | 311 | with os.popen('id -G 2>/dev/null') as idg: |
Ronald Oussoren | ac08e30 | 2010-07-24 10:05:19 +0000 | [diff] [blame] | 312 | groups = idg.read().strip() |
| 313 | |
| 314 | if not groups: |
Ronald Oussoren | 1fa5e05 | 2010-08-01 18:46:05 +0000 | [diff] [blame] | 315 | # This test needs 'id -G' |
| 316 | return |
Ronald Oussoren | ac08e30 | 2010-07-24 10:05:19 +0000 | [diff] [blame] | 317 | |
Ronald Oussoren | 7c97345 | 2010-07-24 14:23:23 +0000 | [diff] [blame] | 318 | # The order of groups isn't important, hence the calls |
| 319 | # to sorted. |
| 320 | self.assertEqual( |
| 321 | list(sorted([int(x) for x in groups.split()])), |
| 322 | list(sorted(posix.getgroups()))) |
Ronald Oussoren | ac08e30 | 2010-07-24 10:05:19 +0000 | [diff] [blame] | 323 | |
| 324 | class PosixGroupsTester(unittest.TestCase): |
| 325 | if posix.getuid() == 0 and hasattr(posix, 'getgroups') and sys.platform != 'darwin': |
| 326 | |
| 327 | def setUp(self): |
| 328 | self.saved_groups = posix.getgroups() |
| 329 | |
| 330 | def tearDown(self): |
| 331 | if hasattr(posix, 'setgroups'): |
| 332 | posix.setgroups(self.saved_groups) |
| 333 | elif hasattr(posix, 'initgroups'): |
| 334 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 335 | posix.initgroups(name, self.saved_groups[0]) |
| 336 | |
| 337 | if hasattr(posix, 'initgroups'): |
| 338 | def test_initgroups(self): |
| 339 | # find missing group |
| 340 | |
| 341 | groups = sorted(self.saved_groups) |
| 342 | for g1,g2 in zip(groups[:-1], groups[1:]): |
| 343 | g = g1 + 1 |
| 344 | if g < g2: |
| 345 | break |
| 346 | else: |
| 347 | g = g2 + 1 |
| 348 | name = pwd.getpwuid(posix.getuid()).pw_name |
| 349 | posix.initgroups(name, g) |
| 350 | self.assertIn(g, posix.getgroups()) |
| 351 | |
| 352 | if hasattr(posix, 'setgroups'): |
| 353 | def test_setgroups(self): |
| 354 | for groups in [[0], range(16)]: |
| 355 | posix.setgroups(groups) |
| 356 | self.assertListEqual(groups, posix.getgroups()) |
| 357 | |
Facundo Batista | 5596b0c | 2008-06-22 13:36:20 +0000 | [diff] [blame] | 358 | |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 359 | def test_main(): |
Ronald Oussoren | ac08e30 | 2010-07-24 10:05:19 +0000 | [diff] [blame] | 360 | test_support.run_unittest(PosixTester, PosixGroupsTester) |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 361 | |
| 362 | if __name__ == '__main__': |
| 363 | test_main() |