blob: ae1c8feca6a1cbe8e919c3996d0e8a3131e94efe [file] [log] [blame]
Collin Winterb24ef192010-03-18 00:23:44 +00001import sys
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +00002import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +00004
R. David Murraya21e4ca2009-03-31 23:16:50 +00005pwd = support.import_module('pwd')
Barry Warsaw3d070fb1996-12-11 16:53:51 +00006
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +00007class PwdTest(unittest.TestCase):
Barry Warsaw3d070fb1996-12-11 16:53:51 +00008
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +00009 def test_values(self):
10 entries = pwd.getpwall()
Walter Dörwaldecd2fdc2003-04-22 11:05:57 +000011 entriesbyname = {}
Walter Dörwald66e1e502003-04-15 15:39:08 +000012 entriesbyuid = {}
Barry Warsaw3d070fb1996-12-11 16:53:51 +000013
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000014 for e in entries:
15 self.assertEqual(len(e), 7)
16 self.assertEqual(e[0], e.pw_name)
Ezio Melottie9615932010-01-24 19:26:24 +000017 self.assertIsInstance(e.pw_name, str)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000018 self.assertEqual(e[1], e.pw_passwd)
Ezio Melottie9615932010-01-24 19:26:24 +000019 self.assertIsInstance(e.pw_passwd, str)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000020 self.assertEqual(e[2], e.pw_uid)
Ezio Melottie9615932010-01-24 19:26:24 +000021 self.assertIsInstance(e.pw_uid, int)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000022 self.assertEqual(e[3], e.pw_gid)
Ezio Melottie9615932010-01-24 19:26:24 +000023 self.assertIsInstance(e.pw_gid, int)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000024 self.assertEqual(e[4], e.pw_gecos)
Ezio Melottie9615932010-01-24 19:26:24 +000025 self.assertIsInstance(e.pw_gecos, str)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000026 self.assertEqual(e[5], e.pw_dir)
Ezio Melottie9615932010-01-24 19:26:24 +000027 self.assertIsInstance(e.pw_dir, str)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000028 self.assertEqual(e[6], e.pw_shell)
Ezio Melottie9615932010-01-24 19:26:24 +000029 self.assertIsInstance(e.pw_shell, str)
Barry Warsaw3d070fb1996-12-11 16:53:51 +000030
Walter Dörwald66e1e502003-04-15 15:39:08 +000031 # 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
Walter Dörwaldecd2fdc2003-04-22 11:05:57 +000036 entriesbyname.setdefault(e.pw_name, []).append(e)
Walter Dörwald66e1e502003-04-15 15:39:08 +000037 entriesbyuid.setdefault(e.pw_uid, []).append(e)
38
Christian Heimes1af737c2008-01-23 08:24:23 +000039 if len(entries) > 1000: # Huge passwd file (NIS?) -- skip the rest
40 return
41
Walter Dörwald66e1e502003-04-15 15:39:08 +000042 # check whether the entry returned by getpwuid()
43 # for each uid is among those from getpwall() for this uid
44 for e in entries:
Fredrik Lundh0e664e12005-12-12 20:53:40 +000045 if not e[0] or e[0] == '+':
46 continue # skip NIS entries etc.
Benjamin Peterson577473f2010-01-19 00:09:57 +000047 self.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name])
48 self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid])
Fred Drake004d5e62000-10-23 17:22:08 +000049
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000050 def test_errors(self):
51 self.assertRaises(TypeError, pwd.getpwuid)
52 self.assertRaises(TypeError, pwd.getpwnam)
53 self.assertRaises(TypeError, pwd.getpwall, 42)
Barry Warsaw3d070fb1996-12-11 16:53:51 +000054
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000055 # 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
Barry Warsaw3d070fb1996-12-11 16:53:51 +000061
Guido van Rossum9afbb4e2007-02-11 18:54:18 +000062 allnames = list(bynames.keys())
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000063 namei = 0
64 fakename = allnames[namei]
65 while fakename in bynames:
Guido van Rossumc1f779c2007-07-03 08:25:58 +000066 chars = list(fakename)
Guido van Rossum805365e2007-05-07 22:24:25 +000067 for i in range(len(chars)):
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000068 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
Guido van Rossumc1f779c2007-07-03 08:25:58 +000083 fakename = ''.join(chars)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000084
85 self.assertRaises(KeyError, pwd.getpwnam, fakename)
86
Collin Winterb24ef192010-03-18 00:23:44 +000087 # 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.maxsize
93 self.assertNotIn(fakeuid, byuids)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000094 self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
95
96def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000097 support.run_unittest(PwdTest)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000098
99if __name__ == "__main__":
100 test_main()