test: rpc_users: Add function for auth'd requests.

This commit is contained in:
Carl Dong 2019-07-03 13:33:00 -04:00
parent c799976c86
commit 604e2a997f
No known key found for this signature in database
GPG key ID: 0CC52153197991A5

View file

@ -20,6 +20,17 @@ import string
import configparser import configparser
import sys import sys
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
class HTTPBasicsTest(BitcoinTestFramework): class HTTPBasicsTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
@ -57,108 +68,39 @@ class HTTPBasicsTest(BitcoinTestFramework):
################################################## ##################################################
url = urllib.parse.urlparse(self.nodes[0].url) url = urllib.parse.urlparse(self.nodes[0].url)
#Old authpair
authpair = url.username + ':' + url.password
#New authpair generated via share/rpcauth tool
password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM=" password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
#Second authpair with different username
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI=" password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
authpairnew = "rt:"+password
self.log.info('Correct...') self.log.info('Correct...')
headers = {"Authorization": "Basic " + str_to_b64str(authpair)} assert_equal(200, call_with_auth(self.nodes[0], url.username, url.password).status)
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()
#Use new authpair to confirm both work #Use new authpair to confirm both work
self.log.info('Correct...') self.log.info('Correct...')
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)} assert_equal(200, call_with_auth(self.nodes[0], 'rt', password).status)
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 rt's password #Wrong login name with rt's password
self.log.info('Wrong...') self.log.info('Wrong...')
authpairnew = "rtwrong:"+password assert_equal(401, call_with_auth(self.nodes[0], 'rtwrong', password).status)
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
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 rt #Wrong password for rt
self.log.info('Wrong...') self.log.info('Wrong...')
authpairnew = "rt:"+password+"wrong" assert_equal(401, call_with_auth(self.nodes[0], 'rt', password+'wrong').status)
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
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()
#Correct for rt2 #Correct for rt2
self.log.info('Correct...') self.log.info('Correct...')
authpairnew = "rt2:"+password2 assert_equal(200, call_with_auth(self.nodes[0], 'rt2', password2).status)
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
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 password for rt2 #Wrong password for rt2
self.log.info('Wrong...') self.log.info('Wrong...')
authpairnew = "rt2:"+password2+"wrong" assert_equal(401, call_with_auth(self.nodes[0], 'rt2', password2+'wrong').status)
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
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()
#Correct for randomly generated user #Correct for randomly generated user
self.log.info('Correct...') self.log.info('Correct...')
authpairnew = self.user+":"+self.password assert_equal(200, call_with_auth(self.nodes[0], self.user, self.password).status)
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
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 password for randomly generated user #Wrong password for randomly generated user
self.log.info('Wrong...') self.log.info('Wrong...')
authpairnew = self.user+":"+self.password+"Wrong" assert_equal(401, call_with_auth(self.nodes[0], self.user, self.password+'Wrong').status)
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
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()
############################################################### ###############################################################
# Check correctness of the rpcuser/rpcpassword config options # # Check correctness of the rpcuser/rpcpassword config options #
@ -167,40 +109,15 @@ class HTTPBasicsTest(BitcoinTestFramework):
# rpcuser and rpcpassword authpair # rpcuser and rpcpassword authpair
self.log.info('Correct...') self.log.info('Correct...')
rpcuserauthpair = "rpcuser💻:rpcpassword🔑" assert_equal(200, call_with_auth(self.nodes[1], "rpcuser💻", "rpcpassword🔑").status)
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 #Wrong login name with rpcuser's password
rpcuserauthpair = "rpcuserwrong:rpcpassword" self.log.info('Wrong...')
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)} assert_equal(401, call_with_auth(self.nodes[1], 'rpcuserwrong', 'rpcpassword').status)
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 #Wrong password for rpcuser
self.log.info('Wrong...') self.log.info('Wrong...')
rpcuserauthpair = "rpcuser:rpcpasswordwrong" assert_equal(401, call_with_auth(self.nodes[1], 'rpcuser', 'rpcpasswordwrong').status)
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()
if __name__ == '__main__': if __name__ == '__main__':
HTTPBasicsTest ().main () HTTPBasicsTest ().main ()