Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_pwd.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | f2c90fd05d66 |
| children | 6768b2350c57 |
| rev | line source |
|---|---|
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
1 import unittest |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
2 from test import test_support |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
3 |
|
52456
f2c90fd05d66
Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents:
48018
diff
changeset
|
4 pwd = test_support.import_module('pwd') |
| 4682 | 5 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
6 class PwdTest(unittest.TestCase): |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
7 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
8 def test_values(self): |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
9 entries = pwd.getpwall() |
|
28719
5e6ffa655462
Change test_pwd and test_grp so they can handle duplicate user
Walter Dörwald <walter@livinglogic.de>
parents:
28617
diff
changeset
|
10 entriesbyname = {} |
|
28617
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
11 entriesbyuid = {} |
| 4682 | 12 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
13 for e in entries: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
14 self.assertEqual(len(e), 7) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
15 self.assertEqual(e[0], e.pw_name) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
16 self.assert_(isinstance(e.pw_name, basestring)) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
17 self.assertEqual(e[1], e.pw_passwd) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
18 self.assert_(isinstance(e.pw_passwd, basestring)) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
19 self.assertEqual(e[2], e.pw_uid) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
20 self.assert_(isinstance(e.pw_uid, int)) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
21 self.assertEqual(e[3], e.pw_gid) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
22 self.assert_(isinstance(e.pw_gid, int)) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
23 self.assertEqual(e[4], e.pw_gecos) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
24 self.assert_(isinstance(e.pw_gecos, basestring)) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
25 self.assertEqual(e[5], e.pw_dir) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
26 self.assert_(isinstance(e.pw_dir, basestring)) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
27 self.assertEqual(e[6], e.pw_shell) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
28 self.assert_(isinstance(e.pw_shell, basestring)) |
| 4682 | 29 |
|
28617
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
30 # The following won't work, because of duplicate entries |
|
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
31 # for one uid |
|
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
32 # self.assertEqual(pwd.getpwuid(e.pw_uid), e) |
|
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
33 # instead of this collect all entries for one uid |
|
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
34 # and check afterwards |
|
28719
5e6ffa655462
Change test_pwd and test_grp so they can handle duplicate user
Walter Dörwald <walter@livinglogic.de>
parents:
28617
diff
changeset
|
35 entriesbyname.setdefault(e.pw_name, []).append(e) |
|
28617
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
36 entriesbyuid.setdefault(e.pw_uid, []).append(e) |
|
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
37 |
|
44884
d62c126fe56a
Backport r60208, skip some tests for huge passwd/group files.
Guido van Rossum <guido@python.org>
parents:
36011
diff
changeset
|
38 if len(entries) > 1000: # Huge passwd file (NIS?) -- skip the rest |
|
d62c126fe56a
Backport r60208, skip some tests for huge passwd/group files.
Guido van Rossum <guido@python.org>
parents:
36011
diff
changeset
|
39 return |
|
d62c126fe56a
Backport r60208, skip some tests for huge passwd/group files.
Guido van Rossum <guido@python.org>
parents:
36011
diff
changeset
|
40 |
|
28617
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
41 # check whether the entry returned by getpwuid() |
|
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
42 # for each uid is among those from getpwall() for this uid |
|
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
43 for e in entries: |
|
36011
f94418715e55
skip NIS entries, empty entries, etc
Fredrik Lundh <fredrik@pythonware.com>
parents:
36010
diff
changeset
|
44 if not e[0] or e[0] == '+': |
|
f94418715e55
skip NIS entries, empty entries, etc
Fredrik Lundh <fredrik@pythonware.com>
parents:
36010
diff
changeset
|
45 continue # skip NIS entries etc. |
|
28719
5e6ffa655462
Change test_pwd and test_grp so they can handle duplicate user
Walter Dörwald <walter@livinglogic.de>
parents:
28617
diff
changeset
|
46 self.assert_(pwd.getpwnam(e.pw_name) in entriesbyname[e.pw_name]) |
|
28617
d5357aade4b7
Fix the test so that it works even when /etc/passwd has two entries
Walter Dörwald <walter@livinglogic.de>
parents:
28611
diff
changeset
|
47 self.assert_(pwd.getpwuid(e.pw_uid) in entriesbyuid[e.pw_uid]) |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
48 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
49 def test_errors(self): |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
50 self.assertRaises(TypeError, pwd.getpwuid) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
51 self.assertRaises(TypeError, pwd.getpwnam) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
52 self.assertRaises(TypeError, pwd.getpwall, 42) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
53 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
54 # try to get some errors |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
55 bynames = {} |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
56 byuids = {} |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
57 for (n, p, u, g, gecos, d, s) in pwd.getpwall(): |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
58 bynames[n] = u |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
59 byuids[u] = n |
| 4682 | 60 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
61 allnames = bynames.keys() |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
62 namei = 0 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
63 fakename = allnames[namei] |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
64 while fakename in bynames: |
|
48018
49726dca16d8
Replace all map(None, a) with list(a).
Georg Brandl <georg@python.org>
parents:
44884
diff
changeset
|
65 chars = list(fakename) |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
66 for i in xrange(len(chars)): |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
67 if chars[i] == 'z': |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
68 chars[i] = 'A' |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
69 break |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
70 elif chars[i] == 'Z': |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
71 continue |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
72 else: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
73 chars[i] = chr(ord(chars[i]) + 1) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
74 break |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
75 else: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
76 namei = namei + 1 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
77 try: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
78 fakename = allnames[namei] |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
79 except IndexError: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
80 # should never happen... if so, just forget it |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
81 break |
|
48018
49726dca16d8
Replace all map(None, a) with list(a).
Georg Brandl <georg@python.org>
parents:
44884
diff
changeset
|
82 fakename = ''.join(chars) |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13050
diff
changeset
|
83 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
84 self.assertRaises(KeyError, pwd.getpwnam, fakename) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
85 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
86 # Choose a non-existent uid. |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
87 fakeuid = 4127 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
88 while fakeuid in byuids: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
89 fakeuid = (fakeuid * 3) % 0x10000 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
90 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
91 self.assertRaises(KeyError, pwd.getpwuid, fakeuid) |
| 4682 | 92 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
93 def test_main(): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28774
diff
changeset
|
94 test_support.run_unittest(PwdTest) |
| 4682 | 95 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
96 if __name__ == "__main__": |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
97 test_main() |
