ct_misc.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test.cmdtest_py_d.ct_misc: Miscellaneous test groups for the cmdtest.py test suite
  20. """
  21. import sys,os,re,time
  22. from mmgen.util import ymsg
  23. from ..include.common import cfg,start_test_daemons,stop_test_daemons,imsg
  24. from .common import get_file_with_ext,dfl_words_file
  25. from .ct_base import CmdTestBase
  26. from .ct_main import CmdTestMain
  27. class CmdTestMisc(CmdTestBase):
  28. 'miscellaneous tests (RPC backends, xmrwallet_txview, term)'
  29. networks = ('btc',)
  30. tmpdir_nums = [99]
  31. passthru_opts = ('daemon_data_dir','rpc_port')
  32. cmd_group = (
  33. ('rpc_backends', 'RPC backends'),
  34. ('xmrwallet_txview', "'mmgen-xmrwallet' txview"),
  35. ('xmrwallet_txlist', "'mmgen-xmrwallet' txlist"),
  36. ('coin_daemon_info', "'examples/coin-daemon-info.py'"),
  37. ('term_echo', "term.set('echo')"),
  38. ('term_cleanup', 'term.register_cleanup()'),
  39. )
  40. need_daemon = True
  41. color = True
  42. def rpc_backends(self):
  43. backends = cfg._autoset_opts['rpc_backend'][1]
  44. for b in backends:
  45. t = self.spawn_chk('mmgen-tool',[f'--rpc-backend={b}','daemon_version'],extra_desc=f'({b})')
  46. return t
  47. def xmrwallet_txview(self,op='txview'):
  48. if cfg.no_altcoin:
  49. return 'skip'
  50. files = get_file_with_ext('test/ref/monero','tx',no_dot=True,delete=False,return_list=True)
  51. t = self.spawn( 'mmgen-xmrwallet', [op] + files )
  52. res = t.read(strip_color=True)
  53. if op == 'txview':
  54. for s in (
  55. 'Amount: 0.74 XMR',
  56. 'Dest: 56VQ9M6k',
  57. ):
  58. assert s in res, f'{s} not in {res}'
  59. elif op == 'txlist':
  60. assert re.search( '3EBD06-.*D94583-.*8BFA29-', res, re.DOTALL )
  61. return t
  62. def xmrwallet_txlist(self):
  63. return self.xmrwallet_txview(op='txlist')
  64. def coin_daemon_info(self):
  65. if cfg.no_altcoin:
  66. coins = ['btc']
  67. else:
  68. coins = ['btc','ltc','eth']
  69. start_test_daemons('ltc','eth')
  70. t = self.spawn('examples/coin-daemon-info.py',coins,cmd_dir='.')
  71. for coin in coins:
  72. t.expect(coin.upper() + r'\s+mainnet\s+Up',regex=True)
  73. if cfg.pexpect_spawn:
  74. t.send('q')
  75. if not cfg.no_altcoin:
  76. stop_test_daemons('ltc','eth')
  77. return t
  78. def term_echo(self):
  79. def test_echo():
  80. t.expect('echo> ','foo\n')
  81. t.expect('foo')
  82. def test_noecho():
  83. t.expect('noecho> ','foo\n')
  84. import pexpect
  85. try:
  86. t.expect('foo')
  87. except pexpect.TIMEOUT:
  88. imsg('[input not echoed - OK]')
  89. t.send('x')
  90. if self.skip_for_win():
  91. return 'skip'
  92. t = self.spawn('test/misc/term_ni.py',['echo'],cmd_dir='.',pexpect_spawn=True,timeout=1)
  93. t.p.logfile = None
  94. t.p.logfile_read = sys.stdout if cfg.verbose or cfg.exact_output else None
  95. t.p.logfile_send = None
  96. test_noecho()
  97. test_echo()
  98. test_noecho()
  99. return t
  100. def term_cleanup(self):
  101. if self.skip_for_win():
  102. return 'skip'
  103. return self.spawn('test/misc/term_ni.py',['cleanup'],cmd_dir='.',pexpect_spawn=True)
  104. class CmdTestHelp(CmdTestBase):
  105. 'help, info and usage screens'
  106. networks = ('btc','ltc','bch','eth','xmr')
  107. passthru_opts = ('daemon_data_dir','rpc_port','coin','testnet')
  108. cmd_group = (
  109. ('usage', (1,'usage message',[])),
  110. ('version', (1,'version message',[])),
  111. ('license', (1,'license message',[])),
  112. ('helpscreens', (1,'help screens', [])),
  113. ('longhelpscreens', (1,'help screens (--longhelp)',[])),
  114. ('show_hash_presets', (1,'info screen (--show-hash-presets)',[])),
  115. ('tool_help', (1,"'mmgen-tool' usage screen",[])),
  116. ('tool_cmd_usage', (1,"'mmgen-tool' usage screen",[])),
  117. ('test_help', (1,"'cmdtest.py' help screens",[])),
  118. ('tooltest_help', (1,"'tooltest.py' help screens",[])),
  119. )
  120. def usage(self):
  121. t = self.spawn('mmgen-walletgen',['foo'])
  122. t.expect('MMGenSystemExit(1)')
  123. t.expect('USAGE: mmgen-walletgen')
  124. t.req_exit_val = 1
  125. return t
  126. def version(self):
  127. t = self.spawn('mmgen-tool',['--version'])
  128. t.expect('MMGEN-TOOL version')
  129. return t
  130. def license(self):
  131. t = self.spawn(
  132. 'mmgen-walletconv',
  133. ['--stdout','--in-fmt=hex','--out-fmt=hex'],
  134. env = {'MMGEN_NO_LICENSE':''} )
  135. t.expect('to continue: ', 'w')
  136. t.expect('TERMS AND CONDITIONS') # start of GPL text
  137. if cfg.pexpect_spawn:
  138. t.send('G')
  139. t.expect('return for a fee.') # end of GPL text
  140. if cfg.pexpect_spawn:
  141. t.send('q')
  142. t.expect('to continue: ', 'c')
  143. t.expect('data: ','beadcafe'*4 + '\n')
  144. t.expect('to confirm: ', 'YES\n')
  145. return t
  146. def spawn_chk_expect(self,*args,**kwargs):
  147. expect = kwargs.pop('expect')
  148. t = self.spawn(*args,**kwargs)
  149. t.expect(expect)
  150. if t.pexpect_spawn:
  151. time.sleep(0.4)
  152. t.send('q')
  153. t.read()
  154. t.ok()
  155. t.skip_ok = True
  156. return t
  157. def helpscreens(self,arg='--help',scripts=(),expect='USAGE:.*OPTIONS:',pager=True):
  158. scripts = list(scripts) or [s.replace('mmgen-','') for s in os.listdir('cmds')]
  159. if 'tx' not in self.proto.mmcaps:
  160. scripts = [s for s in scripts if not (s == 'regtest' or s.startswith('tx'))]
  161. if self.proto.coin not in ('BTC','XMR') and 'xmrwallet' in scripts:
  162. scripts.remove('xmrwallet')
  163. if sys.platform == 'win32' and 'autosign' in scripts:
  164. scripts.remove('autosign')
  165. for s in sorted(scripts):
  166. t = self.spawn(f'mmgen-{s}',[arg],extra_desc=f'(mmgen-{s})')
  167. t.expect(expect,regex=True)
  168. if pager and t.pexpect_spawn:
  169. time.sleep(0.2)
  170. t.send('q')
  171. t.read()
  172. t.ok()
  173. t.skip_ok = True
  174. return t
  175. def longhelpscreens(self):
  176. return self.helpscreens(arg='--longhelp',expect='USAGE:.*LONG OPTIONS:')
  177. def show_hash_presets(self):
  178. return self.helpscreens(
  179. arg = '--show-hash-presets',
  180. scripts = (
  181. 'walletgen','walletconv','walletchk','passchg','subwalletgen',
  182. 'addrgen','keygen','passgen',
  183. 'txsign','txdo','txbump'),
  184. expect = 'Available parameters.*Preset',
  185. pager = False )
  186. def tool_help(self):
  187. if os.getenv('PYTHONOPTIMIZE') == '2':
  188. ymsg('Skipping tool help with PYTHONOPTIMIZE=2 (no docstrings)')
  189. return 'skip'
  190. for arg in (
  191. 'help',
  192. 'usage',
  193. ):
  194. t = self.spawn_chk_expect(
  195. 'mmgen-tool',
  196. [arg],
  197. extra_desc = f'(mmgen-tool {arg})',
  198. expect = 'GENERAL USAGE' )
  199. return t
  200. def tool_cmd_usage(self):
  201. if os.getenv('PYTHONOPTIMIZE') == '2':
  202. ymsg('Skipping tool cmd usage with PYTHONOPTIMIZE=2 (no docstrings)')
  203. return 'skip'
  204. from mmgen.main_tool import mods
  205. for cmdlist in mods.values():
  206. for cmd in cmdlist:
  207. t = self.spawn_chk( 'mmgen-tool', ['help',cmd], extra_desc=f'({cmd})' )
  208. return t
  209. def test_help(self):
  210. for arg,expect in (
  211. ('--help','USAGE'),
  212. ('--list-cmds','AVAILABLE COMMANDS'),
  213. ('--list-cmd-groups','AVAILABLE COMMAND GROUPS')
  214. ):
  215. t = self.spawn_chk_expect(
  216. 'cmdtest.py',
  217. [arg],
  218. cmd_dir = 'test',
  219. extra_desc = f'(cmdtest.py {arg})',
  220. expect = expect )
  221. return t
  222. def tooltest_help(self):
  223. for arg,expect in (
  224. ('--list-cmds','Available commands'),
  225. ('--testing-status','Testing status')
  226. ):
  227. t = self.spawn_chk_expect(
  228. 'tooltest.py',
  229. [arg],
  230. cmd_dir = 'test',
  231. extra_desc = f'(tooltest.py {arg})',
  232. expect = expect )
  233. return t
  234. class CmdTestOutput(CmdTestBase):
  235. 'screen output'
  236. networks = ('btc',)
  237. cmd_group = (
  238. ('output_gr', (1,"Greek text", [])),
  239. ('output_ru', (1,"Russian text", [])),
  240. ('output_zh', (1,"Chinese text", [])),
  241. ('output_jp', (1,"Japanese text", [])),
  242. ('oneshot_warning', (1,"Oneshot warnings", [])),
  243. ('oneshot_warning_term', (1,"Oneshot warnings (pexpect_spawn)", []))
  244. )
  245. color = True
  246. def screen_output(self,lang):
  247. return self.spawn('test/misc/utf8_output.py',[lang],cmd_dir='.')
  248. def output_gr(self):
  249. return self.screen_output('gr')
  250. def output_ru(self):
  251. return self.screen_output('ru')
  252. def output_zh(self):
  253. return self.screen_output('zh')
  254. def output_jp(self):
  255. return self.screen_output('jp')
  256. def oneshot_warning(self,pexpect_spawn=None):
  257. t = self.spawn('test/misc/oneshot_warning.py',cmd_dir='.',pexpect_spawn=pexpect_spawn)
  258. nl = '\r\n' if sys.platform == 'win32' or t.pexpect_spawn else '\n'
  259. for s in (
  260. f'pw{nl}wg1',
  261. 'foo is experimental',
  262. 'wg2', 'The bar command is dangerous',
  263. 'wg3', 'baz variant alpha',
  264. 'wg4', 'baz variant beta',
  265. 'w1', 'foo variant alpha',
  266. 'w2', 'foo variant beta',
  267. 'w3', 'bar is experimental',
  268. 'pw',
  269. "passphrase from file 'A'",
  270. "passphrase from file 'B'",
  271. f'wg1{nl}wg2{nl}wg3{nl}wg4{nl}w1{nl}w2{nl}w3',
  272. 'pw',
  273. "passphrase from file 'A'",
  274. "passphrase from file 'B'",
  275. f'wg1{nl}wg2{nl}wg3{nl}wg4{nl}w1{nl}w2{nl}w3',
  276. 'bottom',
  277. ):
  278. t.expect(s)
  279. return t
  280. def oneshot_warning_term(self):
  281. if self.skip_for_win():
  282. return 'skip'
  283. return self.oneshot_warning(pexpect_spawn=True)
  284. class CmdTestRefTX(CmdTestMain,CmdTestBase):
  285. 'create a reference transaction file (administrative command)'
  286. segwit_opts_ok = False
  287. passthru_opts = ('daemon_data_dir','rpc_port','coin','testnet')
  288. tmpdir_nums = [31,32,33,34]
  289. need_daemon = True
  290. cmd_group = (
  291. ('ref_tx_addrgen1', (31,'address generation (legacy)', [[[],1]])),
  292. ('ref_tx_addrgen2', (32,'address generation (compressed)', [[[],1]])),
  293. ('ref_tx_addrgen3', (33,'address generation (segwit)', [[[],1]])),
  294. ('ref_tx_addrgen4', (34,'address generation (bech32)', [[[],1]])),
  295. ('ref_tx_txcreate', (31,'transaction creation',
  296. ([['addrs'],31],[['addrs'],32],[['addrs'],33],[['addrs'],34]))),
  297. )
  298. def __init__(self,trunner,cfgs,spawn):
  299. if cfgs:
  300. for n in self.tmpdir_nums:
  301. cfgs[str(n)].update({ 'addr_idx_list': '1-2',
  302. 'segwit': n in (33,34),
  303. 'dep_generators': { 'addrs':'ref_tx_addrgen'+str(n)[-1] }})
  304. CmdTestMain.__init__(self,trunner,cfgs,spawn)
  305. def ref_tx_addrgen(self,atype):
  306. if atype not in self.proto.mmtypes:
  307. return
  308. return self.spawn('mmgen-addrgen',['--outdir='+self.tmpdir,'--type='+atype,dfl_words_file,'1-2'])
  309. def ref_tx_addrgen1(self):
  310. return self.ref_tx_addrgen(atype='L')
  311. def ref_tx_addrgen2(self):
  312. return self.ref_tx_addrgen(atype='C')
  313. def ref_tx_addrgen3(self):
  314. return self.ref_tx_addrgen(atype='S')
  315. def ref_tx_addrgen4(self):
  316. return self.ref_tx_addrgen(atype='B')
  317. def ref_tx_txcreate(self,f1,f2,f3,f4):
  318. sources = ['31','32']
  319. if 'S' in self.proto.mmtypes:
  320. sources += ['33']
  321. if 'B' in self.proto.mmtypes:
  322. sources += ['34']
  323. return self.txcreate_common(
  324. addrs_per_wallet = 2,
  325. sources = sources,
  326. add_args = ['--locktime=1320969600'],
  327. do_label = True)