Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 1 | from test_support import verbose |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 2 | import pwd |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 3 | |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 4 | print 'pwd.getpwall()' |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 5 | entries = pwd.getpwall() |
| 6 | |
| 7 | for e in entries: |
| 8 | name = e[0] |
| 9 | uid = e[2] |
| 10 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 11 | print name, uid |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 12 | print 'pwd.getpwuid()' |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 13 | dbuid = pwd.getpwuid(uid) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 14 | if dbuid[0] != name: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 15 | print 'Mismatch in pwd.getpwuid()' |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 16 | print 'pwd.getpwnam()' |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 17 | dbname = pwd.getpwnam(name) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 18 | if dbname[2] != uid: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 19 | print 'Mismatch in pwd.getpwnam()' |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 20 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 21 | print 'name matches uid' |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 22 | break |
| 23 | |
| 24 | # try to get some errors |
| 25 | bynames = {} |
| 26 | byuids = {} |
| 27 | for n, p, u, g, gecos, d, s in entries: |
| 28 | bynames[n] = u |
| 29 | byuids[u] = n |
| 30 | |
| 31 | allnames = bynames.keys() |
| 32 | namei = 0 |
| 33 | fakename = allnames[namei] |
| 34 | while bynames.has_key(fakename): |
| 35 | chars = map(None, fakename) |
| 36 | for i in range(len(chars)): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 37 | if chars[i] == 'z': |
| 38 | chars[i] = 'A' |
| 39 | break |
| 40 | elif chars[i] == 'Z': |
| 41 | continue |
| 42 | else: |
| 43 | chars[i] = chr(ord(chars[i]) + 1) |
| 44 | break |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 45 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 46 | namei = namei + 1 |
| 47 | try: |
| 48 | fakename = allnames[namei] |
| 49 | except IndexError: |
| 50 | # should never happen... if so, just forget it |
| 51 | break |
Eric S. Raymond | 2846b0a | 2001-02-09 12:00:47 +0000 | [diff] [blame] | 52 | fakename = ''.join(map(None, chars)) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 53 | |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 54 | try: |
| 55 | pwd.getpwnam(fakename) |
| 56 | except KeyError: |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 57 | print 'caught expected exception' |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 58 | else: |
| 59 | print 'fakename', fakename, 'did not except pwd.getpwnam()' |
| 60 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 61 | # Choose a non-existent uid. |
Guido van Rossum | 6308d51 | 1997-05-20 19:31:25 +0000 | [diff] [blame] | 62 | fakeuid = 4127 |
| 63 | while byuids.has_key(fakeuid): |
Guido van Rossum | 462d6e6 | 1997-05-20 19:32:22 +0000 | [diff] [blame] | 64 | fakeuid = (fakeuid * 3) % 0x10000 |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 65 | |
| 66 | try: |
| 67 | pwd.getpwuid(fakeuid) |
| 68 | except KeyError: |
Barry Warsaw | 5e056bb | 1996-12-23 23:39:42 +0000 | [diff] [blame] | 69 | print 'caught expected exception' |
Barry Warsaw | 3d070fb | 1996-12-11 16:53:51 +0000 | [diff] [blame] | 70 | else: |
| 71 | print 'fakeuid', fakeuid, 'did not except pwd.getpwuid()' |