objtest.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/objtest.py: Test MMGen data objects
  20. """
  21. import sys,os
  22. pn = os.path.dirname(sys.argv[0])
  23. os.chdir(os.path.join(pn,os.pardir))
  24. sys.path.__setitem__(0,os.path.abspath(os.curdir))
  25. # Import these _after_ local path's been added to sys.path
  26. from mmgen.common import *
  27. from mmgen.obj import *
  28. from mmgen.altcoins.eth.obj import *
  29. from mmgen.seed import *
  30. opts_data = {
  31. 'sets': [('super_silent', True, 'silent', True)],
  32. 'text': {
  33. 'desc': 'Test MMGen data objects',
  34. 'usage':'[options] [object]',
  35. 'options': """
  36. -h, --help Print this help message
  37. --, --longhelp Print help message for long options (common options)
  38. -q, --quiet Produce quieter output
  39. -s, --silent Silence output of tested objects
  40. -S, --super-silent Silence all output except for errors
  41. -v, --verbose Produce more verbose output
  42. """
  43. }
  44. }
  45. cmd_args = opts.init(opts_data)
  46. def run_test(test,arg,input_data):
  47. arg_copy = arg
  48. kwargs = {'on_fail':'silent'} if opt.silent else {}
  49. ret_chk = arg
  50. if input_data == 'good' and type(arg) == tuple: arg,ret_chk = arg
  51. if type(arg) == dict: # pass one arg + kwargs to constructor
  52. arg_copy = arg.copy()
  53. if 'arg' in arg:
  54. args = [arg['arg']]
  55. ret_chk = args[0]
  56. del arg['arg']
  57. else:
  58. args = []
  59. ret_chk = list(arg.values())[0] # assume only one key present
  60. if 'ret' in arg:
  61. ret_chk = arg['ret']
  62. del arg['ret']
  63. del arg_copy['ret']
  64. kwargs.update(arg)
  65. elif type(arg) == tuple:
  66. args = arg
  67. else:
  68. args = [arg]
  69. try:
  70. if not opt.super_silent:
  71. msg_r((orange,green)[input_data=='good']('{:<22}'.format(repr(arg_copy)+':')))
  72. cls = globals()[test]
  73. ret = cls(*args,**kwargs)
  74. bad_ret = list() if issubclass(cls,list) else None
  75. if issubclass(type(ret_chk),str): ret_chk = ret_chk.encode()
  76. if issubclass(type(ret),str): ret = ret.encode()
  77. if (opt.silent and input_data=='bad' and ret!=bad_ret) or (not opt.silent and input_data=='bad'):
  78. raise UserWarning("Non-'None' return value {} with bad input data".format(repr(ret)))
  79. if opt.silent and input_data=='good' and ret==bad_ret:
  80. raise UserWarning("'None' returned with good input data")
  81. if input_data=='good' and ret != ret_chk and repr(ret) != repr(ret_chk):
  82. raise UserWarning("Return value ({!r}) doesn't match expected value ({!r})".format(ret,ret_chk))
  83. if not opt.super_silent:
  84. msg('==> {}'.format(ret))
  85. if opt.verbose and issubclass(cls,MMGenObject):
  86. ret.pmsg() if hasattr(ret,'pmsg') else pmsg(ret)
  87. except SystemExit as e:
  88. if input_data == 'good':
  89. raise ValueError('Error on good input data')
  90. if opt.verbose:
  91. msg('exitval: {}'.format(e.code))
  92. except UserWarning as e:
  93. msg('==> {!r}'.format(ret))
  94. die(2,red('{}'.format(e.args[0])))
  95. def do_loop():
  96. utests = cmd_args
  97. network = ('mainnet','testnet')[bool(g.testnet)]
  98. gl = globals()
  99. exec('from test.objtest_py_d.ot_{}_{} import tests'.format(g.coin.lower(),network),gl,gl)
  100. gmsg('Running data objest tests for {} {}'.format(g.coin,network))
  101. for test in tests:
  102. if utests and test not in utests: continue
  103. msg((blue,nocolor)[bool(opt.super_silent)]('Testing {}'.format(test)))
  104. for k in ('bad','good'):
  105. for arg in tests[test][k]:
  106. run_test(test,arg,input_data=k)
  107. do_loop()