Skip to content

Commit c53c983

Browse files
committed
Replace cookie auth in tests
Since rpcuser and rpcpassword are now deprecated, replace them with cookie auth. Fix test failures with cookie auth
1 parent 9fec4da commit c53c983

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

test/functional/p2p-segwit.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ def test_p2sh_witness(self, segwit_activated):
14861486
# nodes would have stored, this requires special handling.
14871487
# To enable this test, pass --oldbinary=<path-to-pre-segwit-bitcoind> to
14881488
# the test.
1489-
def test_upgrade_after_activation(self, node, node_id):
1489+
def test_upgrade_after_activation(self, node_id):
14901490
self.log.info("Testing software upgrade after softfork activation")
14911491

14921492
assert(node_id != 0) # node0 is assumed to be a segwit-active bitcoind
@@ -1502,14 +1502,14 @@ def test_upgrade_after_activation(self, node, node_id):
15021502
sync_blocks(self.nodes)
15031503

15041504
# Make sure that this peer thinks segwit has activated.
1505-
assert(get_bip9_status(node, 'segwit')['status'] == "active")
1505+
assert(get_bip9_status(self.nodes[node_id], 'segwit')['status'] == "active")
15061506

15071507
# Make sure this peers blocks match those of node0.
1508-
height = node.getblockcount()
1508+
height = self.nodes[node_id].getblockcount()
15091509
while height >= 0:
1510-
block_hash = node.getblockhash(height)
1510+
block_hash = self.nodes[node_id].getblockhash(height)
15111511
assert_equal(block_hash, self.nodes[0].getblockhash(height))
1512-
assert_equal(self.nodes[0].getblock(block_hash), node.getblock(block_hash))
1512+
assert_equal(self.nodes[0].getblock(block_hash), self.nodes[node_id].getblock(block_hash))
15131513
height -= 1
15141514

15151515

@@ -1944,7 +1944,7 @@ def run_test(self):
19441944
self.test_signature_version_1()
19451945
self.test_non_standard_witness()
19461946
sync_blocks(self.nodes)
1947-
self.test_upgrade_after_activation(self.nodes[2], 2)
1947+
self.test_upgrade_after_activation(2)
19481948
self.test_witness_sigops()
19491949

19501950

test/functional/test_framework/test_framework.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
get_mocktime,
2929
get_rpc_proxy,
3030
initialize_datadir,
31+
get_datadir_path,
3132
log_filename,
3233
p2p_port,
3334
rpc_url,
@@ -300,13 +301,13 @@ def _initialize_chain(self, test_dir, num_nodes, cachedir):
300301
args.append("-connect=127.0.0.1:" + str(p2p_port(0)))
301302
bitcoind_processes[i] = subprocess.Popen(args)
302303
self.log.debug("initialize_chain: bitcoind started, waiting for RPC to come up")
303-
wait_for_bitcoind_start(bitcoind_processes[i], rpc_url(i), i)
304+
wait_for_bitcoind_start(bitcoind_processes[i], datadir, i)
304305
self.log.debug("initialize_chain: RPC successfully started")
305306

306307
self.nodes = []
307308
for i in range(MAX_NODES):
308309
try:
309-
self.nodes.append(get_rpc_proxy(rpc_url(i), i))
310+
self.nodes.append(get_rpc_proxy(rpc_url(get_datadir_path(cachedir, i), i), i))
310311
except:
311312
self.log.exception("Error connecting to node %d" % i)
312313
sys.exit(1)

