tooltest2.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 run,PIPE
  25. from decimal import Decimal
  26. from tests_header import repo_root
  27. from mmgen.common import *
  28. from test.common import *
  29. from mmgen.obj import is_wif,is_coin_addr
  30. from mmgen.seed import is_bip39_mnemonic,is_mmgen_mnemonic
  31. from mmgen.baseconv import *
  32. NL = ('\n','\r\n')[g.platform=='win']
  33. def is_str(s): return type(s) == str
  34. def md5_hash(s):
  35. from hashlib import md5
  36. return md5(s.encode()).hexdigest()
  37. def md5_hash_strip(s):
  38. import re
  39. s = re.sub('\x1b\[[;0-9]+?m','',s) # strip ANSI color sequences
  40. s = s.replace(NL,'\n') # fix DOS newlines
  41. return md5_hash(s.strip())
  42. opts_data = {
  43. 'text': {
  44. 'desc': "Simple test suite for the 'mmgen-tool' utility",
  45. 'usage':'[options] [command]...',
  46. 'options': """
  47. -h, --help Print this help message
  48. -C, --coverage Produce code coverage info using trace module
  49. -d, --die-on-missing Abort if no test data found for given command
  50. --, --longhelp Print help message for long options (common options)
  51. -l, --list-tests List the test groups in this test suite
  52. -L, --list-tested-cmds Output the 'mmgen-tool' commands that are tested by this test suite
  53. -n, --names Print command names instead of descriptions
  54. -q, --quiet Produce quieter output
  55. -s, --system Test scripts and modules installed on system rather than
  56. those in the repo root
  57. -t, --type= Specify coin type
  58. -f, --fork Run commands via tool executable instead of importing tool module
  59. -t, --traceback Run tool inside traceback script
  60. -v, --verbose Produce more verbose output
  61. """,
  62. 'notes': """
  63. If no command is given, the whole suite of tests is run.
  64. """
  65. }
  66. }
  67. sample_text_hexdump = (
  68. '000000: 5468 6520 5469 6d65 7320 3033 2f4a 616e{n}' +
  69. '000010: 2f32 3030 3920 4368 616e 6365 6c6c 6f72{n}' +
  70. '000020: 206f 6e20 6272 696e 6b20 6f66 2073 6563{n}' +
  71. '000030: 6f6e 6420 6261 696c 6f75 7420 666f 7220{n}' +
  72. '000040: 6261 6e6b 73').format(n=NL)
  73. kafile_opts = ['-p1','-Ptest/ref/keyaddrfile_password']
  74. kafile_code = (
  75. "\nopt.hash_preset = '1'" +
  76. "\nopt.set_by_user = ['hash_preset']" +
  77. "\nopt.use_old_ed25519 = None" +
  78. "\nopt.passwd_file = 'test/ref/keyaddrfile_password'" )
  79. from test.unit_tests_d.ut_bip39 import unit_test as bip39
  80. tests = {
  81. 'Mnemonic': {
  82. 'hex2mn': [
  83. ( ['deadbeefdeadbeefdeadbeefdeadbeef','fmt=mmgen'],
  84. 'table cast forgive master funny gaze sadness ripple million paint moral match' ),
  85. ( ['deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'],
  86. ('swirl maybe anymore mix scale stray fog use approach page crime rhyme ' +
  87. 'class former strange window snap soon') ),
  88. ( ['deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'],
  89. ('swell type milk figure cheese phone fill black test bloom heard comfort ' +
  90. 'image terrible radio lesson own reply battle goal goodbye need laugh stream') ),
  91. ( ['ffffffffffffffffffffffffffffffff'],
  92. 'yellow yeah show bowl season spider cling defeat poison law shelter reflect' ),
  93. ( ['ffffffffffffffffffffffffffffffffffffffffffffffff'],
  94. ('yeah youth quit fail perhaps drum out person young click skin ' +
  95. 'weird inside silently perfectly together anyone memory') ),
  96. ( ['ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'],
  97. ('wrote affection object cell opinion here laughter stare honest north cost begin ' +
  98. 'murder something yourself effort acid dot doubt game broke tell guilt innocent') ),
  99. ( ['0000000000000000000000000000000000000000000000000000000000000001'],
  100. ('able able able able able able able able able able able able ' +
  101. 'able able able able able able able able able able able about') ),
  102. ] + [([a,'fmt=bip39'],b) for a,b in bip39.vectors],
  103. 'mn2hex': [
  104. ( ['table cast forgive master funny gaze sadness ripple million paint moral match','fmt=mmgen'],
  105. 'deadbeefdeadbeefdeadbeefdeadbeef' ),
  106. ( ['swirl maybe anymore mix scale stray fog use approach page crime rhyme ' +
  107. 'class former strange window snap soon'],
  108. 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'),
  109. ( ['swell type milk figure cheese phone fill black test bloom heard comfort ' +
  110. 'image terrible radio lesson own reply battle goal goodbye need laugh stream'],
  111. 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef' ),
  112. ( ['yellow yeah show bowl season spider cling defeat poison law shelter reflect'],
  113. 'ffffffffffffffffffffffffffffffff' ),
  114. ( ['yeah youth quit fail perhaps drum out person young click skin ' +
  115. 'weird inside silently perfectly together anyone memory'],
  116. 'ffffffffffffffffffffffffffffffffffffffffffffffff' ) ,
  117. ( ['wrote affection object cell opinion here laughter stare honest north cost begin ' +
  118. 'murder something yourself effort acid dot doubt game broke tell guilt innocent'],
  119. 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'),
  120. ( ['able able able able able able able able able able able able ' +
  121. 'able able able able able able able able able able able about'],
  122. '0000000000000000000000000000000000000000000000000000000000000001'),
  123. ] + [([b,'fmt=bip39'],a) for a,b in bip39.vectors],
  124. 'mn_rand128': [
  125. ( [], is_mmgen_mnemonic, ['-r0']),
  126. ( ['fmt=mmgen'], is_mmgen_mnemonic, ['-r0']),
  127. ( ['fmt=bip39'], is_bip39_mnemonic, ['-r0']),
  128. ],
  129. 'mn_rand192': [
  130. ( ['fmt=mmgen'], is_mmgen_mnemonic, ['-r0']),
  131. ( ['fmt=bip39'], is_bip39_mnemonic, ['-r0']),
  132. ],
  133. 'mn_rand256': [
  134. ( ['fmt=mmgen'], is_mmgen_mnemonic, ['-r0']),
  135. ( ['fmt=bip39'], is_bip39_mnemonic, ['-r0']),
  136. ],
  137. 'mn_stats': [
  138. ( [], is_str ),
  139. ( ['fmt=mmgen'], is_str ),
  140. ( ['fmt=bip39'], is_str ),
  141. ],
  142. 'mn_printlist': [
  143. ( [], is_str ),
  144. ( ['fmt=mmgen'], is_str ),
  145. ( ['fmt=bip39'], is_str ),
  146. ],
  147. },
  148. 'Util': {
  149. 'hextob32': [
  150. ( ['deadbeef'], 'DPK3PXP' ),
  151. ( ['deadbeefdeadbeef'], 'N5LN657PK3PXP' ),
  152. ( ['ffffffffffffffff'], 'P777777777777' ),
  153. ( ['0000000000000000'], 'A' ),
  154. ( ['0000000000000000','pad=10'], 'AAAAAAAAAA' ),
  155. ( ['ff','pad=10'], 'AAAAAAAAH7' ),
  156. ],
  157. 'b32tohex': [
  158. ( ['DPK3PXP'], 'deadbeef' ),
  159. ( ['N5LN657PK3PXP'], 'deadbeefdeadbeef' ),
  160. ( ['P777777777777'], 'ffffffffffffffff' ),
  161. ( ['A','pad=16'], '0000000000000000' ),
  162. ( ['AAAAAAAAAA','pad=16'], '0000000000000000' ),
  163. ( ['AAAAAAAAH7','pad=2'], 'ff' ),
  164. ],
  165. 'hextob58chk': [
  166. ( ['deadbeef'], 'eFGDJPketnz' ),
  167. ( ['deadbeefdeadbeef'], '5CizhNNRPYpBjrbYX' ),
  168. ( ['ffffffffffffffff'], '5qCHTcgbQwprzjWrb' ),
  169. ( ['0000000000000000'], '111111114FCKVB' ),
  170. ( ['00'], '1Wh4bh' ),
  171. ( ['000000000000000000000000000000000000000000'], '1111111111111111111114oLvT2' ),
  172. ],
  173. 'b58chktohex': [
  174. ( ['eFGDJPketnz'], 'deadbeef' ),
  175. ( ['5CizhNNRPYpBjrbYX'], 'deadbeefdeadbeef' ),
  176. ( ['5qCHTcgbQwprzjWrb'], 'ffffffffffffffff' ),
  177. ( ['111111114FCKVB'], '0000000000000000' ),
  178. ( ['3QJmnh'], '' ),
  179. ( ['1111111111111111111114oLvT2'], '000000000000000000000000000000000000000000' ),
  180. ],
  181. 'bytestob58': [
  182. ( [b'\xde\xad\xbe\xef'], '6h8cQN' ),
  183. ( [b'\xde\xad\xbe\xef\xde\xad\xbe\xef'], 'eFGDJURJykA' ),
  184. ( [b'\xff\xff\xff\xff\xff\xff\xff\xff'], 'jpXCZedGfVQ' ),
  185. ( [b'\x00\x00\x00\x00\x00\x00\x00\x00'], '1' ),
  186. ( [b'\x00\x00\x00\x00\x00\x00\x00\x00','pad=10'], '1111111111' ),
  187. ( [b'\xff','pad=10'], '111111115Q' ),
  188. ],
  189. 'b58tobytes': [
  190. ( ['6h8cQN'], b'\xde\xad\xbe\xef' ),
  191. ( ['eFGDJURJykA'], b'\xde\xad\xbe\xef\xde\xad\xbe\xef' ),
  192. ( ['jpXCZedGfVQ'], b'\xff\xff\xff\xff\xff\xff\xff\xff' ),
  193. ( ['1','pad=8'], b'\x00\x00\x00\x00\x00\x00\x00\x00' ),
  194. ( ['1111111111','pad=8'], b'\x00\x00\x00\x00\x00\x00\x00\x00' ),
  195. ( ['111111115Q','pad=1'], b'\xff' ),
  196. ],
  197. 'hextob58': [
  198. ( ['deadbeef'], '6h8cQN' ),
  199. ( ['deadbeefdeadbeef'], 'eFGDJURJykA' ),
  200. ( ['ffffffffffffffff'], 'jpXCZedGfVQ' ),
  201. ( ['0000000000000000'], '1' ),
  202. ( ['0000000000000000','pad=10'], '1111111111' ),
  203. ( ['ff','pad=10'], '111111115Q' ),
  204. ],
  205. 'b58tohex': [
  206. ( ['6h8cQN'], 'deadbeef' ),
  207. ( ['eFGDJURJykA'], 'deadbeefdeadbeef' ),
  208. ( ['jpXCZedGfVQ'], 'ffffffffffffffff' ),
  209. ( ['1','pad=16'], '0000000000000000' ),
  210. ( ['1111111111','pad=16'], '0000000000000000' ),
  211. ( ['111111115Q','pad=2'], 'ff' ),
  212. ],
  213. 'bytespec': [
  214. ( ['1G'], str(1024*1024*1024) ),
  215. ( ['1234G'], str(1234*1024*1024*1024) ),
  216. ( ['1GB'], str(1000*1000*1000) ),
  217. ( ['1234GB'], str(1234*1000*1000*1000) ),
  218. ( ['1.234MB'], str(1234*1000) ),
  219. ( ['1.234567M'], str(int(Decimal('1.234567')*1024*1024)) ),
  220. ( ['1234'], str(1234) ),
  221. ],
  222. 'hash160': [ # TODO: check that hextob58chk(hash160) = pubhex2addr
  223. ( ['deadbeef'], 'f04df4c4b30d2b7ac6e1ed2445aeb12a9cb4d2ec' ),
  224. ( ['000000000000000000000000000000000000000000'], '2db95e704e2d9b0474acf76182f3f985b7064a8a' ),
  225. ( [''], 'b472a266d0bd89c13706a4132ccfb16f7c3b9fcb' ),
  226. ( ['ffffffffffffffff'], 'f86221f5a1fca059a865c0b7d374dfa9d5f3aeb4' ),
  227. ],
  228. 'hash256': [
  229. ( ['deadbeef'], 'e107944e77a688feae4c2d4db5951923812dd0f72026a11168104ee1b248f8a9' ),
  230. ( ['000000000000000000000000000000000000000000'], 'fd5181fcd097a334ab340569e5edcd09f702fef7994abab01f4b66e86b32ebbe' ),
  231. ( [''], '5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456' ),
  232. ( ['ffffffffffffffff'], '57b2d2c3455e0f76c61c5237ff04fc9fc0f3fe691e587ea9c951949e1a5e0fed' ),
  233. ],
  234. 'hexdump': [
  235. ( [sample_text.encode()], sample_text_hexdump ),
  236. ],
  237. 'unhexdump': [
  238. ( [sample_text_hexdump.encode()], sample_text.encode() ),
  239. ],
  240. 'hexlify': [
  241. ( [b'foobar'], '666f6f626172' ),
  242. ],
  243. 'unhexlify': [
  244. ( ['666f6f626172'], 'foobar' ),
  245. ],
  246. 'hexreverse': [
  247. ( ['deadbeefcafe'], 'fecaefbeadde' ),
  248. ],
  249. 'id6': [
  250. ( [sample_text.encode()], 'a6d72b' ),
  251. ],
  252. 'id8': [
  253. ( [sample_text.encode()], '687C09C2' ),
  254. ],
  255. 'str2id6': [
  256. ( ['74ev zjeq Zw2g DspF RKpE 7H'], '70413d' ), # checked
  257. ],
  258. 'randhex': [
  259. ( [], {'boolfunc':is_hex_str,'len':64}, ['-r0'] ),
  260. ( ['nbytes=16'], {'boolfunc':is_hex_str,'len':32}, ['-r0'] ),
  261. ( ['nbytes=6'], {'boolfunc':is_hex_str,'len':12}, ['-r0'] ),
  262. ],
  263. 'randb58': [
  264. ( [], {'boolfunc':is_b58_str}, ['-r0'] ),
  265. ( ['nbytes=16'], {'boolfunc':is_b58_str}, ['-r0'] ),
  266. ( ['nbytes=12','pad=0'], is_b58_str, ['-r0'] ),
  267. ],
  268. },
  269. 'Wallet': {
  270. 'gen_key': [
  271. ( ['98831F3A:11','wallet=test/ref/98831F3A.mmwords'],
  272. '5JKLcdYbhP6QQ4BXc9HtjfqJ79FFRXP2SZTKUyEuyXJo9QSFUkv'
  273. ),
  274. ( ['98831F3A:C:11','wallet=test/ref/98831F3A.mmwords'],
  275. 'L2LwXv94XTU2HjCbJPXCFuaHjrjucGipWPWUi1hkM5EykgektyqR'
  276. ),
  277. ( ['98831F3A:B:11','wallet=test/ref/98831F3A.mmwords'],
  278. 'L2K4Y9MWb5oUfKKZtwdgCm6FLZdUiWJDHjh9BYxpEvtfcXt4iM5g'
  279. ),
  280. ( ['98831F3A:S:11','wallet=test/ref/98831F3A.mmwords'],
  281. 'KwmkkfC9GghnJhnKoRXRn5KwGCgXrCmDw6Uv83NzE4kJS5axCR9A'
  282. ),
  283. ],
  284. 'gen_addr': [
  285. ( ['98831F3A:11','wallet=test/ref/98831F3A.mmwords'],
  286. '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'
  287. ),
  288. ( ['98831F3A:L:11','wallet=test/ref/98831F3A.mmwords'],
  289. '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'
  290. ),
  291. ( ['98831F3A:C:11','wallet=test/ref/98831F3A.mmwords'],
  292. '1MPsZ7BY9qikqfPxqmrovE8gLDX2rYArZk'
  293. ),
  294. ( ['98831F3A:B:11','wallet=test/ref/98831F3A.mmwords'],
  295. 'bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms'
  296. ),
  297. ( ['98831F3A:S:11','wallet=test/ref/98831F3A.mmwords'],
  298. '3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn'
  299. ),
  300. ],
  301. 'get_subseed': [
  302. ( ['3s','wallet=test/ref/98831F3A.mmwords'], '4018EB17' ),
  303. ( ['200','wallet=test/ref/98831F3A.mmwords'], '2B05AE73' ),
  304. ],
  305. 'get_subseed_by_seed_id': [
  306. ( ['4018EB17','wallet=test/ref/98831F3A.mmwords'], '3S' ),
  307. ( ['2B05AE73','wallet=test/ref/98831F3A.mmwords'], None ),
  308. ( ['2B05AE73','wallet=test/ref/98831F3A.mmwords','last_idx=200'], '200L' ),
  309. ],
  310. 'list_subseeds': [
  311. ( ['1-5','wallet=test/ref/98831F3A.mmwords'],
  312. (md5_hash_strip,'996c047e8543d5dde6f82efc3214a6a1')
  313. ),
  314. ],
  315. 'list_shares': [
  316. ( ['3','wallet=test/ref/98831F3A.bip39'],
  317. (md5_hash_strip,'84e8bdaebf9c816a8a3bd2ebec5a2e12')
  318. ),
  319. ( ['3','id_str=default','wallet=test/ref/98831F3A.mmwords'],
  320. (md5_hash_strip,'84e8bdaebf9c816a8a3bd2ebec5a2e12')
  321. ),
  322. ( ['3','id_str=foo','wallet=test/ref/98831F3A.bip39'],
  323. (md5_hash_strip,'d2ac20823c4ea26f15234b5ca8df5d6f')
  324. ),
  325. ( ['3','id_str=foo','master_share=0','wallet=test/ref/98831F3A.mmwords'],
  326. (md5_hash_strip,'d2ac20823c4ea26f15234b5ca8df5d6f')
  327. ),
  328. ( ['3','id_str=foo','master_share=5','wallet=test/ref/98831F3A.mmwords'],
  329. (md5_hash_strip,'c4feedce40bb5959011ee4a996710832')
  330. ),
  331. ( ['3','id_str=βαρ','master_share=5','wallet=test/ref/98831F3A.mmwords'],
  332. (md5_hash_strip,'f7d254798fe2e34b94b5f4ff312998db')
  333. ),
  334. ( ['4','id_str=βαρ','master_share=5','wallet=test/ref/98831F3A.bip39'],
  335. (md5_hash_strip,'d3e479f55792181372a9f32a569c04e5')
  336. ),
  337. ],
  338. },
  339. 'Coin': {
  340. 'addr2pubhash': {
  341. 'btc_mainnet': [
  342. ( ['12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'], '118089d66b4a5853765e94923abdd5de4616c6e5' ),
  343. ( ['3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn'], '8e34586186551f6320fa3eb2d238a9c61ab8264b' ),
  344. ( ['bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms'], '3057f66ddd26fa6ef826b0d5ca067ec3e8f3c178' ),
  345. ],
  346. },
  347. 'pubhash2addr': {
  348. 'btc_mainnet': [
  349. ( ['118089d66b4a5853765e94923abdd5de4616c6e5'], '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm',
  350. None, 'opt.type="legacy"' ),
  351. ( ['8e34586186551f6320fa3eb2d238a9c61ab8264b'], '3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn',
  352. ['--type=segwit'], 'opt.type="segwit"' ),
  353. ( ['3057f66ddd26fa6ef826b0d5ca067ec3e8f3c178'], 'bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms',
  354. ['--type=bech32'], 'opt.type="bech32"' ),
  355. ],
  356. },
  357. 'addr2scriptpubkey': {
  358. 'btc_mainnet': [
  359. ( ['12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm'], '76a914118089d66b4a5853765e94923abdd5de4616c6e588ac' ),
  360. ( ['3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn'], 'a9148e34586186551f6320fa3eb2d238a9c61ab8264b87' ),
  361. ( ['bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms'], '00143057f66ddd26fa6ef826b0d5ca067ec3e8f3c178' ),
  362. ],
  363. },
  364. 'scriptpubkey2addr': {
  365. 'btc_mainnet': [
  366. ( ['76a914118089d66b4a5853765e94923abdd5de4616c6e588ac'], '12bYUGXS8SRArZneQDN9YEEYAtEa59Rykm' ),
  367. ( ['a9148e34586186551f6320fa3eb2d238a9c61ab8264b87'], '3Eevao3DRVXnYym3tdrJDqS3Wc39PQzahn' ),
  368. ( ['00143057f66ddd26fa6ef826b0d5ca067ec3e8f3c178'], 'bc1qxptlvmwaymaxa7pxkr2u5pn7c0508stcncv7ms' ),
  369. ],
  370. },
  371. 'hex2wif': {
  372. 'btc_mainnet': [
  373. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  374. '5HwzecKMWD82ppJK3qMKpC7ohXXAwcyAN5VgdJ9PLFaAzpBG4sX',
  375. None, 'opt.type="legacy"' ),
  376. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  377. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  378. ['--type=compressed'], 'opt.type="compressed"' ),
  379. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  380. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  381. ['--type=segwit'], 'opt.type="segwit"' ),
  382. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  383. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  384. ['--type=bech32'], 'opt.type="bech32"' ),
  385. ],
  386. },
  387. 'privhex2addr': {
  388. 'btc_mainnet': [
  389. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  390. '1C5VPtgq9xQ6AcTgMAR3J6GDrs72HC4pS1',
  391. None, 'opt.type="legacy"' ),
  392. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  393. '1Kz9fVSUMshzPejpzW9D95kScgA3rY6QxF',
  394. ['--type=compressed'], 'opt.type="compressed"' ),
  395. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  396. '3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg',
  397. ['--type=segwit'], 'opt.type="segwit"' ),
  398. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  399. 'bc1q6pqnfwwakuuejpm9w52ds342f9d5u36v0qnz7c',
  400. ['--type=bech32'], 'opt.type="bech32"' ),
  401. ],
  402. },
  403. 'privhex2pubhex': {
  404. 'btc_mainnet': [
  405. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  406. '044281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e972757f3254c322eeaa3cb6bf97cc5ecf8d4387b0df2c0b1e6ee18fe3a6977a7d57a',
  407. None, 'opt.type="legacy"' ),
  408. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  409. '024281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e9727',
  410. ['--type=compressed'], 'opt.type="compressed"' ),
  411. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  412. '024281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e9727',
  413. ['--type=segwit'], 'opt.type="segwit"' ),
  414. ( ['118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492'],
  415. '024281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e9727',
  416. ['--type=bech32'], 'opt.type="bech32"' ),
  417. ],
  418. },
  419. 'pubhex2addr': {
  420. 'btc_mainnet': [
  421. ( ['044281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e972757f3254c322eeaa3cb6bf97cc5ecf8d4387b0df2c0b1e6ee18fe3a6977a7d57a'],
  422. '1C5VPtgq9xQ6AcTgMAR3J6GDrs72HC4pS1',
  423. None, 'opt.type="legacy"' ),
  424. ( ['024281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e9727'],
  425. '1Kz9fVSUMshzPejpzW9D95kScgA3rY6QxF',
  426. ['--type=compressed'], 'opt.type="compressed"' ),
  427. ( ['024281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e9727'],
  428. '3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg',
  429. ['--type=segwit'], 'opt.type="segwit"' ),
  430. ( ['024281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e9727'],
  431. 'bc1q6pqnfwwakuuejpm9w52ds342f9d5u36v0qnz7c',
  432. ['--type=bech32'], 'opt.type="bech32"' ),
  433. ],
  434. },
  435. 'pubhex2redeem_script': {
  436. 'btc_mainnet': [
  437. ( ['024281a85c9ce87279e028410b851410d65136304cfbbbeaaa8e2e3931cf4e9727'],
  438. '0014d04134b9ddb7399907657514d846aa495b4e474c',
  439. ['--type=segwit'], 'opt.type="segwit"' ),
  440. ],
  441. },
  442. 'redeem_script2addr': {
  443. 'btc_mainnet': [
  444. ( ['0014d04134b9ddb7399907657514d846aa495b4e474c'],
  445. '3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg',
  446. ['--type=segwit'], 'opt.type="segwit"' ),
  447. ],
  448. },
  449. 'randpair': {
  450. 'btc_mainnet': [ ( [], [is_wif,is_coin_addr], ['-r0'] ) ],
  451. 'btc_testnet': [ ( [], [is_wif,is_coin_addr], ['-r0'] ) ],
  452. },
  453. 'randwif': {
  454. 'btc_mainnet': [ ( [], is_wif, ['-r0'] ) ],
  455. 'btc_testnet': [ ( [], is_wif, ['-r0'] ) ],
  456. },
  457. 'wif2addr': {
  458. 'btc_mainnet': [
  459. ( ['5HwzecKMWD82ppJK3qMKpC7ohXXAwcyAN5VgdJ9PLFaAzpBG4sX'],
  460. '1C5VPtgq9xQ6AcTgMAR3J6GDrs72HC4pS1', ['--type=legacy'], 'opt.type="legacy"' ),
  461. ( ['KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm'],
  462. '1Kz9fVSUMshzPejpzW9D95kScgA3rY6QxF', ['--type=compressed'], 'opt.type="compressed"' ),
  463. ( ['KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm'],
  464. '3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg', ['--type=segwit'], 'opt.type="segwit"' ),
  465. ( ['KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm'],
  466. 'bc1q6pqnfwwakuuejpm9w52ds342f9d5u36v0qnz7c', ['--type=bech32'], 'opt.type="bech32"' ),
  467. ],
  468. },
  469. 'wif2hex': {
  470. 'btc_mainnet': [
  471. ( ['5HwzecKMWD82ppJK3qMKpC7ohXXAwcyAN5VgdJ9PLFaAzpBG4sX'],
  472. '118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492',
  473. None, 'opt.type="legacy"' ),
  474. ( ['KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm'],
  475. '118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492',
  476. ['--type=compressed'], 'opt.type="compressed"' ),
  477. ( ['KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm'],
  478. '118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492',
  479. ['--type=segwit'], 'opt.type="segwit"' ),
  480. ( ['KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm'],
  481. '118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492',
  482. ['--type=bech32'], 'opt.type="bech32"' ),
  483. ],
  484. },
  485. 'wif2redeem_script': {
  486. 'btc_mainnet': [
  487. ( ['KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm'],
  488. '0014d04134b9ddb7399907657514d846aa495b4e474c',
  489. ['--type=segwit'], 'opt.type="segwit"' ),
  490. ],
  491. },
  492. 'wif2segwit_pair': {
  493. 'btc_mainnet': [
  494. ( ['KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm'],
  495. ('0014d04134b9ddb7399907657514d846aa495b4e474c','3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg'),
  496. ['--type=segwit'], 'opt.type="segwit"' ),
  497. ],
  498. },
  499. },
  500. # TODO: compressed address files are missing
  501. # 'addrfile_compressed_chk':
  502. # 'btc': ('A33C 4FDE F515 F5BC','6C48 AA57 2056 C8C8'),
  503. # 'ltc': ('3FC0 8F03 C2D6 BD19','4C0A 49B6 2DD1 1BE0'),
  504. 'File': {
  505. 'addrfile_chksum': {
  506. 'btc_mainnet': [
  507. ( ['test/ref/98831F3A[1,31-33,500-501,1010-1011].addrs'],
  508. '6FEF 6FB9 7B13 5D91'),
  509. ( ['test/ref/98831F3A-S[1,31-33,500-501,1010-1011].addrs'],
  510. '06C1 9C87 F25C 4EE6'),
  511. ( ['test/ref/98831F3A-B[1,31-33,500-501,1010-1011].addrs'],
  512. '9D2A D4B6 5117 F02E'),
  513. ],
  514. 'btc_testnet': [
  515. ( ['test/ref/98831F3A[1,31-33,500-501,1010-1011].testnet.addrs'],
  516. '424E 4326 CFFE 5F51'),
  517. ( ['test/ref/98831F3A-S[1,31-33,500-501,1010-1011].testnet.addrs'],
  518. '072C 8B07 2730 CB7A'),
  519. ( ['test/ref/98831F3A-B[1,31-33,500-501,1010-1011].testnet.addrs'],
  520. '0527 9C39 6C1B E39A'),
  521. ],
  522. 'ltc_mainnet': [
  523. ( ['test/ref/litecoin/98831F3A-LTC[1,31-33,500-501,1010-1011].addrs'],
  524. 'AD52 C3FE 8924 AAF0'),
  525. ( ['test/ref/litecoin/98831F3A-LTC-S[1,31-33,500-501,1010-1011].addrs'],
  526. '63DF E42A 0827 21C3'),
  527. ( ['test/ref/litecoin/98831F3A-LTC-B[1,31-33,500-501,1010-1011].addrs'],
  528. 'FF1C 7939 5967 AB82'),
  529. ],
  530. 'ltc_testnet': [
  531. ( ['test/ref/litecoin/98831F3A-LTC[1,31-33,500-501,1010-1011].testnet.addrs'],
  532. '4EBE 2E85 E969 1B30'),
  533. ( ['test/ref/litecoin/98831F3A-LTC-S[1,31-33,500-501,1010-1011].testnet.addrs'],
  534. '5DD1 D186 DBE1 59F2'),
  535. ( ['test/ref/litecoin/98831F3A-LTC-B[1,31-33,500-501,1010-1011].testnet.addrs'],
  536. 'ED3D 8AA4 BED4 0B40'),
  537. ],
  538. 'zec_mainnet': [
  539. ( ['test/ref/zcash/98831F3A-ZEC-C[1,31-33,500-501,1010-1011].addrs'],'903E 7225 DD86 6E01'), ],
  540. 'zec_z_mainnet': [
  541. ( ['test/ref/zcash/98831F3A-ZEC-Z[1,31-33,500-501,1010-1011].addrs'],'9C7A 72DC 3D4A B3AF'), ],
  542. 'xmr_mainnet': [
  543. ( ['test/ref/monero/98831F3A-XMR-M[1,31-33,500-501,1010-1011].addrs'],'4369 0253 AC2C 0E38'), ],
  544. 'dash_mainnet': [
  545. ( ['test/ref/dash/98831F3A-DASH-C[1,31-33,500-501,1010-1011].addrs'],'FBC1 6B6A 0988 4403'), ],
  546. 'eth_mainnet': [
  547. ( ['test/ref/ethereum/98831F3A-ETH[1,31-33,500-501,1010-1011].addrs'],'E554 076E 7AF6 66A3'), ],
  548. 'etc_mainnet': [
  549. ( ['test/ref/ethereum_classic/98831F3A-ETC[1,31-33,500-501,1010-1011].addrs'],
  550. 'E97A D796 B495 E8BC'), ],
  551. },
  552. 'keyaddrfile_chksum': {
  553. 'btc_mainnet': [
  554. ( ['test/ref/98831F3A[1,31-33,500-501,1010-1011].akeys.mmenc'],
  555. '9F2D D781 1812 8BAD', kafile_opts, kafile_code ),
  556. ],
  557. 'btc_testnet': [
  558. ( ['test/ref/98831F3A[1,31-33,500-501,1010-1011].testnet.akeys.mmenc'],
  559. '88CC 5120 9A91 22C2', kafile_opts, kafile_code ),
  560. ],
  561. 'ltc_mainnet': [
  562. ( ['test/ref/litecoin/98831F3A-LTC[1,31-33,500-501,1010-1011].akeys.mmenc'],
  563. 'B804 978A 8796 3ED4', kafile_opts, kafile_code ),
  564. ],
  565. 'ltc_testnet': [
  566. ( ['test/ref/litecoin/98831F3A-LTC[1,31-33,500-501,1010-1011].testnet.akeys.mmenc'],
  567. '98B5 AC35 F334 0398', kafile_opts, kafile_code ),
  568. ],
  569. 'zec_mainnet': [
  570. ( ['test/ref/zcash/98831F3A-ZEC-C[1,31-33,500-501,1010-1011].akeys.mmenc'],
  571. 'F05A 5A5C 0C8E 2617', kafile_opts, kafile_code ), ],
  572. 'zec_z_mainnet': [
  573. ( ['test/ref/zcash/98831F3A-ZEC-Z[1,31-33,500-501,1010-1011].akeys.mmenc'],
  574. '6B87 9B2D 0D8D 8D1E', kafile_opts, kafile_code ), ],
  575. 'xmr_mainnet': [
  576. ( ['test/ref/monero/98831F3A-XMR-M[1,31-33,500-501,1010-1011].akeys.mmenc'],
  577. 'E0D7 9612 3D67 404A', kafile_opts, kafile_code ), ],
  578. 'dash_mainnet': [
  579. ( ['test/ref/dash/98831F3A-DASH-C[1,31-33,500-501,1010-1011].akeys.mmenc'],
  580. 'E83D 2C63 FEA2 4142', kafile_opts, kafile_code ), ],
  581. 'eth_mainnet': [
  582. ( ['test/ref/ethereum/98831F3A-ETH[1,31-33,500-501,1010-1011].akeys.mmenc'],
  583. 'E400 70D9 0AE3 C7C2', kafile_opts, kafile_code ), ],
  584. 'etc_mainnet': [
  585. ( ['test/ref/ethereum_classic/98831F3A-ETC[1,31-33,500-501,1010-1011].akeys.mmenc'],
  586. 'EF49 967D BD6C FE45', kafile_opts, kafile_code ), ],
  587. },
  588. 'passwdfile_chksum': {
  589. 'btc_mainnet': [
  590. ( ['test/ref/98831F3A-фубар@crypto.org-b58-20[1,4,1100].pws'],
  591. 'DDD9 44B0 CA28 183F', kafile_opts, kafile_code ), ],
  592. },
  593. 'txview': {
  594. 'btc_mainnet': [ ( ['test/ref/0B8D5A[15.31789,14,tl=1320969600].rawtx'], None ), ],
  595. 'btc_testnet': [ ( ['test/ref/0C7115[15.86255,14,tl=1320969600].testnet.rawtx'], None ), ],
  596. 'bch_mainnet': [ ( ['test/ref/460D4D-BCH[10.19764,tl=1320969600].rawtx'], None ), ],
  597. 'bch_testnet': [ ( ['test/ref/359FD5-BCH[6.68868,tl=1320969600].testnet.rawtx'], None ), ],
  598. 'ltc_mainnet': [ ( ['test/ref/litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx'], None ), ],
  599. 'ltc_testnet': [ ( ['test/ref/litecoin/A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx'],
  600. None ), ],
  601. 'eth_mainnet': [ ( ['test/ref/ethereum/88FEFD-ETH[23.45495,40000].rawtx'], None ), ],
  602. 'eth_testnet': [ ( ['test/ref/ethereum/B472BD-ETH[23.45495,40000].testnet.rawtx'], None ), ],
  603. 'mm1_mainnet': [ ( ['test/ref/ethereum/5881D2-MM1[1.23456,50000].rawtx'], None ), ],
  604. 'mm1_testnet': [ ( ['test/ref/ethereum/6BDB25-MM1[1.23456,50000].testnet.rawtx'], None ), ],
  605. 'etc_mainnet': [ ( ['test/ref/ethereum_classic/ED3848-ETC[1.2345,40000].rawtx'], None ), ],
  606. },
  607. },
  608. }
  609. coin_dependent_groups = ('Coin','File') # TODO: do this as attr of each group in tool.py
  610. def run_test(gid,cmd_name):
  611. data = tests[gid][cmd_name]
  612. # behavior is like test.py: run coin-dependent tests only if g.testnet or g.coin != BTC
  613. if gid in coin_dependent_groups:
  614. k = '{}_{}net'.format((g.token.lower() if g.token else g.coin.lower()),('main','test')[g.testnet])
  615. if k in data:
  616. data = data[k]
  617. m2 = ' ({})'.format(k)
  618. else:
  619. msg("-- no data for {} ({}) - skipping".format(cmd_name,k))
  620. return
  621. else:
  622. if g.coin != 'BTC' or g.testnet: return
  623. m2 = ''
  624. m = '{} {}{}'.format(purple('Testing'), cmd_name if opt.names else
  625. extract_docstring(getattr(getattr(tool,'MMGenToolCmd'+gid),cmd_name)),m2)
  626. msg_r(green(m)+'\n' if opt.verbose else m)
  627. def fork_cmd(cmd_name,args,out,opts,exec_code):
  628. cmd = list(tool_cmd) + (opts or []) + [cmd_name] + args
  629. vmsg('{} {}'.format(green('Executing'),cyan(' '.join(cmd))))
  630. cp = run(cmd,input=stdin_input or None,stdout=PIPE,stderr=PIPE)
  631. try: cmd_out = cp.stdout.decode()
  632. except: cmd_out = cp.stdout
  633. if cp.stderr:
  634. vmsg(cp.stderr.strip().decode())
  635. if cp.returncode != 0:
  636. import re
  637. m = re.match(b"tool command returned '(None|False)'"+NL.encode(),cp.stderr)
  638. if m:
  639. return { b'None': None, b'False': False }[m.group(1)]
  640. else:
  641. ydie(1,'Spawned program exited with error: {}'.format(cp.stderr))
  642. return cmd_out.strip()
  643. def run_func(cmd_name,args,out,opts,exec_code):
  644. vmsg('{}: {}{}'.format(purple('Running'),
  645. ' '.join([cmd_name]+[repr(e) for e in args]),
  646. ' '+exec_code if exec_code else '' ))
  647. if exec_code: exec(exec_code)
  648. aargs,kwargs = tool._process_args(cmd_name,args)
  649. oq_save = opt.quiet
  650. if not opt.verbose: opt.quiet = True
  651. if stdin_input:
  652. fd0,fd1 = os.pipe()
  653. if os.fork(): # parent
  654. os.close(fd1)
  655. stdin_save = os.dup(0)
  656. os.dup2(fd0,0)
  657. cmd_out = getattr(tc,cmd_name)(*aargs,**kwargs)
  658. os.dup2(stdin_save,0)
  659. os.wait()
  660. opt.quiet = oq_save
  661. return cmd_out
  662. else: # child
  663. os.close(fd0)
  664. os.write(fd1,stdin_input)
  665. vmsg('Input: {!r}'.format(stdin_input))
  666. sys.exit(0)
  667. else:
  668. ret = getattr(tc,cmd_name)(*aargs,**kwargs)
  669. opt.quiet = oq_save
  670. return ret
  671. for d in data:
  672. args,out,opts,exec_code = d + tuple([None] * (4-len(d)))
  673. stdin_input = None
  674. if args and type(args[0]) == bytes:
  675. stdin_input = args[0]
  676. args[0] = '-'
  677. if opt.fork:
  678. cmd_out = fork_cmd(cmd_name,args,out,opts,exec_code)
  679. else:
  680. if stdin_input and g.platform == 'win':
  681. msg('Skipping for MSWin - no os.fork()')
  682. continue
  683. cmd_out = run_func(cmd_name,args,out,opts,exec_code)
  684. try: vmsg('Output:\n{}\n'.format(cmd_out))
  685. except: vmsg('Output:\n{}\n'.format(repr(cmd_out)))
  686. def check_output(out,chk):
  687. if isinstance(chk,str): chk = chk.encode()
  688. if isinstance(out,int): out = str(out).encode()
  689. if isinstance(out,str): out = out.encode()
  690. err_fs = "Output ({!r}) doesn't match expected output ({!r})"
  691. try: outd = out.decode()
  692. except: outd = None
  693. if type(chk).__name__ == 'function':
  694. assert chk(outd),"{}({}) failed!".format(chk.__name__,outd)
  695. elif type(chk) == dict:
  696. for k,v in chk.items():
  697. if k == 'boolfunc':
  698. assert v(outd),"{}({}) failed!".format(v.__name__,outd)
  699. elif k == 'value':
  700. assert outd == v, err_fs.format(outd,v)
  701. else:
  702. outval = getattr(__builtins__,k)(out)
  703. if outval != v:
  704. die(1,"{}({}) returned {}, not {}!".format(k,out,outval,v))
  705. elif chk is not None:
  706. assert out == chk, err_fs.format(out,chk)
  707. if type(out) == tuple and type(out[0]).__name__ == 'function':
  708. func_out = out[0](cmd_out)
  709. assert func_out == out[1],(
  710. "{}({}) == {} failed!\nOutput: {}".format(out[0].__name__,cmd_out,out[1],func_out))
  711. elif isinstance(out,(list,tuple)):
  712. for co,o in zip(cmd_out.split(NL) if opt.fork else cmd_out,out):
  713. check_output(co,o)
  714. else:
  715. check_output(cmd_out,out)
  716. if not opt.verbose: msg_r('.')
  717. if not opt.verbose:
  718. msg('OK')
  719. def extract_docstring(obj):
  720. return obj.__doc__.strip().split('\n')[0]
  721. def do_group(gid):
  722. qmsg(blue("Testing {}".format(
  723. "command group '{}'".format(gid) if opt.names
  724. else extract_docstring(getattr(tool,'MMGenToolCmd'+gid)))))
  725. for cname in [e for e in dir(getattr(tool,'MMGenToolCmd'+gid)) if e[0] != '_']:
  726. if cname not in tests[gid]:
  727. m = 'No test for command {!r} in group {!r}!'.format(cname,gid)
  728. if opt.die_on_missing:
  729. die(1,m+' Aborting')
  730. else:
  731. msg(m)
  732. continue
  733. run_test(gid,cname)
  734. def do_cmd_in_group(cmd):
  735. for gid in tests:
  736. for cname in tests[gid]:
  737. if cname == cmd:
  738. run_test(gid,cname)
  739. return True
  740. return False
  741. def list_tested_cmds():
  742. for gid in tests:
  743. for cname in [e for e in dir(getattr(tool,'MMGenToolCmd'+gid)) if e[0] != '_']:
  744. Msg(cname)
  745. sys.argv = [sys.argv[0]] + ['--skip-cfg-file'] + sys.argv[1:]
  746. cmd_args = opts.init(opts_data)
  747. import mmgen.tool as tool
  748. if opt.list_tests:
  749. Msg('Available tests:')
  750. for gid in tests:
  751. Msg(' {:6} - {}'.format(gid,extract_docstring(getattr(tool,'MMGenToolCmd'+gid))))
  752. sys.exit(0)
  753. if opt.list_tested_cmds:
  754. list_tested_cmds()
  755. sys.exit(0)
  756. if opt.system:
  757. tool_exec = 'mmgen-tool'
  758. sys.path.pop(0)
  759. else:
  760. os.environ['PYTHONPATH'] = repo_root
  761. tool_exec = os.path.relpath(os.path.join('cmds','mmgen-tool'))
  762. if opt.fork:
  763. tool_cmd = (tool_exec,'--skip-cfg-file')
  764. passthru_args = ['coin','type','testnet','token']
  765. tool_cmd += tuple(['--{}{}'.format(k.replace('_','-'),
  766. '='+getattr(opt,k) if getattr(opt,k) != True else ''
  767. ) for k in passthru_args if getattr(opt,k)])
  768. if opt.traceback:
  769. tool_cmd = (os.path.join('scripts','traceback_run.py'),) + tool_cmd
  770. if opt.coverage:
  771. d,f = init_coverage()
  772. tool_cmd = ('python3','-m','trace','--count','--coverdir='+d,'--file='+f) + tool_cmd
  773. elif g.platform == 'win':
  774. tool_cmd = ('python3',) + tool_cmd
  775. else:
  776. opt.usr_randchars = 0
  777. tc = tool.MMGenToolCmd()
  778. start_time = int(time.time())
  779. try:
  780. if cmd_args:
  781. for cmd in cmd_args:
  782. if cmd in tests:
  783. do_group(cmd)
  784. else:
  785. if not do_cmd_in_group(cmd):
  786. die(1,"'{}': not a recognized test or test group".format(cmd))
  787. else:
  788. for garg in tests:
  789. do_group(garg)
  790. except KeyboardInterrupt:
  791. die(1,green('\nExiting at user request'))
  792. t = int(time.time()) - start_time
  793. gmsg('All requested tests finished OK, elapsed time: {:02}:{:02}'.format(t//60,t%60))