electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit cf88e239d7e5d8d690aae0742f6daafb3fe0eb45
parent bfb0141b2075110945fa79072d28223eb43a7321
Author: ghost43 <somber.night@protonmail.com>
Date:   Fri,  6 Apr 2018 18:53:13 +0200

fix parsing values in setconfig (#4225)


Diffstat:
Mlib/commands.py | 13+++++++++++--
Alib/tests/test_commands.py | 33+++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/lib/commands.py b/lib/commands.py @@ -150,11 +150,20 @@ class Commands: """Return a configuration variable. """ return self.config.get(key) + @classmethod + def _setconfig_normalize_value(cls, key, value): + if key not in ('rpcuser', 'rpcpassword'): + value = json_decode(value) + try: + value = ast.literal_eval(value) + except: + pass + return value + @command('') def setconfig(self, key, value): """Set a configuration variable. 'value' may be a string or a Python expression.""" - if key not in ('rpcuser', 'rpcpassword'): - value = json_decode(value) + value = self._setconfig_normalize_value(key, value) self.config.set_key(key, value) return True diff --git a/lib/tests/test_commands.py b/lib/tests/test_commands.py @@ -0,0 +1,33 @@ +import unittest +from decimal import Decimal + +from lib.commands import Commands + + +class TestCommands(unittest.TestCase): + + def test_setconfig_non_auth_number(self): + self.assertEqual(7777, Commands._setconfig_normalize_value('rpcport', "7777")) + self.assertEqual(7777, Commands._setconfig_normalize_value('rpcport', '7777')) + self.assertAlmostEqual(Decimal(2.3), Commands._setconfig_normalize_value('somekey', '2.3')) + + def test_setconfig_non_auth_number_as_string(self): + self.assertEqual("7777", Commands._setconfig_normalize_value('somekey', "'7777'")) + + def test_setconfig_non_auth_boolean(self): + self.assertEqual(True, Commands._setconfig_normalize_value('show_console_tab', "true")) + self.assertEqual(True, Commands._setconfig_normalize_value('show_console_tab', "True")) + + def test_setconfig_non_auth_list(self): + self.assertEqual(['file:///var/www/', 'https://electrum.org'], + Commands._setconfig_normalize_value('url_rewrite', "['file:///var/www/','https://electrum.org']")) + self.assertEqual(['file:///var/www/', 'https://electrum.org'], + Commands._setconfig_normalize_value('url_rewrite', '["file:///var/www/","https://electrum.org"]')) + + def test_setconfig_auth(self): + self.assertEqual("7777", Commands._setconfig_normalize_value('rpcuser', "7777")) + self.assertEqual("7777", Commands._setconfig_normalize_value('rpcuser', '7777')) + self.assertEqual("7777", Commands._setconfig_normalize_value('rpcpassword', '7777')) + self.assertEqual("2asd", Commands._setconfig_normalize_value('rpcpassword', '2asd')) + self.assertEqual("['file:///var/www/','https://electrum.org']", + Commands._setconfig_normalize_value('rpcpassword', "['file:///var/www/','https://electrum.org']"))