ts_autosign.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 read_from_file
  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. target = joinpath(mountpoint,'tx',fn)
  79. remove_signed_only or shutil.copyfile(f,target)
  80. try: os.unlink(target.replace('.rawtx','.sigtx'))
  81. except: pass
  82. # make 2 bad tx files
  83. for n in (1,2):
  84. bad_tx = joinpath(mountpoint,'tx','bad{}.rawtx'.format(n))
  85. if include_bad_tx and not remove_signed_only:
  86. open(bad_tx,'w').write('bad tx data')
  87. if not include_bad_tx:
  88. try: os.unlink(bad_tx)
  89. except: pass
  90. def do_autosign_live(opts,mountpoint,led_opts=[],gen_wallet=True):
  91. def do_mount():
  92. try: subprocess.check_call(['mount',mountpoint])
  93. except: pass
  94. def do_unmount():
  95. try: subprocess.check_call(['umount',mountpoint])
  96. except: pass
  97. omsg_r(blue('\nRemove removable device and then hit ENTER '))
  98. input()
  99. if gen_wallet: make_wallet(opts)
  100. else: do_mount()
  101. copy_files(mountpoint,include_bad_tx=not led_opts)
  102. desc = '(sign)'
  103. m1 = "Running 'mmgen-autosign wait'"
  104. m2 = 'Insert removable device '
  105. if led_opts:
  106. if led_opts == ['--led']:
  107. m1 = "Running 'mmgen-autosign wait' with --led. The LED should start blinking slowly now"
  108. elif led_opts == ['--stealth-led']:
  109. m1 = "Running 'mmgen-autosign wait' with --stealth-led. You should see no LED activity now"
  110. m2 = 'Insert removable device and watch for fast LED activity during signing'
  111. desc = '(sign - {})'.format(led_opts[0])
  112. def do_loop():
  113. omsg(blue(m2))
  114. t.expect('{} transactions signed'.format(txcount))
  115. if not led_opts:
  116. t.expect('2 transactions failed to sign')
  117. t.expect('Waiting')
  118. do_unmount()
  119. omsg(green(m1))
  120. t = self.spawn('mmgen-autosign',opts+led_opts+['wait'],extra_desc=desc)
  121. if not opt.exact_output: omsg('')
  122. do_loop()
  123. do_mount() # race condition due to device insertion detection
  124. copy_files(mountpoint,remove_signed_only=True,include_bad_tx=not led_opts)
  125. do_unmount()
  126. do_loop()
  127. t.kill(2) # 2 = SIGINT
  128. t.req_exit_val = 1
  129. return t
  130. def do_autosign(opts,mountpoint):
  131. make_wallet(opts)
  132. copy_files(mountpoint,include_bad_tx=True)
  133. t = self.spawn('mmgen-autosign',opts+['--full-summary','wait'],extra_desc='(sign - full summary)')
  134. t.expect('{} transactions signed'.format(txcount))
  135. t.expect('2 transactions failed to sign')
  136. t.expect('Waiting')
  137. t.kill(2)
  138. t.req_exit_val = 1
  139. imsg('')
  140. t.ok()
  141. copy_files(mountpoint,remove_signed_only=True)
  142. t = self.spawn('mmgen-autosign',opts+['wait'],extra_desc='(sign)')
  143. t.expect('{} transactions signed'.format(txcount))
  144. t.expect('2 transactions failed to sign')
  145. t.expect('Waiting')
  146. t.kill(2)
  147. t.req_exit_val = 1
  148. imsg('')
  149. return t
  150. if live:
  151. mountpoint = '/mnt/tx'
  152. if not os.path.ismount(mountpoint):
  153. try:
  154. subprocess.check_call(['mount',mountpoint])
  155. imsg("Mounted '{}'".format(mountpoint))
  156. except:
  157. ydie(1,"Could not mount '{}'! Exiting".format(mountpoint))
  158. txdir = joinpath(mountpoint,'tx')
  159. if not os.path.isdir(txdir):
  160. ydie(1,"Directory '{}' does not exist! Exiting".format(mountpoint))
  161. opts = ['--coins='+','.join(coins)]
  162. led_files = { 'opi': ('/sys/class/leds/orangepi:red:status/brightness',),
  163. 'rpi': ('/sys/class/leds/led0/brightness','/sys/class/leds/led0/trigger') }
  164. for k in ('opi','rpi'):
  165. if os.path.exists(led_files[k][0]):
  166. led_support = k
  167. break
  168. else:
  169. led_support = None
  170. if led_support:
  171. for fn in (led_files[led_support]):
  172. subprocess.check_call(['sudo','chmod','0666',fn])
  173. omsg(purple('Running autosign test with no LED'))
  174. do_autosign_live(opts,mountpoint)
  175. omsg(purple("Running autosign test with '--led'"))
  176. do_autosign_live(opts,mountpoint,led_opts=['--led'],gen_wallet=False)
  177. omsg(purple("Running autosign test with '--stealth-led'"))
  178. return do_autosign_live(opts,mountpoint,led_opts=['--stealth-led'],gen_wallet=False)
  179. else:
  180. return do_autosign_live(opts,mountpoint)
  181. else:
  182. mountpoint = self.tmpdir
  183. opts = ['--no-insert-check','--mountpoint='+mountpoint,'--coins='+','.join(coins)]
  184. try: os.mkdir(joinpath(mountpoint,'tx'))
  185. except: pass
  186. return do_autosign(opts,mountpoint)
  187. class TestSuiteAutosignMinimal(TestSuiteAutosign):
  188. 'autosigning with BTC, ETH and ETC'
  189. cmd_group = (
  190. ('autosign_minimal', 'transaction autosigning (BTC,ETH,ETC)'),
  191. )
  192. class TestSuiteAutosignLive(TestSuiteAutosignMinimal):
  193. 'live autosigning operations'
  194. cmd_group = (
  195. ('autosign_live', 'transaction autosigning (BTC,ETH,ETC - test device insertion/removal + LED)'),
  196. )