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