make random value generator respect byte list type limit

This commit is contained in:
protolambda 2019-07-27 23:57:57 +02:00
parent 88dbd18394
commit adb6bff365
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
1 changed files with 5 additions and 5 deletions

View File

@ -56,15 +56,15 @@ def get_random_ssz_object(rng: Random,
if mode == RandomizationMode.mode_nil_count: if mode == RandomizationMode.mode_nil_count:
return typ(b'') return typ(b'')
elif mode == RandomizationMode.mode_max_count: elif mode == RandomizationMode.mode_max_count:
return typ(get_random_bytes_list(rng, max_bytes_length)) return typ(get_random_bytes_list(rng, min(max_bytes_length, typ.length)))
elif mode == RandomizationMode.mode_one_count: elif mode == RandomizationMode.mode_one_count:
return typ(get_random_bytes_list(rng, 1)) return typ(get_random_bytes_list(rng, min(1, typ.length)))
elif mode == RandomizationMode.mode_zero: elif mode == RandomizationMode.mode_zero:
return typ(b'\x00') return typ(b'\x00' * min(1, typ.length))
elif mode == RandomizationMode.mode_max: elif mode == RandomizationMode.mode_max:
return typ(b'\xff') return typ(b'\xff' * min(1, typ.length))
else: else:
return typ(get_random_bytes_list(rng, rng.randint(0, max_bytes_length))) return typ(get_random_bytes_list(rng, rng.randint(0, min(max_bytes_length, typ.length))))
elif issubclass(typ, BytesN): elif issubclass(typ, BytesN):
# Sanity, don't generate absurdly big random values # Sanity, don't generate absurdly big random values
# If a client is aiming to performance-test, they should create a benchmark suite. # If a client is aiming to performance-test, they should create a benchmark suite.