blob: b7b1a4a5f641735782533d0dabff3248f9c0fb52 [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()
Barry Warsaw3d070fb1996-12-11 16:53:51 +000011
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000012 for e in entries:
13 self.assertEqual(len(e), 7)
14 self.assertEqual(e[0], e.pw_name)
Ezio Melottie9615932010-01-24 19:26:24 +000015 self.assertIsInstance(e.pw_name, str)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000016 self.assertEqual(e[1], e.pw_passwd)
Ezio Melottie9615932010-01-24 19:26:24 +000017 self.assertIsInstance(e.pw_passwd, str)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000018 self.assertEqual(e[2], e.pw_uid)
Ezio Melottie9615932010-01-24 19:26:24 +000019 self.assertIsInstance(e.pw_uid, int)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000020 self.assertEqual(e[3], e.pw_gid)
Ezio Melottie9615932010-01-24 19:26:24 +000021 self.assertIsInstance(e.pw_gid, int)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000022 self.assertEqual(e[4], e.pw_gecos)
Ezio Melottie9615932010-01-24 19:26:24 +000023 self.assertIsInstance(e.pw_gecos, str)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000024 self.assertEqual(e[5], e.pw_dir)
Ezio Melottie9615932010-01-24 19:26:24 +000025 self.assertIsInstance(e.pw_dir, str)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000026 self.assertEqual(e[6], e.pw_shell)
Ezio Melottie9615932010-01-24 19:26:24 +000027 self.assertIsInstance(e.pw_shell, str)
Barry Warsaw3d070fb1996-12-11 16:53:51 +000028
Walter Dörwald66e1e502003-04-15 15:39:08 +000029 # 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
Zachary Ware9fe6d862013-12-08 00:20:35 -060033 # 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:
Walter Dörwaldecd2fdc2003-04-22 11:05:57 +000044 entriesbyname.setdefault(e.pw_name, []).append(e)
Walter Dörwald66e1e502003-04-15 15:39:08 +000045 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:
Fredrik Lundh0e664e12005-12-12 20:53:40 +000050 if not e[0] or e[0] == '+':
51 continue # skip NIS entries etc.
Benjamin Peterson577473f2010-01-19 00:09:57 +000052 self.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name])
53 self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid])
Fred Drake004d5e62000-10-23 17:22:08 +000054
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000055 def test_errors(self):
56 self.assertRaises(TypeError, pwd.getpwuid)
Serhiy Storchaka55e22382013-02-11 20:32:47 +020057 self.assertRaises(TypeError, pwd.getpwuid, 3.14)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000058 self.assertRaises(TypeError, pwd.getpwnam)
Serhiy Storchaka55e22382013-02-11 20:32:47 +020059 self.assertRaises(TypeError, pwd.getpwnam, 42)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000060 self.assertRaises(TypeError, pwd.getpwall, 42)
Barry Warsaw3d070fb1996-12-11 16:53:51 +000061
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000062 # 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
Barry Warsaw3d070fb1996-12-11 16:53:51 +000068
Guido van Rossum9afbb4e2007-02-11 18:54:18 +000069 allnames = list(bynames.keys())
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000070 namei = 0
71 fakename = allnames[namei]
72 while fakename in bynames:
Guido van Rossumc1f779c2007-07-03 08:25:58 +000073 chars = list(fakename)
Guido van Rossum805365e2007-05-07 22:24:25 +000074 for i in range(len(chars)):
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000075 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
Guido van Rossumc1f779c2007-07-03 08:25:58 +000090 fakename = ''.join(chars)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000091
92 self.assertRaises(KeyError, pwd.getpwnam, fakename)
93
Collin Winterb24ef192010-03-18 00:23:44 +000094 # 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.maxsize
100 self.assertNotIn(fakeuid, byuids)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +0000101 self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
102
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200103 # -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
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +0000110if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500111 unittest.main()