2022-01-28 13:55:23 +01:00
|
|
|
# pylint: disable=no-member, no-self-use
|
2018-04-18 21:58:46 +02:00
|
|
|
"""
|
|
|
|
Various tests for config
|
|
|
|
"""
|
|
|
|
import unittest
|
2021-05-27 15:18:07 +02:00
|
|
|
|
2021-09-27 12:32:17 +02:00
|
|
|
from six import StringIO
|
2018-04-18 21:58:46 +02:00
|
|
|
from pybitmessage.bmconfigparser import BMConfigParser
|
|
|
|
|
2021-09-27 12:32:17 +02:00
|
|
|
test_config = """[bitmessagesettings]
|
|
|
|
maxaddrperstreamsend = 100
|
|
|
|
maxbootstrapconnections = 10
|
|
|
|
maxdownloadrate = 0
|
|
|
|
maxoutboundconnections = 8
|
|
|
|
maxtotalconnections = 100
|
|
|
|
maxuploadrate = 0
|
|
|
|
apiinterface = 127.0.0.1
|
|
|
|
apiport = 8442
|
|
|
|
udp = True
|
|
|
|
|
|
|
|
[threads]
|
|
|
|
receive = 3
|
|
|
|
|
|
|
|
[network]
|
|
|
|
bind = None
|
|
|
|
dandelion = 90
|
|
|
|
|
|
|
|
[inventory]
|
|
|
|
storage = sqlite
|
|
|
|
acceptmismatch = False
|
|
|
|
|
|
|
|
[knownnodes]
|
|
|
|
maxnodes = 15000
|
|
|
|
|
|
|
|
[zlib]
|
|
|
|
maxsize = 1048576"""
|
2021-05-27 15:18:07 +02:00
|
|
|
|
2018-04-18 21:58:46 +02:00
|
|
|
|
|
|
|
class TestConfig(unittest.TestCase):
|
|
|
|
"""A test case for bmconfigparser"""
|
2021-09-27 12:32:17 +02:00
|
|
|
configfile = StringIO('')
|
2021-05-27 15:18:07 +02:00
|
|
|
|
2018-04-18 21:58:46 +02:00
|
|
|
def test_safeGet(self):
|
|
|
|
"""safeGet retuns provided default for nonexistent option or None"""
|
2022-01-28 13:55:23 +01:00
|
|
|
config = BMConfigParser()
|
2018-04-18 21:58:46 +02:00
|
|
|
self.assertIs(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGet('nonexistent', 'nonexistent'), None)
|
2018-04-18 21:58:46 +02:00
|
|
|
self.assertEqual(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGet('nonexistent', 'nonexistent', 42), 42)
|
2018-04-18 21:58:46 +02:00
|
|
|
|
|
|
|
def test_safeGetBoolean(self):
|
|
|
|
"""safeGetBoolean returns False for nonexistent option, no default"""
|
2022-01-28 13:55:23 +01:00
|
|
|
config = BMConfigParser()
|
2018-04-18 21:58:46 +02:00
|
|
|
self.assertIs(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGetBoolean('nonexistent', 'nonexistent'),
|
2018-04-18 21:58:46 +02:00
|
|
|
False
|
|
|
|
)
|
|
|
|
# no arg for default
|
2021-09-28 17:14:32 +02:00
|
|
|
# pylint: disable=too-many-function-args
|
2018-04-18 21:58:46 +02:00
|
|
|
with self.assertRaises(TypeError):
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGetBoolean(
|
2018-04-18 21:58:46 +02:00
|
|
|
'nonexistent', 'nonexistent', True)
|
|
|
|
|
|
|
|
def test_safeGetInt(self):
|
|
|
|
"""safeGetInt retuns provided default for nonexistent option or 0"""
|
2022-01-28 13:55:23 +01:00
|
|
|
config = BMConfigParser()
|
2018-04-18 21:58:46 +02:00
|
|
|
self.assertEqual(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGetInt('nonexistent', 'nonexistent'), 0)
|
2018-04-18 21:58:46 +02:00
|
|
|
self.assertEqual(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGetInt('nonexistent', 'nonexistent', 42), 42)
|
2021-08-16 17:35:04 +02:00
|
|
|
|
|
|
|
def test_safeGetFloat(self):
|
|
|
|
"""safeGetFloat retuns provided default for nonexistent option or 0.0"""
|
2022-01-28 13:55:23 +01:00
|
|
|
config = BMConfigParser()
|
2021-08-16 17:35:04 +02:00
|
|
|
self.assertEqual(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGetFloat('nonexistent', 'nonexistent'), 0.0)
|
2021-08-16 17:35:04 +02:00
|
|
|
self.assertEqual(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGetFloat('nonexistent', 'nonexistent', 42.0), 42.0)
|
2021-05-27 15:18:07 +02:00
|
|
|
|
|
|
|
def test_reset(self):
|
|
|
|
"""safeGetInt retuns provided default for bitmessagesettings option or 0"""
|
2022-01-28 13:55:23 +01:00
|
|
|
config = BMConfigParser()
|
2021-09-27 12:32:17 +02:00
|
|
|
test_config_object = StringIO(test_config)
|
2022-02-16 22:04:52 +01:00
|
|
|
config.read_file(test_config_object)
|
2021-05-27 15:18:07 +02:00
|
|
|
self.assertEqual(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGetInt('bitmessagesettings', 'maxaddrperstreamsend'), 100)
|
2021-09-28 17:41:45 +02:00
|
|
|
# pylint: disable=protected-access
|
2022-01-28 13:55:23 +01:00
|
|
|
config._reset()
|
|
|
|
self.assertEqual(config.sections(), [])
|
|
|
|
|
|
|
|
def test_defaults(self):
|
|
|
|
"""Loading defaults"""
|
|
|
|
config = BMConfigParser()
|
|
|
|
config.add_section('bitmessagesettings')
|
|
|
|
config.set("bitmessagesettings", "maxaddrperstreamsend", "100")
|
|
|
|
config.read()
|
2021-05-27 15:18:07 +02:00
|
|
|
self.assertEqual(
|
2022-01-28 13:55:23 +01:00
|
|
|
config.safeGetInt('bitmessagesettings', 'maxaddrperstreamsend'), 500)
|