Completed
Push — master ( 60e59c...00b80e )
by
unknown
16s queued 13s
created

tests.protocols.gmp.test_gmp_create_target.hosts()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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.gmpv7 import Gmp
23
24
from .. import MockConnection
25
26
def hosts(hostlist):
27
    return ', '.join(hostlist)
28
29
class GMPCreateTargetCommandTestCase(unittest.TestCase):
30
31
    TARGET_NAME = 'Unittest Target'
32
    TARGET_HOSTS = ['127.0.0.1', 'foo.bar']
33
    COMMENT = 'This is a comment'
34
    UUID = '00000000-0000-0000-0000-000000000000'
35
    PORT = '1234'
36
    ALIVE_TEST = 'ICMP Ping'
37
    PORT_RANGE = 'T:10-20,U:10-30'
38
39
    def setUp(self):
40
        self.connection = MockConnection()
41
        self.gmp = Gmp(self.connection)
42
43
    def test_valid_name_make_unique_true_correct(self):
44
        self.gmp.create_target(self.TARGET_NAME, make_unique=True,
45
                               hosts=self.TARGET_HOSTS)
46
47
        self.connection.send.has_been_called_with(
48
            '<create_target><name>{target}<make_unique>1</make_unique></name>'
49
            '<hosts>{hosts}</hosts>'
50
            '</create_target>'.format(
51
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS))
52
        )
53
54
    def test_valid_name_make_unique_false_correct(self):
55
        self.gmp.create_target(self.TARGET_NAME, make_unique=False,
56
                               hosts=self.TARGET_HOSTS)
57
58
        self.connection.send.has_been_called_with(
59
            '<create_target><name>{target}</name>'
60
            '<hosts>{hosts}</hosts>'
61
            '</create_target>'.format(
62
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS))
63
        )
64
65
    def test_empty_name_value_error(self):
66
        with self.assertRaises(RequiredArgument):
67
            self.gmp.create_target(None)
68
69
        with self.assertRaises(RequiredArgument):
70
            self.gmp.create_target("")
71
72
    def test_asset_hosts_correct(self):
73
        self.gmp.create_target(self.TARGET_NAME, asset_hosts_filter='name=foo')
74
75
        self.connection.send.has_been_called_with(
76
            '<create_target><name>{target}</name>'
77
            '<asset_hosts filter="name=foo"/>'
78
            '</create_target>'.format(target=self.TARGET_NAME)
79
        )
80
81
    def test_no_host_no_asset_hosts_value_error(self):
82
        with self.assertRaises(RequiredArgument):
83
            self.gmp.create_target(self.TARGET_NAME)
84
85
    def test_comment_correct(self):
86
        self.gmp.create_target(self.TARGET_NAME, hosts=self.TARGET_HOSTS,
87
                               comment=self.COMMENT)
88
89
        self.connection.send.has_been_called_with(
90
            '<create_target><name>{target}</name>'
91
            '<hosts>{hosts}</hosts>'
92
            '<comment>{comment}</comment>'
93
            '</create_target>'.format(
94
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
95
                comment=self.COMMENT)
96
        )
97
98
    def test_copy_correct(self):
99
        self.gmp.create_target(self.TARGET_NAME, hosts=self.TARGET_HOSTS,
100
                               copy=self.UUID)
101
102
        self.connection.send.has_been_called_with(
103
            '<create_target><name>{target}</name>'
104
            '<hosts>{hosts}</hosts>'
105
            '<copy>{copy}</copy>'
106
            '</create_target>'.format(
107
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
108
                copy=self.UUID,
109
            )
110
        )
111
112
    def test_exclude_hosts_correct(self):
113
        self.gmp.create_target(self.TARGET_NAME,
114
                               hosts=self.TARGET_HOSTS,
115
                               exclude_hosts=self.TARGET_HOSTS)
116
117
        self.connection.send.has_been_called_with(
118
            '<create_target><name>{target}</name>'
119
            '<hosts>{hosts}</hosts>'
120
            '<exclude_hosts>{hosts}</exclude_hosts>'
121
            '</create_target>'.format(
122
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
123
            )
124
        )
125
126
    def test_ssh_credential_correct(self):
127
        self.gmp.create_target(self.TARGET_NAME,
128
                               hosts=self.TARGET_HOSTS,
129
                               ssh_credential_id=self.UUID)
130
131
        self.connection.send.has_been_called_with(
132
            '<create_target><name>{target}</name>'
133
            '<hosts>{hosts}</hosts>'
134
            '<ssh_credential id="{uuid}"/>'
135
            '</create_target>'.format(
136
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
137
                uuid=self.UUID,
138
            )
139
        )
140
141
    def test_ssh_credential_with_port_correct(self):
142
        self.gmp.create_target(self.TARGET_NAME,
143
                               hosts=self.TARGET_HOSTS,
144
                               ssh_credential_id=self.UUID,
145
                               ssh_credential_port=self.PORT)
146
147
        self.connection.send.has_been_called_with(
148
            '<create_target><name>{target}</name>'
149
            '<hosts>{hosts}</hosts>'
150
            '<ssh_credential id="{uuid}">'
151
            '<port>{port}</port>'
152
            '</ssh_credential>'
153
            '</create_target>'.format(
154
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
155
                uuid=self.UUID, port=self.PORT,
156
            )
157
        )
158
159
    def test_smb_credential_correct(self):
