• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import os
2import unittest
3from test import test_support
4
5spwd = test_support.import_module('spwd')
6
7
8@unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() == 0,
9                     'root privileges required')
10class TestSpwdRoot(unittest.TestCase):
11
12    def test_getspall(self):
13        entries = spwd.getspall()
14        self.assertIsInstance(entries, list)
15        for entry in entries:
16            self.assertIsInstance(entry, spwd.struct_spwd)
17
18    def test_getspnam(self):
19        entries = spwd.getspall()
20        if not entries:
21            self.skipTest('empty shadow password database')
22        random_name = entries[0].sp_nam
23        entry = spwd.getspnam(random_name)
24        self.assertIsInstance(entry, spwd.struct_spwd)
25        self.assertEqual(entry.sp_nam, random_name)
26        self.assertEqual(entry.sp_nam, entry[0])
27        self.assertIsInstance(entry.sp_pwd, str)
28        self.assertEqual(entry.sp_pwd, entry[1])
29        self.assertIsInstance(entry.sp_lstchg, int)
30        self.assertEqual(entry.sp_lstchg, entry[2])
31        self.assertIsInstance(entry.sp_min, int)
32        self.assertEqual(entry.sp_min, entry[3])
33        self.assertIsInstance(entry.sp_max, int)
34        self.assertEqual(entry.sp_max, entry[4])
35        self.assertIsInstance(entry.sp_warn, int)
36        self.assertEqual(entry.sp_warn, entry[5])
37        self.assertIsInstance(entry.sp_inact, int)
38        self.assertEqual(entry.sp_inact, entry[6])
39        self.assertIsInstance(entry.sp_expire, int)
40        self.assertEqual(entry.sp_expire, entry[7])
41        self.assertIsInstance(entry.sp_flag, int)
42        self.assertEqual(entry.sp_flag, entry[8])
43        with self.assertRaises(KeyError) as cx:
44            spwd.getspnam('invalid user name')
45        self.assertEqual(str(cx.exception), "'getspnam(): name not found'")
46        self.assertRaises(TypeError, spwd.getspnam)
47        self.assertRaises(TypeError, spwd.getspnam, 0)
48        self.assertRaises(TypeError, spwd.getspnam, random_name, 0)
49        if test_support.have_unicode:
50            try:
51                unicode_name = unicode(random_name)
52            except UnicodeDecodeError:
53                pass
54            else:
55                self.assertEqual(spwd.getspnam(unicode_name), entry)
56
57
58def test_main():
59    test_support.run_unittest(TestSpwdRoot)
60
61if __name__ == "__main__":
62    test_main()
63