|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2015 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 | + |
| 6 | +# |
| 7 | +# Test mulitple rpc user config option rpcauth |
| 8 | +# |
| 9 | + |
| 10 | +from test_framework.test_framework import BitcoinTestFramework |
| 11 | +from test_framework.util import * |
| 12 | +import base64 |
| 13 | + |
| 14 | +try: |
| 15 | + import http.client as httplib |
| 16 | +except ImportError: |
| 17 | + import httplib |
| 18 | +try: |
| 19 | + import urllib.parse as urlparse |
| 20 | +except ImportError: |
| 21 | + import urlparse |
| 22 | + |
| 23 | +class HTTPBasicsTest (BitcoinTestFramework): |
| 24 | + def setup_nodes(self): |
| 25 | + return start_nodes(4, self.options.tmpdir) |
| 26 | + |
| 27 | + def setup_chain(self): |
| 28 | + print("Initializing test directory "+self.options.tmpdir) |
| 29 | + initialize_chain(self.options.tmpdir) |
| 30 | + #Append rpcauth to bitcoin.conf before initialization |
| 31 | + rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144" |
| 32 | + rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e" |
| 33 | + with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a') as f: |
| 34 | + f.write(rpcauth+"\n") |
| 35 | + f.write(rpcauth2+"\n") |
| 36 | + |
| 37 | + def run_test(self): |
| 38 | + |
| 39 | + ################################################## |
| 40 | + # Check correctness of the rpcauth config option # |
| 41 | + ################################################## |
| 42 | + url = urlparse.urlparse(self.nodes[0].url) |
| 43 | + |
| 44 | + #Old authpair |
| 45 | + authpair = url.username + ':' + url.password |
| 46 | + |
| 47 | + #New authpair generated via contrib/rpcuser tool |
| 48 | + rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144" |
| 49 | + password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM=" |
| 50 | + |
| 51 | + #Second authpair with different username |
| 52 | + rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e" |
| 53 | + password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI=" |
| 54 | + authpairnew = "rt:"+password |
| 55 | + |
| 56 | + headers = {"Authorization": "Basic " + base64.b64encode(authpair)} |
| 57 | + |
| 58 | + conn = httplib.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, False) |
| 63 | + conn.close() |
| 64 | + |
| 65 | + #Use new authpair to confirm both work |
| 66 | + headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} |
| 67 | + |
| 68 | + conn = httplib.HTTPConnection(url.hostname, url.port) |
| 69 | + conn.connect() |
| 70 | + conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
| 71 | + resp = conn.getresponse() |
| 72 | + assert_equal(resp.status==401, False) |
| 73 | + conn.close() |
| 74 | + |
| 75 | + #Wrong login name with rt's password |
| 76 | + authpairnew = "rtwrong:"+password |
| 77 | + headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} |
| 78 | + |
| 79 | + conn = httplib.HTTPConnection(url.hostname, url.port) |
| 80 | + conn.connect() |
| 81 | + conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
| 82 | + resp = conn.getresponse() |
| 83 | + assert_equal(resp.status==401, True) |
| 84 | + conn.close() |
| 85 | + |
| 86 | + #Wrong password for rt |
| 87 | + authpairnew = "rt:"+password+"wrong" |
| 88 | + headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} |
| 89 | + |
| 90 | + conn = httplib.HTTPConnection(url.hostname, url.port) |
| 91 | + conn.connect() |
| 92 | + conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
| 93 | + resp = conn.getresponse() |
| 94 | + assert_equal(resp.status==401, True) |
| 95 | + conn.close() |
| 96 | + |
| 97 | + #Correct for rt2 |
| 98 | + authpairnew = "rt2:"+password2 |
| 99 | + headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} |
| 100 | + |
| 101 | + conn = httplib.HTTPConnection(url.hostname, url.port) |
| 102 | + conn.connect() |
| 103 | + conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
| 104 | + resp = conn.getresponse() |
| 105 | + assert_equal(resp.status==401, False) |
| 106 | + conn.close() |
| 107 | + |
| 108 | + #Wrong password for rt2 |
| 109 | + authpairnew = "rt2:"+password2+"wrong" |
| 110 | + headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} |
| 111 | + |
| 112 | + conn = httplib.HTTPConnection(url.hostname, url.port) |
| 113 | + conn.connect() |
| 114 | + conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
| 115 | + resp = conn.getresponse() |
| 116 | + assert_equal(resp.status==401, True) |
| 117 | + conn.close() |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + HTTPBasicsTest ().main () |
0 commit comments