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.

multi_rpc.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2015-2016 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. """Test multiple RPC users."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import str_to_b64str, assert_equal
  8. import os
  9. import http.client
  10. import urllib.parse
  11. class HTTPBasicsTest (BitcoinTestFramework):
  12. def set_test_params(self):
  13. self.num_nodes = 2
  14. def setup_chain(self):
  15. super().setup_chain()
  16. #Append rpcauth to bitcoin.conf before initialization
  17. rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
  18. rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
  19. rpcuser = "rpcuser=rpcuser💻"
  20. rpcpassword = "rpcpassword=rpcpassword🔑"
  21. with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a', encoding='utf8') as f:
  22. f.write(rpcauth+"\n")
  23. f.write(rpcauth2+"\n")
  24. with open(os.path.join(self.options.tmpdir+"/node1", "bitcoin.conf"), 'a', encoding='utf8') as f:
  25. f.write(rpcuser+"\n")
  26. f.write(rpcpassword+"\n")
  27. def run_test(self):
  28. ##################################################
  29. # Check correctness of the rpcauth config option #
  30. ##################################################
  31. url = urllib.parse.urlparse(self.nodes[0].url)
  32. #Old authpair
  33. authpair = url.username + ':' + url.password
  34. #New authpair generated via share/rpcuser tool
  35. password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
  36. #Second authpair with different username
  37. password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
  38. authpairnew = "rt:"+password
  39. headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
  40. conn = http.client.HTTPConnection(url.hostname, url.port)
  41. conn.connect()
  42. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  43. resp = conn.getresponse()
  44. assert_equal(resp.status, 200)
  45. conn.close()
  46. #Use new authpair to confirm both work
  47. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  48. conn = http.client.HTTPConnection(url.hostname, url.port)
  49. conn.connect()
  50. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  51. resp = conn.getresponse()
  52. assert_equal(resp.status, 200)
  53. conn.close()
  54. #Wrong login name with rt's password
  55. authpairnew = "rtwrong:"+password
  56. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  57. conn = http.client.HTTPConnection(url.hostname, url.port)
  58. conn.connect()
  59. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  60. resp = conn.getresponse()
  61. assert_equal(resp.status, 401)
  62. conn.close()
  63. #Wrong password for rt
  64. authpairnew = "rt:"+password+"wrong"
  65. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  66. conn = http.client.HTTPConnection(url.hostname, url.port)
  67. conn.connect()
  68. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  69. resp = conn.getresponse()
  70. assert_equal(resp.status, 401)
  71. conn.close()
  72. #Correct for rt2
  73. authpairnew = "rt2:"+password2
  74. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  75. conn = http.client.HTTPConnection(url.hostname, url.port)
  76. conn.connect()
  77. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  78. resp = conn.getresponse()
  79. assert_equal(resp.status, 200)
  80. conn.close()
  81. #Wrong password for rt2
  82. authpairnew = "rt2:"+password2+"wrong"
  83. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  84. conn = http.client.HTTPConnection(url.hostname, url.port)
  85. conn.connect()
  86. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  87. resp = conn.getresponse()
  88. assert_equal(resp.status, 401)
  89. conn.close()
  90. ###############################################################
  91. # Check correctness of the rpcuser/rpcpassword config options #
  92. ###############################################################
  93. url = urllib.parse.urlparse(self.nodes[1].url)
  94. # rpcuser and rpcpassword authpair
  95. rpcuserauthpair = "rpcuser💻:rpcpassword🔑"
  96. headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
  97. conn = http.client.HTTPConnection(url.hostname, url.port)
  98. conn.connect()
  99. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  100. resp = conn.getresponse()
  101. assert_equal(resp.status, 200)
  102. conn.close()
  103. #Wrong login name with rpcuser's password
  104. rpcuserauthpair = "rpcuserwrong:rpcpassword"
  105. headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
  106. conn = http.client.HTTPConnection(url.hostname, url.port)
  107. conn.connect()
  108. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  109. resp = conn.getresponse()
  110. assert_equal(resp.status, 401)
  111. conn.close()
  112. #Wrong password for rpcuser
  113. rpcuserauthpair = "rpcuser:rpcpasswordwrong"
  114. headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
  115. conn = http.client.HTTPConnection(url.hostname, url.port)
  116. conn.connect()
  117. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  118. resp = conn.getresponse()
  119. assert_equal(resp.status, 401)
  120. conn.close()
  121. if __name__ == '__main__':
  122. HTTPBasicsTest ().main ()