Turn rpcauth.py test into functional test

This commit is contained in:
MarcoFalke
2025-07-05 17:42:13 +02:00
parent e3f416dbf7
commit fa4d68cf97
7 changed files with 30 additions and 34 deletions

View File

@@ -353,6 +353,7 @@ BASE_SCRIPTS = [
'rpc_getdescriptorinfo.py',
'rpc_mempool_info.py',
'rpc_help.py',
'tool_rpcauth.py',
'p2p_handshake.py',
'p2p_handshake.py --v2transport',
'feature_dirsymlinks.py',

57
test/functional/tool_rpcauth.py Executable file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# Copyright (c) 2015-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test share/rpcauth/rpcauth.py
"""
import hmac
import importlib
import os
import re
import sys
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
class RpcAuthTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 0 # No node/datadir needed
def setup_network(self):
pass
def setUp(self):
sys.path.insert(0, os.path.dirname(self.config["environment"]["RPCAUTH"]))
self.rpcauth = importlib.import_module('rpcauth')
def run_test(self):
self.setUp()
self.test_generate_salt()
self.test_generate_password()
self.test_check_password_hmac()
def test_generate_salt(self):
for i in range(16, 32 + 1):
assert_equal(len(self.rpcauth.generate_salt(i)), i * 2)
def test_generate_password(self):
"""Test that generated passwords only consist of urlsafe characters."""
r = re.compile(r"[0-9a-zA-Z_-]*")
password = self.rpcauth.generate_password()
assert r.fullmatch(password)
def test_check_password_hmac(self):
salt = self.rpcauth.generate_salt(16)
password = self.rpcauth.generate_password()
password_hmac = self.rpcauth.password_to_hmac(salt, password)
m = hmac.new(salt.encode('utf-8'), password.encode('utf-8'), 'SHA256')
expected_password_hmac = m.hexdigest()
assert_equal(expected_password_hmac, password_hmac)
if __name__ == '__main__':
RpcAuthTest(__file__).main()