14
14
import os
15
15
import http .client
16
16
import urllib .parse
17
+ import subprocess
18
+ from random import SystemRandom
19
+ import string
20
+ import configparser
17
21
18
22
19
23
class HTTPBasicsTest (BitcoinTestFramework ):
@@ -27,9 +31,20 @@ def setup_chain(self):
27
31
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
28
32
rpcuser = "rpcuser=rpcuser💻"
29
33
rpcpassword = "rpcpassword=rpcpassword🔑"
34
+
35
+ self .user = '' .join (SystemRandom ().choice (string .ascii_letters + string .digits ) for _ in range (10 ))
36
+ config = configparser .ConfigParser ()
37
+ config .read_file (open (self .options .configfile ))
38
+ gen_rpcauth = config ['environment' ]['RPCAUTH' ]
39
+ p = subprocess .Popen ([gen_rpcauth , self .user ], stdout = subprocess .PIPE , universal_newlines = True )
40
+ lines = p .stdout .read ().splitlines ()
41
+ rpcauth3 = lines [1 ]
42
+ self .password = lines [3 ]
43
+
30
44
with open (os .path .join (get_datadir_path (self .options .tmpdir , 0 ), "bitcoin.conf" ), 'a' , encoding = 'utf8' ) as f :
31
45
f .write (rpcauth + "\n " )
32
46
f .write (rpcauth2 + "\n " )
47
+ f .write (rpcauth3 + "\n " )
33
48
with open (os .path .join (get_datadir_path (self .options .tmpdir , 1 ), "bitcoin.conf" ), 'a' , encoding = 'utf8' ) as f :
34
49
f .write (rpcuser + "\n " )
35
50
f .write (rpcpassword + "\n " )
@@ -51,6 +66,7 @@ def run_test(self):
51
66
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
52
67
authpairnew = "rt:" + password
53
68
69
+ self .log .info ('Correct...' )
54
70
headers = {"Authorization" : "Basic " + str_to_b64str (authpair )}
55
71
56
72
conn = http .client .HTTPConnection (url .hostname , url .port )
@@ -61,6 +77,7 @@ def run_test(self):
61
77
conn .close ()
62
78
63
79
#Use new authpair to confirm both work
80
+ self .log .info ('Correct...' )
64
81
headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
65
82
66
83
conn = http .client .HTTPConnection (url .hostname , url .port )
@@ -71,6 +88,7 @@ def run_test(self):
71
88
conn .close ()
72
89
73
90
#Wrong login name with rt's password
91
+ self .log .info ('Wrong...' )
74
92
authpairnew = "rtwrong:" + password
75
93
headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
76
94
@@ -82,6 +100,7 @@ def run_test(self):
82
100
conn .close ()
83
101
84
102
#Wrong password for rt
103
+ self .log .info ('Wrong...' )
85
104
authpairnew = "rt:" + password + "wrong"
86
105
headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
87
106
@@ -93,6 +112,7 @@ def run_test(self):
93
112
conn .close ()
94
113
95
114
#Correct for rt2
115
+ self .log .info ('Correct...' )
96
116
authpairnew = "rt2:" + password2
97
117
headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
98
118
@@ -104,6 +124,7 @@ def run_test(self):
104
124
conn .close ()
105
125
106
126
#Wrong password for rt2
127
+ self .log .info ('Wrong...' )
107
128
authpairnew = "rt2:" + password2 + "wrong"
108
129
headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
109
130
@@ -114,12 +135,37 @@ def run_test(self):
114
135
assert_equal (resp .status , 401 )
115
136
conn .close ()
116
137
138
+ #Correct for randomly generated user
139
+ self .log .info ('Correct...' )
140
+ authpairnew = self .user + ":" + self .password
141
+ headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
142
+
143
+ conn = http .client .HTTPConnection (url .hostname , url .port )
144
+ conn .connect ()
145
+ conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
146
+ resp = conn .getresponse ()
147
+ assert_equal (resp .status , 200 )
148
+ conn .close ()
149
+
150
+ #Wrong password for randomly generated user
151
+ self .log .info ('Wrong...' )
152
+ authpairnew = self .user + ":" + self .password + "Wrong"
153
+ headers = {"Authorization" : "Basic " + str_to_b64str (authpairnew )}
154
+
155
+ conn = http .client .HTTPConnection (url .hostname , url .port )
156
+ conn .connect ()
157
+ conn .request ('POST' , '/' , '{"method": "getbestblockhash"}' , headers )
158
+ resp = conn .getresponse ()
159
+ assert_equal (resp .status , 401 )
160
+ conn .close ()
161
+
117
162
###############################################################
118
163
# Check correctness of the rpcuser/rpcpassword config options #
119
164
###############################################################
120
165
url = urllib .parse .urlparse (self .nodes [1 ].url )
121
166
122
167
# rpcuser and rpcpassword authpair
168
+ self .log .info ('Correct...' )
123
169
rpcuserauthpair = "rpcuser💻:rpcpassword🔑"
124
170
125
171
headers = {"Authorization" : "Basic " + str_to_b64str (rpcuserauthpair )}
@@ -143,6 +189,7 @@ def run_test(self):
143
189
conn .close ()
144
190
145
191
#Wrong password for rpcuser
192
+ self .log .info ('Wrong...' )
146
193
rpcuserauthpair = "rpcuser:rpcpasswordwrong"
147
194
headers = {"Authorization" : "Basic " + str_to_b64str (rpcuserauthpair )}
148
195
0 commit comments