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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 __init__(self):
  13. super().__init__()
  14. self.setup_clean_chain = False
  15. self.num_nodes = 1
  16. def setup_chain(self):
  17. super().setup_chain()
  18. #Append rpcauth to bitcoin.conf before initialization
  19. rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
  20. rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
  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. def setup_network(self):
  25. self.nodes = self.setup_nodes()
  26. def run_test(self):
  27. ##################################################
  28. # Check correctness of the rpcauth config option #
  29. ##################################################
  30. url = urllib.parse.urlparse(self.nodes[0].url)
  31. #Old authpair
  32. authpair = url.username + ':' + url.password
  33. #New authpair generated via share/rpcuser tool
  34. rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
  35. password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
  36. #Second authpair with different username
  37. rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
  38. password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
  39. authpairnew = "rt:"+password
  40. headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
  41. conn = http.client.HTTPConnection(url.hostname, url.port)
  42. conn.connect()
  43. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  44. resp = conn.getresponse()
  45. assert_equal(resp.status==401, False)
  46. conn.close()
  47. #Use new authpair to confirm both work
  48. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  49. conn = http.client.HTTPConnection(url.hostname, url.port)
  50. conn.connect()
  51. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  52. resp = conn.getresponse()
  53. assert_equal(resp.status==401, False)
  54. conn.close()
  55. #Wrong login name with rt's password
  56. authpairnew = "rtwrong:"+password
  57. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  58. conn = http.client.HTTPConnection(url.hostname, url.port)
  59. conn.connect()
  60. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  61. resp = conn.getresponse()
  62. assert_equal(resp.status==401, True)
  63. conn.close()
  64. #Wrong password for rt
  65. authpairnew = "rt:"+password+"wrong"
  66. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  67. conn = http.client.HTTPConnection(url.hostname, url.port)
  68. conn.connect()
  69. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  70. resp = conn.getresponse()
  71. assert_equal(resp.status==401, True)
  72. conn.close()
  73. #Correct for rt2
  74. authpairnew = "rt2:"+password2
  75. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  76. conn = http.client.HTTPConnection(url.hostname, url.port)
  77. conn.connect()
  78. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  79. resp = conn.getresponse()
  80. assert_equal(resp.status==401, False)
  81. conn.close()
  82. #Wrong password for rt2
  83. authpairnew = "rt2:"+password2+"wrong"
  84. headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
  85. conn = http.client.HTTPConnection(url.hostname, url.port)
  86. conn.connect()
  87. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  88. resp = conn.getresponse()
  89. assert_equal(resp.status==401, True)
  90. conn.close()
  91. if __name__ == '__main__':
  92. HTTPBasicsTest ().main ()