ts_tool.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 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_extract_key_from_geth_wallet',(9,"'mmgen-tool extract_key_from_geth_wallet'", [])),
  27. ('tool_api', (9,'tool API (initialization, config methods, wif2addr)',[])),
  28. # ('tool_encrypt_ref', (9,"'mmgen-tool encrypt' (reference text)", [])),
  29. )
  30. def tool_rand2file(self):
  31. outfile = os.path.join(self.tmpdir,'rand2file.out')
  32. from mmgen.util import parse_bytespec
  33. for nbytes in ('1','1023','1K','1048575','1M','1048577','123M'):
  34. t = self.spawn(
  35. 'mmgen-tool',
  36. ['-d',self.tmpdir,'-r0','rand2file','rand2file.out',nbytes],
  37. extra_desc='({} byte{})'.format(
  38. nbytes,
  39. suf(parse_bytespec(nbytes)) )
  40. )
  41. t.expect('random data written to file')
  42. t.read()
  43. t.p.wait()
  44. t.ok()
  45. t.skip_ok = True
  46. return t
  47. def tool_encrypt(self):
  48. infile = joinpath(self.tmpdir,self.enc_infn)
  49. write_to_file(infile,getrand(1033),binary=True)
  50. t = self.spawn('mmgen-tool',['-d',self.tmpdir,self.usr_rand_arg,'encrypt',infile])
  51. t.usr_rand(self.usr_rand_chars)
  52. t.hash_preset('data','1')
  53. t.passphrase_new('data',tool_enc_passwd)
  54. t.written_to_file('Encrypted data')
  55. return t
  56. def tool_decrypt(self,f1):
  57. out_fn = 'tool_encrypt.out'
  58. t = self.spawn('mmgen-tool',['-d',self.tmpdir,'decrypt',f1,'outfile='+out_fn,'hash_preset=1'])
  59. t.passphrase('data',tool_enc_passwd)
  60. t.written_to_file('Decrypted data')
  61. d1 = self.read_from_tmpfile(self.enc_infn,binary=True)
  62. d2 = self.read_from_tmpfile(out_fn,binary=True)
  63. cmp_or_die(d1,d2)
  64. return t
  65. def tool_find_incog_data(self,f1,f2):
  66. i_id = read_from_file(f2).rstrip()
  67. vmsg(f'Incog ID: {cyan(i_id)}')
  68. t = self.spawn('mmgen-tool',['-d',self.tmpdir,'find_incog_data',f1,i_id])
  69. o = t.expect_getend(f'Incog data for ID {i_id} found at offset ')
  70. if not g.platform == 'win':
  71. os.unlink(f1) # causes problems with MSYS2
  72. cmp_or_die(hincog_offset,int(o))
  73. return t
  74. def tool_twview_bad_comment(self): # test correct operation of get_tw_label()
  75. os.environ['MMGEN_BOGUS_WALLET_DATA'] = joinpath(ref_dir,'bad-comment-unspent.json')
  76. t = self.spawn('mmgen-tool',['twview'])
  77. t.expect('cannot be converted to TwComment')
  78. t.req_exit_val = 2
  79. return t
  80. def tool_extract_key_from_geth_wallet(self):
  81. if opt.no_altcoin:
  82. return 'skip'
  83. fn = 'test/ref/ethereum/geth-wallet.json'
  84. key = '9627ddb68354f5e0ff45fb2da49d7a20a013b7257a83ef4adbbbd87aeaccc75e'
  85. t = self.spawn('mmgen-tool',['-d',self.tmpdir,'extract_key_from_geth_wallet',fn])
  86. t.expect('Enter passphrase: ','\n')
  87. t.expect(key)
  88. return t
  89. def tool_api(self):
  90. t = self.spawn(
  91. 'tool_api_test.py',
  92. (['no_altcoin'] if opt.no_altcoin else []),
  93. cmd_dir = 'test/misc' )
  94. t.expect('legacy.*compressed.*segwit.*bech32',regex=True)
  95. return t