ut_addrlist.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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
  9. from mmgen.passwdlist import PasswordList
  10. from mmgen.protocol import init_proto
  11. def do_test(list_type,chksum,idx_spec=None,pw_id_str=None,add_kwargs=None):
  12. qmsg(blue(f'Testing {list_type.__name__}'))
  13. proto = init_proto('btc')
  14. seed = Seed(seed_bin=bytes.fromhex('feedbead'*8))
  15. mmtype = MMGenAddrType(proto,'C')
  16. idxs = AddrIdxList(idx_spec or '1-3')
  17. if opt.verbose:
  18. debug_addrlist_save = g.debug_addrlist
  19. g.debug_addrlist = True
  20. kwargs = {
  21. 'seed': seed,
  22. 'pw_idxs': idxs,
  23. 'pw_id_str': pw_id_str,
  24. 'pw_fmt': 'b58',
  25. } if pw_id_str else {
  26. 'seed': seed,
  27. 'addr_idxs': idxs,
  28. 'mmtype': mmtype,
  29. }
  30. if add_kwargs:
  31. kwargs.update(add_kwargs)
  32. al = list_type( proto, **kwargs )
  33. af = al.get_file()
  34. af.format()
  35. qmsg(f'Filename: {af.filename}\n')
  36. # af.write('-')
  37. vmsg(f'------------\n{af.fmt_data}\n------------')
  38. if chksum:
  39. assert al.chksum == chksum, f'{al.chksum} != {chksum}'
  40. if opt.verbose:
  41. g.debug_addrlist = debug_addrlist_save
  42. return True
  43. class unit_tests:
  44. def idxlist(self,name,ut):
  45. for i,o in (
  46. ('99,88-102,1-3,4,9,818,444-445,816', '1-4,9,88-102,444-445,816,818'),
  47. ('99,88-99,100,102,4-7,9,818,444-445,816,1', '1,4-7,9,88-100,102,444-445,816,818'),
  48. ('8', '8'),
  49. ('2-4', '2-4'),
  50. ('1,2-4', '1-4'),
  51. ('2-4,1-9,9,1,8', '1-9'),
  52. ('2-4,1', '1-4'),
  53. ('2-2', '2'),
  54. ('2,2', '2'),
  55. ('2-3', '2-3'),
  56. ('2,3', '2-3'),
  57. ('3,2', '2-3'),
  58. ('2,4', '2,4'),
  59. ('', ''),
  60. ):
  61. l = AddrIdxList(i)
  62. if opt.verbose:
  63. msg('list: {}\nin: {}\nout: {}\n'.format(list(l),i,o))
  64. assert l.id_str == o, f'{l.id_str} != {o}'
  65. return True
  66. def addr(self,name,ut):
  67. return (
  68. do_test(AddrList,'BCE8 082C 0973 A525','1-3') and
  69. do_test(AddrList,'88FA B04B A380 C1CB','199999,99-101,77-78,7,3,2-9')
  70. )
  71. def key(self,name,ut):
  72. return do_test(KeyList,None)
  73. def keyaddr(self,name,ut):
  74. return do_test(KeyAddrList,'4A36 AA65 8C2B 7C35')
  75. def passwd(self,name,ut):
  76. return do_test(PasswordList,'FF4A B716 4513 8F8F',pw_id_str='foo')
  77. def passwd_bip39(self,name,ut):
  78. return do_test(PasswordList,'C3A8 B2B2 1AA1 FB40',pw_id_str='foo',add_kwargs={'pw_fmt':'bip39'})