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