tooltest2.py 29 KB

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