mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-08 11:44:14 +01:00
Add test for rpcuser/rpcpassword
This commit is contained in:
@@ -16,16 +16,21 @@ class HTTPBasicsTest (BitcoinTestFramework):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setup_clean_chain = False
|
||||
self.num_nodes = 1
|
||||
self.num_nodes = 2
|
||||
|
||||
def setup_chain(self):
|
||||
super().setup_chain()
|
||||
#Append rpcauth to bitcoin.conf before initialization
|
||||
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
|
||||
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
|
||||
rpcuser = "rpcuser=rpcuser💻"
|
||||
rpcpassword = "rpcpassword=rpcpassword🔑"
|
||||
with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a', encoding='utf8') as f:
|
||||
f.write(rpcauth+"\n")
|
||||
f.write(rpcauth2+"\n")
|
||||
with open(os.path.join(self.options.tmpdir+"/node1", "bitcoin.conf"), 'a', encoding='utf8') as f:
|
||||
f.write(rpcuser+"\n")
|
||||
f.write(rpcpassword+"\n")
|
||||
|
||||
def run_test(self):
|
||||
|
||||
@@ -50,7 +55,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
|
||||
conn.connect()
|
||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||||
resp = conn.getresponse()
|
||||
assert_equal(resp.status==401, False)
|
||||
assert_equal(resp.status, 200)
|
||||
conn.close()
|
||||
|
||||
#Use new authpair to confirm both work
|
||||
@@ -60,7 +65,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
|
||||
conn.connect()
|
||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||||
resp = conn.getresponse()
|
||||
assert_equal(resp.status==401, False)
|
||||
assert_equal(resp.status, 200)
|
||||
conn.close()
|
||||
|
||||
#Wrong login name with rt's password
|
||||
@@ -71,7 +76,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
|
||||
conn.connect()
|
||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||||
resp = conn.getresponse()
|
||||
assert_equal(resp.status==401, True)
|
||||
assert_equal(resp.status, 401)
|
||||
conn.close()
|
||||
|
||||
#Wrong password for rt
|
||||
@@ -82,7 +87,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
|
||||
conn.connect()
|
||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||||
resp = conn.getresponse()
|
||||
assert_equal(resp.status==401, True)
|
||||
assert_equal(resp.status, 401)
|
||||
conn.close()
|
||||
|
||||
#Correct for rt2
|
||||
@@ -93,7 +98,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
|
||||
conn.connect()
|
||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||||
resp = conn.getresponse()
|
||||
assert_equal(resp.status==401, False)
|
||||
assert_equal(resp.status, 200)
|
||||
conn.close()
|
||||
|
||||
#Wrong password for rt2
|
||||
@@ -104,7 +109,46 @@ class HTTPBasicsTest (BitcoinTestFramework):
|
||||
conn.connect()
|
||||
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||||
resp = conn.getresponse()
|
||||
assert_equal(resp.status==401, True)
|
||||
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)
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user