Passed
Pull Request — master (#154)
by
unknown
01:13
created

tests.test_parser.ConfigParserTestCase.setUp()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2019 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 pathlib import Path
22
23
from gvmtools.parser import CliParser
24
25
26
class ConfigParserTestCase(unittest.TestCase):
27
    def setUp(self):
28
        self.test_config_path = Path(__file__).parent.resolve() / 'test.cfg'
29
30
        self.assertTrue(self.test_config_path.is_file())
31
32
        self.parser = CliParser('TestParser', 'test.log')
33
34
    def test_socket_defaults_from_config(self):
35
        args = self.parser.parse_args(
36
            ['--config', str(self.test_config_path), 'socket']
37
        )
38
39
        self.assertEqual(args.foo, 'bar')
40
        self.assertEqual(args.timeout, 1000)
41
        self.assertEqual(args.gmp_password, 'bar')
42
        self.assertEqual(args.gmp_username, 'bar')
43
        self.assertEqual(args.socketpath, '/foo/bar.sock')
44
45
    def test_ssh_defaults_from_config(self):
46
        args = self.parser.parse_args(
47
            ['--config', str(self.test_config_path), 'ssh', '--hostname', 'foo']
48
        )
49
50
        self.assertEqual(args.foo, 'bar')
51
        self.assertEqual(args.timeout, 1000)
52
        self.assertEqual(args.gmp_password, 'bar')
53
        self.assertEqual(args.gmp_username, 'bar')
54
        self.assertEqual(args.ssh_password, 'lorem')
55
        self.assertEqual(args.ssh_username, 'ipsum')
56
        self.assertEqual(args.port, 123)
57
58
    def test_tls_defaults_from_config(self):
59
        args = self.parser.parse_args(
60
            ['--config', str(self.test_config_path), 'tls', '--hostname', 'foo']
61
        )
62
63
        self.assertEqual(args.foo, 'bar')
64
        self.assertEqual(args.timeout, 1000)
65
        self.assertEqual(args.gmp_password, 'bar')
66
        self.assertEqual(args.gmp_username, 'bar')
67
        self.assertEqual(args.certfile, 'foo.cert')
68
        self.assertEqual(args.keyfile, 'foo.key')
69
        self.assertEqual(args.cafile, 'foo.ca')
70
        self.assertEqual(args.port, 123)
71
72
73
class ParserTestCase(unittest.TestCase):
74
    def setUp(self):
75
        self.parser = CliParser('TestParser', 'test.log', ignore_config=True)
76
77
78
class RootArgumentsParserTest(ParserTestCase):
79
    def test_config(self):
80
        args = self.parser.parse_args(['--config', 'foo.cfg', 'socket'])
81
        self.assertEqual(args.config, 'foo.cfg')
82
83
    def test_defaults(self):
84
        args = self.parser.parse_args(['socket'])
85
        self.assertEqual(args.config, '~/.config/gvm-tools.conf')
86
        self.assertEqual(args.gmp_password, '')
87
        self.assertEqual(args.gmp_username, '')
88
        self.assertEqual(args.timeout, 60)
89
        self.assertIsNone(args.loglevel)
90
91
    def test_loglevel(self):
92
        args = self.parser.parse_args(['--log', 'ERROR', 'socket'])
93
        self.assertEqual(args.loglevel, 'ERROR')
94
95
    def test_timeout(self):
96
        args = self.parser.parse_args(['--timeout', '1000', 'socket'])
97
        self.assertEqual(args.timeout, 1000)
98
99
    def test_gmp_username(self):
100
        args = self.parser.parse_args(['--gmp-username', 'foo', 'socket'])
101
        self.assertEqual(args.gmp_username, 'foo')
102
103
    def test_gmp_password(self):
104
        args = self.parser.parse_args(['--gmp-password', 'foo', 'socket'])
105
        self.assertEqual(args.gmp_password, 'foo')
106
107
108
class SocketParserTestCase(ParserTestCase):
109
    def test_defaults(self):
110
        args = self.parser.parse_args(['socket'])
111
        self.assertIsNone(args.sockpath)
112
        self.assertEqual(args.socketpath, '/usr/local/var/run/gvmd.sock')
113
114
    def test_connection_type(self):
115
        args = self.parser.parse_args(['socket'])
116
        self.assertEqual(args.connection_type, 'socket')
117
118
    def test_sockpath(self):
119
        args = self.parser.parse_args(['socket', '--sockpath', 'foo.sock'])
120
        self.assertEqual(args.sockpath, 'foo.sock')
121
122
    def test_socketpath(self):
123
        args = self.parser.parse_args(['socket', '--socketpath', 'foo.sock'])
124
        self.assertEqual(args.socketpath, 'foo.sock')
125
126
127
class SshParserTestCase(ParserTestCase):
128
    def test_defaults(self):
129
        args = self.parser.parse_args(['ssh', '--hostname=foo'])
130
        self.assertEqual(args.port, 22)
131
        self.assertEqual(args.ssh_username, 'gmp')
132
        self.assertEqual(args.ssh_password, 'gmp')
133
134
    def test_connection_type(self):
135
        args = self.parser.parse_args(['ssh', '--hostname=foo'])
136
        self.assertEqual(args.connection_type, 'ssh')
137
138
    def test_hostname(self):
139
        args = self.parser.parse_args(['ssh', '--hostname', 'foo'])
140
        self.assertEqual(args.hostname, 'foo')
141
142
    def test_port(self):
143
        args = self.parser.parse_args(
144
            ['ssh', '--hostname', 'foo', '--port', '123']
145
        )
146
        self.assertEqual(args.port, 123)
147
148
    def test_ssh_username(self):
149
        args = self.parser.parse_args(
150
            ['ssh', '--hostname', 'foo', '--ssh-username', 'foo']
151
        )
152
        self.assertEqual(args.ssh_username, 'foo')
153
154
    def test_ssh_password(self):
155
        args = self.parser.parse_args(
156
            ['ssh', '--hostname', 'foo', '--ssh-password', 'foo']
157
        )
158
        self.assertEqual(args.ssh_password, 'foo')
159
160
161
class TlsParserTestCase(ParserTestCase):
162
    def test_defaults(self):
163
        args = self.parser.parse_args(['tls', '--hostname=foo'])
164
        self.assertIsNone(args.certfile)
165
        self.assertIsNone(args.keyfile)
166
        self.assertIsNone(args.cafile)
167
        self.assertEqual(args.port, 9390)
168
169
    def test_connection_type(self):
170
        args = self.parser.parse_args(['tls', '--hostname=foo'])
171
        self.assertEqual(args.connection_type, 'tls')
172
173
    def test_hostname(self):
174
        args = self.parser.parse_args(['tls', '--hostname', 'foo'])
175
        self.assertEqual(args.hostname, 'foo')
176
177
    def test_port(self):
178
        args = self.parser.parse_args(
179
            ['tls', '--hostname', 'foo', '--port', '123']
180
        )
181
        self.assertEqual(args.port, 123)
182
183
    def test_certfile(self):
184
        args = self.parser.parse_args(
185
            ['tls', '--hostname', 'foo', '--certfile', 'foo.cert']
186
        )
187
        self.assertEqual(args.certfile, 'foo.cert')
188
189
    def test_keyfile(self):
190
        args = self.parser.parse_args(
191
            ['tls', '--hostname', 'foo', '--keyfile', 'foo.key']
192
        )
193
        self.assertEqual(args.keyfile, 'foo.key')
194
195
    def test_cafile(self):
196
        args = self.parser.parse_args(
197
            ['tls', '--hostname', 'foo', '--cafile', 'foo.ca']
198
        )
199
        self.assertEqual(args.cafile, 'foo.ca')
200
201
    def test_no_credentials(self):
202
        args = self.parser.parse_args(
203
            ['tls', '--hostname', 'foo', '--no-credentials']
204
        )
205
        self.assertTrue(args.no_credentials)
206