scrambletest.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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/scrambletest.py: seed scrambling and addrlist data generation tests for all
  20. supported coins + passwords
  21. """
  22. import sys,os,time
  23. from subprocess import run,PIPE
  24. from collections import namedtuple
  25. try:
  26. from include import test_init
  27. except ImportError:
  28. from test.include import test_init
  29. from mmgen.cfg import Config
  30. from mmgen.util import msg,msg_r,bmsg,die
  31. opts_data = {
  32. 'text': {
  33. 'desc': 'Test seed scrambling and addrlist data generation for all supported altcoins',
  34. 'usage':'[options] [command]',
  35. 'options': """
  36. -h, --help Print this help message
  37. --, --longhelp Print help message for long options (common options)
  38. -a, --no-altcoin Skip altcoin tests
  39. -C, --coverage Produce code coverage info using trace module
  40. -l, --list-cmds List and describe the tests and commands in this test suite
  41. -v, --verbose Produce more verbose output
  42. """,
  43. 'notes': """
  44. Valid commands: 'coin','pw'
  45. If no command is given, the whole suite of tests is run.
  46. """
  47. }
  48. }
  49. cfg = Config(opts_data=opts_data)
  50. from test.include.common import set_globals,init_coverage,end_msg,green
  51. set_globals(cfg)
  52. td = namedtuple('scrambletest_entry',['seed','str','id_str','lbl','addr'])
  53. bitcoin_data = {
  54. # SCRAMBLED_SEED[:8] SCRAMBLE_KEY ID_STR LBL FIRST ADDR
  55. 'btc': td('456d7f5f1c4bfe3b','(none)', '', '', '1MU7EdgqYy9JX35L25hR6CmXXcSEBDAwyv'),
  56. 'btc_compressed':td('bf98a4af5464a4ef','compressed', '-C', 'COMPRESSED', '1F97Jd89wwmu4ELadesAdGDzg3d8Y6j5iP'),
  57. 'btc_segwit': td('b56962d829ffc678','segwit', '-S', 'SEGWIT', '36TvVzU5mxSjJ3D9qKAmYzCV7iUqtTDezF'),
  58. 'btc_bech32': td('d09eea818f9ad17f','bech32', '-B', 'BECH32', 'bc1q8snv94j6959y3gkqv4gku0cm5mersnpucsvw5z'),
  59. }
  60. altcoin_data = {
  61. 'bch': td('456d7f5f1c4bfe3b','(none)', '', '', '1MU7EdgqYy9JX35L25hR6CmXXcSEBDAwyv'),
  62. 'bch_compressed':td('bf98a4af5464a4ef','compressed', '-C', 'COMPRESSED', '1F97Jd89wwmu4ELadesAdGDzg3d8Y6j5iP'),
  63. 'ltc': td('b11f16632e63ba92','ltc:legacy', '-LTC', 'LTC', 'LMxB474SVfxeYdqxNrM1WZDZMnifteSMv1'),
  64. 'ltc_compressed':td('7ccf465d466ee7d3','ltc:compressed','-LTC-C','LTC:COMPRESSED','LdkebBKVXSs6NNoPJWGM8KciDnL8LhXXjb'),
  65. 'ltc_segwit': td('9460f5ba15e82768','ltc:segwit', '-LTC-S','LTC:SEGWIT', 'MQrY3vEbqKMBgegXrSaR93R2HoTDE5bKrY'),
  66. 'ltc_bech32': td('dbdbff2e196e27d3','ltc:bech32', '-LTC-B','LTC:BECH32', 'ltc1qdvgqsz94ht20lr8fyk5v7n884hu9p7d8k9easu'),
  67. 'eth': td('213ed116869b19f2','eth', '-ETH', 'ETH', 'e704b6cfd9f0edb2e6cfbd0c913438d37ede7b35'),
  68. 'etc': td('909def37096f5ab8','etc', '-ETC', 'ETC', '1a6acbef8c38f52f20d04ecded2992b04d8608d7'),
  69. 'dash': td('1319d347b021f952','dash:legacy', '-DASH', 'DASH', 'XoK491fppGNZQUUS9uEFkT6L9u8xxVFJNJ'),
  70. 'emc': td('7e1a29853d2db875','emc:legacy', '-EMC', 'EMC', 'EU4L6x2b5QUb2gRQsBAAuB8TuPEwUxCNZU'),
  71. 'zec': td('0bf9b5b20af7b5a0','zec:legacy', '-ZEC', 'ZEC', 't1URz8BHxV38v3gsaN6oHQNKC16s35R9WkY'),
  72. 'zec_zcash_z': td('b15570d033df9b1a','zec:zcash_z', '-ZEC-Z','ZEC:ZCASH_Z', 'zcLMMsnzfFYZWU4avRBnuc83yh4jTtJXbtP32uWrs3ickzu1krMU4ppZCQPTwwfE9hLnRuFDSYF8VFW13aT9eeQK8aov3Ge'),
  73. 'xmr': td('c76af3b088da3364','xmr:monero', '-XMR-M','XMR:MONERO', '41tmwZd2CdXEGtWqGY9fH9FVtQM8VxZASYPQ3VJQhFjtGWYzQFuidD21vJYTi2yy3tXRYXTNXBTaYVLav62rwUUpFFyicZU'),
  74. }
  75. passwd_data = {
  76. 'dfl_dfl_αω': td('b78c363f3d6714f6', 'b58:20:αω', '-αω-b58-20', 'αω b58:20', 'L3JcAZze6LQS2TFodoW9'),
  77. 'dfl_H_αω': td('2cba1ba1a73d5497', 'b58:10:αω', '-αω-b58-10', 'αω b58:10', 'KgSBfzPBWj'),
  78. 'b32_dfl_αω': td('ad71ff8d0512660d', 'b32:24:αω', '-αω-b32-24', 'αω b32:24', 'VQFESDGWAQCOCSG6GDLIG5OQ'),
  79. 'b32_H_αω': td('b050a1613dd4d3ad', 'b32:12:αω', '-αω-b32-12', 'αω b32:12', 'QTI7HTVN3JOE'),
  80. 'hex_dfl_αω': td('8322b26abd931d55', 'hex:64:αω', '-αω-hex-64', 'αω hex:64', '6e9f8d5d2ec6c4f2a079b163e37f5dc2a7e6adb221c4de315078ffad971ba260'),
  81. 'hex_H_αω': td('46beb852d38ed5c4', 'hex:32:αω', '-αω-hex-32', 'αω hex:32', '86143b36b649728cc8f3677872ed37d3'),
  82. 'bip39_dfl_αω':td('95b383d5092a55df', 'bip39:24:αω','-αω-bip39-24','αω bip39:24','treat athlete brand top beauty poverty senior unhappy vacant domain yellow scale fossil aim lonely fatal sun nuclear such ancient stage require stool similar'),
  83. 'bip39_18_αω': td('29e5a605ffa36142', 'bip39:18:αω','-αω-bip39-18','αω bip39:18','better legal various ketchup then range festival either tomato cradle say absorb solar earth alter pattern canyon liar'),
  84. 'bip39_12_αω': td('efa13cb309d7fc1d', 'bip39:12:αω','-αω-bip39-12','αω bip39:12','lady oppose theme fit position merry reopen acquire tuna dentist young chunk'),
  85. 'xmrseed_dfl_αω':td('62f5b72a5ca89cab', 'xmrseed:25:αω','-αω-xmrseed-25','αω xmrseed:25','tequila eden skulls giving jester hospital dreams bakery adjust nanny cactus inwardly films amply nanny soggy vials muppet yellow woken ashtray organs exhale foes eden'),
  86. }
  87. cvr_opts = ' -m trace --count --coverdir={} --file={}'.format( *init_coverage() ) if cfg.coverage else ''
  88. cmd_base = f'python3{cvr_opts} cmds/mmgen-{{}}gen -qS'
  89. run_env = dict(os.environ)
  90. run_env['MMGEN_DEBUG_ADDRLIST'] = '1'
  91. def get_cmd_output(cmd):
  92. cp = run( cmd.split(), stdout=PIPE, stderr=PIPE, env=run_env )
  93. if cp.returncode != 0:
  94. die(2,f'\nSpawned program exited with error code {cp.returncode}:\n{cp.stderr.decode()}')
  95. return cp.stdout.decode().splitlines()
  96. def do_test(cmd,tdata,msg_str,addr_desc):
  97. cfg._util.vmsg(green(f'Executing: {cmd}'))
  98. msg_r('Testing: ' + msg_str)
  99. lines = get_cmd_output(cmd)
  100. cmd_out = dict([e[9:].split(': ') for e in lines if e.startswith('sc_debug_')])
  101. cmd_out['addr'] = lines[-2].split(None,1)[-1]
  102. ref_data = tdata._asdict()
  103. cfg._util.vmsg('')
  104. for k in ref_data:
  105. if cmd_out[k] == ref_data[k]:
  106. s = k.replace('seed','seed[:8]').replace('addr',addr_desc)
  107. cfg._util.vmsg(f' {s:9}: {cmd_out[k]}')
  108. else:
  109. die(4,f'\nError: sc_{k} value {cmd_out[k]} does not match reference value {ref_data[k]}')
  110. msg('OK')
  111. def do_coin_tests():
  112. bmsg('Testing address scramble strings and list IDs')
  113. for tname,tdata in (
  114. tuple(bitcoin_data.items()) +
  115. tuple(altcoin_data.items() if not cfg.no_altcoin else []) ):
  116. if tname == 'zec_zcash_z' and sys.platform == 'win32':
  117. msg("Skipping 'zec_zcash_z' test for Windows platform")
  118. continue
  119. coin,mmtype = tname.split('_',1) if '_' in tname else (tname,None)
  120. type_arg = ' --type='+mmtype if mmtype else ''
  121. cmd = cmd_base.format('addr') + f' --coin={coin}{type_arg} test/ref/98831F3A.mmwords 1'
  122. do_test(cmd,tdata,f'--coin {coin.upper():4} {type_arg:22}','address')
  123. def do_passwd_tests():
  124. bmsg('Testing password scramble strings and list IDs')
  125. for tname,tdata in passwd_data.items():
  126. if tname.startswith('xmrseed') and cfg.no_altcoin:
  127. continue
  128. a,b,pwid = tname.split('_')
  129. fmt_arg = '' if a == 'dfl' else f'--passwd-fmt={a} '
  130. len_arg = '' if b == 'dfl' else f'--passwd-len={b} '
  131. fs = '{}' + fmt_arg + len_arg + '{}' + pwid + ' 1'
  132. cmd = cmd_base.format('pass') + ' ' + fs.format('--accept-defaults ','test/ref/98831F3A.mmwords ')
  133. s = fs.format('','')
  134. do_test(cmd,tdata,s+' '*(40-len(s)),'password')
  135. if __name__ == '__main__':
  136. start_time = int(time.time())
  137. cmds = cfg._args or ('coin','pw')
  138. for cmd in cmds:
  139. {'coin': do_coin_tests, 'pw': do_passwd_tests }[cmd]()
  140. end_msg(int(time.time()) - start_time)