ut_mn_entry.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_mn_entry: Mnemonic user entry unit test for the MMGen suite
  4. """
  5. from mmgen.util import msg,msg_r
  6. from ..include.common import cfg,qmsg
  7. class unit_test:
  8. vectors = {
  9. 'mmgen': {
  10. 'usl': 10, 'sw': 3, 'lw': 12,
  11. 'idx_minimal': ( # None: non-unique match. False: no match
  12. ('a', None),
  13. ('aa', False),
  14. ('as', None),
  15. ('ask', 70),
  16. ('afte', None),
  17. ('after', None),
  18. ('aftern', 20),
  19. ('afternoon', 20),
  20. ('afternoons',False),
  21. ('g', None),
  22. ('gg', False),
  23. ('z', False),
  24. ('abi', False),
  25. ('abo', None),
  26. ('abl', 0),
  27. ('able', 0),
  28. ('abler', False),
  29. ('you', None),
  30. ('yout', 1625),
  31. ('youth', 1625),
  32. ('youths', False),
  33. ),
  34. },
  35. 'xmrseed': { 'usl': 3, 'sw': 4, 'lw': 12 },
  36. 'bip39': { 'usl': 4, 'sw': 3, 'lw': 8 },
  37. }
  38. def run_test(self,name,ut):
  39. msg_r('Testing MnemonicEntry methods...')
  40. from mmgen.mn_entry import mn_entry
  41. msg_r('\nTesting computed wordlist constants...')
  42. for wl_id in self.vectors:
  43. for j,k in (('uniq_ss_len','usl'),('shortest_word','sw'),('longest_word','lw')):
  44. a = getattr(mn_entry( cfg, wl_id ),j)
  45. b = self.vectors[wl_id][k]
  46. assert a == b, f'{wl_id}:{j} {a} != {b}'
  47. msg('OK')
  48. msg_r('Testing idx()...')
  49. qmsg('')
  50. junk = 'a g z aa gg zz aaa ggg zzz aaaa gggg zzzz aaaaaaaaaaaaaa gggggggggggggg zzzzzzzzzzzzzz'
  51. for wl_id in self.vectors:
  52. m = mn_entry( cfg, wl_id )
  53. qmsg('Wordlist: '+wl_id)
  54. for entry_mode in ('full','short'):
  55. for a,word in enumerate(m.wl):
  56. b = m.idx(word,entry_mode)
  57. assert a == b, f'{a} != {b} ({word!r} - entry mode: {entry_mode!r})'
  58. a = None
  59. for word in junk.split():
  60. b = m.idx(word,entry_mode)
  61. assert a == b, f'{a} != {b} ({word!r} - entry mode: {entry_mode!r})'
  62. if 'idx_minimal' in self.vectors[wl_id]:
  63. for vec in self.vectors[wl_id]['idx_minimal']:
  64. chk = vec[1]
  65. b = m.idx(vec[0],'minimal')
  66. if chk is False:
  67. assert b is None, (b,None)
  68. elif chk is None:
  69. assert type(b) is tuple, (type(b),tuple)
  70. elif type(chk) is int:
  71. assert b == chk, (b,chk)
  72. msg('OK')
  73. return True