1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2018 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-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 General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (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 General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
import unittest |
20
|
|
|
|
21
|
|
|
from gvm.errors import RequiredArgument |
22
|
|
|
from gvm.protocols.ospv1 import Osp |
23
|
|
|
|
24
|
|
|
from .. import MockConnection |
25
|
|
|
|
26
|
|
|
class OSPStartScanTestCase(unittest.TestCase): |
27
|
|
|
def setUp(self): |
28
|
|
|
self.connection = MockConnection() |
29
|
|
|
self.osp = Osp(self.connection) |
30
|
|
|
|
31
|
|
|
def test_start_scan(self): |
32
|
|
|
scanner_params = dict() |
33
|
|
|
scanner_params['key1'] = 'value1' |
34
|
|
|
|
35
|
|
|
targets = list() |
36
|
|
|
_target1 = dict() |
37
|
|
|
_target1['hosts'] = 'localhost' |
38
|
|
|
_target1['ports'] = '22,80' |
39
|
|
|
targets.append(_target1) |
40
|
|
|
|
41
|
|
|
_target2 = dict() |
42
|
|
|
_target2['hosts'] = '192.168.10.1' |
43
|
|
|
_target2['ports'] = '443' |
44
|
|
|
_credential1 = dict() |
45
|
|
|
_credential1 = {'smb': {'username': 'username', |
46
|
|
|
'password': 'pass', |
|
|
|
|
47
|
|
|
'port': 'port', |
|
|
|
|
48
|
|
|
'type': 'type'}} |
|
|
|
|
49
|
|
|
_target2['credentials'] = _credential1 |
50
|
|
|
targets.append(_target2) |
51
|
|
|
|
52
|
|
|
vts = dict() |
53
|
|
|
vts['vt1'] = {'value_id': 'value'} |
54
|
|
|
vts['vt_groups'] = ['family=A', 'family=B'] |
55
|
|
|
|
56
|
|
|
self.osp.start_scan( |
57
|
|
|
scan_id='123-456', |
58
|
|
|
parallel='10', |
59
|
|
|
scanner_params=scanner_params, |
60
|
|
|
targets=targets, |
61
|
|
|
vt_selection=vts |
62
|
|
|
) |
63
|
|
|
|
64
|
|
|
self.connection.send.has_been_called_with( |
|
|
|
|
65
|
|
|
'<start_scan scan_id="123-456" parallel="10">' |
66
|
|
|
'<scanner_params key1="value1"/>' |
67
|
|
|
'<targets><target><hosts>localhost</hosts>' |
68
|
|
|
'<ports>22,80</ports></target>' |
69
|
|
|
'<target><hosts>192.168.10.1</hosts>' |
70
|
|
|
'<ports>443</ports>' |
71
|
|
|
'<credentials>' |
72
|
|
|
'<credential port="port" service="smb" type="type">' |
73
|
|
|
'<username>username</username>' |
74
|
|
|
'<password>pass</password>' |
75
|
|
|
'</credential></credentials>' |
76
|
|
|
'</target></targets><vt_selection>' |
77
|
|
|
'<vt_single id="vt1">' |
78
|
|
|
'<vt_value id="value_id">value</vt_value></vt_single>' |
79
|
|
|
'<vt_group filter="family=A"/><vt_group filter="family=B"/>' |
80
|
|
|
'</vt_selection></start_scan>' |
81
|
|
|
) |
82
|
|
|
|
83
|
|
|
def test_start_scan_without_target(self): |
84
|
|
|
with self.assertRaises(RequiredArgument): |
85
|
|
|
self.osp.start_scan() |
|
|
|
|
86
|
|
|
|
87
|
|
|
def test_start_scan_legacy(self): |
88
|
|
|
self.osp.start_scan( |
89
|
|
|
scan_id='123-456', |
90
|
|
|
parallel='10', |
91
|
|
|
target="localhost", |
92
|
|
|
ports="22" |
93
|
|
|
) |
94
|
|
|
self.connection.send.has_been_called_with( |
|
|
|
|
95
|
|
|
'<start_scan scan_id="123-456" parallel="10" ' |
96
|
|
|
'target="localhost" ports="22">' |
97
|
|
|
'<scanner_params/></start_scan>' |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
if __name__ == '__main__': |
102
|
|
|
unittest.main() |
103
|
|
|
|