ct_input.py 17 KB

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