common.py 3.5 KB

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