PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_pwd.py

http://unladen-swallow.googlecode.com/
Python | 100 lines | 75 code | 12 blank | 13 comment | 20 complexity | eb041dabd6fe401da62971ef795686be MD5 | raw file
  1import sys
  2import unittest
  3from test import test_support
  4
  5import pwd
  6
  7class PwdTest(unittest.TestCase):
  8
  9    def test_values(self):
 10        entries = pwd.getpwall()
 11        entriesbyname = {}
 12        entriesbyuid = {}
 13
 14        for e in entries:
 15            self.assertEqual(len(e), 7)
 16            self.assertEqual(e[0], e.pw_name)
 17            self.assert_(isinstance(e.pw_name, basestring))
 18            self.assertEqual(e[1], e.pw_passwd)
 19            self.assert_(isinstance(e.pw_passwd, basestring))
 20            self.assertEqual(e[2], e.pw_uid)
 21            self.assert_(isinstance(e.pw_uid, int))
 22            self.assertEqual(e[3], e.pw_gid)
 23            self.assert_(isinstance(e.pw_gid, int))
 24            self.assertEqual(e[4], e.pw_gecos)
 25            self.assert_(isinstance(e.pw_gecos, basestring))
 26            self.assertEqual(e[5], e.pw_dir)
 27            self.assert_(isinstance(e.pw_dir, basestring))
 28            self.assertEqual(e[6], e.pw_shell)
 29            self.assert_(isinstance(e.pw_shell, basestring))
 30
 31            # The following won't work, because of duplicate entries
 32            # for one uid
 33            #    self.assertEqual(pwd.getpwuid(e.pw_uid), e)
 34            # instead of this collect all entries for one uid
 35            # and check afterwards
 36            entriesbyname.setdefault(e.pw_name, []).append(e)
 37            entriesbyuid.setdefault(e.pw_uid, []).append(e)
 38
 39        if len(entries) > 1000:  # Huge passwd file (NIS?) -- skip the rest
 40            return
 41
 42        # check whether the entry returned by getpwuid()
 43        # for each uid is among those from getpwall() for this uid
 44        for e in entries:
 45            if not e[0] or e[0] == '+':
 46                continue # skip NIS entries etc.
 47            self.assert_(pwd.getpwnam(e.pw_name) in entriesbyname[e.pw_name])
 48            self.assert_(pwd.getpwuid(e.pw_uid) in entriesbyuid[e.pw_uid])
 49
 50    def test_errors(self):
 51        self.assertRaises(TypeError, pwd.getpwuid)
 52        self.assertRaises(TypeError, pwd.getpwnam)
 53        self.assertRaises(TypeError, pwd.getpwall, 42)
 54
 55        # try to get some errors
 56        bynames = {}
 57        byuids = {}
 58        for (n, p, u, g, gecos, d, s) in pwd.getpwall():
 59            bynames[n] = u
 60            byuids[u] = n
 61
 62        allnames = bynames.keys()
 63        namei = 0
 64        fakename = allnames[namei]
 65        while fakename in bynames:
 66            chars = list(fakename)
 67            for i in xrange(len(chars)):
 68                if chars[i] == 'z':
 69                    chars[i] = 'A'
 70                    break
 71                elif chars[i] == 'Z':
 72                    continue
 73                else:
 74                    chars[i] = chr(ord(chars[i]) + 1)
 75                    break
 76            else:
 77                namei = namei + 1
 78                try:
 79                    fakename = allnames[namei]
 80                except IndexError:
 81                    # should never happen... if so, just forget it
 82                    break
 83            fakename = ''.join(chars)
 84
 85        self.assertRaises(KeyError, pwd.getpwnam, fakename)
 86
 87        # In some cases, byuids isn't a complete list of all users in the
 88        # system, so if we try to pick a value not in byuids (via a perturbing
 89        # loop, say), pwd.getpwuid() might still be able to find data for that
 90        # uid. Using sys.maxint may provoke the same problems, but hopefully
 91        # it will be a more repeatable failure.
 92        fakeuid = sys.maxint
 93        self.failIf(fakeuid in byuids)
 94        self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
 95
 96def test_main():
 97    test_support.run_unittest(PwdTest)
 98
 99if __name__ == "__main__":
100    test_main()