PyXR

c:\python24\lib \ test \ test_grp.py



0001 """Test script for the grp module."""
0002 
0003 import grp
0004 import unittest
0005 from test import test_support
0006 
0007 class GroupDatabaseTestCase(unittest.TestCase):
0008 
0009     def check_value(self, value):
0010         # check that a grp tuple has the entries and
0011         # attributes promised by the docs
0012         self.assertEqual(len(value), 4)
0013         self.assertEqual(value[0], value.gr_name)
0014         self.assert_(isinstance(value.gr_name, basestring))
0015         self.assertEqual(value[1], value.gr_passwd)
0016         self.assert_(isinstance(value.gr_passwd, basestring))
0017         self.assertEqual(value[2], value.gr_gid)
0018         self.assert_(isinstance(value.gr_gid, int))
0019         self.assertEqual(value[3], value.gr_mem)
0020         self.assert_(isinstance(value.gr_mem, list))
0021 
0022     def test_values(self):
0023         entries = grp.getgrall()
0024 
0025         for e in entries:
0026             self.check_value(e)
0027 
0028         for e in entries:
0029             e2 = grp.getgrgid(e.gr_gid)
0030             self.check_value(e2)
0031             self.assertEqual(e2.gr_gid, e.gr_gid)
0032             e2 = grp.getgrnam(e.gr_name)
0033             self.check_value(e2)
0034             self.assertEqual(e2.gr_name, e.gr_name)
0035 
0036     def test_errors(self):
0037         self.assertRaises(TypeError, grp.getgrgid)
0038         self.assertRaises(TypeError, grp.getgrnam)
0039         self.assertRaises(TypeError, grp.getgrall, 42)
0040 
0041         # try to get some errors
0042         bynames = {}
0043         bygids = {}
0044         for (n, p, g, mem) in grp.getgrall():
0045             bynames[n] = g
0046             bygids[g] = n
0047 
0048         allnames = bynames.keys()
0049         namei = 0
0050         fakename = allnames[namei]
0051         while fakename in bynames:
0052             chars = map(None, fakename)
0053             for i in xrange(len(chars)):
0054                 if chars[i] == 'z':
0055                     chars[i] = 'A'
0056                     break
0057                 elif chars[i] == 'Z':
0058                     continue
0059                 else:
0060                     chars[i] = chr(ord(chars[i]) + 1)
0061                     break
0062             else:
0063                 namei = namei + 1
0064                 try:
0065                     fakename = allnames[namei]
0066                 except IndexError:
0067                     # should never happen... if so, just forget it
0068                     break
0069             fakename = ''.join(map(None, chars))
0070 
0071         self.assertRaises(KeyError, grp.getgrnam, fakename)
0072 
0073         # Choose a non-existent gid.
0074         fakegid = 4127
0075         while fakegid in bygids:
0076             fakegid = (fakegid * 3) % 0x10000
0077 
0078         self.assertRaises(KeyError, grp.getgrgid, fakegid)
0079 
0080 def test_main():
0081     test_support.run_unittest(GroupDatabaseTestCase)
0082 
0083 if __name__ == "__main__":
0084     test_main()
0085 

Generated by PyXR 0.9.4
SourceForge.net Logo