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 shadow.py.
17
18We only test what is overridden in the shadow 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 passwd
28from nss_cache.maps import shadow
29
30
31class TestShadowMap(unittest.TestCase):
32    """Tests for the ShadowMap class."""
33
34    def __init__(self, obj):
35        """Set some default avalible data for testing."""
36        super(TestShadowMap, self).__init__(obj)
37        self._good_entry = shadow.ShadowMapEntry()
38        self._good_entry.name = 'foo'
39        self._good_entry.lstchg = None
40        self._good_entry.min = None
41        self._good_entry.max = None
42        self._good_entry.warn = None
43        self._good_entry.inact = None
44        self._good_entry.expire = None
45        self._good_entry.flag = None
46
47    def testInit(self):
48        """Construct an empty or seeded ShadowMap."""
49        self.assertEqual(shadow.ShadowMap,
50                         type(shadow.ShadowMap()),
51                         msg='failed to create emtpy ShadowMap')
52        smap = shadow.ShadowMap([self._good_entry])
53        self.assertEqual(self._good_entry,
54                         smap.PopItem(),
55                         msg='failed to seed ShadowMap with list')
56        self.assertRaises(TypeError, shadow.ShadowMap, ['string'])
57
58    def testAdd(self):
59        """Add throws an error for objects it can't verify."""
60        smap = shadow.ShadowMap()
61        entry = self._good_entry
62        self.assertTrue(smap.Add(entry), msg='failed to append new entry.')
63
64        self.assertEqual(1, len(smap), msg='unexpected size for Map.')
65
66        ret_entry = smap.PopItem()
67        self.assertEqual(ret_entry, entry, msg='failed to pop existing entry.')
68
69        pentry = passwd.PasswdMapEntry()
70        pentry.name = 'foo'
71        pentry.uid = 10
72        pentry.gid = 10
73        self.assertRaises(TypeError, smap.Add, pentry)
74
75
76class TestShadowMapEntry(unittest.TestCase):
77    """Tests for the ShadowMapEntry class."""
78
79    def testInit(self):
80        """Construct empty and seeded ShadowMapEntry."""
81        self.assertTrue(shadow.ShadowMapEntry(),
82                        msg='Could not create empty ShadowMapEntry')
83        seed = {'name': 'foo'}
84        entry = shadow.ShadowMapEntry(seed)
85        self.assertTrue(entry.Verify(),
86                        msg='Could not verify seeded ShadowMapEntry')
87        self.assertEqual(entry.name,
88                         'foo',
89                         msg='Entry returned wrong value for name')
90        self.assertEqual(entry.passwd,
91                         '!!',
92                         msg='Entry returned wrong value for passwd')
93        self.assertEqual(entry.lstchg,
94                         None,
95                         msg='Entry returned wrong value for lstchg')
96        self.assertEqual(entry.min,
97                         None,
98                         msg='Entry returned wrong value for min')
99        self.assertEqual(entry.max,
100                         None,
101                         msg='Entry returned wrong value for max')
102        self.assertEqual(entry.warn,
103                         None,
104                         msg='Entry returned wrong value for warn')
105        self.assertEqual(entry.inact,
106                         None,
107                         msg='Entry returned wrong value for inact')
108        self.assertEqual(entry.expire,
109                         None,
110                         msg='Entry returned wrong value for expire')
111        self.assertEqual(entry.flag,
112                         None,
113                         msg='Entry returned wrong value for flag')
114
115    def testAttributes(self):
116        """Test that we can get and set all expected attributes."""
117        entry = shadow.ShadowMapEntry()
118        entry.name = 'foo'
119        self.assertEqual(entry.name, 'foo', msg='Could not set attribute: name')
120        entry.passwd = 'seekret'
121        self.assertEqual(entry.passwd,
122                         'seekret',
123                         msg='Could not set attribute: passwd')
124        entry.lstchg = 0
125        self.assertEqual(entry.lstchg, 0, msg='Could not set attribute: lstchg')
126        entry.min = 0
127        self.assertEqual(entry.min, 0, msg='Could not set attribute: min')
128        entry.max = 0
129        self.assertEqual(entry.max, 0, msg='Could not set attribute: max')
130        entry.warn = 0
131        self.assertEqual(entry.warn, 0, msg='Could not set attribute: warn')
132        entry.inact = 0
133        self.assertEqual(entry.inact, 0, msg='Could not set attribute: inact')
134        entry.expire = 0
135        self.assertEqual(entry.expire, 0, msg='Could not set attribute: expire')
136        entry.flag = 0
137        self.assertEqual(entry.flag, 0, msg='Could not set attribute: flag')
138
139    def testVerify(self):
140        """Test that the object can verify it's attributes and itself."""
141        entry = shadow.ShadowMapEntry()
142
143        # Emtpy object should bomb
144        self.assertFalse(entry.Verify())
145
146    def testKey(self):
147        """Key() should return the value of the 'name' attribute."""
148        entry = shadow.ShadowMapEntry()
149        entry.name = 'foo'
150        self.assertEqual(entry.Key(), entry.name)
151
152
153if __name__ == '__main__':
154    unittest.main()
155