2010-03-17 21:10:34 -03:00
|
|
|
import sys
|
2003-04-15 08:10:33 -03:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
|
|
|
|
2009-03-30 16:04:00 -03:00
|
|
|
pwd = test_support.import_module('pwd')
|
1996-12-11 12:53:51 -04:00
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
class PwdTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_values(self):
|
|
|
|
entries = pwd.getpwall()
|
2003-04-22 08:05:57 -03:00
|
|
|
entriesbyname = {}
|
2003-04-15 12:39:08 -03:00
|
|
|
entriesbyuid = {}
|
2003-04-15 08:10:33 -03:00
|
|
|
|
|
|
|
for e in entries:
|
|
|
|
self.assertEqual(len(e), 7)
|
|
|
|
self.assertEqual(e[0], e.pw_name)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(e.pw_name, basestring)
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[1], e.pw_passwd)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(e.pw_passwd, basestring)
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[2], e.pw_uid)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(e.pw_uid, int)
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[3], e.pw_gid)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(e.pw_gid, int)
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[4], e.pw_gecos)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(e.pw_gecos, basestring)
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[5], e.pw_dir)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(e.pw_dir, basestring)
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertEqual(e[6], e.pw_shell)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(e.pw_shell, basestring)
|
2003-04-15 08:10:33 -03:00
|
|
|
|
2003-04-15 12:39:08 -03: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 08:05:57 -03:00
|
|
|
entriesbyname.setdefault(e.pw_name, []).append(e)
|
2003-04-15 12:39:08 -03:00
|
|
|
entriesbyuid.setdefault(e.pw_uid, []).append(e)
|
|
|
|
|
2008-01-22 21:18:27 -04:00
|
|
|
if len(entries) > 1000: # Huge passwd file (NIS?) -- skip the rest
|
|
|
|
return
|
|
|
|
|
2003-04-15 12:39:08 -03:00
|
|
|
# check whether the entry returned by getpwuid()
|
|
|
|
# for each uid is among those from getpwall() for this uid
|
|
|
|
for e in entries:
|
2005-12-12 16:53:40 -04:00
|
|
|
if not e[0] or e[0] == '+':
|
|
|
|
continue # skip NIS entries etc.
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name])
|
|
|
|
self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid])
|
2003-04-15 08:10:33 -03:00
|
|
|
|
|
|
|
def test_errors(self):
|
|
|
|
self.assertRaises(TypeError, pwd.getpwuid)
|
2013-02-12 03:27:53 -04:00
|
|
|
self.assertRaises(TypeError, pwd.getpwuid, 3.14)
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertRaises(TypeError, pwd.getpwnam)
|
2013-02-12 03:27:53 -04:00
|
|
|
self.assertRaises(TypeError, pwd.getpwnam, 42)
|
2003-04-15 08:10:33 -03:00
|
|
|
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:
|
2008-07-18 16:06:13 -03:00
|
|
|
chars = list(fakename)
|
2003-04-15 08:10:33 -03:00
|
|
|
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
|
2008-07-18 16:06:13 -03:00
|
|
|
fakename = ''.join(chars)
|
1996-12-11 12:53:51 -04:00
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertRaises(KeyError, pwd.getpwnam, fakename)
|
1996-12-11 12:53:51 -04:00
|
|
|
|
2010-03-17 21:10:34 -03:00
|
|
|
# In some cases, byuids isn't a complete list of all users in the
|
|
|
|
# system, so if we try to pick a value not in byuids (via a perturbing
|
|
|
|
# loop, say), pwd.getpwuid() might still be able to find data for that
|
|
|
|
# uid. Using sys.maxint may provoke the same problems, but hopefully
|
|
|
|
# it will be a more repeatable failure.
|
|
|
|
fakeuid = sys.maxint
|
|
|
|
self.assertNotIn(fakeuid, byuids)
|
2003-04-15 08:10:33 -03:00
|
|
|
self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
|
2000-10-23 14:22:08 -03:00
|
|
|
|
2013-02-12 03:27:53 -04:00
|
|
|
# -1 shouldn't be a valid uid because it has a special meaning in many
|
|
|
|
# uid-related functions
|
|
|
|
self.assertRaises(KeyError, pwd.getpwuid, -1)
|
|
|
|
# should be out of uid_t range
|
|
|
|
self.assertRaises(KeyError, pwd.getpwuid, 2**128)
|
|
|
|
self.assertRaises(KeyError, pwd.getpwuid, -2**128)
|
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(PwdTest)
|
1996-12-11 12:53:51 -04:00
|
|
|
|
2003-04-15 08:10:33 -03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|