common.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 MMGen test suites
  20. """
  21. class TestSuiteException(Exception): pass
  22. class TestSuiteFatalException(Exception): pass
  23. import os
  24. from mmgen.common import *
  25. ascii_uc = ''.join(map(chr,list(range(65,91)))) # 26 chars
  26. ascii_lc = ''.join(map(chr,list(range(97,123)))) # 26 chars
  27. lat_accent = ''.join(map(chr,list(range(192,383)))) # 191 chars, L,S
  28. ru_uc = ''.join(map(chr,list(range(1040,1072)))) # 32 chars
  29. gr_uc = ''.join(map(chr,list(range(913,930)) + list(range(931,940)))) # 26 chars (930 is ctrl char)
  30. gr_uc_w_ctrl = ''.join(map(chr,list(range(913,940)))) # 27 chars, L,C
  31. lat_cyr_gr = lat_accent[:130:5] + ru_uc + gr_uc # 84 chars
  32. ascii_cyr_gr = ascii_uc + ru_uc + gr_uc # 84 chars
  33. utf8_text = '[α-$ample UTF-8 text-ω]' * 10 # 230 chars, L,N,P,S,Z
  34. utf8_combining = '[α-$ámple UTF-8 téxt-ω]' * 10 # L,N,P,S,Z,M
  35. utf8_ctrl = '[α-$ample\nUTF-8\ntext-ω]' * 10 # L,N,P,S,Z,C
  36. text_jp = '必要なのは、信用ではなく暗号化された証明に基づく電子取引システムであり、これにより希望する二者が信用できる第三者機関を介さずに直接取引できるよう' # 72 chars ('W'ide)
  37. text_zh = '所以,我們非常需要這樣一種電子支付系統,它基於密碼學原理而不基於信用,使得任何達成一致的雙方,能夠直接進行支付,從而不需要協力廠商仲介的參與。。' # 72 chars ('F'ull + 'W'ide)
  38. sample_text = 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks'
  39. ref_kafile_pass = 'kafile password'
  40. ref_kafile_hash_preset = '1'
  41. def getrandnum(n): return int(os.urandom(n).hex(),16)
  42. def getrandhex(n): return os.urandom(n).hex()
  43. def getrandnum_range(nbytes,rn_max):
  44. while True:
  45. rn = int(os.urandom(nbytes).hex(),16)
  46. if rn < rn_max: return rn
  47. def getrandstr(num_chars,no_space=False):
  48. n,m = 95,32
  49. if no_space: n,m = 94,33
  50. return ''.join([chr(i%n+m) for i in list(os.urandom(num_chars))])
  51. # Windows uses non-UTF8 encodings in filesystem, so use raw bytes here
  52. def cleandir(d,do_msg=False):
  53. d_enc = d.encode()
  54. try: files = os.listdir(d_enc)
  55. except: return
  56. from shutil import rmtree
  57. if do_msg: gmsg("Cleaning directory '{}'".format(d))
  58. for f in files:
  59. try:
  60. os.unlink(os.path.join(d_enc,f))
  61. except:
  62. rmtree(os.path.join(d_enc,f),ignore_errors=True)
  63. def mk_tmpdir(d):
  64. try: os.mkdir(d,0o755)
  65. except OSError as e:
  66. if e.errno != 17: raise
  67. else:
  68. vmsg("Created directory '{}'".format(d))
  69. # def mk_tmpdir_path(path,cfg):
  70. # try:
  71. # name = os.path.split(cfg['tmpdir'])[-1]
  72. # src = os.path.join(path,name)
  73. # try:
  74. # os.unlink(cfg['tmpdir'])
  75. # except OSError as e:
  76. # if e.errno != 2: raise
  77. # finally:
  78. # os.mkdir(src)
  79. # os.symlink(src,cfg['tmpdir'])
  80. # except OSError as e:
  81. # if e.errno != 17: raise
  82. # else: msg("Created directory '{}'".format(cfg['tmpdir']))
  83. def get_tmpfile(cfg,fn):
  84. return os.path.join(cfg['tmpdir'],fn)
  85. def write_to_file(fn,data,binary=False):
  86. write_data_to_file( fn,
  87. data,
  88. quiet = True,
  89. binary = binary,
  90. ignore_opt_outdir = True )
  91. def write_to_tmpfile(cfg,fn,data,binary=False):
  92. write_to_file( os.path.join(cfg['tmpdir'],fn), data=data, binary=binary )
  93. def read_from_file(fn,binary=False):
  94. from mmgen.util import get_data_from_file
  95. return get_data_from_file(fn,quiet=True,binary=binary)
  96. def read_from_tmpfile(cfg,fn,binary=False):
  97. return read_from_file(os.path.join(cfg['tmpdir'],fn),binary=binary)
  98. def joinpath(*args,**kwargs):
  99. return os.path.join(*args,**kwargs)
  100. def ok():
  101. if opt.profile: return
  102. if opt.verbose or opt.exact_output:
  103. gmsg('OK')
  104. else: msg(' OK')
  105. def cmp_or_die(s,t,desc=None):
  106. if s != t:
  107. m = 'ERROR: recoded data:\n{!r}\ndiffers from original data:\n{!r}'
  108. if desc: m = 'For {}:\n{}'.format(desc,m)
  109. raise TestSuiteFatalException(m.format(t,s))
  110. def init_coverage():
  111. coverdir = os.path.join('test','trace')
  112. acc_file = os.path.join('test','trace.acc')
  113. try: os.mkdir(coverdir,0o755)
  114. except: pass
  115. return coverdir,acc_file
  116. devnull_fh = open(('/dev/null','null.out')[g.platform == 'win'],'w')
  117. def silence():
  118. if not (opt.verbose or (hasattr(opt,'exact_output') and opt.exact_output)):
  119. g.stdout = g.stderr = devnull_fh
  120. def end_silence():
  121. if not (opt.verbose or (hasattr(opt,'exact_output') and opt.exact_output)):
  122. g.stdout = sys.stdout
  123. g.stderr = sys.stderr
  124. def omsg(s):
  125. sys.stderr.write(s + '\n')
  126. def omsg_r(s):
  127. sys.stderr.write(s)
  128. sys.stderr.flush()
  129. def imsg(s):
  130. if opt.verbose or (hasattr(opt,'exact_output') and opt.exact_output):
  131. omsg(s)
  132. def imsg_r(s):
  133. if opt.verbose or (hasattr(opt,'exact_output') and opt.exact_output):
  134. omsg_r(s)
  135. def iqmsg(s):
  136. if not opt.quiet: omsg(s)
  137. def iqmsg_r(s):
  138. if not opt.quiet: omsg_r(s)