blob: a6a73063ce378e4c7c4bedd83809c496db591e82 [file] [log] [blame]
Neal Norwitze241ce82003-02-17 18:17:05 +00001"Test posix functions"
2
Walter Dörwald21d3a322003-05-01 17:45:56 +00003from test import test_support
Neal Norwitze241ce82003-02-17 18:17:05 +00004
R. David Murray95fb46c2009-04-21 13:06:04 +00005# Skip these tests if there is no posix module.
6posix = test_support.import_module('posix')
Neal Norwitze241ce82003-02-17 18:17:05 +00007
Antoine Pitrou30b3b352009-12-02 20:37:54 +00008import errno
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +00009import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000010import time
11import os
Gregory P. Smithf48da8f2008-03-18 19:05:32 +000012import pwd
Facundo Batista5596b0c2008-06-22 13:36:20 +000013import shutil
Ned Deily43e10542011-06-27 23:41:53 -070014import stat
Stefan Krah182ae642010-07-13 19:17:08 +000015import sys
Ned Deilyd88131a2011-07-26 13:52:14 -070016import tempfile
Neal Norwitze241ce82003-02-17 18:17:05 +000017import unittest
18import warnings
R. David Murray59beec32009-03-30 19:04:00 +000019
Ned Deilyd88131a2011-07-26 13:52:14 -070020_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
21 test_support.TESTFN + '-dummy-symlink')
R. David Murray59beec32009-03-30 19:04:00 +000022
Neal Norwitze241ce82003-02-17 18:17:05 +000023warnings.filterwarnings('ignore', '.* potential security risk .*',
24 RuntimeWarning)
25
26class PosixTester(unittest.TestCase):
27
28 def setUp(self):
29 # create empty file
Walter Dörwald21d3a322003-05-01 17:45:56 +000030 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000031 fp.close()
Ned Deily43e10542011-06-27 23:41:53 -070032 self.teardown_files = [ test_support.TESTFN ]
Neal Norwitze241ce82003-02-17 18:17:05 +000033
34 def tearDown(self):
Ned Deily43e10542011-06-27 23:41:53 -070035 for teardown_file in self.teardown_files:
36 os.unlink(teardown_file)
Neal Norwitze241ce82003-02-17 18:17:05 +000037
38 def testNoArgFunctions(self):
39 # test posix functions which take no arguments and have
40 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
41 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
Neal Norwitz71b13e82003-02-23 22:12:24 +000042 "times", "getloadavg", "tmpnam",
Neal Norwitze241ce82003-02-17 18:17:05 +000043 "getegid", "geteuid", "getgid", "getgroups",
44 "getpid", "getpgrp", "getppid", "getuid",
45 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000046
Antoine Pitroub0614612011-01-02 20:04:52 +000047 with warnings.catch_warnings():
48 warnings.filterwarnings("ignore", "", DeprecationWarning)
49 for name in NO_ARG_FUNCTIONS:
50 posix_func = getattr(posix, name, None)
51 if posix_func is not None:
52 posix_func()
53 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000054
Martin v. Löwis50ea4562009-11-27 13:56:01 +000055 if hasattr(posix, 'getresuid'):
56 def test_getresuid(self):
57 user_ids = posix.getresuid()
58 self.assertEqual(len(user_ids), 3)
59 for val in user_ids:
60 self.assertGreaterEqual(val, 0)
61
62 if hasattr(posix, 'getresgid'):
63 def test_getresgid(self):
64 group_ids = posix.getresgid()
65 self.assertEqual(len(group_ids), 3)
66 for val in group_ids:
67 self.assertGreaterEqual(val, 0)
68
69 if hasattr(posix, 'setresuid'):
70 def test_setresuid(self):
71 current_user_ids = posix.getresuid()
72 self.assertIsNone(posix.setresuid(*current_user_ids))
73 # -1 means don't change that value.
74 self.assertIsNone(posix.setresuid(-1, -1, -1))
75
76 def test_setresuid_exception(self):
77 # Don't do this test if someone is silly enough to run us as root.
78 current_user_ids = posix.getresuid()
79 if 0 not in current_user_ids:
80 new_user_ids = (current_user_ids[0]+1, -1, -1)
81 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
82
83 if hasattr(posix, 'setresgid'):
84 def test_setresgid(self):
85 current_group_ids = posix.getresgid()
86 self.assertIsNone(posix.setresgid(*current_group_ids))
87 # -1 means don't change that value.
88 self.assertIsNone(posix.setresgid(-1, -1, -1))
89
90 def test_setresgid_exception(self):
91 # Don't do this test if someone is silly enough to run us as root.
92 current_group_ids = posix.getresgid()
93 if 0 not in current_group_ids:
94 new_group_ids = (current_group_ids[0]+1, -1, -1)
95 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
96
Antoine Pitrou30b3b352009-12-02 20:37:54 +000097 @unittest.skipUnless(hasattr(posix, 'initgroups'),
98 "test needs os.initgroups()")
99 def test_initgroups(self):
100 # It takes a string and an integer; check that it raises a TypeError
101 # for other argument lists.
102 self.assertRaises(TypeError, posix.initgroups)
103 self.assertRaises(TypeError, posix.initgroups, None)
104 self.assertRaises(TypeError, posix.initgroups, 3, "foo")
105 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
106
107 # If a non-privileged user invokes it, it should fail with OSError
108 # EPERM.
109 if os.getuid() != 0:
110 name = pwd.getpwuid(posix.getuid()).pw_name
111 try:
112 posix.initgroups(name, 13)
113 except OSError as e:
Ezio Melotti2623a372010-11-21 13:34:58 +0000114 self.assertEqual(e.errno, errno.EPERM)
Antoine Pitrou30b3b352009-12-02 20:37:54 +0000115 else:
116 self.fail("Expected OSError to be raised by initgroups")
117
Neal Norwitze241ce82003-02-17 18:17:05 +0000118 def test_statvfs(self):
119 if hasattr(posix, 'statvfs'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000120 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000121
122 def test_fstatvfs(self):
123 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000124 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000125 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000126 self.assertTrue(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000127 finally:
128 fp.close()
129
130 def test_ftruncate(self):
131 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000132 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +0000133 try:
134 # we need to have some data to truncate
135 fp.write('test')
136 fp.flush()
137 posix.ftruncate(fp.fileno(), 0)
138 finally:
139 fp.close()
140
141 def test_dup(self):
142 if hasattr(posix, 'dup'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000143 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000144 try:
145 fd = posix.dup(fp.fileno())
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000146 self.assertIsInstance(fd, int)
Neal Norwitze241ce82003-02-17 18:17:05 +0000147 os.close(fd)
148 finally:
149 fp.close()
150
Skip Montanaro94785ef2006-04-20 01:29:48 +0000151 def test_confstr(self):
152 if hasattr(posix, 'confstr'):
153 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
154 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
155
Neal Norwitze241ce82003-02-17 18:17:05 +0000156 def test_dup2(self):
157 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000158 fp1 = open(test_support.TESTFN)
159 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000160 try:
161 posix.dup2(fp1.fileno(), fp2.fileno())
162 finally:
163 fp1.close()
164 fp2.close()
165
166 def fdopen_helper(self, *args):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000167 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +0000168 fp2 = posix.fdopen(fd, *args)
169 fp2.close()
170
171 def test_fdopen(self):
172 if hasattr(posix, 'fdopen'):
173 self.fdopen_helper()
174 self.fdopen_helper('r')
175 self.fdopen_helper('r', 100)
176
Skip Montanaro98470002005-06-17 01:14:49 +0000177 def test_osexlock(self):
178 if hasattr(posix, "O_EXLOCK"):
179 fd = os.open(test_support.TESTFN,
180 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
181 self.assertRaises(OSError, os.open, test_support.TESTFN,
182 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
183 os.close(fd)
184
185 if hasattr(posix, "O_SHLOCK"):
186 fd = os.open(test_support.TESTFN,
187 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
188 self.assertRaises(OSError, os.open, test_support.TESTFN,
189 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
190 os.close(fd)
191
192 def test_osshlock(self):
193 if hasattr(posix, "O_SHLOCK"):
194 fd1 = os.open(test_support.TESTFN,
195 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
196 fd2 = os.open(test_support.TESTFN,
197 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
198 os.close(fd2)
199 os.close(fd1)
200
201 if hasattr(posix, "O_EXLOCK"):
202 fd = os.open(test_support.TESTFN,
203 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
204 self.assertRaises(OSError, os.open, test_support.TESTFN,
205 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
206 os.close(fd)
207
Neal Norwitze241ce82003-02-17 18:17:05 +0000208 def test_fstat(self):
209 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000210 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000211 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000212 self.assertTrue(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000213 finally:
214 fp.close()
215
216 def test_stat(self):
217 if hasattr(posix, 'stat'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000218 self.assertTrue(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000219
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000220 def _test_all_chown_common(self, chown_func, first_param):
221 """Common code for chown, fchown and lchown tests."""
222 if os.getuid() == 0:
223 try:
224 # Many linux distros have a nfsnobody user as MAX_UID-2
225 # that makes a good test case for signedness issues.
226 # http://bugs.python.org/issue1747858
227 # This part of the test only runs when run as root.
228 # Only scary people run their tests as root.
229 ent = pwd.getpwnam('nfsnobody')
230 chown_func(first_param, ent.pw_uid, ent.pw_gid)
231 except KeyError:
232 pass
233 else:
234 # non-root cannot chown to root, raises OSError
235 self.assertRaises(OSError, chown_func,
236 first_param, 0, 0)
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000237
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000238 # test a successful chown call
239 chown_func(first_param, os.getuid(), os.getgid())
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000240
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000241 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
242 def test_chown(self):
243 # raise an OSError if the file does not exist
244 os.unlink(test_support.TESTFN)
245 self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
246
247 # re-create the file
248 open(test_support.TESTFN, 'w').close()
249 self._test_all_chown_common(posix.chown, test_support.TESTFN)
250
251 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
252 def test_fchown(self):
253 os.unlink(test_support.TESTFN)
254
255 # re-create the file
256 test_file = open(test_support.TESTFN, 'w')
257 try:
258 fd = test_file.fileno()
259 self._test_all_chown_common(posix.fchown, fd)
260 finally:
261 test_file.close()
262
263 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
264 def test_lchown(self):
265 os.unlink(test_support.TESTFN)
266 # create a symlink
Ned Deily43e10542011-06-27 23:41:53 -0700267 os.symlink(_DUMMY_SYMLINK, test_support.TESTFN)
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000268 self._test_all_chown_common(posix.lchown, test_support.TESTFN)
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000269
Neal Norwitze241ce82003-02-17 18:17:05 +0000270 def test_chdir(self):
271 if hasattr(posix, 'chdir'):
272 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000273 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000274
275 def test_lsdir(self):
276 if hasattr(posix, 'lsdir'):
Ezio Melottiaa980582010-01-23 23:04:36 +0000277 self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000278
279 def test_access(self):
280 if hasattr(posix, 'access'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000281 self.assertTrue(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000282
283 def test_umask(self):
284 if hasattr(posix, 'umask'):
285 old_mask = posix.umask(0)
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000286 self.assertIsInstance(old_mask, int)
Neal Norwitze241ce82003-02-17 18:17:05 +0000287 posix.umask(old_mask)
288
289 def test_strerror(self):
290 if hasattr(posix, 'strerror'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000291 self.assertTrue(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000292
293 def test_pipe(self):
294 if hasattr(posix, 'pipe'):
Antoine Pitroubba8f2d2010-04-10 23:32:12 +0000295 reader, writer = posix.pipe()
296 os.close(reader)
297 os.close(writer)
Neal Norwitze241ce82003-02-17 18:17:05 +0000298
299 def test_tempnam(self):
300 if hasattr(posix, 'tempnam'):
Antoine Pitroub0614612011-01-02 20:04:52 +0000301 with warnings.catch_warnings():
302 warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
303 self.assertTrue(posix.tempnam())
304 self.assertTrue(posix.tempnam(os.curdir))
305 self.assertTrue(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000306
307 def test_tmpfile(self):
308 if hasattr(posix, 'tmpfile'):
Antoine Pitroub0614612011-01-02 20:04:52 +0000309 with warnings.catch_warnings():
310 warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
311 fp = posix.tmpfile()
312 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000313
314 def test_utime(self):
315 if hasattr(posix, 'utime'):
316 now = time.time()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000317 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000318 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
319 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
320 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
321 posix.utime(test_support.TESTFN, (int(now), int(now)))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000322 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000323
Ned Deily43e10542011-06-27 23:41:53 -0700324 def _test_chflags_regular_file(self, chflags_func, target_file):
325 st = os.stat(target_file)
326 self.assertTrue(hasattr(st, 'st_flags'))
327 chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE)
328 try:
329 new_st = os.stat(target_file)
330 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
331 try:
332 fd = open(target_file, 'w+')
333 except IOError as e:
334 self.assertEqual(e.errno, errno.EPERM)
335 finally:
336 posix.chflags(target_file, st.st_flags)
Martin v. Löwis382abef2007-02-19 10:55:19 +0000337
Ned Deily43e10542011-06-27 23:41:53 -0700338 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
339 def test_chflags(self):
340 self._test_chflags_regular_file(posix.chflags, test_support.TESTFN)
341
342 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
343 def test_lchflags_regular_file(self):
344 self._test_chflags_regular_file(posix.lchflags, test_support.TESTFN)
345
346 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
347 def test_lchflags_symlink(self):
348 testfn_st = os.stat(test_support.TESTFN)
349
350 self.assertTrue(hasattr(testfn_st, 'st_flags'))
351
352 os.symlink(test_support.TESTFN, _DUMMY_SYMLINK)
353 self.teardown_files.append(_DUMMY_SYMLINK)
354 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
355
356 posix.lchflags(_DUMMY_SYMLINK,
357 dummy_symlink_st.st_flags | stat.UF_IMMUTABLE)
358 try:
359 new_testfn_st = os.stat(test_support.TESTFN)
360 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
361
362 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
363 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
364 new_dummy_symlink_st.st_flags)
365 finally:
366 posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
Martin v. Löwis382abef2007-02-19 10:55:19 +0000367
Facundo Batista5596b0c2008-06-22 13:36:20 +0000368 def test_getcwd_long_pathnames(self):
369 if hasattr(posix, 'getcwd'):
370 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
371 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000372 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000373
374 try:
375 os.mkdir(base_path)
376 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000377 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000378# Just returning nothing instead of the SkipTest exception,
Facundo Batista2694eb02008-06-22 19:35:24 +0000379# because the test results in Error in that case.
380# Is that ok?
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000381# raise unittest.SkipTest, "cannot create directory for testing"
Facundo Batista2694eb02008-06-22 19:35:24 +0000382 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000383
Facundo Batista96f3dc32008-06-22 18:23:55 +0000384 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000385 def _create_and_do_getcwd(dirname, current_path_length = 0):
386 try:
387 os.mkdir(dirname)
388 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000389 raise unittest.SkipTest, "mkdir cannot create directory sufficiently deep for getcwd test"
Facundo Batista5596b0c2008-06-22 13:36:20 +0000390
391 os.chdir(dirname)
392 try:
393 os.getcwd()
Stefan Krah182ae642010-07-13 19:17:08 +0000394 if current_path_length < 4099:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000395 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
Stefan Krah182ae642010-07-13 19:17:08 +0000396 except OSError as e:
397 expected_errno = errno.ENAMETOOLONG
398 if 'sunos' in sys.platform or 'openbsd' in sys.platform:
399 expected_errno = errno.ERANGE # Issue 9185
400 self.assertEqual(e.errno, expected_errno)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000401 finally:
402 os.chdir('..')
403 os.rmdir(dirname)
404
405 _create_and_do_getcwd(dirname)
406
407 finally:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000408 os.chdir(curdir)
R. David Murrayb0c828a2009-07-09 18:41:03 +0000409 shutil.rmtree(base_path)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000410
Antoine Pitrou8bc59fa2011-01-12 18:56:09 +0000411 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +0000412 def test_getgroups(self):
413 with os.popen('id -G') as idg:
414 groups = idg.read().strip()
415
416 if not groups:
417 raise unittest.SkipTest("need working 'id -G'")
418
Ronald Oussorenac72e582010-08-03 07:31:12 +0000419 # 'id -G' and 'os.getgroups()' should return the same
420 # groups, ignoring order and duplicates.
Antoine Pitrou8bc59fa2011-01-12 18:56:09 +0000421 # #10822 - it is implementation defined whether posix.getgroups()
422 # includes the effective gid so we include it anyway, since id -G does
Ronald Oussoren5719c2f2010-07-24 14:21:29 +0000423 self.assertEqual(
Ronald Oussorenac72e582010-08-03 07:31:12 +0000424 set([int(x) for x in groups.split()]),
Antoine Pitrou8bc59fa2011-01-12 18:56:09 +0000425 set(posix.getgroups() + [posix.getegid()]))
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +0000426
427class PosixGroupsTester(unittest.TestCase):
428
429 def setUp(self):
430 if posix.getuid() != 0:
431 raise unittest.SkipTest("not enough privileges")
432 if not hasattr(posix, 'getgroups'):
433 raise unittest.SkipTest("need posix.getgroups")
434 if sys.platform == 'darwin':
435 raise unittest.SkipTest("getgroups(2) is broken on OSX")
436 self.saved_groups = posix.getgroups()
437
438 def tearDown(self):
439 if hasattr(posix, 'setgroups'):
440 posix.setgroups(self.saved_groups)
441 elif hasattr(posix, 'initgroups'):
442 name = pwd.getpwuid(posix.getuid()).pw_name
443 posix.initgroups(name, self.saved_groups[0])
444
445 @unittest.skipUnless(hasattr(posix, 'initgroups'),
446 "test needs posix.initgroups()")
447 def test_initgroups(self):
448 # find missing group
449
Antoine Pitroudd806ce2010-09-04 17:34:12 +0000450 g = max(self.saved_groups) + 1
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +0000451 name = pwd.getpwuid(posix.getuid()).pw_name
452 posix.initgroups(name, g)
453 self.assertIn(g, posix.getgroups())
454
455 @unittest.skipUnless(hasattr(posix, 'setgroups'),
456 "test needs posix.setgroups()")
457 def test_setgroups(self):
458 for groups in [[0], range(16)]:
459 posix.setgroups(groups)
460 self.assertListEqual(groups, posix.getgroups())
461
Facundo Batista5596b0c2008-06-22 13:36:20 +0000462
Neal Norwitze241ce82003-02-17 18:17:05 +0000463def test_main():
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +0000464 test_support.run_unittest(PosixTester, PosixGroupsTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000465
466if __name__ == '__main__':
467 test_main()