ui: Add wallet encryption templates.

tests: Test wallet encryption.
This commit is contained in:
tecnovert committed 2022-11-18 00:58:14 +02:00
1 parent 0f7df9e5f1
commit c5f31f0d1e
28 files changed
+430 -113

No files matched your search

+3 -1
View File
@@ -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,
+3 -1
View File
@@ -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,
+3 -1
View File
@@ -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,
+3 -1
View File
@@ -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,
+3 -1
View File
@@ -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,
+161 -40
View File
@@ -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__':
+1 -1
View File
@@ -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,