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 ):
@@ -28,15 +39,24 @@ def set_test_params(self):
28
39
def setup_chain (self ):
29
40
super ().setup_chain ()
30
41
#Append rpcauth to bitcoin.conf before initialization
42
+ self .rtpassword = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
31
43
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
32
- rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
33
- rpcuser = "rpcuser=rpcuser💻"
34
- rpcpassword = "rpcpassword=rpcpassword🔑"
35
44
36
- self .user = '' .join (SystemRandom ().choice (string .ascii_letters + string .digits ) for _ in range (10 ))
45
+ self .rpcuser = "rpcuser💻"
46
+ self .rpcpassword = "rpcpassword🔑"
47
+
37
48
config = configparser .ConfigParser ()
38
49
config .read_file (open (self .options .configfile ))
39
50
gen_rpcauth = config ['environment' ]['RPCAUTH' ]
51
+
52
+ # Generate RPCAUTH with specified password
53
+ self .rt2password = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
54
+ p = subprocess .Popen ([sys .executable , gen_rpcauth , 'rt2' , self .rt2password ], stdout = subprocess .PIPE , universal_newlines = True )
55
+ lines = p .stdout .read ().splitlines ()
56
+ rpcauth2 = lines [1 ]
57
+
58
+ # Generate RPCAUTH without specifying password
59
+ self .user = '' .join (SystemRandom ().choice (string .ascii_letters + string .digits ) for _ in range (10 ))
40
60
p = subprocess .Popen ([sys .executable , gen_rpcauth , self .user ], stdout = subprocess .PIPE , universal_newlines = True )
41
61
lines = p .stdout .read ().splitlines ()
42
62
rpcauth3 = lines [1 ]
@@ -47,160 +67,40 @@ def setup_chain(self):
47
67
f .write (rpcauth2 + "\n " )
48
68
f .write (rpcauth3 + "\n " )
49
69
with open (os .path .join (get_datadir_path (self .options .tmpdir , 1 ), "bitcoin.conf" ), 'a' , encoding = 'utf8' ) as f :
50
- f .write (rpcuser + "\n " )
51
- f .write (rpcpassword + "\n " )
52
-
53
- def run_test (self ):
54
-
55
- ##################################################
56
- # Check correctness of the rpcauth config option #
57
- ##################################################
58
- url = urllib .parse .urlparse (self .nodes [0 ].url )
59
-
60
- #Old authpair
61
- authpair = url .username + ':' + url .password
62
-
63
- #New authpair generated via share/rpcauth tool
64
- password = "cA773lm788buwYe4g4WT+05pKyNruVKjQ25x3n0DQcM="
65
-
66
- #Second authpair with different username
67
- password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
68
- authpairnew = "rt:" + password
70
+ f .write ("rpcuser={}\n " .format (self .rpcuser ))
71
+ f .write ("rpcpassword={}\n " .format (self .rpcpassword ))
69
72
73
+ def test_auth (self , node , user , password ):
70
74
self .log .info ('Correct...' )
71
- headers = { "Authorization" : "Basic " + str_to_b64str ( authpair )}
75
+ assert_equal ( 200 , call_with_auth ( node , user , password ). status )
72
76
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 ()
79
-
80
- #Use new authpair to confirm both work
81
- 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 ()
90
-
91
- #Wrong login name with rt's password
92
77
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 ()
78
+ assert_equal (401 , call_with_auth (node , user , password + 'wrong' ).status )
102
79
103
- #Wrong password for rt
104
80
self .log .info ('Wrong...' )
105
- authpairnew = "rt:" + password + "wrong"
106
- headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
81
+ assert_equal (401 , call_with_auth (node , user + 'wrong' , password ).status )
107
82
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 ()
114
-
115
- #Correct for rt2
116
- 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 ()
126
-
127
- #Wrong password for rt2
128
83
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 ()
84
+ assert_equal (401 , call_with_auth (node , user + 'wrong' , password + 'wrong' ).status )
138
85
139
- #Correct for randomly generated user
140
- 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 ()
86
+ def run_test (self ):
150
87
151
- #Wrong password for randomly generated user
152
- self . log . info ( 'Wrong...' )
153
- authpairnew = self . user + ":" + self . password + "Wrong"
154
- headers = { "Authorization" : "Basic " + str_to_b64str ( authpairnew )}
88
+ ##################################################
89
+ # Check correctness of the rpcauth config option #
90
+ ##################################################
91
+ url = urllib . parse . urlparse ( self . nodes [ 0 ]. url )
155
92
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 ()
93
+ self .test_auth (self .nodes [0 ], url .username , url .password )
94
+ self .test_auth (self .nodes [0 ], 'rt' , self .rtpassword )
95
+ self .test_auth (self .nodes [0 ], 'rt2' , self .rt2password )
96
+ self .test_auth (self .nodes [0 ], self .user , self .password )
162
97
163
98
###############################################################
164
99
# Check correctness of the rpcuser/rpcpassword config options #
165
100
###############################################################
166
101
url = urllib .parse .urlparse (self .nodes [1 ].url )
167
102
168
- # rpcuser and rpcpassword authpair
169
- 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 ()
180
-
181
- #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 ()
191
-
192
- #Wrong password for rpcuser
193
- 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
-
103
+ self .test_auth (self .nodes [1 ], self .rpcuser , self .rpcpassword )
204
104
205
105
if __name__ == '__main__' :
206
106
HTTPBasicsTest ().main ()
0 commit comments