From 0a15307e0526f8723acdc88eaded9d2e9914d566 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 6 Sep 2024 12:20:13 +0000 Subject: [PATCH] autosign, led, unit test testdep: minor cleanups --- mmgen/autosign.py | 11 ++++++++++- mmgen/led.py | 14 +------------- mmgen/main_autosign.py | 17 +++-------------- mmgen/util.py | 9 +++++++++ test/cmdtest.py | 3 +++ test/cmdtest_py_d/ct_automount.py | 29 +++++++++++++++++------------ test/cmdtest_py_d/ct_autosign.py | 11 +++++++---- test/unit_tests_d/ut_testdep.py | 8 ++++---- 8 files changed, 54 insertions(+), 48 deletions(-) diff --git a/mmgen/autosign.py b/mmgen/autosign.py index 06b10089..f36bd9a4 100755 --- a/mmgen/autosign.py +++ b/mmgen/autosign.py @@ -329,6 +329,15 @@ class Autosign: macOS_ramdisk_name = 'AutosignRamDisk' wallet_subdir = 'autosign' + cmds = ('setup', 'xmr_setup', 'sign', 'wait') + + util_cmds = ( + 'gen_key', + 'macos_ramdisk_setup', + 'macos_ramdisk_delete', + 'clean', + 'wipe_key') + mn_fmts = { 'mmgen': 'words', 'bip39': 'bip39', @@ -416,7 +425,7 @@ class Autosign: if cfg.xmrwallets and not 'XMR' in self.coins: self.coins.append('XMR') - if not self.coins and cmd not in ('gen_key','wipe_key'): + if not self.coins and cmd in self.cmds: ymsg('Warning: no coins specified, defaulting to BTC') self.coins = ['BTC'] diff --git a/mmgen/led.py b/mmgen/led.py index cceddefd..ac38874a 100755 --- a/mmgen/led.py +++ b/mmgen/led.py @@ -78,10 +78,7 @@ class LEDControl: msg(f'{board.name} board detected') if self.debug: - msg(fmt(f""" - Status file: {board.status} - Trigger file: {board.trigger} - """,indent=' ',strip_char='\t')) + msg(f'\n Status file: {board.status}\n Trigger file: {board.trigger}') def check_access(fn,desc,init_val=None): try: @@ -114,15 +111,6 @@ class LEDControl: with open(db.trigger,'w') as fp: fp.write(db.trigger_states[1]+'\n') - @classmethod - def delete_dummy_control_files(cls): - db = cls.boards['dummy'] - for fn in (db.status, db.trigger): - try: - os.unlink(fn) - except Exception as e: - msg(str(e)) - def noop(self,*args,**kwargs): pass diff --git a/mmgen/main_autosign.py b/mmgen/main_autosign.py index 5fcf873b..448eafe7 100755 --- a/mmgen/main_autosign.py +++ b/mmgen/main_autosign.py @@ -70,8 +70,8 @@ gen_key - generate the wallet encryption key and copy it to the removable configured) setup - full setup: run ‘gen_key’ and create temporary signing wallet(s) for all configured coins -xmr_setup - set up Monero temporary signing wallet(s). Not required in normal - operation: use ‘setup’ with --xmrwallets instead +xmr_setup - set up Monero temporary signing wallet(s). Not required during + normal operation: use ‘setup’ with --xmrwallets instead macos_ramdisk_setup - set up the ramdisk used for storing the temporary signing wallet(s) (macOS only). Required only when creating the wallet(s) manually, without ‘setup’ @@ -191,18 +191,7 @@ cfg = Config( cmd = cfg._args[0] if len(cfg._args) == 1 else 'sign' if not cfg._args else cfg._opts.usage() -valid_cmds = ( - 'gen_key', - 'setup', - 'xmr_setup', - 'macos_ramdisk_setup', - 'macos_ramdisk_delete', - 'sign', - 'wait', - 'clean', - 'wipe_key') - -if cmd not in valid_cmds: +if cmd not in Autosign.cmds + Autosign.util_cmds: die(1,f'‘{cmd}’: unrecognized command') if cmd != 'setup': diff --git a/mmgen/util.py b/mmgen/util.py index f4871866..23514916 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -456,3 +456,12 @@ def wrap_ripemd160(called=[]): def exit_if_mswin(feature): if sys.platform == 'win32': die(2, capfirst(feature) + ' not supported on the MSWin / MSYS2 platform' ) + +def have_sudo(silent=False): + from subprocess import run, DEVNULL + redir = DEVNULL if silent else None + try: + run(['sudo', '--non-interactive', 'true'], stdout=redir, stderr=redir, check=True) + return True + except: + return False diff --git a/test/cmdtest.py b/test/cmdtest.py index 75141174..94be89e9 100755 --- a/test/cmdtest.py +++ b/test/cmdtest.py @@ -1004,6 +1004,9 @@ if __name__ == '__main__': if tr.daemon_started and not cfg.no_daemon_stop: stop_test_daemons(network_id, remove_datadir=True) tr.warn_skipped() + if hasattr(tr, 'tg'): + del tr.tg + del tr die(1,yellow('\ntest.py exiting at user request')) except TestSuiteSpawnedScriptException as e: # if spawned script is not running under exec_wrapper, output brief error msg: diff --git a/test/cmdtest_py_d/ct_automount.py b/test/cmdtest_py_d/ct_automount.py index b72a08d4..079f2f3e 100755 --- a/test/cmdtest_py_d/ct_automount.py +++ b/test/cmdtest_py_d/ct_automount.py @@ -83,8 +83,17 @@ class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtestBDBWallet) self.opts.append('--alice') - def _alice_txcreate(self, chg_addr, opts=[], exit_val=0, expect=None): + def _alice_txcreate(self, chg_addr, opts=[], exit_val=0, expect_str=None): + + def do_return(): + if expect_str: + t.expect(expect_str) + t.read() + self.remove_device_online() + return t + self.insert_device_online() + sid = self._user_sid('alice') t = self.spawn( 'mmgen-txcreate', @@ -92,21 +101,17 @@ class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtestBDBWallet) + ['--alice', '--autosign'] + [f'{self.burn_addr},1.23456', f'{sid}:{chg_addr}'], exit_val = exit_val or None) + if exit_val: - t.expect(expect) - t.read() - self.remove_device_online() - return t + return do_return() + t = self.txcreate_ui_common( t, inputs = '1', interactive_fee = '32s', file_desc = 'Unsigned automount transaction') - if expect: - t.expect(expect) - t.read() - self.remove_device_online() - return t + + return do_return() def alice_txcreate1(self): return self._alice_txcreate(chg_addr='C:5') @@ -155,10 +160,10 @@ class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtestBDBWallet) alice_txsend_abort5 = alice_txsend_abort2 def alice_txcreate_bad_have_unsigned(self): - return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect='already present') + return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect_str='already present') def alice_txcreate_bad_have_unsent(self): - return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect='unsent transaction') + return self._alice_txcreate(chg_addr='C:5', exit_val=2, expect_str='unsent transaction') def copy_wallet(self): self.spawn('', msg_only=True) diff --git a/test/cmdtest_py_d/ct_autosign.py b/test/cmdtest_py_d/ct_autosign.py index fbd5e1ea..82a2860d 100755 --- a/test/cmdtest_py_d/ct_autosign.py +++ b/test/cmdtest_py_d/ct_autosign.py @@ -90,7 +90,10 @@ class CmdTestAutosignBase(CmdTestBase): def __del__(self): if hasattr(self,'have_dummy_control_files'): - LEDControl.delete_dummy_control_files() + db = LEDControl.boards['dummy'] + for fn in (db.status, db.trigger): + run('sudo rm -f {fn}'.split(), check=True) + if hasattr(self, 'txdev'): del self.txdev if not cfg.no_daemon_stop: @@ -781,7 +784,7 @@ class CmdTestAutosign(CmdTestAutosignBase): tx_desc = Signable.transaction.desc self.insert_device() - def do_exit(): + def do_return(): if expect_str: t.expect(expect_str) t.read() @@ -795,7 +798,7 @@ class CmdTestAutosign(CmdTestAutosignBase): exit_val = exc_exit_val or (1 if self.bad_tx_count or (have_msg and self.bad_msg_count) else None)) if exc_exit_val: - return do_exit() + return do_return() t.expect( f'{self.tx_count} {tx_desc}{suf(self.tx_count)} signed' if self.tx_count else @@ -815,7 +818,7 @@ class CmdTestAutosign(CmdTestAutosignBase): f'{self.bad_msg_count} message file{suf(self.bad_msg_count)}{{0,1}} failed to sign', regex = True) - return do_exit() + return do_return() def sign_quiet(self): return self.do_sign(['--quiet']) diff --git a/test/unit_tests_d/ut_testdep.py b/test/unit_tests_d/ut_testdep.py index 9ee5e6fc..d5454513 100755 --- a/test/unit_tests_d/ut_testdep.py +++ b/test/unit_tests_d/ut_testdep.py @@ -16,7 +16,7 @@ class unit_tests: altcoin_deps = ('pycoin','monero_python','keyconv','zcash_mini','ethkey','ssh_socks_proxy') win_skip = ('losetup', 'zcash_mini', 'sudo') - mac_skip = ('losetup', 'sudo') + mac_skip = ('losetup',) def pylint(self,name,ut): try: @@ -76,9 +76,9 @@ class unit_tests: return CmdTestXMRWallet.init_proxy(external_call=True) def sudo(self,name,ut): - try: - run(['sudo', '--non-interactive', 'true'], check=True) + from mmgen.util import have_sudo + if have_sudo(): return True - except: + else: ymsg(f'To run the test suite, please enable sudo without password for user ‘{os.getenv("USER")}’') return False