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
):
15 def set_test_params(self
):
18 def setup_chain(self
):
20 #Append rpcauth to bitcoin.conf before initialization
21 rpcauth
= "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
22 rpcauth2
= "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
23 rpcuser
= "rpcuser=rpcuser💻"
24 rpcpassword
= "rpcpassword=rpcpassword🔑"
25 with
open(os
.path
.join(self
.options
.tmpdir
+"/node0", "bitcoin.conf"), 'a', encoding
='utf8') as f
:
27 f
.write(rpcauth2
+"\n")
28 with
open(os
.path
.join(self
.options
.tmpdir
+"/node1", "bitcoin.conf"), 'a', encoding
='utf8') as f
:
30 f
.write(rpcpassword
+"\n")
34 ##################################################
35 # Check correctness of the rpcauth config option #
36 ##################################################
37 url
= urllib
.parse
.urlparse(self
.nodes
[0].url
)
40 authpair
= url
.username
+ ':' + url
.password
42 #New authpair generated via share/rpcuser tool
43 password
= "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
45 #Second authpair with different username
46 password2
= "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
47 authpairnew
= "rt:"+password
49 headers
= {"Authorization": "Basic " + str_to_b64str(authpair
)}
51 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
53 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
54 resp
= conn
.getresponse()
55 assert_equal(resp
.status
, 200)
58 #Use new authpair to confirm both work
59 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
61 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
63 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
64 resp
= conn
.getresponse()
65 assert_equal(resp
.status
, 200)
68 #Wrong login name with rt's password
69 authpairnew
= "rtwrong:"+password
70 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
72 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
74 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
75 resp
= conn
.getresponse()
76 assert_equal(resp
.status
, 401)
79 #Wrong password for rt
80 authpairnew
= "rt:"+password
+"wrong"
81 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
83 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
85 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
86 resp
= conn
.getresponse()
87 assert_equal(resp
.status
, 401)
91 authpairnew
= "rt2:"+password2
92 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
94 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
96 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
97 resp
= conn
.getresponse()
98 assert_equal(resp
.status
, 200)
101 #Wrong password for rt2
102 authpairnew
= "rt2:"+password2
+"wrong"
103 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
105 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
107 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
108 resp
= conn
.getresponse()
109 assert_equal(resp
.status
, 401)
112 ###############################################################
113 # Check correctness of the rpcuser/rpcpassword config options #
114 ###############################################################
115 url
= urllib
.parse
.urlparse(self
.nodes
[1].url
)
117 # rpcuser and rpcpassword authpair
118 rpcuserauthpair
= "rpcuser💻:rpcpassword🔑"
120 headers
= {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair
)}
122 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
124 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
125 resp
= conn
.getresponse()
126 assert_equal(resp
.status
, 200)
129 #Wrong login name with rpcuser's password
130 rpcuserauthpair
= "rpcuserwrong:rpcpassword"
131 headers
= {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair
)}
133 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
135 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
136 resp
= conn
.getresponse()
137 assert_equal(resp
.status
, 401)
140 #Wrong password for rpcuser
141 rpcuserauthpair
= "rpcuser:rpcpasswordwrong"
142 headers
= {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair
)}
144 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
146 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
147 resp
= conn
.getresponse()
148 assert_equal(resp
.status
, 401)
152 if __name__
== '__main__':
153 HTTPBasicsTest ().main ()