Home | History | Annotate | Download | only in test
      1 import sys
      2 import unittest
      3 from test import test_support
      4 
      5 pwd = test_support.import_module('pwd')
      6 
      7 class 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.assertIsInstance(e.pw_name, basestring)
     18             self.assertEqual(e[1], e.pw_passwd)
     19             self.assertIsInstance(e.pw_passwd, basestring)
     20             self.assertEqual(e[2], e.pw_uid)
     21             self.assertIsInstance(e.pw_uid, (int, long))
     22             self.assertEqual(e[3], e.pw_gid)
     23             self.assertIsInstance(e.pw_gid, (int, long))
     24             self.assertEqual(e[4], e.pw_gecos)
     25             self.assertIsInstance(e.pw_gecos, basestring)
     26             self.assertEqual(e[5], e.pw_dir)
     27             self.assertIsInstance(e.pw_dir, basestring)
     28             self.assertEqual(e[6], e.pw_shell)
     29             self.assertIsInstance(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.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name])
     48             self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid])
     49 
     50     def test_errors(self):
     51         self.assertRaises(TypeError, pwd.getpwuid)
     52         self.assertRaises(TypeError, pwd.getpwuid, 3.14)
     53         self.assertRaises(TypeError, pwd.getpwnam)
     54         self.assertRaises(TypeError, pwd.getpwnam, 42)
     55         self.assertRaises(TypeError, pwd.getpwall, 42)
     56 
     57         # try to get some errors
     58         bynames = {}
     59         byuids = {}
     60         for (n, p, u, g, gecos, d, s) in pwd.getpwall():
     61             bynames[n] = u
     62             byuids[u] = n
     63 
     64         allnames = bynames.keys()
     65         namei = 0
     66         fakename = allnames[namei]
     67         while fakename in bynames:
     68             chars = list(fakename)
     69             for i in xrange(len(chars)):
     70                 if chars[i] == 'z':
     71                     chars[i] = 'A'
     72                     break
     73                 elif chars[i] == 'Z':
     74                     continue
     75                 else:
     76                     chars[i] = chr(ord(chars[i]) + 1)
     77                     break
     78             else:
     79                 namei = namei + 1
     80                 try:
     81                     fakename = allnames[namei]
     82                 except IndexError:
     83                     # should never happen... if so, just forget it
     84                     break
     85             fakename = ''.join(chars)
     86 
     87         self.assertRaises(KeyError, pwd.getpwnam, fakename)
     88 
     89         # In some cases, byuids isn't a complete list of all users in the
     90         # system, so if we try to pick a value not in byuids (via a perturbing
     91         # loop, say), pwd.getpwuid() might still be able to find data for that
     92         # uid. Using sys.maxint may provoke the same problems, but hopefully
     93         # it will be a more repeatable failure.
     94         fakeuid = sys.maxint
     95         self.assertNotIn(fakeuid, byuids)
     96         self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
     97 
     98         # -1 shouldn't be a valid uid because it has a special meaning in many
     99         # uid-related functions
    100         self.assertRaises(KeyError, pwd.getpwuid, -1)
    101         # should be out of uid_t range
    102         self.assertRaises(KeyError, pwd.getpwuid, 2**128)
    103         self.assertRaises(KeyError, pwd.getpwuid, -2**128)
    104 
    105 def test_main():
    106     test_support.run_unittest(PwdTest)
    107 
    108 if __name__ == "__main__":
    109     test_main()
    110