ts_autosign.py 7.5 KB

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