ct_misc.py 11 KB

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