2021-05-21 16:27:01 +00:00
|
|
|
import unittest2
|
2018-12-16 02:55:02 +00:00
|
|
|
import ../libp2p/[cid, multihash, multicodec]
|
|
|
|
|
2019-10-29 18:51:48 +00:00
|
|
|
when defined(nimHasUsed): {.used.}
|
|
|
|
|
2018-12-16 02:55:02 +00:00
|
|
|
suite "Content identifier CID test suite":
|
|
|
|
|
|
|
|
test "CIDv0 test vector":
|
|
|
|
var cid0Text = "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
|
2020-12-15 13:19:18 +00:00
|
|
|
var cid0 = Cid.init(cid0Text).tryGet()
|
2018-12-16 02:55:02 +00:00
|
|
|
check:
|
|
|
|
$cid0 == cid0Text
|
|
|
|
cid0.version() == CIDv0
|
2020-12-15 13:19:18 +00:00
|
|
|
cid0.contentType().tryGet() == multiCodec("dag-pb")
|
|
|
|
cid0.mhash().tryGet().mcodec == multiCodec("sha2-256")
|
|
|
|
Cid.init("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII").isErr()
|
2018-12-16 02:55:02 +00:00
|
|
|
|
|
|
|
test "CIDv1 test vector":
|
|
|
|
var cid1Text = "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG"
|
|
|
|
var chex = "015512209D8453505BDC6F269678E16B3E56" &
|
|
|
|
"C2A2948A41F2C792617CC9611ED363C95B63"
|
2020-12-15 13:19:18 +00:00
|
|
|
var cid1 = Cid.init(cid1Text).tryGet()
|
2018-12-16 02:55:02 +00:00
|
|
|
check:
|
|
|
|
$cid1 == cid1Text
|
|
|
|
cid1.version() == CIDv1
|
2020-12-15 13:19:18 +00:00
|
|
|
cid1.contentType().tryGet() == multiCodec("raw")
|
|
|
|
cid1.mhash().tryGet().mcodec == multiCodec("sha2-256")
|
2018-12-16 02:55:02 +00:00
|
|
|
hex(cid1) == chex
|
2019-10-29 18:51:48 +00:00
|
|
|
|
2018-12-16 02:55:02 +00:00
|
|
|
test "Comparison test":
|
|
|
|
var msg = "Hello World!"
|
|
|
|
var mmsg = "Hello World!Hello World!"
|
|
|
|
var bmsg = cast[seq[byte]](msg)
|
|
|
|
var bmmsg = cast[seq[byte]](mmsg)
|
|
|
|
var cid0 = Cid.init(CIDv0, multiCodec("dag-pb"),
|
2020-12-15 13:19:18 +00:00
|
|
|
MultiHash.digest("sha2-256", bmsg).get()).tryGet()
|
2018-12-16 02:55:02 +00:00
|
|
|
var cid1 = Cid.init(CIDv1, multiCodec("dag-pb"),
|
2020-12-15 13:19:18 +00:00
|
|
|
MultiHash.digest("sha2-256", bmsg).get()).tryGet()
|
2018-12-16 02:55:02 +00:00
|
|
|
var cid2 = cid1
|
|
|
|
var cid3 = cid0
|
|
|
|
var cid4 = Cid.init(CIDv1, multiCodec("dag-cbor"),
|
2020-12-15 13:19:18 +00:00
|
|
|
MultiHash.digest("sha2-256", bmsg).get()).tryGet()
|
2018-12-16 02:55:02 +00:00
|
|
|
var cid5 = Cid.init(CIDv1, multiCodec("dag-pb"),
|
2020-12-15 13:19:18 +00:00
|
|
|
MultiHash.digest("sha2-256", bmmsg).get()).tryGet()
|
2018-12-16 02:55:02 +00:00
|
|
|
var cid6 = Cid.init(CIDv1, multiCodec("dag-pb"),
|
2020-12-15 13:19:18 +00:00
|
|
|
MultiHash.digest("keccak-256", bmsg).get()).tryGet()
|
2018-12-16 02:55:02 +00:00
|
|
|
check:
|
|
|
|
cid0 == cid1
|
|
|
|
cid1 == cid2
|
|
|
|
cid2 == cid3
|
|
|
|
cid3 == cid0
|
|
|
|
cid0 != cid4
|
|
|
|
cid1 != cid5
|
|
|
|
cid2 != cid4
|
|
|
|
cid3 != cid6
|