blob: 79bf866994c11ad4078e972232cd7e9003294482 [file] [log] [blame]
Barry Warsaw5e056bb1996-12-23 23:39:42 +00001from test_support import verbose
Barry Warsaw3d070fb1996-12-11 16:53:51 +00002import pwd
3import string
4
Barry Warsaw5e056bb1996-12-23 23:39:42 +00005print 'pwd.getpwall()'
Barry Warsaw3d070fb1996-12-11 16:53:51 +00006entries = pwd.getpwall()
7
8for e in entries:
9 name = e[0]
10 uid = e[2]
11 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000012 print name, uid
Barry Warsaw5e056bb1996-12-23 23:39:42 +000013 print 'pwd.getpwuid()'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000014 dbuid = pwd.getpwuid(uid)
15 if dbuid[0] <> name:
Guido van Rossum41360a41998-03-26 19:42:58 +000016 print 'Mismatch in pwd.getpwuid()'
Barry Warsaw5e056bb1996-12-23 23:39:42 +000017 print 'pwd.getpwnam()'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000018 dbname = pwd.getpwnam(name)
19 if dbname[2] <> uid:
Guido van Rossum41360a41998-03-26 19:42:58 +000020 print 'Mismatch in pwd.getpwnam()'
Barry Warsaw5e056bb1996-12-23 23:39:42 +000021 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000022 print 'name matches uid'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000023 break
24
25# try to get some errors
26bynames = {}
27byuids = {}
28for n, p, u, g, gecos, d, s in entries:
29 bynames[n] = u
30 byuids[u] = n
31
32allnames = bynames.keys()
33namei = 0
34fakename = allnames[namei]
35while bynames.has_key(fakename):
36 chars = map(None, fakename)
37 for i in range(len(chars)):
Guido van Rossum41360a41998-03-26 19:42:58 +000038 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 Warsaw3d070fb1996-12-11 16:53:51 +000046 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000047 namei = namei + 1
48 try:
49 fakename = allnames[namei]
50 except IndexError:
51 # should never happen... if so, just forget it
52 break
Barry Warsaw3d070fb1996-12-11 16:53:51 +000053 fakename = string.join(map(None, chars), '')
54
55try:
56 pwd.getpwnam(fakename)
57except KeyError:
Barry Warsaw5e056bb1996-12-23 23:39:42 +000058 print 'caught expected exception'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000059else:
60 print 'fakename', fakename, 'did not except pwd.getpwnam()'
61
Guido van Rossum6308d511997-05-20 19:31:25 +000062# Choose a non-existant uid.
63fakeuid = 4127
64while byuids.has_key(fakeuid):
Guido van Rossum462d6e61997-05-20 19:32:22 +000065 fakeuid = (fakeuid * 3) % 0x10000
Barry Warsaw3d070fb1996-12-11 16:53:51 +000066
67try:
68 pwd.getpwuid(fakeuid)
69except KeyError:
Barry Warsaw5e056bb1996-12-23 23:39:42 +000070 print 'caught expected exception'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000071else:
72 print 'fakeuid', fakeuid, 'did not except pwd.getpwuid()'