2018-04-18 21:58:46 +02:00
|
|
|
"""
|
|
|
|
Various tests for config
|
|
|
|
"""
|
|
|
|
|
2019-03-13 16:56:26 +01:00
|
|
|
import os
|
2018-04-18 21:58:46 +02:00
|
|
|
import unittest
|
2020-11-02 11:27:02 +01:00
|
|
|
import tempfile
|
2018-04-18 21:58:46 +02:00
|
|
|
|
|
|
|
from pybitmessage.bmconfigparser import BMConfigParser
|
2019-03-13 16:56:26 +01:00
|
|
|
from test_process import TestProcessProto
|
2018-04-18 21:58:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestConfig(unittest.TestCase):
|
|
|
|
"""A test case for bmconfigparser"""
|
|
|
|
|
|
|
|
def test_safeGet(self):
|
|
|
|
"""safeGet retuns provided default for nonexistent option or None"""
|
|
|
|
self.assertIs(
|
|
|
|
BMConfigParser().safeGet('nonexistent', 'nonexistent'), None)
|
|
|
|
self.assertEqual(
|
|
|
|
BMConfigParser().safeGet('nonexistent', 'nonexistent', 42), 42)
|
|
|
|
|
|
|
|
def test_safeGetBoolean(self):
|
|
|
|
"""safeGetBoolean returns False for nonexistent option, no default"""
|
|
|
|
self.assertIs(
|
|
|
|
BMConfigParser().safeGetBoolean('nonexistent', 'nonexistent'),
|
|
|
|
False
|
|
|
|
)
|
|
|
|
# no arg for default
|
2019-09-26 13:27:17 +02:00
|
|
|
# pylint: disable=too-many-function-args
|
2018-04-18 21:58:46 +02:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
BMConfigParser().safeGetBoolean(
|
|
|
|
'nonexistent', 'nonexistent', True)
|
|
|
|
|
|
|
|
def test_safeGetInt(self):
|
|
|
|
"""safeGetInt retuns provided default for nonexistent option or 0"""
|
|
|
|
self.assertEqual(
|
|
|
|
BMConfigParser().safeGetInt('nonexistent', 'nonexistent'), 0)
|
|
|
|
self.assertEqual(
|
|
|
|
BMConfigParser().safeGetInt('nonexistent', 'nonexistent', 42), 42)
|
2019-03-13 16:56:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestProcessConfig(TestProcessProto):
|
|
|
|
"""A test case for keys.dat"""
|
2020-11-02 11:27:02 +01:00
|
|
|
home = tempfile.mkdtemp()
|
2019-03-13 16:56:26 +01:00
|
|
|
|
|
|
|
def test_config_defaults(self):
|
|
|
|
"""Test settings in the generated config"""
|
|
|
|
self._stop_process()
|
|
|
|
config = BMConfigParser()
|
|
|
|
config.read(os.path.join(self.home, 'keys.dat'))
|
|
|
|
|
2019-09-26 13:27:17 +02:00
|
|
|
self.assertEqual(config.safeGetInt(
|
2019-03-13 16:56:26 +01:00
|
|
|
'bitmessagesettings', 'settingsversion'), 10)
|
2019-09-26 13:27:17 +02:00
|
|
|
self.assertEqual(config.safeGetInt(
|
2019-03-13 16:56:26 +01:00
|
|
|
'bitmessagesettings', 'port'), 8444)
|
|
|
|
# don't connect
|
|
|
|
self.assertTrue(config.safeGetBoolean(
|
|
|
|
'bitmessagesettings', 'dontconnect'))
|
|
|
|
# API disabled
|
|
|
|
self.assertFalse(config.safeGetBoolean(
|
|
|
|
'bitmessagesettings', 'apienabled'))
|
|
|
|
|
|
|
|
# extralowdifficulty is false
|
2019-09-26 13:27:17 +02:00
|
|
|
self.assertEqual(config.safeGetInt(
|
2019-03-13 16:56:26 +01:00
|
|
|
'bitmessagesettings', 'defaultnoncetrialsperbyte'), 1000)
|
2019-09-26 13:27:17 +02:00
|
|
|
self.assertEqual(config.safeGetInt(
|
2019-03-13 16:56:26 +01:00
|
|
|
'bitmessagesettings', 'defaultpayloadlengthextrabytes'), 1000)
|