Collin Winter | b24ef19 | 2010-03-18 00:23:44 +0000 | [diff] [blame] | 1 | import sys |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 2 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 4 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 5 | pwd = support.import_module('pwd') |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 6 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 7 | class PwdTest(unittest.TestCase): |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 8 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 9 | def test_values(self): |
| 10 | entries = pwd.getpwall() |
Walter Dörwald | ecd2fdc | 2003-04-22 11:05:57 +0000 | [diff] [blame] | 11 | entriesbyname = {} |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 12 | entriesbyuid = {} |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 13 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 14 | for e in entries: |
| 15 | self.assertEqual(len(e), 7) |
| 16 | self.assertEqual(e[0], e.pw_name) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 17 | self.assertIsInstance(e.pw_name, str) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 18 | self.assertEqual(e[1], e.pw_passwd) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 19 | self.assertIsInstance(e.pw_passwd, str) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 20 | self.assertEqual(e[2], e.pw_uid) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 21 | self.assertIsInstance(e.pw_uid, int) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 22 | self.assertEqual(e[3], e.pw_gid) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 23 | self.assertIsInstance(e.pw_gid, int) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 24 | self.assertEqual(e[4], e.pw_gecos) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 25 | self.assertIsInstance(e.pw_gecos, str) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 26 | self.assertEqual(e[5], e.pw_dir) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 27 | self.assertIsInstance(e.pw_dir, str) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 28 | self.assertEqual(e[6], e.pw_shell) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 29 | self.assertIsInstance(e.pw_shell, str) |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 30 | |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 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 |
Walter Dörwald | ecd2fdc | 2003-04-22 11:05:57 +0000 | [diff] [blame] | 36 | entriesbyname.setdefault(e.pw_name, []).append(e) |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 37 | entriesbyuid.setdefault(e.pw_uid, []).append(e) |
| 38 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 39 | if len(entries) > 1000: # Huge passwd file (NIS?) -- skip the rest |
| 40 | return |
| 41 | |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 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: |
Fredrik Lundh | 0e664e1 | 2005-12-12 20:53:40 +0000 | [diff] [blame] | 45 | if not e[0] or e[0] == '+': |
| 46 | continue # skip NIS entries etc. |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 47 | self.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name]) |
| 48 | self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid]) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 49 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 50 | def test_errors(self): |
| 51 | self.assertRaises(TypeError, pwd.getpwuid) |
| 52 | self.assertRaises(TypeError, pwd.getpwnam) |
| 53 | self.assertRaises(TypeError, pwd.getpwall, 42) |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 54 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 55 | # 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 Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 9afbb4e | 2007-02-11 18:54:18 +0000 | [diff] [blame] | 62 | allnames = list(bynames.keys()) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 63 | namei = 0 |
| 64 | fakename = allnames[namei] |
| 65 | while fakename in bynames: |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 66 | chars = list(fakename) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 67 | for i in range(len(chars)): |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 68 | 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 Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 83 | fakename = ''.join(chars) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 84 | |
| 85 | self.assertRaises(KeyError, pwd.getpwnam, fakename) |
| 86 | |
Collin Winter | b24ef19 | 2010-03-18 00:23:44 +0000 | [diff] [blame] | 87 | # 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örwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 94 | self.assertRaises(KeyError, pwd.getpwuid, fakeuid) |
| 95 | |
| 96 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 97 | support.run_unittest(PwdTest) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 98 | |
| 99 | if __name__ == "__main__": |
| 100 | test_main() |