Fred Drake | 5b811be | 2001-05-18 21:38:52 +0000 | [diff] [blame] | 1 | """Test script for the grp module.""" |
| 2 | |
Fred Drake | 5b811be | 2001-05-18 21:38:52 +0000 | [diff] [blame] | 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Roger E. Masse | 749cc6b | 1996-12-18 19:36:34 +0000 | [diff] [blame] | 5 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 6 | grp = support.import_module('grp') |
| 7 | |
Fred Drake | 5b811be | 2001-05-18 21:38:52 +0000 | [diff] [blame] | 8 | class GroupDatabaseTestCase(unittest.TestCase): |
Roger E. Masse | 749cc6b | 1996-12-18 19:36:34 +0000 | [diff] [blame] | 9 | |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 10 | 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 15 | self.assertTrue(isinstance(value.gr_name, str)) |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 16 | self.assertEqual(value[1], value.gr_passwd) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 17 | self.assertTrue(isinstance(value.gr_passwd, str)) |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 18 | self.assertEqual(value[2], value.gr_gid) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 19 | self.assertTrue(isinstance(value.gr_gid, int)) |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 20 | self.assertEqual(value[3], value.gr_mem) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 21 | self.assertTrue(isinstance(value.gr_mem, list)) |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 22 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 23 | def test_values(self): |
| 24 | entries = grp.getgrall() |
Fred Drake | 5b811be | 2001-05-18 21:38:52 +0000 | [diff] [blame] | 25 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 26 | for e in entries: |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 27 | self.check_value(e) |
Walter Dörwald | 43bc1f1 | 2003-04-15 15:59:36 +0000 | [diff] [blame] | 28 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 29 | if len(entries) > 1000: # Huge group file (NIS?) -- skip the rest |
| 30 | return |
| 31 | |
Walter Dörwald | 43bc1f1 | 2003-04-15 15:59:36 +0000 | [diff] [blame] | 32 | for e in entries: |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 33 | e2 = grp.getgrgid(e.gr_gid) |
| 34 | self.check_value(e2) |
Walter Dörwald | ce6829a | 2003-05-05 20:37:33 +0000 | [diff] [blame] | 35 | self.assertEqual(e2.gr_gid, e.gr_gid) |
R. David Murray | d8fec5f | 2010-12-14 16:24:18 +0000 | [diff] [blame] | 36 | name = e.gr_name |
| 37 | if name.startswith('+') or name.startswith('-'): |
| 38 | # NIS-related entry |
| 39 | continue |
| 40 | e2 = grp.getgrnam(name) |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 41 | self.check_value(e2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 42 | # 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 Murray | d8fec5f | 2010-12-14 16:24:18 +0000 | [diff] [blame] | 45 | self.assertEqual(e2.gr_name.lower(), name.lower()) |
Fred Drake | 5b811be | 2001-05-18 21:38:52 +0000 | [diff] [blame] | 46 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 47 | 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 Lundh | 0e664e1 | 2005-12-12 20:53:40 +0000 | [diff] [blame] | 56 | if not n or n == '+': |
| 57 | continue # skip NIS entries etc. |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 58 | bynames[n] = g |
| 59 | bygids[g] = n |
| 60 | |
Guido van Rossum | 9afbb4e | 2007-02-11 18:54:18 +0000 | [diff] [blame] | 61 | allnames = list(bynames.keys()) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 62 | namei = 0 |
| 63 | fakename = allnames[namei] |
| 64 | while fakename in bynames: |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 65 | chars = list(fakename) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 66 | for i in range(len(chars)): |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 67 | 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 Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 82 | fakename = ''.join(chars) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 83 | |
| 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 Drake | 5b811be | 2001-05-18 21:38:52 +0000 | [diff] [blame] | 92 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 93 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 94 | support.run_unittest(GroupDatabaseTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 95 | |
| 96 | if __name__ == "__main__": |
| 97 | test_main() |