ct_input.py 16 KB

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