Skip to content

Commit

Permalink
Add unlock locked test
Browse files Browse the repository at this point in the history
  • Loading branch information
roshii committed Aug 2, 2023
1 parent 4f18141 commit e5cdafd
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions jmclient/test/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
from jmbase import get_nontor_agent, hextobin, BytesProducer, get_log
from jmbase.support import get_free_tcp_ports
from jmbitcoin import CTransaction
from jmclient import (load_test_config, jm_single, SegwitWalletFidelityBonds,
JMWalletDaemon, validate_address, start_reactor,
SegwitWallet)
from jmclient import (
load_test_config,
jm_single,
SegwitWalletFidelityBonds,
JMWalletDaemon,
validate_address,
start_reactor,
SegwitWallet,
storage,
)
from jmclient.wallet_rpc import api_version_string, CJ_MAKER_RUNNING, CJ_NOT_RUNNING
from commontest import make_wallets
from test_coinjoin import make_wallets_to_list, sync_wallets
Expand Down Expand Up @@ -105,6 +112,11 @@ def clean_out_wallet_files(self):
if os.path.exists(wfn):
os.remove(wfn)

parent, name = os.path.split(wfn)
lockfile = os.path.join(parent, f".{name}.lock")
if os.path.exists(lockfile):
os.remove(lockfile)

def get_wallet_file_name(self, i, fullpath=False):
tfn = testfilename + str(i) + ".jmdat"
if fullpath:
Expand Down Expand Up @@ -413,6 +425,38 @@ def test_create_list_lock_unlock(self):
yield self.do_request(agent, b"POST", addr, body,
self.process_unlock_response)

@defer.inlineCallbacks
def test_unlock_locked(self):
"""Assert if unlocking a wallet locked by another process fails."""
self.clean_out_wallet_files()
self.daemon.services["wallet"] = None
self.daemon.stopService()
self.daemon.auth_disabled = False

wfn = self.get_wallet_file_name(1)
self.wfnames = [wfn]

agent = get_nontor_agent()
root = self.get_route_root()

# Create first
p = self.get_wallet_file_name(1, True)
pw = "None"

s = storage.Storage(p, bytes(pw, "utf-8"), create=True)
assert s.is_locked()

# Unlocking a locked wallet should fail

addr = root + "/wallet/" + wfn + "/unlock"
addr = addr.encode()
body = BytesProducer(json.dumps({"password": pw}).encode())
yield self.do_request(
agent, b"POST", addr, body, self.process_failed_unlock_response
)

s.close()

def process_create_wallet_response(self, response, code):
assert code == 201
json_body = json.loads(response.decode("utf-8"))
Expand Down Expand Up @@ -610,6 +654,9 @@ def process_unlock_response(self, response, code):
assert json_body["walletname"] in self.wfnames
self.jwt_token = json_body["token"]

def process_failed_unlock_response(self, response, code):
assert code == 409

def process_lock_response(self, response, code):
assert code == 200
json_body = json.loads(response.decode("utf-8"))
Expand Down

0 comments on commit e5cdafd

Please sign in to comment.