mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
58ba7a3d88
faf45d1f1f997c316fc4c611a23c4456533eefe9 http: Avoid crash when g_thread_http was never started (MarcoFalke) fa12a37b27f0570a551b8c103ea6537ee4a8e399 test: Replace inline-comments with logs, pep8 formatting (MarcoFalke) fa83b39ff3ae3fbad93df002915c0e5f99c104a9 init: Remove confusing and redundant InitError (MarcoFalke) Pull request description: Avoid a crash during shutdown when the init sequence failed for some reason ACKs for top commit: promag: Tested ACK faf45d1f1f997c316fc4c611a23c4456533eefe9. ryanofsky: Code review ACK faf45d1f1f997c316fc4c611a23c4456533eefe9. Thanks for updates, this is much easier to parse for me now. Since previous reviews: split out and reverted some cleanups & replaced chmod with mkdir in test hebasto: ACK faf45d1f1f997c316fc4c611a23c4456533eefe9, tested on Linux Mint 19.3 with the following patch: Tree-SHA512: 59632bf01c999e65c724e2728ac103250ccd8b0b16fac19d3a2a82639ab73e4f2efb86c78e63c588a5954625d8d0cf9545e2a7e070e6e15d2a54beeb50e00b61
211 lines
7.9 KiB
Python
Executable File
211 lines
7.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
"""Test multiple RPC users."""
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_equal,
|
|
get_datadir_path,
|
|
str_to_b64str,
|
|
)
|
|
|
|
import os
|
|
import http.client
|
|
import urllib.parse
|
|
import subprocess
|
|
from random import SystemRandom
|
|
import string
|
|
import configparser
|
|
import sys
|
|
|
|
|
|
class HTTPBasicsTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 2
|
|
self.supports_cli = False
|
|
|
|
def setup_chain(self):
|
|
super().setup_chain()
|
|
#Append rpcauth to dash.conf before initialization
|
|
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
|
|
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
|
|
rpcuser = "rpcuser=rpcuser💻"
|
|
rpcpassword = "rpcpassword=rpcpassword🔑"
|
|
|
|
self.user = ''.join(SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(10))
|
|
config = configparser.ConfigParser()
|
|
config.read_file(open(self.options.configfile))
|
|
gen_rpcauth = config['environment']['RPCAUTH']
|
|
p = subprocess.Popen([sys.executable, gen_rpcauth, self.user], stdout=subprocess.PIPE, universal_newlines=True)
|
|
lines = p.stdout.read().splitlines()
|
|
rpcauth3 = lines[1]
|
|
self.password = lines[3]
|
|
|
|
with open(os.path.join(get_datadir_path(self.options.tmpdir, 0), "dash.conf"), 'a', encoding='utf8') as f:
|
|
f.write(rpcauth + "\n")
|
|
f.write(rpcauth2 + "\n")
|
|
f.write(rpcauth3 + "\n")
|
|
with open(os.path.join(get_datadir_path(self.options.tmpdir, 1), "dash.conf"), 'a', encoding='utf8') as f:
|
|
f.write(rpcuser + "\n")
|
|
f.write(rpcpassword + "\n")
|
|
|
|
def run_test(self):
|
|
self.log.info('Check correctness of the rpcauth config option')
|
|
url = urllib.parse.urlparse(self.nodes[0].url)
|
|
|
|
#Old authpair
|
|
authpair = url.username + ':' + url.password
|
|
|
|
#New authpair generated via share/rpcauth tool
|
|
password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
|
|
|
|
#Second authpair with different username
|
|
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
|
|
authpairnew = "rt:"+password
|
|
|
|
self.log.info('Correct...')
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 200)
|
|
conn.close()
|
|
|
|
#Use new authpair to confirm both work
|
|
self.log.info('Correct...')
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 200)
|
|
conn.close()
|
|
|
|
#Wrong login name with rt's password
|
|
self.log.info('Wrong...')
|
|
authpairnew = "rtwrong:"+password
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 401)
|
|
conn.close()
|
|
|
|
#Wrong password for rt
|
|
self.log.info('Wrong...')
|
|
authpairnew = "rt:"+password+"wrong"
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 401)
|
|
conn.close()
|
|
|
|
#Correct for rt2
|
|
self.log.info('Correct...')
|
|
authpairnew = "rt2:"+password2
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 200)
|
|
conn.close()
|
|
|
|
#Wrong password for rt2
|
|
self.log.info('Wrong...')
|
|
authpairnew = "rt2:"+password2+"wrong"
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 401)
|
|
conn.close()
|
|
|
|
#Correct for randomly generated user
|
|
self.log.info('Correct...')
|
|
authpairnew = self.user+":"+self.password
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 200)
|
|
conn.close()
|
|
|
|
#Wrong password for randomly generated user
|
|
self.log.info('Wrong...')
|
|
authpairnew = self.user+":"+self.password+"Wrong"
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 401)
|
|
conn.close()
|
|
|
|
self.log.info('Check correctness of the rpcuser/rpcpassword config options')
|
|
url = urllib.parse.urlparse(self.nodes[1].url)
|
|
|
|
# rpcuser and rpcpassword authpair
|
|
self.log.info('Correct...')
|
|
rpcuserauthpair = "rpcuser💻:rpcpassword🔑"
|
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 200)
|
|
conn.close()
|
|
|
|
#Wrong login name with rpcuser's password
|
|
rpcuserauthpair = "rpcuserwrong:rpcpassword"
|
|
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 401)
|
|
conn.close()
|
|
|
|
#Wrong password for rpcuser
|
|
self.log.info('Wrong...')
|
|
rpcuserauthpair = "rpcuser:rpcpasswordwrong"
|
|
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
conn.connect()
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
resp = conn.getresponse()
|
|
assert_equal(resp.status, 401)
|
|
conn.close()
|
|
|
|
|
|
self.log.info('Check that failure to write cookie file will abort the node gracefully')
|
|
self.stop_node(0)
|
|
cookie_file = os.path.join(get_datadir_path(self.options.tmpdir, 0), self.chain, '.cookie.tmp')
|
|
os.mkdir(cookie_file)
|
|
init_error = 'Error: Unable to start HTTP server. See debug log for details.'
|
|
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
HTTPBasicsTest().main()
|