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