Passed
Pull Request — master (#236)
by Juan José
01:49
created

VtHelperTestCase.test_get_single_vt()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2014-2020 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: AGPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as
8
# published by the Free Software Foundation, either version 3 of the
9
# License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20
import logging
21
22
from hashlib import sha256
23
from unittest import TestCase
24
25
from tests.dummydaemon import DummyDaemon
26
from tests.helper import assert_called_once
27
28
from ospd_openvas.vthelper import VtHelper
29
30
31
class VtHelperTestCase(TestCase):
32
    def test_get_single_vt(self):
33
        w = DummyDaemon()
34
        vthelper = VtHelper(w.nvti)
35
        res = vthelper.get_single_vt("1.3.6.1.4.1.25623.1.0.100061")
36
37
        assert_called_once(w.nvti.get_nvt_metadata)
38
        self.assertEqual("Mantis Detection", res.get('name'))
39
40
    def test_calculate_vts_collection_hash_no_params(self):
41
        w = DummyDaemon()
42
        vthelper = VtHelper(w.nvti)
43
        hash_out = vthelper.calculate_vts_collection_hash()
44
45
        vt_hash_str = (
46
            '1.3.6.1.4.1.25623.1.0.10006115339065651Data '
47
            + 'length :2Do not randomize the  order  in  which '
48
            + 'ports are scannedno'
49
        )
50
51
        vt_hash = sha256()
52
        vt_hash.update(vt_hash_str.encode('utf-8'))
53
        hash_test = vt_hash.hexdigest()
54
55
        self.assertEqual(hash_test, hash_out)
56
57
    def test_get_vt_iterator(self):
58
        w = DummyDaemon()
59
        vthelper = VtHelper(w.nvti)
60
61
        vt = ["1.3.6.1.4.1.25623.1.0.100061"]
62
63
        for key, _ in vthelper.get_vt_iterator():
64
            self.assertIn(key, vt)
65
66
    def test_get_vt_iterator_with_filter(self):
67
        w = DummyDaemon()
68
        vthelper = VtHelper(w.nvti)
69
70
        vt = ["1.3.6.1.4.1.25623.1.0.100061"]
71
72
        vtout = w.VTS["1.3.6.1.4.1.25623.1.0.100061"]
73
74
        for key, vt_dict in vthelper.get_vt_iterator(vt_selection=vt):
75
            self.assertIn(key, vt)
76
            for key in vtout:
77
                self.assertIn(key, vt_dict)
78
79
    def test_get_vt_iterator_with_filter_no_vt(self):
80
        w = DummyDaemon()
81
        vthelper = VtHelper(w.nvti)
82
        w.nvti.get_nvt_metadata.return_value = None
83
        vt = ["1.3.6.1.4.1.25623.1.0.100065"]
84
85
        for _, values in vthelper.get_vt_iterator(vt_selection=vt):
86
            self.assertIs(values, None)
87
88
    def test_get_vt_iterator_with_filter_no_vt(self):
89
        w = DummyDaemon()
90
        vthelper = VtHelper(w.nvti)
91
        w.nvti.get_nvt_metadata.return_value = None
92
        vt = ["1.3.6.1.4.1.25623.1.0.100065"]
93
94
        for _, values in vthelper.get_vt_iterator(vt_selection=vt):
95
            self.assertIs(values, None)
96