2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-07-27 00:36:45 +02:00
|
|
|
# Copyright (c) 2015-2018 The Bitcoin Core developers
|
2015-11-11 16:49:32 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test multiple RPC users."""
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-01-02 14:57:27 +01:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
get_datadir_path,
|
|
|
|
str_to_b64str,
|
|
|
|
)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-14 13:01:31 +02:00
|
|
|
import os
|
2016-03-19 20:58:06 +01:00
|
|
|
import http.client
|
|
|
|
import urllib.parse
|
2018-04-18 23:39:24 +02:00
|
|
|
import subprocess
|
|
|
|
from random import SystemRandom
|
|
|
|
import string
|
|
|
|
import configparser
|
2018-08-03 22:30:15 +02:00
|
|
|
import sys
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2019-07-03 19:33:00 +02:00
|
|
|
def call_with_auth(node, user, password):
|
|
|
|
url = urllib.parse.urlparse(node.url)
|
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str('{}:{}'.format(user, password))}
|
|
|
|
|
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
|
|
|
conn.close()
|
|
|
|
return resp
|
|
|
|
|
2018-01-02 14:57:27 +01:00
|
|
|
|
|
|
|
class HTTPBasicsTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2017-06-06 20:43:24 +02:00
|
|
|
self.num_nodes = 2
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
def setup_chain(self):
|
2016-05-14 13:01:31 +02:00
|
|
|
super().setup_chain()
|
2015-11-11 16:49:32 +01:00
|
|
|
#Append rpcauth to bitcoin.conf before initialization
|
2019-07-04 18:41:34 +02:00
|
|
|
self.rtpassword = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
|
2015-11-11 16:49:32 +01:00
|
|
|
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
|
2019-07-04 18:41:34 +02:00
|
|
|
|
|
|
|
self.rpcuser = "rpcuser💻"
|
|
|
|
self.rpcpassword = "rpcpassword🔑"
|
2018-04-18 23:39:24 +02:00
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read_file(open(self.options.configfile))
|
|
|
|
gen_rpcauth = config['environment']['RPCAUTH']
|
2019-07-03 20:56:28 +02:00
|
|
|
|
|
|
|
# Generate RPCAUTH with specified password
|
|
|
|
self.rt2password = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
|
|
|
|
p = subprocess.Popen([sys.executable, gen_rpcauth, 'rt2', self.rt2password], stdout=subprocess.PIPE, universal_newlines=True)
|
|
|
|
lines = p.stdout.read().splitlines()
|
|
|
|
rpcauth2 = lines[1]
|
|
|
|
|
|
|
|
# Generate RPCAUTH without specifying password
|
|
|
|
self.user = ''.join(SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(10))
|
2018-08-03 22:30:15 +02:00
|
|
|
p = subprocess.Popen([sys.executable, gen_rpcauth, self.user], stdout=subprocess.PIPE, universal_newlines=True)
|
2018-04-18 23:39:24 +02:00
|
|
|
lines = p.stdout.read().splitlines()
|
|
|
|
rpcauth3 = lines[1]
|
|
|
|
self.password = lines[3]
|
|
|
|
|
2018-01-02 14:57:27 +01:00
|
|
|
with open(os.path.join(get_datadir_path(self.options.tmpdir, 0), "bitcoin.conf"), 'a', encoding='utf8') as f:
|
2015-11-11 16:49:32 +01:00
|
|
|
f.write(rpcauth+"\n")
|
|
|
|
f.write(rpcauth2+"\n")
|
2018-04-18 23:39:24 +02:00
|
|
|
f.write(rpcauth3+"\n")
|
2018-01-02 14:57:27 +01:00
|
|
|
with open(os.path.join(get_datadir_path(self.options.tmpdir, 1), "bitcoin.conf"), 'a', encoding='utf8') as f:
|
2019-07-04 18:41:34 +02:00
|
|
|
f.write("rpcuser={}\n".format(self.rpcuser))
|
|
|
|
f.write("rpcpassword={}\n".format(self.rpcpassword))
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2019-07-03 20:39:49 +02:00
|
|
|
def test_auth(self, node, user, password):
|
2018-04-18 23:39:24 +02:00
|
|
|
self.log.info('Correct...')
|
2019-07-03 20:39:49 +02:00
|
|
|
assert_equal(200, call_with_auth(node, user, password).status)
|
2018-01-02 14:57:27 +01:00
|
|
|
|
2019-07-03 20:39:49 +02:00
|
|
|
self.log.info('Wrong...')
|
|
|
|
assert_equal(401, call_with_auth(node, user, password+'wrong').status)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2018-04-18 23:39:24 +02:00
|
|
|
self.log.info('Wrong...')
|
2019-07-03 20:39:49 +02:00
|
|
|
assert_equal(401, call_with_auth(node, user+'wrong', password).status)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2018-04-18 23:39:24 +02:00
|
|
|
self.log.info('Wrong...')
|
2019-07-03 20:39:49 +02:00
|
|
|
assert_equal(401, call_with_auth(node, user+'wrong', password+'wrong').status)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2019-07-03 20:39:49 +02:00
|
|
|
def run_test(self):
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2019-07-03 20:39:49 +02:00
|
|
|
##################################################
|
|
|
|
# Check correctness of the rpcauth config option #
|
|
|
|
##################################################
|
|
|
|
url = urllib.parse.urlparse(self.nodes[0].url)
|
2017-06-06 20:43:24 +02:00
|
|
|
|
2019-07-03 20:39:49 +02:00
|
|
|
self.test_auth(self.nodes[0], url.username, url.password)
|
2019-07-04 18:41:34 +02:00
|
|
|
self.test_auth(self.nodes[0], 'rt', self.rtpassword)
|
2019-07-03 20:56:28 +02:00
|
|
|
self.test_auth(self.nodes[0], 'rt2', self.rt2password)
|
2019-07-03 20:39:49 +02:00
|
|
|
self.test_auth(self.nodes[0], self.user, self.password)
|
2018-04-18 23:39:24 +02:00
|
|
|
|
2017-06-06 20:43:24 +02:00
|
|
|
###############################################################
|
|
|
|
# Check correctness of the rpcuser/rpcpassword config options #
|
|
|
|
###############################################################
|
|
|
|
url = urllib.parse.urlparse(self.nodes[1].url)
|
|
|
|
|
2019-07-04 18:41:34 +02:00
|
|
|
self.test_auth(self.nodes[1], self.rpcuser, self.rpcpassword)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
HTTPBasicsTest ().main ()
|