Skip to content

Commit 3ec5ad8

Browse files
committed
Add test for rpcuser/rpcpassword
1 parent c53c983 commit 3ec5ad8

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

test/functional/multi_rpc.py

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@ class HTTPBasicsTest (BitcoinTestFramework):
1616
def __init__(self):
1717
super().__init__()
1818
self.setup_clean_chain = False
19-
self.num_nodes = 1
19+
self.num_nodes = 2
2020

2121
def setup_chain(self):
2222
super().setup_chain()
2323
#Append rpcauth to bitcoin.conf before initialization
2424
rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
2525
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
26+
rpcuser = "rpcuser=rpcuser💻"
27+
rpcpassword = "rpcpassword=rpcpassword🔑"
2628
with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a', encoding='utf8') as f:
2729
f.write(rpcauth+"\n")
2830
f.write(rpcauth2+"\n")
31+
with open(os.path.join(self.options.tmpdir+"/node1", "bitcoin.conf"), 'a', encoding='utf8') as f:
32+
f.write(rpcuser+"\n")
33+
f.write(rpcpassword+"\n")
2934

3035
def run_test(self):
3136

@@ -50,7 +55,7 @@ def run_test(self):
5055
conn.connect()
5156
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
5257
resp = conn.getresponse()
53-
assert_equal(resp.status==401, False)
58+
assert_equal(resp.status, 200)
5459
conn.close()
5560

5661
#Use new authpair to confirm both work
@@ -60,7 +65,7 @@ def run_test(self):
6065
conn.connect()
6166
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
6267
resp = conn.getresponse()
63-
assert_equal(resp.status==401, False)
68+
assert_equal(resp.status, 200)
6469
conn.close()
6570

6671
#Wrong login name with rt's password
@@ -71,7 +76,7 @@ def run_test(self):
7176
conn.connect()
7277
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
7378
resp = conn.getresponse()
74-
assert_equal(resp.status==401, True)
79+
assert_equal(resp.status, 401)
7580
conn.close()
7681

7782
#Wrong password for rt
@@ -82,7 +87,7 @@ def run_test(self):
8287
conn.connect()
8388
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
8489
resp = conn.getresponse()
85-
assert_equal(resp.status==401, True)
90+
assert_equal(resp.status, 401)
8691
conn.close()
8792

8893
#Correct for rt2
@@ -93,7 +98,7 @@ def run_test(self):
9398
conn.connect()
9499
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
95100
resp = conn.getresponse()
96-
assert_equal(resp.status==401, False)
101+
assert_equal(resp.status, 200)
97102
conn.close()
98103

99104
#Wrong password for rt2
@@ -104,7 +109,46 @@ def run_test(self):
104109
conn.connect()
105110
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
106111
resp = conn.getresponse()
107-
assert_equal(resp.status==401, True)
112+
assert_equal(resp.status, 401)
113+
conn.close()
114+
115+
###############################################################
116+
# Check correctness of the rpcuser/rpcpassword config options #
117+
###############################################################
118+
url = urllib.parse.urlparse(self.nodes[1].url)
119+
120+
# rpcuser and rpcpassword authpair
121+
rpcuserauthpair = "rpcuser💻:rpcpassword🔑"
122+
123+
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
124+
125+
conn = http.client.HTTPConnection(url.hostname, url.port)
126+
conn.connect()
127+
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
128+
resp = conn.getresponse()
129+
assert_equal(resp.status, 200)
130+
conn.close()
131+
132+
#Wrong login name with rpcuser's password
133+
rpcuserauthpair = "rpcuserwrong:rpcpassword"
134+
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
135+
136+
conn = http.client.HTTPConnection(url.hostname, url.port)
137+
conn.connect()
138+
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
139+
resp = conn.getresponse()
140+
assert_equal(resp.status, 401)
141+
conn.close()
142+
143+
#Wrong password for rpcuser
144+
rpcuserauthpair = "rpcuser:rpcpasswordwrong"
145+
headers = {"Authorization": "Basic " + str_to_b64str(rpcuserauthpair)}
146+
147+
conn = http.client.HTTPConnection(url.hostname, url.port)
148+
conn.connect()
149+
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
150+
resp = conn.getresponse()
151+
assert_equal(resp.status, 401)
108152
conn.close()
109153