160
        self.gmp.create_target(self.TARGET_NAME,
161
                               hosts=self.TARGET_HOSTS,
162
                               smb_credential_id=self.UUID)
163
164
        self.connection.send.has_been_called_with(
165
            '<create_target><name>{target}</name>'
166
            '<hosts>{hosts}</hosts>'
167
            '<smb_credential id="{uuid}"/>'
168
            '</create_target>'.format(
169
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
170
                uuid=self.UUID,
171
            )
172
        )
173
174
    def test_esxi_credential_correct(self):
175
        self.gmp.create_target(self.TARGET_NAME,
176
                               hosts=self.TARGET_HOSTS,
177
                               esxi_credential_id=self.UUID)
178
179
        self.connection.send.has_been_called_with(
180
            '<create_target><name>{target}</name>'
181
            '<hosts>{hosts}</hosts>'
182
            '<esxi_credential id="{uuid}"/>'
183
            '</create_target>'.format(
184
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
185
                uuid=self.UUID,
186
            )
187
        )
188
189
    def test_snmp_credential_correct_cmd(self):
190
        self.gmp.create_target(self.TARGET_NAME,
191
                               hosts=self.TARGET_HOSTS,
192
                               snmp_credential_id=self.UUID)
193
194
        self.connection.send.has_been_called_with(
195
            '<create_target><name>{target}</name>'
196
            '<hosts>{hosts}</hosts>'
197
            '<snmp_credential id="{uuid}"/>'
198
            '</create_target>'.format(
199
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
200
                uuid=self.UUID,
201
            )
202
        )
203
204
    def test_alive_tests_correct(self):
205
        self.gmp.create_target(self.TARGET_NAME,
206
                               hosts=self.TARGET_HOSTS,
207
                               alive_tests=self.ALIVE_TEST)
208
209
        self.connection.send.has_been_called_with(
210
            '<create_target><name>{target}</name>'
211
            '<hosts>{hosts}</hosts>'
212
            '<alive_tests>{alive}</alive_tests>'
213
            '</create_target>'.format(
214
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
215
                alive=self.ALIVE_TEST,
216
            )
217
        )
218
219
    def test_reverse_lookup_only_true_correct(self):
220
        self.gmp.create_target(self.TARGET_NAME,
221
                               hosts=self.TARGET_HOSTS,
222
                               reverse_lookup_only=True)
223
224
        self.connection.send.has_been_called_with(
225
            '<create_target><name>{target}</name>'
226
            '<hosts>{hosts}</hosts>'
227
            '<reverse_lookup_only>1</reverse_lookup_only>'
228
            '</create_target>'.format(
229
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
230
            )
231
        )
232
233
    def test_reverse_lookup_only_false_correct(self):
234
        self.gmp.create_target(self.TARGET_NAME,
235
                               hosts=self.TARGET_HOSTS,
236
                               reverse_lookup_only=False)
237
238
        self.connection.send.has_been_called_with(
239
            '<create_target><name>{target}</name>'
240
            '<hosts>{hosts}</hosts>'
241
            '<reverse_lookup_only>0</reverse_lookup_only>'
242
            '</create_target>'.format(
243
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
244
            )
245
        )
246
247
    def test_reverse_lookup_unify_true_correct(self):
248
        self.gmp.create_target(self.TARGET_NAME,
249
                               hosts=self.TARGET_HOSTS,
250
                               reverse_lookup_unify=True)
251
252
        self.connection.send.has_been_called_with(
253
            '<create_target><name>{target}</name>'
254
            '<hosts>{hosts}</hosts>'
255
            '<reverse_lookup_unify>1</reverse_lookup_unify>'
256
            '</create_target>'.format(
257
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
258
            )
259
        )
260
261
    def test_reverse_lookup_unify_false_correct_cmd(self):
262
        self.gmp.create_target(self.TARGET_NAME,
263
                               hosts=self.TARGET_HOSTS,
264
                               reverse_lookup_unify=False)
265
266
        self.connection.send.has_been_called_with(
267
            '<create_target><name>{target}</name>'
268
            '<hosts>{hosts}</hosts>'
269
            '<reverse_lookup_unify>0</reverse_lookup_unify>'
270
            '</create_target>'.format(
271
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
272
            )
273
        )
274
275
    def test_port_range_correct(self):
276
        self.gmp.create_target(self.TARGET_NAME,
277
                               hosts=self.TARGET_HOSTS,
278
                               port_range=self.PORT_RANGE)
279
280
        self.connection.send.has_been_called_with(
281
            '<create_target><name>{target}</name>'
282
            '<hosts>{hosts}</hosts>'
283
            '<port_range>{range}</port_range>'
284
            '</create_target>'.format(
285
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
286
                range=self.PORT_RANGE,
287
            )
288
        )
289
290
    def test_port_list_correct_cmd(self):
291
        self.gmp.create_target(self.TARGET_NAME,
292
                               hosts=self.TARGET_HOSTS,
293
                               port_list_id=self.UUID)
294
295
        self.connection.send.has_been_called_with(
296
            '<create_target><name>{target}</name>'
297
            '<hosts>{hosts}</hosts>'
298
            '<port_list id="{uuid}"/>'
299
            '</create_target>'.format(
300
                target=self.TARGET_NAME, hosts=hosts(self.TARGET_HOSTS),
301
                uuid=self.UUID,
302
            )
303
        )
304
305
306
if __name__ == '__main__':
307
    unittest.main()
308