1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2020 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
|
|
|
from unittest.mock import patch |
21
|
|
|
import importlib |
22
|
|
|
from pathlib import Path |
23
|
|
|
from argparse import Namespace |
24
|
|
|
from lxml import etree |
25
|
|
|
from . import GmpMockFactory |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
CWD = Path(__file__).absolute().parent |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class SendTargetTestCase(unittest.TestCase): |
32
|
|
|
def setUp(self): |
33
|
|
|
self.send_targets = importlib.import_module( |
34
|
|
|
'scripts.send-targets', 'gvmtools' |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
@patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory) |
38
|
|
|
def test_sent_target(self, mock_gmp: GmpMockFactory): |
39
|
|
|
target_xml_path = CWD / 'example_target.xml' |
40
|
|
|
target_xml_str = target_xml_path.read_text() |
41
|
|
|
|
42
|
|
|
mock_gmp.mock_response( |
43
|
|
|
'get_credentials', |
44
|
|
|
""" |
45
|
|
|
<get_credentials_response status="200" status_text="OK"> |
46
|
|
|
<credential id="6da5b7de-92ad-4dd2-8610-d5711b9c5937"> |
47
|
|
|
<owner> |
48
|
|
|
<name>jloechte</name> |
49
|
|
|
</owner> |
50
|
|
|
<name>test_</name> |
51
|
|
|
<comment>comm</comment> |
52
|
|
|
<creation_time>2020-09-17T07:34:32Z</creation_time> |
53
|
|
|
<modification_time>2020-09-30T00:46:44Z</modification_time> |
54
|
|
|
<writable>1</writable> |
55
|
|
|
<in_use>1</in_use> |
56
|
|
|
<permissions> |
57
|
|
|
<permission> |
58
|
|
|
<name>Everything</name> |
59
|
|
|
</permission> |
60
|
|
|
</permissions> |
61
|
|
|
<allow_insecure>0</allow_insecure> |
62
|
|
|
<login>sdfsdf</login> |
63
|
|
|
<type>up</type> |
64
|
|
|
<full_type>username + password</full_type> |
65
|
|
|
<formats> |
66
|
|
|
<format>exe</format> |
67
|
|
|
</formats> |
68
|
|
|
</credential> |
69
|
|
|
<credential id="7802648d-1a31-4f69-bb30-00766a1ae1e6"> |
70
|
|
|
<owner> |
71
|
|
|
<name>jloechte</name> |
72
|
|
|
</owner> |
73
|
|
|
<name>Unnamed</name> |
74
|
|
|
<comment></comment> |
75
|
|
|
<creation_time>2020-09-29T20:44:49Z</creation_time> |
76
|
|
|
<modification_time>2020-09-29T20:45:00Z</modification_time> |
77
|
|
|
<writable>1</writable> |
78
|
|
|
<in_use>1</in_use> |
79
|
|
|
<permissions> |
80
|
|
|
<permission> |
81
|
|
|
<name>Everything</name> |
82
|
|
|
</permission> |
83
|
|
|
</permissions> |
84
|
|
|
<allow_insecure>0</allow_insecure> |
85
|
|
|
<login>rterte</login> |
86
|
|
|
<type>usk</type> |
87
|
|
|
<full_type>username + SSH key</full_type> |
88
|
|
|
<formats> |
89
|
|
|
<format>key</format> |
90
|
|
|
<format>rpm</format> |
91
|
|
|
<format>deb</format> |
92
|
|
|
</formats> |
93
|
|
|
</credential> |
94
|
|
|
<credential id="70a63257-4923-4bf4-a9bb-dd8b710b2d80"> |
95
|
|
|
<owner> |
96
|
|
|
<name>jloechte</name> |
97
|
|
|
</owner> |
98
|
|
|
<name>Unnamedas</name> |
99
|
|
|
<comment></comment> |
100
|
|
|
<creation_time>2020-09-29T21:31:50Z</creation_time> |
101
|
|
|
<modification_time>2020-09-29T21:32:57Z</modification_time> |
102
|
|
|
<writable>1</writable> |
103
|
|
|
<in_use>0</in_use> |
104
|
|
|
<permissions> |
105
|
|
|
<permission> |
106
|
|
|
<name>Everything</name> |
107
|
|
|
</permission> |
108
|
|
|
</permissions> |
109
|
|
|
<allow_insecure>0</allow_insecure> |
110
|
|
|
<login>abc</login> |
111
|
|
|
<type>snmp</type> |
112
|
|
|
<full_type>SNMP</full_type> |
113
|
|
|
<formats></formats> |
114
|
|
|
<auth_algorithm>sha1</auth_algorithm> |
115
|
|
|
<privacy> |
116
|
|
|
<algorithm>des</algorithm> |
117
|
|
|
</privacy> |
118
|
|
|
</credential> |
119
|
|
|
<credential id="2bac0c76-795e-4742-b17a-808a0ec8e409"> |
120
|
|
|
<owner> |
121
|
|
|
<name>jloechte</name> |
122
|
|
|
</owner> |
123
|
|
|
<name>work</name> |
124
|
|
|
<comment>test</comment> |
125
|
|
|
<creation_time>2020-08-18T18:35:04Z</creation_time> |
126
|
|
|
<modification_time>2020-09-29T20:43:16Z</modification_time> |
127
|
|
|
<writable>1</writable> |
128
|
|
|
<in_use>1</in_use> |
129
|
|
|
<permissions> |
130
|
|
|
<permission> |
131
|
|
|
<name>Everything</name> |
132
|
|
|
</permission> |
133
|
|
|
</permissions> |
134
|
|
|
<allow_insecure>0</allow_insecure> |
135
|
|
|
<login>jloechte</login> |
136
|
|
|
<type>up</type> |
137
|
|
|
<full_type>username + password</full_type> |
138
|
|
|
<formats> |
139
|
|
|
<format>exe</format> |
140
|
|
|
</formats> |
141
|
|
|
</credential> |
142
|
|
|
<filters id=""> |
143
|
|
|
<term>first=1 rows=10 sort=name</term> |
144
|
|
|
<keywords> |
145
|
|
|
<keyword> |
146
|
|
|
<column>first</column> |
147
|
|
|
<relation>=</relation> |
148
|
|
|
<value>1</value> |
149
|
|
|
</keyword> |
150
|
|
|
<keyword> |
151
|
|
|
<column>rows</column> |
152
|
|
|
<relation>=</relation> |
153
|
|
|
<value>10</value> |
154
|
|
|
</keyword> |
155
|
|
|
<keyword> |
156
|
|
|
<column>sort</column> |
157
|
|
|
<relation>=</relation> |
158
|
|
|
<value>name</value> |
159
|
|
|
</keyword> |
160
|
|
|
</keywords> |
161
|
|
|
</filters> |
162
|
|
|
<sort> |
163
|
|
|
<field>name<order>ascending</order> |
164
|
|
|
</field> |
165
|
|
|
</sort> |
166
|
|
|
<credentials start="1" max="-2"/> |
167
|
|
|
<credential_count>4<filtered>4</filtered> |
168
|
|
|
<page>4</page> |
169
|
|
|
</credential_count> |
170
|
|
|
</get_credentials_response> |
171
|
|
|
""", |
172
|
|
|
) |
173
|
|
|
mock_gmp.mock_response( |
174
|
|
|
'create_target', |
175
|
|
|
'<create_target_response status="201" status_text="OK,' |
176
|
|
|
'resource created" id="6c9f73f5-f14c-42bf-ab44-edb8d2493dbc"/>', |
177
|
|
|
) |
178
|
|
|
|
179
|
|
|
target = etree.XML(target_xml_str) |
180
|
|
|
|
181
|
|
|
self.send_targets.parse_send_xml_tree(mock_gmp.gmp_protocol, target) |
182
|
|
|
|
183
|
|
|
def test_args(self): |
184
|
|
|
args = Namespace(script=['foo']) |
185
|
|
|
with self.assertRaises(SystemExit): |
186
|
|
|
self.send_targets.check_args(args) |
187
|
|
|
|
188
|
|
|
args2 = Namespace(script=['foo', 'bar', 'baz']) |
189
|
|
|
|
190
|
|
|
with self.assertRaises(SystemExit): |
191
|
|
|
self.send_targets.check_args(args2) |
192
|
|
|
|
193
|
|
|
@patch('builtins.input', lambda *args: 'y') |
194
|
|
|
def test_yes(self): |
195
|
|
|
yes = self.send_targets.yes_or_no('foo?') |
196
|
|
|
self.assertTrue(yes) |
197
|
|
|
|
198
|
|
|
@patch('builtins.input', lambda *args: 'n') |
199
|
|
|
def test_no(self): |
200
|
|
|
no = self.send_targets.yes_or_no('bar?') |
201
|
|
|
self.assertFalse(no) |
202
|
|
|
|
203
|
|
|
def test_error_and_exit(self): |
204
|
|
|
with self.assertRaises(SystemExit): |
205
|
|
|
self.send_targets.error_and_exit('foo') |
206
|
|
|
|
207
|
|
|
def test_create_xml_tree(self): |
208
|
|
|
target_xml_path = CWD / 'example_target.xml' |
209
|
|
|
|
210
|
|
|
tree = self.send_targets.create_xml_tree(str(target_xml_path)) |
211
|
|
|
self.assertIsInstance( |
212
|
|
|
tree, etree._Element # pylint: disable=protected-access |
213
|
|
|
) |
214
|
|
|
self.assertEqual(tree.tag, 'get_targets_response') |
215
|
|
|
|
216
|
|
|
def test_create_xml_tree_invalid_file(self): |
217
|
|
|
target_xml_path = CWD / 'invalid_file.xml' |
218
|
|
|
|
219
|
|
|
with self.assertRaises(SystemExit): |
220
|
|
|
with self.assertRaises(OSError): |
221
|
|
|
self.send_targets.create_xml_tree(str(target_xml_path)) |
222
|
|
|
|
223
|
|
|
def test_create_xml_tree_invalid_xml(self): |
224
|
|
|
target_xml_path = CWD / 'invalid_xml.xml' |
225
|
|
|
|
226
|
|
|
with self.assertRaises(SystemExit): |
227
|
|
|
with self.assertRaises(etree.Error): |
228
|
|
|
self.send_targets.create_xml_tree(str(target_xml_path)) |
229
|
|
|
|