mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-05 10:28:10 +01:00
ui: Add wallet encryption templates.
tests: Test wallet encryption.
This commit is contained in:
@@ -7,11 +7,11 @@
|
||||
|
||||
import os
|
||||
import json
|
||||
import urllib
|
||||
import signal
|
||||
import logging
|
||||
from urllib.request import urlopen
|
||||
|
||||
from .util import read_json_api
|
||||
from basicswap.rpc import callrpc
|
||||
from basicswap.contrib.rpcauth import generate_salt, password_to_hmac
|
||||
from bin.basicswap_prepare import downloadPIVXParams
|
||||
@@ -215,38 +215,6 @@ def wait_for_in_progress(delay_event, swap_client, bid_id, sent=False):
|
||||
raise ValueError('wait_for_in_progress timed out.')
|
||||
|
||||
|
||||
def post_json_req(url, json_data):
|
||||
req = urllib.request.Request(url)
|
||||
req.add_header('Content-Type', 'application/json; charset=utf-8')
|
||||
post_bytes = json.dumps(json_data).encode('utf-8')
|
||||
req.add_header('Content-Length', len(post_bytes))
|
||||
return urlopen(req, post_bytes, timeout=300).read()
|
||||
|
||||
|
||||
def read_text_api(port, path=None):
|
||||
url = f'http://127.0.0.1:{port}/json'
|
||||
if path is not None:
|
||||
url += '/' + path
|
||||
return urlopen(url, timeout=300).read().decode('utf-8')
|
||||
|
||||
|
||||
def read_json_api(port, path=None, json_data=None):
|
||||
url = f'http://127.0.0.1:{port}/json'
|
||||
if path is not None:
|
||||
url += '/' + path
|
||||
|
||||
if json_data is not None:
|
||||
return json.loads(post_json_req(url, json_data))
|
||||
return json.loads(urlopen(url, timeout=300).read())
|
||||
|
||||
|
||||
def post_json_api(port, path, json_data):
|
||||
url = f'http://127.0.0.1:{port}/json'
|
||||
if path is not None:
|
||||
url += '/' + path
|
||||
return json.loads(post_json_req(url, json_data))
|
||||
|
||||
|
||||
def wait_for_none_active(delay_event, port, wait_for=30):
|
||||
for i in range(wait_for):
|
||||
if delay_event.is_set():
|
||||
@@ -258,19 +226,6 @@ def wait_for_none_active(delay_event, port, wait_for=30):
|
||||
raise ValueError('wait_for_none_active timed out.')
|
||||
|
||||
|
||||
def waitForServer(delay_event, port, wait_for=20):
|
||||
for i in range(wait_for):
|
||||
if delay_event.is_set():
|
||||
raise ValueError('Test stopped.')
|
||||
try:
|
||||
delay_event.wait(1)
|
||||
summary = read_json_api(port)
|
||||
return
|
||||
except Exception as e:
|
||||
print('waitForServer, error:', str(e))
|
||||
raise ValueError('waitForServer failed')
|
||||
|
||||
|
||||
def waitForNumOffers(delay_event, port, offers, wait_for=20):
|
||||
for i in range(wait_for):
|
||||
if delay_event.is_set():
|
||||
@@ -321,10 +276,6 @@ def delay_for(delay_event, delay_for=60):
|
||||
delay_event.wait(delay_for)
|
||||
|
||||
|
||||
def make_boolean(s):
|
||||
return s.lower() in ['1', 'true']
|
||||
|
||||
|
||||
def make_rpc_func(node_id, base_rpc_port=BASE_RPC_PORT):
|
||||
node_id = node_id
|
||||
auth = 'test{0}:test_pass{0}'.format(node_id)
|
||||
|
||||
@@ -22,8 +22,10 @@ from basicswap.rpc_xmr import (
|
||||
callrpc_xmr_na,
|
||||
)
|
||||
from tests.basicswap.mnemonics import mnemonics
|
||||
from tests.basicswap.common import (
|
||||
from tests.basicswap.util import (
|
||||
waitForServer,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
BASE_PORT, BASE_RPC_PORT,
|
||||
BTC_BASE_PORT, BTC_BASE_RPC_PORT, BTC_BASE_TOR_PORT,
|
||||
LTC_BASE_PORT,
|
||||
|
||||
@@ -48,6 +48,9 @@ from basicswap.contrib.key import (
|
||||
from basicswap.http_server import (
|
||||
HttpThread,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
checkForks,
|
||||
stopDaemons,
|
||||
@@ -55,7 +58,6 @@ from tests.basicswap.common import (
|
||||
wait_for_bid,
|
||||
wait_for_bid_tx_state,
|
||||
wait_for_in_progress,
|
||||
read_json_api,
|
||||
TEST_HTTP_HOST,
|
||||
TEST_HTTP_PORT,
|
||||
BASE_PORT,
|
||||
|
||||
@@ -29,11 +29,13 @@ from basicswap.rpc import (
|
||||
callrpc_cli,
|
||||
waitForRPC,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
stopDaemons,
|
||||
wait_for_bid,
|
||||
make_rpc_func,
|
||||
read_json_api,
|
||||
TEST_HTTP_PORT,
|
||||
wait_for_offer,
|
||||
wait_for_in_progress,
|
||||
|
||||
@@ -39,13 +39,15 @@ from basicswap.contrib.key import (
|
||||
from basicswap.http_server import (
|
||||
HttpThread,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
prepareDataDir,
|
||||
make_rpc_func,
|
||||
checkForks,
|
||||
stopDaemons,
|
||||
delay_for,
|
||||
read_json_api,
|
||||
TEST_HTTP_HOST,
|
||||
TEST_HTTP_PORT,
|
||||
BASE_P2P_PORT,
|
||||
|
||||
@@ -47,6 +47,9 @@ from basicswap.contrib.key import (
|
||||
from basicswap.http_server import (
|
||||
HttpThread,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
checkForks,
|
||||
stopDaemons,
|
||||
@@ -54,7 +57,6 @@ from tests.basicswap.common import (
|
||||
wait_for_bid,
|
||||
wait_for_bid_tx_state,
|
||||
wait_for_in_progress,
|
||||
read_json_api,
|
||||
TEST_HTTP_HOST,
|
||||
TEST_HTTP_PORT,
|
||||
BASE_PORT,
|
||||
|
||||
@@ -47,6 +47,9 @@ from basicswap.contrib.key import (
|
||||
from basicswap.http_server import (
|
||||
HttpThread,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
checkForks,
|
||||
stopDaemons,
|
||||
@@ -54,7 +57,6 @@ from tests.basicswap.common import (
|
||||
wait_for_bid,
|
||||
wait_for_bid_tx_state,
|
||||
wait_for_in_progress,
|
||||
read_json_api,
|
||||
TEST_HTTP_HOST,
|
||||
TEST_HTTP_PORT,
|
||||
BASE_PORT,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2019 tecnovert
|
||||
# Copyright (c) 2019-2022 tecnovert
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
@@ -11,71 +11,192 @@ import json
|
||||
import shutil
|
||||
import logging
|
||||
import unittest
|
||||
import threading
|
||||
import multiprocessing
|
||||
|
||||
from io import StringIO
|
||||
from unittest.mock import patch
|
||||
|
||||
import basicswap.config as cfg
|
||||
import bin.basicswap_prepare as prepareSystem
|
||||
test_path = os.path.expanduser(os.getenv('TEST_PREPARE_PATH', '~/test_basicswap'))
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
waitForServer,
|
||||
)
|
||||
|
||||
bin_path = os.path.expanduser(os.getenv('TEST_BIN_PATH', ''))
|
||||
test_base_path = os.path.expanduser(os.getenv('TEST_PREPARE_PATH', '~/test_basicswap'))
|
||||
test_path_plain = os.path.join(test_base_path, 'plain')
|
||||
test_path_encrypted = os.path.join(test_base_path, 'encrypted')
|
||||
test_path_encrypt = os.path.join(test_base_path, 'encrypt')
|
||||
|
||||
delay_event = threading.Event()
|
||||
logger = logging.getLogger()
|
||||
logger.level = logging.DEBUG
|
||||
if not len(logger.handlers):
|
||||
logger.addHandler(logging.StreamHandler(sys.stdout))
|
||||
logger.addHandler(logging.StreamHandler(sys.stdout))
|
||||
|
||||
|
||||
def start_prepare(args, env_pairs=[]):
|
||||
for pair in env_pairs:
|
||||
os.environ[pair[0]] = pair[1]
|
||||
print(pair[0], os.environ[pair[0]])
|
||||
import bin.basicswap_prepare as prepareSystemThread
|
||||
with patch.object(sys, 'argv', args):
|
||||
prepareSystemThread.main()
|
||||
del prepareSystemThread
|
||||
|
||||
|
||||
def start_run(args, env_pairs=[]):
|
||||
for pair in env_pairs:
|
||||
os.environ[pair[0]] = pair[1]
|
||||
print(pair[0], os.environ[pair[0]])
|
||||
import bin.basicswap_run as runSystemThread
|
||||
with patch.object(sys, 'argv', args):
|
||||
runSystemThread.main()
|
||||
del runSystemThread
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
@classmethod
|
||||
def tearDownClass(self):
|
||||
try:
|
||||
shutil.rmtree(test_path)
|
||||
for test_dir in (test_path_plain, test_path_encrypted, test_path_encrypt):
|
||||
if os.path.exists(test_dir):
|
||||
shutil.rmtree(test_dir)
|
||||
except Exception as ex:
|
||||
logger.warning('tearDownClass %s', str(ex))
|
||||
super(Test, self).tearDownClass()
|
||||
|
||||
def test(self):
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path, '-withcoin=litecoin']
|
||||
with patch.object(sys, 'argv', testargs):
|
||||
prepareSystem.main()
|
||||
def test_plain(self):
|
||||
if os.path.exists(test_path_plain):
|
||||
shutil.rmtree(test_path_plain)
|
||||
if bin_path != '':
|
||||
os.makedirs(test_path_plain)
|
||||
os.symlink(bin_path, os.path.join(test_path_plain, 'bin'))
|
||||
|
||||
config_path = os.path.join(test_path, cfg.CONFIG_FILENAME)
|
||||
testargs = ('basicswap-prepare', '-datadir=' + test_path_plain, '-withcoin=litecoin')
|
||||
process = multiprocessing.Process(target=start_prepare, args=(testargs,))
|
||||
process.start()
|
||||
process.join()
|
||||
|
||||
config_path = os.path.join(test_path_plain, cfg.CONFIG_FILENAME)
|
||||
self.assertTrue(os.path.exists(config_path))
|
||||
|
||||
logger.info('Test no overwrite')
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path, '-withcoin=litecoin']
|
||||
with patch('sys.stderr', new=StringIO()) as fake_stderr:
|
||||
import bin.basicswap_prepare as prepareSystem
|
||||
try:
|
||||
logging.info('Test no overwrite')
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path_plain, '-withcoin=litecoin']
|
||||
with patch('sys.stderr', new=StringIO()) as fake_stderr:
|
||||
with patch.object(sys, 'argv', testargs):
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
prepareSystem.main()
|
||||
|
||||
self.assertEqual(cm.exception.code, 1)
|
||||
logger.info('fake_stderr.getvalue() %s', fake_stderr.getvalue())
|
||||
self.assertTrue('exists, exiting' in fake_stderr.getvalue())
|
||||
|
||||
logger.info('Test addcoin new')
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path_plain, '-addcoin=namecoin']
|
||||
with patch.object(sys, 'argv', testargs):
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
prepareSystem.main()
|
||||
prepareSystem.main()
|
||||
with open(config_path) as fs:
|
||||
settings = json.load(fs)
|
||||
self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'rpc')
|
||||
|
||||
self.assertEqual(cm.exception.code, 1)
|
||||
logger.info('fake_stderr.getvalue() %s', fake_stderr.getvalue())
|
||||
self.assertTrue('exists, exiting' in fake_stderr.getvalue())
|
||||
logger.info('Test disablecoin')
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path_plain, '-disablecoin=namecoin']
|
||||
with patch.object(sys, 'argv', testargs):
|
||||
prepareSystem.main()
|
||||
with open(config_path) as fs:
|
||||
settings = json.load(fs)
|
||||
self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'none')
|
||||
|
||||
logger.info('Test addcoin new')
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path, '-addcoin=namecoin']
|
||||
with patch.object(sys, 'argv', testargs):
|
||||
prepareSystem.main()
|
||||
with open(config_path) as fs:
|
||||
settings = json.load(fs)
|
||||
self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'rpc')
|
||||
logger.info('Test addcoin existing')
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path_plain, '-addcoin=namecoin']
|
||||
with patch.object(sys, 'argv', testargs):
|
||||
prepareSystem.main()
|
||||
with open(config_path) as fs:
|
||||
settings = json.load(fs)
|
||||
self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'rpc')
|
||||
finally:
|
||||
del prepareSystem
|
||||
|
||||
logger.info('Test disablecoin')
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path, '-disablecoin=namecoin']
|
||||
with patch.object(sys, 'argv', testargs):
|
||||
prepareSystem.main()
|
||||
with open(config_path) as fs:
|
||||
settings = json.load(fs)
|
||||
self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'none')
|
||||
def test_encrypted(self):
|
||||
if os.path.exists(test_path_encrypted):
|
||||
shutil.rmtree(test_path_encrypted)
|
||||
if bin_path != '':
|
||||
os.makedirs(test_path_encrypted)
|
||||
os.symlink(bin_path, os.path.join(test_path_encrypted, 'bin'))
|
||||
|
||||
logger.info('Test addcoin existing')
|
||||
testargs = ['basicswap-prepare', '-datadir=' + test_path, '-addcoin=namecoin']
|
||||
with patch.object(sys, 'argv', testargs):
|
||||
prepareSystem.main()
|
||||
with open(config_path) as fs:
|
||||
settings = json.load(fs)
|
||||
self.assertTrue(settings['chainclients']['namecoin']['connection_type'] == 'rpc')
|
||||
env_vars = [('WALLET_ENCRYPTION_PWD', 'test123'), ]
|
||||
testargs = ('basicswap-prepare', '-datadir=' + test_path_encrypted, '-withcoin=litecoin,monero')
|
||||
process = multiprocessing.Process(target=start_prepare, args=(testargs, env_vars))
|
||||
process.start()
|
||||
process.join()
|
||||
assert (process.exitcode == 0)
|
||||
|
||||
logger.info('Should not be able to add a coin without setting WALLET_ENCRYPTION_PWD')
|
||||
testargs = ('basicswap-prepare', '-datadir=' + test_path_encrypted, '-addcoin=bitcoin')
|
||||
process = multiprocessing.Process(target=start_prepare, args=(testargs, []))
|
||||
process.start()
|
||||
process.join()
|
||||
assert (process.exitcode == 1)
|
||||
|
||||
testargs = ('basicswap-prepare', '-datadir=' + test_path_encrypted, '-addcoin=bitcoin')
|
||||
process = multiprocessing.Process(target=start_prepare, args=(testargs, env_vars))
|
||||
process.start()
|
||||
process.join()
|
||||
assert (process.exitcode == 0)
|
||||
|
||||
def test_encrypt(self):
|
||||
if os.path.exists(test_path_encrypt):
|
||||
shutil.rmtree(test_path_encrypt)
|
||||
if bin_path != '':
|
||||
os.makedirs(test_path_encrypt)
|
||||
os.symlink(bin_path, os.path.join(test_path_encrypt, 'bin'))
|
||||
|
||||
testargs = ('basicswap-prepare', '-regtest=1', '-datadir=' + test_path_encrypt, '-withcoin=litecoin,monero')
|
||||
process = multiprocessing.Process(target=start_prepare, args=(testargs, ))
|
||||
process.start()
|
||||
process.join()
|
||||
assert (process.exitcode == 0)
|
||||
|
||||
logger.info('basicswap-run should fail if WALLET_ENCRYPTION_PWD is set')
|
||||
env_vars = [('WALLET_ENCRYPTION_PWD', 'test123'), ]
|
||||
testargs = ('basicswap-run', '-regtest=1', '-datadir=' + test_path_encrypt)
|
||||
process = multiprocessing.Process(target=start_run, args=(testargs, env_vars))
|
||||
process.start()
|
||||
process.join()
|
||||
assert (process.exitcode == 1)
|
||||
|
||||
testargs = ('basicswap-run', '-regtest=1', '-datadir=' + test_path_encrypt)
|
||||
process = multiprocessing.Process(target=start_run, args=(testargs, ))
|
||||
process.start()
|
||||
|
||||
waitForServer(delay_event, 12700)
|
||||
rv = read_json_api(12700, 'setpassword', {'oldpassword': 'wrongpass', 'newpassword': 'test123'})
|
||||
assert ('error' in rv)
|
||||
|
||||
rv = read_json_api(12700, 'setpassword', {'oldpassword': '', 'newpassword': 'test123'})
|
||||
assert ('success' in rv)
|
||||
|
||||
rv = read_json_api(12700, 'setpassword', {'oldpassword': 'test123', 'newpassword': 'next123'})
|
||||
assert ('success' in rv)
|
||||
|
||||
rv = read_json_api(12700, 'lock')
|
||||
assert ('success' in rv)
|
||||
|
||||
rv = read_json_api(12700, 'wallets')
|
||||
assert ('error' in rv)
|
||||
|
||||
rv = read_json_api(12700, 'unlock', {'password': 'next123'})
|
||||
assert ('success' in rv)
|
||||
|
||||
rv = read_json_api(12700, 'wallets')
|
||||
assert ('PART' in rv)
|
||||
|
||||
process.terminate()
|
||||
process.join()
|
||||
assert (process.exitcode == 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -27,7 +27,7 @@ import multiprocessing
|
||||
from unittest.mock import patch
|
||||
|
||||
from tests.basicswap.mnemonics import mnemonics
|
||||
from tests.basicswap.common import (
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
waitForServer,
|
||||
)
|
||||
|
||||
@@ -26,10 +26,12 @@ import traceback
|
||||
import multiprocessing
|
||||
from unittest.mock import patch
|
||||
|
||||
from tests.basicswap.common import (
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
post_json_api,
|
||||
waitForServer,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
waitForNumOffers,
|
||||
waitForNumBids,
|
||||
)
|
||||
|
||||
@@ -34,11 +34,13 @@ from basicswap.rpc import (
|
||||
callrpc,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
BASE_RPC_PORT,
|
||||
BTC_BASE_RPC_PORT,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
make_boolean,
|
||||
read_json_api,
|
||||
waitForServer,
|
||||
BASE_RPC_PORT,
|
||||
BTC_BASE_RPC_PORT,
|
||||
)
|
||||
from tests.basicswap.common_xmr import (
|
||||
prepare_nodes,
|
||||
|
||||
@@ -22,9 +22,11 @@ from basicswap.util import (
|
||||
make_int,
|
||||
format_amount,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
wait_for_bid,
|
||||
read_json_api,
|
||||
wait_for_offer,
|
||||
wait_for_none_active,
|
||||
BTC_BASE_RPC_PORT,
|
||||
|
||||
@@ -17,9 +17,11 @@ from basicswap.basicswap import (
|
||||
from basicswap.util import (
|
||||
COIN,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
wait_for_bid,
|
||||
read_json_api,
|
||||
wait_for_offer,
|
||||
wait_for_in_progress,
|
||||
LTC_BASE_RPC_PORT,
|
||||
|
||||
@@ -25,13 +25,15 @@ from basicswap.util import (
|
||||
make_int,
|
||||
format_amount,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
post_json_req,
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
wait_for_bid,
|
||||
read_json_api,
|
||||
wait_for_offer,
|
||||
wait_for_none_active,
|
||||
wait_for_balance,
|
||||
post_json_req,
|
||||
)
|
||||
|
||||
from .test_xmr import BaseTest, test_delay_event
|
||||
|
||||
@@ -26,10 +26,12 @@ from unittest.mock import patch
|
||||
from basicswap.rpc import (
|
||||
callrpc_cli,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
post_json_api,
|
||||
waitForServer,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
waitForNumOffers,
|
||||
waitForNumBids,
|
||||
waitForNumSwapping,
|
||||
|
||||
@@ -36,13 +36,15 @@ from basicswap.util import (
|
||||
make_int,
|
||||
format_amount,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
wait_for_offer,
|
||||
wait_for_bid,
|
||||
wait_for_balance,
|
||||
wait_for_bid_tx_state,
|
||||
wait_for_in_progress,
|
||||
read_json_api,
|
||||
TEST_HTTP_PORT,
|
||||
LTC_BASE_RPC_PORT,
|
||||
BTC_BASE_RPC_PORT,
|
||||
|
||||
@@ -57,9 +57,15 @@ from basicswap.contrib.key import (
|
||||
from basicswap.http_server import (
|
||||
HttpThread,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
make_boolean,
|
||||
post_json_req,
|
||||
)
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
prepareDataDir,
|
||||
make_boolean,
|
||||
make_rpc_func,
|
||||
checkForks,
|
||||
stopDaemons,
|
||||
@@ -69,8 +75,6 @@ from tests.basicswap.common import (
|
||||
wait_for_no_offer,
|
||||
wait_for_none_active,
|
||||
wait_for_balance,
|
||||
post_json_req,
|
||||
read_json_api,
|
||||
compare_bid_states,
|
||||
extract_states_from_xu_file,
|
||||
TEST_HTTP_HOST,
|
||||
|
||||
@@ -23,9 +23,11 @@ import multiprocessing
|
||||
from urllib import parse
|
||||
from urllib.request import urlopen
|
||||
|
||||
from tests.basicswap.common import (
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
waitForServer,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
waitForNumOffers,
|
||||
waitForNumBids,
|
||||
)
|
||||
|
||||
@@ -20,10 +20,12 @@ import logging
|
||||
import unittest
|
||||
import multiprocessing
|
||||
|
||||
from tests.basicswap.common import (
|
||||
from tests.basicswap.util import (
|
||||
read_json_api,
|
||||
post_json_api,
|
||||
waitForServer,
|
||||
)
|
||||
from tests.basicswap.common import (
|
||||
waitForNumOffers,
|
||||
waitForNumBids,
|
||||
waitForNumSwapping,
|
||||
|
||||
59
tests/basicswap/util.py
Normal file
59
tests/basicswap/util.py
Normal file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2022 tecnovert
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file LICENSE.txt or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
import json
|
||||
import urllib
|
||||
from urllib.request import urlopen
|
||||
|
||||
|
||||
def make_boolean(s):
|
||||
return s.lower() in ['1', 'true']
|
||||
|
||||
|
||||
def post_json_req(url, json_data):
|
||||
req = urllib.request.Request(url)
|
||||
req.add_header('Content-Type', 'application/json; charset=utf-8')
|
||||
post_bytes = json.dumps(json_data).encode('utf-8')
|
||||
req.add_header('Content-Length', len(post_bytes))
|
||||
return urlopen(req, post_bytes, timeout=300).read()
|
||||
|
||||
|
||||
def read_text_api(port, path=None):
|
||||
url = f'http://127.0.0.1:{port}/json'
|
||||
if path is not None:
|
||||
url += '/' + path
|
||||
return urlopen(url, timeout=300).read().decode('utf-8')
|
||||
|
||||
|
||||
def read_json_api(port, path=None, json_data=None):
|
||||
url = f'http://127.0.0.1:{port}/json'
|
||||
if path is not None:
|
||||
url += '/' + path
|
||||
|
||||
if json_data is not None:
|
||||
return json.loads(post_json_req(url, json_data))
|
||||
return json.loads(urlopen(url, timeout=300).read())
|
||||
|
||||
|
||||
def post_json_api(port, path, json_data):
|
||||
url = f'http://127.0.0.1:{port}/json'
|
||||
if path is not None:
|
||||
url += '/' + path
|
||||
return json.loads(post_json_req(url, json_data))
|
||||
|
||||
|
||||
def waitForServer(delay_event, port, wait_for=20):
|
||||
for i in range(wait_for):
|
||||
if delay_event.is_set():
|
||||
raise ValueError('Test stopped.')
|
||||
try:
|
||||
delay_event.wait(1)
|
||||
summary = read_json_api(port)
|
||||
return
|
||||
except Exception as e:
|
||||
print('waitForServer, error:', str(e))
|
||||
raise ValueError('waitForServer failed')
|
||||
Reference in New Issue
Block a user