20
20
import configparser
21
21
import sys
22
22
23
+ def call_with_auth (node , user , password ):
24
+ url = urllib .parse .urlparse (node .url )
25
+ headers = {"Authorization" : "Basic " + str_to_b64str ('{}:{}' .format (user , password ))}
26
+
27
+ conn = http .client .HTTPConnection (url .hostname , url .port )
28
+ conn .connect ()
29
+ conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
30
+ resp = conn .getresponse ()
31
+ conn .close ()
32
+ return resp
33
+
23
34
24
35
class HTTPBasicsTest (BitcoinTestFramework ):
25
36
def set_test_params (self ):
@@ -57,108 +68,39 @@ def run_test(self):
57
68
##################################################
58
69
url = urllib .parse .urlparse (self .nodes [0 ].url )
59
70
60
- #Old authpair
61
- authpair = url .username + ':' + url .password
62
-
63
- #New authpair generated via share/rpcauth tool
64
71
password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
65
-
66
- #Second authpair with different username
67
72
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
68
- authpairnew = "rt:" + password
69
73
70
74
self .log .info ('Correct...' )
71
- headers = {"Authorization" : "Basic " + str_to_b64str (authpair )}
72
-
73
- conn = http .client .HTTPConnection (url .hostname , url .port )
74
- conn .connect ()
75
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
76
- resp = conn .getresponse ()
77
- assert_equal (resp .status , 200 )
78
- conn .close ()
75
+ assert_equal (200 , call_with_auth (self .nodes [0 ], url .username , url .password ).status )
79
76
80
77
#Use new authpair to confirm both work
81
78
self .log .info ('Correct...' )
82
- headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
83
-
84
- conn = http .client .HTTPConnection (url .hostname , url .port )
85
- conn .connect ()
86
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
87
- resp = conn .getresponse ()
88
- assert_equal (resp .status , 200 )
89
- conn .close ()
79
+ assert_equal (200 , call_with_auth (self .nodes [0 ], 'rt' , password ).status )
90
80
91
81
#Wrong login name with rt's password
92
82
self .log .info ('Wrong...' )
93
- authpairnew = "rtwrong:" + password
94
- headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
95
-
96
- conn = http .client .HTTPConnection (url .hostname , url .port )
97
- conn .connect ()
98
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
99
- resp = conn .getresponse ()
100
- assert_equal (resp .status , 401 )
101
- conn .close ()
83
+ assert_equal (401 , call_with_auth (self .nodes [0 ], 'rtwrong' , password ).status )
102
84
103
85
#Wrong password for rt
104
86
self .log .info ('Wrong...' )
105
- authpairnew = "rt:" + password + "wrong"
106
- headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
107
-
108
- conn = http .client .HTTPConnection (url .hostname , url .port )
109
- conn .connect ()
110
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
111
- resp = conn .getresponse ()
112
- assert_equal (resp .status , 401 )
113
- conn .close ()
87
+ assert_equal (401 , call_with_auth (self .nodes [0 ], 'rt' , password + 'wrong' ).status )
114
88
115
89
#Correct for rt2
116
90
self .log .info ('Correct...' )
117
- authpairnew = "rt2:" + password2
118
- headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
119
-
120
- conn = http .client .HTTPConnection (url .hostname , url .port )
121
- conn .connect ()
122
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
123
- resp = conn .getresponse ()
124
- assert_equal (resp .status , 200 )
125
- conn .close ()
91
+ assert_equal (200 , call_with_auth (self .nodes [0 ], 'rt2' , password2 ).status )
126
92
127
93
#Wrong password for rt2
128
94
self .log .info ('Wrong...' )
129
- authpairnew = "rt2:" + password2 + "wrong"
130
- headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
131
-
132
- conn = http .client .HTTPConnection (url .hostname , url .port )
133
- conn .connect ()
134
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
135
- resp = conn .getresponse ()
136
- assert_equal (resp .status , 401 )
137
- conn .close ()
95
+ assert_equal (401 , call_with_auth (self .nodes [0 ], 'rt2' , password2 + 'wrong' ).status )
138
96
139
97
#Correct for randomly generated user
140
98
self .log .info ('Correct...' )
141
- authpairnew = self .user + ":" + self .password
142
- headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
143
-
144
- conn = http .client .HTTPConnection (url .hostname , url .port )
145
- conn .connect ()
146
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
147
- resp = conn .getresponse ()
148
- assert_equal (resp .status , 200 )
149
- conn .close ()
99
+ assert_equal (200 , call_with_auth (self .nodes [0 ], self .user , self .password ).status )
150
100
151
101
#Wrong password for randomly generated user
152
102
self .log .info ('Wrong...' )
153
- authpairnew = self .user + ":" + self .password + "Wrong"
154
- headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
155
-
156
- conn = http .client .HTTPConnection (url .hostname , url .port )
157
- conn .connect ()
158
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
159
- resp = conn .getresponse ()
160
- assert_equal (resp .status , 401 )
161
- conn .close ()
103
+ assert_equal (401 , call_with_auth (self .nodes [0 ], self .user , self .password + 'Wrong' ).status )
162
104
163
105
###############################################################
164
106
# Check correctness of the rpcuser/rpcpassword config options #
@@ -167,40 +109,15 @@ def run_test(self):
167
109
168
110
# rpcuser and rpcpassword authpair
169
111
self .log .info ('Correct...' )
170
- rpcuserauthpair = "rpcuser💻:rpcpassword🔑"
171
-
172
- headers = {"Authorization" : "Basic " + str_to_b64str (rpcuserauthpair )}
173
-
174
- conn = http .client .HTTPConnection (url .hostname , url .port )
175
- conn .connect ()
176
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
177
- resp = conn .getresponse ()
178
- assert_equal (resp .status , 200 )
179
- conn .close ()
112
+ assert_equal (200 , call_with_auth (self .nodes [1 ], "rpcuser💻" , "rpcpassword🔑" ).status )
180
113
181
114
#Wrong login name with rpcuser's password
182
- rpcuserauthpair = "rpcuserwrong:rpcpassword"
183
- headers = {"Authorization" : "Basic " + str_to_b64str (rpcuserauthpair )}
184
-
185
- conn = http .client .HTTPConnection (url .hostname , url .port )
186
- conn .connect ()
187
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
188
- resp = conn .getresponse ()
189
- assert_equal (resp .status , 401 )
190
- conn .close ()
115
+ self .log .info ('Wrong...' )
116
+ assert_equal (401 , call_with_auth (self .nodes [1 ], 'rpcuserwrong' , 'rpcpassword' ).status )
191
117
192
118
#Wrong password for rpcuser
193
119
self .log .info ('Wrong...' )
194
- rpcuserauthpair = "rpcuser:rpcpasswordwrong"
195
- headers = {"Authorization" : "Basic " + str_to_b64str (rpcuserauthpair )}
196
-
197
- conn = http .client .HTTPConnection (url .hostname , url .port )
198
- conn .connect ()
199
- conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
200
- resp = conn .getresponse ()
201
- assert_equal (resp .status , 401 )
202
- conn .close ()
203
-
120
+ assert_equal (401 , call_with_auth (self .nodes [1 ], 'rpcuser' , 'rpcpasswordwrong' ).status )
204
121
205
122
if __name__ == '__main__' :
206
123
HTTPBasicsTest ().main ()
0 commit comments