ut_addrlist.py 2.8 KB

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