test.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2016 Philemon <mmgen-py@yandex.com>
  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. msg(green("Cleaning directory '%s'" % 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).lstrip('0'))
  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: msg("Created directory '%s'" % d)
  45. def mk_tmpdir_path(path,cfg):
  46. try:
  47. name = os.path.split(cfg['tmpdir'])[-1]
  48. src = os.path.join(path,name)
  49. try:
  50. os.unlink(cfg['tmpdir'])
  51. except OSError as e:
  52. if e.errno != 2: raise
  53. finally:
  54. os.mkdir(src)
  55. os.symlink(src,cfg['tmpdir'])
  56. except OSError as e:
  57. if e.errno != 17: raise
  58. else: msg("Created directory '%s'" % cfg['tmpdir'])
  59. def get_tmpfile_fn(cfg,fn):
  60. return os.path.join(cfg['tmpdir'],fn)
  61. def write_to_tmpfile(cfg,fn,data,binary=False):
  62. write_data_to_file(
  63. os.path.join(cfg['tmpdir'],fn),
  64. data,
  65. silent=True,
  66. binary=binary
  67. )
  68. def read_from_file(fn,binary=False):
  69. from mmgen.util import get_data_from_file
  70. return get_data_from_file(fn,silent=True,binary=binary)
  71. def read_from_tmpfile(cfg,fn,binary=False):
  72. return read_from_file(os.path.join(cfg['tmpdir'],fn),binary=binary)
  73. def ok():
  74. if opt.profile: return
  75. if opt.verbose or opt.exact_output:
  76. sys.stderr.write(green('OK\n'))
  77. else: msg(' OK')
  78. def ok_or_die(val,chk_func,s,skip_ok=False):
  79. try: ret = chk_func(val)
  80. except: ret = False
  81. if ret:
  82. if not skip_ok: ok()
  83. else:
  84. msg(red("Returned value '%s' is not a %s" % (val,s)))
  85. sys.exit(3)
  86. def cmp_or_die(s,t,skip_ok=False):
  87. if s == t:
  88. if not skip_ok: ok()
  89. else:
  90. sys.stderr.write(red(
  91. 'ERROR: recoded data:\n%s\ndiffers from original data:\n%s\n' %
  92. (repr(t),repr(s))))
  93. sys.exit(3)