"Fossies" - the Fresh Open Source Software Archive

Member "ospd-2.0.1/tests/test_argument_parser.py" (12 May 2020, 3636 Bytes) of package /linux/misc/openvas/ospd-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_argument_parser.py": 2.0.0_vs_2.0.1.

    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 unittest.mock import patch
   26 
   27 from io import StringIO
   28 from typing import List
   29 
   30 from ospd.parser import (
   31     create_parser,
   32     Arguments,
   33     DEFAULT_ADDRESS,
   34     DEFAULT_PORT,
   35     DEFAULT_KEY_FILE,
   36     DEFAULT_NICENESS,
   37     DEFAULT_SCANINFO_STORE_TIME,
   38     DEFAULT_CONFIG_PATH,
   39     DEFAULT_UNIX_SOCKET_PATH,
   40     DEFAULT_PID_PATH,
   41     DEFAULT_LOCKFILE_DIR_PATH,
   42 )
   43 
   44 
   45 class ArgumentParserTestCase(unittest.TestCase):
   46     def setUp(self):
   47         self.parser = create_parser('Wrapper name')
   48 
   49     def parse_args(self, args: List[str]) -> Arguments:
   50         return self.parser.parse_arguments(args)
   51 
   52     @patch('sys.stderr', new_callable=StringIO)
   53     def test_port_interval(self, _mock_stderr):
   54         with self.assertRaises(SystemExit):
   55             self.parse_args(['--port=65536'])
   56 
   57         with self.assertRaises(SystemExit):
   58             self.parse_args(['--port=0'])
   59 
   60         args = self.parse_args(['--port=3353'])
   61         self.assertEqual(3353, args.port)
   62 
   63     @patch('sys.stderr', new_callable=StringIO)
   64     def test_port_as_string(self, _mock_stderr):
   65         with self.assertRaises(SystemExit):
   66             self.parse_args(['--port=abcd'])
   67 
   68     def test_address_param(self):
   69         args = self.parse_args('-b 1.2.3.4'.split())
   70         self.assertEqual('1.2.3.4', args.address)
   71 
   72     def test_correct_lower_case_log_level(self):
   73         args = self.parse_args('-L error'.split())
   74         self.assertEqual(logging.ERROR, args.log_level)
   75 
   76     def test_correct_upper_case_log_level(self):
   77         args = self.parse_args('-L INFO'.split())
   78         self.assertEqual(logging.INFO, args.log_level)
   79 
   80     @patch('sys.stderr', new_callable=StringIO)
   81     def test_correct_log_level(self, _mock_stderr):
   82         with self.assertRaises(SystemExit):
   83             self.parse_args('-L blah'.split())
   84 
   85     def test_non_existing_key(self):
   86         args = self.parse_args('-k foo'.split())
   87         self.assertEqual('foo', args.key_file)
   88 
   89     def test_existing_key(self):
   90         args = self.parse_args('-k /etc/passwd'.split())
   91         self.assertEqual('/etc/passwd', args.key_file)
   92 
   93     def test_defaults(self):
   94         args = self.parse_args([])
   95 
   96         self.assertEqual(args.key_file, DEFAULT_KEY_FILE)
   97         self.assertEqual(args.niceness, DEFAULT_NICENESS)
   98         self.assertEqual(args.log_level, logging.WARNING)
   99         self.assertEqual(args.address, DEFAULT_ADDRESS)
  100         self.assertEqual(args.port, DEFAULT_PORT)
  101         self.assertEqual(args.scaninfo_store_time, DEFAULT_SCANINFO_STORE_TIME)
  102         self.assertEqual(args.config, DEFAULT_CONFIG_PATH)
  103         self.assertEqual(args.unix_socket, DEFAULT_UNIX_SOCKET_PATH)
  104         self.assertEqual(args.pid_file, DEFAULT_PID_PATH)
  105         self.assertEqual(args.lock_file_dir, DEFAULT_LOCKFILE_DIR_PATH)