PageRenderTime 66ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 35ms app.codeStats 0ms

/Lib/test/test_grp.py

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