2017-05-02 03:26:33 +02:00
#!/usr/bin/env python3
2019-12-30 10:39:22 +01:00
# Copyright (c) 2018-2019 The Bitcoin Core developers
2017-05-02 03:26:33 +02:00
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
""" Tests the includeconf argument
Verify that :
1. adding includeconf to the configuration file causes the includeconf
file to be loaded in the correct order .
2. includeconf cannot be used as a command line argument .
3. includeconf cannot be used recursively ( ie includeconf can only
be used from the base config file ) .
4. multiple includeconf arguments can be specified in the main config
file .
"""
import os
2018-02-07 16:51:13 +01:00
from test_framework . test_framework import BitcoinTestFramework
2017-05-02 03:26:33 +02:00
class IncludeConfTest ( BitcoinTestFramework ) :
def set_test_params ( self ) :
self . setup_clean_chain = False
self . num_nodes = 1
def setup_chain ( self ) :
super ( ) . setup_chain ( )
# Create additional config files
# - tmpdir/node0/relative.conf
with open ( os . path . join ( self . options . tmpdir , " node0 " , " relative.conf " ) , " w " , encoding = " utf8 " ) as f :
f . write ( " uacomment=relative \n " )
# - tmpdir/node0/relative2.conf
with open ( os . path . join ( self . options . tmpdir , " node0 " , " relative2.conf " ) , " w " , encoding = " utf8 " ) as f :
f . write ( " uacomment=relative2 \n " )
with open ( os . path . join ( self . options . tmpdir , " node0 " , " bitcoin.conf " ) , " a " , encoding = ' utf8 ' ) as f :
f . write ( " uacomment=main \n includeconf=relative.conf \n " )
def run_test ( self ) :
self . log . info ( " -includeconf works from config file. subversion should end with ' main; relative)/ ' " )
subversion = self . nodes [ 0 ] . getnetworkinfo ( ) [ " subversion " ]
assert subversion . endswith ( " main; relative)/ " )
2018-06-01 19:28:14 +02:00
self . log . info ( " -includeconf cannot be used as command-line arg " )
2017-05-02 03:26:33 +02:00
self . stop_node ( 0 )
2019-07-10 14:06:57 +02:00
self . nodes [ 0 ] . assert_start_raises_init_error ( extra_args = [ " -includeconf=relative2.conf " ] , expected_msg = " Error: Error parsing command line arguments: -includeconf cannot be used from commandline; -includeconf=relative2.conf " )
2017-05-02 03:26:33 +02:00
self . log . info ( " -includeconf cannot be used recursively. subversion should end with ' main; relative)/ ' " )
with open ( os . path . join ( self . options . tmpdir , " node0 " , " relative.conf " ) , " a " , encoding = " utf8 " ) as f :
f . write ( " includeconf=relative2.conf \n " )
2018-02-07 16:51:13 +01:00
self . start_node ( 0 )
2017-05-02 03:26:33 +02:00
subversion = self . nodes [ 0 ] . getnetworkinfo ( ) [ " subversion " ]
assert subversion . endswith ( " main; relative)/ " )
2018-05-09 09:50:02 +02:00
self . stop_node ( 0 , expected_stderr = " warning: -includeconf cannot be used from included files; ignoring -includeconf=relative2.conf " )
2017-05-02 03:26:33 +02:00
2018-06-01 19:28:14 +02:00
self . log . info ( " -includeconf cannot contain invalid arg " )
2018-07-30 09:25:25 +02:00
# Commented out as long as we ignore invalid arguments in configuration files
#with open(os.path.join(self.options.tmpdir, "node0", "relative.conf"), "w", encoding="utf8") as f:
# f.write("foo=bar\n")
2019-07-10 14:06:57 +02:00
#self.nodes[0].assert_start_raises_init_error(expected_msg="Error: Error reading configuration file: Invalid configuration value foo")
2018-06-01 19:28:14 +02:00
self . log . info ( " -includeconf cannot be invalid path " )
os . remove ( os . path . join ( self . options . tmpdir , " node0 " , " relative.conf " ) )
2019-07-10 14:06:57 +02:00
self . nodes [ 0 ] . assert_start_raises_init_error ( expected_msg = " Error: Error reading configuration file: Failed to include configuration file relative.conf " )
2018-06-01 19:28:14 +02:00
2017-05-02 03:26:33 +02:00
self . log . info ( " multiple -includeconf args can be used from the base config file. subversion should end with ' main; relative; relative2)/ ' " )
with open ( os . path . join ( self . options . tmpdir , " node0 " , " relative.conf " ) , " w " , encoding = " utf8 " ) as f :
2018-06-01 19:28:14 +02:00
# Restore initial file contents
2017-05-02 03:26:33 +02:00
f . write ( " uacomment=relative \n " )
with open ( os . path . join ( self . options . tmpdir , " node0 " , " bitcoin.conf " ) , " a " , encoding = ' utf8 ' ) as f :
f . write ( " includeconf=relative2.conf \n " )
2018-05-09 09:50:02 +02:00
self . start_node ( 0 )
2017-05-02 03:26:33 +02:00
subversion = self . nodes [ 0 ] . getnetworkinfo ( ) [ " subversion " ]
assert subversion . endswith ( " main; relative; relative2)/ " )
if __name__ == ' __main__ ' :
IncludeConfTest ( ) . main ( )