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

Python code coverage for Lib/test/test_spwd.py

#countcontent
1n/aimport os
2n/aimport unittest
3n/afrom test import support
4n/a
5n/aspwd = support.import_module('spwd')
6n/a
7n/a
8n/a@unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() == 0,
9n/a 'root privileges required')
10n/aclass TestSpwdRoot(unittest.TestCase):
11n/a
12n/a def test_getspall(self):
13n/a entries = spwd.getspall()
14n/a self.assertIsInstance(entries, list)
15n/a for entry in entries:
16n/a self.assertIsInstance(entry, spwd.struct_spwd)
17n/a
18n/a def test_getspnam(self):
19n/a entries = spwd.getspall()
20n/a if not entries:
21n/a self.skipTest('empty shadow password database')
22n/a random_name = entries[0].sp_namp
23n/a entry = spwd.getspnam(random_name)
24n/a self.assertIsInstance(entry, spwd.struct_spwd)
25n/a self.assertEqual(entry.sp_namp, random_name)
26n/a self.assertEqual(entry.sp_namp, entry[0])
27n/a self.assertEqual(entry.sp_namp, entry.sp_nam)
28n/a self.assertIsInstance(entry.sp_pwdp, str)
29n/a self.assertEqual(entry.sp_pwdp, entry[1])
30n/a self.assertEqual(entry.sp_pwdp, entry.sp_pwd)
31n/a self.assertIsInstance(entry.sp_lstchg, int)
32n/a self.assertEqual(entry.sp_lstchg, entry[2])
33n/a self.assertIsInstance(entry.sp_min, int)
34n/a self.assertEqual(entry.sp_min, entry[3])
35n/a self.assertIsInstance(entry.sp_max, int)
36n/a self.assertEqual(entry.sp_max, entry[4])
37n/a self.assertIsInstance(entry.sp_warn, int)
38n/a self.assertEqual(entry.sp_warn, entry[5])
39n/a self.assertIsInstance(entry.sp_inact, int)
40n/a self.assertEqual(entry.sp_inact, entry[6])
41n/a self.assertIsInstance(entry.sp_expire, int)
42n/a self.assertEqual(entry.sp_expire, entry[7])
43n/a self.assertIsInstance(entry.sp_flag, int)
44n/a self.assertEqual(entry.sp_flag, entry[8])
45n/a with self.assertRaises(KeyError) as cx:
46n/a spwd.getspnam('invalid user name')
47n/a self.assertEqual(str(cx.exception), "'getspnam(): name not found'")
48n/a self.assertRaises(TypeError, spwd.getspnam)
49n/a self.assertRaises(TypeError, spwd.getspnam, 0)
50n/a self.assertRaises(TypeError, spwd.getspnam, random_name, 0)
51n/a try:
52n/a bytes_name = os.fsencode(random_name)
53n/a except UnicodeEncodeError:
54n/a pass
55n/a else:
56n/a self.assertRaises(TypeError, spwd.getspnam, bytes_name)
57n/a
58n/a
59n/a@unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() != 0,
60n/a 'non-root user required')
61n/aclass TestSpwdNonRoot(unittest.TestCase):
62n/a
63n/a def test_getspnam_exception(self):
64n/a name = 'bin'
65n/a try:
66n/a with self.assertRaises(PermissionError) as cm:
67n/a spwd.getspnam(name)
68n/a except KeyError as exc:
69n/a self.skipTest("spwd entry %r doesn't exist: %s" % (name, exc))
70n/a else:
71n/a self.assertEqual(str(cm.exception), '[Errno 13] Permission denied')
72n/a
73n/a
74n/aif __name__ == "__main__":
75n/a unittest.main()