diff --git a/test/functional/rpc_users.py b/test/functional/rpc_users.py index 66cdd7cf9a96d4..16c115abddb565 100755 --- a/test/functional/rpc_users.py +++ b/test/functional/rpc_users.py @@ -4,13 +4,14 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test multiple RPC users.""" -from test_framework.test_framework import BitcoinTestFramework +from test_framework.test_framework import BitcoinTestFramework, SkipTest from test_framework.util import ( assert_equal, str_to_b64str, ) import http.client +import os import urllib.parse import subprocess from random import SystemRandom @@ -84,6 +85,36 @@ def test_auth(self, node, user, password): self.log.info('Wrong...') assert_equal(401, call_with_auth(node, user + 'wrong', password + 'wrong').status) + def test_rpccookieperms(self): + + def test_perm(perm: str): + self.restart_node(1, extra_args=[f"-rpccookieperms={perm}"]) + + # Pad perm with `0` to 4 chars for str cmp + # as we always include special bits in test mask + perm = perm.zfill(4) + + cookie_file_path = self.nodes[1].chain_path / '.cookie' + file_stat = os.stat(cookie_file_path) + actual_perms = f"{(file_stat.st_mode & 0o7777):04o}" + assert_equal(perm, actual_perms) + + if os.name == 'nt': + raise SkipTest("Can't test file perms using os.stat on Windows") + + self.log.info('Check that cookie file permissions can be set using -rpccookieperms') + + # Remove any leftover rpc{user|password} config options from previous tests + conf = self.nodes[1].bitcoinconf + with conf.open('r') as file: + lines = file.readlines() + filtered_lines = [line for line in lines if not line.startswith('rpcuser') and not line.startswith('rpcpassword')] + with conf.open('w') as file: + file.writelines(filtered_lines) + + test_perms = ["0640", "666", "1660"] + [test_perm(perm) for perm in test_perms] + def run_test(self): self.conf_setup() self.log.info('Check correctness of the rpcauth config option') @@ -115,6 +146,9 @@ def run_test(self): (self.nodes[0].chain_path / ".cookie.tmp").mkdir() self.nodes[0].assert_start_raises_init_error(expected_msg=init_error) + self.test_rpccookieperms() + + if __name__ == '__main__': HTTPBasicsTest().main()