Skip to content

Commit 018c7e5

Browse files
committed
Merge #13024: test: Add rpcauth pair that generated by rpcauth.py
8b8032e test: Add rpcauth pair that generated by rpcauth (Chun Kuan Lee) Pull request description: This PR adds a rpcauth pair that is randomly generated. Also checks that rpcauth.py works fine. Resolve #12995 Tree-SHA512: d9661f40e306bcf528dc25919c874ebcdbdd21101319985dc12ce133c80fd0021cfee5e4bfe8ee7970eccc2e24c97e596263b270fe0b79f3613ae573a825ed63
2 parents 54865cf + 8b8032e commit 018c7e5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

test/config.ini.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
SRCDIR=@abs_top_srcdir@
1010
BUILDDIR=@abs_top_builddir@
1111
EXEEXT=@EXEEXT@
12+
RPCAUTH=@abs_top_srcdir@/share/rpcauth/rpcauth.py
1213

1314
[components]
1415
# Which components are enabled. These are commented out by `configure` if they were disabled when running config.

test/functional/rpc_users.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import os
1515
import http.client
1616
import urllib.parse
17+
import subprocess
18+
from random import SystemRandom
19+
import string
20+
import configparser
1721

1822

1923
class HTTPBasicsTest(BitcoinTestFramework):
@@ -27,9 +31,20 @@ def setup_chain(self):
2731
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
2832
rpcuser = "rpcuser=rpcuser💻"
2933
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+
3044
with open(os.path.join(get_datadir_path(self.options.tmpdir, 0), "bitcoin.conf"), 'a', encoding='utf8') as f:
3145
f.write(rpcauth+"\n")
3246
f.write(rpcauth2+"\n")
47+
f.write(rpcauth3+"\n")
3348
with open(os.path.join(get_datadir_path(self.options.tmpdir, 1), "bitcoin.conf"), 'a', encoding='utf8') as f:
3449
f.write(rpcuser+"\n")
3550
f.write(rpcpassword+"\n")
@@ -51,6 +66,7 @@ def run_test(self):
5166
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
5267
authpairnew = "rt:"+password
5368

69+
self.log.info('Correct...')
5470
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
5571

5672
conn = http.client.HTTPConnection(url.hostname, url.port)
@@ -61,6 +77,7 @@ def run_test(self):
6177
conn.close()
6278

6379
#Use new authpair to confirm both work
80+
self.log.info('Correct...')
6481
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
6582

6683
conn = http.client.HTTPConnection(url.hostname, url.port)
@@ -71,6 +88,7 @@ def run_test(self):
7188
conn.close()
7289

7390
#Wrong login name with rt's password
91+
self.log.info('Wrong...')
7492
authpairnew = "rtwrong:"+password
7593
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
7694

@@ -82,6 +100,7 @@ def run_test(self):
82100
conn.close()
83101

84102
#Wrong password for rt
103+
self.log.info('Wrong...')
85104
authpairnew = "rt:"+password+"wrong"
86105
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
87106

@@ -93,6 +112,7 @@ def run_test(self):
93112
conn.close()
94113

95114
#Correct for rt2
115+
self.log.info('Correct...')
96116
authpairnew = "rt2:"+password2
97117
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
98118

@@ -104,6 +124,7 @@ def run_test(self):
104124
conn.close()
105125

106126
#Wrong password for rt2
127+
self.log.info('Wrong...')
107128
authpairnew = "rt2:"+password2+"wrong"
108129
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
109130

@@ -114,12 +135,37 @@ def run_test(self):
114135
assert_equal(resp.status, 401)
115136
conn.close()
116137

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+
117162
###############################################################
118163
# Check correctness of the rpcuser/rpcpassword config options #
119164
###############################################################
120165
url = urllib.parse.urlparse(self.nodes[1].url)
121166

122167
# rpcuser and rpcpassword authpair
168+
self.log.info('Correct...')
123169
rpcuserauthpair = "rpcuser💻:rpcpassword🔑"
124170

125171
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
@@ -143,6 +189,7 @@ def run_test(self):
143189
conn.close()
144190

145191
#Wrong password for rpcuser
192+
self.log.info('Wrong...')
146193
rpcuserauthpair = "rpcuser:rpcpasswordwrong"
147194
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
148195

0 commit comments

Comments
 (0)