blob: 2cb122beeea6c458900cc03387452390f780fab9 [file] [log] [blame]
Fredrik Lundhf7850422001-01-17 21:51:36 +00001from test_support import verbose
Barry Warsaw3d070fb1996-12-11 16:53:51 +00002import pwd
Barry Warsaw3d070fb1996-12-11 16:53:51 +00003
Barry Warsaw5e056bb1996-12-23 23:39:42 +00004print 'pwd.getpwall()'
Barry Warsaw3d070fb1996-12-11 16:53:51 +00005entries = pwd.getpwall()
6
7for e in entries:
8 name = e[0]
9 uid = e[2]
10 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000011 print name, uid
Barry Warsaw5e056bb1996-12-23 23:39:42 +000012 print 'pwd.getpwuid()'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000013 dbuid = pwd.getpwuid(uid)
Fred Drake132dce22000-12-12 23:11:42 +000014 if dbuid[0] != name:
Guido van Rossum41360a41998-03-26 19:42:58 +000015 print 'Mismatch in pwd.getpwuid()'
Barry Warsaw5e056bb1996-12-23 23:39:42 +000016 print 'pwd.getpwnam()'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000017 dbname = pwd.getpwnam(name)
Fred Drake132dce22000-12-12 23:11:42 +000018 if dbname[2] != uid:
Guido van Rossum41360a41998-03-26 19:42:58 +000019 print 'Mismatch in pwd.getpwnam()'
Barry Warsaw5e056bb1996-12-23 23:39:42 +000020 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000021 print 'name matches uid'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000022 break
23
24# try to get some errors
25bynames = {}
26byuids = {}
27for n, p, u, g, gecos, d, s in entries:
28 bynames[n] = u
29 byuids[u] = n
30
31allnames = bynames.keys()
32namei = 0
33fakename = allnames[namei]
34while bynames.has_key(fakename):
35 chars = map(None, fakename)
36 for i in range(len(chars)):
Guido van Rossum41360a41998-03-26 19:42:58 +000037 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 Warsaw3d070fb1996-12-11 16:53:51 +000045 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000046 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. Raymond2846b0a2001-02-09 12:00:47 +000052 fakename = ''.join(map(None, chars))
Fred Drake004d5e62000-10-23 17:22:08 +000053
Barry Warsaw3d070fb1996-12-11 16:53:51 +000054try:
55 pwd.getpwnam(fakename)
56except KeyError:
Barry Warsaw5e056bb1996-12-23 23:39:42 +000057 print 'caught expected exception'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000058else:
59 print 'fakename', fakename, 'did not except pwd.getpwnam()'
60
Jeremy Hyltona05e2932000-06-28 14:48:01 +000061# Choose a non-existent uid.
Guido van Rossum6308d511997-05-20 19:31:25 +000062fakeuid = 4127
63while byuids.has_key(fakeuid):
Guido van Rossum462d6e61997-05-20 19:32:22 +000064 fakeuid = (fakeuid * 3) % 0x10000
Barry Warsaw3d070fb1996-12-11 16:53:51 +000065
66try:
67 pwd.getpwuid(fakeuid)
68except KeyError:
Barry Warsaw5e056bb1996-12-23 23:39:42 +000069 print 'caught expected exception'
Barry Warsaw3d070fb1996-12-11 16:53:51 +000070else:
71 print 'fakeuid', fakeuid, 'did not except pwd.getpwuid()'