Passed
Pull Request — master (#232)
by Juan José
01:24
created

VtsTestCase.test_calculate_vts_collection_hash()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 1
dl 0
loc 12
rs 9.95
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
from unittest import TestCase
20
21
from ospd.errors import OspdError
22
from ospd.vts import Vts
23
from hashlib import sha256
24
25
26
class VtsTestCase(TestCase):
27
    def test_add_vt(self):
28
        vts = Vts()
29
30
        vts.add('id_1', name='foo')
31
32
        self.assertEqual(len(vts.vts), 1)
33
34
    def test_add_duplicate_vt(self):
35
        vts = Vts()
36
37
        vts.add('id_1', name='foo')
38
39
        with self.assertRaises(OspdError):
40
            vts.add('id_1', name='bar')
41
42
        self.assertEqual(len(vts.vts), 1)
43
44
    def test_add_vt_with_empty_id(self):
45
        vts = Vts()
46
47
        with self.assertRaises(OspdError):
48
            vts.add(None, name='foo')
49
50
        with self.assertRaises(OspdError):
51
            vts.add('', name='foo')
52
53
    def test_add_vt_with_invalid_id(self):
54
        vts = Vts()
55
56
        with self.assertRaises(OspdError):
57
            vts.add('$$$_1', name='foo')
58
59
        self.assertEqual(len(vts.vts), 0)
60
61
    def test_contains(self):
62
        vts = Vts()
63
64
        vts.add('id_1', name='foo')
65
66
        self.assertIn('id_1', vts)
67
68
    def test_get(self):
69
        vts = Vts()
70
71
        vts.add('id_1', name='foo')
72
        vt = vts.get('id_1')
73
74
        self.assertIsNotNone(vt)
75
        self.assertEqual(vt['name'], 'foo')
76
77
        self.assertIsNone(vt.get('bar'))
78
79
    def test_iterator(self):
80
        vts = Vts()
81
82
        vts.add('id_1', name='foo')
83
        vts.add('id_2', name='bar')
84
85
        it = iter(vts)
86
87
        vt_id = next(it)
88
        # Python 3.5 doesn't ensure the order of the retuned keys
89
        self.assertIn(vt_id, ['id_1', 'id_2'])
90
91
        vt_id = next(it)
92
        self.assertIn(vt_id, ['id_1', 'id_2'])
93
94
        with self.assertRaises(StopIteration):
95
            next(it)
96
97
    def test_keys(self):
98
        vts = Vts()
99
100
        vts.add('id_1', name='foo')
101
        vts.add('id_2', name='bar')
102
103
        # use assertCountEqual for Python 3.5 because dict.keys order is
104
        # undefined
105
        self.assertCountEqual(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()
142
143
        vts.add('id_2', name='bar', vt_modification_time='56789')
144
        vts.add('id_1', name='foo', vt_modification_time='01234')
145
        vts.calculate_vts_collection_hash()
146
147
        h = sha256()
148
        h.update("id_101234id_256789".encode('utf-8'))
149
        hash_test = h.hexdigest()
150
151
        self.assertEqual(hash_test, vts.sha256_hash)
152