Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pytests/test_dag_cbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
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()
Expand Down Expand Up @@ -239,7 +239,7 @@ fn encode_dag_cbor_from_pyobject<'py, W: Write>(

Ok(())
} else if obj.is_instance_of::<PyInt>() {
let i: i64 = obj.extract()?;
let i: i128 = obj.extract()?;

if i.is_negative() {
encode::write_u64(w, MajorKind::NegativeInt, -(i + 1) as u64)?
Expand Down