ts_tool.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. # Project source code repository: https://github.com/mmgen/mmgen
  7. # Licensed according to the terms of GPL Version 3. See LICENSE for details.
  8. """
  9. ts_tool.py: tool tests for the MMGen test.py test suite
  10. """
  11. from ..include.common import *
  12. from .ts_base import *
  13. from .ts_main import TestSuiteMain
  14. class TestSuiteTool(TestSuiteMain,TestSuiteBase):
  15. "interactive 'mmgen-tool' commands"
  16. networks = ('btc',)
  17. segwit_opts_ok = False
  18. tmpdir_nums = [9]
  19. enc_infn = 'tool_encrypt.in'
  20. cmd_group = (
  21. ('tool_find_incog_data', (9,"'mmgen-tool find_incog_data'", [[[hincog_fn],1],[[incog_id_fn],1]])),
  22. ('tool_rand2file', (9,"'mmgen-tool rand2file'", [])),
  23. ('tool_encrypt', (9,"'mmgen-tool encrypt' (random data)", [])),
  24. ('tool_decrypt', (9,"'mmgen-tool decrypt' (random data)", [[[enc_infn+'.mmenc'],9]])),
  25. ('tool_twview_bad_comment',(9,"'mmgen-tool twview' (with bad comment)", [])),
  26. # ('tool_encrypt_ref', (9,"'mmgen-tool encrypt' (reference text)", [])),
  27. )
  28. def tool_rand2file(self):
  29. outfile = os.path.join(self.tmpdir,'rand2file.out')
  30. from mmgen.tool import MMGenToolCmd
  31. tu = MMGenToolCmd()
  32. for nbytes in ('1','1023','1K','1048575','1M','1048577','123M'):
  33. t = self.spawn( 'mmgen-tool',
  34. ['-d',self.tmpdir,'-r0','rand2file','rand2file.out',nbytes],
  35. extra_desc='({} byte{})'.format(nbytes,suf(tu.bytespec(nbytes)))
  36. )
  37. t.expect('random data written to file')
  38. t.read()
  39. t.p.wait()
  40. t.ok()
  41. t.skip_ok = True
  42. return t
  43. def tool_encrypt(self):
  44. infile = joinpath(self.tmpdir,self.enc_infn)
  45. write_to_file(infile,os.urandom(1033),binary=True)
  46. t = self.spawn('mmgen-tool',['-d',self.tmpdir,self.usr_rand_arg,'encrypt',infile])
  47. t.usr_rand(self.usr_rand_chars)
  48. t.hash_preset('user data','1')
  49. t.passphrase_new('user data',tool_enc_passwd)
  50. t.written_to_file('Encrypted data')
  51. return t
  52. def tool_decrypt(self,f1):
  53. out_fn = 'tool_encrypt.out'
  54. t = self.spawn('mmgen-tool',['-d',self.tmpdir,'decrypt',f1,'outfile='+out_fn,'hash_preset=1'])
  55. t.passphrase('user data',tool_enc_passwd)
  56. t.written_to_file('Decrypted data')
  57. d1 = self.read_from_tmpfile(self.enc_infn,binary=True)
  58. d2 = self.read_from_tmpfile(out_fn,binary=True)
  59. cmp_or_die(d1,d2)
  60. return t
  61. def tool_find_incog_data(self,f1,f2):
  62. i_id = read_from_file(f2).rstrip()
  63. vmsg('Incog ID: {}'.format(cyan(i_id)))
  64. t = self.spawn('mmgen-tool',['-d',self.tmpdir,'find_incog_data',f1,i_id])
  65. o = t.expect_getend('Incog data for ID {} found at offset '.format(i_id))
  66. if not g.platform == 'win':
  67. os.unlink(f1) # causes problems with MSYS2
  68. cmp_or_die(hincog_offset,int(o))
  69. return t
  70. def tool_twview_bad_comment(self): # test correct operation of get_tw_label()
  71. bw_save = os.getenv('MMGEN_BOGUS_WALLET_DATA')
  72. os.environ['MMGEN_BOGUS_WALLET_DATA'] = joinpath(ref_dir,'bad-comment-unspent.json')
  73. t = self.spawn('mmgen-tool',['twview'])
  74. if bw_save:
  75. os.environ['MMGEN_BOGUS_WALLET_DATA'] = bw_save
  76. t.read()
  77. t.req_exit_val = 2
  78. return t