added support for 0-truncated hashes in stringvalidators, added test for hash validation

master
Kevin Froman 2019-06-28 00:00:38 -05:00
parent 03fbf32af9
commit a27a5e5a45
2 changed files with 22 additions and 1 deletions

View File

@ -23,12 +23,14 @@ from onionrutils import bytesconverter
def validate_hash(data, length=64): def validate_hash(data, length=64):
''' '''
Validate if a string is a valid hash hex digest (does not compare, just checks length and charset) Validate if a string is a valid hash hex digest (does not compare, just checks length and charset)
Length is only invalid if its *more* than the specified
''' '''
retVal = True retVal = True
if data == False or data == True: if data == False or data == True:
return False return False
data = data.strip() data = data.strip()
if len(data) != length: if len(data) > length:
retVal = False retVal = False
else: else:
try: try:

View File

@ -27,6 +27,25 @@ class OnionrValidations(unittest.TestCase):
print('testing', x) print('testing', x)
self.assertFalse(stringvalidators.validate_transport(x)) self.assertFalse(stringvalidators.validate_transport(x))
def test_hash_validator(self):
valid = ['00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c96\n', '00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c96', b'00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c96',
'00003b3813a166e706e490238e9515633cc36', b'00003b3813a166e706e490238e9515633cc3d083']
invalid = [None, 0, 1, True, False, '%#W483242#', '00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c9666', '', b'',
b'00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00c9666666', b' ', '\n', '00003b3813a166e706e490238e9515633cc3d083efe982a67753d50d87a00ccccc\n']
for x in valid:
self.assertTrue(stringvalidators.validate_hash(x))
for x in invalid:
try:
result = stringvalidators.validate_hash(x)
print('testing', x, result)
except AttributeError:
result = False
try:
self.assertFalse(result)
except AssertionError:
raise AssertionError("%s returned true" % (x,))
def test_pubkey_validator(self): def test_pubkey_validator(self):
# Test ed25519 public key validity # Test ed25519 public key validity
valids = ['JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIRRQ====', 'JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIRRQ'] valids = ['JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIRRQ====', 'JZ5VE72GUS3C7BOHDRIYZX4B5U5EJMCMLKHLYCVBQQF3UKHYIRRQ']