ts_input.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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. test.test_py_d.ts_input: user input tests for the MMGen test.py test suite
  10. """
  11. import time
  12. from ..include.common import *
  13. from .ts_base import *
  14. from .input import *
  15. from mmgen.wallet import get_wallet_cls
  16. class TestSuiteInput(TestSuiteBase):
  17. 'user input'
  18. networks = ('btc',)
  19. tmpdir_nums = [1]
  20. color = True
  21. cmd_group_in = (
  22. ('subgroup.char', []),
  23. ('subgroup.line', []),
  24. ('subgroup.password', []),
  25. ('subgroup.misc', []),
  26. ('subgroup.wallet', []),
  27. ('subgroup.mnemonic', []),
  28. ('subgroup.dieroll', []),
  29. )
  30. cmd_subgroups = {
  31. 'char': (
  32. 'get_char() function',
  33. ('get_char1', 'get_char()'),
  34. ('get_char2', 'get_char() [multiple characters]'),
  35. ('get_char3', 'get_char() [no prompt]'),
  36. ('get_char4', 'get_char() [utf8]'),
  37. ('get_char_term1', 'get_char() [term, utf8]'),
  38. ('get_char_term2', 'get_char() [term, multiple characters]'),
  39. ('get_char_term3', 'get_char() [term, prehold_protect=False]'),
  40. ('get_char_term4', 'get_char() [term, immed_chars="xyz"]'),
  41. ),
  42. 'line': (
  43. 'line_input() function',
  44. ('line_input', 'line_input()'),
  45. ('line_input_term1', 'line_input() [term]'),
  46. ('line_input_term2', 'line_input() [term, no hold protect]'),
  47. ('line_input_insert', 'line_input() [inserted text]'),
  48. ('line_input_insert_term1', 'line_input() [inserted text, term]'),
  49. ('line_input_insert_term2', 'line_input() [inserted text, term, no hold protect]'),
  50. ('line_input_edit_term', 'line_input() [inserted + edited text, term, utf8]'),
  51. ('line_input_erase_term', 'line_input() [inserted + erased text, term]'),
  52. ),
  53. 'password': (
  54. 'password entry via line_input()',
  55. ('password_entry_noecho', 'utf8 password entry'),
  56. ('password_entry_noecho_term', 'utf8 password entry [term]'),
  57. ('password_entry_echo', 'utf8 password entry (echoed)'),
  58. ('password_entry_echo_term', 'utf8 password entry (echoed) [term]'),
  59. ),
  60. 'misc': (
  61. 'miscellaneous user-level UI functions',
  62. ('get_seed_from_stdin', 'reading seed phrase from STDIN'),
  63. ),
  64. 'wallet': (
  65. 'hash preset, password and label entry',
  66. ('get_passphrase_ui', 'hash preset, password and label (wallet.py)'),
  67. ('get_passphrase_cmdline', 'hash preset, password and label (wallet.py - from cmdline)'),
  68. ('get_passphrase_crypto', 'hash preset, password and label (crypto.py)'),
  69. ),
  70. 'mnemonic': (
  71. 'mnemonic entry',
  72. ('mnemonic_entry_mmgen', 'stealth mnemonic entry (mmgen)'),
  73. ('mnemonic_entry_mmgen_minimal', 'stealth mnemonic entry (mmgen - minimal entry mode)'),
  74. ('mnemonic_entry_bip39', 'stealth mnemonic entry (bip39)'),
  75. ('mnemonic_entry_bip39_short', 'stealth mnemonic entry (bip39 - short entry mode)'),
  76. ('mn2hex_interactive_mmgen', 'mn2hex_interactive (mmgen)'),
  77. ('mn2hex_interactive_mmgen_fixed','mn2hex_interactive (mmgen - fixed (10-letter) entry mode)'),
  78. ('mn2hex_interactive_bip39', 'mn2hex_interactive (bip39)'),
  79. ('mn2hex_interactive_bip39_short','mn2hex_interactive (bip39 - short entry mode (+pad entry))'),
  80. ('mn2hex_interactive_bip39_fixed','mn2hex_interactive (bip39 - fixed (4-letter) entry mode)'),
  81. ('mn2hex_interactive_xmr', 'mn2hex_interactive (xmrseed)'),
  82. ('mn2hex_interactive_xmr_short', 'mn2hex_interactive (xmrseed - short entry mode)'),
  83. ),
  84. 'dieroll': (
  85. 'dieroll entry',
  86. ('dieroll_entry', 'dieroll entry (base6d)'),
  87. ('dieroll_entry_usrrand', 'dieroll entry (base6d) with added user entropy'),
  88. )
  89. }
  90. def get_seed_from_stdin(self):
  91. self.spawn('',msg_only=True)
  92. from subprocess import run,PIPE
  93. cmd = ['python3','cmds/mmgen-walletconv','--in-fmt=words','--out-fmt=words','--outdir=test/trash']
  94. mn = sample_mn['mmgen']['mn']
  95. run_env = dict(os.environ)
  96. run_env['MMGEN_TEST_SUITE'] = ''
  97. # the test can fail the first time if cfg file has changed, so run it twice if necessary:
  98. for i in range(2):
  99. cp = run( cmd, input=mn.encode(), stdout=PIPE, stderr=PIPE, env=run_env )
  100. if b'written to file' in cp.stderr:
  101. break
  102. from mmgen.color import set_vt100
  103. set_vt100()
  104. imsg(cp.stderr.decode().strip())
  105. res = get_data_from_file(cfg,'test/trash/A773B05C[128].mmwords',silent=True).strip()
  106. assert res == mn, f'{res} != {mn}'
  107. return 'ok' if b'written to file' in cp.stderr else 'error'
  108. def get_passphrase_ui(self):
  109. t = self.spawn('test/misc/get_passphrase.py',['--usr-randchars=0','seed'],cmd_dir='.')
  110. # 1 - new wallet, default hp,label;empty pw
  111. t.expect('accept the default.*: ','\n',regex=True)
  112. # bad repeat
  113. t.expect('new MMGen wallet: ','pass1\n')
  114. t.expect('peat passphrase: ','pass2\n')
  115. # good repeat
  116. t.expect('new MMGen wallet: ','\n')
  117. t.expect('peat passphrase: ','\n')
  118. t.expect('mpty pass')
  119. t.expect('no label: ','\n')
  120. t.expect('[][3][No Label]')
  121. # 2 - new wallet, user-selected hp,pw,label
  122. t.expect('accept the default.*: ', '1\n', regex=True)
  123. t.expect('new MMGen wallet: ','pass1\n')
  124. t.expect('peat passphrase: ','pass1\n')
  125. t.expect('no label: ','lbl1\n')
  126. t.expect('[pass1][1][lbl1]')
  127. # 3 - passchg, nothing changes
  128. t.expect('new hash preset')
  129. t.expect('reuse the old value.*: ','\n',regex=True)
  130. t.expect('unchanged')
  131. t.expect('new passphrase.*: ','pass1\n',regex=True)
  132. t.expect('peat new passphrase: ','pass1\n')
  133. t.expect('unchanged')
  134. t.expect('reuse the label .*: ','\n',regex=True)
  135. t.expect('unchanged')
  136. t.expect('[pass1][1][lbl1]')
  137. # 4 - passchg, everything changes
  138. t.expect('new hash preset')
  139. t.expect('reuse the old value.*: ','2\n',regex=True)
  140. t.expect(' changed to')
  141. t.expect('new passphrase.*: ','pass2\n',regex=True)
  142. t.expect('peat new passphrase: ','pass2\n')
  143. t.expect(' changed')
  144. t.expect('reuse the label .*: ','lbl2\n',regex=True)
  145. t.expect(' changed to')
  146. t.expect('[pass2][2][lbl2]')
  147. # 5 - wallet from file
  148. t.expect('from file')
  149. # bad passphrase
  150. t.expect('passphrase for MMGen wallet: ','bad\n')
  151. t.expect('Trying again')
  152. # good passphrase
  153. t.expect('passphrase for MMGen wallet: ','reference password\n')
  154. t.expect('[reference password][1][No Label]')
  155. return t
  156. def get_passphrase_cmdline(self):
  157. with open('test/trash/pwfile','w') as fp:
  158. fp.write('reference password\n')
  159. t = self.spawn('test/misc/get_passphrase.py', [
  160. '--usr-randchars=0',
  161. '--label=MyLabel',
  162. '--passwd-file=test/trash/pwfile',
  163. '--hash-preset=1',
  164. 'seed' ],
  165. cmd_dir = '.' )
  166. for foo in range(4):
  167. t.expect('[reference password][1][MyLabel]')
  168. return t
  169. def get_passphrase_crypto(self):
  170. t = self.spawn('test/misc/get_passphrase.py',['--usr-randchars=0','crypto'],cmd_dir='.')
  171. # new passwd
  172. t.expect('passphrase for .*: ', 'x\n', regex=True)
  173. t.expect('peat passphrase: ', '\n')
  174. t.expect('passphrase for .*: ', 'pass1\n', regex=True)
  175. t.expect('peat passphrase: ', 'pass1\n')
  176. t.expect('[pass1]')
  177. # existing passwd
  178. t.expect('passphrase for .*: ', 'pass2\n', regex=True)
  179. t.expect('[pass2]')
  180. # hash preset
  181. t.expect('accept the default .*: ', '0\n', regex=True)
  182. t.expect('nvalid')
  183. t.expect('accept the default .*: ', '8\n', regex=True)
  184. t.expect('nvalid')
  185. t.expect('accept the default .*: ', '7\n', regex=True)
  186. t.expect('[7]')
  187. # hash preset (default)
  188. t.expect('accept the default .*: ', '\n', regex=True)
  189. t.expect(f'[{gc.dfl_hash_preset}]')
  190. return t
  191. def _input_func(self,func_name,arg_dfls,func_args,text,expect,term):
  192. if term and gc.platform == 'win':
  193. return ('skip_warn','pexpect_spawn not supported on Windows platform')
  194. func_args = {k:v for k,v in zip(arg_dfls.keys(),func_args)}
  195. t = self.spawn(
  196. 'test/misc/input_func.py',
  197. [func_name,repr(func_args)],
  198. cmd_dir='.',
  199. pexpect_spawn=term )
  200. imsg('Parameters:')
  201. imsg(' pexpect_spawn: {}'.format(term))
  202. imsg(' sending: {!r}'.format(text))
  203. imsg(' expecting: {!r}'.format(expect))
  204. imsg('\nFunction args:')
  205. for k,v in func_args.items():
  206. imsg(' {:14} {!r}'.format(k+':',v))
  207. imsg_r('\nScript output: ')
  208. prompt_add = (func_args['insert_txt'] if term else '') if func_name == 'line_input' else ''
  209. prompt = func_args['prompt'] + prompt_add
  210. t.expect('Calling ')
  211. if prompt:
  212. t.expect(prompt,text)
  213. else:
  214. t.send(text)
  215. ret = t.expect_getend(' ==> ')
  216. assert ret == repr(expect), f'Text mismatch! {ret} != {repr(expect)}'
  217. return t
  218. def _get_char(self,func_args,text,expect,term):
  219. arg_dfls = {
  220. 'prompt': '',
  221. 'immed_chars': '',
  222. 'prehold_protect': True,
  223. 'num_bytes': 5,
  224. }
  225. return self._input_func('get_char',arg_dfls,func_args,text,expect,term)
  226. def _line_input(self,func_args,text,expect,term):
  227. arg_dfls = {
  228. 'prompt': '', # positional
  229. 'echo': True,
  230. 'insert_txt': '',
  231. 'hold_protect': True,
  232. }
  233. return self._input_func('line_input',arg_dfls,func_args,text+'\n',expect,term)
  234. def get_char1(self):
  235. return self._get_char(['prompt> ','',True,5],'x','x',False)
  236. def get_char2(self):
  237. expect = 'x' if gc.platform == 'win' else 'xxxxx'
  238. return self._get_char(['prompt> ','',True,5],'xxxxx',expect,False)
  239. def get_char3(self):
  240. return self._get_char(['','',True,5],'x','x',False)
  241. def get_char4(self):
  242. return self._get_char(['prompt> ','',True,2],'α','α',False) # UTF-8, must get 2 bytes
  243. def get_char_term1(self):
  244. return self._get_char(['prompt> ','',True,2],'β','β',True) # UTF-8, must get 2 bytes
  245. def get_char_term2(self):
  246. return self._get_char(['prompt> ','',True,5],'xxxxx','xxxxx',True)
  247. def get_char_term3(self):
  248. return self._get_char(['','',False,5],'x','x',True)
  249. def get_char_term4(self):
  250. return self._get_char(['prompt> ','xyz',False,5],'x','x',True)
  251. def line_input(self):
  252. return self._line_input(['prompt> ',True,'',True],'foo','foo',False)
  253. def line_input_term1(self):
  254. return self._line_input(['prompt> ',True,'',True],'foo','foo',True)
  255. def line_input_term2(self):
  256. return self._line_input(['prompt> ',True,'',False],'foo','foo',True)
  257. def line_input_insert(self):
  258. return self._line_input(['prompt> ',True,'inserted text',True],'foo','foo',False)
  259. def line_input_insert_term1(self):
  260. return self._line_input(['prompt> ',True,'foo',True],'bar','foobar',True)
  261. def line_input_insert_term2(self):
  262. return self._line_input(['prompt> ',True,'foo',False],'bar','foobar',True)
  263. def line_input_edit_term(self):
  264. return self._line_input(['prompt> ',True,'φυφυ',True],'\b\bβαρ','φυβαρ',True)
  265. def line_input_erase_term(self):
  266. return self._line_input(['prompt> ',True,'foobarbaz',True],Ctrl_U+'foobar','foobar',True)
  267. def _password_entry(self,prompt,opts=[],term=False):
  268. if term and gc.platform == 'win':
  269. return ('skip_warn','pexpect_spawn not supported on Windows platform')
  270. t = self.spawn( 'test/misc/input_func.py', opts + ['passphrase'], cmd_dir='.', pexpect_spawn=term )
  271. imsg('Terminal: {}'.format(term))
  272. pw = 'abc-α'
  273. t.expect(prompt,pw+'\n')
  274. ret = t.expect_getend('Entered: ')
  275. assert ret == pw, f'Password mismatch! {ret} != {pw}'
  276. return t
  277. winskip_msg = """
  278. pexpect_spawn not supported on Windows platform
  279. Perform the following test by hand with non-ASCII password abc-α
  280. or another password in your native alphabet:
  281. test/misc/input_func.py{} passphrase
  282. """
  283. def password_entry_noecho(self,term=False):
  284. return self._password_entry('Enter passphrase: ',term=term)
  285. def password_entry_noecho_term(self):
  286. if self.skip_for_win():
  287. return ('skip_warn','\n' + fmt(self.winskip_msg.format(''),strip_char='\t'))
  288. return self.password_entry_noecho(term=True)
  289. def password_entry_echo(self,term=False):
  290. return self._password_entry('Enter passphrase (echoed): ',['--echo-passphrase'],term=term)
  291. def password_entry_echo_term(self):
  292. if self.skip_for_win():
  293. return ('skip_warn','\n' + fmt(self.winskip_msg.format(' --echo-passphrase'),strip_char='\t'))
  294. return self.password_entry_echo(term=True)
  295. def _mn2hex(self,fmt,entry_mode='full',mn=None,pad_entry=False,enter_for_dfl=False):
  296. mn = mn or sample_mn[fmt]['mn'].split()
  297. t = self.spawn('mmgen-tool',['mn2hex_interactive','fmt='+fmt,'mn_len=12','print_mn=1'])
  298. from mmgen.mn_entry import mn_entry
  299. mne = mn_entry( cfg, fmt, entry_mode )
  300. t.expect(
  301. 'Type a number.*: ',
  302. ('\n' if enter_for_dfl else str(mne.entry_modes.index(entry_mode)+1)),
  303. regex = True )
  304. t.expect('Using (.+) entry mode',regex=True)
  305. mode = strip_ansi_escapes(t.p.match.group(1)).lower()
  306. assert mode == mne.em.name.lower(), f'{mode} != {mne.em.name.lower()}'
  307. stealth_mnemonic_entry(t,mne,mn,entry_mode=entry_mode,pad_entry=pad_entry)
  308. t.expect(sample_mn[fmt]['hex'])
  309. return t
  310. def _user_seed_entry(self,fmt,usr_rand=False,out_fmt=None,entry_mode='full',mn=None):
  311. wcls = get_wallet_cls(fmt_code=fmt)
  312. wf = os.path.join(ref_dir,f'FE3C6545.{wcls.ext}')
  313. if wcls.base_type == 'mnemonic':
  314. mn = mn or read_from_file(wf).strip().split()
  315. elif wcls.type == 'dieroll':
  316. mn = mn or list(remove_whitespace(read_from_file(wf)))
  317. for idx,val in ((5,'x'),(18,'0'),(30,'7'),(44,'9')):
  318. mn.insert(idx,val)
  319. t = self.spawn('mmgen-walletconv',['-r10','-S','-i',fmt,'-o',out_fmt or fmt])
  320. t.expect(f'{capfirst(wcls.base_type or wcls.type)} type:.*{wcls.mn_type}',regex=True)
  321. t.expect(wcls.choose_seedlen_prompt,'1')
  322. t.expect('(Y/n): ','y')
  323. if wcls.base_type == 'mnemonic':
  324. t.expect('Type a number.*: ','6',regex=True)
  325. t.expect('invalid')
  326. from mmgen.mn_entry import mn_entry
  327. mne = mn_entry( cfg, fmt, entry_mode )
  328. t.expect('Type a number.*: ',str(mne.entry_modes.index(entry_mode)+1),regex=True)
  329. t.expect('Using (.+) entry mode',regex=True)
  330. mode = strip_ansi_escapes(t.p.match.group(1)).lower()
  331. assert mode == mne.em.name.lower(), f'{mode} != {mne.em.name.lower()}'
  332. stealth_mnemonic_entry(t,mne,mn,entry_mode=entry_mode)
  333. elif wcls.type == 'dieroll':
  334. user_dieroll_entry(t,mn)
  335. if usr_rand:
  336. t.expect(wcls.user_entropy_prompt,'y')
  337. t.usr_rand(10)
  338. else:
  339. t.expect(wcls.user_entropy_prompt,'n')
  340. if not usr_rand:
  341. sid_chk = 'FE3C6545'
  342. sid = t.expect_getend(f'Valid {wcls.desc} for Seed ID ')
  343. sid = strip_ansi_escapes(sid.split(',')[0])
  344. assert sid == sid_chk, f'Seed ID mismatch! {sid} != {sid_chk}'
  345. t.expect('to confirm: ','YES\n')
  346. return t
  347. def mnemonic_entry_mmgen_minimal(self):
  348. from mmgen.mn_entry import mn_entry
  349. # erase_chars: '\b\x7f'
  350. m = mn_entry( cfg, 'mmgen', 'minimal' )
  351. np = 2
  352. mn = (
  353. 'z',
  354. 'aa',
  355. '1d2ud',
  356. 'fo{}ot{}#'.format('1' * np, '2' * (m.em.pad_max - np)), # substring of 'football'
  357. 'des1p)%erate\n', # substring of 'desperately'
  358. '#t!(ie',
  359. '!)sto8o',
  360. 'the123m8!%s',
  361. '349t(5)rip',
  362. 'di\b\bdesce',
  363. 'cea',
  364. 'bu\x7f\x7fsuic',
  365. 'app\bpl',
  366. 'wd',
  367. 'busy')
  368. return self._user_seed_entry('words',entry_mode='minimal',mn=mn)
  369. def mnemonic_entry_mmgen(self): return self._user_seed_entry('words',entry_mode='full')
  370. def mnemonic_entry_bip39(self): return self._user_seed_entry('bip39',entry_mode='full')
  371. def mnemonic_entry_bip39_short(self): return self._user_seed_entry('bip39',entry_mode='short')
  372. def mn2hex_interactive_mmgen(self): return self._mn2hex('mmgen',entry_mode='full')
  373. def mn2hex_interactive_mmgen_fixed(self): return self._mn2hex('mmgen',entry_mode='fixed')
  374. def mn2hex_interactive_bip39(self): return self._mn2hex('bip39',entry_mode='full')
  375. def mn2hex_interactive_bip39_short(self): return self._mn2hex('bip39',entry_mode='short',pad_entry=True)
  376. def mn2hex_interactive_bip39_fixed(self): return self._mn2hex('bip39',entry_mode='fixed',enter_for_dfl=True)
  377. def mn2hex_interactive_xmr(self): return self._mn2hex('xmrseed',entry_mode='full')
  378. def mn2hex_interactive_xmr_short(self): return self._mn2hex('xmrseed',entry_mode='short')
  379. def dieroll_entry(self): return self._user_seed_entry('dieroll')
  380. def dieroll_entry_usrrand(self): return self._user_seed_entry('dieroll',usr_rand=True,out_fmt='bip39')