ts_input.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2020 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # Project source code repository: https://github.com/mmgen/mmgen
  7. # Licensed according to the terms of GPL Version 3. See LICENSE for details.
  8. """
  9. ts_input.py: user input tests for the MMGen test.py test suite
  10. """
  11. from test.common import *
  12. from test.test_py_d.ts_base import *
  13. from mmgen.seed import SeedSource
  14. class TestSuiteInput(TestSuiteBase):
  15. 'user input'
  16. networks = ('btc',)
  17. tmpdir_nums = []
  18. cmd_group = (
  19. ('password_entry_noecho', (1,"utf8 password entry", [])),
  20. ('password_entry_echo', (1,"utf8 password entry (echoed)", [])),
  21. ('mnemonic_entry_mmgen', (1,"stealth mnemonic entry (mmgen)", [])),
  22. ('mnemonic_entry_bip39', (1,"stealth mnemonic entry (bip39)", [])),
  23. ('dieroll_entry', (1,"dieroll entry (base6d)", [])),
  24. ('dieroll_entry_usrrand', (1,"dieroll entry (base6d) with added user entropy", [])),
  25. )
  26. def password_entry(self,prompt,cmd_args):
  27. t = self.spawn('test/misc/password_entry.py',cmd_args,cmd_dir='.')
  28. pw = 'abc-α'
  29. t.expect(prompt,pw)
  30. ret = t.expect_getend('Entered: ')
  31. assert ret == pw,'Password mismatch! {} != {}'.format(ret,pw)
  32. return t
  33. def password_entry_noecho(self):
  34. if self.skip_for_win():
  35. m = "getpass() doesn't work with pexpect.popen_spawn!\n"
  36. m += 'Perform the following test by hand with non-ASCII password abc-α:\n'
  37. m += ' test/misc/password_entry.py'
  38. return ('skip_warn',m)
  39. return self.password_entry('Enter passphrase: ',[])
  40. def password_entry_echo(self):
  41. if self.skip_for_win():
  42. m = "getpass() doesn't work with pexpect.popen_spawn!\n"
  43. m += 'Perform the following test by hand with non-ASCII password abc-α:\n'
  44. m += ' test/misc/password_entry.py --echo-passphrase'
  45. return ('skip_warn',m)
  46. return self.password_entry('Enter passphrase (echoed): ',['--echo-passphrase'])
  47. def _user_seed_entry(self,fmt,usr_rand=False,out_fmt=None,mn=None):
  48. wcls = SeedSource.fmt_code_to_type(fmt)
  49. wf = os.path.join(ref_dir,'FE3C6545.{}'.format(wcls.ext))
  50. if wcls.wclass == 'mnemonic':
  51. mn = mn or read_from_file(wf).strip().split()
  52. elif wcls.wclass == 'dieroll':
  53. mn = mn or list(read_from_file(wf).strip().translate(dict((ord(ws),None) for ws in '\t\n ')))
  54. for idx,val in ((5,'x'),(18,'0'),(30,'7'),(44,'9')):
  55. mn.insert(idx,val)
  56. t = self.spawn('mmgen-walletconv',['-r10','-S','-i',fmt,'-o',out_fmt or fmt])
  57. t.expect('{} type: {}'.format(capfirst(wcls.wclass),wcls.mn_type))
  58. t.expect(wcls.choose_seedlen_prompt,'1')
  59. t.expect('(Y/n): ','y')
  60. if wcls.wclass == 'mnemonic':
  61. stealth_mnemonic_entry(t,mn,fmt=fmt)
  62. elif wcls.wclass == 'dieroll':
  63. user_dieroll_entry(t,mn)
  64. if usr_rand:
  65. t.expect(wcls.user_entropy_prompt,'y')
  66. t.usr_rand(10)
  67. else:
  68. t.expect(wcls.user_entropy_prompt,'n')
  69. if not usr_rand:
  70. sid_chk = 'FE3C6545'
  71. sid = t.expect_getend('Valid {} for Seed ID '.format(wcls.desc))[:8]
  72. assert sid == sid_chk,'Seed ID mismatch! {} != {}'.format(sid,sid_chk)
  73. t.expect('to confirm: ','YES\n')
  74. t.read()
  75. return t
  76. def mnemonic_entry_mmgen(self): return self._user_seed_entry('words',entry_mode='full')
  77. def mnemonic_entry_bip39(self): return self._user_seed_entry('bip39',entry_mode='full')
  78. def dieroll_entry(self): return self._user_seed_entry('dieroll')
  79. def dieroll_entry_usrrand(self): return self._user_seed_entry('dieroll',usr_rand=True,out_fmt='bip39')