Changeset View
Changeset View
Standalone View
Standalone View
qa/rpc-tests/multi_rpc.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2016 The Bitcoin Core developers | # Copyright (c) 2015-2016 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
# | # | ||||
# Test multiple rpc user config option rpcauth | # Test multiple rpc user config option rpcauth | ||||
# | # | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import str_to_b64str, assert_equal | from test_framework.util import str_to_b64str, assert_equal | ||||
import os | import os | ||||
import http.client | import http.client | ||||
import urllib.parse | import urllib.parse | ||||
class HTTPBasicsTest (BitcoinTestFramework): | class HTTPBasicsTest (BitcoinTestFramework): | ||||
def __init__(self): | def __init__(self): | ||||
super().__init__() | super().__init__() | ||||
self.setup_clean_chain = False | self.setup_clean_chain = False | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
def setup_chain(self): | def setup_chain(self): | ||||
super().setup_chain() | super().setup_chain() | ||||
#Append rpcauth to bitcoin.conf before initialization | # Append rpcauth to bitcoin.conf before initialization | ||||
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144" | rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144" | ||||
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e" | rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e" | ||||
with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a', encoding='utf8') as f: | with open(os.path.join(self.options.tmpdir + "/node0", "bitcoin.conf"), 'a', encoding='utf8') as f: | ||||
f.write(rpcauth+"\n") | f.write(rpcauth + "\n") | ||||
f.write(rpcauth2+"\n") | f.write(rpcauth2 + "\n") | ||||
def setup_network(self): | def setup_network(self): | ||||
self.nodes = self.setup_nodes() | self.nodes = self.setup_nodes() | ||||
def run_test(self): | def run_test(self): | ||||
################################################## | # | ||||
# Check correctness of the rpcauth config option # | # Check correctness of the rpcauth config option # | ||||
################################################## | # | ||||
url = urllib.parse.urlparse(self.nodes[0].url) | url = urllib.parse.urlparse(self.nodes[0].url) | ||||
#Old authpair | # Old authpair | ||||
authpair = url.username + ':' + url.password | authpair = url.username + ':' + url.password | ||||
#New authpair generated via share/rpcuser tool | # New authpair generated via share/rpcuser tool | ||||
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144" | rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144" | ||||
password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM=" | password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM=" | ||||
#Second authpair with different username | # Second authpair with different username | ||||
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e" | rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e" | ||||
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI=" | password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI=" | ||||
authpairnew = "rt:"+password | authpairnew = "rt:" + password | ||||
headers = {"Authorization": "Basic " + str_to_b64str(authpair)} | headers = {"Authorization": "Basic " + str_to_b64str(authpair)} | ||||
conn = http.client.HTTPConnection(url.hostname, url.port) | conn = http.client.HTTPConnection(url.hostname, url.port) | ||||
conn.connect() | conn.connect() | ||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | ||||
resp = conn.getresponse() | resp = conn.getresponse() | ||||
assert_equal(resp.status==401, False) | assert_equal(resp.status == 401, False) | ||||
conn.close() | conn.close() | ||||
#Use new authpair to confirm both work | # Use new authpair to confirm both work | ||||
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | ||||
conn = http.client.HTTPConnection(url.hostname, url.port) | conn = http.client.HTTPConnection(url.hostname, url.port) | ||||
conn.connect() | conn.connect() | ||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | ||||
resp = conn.getresponse() | resp = conn.getresponse() | ||||
assert_equal(resp.status==401, False) | assert_equal(resp.status == 401, False) | ||||
conn.close() | conn.close() | ||||
#Wrong login name with rt's password | # Wrong login name with rt's password | ||||
authpairnew = "rtwrong:"+password | authpairnew = "rtwrong:" + password | ||||
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | ||||
conn = http.client.HTTPConnection(url.hostname, url.port) | conn = http.client.HTTPConnection(url.hostname, url.port) | ||||
conn.connect() | conn.connect() | ||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | ||||
resp = conn.getresponse() | resp = conn.getresponse() | ||||
assert_equal(resp.status==401, True) | assert_equal(resp.status == 401, True) | ||||
conn.close() | conn.close() | ||||
#Wrong password for rt | # Wrong password for rt | ||||
authpairnew = "rt:"+password+"wrong" | authpairnew = "rt:" + password + "wrong" | ||||
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | ||||
conn = http.client.HTTPConnection(url.hostname, url.port) | conn = http.client.HTTPConnection(url.hostname, url.port) | ||||
conn.connect() | conn.connect() | ||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | ||||
resp = conn.getresponse() | resp = conn.getresponse() | ||||
assert_equal(resp.status==401, True) | assert_equal(resp.status == 401, True) | ||||
conn.close() | conn.close() | ||||
#Correct for rt2 | # Correct for rt2 | ||||
authpairnew = "rt2:"+password2 | authpairnew = "rt2:" + password2 | ||||
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | ||||
conn = http.client.HTTPConnection(url.hostname, url.port) | conn = http.client.HTTPConnection(url.hostname, url.port) | ||||
conn.connect() | conn.connect() | ||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | ||||
resp = conn.getresponse() | resp = conn.getresponse() | ||||
assert_equal(resp.status==401, False) | assert_equal(resp.status == 401, False) | ||||
conn.close() | conn.close() | ||||
#Wrong password for rt2 | # Wrong password for rt2 | ||||
authpairnew = "rt2:"+password2+"wrong" | authpairnew = "rt2:" + password2 + "wrong" | ||||
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} | ||||
conn = http.client.HTTPConnection(url.hostname, url.port) | conn = http.client.HTTPConnection(url.hostname, url.port) | ||||
conn.connect() | conn.connect() | ||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) | ||||
resp = conn.getresponse() | resp = conn.getresponse() | ||||
assert_equal(resp.status==401, True) | assert_equal(resp.status == 401, True) | ||||
conn.close() | conn.close() | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
HTTPBasicsTest ().main () | HTTPBasicsTest().main() |