ts_autosign.py 7.6 KB

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