110154

test/functional/pruning.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def wallet_test(self):
315315
# check that the pruning node's wallet is still in good shape
316316
self.log.info("Stop and start pruning node to trigger wallet rescan")
317317
self.stop_node(2)
318-
self.start_node(2, self.options.tmpdir, ["-prune=550"])
318+
self.nodes[2] = self.start_node(2, self.options.tmpdir, ["-prune=550"])
319319
self.log.info("Success")
320320

321321
# check that wallet loads loads successfully when restarting a pruned node after IBD.
@@ -325,7 +325,7 @@ def wallet_test(self):
325325
nds = [self.nodes[0], self.nodes[5]]
326326
sync_blocks(nds, wait=5, timeout=300)
327327
self.stop_node(5) #stop and start to trigger rescan
328-
self.start_node(5, self.options.tmpdir, ["-prune=550"])
328+
self.nodes[5] = self.start_node(5, self.options.tmpdir, ["-prune=550"])
329329
self.log.info("Success")
330330

331331
def run_test(self):

test/functional/rpcbind_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def run_allowip_test(self, allow_ips, rpchost, rpcport):
4949
base_args = ['-disablewallet', '-nolisten'] + ['-rpcallowip='+x for x in allow_ips]
5050
self.nodes = self.start_nodes(self.num_nodes, self.options.tmpdir, [base_args])
5151
# connect to node through non-loopback interface
52-
node = get_rpc_proxy(rpc_url(0, "%s:%d" % (rpchost, rpcport)), 0)
52+
node = get_rpc_proxy(rpc_url(get_datadir_path(self.options.tmpdir, 0), 0, "%s:%d" % (rpchost, rpcport)), 0)
5353
node.getnetworkinfo()
5454
self.stop_nodes()
5555

test/functional/test_framework/util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ def initialize_datadir(dirname, n):
187187
f.write("rpcport="+str(rpc_port(n))+"\n")
188188
f.write("listenonion=0\n")
189189
return datadir
190-
190+
191191
def get_datadir_path(dirname, n):
192192
return os.path.join(dirname, "node"+str(n))
193-
193+
194194
def get_auth_cookie(datadir, n):
195195
if os.path.isfile(os.path.join(datadir, "regtest", ".cookie")):
196196
with open(os.path.join(datadir, "regtest", ".cookie"), 'r') as f:
@@ -224,7 +224,7 @@ def rpc_url(datadir, i, rpchost=None):
224224
host = rpchost
225225
return "http://%s:%s@%s:%d" % (rpc_u, rpc_p, host, int(port))
226226

227-
def wait_for_bitcoind_start(process, datadir, i):
227+
def wait_for_bitcoind_start(process, datadir, i, rpchost=None):
228228
'''
229229
Wait for bitcoind to start. This means that RPC is accessible and fully initialized.
230230
Raise an exception if bitcoind exits during initialization.
@@ -234,7 +234,7 @@ def wait_for_bitcoind_start(process, datadir, i):
234234
raise Exception('bitcoind exited with status %i during initialization' % process.returncode)
235235
try:
236236
# Check if .cookie file to be created
237-
rpc = get_rpc_proxy(rpc_url(datadir, i), i)
237+
rpc = get_rpc_proxy(rpc_url(datadir, i, rpchost), i)
238238
blocks = rpc.getblockcount()
239239
break # break out of loop on success
240240
except IOError as e:
@@ -261,7 +261,7 @@ def _start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary
261261
if extra_args is not None: args.extend(extra_args)
262262
bitcoind_processes[i] = subprocess.Popen(args, stderr=stderr)
263263
logger.debug("initialize_chain: bitcoind started, waiting for RPC to come up")
264-
wait_for_bitcoind_start(bitcoind_processes[i], datadir, i)
264+
wait_for_bitcoind_start(bitcoind_processes[i], datadir, i, rpchost)
265265
logger.debug("initialize_chain: RPC successfully started")
266266
proxy = get_rpc_proxy(rpc_url(datadir, i, rpchost), i, timeout=timewait)
267267

0 commit comments

Comments
 (0)