ut_addrlist.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_addrlist: address list unit tests for the MMGen suite
  4. """
  5. from mmgen.color import blue
  6. from mmgen.util import msg
  7. from mmgen.seed import Seed
  8. from mmgen.addr import MMGenAddrType
  9. from mmgen.addrlist import AddrIdxList, AddrList, KeyList, KeyAddrList, ViewKeyAddrList
  10. from mmgen.passwdlist import PasswordList
  11. from mmgen.protocol import init_proto
  12. from ..include.common import cfg, qmsg, vmsg
  13. def do_test(
  14. list_type,
  15. chksum,
  16. idx_spec = None,
  17. pw_id_str = None,
  18. add_kwargs = None,
  19. coin = None,
  20. addrtype = None):
  21. qmsg(blue(f'Testing {list_type.__name__}'))
  22. proto = init_proto(cfg, coin or 'btc')
  23. seed = Seed(cfg, seed_bin=bytes.fromhex('feedbead'*8))
  24. mmtype = MMGenAddrType(proto, addrtype or 'C')
  25. idxs = AddrIdxList(idx_spec or '1-3')
  26. if cfg.verbose:
  27. debug_addrlist_save = cfg.debug_addrlist
  28. cfg.debug_addrlist = True
  29. kwargs = {
  30. 'seed': seed,
  31. 'pw_idxs': idxs,
  32. 'pw_id_str': pw_id_str,
  33. 'pw_fmt': 'b58',
  34. } if pw_id_str else {
  35. 'seed': seed,
  36. 'addr_idxs': idxs,
  37. 'mmtype': mmtype,
  38. }
  39. if add_kwargs:
  40. kwargs.update(add_kwargs)
  41. al = list_type(cfg, proto, **kwargs)
  42. al.file.format()
  43. qmsg(f'Filename: {al.file.filename}\n')
  44. vmsg(f'------------\n{al.file.fmt_data}\n------------')
  45. if chksum:
  46. assert al.chksum == chksum, f'{al.chksum} != {chksum}'
  47. if cfg.verbose:
  48. cfg.debug_addrlist = debug_addrlist_save
  49. return True
  50. class unit_tests:
  51. altcoin_deps = ('keyaddr_xmr', 'viewkeyaddr')
  52. def idxlist(self, name, ut):
  53. for i, o in (
  54. ('99,88-102,1-3,4,9,818,444-445,816', '1-4,9,88-102,444-445,816,818'),
  55. ('99,88-99,100,102,4-7,9,818,444-445,816,1', '1,4-7,9,88-100,102,444-445,816,818'),
  56. ('8', '8'),
  57. ('2-4', '2-4'),
  58. ('1,2-4', '1-4'),
  59. ('2-4,1-9,9,1,8', '1-9'),
  60. ('2-4,1', '1-4'),
  61. ('2-2', '2'),
  62. ('2,2', '2'),
  63. ('2-3', '2-3'),
  64. ('2,3', '2-3'),
  65. ('3,2', '2-3'),
  66. ('2,4', '2,4'),
  67. ('', ''),
  68. ):
  69. l = AddrIdxList(i)
  70. if cfg.verbose:
  71. msg(f'list: {list(l)}\nin: {i}\nout: {o}\n')
  72. assert l.id_str == o, f'{l.id_str} != {o}'
  73. return True
  74. def addr(self, name, ut):
  75. return (
  76. do_test(AddrList, 'BCE8 082C 0973 A525', '1-3') and
  77. do_test(AddrList, '88FA B04B A380 C1CB', '199999,99-101,77-78,7,3,2-9')
  78. )
  79. def key(self, name, ut):
  80. return do_test(KeyList, None)
  81. def keyaddr(self, name, ut):
  82. return do_test(KeyAddrList, '4A36 AA65 8C2B 7C35')
  83. def keyaddr_xmr(self, name, ut):
  84. return do_test(KeyAddrList, 'AAA2 BA69 17FC 9A88', coin='XMR', addrtype='M')
  85. def viewkeyaddr(self, name, ut):
  86. return do_test(ViewKeyAddrList, 'C122 2E58 DC28 D6AE', coin='XMR', addrtype='M')
  87. def passwd(self, name, ut):
  88. return do_test(PasswordList, 'FF4A B716 4513 8F8F', pw_id_str='foo')
  89. def passwd_bip39(self, name, ut):
  90. return do_test(PasswordList, 'C3A8 B2B2 1AA1 FB40', pw_id_str='foo', add_kwargs={'pw_fmt': 'bip39'})