Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def st_der_octet_string(*args, **kwargs):
"""
Hypothesis strategy that returns a random DER OCTET STRING object.
Parameters are passed to hypothesis.strategy.binary
"""
return st.builds(encode_octet_string, st.binary(*args, **kwargs))
from hypothesis.strategies import (
binary,
builds,
lists,
sampled_from,
text,
)
from tchannel.testing.vcr import proxy
arg_schemes = sampled_from(proxy.ArgScheme.values)
transport_headers = builds(
proxy.TransportHeader,
key=binary(),
value=binary(),
)
requests = builds(
proxy.Request,
serviceName=text(),
hostPort=sampled_from(('localhost', '')),
endpoint=text(min_size=1),
headers=binary(),
body=binary(),
argScheme=arg_schemes,
transportHeaders=lists(transport_headers),
)
responses = builds(
from hypothesis import strategies as st
from hypothesis import given, example
st_bytestring = st.binary() | st.binary().map(bytearray)
@given(st_bytestring, st_bytestring, st_bytestring)
def test_find(u, prefix, suffix):
s = prefix + u + suffix
assert 0 <= s.find(u) <= len(prefix)
assert s.find(u, len(prefix), len(s) - len(suffix)) == len(prefix)
@given(st_bytestring, st_bytestring, st_bytestring)
def test_index(u, prefix, suffix):
s = prefix + u + suffix
assert 0 <= s.index(u) <= len(prefix)
assert s.index(u, len(prefix), len(s) - len(suffix)) == len(prefix)
@given(st_bytestring, st_bytestring, st_bytestring)
def test_rfind(u, prefix, suffix):
s = prefix + u + suffix
@given(lists(tuples(binary(), binary())))
@small_settings
def test_backend_returns_what_you_put_in(xs):
backend = SQLiteExampleDatabase(':memory:')
mapping = {}
for key, value in xs:
mapping.setdefault(key, set()).add(value)
backend.save(key, value)
for key, values in mapping.items():
backend_contents = list(backend.fetch(key))
distinct_backend_contents = set(backend_contents)
assert len(backend_contents) == len(distinct_backend_contents)
assert distinct_backend_contents == set(values)
@given(st.binary(), st.random_module())
def test_partition_by_length(b, _):
shrunk = shrink(b, len)
assert len(shrunk) == len(b) + 1
@given(st.binary(min_size=10))
def test_mostly_assumes_false(xs):
if not seen:
seen.append(xs)
assume(xs in seen)
binary(),
binary(),
)
@example(
CORRECT_LEN_IV,
CORRECT_LEN_HASH,
CORRECT_LEN_HASH,
CORRECT_LEN_KEY,
CORRECT_LEN_HANDSHAKE_PACKET,
)
def test_build_request_packet(iv,
iv_hash,
handshake_packet_hash,
request_key,
handshake_packet):
data = join_encode_data([iv,
iv_hash,
@given(value=st.binary(min_size=0, max_size=256))
def test_round_trip_with_bytestring_start(value):
intermediate_value = encode_hex(value)
round_trip_value = decode_hex(intermediate_value)
assert round_trip_value == value
_bytes=st.binary(min_size=0, max_size=256).filter(complement(is_utf8_decodable)),
pad_size=st.integers(min_value=0, max_value=32),
)
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):
st.binary(min_size=0, max_size=256),
),
)
def test_encode_text_string(string_value):
encoder = TextStringEncoder()
if not is_text(string_value):
with pytest.raises(EncodingTypeError) as exception_info:
encoder(string_value)
assert 'TextStringEncoder' in str(exception_info.value)
return
string_value_as_bytes = codecs.encode(string_value, 'utf8')
expected_value = (
encode_uint_256(len(string_value_as_bytes)) +
(