ts_misc.py 8.2 KB

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