Browse Source

bip39 and baseconv unit tests: reformat bad input data

The MMGen Project 5 years ago
parent
commit
ec6f976a8c
3 changed files with 37 additions and 31 deletions
  1. 8 4
      test/unit_tests.py
  2. 16 15
      test/unit_tests_d/ut_baseconv.py
  3. 13 12
      test/unit_tests_d/ut_bip39.py

+ 8 - 4
test/unit_tests.py

@@ -61,20 +61,24 @@ class UnitTestHelpers(object):
 
 	@classmethod
 	def process_bad_data(cls,data):
+		import re
 		desc_w = max(len(e[0]) for e in data)
 		exc_w = max(len(e[1]) for e in data)
+		m_exc = '{!r}: incorrect exception type (expected {!r})'
+		m_err = '{!r}: incorrect error msg (should match {!r}'
+		m_noraise = "\nillegal action 'bad {}' failed to raise exception {!r}"
 		for (desc,exc_chk,emsg_chk,func) in data:
 			try:
-				vmsg_r('  {:{w}}'.format(desc+':',w=desc_w+1))
+				vmsg_r('  bad {:{w}}'.format(desc+':',w=desc_w+1))
 				func()
 			except Exception as e:
 				exc = type(e).__name__
 				emsg = e.args[0]
 				vmsg(' {:{w}} [{}]'.format(exc,emsg,w=exc_w))
-				assert exc == exc_chk,'{!r}: incorrect exception type (expected {!r})'.format(exc,exc_chk)
-				assert emsg_chk in emsg,'{!r}: incorrect error msg (should contain {!r}'.format(emsg,emsg_chk)
+				assert exc == exc_chk, m_exc.format(exc,exc_chk)
+				assert re.search(emsg_chk,emsg), m_err.format(emsg,emsg_chk)
 			else:
-				rdie(3,"\nillegal action '{}' failed to raise exception {!r}".format(desc,exc_chk))
+				rdie(3,m_noraise.format(desc,exc_chk))
 
 try:
 	for test in cmd_args:

+ 16 - 15
test/unit_tests_d/ut_baseconv.py

@@ -148,22 +148,23 @@ class unit_test(object):
 		vmsg('')
 		qmsg('Checking error handling:')
 
-		b = baseconv
+		th = baseconv.tohex
+		fh = baseconv.fromhex
 		bad_data = (
-('bad hexstr',       'HexadecimalStringError','not a hexadecimal str',   lambda:b.fromhex('x','b58')),
-('empty hexstr',     'HexadecimalStringError','empty hex strings not',   lambda:b.fromhex('','b58')),
-('bad b58 data',     'BaseConversionError',   'not in b58',              lambda:b.tohex('IfFzZ','b58')),
-('empty b58 data',   'BaseConversionError',   'empty b58 data',          lambda:b.tohex('','b58')),
-('empty b8 data' ,   'BaseConversionError',   'empty b8 data',           lambda:b.tohex('','b8')),
-('bad b32 data',     'BaseConversionError',   'not in b32',              lambda:b.tohex('1az','b32')),
-('bad pad arg (in)', 'BaseConversionPadError',"illegal value for 'pad'", lambda:b.fromhex('ff','b58',pad='foo')),
-('bad pad arg (in)', 'BaseConversionPadError',"illegal value for 'pad'", lambda:b.fromhex('ff','b58',pad=False)),
-('bad pad arg (in)', 'BaseConversionPadError',"illegal value for 'pad'", lambda:b.fromhex('ff','b58',pad=True)),
-('bad seedlen (in)', 'SeedLengthError',       "invalid seed byte length",lambda:b.fromhex('ff','b58',pad='seed')),
-('bad pad arg (out)','BaseConversionPadError',"illegal value for 'pad'", lambda:b.tohex('Z','b58',pad='foo')),
-('bad pad arg (out)','BaseConversionPadError',"illegal value for 'pad'", lambda:b.tohex('Z','b58',pad=False)),
-('bad pad arg (out)','BaseConversionPadError',"illegal value for 'pad'", lambda:b.tohex('Z','b58',pad=True)),
-('bad seedlen (out)','BaseConversionError',   "invalid length for seed", lambda:b.tohex('Z','b58',pad='seed')),
+('hexstr',          'HexadecimalStringError', ': not a hexadecimal str', lambda:fh('x','b58')),
+('hexstr (empty)',  'HexadecimalStringError', 'empty hex strings not',   lambda:fh('','b58')),
+('b58 data',        'BaseConversionError',    ': not in b58',            lambda:th('IfFzZ','b58')),
+('b58 data (empty)','BaseConversionError',    'empty b58 data',          lambda:th('','b58')),
+('b8 data (empty)' ,'BaseConversionError',    'empty b8 data',           lambda:th('','b8')),
+('b32 data',        'BaseConversionError',    'not in b32',              lambda:th('1az','b32')),
+('pad arg (in)',    'BaseConversionPadError', "illegal value for 'pad'", lambda:fh('ff','b58',pad='foo')),
+('pad arg (in)',    'BaseConversionPadError', "illegal value for 'pad'", lambda:fh('ff','b58',pad=False)),
+('pad arg (in)',    'BaseConversionPadError', "illegal value for 'pad'", lambda:fh('ff','b58',pad=True)),
+('seedlen (in)',    'SeedLengthError',        'invalid seed byte length',lambda:fh('ff','b58',pad='seed')),
+('pad arg (out)',   'BaseConversionPadError', "illegal value for 'pad'", lambda:th('Z','b58',pad='foo')),
+('pad arg (out)',   'BaseConversionPadError', "illegal value for 'pad'", lambda:th('Z','b58',pad=False)),
+('pad arg (out)',   'BaseConversionPadError', "illegal value for 'pad'", lambda:th('Z','b58',pad=True)),
+('seedlen (out)',   'BaseConversionError',    'invalid length for seed', lambda:th('Z','b58',pad='seed')),
 		)
 
 		ut.process_bad_data(bad_data)

