tool.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # Project source code repository: https://github.com/mmgen/mmgen-wallet
  7. # Licensed according to the terms of GPL Version 3. See LICENSE for details.
  8. """
  9. test.cmdtest_d.tool: tool tests for the MMGen cmdtest.py test suite
  10. """
  11. import sys, os
  12. from mmgen.util import suf
  13. from mmgen.color import cyan
  14. from ..include.common import (
  15. vmsg,
  16. read_from_file,
  17. write_to_file,
  18. cmp_or_die,
  19. joinpath,
  20. getrand
  21. )
  22. from .include.common import hincog_fn, incog_id_fn, hincog_offset, tool_enc_passwd, ref_dir
  23. from .base import CmdTestBase
  24. from .main import CmdTestMain
  25. class CmdTestTool(CmdTestMain, CmdTestBase):
  26. "interactive 'mmgen-tool' commands"
  27. networks = ('btc',)
  28. segwit_opts_ok = False
  29. tmpdir_nums = [9]
  30. enc_infn = 'tool_encrypt.in'
  31. cmd_group = (
  32. ('tool_find_incog_data',
  33. (9, '‘mmgen-tool find_incog_data’', [[[hincog_fn], 1], [[incog_id_fn], 1]])
  34. ),
  35. ('tool_rand2file',
  36. (9, '‘mmgen-tool rand2file’', [])
  37. ),
  38. ('tool_encrypt',
  39. (9, '‘mmgen-tool encrypt’ (random data)', [])
  40. ),
  41. ('tool_decrypt',
  42. (9, '‘mmgen-tool decrypt’ (random data)', [[[enc_infn+'.mmenc'], 9]])
  43. ),
  44. ('tool_twview_bad_comment',
  45. (9, '‘mmgen-tool twview’ (with bad comment)', [])
  46. ),
  47. ('tool_decrypt_keystore',
  48. (9, '‘mmgen-tool decrypt_keystore’', [])
  49. ),
  50. ('tool_decrypt_geth_keystore',
  51. (9, '‘mmgen-tool decrypt_geth_keystore’', [])
  52. ),
  53. ('tool_api',
  54. (9, 'tool API (initialization, config methods, wif2addr)', [])
  55. ),
  56. # ('tool_encrypt_ref', (9, '‘mmgen-tool encrypt’ (reference text)', [])),
  57. )
  58. def tool_rand2file(self):
  59. from mmgen.util2 import parse_bytespec
  60. for nbytes in ('1', '1023', '1K', '1048575', '1M', '1048577', '123M'):
  61. t = self.spawn(
  62. 'mmgen-tool',
  63. ['-d', self.tmpdir, '-r0', 'rand2file', 'rand2file.out', nbytes],
  64. extra_desc='({} byte{})'.format(nbytes, suf(parse_bytespec(nbytes)))
  65. )
  66. t.expect('random data written to file')
  67. t.read()
  68. t.p.wait()
  69. t.ok()
  70. t.skip_ok = True
  71. return t
  72. def tool_encrypt(self):
  73. infile = joinpath(self.tmpdir, self.enc_infn)
  74. write_to_file(infile, getrand(1033), binary=True)
  75. t = self.spawn('mmgen-tool', ['-d', self.tmpdir, self.usr_rand_arg, 'encrypt', infile])
  76. t.usr_rand(self.usr_rand_chars)
  77. t.hash_preset('data', '1')
  78. t.passphrase_new('data', tool_enc_passwd)
  79. t.written_to_file('Encrypted data')
  80. return t
  81. def tool_decrypt(self, f1):
  82. out_fn = 'tool_encrypt.out'
  83. t = self.spawn('mmgen-tool', ['-d', self.tmpdir, 'decrypt', f1, 'outfile='+out_fn, 'hash_preset=1'])
  84. t.passphrase('data', tool_enc_passwd)
  85. t.written_to_file('Decrypted data')
  86. d1 = self.read_from_tmpfile(self.enc_infn, binary=True)
  87. d2 = self.read_from_tmpfile(out_fn, binary=True)
  88. cmp_or_die(d1, d2)
  89. return t
  90. def tool_find_incog_data(self, f1, f2):
  91. i_id = read_from_file(f2).rstrip()
  92. vmsg(f'Incog ID: {cyan(i_id)}')
  93. t = self.spawn('mmgen-tool', ['-d', self.tmpdir, 'find_incog_data', f1, i_id])
  94. o = t.expect_getend(f'Incog data for ID {i_id} found at offset ')
  95. if not sys.platform == 'win32':
  96. os.unlink(f1) # causes problems with MSYS2
  97. cmp_or_die(hincog_offset, int(o))
  98. return t
  99. def tool_twview_bad_comment(self): # test correct operation of get_tw_label()
  100. t = self.spawn(
  101. 'mmgen-tool',
  102. ['twview'],
  103. env = {'MMGEN_BOGUS_UNSPENT_DATA': joinpath(ref_dir, 'bad-comment-unspent.json')},
  104. exit_val = 2)
  105. t.expect('cannot be converted to TwComment')
  106. return t
  107. def _decrypt_keystore(self, cmd, fn, pw, chk):
  108. if self.cfg.no_altcoin:
  109. return 'skip'
  110. t = self.spawn('mmgen-tool', ['-d', self.tmpdir, cmd, fn])
  111. t.expect('Enter passphrase: ', pw+'\n')
  112. t.expect(chk)
  113. return t
  114. def tool_decrypt_keystore(self):
  115. return self._decrypt_keystore(
  116. cmd = 'decrypt_keystore',
  117. fn = 'test/ref/altcoin/98831F3A-keystore-wallet.json',
  118. pw = 'abc',
  119. chk = read_from_file('test/ref/98831F3A.bip39').strip())
  120. def tool_decrypt_geth_keystore(self):
  121. return self._decrypt_keystore(
  122. cmd = 'decrypt_geth_keystore',
  123. fn = 'test/ref/ethereum/geth-wallet.json',
  124. pw = '',
  125. chk = '9627ddb68354f5e0ff45fb2da49d7a20a013b7257a83ef4adbbbd87aeaccc75e')
  126. def tool_api(self):
  127. t = self.spawn(
  128. 'tool_api_test.py',
  129. (['no_altcoin'] if self.cfg.no_altcoin else []),
  130. cmd_dir = 'test/misc')
  131. t.expect('legacy.*compressed.*segwit.*bech32', regex=True)
  132. return t