test.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013 by 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 mmgen test suite
  20. """
  21. import sys
  22. from mmgen.utils import msg
  23. def nomsg(s): pass
  24. def test_equality(num_in,num_out,wl,quiet=False):
  25. do_msg = nomsg if quiet else msg
  26. if num_in != num_out:
  27. do_msg("WARNING! Recoded number doesn't match input stringwise!")
  28. do_msg("Input: %s" % num_in)
  29. do_msg("Output: %s" % num_out)
  30. i = num_in.lstrip(wl[0])
  31. o = num_out.lstrip(wl[0])
  32. if i != o:
  33. print "ERROR! Recoded number doesn't match input numerically!"
  34. sys.exit(9)
  35. def get_random(length):
  36. from Crypto import Random
  37. return Random.new().read(length)
  38. def process_test_args(argv, tests):
  39. if (len(argv) == 1):
  40. print "Usage: %s <test>" % argv[0].split("/")[-1]
  41. ls = "\n "
  42. print "Available tests:%s%s" % (ls,ls.join(sorted(tests.keys())))
  43. sys.exit(1)
  44. elif argv[1] not in tests:
  45. print "'%s': no such test" % argv[1]
  46. sys.exit(2)
  47. else:
  48. cargs = tests[argv[1]]
  49. uargs = argv[2:]
  50. if len(uargs) > len(cargs):
  51. print "Too many arguments\nUsage: %s(%s)" % \
  52. (argv[1], ", ".join(cargs))
  53. sys.exit()
  54. for i in range(len(cargs)):
  55. try: uarg = uargs[i]
  56. except: uarg = None
  57. cname,ctype_arg = cargs[i].split()
  58. c = ctype_arg[1:-1].split("=")[0:]
  59. ctype,cdflt = c[0],c[1:]
  60. if uarg == None and not cdflt:
  61. print "Usage: %s(%s)" % \
  62. (argv[1], ", ".join(cargs))
  63. sys.exit()
  64. # print "%-10s %-7s %s" % (uarg, cargs[i], carg)
  65. if uarg:
  66. try: eval(ctype + "('" + uarg + "')")
  67. except:
  68. print "'%s' Invalid argument (%s required)" % (uarg, ctype)
  69. sys.exit()
  70. return uargs