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