base.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. # 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. test.cmdtest_d.base: Base class for the cmdtest.py test suite
  20. """
  21. import sys, os
  22. from mmgen.util import msg
  23. from mmgen.color import gray, purple, yellow
  24. from ..include.common import write_to_file, read_from_file, imsg
  25. from .include.common import get_file_with_ext
  26. class CmdTestBase:
  27. 'initializer class for the cmdtest.py test suite'
  28. base_passthru_opts = ('data_dir', 'skip_cfg_file')
  29. passthru_opts = ()
  30. networks = ()
  31. segwit_opts_ok = False
  32. color = False
  33. need_daemon = False
  34. platform_skip = ()
  35. tmpdir_nums = []
  36. skip_cmds = ()
  37. test_name = None
  38. is_helper = False
  39. def __init__(self, cfg, trunner, cfgs, spawn):
  40. if hasattr(self, 'name'): # init may called multiple times
  41. return
  42. self.name = type(self).__name__
  43. self.cfg = cfg
  44. self.proto = cfg._proto
  45. self.tr = trunner
  46. self.cfgs = cfgs
  47. self.spawn = spawn
  48. self.have_dfl_wallet = False
  49. self.usr_rand_chars = (5, 30)[bool(cfg.usr_random)]
  50. self.usr_rand_arg = f'-r{self.usr_rand_chars}'
  51. self.tn_ext = ('', '.testnet')[self.proto.testnet]
  52. self.coin = self.proto.coin.lower()
  53. self.fork = 'btc' if self.coin == 'bch' and not cfg.cashaddr else self.coin
  54. self.altcoin_pfx = '' if self.fork == 'btc' else f'-{self.proto.coin}'
  55. self.testnet_opt = ['--testnet=1'] if cfg.testnet else []
  56. if len(self.tmpdir_nums) == 1:
  57. self.tmpdir_num = self.tmpdir_nums[0]
  58. if self.tr:
  59. self.spawn_env = dict(self.tr.spawn_env)
  60. self.spawn_env['MMGEN_TEST_SUITE_ENABLE_COLOR'] = '1' if self.color else ''
  61. else:
  62. self.spawn_env = {} # placeholder
  63. def get_altcoin_pfx(self, coin, cashaddr=True):
  64. coin = coin.lower()
  65. fork = 'btc' if coin == 'bch' and not cashaddr else coin
  66. return '' if fork == 'btc' else f'-{coin.upper()}'
  67. @property
  68. def tmpdir(self):
  69. return os.path.join('test', 'tmp', '{}{}'.format(self.tmpdir_num, '-α' if self.cfg.debug_utf8 else ''))
  70. def get_file_with_ext(self, ext, **kwargs):
  71. return get_file_with_ext(self.tmpdir, ext, **kwargs)
  72. def read_from_tmpfile(self, fn, binary=False, subdir=None):
  73. tdir = os.path.join(self.tmpdir, subdir) if subdir else self.tmpdir
  74. return read_from_file(os.path.join(tdir, fn), binary=binary)
  75. def write_to_tmpfile(self, fn, data, binary=False):
  76. return write_to_file(os.path.join(self.tmpdir, fn), data, binary=binary)
  77. def delete_tmpfile(self, fn):
  78. fn = os.path.join(self.tmpdir, fn)
  79. try:
  80. return os.unlink(fn)
  81. except:
  82. msg(f'{fn}: file does not exist or could not be deleted')
  83. def skip_for_platform(self, name, extra_msg=None):
  84. if sys.platform == name:
  85. msg(gray('Skipping test {!r} for {} platform{}'.format(
  86. self.test_name,
  87. name,
  88. f' ({extra_msg})' if extra_msg else "")))
  89. return True
  90. else:
  91. return False
  92. def skip_for_mac(self, extra_msg=None):
  93. return self.skip_for_platform('darwin', extra_msg)
  94. def skip_for_win(self, extra_msg=None):
  95. return self.skip_for_platform('win32', extra_msg)
  96. def spawn_chk(self, *args, **kwargs):
  97. """
  98. Drop-in replacement for spawn() + t.read() for tests that spawn more than one process.
  99. Ensures that test script execution stops when a spawned process fails.
  100. """
  101. t = self.spawn(*args, **kwargs)
  102. t.read()
  103. t.ok()
  104. t.skip_ok = True
  105. return t
  106. def noop(self):
  107. return 'ok'
  108. def _cashaddr_opt(self, val):
  109. return [f'--cashaddr={val}'] if self.proto.coin == 'BCH' else []
  110. def _kill_process_from_pid_file(self, fn, desc):
  111. self.spawn(msg_only=True)
  112. pid = int(self.read_from_tmpfile(fn))
  113. self.delete_tmpfile(fn)
  114. from signal import SIGTERM
  115. imsg(purple(f'Killing {desc} [PID {pid}]'))
  116. try:
  117. os.kill(pid, SIGTERM)
  118. except:
  119. imsg(yellow(f'{pid}: no such process'))
  120. return 'ok'