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