ts_tool.py 3.8 KB

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