Passed
Pull Request — master (#111)
by
unknown
01:50
created

ArgumentParserTestCase.test_defaults()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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.parser import (
26
    create_args_parser,
27
    get_common_args,
28
    DEFAULT_ADDRESS,
29
    DEFAULT_PORT,
30
    DEFAULT_KEY_FILE,
31
    DEFAULT_NICENESS,
32
)
33
34
35
class ArgumentParserTestCase(unittest.TestCase):
36
    def setUp(self):
37
        self.parser = create_args_parser('Wrapper name')
38
39
    def test_port_interval(self):
40
        with self.assertRaises(SystemExit):
41
            get_common_args(self.parser, ['--port=65536'])
42
43
        with self.assertRaises(SystemExit):
44
            get_common_args(self.parser, ['--port=0'])
45
46
        args = get_common_args(self.parser, ['--port=3353'])
47
        self.assertEqual(3353, args['port'])
48
49
    def test_port_as_string(self):
50
        with self.assertRaises(SystemExit):
51
            get_common_args(self.parser, ['--port=abcd'])
52
53
    def test_address_param(self):
54
        args = get_common_args(self.parser, '-b 1.2.3.4'.split())
55
        self.assertEqual('1.2.3.4', args['address'])
56
57
    def test_correct_lower_case_log_level(self):
58
        args = get_common_args(self.parser, '-L error'.split())
59
        self.assertEqual(logging.ERROR, args['log_level'])
60
61
    def test_correct_upper_case_log_level(self):
62
        args = get_common_args(self.parser, '-L INFO'.split())
63
        self.assertEqual(logging.INFO, args['log_level'])
64
65
    def test_correct_log_level(self):
66
        with self.assertRaises(SystemExit):
67
            get_common_args(self.parser, '-L blah'.split())
68
69
    def test_non_existing_key(self):
70
        with self.assertRaises(SystemExit):
71
            get_common_args(self.parser, '-k abcdef.ghijkl'.split())
72
73
    def test_existing_key(self):
74
        args = get_common_args(self.parser, '-k /etc/passwd'.split())
75
        self.assertEqual('/etc/passwd', args['keyfile'])
76
77
    def test_defaults(self):
78
        args = get_common_args(self.parser, [])
79
80
        self.assertEqual(DEFAULT_KEY_FILE, args['keyfile'])
81
        self.assertEqual(DEFAULT_NICENESS, args['niceness'])
82
        self.assertEqual(logging.WARNING, args['log_level'])
83
        self.assertEqual(DEFAULT_ADDRESS, args['address'])
84
        self.assertEqual(DEFAULT_PORT, args['port'])
85