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

Python code coverage for Lib/test/test_pwd.py

#countcontent
1n/aimport sys
2n/aimport unittest
3n/afrom test import support
4n/a
5n/apwd = support.import_module('pwd')
6n/a
7n/adef _getpwall():
8n/a # Android does not have getpwall.
9n/a if hasattr(pwd, 'getpwall'):
10n/a return pwd.getpwall()
11n/a elif hasattr(pwd, 'getpwuid'):
12n/a return [pwd.getpwuid(0)]
13n/a else:
14n/a return []
15n/a
16n/aclass PwdTest(unittest.TestCase):
17n/a
18n/a def test_values(self):
19n/a entries = _getpwall()
20n/a
21n/a for e in entries:
22n/a self.assertEqual(len(e), 7)
23n/a self.assertEqual(e[0], e.pw_name)
24n/a self.assertIsInstance(e.pw_name, str)
25n/a self.assertEqual(e[1], e.pw_passwd)
26n/a self.assertIsInstance(e.pw_passwd, str)
27n/a self.assertEqual(e[2], e.pw_uid)
28n/a self.assertIsInstance(e.pw_uid, int)
29n/a self.assertEqual(e[3], e.pw_gid)
30n/a self.assertIsInstance(e.pw_gid, int)
31n/a self.assertEqual(e[4], e.pw_gecos)
32n/a self.assertIsInstance(e.pw_gecos, str)
33n/a self.assertEqual(e[5], e.pw_dir)
34n/a self.assertIsInstance(e.pw_dir, str)
35n/a self.assertEqual(e[6], e.pw_shell)
36n/a self.assertIsInstance(e.pw_shell, str)
37n/a
38n/a # The following won't work, because of duplicate entries
39n/a # for one uid
40n/a # self.assertEqual(pwd.getpwuid(e.pw_uid), e)
41n/a # instead of this collect all entries for one uid
42n/a # and check afterwards (done in test_values_extended)
43n/a
44n/a def test_values_extended(self):
45n/a entries = _getpwall()
46n/a entriesbyname = {}
47n/a entriesbyuid = {}
48n/a
49n/a if len(entries) > 1000: # Huge passwd file (NIS?) -- skip this test
50n/a self.skipTest('passwd file is huge; extended test skipped')
51n/a
52n/a for e in entries:
53n/a entriesbyname.setdefault(e.pw_name, []).append(e)
54n/a entriesbyuid.setdefault(e.pw_uid, []).append(e)
55n/a
56n/a # check whether the entry returned by getpwuid()
57n/a # for each uid is among those from getpwall() for this uid
58n/a for e in entries:
59n/a if not e[0] or e[0] == '+':
60n/a continue # skip NIS entries etc.
61n/a self.assertIn(pwd.getpwnam(e.pw_name), entriesbyname[e.pw_name])
62n/a self.assertIn(pwd.getpwuid(e.pw_uid), entriesbyuid[e.pw_uid])
63n/a
64n/a def test_errors(self):
65n/a self.assertRaises(TypeError, pwd.getpwuid)
66n/a self.assertRaises(TypeError, pwd.getpwuid, 3.14)
67n/a self.assertRaises(TypeError, pwd.getpwnam)
68n/a self.assertRaises(TypeError, pwd.getpwnam, 42)
69n/a if hasattr(pwd, 'getpwall'):
70n/a self.assertRaises(TypeError, pwd.getpwall, 42)
71n/a
72n/a # try to get some errors
73n/a bynames = {}
74n/a byuids = {}
75n/a for (n, p, u, g, gecos, d, s) in _getpwall():
76n/a bynames[n] = u
77n/a byuids[u] = n
78n/a
79n/a allnames = list(bynames.keys())
80n/a namei = 0
81n/a fakename = allnames[namei]
82n/a while fakename in bynames:
83n/a chars = list(fakename)
84n/a for i in range(len(chars)):
85n/a if chars[i] == 'z':
86n/a chars[i] = 'A'
87n/a break
88n/a elif chars[i] == 'Z':
89n/a continue
90n/a else:
91n/a chars[i] = chr(ord(chars[i]) + 1)
92n/a break
93n/a else:
94n/a namei = namei + 1
95n/a try:
96n/a fakename = allnames[namei]
97n/a except IndexError:
98n/a # should never happen... if so, just forget it
99n/a break
100n/a fakename = ''.join(chars)
101n/a
102n/a self.assertRaises(KeyError, pwd.getpwnam, fakename)
103n/a
104n/a # In some cases, byuids isn't a complete list of all users in the
105n/a # system, so if we try to pick a value not in byuids (via a perturbing
106n/a # loop, say), pwd.getpwuid() might still be able to find data for that
107n/a # uid. Using sys.maxint may provoke the same problems, but hopefully
108n/a # it will be a more repeatable failure.
109n/a # Android accepts a very large span of uids including sys.maxsize and
110n/a # -1; it raises KeyError with 1 or 2 for example.
111n/a fakeuid = sys.maxsize
112n/a self.assertNotIn(fakeuid, byuids)
113n/a if not support.is_android:
114n/a self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
115n/a
116n/a # -1 shouldn't be a valid uid because it has a special meaning in many
117n/a # uid-related functions
118n/a if not support.is_android:
119n/a self.assertRaises(KeyError, pwd.getpwuid, -1)
120n/a # should be out of uid_t range
121n/a self.assertRaises(KeyError, pwd.getpwuid, 2**128)
122n/a self.assertRaises(KeyError, pwd.getpwuid, -2**128)
123n/a
124n/aif __name__ == "__main__":
125n/a unittest.main()