ts_autosign.py 7.7 KB

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