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) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 15 | self.assertIsInstance(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) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 17 | self.assertIsInstance(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) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 19 | self.assertIsInstance(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) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 21 | self.assertIsInstance(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 | |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 29 | def test_values_extended(self): |
| 30 | entries = grp.getgrall() |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 31 | if len(entries) > 1000: # Huge group file (NIS?) -- skip the rest |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 32 | self.skipTest('huge group file, extended test skipped') |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 33 | |
Walter Dörwald | 43bc1f1 | 2003-04-15 15:59:36 +0000 | [diff] [blame] | 34 | for e in entries: |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 35 | e2 = grp.getgrgid(e.gr_gid) |
| 36 | self.check_value(e2) |
Walter Dörwald | ce6829a | 2003-05-05 20:37:33 +0000 | [diff] [blame] | 37 | self.assertEqual(e2.gr_gid, e.gr_gid) |
R. David Murray | ec07331 | 2010-12-14 16:20:53 +0000 | [diff] [blame] | 38 | name = e.gr_name |
| 39 | if name.startswith('+') or name.startswith('-'): |
| 40 | # NIS-related entry |
| 41 | continue |
| 42 | e2 = grp.getgrnam(name) |
Walter Dörwald | ce07c8a | 2003-04-23 19:50:24 +0000 | [diff] [blame] | 43 | self.check_value(e2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 44 | # There are instances where getgrall() returns group names in |
| 45 | # lowercase while getgrgid() returns proper casing. |
| 46 | # Discovered on Ubuntu 5.04 (custom). |
R. David Murray | ec07331 | 2010-12-14 16:20:53 +0000 | [diff] [blame] | 47 | self.assertEqual(e2.gr_name.lower(), name.lower()) |
Fred Drake | 5b811be | 2001-05-18 21:38:52 +0000 | [diff] [blame] | 48 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 49 | def test_errors(self): |
| 50 | self.assertRaises(TypeError, grp.getgrgid) |
| 51 | self.assertRaises(TypeError, grp.getgrnam) |
| 52 | self.assertRaises(TypeError, grp.getgrall, 42) |
| 53 | |
| 54 | # try to get some errors |
| 55 | bynames = {} |
| 56 | bygids = {} |
| 57 | for (n, p, g, mem) in grp.getgrall(): |
Fredrik Lundh | 0e664e1 | 2005-12-12 20:53:40 +0000 | [diff] [blame] | 58 | if not n or n == '+': |
| 59 | continue # skip NIS entries etc. |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 60 | bynames[n] = g |
| 61 | bygids[g] = n |
| 62 | |
Guido van Rossum | 9afbb4e | 2007-02-11 18:54:18 +0000 | [diff] [blame] | 63 | allnames = list(bynames.keys()) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 64 | namei = 0 |
| 65 | fakename = allnames[namei] |
| 66 | while fakename in bynames: |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 67 | chars = list(fakename) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 68 | for i in range(len(chars)): |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 69 | if chars[i] == 'z': |
| 70 | chars[i] = 'A' |
| 71 | break |
| 72 | elif chars[i] == 'Z': |
| 73 | continue |
| 74 | else: |
| 75 | chars[i] = chr(ord(chars[i]) + 1) |
| 76 | break |
| 77 | else: |
| 78 | namei = namei + 1 |
| 79 | try: |
| 80 | fakename = allnames[namei] |
| 81 | except IndexError: |
| 82 | # should never happen... if so, just forget it |
| 83 | break |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 84 | fakename = ''.join(chars) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 85 | |
| 86 | self.assertRaises(KeyError, grp.getgrnam, fakename) |
| 87 | |
| 88 | # Choose a non-existent gid. |
| 89 | fakegid = 4127 |
| 90 | while fakegid in bygids: |
| 91 | fakegid = (fakegid * 3) % 0x10000 |
| 92 | |
| 93 | self.assertRaises(KeyError, grp.getgrgid, fakegid) |
Fred Drake | 5b811be | 2001-05-18 21:38:52 +0000 | [diff] [blame] | 94 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 95 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 96 | unittest.main() |