add test for -walletrejectlongchains
[bitcoinplatinum.git] / qa / rpc-tests / multi_rpc.py
blob95d9090ce25d15053bf009db44f17430ea227d6b
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.
7 # Test multiple rpc user config option rpcauth
10 from test_framework.test_framework import BitcoinTestFramework
11 from test_framework.util import str_to_b64str, assert_equal
13 import os
14 import http.client
15 import urllib.parse
17 class HTTPBasicsTest (BitcoinTestFramework):
19 def __init__(self):
20 super().__init__()
21 self.setup_clean_chain = False
22 self.num_nodes = 1
24 def setup_chain(self):
25 super().setup_chain()
26 #Append rpcauth to bitcoin.conf before initialization
27 rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
28 rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
29 with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a', encoding='utf8') as f:
30 f.write(rpcauth+"\n")
31 f.write(rpcauth2+"\n")
33 def setup_network(self):
34 self.nodes = self.setup_nodes()
36 def run_test(self):
38 ##################################################
39 # Check correctness of the rpcauth config option #
40 ##################################################
41 url = urllib.parse.urlparse(self.nodes[0].url)
43 #Old authpair
44 authpair = url.username + ':' + url.password
46 #New authpair generated via share/rpcuser tool
47 rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
48 password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
50 #Second authpair with different username
51 rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
52 password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
53 authpairnew = "rt:"+password
55 headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
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, False)
62 conn.close()
64 #Use new authpair to confirm both work
65 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, False)
72 conn.close()
74 #Wrong login name with rt's password
75 authpairnew = "rtwrong:"+password
76 headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
78 conn = http.client.HTTPConnection(url.hostname, url.port)
79 conn.connect()
80 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
81 resp = conn.getresponse()
82 assert_equal(resp.status==401, True)
83 conn.close()
85 #Wrong password for rt
86 authpairnew = "rt:"+password+"wrong"
87 headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
89 conn = http.client.HTTPConnection(url.hostname, url.port)
90 conn.connect()
91 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
92 resp = conn.getresponse()
93 assert_equal(resp.status==401, True)
94 conn.close()
96 #Correct for rt2
97 authpairnew = "rt2:"+password2
98 headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
100 conn = http.client.HTTPConnection(url.hostname, url.port)
101 conn.connect()
102 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
103 resp = conn.getresponse()
104 assert_equal(resp.status==401, False)
105 conn.close()
107 #Wrong password for rt2
108 authpairnew = "rt2:"+password2+"wrong"
109 headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
111 conn = http.client.HTTPConnection(url.hostname, url.port)
112 conn.connect()
113 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
114 resp = conn.getresponse()
115 assert_equal(resp.status==401, True)
116 conn.close()
119 if __name__ == '__main__':
120 HTTPBasicsTest ().main ()