2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2015-2016 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.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test multiple RPC users."""
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2016-05-20 15:16:51 +02:00
|
|
|
from test_framework.util import str_to_b64str, assert_equal
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-20 15:16:51 +02:00
|
|
|
import os
|
2016-05-06 11:23:48 +02:00
|
|
|
import http.client
|
|
|
|
import urllib.parse
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
class HTTPBasicsTest (BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2017-06-21 14:26:10 +02:00
|
|
|
self.num_nodes = 2
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
def setup_chain(self):
|
2016-05-20 15:16:51 +02:00
|
|
|
super().setup_chain()
|
2016-03-06 16:14:39 +01:00
|
|
|
#Append rpcauth to dash.conf before initialization
|
2015-11-11 16:49:32 +01:00
|
|
|
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
|
|
|
|
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
|
2017-06-21 14:26:10 +02:00
|
|
|
rpcuser = "rpcuser=rpcuser💻"
|
|
|
|
rpcpassword = "rpcpassword=rpcpassword🔑"
|
2016-09-30 12:34:37 +02:00
|
|
|
with open(os.path.join(self.options.tmpdir+"/node0", "dash.conf"), 'a', encoding='utf8') as f:
|
2015-11-11 16:49:32 +01:00
|
|
|
f.write(rpcauth+"\n")
|
|
|
|
f.write(rpcauth2+"\n")
|
2019-07-10 16:45:09 +02:00
|
|
|
with open(os.path.join(self.options.tmpdir+"/node1", "dash.conf"), 'a', encoding='utf8') as f:
|
2017-06-21 14:26:10 +02:00
|
|
|
f.write(rpcuser+"\n")
|
|
|
|
f.write(rpcpassword+"\n")
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
|
|
|
|
##################################################
|
|
|
|
# Check correctness of the rpcauth config option #
|
|
|
|
##################################################
|
2016-05-06 11:23:48 +02:00
|
|
|
url = urllib.parse.urlparse(self.nodes[0].url)
|
2015-11-11 16:49:32 +01:00
|
|
|
|
|
|
|
#Old authpair
|
|
|
|
authpair = url.username + ':' + url.password
|
|
|
|
|
2015-12-19 14:27:15 +01:00
|
|
|
#New authpair generated via share/rpcuser tool
|
2015-11-11 16:49:32 +01:00
|
|
|
password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
|
|
|
|
|
|
|
|
#Second authpair with different username
|
|
|
|
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
|
|
|
|
authpairnew = "rt:"+password
|
|
|
|
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 200)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Use new authpair to confirm both work
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 200)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong login name with rt's password
|
|
|
|
authpairnew = "rtwrong:"+password
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 401)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong password for rt
|
|
|
|
authpairnew = "rt:"+password+"wrong"
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 401)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Correct for rt2
|
|
|
|
authpairnew = "rt2:"+password2
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 200)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
#Wrong password for rt2
|
|
|
|
authpairnew = "rt2:"+password2+"wrong"
|
2016-04-10 16:54:28 +02:00
|
|
|
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
|
2015-11-11 16:49:32 +01:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.connect()
|
|
|
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
|
|
|
resp = conn.getresponse()
|
2017-06-21 14:26:10 +02:00
|
|
|
assert_equal(resp.status, 401)
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
###############################################################
|
|
|
|
# Check correctness of the rpcuser/rpcpassword config options #
|
|
|
|
###############################################################
|
|
|
|
url = urllib.parse.urlparse(self.nodes[1].url)
|
|
|
|
|
|
|
|
# rpcuser and rpcpassword authpair
|
|
|
|
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
|
|
|
|
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)
|
2015-11-11 16:49:32 +01:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
HTTPBasicsTest ().main ()
|