|
1
|
|
|
# Copyright (C) 2014-2018 Greenbone Networks GmbH |
|
2
|
|
|
# |
|
3
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later |
|
4
|
|
|
# |
|
5
|
|
|
# This program is free software; you can redistribute it and/or |
|
6
|
|
|
# modify it under the terms of the GNU General Public License |
|
7
|
|
|
# as published by the Free Software Foundation; either version 2 |
|
8
|
|
|
# of the License, or (at your option) any later version. |
|
9
|
|
|
# |
|
10
|
|
|
# This program is distributed in the hope that it will be useful, |
|
11
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
# GNU General Public License for more details. |
|
14
|
|
|
# |
|
15
|
|
|
# You should have received a copy of the GNU General Public License |
|
16
|
|
|
# along with this program; if not, write to the Free Software |
|
17
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18
|
|
|
|
|
19
|
|
|
""" Test module for command line arguments. |
|
20
|
|
|
""" |
|
21
|
|
|
|
|
22
|
|
|
import logging |
|
23
|
|
|
import unittest |
|
24
|
|
|
|
|
25
|
|
|
from ospd import misc |
|
26
|
|
|
from ospd.misc import create_args_parser, get_common_args |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class ArgumentParserTestCase(unittest.TestCase): |
|
30
|
|
|
def setUp(self): |
|
31
|
|
|
self.parser = create_args_parser('Wrapper name') |
|
32
|
|
|
|
|
33
|
|
|
def test_port_interval(self): |
|
34
|
|
|
self.assertRaises( |
|
35
|
|
|
SystemExit, get_common_args, self.parser, '--port=65536'.split() |
|
36
|
|
|
) |
|
37
|
|
|
self.assertRaises( |
|
38
|
|
|
SystemExit, get_common_args, self.parser, '--port=0'.split() |
|
39
|
|
|
) |
|
40
|
|
|
args = get_common_args(self.parser, '--port=3353'.split()) |
|
41
|
|
|
self.assertEqual(3353, args['port']) |
|
42
|
|
|
|
|
43
|
|
|
def test_port_as_string(self): |
|
44
|
|
|
self.assertRaises( |
|
45
|
|
|
SystemExit, get_common_args, self.parser, '--port=abcd'.split() |
|
46
|
|
|
) |
|
47
|
|
|
|
|
48
|
|
|
def test_default_port(self): |
|
49
|
|
|
args = get_common_args(self.parser, []) |
|
50
|
|
|
self.assertEqual(misc.PORT, args['port']) |
|
51
|
|
|
|
|
52
|
|
|
def test_default_address(self): |
|
53
|
|
|
args = get_common_args(self.parser, []) |
|
54
|
|
|
self.assertEqual(misc.ADDRESS, args['address']) |
|
55
|
|
|
|
|
56
|
|
|
def test_address_param(self): |
|
57
|
|
|
args = get_common_args(self.parser, '-b 1.2.3.4'.split()) |
|
58
|
|
|
self.assertEqual('1.2.3.4', args['address']) |
|
59
|
|
|
|
|
60
|
|
|
def test_default_log_level(self): |
|
61
|
|
|
args = get_common_args(self.parser, []) |
|
62
|
|
|
self.assertEqual(logging.WARNING, args['log_level']) |
|
63
|
|
|
|
|
64
|
|
|
def test_correct_lower_case_log_level(self): |
|
65
|
|
|
args = get_common_args(self.parser, '-L error'.split()) |
|
66
|
|
|
self.assertEqual(logging.ERROR, args['log_level']) |
|
67
|
|
|
|
|
68
|
|
|
def test_correct_upper_case_log_level(self): |
|
69
|
|
|
args = get_common_args(self.parser, '-L INFO'.split()) |
|
70
|
|
|
self.assertEqual(logging.INFO, args['log_level']) |
|
71
|
|
|
|
|
72
|
|
|
def test_correct_log_level(self): |
|
73
|
|
|
self.assertRaises( |
|
74
|
|
|
SystemExit, get_common_args, self.parser, '-L blah'.split() |
|
75
|
|
|
) |
|
76
|
|
|
|
|
77
|
|
|
def test_non_existing_key(self): |
|
78
|
|
|
self.assertRaises( |
|
79
|
|
|
SystemExit, get_common_args, self.parser, '-k abcdef.ghijkl'.split() |
|
80
|
|
|
) |
|
81
|
|
|
|
|
82
|
|
|
def test_existing_key(self): |
|
83
|
|
|
args = get_common_args(self.parser, '-k /etc/passwd'.split()) |
|
84
|
|
|
self.assertEqual('/etc/passwd', args['keyfile']) |
|
85
|
|
|
|
|
86
|
|
|
def test_default_key(self): |
|
87
|
|
|
args = get_common_args(self.parser, []) |
|
88
|
|
|
self.assertEqual(misc.KEY_FILE, args['keyfile']) |
|
89
|
|
|
|