home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_pwd.py < prev    next >
Text File  |  2003-12-30  |  3KB  |  93 lines

  1. import unittest
  2. from test import test_support
  3.  
  4. import pwd
  5.  
  6. class PwdTest(unittest.TestCase):
  7.  
  8.     def test_values(self):
  9.         entries = pwd.getpwall()
  10.         entriesbyname = {}
  11.         entriesbyuid = {}
  12.  
  13.         for e in entries:
  14.             self.assertEqual(len(e), 7)
  15.             self.assertEqual(e[0], e.pw_name)
  16.             self.assert_(isinstance(e.pw_name, basestring))
  17.             self.assertEqual(e[1], e.pw_passwd)
  18.             self.assert_(isinstance(e.pw_passwd, basestring))
  19.             self.assertEqual(e[2], e.pw_uid)
  20.             self.assert_(isinstance(e.pw_uid, int))
  21.             self.assertEqual(e[3], e.pw_gid)
  22.             self.assert_(isinstance(e.pw_gid, int))
  23.             self.assertEqual(e[4], e.pw_gecos)
  24.             self.assert_(isinstance(e.pw_gecos, basestring))
  25.             self.assertEqual(e[5], e.pw_dir)
  26.             self.assert_(isinstance(e.pw_dir, basestring))
  27.             self.assertEqual(e[6], e.pw_shell)
  28.             self.assert_(isinstance(e.pw_shell, basestring))
  29.  
  30.             # The following won't work, because of duplicate entries
  31.             # for one uid
  32.             #    self.assertEqual(pwd.getpwuid(e.pw_uid), e)
  33.             # instead of this collect all entries for one uid
  34.             # and check afterwards
  35.             entriesbyname.setdefault(e.pw_name, []).append(e)
  36.             entriesbyuid.setdefault(e.pw_uid, []).append(e)
  37.  
  38.         # check whether the entry returned by getpwuid()
  39.         # for each uid is among those from getpwall() for this uid
  40.         for e in entries:
  41.             self.assert_(pwd.getpwnam(e.pw_name) in entriesbyname[e.pw_name])
  42.             self.assert_(pwd.getpwuid(e.pw_uid) in entriesbyuid[e.pw_uid])
  43.  
  44.     def test_errors(self):
  45.         self.assertRaises(TypeError, pwd.getpwuid)
  46.         self.assertRaises(TypeError, pwd.getpwnam)
  47.         self.assertRaises(TypeError, pwd.getpwall, 42)
  48.  
  49.         # try to get some errors
  50.         bynames = {}
  51.         byuids = {}
  52.         for (n, p, u, g, gecos, d, s) in pwd.getpwall():
  53.             bynames[n] = u
  54.             byuids[u] = n
  55.  
  56.         allnames = bynames.keys()
  57.         namei = 0
  58.         fakename = allnames[namei]
  59.         while fakename in bynames:
  60.             chars = map(None, fakename)
  61.             for i in xrange(len(chars)):
  62.                 if chars[i] == 'z':
  63.                     chars[i] = 'A'
  64.                     break
  65.                 elif chars[i] == 'Z':
  66.                     continue
  67.                 else:
  68.                     chars[i] = chr(ord(chars[i]) + 1)
  69.                     break
  70.             else:
  71.                 namei = namei + 1
  72.                 try:
  73.                     fakename = allnames[namei]
  74.                 except IndexError:
  75.                     # should never happen... if so, just forget it
  76.                     break
  77.             fakename = ''.join(map(None, chars))
  78.  
  79.         self.assertRaises(KeyError, pwd.getpwnam, fakename)
  80.  
  81.         # Choose a non-existent uid.
  82.         fakeuid = 4127
  83.         while fakeuid in byuids:
  84.             fakeuid = (fakeuid * 3) % 0x10000
  85.  
  86.         self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
  87.  
  88. def test_main():
  89.     test_support.run_unittest(PwdTest)
  90.  
  91. if __name__ == "__main__":
  92.     test_main()
  93.