blob: 8acc97c638a06661c38a0e0d198c510ee18bf856 [file] [log] [blame]
Fred Drake5b811be2001-05-18 21:38:52 +00001"""Test script for the grp module."""
2
Fred Drake5b811be2001-05-18 21:38:52 +00003import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Roger E. Masse749cc6b1996-12-18 19:36:34 +00005
R. David Murraya21e4ca2009-03-31 23:16:50 +00006grp = support.import_module('grp')
7
Fred Drake5b811be2001-05-18 21:38:52 +00008class GroupDatabaseTestCase(unittest.TestCase):
Roger E. Masse749cc6b1996-12-18 19:36:34 +00009
Walter Dörwaldce07c8a2003-04-23 19:50:24 +000010 def check_value(self, value):
11 # check that a grp tuple has the entries and
12 # attributes promised by the docs
13 self.assertEqual(len(value), 4)
14 self.assertEqual(value[0], value.gr_name)
Georg Brandlab91fde2009-08-13 08:51:18 +000015 self.assertTrue(isinstance(value.gr_name, str))
Walter Dörwaldce07c8a2003-04-23 19:50:24 +000016 self.assertEqual(value[1], value.gr_passwd)
Georg Brandlab91fde2009-08-13 08:51:18 +000017 self.assertTrue(isinstance(value.gr_passwd, str))
Walter Dörwaldce07c8a2003-04-23 19:50:24 +000018 self.assertEqual(value[2], value.gr_gid)
Georg Brandlab91fde2009-08-13 08:51:18 +000019 self.assertTrue(isinstance(value.gr_gid, int))
Walter Dörwaldce07c8a2003-04-23 19:50:24 +000020 self.assertEqual(value[3], value.gr_mem)
Georg Brandlab91fde2009-08-13 08:51:18 +000021 self.assertTrue(isinstance(value.gr_mem, list))
Walter Dörwaldce07c8a2003-04-23 19:50:24 +000022
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000023 def test_values(self):
24 entries = grp.getgrall()
Fred Drake5b811be2001-05-18 21:38:52 +000025
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000026 for e in entries:
Walter Dörwaldce07c8a2003-04-23 19:50:24 +000027 self.check_value(e)
Walter Dörwald43bc1f12003-04-15 15:59:36 +000028
Christian Heimes1af737c2008-01-23 08:24:23 +000029 if len(entries) > 1000: # Huge group file (NIS?) -- skip the rest
30 return
31
Walter Dörwald43bc1f12003-04-15 15:59:36 +000032 for e in entries:
Walter Dörwaldce07c8a2003-04-23 19:50:24 +000033 e2 = grp.getgrgid(e.gr_gid)
34 self.check_value(e2)
Walter Dörwaldce6829a2003-05-05 20:37:33 +000035 self.assertEqual(e2.gr_gid, e.gr_gid)
R. David Murrayd8fec5f2010-12-14 16:24:18 +000036 name = e.gr_name
37 if name.startswith('+') or name.startswith('-'):
38 # NIS-related entry
39 continue
40 e2 = grp.getgrnam(name)
Walter Dörwaldce07c8a2003-04-23 19:50:24 +000041 self.check_value(e2)
Thomas Wouters477c8d52006-05-27 19:21:47 +000042 # There are instances where getgrall() returns group names in
43 # lowercase while getgrgid() returns proper casing.
44 # Discovered on Ubuntu 5.04 (custom).
R. David Murrayd8fec5f2010-12-14 16:24:18 +000045 self.assertEqual(e2.gr_name.lower(), name.lower())
Fred Drake5b811be2001-05-18 21:38:52 +000046
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000047 def test_errors(self):
48 self.assertRaises(TypeError, grp.getgrgid)
49 self.assertRaises(TypeError, grp.getgrnam)
50 self.assertRaises(TypeError, grp.getgrall, 42)
51
52 # try to get some errors
53 bynames = {}
54 bygids = {}
55 for (n, p, g, mem) in grp.getgrall():
Fredrik Lundh0e664e12005-12-12 20:53:40 +000056 if not n or n == '+':
57 continue # skip NIS entries etc.
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000058 bynames[n] = g
59 bygids[g] = n
60
Guido van Rossum9afbb4e2007-02-11 18:54:18 +000061 allnames = list(bynames.keys())
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000062 namei = 0
63 fakename = allnames[namei]
64 while fakename in bynames:
Guido van Rossumc1f779c2007-07-03 08:25:58 +000065 chars = list(fakename)
Guido van Rossum805365e2007-05-07 22:24:25 +000066 for i in range(len(chars)):
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000067 if chars[i] == 'z':
68 chars[i] = 'A'
69 break
70 elif chars[i] == 'Z':
71 continue
72 else:
73 chars[i] = chr(ord(chars[i]) + 1)
74 break
75 else:
76 namei = namei + 1
77 try:
78 fakename = allnames[namei]
79 except IndexError:
80 # should never happen... if so, just forget it
81 break
Guido van Rossumc1f779c2007-07-03 08:25:58 +000082 fakename = ''.join(chars)
Walter Dörwaldb1ded1e2003-04-15 11:10:33 +000083
84 self.assertRaises(KeyError, grp.getgrnam, fakename)
85
86 # Choose a non-existent gid.
87 fakegid = 4127
88 while fakegid in bygids:
89 fakegid = (fakegid * 3) % 0x10000
90
91 self.assertRaises(KeyError, grp.getgrgid, fakegid)
Fred Drake5b811be2001-05-18 21:38:52 +000092
Fred Drake2e2be372001-09-20 21:33:42 +000093def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000094 support.run_unittest(GroupDatabaseTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +000095
96if __name__ == "__main__":
97 test_main()