Passed
Push — master ( a72f9d...dc472d )
by
unknown
01:54 queued 27s
created

tests.test_vts.VtsTestCase.test_keys()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
# Copyright (C) 2020 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: GPL-2.0-or-later
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
import logging
20
21
from unittest import TestCase
22
from unittest.mock import Mock
23
24
from ospd.errors import OspdError
25
from ospd.vts import Vts
26
from hashlib import sha256
27
28
29
class VtsTestCase(TestCase):
30
    def test_add_vt(self):
31
        vts = Vts()
32
33
        vts.add('id_1', name='foo')
34
35
        self.assertEqual(len(vts.vts), 1)
36
37
    def test_add_duplicate_vt(self):
38
        vts = Vts()
39
40
        vts.add('id_1', name='foo')
41
42
        with self.assertRaises(OspdError):
43
            vts.add('id_1', name='bar')
44
45
        self.assertEqual(len(vts.vts), 1)
46
47
    def test_add_vt_with_empty_id(self):
48
        vts = Vts()
49
50
        with self.assertRaises(OspdError):
51
            vts.add(None, name='foo')
52
53
        with self.assertRaises(OspdError):
54
            vts.add('', name='foo')
55
56
    def test_add_vt_with_invalid_id(self):
57
        vts = Vts()
58
59
        with self.assertRaises(OspdError):
60
            vts.add('$$$_1', name='foo')
61
62
        self.assertEqual(len(vts.vts), 0)
63
64
    def test_contains(self):
65
        vts = Vts()
66
67
        vts.add('id_1', name='foo')
68
69
        self.assertIn('id_1', vts)
70
71
    def test_get(self):
72
        vts = Vts()
73
74
        vts.add('id_1', name='foo')
75
        vt = vts.get('id_1')
76
77
        self.assertIsNotNone(vt)
78
        self.assertEqual(vt['name'], 'foo')
79
80
        self.assertIsNone(vt.get('bar'))
81
82
    def test_iterator(self):
83
        vts = Vts()
84
85
        vts.add('id_1', name='foo')
86
        vts.add('id_2', name='bar')
87
88
        it = iter(vts)
89
90
        vt_id = next(it)
91
        # Python 3.5 doesn't ensure the order of the retuned keys
92
        self.assertIn(vt_id, ['id_1', 'id_2'])
93
94
        vt_id = next(it)
95
        self.assertIn(vt_id, ['id_1', 'id_2'])
96
97
        with self.assertRaises(StopIteration):
98
            next(it)
99
100
    def test_keys(self):
101
        vts = Vts()
102
103
        vts.add('id_1', name='foo')
104
        vts.add('id_2', name='bar')
105
106
        # use assertCountEqual for Python 3.5 because dict.keys order is
107
        # undefined
108
        self.assertCountEqual(vts.keys(), ['id_1', 'id_2'])
109
110
    def test_getitem(self):
111
        vts = Vts()
112
113
        vts.add('id_1', name='foo')
114
115
        vt = vts['id_1']
116
117
        self.assertEqual(vt['name'], 'foo')
118
119
        with self.assertRaises(KeyError):
120
            vt = vts['foo']
121
122
    def test_copy(self):
123
        vts = Vts()
124
125
        vts.add('id_1', name='foo')
126
        vts.add('id_2', name='bar')
127
128
        vts2 = vts.copy()
129
130
        self.assertIsNot(vts, vts2)
131
        self.assertIsNot(vts.vts, vts2.vts)
132
133
        vta = vts.get('id_1')
134
        vtb = vts2.get('id_1')
135
        self.assertEqual(vta['name'], vtb['name'])
136
        self.assertIsNot(vta, vtb)
137
138
        vta = vts.get('id_2')
139
        vtb = vts2.get('id_2')
140
        self.assertEqual(vta['name'], vtb['name'])
141
        self.assertIsNot(vta, vtb)
142
143
    def test_calculate_vts_collection_hash(self):
144
        vts = Vts()
145
146
        vts.add('id_2', name='bar', vt_modification_time='56789')
147
        vts.add('id_1', name='foo', vt_modification_time='01234')
148
        vts.calculate_vts_collection_hash()
149
150
        h = sha256()
151
        h.update("id_101234id_256789".encode('utf-8'))
152
        hash_test = h.hexdigest()
153
154
        self.assertEqual(hash_test, vts.sha256_hash)
155
156
    def test_calculate_vts_collection_hash_empty(self):
157
        vts = Vts()
158
        logging.Logger.debug = Mock()
159
160
        vts.calculate_vts_collection_hash()
161
162
        self.assertEqual(vts.sha256_hash, None)
163
        logging.Logger.debug.assert_called_with(
164
            "Error calculating VTs collection hash. Cache is empty"
165
        )
166