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