annotate Lib/test/test_pwd.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents f1b038758bda
children aedd2d0dcb31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4825
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4682
diff changeset
1 from test_support import verbose
4682
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
2 import pwd
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 import string
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
4
4825
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4682
diff changeset
5 print 'pwd.getpwall()'
4682
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
6 entries = pwd.getpwall()
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
7
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
8 for e in entries:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
9 name = e[0]
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
10 uid = e[2]
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
11 if verbose:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
12 print name, uid
4825
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4682
diff changeset
13 print 'pwd.getpwuid()'
4682
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
14 dbuid = pwd.getpwuid(uid)
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
15 if dbuid[0] <> name:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
16 print 'Mismatch in pwd.getpwuid()'
4825
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4682
diff changeset
17 print 'pwd.getpwnam()'
4682
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
18 dbname = pwd.getpwnam(name)
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
19 if dbname[2] <> uid:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
20 print 'Mismatch in pwd.getpwnam()'
4825
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4682
diff changeset
21 else:
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4682
diff changeset
22 print 'name matches uid'
4682
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
23 break
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
24
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
25 # try to get some errors
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
26 bynames = {}
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
27 byuids = {}
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
28 for n, p, u, g, gecos, d, s in entries:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
29 bynames[n] = u
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
30 byuids[u] = n
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
31
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
32 allnames = bynames.keys()
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
33 namei = 0
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
34 fakename = allnames[namei]
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
35 while bynames.has_key(fakename):
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
36 chars = map(None, fakename)
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
37 for i in range(len(chars)):
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
38 if chars[i] == 'z':
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
39 chars[i] = 'A'
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
40 break
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
41 elif chars[i] == 'Z':
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
42 continue
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
43 else:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
44 chars[i] = chr(ord(chars[i]) + 1)
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
45 break
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
46 else:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
47 namei = namei + 1
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
48 try:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
49 fakename = allnames[namei]
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
50 except IndexError:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
51 # should never happen... if so, just forget it
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
52 break
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
53 fakename = string.join(map(None, chars), '')
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
54
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
55 try:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
56 pwd.getpwnam(fakename)
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
57 except KeyError:
4825
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4682
diff changeset
58 print 'caught expected exception'
4682
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
59 else:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
60 print 'fakename', fakename, 'did not except pwd.getpwnam()'
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
61
5562
3f3d2d474aca More robust way of choosing a non-existant uid (faster, too).
Guido van Rossum <guido@python.org>
parents: 4825
diff changeset
62 # Choose a non-existant uid.
3f3d2d474aca More robust way of choosing a non-existant uid (faster, too).
Guido van Rossum <guido@python.org>
parents: 4825
diff changeset
63 fakeuid = 4127
3f3d2d474aca More robust way of choosing a non-existant uid (faster, too).
Guido van Rossum <guido@python.org>
parents: 4825
diff changeset
64 while byuids.has_key(fakeuid):
5563
f1b038758bda More robust way of choosing a non-existant uid (faster, too).
Guido van Rossum <guido@python.org>
parents: 5562
diff changeset
65 fakeuid = (fakeuid * 3) % 0x10000
4682
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
66
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
67 try:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
68 pwd.getpwuid(fakeuid)
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
69 except KeyError:
4825
b8a97ad5f7ce test_rotor.py: New test of the rotor module.
Barry Warsaw <barry@python.org>
parents: 4682
diff changeset
70 print 'caught expected exception'
4682
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
71 else:
19c8e61cad2d Test of the pwd module
Barry Warsaw <barry@python.org>
parents:
diff changeset
72 print 'fakeuid', fakeuid, 'did not except pwd.getpwuid()'