1# -*- coding: utf-8 -*- 2# Copyright (C) 2019-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/>. 18import logging 19import unittest 20 21from pathlib import Path 22 23from gvm.connections import DEFAULT_UNIX_SOCKET_PATH, DEFAULT_GVM_PORT 24 25from gvmtools.config import Config, DEFAULT_SSH_PORT, __name__ as name 26 27__here__ = Path(__file__).parent.resolve() 28 29 30class ConfigTestCase(unittest.TestCase): 31 def test_config_defaults(self): 32 config = Config() 33 34 self.assertEqual(config.get('gmp', 'username'), '') 35 self.assertEqual(config.get('gmp', 'password'), '') 36 37 self.assertEqual(config.get('ssh', 'username'), 'gmp') 38 self.assertEqual(config.get('ssh', 'password'), 'gmp') 39 self.assertEqual(config.get('ssh', 'port'), DEFAULT_SSH_PORT) 40 41 self.assertEqual( 42 config.get('unixsocket', 'socketpath'), DEFAULT_UNIX_SOCKET_PATH 43 ) 44 45 self.assertEqual(config.get('tls', 'port'), DEFAULT_GVM_PORT) 46 47 def test_get_unknown_setting(self): 48 config = Config() 49 self.assertIsNone(config.get('foo', 'bar')) 50 51 def test_load(self): 52 test_config_path = __here__ / 'test.cfg' 53 54 self.assertTrue(test_config_path.is_file()) 55 56 config = Config() 57 config.load(test_config_path) 58 59 self.assertEqual(config.get('gmp', 'username'), 'bar') 60 self.assertEqual(config.get('gmp', 'password'), 'bar') 61 62 self.assertEqual(config.get('ssh', 'username'), 'ipsum') 63 self.assertEqual(config.get('ssh', 'password'), 'lorem') 64 self.assertEqual(config.get('ssh', 'port'), '123') 65 66 self.assertEqual( 67 config.get('unixsocket', 'socketpath'), '/foo/bar.sock' 68 ) 69 70 self.assertEqual(config.get('tls', 'port'), '123') 71 self.assertEqual(config.get('tls', 'certfile'), 'foo.cert') 72 self.assertEqual(config.get('tls', 'keyfile'), 'foo.key') 73 self.assertEqual(config.get('tls', 'cafile'), 'foo.ca') 74 75 self.assertDictEqual( 76 config.defaults(), dict(timeout='1000', foo='bar', username='ipsum') 77 ) 78 79 def test_load_auth(self): 80 root = logging.getLogger(name) 81 root.disabled = True 82 83 test_config_path = __here__ / 'test_auth.cfg' 84 85 self.assertTrue(test_config_path.is_file()) 86 87 config = Config() 88 config.load(test_config_path) 89 90 self.assertEqual(config.get('gmp', 'username'), 'foo') 91 self.assertEqual(config.get('gmp', 'password'), 'bar') 92 93 root.disabled = False 94 95 def test_load_with_non_existing_configfile(self): 96 test_config_path = __here__ / 'foo.cfg' 97 98 self.assertFalse(test_config_path.is_file()) 99 100 config = Config() 101 102 with self.assertRaises(FileNotFoundError): 103 config.load(test_config_path) 104