blob: 3b9bc205a0660dcd5dfbf8427ef1e55ef21df817 [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
Neal Norwitze241ce82003-02-17 18:17:05 +000016import unittest
17import warnings
R. David Murray59beec32009-03-30 19:04:00 +000018
Ned Deily43e10542011-06-27 23:41:53 -070019_DUMMY_SYMLINK = '%s/dummy-symlink' % os.getenv('TMPDIR', '/tmp')
R. David Murray59beec32009-03-30 19:04:00 +000020
Neal Norwitze241ce82003-02-17 18:17:05 +000021warnings.filterwarnings('ignore', '.* potential security risk .*',
22 RuntimeWarning)
23
24class PosixTester(unittest.TestCase):
25
26 def setUp(self):
27 # create empty file
Walter Dörwald21d3a322003-05-01 17:45:56 +000028 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000029 fp.close()
Ned Deily43e10542011-06-27 23:41:53 -070030 self.teardown_files = [ test_support.TESTFN ]
Neal Norwitze241ce82003-02-17 18:17:05 +000031
32 def tearDown(self):
Ned Deily43e10542011-06-27 23:41:53 -070033 for teardown_file in self.teardown_files:
34 os.unlink(teardown_file)
Neal Norwitze241ce82003-02-17 18:17:05 +000035
36 def testNoArgFunctions(self):
37 # test posix functions which take no arguments and have
38 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
39 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
Neal Norwitz71b13e82003-02-23 22:12:24 +000040 "times", "getloadavg", "tmpnam",
Neal Norwitze241ce82003-02-17 18:17:05 +000041 "getegid", "geteuid", "getgid", "getgroups",
42 "getpid", "getpgrp", "getppid", "getuid",
43 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000044
Antoine Pitroub0614612011-01-02 20:04:52 +000045 with warnings.catch_warnings():
46 warnings.filterwarnings("ignore", "", DeprecationWarning)
47 for name in NO_ARG_FUNCTIONS:
48 posix_func = getattr(posix, name, None)
49 if posix_func is not None:
50 posix_func()
51 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000052
Martin v. Löwis50ea4562009-11-27 13:56:01 +000053 if hasattr(posix, 'getresuid'):
54 def test_getresuid(self):
55 user_ids = posix.getresuid()
56 self.assertEqual(len(user_ids), 3)
57 for val in user_ids:
58 self.assertGreaterEqual(val, 0)
59
60 if hasattr(posix, 'getresgid'):
61 def test_getresgid(self):
62 group_ids = posix.getresgid()
63 self.assertEqual(len(group_ids), 3)
64 for val in group_ids:
65 self.assertGreaterEqual(val, 0)
66
67 if hasattr(posix, 'setresuid'):
68 def test_setresuid(self):
69 current_user_ids = posix.getresuid()
70 self.assertIsNone(posix.setresuid(*current_user_ids))
71 # -1 means don't change that value.
72 self.assertIsNone(posix.setresuid(-1, -1, -1))
73
74 def test_setresuid_exception(self):
75 # Don't do this test if someone is silly enough to run us as root.
76 current_user_ids = posix.getresuid()
77 if 0 not in current_user_ids:
78 new_user_ids = (current_user_ids[0]+1, -1, -1)
79 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
80
81 if hasattr(posix, 'setresgid'):
82 def test_setresgid(self):
83 current_group_ids = posix.getresgid()
84 self.assertIsNone(posix.setresgid(*current_group_ids))
85 # -1 means don't change that value.
86 self.assertIsNone(posix.setresgid(-1, -1, -1))
87
88 def test_setresgid_exception(self):
89 # Don't do this test if someone is silly enough to run us as root.
90 current_group_ids = posix.getresgid()
91 if 0 not in current_group_ids:
92 new_group_ids = (current_group_ids[0]+1, -1, -1)
93 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
94
Antoine Pitrou30b3b352009-12-02 20:37:54 +000095 @unittest.skipUnless(hasattr(posix, 'initgroups'),
96 "test needs os.initgroups()")
97 def test_initgroups(self):
98 # It takes a string and an integer; check that it raises a TypeError
99 # for other argument lists.
100 self.assertRaises(TypeError, posix.initgroups)
101 self.assertRaises(TypeError, posix.initgroups, None)
102 self.assertRaises(TypeError, posix.initgroups, 3, "foo")
103 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
104
105 # If a non-privileged user invokes it, it should fail with OSError
106 # EPERM.
107 if os.getuid() != 0:
108 name = pwd.getpwuid(posix.getuid()).pw_name
109 try:
110 posix.initgroups(name, 13)
111 except OSError as e:
Ezio Melotti2623a372010-11-21 13:34:58 +0000112 self.assertEqual(e.errno, errno.EPERM)
Antoine Pitrou30b3b352009-12-02 20:37:54 +0000113 else:
114 self.fail("Expected OSError to be raised by initgroups")
115
Neal Norwitze241ce82003-02-17 18:17:05 +0000116 def test_statvfs(self):
117 if hasattr(posix, 'statvfs'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000118 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000119
120 def test_fstatvfs(self):
121 if hasattr(posix, 'fstatvfs'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000122 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000123 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000124 self.assertTrue(posix.fstatvfs(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000125 finally:
126 fp.close()
127
128 def test_ftruncate(self):
129 if hasattr(posix, 'ftruncate'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000130 fp = open(test_support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +0000131 try:
132 # we need to have some data to truncate
133 fp.write('test')
134 fp.flush()
135 posix.ftruncate(fp.fileno(), 0)
136 finally:
137 fp.close()
138
139 def test_dup(self):
140 if hasattr(posix, 'dup'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000141 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000142 try:
143 fd = posix.dup(fp.fileno())
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000144 self.assertIsInstance(fd, int)
Neal Norwitze241ce82003-02-17 18:17:05 +0000145 os.close(fd)
146 finally:
147 fp.close()
148
Skip Montanaro94785ef2006-04-20 01:29:48 +0000149 def test_confstr(self):
150 if hasattr(posix, 'confstr'):
151 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
152 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
153
Neal Norwitze241ce82003-02-17 18:17:05 +0000154 def test_dup2(self):
155 if hasattr(posix, 'dup2'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000156 fp1 = open(test_support.TESTFN)
157 fp2 = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000158 try:
159 posix.dup2(fp1.fileno(), fp2.fileno())
160 finally:
161 fp1.close()
162 fp2.close()
163
164 def fdopen_helper(self, *args):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000165 fd = os.open(test_support.TESTFN, os.O_RDONLY)
Neal Norwitze241ce82003-02-17 18:17:05 +0000166 fp2 = posix.fdopen(fd, *args)
167 fp2.close()
168
169 def test_fdopen(self):
170 if hasattr(posix, 'fdopen'):
171 self.fdopen_helper()
172 self.fdopen_helper('r')
173 self.fdopen_helper('r', 100)
174
Skip Montanaro98470002005-06-17 01:14:49 +0000175 def test_osexlock(self):
176 if hasattr(posix, "O_EXLOCK"):
177 fd = os.open(test_support.TESTFN,
178 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
179 self.assertRaises(OSError, os.open, test_support.TESTFN,
180 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
181 os.close(fd)
182
183 if hasattr(posix, "O_SHLOCK"):
184 fd = os.open(test_support.TESTFN,
185 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
186 self.assertRaises(OSError, os.open, test_support.TESTFN,
187 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
188 os.close(fd)
189
190 def test_osshlock(self):
191 if hasattr(posix, "O_SHLOCK"):
192 fd1 = os.open(test_support.TESTFN,
193 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
194 fd2 = os.open(test_support.TESTFN,
195 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
196 os.close(fd2)
197 os.close(fd1)
198
199 if hasattr(posix, "O_EXLOCK"):
200 fd = os.open(test_support.TESTFN,
201 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
202 self.assertRaises(OSError, os.open, test_support.TESTFN,
203 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
204 os.close(fd)
205
Neal Norwitze241ce82003-02-17 18:17:05 +0000206 def test_fstat(self):
207 if hasattr(posix, 'fstat'):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000208 fp = open(test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000209 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000210 self.assertTrue(posix.fstat(fp.fileno()))
Neal Norwitze241ce82003-02-17 18:17:05 +0000211 finally:
212 fp.close()
213
214 def test_stat(self):
215 if hasattr(posix, 'stat'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000216 self.assertTrue(posix.stat(test_support.TESTFN))
Neal Norwitze241ce82003-02-17 18:17:05 +0000217
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000218 def _test_all_chown_common(self, chown_func, first_param):
219 """Common code for chown, fchown and lchown tests."""
220 if os.getuid() == 0:
221 try:
222 # Many linux distros have a nfsnobody user as MAX_UID-2
223 # that makes a good test case for signedness issues.
224 # http://bugs.python.org/issue1747858
225 # This part of the test only runs when run as root.
226 # Only scary people run their tests as root.
227 ent = pwd.getpwnam('nfsnobody')
228 chown_func(first_param, ent.pw_uid, ent.pw_gid)
229 except KeyError:
230 pass
231 else:
232 # non-root cannot chown to root, raises OSError
233 self.assertRaises(OSError, chown_func,
234 first_param, 0, 0)
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000235
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000236 # test a successful chown call
237 chown_func(first_param, os.getuid(), os.getgid())
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000238
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000239 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
240 def test_chown(self):
241 # raise an OSError if the file does not exist
242 os.unlink(test_support.TESTFN)
243 self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
244
245 # re-create the file
246 open(test_support.TESTFN, 'w').close()
247 self._test_all_chown_common(posix.chown, test_support.TESTFN)
248
249 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
250 def test_fchown(self):
251 os.unlink(test_support.TESTFN)
252
253 # re-create the file
254 test_file = open(test_support.TESTFN, 'w')
255 try:
256 fd = test_file.fileno()
257 self._test_all_chown_common(posix.fchown, fd)
258 finally:
259 test_file.close()
260
261 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
262 def test_lchown(self):
263 os.unlink(test_support.TESTFN)
264 # create a symlink
Ned Deily43e10542011-06-27 23:41:53 -0700265 os.symlink(_DUMMY_SYMLINK, test_support.TESTFN)
Gregory P. Smith9f12d462009-12-23 09:31:11 +0000266 self._test_all_chown_common(posix.lchown, test_support.TESTFN)
Gregory P. Smithf48da8f2008-03-18 19:05:32 +0000267
Neal Norwitze241ce82003-02-17 18:17:05 +0000268 def test_chdir(self):
269 if hasattr(posix, 'chdir'):
270 posix.chdir(os.curdir)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000271 self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000272
273 def test_lsdir(self):
274 if hasattr(posix, 'lsdir'):
Ezio Melottiaa980582010-01-23 23:04:36 +0000275 self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000276
277 def test_access(self):
278 if hasattr(posix, 'access'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000279 self.assertTrue(posix.access(test_support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000280
281 def test_umask(self):
282 if hasattr(posix, 'umask'):
283 old_mask = posix.umask(0)
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000284 self.assertIsInstance(old_mask, int)
Neal Norwitze241ce82003-02-17 18:17:05 +0000285 posix.umask(old_mask)
286
287 def test_strerror(self):
288 if hasattr(posix, 'strerror'):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000289 self.assertTrue(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000290
291 def test_pipe(self):
292 if hasattr(posix, 'pipe'):
Antoine Pitroubba8f2d2010-04-10 23:32:12 +0000293 reader, writer = posix.pipe()
294 os.close(reader)
295 os.close(writer)
Neal Norwitze241ce82003-02-17 18:17:05 +0000296
297 def test_tempnam(self):
298 if hasattr(posix, 'tempnam'):
Antoine Pitroub0614612011-01-02 20:04:52 +0000299 with warnings.catch_warnings():
300 warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
301 self.assertTrue(posix.tempnam())
302 self.assertTrue(posix.tempnam(os.curdir))
303 self.assertTrue(posix.tempnam(os.curdir, 'blah'))
Neal Norwitze241ce82003-02-17 18:17:05 +0000304
305 def test_tmpfile(self):
306 if hasattr(posix, 'tmpfile'):
Antoine Pitroub0614612011-01-02 20:04:52 +0000307 with warnings.catch_warnings():
308 warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
309 fp = posix.tmpfile()
310 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000311
312 def test_utime(self):
313 if hasattr(posix, 'utime'):
314 now = time.time()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000315 posix.utime(test_support.TESTFN, None)
Neal Norwitzc28e7ad2004-06-06 20:27:05 +0000316 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
317 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
318 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
319 posix.utime(test_support.TESTFN, (int(now), int(now)))
Walter Dörwald21d3a322003-05-01 17:45:56 +0000320 posix.utime(test_support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000321
Ned Deily43e10542011-06-27 23:41:53 -0700322 def _test_chflags_regular_file(self, chflags_func, target_file):
323 st = os.stat(target_file)
324 self.assertTrue(hasattr(st, 'st_flags'))
325 chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE)
326 try:
327 new_st = os.stat(target_file)
328 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
329 try:
330 fd = open(target_file, 'w+')
331 except IOError as e:
332 self.assertEqual(e.errno, errno.EPERM)
333 finally:
334 posix.chflags(target_file, st.st_flags)
Martin v. Löwis382abef2007-02-19 10:55:19 +0000335
Ned Deily43e10542011-06-27 23:41:53 -0700336 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
337 def test_chflags(self):
338 self._test_chflags_regular_file(posix.chflags, test_support.TESTFN)
339
340 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
341 def test_lchflags_regular_file(self):
342 self._test_chflags_regular_file(posix.lchflags, test_support.TESTFN)
343
344 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
345 def test_lchflags_symlink(self):
346 testfn_st = os.stat(test_support.TESTFN)
347
348 self.assertTrue(hasattr(testfn_st, 'st_flags'))
349
350 os.symlink(test_support.TESTFN, _DUMMY_SYMLINK)
351 self.teardown_files.append(_DUMMY_SYMLINK)
352 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
353
354 posix.lchflags(_DUMMY_SYMLINK,
355 dummy_symlink_st.st_flags | stat.UF_IMMUTABLE)
356 try:
357 new_testfn_st = os.stat(test_support.TESTFN)
358 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
359
360 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
361 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
362 new_dummy_symlink_st.st_flags)
363 finally:
364 posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
Martin v. Löwis382abef2007-02-19 10:55:19 +0000365
Facundo Batista5596b0c2008-06-22 13:36:20 +0000366 def test_getcwd_long_pathnames(self):
367 if hasattr(posix, 'getcwd'):
368 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
369 curdir = os.getcwd()
Facundo Batista96f3dc32008-06-22 18:23:55 +0000370 base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
Facundo Batista5596b0c2008-06-22 13:36:20 +0000371
372 try:
373 os.mkdir(base_path)
374 os.chdir(base_path)
Facundo Batista96f3dc32008-06-22 18:23:55 +0000375 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000376# Just returning nothing instead of the SkipTest exception,
Facundo Batista2694eb02008-06-22 19:35:24 +0000377# because the test results in Error in that case.
378# Is that ok?
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000379# raise unittest.SkipTest, "cannot create directory for testing"
Facundo Batista2694eb02008-06-22 19:35:24 +0000380 return
Facundo Batista5596b0c2008-06-22 13:36:20 +0000381
Facundo Batista96f3dc32008-06-22 18:23:55 +0000382 try:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000383 def _create_and_do_getcwd(dirname, current_path_length = 0):
384 try:
385 os.mkdir(dirname)
386 except:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000387 raise unittest.SkipTest, "mkdir cannot create directory sufficiently deep for getcwd test"
Facundo Batista5596b0c2008-06-22 13:36:20 +0000388
389 os.chdir(dirname)
390 try:
391 os.getcwd()
Stefan Krah182ae642010-07-13 19:17:08 +0000392 if current_path_length < 4099:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000393 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
Stefan Krah182ae642010-07-13 19:17:08 +0000394 except OSError as e:
395 expected_errno = errno.ENAMETOOLONG
396 if 'sunos' in sys.platform or 'openbsd' in sys.platform:
397 expected_errno = errno.ERANGE # Issue 9185
398 self.assertEqual(e.errno, expected_errno)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000399 finally:
400 os.chdir('..')
401 os.rmdir(dirname)
402
403 _create_and_do_getcwd(dirname)
404
405 finally:
Facundo Batista5596b0c2008-06-22 13:36:20 +0000406 os.chdir(curdir)
R. David Murrayb0c828a2009-07-09 18:41:03 +0000407 shutil.rmtree(base_path)
Facundo Batista5596b0c2008-06-22 13:36:20 +0000408
Antoine Pitrou8bc59fa2011-01-12 18:56:09 +0000409 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +0000410 def test_getgroups(self):
411 with os.popen('id -G') as idg:
412 groups = idg.read().strip()
413
414 if not groups:
415 raise unittest.SkipTest("need working 'id -G'")
416
Ronald Oussorenac72e582010-08-03 07:31:12 +0000417 # 'id -G' and 'os.getgroups()' should return the same
418 # groups, ignoring order and duplicates.
Antoine Pitrou8bc59fa2011-01-12 18:56:09 +0000419 # #10822 - it is implementation defined whether posix.getgroups()
420 # includes the effective gid so we include it anyway, since id -G does
Ronald Oussoren5719c2f2010-07-24 14:21:29 +0000421 self.assertEqual(
Ronald Oussorenac72e582010-08-03 07:31:12 +0000422 set([int(x) for x in groups.split()]),
Antoine Pitrou8bc59fa2011-01-12 18:56:09 +0000423 set(posix.getgroups() + [posix.getegid()]))
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +0000424
425class PosixGroupsTester(unittest.TestCase):
426
427 def setUp(self):
428 if posix.getuid() != 0:
429 raise unittest.SkipTest("not enough privileges")
430 if not hasattr(posix, 'getgroups'):
431 raise unittest.SkipTest("need posix.getgroups")
432 if sys.platform == 'darwin':
433 raise unittest.SkipTest("getgroups(2) is broken on OSX")
434 self.saved_groups = posix.getgroups()
435
436 def tearDown(self):
437 if hasattr(posix, 'setgroups'):
438 posix.setgroups(self.saved_groups)
439 elif hasattr(posix, 'initgroups'):
440 name = pwd.getpwuid(posix.getuid()).pw_name
441 posix.initgroups(name, self.saved_groups[0])
442
443 @unittest.skipUnless(hasattr(posix, 'initgroups'),
444 "test needs posix.initgroups()")
445 def test_initgroups(self):
446 # find missing group
447
Antoine Pitroudd806ce2010-09-04 17:34:12 +0000448 g = max(self.saved_groups) + 1
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +0000449 name = pwd.getpwuid(posix.getuid()).pw_name
450 posix.initgroups(name, g)
451 self.assertIn(g, posix.getgroups())
452
453 @unittest.skipUnless(hasattr(posix, 'setgroups'),
454 "test needs posix.setgroups()")
455 def test_setgroups(self):
456 for groups in [[0], range(16)]:
457 posix.setgroups(groups)
458 self.assertListEqual(groups, posix.getgroups())
459
Facundo Batista5596b0c2008-06-22 13:36:20 +0000460
Neal Norwitze241ce82003-02-17 18:17:05 +0000461def test_main():
Ronald Oussoren9e7ffae2010-07-24 09:46:41 +0000462 test_support.run_unittest(PosixTester, PosixGroupsTester)
Neal Norwitze241ce82003-02-17 18:17:05 +0000463
464if __name__ == '__main__':
465 test_main()