Kaydet (Commit) b1ded1e5 authored tarafından Walter Dörwald's avatar Walter Dörwald

Port test_pwd.py to PyUnit. Check that getpwall() and

getpwnam()/getpwuid() return consistent data.

Change test_grp to check that getgrall() and
getgrnam()/getgrgid() return consistent data.
Add error checks similar to test_pwd.py.

Port test___all__.py to PyUnit.

From SF patch #662807.
üst 255a3d08
test_pwd
pwd.getpwall()
pwd.getpwuid()
pwd.getpwnam()
name matches uid
caught expected exception
caught expected exception
This diff is collapsed.
"""Test script for the grp module."""
# XXX This really needs some work, but what are the expected invariants?
import grp
import unittest
from test import test_support
......@@ -9,19 +7,71 @@ from test import test_support
class GroupDatabaseTestCase(unittest.TestCase):
def setUp(self):
self.groups = grp.getgrall()
def test_values(self):
entries = grp.getgrall()
def test_getgrgid(self):
entry = grp.getgrgid(self.groups[0][2])
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))
def test_getgrnam(self):
entry = grp.getgrnam(self.groups[0][0])
self.assertEqual(grp.getgrnam(e.gr_name), e)
self.assertEqual(grp.getgrgid(e.gr_gid), e)
def test_errors(self):
self.assertRaises(TypeError, grp.getgrgid)
self.assertRaises(TypeError, grp.getgrnam)
self.assertRaises(TypeError, grp.getgrall, 42)
def test_main():
test_support.run_unittest(GroupDatabaseTestCase)
# 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()
from test.test_support import verbose
import unittest
from test import test_support
import pwd
print 'pwd.getpwall()'
entries = pwd.getpwall()
class PwdTest(unittest.TestCase):
def test_values(self):
entries = pwd.getpwall()
for e in entries:
self.assertEqual(len(e), 7)
self.assertEqual(e[0], e.pw_name)
self.assert_(isinstance(e.pw_name, basestring))
self.assertEqual(e[1], e.pw_passwd)
self.assert_(isinstance(e.pw_passwd, basestring))
self.assertEqual(e[2], e.pw_uid)
self.assert_(isinstance(e.pw_uid, int))
self.assertEqual(e[3], e.pw_gid)
self.assert_(isinstance(e.pw_gid, int))
self.assertEqual(e[4], e.pw_gecos)
self.assert_(isinstance(e.pw_gecos, basestring))
self.assertEqual(e[5], e.pw_dir)
self.assert_(isinstance(e.pw_dir, basestring))
self.assertEqual(e[6], e.pw_shell)
self.assert_(isinstance(e.pw_shell, basestring))
self.assertEqual(pwd.getpwnam(e.pw_name), e)
self.assertEqual(pwd.getpwuid(e.pw_uid), e)
def test_errors(self):
self.assertRaises(TypeError, pwd.getpwuid)
self.assertRaises(TypeError, pwd.getpwnam)
self.assertRaises(TypeError, pwd.getpwall, 42)
# try to get some errors
bynames = {}
byuids = {}
for (n, p, u, g, gecos, d, s) in pwd.getpwall():
bynames[n] = u
byuids[u] = 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))
for e in entries:
name = e[0]
uid = e[2]
if verbose:
print name, uid
print 'pwd.getpwuid()'
dbuid = pwd.getpwuid(uid)
if dbuid[0] != name:
print 'Mismatch in pwd.getpwuid()'
print 'pwd.getpwnam()'
dbname = pwd.getpwnam(name)
if dbname[2] != uid:
print 'Mismatch in pwd.getpwnam()'
else:
print 'name matches uid'
break
self.assertRaises(KeyError, pwd.getpwnam, fakename)
# try to get some errors
bynames = {}
byuids = {}
for n, p, u, g, gecos, d, s in entries:
bynames[n] = u
byuids[u] = n
# Choose a non-existent uid.
fakeuid = 4127
while fakeuid in byuids:
fakeuid = (fakeuid * 3) % 0x10000
allnames = bynames.keys()
namei = 0
fakename = allnames[namei]
while bynames.has_key(fakename):
chars = map(None, fakename)
for i in range(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, pwd.getpwuid, fakeuid)
try:
pwd.getpwnam(fakename)
except KeyError:
print 'caught expected exception'
else:
print 'fakename', fakename, 'did not except pwd.getpwnam()'
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(PwdTest))
test_support.run_suite(suite)
# Choose a non-existent uid.
fakeuid = 4127
while byuids.has_key(fakeuid):
fakeuid = (fakeuid * 3) % 0x10000
if __name__ == "__main__":
test_main()
try:
pwd.getpwuid(fakeuid)
except KeyError:
print 'caught expected exception'
else:
print 'fakeuid', fakeuid, 'did not except pwd.getpwuid()'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment