|
1
|
|
|
# Copyright (C) 2014-2021 Greenbone Networks GmbH |
|
2
|
|
|
# |
|
3
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later |
|
4
|
|
|
# |
|
5
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
# it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
# published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
# 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 Affero General Public License for more details. |
|
14
|
|
|
# |
|
15
|
|
|
# You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
|
|
18
|
|
|
import logging |
|
19
|
|
|
|
|
20
|
|
|
from hashlib import sha256 |
|
21
|
|
|
from unittest import TestCase |
|
22
|
|
|
from unittest.mock import Mock |
|
23
|
|
|
|
|
24
|
|
|
from collections import OrderedDict |
|
25
|
|
|
from ospd.errors import OspdError |
|
26
|
|
|
from ospd.vts import Vts |
|
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
|
|
|
self.assertIn(vt_id, ['id_1', 'id_2']) |
|
92
|
|
|
|
|
93
|
|
|
vt_id = next(it) |
|
94
|
|
|
self.assertIn(vt_id, ['id_1', 'id_2']) |
|
95
|
|
|
|
|
96
|
|
|
with self.assertRaises(StopIteration): |
|
97
|
|
|
next(it) |
|
98
|
|
|
|
|
99
|
|
|
def test_keys(self): |
|
100
|
|
|
vts = Vts() |
|
101
|
|
|
|
|
102
|
|
|
vts.add('id_1', name='foo') |
|
103
|
|
|
vts.add('id_2', name='bar') |
|
104
|
|
|
|
|
105
|
|
|
self.assertEqual(vts.keys(), ['id_1', 'id_2']) |
|
106
|
|
|
|
|
107
|
|
|
def test_getitem(self): |
|
108
|
|
|
vts = Vts() |
|
109
|
|
|
|
|
110
|
|
|
vts.add('id_1', name='foo') |
|
111
|
|
|
|
|
112
|
|
|
vt = vts['id_1'] |
|
113
|
|
|
|
|
114
|
|
|
self.assertEqual(vt['name'], 'foo') |
|
115
|
|
|
|
|
116
|
|
|
with self.assertRaises(KeyError): |
|
117
|
|
|
vt = vts['foo'] |
|
118
|
|
|
|
|
119
|
|
|
def test_copy(self): |
|
120
|
|
|
vts = Vts() |
|
121
|
|
|
|
|
122
|
|
|
vts.add('id_1', name='foo') |
|
123
|
|
|
vts.add('id_2', name='bar') |
|
124
|
|
|
|
|
125
|
|
|
vts2 = vts.copy() |
|
126
|
|
|
|
|
127
|
|
|
self.assertIsNot(vts, vts2) |
|
128
|
|
|
self.assertIsNot(vts.vts, vts2.vts) |
|
129
|
|
|
|
|
130
|
|
|
vta = vts.get('id_1') |
|
131
|
|
|
vtb = vts2.get('id_1') |
|
132
|
|
|
self.assertEqual(vta['name'], vtb['name']) |
|
133
|
|
|
self.assertIsNot(vta, vtb) |
|
134
|
|
|
|
|
135
|
|
|
vta = vts.get('id_2') |
|
136
|
|
|
vtb = vts2.get('id_2') |
|
137
|
|
|
self.assertEqual(vta['name'], vtb['name']) |
|
138
|
|
|
self.assertIsNot(vta, vtb) |
|
139
|
|
|
|
|
140
|
|
|
def test_calculate_vts_collection_hash(self): |
|
141
|
|
|
vts = Vts(storage=OrderedDict) |
|
142
|
|
|
|
|
143
|
|
|
vts.add( |
|
144
|
|
|
'id_1', |
|
145
|
|
|
name='foo', |
|
146
|
|
|
vt_modification_time='01234', |
|
147
|
|
|
vt_params={ |
|
148
|
|
|
'0': { |
|
149
|
|
|
'id': '0', |
|
150
|
|
|
'name': 'timeout', |
|
151
|
|
|
'default': '20', |
|
152
|
|
|
}, |
|
153
|
|
|
'1': { |
|
154
|
|
|
'id': '1', |
|
155
|
|
|
'name': 'foo_pref:', |
|
156
|
|
|
'default': 'bar_value', |
|
157
|
|
|
}, |
|
158
|
|
|
}, |
|
159
|
|
|
) |
|
160
|
|
|
vts.add('id_2', name='bar', vt_modification_time='56789') |
|
161
|
|
|
|
|
162
|
|
|
vts.calculate_vts_collection_hash() |
|
163
|
|
|
|
|
164
|
|
|
vt_hash = sha256() |
|
165
|
|
|
vt_hash.update( |
|
166
|
|
|
"id_1012340timeout201foo_pref:bar_valueid_256789".encode('utf-8') |
|
167
|
|
|
) |
|
168
|
|
|
hash_test = vt_hash.hexdigest() |
|
169
|
|
|
|
|
170
|
|
|
self.assertEqual(hash_test, vts.sha256_hash) |
|
171
|
|
|
|
|
172
|
|
|
def test_calculate_vts_collection_hash_no_params(self): |
|
173
|
|
|
vts = Vts(storage=OrderedDict) |
|
174
|
|
|
|
|
175
|
|
|
vts.add( |
|
176
|
|
|
'id_1', |
|
177
|
|
|
name='foo', |
|
178
|
|
|
vt_modification_time='01234', |
|
179
|
|
|
vt_params={ |
|
180
|
|
|
'0': { |
|
181
|
|
|
'id': '0', |
|
182
|
|
|
'name': 'timeout', |
|
183
|
|
|
'default': '20', |
|
184
|
|
|
}, |
|
185
|
|
|
'1': { |
|
186
|
|
|
'id': '1', |
|
187
|
|
|
'name': 'foo_pref:', |
|
188
|
|
|
'default': 'bar_value', |
|
189
|
|
|
}, |
|
190
|
|
|
}, |
|
191
|
|
|
) |
|
192
|
|
|
vts.add('id_2', name='bar', vt_modification_time='56789') |
|
193
|
|
|
|
|
194
|
|
|
vts.calculate_vts_collection_hash(include_vt_params=False) |
|
195
|
|
|
|
|
196
|
|
|
vt_hash = sha256() |
|
197
|
|
|
vt_hash.update("id_101234id_256789".encode('utf-8')) |
|
198
|
|
|
hash_test = vt_hash.hexdigest() |
|
199
|
|
|
|
|
200
|
|
|
self.assertEqual(hash_test, vts.sha256_hash) |
|
201
|
|
|
|
|
202
|
|
|
def test_calculate_vts_collection_hash_empty(self): |
|
203
|
|
|
vts = Vts() |
|
204
|
|
|
logging.Logger.debug = Mock() |
|
205
|
|
|
|
|
206
|
|
|
vts.calculate_vts_collection_hash() |
|
207
|
|
|
|
|
208
|
|
|
self.assertEqual(vts.sha256_hash, None) |
|
209
|
|
|
logging.Logger.debug.assert_called_with( # pylint: disable=no-member |
|
210
|
|
|
"Error calculating VTs collection hash. Cache is empty" |
|
211
|
|
|
) |
|
212
|
|
|
|