common.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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,subprocess
  22. from mmgen.common import *
  23. log_file = 'test.py.log'
  24. rt_pw = 'abc-α'
  25. ref_wallet_brainpass = 'abc'
  26. ref_wallet_hash_preset = '1'
  27. ref_wallet_incog_offset = 123
  28. dfl_seed_id = '98831F3A'
  29. dfl_addr_idx_list = '1010,500-501,31-33,1,33,500,1011'
  30. dfl_wpasswd = 'reference password'
  31. pwfile = 'passwd_file'
  32. hincog_fn = 'rand_data'
  33. hincog_bytes = 1024*1024
  34. hincog_offset = 98765
  35. hincog_seedlen = 256
  36. incog_id_fn = 'incog_id'
  37. non_mmgen_fn = 'coinkey'
  38. ref_dir = os.path.join('test','ref')
  39. dfl_words_file = os.path.join(ref_dir,'98831F3A.mmwords')
  40. from mmgen.obj import MMGenTXLabel
  41. ref_tx_label_jp = '必要なのは、信用ではなく暗号化された証明に基づく電子取引システムであり、これにより希望する二者が信用できる第三者機関を介さずに直接取引できるよう' # 72 chars ('W'ide)
  42. ref_tx_label_zh = '所以,我們非常需要這樣一種電子支付系統,它基於密碼學原理而不基於信用,使得任何達成一致的雙方,能夠直接進行支付,從而不需要協力廠商仲介的參與。。' # 72 chars ('F'ull + 'W'ide)
  43. ref_tx_label_lat_cyr_gr = ''.join(map(chr,
  44. list(range(65,91)) +
  45. list(range(1040,1072)) + # cyrillic
  46. list(range(913,939)) + # greek
  47. list(range(97,123))))[:MMGenTXLabel.max_len] # 72 chars
  48. utf8_label = ref_tx_label_zh[:40]
  49. utf8_label_pat = utf8_label
  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. def ok_msg():
  57. if opt.profile: return
  58. sys.stderr.write(green('\nOK\n') if opt.exact_output or opt.verbose else ' OK\n')
  59. def skip(name,reason=None):
  60. msg('Skipping {}{}'.format(name,' ({})'.format(reason) if reason else ''))
  61. return 'skip'
  62. def confirm_continue():
  63. if keypress_confirm(blue('Continue? (Y/n): '),default_yes=True,complete_prompt=True):
  64. if opt.verbose or opt.exact_output: sys.stderr.write('\n')
  65. else:
  66. raise KeyboardInterrupt('Exiting at user request')
  67. def omsg(s):
  68. sys.stderr.write(s + '\n')
  69. def omsg_r(s):
  70. sys.stderr.write(s)
  71. def imsg(s):
  72. if opt.exact_output or opt.verbose: omsg(s)
  73. def imsg_r(s):
  74. if opt.exact_output or opt.verbose: omsg_r(s)
  75. def iqmsg(s):
  76. if not opt.quiet: omsg(s)
  77. def iqmsg_r(s):
  78. if not opt.quiet: omsg_r(s)
  79. devnull_fh = open(('/dev/null','null.out')[g.platform == 'win'],'w')
  80. def silence():
  81. if not (opt.verbose or opt.exact_output):
  82. g.stdout = g.stderr = devnull_fh
  83. def end_silence():
  84. if not (opt.verbose or opt.exact_output):
  85. g.stdout = sys.stdout
  86. g.stderr = sys.stderr
  87. def randbool():
  88. return os.urandom(1).hex()[0] in '02468ace'
  89. def disable_debug():
  90. global save_debug
  91. save_debug = {}
  92. for k in g.env_opts:
  93. if k[:11] == 'MMGEN_DEBUG':
  94. save_debug[k] = os.getenv(k)
  95. os.environ[k] = ''
  96. def restore_debug():
  97. for k in save_debug:
  98. os.environ[k] = save_debug[k] or ''
  99. def get_file_with_ext(tdir,ext,delete=True,no_dot=False,return_list=False,delete_all=False):
  100. dot = ('.','')[bool(no_dot)]
  101. flist = [os.path.join(tdir,f) for f in os.listdir(tdir) if f == ext or f[-len(dot+ext):] == dot+ext]
  102. if not flist: return False
  103. if return_list: return flist
  104. if len(flist) > 1 or delete_all:
  105. if delete or delete_all:
  106. if not opt.quiet:
  107. msg("Multiple *.{} files in '{}' - deleting".format(ext,tdir))
  108. for f in flist:
  109. os.unlink(f)
  110. return False
  111. else:
  112. return flist[0]
  113. labels = [
  114. "Automotive",
  115. "Travel expenses",
  116. "Healthcare",
  117. ref_tx_label_jp[:40],
  118. ref_tx_label_zh[:40],
  119. "Alice's allowance",
  120. "Bob's bequest",
  121. "House purchase",
  122. "Real estate fund",
  123. "Job 1",
  124. "XYZ Corp.",
  125. "Eddie's endowment",
  126. "Emergency fund",
  127. "Real estate fund",
  128. "Ian's inheritance",
  129. "",
  130. "Rainy day",
  131. "Fred's funds",
  132. "Job 2",
  133. "Carl's capital",
  134. ]
  135. def get_label(do_shuffle=False):
  136. from random import shuffle
  137. global label_iter
  138. try:
  139. return next(label_iter)
  140. except:
  141. if do_shuffle: shuffle(labels)
  142. label_iter = iter(labels)
  143. return next(label_iter)