ยปCore Development>Code coverage>Lib/test/test_grp.py

Python code coverage for Lib/test/test_grp.py

#countcontent
1n/a"""Test script for the grp module."""
2n/a
3n/aimport unittest
4n/afrom test import support
5n/a
6n/agrp = support.import_module('grp')
7n/a
8n/aclass GroupDatabaseTestCase(unittest.TestCase):
9n/a
10n/a def check_value(self, value):
11n/a # check that a grp tuple has the entries and
12n/a # attributes promised by the docs
13n/a self.assertEqual(len(value), 4)
14n/a self.assertEqual(value[0], value.gr_name)
15n/a self.assertIsInstance(value.gr_name, str)
16n/a self.assertEqual(value[1], value.gr_passwd)
17n/a self.assertIsInstance(value.gr_passwd, str)
18n/a self.assertEqual(value[2], value.gr_gid)
19n/a self.assertIsInstance(value.gr_gid, int)
20n/a self.assertEqual(value[3], value.gr_mem)
21n/a self.assertIsInstance(value.gr_mem, list)
22n/a
23n/a def test_values(self):
24n/a entries = grp.getgrall()
25n/a
26n/a for e in entries:
27n/a self.check_value(e)
28n/a
29n/a def test_values_extended(self):
30n/a entries = grp.getgrall()
31n/a if len(entries) > 1000: # Huge group file (NIS?) -- skip the rest
32n/a self.skipTest('huge group file, extended test skipped')
33n/a
34n/a for e in entries:
35n/a e2 = grp.getgrgid(e.gr_gid)
36n/a self.check_value(e2)
37n/a self.assertEqual(e2.gr_gid, e.gr_gid)
38n/a name = e.gr_name
39n/a if name.startswith('+') or name.startswith('-'):
40n/a # NIS-related entry
41n/a continue
42n/a e2 = grp.getgrnam(name)
43n/a self.check_value(e2)
44n/a # There are instances where getgrall() returns group names in
45n/a # lowercase while getgrgid() returns proper casing.
46n/a # Discovered on Ubuntu 5.04 (custom).
47n/a self.assertEqual(e2.gr_name.lower(), name.lower())
48n/a
49n/a def test_errors(self):
50n/a self.assertRaises(TypeError, grp.getgrgid)
51n/a self.assertRaises(TypeError, grp.getgrnam)
52n/a self.assertRaises(TypeError, grp.getgrall, 42)
53n/a
54n/a # try to get some errors
55n/a bynames = {}
56n/a bygids = {}
57n/a for (n, p, g, mem) in grp.getgrall():
58n/a if not n or n == '+':
59n/a continue # skip NIS entries etc.
60n/a bynames[n] = g
61n/a bygids[g] = n
62n/a
63n/a allnames = list(bynames.keys())
64n/a namei = 0
65n/a fakename = allnames[namei]
66n/a while fakename in bynames:
67n/a chars = list(fakename)
68n/a for i in range(len(chars)):
69n/a if chars[i] == 'z':
70n/a chars[i] = 'A'
71n/a break
72n/a elif chars[i] == 'Z':
73n/a continue
74n/a else:
75n/a chars[i] = chr(ord(chars[i]) + 1)
76n/a break
77n/a else:
78n/a namei = namei + 1
79n/a try:
80n/a fakename = allnames[namei]
81n/a except IndexError:
82n/a # should never happen... if so, just forget it
83n/a break
84n/a fakename = ''.join(chars)
85n/a
86n/a self.assertRaises(KeyError, grp.getgrnam, fakename)
87n/a
88n/a # Choose a non-existent gid.
89n/a fakegid = 4127
90n/a while fakegid in bygids:
91n/a fakegid = (fakegid * 3) % 0x10000
92n/a
93n/a self.assertRaises(KeyError, grp.getgrgid, fakegid)
94n/a
95n/a def test_noninteger_gid(self):
96n/a entries = grp.getgrall()
97n/a if not entries:
98n/a self.skipTest('no groups')
99n/a # Choose an existent gid.
100n/a gid = entries[0][2]
101n/a self.assertWarns(DeprecationWarning, grp.getgrgid, float(gid))
102n/a self.assertWarns(DeprecationWarning, grp.getgrgid, str(gid))
103n/a
104n/a
105n/aif __name__ == "__main__":
106n/a unittest.main()