tooltest2.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. # TODO: move all non-interactive 'mmgen-tool' tests in 'test.py' here
  22. # TODO: move all(?) tests in 'tooltest.py' here (or duplicate them?)
  23. import sys,os,time
  24. from subprocess import Popen,PIPE
  25. from decimal import Decimal
  26. repo_root = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),os.pardir)))
  27. os.chdir(repo_root)
  28. sys.path[0] = repo_root
  29. os.environ['MMGEN_TEST_SUITE'] = '1'
  30. # Import these _after_ prepending repo_root to sys.path
  31. from mmgen.common import *
  32. from mmgen.test import *
  33. from mmgen.obj import is_wif,is_coin_addr
  34. opts_data = lambda: {
  35. 'desc': "Simple test suite for the 'mmgen-tool' utility",
  36. 'usage':'[options] [command]',
  37. 'options': """
  38. -h, --help Print this help message
  39. -C, --coverage Produce code coverage info using trace module
  40. -d, --coin-dependent Run only coin-dependent tests
  41. -D, --non-coin-dependent Run only non-coin-dependent tests
  42. --, --longhelp Print help message for long options (common options)
  43. -l, --list-tests List the tests in this test suite
  44. -L, --list-tested-cmds Output the 'mmgen-tool' commands that are tested by this test suite
  45. -n, --names Print command names instead of descriptions
  46. -q, --quiet Produce quieter output
  47. -s, --system Test scripts and modules installed on system rather than
  48. those in the repo root
  49. -t, --type= Specify coin type
  50. -f, --fork Run commands via tool executable instead of importing tool module
  51. -t, --traceback Run tool inside traceback script
  52. -v, --verbose Produce more verbose output
  53. """,
  54. 'notes': """
  55. If no command is given, the whole suite of tests is run.
  56. """
  57. }
  58. tests = (
  59. ('util', 'base conversion, hashing and file utilities',
  60. (
  61. ('b58chktohex','conversion from base58chk to hex', [
  62. ( ['eFGDJPketnz'], 'deadbeef' ),
  63. ( ['5CizhNNRPYpBjrbYX'], 'deadbeefdeadbeef' ),
  64. ( ['5qCHTcgbQwprzjWrb'], 'ffffffffffffffff' ),
  65. ( ['111111114FCKVB'], '0000000000000000' ),
  66. ( ['3QJmnh'], '' ),
  67. ( ['1111111111111111111114oLvT2'], '000000000000000000000000000000000000000000' ),
  68. ]),
  69. ('hextob58chk','conversion from hex to base58chk', [
  70. ( ['deadbeef'], 'eFGDJPketnz' ),
  71. ( ['deadbeefdeadbeef'], '5CizhNNRPYpBjrbYX' ),
  72. ( ['ffffffffffffffff'], '5qCHTcgbQwprzjWrb' ),
  73. ( ['0000000000000000'], '111111114FCKVB' ),
  74. ( [''], '3QJmnh' ),
  75. ( ['000000000000000000000000000000000000000000'], '1111111111111111111114oLvT2' ),
  76. ]),
  77. ('bytespec',"conversion of 'dd'-style byte specifier to bytes", [
  78. ( ['1G'], str(1024*1024*1024) ),
  79. ( ['1234G'], str(1234*1024*1024*1024) ),
  80. ( ['1GB'], str(1000*1000*1000) ),
  81. ( ['1234GB'], str(1234*1000*1000*1000) ),
  82. ( ['1.234MB'], str(1234*1000) ),
  83. ( ['1.234567M'], str(int(Decimal('1.234567')*1024*1024)) ),
  84. ( ['1234'], str(1234) ),
  85. ]),
  86. ),
  87. ),
  88. ('wallet', 'MMGen wallet operations',
  89. (
  90. ('gen_key','generation of single key from wallet', [
  91. ( ['98831F3A:11','wallet=test/ref/98831F3A.mmwords'],
  92. '5JKLcdYbhP6QQ4BXc9HtjfqJ79FFRXP2SZTKUyEuyXJo9QSFUkv'
  93. ),
  94. ( ['98831F3A:C:11','wallet=test/ref/98831F3A.mmwords'],
  95. 'L2LwXv94XTU2HjCbJPXCFuaHjrjucGipWPWUi1hkM5EykgektyqR'
  96. ),
  97. ( ['98831F3A:B:11','wallet=test/ref/98831F3A.mmwords'],
  98. 'L2K4Y9MWb5oUfKKZtwdgCm6FLZdUiWJDHjh9BYxpEvtfcXt4iM5g'
  99. ),
  100. ( ['98831F3A:S:11','wallet=test/ref/98831F3A.mmwords'],
  101. 'KwmkkfC9GghnJhnKoRXRn5KwGCgXrCmDw6Uv83NzE4kJS5axCR9A'
  102. ),
  103. ]),
  104. ('gen_addr','generation of single address from wallet', [
  105. ( ['98831F3A:11','wallet=test/ref/98831F3A.mmwords'],
  106. '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'
  107. ),
  108. ( ['98831F3A:L:11','wallet=test/ref/98831F3A.mmwords'],
  109. '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'
  110. ),
  111. ( ['98831F3A:C:11','wallet=test/ref/98831F3A.mmwords'],
  112. '1MPsZ7BY9qikqfPxqmrovE8gLDX2rYArZk'
  113. ),
  114. ( ['98831F3A:B:11','wallet=test/ref/98831F3A.mmwords'],
  115. 'bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms'
  116. ),
  117. ( ['98831F3A:S:11','wallet=test/ref/98831F3A.mmwords'],
  118. '3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn'
  119. ),
  120. ]),
  121. ),
  122. ),
  123. ('cryptocoin', 'coin-dependent utilities',
  124. (
  125. ('randwif','random WIF key', {
  126. 'btc_mainnet': [ ( [], is_wif, ['-r0'] ) ],
  127. 'btc_testnet': [ ( [], is_wif, ['-r0'] ) ],
  128. }),
  129. ('randpair','random key/address pair', {
  130. 'btc_mainnet': [ ( [], [is_wif,is_coin_addr], ['-r0'] ) ],
  131. 'btc_testnet': [ ( [], [is_wif,is_coin_addr], ['-r0'] ) ],
  132. }),
  133. ('wif2addr','WIF-to-address conversion', {
  134. 'btc_mainnet': [
  135. ( ['5JKLcdYbhP6QQ4BXc9HtjfqJ79FFRXP2SZTKUyEuyXJo9QSFUkv'],
  136. '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm', ['--type=legacy'], 'opt.type="legacy"' ),
  137. ( ['L2LwXv94XTU2HjCbJPXCFuaHjrjucGipWPWUi1hkM5EykgektyqR'],
  138. '1MPsZ7BY9qikqfPxqmrovE8gLDX2rYArZk', ['--type=compressed'], 'opt.type="compressed"' ),
  139. ( ['KwmkkfC9GghnJhnKoRXRn5KwGCgXrCmDw6Uv83NzE4kJS5axCR9A'],
  140. '3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn', ['--type=segwit'], 'opt.type="segwit"' ),
  141. ( ['L2K4Y9MWb5oUfKKZtwdgCm6FLZdUiWJDHjh9BYxpEvtfcXt4iM5g'],
  142. 'bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms', ['--type=bech32'], 'opt.type="bech32"' ),
  143. ],
  144. }),
  145. ),
  146. ),
  147. # TODO: compressed address files are missing
  148. # 'addrfile_compressed_chk': {
  149. # 'btc': ('A33C 4FDE F515 F5BC','6C48 AA57 2056 C8C8'),
  150. # 'ltc': ('3FC0 8F03 C2D6 BD19','4C0A 49B6 2DD1 1BE0'),
  151. ('file', 'Operations with MMGen files',
  152. (
  153. ('addrfile_chksum','address file checksums', {
  154. 'btc_mainnet': [
  155. ( ['test/ref/98831F3A[1,31-33,500-501,1010-1011].addrs'],
  156. '6FEF 6FB9 7B13 5D91'),
  157. ( ['test/ref/98831F3A-S[1,31-33,500-501,1010-1011].addrs'],
  158. '06C1 9C87 F25C 4EE6'),
  159. ( ['test/ref/98831F3A-B[1,31-33,500-501,1010-1011].addrs'],
  160. '9D2A D4B6 5117 F02E'),
  161. ],
  162. 'btc_testnet': [
  163. ( ['test/ref/98831F3A[1,31-33,500-501,1010-1011].testnet.addrs'],
  164. '424E 4326 CFFE 5F51'),
  165. ( ['test/ref/98831F3A-S[1,31-33,500-501,1010-1011].testnet.addrs'],
  166. '072C 8B07 2730 CB7A'),
  167. ( ['test/ref/98831F3A-B[1,31-33,500-501,1010-1011].testnet.addrs'],
  168. '0527 9C39 6C1B E39A'),
  169. ],
  170. 'ltc_mainnet': [
  171. ( ['test/ref/litecoin/98831F3A-LTC[1,31-33,500-501,1010-1011].addrs'],
  172. 'AD52 C3FE 8924 AAF0'),
  173. ( ['test/ref/litecoin/98831F3A-LTC-S[1,31-33,500-501,1010-1011].addrs'],
  174. '63DF E42A 0827 21C3'),
  175. ( ['test/ref/litecoin/98831F3A-LTC-B[1,31-33,500-501,1010-1011].addrs'],
  176. 'FF1C 7939 5967 AB82'),
  177. ],
  178. 'ltc_testnet': [
  179. ( ['test/ref/litecoin/98831F3A-LTC[1,31-33,500-501,1010-1011].testnet.addrs'],
  180. '4EBE 2E85 E969 1B30'),
  181. ( ['test/ref/litecoin/98831F3A-LTC-S[1,31-33,500-501,1010-1011].testnet.addrs'],
  182. '5DD1 D186 DBE1 59F2'),
  183. ( ['test/ref/litecoin/98831F3A-LTC-B[1,31-33,500-501,1010-1011].testnet.addrs'],
  184. 'ED3D 8AA4 BED4 0B40'),
  185. ],
  186. 'zec_mainnet': [
  187. ( ['test/ref/zcash/98831F3A-ZEC-C[1,31-33,500-501,1010-1011].addrs'],'903E 7225 DD86 6E01'), ],
  188. 'zec_z_mainnet': [
  189. ( ['test/ref/zcash/98831F3A-ZEC-Z[1,31-33,500-501,1010-1011].addrs'],'9C7A 72DC 3D4A B3AF'), ],
  190. 'xmr_mainnet': [
  191. ( ['test/ref/monero/98831F3A-XMR-M[1,31-33,500-501,1010-1011].addrs'],'4369 0253 AC2C 0E38'), ],
  192. 'dash_mainnet': [
  193. ( ['test/ref/dash/98831F3A-DASH-C[1,31-33,500-501,1010-1011].addrs'],'FBC1 6B6A 0988 4403'), ],
  194. 'eth_mainnet': [
  195. ( ['test/ref/ethereum/98831F3A-ETH[1,31-33,500-501,1010-1011].addrs'],'E554 076E 7AF6 66A3'), ],
  196. 'etc_mainnet': [
  197. ( ['test/ref/ethereum_classic/98831F3A-ETC[1,31-33,500-501,1010-1011].addrs'],
  198. 'E97A D796 B495 E8BC'), ],
  199. }),
  200. ('txview','transaction file view', {
  201. 'btc_mainnet': [ ( ['test/ref/0B8D5A[15.31789,14,tl=1320969600].rawtx'], None ), ],
  202. 'btc_testnet': [ ( ['test/ref/0C7115[15.86255,14,tl=1320969600].testnet.rawtx'], None ), ],
  203. 'bch_mainnet': [ ( ['test/ref/460D4D-BCH[10.19764,tl=1320969600].rawtx'], None ), ],
  204. 'bch_testnet': [ ( ['test/ref/359FD5-BCH[6.68868,tl=1320969600].testnet.rawtx'], None ), ],
  205. 'ltc_mainnet': [ ( ['test/ref/litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx'], None ), ],
  206. 'ltc_testnet': [ ( ['test/ref/litecoin/A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx'],
  207. None ), ],
  208. 'eth_mainnet': [ ( ['test/ref/ethereum/88FEFD-ETH[23.45495,40000].rawtx'], None ), ],
  209. 'eth_testnet': [ ( ['test/ref/ethereum/B472BD-ETH[23.45495,40000].testnet.rawtx'], None ), ],
  210. 'mm1_mainnet': [ ( ['test/ref/ethereum/5881D2-MM1[1.23456,50000].rawtx'], None ), ],
  211. 'mm1_testnet': [ ( ['test/ref/ethereum/6BDB25-MM1[1.23456,50000].testnet.rawtx'], None ), ],
  212. 'etc_mainnet': [ ( ['test/ref/ethereum_classic/ED3848-ETC[1.2345,40000].rawtx'], None ), ],
  213. }),
  214. ),
  215. ),
  216. )
  217. def do_cmd(cdata):
  218. cmd_name,desc,data = cdata
  219. if type(data) == dict:
  220. if opt.non_coin_dependent: return
  221. k = '{}_{}net'.format((g.token.lower() if g.token else g.coin.lower()),('main','test')[g.testnet])
  222. if k in data:
  223. data = data[k]
  224. m2 = ' ({})'.format(k)
  225. else:
  226. msg("-- no data for {} ({}) - skipping".format(cmd_name,k))
  227. return
  228. else:
  229. if opt.coin_dependent: return
  230. m2 = ''
  231. m = '{} {}{}'.format(cyan('Testing'),cmd_name if opt.names else desc,m2)
  232. msg_r(green(m)+'\n' if opt.verbose else m)
  233. for d in data:
  234. args,out,opts,exec_code = d + tuple([None] * (4-len(d)))
  235. if opt.fork:
  236. cmd = list(tool_cmd) + (opts or []) + [cmd_name] + args
  237. vmsg('{} {}'.format(green('Executing'),cyan(' '.join(cmd))))
  238. p = Popen(cmd,stdout=PIPE,stderr=PIPE)
  239. cmd_out = p.stdout.read()
  240. if type(out) != bytes:
  241. cmd_out = cmd_out.strip().decode()
  242. cmd_err = p.stderr.read()
  243. if cmd_err: vmsg(cmd_err.strip().decode())
  244. if p.wait() != 0:
  245. die(1,'Spawned program exited with error')
  246. else:
  247. vmsg('{}: {}'.format(purple('Running'),' '.join([cmd_name]+args)))
  248. if exec_code: exec(exec_code)
  249. aargs,kwargs = tool._process_args(cmd_name,args)
  250. oq_save = opt.quiet
  251. if not opt.verbose: opt.quiet = True
  252. cmd_out = tool._process_result(getattr(tc,cmd_name)(*aargs,**kwargs))
  253. opt.quiet = oq_save
  254. if type(out) != bytes:
  255. cmd_out = cmd_out.strip()
  256. vmsg('Output: {}\n'.format(cmd_out))
  257. else:
  258. vmsg('Output: {}\n'.format(repr(cmd_out)))
  259. if type(out).__name__ == 'function':
  260. assert out(cmd_out),"{}({}) failed!".format(out.__name__,cmd_out)
  261. elif type(out) == list and type(out[0]).__name__ == 'function':
  262. for i in range(len(out)):
  263. s = cmd_out.split('\n')[i]
  264. assert out[i](s),"{}({}) failed!".format(out[i].__name__,s)
  265. elif out is not None:
  266. assert cmd_out == out,"Output ({}) doesn't match expected output ({})".format(cmd_out,out)
  267. if not opt.verbose: msg_r('.')
  268. if not opt.verbose:
  269. msg('OK')
  270. def do_group(garg):
  271. gid,gdesc,gdata = garg
  272. qmsg(blue("Testing {}".format("command group '{}'".format(gid) if opt.names else gdesc)))
  273. for cdata in gdata:
  274. do_cmd(cdata)
  275. def do_cmd_in_group(cmd):
  276. for g in tests:
  277. for cdata in g[2]:
  278. if cdata[0] == cmd:
  279. do_cmd(cdata)
  280. return True
  281. return False
  282. def list_tested_cmds():
  283. for g in tests:
  284. for cdata in g[2]:
  285. Msg(cdata[0])
  286. sys.argv = [sys.argv[0]] + ['--skip-cfg-file'] + sys.argv[1:]
  287. cmd_args = opts.init(opts_data)
  288. if opt.list_tests:
  289. Msg('Available commands:')
  290. for gid,gdesc,gdata in tests:
  291. Msg(' {:12} - {}'.format(gid,gdesc))
  292. sys.exit(0)
  293. if opt.list_tested_cmds:
  294. list_tested_cmds()
  295. sys.exit(0)
  296. if opt.system:
  297. tool_exec = 'mmgen-tool'
  298. sys.path.pop(0)
  299. else:
  300. os.environ['PYTHONPATH'] = repo_root
  301. tool_exec = os.path.relpath(os.path.join('cmds','mmgen-tool'))
  302. if opt.fork:
  303. tool_cmd = (tool_exec,'--skip-cfg-file')
  304. passthru_args = ['coin','type','testnet','token']
  305. tool_cmd += tuple(['--{}{}'.format(k.replace('_','-'),
  306. '='+getattr(opt,k) if getattr(opt,k) != True else ''
  307. ) for k in passthru_args if getattr(opt,k)])
  308. if opt.traceback:
  309. tool_cmd = (os.path.join('scripts','traceback_run.py'),) + tool_cmd
  310. if opt.coverage:
  311. d,f = init_coverage()
  312. tool_cmd = ('python3','-m','trace','--count','--coverdir='+d,'--file='+f) + tool_cmd
  313. elif g.platform == 'win':
  314. tool_cmd = ('python3') + tool_cmd
  315. else:
  316. opt.usr_randchars = 0
  317. import mmgen.tool as tool
  318. tc = tool.MMGenToolCmd()
  319. start_time = int(time.time())
  320. try:
  321. if cmd_args:
  322. if len(cmd_args) != 1:
  323. die(1,'Only one command may be specified')
  324. cmd = cmd_args[0]
  325. group = [e for e in tests if e[0] == cmd]
  326. if group:
  327. do_group(group[0])
  328. else:
  329. if not do_cmd_in_group(cmd):
  330. die(1,"'{}': not a recognized test or test group".format(cmd))
  331. else:
  332. for garg in tests:
  333. do_group(garg)
  334. except KeyboardInterrupt:
  335. die(1,green('\nExiting at user request'))
  336. t = int(time.time()) - start_time
  337. gmsg('All requested tests finished OK, elapsed time: {:02}:{:02}'.format(t//60,t%60))