Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 3 | |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 4 | import pwd |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 5 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 6 | class PwdTest(unittest.TestCase): |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 7 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 8 | def test_values(self): |
| 9 | entries = pwd.getpwall() |
Walter Dörwald | ecd2fdc | 2003-04-22 11:05:57 +0000 | [diff] [blame] | 10 | entriesbyname = {} |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 11 | entriesbyuid = {} |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 13 | for e in entries: |
| 14 | self.assertEqual(len(e), 7) |
| 15 | self.assertEqual(e[0], e.pw_name) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 16 | self.assert_(isinstance(e.pw_name, str)) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 17 | self.assertEqual(e[1], e.pw_passwd) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 18 | self.assert_(isinstance(e.pw_passwd, str)) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 19 | self.assertEqual(e[2], e.pw_uid) |
| 20 | self.assert_(isinstance(e.pw_uid, int)) |
| 21 | self.assertEqual(e[3], e.pw_gid) |
| 22 | self.assert_(isinstance(e.pw_gid, int)) |
| 23 | self.assertEqual(e[4], e.pw_gecos) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 24 | self.assert_(isinstance(e.pw_gecos, str)) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 25 | self.assertEqual(e[5], e.pw_dir) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 26 | self.assert_(isinstance(e.pw_dir, str)) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 27 | self.assertEqual(e[6], e.pw_shell) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 28 | self.assert_(isinstance(e.pw_shell, str)) |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 29 | |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 30 | # The following won't work, because of duplicate entries |
| 31 | # for one uid |
| 32 | # self.assertEqual(pwd.getpwuid(e.pw_uid), e) |
| 33 | # instead of this collect all entries for one uid |
| 34 | # and check afterwards |
Walter Dörwald | ecd2fdc | 2003-04-22 11:05:57 +0000 | [diff] [blame] | 35 | entriesbyname.setdefault(e.pw_name, []).append(e) |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 36 | entriesbyuid.setdefault(e.pw_uid, []).append(e) |
| 37 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 38 | if len(entries) > 1000: # Huge passwd file (NIS?) -- skip the rest |
| 39 | return |
| 40 | |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 41 | # check whether the entry returned by getpwuid() |
| 42 | # for each uid is among those from getpwall() for this uid |
| 43 | for e in entries: |
Fredrik Lundh | 0e664e1 | 2005-12-12 20:53:40 +0000 | [diff] [blame] | 44 | if not e[0] or e[0] == '+': |
| 45 | continue # skip NIS entries etc. |
Walter Dörwald | ecd2fdc | 2003-04-22 11:05:57 +0000 | [diff] [blame] | 46 | self.assert_(pwd.getpwnam(e.pw_name) in entriesbyname[e.pw_name]) |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 47 | self.assert_(pwd.getpwuid(e.pw_uid) in entriesbyuid[e.pw_uid]) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 48 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 49 | def test_errors(self): |
| 50 | self.assertRaises(TypeError, pwd.getpwuid) |
| 51 | self.assertRaises(TypeError, pwd.getpwnam) |
| 52 | self.assertRaises(TypeError, pwd.getpwall, 42) |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 53 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 54 | # try to get some errors |
| 55 | bynames = {} |
| 56 | byuids = {} |
| 57 | for (n, p, u, g, gecos, d, s) in pwd.getpwall(): |
| 58 | bynames[n] = u |
| 59 | byuids[u] = n |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 9afbb4e | 2007-02-11 18:54:18 +0000 | [diff] [blame] | 61 | allnames = list(bynames.keys()) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 62 | namei = 0 |
| 63 | fakename = allnames[namei] |
| 64 | while fakename in bynames: |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 65 | chars = list(fakename) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 66 | for i in range(len(chars)): |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 67 | if chars[i] == 'z': |
| 68 | chars[i] = 'A' |
| 69 | break |
| 70 | elif chars[i] == 'Z': |
| 71 | continue |
| 72 | else: |
| 73 | chars[i] = chr(ord(chars[i]) + 1) |
| 74 | break |
| 75 | else: |
| 76 | namei = namei + 1 |
| 77 | try: |
| 78 | fakename = allnames[namei] |
| 79 | except IndexError: |
| 80 | # should never happen... if so, just forget it |
| 81 | break |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 82 | fakename = ''.join(chars) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 83 | |
| 84 | self.assertRaises(KeyError, pwd.getpwnam, fakename) |
| 85 | |
| 86 | # Choose a non-existent uid. |
| 87 | fakeuid = 4127 |
| 88 | while fakeuid in byuids: |
| 89 | fakeuid = (fakeuid * 3) % 0x10000 |
| 90 | |
| 91 | self.assertRaises(KeyError, pwd.getpwuid, fakeuid) |
| 92 | |
| 93 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 94 | support.run_unittest(PwdTest) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 95 | |
| 96 | if __name__ == "__main__": |
| 97 | test_main() |