Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_grp.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 |
|---|---|
| 17858 | 1 """Test script for the grp module.""" |
| 2 | |
| 3 import unittest | |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
4 from test import test_support |
| 17858 | 5 |
|
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
|
6 grp = test_support.import_module('grp') |
|
f2c90fd05d66
Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents:
48018
diff
changeset
|
7 |
|
28749
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
8 class GroupDatabaseTestCase(unittest.TestCase): |
|
4772
b85f10ae4857
Test script for the UNIX group file access module (grp)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff
changeset
|
9 |
|
28749
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
10 def check_value(self, value): |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
11 # check that a grp tuple has the entries and |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
12 # attributes promised by the docs |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
13 self.assertEqual(len(value), 4) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
14 self.assertEqual(value[0], value.gr_name) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
15 self.assert_(isinstance(value.gr_name, basestring)) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
16 self.assertEqual(value[1], value.gr_passwd) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
17 self.assert_(isinstance(value.gr_passwd, basestring)) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
18 self.assertEqual(value[2], value.gr_gid) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
19 self.assert_(isinstance(value.gr_gid, int)) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
20 self.assertEqual(value[3], value.gr_mem) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
21 self.assert_(isinstance(value.gr_mem, list)) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
22 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
23 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
|
24 entries = grp.getgrall() |
|
28749
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
25 |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
26 for e in entries: |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
27 self.check_value(e) |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
28 |
|
44884
d62c126fe56a
Backport r60208, skip some tests for huge passwd/group files.
Guido van Rossum <guido@python.org>
parents:
38325
diff
changeset
|
29 if len(entries) > 1000: # Huge group file (NIS?) -- skip the rest |
|
d62c126fe56a
Backport r60208, skip some tests for huge passwd/group files.
Guido van Rossum <guido@python.org>
parents:
38325
diff
changeset
|
30 return |
|
d62c126fe56a
Backport r60208, skip some tests for huge passwd/group files.
Guido van Rossum <guido@python.org>
parents:
38325
diff
changeset
|
31 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
32 for e in entries: |
|
28749
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
33 e2 = grp.getgrgid(e.gr_gid) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
34 self.check_value(e2) |
|
28980
5892c1e515b4
On Mac OS X pwd.getpwall() might return the tuples
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
35 self.assertEqual(e2.gr_gid, e.gr_gid) |
|
28749
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
36 e2 = grp.getgrnam(e.gr_name) |
|
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
37 self.check_value(e2) |
|
38325
8a58aa40dcc0
Change test_values so that it compares the lowercasing of group names since getgrall() can return all lowercase names while getgrgid() returns proper casing.
Brett Cannon <bcannon@gmail.com>
parents:
36011
diff
changeset
|
38 # There are instances where getgrall() returns group names in |
|
8a58aa40dcc0
Change test_values so that it compares the lowercasing of group names since getgrall() can return all lowercase names while getgrgid() returns proper casing.
Brett Cannon <bcannon@gmail.com>
parents:
36011
diff
changeset
|
39 # lowercase while getgrgid() returns proper casing. |
|
8a58aa40dcc0
Change test_values so that it compares the lowercasing of group names since getgrall() can return all lowercase names while getgrgid() returns proper casing.
Brett Cannon <bcannon@gmail.com>
parents:
36011
diff
changeset
|
40 # Discovered on Ubuntu 5.04 (custom). |
|
8a58aa40dcc0
Change test_values so that it compares the lowercasing of group names since getgrall() can return all lowercase names while getgrgid() returns proper casing.
Brett Cannon <bcannon@gmail.com>
parents:
36011
diff
changeset
|
41 self.assertEqual(e2.gr_name.lower(), e.gr_name.lower()) |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
42 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
43 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
|
44 self.assertRaises(TypeError, grp.getgrgid) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
45 self.assertRaises(TypeError, grp.getgrnam) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
46 self.assertRaises(TypeError, grp.getgrall, 42) |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
47 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
48 # 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
|
49 bynames = {} |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
50 bygids = {} |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
51 for (n, p, g, mem) in grp.getgrall(): |
|
36011
f94418715e55
skip NIS entries, empty entries, etc
Fredrik Lundh <fredrik@pythonware.com>
parents:
28980
diff
changeset
|
52 if not n or n == '+': |
|
f94418715e55
skip NIS entries, empty entries, etc
Fredrik Lundh <fredrik@pythonware.com>
parents:
28980
diff
changeset
|
53 continue # skip NIS entries etc. |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
54 bynames[n] = g |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
55 bygids[g] = n |
|
4772
b85f10ae4857
Test script for the UNIX group file access module (grp)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff
changeset
|
56 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
57 allnames = bynames.keys() |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
58 namei = 0 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
59 fakename = allnames[namei] |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
60 while fakename in bynames: |
|
48018
49726dca16d8
Replace all map(None, a) with list(a).
Georg Brandl <georg@python.org>
parents:
44884
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 chars[i] = 'A' |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
65 break |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
66 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
|
67 continue |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
68 else: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
69 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
|
70 break |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
71 else: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
72 namei = namei + 1 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
73 try: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
74 fakename = allnames[namei] |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
75 except IndexError: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
76 # 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
|
77 break |
|
48018
49726dca16d8
Replace all map(None, a) with list(a).
Georg Brandl <georg@python.org>
parents:
44884
diff
changeset
|
78 fakename = ''.join(chars) |
|
4772
b85f10ae4857
Test script for the UNIX group file access module (grp)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff
changeset
|
79 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
80 self.assertRaises(KeyError, grp.getgrnam, fakename) |
| 17858 | 81 |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
82 # Choose a non-existent gid. |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
83 fakegid = 4127 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
84 while fakegid in bygids: |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
85 fakegid = (fakegid * 3) % 0x10000 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
86 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
87 self.assertRaises(KeyError, grp.getgrgid, fakegid) |
| 17858 | 88 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17858
diff
changeset
|
89 def test_main(): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28749
diff
changeset
|
90 test_support.run_unittest(GroupDatabaseTestCase) |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17858
diff
changeset
|
91 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17858
diff
changeset
|
92 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17858
diff
changeset
|
93 test_main() |
