feature_settings.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2017-present The Bitcoin Core developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  """Test various command line arguments and configuration file parameters."""
   6  
   7  import json
   8  
   9  
  10  from test_framework.test_framework import BitcoinTestFramework
  11  from test_framework.test_node import ErrorMatch
  12  from test_framework.util import assert_equal
  13  
  14  
  15  class SettingsTest(BitcoinTestFramework):
  16      def set_test_params(self):
  17          self.setup_clean_chain = True
  18          self.num_nodes = 1
  19          self.wallet_names = []
  20          self.uses_wallet = None
  21  
  22      def test_wallet_settings(self, settings_path):
  23          if not self.is_wallet_compiled():
  24              return
  25  
  26          self.log.info("Testing wallet settings..")
  27          node = self.nodes[0]
  28          # Create wallet to use it during tests
  29          self.start_node(0)
  30          node.createwallet(wallet_name='w1')
  31          self.stop_node(0)
  32  
  33          # Verify wallet settings can only be strings. Either names or paths. Not booleans, nums nor anything else.
  34          for wallets_data in [[10], [True], [[]], [{}], ["w1", 10], ["w1", False]]:
  35              with settings_path.open("w") as fp:
  36                  json.dump({"wallet": wallets_data}, fp)
  37              node.assert_start_raises_init_error(expected_msg="Error: Invalid value detected for '-wallet' or '-nowallet'. '-wallet' requires a string value, while '-nowallet' accepts only '1' to disable all wallets",
  38                                                  extra_args=[f'-settings={settings_path}'])
  39  
  40      def run_test(self):
  41          node, = self.nodes
  42          settings = node.chain_path / "settings.json"
  43          conf = node.datadir_path / "bitcoin.conf"
  44  
  45          # Assert default settings file was created
  46          self.stop_node(0)
  47          default_settings = {"_warning_": f"This file is automatically generated and updated by {self.config['environment']['CLIENT_NAME']}. Please do not edit this file while the node is running, as any changes might be ignored or overwritten."}
  48          with settings.open() as fp:
  49              assert_equal(json.load(fp), default_settings)
  50  
  51          # Assert settings are parsed and logged
  52          with settings.open("w") as fp:
  53              json.dump({"string": "string", "num": 5, "bool": True, "null": None, "list": [6, 7]}, fp)
  54          with node.assert_debug_log(expected_msgs=[
  55                  'Ignoring unknown rw_settings value bool',
  56                  'Ignoring unknown rw_settings value list',
  57                  'Ignoring unknown rw_settings value null',
  58                  'Ignoring unknown rw_settings value num',
  59                  'Ignoring unknown rw_settings value string',
  60                  'Setting file arg: string = "string"',
  61                  'Setting file arg: num = 5',
  62                  'Setting file arg: bool = true',
  63                  'Setting file arg: null = null',
  64                  'Setting file arg: list = [6,7]',
  65          ]):
  66              self.start_node(0)
  67              self.stop_node(0)
  68  
  69          # Assert settings are unchanged after shutdown
  70          with settings.open() as fp:
  71              assert_equal(json.load(fp), {**default_settings, **{"string": "string", "num": 5, "bool": True, "null": None, "list": [6, 7]}})
  72  
  73          # Test invalid json
  74          with settings.open("w") as fp:
  75              fp.write("invalid json")
  76          node.assert_start_raises_init_error(expected_msg='does not contain valid JSON. This may be caused by a crash, power loss, full disk, or storage error', match=ErrorMatch.PARTIAL_REGEX)
  77  
  78          # Test invalid json object
  79          with settings.open("w") as fp:
  80              fp.write('"string"')
  81          node.assert_start_raises_init_error(expected_msg='Found non-object value "string" in settings file', match=ErrorMatch.PARTIAL_REGEX)
  82  
  83          # Test invalid settings file containing duplicate keys
  84          with settings.open("w") as fp:
  85              fp.write('{"key": 1, "key": 2}')
  86          node.assert_start_raises_init_error(expected_msg='Found duplicate key key in settings file', match=ErrorMatch.PARTIAL_REGEX)
  87  
  88          # Test invalid settings file is ignored with command line -nosettings
  89          with node.assert_debug_log(expected_msgs=['Command-line arg: settings=false']):
  90              self.start_node(0, extra_args=["-nosettings"])
  91              self.stop_node(0)
  92  
  93          # Test invalid settings file is ignored with config file -nosettings
  94          with conf.open('a') as conf:
  95              conf.write('nosettings=1\n')
  96          with node.assert_debug_log(expected_msgs=['Config file arg: [regtest] settings=false']):
  97              self.start_node(0)
  98              self.stop_node(0)
  99  
 100          # Test alternate settings path
 101          altsettings = node.datadir_path / "altsettings.json"
 102          with altsettings.open("w") as fp:
 103              fp.write('{"key": "value"}')
 104          with node.assert_debug_log(expected_msgs=['Setting file arg: key = "value"']):
 105              self.start_node(0, extra_args=[f"-settings={altsettings}"])
 106              self.stop_node(0)
 107  
 108          self.test_wallet_settings(settings)
 109  
 110  
 111  if __name__ == '__main__':
 112      SettingsTest(__file__).main()
 113