/ test / functional / feature_settings.py
feature_settings.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2017-2021 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  
21      def run_test(self):
22          node, = self.nodes
23          settings = node.chain_path / "settings.json"
24          conf = node.datadir_path / "bitcoin.conf"
25  
26          # Assert default settings file was created
27          self.stop_node(0)
28          default_settings = {"_warning_": "This file is automatically generated and updated by Bitcoin Core. Please do not edit this file while the node is running, as any changes might be ignored or overwritten."}
29          with settings.open() as fp:
30              assert_equal(json.load(fp), default_settings)
31  
32          # Assert settings are parsed and logged
33          with settings.open("w") as fp:
34              json.dump({"string": "string", "num": 5, "bool": True, "null": None, "list": [6, 7]}, fp)
35          with node.assert_debug_log(expected_msgs=[
36                  'Ignoring unknown rw_settings value bool',
37                  'Ignoring unknown rw_settings value list',
38                  'Ignoring unknown rw_settings value null',
39                  'Ignoring unknown rw_settings value num',
40                  'Ignoring unknown rw_settings value string',
41                  'Setting file arg: string = "string"',
42                  'Setting file arg: num = 5',
43                  'Setting file arg: bool = true',
44                  'Setting file arg: null = null',
45                  'Setting file arg: list = [6,7]',
46          ]):
47              self.start_node(0)
48              self.stop_node(0)
49  
50          # Assert settings are unchanged after shutdown
51          with settings.open() as fp:
52              assert_equal(json.load(fp), {**default_settings, **{"string": "string", "num": 5, "bool": True, "null": None, "list": [6, 7]}})
53  
54          # Test invalid json
55          with settings.open("w") as fp:
56              fp.write("invalid json")
57          node.assert_start_raises_init_error(expected_msg='does not contain valid JSON. This is probably caused by disk corruption or a crash', match=ErrorMatch.PARTIAL_REGEX)
58  
59          # Test invalid json object
60          with settings.open("w") as fp:
61              fp.write('"string"')
62          node.assert_start_raises_init_error(expected_msg='Found non-object value "string" in settings file', match=ErrorMatch.PARTIAL_REGEX)
63  
64          # Test invalid settings file containing duplicate keys
65          with settings.open("w") as fp:
66              fp.write('{"key": 1, "key": 2}')
67          node.assert_start_raises_init_error(expected_msg='Found duplicate key key in settings file', match=ErrorMatch.PARTIAL_REGEX)
68  
69          # Test invalid settings file is ignored with command line -nosettings
70          with node.assert_debug_log(expected_msgs=['Command-line arg: settings=false']):
71              self.start_node(0, extra_args=["-nosettings"])
72              self.stop_node(0)
73  
74          # Test invalid settings file is ignored with config file -nosettings
75          with conf.open('a') as conf:
76              conf.write('nosettings=1\n')
77          with node.assert_debug_log(expected_msgs=['Config file arg: [regtest] settings=false']):
78              self.start_node(0)
79              self.stop_node(0)
80  
81          # Test alternate settings path
82          altsettings = node.datadir_path / "altsettings.json"
83          with altsettings.open("w") as fp:
84              fp.write('{"key": "value"}')
85          with node.assert_debug_log(expected_msgs=['Setting file arg: key = "value"']):
86              self.start_node(0, extra_args=[f"-settings={altsettings}"])
87              self.stop_node(0)
88  
89  
90  if __name__ == '__main__':
91      SettingsTest().main()