Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_grp.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 79a2b6a12805 |
| children | c982393bea4e |
| 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) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55002
diff
changeset
|
15 self.assertIsInstance(value.gr_name, basestring) |
|
28749
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) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55002
diff
changeset
|
17 self.assertIsInstance(value.gr_passwd, basestring) |
|
28749
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) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55002
diff
changeset
|
19 self.assertIsInstance(value.gr_gid, int) |
|
28749
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) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55002
diff
changeset
|
21 self.assertIsInstance(value.gr_mem, list) |
|
28749
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) |
|
66900
79a2b6a12805
Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58472
diff
changeset
|
36 name = e.gr_name |
|
79a2b6a12805
Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58472
diff
changeset
|
37 if name.startswith('+') or name.startswith('-'): |
|
79a2b6a12805
Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58472
diff
changeset
|
38 # NIS-related entry |
|
79a2b6a12805
Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58472
diff
changeset
|
39 continue |
|
79a2b6a12805
Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58472
diff
changeset
|
40 e2 = grp.getgrnam(name) |
|
28749
89dc32674554
Max OS X returns "*" as the password in grp.getgrall()
Walter Dörwald <walter@livinglogic.de>
parents:
28719
diff
changeset
|
41 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
|
42 # 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
|
43 # 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
|
44 # Discovered on Ubuntu 5.04 (custom). |
|
66900
79a2b6a12805
Merged revisions 87238 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
58472
diff
changeset
|
45 self.assertEqual(e2.gr_name.lower(), name.lower()) |
|
28611
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
46 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
52 # 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
|
53 bynames = {} |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
54 bygids = {} |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
55 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
|
56 if not n or n == '+': |
|
f94418715e55
skip NIS entries, empty entries, etc
Fredrik Lundh <fredrik@pythonware.com>
parents:
28980
diff
changeset
|
57 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
|
58 bynames[n] = g |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
59 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
|
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) |
|
4772
b85f10ae4857
Test script for the UNIX group file access module (grp)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
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, grp.getgrnam, fakename) |
| 17858 | 85 |
|
28611
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 gid. |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
87 fakegid = 4127 |
|
c71b9ba1f04c
Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
88 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
|
89 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
|
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, grp.getgrgid, fakegid) |
| 17858 | 92 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17858
diff
changeset
|
93 def test_main(): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28749
diff
changeset
|
94 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
|
95 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17858
diff
changeset
|
96 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
|
97 test_main() |
