common.py 4.3 KB

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