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() |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 11 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 12 | for e in entries: |
| 13 | self.assertEqual(len(e), 7) |
| 14 | self.assertEqual(e[0], e.pw_name) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 15 | self.assertIsInstance(e.pw_name, str) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 16 | self.assertEqual(e[1], e.pw_passwd) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 17 | self.assertIsInstance(e.pw_passwd, str) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 18 | self.assertEqual(e[2], e.pw_uid) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 19 | self.assertIsInstance(e.pw_uid, int) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 20 | self.assertEqual(e[3], e.pw_gid) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 21 | self.assertIsInstance(e.pw_gid, int) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 22 | self.assertEqual(e[4], e.pw_gecos) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 23 | self.assertIsInstance(e.pw_gecos, str) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 24 | self.assertEqual(e[5], e.pw_dir) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 25 | self.assertIsInstance(e.pw_dir, str) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 26 | self.assertEqual(e[6], e.pw_shell) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 27 | self.assertIsInstance(e.pw_shell, str) |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 28 | |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 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 |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 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: |
Walter Dörwald | ecd2fdc | 2003-04-22 11:05:57 +0000 | [diff] [blame] | 44 | entriesbyname.setdefault(e.pw_name, []).append(e) |
Walter Dörwald | 66e1e50 | 2003-04-15 15:39:08 +0000 | [diff] [blame] | 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: |
Fredrik Lundh | 0e664e1 | 2005-12-12 20:53:40 +0000 | [diff] [blame] | 50 | if not e[0] or e[0] == '+': |
| 51 | continue # skip NIS entries etc. |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 52 | self.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name]) |
| 53 | self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid]) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 54 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 55 | def test_errors(self): |
| 56 | self.assertRaises(TypeError, pwd.getpwuid) |
Serhiy Storchaka | 55e2238 | 2013-02-11 20:32:47 +0200 | [diff] [blame] | 57 | self.assertRaises(TypeError, pwd.getpwuid, 3.14) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 58 | self.assertRaises(TypeError, pwd.getpwnam) |
Serhiy Storchaka | 55e2238 | 2013-02-11 20:32:47 +0200 | [diff] [blame] | 59 | self.assertRaises(TypeError, pwd.getpwnam, 42) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 60 | self.assertRaises(TypeError, pwd.getpwall, 42) |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 61 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 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 |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 9afbb4e | 2007-02-11 18:54:18 +0000 | [diff] [blame] | 69 | allnames = list(bynames.keys()) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 70 | namei = 0 |
| 71 | fakename = allnames[namei] |
| 72 | while fakename in bynames: |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 73 | chars = list(fakename) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 74 | for i in range(len(chars)): |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 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 |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 90 | fakename = ''.join(chars) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 91 | |
| 92 | self.assertRaises(KeyError, pwd.getpwnam, fakename) |
| 93 | |
Collin Winter | b24ef19 | 2010-03-18 00:23:44 +0000 | [diff] [blame] | 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.maxsize |
| 100 | self.assertNotIn(fakeuid, byuids) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 101 | self.assertRaises(KeyError, pwd.getpwuid, fakeuid) |
| 102 | |
Serhiy Storchaka | 55e2238 | 2013-02-11 20:32:47 +0200 | [diff] [blame] | 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 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 110 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 111 | unittest.main() |