test.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2018 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. def cleandir(d):
  25. from shutil import rmtree
  26. try: files = os.listdir(d)
  27. except: return
  28. gmsg("Cleaning directory '{}'".format(d))
  29. for f in files:
  30. try:
  31. os.unlink(os.path.join(d,f))
  32. except:
  33. rmtree(os.path.join(d,f))
  34. def getrandnum(n): return int(hexlify(os.urandom(n)),16)
  35. def getrandhex(n): return hexlify(os.urandom(n))
  36. def getrandstr(num_chars,no_space=False):
  37. n,m = 95,32
  38. if no_space: n,m = 94,33
  39. return ''.join([chr(ord(i)%n+m) for i in list(os.urandom(num_chars))])
  40. def mk_tmpdir(d):
  41. try: os.mkdir(d,0755)
  42. except OSError as e:
  43. if e.errno != 17: raise
  44. else:
  45. vmsg("Created directory '{}'".format(d))
  46. def mk_tmpdir_path(path,cfg):
  47. try:
  48. name = os.path.split(cfg['tmpdir'])[-1]
  49. src = os.path.join(path,name)
  50. try:
  51. os.unlink(cfg['tmpdir'])
  52. except OSError as e:
  53. if e.errno != 2: raise
  54. finally:
  55. os.mkdir(src)
  56. os.symlink(src,cfg['tmpdir'])
  57. except OSError as e:
  58. if e.errno != 17: raise
  59. else: msg("Created directory '{}'".format(cfg['tmpdir']))
  60. def get_tmpfile_fn(cfg,fn):
  61. return os.path.join(cfg['tmpdir'],fn)
  62. def write_to_tmpfile(cfg,fn,data,binary=False):
  63. write_data_to_file(
  64. os.path.join(cfg['tmpdir'],fn),
  65. data,
  66. silent=True,
  67. binary=binary
  68. )
  69. def read_from_file(fn,binary=False):
  70. from mmgen.util import get_data_from_file
  71. return get_data_from_file(fn,silent=True,binary=binary)
  72. def read_from_tmpfile(cfg,fn,binary=False):
  73. return read_from_file(os.path.join(cfg['tmpdir'],fn),binary=binary)
  74. def ok():
  75. if opt.profile: return
  76. if opt.verbose or opt.exact_output:
  77. gmsg('OK')
  78. else: msg(' OK')
  79. def ok_or_die(val,chk_func,s,skip_ok=False):
  80. try: ret = chk_func(val)
  81. except: ret = False
  82. if ret:
  83. if not skip_ok: ok()
  84. else:
  85. rdie(3,"Returned value '{}' is not a {}".format((val,s)))
  86. def cmp_or_die(s,t,skip_ok=False):
  87. if s == t:
  88. if not skip_ok: ok()
  89. else:
  90. m = 'ERROR: recoded data:\n{}\ndiffers from original data:\n{}'
  91. rdie(3,m.format(repr(t),repr(s)))
  92. def init_coverage():
  93. coverdir = os.path.join('test','trace')
  94. acc_file = os.path.join('test','trace.acc')
  95. try: os.mkdir(coverdir,0755)
  96. except: pass
  97. return coverdir,acc_file