ts_autosign.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2020 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_autosign.py: Autosign tests for the test.py test suite
  20. """
  21. import os,shutil
  22. from subprocess import run
  23. from mmgen.globalvars import g
  24. from mmgen.opts import opt
  25. from ..include.common import *
  26. from .common import *
  27. from .ts_base import *
  28. from .ts_shared import *
  29. from .input import *
  30. class TestSuiteAutosign(TestSuiteBase):
  31. 'autosigning with BTC, BCH, LTC, ETH and ETC'
  32. networks = ('btc',)
  33. tmpdir_nums = [18]
  34. cmd_group = (
  35. ('autosign', 'transaction autosigning (BTC,BCH,LTC,ETH,ETC)'),
  36. )
  37. def autosign_live(self):
  38. return self.autosign_btc(live=True)
  39. def autosign_btc(self,live=False):
  40. return self.autosign(
  41. coins=['btc'],
  42. daemon_coins=['btc'],
  43. txfiles=['btc'],
  44. txcount=3,
  45. live=live)
  46. # tests everything except device detection, mount/unmount
  47. def autosign( self,
  48. coins=['btc','bch','ltc','eth'],
  49. daemon_coins=['btc','bch','ltc'],
  50. txfiles=['btc','bch','ltc','eth','mm1','etc'],
  51. txcount=12,
  52. live=False):
  53. if self.skip_for_win(): return 'skip'
  54. def make_wallet(opts):
  55. t = self.spawn('mmgen-autosign',opts+['gen_key'],extra_desc='(gen_key)')
  56. t.expect_getend('Wrote key file ')
  57. t.ok()
  58. t = self.spawn('mmgen-autosign',opts+['setup'],extra_desc='(setup)')
  59. t.expect('words: ','3')
  60. t.expect('OK? (Y/n): ','\n')
  61. mn_file = dfl_words_file
  62. mn = read_from_file(mn_file).strip().split()
  63. from mmgen.mn_entry import mn_entry
  64. entry_mode = 'full'
  65. mne = mn_entry('mmgen',entry_mode)
  66. t.expect('Entry mode: ',str(mne.entry_modes.index(entry_mode)+1))
  67. stealth_mnemonic_entry(t,mne,mn,entry_mode)
  68. wf = t.written_to_file('Autosign wallet')
  69. t.ok()
  70. def copy_files(mountpoint,remove_signed_only=False,include_bad_tx=True):
  71. fdata_in = (('btc',''),
  72. ('bch',''),
  73. ('ltc','litecoin'),
  74. ('eth','ethereum'),
  75. ('mm1','ethereum'),
  76. ('etc','ethereum_classic'))
  77. fdata = [e for e in fdata_in if e[0] in txfiles]
  78. from .ts_ref import TestSuiteRef
  79. tfns = [TestSuiteRef.sources['ref_tx_file'][c][1] for c,d in fdata] + \
  80. [TestSuiteRef.sources['ref_tx_file'][c][0] for c,d in fdata] + \
  81. ['25EFA3[2.34].testnet.rawtx'] # TX with 2 non-MMGen outputs
  82. tfs = [joinpath(ref_dir,d[1],fn) for d,fn in zip(fdata+fdata+[('btc','')],tfns)]
  83. for f,fn in zip(tfs,tfns):
  84. if fn: # use empty fn to skip file
  85. if g.debug_utf8:
  86. ext = '.testnet.rawtx' if fn.endswith('.testnet.rawtx') else '.rawtx'
  87. fn = fn[:-len(ext)] + '-α' + ext
  88. target = joinpath(mountpoint,'tx',fn)
  89. remove_signed_only or shutil.copyfile(f,target)
  90. try: os.unlink(target.replace('.rawtx','.sigtx'))
  91. except: pass
  92. # make 2 bad tx files
  93. for n in (1,2):
  94. bad_tx = joinpath(mountpoint,'tx','bad{}.rawtx'.format(n))
  95. if include_bad_tx and not remove_signed_only:
  96. open(bad_tx,'w').write('bad tx data')
  97. if not include_bad_tx:
  98. try: os.unlink(bad_tx)
  99. except: pass
  100. def do_autosign_live(opts,mountpoint,led_opts=[],gen_wallet=True):
  101. def do_mount():
  102. try: run(['mount',mountpoint],check=True)
  103. except: pass
  104. def do_unmount():
  105. try: run(['umount',mountpoint],check=True)
  106. except: pass
  107. omsg_r(blue('\nRemove removable device and then hit ENTER '))
  108. input()
  109. if gen_wallet: make_wallet(opts)
  110. else: do_mount()
  111. copy_files(mountpoint,include_bad_tx=not led_opts)
  112. desc = '(sign)'
  113. m1 = "Running 'mmgen-autosign wait'"
  114. m2 = 'Insert removable device '
  115. if led_opts:
  116. if led_opts == ['--led']:
  117. m1 = "Running 'mmgen-autosign wait' with --led. The LED should start blinking slowly now"
  118. elif led_opts == ['--stealth-led']:
  119. m1 = "Running 'mmgen-autosign wait' with --stealth-led. You should see no LED activity now"
  120. m2 = 'Insert removable device and watch for fast LED activity during signing'
  121. desc = '(sign - {})'.format(led_opts[0])
  122. def do_loop():
  123. omsg(blue(m2))
  124. t.expect('{} transactions signed'.format(txcount))
  125. if not led_opts:
  126. t.expect('2 transactions failed to sign')
  127. t.expect('Waiting')
  128. do_unmount()
  129. omsg(green(m1))
  130. t = self.spawn('mmgen-autosign',opts+led_opts+['wait'],extra_desc=desc)
  131. if not opt.exact_output: omsg('')
  132. do_loop()
  133. do_mount() # race condition due to device insertion detection
  134. copy_files(mountpoint,remove_signed_only=True,include_bad_tx=not led_opts)
  135. do_unmount()
  136. do_loop()
  137. t.kill(2) # 2 = SIGINT
  138. t.req_exit_val = 1
  139. return t
  140. def do_autosign(opts,mountpoint):
  141. make_wallet(opts)
  142. copy_files(mountpoint,include_bad_tx=True)
  143. t = self.spawn('mmgen-autosign',opts+['--full-summary','wait'],extra_desc='(sign - full summary)')
  144. t.expect('{} transactions signed'.format(txcount))
  145. t.expect('2 transactions failed to sign')
  146. t.expect('Waiting')
  147. t.kill(2)
  148. t.req_exit_val = 1
  149. imsg('')
  150. t.ok()
  151. copy_files(mountpoint,remove_signed_only=True)
  152. t = self.spawn('mmgen-autosign',opts+['wait'],extra_desc='(sign)')
  153. t.expect('{} transactions signed'.format(txcount))
  154. t.expect('2 transactions failed to sign')
  155. t.expect('Waiting')
  156. t.kill(2)
  157. t.req_exit_val = 1
  158. imsg('')
  159. return t
  160. network_ids = [c+'_tn' for c in daemon_coins] + daemon_coins
  161. start_test_daemons(*network_ids)
  162. if live:
  163. mountpoint = '/mnt/tx'
  164. if not os.path.ismount(mountpoint):
  165. try:
  166. run(['mount',mountpoint],check=True)
  167. imsg("Mounted '{}'".format(mountpoint))
  168. except:
  169. ydie(1,"Could not mount '{}'! Exiting".format(mountpoint))
  170. txdir = joinpath(mountpoint,'tx')
  171. if not os.path.isdir(txdir):
  172. ydie(1,"Directory '{}' does not exist! Exiting".format(mountpoint))
  173. opts = ['--coins='+','.join(coins)]
  174. led_files = { 'opi': ('/sys/class/leds/orangepi:red:status/brightness',),
  175. 'rpi': ('/sys/class/leds/led0/brightness','/sys/class/leds/led0/trigger') }
  176. for k in ('opi','rpi'):
  177. if os.path.exists(led_files[k][0]):
  178. led_support = k
  179. break
  180. else:
  181. led_support = None
  182. if led_support:
  183. for fn in (led_files[led_support]):
  184. run(['sudo','chmod','0666',fn],check=True)
  185. omsg(purple('Running autosign test with no LED'))
  186. do_autosign_live(opts,mountpoint)
  187. omsg(purple("Running autosign test with '--led'"))
  188. do_autosign_live(opts,mountpoint,led_opts=['--led'],gen_wallet=False)
  189. omsg(purple("Running autosign test with '--stealth-led'"))
  190. ret = do_autosign_live(opts,mountpoint,led_opts=['--stealth-led'],gen_wallet=False)
  191. else:
  192. ret = do_autosign_live(opts,mountpoint)
  193. else:
  194. mountpoint = self.tmpdir
  195. opts = ['--no-insert-check','--mountpoint='+mountpoint,'--coins='+','.join(coins)]
  196. try: os.mkdir(joinpath(mountpoint,'tx'))
  197. except: pass
  198. ret = do_autosign(opts,mountpoint)
  199. stop_test_daemons(*network_ids)
  200. return ret
  201. class TestSuiteAutosignBTC(TestSuiteAutosign):
  202. 'autosigning with BTC'
  203. cmd_group = (
  204. ('autosign_btc', 'transaction autosigning (BTC only)'),
  205. )
  206. class TestSuiteAutosignLive(TestSuiteAutosignBTC):
  207. 'live autosigning operations with device insertion/removal and LED check'
  208. cmd_group = (
  209. ('autosign_live', 'transaction autosigning (BTC,ETH,ETC - test device insertion/removal + LED)'),
  210. )