ts_misc.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 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. ts_misc.py: Miscellaneous test groups for the test.py test suite
  20. """
  21. from mmgen.globalvars import g
  22. from test.common import *
  23. from test.test_py_d.common import *
  24. from test.test_py_d.ts_base import *
  25. from test.test_py_d.ts_main import TestSuiteMain
  26. class TestSuiteHelp(TestSuiteBase):
  27. 'help, info and usage screens'
  28. tmpdir_nums = []
  29. passthru_opts = ('coin','testnet')
  30. cmd_group = (
  31. ('helpscreens', (1,'help screens', [])),
  32. ('longhelpscreens', (1,'help screens (--longhelp)',[])),
  33. ('opt_show_hash_presets', (1,'info screen (--show-hash-presets)',[])),
  34. ('tool_help', (1,"'mmgen-tool' usage screen",[])),
  35. ('test_help', (1,"'test.py' help screens",[])),
  36. )
  37. def helpscreens(self,
  38. arg = '--help',
  39. scripts = ( 'walletgen','walletconv','walletchk','passchg','subwalletgen',
  40. 'addrgen','keygen','passgen',
  41. 'txcreate','txsign','txsend','txdo','txbump',
  42. 'addrimport','tool','regtest','autosign')):
  43. for s in scripts:
  44. t = self._run_cmd('mmgen-'+s,[arg],extra_desc='(mmgen-{})'.format(s),no_output=True)
  45. return t
  46. def longhelpscreens(self):
  47. return self.helpscreens(arg='--longhelp')
  48. def opt_show_hash_presets(self):
  49. return self.helpscreens(
  50. arg = '--show-hash-presets',
  51. scripts = (
  52. 'walletgen','walletconv','walletchk','passchg','subwalletgen',
  53. 'addrgen','keygen','passgen',
  54. 'txsign','txdo','txbump'))
  55. def _run_cmd( self, cmd_name,
  56. cmd_args = [],
  57. no_msg = False,
  58. extra_desc = '',
  59. cmd_dir = 'cmds',
  60. no_output = False):
  61. t = self.spawn( cmd_name,
  62. args = cmd_args,
  63. no_msg = no_msg,
  64. extra_desc = extra_desc,
  65. cmd_dir = cmd_dir,
  66. no_output = no_output)
  67. t.read()
  68. ret = t.p.wait()
  69. if ret == 0:
  70. msg('OK')
  71. else:
  72. rdie(1,"\n'{}' returned {}".format(self.test_name,ret))
  73. t.skip_ok = True
  74. return t
  75. def tool_help(self):
  76. self._run_cmd('mmgen-tool',['help'],extra_desc="('mmgen-tool help')")
  77. return self._run_cmd('mmgen-tool',['usage'],extra_desc="('mmgen-tool usage')")
  78. def test_help(self):
  79. self._run_cmd('test.py',['-h'],cmd_dir='test')
  80. self._run_cmd('test.py',['-L'],cmd_dir='test',extra_desc='(cmd group list)')
  81. return self._run_cmd('test.py',['-l'],cmd_dir='test',extra_desc='(cmd list)')
  82. class TestSuiteOutput(TestSuiteBase):
  83. 'screen output tests'
  84. networks = ('btc',)
  85. tmpdir_nums = []
  86. cmd_group = (
  87. ('output_gr', (1,"Greek text", [])),
  88. ('output_ru', (1,"Russian text", [])),
  89. ('output_zh', (1,"Chinese text", [])),
  90. ('output_jp', (1,"Japanese text", []))
  91. )
  92. def screen_output(self,lang):
  93. t = self.spawn('test/misc/utf8_output.py',[lang],cmd_dir='.')
  94. t.read()
  95. return t
  96. def output_gr(self): return self.screen_output('gr')
  97. def output_ru(self): return self.screen_output('ru')
  98. def output_zh(self): return self.screen_output('zh')
  99. def output_jp(self): return self.screen_output('jp')
  100. class TestSuiteInput(TestSuiteBase):
  101. 'user input tests'
  102. networks = ('btc',)
  103. tmpdir_nums = []
  104. cmd_group = (
  105. ('password_entry_noecho', (1,"utf8 password entry", [])),
  106. ('password_entry_echo', (1,"utf8 password entry (echoed)", [])),
  107. ('mnemonic_entry', (1,"stealth mnemonic entry", [])),
  108. )
  109. def password_entry(self,prompt,cmd_args):
  110. t = self.spawn('test/misc/password_entry.py',cmd_args,cmd_dir='.')
  111. pw = 'abc-α'
  112. t.expect(prompt,pw)
  113. ret = t.expect_getend('Entered: ')
  114. assert ret == pw,'Password mismatch! {} != {}'.format(ret,pw)
  115. return t
  116. def password_entry_noecho(self):
  117. if self.skip_for_win():
  118. msg('Perform this test by hand on MSWin with non-ASCII password abc-α:')
  119. msg(' test/misc/password_entry.py')
  120. return 'skip' # getpass() can't handle utf8, and pexpect double-escapes utf8, so skip
  121. return self.password_entry('Enter passphrase: ',[])
  122. def password_entry_echo(self):
  123. if self.skip_for_win():
  124. msg('Perform this test by hand on MSWin with non-ASCII password abc-α:')
  125. msg(' test/misc/password_entry.py --echo-passphrase')
  126. return 'skip' # pexpect double-escapes utf8, so skip
  127. return self.password_entry('Enter passphrase (echoed): ',['--echo-passphrase'])
  128. def mnemonic_entry(self):
  129. mn = read_from_file(dfl_words_file).strip().split()[:12]
  130. mn = ['foo'] + mn[:5] + ['realiz','realized'] + mn[5:]
  131. t = self.spawn('mmgen-walletconv',['-S','-i','words','-o','words'])
  132. t.expect('words: ','1')
  133. t.expect('(Y/n): ','y')
  134. stealth_mnemonic_entry(t,mn)
  135. sid_chk = '5F9BC42F'
  136. sid = t.expect_getend('Valid mnemonic data for Seed ID ')[:8]
  137. assert sid == sid_chk,'Seed ID mismatch! {} != {}'.format(sid,sid_chk)
  138. t.expect('to confirm: ','YES\n')
  139. t.read()
  140. return t
  141. class TestSuiteTool(TestSuiteMain,TestSuiteBase):
  142. "tests for interactive 'mmgen-tool' commands"
  143. networks = ('btc',)
  144. segwit_opts_ok = False
  145. tmpdir_nums = [9]
  146. enc_infn = 'tool_encrypt.in'
  147. cmd_group = (
  148. ('tool_find_incog_data', (9,"'mmgen-tool find_incog_data'", [[[hincog_fn],1],[[incog_id_fn],1]])),
  149. ('tool_rand2file', (9,"'mmgen-tool rand2file'", [])),
  150. ('tool_encrypt', (9,"'mmgen-tool encrypt' (random data)", [])),
  151. ('tool_decrypt', (9,"'mmgen-tool decrypt' (random data)", [[[enc_infn+'.mmenc'],9]])),
  152. # ('tool_encrypt_ref', (9,"'mmgen-tool encrypt' (reference text)", [])),
  153. )
  154. def tool_rand2file(self):
  155. outfile = os.path.join(self.tmpdir,'rand2file.out')
  156. from mmgen.tool import MMGenToolCmd
  157. tu = MMGenToolCmd()
  158. for nbytes in ('1','1023','1K','1048575','1M','1048577','123M'):
  159. t = self.spawn( 'mmgen-tool',
  160. ['-d',self.tmpdir,'-r0','rand2file','rand2file.out',nbytes],
  161. extra_desc='({} byte{})'.format(nbytes,suf(tu.bytespec(nbytes)))
  162. )
  163. t.expect('random data written to file')
  164. t.read()
  165. t.p.wait()
  166. t.ok()
  167. t.skip_ok = True
  168. return t
  169. def tool_encrypt(self):
  170. infile = joinpath(self.tmpdir,self.enc_infn)
  171. write_to_file(infile,os.urandom(1033),binary=True)
  172. t = self.spawn('mmgen-tool',['-d',self.tmpdir,self.usr_rand_arg,'encrypt',infile])
  173. t.usr_rand(self.usr_rand_chars)
  174. t.hash_preset('user data','1')
  175. t.passphrase_new('user data',tool_enc_passwd)
  176. t.written_to_file('Encrypted data')
  177. return t
  178. def tool_decrypt(self,f1):
  179. out_fn = 'tool_encrypt.out'
  180. t = self.spawn('mmgen-tool',['-d',self.tmpdir,'decrypt',f1,'outfile='+out_fn,'hash_preset=1'])
  181. t.passphrase('user data',tool_enc_passwd)
  182. t.written_to_file('Decrypted data')
  183. d1 = self.read_from_tmpfile(self.enc_infn,binary=True)
  184. d2 = self.read_from_tmpfile(out_fn,binary=True)
  185. cmp_or_die(d1,d2)
  186. return t
  187. def tool_find_incog_data(self,f1,f2):
  188. i_id = read_from_file(f2).rstrip()
  189. vmsg('Incog ID: {}'.format(cyan(i_id)))
  190. t = self.spawn('mmgen-tool',['-d',self.tmpdir,'find_incog_data',f1,i_id])
  191. o = t.expect_getend('Incog data for ID {} found at offset '.format(i_id))
  192. if not g.platform == 'win':
  193. os.unlink(f1) # causes problems with MSYS2
  194. cmp_or_die(hincog_offset,int(o))
  195. return t
  196. class TestSuiteRefTX(TestSuiteMain,TestSuiteBase):
  197. 'create a reference transaction file (administrative command)'
  198. segwit_opts_ok = False
  199. passthru_opts = ('coin','testnet')
  200. tmpdir_nums = [31,32,33,34]
  201. cmd_group = (
  202. ('ref_tx_addrgen1', (31,'address generation (legacy)', [[[],1]])),
  203. ('ref_tx_addrgen2', (32,'address generation (compressed)', [[[],1]])),
  204. ('ref_tx_addrgen3', (33,'address generation (segwit)', [[[],1]])),
  205. ('ref_tx_addrgen4', (34,'address generation (bech32)', [[[],1]])),
  206. ('ref_tx_txcreate', (31,'transaction creation',
  207. ([['addrs'],31],[['addrs'],32],[['addrs'],33],[['addrs'],34]))),
  208. )
  209. def __init__(self,trunner,cfgs,spawn):
  210. if cfgs:
  211. for n in self.tmpdir_nums:
  212. cfgs[str(n)].update({ 'addr_idx_list': '1-2',
  213. 'segwit': n in (33,34),
  214. 'dep_generators': { 'addrs':'ref_tx_addrgen'+str(n)[-1] }})
  215. return TestSuiteMain.__init__(self,trunner,cfgs,spawn)
  216. def ref_tx_addrgen(self,atype):
  217. if atype not in g.proto.mmtypes: return
  218. t = self.spawn('mmgen-addrgen',['--outdir='+self.tmpdir,'--type='+atype,dfl_words_file,'1-2'])
  219. t.read()
  220. return t
  221. def ref_tx_addrgen1(self): return self.ref_tx_addrgen(atype='L')
  222. def ref_tx_addrgen2(self): return self.ref_tx_addrgen(atype='C')
  223. def ref_tx_addrgen3(self): return self.ref_tx_addrgen(atype='S')
  224. def ref_tx_addrgen4(self): return self.ref_tx_addrgen(atype='B')
  225. def ref_tx_txcreate(self,f1,f2,f3,f4):
  226. sources = ['31','32']
  227. if 'S' in g.proto.mmtypes: sources += ['33']
  228. if 'B' in g.proto.mmtypes: sources += ['34']
  229. return self.txcreate_common(
  230. addrs_per_wallet = 2,
  231. sources = sources,
  232. add_args = ['--locktime=1320969600'],
  233. do_label = True )