updated tests for Base58Field
import pytest
import string
+import base58
from plenum.common.messages.fields import Base58Field
from plenum.common.util import randomString
from plenum.test.input_validation.utils import *
@@ -11,24 +12,38 @@ LENGTH_SHORT_MIN = 15
LENGTH_SHORT_MAX = 26
valid_base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyzzzzaaaaa'
-validator = Base58Field(short=True, long=True)
+validator = Base58Field(decodedLengthConstraints=(16, 32))
-def test_valid_base58():
- assert not validator.validate(valid_base58[:LENGTH_LONG_MIN])
- assert not validator.validate(valid_base58[:LENGTH_SHORT_MIN])
+def get_chunk(length):
+ chunk = valid_base58[:length]
+ return (chunk, len(base58.b58decode(chunk)))
+def test_valid_base58():
+ for i in range(1, len(valid_base58) + 1):
+ (chunk, decoded_len) = get_chunk(i)
+ assert not Base58Field().validate(chunk) # no constraints
+ assert not Base58Field(
+ decodedLengthConstraints=(decoded_len,)).validate(chunk)
+ assert Base58Field(
+ decodedLengthConstraints=(decoded_len - 1,)).validate(chunk)
def test_empty_string():
- assert validator.validate('')
+ assert not Base58Field().validate('')
+ assert Base58Field(
+ decodedLengthConstraints=(len(randomString()),)).validate('')
-def test_wrong_lengths():
- assert validator.validate(valid_base58[:LENGTH_LONG_MIN - 1])
- assert validator.validate(valid_base58[:LENGTH_LONG_MAX + 1])
- assert validator.validate(valid_base58[:LENGTH_SHORT_MIN - 1])
- assert validator.validate(valid_base58[:LENGTH_SHORT_MAX + 1])
+def test_multiple_constraints():
+ (chunk1, decoded_len1) = get_chunk(10)
+ (chunk2, decoded_len2) = get_chunk(20)
+ validator = Base58Field(decodedLengthConstraints=(
+ decoded_len1, decoded_len2)
+ )
+ assert not validator.validate(chunk1)
+ assert not validator.validate(chunk2)
+ assert validator.validate(get_chunk(30)[0])
def test_invalid_symbol():
- assert validator.validate(valid_base58[:LENGTH_LONG_MIN - 1] + '0')
+ assert Base58Field().validate(valid_base58[:10] + '0')
