1# Copyright 2007 Google Inc. 2# 3# This program is free software; you can redistribute it and/or 4# modify it under the terms of the GNU General Public License 5# as published by the Free Software Foundation; either version 2 6# of the License, or (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program; if not, write to the Free Software Foundation, 15# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16"""Unit tests for group.py. 17 18We only test what is overridden in the group subclasses, most 19functionality is in base.py and tested in passwd_test.py since a 20subclass is required to test the abstract class functionality. 21""" 22 23__author__ = 'vasilios@google.com (Vasilios Hoffman)' 24 25import unittest 26 27from nss_cache.maps import group 28from nss_cache.maps import passwd 29 30 31class TestGroupMap(unittest.TestCase): 32 """Tests for the GroupMap class.""" 33 34 def __init__(self, obj): 35 """Set some default avalible data for testing.""" 36 super(TestGroupMap, self).__init__(obj) 37 self._good_entry = group.GroupMapEntry() 38 self._good_entry.name = 'foo' 39 self._good_entry.passwd = 'x' 40 self._good_entry.gid = 10 41 self._good_entry.members = ['foo', 'bar'] 42 43 def testInit(self): 44 """Construct an empty or seeded GroupMap.""" 45 self.assertEqual(group.GroupMap, 46 type(group.GroupMap()), 47 msg='failed to create an empty GroupMap') 48 gmap = group.GroupMap([self._good_entry]) 49 self.assertEqual(self._good_entry, 50 gmap.PopItem(), 51 msg='failed to seed GroupMap with list') 52 self.assertRaises(TypeError, group.GroupMap, ['string']) 53 54 def testAdd(self): 55 """Add throws an error for objects it can't verify.""" 56 gmap = group.GroupMap() 57 entry = self._good_entry 58 self.assertTrue(gmap.Add(entry), msg='failed to append new entry.') 59 60 self.assertEqual(1, len(gmap), msg='unexpected size for Map.') 61 62 ret_entry = gmap.PopItem() 63 self.assertEqual(ret_entry, entry, msg='failed to pop correct entry.') 64 65 pentry = passwd.PasswdMapEntry() 66 pentry.name = 'foo' 67 pentry.uid = 10 68 pentry.gid = 10 69 self.assertRaises(TypeError, gmap.Add, pentry) 70 71 72class TestGroupMapEntry(unittest.TestCase): 73 """Tests for the GroupMapEntry class.""" 74 75 def testInit(self): 76 """Construct an empty and seeded GroupMapEntry.""" 77 self.assertTrue(group.GroupMapEntry(), 78 msg='Could not create empty GroupMapEntry') 79 seed = {'name': 'foo', 'gid': 10} 80 entry = group.GroupMapEntry(seed) 81 self.assertTrue(entry.Verify(), 82 msg='Could not verify seeded PasswdMapEntry') 83 self.assertEqual(entry.name, 84 'foo', 85 msg='Entry returned wrong value for name') 86 self.assertEqual(entry.passwd, 87 'x', 88 msg='Entry returned wrong value for passwd') 89 self.assertEqual(entry.gid, 90 10, 91 msg='Entry returned wrong value for gid') 92 self.assertEqual(entry.members, [], 93 msg='Entry returned wrong value for members') 94 95 def testAttributes(self): 96 """Test that we can get and set all expected attributes.""" 97 entry = group.GroupMapEntry() 98 entry.name = 'foo' 99 self.assertEqual(entry.name, 'foo', msg='Could not set attribute: name') 100 entry.passwd = 'x' 101 self.assertEqual(entry.passwd, 102 'x', 103 msg='Could not set attribute: passwd') 104 entry.gid = 10 105 self.assertEqual(entry.gid, 10, msg='Could not set attribute: gid') 106 members = ['foo', 'bar'] 107 entry.members = members 108 self.assertEqual(entry.members, 109 members, 110 msg='Could not set attribute: members') 111 112 def testVerify(self): 113 """Test that the object can verify it's attributes and itself.""" 114 entry = group.GroupMapEntry() 115 116 # Empty object should bomb 117 self.assertFalse(entry.Verify()) 118 119 def testKey(self): 120 """Key() should return the value of the 'name' attribute.""" 121 entry = group.GroupMapEntry() 122 entry.name = 'foo' 123 self.assertEqual(entry.Key(), entry.name) 124 125 126if __name__ == '__main__': 127 unittest.main() 128