2003-04-15 19:10:33 +08:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
|
|
|
|
1996-12-12 00:53:51 +08:00
|
|
|
import pwd
|
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
class PwdTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_values(self):
|
|
|
|
entries = pwd.getpwall()
|
2003-04-22 19:05:57 +08:00
|
|
|
entriesbyname = {}
|
2003-04-15 23:39:08 +08:00
|
|
|
entriesbyuid = {}
|
2003-04-15 19:10:33 +08:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2003-04-15 23:39:08 +08:00
|
|
|
# The following won't work, because of duplicate entries
|
|
|
|
# for one uid
|
|
|
|
# self.assertEqual(pwd.getpwuid(e.pw_uid), e)
|
|
|
|
# instead of this collect all entries for one uid
|
|
|
|
# and check afterwards
|
2003-04-22 19:05:57 +08:00
|
|
|
entriesbyname.setdefault(e.pw_name, []).append(e)
|
2003-04-15 23:39:08 +08:00
|
|
|
entriesbyuid.setdefault(e.pw_uid, []).append(e)
|
|
|
|
|
|
|
|
# check whether the entry returned by getpwuid()
|
|
|
|
# for each uid is among those from getpwall() for this uid
|
|
|
|
for e in entries:
|
2005-12-13 04:53:40 +08:00
|
|
|
if not e[0] or e[0] == '+':
|
|
|
|
continue # skip NIS entries etc.
|
2003-04-22 19:05:57 +08:00
|
|
|
self.assert_(pwd.getpwnam(e.pw_name) in entriesbyname[e.pw_name])
|
2003-04-15 23:39:08 +08:00
|
|
|
self.assert_(pwd.getpwuid(e.pw_uid) in entriesbyuid[e.pw_uid])
|
2003-04-15 19:10:33 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2007-02-12 02:54:18 +08:00
|
|
|
allnames = list(bynames.keys())
|
2003-04-15 19:10:33 +08:00
|
|
|
namei = 0
|
|
|
|
fakename = allnames[namei]
|
|
|
|
while fakename in bynames:
|
|
|
|
chars = map(None, fakename)
|
2007-05-08 06:24:25 +08:00
|
|
|
for i in range(len(chars)):
|
2003-04-15 19:10:33 +08:00
|
|
|
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))
|
1996-12-12 00:53:51 +08:00
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
self.assertRaises(KeyError, pwd.getpwnam, fakename)
|
1996-12-12 00:53:51 +08:00
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
# Choose a non-existent uid.
|
|
|
|
fakeuid = 4127
|
|
|
|
while fakeuid in byuids:
|
|
|
|
fakeuid = (fakeuid * 3) % 0x10000
|
1996-12-12 00:53:51 +08:00
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
|
2000-10-24 01:22:08 +08:00
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
def test_main():
|
2003-05-02 01:45:56 +08:00
|
|
|
test_support.run_unittest(PwdTest)
|
1996-12-12 00:53:51 +08:00
|
|
|
|
2003-04-15 19:10:33 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|