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 netgroup.py.
17
18We only test what is overridden in the netgroup 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 netgroup
28from nss_cache.maps import passwd
29
30
31class TestNetgroupMap(unittest.TestCase):
32    """Tests for the NetgroupMap class."""
33
34    def __init__(self, obj):
35        """Set some default avalible data for testing."""
36        super(TestNetgroupMap, self).__init__(obj)
37        self._good_entry = netgroup.NetgroupMapEntry()
38        self._good_entry.name = 'foo'
39        self._good_entry.entries = [('-', 'bob', None), 'othernetgroup']
40
41    def testInit(self):
42        """Construct an empty or seeded NetgroupMap."""
43        self.assertEqual(netgroup.NetgroupMap,
44                         type(netgroup.NetgroupMap()),
45                         msg='failed to create an empty NetgroupMap')
46        nmap = netgroup.NetgroupMap([self._good_entry])
47        self.assertEqual(self._good_entry,
48                         nmap.PopItem(),
49                         msg='failed to seed NetgroupMap with list')
50        self.assertRaises(TypeError, netgroup.NetgroupMap, ['string'])
51
52    def testAdd(self):
53        """Add throws an error for objects it can't verify."""
54        nmap = netgroup.NetgroupMap()
55        entry = self._good_entry
56        self.assertTrue(nmap.Add(entry), msg='failed to append new entry.')
57
58        self.assertEqual(1, len(nmap), msg='unexpected size for Map.')
59
60        ret_entry = nmap.PopItem()
61        self.assertEqual(ret_entry, entry, msg='failed to pop correct entry.')
62
63        pentry = passwd.PasswdMapEntry()
64        pentry.name = 'foo'
65        pentry.uid = 10
66        pentry.gid = 10
67        self.assertRaises(TypeError, nmap.Add, pentry)
68
69
70class TestNetgroupMapEntry(unittest.TestCase):
71    """Tests for the NetgroupMapEntry class."""
72
73    def testInit(self):
74        """Construct an empty and seeded NetgroupMapEntry."""
75        self.assertTrue(netgroup.NetgroupMapEntry(),
76                        msg='Could not create empty NetgroupMapEntry')
77        entries = ['bar', ('baz', '-', None)]
78        seed = {'name': 'foo', 'entries': entries}
79        entry = netgroup.NetgroupMapEntry(seed)
80        self.assertTrue(entry.Verify(),
81                        msg='Could not verify seeded NetgroupMapEntry')
82        self.assertEqual(entry.name,
83                         'foo',
84                         msg='Entry returned wrong value for name')
85        self.assertEqual(entry.entries,
86                         entries,
87                         msg='Entry returned wrong value for entries')
88
89    def testAttributes(self):
90        """Test that we can get and set all expected attributes."""
91        entry = netgroup.NetgroupMapEntry()
92        entry.name = 'foo'
93        self.assertEqual(entry.name, 'foo', msg='Could not set attribute: name')
94        entries = ['foo', '(-,bar,)']
95        entry.entries = entries
96        self.assertEqual(entry.entries,
97                         entries,
98                         msg='Could not set attribute: entries')
99
100    def testVerify(self):
101        """Test that the object can verify it's attributes and itself."""
102        entry = netgroup.NetgroupMapEntry()
103
104        # Empty object should bomb
105        self.assertFalse(entry.Verify())
106
107    def testKey(self):
108        """Key() should return the value of the 'name' attribute."""
109        entry = netgroup.NetgroupMapEntry()
110        entry.name = 'foo'
111        self.assertEqual(entry.Key(), entry.name)
112
113
114if __name__ == '__main__':
115    unittest.main()
116