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
17 class HTTPBasicsTest (BitcoinTestFramework
):
21 self
.setup_clean_chain
= False
24 def setup_chain(self
):
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
:
31 f
.write(rpcauth2
+"\n")
33 def setup_network(self
):
34 self
.nodes
= self
.setup_nodes()
38 ##################################################
39 # Check correctness of the rpcauth config option #
40 ##################################################
41 url
= urllib
.parse
.urlparse(self
.nodes
[0].url
)
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
)
59 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
60 resp
= conn
.getresponse()
61 assert_equal(resp
.status
==401, False)
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
)
69 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
70 resp
= conn
.getresponse()
71 assert_equal(resp
.status
==401, False)
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
)
80 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
81 resp
= conn
.getresponse()
82 assert_equal(resp
.status
==401, True)
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
)
91 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
92 resp
= conn
.getresponse()
93 assert_equal(resp
.status
==401, True)
97 authpairnew
= "rt2:"+password2
98 headers
= {"Authorization": "Basic " + str_to_b64str(authpairnew
)}
100 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
102 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
103 resp
= conn
.getresponse()
104 assert_equal(resp
.status
==401, False)
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
)
113 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
114 resp
= conn
.getresponse()
115 assert_equal(resp
.status
==401, True)
119 if __name__
== '__main__':
120 HTTPBasicsTest ().main ()