2001-05-19 05:38:52 +08:00
|
|
|
"""Test script for the grp module."""
|
|
|
|
|
1996-12-19 03:36:34 +08:00
|
|
|
import grp
|
2001-05-19 05:38:52 +08:00
|
|
|
import unittest
|
2002-07-24 03:04:11 +08:00
|
|
|
from test import test_support
|
2001-05-19 05:38:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GroupDatabaseTestCase(unittest.TestCase):
|
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
def test_values(self):
|
|
|
|
entries = grp.getgrall()
|
2001-05-19 05:38:52 +08:00
|
|
|
|
2003-04-15 19:10:33 +08: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-19 05:38:52 +08:00
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
self.assertEqual(grp.getgrnam(e.gr_name), e)
|
|
|
|
self.assertEqual(grp.getgrgid(e.gr_gid), e)
|
2001-05-19 05:38:52 +08:00
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
def test_errors(self):
|
|
|
|
self.assertRaises(TypeError, grp.getgrgid)
|
|
|
|
self.assertRaises(TypeError, grp.getgrnam)
|
|
|
|
self.assertRaises(TypeError, grp.getgrall, 42)
|
2001-05-19 05:38:52 +08:00
|
|
|
|
2003-04-15 19:10:33 +08: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)
|
2001-09-21 05:33:42 +08:00
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
# 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)
|
2001-09-21 05:33:42 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|