test.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. test.py: Shared routines for the test suites
  20. """
  21. import os
  22. from binascii import hexlify
  23. from mmgen.common import *
  24. # Windows uses non-UTF8 encodings in filesystem, so use raw bytes here
  25. def cleandir(d):
  26. d_enc = d.encode()
  27. try: files = os.listdir(d_enc)
  28. except: return
  29. from shutil import rmtree
  30. gmsg("Cleaning directory '{}'".format(d))
  31. for f in files:
  32. try:
  33. os.unlink(os.path.join(d_enc,f))
  34. except:
  35. rmtree(os.path.join(d_enc,f))
  36. def getrandnum(n): return int(hexlify(os.urandom(n)),16)
  37. def getrandhex(n): return hexlify(os.urandom(n)).decode()
  38. def getrandnum_range(nbytes,rn_max):
  39. while True:
  40. rn = int(hexlify(os.urandom(nbytes)),16)
  41. if rn < rn_max: return rn
  42. def getrandstr(num_chars,no_space=False):
  43. n,m = 95,32
  44. if no_space: n,m = 94,33
  45. return ''.join([chr(i%n+m) for i in list(os.urandom(num_chars))])
  46. def mk_tmpdir(d):
  47. try: os.mkdir(d,0o755)
  48. except OSError as e:
  49. if e.errno != 17: raise
  50. else:
  51. vmsg("Created directory '{}'".format(d))
  52. def mk_tmpdir_path(path,cfg):
  53. try:
  54. name = os.path.split(cfg['tmpdir'])[-1]
  55. src = os.path.join(path,name)
  56. try:
  57. os.unlink(cfg['tmpdir'])
  58. except OSError as e:
  59. if e.errno != 2: raise
  60. finally:
  61. os.mkdir(src)
  62. os.symlink(src,cfg['tmpdir'])
  63. except OSError as e:
  64. if e.errno != 17: raise
  65. else: msg("Created directory '{}'".format(cfg['tmpdir']))
  66. def get_tmpfile_fn(cfg,fn):
  67. return os.path.join(cfg['tmpdir'],fn)
  68. def write_to_tmpfile(cfg,fn,data,binary=False):
  69. write_data_to_file(
  70. os.path.join(cfg['tmpdir'],fn),
  71. data,
  72. silent=True,
  73. binary=binary,
  74. ignore_opt_outdir=True)
  75. def read_from_file(fn,binary=False):
  76. from mmgen.util import get_data_from_file
  77. return get_data_from_file(fn,silent=True,binary=binary)
  78. def read_from_tmpfile(cfg,fn,binary=False):
  79. return read_from_file(os.path.join(cfg['tmpdir'],fn),binary=binary)
  80. def ok():
  81. if opt.profile: return
  82. if opt.verbose or opt.exact_output:
  83. gmsg('OK')
  84. else: msg(' OK')
  85. def ok_or_die(val,chk_func,s,skip_ok=False):
  86. try: ret = chk_func(val)
  87. except: ret = False
  88. if ret:
  89. if not skip_ok: ok()
  90. else:
  91. rdie(3,"Returned value '{}' is not a {}".format((val,s)))
  92. def cmp_or_die(s,t,skip_ok=False):
  93. if s == t:
  94. if not skip_ok: ok()
  95. else:
  96. m = 'ERROR: recoded data:\n{}\ndiffers from original data:\n{}'
  97. rdie(3,m.format(repr(t),repr(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