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."""
7 from test_framework
.test_framework
import BitcoinTestFramework
8 from test_framework
.util
import str_to_b64str
, assert_equal
14 class HTTPBasicsTest (BitcoinTestFramework
):
18 self
.setup_clean_chain
= False
21 def setup_chain(self
):
23 #Append rpcauth to bitcoin.conf before initialization
24 rpcauth
= "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
25 rpcauth2
= "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
26 rpcuser
= "rpcuser=rpcuser💻"
27 rpcpassword
= "rpcpassword=rpcpassword🔑"
28 with
open(os
.path
.join(self
.options
.tmpdir
+"/node0", "bitcoin.conf"), 'a', encoding
='utf8') as f
:
30 f
.write(rpcauth2
+"\n")
31 with
open(os
.path
.join(self
.options
.tmpdir
+"/node1", "bitcoin.conf"), 'a', encoding
='utf8') as f
:
33 f
.write(rpcpassword
+"\n")
37 ##################################################
38 # Check correctness of the rpcauth config option #
39 ##################################################
40 url
= urllib
.parse
.urlparse(self
.nodes
[0].url
)
43 authpair
= url
.username
+ ':' + url
.password
45 #New authpair generated via share/rpcuser tool
46 password
= "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
48 #Second authpair with different username
49 password2
= "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
50 authpairnew
= "rt:"+password
52 headers
= {"Authorization": "Basic " + str_to_b64str(authpair
)}
54 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
56 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
57 resp
= conn
.getresponse()
58 assert_equal(resp
.status
, 200)
61 #Use new authpair to confirm both work
62 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
64 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
66 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
67 resp
= conn
.getresponse()
68 assert_equal(resp
.status
, 200)
71 #Wrong login name with rt's password
72 authpairnew
= "rtwrong:"+password
73 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
75 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
77 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
78 resp
= conn
.getresponse()
79 assert_equal(resp
.status
, 401)
82 #Wrong password for rt
83 authpairnew
= "rt:"+password
+"wrong"
84 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
86 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
88 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
89 resp
= conn
.getresponse()
90 assert_equal(resp
.status
, 401)
94 authpairnew
= "rt2:"+password2
95 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
97 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
99 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
100 resp
= conn
.getresponse()
101 assert_equal(resp
.status
, 200)
104 #Wrong password for rt2
105 authpairnew
= "rt2:"+password2
+"wrong"
106 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
108 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
110 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
111 resp
= conn
.getresponse()
112 assert_equal(resp
.status
, 401)
115 ###############################################################
116 # Check correctness of the rpcuser/rpcpassword config options #
117 ###############################################################
118 url
= urllib
.parse
.urlparse(self
.nodes
[1].url
)
120 # rpcuser and rpcpassword authpair
121 rpcuserauthpair
= "rpcuser💻:rpcpassword🔑"
123 headers
= {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair
)}
125 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
127 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
128 resp
= conn
.getresponse()
129 assert_equal(resp
.status
, 200)
132 #Wrong login name with rpcuser's password
133 rpcuserauthpair
= "rpcuserwrong:rpcpassword"
134 headers
= {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair
)}
136 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
138 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
139 resp
= conn
.getresponse()
140 assert_equal(resp
.status
, 401)
143 #Wrong password for rpcuser
144 rpcuserauthpair
= "rpcuser:rpcpasswordwrong"
145 headers
= {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair
)}
147 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
149 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
150 resp
= conn
.getresponse()
151 assert_equal(resp
.status
, 401)
155 if __name__
== '__main__':
156 HTTPBasicsTest ().main ()