| 1 |
import ast |
| 2 |
import sys |
| 3 |
import os |
| 4 |
import tempfile |
| 5 |
import shutil |
| 6 |
from io import StringIO |
| 7 |
|
| 8 |
from electrum.simple_config import SimpleConfig, read_user_config |
| 9 |
|
| 10 |
from . import ElectrumTestCase |
| 11 |
|
| 12 |
|
| 13 |
MAX_MSG_SIZE_DEFAULT = SimpleConfig.NETWORK_MAX_INCOMING_MSG_SIZE.get_default_value() |
| 14 |
assert isinstance(MAX_MSG_SIZE_DEFAULT, int), MAX_MSG_SIZE_DEFAULT |
| 15 |
|
| 16 |
|
| 17 |
class Test_SimpleConfig(ElectrumTestCase): |
| 18 |
|
| 19 |
def setUp(self): |
| 20 |
super(Test_SimpleConfig, self).setUp() |
| 21 |
|
| 22 |
self.electrum_dir = tempfile.mkdtemp() |
| 23 |
|
| 24 |
|
| 25 |
self.user_dir = tempfile.mkdtemp() |
| 26 |
|
| 27 |
self.options = {"electrum_path": self.electrum_dir} |
| 28 |
self._saved_stdout = sys.stdout |
| 29 |
self._stdout_buffer = StringIO() |
| 30 |
sys.stdout = self._stdout_buffer |
| 31 |
|
| 32 |
def tearDown(self): |
| 33 |
super(Test_SimpleConfig, self).tearDown() |
| 34 |
|
| 35 |
|
| 36 |
shutil.rmtree(self.electrum_dir) |
| 37 |
shutil.rmtree(self.user_dir) |
| 38 |
|
| 39 |
|
| 40 |
sys.stdout = self._saved_stdout |
| 41 |
|
| 42 |
def test_simple_config_key_rename(self): |
| 43 |
"""auto_cycle was renamed auto_connect""" |
| 44 |
fake_read_user = lambda _: {"auto_cycle": True} |
| 45 |
read_user_dir = lambda : self.user_dir |
| 46 |
config = SimpleConfig(options=self.options, |
| 47 |
read_user_config_function=fake_read_user, |
| 48 |
read_user_dir_function=read_user_dir) |
| 49 |
self.assertEqual(config.get("auto_connect"), True) |
| 50 |
self.assertEqual(config.get("auto_cycle"), None) |
| 51 |
fake_read_user = lambda _: {"auto_connect": False, "auto_cycle": True} |
| 52 |
config = SimpleConfig(options=self.options, |
| 53 |
read_user_config_function=fake_read_user, |
| 54 |
read_user_dir_function=read_user_dir) |
| 55 |
self.assertEqual(config.get("auto_connect"), False) |
| 56 |
self.assertEqual(config.get("auto_cycle"), None) |
| 57 |
|
| 58 |
def test_simple_config_command_line_overrides_everything(self): |
| 59 |
"""Options passed by command line override all other configuration |
| 60 |
sources""" |
| 61 |
fake_read_user = lambda _: {"electrum_path": "b"} |
| 62 |
read_user_dir = lambda : self.user_dir |
| 63 |
config = SimpleConfig(options=self.options, |
| 64 |
read_user_config_function=fake_read_user, |
| 65 |
read_user_dir_function=read_user_dir) |
| 66 |
self.assertEqual(self.options.get("electrum_path"), |
| 67 |
config.get("electrum_path")) |
| 68 |
|
| 69 |
def test_simple_config_user_config_is_used_if_others_arent_specified(self): |
| 70 |
"""If no system-wide configuration and no command-line options are |
| 71 |
specified, the user configuration is used instead.""" |
| 72 |
fake_read_user = lambda _: {"electrum_path": self.electrum_dir} |
| 73 |
read_user_dir = lambda : self.user_dir |
| 74 |
config = SimpleConfig(options={}, |
| 75 |
read_user_config_function=fake_read_user, |
| 76 |
read_user_dir_function=read_user_dir) |
| 77 |
self.assertEqual(self.options.get("electrum_path"), |
| 78 |
config.get("electrum_path")) |
| 79 |
|
| 80 |
def test_cannot_set_options_passed_by_command_line(self): |
| 81 |
fake_read_user = lambda _: {"electrum_path": "b"} |
| 82 |
read_user_dir = lambda : self.user_dir |
| 83 |
config = SimpleConfig(options=self.options, |
| 84 |
read_user_config_function=fake_read_user, |
| 85 |
read_user_dir_function=read_user_dir) |
| 86 |
config.set_key("electrum_path", "c") |
| 87 |
self.assertEqual(self.options.get("electrum_path"), |
| 88 |
config.get("electrum_path")) |
| 89 |
|
| 90 |
def test_can_set_options_set_in_user_config(self): |
| 91 |
another_path = tempfile.mkdtemp() |
| 92 |
fake_read_user = lambda _: {"electrum_path": self.electrum_dir} |
| 93 |
read_user_dir = lambda : self.user_dir |
| 94 |
config = SimpleConfig(options={}, |
| 95 |
read_user_config_function=fake_read_user, |
| 96 |
read_user_dir_function=read_user_dir) |
| 97 |
config.set_key("electrum_path", another_path) |
| 98 |
self.assertEqual(another_path, config.get("electrum_path")) |
| 99 |
|
| 100 |
def test_user_config_is_not_written_with_read_only_config(self): |
| 101 |
"""The user config does not contain command-line options when saved.""" |
| 102 |
fake_read_user = lambda _: {"something": "a"} |
| 103 |
read_user_dir = lambda : self.user_dir |
| 104 |
self.options.update({"something": "c"}) |
| 105 |
config = SimpleConfig(options=self.options, |
| 106 |
read_user_config_function=fake_read_user, |
| 107 |
read_user_dir_function=read_user_dir) |
| 108 |
config.save_user_config() |
| 109 |
contents = None |
| 110 |
with open(os.path.join(self.electrum_dir, "config"), "r") as f: |
| 111 |
contents = f.read() |
| 112 |
result = ast.literal_eval(contents) |
| 113 |
result.pop('config_version', None) |
| 114 |
self.assertEqual({"something": "a"}, result) |
| 115 |
|
| 116 |
def test_configvars_set_and_get(self): |
| 117 |
config = SimpleConfig(self.options) |
| 118 |
self.assertEqual("server", config.cv.NETWORK_SERVER.key()) |
| 119 |
|
| 120 |
def _set_via_assignment(): |
| 121 |
config.NETWORK_SERVER = "example.com:443:s" |
| 122 |
|
| 123 |
for f in ( |
| 124 |
lambda: config.set_key("server", "example.com:443:s"), |
| 125 |
_set_via_assignment, |
| 126 |
lambda: config.cv.NETWORK_SERVER.set("example.com:443:s"), |
| 127 |
): |
| 128 |
self.assertTrue(config.get("server") is None) |
| 129 |
self.assertTrue(config.NETWORK_SERVER is None) |
| 130 |
self.assertTrue(config.cv.NETWORK_SERVER.get() is None) |
| 131 |
f() |
| 132 |
self.assertEqual("example.com:443:s", config.get("server")) |
| 133 |
self.assertEqual("example.com:443:s", config.NETWORK_SERVER) |
| 134 |
self.assertEqual("example.com:443:s", config.cv.NETWORK_SERVER.get()) |
| 135 |
|
| 136 |
config.NETWORK_SERVER = None |
| 137 |
|
| 138 |
def test_configvars_setter_catches_typo(self): |
| 139 |
config = SimpleConfig(self.options) |
| 140 |
assert not hasattr(config, "NETORK_AUTO_CONNECTT") |
| 141 |
with self.assertRaises(AttributeError): |
| 142 |
config.NETORK_AUTO_CONNECTT = False |
| 143 |
|
| 144 |
def test_configvars_get_default_value(self): |
| 145 |
config = SimpleConfig(self.options) |
| 146 |
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.cv.NETWORK_MAX_INCOMING_MSG_SIZE.get_default_value()) |
| 147 |
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 148 |
|
| 149 |
config.NETWORK_MAX_INCOMING_MSG_SIZE = 5_555_555 |
| 150 |
self.assertEqual(5_555_555, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 151 |
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.cv.NETWORK_MAX_INCOMING_MSG_SIZE.get_default_value()) |
| 152 |
|
| 153 |
config.NETWORK_MAX_INCOMING_MSG_SIZE = None |
| 154 |
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 155 |
|
| 156 |
def test_configvars_convert_getter(self): |
| 157 |
config = SimpleConfig(self.options) |
| 158 |
self.assertEqual(None, config.NETWORK_PROXY) |
| 159 |
config.user_config[config.cv.NETWORK_PROXY.key()] = None |
| 160 |
self.assertEqual("none", config.NETWORK_PROXY) |
| 161 |
config.NETWORK_PROXY = None |
| 162 |
self.assertEqual(None, config.NETWORK_PROXY) |
| 163 |
|
| 164 |
def test_configvars_convert_setter(self): |
| 165 |
config = SimpleConfig(self.options) |
| 166 |
self.assertEqual(60, config.CLI_TIMEOUT) |
| 167 |
assert isinstance(config.CLI_TIMEOUT, float) |
| 168 |
|
| 169 |
config.CLI_TIMEOUT = 10 |
| 170 |
self.assertEqual(10, config.CLI_TIMEOUT) |
| 171 |
assert isinstance(config.CLI_TIMEOUT, float) |
| 172 |
|
| 173 |
config.CLI_TIMEOUT = None |
| 174 |
self.assertEqual(60, config.CLI_TIMEOUT) |
| 175 |
assert isinstance(config.CLI_TIMEOUT, float) |
| 176 |
|
| 177 |
def test_configvars_is_set(self): |
| 178 |
config = SimpleConfig(self.options) |
| 179 |
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 180 |
self.assertFalse(config.cv.NETWORK_MAX_INCOMING_MSG_SIZE.is_set()) |
| 181 |
|
| 182 |
config.NETWORK_MAX_INCOMING_MSG_SIZE = 5_555_555 |
| 183 |
self.assertTrue(config.cv.NETWORK_MAX_INCOMING_MSG_SIZE.is_set()) |
| 184 |
|
| 185 |
config.NETWORK_MAX_INCOMING_MSG_SIZE = None |
| 186 |
self.assertFalse(config.cv.NETWORK_MAX_INCOMING_MSG_SIZE.is_set()) |
| 187 |
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 188 |
|
| 189 |
config.NETWORK_MAX_INCOMING_MSG_SIZE = MAX_MSG_SIZE_DEFAULT |
| 190 |
self.assertTrue(config.cv.NETWORK_MAX_INCOMING_MSG_SIZE.is_set()) |
| 191 |
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 192 |
|
| 193 |
def test_configvars_is_modifiable(self): |
| 194 |
config = SimpleConfig({**self.options, "server": "example.com:443:s"}) |
| 195 |
|
| 196 |
self.assertFalse(config.is_modifiable("server")) |
| 197 |
self.assertFalse(config.cv.NETWORK_SERVER.is_modifiable()) |
| 198 |
|
| 199 |
config.NETWORK_SERVER = "other-example.com:80:t" |
| 200 |
self.assertEqual("example.com:443:s", config.NETWORK_SERVER) |
| 201 |
|
| 202 |
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 203 |
self.assertTrue(config.cv.NETWORK_MAX_INCOMING_MSG_SIZE.is_modifiable()) |
| 204 |
config.NETWORK_MAX_INCOMING_MSG_SIZE = 5_555_555 |
| 205 |
self.assertEqual(5_555_555, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 206 |
|
| 207 |
config.make_key_not_modifiable(config.cv.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 208 |
self.assertFalse(config.cv.NETWORK_MAX_INCOMING_MSG_SIZE.is_modifiable()) |
| 209 |
config.NETWORK_MAX_INCOMING_MSG_SIZE = 2_222_222 |
| 210 |
self.assertEqual(5_555_555, config.NETWORK_MAX_INCOMING_MSG_SIZE) |
| 211 |
|
| 212 |
def test_configvars_from_key(self): |
| 213 |
config = SimpleConfig(self.options) |
| 214 |
self.assertEqual(config.cv.NETWORK_SERVER, config.cv.from_key("server")) |
| 215 |
with self.assertRaises(KeyError): |
| 216 |
config.cv.from_key("server333") |
| 217 |
|
| 218 |
def test_recursive_config(self): |
| 219 |
config = SimpleConfig(self.options) |
| 220 |
n = len(config.user_config) |
| 221 |
config.set_key('x.y.z', 1) |
| 222 |
self.assertEqual(len(config.user_config), n + 1) |
| 223 |
config.set_key('x.y.w', 1) |
| 224 |
self.assertEqual(len(config.user_config), n + 1) |
| 225 |
config.set_key('x.y.z', None) |
| 226 |
self.assertEqual(len(config.user_config), n + 1) |
| 227 |
config.set_key('x.y.w', None) |
| 228 |
self.assertEqual(len(config.user_config), n) |
| 229 |
|
| 230 |
|
| 231 |
class TestUserConfig(ElectrumTestCase): |
| 232 |
|
| 233 |
def setUp(self): |
| 234 |
super(TestUserConfig, self).setUp() |
| 235 |
self._saved_stdout = sys.stdout |
| 236 |
self._stdout_buffer = StringIO() |
| 237 |
sys.stdout = self._stdout_buffer |
| 238 |
|
| 239 |
self.user_dir = tempfile.mkdtemp() |
| 240 |
|
| 241 |
def tearDown(self): |
| 242 |
super(TestUserConfig, self).tearDown() |
| 243 |
shutil.rmtree(self.user_dir) |
| 244 |
sys.stdout = self._saved_stdout |
| 245 |
|
| 246 |
def test_no_path_means_no_result(self): |
| 247 |
result = read_user_config(None) |
| 248 |
self.assertEqual({}, result) |
| 249 |
|
| 250 |
def test_path_without_config_file(self): |
| 251 |
"""We pass a path but if does not contain a "config" file.""" |
| 252 |
result = read_user_config(self.user_dir) |
| 253 |
self.assertEqual({}, result) |
| 254 |
|
| 255 |
def test_path_with_reprd_object(self): |
| 256 |
|
| 257 |
class something(object): |
| 258 |
pass |
| 259 |
|
| 260 |
thefile = os.path.join(self.user_dir, "config") |
| 261 |
payload = something() |
| 262 |
with open(thefile, "w") as f: |
| 263 |
f.write(repr(payload)) |
| 264 |
|
| 265 |
with self.assertRaises(ValueError): |
| 266 |
read_user_config(self.user_dir) |