2001-05-18 18:38:52 -03:00
|
|
|
"""Test script for the grp module."""
|
|
|
|
|
1996-12-18 15:36:34 -04:00
|
|
|
import grp
|
2001-05-18 18:38:52 -03:00
|
|
|
import unittest
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2001-05-18 18:38:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
class GroupDatabaseTestCase(unittest.TestCase):
|
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
def test_values(self):
|
|
|
|
entries = grp.getgrall()
|
2003-04-22 08:05:57 -03:00
|
|
|
entriesbyname = {}
|
2003-04-15 12:59:36 -03:00
|
|
|
entriesbygid = {}
|
2001-05-18 18:38:52 -03:00
|
|
|
|
2003-04-15 08:10:33 -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
|
|
|
|
2003-04-15 12:59:36 -03:00
|
|
|
# The following won't work, because of duplicate entries
|
|
|
|
# for one gid
|
|
|
|
# self.assertEqual(grp.getgrgid(e.gr_gid), e)
|
2003-04-22 08:05:57 -03:00
|
|
|
# instead of this collect all entries for one gid/name
|
2003-04-15 12:59:36 -03:00
|
|
|
# and check afterwards
|
2003-04-22 08:05:57 -03:00
|
|
|
entriesbyname.setdefault(e.gr_name, []).append(e)
|
2003-04-15 12:59:36 -03:00
|
|
|
entriesbygid.setdefault(e.gr_gid, []).append(e)
|
|
|
|
|
|
|
|
# check whether the entry returned by getgrgid()
|
|
|
|
# for each uid is among those from getgrall() for this uid
|
|
|
|
for e in entries:
|
|
|
|
self.assert_(grp.getgrgid(e.gr_gid) in entriesbygid[e.gr_gid])
|
2003-04-22 08:05:57 -03:00
|
|
|
self.assert_(grp.getgrnam(e.gr_name) in entriesbyname[e.gr_name])
|
2001-05-18 18:38:52 -03:00
|
|
|
|
2003-04-15 08:10:33 -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
|
|
|
|
2003-04-15 08:10:33 -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)
|
2001-09-20 18:33:42 -03:00
|
|
|
|
2003-04-15 08:10:33 -03: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-20 18:33:42 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|