summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_posix.py
diff options
context:
space:
mode:
authorRonald Oussoren <ronaldoussoren@mac.com>2010-07-23 15:46:03 (GMT)
committerRonald Oussoren <ronaldoussoren@mac.com>2010-07-23 15:46:03 (GMT)
commit47076f7897427d34a8025c1b4f330d1312facc83 (patch)
tree147962d4e226ee25121a12e5205d133d9f198117 /Lib/test/test_posix.py
parent7180d4878114f8e427764b29a781b7c0a7e1f2dc (diff)
downloadcpython-47076f7897427d34a8025c1b4f330d1312facc83.zip
cpython-47076f7897427d34a8025c1b4f330d1312facc83.tar.gz
cpython-47076f7897427d34a8025c1b4f330d1312facc83.tar.bz2
This fixes issue7900 by adding code that deals
with the fact that getgroups(2) might return more that MAX_GROUPS on OSX. See the issue (and python-dev archives) for the gory details. Summarized: OSX behaves rather oddly and Apple says this is intentional.
Diffstat (limited to 'Lib/test/test_posix.py')
-rw-r--r--Lib/test/test_posix.py53
1 files changed, 52 insertions, 1 deletions
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 78f7592..31459ff 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -5,6 +5,7 @@ from test import support
# Skip these tests if there is no posix module.
posix = support.import_module('posix')
+import sys
import time
import os
import pwd
@@ -273,9 +274,59 @@ class PosixTester(unittest.TestCase):
os.chdir(curdir)
support.rmtree(base_path)
+ def test_getgroups(self):
+ with os.popen('id -G') as idg:
+ groups = idg.read().strip()
+
+ if not groups:
+ raise unittest.SkipTest("need working 'id -G'")
+
+ self.assertEqual([int(x) for x in groups.split()], posix.getgroups())
+
+class PosixGroupsTester(unittest.TestCase):
+
+ def setUp(self):
+ if posix.getuid() != 0:
+ raise unittest.SkipTest("not enough privileges")
+ if not hasattr(posix, 'getgroups'):
+ raise unittest.SkipTest("need posix.getgroups")
+ if sys.platform == 'darwin':
+ raise unittest.SkipTest("getgroups(2) is broken on OSX")
+ self.saved_groups = posix.getgroups()
+
+ def tearDown(self):
+ if hasattr(posix, 'setgroups'):
+ posix.setgroups(self.saved_groups)
+ elif hasattr(posix, 'initgroups'):
+ name = pwd.getpwuid(posix.getuid()).pw_name
+ posix.initgroups(name, self.saved_groups[0])
+
+ @unittest.skipUnless(hasattr(posix, 'initgroups'),
+ "test needs posix.initgroups()")
+ def test_initgroups(self):
+ # find missing group
+
+ groups = sorted(self.saved_groups)
+ for g1,g2 in zip(groups[:-1], groups[1:]):
+ g = g1 + 1
+ if g < g2:
+ break
+ else:
+ g = g2 + 1
+ name = pwd.getpwuid(posix.getuid()).pw_name
+ posix.initgroups(name, g)
+ self.assertIn(g, posix.getgroups())
+
+ @unittest.skipUnless(hasattr(posix, 'setgroups'),
+ "test needs posix.setgroups()")
+ def test_setgroups(self):
+ for groups in [[0], range(16)]:
+ posix.setgroups(groups)
+ self.assertListEqual(groups, posix.getgroups())
+
def test_main():
- support.run_unittest(PosixTester)
+ support.run_unittest(PosixTester, PosixGroupsTester)
if __name__ == '__main__':
test_main()