1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2019-2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import unittest
from pathlib import Path
from gvm.connections import DEFAULT_GVM_PORT, DEFAULT_UNIX_SOCKET_PATH
from gvmtools.config import DEFAULT_SSH_PORT, Config
from gvmtools.config import __name__ as name
__here__ = Path(__file__).parent.resolve()
class ConfigTestCase(unittest.TestCase):
def test_config_defaults(self):
config = Config()
self.assertEqual(config.get("gmp", "username"), "")
self.assertEqual(config.get("gmp", "password"), "")
self.assertEqual(config.get("ssh", "username"), "gmp")
self.assertEqual(config.get("ssh", "password"), "gmp")
self.assertEqual(config.get("ssh", "port"), DEFAULT_SSH_PORT)
self.assertEqual(
config.get("unixsocket", "socketpath"), DEFAULT_UNIX_SOCKET_PATH
)
self.assertEqual(config.get("tls", "port"), DEFAULT_GVM_PORT)
def test_get_unknown_setting(self):
config = Config()
self.assertIsNone(config.get("foo", "bar"))
def test_load(self):
test_config_path = __here__ / "test.cfg"
self.assertTrue(test_config_path.is_file())
config = Config()
config.load(test_config_path)
self.assertEqual(config.get("gmp", "username"), "bar")
self.assertEqual(config.get("gmp", "password"), "bar")
self.assertEqual(config.get("ssh", "username"), "ipsum")
self.assertEqual(config.get("ssh", "password"), "lorem")
self.assertEqual(config.get("ssh", "port"), "123")
self.assertEqual(
config.get("unixsocket", "socketpath"), "/foo/bar.sock"
)
self.assertEqual(config.get("tls", "port"), "123")
self.assertEqual(config.get("tls", "certfile"), "foo.cert")
self.assertEqual(config.get("tls", "keyfile"), "foo.key")
self.assertEqual(config.get("tls", "cafile"), "foo.ca")
self.assertDictEqual(
config.defaults(), dict(timeout="1000", foo="bar", username="ipsum")
)
def test_load_auth(self):
root = logging.getLogger(name)
root.disabled = True
test_config_path = __here__ / "test_auth.cfg"
self.assertTrue(test_config_path.is_file())
config = Config()
config.load(test_config_path)
self.assertEqual(config.get("gmp", "username"), "foo")
self.assertEqual(config.get("gmp", "password"), "bar")
root.disabled = False
def test_load_with_non_existing_configfile(self):
test_config_path = __here__ / "foo.cfg"
self.assertFalse(test_config_path.is_file())
config = Config()
with self.assertRaises(FileNotFoundError):
config.load(test_config_path)
|