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