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 with
open(os
.path
.join(self
.options
.tmpdir
+"/node0", "bitcoin.conf"), 'a', encoding
='utf8') as f
:
28 f
.write(rpcauth2
+"\n")
30 def setup_network(self
):
31 self
.nodes
= self
.setup_nodes()
35 ##################################################
36 # Check correctness of the rpcauth config option #
37 ##################################################
38 url
= urllib
.parse
.urlparse(self
.nodes
[0].url
)
41 authpair
= url
.username
+ ':' + url
.password
43 #New authpair generated via share/rpcuser tool
44 password
= "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
46 #Second authpair with different username
47 password2
= "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
48 authpairnew
= "rt:"+password
50 headers
= {"Authorization": "Basic " + str_to_b64str(authpair
)}
52 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
54 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
55 resp
= conn
.getresponse()
56 assert_equal(resp
.status
==401, False)
59 #Use new authpair to confirm both work
60 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
62 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
64 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
65 resp
= conn
.getresponse()
66 assert_equal(resp
.status
==401, False)
69 #Wrong login name with rt's password
70 authpairnew
= "rtwrong:"+password
71 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
73 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
75 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
76 resp
= conn
.getresponse()
77 assert_equal(resp
.status
==401, True)
80 #Wrong password for rt
81 authpairnew
= "rt:"+password
+"wrong"
82 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
84 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
86 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
87 resp
= conn
.getresponse()
88 assert_equal(resp
.status
==401, True)
92 authpairnew
= "rt2:"+password2
93 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
95 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
97 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
98 resp
= conn
.getresponse()
99 assert_equal(resp
.status
==401, False)
102 #Wrong password for rt2
103 authpairnew
= "rt2:"+password2
+"wrong"
104 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
106 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
108 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
109 resp
= conn
.getresponse()
110 assert_equal(resp
.status
==401, True)
114 if __name__
== '__main__':
115 HTTPBasicsTest ().main ()