test/functional/test_framework/util.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,39 @@ def initialize_datadir(dirname, n):
181181
datadir = os.path.join(dirname, "node"+str(n))
182182
if not os.path.isdir(datadir):
183183
os.makedirs(datadir)
184-
rpc_u, rpc_p = rpc_auth_pair(n)
185184
with open(os.path.join(datadir, "bitcoin.conf"), 'w', encoding='utf8') as f:
186185
f.write("regtest=1\n")
187-
f.write("rpcuser=" + rpc_u + "\n")
188-
f.write("rpcpassword=" + rpc_p + "\n")
189186
f.write("port="+str(p2p_port(n))+"\n")
190187
f.write("rpcport="+str(rpc_port(n))+"\n")
191188
f.write("listenonion=0\n")
192189
return datadir
193-
194-
def rpc_auth_pair(n):
195-
return 'rpcuser💻' + str(n), 'rpcpass🔑' + str(n)
196-
197-
def rpc_url(i, rpchost=None):
198-
rpc_u, rpc_p = rpc_auth_pair(i)
190+
191+
def get_datadir_path(dirname, n):
192+
return os.path.join(dirname, "node"+str(n))
193+
194+
def get_auth_cookie(datadir, n):
195+
if os.path.isfile(os.path.join(datadir, "regtest", ".cookie")):
196+
with open(os.path.join(datadir, "regtest", ".cookie"), 'r') as f:
197+
userpass = f.read()
198+
split_userpass = userpass.split(':')
199+
return split_userpass[0], split_userpass[1]
200+
else:
201+
with open(os.path.join(datadir, "bitcoin.conf"), 'r') as f:
202+
user = None
203+
password = None
204+
for line in f:
205+
if line.startswith("rpcuser="):
206+
assert user is None # Ensure that there is only one rpcuser line
207+
user = line.split("=")[1].strip("\n")
208+
if line.startswith("rpcpassword="):
209+
assert password is None # Ensure that there is only one rpcpassword line
210+
password = line.split("=")[1].strip("\n")
211+
if user is None and password is None:
212+
raise ValueError("No RPC credentials")
213+
return user, password
214+
215+
def rpc_url(datadir, i, rpchost=None):
216+
rpc_u, rpc_p = get_auth_cookie(datadir, i)
199217
host = '127.0.0.1'
200218
port = rpc_port(i)
201219
if rpchost:
@@ -206,7 +224,7 @@ def rpc_url(i, rpchost=None):
206224
host = rpchost
207225
return "http://%s:%s@%s:%d" % (rpc_u, rpc_p, host, int(port))
208226

209-
def wait_for_bitcoind_start(process, url, i):
227+
def wait_for_bitcoind_start(process, datadir, i):
210228
'''
211229
Wait for bitcoind to start. This means that RPC is accessible and fully initialized.
212230
Raise an exception if bitcoind exits during initialization.
@@ -215,7 +233,8 @@ def wait_for_bitcoind_start(process, url, i):
215233
if process.poll() is not None:
216234
raise Exception('bitcoind exited with status %i during initialization' % process.returncode)
217235
try:
218-
rpc = get_rpc_proxy(url, i)
236+
# Check if .cookie file to be created
237+
rpc = get_rpc_proxy(rpc_url(datadir, i), i)
219238
blocks = rpc.getblockcount()
220239
break # break out of loop on success
221240
except IOError as e:
@@ -224,6 +243,9 @@ def wait_for_bitcoind_start(process, url, i):
224243
except JSONRPCException as e: # Initialization phase
225244
if e.error['code'] != -28: # RPC in warmup?
226245
raise # unknown JSON RPC exception
246+
except ValueError as e: # cookie file not found and no rpcuser or rpcassword. bitcoind still starting
247+
if "No RPC credentials" not in str(e):
248+
raise
227249
time.sleep(0.25)
228250

229251

@@ -239,10 +261,9 @@ def _start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary
239261
if extra_args is not None: args.extend(extra_args)
240262
bitcoind_processes[i] = subprocess.Popen(args, stderr=stderr)
241263
logger.debug("initialize_chain: bitcoind started, waiting for RPC to come up")
242-
url = rpc_url(i, rpchost)
243-
wait_for_bitcoind_start(bitcoind_processes[i], url, i)
264+
wait_for_bitcoind_start(bitcoind_processes[i], datadir, i)
244265
logger.debug("initialize_chain: RPC successfully started")
245-
proxy = get_rpc_proxy(url, i, timeout=timewait)
266+
proxy = get_rpc_proxy(rpc_url(datadir, i, rpchost), i, timeout=timewait)
246267

247268
if COVERAGE_DIR:
248269
coverage.write_all_rpc_commands(COVERAGE_DIR, proxy)

0 commit comments

Comments
 (0)