tooltest2.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/tooltest2.py: Simple tests for the 'mmgen-tool' utility
  20. """
  21. import sys,os,time
  22. from subprocess import Popen,PIPE
  23. from decimal import Decimal
  24. repo_root = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),os.pardir)))
  25. os.chdir(repo_root)
  26. sys.path[0] = repo_root
  27. os.environ['MMGEN_TEST_SUITE'] = '1'
  28. # Import these _after_ prepending repo_root to sys.path
  29. from mmgen.common import *
  30. from mmgen.test import *
  31. opts_data = lambda: {
  32. 'desc': "Simple test suite for the 'mmgen-tool' utility",
  33. 'usage':'[options] [command]',
  34. 'options': """
  35. -h, --help Print this help message
  36. -C, --coverage Produce code coverage info using trace module
  37. --, --longhelp Print help message for long options (common options)
  38. -l, --list-tests List the tests in this test suite
  39. -L, --list-tested-cmds Output the 'mmgen-tool' commands that are tested by this test suite
  40. -n, --names Print command names instead of descriptions
  41. -s, --system Test scripts and modules installed on system rather than
  42. those in the repo root
  43. -f, --fork Run commands via tool executable instead of importing tool module
  44. -t, --traceback Run tool inside traceback script
  45. -v, --verbose Produce more verbose output
  46. """,
  47. 'notes': """
  48. If no command is given, the whole suite of tests is run.
  49. """
  50. }
  51. tests = (
  52. ('util', 'base conversion, hashing and file utilities',
  53. (
  54. ('b58chktohex','conversion from base58chk to hex',
  55. [ ( ['eFGDJPketnz'], 'deadbeef' ),
  56. ( ['5CizhNNRPYpBjrbYX'], 'deadbeefdeadbeef' ),
  57. ( ['5qCHTcgbQwprzjWrb'], 'ffffffffffffffff' ),
  58. ( ['111111114FCKVB'], '0000000000000000' ),
  59. ( ['3QJmnh'], '' ),
  60. ( ['1111111111111111111114oLvT2'], '000000000000000000000000000000000000000000' ),
  61. ]),
  62. ('hextob58chk','conversion from hex to base58chk',
  63. [ ( ['deadbeef'], 'eFGDJPketnz' ),
  64. ( ['deadbeefdeadbeef'], '5CizhNNRPYpBjrbYX' ),
  65. ( ['ffffffffffffffff'], '5qCHTcgbQwprzjWrb' ),
  66. ( ['0000000000000000'], '111111114FCKVB' ),
  67. ( [''], '3QJmnh' ),
  68. ( ['000000000000000000000000000000000000000000'], '1111111111111111111114oLvT2' ),
  69. ]),
  70. ('bytespec',"conversion of 'dd'-style byte specifier to bytes",
  71. [ ( ['1G'], str(1024*1024*1024) ),
  72. ( ['1234G'], str(1234*1024*1024*1024) ),
  73. ( ['1GB'], str(1000*1000*1000) ),
  74. ( ['1234GB'], str(1234*1000*1000*1000) ),
  75. ( ['1.234MB'], str(1234*1000) ),
  76. ( ['1.234567M'], str(int(Decimal('1.234567')*1024*1024)) ),
  77. ( ['1234'], str(1234) ),
  78. ]),
  79. ),
  80. ),
  81. ('wallet', 'MMGen wallet operations',
  82. (
  83. ('gen_key','generation of single key from wallet',
  84. [ ( ['98831F3A:11','wallet=test/ref/98831F3A.mmwords'],
  85. '5JKLcdYbhP6QQ4BXc9HtjfqJ79FFRXP2SZTKUyEuyXJo9QSFUkv'
  86. ),
  87. ( ['98831F3A:C:11','wallet=test/ref/98831F3A.mmwords'],
  88. 'L2LwXv94XTU2HjCbJPXCFuaHjrjucGipWPWUi1hkM5EykgektyqR'
  89. ),
  90. ( ['98831F3A:B:11','wallet=test/ref/98831F3A.mmwords'],
  91. 'L2K4Y9MWb5oUfKKZtwdgCm6FLZdUiWJDHjh9BYxpEvtfcXt4iM5g'
  92. ),
  93. ( ['98831F3A:S:11','wallet=test/ref/98831F3A.mmwords'],
  94. 'KwmkkfC9GghnJhnKoRXRn5KwGCgXrCmDw6Uv83NzE4kJS5axCR9A'
  95. ),]),
  96. ('gen_addr','generation of single address from wallet',
  97. [ ( ['98831F3A:11','wallet=test/ref/98831F3A.mmwords'],
  98. '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'
  99. ),
  100. ( ['98831F3A:L:11','wallet=test/ref/98831F3A.mmwords'],
  101. '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'
  102. ),
  103. ( ['98831F3A:C:11','wallet=test/ref/98831F3A.mmwords'],
  104. '1MPsZ7BY9qikqfPxqmrovE8gLDX2rYArZk'
  105. ),
  106. ( ['98831F3A:B:11','wallet=test/ref/98831F3A.mmwords'],
  107. 'bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms'
  108. ),
  109. ( ['98831F3A:S:11','wallet=test/ref/98831F3A.mmwords'],
  110. '3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn'
  111. ),]),
  112. ),
  113. ),
  114. )
  115. def do_cmd(cdata):
  116. cmd_name,desc,data = cdata
  117. m = 'Testing {}'.format(cmd_name if opt.names else desc)
  118. msg_r(green(m)+'\n' if opt.verbose else m)
  119. for args,out in data:
  120. if opt.fork:
  121. cmd = list(tool_cmd) + [cmd_name] + args
  122. vmsg('{} {}'.format(green('Executing'),cyan(' '.join(cmd))))
  123. p = Popen(cmd,stdout=PIPE,stderr=PIPE)
  124. cmd_out = p.stdout.read()
  125. cmd_err = p.stderr.read()
  126. if cmd_err: vmsg(cmd_err.strip().decode())
  127. if p.wait() != 0:
  128. die(1,'Spawned program exited with error')
  129. else:
  130. vmsg('{}: {}'.format(purple('Running'),' '.join([cmd_name]+args)))
  131. aargs,kwargs = tool._process_args(cmd_name,args)
  132. cmd_out = tool._get_result(getattr(tc,cmd_name)(*aargs,**kwargs))
  133. if type(out) == str:
  134. cmd_out = cmd_out.strip()
  135. if opt.fork:
  136. cmd_out = cmd_out.decode()
  137. vmsg('Output: {}\n'.format(cmd_out))
  138. else:
  139. vmsg('Output: {}\n'.format(repr(cmd_out)))
  140. assert cmd_out == out,"Output ({}) doesn't match expected output ({})".format(cmd_out,out)
  141. if not opt.verbose: msg_r('.')
  142. if not opt.verbose:
  143. msg('OK')
  144. def do_group(garg):
  145. gid,gdesc,gdata = garg
  146. msg(blue("Testing {}".format("command group '{}'".format(gid) if opt.names else gdesc)))
  147. for cdata in gdata:
  148. do_cmd(cdata)
  149. def do_cmd_in_group(cmd):
  150. for g in tests:
  151. for cdata in g[2]:
  152. if cdata[0] == cmd:
  153. do_cmd(cdata)
  154. return True
  155. return False
  156. def list_tested_cmds():
  157. for g in tests:
  158. for cdata in g[2]:
  159. Msg(cdata[0])
  160. sys.argv = [sys.argv[0]] + ['--skip-cfg-file'] + sys.argv[1:]
  161. cmd_args = opts.init(opts_data)
  162. if opt.list_tests:
  163. Msg('Available commands:')
  164. for gid,gdesc,gdata in tests:
  165. Msg(' {:12} - {}'.format(gid,gdesc))
  166. sys.exit(0)
  167. if opt.list_tested_cmds:
  168. list_tested_cmds()
  169. sys.exit(0)
  170. if opt.system:
  171. tool_exec = 'mmgen-tool'
  172. sys.path.pop(0)
  173. else:
  174. os.environ['PYTHONPATH'] = repo_root
  175. tool_exec = os.path.relpath(os.path.join('cmds','mmgen-tool'))
  176. if opt.fork:
  177. tool_cmd = (tool_exec,'--skip-cfg-file')
  178. if opt.traceback:
  179. tool_cmd = (os.path.join('scripts','traceback_run.py'),) + tool_cmd
  180. if opt.coverage:
  181. d,f = init_coverage()
  182. tool_cmd = ('python3','-m','trace','--count','--coverdir='+d,'--file='+f) + tool_cmd
  183. elif g.platform == 'win':
  184. tool_cmd = ('python3') + tool_cmd
  185. else:
  186. opt.quiet = True
  187. import mmgen.tool as tool
  188. tc = tool.MMGenToolCmd()
  189. start_time = int(time.time())
  190. try:
  191. if cmd_args:
  192. if len(cmd_args) != 1:
  193. die(1,'Only one command may be specified')
  194. cmd = cmd_args[0]
  195. group = [e for e in tests if e[0] == cmd]
  196. if group:
  197. do_group(group[0])
  198. else:
  199. if not do_cmd_in_group(cmd):
  200. die(1,"'{}': not a recognized test or test group".format(cmd))
  201. else:
  202. for garg in tests:
  203. do_group(garg)
  204. except KeyboardInterrupt:
  205. die(1,green('\nExiting at user request'))
  206. t = int(time.time()) - start_time
  207. gmsg('All requested tests finished OK, elapsed time: {:02}:{:02}'.format(t//60,t%60))