Passed
Push — master ( 8a13b5...2a5f1b )
by Jaspar
84:44 queued 83:23
created

GmpModifyTargetTestCase.test_modify_target()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2021 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, InvalidArgumentType
22
23
from gvm.protocols.gmpv208 import AliveTest
24
25
26
class GmpModifyTargetTestCase:
27
    def test_modify_target(self):
28
        self.gmp.modify_target(target_id='t1')
29
30
        self.connection.send.has_been_called_with(
31
            '<modify_target target_id="t1"/>'
32
        )
33
34
    def test_modify_target_missing_target_id(self):
35
        with self.assertRaises(RequiredArgument):
36
            self.gmp.modify_target(target_id=None)
37
38
        with self.assertRaises(RequiredArgument):
39
            self.gmp.modify_target(target_id='')
40
41
    def test_modify_target_with_comment(self):
42
        self.gmp.modify_target(target_id='t1', comment='foo')
43
44
        self.connection.send.has_been_called_with(
45
            '<modify_target target_id="t1">'
46
            '<comment>foo</comment>'
47
            '</modify_target>'
48
        )
49
50
    def test_modify_target_with_hosts(self):
51
        self.gmp.modify_target(target_id='t1', hosts=['foo'])
52
53
        self.connection.send.has_been_called_with(
54
            '<modify_target target_id="t1">'
55
            '<hosts>foo</hosts>'
56
            '<exclude_hosts></exclude_hosts>'
57
            '</modify_target>'
58
        )
59
60
        self.gmp.modify_target(target_id='t1', hosts=['foo', 'bar'])
61
62
        self.connection.send.has_been_called_with(
63
            '<modify_target target_id="t1">'
64
            '<hosts>foo,bar</hosts>'
65
            '<exclude_hosts></exclude_hosts>'
66
            '</modify_target>'
67
        )
68
69
    def test_modify_target_with_name(self):
70
        self.gmp.modify_target(target_id='t1', name='foo')
71
72
        self.connection.send.has_been_called_with(
73
            '<modify_target target_id="t1">'
74
            '<name>foo</name>'
75
            '</modify_target>'
76
        )
77
78
    def test_modify_target_with_exclude_hosts(self):
79
        self.gmp.modify_target(target_id='t1', exclude_hosts=['foo'])
80
81
        self.connection.send.has_been_called_with(
82
            '<modify_target target_id="t1">'
83
            '<exclude_hosts>foo</exclude_hosts>'
84
            '</modify_target>'
85
        )
86
87
        self.gmp.modify_target(target_id='t1', exclude_hosts=['foo', 'bar'])
88
89
        self.connection.send.has_been_called_with(
90
            '<modify_target target_id="t1">'
91
            '<exclude_hosts>foo,bar</exclude_hosts>'
92
            '</modify_target>'
93
        )
94
95
    def test_modify_target_with_ssh_credential(self):
96
        self.gmp.modify_target(target_id='t1', ssh_credential_id='c1')
97
98
        self.connection.send.has_been_called_with(
99
            '<modify_target target_id="t1">'
100
            '<ssh_credential id="c1"/>'
101
            '</modify_target>'
102
        )
103
104
    def test_modify_target_with_ssh_credential_port(self):
105
        self.gmp.modify_target(
106
            target_id='t1', ssh_credential_id='c1', ssh_credential_port=123
107
        )
108
109
        self.connection.send.has_been_called_with(
110
            '<modify_target target_id="t1">'
111
            '<ssh_credential id="c1">'
112
            '<port>123</port>'
113
            '</ssh_credential>'
114
            '</modify_target>'
115
        )
116
117
    def test_modify_target_with_smb_credential_id(self):
118
        self.gmp.modify_target(target_id='t1', smb_credential_id='c1')
119
120
        self.connection.send.has_been_called_with(
121
            '<modify_target target_id="t1">'
122
            '<smb_credential id="c1"/>'
123
            '</modify_target>'
124
        )
125
126
    def test_modify_target_with_esxi_credential_id(self):
127
        self.gmp.modify_target(target_id='t1', esxi_credential_id='c1')
128
129
        self.connection.send.has_been_called_with(
130
            '<modify_target target_id="t1">'
131
            '<esxi_credential id="c1"/>'
132
            '</modify_target>'
133
        )
134
135
    def test_modify_target_with_snmp_credential_id(self):
136
        self.gmp.modify_target(target_id='t1', snmp_credential_id='c1')
137
138
        self.connection.send.has_been_called_with(
139
            '<modify_target target_id="t1">'
140
            '<snmp_credential id="c1"/>'
141
            '</modify_target>'
142
        )
143
144
    def test_modify_target_with_alive_tests(self):
145
        self.gmp.modify_target(target_id='t1', alive_test=AliveTest.ICMP_PING)
146
147
        self.connection.send.has_been_called_with(
148
            '<modify_target target_id="t1">'
149
            '<alive_tests>ICMP Ping</alive_tests>'
150
            '</modify_target>'
151
        )
152
153
    def test_modify_target_invalid_alive_tests(self):
154
        with self.assertRaises(InvalidArgumentType):
155
            self.gmp.modify_target(target_id='t1', alive_test='foo')
156
157
    def test_modify_target_with_allow_simultaneous_ips(self):
158
        self.gmp.modify_target(target_id='t1', allow_simultaneous_ips=True)
159
160
        self.connection.send.has_been_called_with(
161
            '<modify_target target_id="t1">'
162
            '<allow_simultaneous_ips>1</allow_simultaneous_ips>'
163
            '</modify_target>'
164
        )
165
166
        self.gmp.modify_target(target_id='t1', allow_simultaneous_ips=False)
167
168
        self.connection.send.has_been_called_with(
169
            '<modify_target target_id="t1">'
170
            '<allow_simultaneous_ips>0</allow_simultaneous_ips>'
171
            '</modify_target>'
172
        )
173
174
    def test_modify_target_with_reverse_lookup_only(self):
175
        self.gmp.modify_target(target_id='t1', reverse_lookup_only=True)
176
177
        self.connection.send.has_been_called_with(
178
            '<modify_target target_id="t1">'
179
            '<reverse_lookup_only>1</reverse_lookup_only>'
180
            '</modify_target>'
181
        )
182
183
        self.gmp.modify_target(target_id='t1', reverse_lookup_only=False)
184
185
        self.connection.send.has_been_called_with(
186
            '<modify_target target_id="t1">'
187
            '<reverse_lookup_only>0</reverse_lookup_only>'
188
            '</modify_target>'
189
        )
190
191
    def test_modify_target_with_reverse_lookup_unify(self):
192
        self.gmp.modify_target(target_id='t1', reverse_lookup_unify=True)
193
194
        self.connection.send.has_been_called_with(
195
            '<modify_target target_id="t1">'
196
            '<reverse_lookup_unify>1</reverse_lookup_unify>'
197
            '</modify_target>'
198
        )
199
200
        self.gmp.modify_target(target_id='t1', reverse_lookup_unify=False)
201
202
        self.connection.send.has_been_called_with(
203
            '<modify_target target_id="t1">'
204
            '<reverse_lookup_unify>0</reverse_lookup_unify>'
205
            '</modify_target>'
206
        )
207
208
    def test_modify_target_with_port_list_id(self):
209
        self.gmp.modify_target(target_id='t1', port_list_id='pl1')
210
211
        self.connection.send.has_been_called_with(
212
            '<modify_target target_id="t1">'
213
            '<port_list id="pl1"/>'
214
            '</modify_target>'
215
        )
216
217
218
if __name__ == '__main__':
219
    unittest.main()
220