common.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. sample_text = 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks'
  22. ref_kafile_pass = 'kafile password'
  23. ref_kafile_hash_preset = '1'
  24. class TestSuiteException(Exception): pass
  25. class TestSuiteFatalException(Exception): pass
  26. import os
  27. from binascii import hexlify
  28. from mmgen.common import *
  29. def getrandnum(n): return int(hexlify(os.urandom(n)),16)
  30. def getrandhex(n): return hexlify(os.urandom(n)).decode()
  31. def getrandnum_range(nbytes,rn_max):
  32. while True:
  33. rn = int(hexlify(os.urandom(nbytes)),16)
  34. if rn < rn_max: return rn
  35. def getrandstr(num_chars,no_space=False):
  36. n,m = 95,32
  37. if no_space: n,m = 94,33
  38. return ''.join([chr(i%n+m) for i in list(os.urandom(num_chars))])
  39. # Windows uses non-UTF8 encodings in filesystem, so use raw bytes here
  40. def cleandir(d,do_msg=False):
  41. d_enc = d.encode()
  42. try: files = os.listdir(d_enc)
  43. except: return
  44. from shutil import rmtree
  45. if do_msg: gmsg("Cleaning directory '{}'".format(d))
  46. for f in files:
  47. try:
  48. os.unlink(os.path.join(d_enc,f))
  49. except:
  50. rmtree(os.path.join(d_enc,f))
  51. def mk_tmpdir(d):
  52. try: os.mkdir(d,0o755)
  53. except OSError as e:
  54. if e.errno != 17: raise
  55. else:
  56. vmsg("Created directory '{}'".format(d))
  57. # def mk_tmpdir_path(path,cfg):
  58. # try:
  59. # name = os.path.split(cfg['tmpdir'])[-1]
  60. # src = os.path.join(path,name)
  61. # try:
  62. # os.unlink(cfg['tmpdir'])
  63. # except OSError as e:
  64. # if e.errno != 2: raise
  65. # finally:
  66. # os.mkdir(src)
  67. # os.symlink(src,cfg['tmpdir'])
  68. # except OSError as e:
  69. # if e.errno != 17: raise
  70. # else: msg("Created directory '{}'".format(cfg['tmpdir']))
  71. def get_tmpfile(cfg,fn):
  72. return os.path.join(cfg['tmpdir'],fn)
  73. def write_to_file(fn,data,binary=False):
  74. write_data_to_file( fn,
  75. data,
  76. silent = True,
  77. binary = binary,
  78. ignore_opt_outdir = True )
  79. def write_to_tmpfile(cfg,fn,data,binary=False):
  80. write_to_file( os.path.join(cfg['tmpdir'],fn), data=data, binary=binary )
  81. def read_from_file(fn,binary=False):
  82. from mmgen.util import get_data_from_file
  83. return get_data_from_file(fn,silent=True,binary=binary)
  84. def read_from_tmpfile(cfg,fn,binary=False):
  85. return read_from_file(os.path.join(cfg['tmpdir'],fn),binary=binary)
  86. def joinpath(*args,**kwargs):
  87. return os.path.join(*args,**kwargs)
  88. def ok():
  89. if opt.profile: return
  90. if opt.verbose or opt.exact_output:
  91. gmsg('OK')
  92. else: msg(' OK')
  93. def cmp_or_die(s,t,desc=None):
  94. if s != t:
  95. m = 'ERROR: recoded data:\n{!r}\ndiffers from original data:\n{!r}'
  96. if desc: m = 'For {}:\n{}'.format(desc,m)
  97. raise TestSuiteFatalException(m.format(t,s))
  98. def init_coverage():
  99. coverdir = os.path.join('test','trace')
  100. acc_file = os.path.join('test','trace.acc')
  101. try: os.mkdir(coverdir,0o755)
  102. except: pass
  103. return coverdir,acc_file