common.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 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.common: Shared routines and data for the cmdtest.py test suite
  20. """
  21. import sys, os
  22. from mmgen.color import green, blue, gray
  23. from mmgen.util import msg
  24. from ..include.common import cfg, getrand, text_jp, text_zh, ascii_cyr_gr, lat_cyr_gr
  25. rt_pw = 'abc-α'
  26. ref_wallet_brainpass = 'abc'
  27. ref_wallet_hash_preset = '1'
  28. ref_wallet_incog_offset = 123
  29. dfl_seed_id = '98831F3A'
  30. dfl_addr_idx_list = '1010,500-501,31-33,1,33,500,1011'
  31. dfl_wpasswd = 'reference password'
  32. pwfile = 'passwd_file'
  33. hincog_fn = 'rand_data'
  34. hincog_bytes = 1024*1024
  35. hincog_offset = 98765
  36. hincog_seedlen = 256
  37. incog_id_fn = 'incog_id'
  38. non_mmgen_fn = 'coinkey'
  39. ref_dir = os.path.join('test', 'ref')
  40. dfl_words_file = os.path.join(ref_dir, '98831F3A.mmwords')
  41. dfl_bip39_file = os.path.join(ref_dir, '98831F3A.bip39')
  42. from mmgen.obj import MMGenTxComment, TwComment
  43. tx_comment_jp = text_jp
  44. tx_comment_zh = text_zh
  45. lcg = ascii_cyr_gr if sys.platform == 'win32' else lat_cyr_gr # MSYS2 popen_spawn issue
  46. tx_comment_lat_cyr_gr = lcg[:MMGenTxComment.max_len] # 72 chars
  47. tw_comment_zh = text_zh[:TwComment.max_screen_width // 2]
  48. tw_comment_lat_cyr_gr = lcg[:TwComment.max_screen_width] # 80 chars
  49. ref_bw_hash_preset = '1'
  50. ref_bw_file = 'wallet.mmbrain'
  51. ref_bw_file_spc = 'wallet-spaced.mmbrain'
  52. ref_enc_fn = 'sample-text.mmenc'
  53. tool_enc_passwd = "Scrypt it, don't hash it!"
  54. chksum_pat = r'\b[A-F0-9]{4} [A-F0-9]{4} [A-F0-9]{4} [A-F0-9]{4}\b'
  55. Ctrl_U = '\x15'
  56. def ok_msg():
  57. if cfg.profile:
  58. return
  59. sys.stderr.write(green('\nOK\n') if cfg.exact_output or cfg.verbose else ' OK\n')
  60. def skip(name, reason=None):
  61. msg(gray('Skipping {}{}'.format(name, f' ({reason})' if reason else '')))
  62. return 'skip'
  63. def confirm_continue():
  64. from mmgen.ui import keypress_confirm
  65. if keypress_confirm(
  66. cfg,
  67. blue('Continue? (Y/n): '),
  68. default_yes = True,
  69. complete_prompt = True):
  70. if cfg.verbose or cfg.exact_output:
  71. sys.stderr.write('\n')
  72. else:
  73. raise KeyboardInterrupt('Exiting at user request')
  74. def randbool():
  75. return getrand(1).hex()[0] in '02468ace'
  76. def get_env_without_debug_vars():
  77. ret = dict(os.environ)
  78. for k in cfg._env_opts:
  79. if k[:11] == 'MMGEN_DEBUG' and k in ret:
  80. del ret[k]
  81. return ret
  82. def get_file_with_ext(
  83. tdir,
  84. ext,
  85. delete = True,
  86. no_dot = False,
  87. return_list = False,
  88. delete_all = False,
  89. substr = False):
  90. dot = '' if no_dot else '.'
  91. def have_match(fn):
  92. return (
  93. fn == ext
  94. or fn.endswith(dot + ext)
  95. or (substr and ext in fn))
  96. # Don’t use os.scandir here - it returns broken paths under Windows/MSYS2
  97. flist = [os.path.join(tdir, name) for name in os.listdir(tdir) if have_match(name)]
  98. if not flist:
  99. return False
  100. if return_list:
  101. return flist
  102. if len(flist) > 1 or delete_all:
  103. if delete or delete_all:
  104. if (cfg.exact_output or cfg.verbose) and not cfg.quiet:
  105. if delete_all:
  106. msg(f'Deleting all *{dot}{ext} files in ‘{tdir}’')
  107. else:
  108. msg(f'Multiple *{dot}{ext} files in ‘{tdir}’ - deleting')
  109. for f in flist:
  110. os.unlink(f)
  111. return False
  112. else:
  113. return flist[0]
  114. def get_comment(do_shuffle=False):
  115. labels = [
  116. 'Automotive',
  117. 'Travel expenses',
  118. 'Healthcare',
  119. tx_comment_jp[:40],
  120. tx_comment_zh[:40],
  121. 'Alice’s allowance',
  122. 'Bob’s bequest',
  123. 'House purchase',
  124. 'Real estate fund',
  125. 'Job 1',
  126. 'XYZ Corp.',
  127. 'Eddie’s endowment',
  128. 'Emergency fund',
  129. 'Real estate fund',
  130. 'Ian’s inheritance',
  131. '',
  132. 'Rainy day',
  133. 'Fred’s funds',
  134. 'Job 2',
  135. 'Carl’s capital',
  136. ]
  137. from random import shuffle
  138. global label_iter
  139. try:
  140. return next(label_iter)
  141. except:
  142. if do_shuffle:
  143. shuffle(labels)
  144. label_iter = iter(labels)
  145. return next(label_iter)