import pytest
from content_addressable import (
ContentId,
content_id,
from_canonical_dagcbor,
from_canonical_dagcbor_checked,
)
FOREIGN_CID_BYTES = bytes.fromhex(
"017112200707070707070707070707070707070707070707070707070707070707070707"
)
FOREIGN_CID_STR = "bafyreiaha4dqobyha4dqobyha4dqobyha4dqobyha4dqobyha4dqobyha4"
FOREIGN_LINK_DAGCBOR = bytes.fromhex(
"d82a5825" "00" "017112200707070707070707070707070707070707070707070707070707070707070707"
)
def test_the_foreign_link_fixture_is_a_well_formed_cbor_item():
assert FOREIGN_LINK_DAGCBOR[:2] == b"\xd8\x2a", "must be an IPLD tag-42 link"
declared = FOREIGN_LINK_DAGCBOR[3]
assert len(FOREIGN_LINK_DAGCBOR) == 4 + declared, (
f"byte string declares {declared} bytes but "
f"{len(FOREIGN_LINK_DAGCBOR) - 4} are present"
)
assert FOREIGN_LINK_DAGCBOR[5:] == FOREIGN_CID_BYTES, "must wrap the foreign CID"
def test_from_bytes_rejects_a_foreign_cid():
with pytest.raises(ValueError):
ContentId.from_bytes(FOREIGN_CID_BYTES)
def test_parse_rejects_a_foreign_cid():
with pytest.raises(ValueError):
ContentId.parse(FOREIGN_CID_STR)
@pytest.mark.parametrize(
"decode", [from_canonical_dagcbor, from_canonical_dagcbor_checked]
)
def test_decoding_a_foreign_cid_link_is_rejected_at_both_doors(decode):
with pytest.raises(ValueError, match="not the content-addressable profile"):
decode(FOREIGN_LINK_DAGCBOR)
def test_an_on_profile_id_still_round_trips():
cid = content_id({"name": "alpha"})
assert ContentId.from_bytes(cid.to_bytes()) == cid
assert ContentId.parse(str(cid)) == cid