cpython/Lib/test/test_grp.py

78 lines
2.3 KiB
Python
Raw Normal View History

2001-05-18 18:38:52 -03:00
"""Test script for the grp module."""
import grp
2001-05-18 18:38:52 -03:00
import unittest
from test import test_support
2001-05-18 18:38:52 -03:00
class GroupDatabaseTestCase(unittest.TestCase):
def test_values(self):
entries = grp.getgrall()
2001-05-18 18:38:52 -03:00
for e in entries:
self.assertEqual(len(e), 4)
self.assertEqual(e[0], e.gr_name)
self.assert_(isinstance(e.gr_name, basestring))
self.assertEqual(e[1], e.gr_passwd)
self.assert_(isinstance(e.gr_passwd, basestring))
self.assertEqual(e[2], e.gr_gid)
self.assert_(isinstance(e.gr_gid, int))
self.assertEqual(e[3], e.gr_mem)
self.assert_(isinstance(e.gr_mem, list))
2001-05-18 18:38:52 -03:00
self.assertEqual(grp.getgrnam(e.gr_name), e)
self.assertEqual(grp.getgrgid(e.gr_gid), e)
2001-05-18 18:38:52 -03:00
def test_errors(self):
self.assertRaises(TypeError, grp.getgrgid)
self.assertRaises(TypeError, grp.getgrnam)
self.assertRaises(TypeError, grp.getgrall, 42)
2001-05-18 18:38:52 -03:00
# try to get some errors
bynames = {}
bygids = {}
for (n, p, g, mem) in grp.getgrall():
bynames[n] = g
bygids[g] = n
allnames = bynames.keys()
namei = 0
fakename = allnames[namei]
while fakename in bynames:
chars = map(None, fakename)
for i in xrange(len(chars)):
if chars[i] == 'z':
chars[i] = 'A'
break
elif chars[i] == 'Z':
continue
else:
chars[i] = chr(ord(chars[i]) + 1)
break
else:
namei = namei + 1
try:
fakename = allnames[namei]
except IndexError:
# should never happen... if so, just forget it
break
fakename = ''.join(map(None, chars))
self.assertRaises(KeyError, grp.getgrnam, fakename)
# Choose a non-existent gid.
fakegid = 4127
while fakegid in bygids:
fakegid = (fakegid * 3) % 0x10000
self.assertRaises(KeyError, grp.getgrgid, fakegid)
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(GroupDatabaseTestCase))
test_support.run_suite(suite)
if __name__ == "__main__":
test_main()