Browse Source

mmgen-autosign: improve args/opts processing, add checks

The MMGen Project 1 year ago
parent
commit
ad6161dbb7
2 changed files with 58 additions and 29 deletions
  1. 41 29
      mmgen/main_autosign.py
  2. 17 0
      test/cmdtest_py_d/ct_autosign.py

+ 41 - 29
mmgen/main_autosign.py

@@ -177,39 +177,51 @@ cfg = Config(
 	},
 	do_post_init = True )
 
-cmd = cfg._args[0] if len(cfg._args) == 1 else None if not cfg._args else cfg._opts.usage()
+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', 'sign', 'wait', 'clean', 'wipe_key')
+
+if cmd not in valid_cmds:
+	die(1,f'‘{cmd}’: unrecognized command')
+
+if cmd != 'setup':
+	for opt in ('seed_len', 'mnemonic_fmt'):
+		if getattr(cfg,opt):
+			die(1, f'--{opt.replace("_","-")} makes sense only for the ‘setup’ operation')
+
+if cmd not in ('sign', 'wait'):
+	for opt in ('no_summary', 'led', 'stealth_led', 'full_summary'):
+		if getattr(cfg,opt):
+			die(1, f'--{opt.replace("_","-")} makes no sense for the ‘{cmd}’ operation')
 
 asi = Autosign(cfg,cmd)
 
 cfg._post_init()
 
-if cmd:
-	if cmd == 'gen_key':
-		asi.gen_key()
-	elif cmd == 'setup':
-		asi.setup()
-		from .ui import keypress_confirm
-		if cfg.xmrwallets and keypress_confirm( cfg, '\nContinue with Monero setup?', default_yes=True ):
-			msg('')
-			asi.xmr_setup()
-		asi.do_umount()
-	elif cmd == 'xmr_setup':
-		if not cfg.xmrwallets:
-			die(1,'Please specify a wallet or range of wallets with the --xmrwallets option')
-		asi.do_mount()
+if cmd == 'gen_key':
+	asi.gen_key()
+elif cmd == 'setup':
+	asi.setup()
+	from .ui import keypress_confirm
+	if cfg.xmrwallets and keypress_confirm( cfg, '\nContinue with Monero setup?', default_yes=True ):
+		msg('')
 		asi.xmr_setup()
-		asi.do_umount()
-	elif cmd == 'wait':
-		main(do_loop=True)
-	elif cmd == 'clean':
-		asi.do_mount()
-		asi.clean_old_files()
-		asi.do_umount()
-	elif cmd == 'wipe_key':
-		asi.do_mount()
-		asi.wipe_encryption_key()
-		asi.do_umount()
-	else:
-		die(1,f'{cmd!r}: unrecognized command')
-else:
+	asi.do_umount()
+elif cmd == 'xmr_setup':
+	if not cfg.xmrwallets:
+		die(1,'Please specify a wallet or range of wallets with the --xmrwallets option')
+	asi.do_mount()
+	asi.xmr_setup()
+	asi.do_umount()
+elif cmd == 'sign':
 	main(do_loop=False)
+elif cmd == 'wait':
+	main(do_loop=True)
+elif cmd == 'clean':
+	asi.do_mount()
+	asi.clean_old_files()
+	asi.do_umount()
+elif cmd == 'wipe_key':
+	asi.do_mount()
+	asi.wipe_encryption_key()
+	asi.do_umount()

+ 17 - 0
test/cmdtest_py_d/ct_autosign.py

@@ -428,6 +428,9 @@ class CmdTestAutosign(CmdTestAutosignBase):
 		('copy_tx_files',            'copying transaction files'),
 		('gen_key',                  'generating key'),
 		('create_dfl_wallet',        'creating default MMGen wallet'),
+		('bad_opt1',                 'running ‘mmgen-autosign’ with --seed-len in invalid context'),
+		('bad_opt2',                 'running ‘mmgen-autosign’ with --mnemonic-fmt in invalid context'),
+		('bad_opt3',                 'running ‘mmgen-autosign’ with --led in invalid context'),
 		('run_setup_dfl_wallet',     'running ‘autosign setup’ (with default wallet)'),
 		('sign_quiet',               'signing transactions (--quiet)'),
 		('remove_signed_txfiles',    'removing signed transaction files'),
@@ -511,6 +514,20 @@ class CmdTestAutosign(CmdTestAutosignBase):
 		t.written_to_file('MMGen wallet')
 		return t
 
+	def _bad_opt(self, cmdline, expect):
+		t = self.spawn('mmgen-autosign', ['--coins=btc'] + cmdline, exit_val=1)
+		t.expect(expect)
+		return t
+
+	def bad_opt1(self):
+		return self._bad_opt(['--seed-len=128'], 'makes sense')
+
+	def bad_opt2(self):
+		return self._bad_opt(['--mnemonic-fmt=bip39', 'wait'], 'makes sense')
+
+	def bad_opt3(self):
+		return self._bad_opt(['--led', 'gen_key'], 'makes no sense')
+
 	def run_setup_dfl_wallet(self):
 		return self.run_setup(mn_type='default',use_dfl_wallet=True)