1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
"""
Tests for CBOR decoding functionality.
"""
import math
from pycddl import Schema
from hypothesis import given, strategies as st, settings
import cbor2
ANY_SCHEMA = Schema("main = any")
PRIMITIVES = (
st.none()
| st.booleans()
| st.floats(allow_nan=False)
| st.text()
| st.integers(max_value=2**63, min_value=-(2**63))
| st.binary()
)
@given(
structure=st.recursive(
PRIMITIVES,
lambda values: st.lists(values) | st.dictionaries(PRIMITIVES, values),
max_leaves=10,
)
)
@settings(max_examples=2_000)
def test_roundtrip_basic_decoding(structure):
"""
Basic serialized structures can be deserialized by pycddl.
"""
serialized = cbor2.dumps(structure)
deserialized = ANY_SCHEMA.validate_cbor(serialized, True)
assert structure == deserialized
def test_decoding_is_optional():
"""
If ``validate_cbor`` isn't told to, it doesn't deserialize the CBOR.
"""
serialized = cbor2.dumps({1: 2})
assert ANY_SCHEMA.validate_cbor(serialized, False) is None
assert ANY_SCHEMA.validate_cbor(serialized) is None
def test_nan():
"""
Floating point nan is decoded correctly.
"""
serialized = cbor2.dumps(math.nan)
result = ANY_SCHEMA.validate_cbor(serialized, True)
assert math.isnan(result)
def test_sets():
"""
Sets can be decoded.
"""
structure = {1, b"abc", "def", 2.3}
serialized = cbor2.dumps(structure)
result = ANY_SCHEMA.validate_cbor(serialized, True)
assert result == structure
@given(data=st.binary(max_size=10_000_000))
def test_large_bytes(data):
"""
Deserialization of large bytes.
"""
serialized = cbor2.dumps(data)
assert ANY_SCHEMA.validate_cbor(serialized, True) == data
|