summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pwd.py
blob: bb07c2b699898c8d473ffa73684bd0570d271774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import unittest
from test import test_support

import pwd

class PwdTest(unittest.TestCase):

    def test_values(self):
        entries = pwd.getpwall()
        entriesbyname = {}
        entriesbyuid = {}

        for e in entries:
            self.assertEqual(len(e), 7)
            self.assertEqual(e[0], e.pw_name)
            self.assert_(isinstance(e.pw_name, basestring))
            self.assertEqual(e[1], e.pw_passwd)
            self.assert_(isinstance(e.pw_passwd, basestring))
            self.assertEqual(e[2], e.pw_uid)
            self.assert_(isinstance(e.pw_uid, int))
            self.assertEqual(e[3], e.pw_gid)
            self.assert_(isinstance(e.pw_gid, int))
            self.assertEqual(e[4], e.pw_gecos)
            self.assert_(isinstance(e.pw_gecos, basestring))
            self.assertEqual(e[5], e.pw_dir)
            self.assert_(isinstance(e.pw_dir, basestring))
            self.assertEqual(e[6], e.pw_shell)
            self.assert_(isinstance(e.pw_shell, basestring))

            # The following won't work, because of duplicate entries
            # for one uid
            #    self.assertEqual(pwd.getpwuid(e.pw_uid), e)
            # instead of this collect all entries for one uid
            # and check afterwards
            entriesbyname.setdefault(e.pw_name, []).append(e)
            entriesbyuid.setdefault(e.pw_uid, []).append(e)

        # check whether the entry returned by getpwuid()
        # for each uid is among those from getpwall() for this uid
        for e in entries:
            self.assert_(pwd.getpwnam(e.pw_name) in entriesbyname[e.pw_name])
            self.assert_(pwd.getpwuid(e.pw_uid) in entriesbyuid[e.pw_uid])

    def test_errors(self):
        self.assertRaises(TypeError, pwd.getpwuid)
        self.assertRaises(TypeError, pwd.getpwnam)
        self.assertRaises(TypeError, pwd.getpwall, 42)

        # try to get some errors
        bynames = {}
        byuids = {}
        for (n, p, u, g, gecos, d, s) in pwd.getpwall():
            bynames[n] = u
            byuids[u] = n

        allnames = bynames.keys()
        namei = 0
        fakename = allnames[namei]
        while fakename in bynames:
            chars = map(None, fakename)
            for i in xrange(len(chars)):
                if chars[i] == 'z':
                    chars[i] = 'A'
                    break
                elif chars[i] == 'Z':
                    continue
                else:
                    chars[i] = chr(ord(chars[i]) + 1)
                    break
            else:
                namei = namei + 1
                try:
                    fakename = allnames[namei]
                except IndexError:
                    # should never happen... if so, just forget it
                    break
            fakename = ''.join(map(None, chars))

        self.assertRaises(KeyError, pwd.getpwnam, fakename)

        # Choose a non-existent uid.
        fakeuid = 4127
        while fakeuid in byuids:
            fakeuid = (fakeuid * 3) % 0x10000

        self.assertRaises(KeyError, pwd.getpwuid, fakeuid)

def test_main():
    test_support.run_unittest(PwdTest)

if __name__ == "__main__":
    test_main()