common.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 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. common.py: Shared routines and data for the test.py test suite
  20. """
  21. import os,time,subprocess
  22. from mmgen.common import *
  23. from ..common import *
  24. log_file = 'test.py.log'
  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. from mmgen.obj import MMGenTXLabel,TwComment
  42. tx_label_jp = text_jp
  43. tx_label_zh = text_zh
  44. lcg = ascii_cyr_gr if g.platform == 'win' else lat_cyr_gr # MSYS2 popen_spawn issue
  45. tx_label_lat_cyr_gr = lcg[:MMGenTXLabel.max_len] # 72 chars
  46. tw_label_zh = text_zh[:TwComment.max_screen_width // 2]
  47. tw_label_lat_cyr_gr = lcg[:TwComment.max_screen_width] # 80 chars
  48. ref_bw_hash_preset = '1'
  49. ref_bw_file = 'wallet.mmbrain'
  50. ref_bw_file_spc = 'wallet-spaced.mmbrain'
  51. ref_enc_fn = 'sample-text.mmenc'
  52. tool_enc_passwd = "Scrypt it, don't hash it!"
  53. chksum_pat = r'\b[A-F0-9]{4} [A-F0-9]{4} [A-F0-9]{4} [A-F0-9]{4}\b'
  54. def ok_msg():
  55. if opt.profile: return
  56. sys.stderr.write(green('\nOK\n') if opt.exact_output or opt.verbose else ' OK\n')
  57. def skip(name,reason=None):
  58. msg('Skipping {}{}'.format(name,' ({})'.format(reason) if reason else ''))
  59. return 'skip'
  60. def confirm_continue():
  61. if keypress_confirm(blue('Continue? (Y/n): '),default_yes=True,complete_prompt=True):
  62. if opt.verbose or opt.exact_output: sys.stderr.write('\n')
  63. else:
  64. raise KeyboardInterrupt('Exiting at user request')
  65. def omsg(s):
  66. sys.stderr.write(s + '\n')
  67. def omsg_r(s):
  68. sys.stderr.write(s)
  69. def imsg(s):
  70. if opt.exact_output or opt.verbose: omsg(s)
  71. def imsg_r(s):
  72. if opt.exact_output or opt.verbose: omsg_r(s)
  73. def iqmsg(s):
  74. if not opt.quiet: omsg(s)
  75. def iqmsg_r(s):
  76. if not opt.quiet: omsg_r(s)
  77. devnull_fh = open(('/dev/null','null.out')[g.platform == 'win'],'w')
  78. def silence():
  79. if not (opt.verbose or opt.exact_output):
  80. g.stdout = g.stderr = devnull_fh
  81. def end_silence():
  82. if not (opt.verbose or opt.exact_output):
  83. g.stdout = sys.stdout
  84. g.stderr = sys.stderr
  85. def randbool():
  86. return os.urandom(1).hex()[0] in '02468ace'
  87. def disable_debug():
  88. global save_debug
  89. save_debug = {}
  90. for k in g.env_opts:
  91. if k[:11] == 'MMGEN_DEBUG':
  92. save_debug[k] = os.getenv(k)
  93. os.environ[k] = ''
  94. def restore_debug():
  95. for k in save_debug:
  96. os.environ[k] = save_debug[k] or ''
  97. def get_file_with_ext(tdir,ext,delete=True,no_dot=False,return_list=False,delete_all=False):
  98. dot = ('.','')[bool(no_dot)]
  99. flist = [os.path.join(tdir,f) for f in os.listdir(tdir) if f == ext or f[-len(dot+ext):] == dot+ext]
  100. if not flist: return False
  101. if return_list: return flist
  102. if len(flist) > 1 or delete_all:
  103. if delete or delete_all:
  104. if not opt.quiet:
  105. msg("Multiple *.{} files in '{}' - deleting".format(ext,tdir))
  106. for f in flist:
  107. os.unlink(f)
  108. return False
  109. else:
  110. return flist[0]
  111. labels = [
  112. "Automotive",
  113. "Travel expenses",
  114. "Healthcare",
  115. tx_label_jp[:40],
  116. tx_label_zh[:40],
  117. "Alice's allowance",
  118. "Bob's bequest",
  119. "House purchase",
  120. "Real estate fund",
  121. "Job 1",
  122. "XYZ Corp.",
  123. "Eddie's endowment",
  124. "Emergency fund",
  125. "Real estate fund",
  126. "Ian's inheritance",
  127. "",
  128. "Rainy day",
  129. "Fred's funds",
  130. "Job 2",
  131. "Carl's capital",
  132. ]
  133. def get_label(do_shuffle=False):
  134. from random import shuffle
  135. global label_iter
  136. try:
  137. return next(label_iter)
  138. except:
  139. if do_shuffle: shuffle(labels)
  140. label_iter = iter(labels)
  141. return next(label_iter)
  142. def stealth_mnemonic_entry(t,mn):
  143. wnum = 1
  144. max_wordlen = 12
  145. def get_pad_chars(n):
  146. ret = ''
  147. for i in range(n):
  148. m = int(os.urandom(1).hex(),16) % 32
  149. ret += r'123579!@#$%^&*()_+-=[]{}"?/,.<>|'[m]
  150. return ret
  151. for i in range(len(mn)):
  152. w = mn[i]
  153. if len(w) > 5:
  154. w = w + '\n'
  155. else:
  156. w = get_pad_chars(3 if randbool() else 0) + w[0] + get_pad_chars(3) + w[1:] + get_pad_chars(7)
  157. w = w[:max_wordlen+1]
  158. em,rm = 'Enter word #{}: ','Repeat word #{}: '
  159. ret = t.expect((em.format(wnum),rm.format(wnum-1)))
  160. if ret == 0: wnum += 1
  161. for j in range(len(w)):
  162. t.send(w[j])
  163. time.sleep(0.005)