ut_mn_entry.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_mn_entry: Mnemonic user entry unit test for the MMGen suite
  4. """
  5. from mmgen.mn_entry import mn_entry
  6. from ..include.common import cfg, vmsg
  7. class unit_tests:
  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 wl(self, name, ut, desc='MnemonicEntry - computed wordlist constants'):
  39. for wl_id in self.vectors:
  40. for j, k in (
  41. ('uniq_ss_len', 'usl'),
  42. ('shortest_word', 'sw'),
  43. ('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. return True
  48. def idx(self, name, ut, desc='MnemonicEntry - idx()'):
  49. junk = 'a g z aa gg zz aaa ggg zzz aaaa gggg zzzz aaaaaaaaaaaaaa gggggggggggggg zzzzzzzzzzzzzz'
  50. for wl_id in self.vectors:
  51. m = mn_entry(cfg, wl_id)
  52. vmsg('Wordlist: '+wl_id)
  53. for entry_mode in ('full', 'short'):
  54. for a, word in enumerate(m.wl):
  55. b = m.idx(word, entry_mode)
  56. assert a == b, f'{a} != {b} ({word!r} - entry mode: {entry_mode!r})'
  57. a = None
  58. for word in junk.split():
  59. b = m.idx(word, entry_mode)
  60. assert a == b, f'{a} != {b} ({word!r} - entry mode: {entry_mode!r})'
  61. if 'idx_minimal' in self.vectors[wl_id]:
  62. for vec in self.vectors[wl_id]['idx_minimal']:
  63. chk = vec[1]
  64. b = m.idx(vec[0], 'minimal')
  65. if chk is False:
  66. assert b is None, (b, None)
  67. elif chk is None:
  68. assert type(b) is tuple, (type(b), tuple)
  69. elif type(chk) is int:
  70. assert b == chk, (b, chk)
  71. return True