+ 13 - 12
test/unit_tests_d/ut_bip39.py

@@ -119,19 +119,20 @@ class unit_test(object):
 		bad_seed = 'deadbeef'
 		good_seed = 'deadbeef' * 4
 
-		b = bip39
+		th = bip39.tohex
+		fh = bip39.fromhex
 		bad_data = (
-('bad hex',             'AssertionError', 'not a hexadecimal', lambda:b.fromhex('xx','bip39')),
-('bad id (tohex)',      'AssertionError', "must be 'bip39'",   lambda:b.fromhex(good_seed,'foo')),
-('bad seed len',        'AssertionError', 'invalid seed bit',  lambda:b.fromhex(bad_seed,'bip39')),
-('bad mnemonic type',   'AssertionError', 'must be list',      lambda:b.tohex('string','bip39')),
-('bad id (fromhex)',    'AssertionError', "must be 'bip39'",   lambda:b.tohex(good_mn,'foo')),
-('tostr = True',        'AssertionError', "'tostr' must be",   lambda:b.fromhex(good_seed,'bip39',tostr=True)),
-('bad padlen (fromhex)','AssertionError', "invalid pad len",   lambda:b.fromhex(good_seed,'bip39',pad=23)),
-('bad padlen (tohex)',  'AssertionError', "invalid pad len",   lambda:b.tohex(good_mn,'bip39',pad=23)),
-('bad word',            'MnemonicError',  "not in the BIP39",  lambda:b.tohex(bad_word_mn,'bip39')),
-('bad checksum',        'MnemonicError',  "checksum",          lambda:b.tohex(bad_chksum_mn,'bip39')),
-('bad seedphrase len',  'MnemonicError',  "phrase len",        lambda:b.tohex(bad_len_mn,'bip39')),
+('hex',              'AssertionError', 'not a hexadecimal',lambda:fh('xx','bip39')),
+('id (tohex)',       'AssertionError', "must be 'bip39'",  lambda:fh(good_seed,'foo')),
+('seed len',         'AssertionError', 'invalid seed bit', lambda:fh(bad_seed,'bip39')),
+('mnemonic type',    'AssertionError', 'must be list',     lambda:th('string','bip39')),
+('id (fromhex)',     'AssertionError', "must be 'bip39'",  lambda:th(good_mn,'foo')),
+('arg (tostr=True)', 'AssertionError', "'tostr' must be",  lambda:fh(good_seed,'bip39',tostr=True)),
+('pad len (fromhex)','AssertionError', "invalid pad len",  lambda:fh(good_seed,'bip39',pad=23)),
+('pad len (tohex)',  'AssertionError', "invalid pad len",  lambda:th(good_mn,'bip39',pad=23)),
+('word',             'MnemonicError',  "not in the BIP39", lambda:th(bad_word_mn,'bip39')),
+('checksum',         'MnemonicError',  "checksum",         lambda:th(bad_chksum_mn,'bip39')),
+('seed phrase len',  'MnemonicError',  "phrase len",       lambda:th(bad_len_mn,'bip39')),
 		)
 
 		ut.process_bad_data(bad_data)