def test_decode_array_of_unsigned_integers(array_size, array_values): size_bytes = zpad32(int_to_big_endian(array_size)) values_bytes = b''.join( (zpad32(int_to_big_endian(v)) for v in array_values)) stream_bytes = size_bytes + values_bytes decoder = DynamicArrayDecoder.as_decoder( item_decoder=UnsignedIntegerDecoder.as_decoder(value_bit_size=256), ) stream = BytesIO(stream_bytes) if len(array_values) < array_size: with pytest.raises(InsufficientDataBytes): decoder(stream) return actual_values = decoder(stream) assert actual_values == array_values[:array_size]
def test_decode_array_of_unsigned_integers(array_size, array_values): size_bytes = zpad32(int_to_big_endian(array_size)) values_bytes = b''.join(( zpad32(int_to_big_endian(v)) for v in array_values )) stream_bytes = size_bytes + values_bytes decoder = DynamicArrayDecoder( item_decoder=UnsignedIntegerDecoder(value_bit_size=256), ) stream = ContextFramesBytesIO(stream_bytes) if len(array_values) < array_size: with pytest.raises(InsufficientDataBytes): decoder(stream) return actual_values = decoder(stream) assert actual_values == array_values[:array_size]
def test_decode_bytes_and_string(string_bytes, pad_size): size_bytes = zpad32(int_to_big_endian(len(string_bytes))) padded_string_bytes = string_bytes + b'\x00' * pad_size stream_bytes = size_bytes + padded_string_bytes stream = BytesIO(stream_bytes) decoder = StringDecoder.as_decoder() if len(padded_string_bytes) < ceil32(len(string_bytes)): with pytest.raises(InsufficientDataBytes): decoder(stream) return decoded_value = decoder(stream) assert decoded_value == string_bytes
def test_decode_strings_raises(_bytes, pad_size): size_bytes = zpad32(int_to_big_endian(len(_bytes))) padded_bytes = _bytes + b'\x00' * pad_size stream_bytes = size_bytes + padded_bytes stream = ContextFramesBytesIO(stream_bytes) decoder = StringDecoder() if len(padded_bytes) < ceil32(len(_bytes)): with pytest.raises(InsufficientDataBytes): decoder(stream) return with pytest.raises(DecodingError): decoder(stream)
def test_decode_strings(_strings, pad_size): size_bytes = zpad32(int_to_big_endian(len(_strings.encode("utf-8")))) padded_bytes = _strings.encode("utf-8") + b'\x00' * pad_size stream_bytes = size_bytes + padded_bytes stream = ContextFramesBytesIO(stream_bytes) decoder = StringDecoder() if len(padded_bytes) < ceil32(len(_strings.encode("utf-8"))): with pytest.raises(InsufficientDataBytes): decoder(stream) return decoded_value = decoder(stream) assert decoded_value == _strings