diff --git a/pytests/test_dag_cbor.py b/pytests/test_dag_cbor.py index 9157d29..d589d76 100644 --- a/pytests/test_dag_cbor.py +++ b/pytests/test_dag_cbor.py @@ -137,6 +137,25 @@ def test_recursion_limit_exceed_on_nested_maps() -> None: assert 'in DAG-CBOR decoding' in str(exc_info.value) +def test_dag_cbor_decode_largest_unsigned_int_roundtrip() -> None: + data = bytes.fromhex('1bffffffffffffffff') + + decoded_result = libipld.decode_dag_cbor(data) + assert decoded_result == 2**64 - 1 + + encoded_result = libipld.encode_dag_cbor(decoded_result) + assert encoded_result == data + + +def test_dag_cbor_decode_smallest_negative_int_roundtrip() -> None: + data = bytes.fromhex('3bffffffffffffffff') + + decoded_result = libipld.decode_dag_cbor(data) + assert decoded_result == -(2**64) + + encoded_result = libipld.encode_dag_cbor(decoded_result) + assert encoded_result == data + def test_dag_cbor_decode_invalid_utf8() -> None: with pytest.raises(ValueError) as exc_info: @@ -160,4 +179,3 @@ def test_dab_cbor_encode_map_int_key() -> None: libipld.encode_dag_cbor(obj) assert 'Map keys must be strings' in str(exc_info.value) - diff --git a/src/lib.rs b/src/lib.rs index 0e2e511..5b4c383 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,7 +130,7 @@ fn decode_dag_cbor_to_pyobject( let major = decode::read_major(r)?; Ok(match major.kind() { MajorKind::UnsignedInt => decode::read_uint(r, major)?.into_pyobject(py)?.into(), - MajorKind::NegativeInt => (-1 - decode::read_uint(r, major)? as i64).into_pyobject(py)?.into(), + MajorKind::NegativeInt => (-1 - decode::read_uint(r, major)? as i128).into_pyobject(py)?.into(), MajorKind::ByteString => { let len = decode::read_uint(r, major)?; PyBytes::new(py, &decode::read_bytes(r, len)?).into_pyobject(py)?.into() @@ -239,7 +239,7 @@ fn encode_dag_cbor_from_pyobject<'py, W: Write>( Ok(()) } else if obj.is_instance_of::() { - let i: i64 = obj.extract()?; + let i: i128 = obj.extract()?; if i.is_negative() { encode::write_u64(w, MajorKind::NegativeInt, -(i + 1) as u64)?